Skip to content

Commit 823738f

Browse files
committed
review changes
1 parent b40341a commit 823738f

File tree

2 files changed

+51
-46
lines changed

2 files changed

+51
-46
lines changed

llvm/lib/Transforms/Scalar/LICM.cpp

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,11 +1497,11 @@ static bool sinkUnusedInvariantsFromPreheaderToExit(
14971497

14981498
bool MadeAnyChanges = false;
14991499
MemoryAccess *ExitDef = nullptr;
1500+
MemorySSA *MSSA = MSSAU.getMemorySSA();
15001501

15011502
for (Instruction &I : llvm::make_early_inc_range(llvm::reverse(*Preheader))) {
1502-
15031503
// Skip terminator.
1504-
if (Preheader->getTerminator() == &I)
1504+
if (I.isTerminator())
15051505
continue;
15061506

15071507
// New instructions were inserted at the end of the preheader.
@@ -1520,18 +1520,13 @@ static bool sinkUnusedInvariantsFromPreheaderToExit(
15201520

15211521
// Determine if there is a use in or before the loop (direct or
15221522
// otherwise).
1523-
bool UsedInLoopOrPreheader = false;
1524-
for (Use &U : I.uses()) {
1523+
bool UsedInLoopOrPreheader = llvm::any_of(I.uses(), [Preheader, L](Use &U) {
15251524
auto *UserI = cast<Instruction>(U.getUser());
15261525
BasicBlock *UseBB = UserI->getParent();
1527-
if (auto *PN = dyn_cast<PHINode>(UserI)) {
1526+
if (auto *PN = dyn_cast<PHINode>(UserI))
15281527
UseBB = PN->getIncomingBlock(U);
1529-
}
1530-
if (UseBB == Preheader || L->contains(UseBB)) {
1531-
UsedInLoopOrPreheader = true;
1532-
break;
1533-
}
1534-
}
1528+
return UseBB == Preheader || L->contains(UseBB);
1529+
});
15351530
if (UsedInLoopOrPreheader)
15361531
continue;
15371532

@@ -1543,43 +1538,22 @@ static bool sinkUnusedInvariantsFromPreheaderToExit(
15431538
SE->forgetValue(&I);
15441539

15451540
// Update MemorySSA.
1546-
if (auto *OldMA = MSSAU.getMemorySSA()->getMemoryAccess(&I)) {
1547-
// apviding the expensive getPreviousDefRecursive call by manually
1541+
if (auto *OldMA = MSSA->getMemoryAccess(&I)) {
1542+
// avoiding the expensive getPreviousDefRecursive call by manually
15481543
// setting the defining access.
1549-
if (!ExitDef) {
1550-
if (auto *MPhi = MSSAU.getMemorySSA()->getMemoryAccess(ExitBlock)) {
1551-
ExitDef = MPhi;
1552-
} else {
1553-
BasicBlock *Current = *predecessors(ExitBlock).begin();
1554-
while (true) {
1555-
if (auto *Accesses =
1556-
MSSAU.getMemorySSA()->getBlockAccesses(Current)) {
1557-
if (!Accesses->empty()) {
1558-
MemoryAccess *Back =
1559-
const_cast<MemoryAccess *>(&Accesses->back());
1560-
if (isa<MemoryDef>(Back) || isa<MemoryPhi>(Back))
1561-
ExitDef = Back;
1562-
else
1563-
ExitDef = MSSAU.getMemorySSA()
1564-
->getWalker()
1565-
->getClobberingMemoryAccess(Back);
1566-
break;
1567-
}
1568-
}
1569-
1570-
if (Current == L->getHeader()) {
1571-
Current = Preheader;
1572-
continue;
1573-
}
1574-
1575-
if (pred_empty(Current)) {
1576-
ExitDef = MSSAU.getMemorySSA()->getLiveOnEntryDef();
1577-
break;
1578-
}
1579-
Current = *pred_begin(Current);
1580-
}
1544+
auto FindExitDef = [MSSA, Preheader]() -> MemoryAccess * {
1545+
if (auto *Accesses = MSSA->getBlockAccesses(Preheader)) {
1546+
if (Accesses->empty())
1547+
return nullptr;
1548+
MemoryAccess *Back = const_cast<MemoryAccess *>(&Accesses->back());
1549+
if (!isa<MemoryUse>(Back))
1550+
return Back;
1551+
return MSSA->getWalker()->getClobberingMemoryAccess(Back);
15811552
}
1582-
}
1553+
return nullptr;
1554+
};
1555+
if (!ExitDef)
1556+
ExitDef = FindExitDef();
15831557
MemoryAccess *NewMA = MSSAU.createMemoryAccessInBB(&I, ExitDef, ExitBlock,
15841558
MemorySSA::Beginning);
15851559
OldMA->replaceAllUsesWith(NewMA);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -passes='print<scalar-evolution>,loop-mssa(licm)' -verify-scev < %s -S | FileCheck %s
3+
4+
; This is to verify scalar-evolution update after sinking from preheader
5+
define i32 @main(i1 %cond) {
6+
; CHECK-LABEL: @main(
7+
; CHECK-NEXT: entry:
8+
; CHECK-NEXT: br label [[LOOP0:%.*]]
9+
; CHECK: loop0:
10+
; CHECK-NEXT: [[IV0:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[IV0_NEXT:%.*]], [[LOOP0]] ]
11+
; CHECK-NEXT: [[IV0_NEXT]] = add i32 [[IV0]], 1
12+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[MID:%.*]], label [[LOOP0]]
13+
; CHECK: mid:
14+
; CHECK-NEXT: [[IV0_LCSSA:%.*]] = phi i32 [ [[IV0]], [[LOOP0]] ]
15+
; CHECK-NEXT: [[A:%.*]] = load i32, ptr addrspace(1) null, align 8
16+
; CHECK-NEXT: [[ADD:%.*]] = add i32 [[A]], [[IV0_LCSSA]]
17+
; CHECK-NEXT: ret i32 [[ADD]]
18+
;
19+
entry:
20+
%a = load i32, ptr addrspace(1) null, align 8
21+
br label %loop0
22+
23+
loop0:
24+
%iv0 = phi i32 [ 0, %entry ], [ %iv0.next, %loop0 ]
25+
%iv0.next = add i32 %iv0, 1
26+
br i1 %cond, label %mid, label %loop0
27+
28+
mid:
29+
%add = add i32 %a, %iv0
30+
ret i32 %add
31+
}

0 commit comments

Comments
 (0)