llvm.org GIT mirror llvm / 83774c7
[MemorySSA] Make the use of moveAllAfterMergeBlocks consistent. Summary: The rule for the moveAllAfterMergeBlocks API si for all instructions from `From` to have been moved to `To`, while keeping the CFG edges (and block terminators) unchanged. Update all the callsites for moveAllAfterMergeBlocks to follow this. Pending follow-up: since the same behavior is needed everytime, merge all callsites into one. The common denominator may be the call to `MergeBlockIntoPredecessor`. Resolves PR43569. Reviewers: george.burgess.iv Subscribers: Prazek, sanjoy.google, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D68659 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@374177 91177308-0d34-0410-b5e6-96231b3b80d8 Alina Sbirlea 1 year, 11 days ago
5 changed file(s) with 114 addition(s) and 30 deletion(s). Raw diff Collapse all Expand all
11581158 if (!Accs)
11591159 return;
11601160
1161 assert(Start->getParent() == To && "Incorrect Start instruction");
11611162 MemoryAccess *FirstInNew = nullptr;
11621163 for (Instruction &I : make_range(Start->getIterator(), To->end()))
11631164 if ((FirstInNew = MSSA->getMemoryAccess(&I)))
11641165 break;
1165 if (!FirstInNew)
1166 return;
1167
1168 auto *MUD = cast(FirstInNew);
1169 do {
1170 auto NextIt = ++MUD->getIterator();
1171 MemoryUseOrDef *NextMUD = (!Accs || NextIt == Accs->end())
1172 ? nullptr
1173 : cast(&*NextIt);
1174 MSSA->moveTo(MUD, To, MemorySSA::End);
1175 // Moving MUD from Accs in the moveTo above, may delete Accs, so we need to
1176 // retrieve it again.
1177 Accs = MSSA->getWritableBlockAccesses(From);
1178 MUD = NextMUD;
1179 } while (MUD);
1166 if (FirstInNew) {
1167 auto *MUD = cast(FirstInNew);
1168 do {
1169 auto NextIt = ++MUD->getIterator();
1170 MemoryUseOrDef *NextMUD = (!Accs || NextIt == Accs->end())
1171 ? nullptr
1172 : cast(&*NextIt);
1173 MSSA->moveTo(MUD, To, MemorySSA::End);
1174 // Moving MUD from Accs in the moveTo above, may delete Accs, so we need
1175 // to retrieve it again.
1176 Accs = MSSA->getWritableBlockAccesses(From);
1177 MUD = NextMUD;
1178 } while (MUD);
1179 }
1180
1181 // If all accesses were moved and only a trivial Phi remains, we try to remove
1182 // that Phi. This is needed when From is going to be deleted.
1183 auto *Defs = MSSA->getWritableBlockDefs(From);
1184 if (Defs && !Defs->empty())
1185 if (auto *Phi = dyn_cast(&*Defs->begin()))
1186 tryRemoveTrivialPhi(Phi);
11801187 }
11811188
11821189 void MemorySSAUpdater::moveAllAfterSpliceBlocks(BasicBlock *From,
11921199
11931200 void MemorySSAUpdater::moveAllAfterMergeBlocks(BasicBlock *From, BasicBlock *To,
11941201 Instruction *Start) {
1195 assert(From->getSinglePredecessor() == To &&
1202 assert(From->getUniquePredecessor() == To &&
11961203 "From block is expected to have a single predecessor (To).");
11971204 moveAllAccesses(From, To, Start);
11981205 for (BasicBlock *Succ : successors(From))
16281628 ReplaceUsesOfWith(PN, PN->getIncomingValue(0), Worklist, L, LPM,
16291629 MSSAU.get());
16301630
1631 Instruction *STI = Succ->getTerminator();
1632 Instruction *Start = &*Succ->begin();
1633 // If there's nothing to move, mark the starting instruction as the last
1634 // instruction in the block.
1635 if (Start == STI)
1636 Start = BI;
1637
1638 // Move all of the successor contents from Succ to Pred.
1639 Pred->getInstList().splice(BI->getIterator(), Succ->getInstList(),
1640 Succ->begin(), STI->getIterator());
1641 if (MSSAU)
1642 MSSAU->moveAllAfterMergeBlocks(Succ, Pred, Start);
1643
1644 // Move terminator instruction from Succ now, we're deleting BI below.
1645 // FIXME: remove BI first might be more intuitive.
1646 Pred->getInstList().splice(Pred->end(), Succ->getInstList());
1647
16311648 // If Succ has any successors with PHI nodes, update them to have
16321649 // entries coming from Pred instead of Succ.
16331650 Succ->replaceAllUsesWith(Pred);
16341651
1635 // Move all of the successor contents from Succ to Pred.
1636 Pred->getInstList().splice(BI->getIterator(), Succ->getInstList(),
1637 Succ->begin(), Succ->end());
1638 if (MSSAU)
1639 MSSAU->moveAllAfterMergeBlocks(Succ, Pred, BI);
16401652 LPM->deleteSimpleAnalysisValue(BI, L);
16411653 RemoveFromWorklist(BI, Worklist);
16421654 BI->eraseFromParent();
226226 Updates.push_back({DominatorTree::Delete, PredBB, BB});
227227 }
228228
229 Instruction *PTI = PredBB->getTerminator();
230 Instruction *STI = BB->getTerminator();
231 Instruction *Start = &*BB->begin();
232 // If there's nothing to move, mark the starting instruction as the last
233 // instruction in the block.
234 if (Start == STI)
235 Start = PTI;
236
237 // Move all definitions in the successor to the predecessor...
238 PredBB->getInstList().splice(PTI->getIterator(), BB->getInstList(),
239 BB->begin(), STI->getIterator());
240
229241 if (MSSAU)
230 MSSAU->moveAllAfterMergeBlocks(BB, PredBB, &*(BB->begin()));
231
232 // Delete the unconditional branch from the predecessor...
233 PredBB->getInstList().pop_back();
242 MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start);
234243
235244 // Make all PHI nodes that referred to BB now refer to Pred as their
236245 // source...
237246 BB->replaceAllUsesWith(PredBB);
238247
239 // Move all definitions in the successor to the predecessor...
248 // Delete the unconditional branch from the predecessor...
249 PredBB->getInstList().pop_back();
250
251 // Move terminator instruction and add unreachable to now empty BB.
240252 PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
241253 new UnreachableInst(BB->getContext(), BB);
242254
273285 "applying corresponding DTU updates.");
274286 DTU->applyUpdatesPermissive(Updates);
275287 DTU->deleteBB(BB);
276 }
277
278 else {
288 } else {
279289 BB->eraseFromParent(); // Nuke BB if DTU is nullptr.
280290 }
291
281292 return true;
282293 }
283294
614614 LLVM_DEBUG(dbgs() << "Folding loop latch " << Latch->getName() << " into "
615615 << LastExit->getName() << "\n");
616616
617 Instruction *FirstLatchInst = &*Latch->begin();
618 // If there's nothing to move, mark the starting instruction as the last
619 // instruction in the block.
620 if (FirstLatchInst == Jmp)
621 FirstLatchInst = BI;
622
617623 // Hoist the instructions from Latch into LastExit.
618 Instruction *FirstLatchInst = &*(Latch->begin());
619624 LastExit->getInstList().splice(BI->getIterator(), Latch->getInstList(),
620625 Latch->begin(), Jmp->getIterator());
621626
0 ; RUN: opt -pgo-kind=pgo-instr-gen-pipeline -aa-pipeline=default -passes="default" -enable-nontrivial-unswitch -S < %s | FileCheck %s
1 ; REQUIRES: asserts
2
3 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
4 target triple = "x86_64-unknown-linux-gnu"
5
6 @__profn_c = private constant [1 x i8] c"c"
7 @b = common dso_local global i32 0, align 4
8 @a = common dso_local global i16 0, align 2
9
10 ; CHECK-LABEL: @c()
11 ; Function Attrs: nounwind uwtable
12 define dso_local void @c() #0 {
13 entry:
14 call void @llvm.instrprof.increment(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @__profn_c, i32 0, i32 0), i64 68269137, i32 3, i32 0)
15 br label %for.cond
16
17 for.cond: ; preds = %for.end, %entry
18 call void @llvm.instrprof.increment(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @__profn_c, i32 0, i32 0), i64 68269137, i32 3, i32 1)
19 store i32 0, i32* @b, align 4
20 br label %for.cond1
21
22 for.cond1: ; preds = %for.inc, %for.cond
23 %0 = load i32, i32* @b, align 4
24 %1 = load i16, i16* @a, align 2
25 %conv = sext i16 %1 to i32
26 %cmp = icmp slt i32 %0, %conv
27 br i1 %cmp, label %for.body, label %for.end
28
29 for.body: ; preds = %for.cond1
30 call void @llvm.instrprof.increment(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @__profn_c, i32 0, i32 0), i64 68269137, i32 3, i32 2)
31 br label %for.inc
32
33 for.inc: ; preds = %for.body
34 %2 = load i32, i32* @b, align 4
35 %inc = add nsw i32 %2, 1
36 store i32 %inc, i32* @b, align 4
37 br label %for.cond1
38
39 for.end: ; preds = %for.cond1
40 br label %for.cond
41 }
42
43 ; Function Attrs: nounwind
44 declare void @llvm.instrprof.increment(i8*, i64, i32, i32) #1
45
46 attributes #0 = { nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
47 attributes #1 = { nounwind }
48