llvm.org GIT mirror llvm / 7cc3dcf
[JumpThreading] Preservation of DT and LVI across the pass Summary: See D37528 for a previous (non-deferred) version of this patch and its description. Preserves dominance in a deferred manner using a new class DeferredDominance. This reduces the performance impact of updating the DominatorTree at every edge insertion and deletion. A user may call DDT->flush() within JumpThreading for an up-to-date DT. This patch currently has one flush() at the end of runImpl() to ensure DT is preserved across the pass. LVI is also preserved to help subsequent passes such as CorrelatedValuePropagation. LVI is simpler to maintain and is done immediately (not deferred). The code to perfom the preversation was minimally altered and was simply marked as preserved for the PassManager to be informed. This extends the analysis available to JumpThreading for future enhancements. One example is loop boundary threading. Reviewers: dberlin, kuhar, sebpop Reviewed By: kuhar, sebpop Subscribers: hiraditya, llvm-commits Differential Revision: https://reviews.llvm.org/D40146 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320612 91177308-0d34-0410-b5e6-96231b3b80d8 Brian M. Rzycki 2 years ago
12 changed file(s) with 896 addition(s) and 102 deletion(s). Raw diff Collapse all Expand all
0 //===- DeferredDominance.h - Deferred Dominators ----------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the DeferredDominance class, which provides deferred
10 // updates to Dominators.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_IR_DEFERREDDOMINANCE_H
15 #define LLVM_IR_DEFERREDDOMINANCE_H
16
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/Dominators.h"
20 #include "llvm/IR/Instructions.h"
21
22 namespace llvm {
23
24 /// \brief Class to defer updates to a DominatorTree.
25 ///
26 /// Definition: Applying updates to every edge insertion and deletion is
27 /// expensive and not necessary. When one needs the DominatorTree for analysis
28 /// they can request a flush() to perform a larger batch update. This has the
29 /// advantage of the DominatorTree inspecting the set of updates to find
30 /// duplicates or unnecessary subtree updates.
31 ///
32 /// The scope of DeferredDominance operates at a Function level.
33 ///
34 /// It is not necessary for the user to scrub the updates for duplicates or
35 /// updates that point to the same block (Delete, BB_A, BB_A). Performance
36 /// can be gained if the caller attempts to batch updates before submitting
37 /// to applyUpdates(ArrayRef) in cases where duplicate edge requests will
38 /// occur.
39 ///
40 /// It is required for the state of the LLVM IR to be applied *before*
41 /// submitting updates. The update routines must analyze the current state
42 /// between a pair of (From, To) basic blocks to determine if the update
43 /// needs to be queued.
44 /// Example (good):
45 /// TerminatorInstructionBB->removeFromParent();
46 /// DDT->deleteEdge(BB, Successor);
47 /// Example (bad):
48 /// DDT->deleteEdge(BB, Successor);
49 /// TerminatorInstructionBB->removeFromParent();
50 class DeferredDominance {
51 public:
52 DeferredDominance(DominatorTree &DT_) : DT(DT_) {}
53
54 /// \brief Queues multiple updates and discards duplicates.
55 void applyUpdates(ArrayRef Updates) {
56 SmallVector Seen;
57 for (auto U : Updates)
58 // Avoid duplicates to applyUpdate() to save on analysis.
59 if (std::none_of(Seen.begin(), Seen.end(),
60 [U](DominatorTree::UpdateType S) { return S == U; })) {
61 Seen.push_back(U);
62 applyUpdate(U.getKind(), U.getFrom(), U.getTo());
63 }
64 }
65
66 void insertEdge(BasicBlock *From, BasicBlock *To) {
67 applyUpdate(DominatorTree::Insert, From, To);
68 }
69
70 void deleteEdge(BasicBlock *From, BasicBlock *To) {
71 applyUpdate(DominatorTree::Delete, From, To);
72 }
73
74 /// \brief Delays the deletion of a basic block until a flush() event.
75 void deleteBB(BasicBlock *DelBB) {
76 assert(DelBB && "Invalid push_back of nullptr DelBB.");
77 assert(pred_empty(DelBB) && "DelBB has one or more predecessors.");
78 // DelBB is unreachable and all its instructions are dead.
79 while (!DelBB->empty()) {
80 Instruction &I = DelBB->back();
81 // Replace used instructions with an arbitrary value (undef).
82 if (!I.use_empty())
83 I.replaceAllUsesWith(llvm::UndefValue::get(I.getType()));
84 DelBB->getInstList().pop_back();
85 }
86 // Make sure DelBB has a valid terminator instruction. As long as DelBB is
87 // a Child of Function F it must contain valid IR.
88 new UnreachableInst(DelBB->getContext(), DelBB);
89 DeletedBBs.insert(DelBB);
90 }
91
92 /// \brief Returns true if DelBB is awaiting deletion at a flush() event.
93 bool pendingDeletedBB(BasicBlock *DelBB) {
94 if (DeletedBBs.empty())
95 return false;
96 return DeletedBBs.count(DelBB) != 0;
97 }
98
99 /// \brief Flushes all pending updates and block deletions. Returns a
100 /// correct DominatorTree reference to be used by the caller for analysis.
101 DominatorTree &flush() {
102 // Updates to DT must happen before blocks are deleted below. Otherwise the
103 // DT traversal will encounter badref blocks and assert.
104 if (!PendUpdates.empty()) {
105 DT.applyUpdates(PendUpdates);
106 PendUpdates.clear();
107 }
108 flushDelBB();
109 return DT;
110 }
111
112 /// \brief Drops all internal state and forces a (slow) recalculation of the
113 /// DominatorTree based on the current state of the LLVM IR in F. This should
114 /// only be used in corner cases such as the Entry block of F being deleted.
115 void recalculate(Function &F) {
116 // flushDelBB must be flushed before the recalculation. The state of the IR
117 // must be consistent before the DT traversal algorithm determines the
118 // actual DT.
119 if (flushDelBB() || !PendUpdates.empty()) {
120 DT.recalculate(F);
121 PendUpdates.clear();
122 }
123 }
124
125 /// \brief Debug method to help view the state of pending updates.
126 LLVM_DUMP_METHOD void dump() const;
127
128 private:
129 DominatorTree &DT;
130 SmallVector PendUpdates;
131 SmallPtrSet DeletedBBs;
132
133 /// Apply an update (Kind, From, To) to the internal queued updates. The
134 /// update is only added when determined to be necessary. Checks for
135 /// self-domination, unnecessary updates, duplicate requests, and balanced
136 /// pairs of requests are all performed. Returns true if the update is
137 /// queued and false if it is discarded.
138 bool applyUpdate(DominatorTree::UpdateKind Kind, BasicBlock *From,
139 BasicBlock *To) {
140 if (From == To)
141 return false; // Cannot dominate self; discard update.
142
143 // Discard updates by inspecting the current state of successors of From.
144 // Since applyUpdate() must be called *after* the Terminator of From is
145 // altered we can determine if the update is unnecessary.
146 bool HasEdge = std::any_of(succ_begin(From), succ_end(From),
147 [To](BasicBlock *B) { return B == To; });
148 if (Kind == DominatorTree::Insert && !HasEdge)
149 return false; // Unnecessary Insert: edge does not exist in IR.
150 if (Kind == DominatorTree::Delete && HasEdge)
151 return false; // Unnecessary Delete: edge still exists in IR.
152
153 // Analyze pending updates to determine if the update is unnecessary.
154 DominatorTree::UpdateType Update = {Kind, From, To};
155 DominatorTree::UpdateType Invert = {Kind != DominatorTree::Insert
156 ? DominatorTree::Insert
157 : DominatorTree::Delete,
158 From, To};
159 for (auto I = PendUpdates.begin(), E = PendUpdates.end(); I != E; ++I) {
160 if (Update == *I)
161 return false; // Discard duplicate updates.
162 if (Invert == *I) {
163 // Update and Invert are both valid (equivalent to a no-op). Remove
164 // Invert from PendUpdates and discard the Update.
165 PendUpdates.erase(I);
166 return false;
167 }
168 }
169 PendUpdates.push_back(Update); // Save the valid update.
170 return true;
171 }
172
173 /// Performs all pending basic block deletions. We have to defer the deletion
174 /// of these blocks until after the DominatorTree updates are applied. The
175 /// internal workings of the DominatorTree code expect every update's From
176 /// and To blocks to exist and to be a member of the same Function.
177 bool flushDelBB() {
178 if (DeletedBBs.empty())
179 return false;
180 for (auto *BB : DeletedBBs)
181 BB->eraseFromParent();
182 DeletedBBs.clear();
183 return true;
184 }
185 };
186
187 } // end namespace llvm
188
189 #endif // LLVM_IR_DEFERREDDOMINANCE_H
3333 class BranchInst;
3434 class CmpInst;
3535 class Constant;
36 class DeferredDominance;
3637 class Function;
3738 class Instruction;
3839 class IntrinsicInst;
7677 TargetLibraryInfo *TLI;
7778 LazyValueInfo *LVI;
7879 AliasAnalysis *AA;
80 DeferredDominance *DDT;
7981 std::unique_ptr BFI;
8082 std::unique_ptr BPI;
8183 bool HasProfileData = false;
106108
107109 // Glue for old PM.
108110 bool runImpl(Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_,
109 AliasAnalysis *AA_, bool HasProfileData_,
110 std::unique_ptr BFI_,
111 AliasAnalysis *AA_, DeferredDominance *DDT_,
112 bool HasProfileData_, std::unique_ptr BFI_,
111113 std::unique_ptr BPI_);
112114
113115 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
2626
2727 class BlockFrequencyInfo;
2828 class BranchProbabilityInfo;
29 class DeferredDominance;
2930 class DominatorTree;
3031 class Function;
3132 class Instruction;
3738 class Value;
3839
3940 /// Delete the specified block, which must have no predecessors.
40 void DeleteDeadBlock(BasicBlock *BB);
41 void DeleteDeadBlock(BasicBlock *BB, DeferredDominance *DDT = nullptr);
4142
4243 /// We know that BB has one predecessor. If there are any single-entry PHI nodes
4344 /// in it, fold them away. This handles the case when all entries to the PHI
2323 #include "llvm/IR/Constant.h"
2424 #include "llvm/IR/Constants.h"
2525 #include "llvm/IR/DataLayout.h"
26 #include "llvm/IR/DeferredDominance.h"
2627 #include "llvm/IR/Dominators.h"
2728 #include "llvm/IR/GetElementPtrTypeIterator.h"
2829 #include "llvm/IR/Operator.h"
108109 /// conditions and indirectbr addresses this might make dead if
109110 /// DeleteDeadConditions is true.
110111 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false,
111 const TargetLibraryInfo *TLI = nullptr);
112 const TargetLibraryInfo *TLI = nullptr,
113 DeferredDominance *DDT = nullptr);
112114
113115 //===----------------------------------------------------------------------===//
114116 // Local dead code elimination.
162164 ///
163165 /// .. and delete the predecessor corresponding to the '1', this will attempt to
164166 /// recursively fold the 'and' to 0.
165 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred);
167 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
168 DeferredDominance *DDT = nullptr);
166169
167170 /// BB is a block with one predecessor and its predecessor is known to have one
168171 /// successor (BB!). Eliminate the edge between them, moving the instructions in
169172 /// the predecessor into BB. This deletes the predecessor block.
170 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr);
173 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr,
174 DeferredDominance *DDT = nullptr);
171175
172176 /// BB is known to contain an unconditional branch, and contains no instructions
173177 /// other than PHI nodes, potential debug intrinsics and the branch. If
174178 /// possible, eliminate BB by rewriting all the predecessors to branch to the
175179 /// successor block and return true. If we can't transform, return false.
176 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
180 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
181 DeferredDominance *DDT = nullptr);
177182
178183 /// Check for and eliminate duplicate PHI nodes in this block. This doesn't try
179184 /// to be clever about PHI nodes which differ only in the order of the incoming
373378 /// Insert an unreachable instruction before the specified
374379 /// instruction, making it and the rest of the code in the block dead.
375380 unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap,
376 bool PreserveLCSSA = false);
381 bool PreserveLCSSA = false,
382 DeferredDominance *DDT = nullptr);
377383
378384 /// Convert the CallInst to InvokeInst with the specified unwind edge basic
379385 /// block. This also splits the basic block where CI is located, because
388394 ///
389395 /// \param BB Block whose terminator will be replaced. Its terminator must
390396 /// have an unwind successor.
391 void removeUnwindEdge(BasicBlock *BB);
397 void removeUnwindEdge(BasicBlock *BB, DeferredDominance *DDT = nullptr);
392398
393399 /// Remove all blocks that can not be reached from the function's entry.
394400 ///
395401 /// Returns true if any basic block was removed.
396 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr);
402 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr,
403 DeferredDominance *DDT = nullptr);
397404
398405 /// Combine the metadata of two instructions so that K can replace J
399406 ///
1717 #include "llvm/ADT/DepthFirstIterator.h"
1818 #include "llvm/ADT/SmallPtrSet.h"
1919 #include "llvm/IR/CFG.h"
20 #include "llvm/IR/DeferredDominance.h"
2021 #include "llvm/IR/Instructions.h"
2122 #include "llvm/IR/PassManager.h"
2223 #include "llvm/Support/CommandLine.h"
388389 DT.print(OS);
389390 }
390391
392 //===----------------------------------------------------------------------===//
393 // DeferredDominance Implementation
394 //===----------------------------------------------------------------------===//
395 //
396 // The implementation details of the DeferredDominance class which allows
397 // one to queue updates to a DominatorTree.
398 //
399 //===----------------------------------------------------------------------===//
400
401 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
402 LLVM_DUMP_METHOD void DeferredDominance::dump() const {
403 raw_ostream &OS = llvm::dbgs();
404 OS << "PendUpdates:\n";
405 int I = 0;
406 for (auto U : PendUpdates) {
407 OS << " " << I << " : ";
408 ++I;
409 if (U.getKind() == DominatorTree::Insert)
410 OS << "Insert, ";
411 else
412 OS << "Delete, ";
413 BasicBlock *From = U.getFrom();
414 if (From) {
415 auto S = From->getName();
416 if (!From->hasName())
417 S = "(no name)";
418 OS << S << "(" << From << "), ";
419 } else {
420 OS << "(badref), ";
421 }
422 BasicBlock *To = U.getTo();
423 if (To) {
424 auto S = To->getName();
425 if (!To->hasName())
426 S = "(no_name)";
427 OS << S << "(" << To << ")\n";
428 } else {
429 OS << "(badref)\n";
430 }
431 }
432 OS << "DeletedBBs:\n";
433 I = 0;
434 for (auto BB : DeletedBBs) {
435 OS << " " << I << " : ";
436 ++I;
437 if (BB->hasName())
438 OS << BB->getName() << "(";
439 else
440 OS << "(no_name)(";
441 OS << BB << ")\n";
442 }
443 }
444 #endif
7676 bool runOnFunction(Function &F) override;
7777
7878 void getAnalysisUsage(AnalysisUsage &AU) const override {
79 AU.addRequired();
7980 AU.addRequired();
8081 AU.addPreserved();
8182 }
8788
8889 INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation",
8990 "Value Propagation", false, false)
91 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
9092 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
9193 INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation",
9294 "Value Propagation", false, false)
3636 #include "llvm/IR/ConstantRange.h"
3737 #include "llvm/IR/Constants.h"
3838 #include "llvm/IR/DataLayout.h"
39 #include "llvm/IR/DeferredDominance.h"
3940 #include "llvm/IR/Dominators.h"
4041 #include "llvm/IR/Function.h"
4142 #include "llvm/IR/InstrTypes.h"
130131 bool runOnFunction(Function &F) override;
131132
132133 void getAnalysisUsage(AnalysisUsage &AU) const override {
133 if (PrintLVIAfterJumpThreading)
134 AU.addRequired();
134 AU.addRequired();
135 AU.addPreserved();
135136 AU.addRequired();
136137 AU.addRequired();
138 AU.addPreserved();
137139 AU.addPreserved();
138140 AU.addRequired();
139141 }
147149
148150 INITIALIZE_PASS_BEGIN(JumpThreading, "jump-threading",
149151 "Jump Threading", false, false)
152 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
150153 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
151154 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
152155 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
277280 if (skipFunction(F))
278281 return false;
279282 auto TLI = &getAnalysis().getTLI();
283 // Get DT analysis before LVI. When LVI is initialized it conditionally adds
284 // DT if it's available.
285 auto DT = &getAnalysis().getDomTree();
280286 auto LVI = &getAnalysis().getLVI();
281287 auto AA = &getAnalysis().getAAResults();
288 DeferredDominance DDT(*DT);
282289 std::unique_ptr BFI;
283290 std::unique_ptr BPI;
284291 bool HasProfileData = F.getEntryCount().hasValue();
288295 BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
289296 }
290297
291 bool Changed = Impl.runImpl(F, TLI, LVI, AA, HasProfileData, std::move(BFI),
292 std::move(BPI));
298 bool Changed = Impl.runImpl(F, TLI, LVI, AA, &DDT, HasProfileData,
299 std::move(BFI), std::move(BPI));
293300 if (PrintLVIAfterJumpThreading) {
294301 dbgs() << "LVI for function '" << F.getName() << "':\n";
295 LVI->printLVI(F, getAnalysis().getDomTree(),
296 dbgs());
302 LVI->printLVI(F, *DT, dbgs());
297303 }
298304 return Changed;
299305 }
301307 PreservedAnalyses JumpThreadingPass::run(Function &F,
302308 FunctionAnalysisManager &AM) {
303309 auto &TLI = AM.getResult(F);
310 // Get DT analysis before LVI. When LVI is initialized it conditionally adds
311 // DT if it's available.
312 auto &DT = AM.getResult(F);
304313 auto &LVI = AM.getResult(F);
305314 auto &AA = AM.getResult(F);
315 DeferredDominance DDT(DT);
306316
307317 std::unique_ptr BFI;
308318 std::unique_ptr BPI;
313323 BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
314324 }
315325
316 bool Changed = runImpl(F, &TLI, &LVI, &AA, HasProfileData, std::move(BFI),
317 std::move(BPI));
326 bool Changed = runImpl(F, &TLI, &LVI, &AA, &DDT, HasProfileData,
327 std::move(BFI), std::move(BPI));
318328
319329 if (!Changed)
320330 return PreservedAnalyses::all();
321331 PreservedAnalyses PA;
322332 PA.preserve();
333 PA.preserve();
334 PA.preserve();
323335 return PA;
324336 }
325337
326338 bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_,
327339 LazyValueInfo *LVI_, AliasAnalysis *AA_,
328 bool HasProfileData_,
340 DeferredDominance *DDT_, bool HasProfileData_,
329341 std::unique_ptr BFI_,
330342 std::unique_ptr BPI_) {
331343 DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n");
332344 TLI = TLI_;
333345 LVI = LVI_;
334346 AA = AA_;
347 DDT = DDT_;
335348 BFI.reset();
336349 BPI.reset();
337350 // When profile data is available, we need to update edge weights after
353366 // back edges. This works for normal cases but not for unreachable blocks as
354367 // they may have cycle with no back edge.
355368 bool EverChanged = false;
356 EverChanged |= removeUnreachableBlocks(F, LVI);
369 EverChanged |= removeUnreachableBlocks(F, LVI, DDT);
357370
358371 FindLoopHeaders(F);
359372
368381
369382 ++I;
370383
384 // Don't thread branches over a block that's slated for deletion.
385 if (DDT->pendingDeletedBB(BB))
386 continue;
387
371388 // If the block is trivially dead, zap it. This eliminates the successor
372389 // edges which simplifies the CFG.
373390 if (pred_empty(BB) &&
376393 << "' with terminator: " << *BB->getTerminator() << '\n');
377394 LoopHeaders.erase(BB);
378395 LVI->eraseBlock(BB);
379 DeleteDeadBlock(BB);
396 DeleteDeadBlock(BB, DDT);
380397 Changed = true;
381398 continue;
382399 }
400417 // awesome, but it allows us to use AssertingVH to prevent nasty
401418 // dangling pointer issues within LazyValueInfo.
402419 LVI->eraseBlock(BB);
403 if (TryToSimplifyUncondBranchFromEmptyBlock(BB))
420 if (TryToSimplifyUncondBranchFromEmptyBlock(BB, DDT))
404421 Changed = true;
405422 }
406423 }
408425 } while (Changed);
409426
410427 LoopHeaders.clear();
428 DDT->flush();
411429 return EverChanged;
412430 }
413431
931949 bool JumpThreadingPass::ProcessBlock(BasicBlock *BB) {
932950 // If the block is trivially dead, just return and let the caller nuke it.
933951 // This simplifies other transformations.
934 if (pred_empty(BB) &&
935 BB != &BB->getParent()->getEntryBlock())
952 if (DDT->pendingDeletedBB(BB) ||
953 (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()))
936954 return false;
937955
938956 // If this block has a single predecessor, and if that pred has a single
948966 LoopHeaders.insert(BB);
949967
950968 LVI->eraseBlock(SinglePred);
951 MergeBasicBlockIntoOnlyPred(BB);
969 MergeBasicBlockIntoOnlyPred(BB, nullptr, DDT);
952970
953971 // Now that BB is merged into SinglePred (i.e. SinglePred Code followed by
954972 // BB code within one basic block `BB`), we need to invalidate the LVI
10311049 // successors to branch to. Let GetBestDestForJumpOnUndef decide.
10321050 if (isa(Condition)) {
10331051 unsigned BestSucc = GetBestDestForJumpOnUndef(BB);
1052 std::vector Updates;
10341053
10351054 // Fold the branch/switch.
10361055 TerminatorInst *BBTerm = BB->getTerminator();
1056 Updates.reserve(BBTerm->getNumSuccessors());
10371057 for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) {
10381058 if (i == BestSucc) continue;
1039 BBTerm->getSuccessor(i)->removePredecessor(BB, true);
1059 BasicBlock *Succ = BBTerm->getSuccessor(i);
1060 Succ->removePredecessor(BB, true);
1061 Updates.push_back({DominatorTree::Delete, BB, Succ});
10401062 }
10411063
10421064 DEBUG(dbgs() << " In block '" << BB->getName()
10431065 << "' folding undef terminator: " << *BBTerm << '\n');
10441066 BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm);
10451067 BBTerm->eraseFromParent();
1068 DDT->applyUpdates(Updates);
10461069 return true;
10471070 }
10481071
10531076 DEBUG(dbgs() << " In block '" << BB->getName()
10541077 << "' folding terminator: " << *BB->getTerminator() << '\n');
10551078 ++NumFolds;
1056 ConstantFoldTerminator(BB, true);
1079 ConstantFoldTerminator(BB, true, nullptr, DDT);
10571080 return true;
10581081 }
10591082
10861109 if (Ret != LazyValueInfo::Unknown) {
10871110 unsigned ToRemove = Ret == LazyValueInfo::True ? 1 : 0;
10881111 unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1;
1089 CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true);
1112 BasicBlock *ToRemoveSucc = CondBr->getSuccessor(ToRemove);
1113 ToRemoveSucc->removePredecessor(BB, true);
10901114 BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr);
10911115 CondBr->eraseFromParent();
10921116 if (CondCmp->use_empty())
11041128 ConstantInt::getFalse(CondCmp->getType());
11051129 ReplaceFoldableUses(CondCmp, CI);
11061130 }
1131 DDT->deleteEdge(BB, ToRemoveSucc);
11071132 return true;
11081133 }
11091134
11821207 Optional Implication =
11831208 isImpliedCondition(PBI->getCondition(), Cond, DL, CondIsTrue);
11841209 if (Implication) {
1185 BI->getSuccessor(*Implication ? 1 : 0)->removePredecessor(BB);
1186 BranchInst::Create(BI->getSuccessor(*Implication ? 0 : 1), BI);
1210 BasicBlock *KeepSucc = BI->getSuccessor(*Implication ? 0 : 1);
1211 BasicBlock *RemoveSucc = BI->getSuccessor(*Implication ? 1 : 0);
1212 RemoveSucc->removePredecessor(BB);
1213 BranchInst::Create(KeepSucc, BI);
11871214 BI->eraseFromParent();
1215 DDT->deleteEdge(BB, RemoveSucc);
11881216 return true;
11891217 }
11901218 CurrentBB = CurrentPred;
15771605 if (PredWithKnownDest ==
15781606 (size_t)std::distance(pred_begin(BB), pred_end(BB))) {
15791607 bool SeenFirstBranchToOnlyDest = false;
1608 std::vector Updates;
1609 Updates.reserve(BB->getTerminator()->getNumSuccessors() - 1);
15801610 for (BasicBlock *SuccBB : successors(BB)) {
1581 if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest)
1611 if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest) {
15821612 SeenFirstBranchToOnlyDest = true; // Don't modify the first branch.
1583 else
1613 } else {
15841614 SuccBB->removePredecessor(BB, true); // This is unreachable successor.
1615 Updates.push_back({DominatorTree::Delete, BB, SuccBB});
1616 }
15851617 }
15861618
15871619 // Finally update the terminator.
15881620 TerminatorInst *Term = BB->getTerminator();
15891621 BranchInst::Create(OnlyDest, Term);
15901622 Term->eraseFromParent();
1623 DDT->applyUpdates(Updates);
15911624
15921625 // If the condition is now dead due to the removal of the old terminator,
15931626 // erase it.
19511984 PredTerm->setSuccessor(i, NewBB);
19521985 }
19531986
1987 DDT->applyUpdates({{DominatorTree::Insert, NewBB, SuccBB},
1988 {DominatorTree::Insert, PredBB, NewBB},
1989 {DominatorTree::Delete, PredBB, BB}});
1990
19541991 // At this point, the IR is fully up to date and consistent. Do a quick scan
19551992 // over the new instructions and zap any that are constants or dead. This
19561993 // frequently happens because of phi translation.
19702007 BasicBlock *JumpThreadingPass::SplitBlockPreds(BasicBlock *BB,
19712008 ArrayRef Preds,
19722009 const char *Suffix) {
2010 SmallVector NewBBs;
2011
19732012 // Collect the frequencies of all predecessors of BB, which will be used to
1974 // update the edge weight on BB->SuccBB.
1975 BlockFrequency PredBBFreq(0);
2013 // update the edge weight of the result of splitting predecessors.
2014 DenseMap FreqMap;
19762015 if (HasProfileData)
19772016 for (auto Pred : Preds)
1978 PredBBFreq += BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB);
1979
1980 BasicBlock *PredBB = SplitBlockPredecessors(BB, Preds, Suffix);
1981
1982 // Set the block frequency of the newly created PredBB, which is the sum of
1983 // frequencies of Preds.
1984 if (HasProfileData)
1985 BFI->setBlockFreq(PredBB, PredBBFreq.getFrequency());
1986 return PredBB;
2017 FreqMap.insert(std::make_pair(
2018 Pred, BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB)));
2019
2020 // In the case when BB is a LandingPad block we create 2 new predecessors
2021 // instead of just one.
2022 if (BB->isLandingPad()) {
2023 std::string NewName = std::string(Suffix) + ".split-lp";
2024 SplitLandingPadPredecessors(BB, Preds, Suffix, NewName.c_str(), NewBBs);
2025 } else {
2026 NewBBs.push_back(SplitBlockPredecessors(BB, Preds, Suffix));
2027 }
2028
2029 std::vector Updates;
2030 Updates.reserve((2 * Preds.size()) + NewBBs.size());
2031 for (auto NewBB : NewBBs) {
2032 BlockFrequency NewBBFreq(0);
2033 Updates.push_back({DominatorTree::Insert, NewBB, BB});
2034 for (auto Pred : predecessors(NewBB)) {
2035 Updates.push_back({DominatorTree::Delete, Pred, BB});
2036 Updates.push_back({DominatorTree::Insert, Pred, NewBB});
2037 if (HasProfileData) // Update frequencies between Pred -> NewBB.
2038 NewBBFreq += FreqMap.lookup(Pred);
2039 }
2040 if (HasProfileData) // Apply the summed frequency to NewBB.
2041 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2042 }
2043
2044 DDT->applyUpdates(Updates);
2045 return NewBBs[0];
19872046 }
19882047
19892048 bool JumpThreadingPass::doesBlockHaveProfileData(BasicBlock *BB) {
21272186 }
21282187
21292188 // And finally, do it! Start by factoring the predecessors if needed.
2189 std::vector Updates;
21302190 BasicBlock *PredBB;
21312191 if (PredBBs.size() == 1)
21322192 PredBB = PredBBs[0];
21352195 << " common predecessors.\n");
21362196 PredBB = SplitBlockPreds(BB, PredBBs, ".thr_comm");
21372197 }
2198 Updates.push_back({DominatorTree::Delete, PredBB, BB});
21382199
21392200 // Okay, we decided to do this! Clone all the instructions in BB onto the end
21402201 // of PredBB.
21472208 BranchInst *OldPredBranch = dyn_cast(PredBB->getTerminator());
21482209
21492210 if (!OldPredBranch || !OldPredBranch->isUnconditional()) {
2150 PredBB = SplitEdge(PredBB, BB);
2211 BasicBlock *OldPredBB = PredBB;
2212 PredBB = SplitEdge(OldPredBB, BB);
2213 Updates.push_back({DominatorTree::Insert, OldPredBB, PredBB});
2214 Updates.push_back({DominatorTree::Insert, PredBB, BB});
2215 Updates.push_back({DominatorTree::Delete, OldPredBB, BB});
21512216 OldPredBranch = cast(PredBB->getTerminator());
21522217 }
21532218
21892254 // Otherwise, insert the new instruction into the block.
21902255 New->setName(BI->getName());
21912256 PredBB->getInstList().insert(OldPredBranch->getIterator(), New);
2257 // Update Dominance from simplified New instruction operands.
2258 for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2259 if (BasicBlock *SuccBB = dyn_cast(New->getOperand(i)))
2260 Updates.push_back({DominatorTree::Insert, PredBB, SuccBB});
21922261 }
21932262 }
21942263
22442313
22452314 // Remove the unconditional branch at the end of the PredBB block.
22462315 OldPredBranch->eraseFromParent();
2316 DDT->applyUpdates(Updates);
22472317
22482318 ++NumDupes;
22492319 return true;
23162386 // The select is now dead.
23172387 SI->eraseFromParent();
23182388
2389 DDT->applyUpdates({{DominatorTree::Insert, NewBB, BB},
2390 {DominatorTree::Insert, Pred, NewBB}});
23192391 // Update any other PHI nodes in BB.
23202392 for (BasicBlock::iterator BI = BB->begin();
23212393 PHINode *Phi = dyn_cast(BI); ++BI)
23942466 // Expand the select.
23952467 TerminatorInst *Term =
23962468 SplitBlockAndInsertIfThen(SI->getCondition(), SI, false);
2469 BasicBlock *SplitBB = SI->getParent();
2470 BasicBlock *NewBB = Term->getParent();
23972471 PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI);
23982472 NewPN->addIncoming(SI->getTrueValue(), Term->getParent());
23992473 NewPN->addIncoming(SI->getFalseValue(), BB);
24002474 SI->replaceAllUsesWith(NewPN);
24012475 SI->eraseFromParent();
2476 // NewBB and SplitBB are newly created blocks which require insertion.
2477 std::vector Updates;
2478 Updates.reserve((2 * SplitBB->getTerminator()->getNumSuccessors()) + 3);
2479 Updates.push_back({DominatorTree::Insert, BB, SplitBB});
2480 Updates.push_back({DominatorTree::Insert, BB, NewBB});
2481 Updates.push_back({DominatorTree::Insert, NewBB, SplitBB});
2482 // BB's successors were moved to SplitBB, update DDT accordingly.
2483 for (auto *Succ : successors(SplitBB)) {
2484 Updates.push_back({DominatorTree::Delete, BB, Succ});
2485 Updates.push_back({DominatorTree::Insert, SplitBB, Succ});
2486 }
2487 DDT->applyUpdates(Updates);
24022488 return true;
24032489 }
24042490 return false;
24852571 if (!TrueDestIsSafe && !FalseDestIsSafe)
24862572 return false;
24872573
2488 BasicBlock *UnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
2489 BasicBlock *GuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
2574 BasicBlock *PredUnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
2575 BasicBlock *PredGuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
24902576
24912577 ValueToValueMapTy UnguardedMapping, GuardedMapping;
24922578 Instruction *AfterGuard = Guard->getNextNode();
24952581 return false;
24962582 // Duplicate all instructions before the guard and the guard itself to the
24972583 // branch where implication is not proved.
2498 GuardedBlock = DuplicateInstructionsInSplitBetween(
2499 BB, GuardedBlock, AfterGuard, GuardedMapping);
2584 BasicBlock *GuardedBlock = DuplicateInstructionsInSplitBetween(
2585 BB, PredGuardedBlock, AfterGuard, GuardedMapping);
25002586 assert(GuardedBlock && "Could not create the guarded block?");
25012587 // Duplicate all instructions before the guard in the unguarded branch.
25022588 // Since we have successfully duplicated the guarded block and this block
25032589 // has fewer instructions, we expect it to succeed.
2504 UnguardedBlock = DuplicateInstructionsInSplitBetween(BB, UnguardedBlock,
2505 Guard, UnguardedMapping);
2590 BasicBlock *UnguardedBlock = DuplicateInstructionsInSplitBetween(
2591 BB, PredUnguardedBlock, Guard, UnguardedMapping);
25062592 assert(UnguardedBlock && "Could not create the unguarded block?");
25072593 DEBUG(dbgs() << "Moved guard " << *Guard << " to block "
25082594 << GuardedBlock->getName() << "\n");
2509
2595 // DuplicateInstructionsInSplitBetween inserts a new block "BB.split" between
2596 // PredBB and BB. We need to perform two inserts and one delete for each of
2597 // the above calls to update Dominators.
2598 DDT->applyUpdates(
2599 {// Guarded block split.
2600 {DominatorTree::Delete, PredGuardedBlock, BB},
2601 {DominatorTree::Insert, PredGuardedBlock, GuardedBlock},
2602 {DominatorTree::Insert, GuardedBlock, BB},
2603 // Unguarded block split.
2604 {DominatorTree::Delete, PredUnguardedBlock, BB},
2605 {DominatorTree::Insert, PredUnguardedBlock, UnguardedBlock},
2606 {DominatorTree::Insert, UnguardedBlock, BB}});
25102607 // Some instructions before the guard may still have uses. For them, we need
25112608 // to create Phi nodes merging their copies in both guarded and unguarded
25122609 // branches. Those instructions that have no uses can be just removed.
4444
4545 using namespace llvm;
4646
47 void llvm::DeleteDeadBlock(BasicBlock *BB) {
47 void llvm::DeleteDeadBlock(BasicBlock *BB, DeferredDominance *DDT) {
4848 assert((pred_begin(BB) == pred_end(BB) ||
4949 // Can delete self loop.
5050 BB->getSinglePredecessor() == BB) && "Block is not dead!");
5151 TerminatorInst *BBTerm = BB->getTerminator();
52 std::vector Updates;
5253
5354 // Loop through all of our successors and make sure they know that one
5455 // of their predecessors is going away.
55 for (BasicBlock *Succ : BBTerm->successors())
56 if (DDT)
57 Updates.reserve(BBTerm->getNumSuccessors());
58 for (BasicBlock *Succ : BBTerm->successors()) {
5659 Succ->removePredecessor(BB);
60 if (DDT)
61 Updates.push_back({DominatorTree::Delete, BB, Succ});
62 }
5763
5864 // Zap all the instructions in the block.
5965 while (!BB->empty()) {
6874 BB->getInstList().pop_back();
6975 }
7076
71 // Zap the block!
72 BB->eraseFromParent();
77 if (DDT) {
78 DDT->applyUpdates(Updates);
79 DDT->deleteBB(BB); // Deferred deletion of BB.
80 } else {
81 BB->eraseFromParent(); // Zap the block!
82 }
7383 }
7484
7585 void llvm::FoldSingleEntryPHINodes(BasicBlock *BB,
9999 /// conditions and indirectbr addresses this might make dead if
100100 /// DeleteDeadConditions is true.
101101 bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
102 const TargetLibraryInfo *TLI) {
102 const TargetLibraryInfo *TLI,
103 DeferredDominance *DDT) {
103104 TerminatorInst *T = BB->getTerminator();
104105 IRBuilder<> Builder(T);
105106
126127 // Replace the conditional branch with an unconditional one.
127128 Builder.CreateBr(Destination);
128129 BI->eraseFromParent();
130 if (DDT)
131 DDT->deleteEdge(BB, OldDest);
129132 return true;
130133 }
131134
196199 createBranchWeights(Weights));
197200 }
198201 // Remove this entry.
199 DefaultDest->removePredecessor(SI->getParent());
202 BasicBlock *ParentBB = SI->getParent();
203 DefaultDest->removePredecessor(ParentBB);
200204 i = SI->removeCase(i);
201205 e = SI->case_end();
206 if (DDT)
207 DDT->deleteEdge(ParentBB, DefaultDest);
202208 continue;
203209 }
204210
224230 // Insert the new branch.
225231 Builder.CreateBr(TheOnlyDest);
226232 BasicBlock *BB = SI->getParent();
233 std::vector Updates;
234 if (DDT)
235 Updates.reserve(SI->getNumSuccessors() - 1);
227236
228237 // Remove entries from PHI nodes which we no longer branch to...
229238 for (BasicBlock *Succ : SI->successors()) {
230239 // Found case matching a constant operand?
231 if (Succ == TheOnlyDest)
240 if (Succ == TheOnlyDest) {
232241 TheOnlyDest = nullptr; // Don't modify the first branch to TheOnlyDest
233 else
242 } else {
234243 Succ->removePredecessor(BB);
244 if (DDT)
245 Updates.push_back({DominatorTree::Delete, BB, Succ});
246 }
235247 }
236248
237249 // Delete the old switch.
239251 SI->eraseFromParent();
240252 if (DeleteDeadConditions)
241253 RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
254 if (DDT)
255 DDT->applyUpdates(Updates);
242256 return true;
243257 }
244258
284298 if (BlockAddress *BA =
285299 dyn_cast(IBI->getAddress()->stripPointerCasts())) {
286300 BasicBlock *TheOnlyDest = BA->getBasicBlock();
301 std::vector Updates;
302 if (DDT)
303 Updates.reserve(IBI->getNumDestinations() - 1);
304
287305 // Insert the new branch.
288306 Builder.CreateBr(TheOnlyDest);
289307
290308 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
291 if (IBI->getDestination(i) == TheOnlyDest)
309 if (IBI->getDestination(i) == TheOnlyDest) {
292310 TheOnlyDest = nullptr;
293 else
294 IBI->getDestination(i)->removePredecessor(IBI->getParent());
311 } else {
312 BasicBlock *ParentBB = IBI->getParent();
313 BasicBlock *DestBB = IBI->getDestination(i);
314 DestBB->removePredecessor(ParentBB);
315 if (DDT)
316 Updates.push_back({DominatorTree::Delete, ParentBB, DestBB});
317 }
295318 }
296319 Value *Address = IBI->getAddress();
297320 IBI->eraseFromParent();
306329 new UnreachableInst(BB->getContext(), BB);
307330 }
308331
332 if (DDT)
333 DDT->applyUpdates(Updates);
309334 return true;
310335 }
311336 }
582607 ///
583608 /// .. and delete the predecessor corresponding to the '1', this will attempt to
584609 /// recursively fold the and to 0.
585 void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred) {
610 void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
611 DeferredDominance *DDT) {
586612 // This only adjusts blocks with PHI nodes.
587613 if (!isa(BB->begin()))
588614 return;
605631 // of the block.
606632 if (PhiIt != OldPhiIt) PhiIt = &BB->front();
607633 }
634 if (DDT)
635 DDT->deleteEdge(Pred, BB);
608636 }
609637
610638 /// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
611639 /// predecessor is known to have one successor (DestBB!). Eliminate the edge
612640 /// between them, moving the instructions in the predecessor into DestBB and
613641 /// deleting the predecessor block.
614 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, DominatorTree *DT) {
642 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, DominatorTree *DT,
643 DeferredDominance *DDT) {
644 assert(!(DT && DDT) && "Cannot call with both DT and DDT.");
645
615646 // If BB has single-entry PHI nodes, fold them.
616647 while (PHINode *PN = dyn_cast(DestBB->begin())) {
617648 Value *NewVal = PN->getIncomingValue(0);
623654
624655 BasicBlock *PredBB = DestBB->getSinglePredecessor();
625656 assert(PredBB && "Block doesn't have a single predecessor!");
657
658 bool ReplaceEntryBB = false;
659 if (PredBB == &DestBB->getParent()->getEntryBlock())
660 ReplaceEntryBB = true;
661
662 // Deferred DT update: Collect all the edges that enter PredBB. These
663 // dominator edges will be redirected to DestBB.
664 std::vector Updates;
665 if (DDT && !ReplaceEntryBB) {
666 Updates.reserve((2 * std::distance(pred_begin(PredBB), pred_end(PredBB))) +
667 1);
668 Updates.push_back({DominatorTree::Delete, PredBB, DestBB});
669 for (auto I = pred_begin(PredBB), E = pred_end(PredBB); I != E; ++I) {
670 Updates.push_back({DominatorTree::Delete, *I, PredBB});
671 Updates.push_back({DominatorTree::Insert, *I, DestBB});
672 }
673 }
626674
627675 // Zap anything that took the address of DestBB. Not doing this will give the
628676 // address an invalid value.
644692
645693 // If the PredBB is the entry block of the function, move DestBB up to
646694 // become the entry block after we erase PredBB.
647 if (PredBB == &DestBB->getParent()->getEntryBlock())
695 if (ReplaceEntryBB)
648696 DestBB->moveAfter(PredBB);
649697
650698 if (DT) {
656704 DT->eraseNode(PredBB);
657705 }
658706 }
659 // Nuke BB.
660 PredBB->eraseFromParent();
707
708 if (DDT) {
709 DDT->deleteBB(PredBB); // Deferred deletion of BB.
710 if (ReplaceEntryBB)
711 // The entry block was removed and there is no external interface for the
712 // dominator tree to be notified of this change. In this corner-case we
713 // recalculate the entire tree.
714 DDT->recalculate(*(DestBB->getParent()));
715 else
716 DDT->applyUpdates(Updates);
717 } else {
718 PredBB->eraseFromParent(); // Nuke BB.
719 }
661720 }
662721
663722 /// CanMergeValues - Return true if we can choose one of these values to use
864923 /// potential side-effect free intrinsics and the branch. If possible,
865924 /// eliminate BB by rewriting all the predecessors to branch to the successor
866925 /// block and return true. If we can't transform, return false.
867 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
926 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
927 DeferredDominance *DDT) {
868928 assert(BB != &BB->getParent()->getEntryBlock() &&
869929 "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
870930
905965
906966 DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
907967
968 std::vector Updates;
969 if (DDT) {
970 Updates.reserve((2 * std::distance(pred_begin(BB), pred_end(BB))) + 1);
971 Updates.push_back({DominatorTree::Delete, BB, Succ});
972 // All predecessors of BB will be moved to Succ.
973 for (auto I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
974 Updates.push_back({DominatorTree::Delete, *I, BB});
975 Updates.push_back({DominatorTree::Insert, *I, Succ});
976 }
977 }
978
908979 if (isa(Succ->begin())) {
909980 // If there is more than one pred of succ, and there are PHI nodes in
910981 // the successor, then we need to add incoming edges for the PHI nodes
9491020 // Everything that jumped to BB now goes to Succ.
9501021 BB->replaceAllUsesWith(Succ);
9511022 if (!Succ->hasName()) Succ->takeName(BB);
952 BB->eraseFromParent(); // Delete the old basic block.
1023
1024 if (DDT) {
1025 DDT->deleteBB(BB); // Deferred deletion of the old basic block.
1026 DDT->applyUpdates(Updates);
1027 } else {
1028 BB->eraseFromParent(); // Delete the old basic block.
1029 }
9531030 return true;
9541031 }
9551032
14511528 }
14521529
14531530 unsigned llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap,
1454 bool PreserveLCSSA) {
1531 bool PreserveLCSSA, DeferredDominance *DDT) {
14551532 BasicBlock *BB = I->getParent();
1533 std::vector Updates;
1534
14561535 // Loop over all of the successors, removing BB's entry from any PHI
14571536 // nodes.
1458 for (BasicBlock *Successor : successors(BB))
1537 if (DDT)
1538 Updates.reserve(BB->getTerminator()->getNumSuccessors());
1539 for (BasicBlock *Successor : successors(BB)) {
14591540 Successor->removePredecessor(BB, PreserveLCSSA);
1460
1541 if (DDT)
1542 Updates.push_back({DominatorTree::Delete, BB, Successor});
1543 }
14611544 // Insert a call to llvm.trap right before this. This turns the undefined
14621545 // behavior into a hard fail instead of falling through into random code.
14631546 if (UseLLVMTrap) {
14771560 BB->getInstList().erase(BBI++);
14781561 ++NumInstrsRemoved;
14791562 }
1563 if (DDT)
1564 DDT->applyUpdates(Updates);
14801565 return NumInstrsRemoved;
14811566 }
14821567
14831568 /// changeToCall - Convert the specified invoke into a normal call.
1484 static void changeToCall(InvokeInst *II) {
1569 static void changeToCall(InvokeInst *II, DeferredDominance *DDT = nullptr) {
14851570 SmallVector Args(II->arg_begin(), II->arg_end());
14861571 SmallVector OpBundles;
14871572 II->getOperandBundlesAsDefs(OpBundles);
14941579 II->replaceAllUsesWith(NewCall);
14951580
14961581 // Follow the call by a branch to the normal destination.
1497 BranchInst::Create(II->getNormalDest(), II);
1582 BasicBlock *NormalDestBB = II->getNormalDest();
1583 BranchInst::Create(NormalDestBB, II);
14981584
14991585 // Update PHI nodes in the unwind destination
1500 II->getUnwindDest()->removePredecessor(II->getParent());
1586 BasicBlock *BB = II->getParent();
1587 BasicBlock *UnwindDestBB = II->getUnwindDest();
1588 UnwindDestBB->removePredecessor(BB);
15011589 II->eraseFromParent();
1590 if (DDT)
1591 DDT->deleteEdge(BB, UnwindDestBB);
15021592 }
15031593
15041594 BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
15391629 }
15401630
15411631 static bool markAliveBlocks(Function &F,
1542 SmallPtrSetImpl &Reachable) {
1632 SmallPtrSetImpl &Reachable,
1633 DeferredDominance *DDT = nullptr) {
15431634 SmallVector Worklist;
15441635 BasicBlock *BB = &F.front();
15451636 Worklist.push_back(BB);
15591650 if (II->getIntrinsicID() == Intrinsic::assume) {
15601651 if (match(II->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) {
15611652 // Don't insert a call to llvm.trap right before the unreachable.
1562 changeToUnreachable(II, false);
1653 changeToUnreachable(II, false, false, DDT);
15631654 Changed = true;
15641655 break;
15651656 }
15761667 // still be useful for widening.
15771668 if (match(II->getArgOperand(0), m_Zero()))
15781669 if (!isa(II->getNextNode())) {
1579 changeToUnreachable(II->getNextNode(), /*UseLLVMTrap=*/ false);
1670 changeToUnreachable(II->getNextNode(), /*UseLLVMTrap=*/false,
1671 false, DDT);
15801672 Changed = true;
15811673 break;
15821674 }
15861678 if (auto *CI = dyn_cast(&I)) {
15871679 Value *Callee = CI->getCalledValue();
15881680 if (isa(Callee) || isa(Callee)) {
1589 changeToUnreachable(CI, /*UseLLVMTrap=*/false);
1681 changeToUnreachable(CI, /*UseLLVMTrap=*/false, false, DDT);
15901682 Changed = true;
15911683 break;
15921684 }
15961688 // though.
15971689 if (!isa(CI->getNextNode())) {
15981690 // Don't insert a call to llvm.trap right before the unreachable.
1599 changeToUnreachable(CI->getNextNode(), false);
1691 changeToUnreachable(CI->getNextNode(), false, false, DDT);
16001692 Changed = true;
16011693 }
16021694 break;
16151707 if (isa(Ptr) ||
16161708 (isa(Ptr) &&
16171709 SI->getPointerAddressSpace() == 0)) {
1618 changeToUnreachable(SI, true);
1710 changeToUnreachable(SI, true, false, DDT);
16191711 Changed = true;
16201712 break;
16211713 }
16271719 // Turn invokes that call 'nounwind' functions into ordinary calls.
16281720 Value *Callee = II->getCalledValue();
16291721 if (isa(Callee) || isa(Callee)) {
1630 changeToUnreachable(II, true);
1722 changeToUnreachable(II, true, false, DDT);
16311723 Changed = true;
16321724 } else if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) {
16331725 if (II->use_empty() && II->onlyReadsMemory()) {
16341726 // jump to the normal destination branch.
1635 BranchInst::Create(II->getNormalDest(), II);
1636 II->getUnwindDest()->removePredecessor(II->getParent());
1727 BasicBlock *NormalDestBB = II->getNormalDest();
1728 BasicBlock *UnwindDestBB = II->getUnwindDest();
1729 BranchInst::Create(NormalDestBB, II);
1730 UnwindDestBB->removePredecessor(II->getParent());
16371731 II->eraseFromParent();
1732 if (DDT)
1733 DDT->deleteEdge(BB, UnwindDestBB);
16381734 } else
1639 changeToCall(II);
1735 changeToCall(II, DDT);
16401736 Changed = true;
16411737 }
16421738 } else if (auto *CatchSwitch = dyn_cast(Terminator)) {
16821778 }
16831779 }
16841780
1685 Changed |= ConstantFoldTerminator(BB, true);
1781 Changed |= ConstantFoldTerminator(BB, true, nullptr, DDT);
16861782 for (BasicBlock *Successor : successors(BB))
16871783 if (Reachable.insert(Successor).second)
16881784 Worklist.push_back(Successor);
16901786 return Changed;
16911787 }
16921788
1693 void llvm::removeUnwindEdge(BasicBlock *BB) {
1789 void llvm::removeUnwindEdge(BasicBlock *BB, DeferredDominance *DDT) {
16941790 TerminatorInst *TI = BB->getTerminator();
16951791
16961792 if (auto *II = dyn_cast(TI)) {
1697 changeToCall(II);
1793 changeToCall(II, DDT);
16981794 return;
16991795 }
17001796
17221818 UnwindDest->removePredecessor(BB);
17231819 TI->replaceAllUsesWith(NewTI);
17241820 TI->eraseFromParent();
1821 if (DDT)
1822 DDT->deleteEdge(BB, UnwindDest);
17251823 }
17261824
17271825 /// removeUnreachableBlocks - Remove blocks that are not reachable, even
17281826 /// if they are in a dead cycle. Return true if a change was made, false
17291827 /// otherwise. If `LVI` is passed, this function preserves LazyValueInfo
17301828 /// after modifying the CFG.
1731 bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI) {
1829 bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI,
1830 DeferredDominance *DDT) {
17321831 SmallPtrSet Reachable;
1733 bool Changed = markAliveBlocks(F, Reachable);
1832 bool Changed = markAliveBlocks(F, Reachable, DDT);
17341833
17351834 // If there are unreachable blocks in the CFG...
17361835 if (Reachable.size() == F.size())
17401839 NumRemoved += F.size()-Reachable.size();
17411840
17421841 // Loop over all of the basic blocks that are not reachable, dropping all of
1743 // their internal references...
1744 for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) {
1745 if (Reachable.count(&*BB))
1842 // their internal references. Update DDT and LVI if available.
1843 std::vector Updates;
1844 for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ++I) {
1845 auto *BB = &*I;
1846 if (Reachable.count(BB))
17461847 continue;
1747
1748 for (BasicBlock *Successor : successors(&*BB))
1848 for (BasicBlock *Successor : successors(BB)) {
17491849 if (Reachable.count(Successor))
1750 Successor->removePredecessor(&*BB);
1850 Successor->removePredecessor(BB);
1851 if (DDT)
1852 Updates.push_back({DominatorTree::Delete, BB, Successor});
1853 }
17511854 if (LVI)
1752 LVI->eraseBlock(&*BB);
1855 LVI->eraseBlock(BB);
17531856 BB->dropAllReferences();
17541857 }
17551858
1756 for (Function::iterator I = ++F.begin(); I != F.end();)
1757 if (!Reachable.count(&*I))
1859 for (Function::iterator I = ++F.begin(); I != F.end();) {
1860 auto *BB = &*I;
1861 if (Reachable.count(BB)) {
1862 ++I;
1863 continue;
1864 }
1865 if (DDT) {
1866 DDT->deleteBB(BB); // deferred deletion of BB.
1867 ++I;
1868 } else {
17581869 I = F.getBasicBlockList().erase(I);
1759 else
1760 ++I;
1761
1870 }
1871 }
1872
1873 if (DDT)
1874 DDT->applyUpdates(Updates);
17621875 return true;
17631876 }
17641877
1818 ; CHECK-NEXT: ; LatticeVal for: 'i32 %a' is: overdefined
1919 ; CHECK-NEXT: ; LatticeVal for: 'i32 %length' is: overdefined
2020 ; CHECK-NEXT: ; LatticeVal for: ' %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]' in BB: '%backedge' is: constantrange<0, 400>
21 ; CHECK-NEXT: ; LatticeVal for: ' %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]' in BB: '%exit' is: constantrange<399, 400>
2122 ; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
2223 ; CHECK-NEXT: ; LatticeVal for: ' %iv.next = add nsw i32 %iv, 1' in BB: '%backedge' is: constantrange<1, 401>
24 ; CHECK-NEXT: ; LatticeVal for: ' %iv.next = add nsw i32 %iv, 1' in BB: '%exit' is: constantrange<400, 401>
2325 ; CHECK-NEXT: %iv.next = add nsw i32 %iv, 1
2426 ; CHECK-NEXT: ; LatticeVal for: ' %cont = icmp slt i32 %iv.next, 400' in BB: '%backedge' is: overdefined
27 ; CHECK-NEXT: ; LatticeVal for: ' %cont = icmp slt i32 %iv.next, 400' in BB: '%exit' is: constantrange<0, -1>
2528 ; CHECK-NEXT: %cont = icmp slt i32 %iv.next, 400
2629 ; CHECK-NOT: loop
2730 loop:
0 ; RUN: opt < %s -jump-threading -disable-output
1
2 %struct.ham = type { i8, i8, i16, i32 }
3 %struct.zot = type { i32 (...)** }
4 %struct.quux.0 = type { %struct.wombat }
5 %struct.wombat = type { %struct.zot }
6
7 @global = external global %struct.ham*, align 8
8 @global.1 = external constant i8*
9
10 declare i32 @wombat.2()
11
12 define void @blam() {
13 bb:
14 %tmp = load i32, i32* undef
15 %tmp1 = icmp eq i32 %tmp, 0
16 br i1 %tmp1, label %bb11, label %bb2
17
18 bb2:
19 %tmp3 = tail call i32 @wombat.2()
20 switch i32 %tmp3, label %bb4 [
21 i32 0, label %bb5
22 i32 1, label %bb7
23 i32 2, label %bb7
24 i32 3, label %bb11
25 ]
26
27 bb4:
28 br label %bb7
29
30 bb5:
31 %tmp6 = tail call i32 @wombat.2()
32 br label %bb7
33
34 bb7:
35 %tmp8 = phi i32 [ 0, %bb5 ], [ 1, %bb4 ], [ 2, %bb2 ], [ 2, %bb2 ]
36 %tmp9 = icmp eq i32 %tmp8, 0
37 br i1 %tmp9, label %bb11, label %bb10
38
39 bb10:
40 ret void
41
42 bb11:
43 ret void
44 }
45
46 define void @spam(%struct.ham* %arg) {
47 bb:
48 %tmp = load i8, i8* undef, align 8
49 switch i8 %tmp, label %bb11 [
50 i8 1, label %bb11
51 i8 2, label %bb11
52 i8 3, label %bb1
53 i8 4, label %bb1
54 ]
55
56 bb1:
57 br label %bb2
58
59 bb2:
60 %tmp3 = phi i32 [ 0, %bb1 ], [ %tmp3, %bb8 ]
61 br label %bb4
62
63 bb4:
64 %tmp5 = load i8, i8* undef, align 8
65 switch i8 %tmp5, label %bb11 [
66 i8 0, label %bb11
67 i8 1, label %bb10
68 i8 2, label %bb10
69 i8 3, label %bb6
70 i8 4, label %bb6
71 ]
72
73 bb6:
74 br label %bb7
75
76 bb7:
77 br i1 undef, label %bb8, label %bb10
78
79 bb8:
80 %tmp9 = icmp eq %struct.ham* undef, %arg
81 br i1 %tmp9, label %bb10, label %bb2
82
83 bb10:
84 switch i32 %tmp3, label %bb4 [
85 i32 0, label %bb14
86 i32 1, label %bb11
87 i32 2, label %bb12
88 ]
89
90 bb11:
91 unreachable
92
93 bb12:
94 %tmp13 = load %struct.ham*, %struct.ham** undef
95 br label %bb14
96
97 bb14:
98 %tmp15 = phi %struct.ham* [ %tmp13, %bb12 ], [ null, %bb10 ]
99 br label %bb16
100
101 bb16:
102 %tmp17 = load i8, i8* undef, align 8
103 switch i8 %tmp17, label %bb11 [
104 i8 0, label %bb11
105 i8 11, label %bb18
106 i8 12, label %bb18
107 ]
108
109 bb18:
110 br label %bb19
111
112 bb19:
113 br label %bb20
114
115 bb20:
116 %tmp21 = load %struct.ham*, %struct.ham** undef
117 switch i8 undef, label %bb22 [
118 i8 0, label %bb4
119 i8 11, label %bb10
120 i8 12, label %bb10
121 ]
122
123 bb22:
124 br label %bb23
125
126 bb23:
127 %tmp24 = icmp eq %struct.ham* %tmp21, null
128 br i1 %tmp24, label %bb35, label %bb25
129
130 bb25:
131 %tmp26 = icmp eq %struct.ham* %tmp15, null
132 br i1 %tmp26, label %bb34, label %bb27
133
134 bb27:
135 %tmp28 = load %struct.ham*, %struct.ham** undef
136 %tmp29 = icmp eq %struct.ham* %tmp28, %tmp21
137 br i1 %tmp29, label %bb35, label %bb30
138
139 bb30:
140 br label %bb31
141
142 bb31:
143 %tmp32 = load i8, i8* undef, align 8
144 %tmp33 = icmp eq i8 %tmp32, 0
145 br i1 %tmp33, label %bb31, label %bb34
146
147 bb34:
148 br label %bb35
149
150 bb35:
151 %tmp36 = phi i1 [ true, %bb34 ], [ false, %bb23 ], [ true, %bb27 ]
152 br label %bb37
153
154 bb37:
155 %tmp38 = icmp eq %struct.ham* %tmp15, null
156 br i1 %tmp38, label %bb39, label %bb41
157
158 bb39:
159 %tmp40 = load %struct.ham*, %struct.ham** @global
160 br label %bb41
161
162 bb41:
163 %tmp42 = select i1 %tmp36, %struct.ham* undef, %struct.ham* undef
164 ret void
165 }
166
167 declare i32 @foo(...)
168
169 define void @zot() align 2 personality i8* bitcast (i32 (...)* @foo to i8*) {
170 bb:
171 invoke void @bar()
172 to label %bb1 unwind label %bb3
173
174 bb1:
175 invoke void @bar()
176 to label %bb2 unwind label %bb4
177
178 bb2:
179 invoke void @bar()
180 to label %bb6 unwind label %bb17
181
182 bb3:
183 %tmp = landingpad { i8*, i32 }
184 catch i8* bitcast (i8** @global.1 to i8*)
185 catch i8* null
186 unreachable
187
188 bb4:
189 %tmp5 = landingpad { i8*, i32 }
190 catch i8* bitcast (i8** @global.1 to i8*)
191 catch i8* null
192 unreachable
193
194 bb6:
195 invoke void @bar()
196 to label %bb7 unwind label %bb19
197
198 bb7:
199 invoke void @bar()
200 to label %bb10 unwind label %bb8
201
202 bb8:
203 %tmp9 = landingpad { i8*, i32 }
204 cleanup
205 catch i8* bitcast (i8** @global.1 to i8*)
206 catch i8* null
207 unreachable
208
209 bb10:
210 %tmp11 = load i32 (%struct.zot*)*, i32 (%struct.zot*)** undef, align 8
211 %tmp12 = invoke i32 %tmp11(%struct.zot* nonnull undef)
212 to label %bb13 unwind label %bb21
213
214 bb13:
215 invoke void @bar()
216 to label %bb14 unwind label %bb23
217
218 bb14:
219 %tmp15 = load i32 (%struct.zot*)*, i32 (%struct.zot*)** undef, align 8
220 %tmp16 = invoke i32 %tmp15(%struct.zot* nonnull undef)
221 to label %bb26 unwind label %bb23
222
223 bb17:
224 %tmp18 = landingpad { i8*, i32 }
225 catch i8* bitcast (i8** @global.1 to i8*)
226 catch i8* null
227 unreachable
228
229 bb19:
230 %tmp20 = landingpad { i8*, i32 }
231 catch i8* bitcast (i8** @global.1 to i8*)
232 catch i8* null
233 unreachable
234
235 bb21:
236 %tmp22 = landingpad { i8*, i32 }
237 catch i8* bitcast (i8** @global.1 to i8*)
238 catch i8* null
239 unreachable
240
241 bb23:
242 %tmp24 = phi %struct.quux.0* [ null, %bb26 ], [ null, %bb14 ], [ undef, %bb13 ]
243 %tmp25 = landingpad { i8*, i32 }
244 catch i8* bitcast (i8** @global.1 to i8*)
245 catch i8* null
246 br label %bb30
247
248 bb26:
249 %tmp27 = load i32 (%struct.zot*)*, i32 (%struct.zot*)** undef, align 8
250 %tmp28 = invoke i32 %tmp27(%struct.zot* nonnull undef)
251 to label %bb29 unwind label %bb23
252
253 bb29:
254 unreachable
255
256 bb30:
257 %tmp31 = icmp eq %struct.quux.0* %tmp24, null
258 br i1 %tmp31, label %bb32, label %bb29
259
260 bb32:
261 unreachable
262 }
263
264 declare void @bar()
0 ; RUN: opt -jump-threading -simplifycfg -S < %s | FileCheck %s
1 ; CHECK-NOT: bb6:
2 ; CHECK-NOT: bb7:
3 ; CHECK-NOT: bb8:
4 ; CHECK-NOT: bb11:
5 ; CHECK-NOT: bb12:
6 ; CHECK: bb:
7 ; CHECK: bb2:
8 ; CHECK: bb4:
9 ; CHECK: bb10:
10 ; CHECK: bb13:
11 declare void @ham()
12
13 define void @hoge() {
14 bb:
15 %tmp = and i32 undef, 1073741823
16 %tmp1 = icmp eq i32 %tmp, 2
17 br i1 %tmp1, label %bb12, label %bb2
18
19 bb2:
20 %tmp3 = icmp eq i32 %tmp, 3
21 br i1 %tmp3, label %bb13, label %bb4
22
23 bb4:
24 %tmp5 = icmp eq i32 %tmp, 5
25 br i1 %tmp5, label %bb6, label %bb7
26
27 bb6:
28 tail call void @ham()
29 br label %bb7
30
31 bb7:
32 br i1 %tmp3, label %bb13, label %bb8
33
34 bb8:
35 %tmp9 = icmp eq i32 %tmp, 4
36 br i1 %tmp9, label %bb13, label %bb10
37
38 bb10:
39 br i1 %tmp9, label %bb11, label %bb13
40
41 bb11:
42 br label %bb13
43
44 bb12:
45 br label %bb2
46
47 bb13:
48 ret void
49 }