llvm.org GIT mirror llvm / d76ae12
Reverting [JumpThreading] Preservation of DT and LVI across the pass Stage 2 bootstrap failed: http://lab.llvm.org:8011/builders/clang-x86_64-linux-selfhost-modules-2/builds/14434 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@320641 91177308-0d34-0410-b5e6-96231b3b80d8 Brian M. Rzycki 2 years ago
12 changed file(s) with 102 addition(s) and 896 deletion(s). Raw diff Collapse all Expand all
+0
-190
include/llvm/IR/DeferredDominance.h less more
None //===- 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;
3736 class Function;
3837 class Instruction;
3938 class IntrinsicInst;
7776 TargetLibraryInfo *TLI;
7877 LazyValueInfo *LVI;
7978 AliasAnalysis *AA;
80 DeferredDominance *DDT;
8179 std::unique_ptr BFI;
8280 std::unique_ptr BPI;
8381 bool HasProfileData = false;
108106
109107 // Glue for old PM.
110108 bool runImpl(Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_,
111 AliasAnalysis *AA_, DeferredDominance *DDT_,
112 bool HasProfileData_, std::unique_ptr BFI_,
109 AliasAnalysis *AA_, bool HasProfileData_,
110 std::unique_ptr BFI_,
113111 std::unique_ptr BPI_);
114112
115113 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
2626
2727 class BlockFrequencyInfo;
2828 class BranchProbabilityInfo;
29 class DeferredDominance;
3029 class DominatorTree;
3130 class Function;
3231 class Instruction;
3837 class Value;
3938
4039 /// Delete the specified block, which must have no predecessors.
41 void DeleteDeadBlock(BasicBlock *BB, DeferredDominance *DDT = nullptr);
40 void DeleteDeadBlock(BasicBlock *BB);
4241
4342 /// We know that BB has one predecessor. If there are any single-entry PHI nodes
4443 /// 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"
2726 #include "llvm/IR/Dominators.h"
2827 #include "llvm/IR/GetElementPtrTypeIterator.h"
2928 #include "llvm/IR/Operator.h"
109108 /// conditions and indirectbr addresses this might make dead if
110109 /// DeleteDeadConditions is true.
111110 bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions = false,
112 const TargetLibraryInfo *TLI = nullptr,
113 DeferredDominance *DDT = nullptr);
111 const TargetLibraryInfo *TLI = nullptr);
114112
115113 //===----------------------------------------------------------------------===//
116114 // Local dead code elimination.
164162 ///
165163 /// .. and delete the predecessor corresponding to the '1', this will attempt to
166164 /// recursively fold the 'and' to 0.
167 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
168 DeferredDominance *DDT = nullptr);
165 void RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred);
169166
170167 /// BB is a block with one predecessor and its predecessor is known to have one
171168 /// successor (BB!). Eliminate the edge between them, moving the instructions in
172169 /// the predecessor into BB. This deletes the predecessor block.
173 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr,
174 DeferredDominance *DDT = nullptr);
170 void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT = nullptr);
175171
176172 /// BB is known to contain an unconditional branch, and contains no instructions
177173 /// other than PHI nodes, potential debug intrinsics and the branch. If
178174 /// possible, eliminate BB by rewriting all the predecessors to branch to the
179175 /// successor block and return true. If we can't transform, return false.
180 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
181 DeferredDominance *DDT = nullptr);
176 bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB);
182177
183178 /// Check for and eliminate duplicate PHI nodes in this block. This doesn't try
184179 /// to be clever about PHI nodes which differ only in the order of the incoming
378373 /// Insert an unreachable instruction before the specified
379374 /// instruction, making it and the rest of the code in the block dead.
380375 unsigned changeToUnreachable(Instruction *I, bool UseLLVMTrap,
381 bool PreserveLCSSA = false,
382 DeferredDominance *DDT = nullptr);
376 bool PreserveLCSSA = false);
383377
384378 /// Convert the CallInst to InvokeInst with the specified unwind edge basic
385379 /// block. This also splits the basic block where CI is located, because
394388 ///
395389 /// \param BB Block whose terminator will be replaced. Its terminator must
396390 /// have an unwind successor.
397 void removeUnwindEdge(BasicBlock *BB, DeferredDominance *DDT = nullptr);
391 void removeUnwindEdge(BasicBlock *BB);
398392
399393 /// Remove all blocks that can not be reached from the function's entry.
400394 ///
401395 /// Returns true if any basic block was removed.
402 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr,
403 DeferredDominance *DDT = nullptr);
396 bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI = nullptr);
404397
405398 /// Combine the metadata of two instructions so that K can replace J
406399 ///
1717 #include "llvm/ADT/DepthFirstIterator.h"
1818 #include "llvm/ADT/SmallPtrSet.h"
1919 #include "llvm/IR/CFG.h"
20 #include "llvm/IR/DeferredDominance.h"
2120 #include "llvm/IR/Instructions.h"
2221 #include "llvm/IR/PassManager.h"
2322 #include "llvm/Support/CommandLine.h"
389388 DT.print(OS);
390389 }
391390
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();
8079 AU.addRequired();
8180 AU.addPreserved();
8281 }
8887
8988 INITIALIZE_PASS_BEGIN(CorrelatedValuePropagation, "correlated-propagation",
9089 "Value Propagation", false, false)
91 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
9290 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
9391 INITIALIZE_PASS_END(CorrelatedValuePropagation, "correlated-propagation",
9492 "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"
4039 #include "llvm/IR/Dominators.h"
4140 #include "llvm/IR/Function.h"
4241 #include "llvm/IR/InstrTypes.h"
131130 bool runOnFunction(Function &F) override;
132131
133132 void getAnalysisUsage(AnalysisUsage &AU) const override {
134 AU.addRequired();
135 AU.addPreserved();
133 if (PrintLVIAfterJumpThreading)
134 AU.addRequired();
136135 AU.addRequired();
137136 AU.addRequired();
138 AU.addPreserved();
139137 AU.addPreserved();
140138 AU.addRequired();
141139 }
149147
150148 INITIALIZE_PASS_BEGIN(JumpThreading, "jump-threading",
151149 "Jump Threading", false, false)
152 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
153150 INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
154151 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
155152 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
280277 if (skipFunction(F))
281278 return false;
282279 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();
286280 auto LVI = &getAnalysis().getLVI();
287281 auto AA = &getAnalysis().getAAResults();
288 DeferredDominance DDT(*DT);
289282 std::unique_ptr BFI;
290283 std::unique_ptr BPI;
291284 bool HasProfileData = F.getEntryCount().hasValue();
295288 BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
296289 }
297290
298 bool Changed = Impl.runImpl(F, TLI, LVI, AA, &DDT, HasProfileData,
299 std::move(BFI), std::move(BPI));
291 bool Changed = Impl.runImpl(F, TLI, LVI, AA, HasProfileData, std::move(BFI),
292 std::move(BPI));
300293 if (PrintLVIAfterJumpThreading) {
301294 dbgs() << "LVI for function '" << F.getName() << "':\n";
302 LVI->printLVI(F, *DT, dbgs());
295 LVI->printLVI(F, getAnalysis().getDomTree(),
296 dbgs());
303297 }
304298 return Changed;
305299 }
307301 PreservedAnalyses JumpThreadingPass::run(Function &F,
308302 FunctionAnalysisManager &AM) {
309303 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);
313304 auto &LVI = AM.getResult(F);
314305 auto &AA = AM.getResult(F);
315 DeferredDominance DDT(DT);
316306
317307 std::unique_ptr BFI;
318308 std::unique_ptr BPI;
323313 BFI.reset(new BlockFrequencyInfo(F, *BPI, LI));
324314 }
325315
326 bool Changed = runImpl(F, &TLI, &LVI, &AA, &DDT, HasProfileData,
327 std::move(BFI), std::move(BPI));
316 bool Changed = runImpl(F, &TLI, &LVI, &AA, HasProfileData, std::move(BFI),
317 std::move(BPI));
328318
329319 if (!Changed)
330320 return PreservedAnalyses::all();
331321 PreservedAnalyses PA;
332322 PA.preserve();
333 PA.preserve();
334 PA.preserve();
335323 return PA;
336324 }
337325
338326 bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_,
339327 LazyValueInfo *LVI_, AliasAnalysis *AA_,
340 DeferredDominance *DDT_, bool HasProfileData_,
328 bool HasProfileData_,
341329 std::unique_ptr BFI_,
342330 std::unique_ptr BPI_) {
343331 DEBUG(dbgs() << "Jump threading on function '" << F.getName() << "'\n");
344332 TLI = TLI_;
345333 LVI = LVI_;
346334 AA = AA_;
347 DDT = DDT_;
348335 BFI.reset();
349336 BPI.reset();
350337 // When profile data is available, we need to update edge weights after
366353 // back edges. This works for normal cases but not for unreachable blocks as
367354 // they may have cycle with no back edge.
368355 bool EverChanged = false;
369 EverChanged |= removeUnreachableBlocks(F, LVI, DDT);
356 EverChanged |= removeUnreachableBlocks(F, LVI);
370357
371358 FindLoopHeaders(F);
372359
381368
382369 ++I;
383370
384 // Don't thread branches over a block that's slated for deletion.
385 if (DDT->pendingDeletedBB(BB))
386 continue;
387
388371 // If the block is trivially dead, zap it. This eliminates the successor
389372 // edges which simplifies the CFG.
390373 if (pred_empty(BB) &&
393376 << "' with terminator: " << *BB->getTerminator() << '\n');
394377 LoopHeaders.erase(BB);
395378 LVI->eraseBlock(BB);
396 DeleteDeadBlock(BB, DDT);
379 DeleteDeadBlock(BB);
397380 Changed = true;
398381 continue;
399382 }
417400 // awesome, but it allows us to use AssertingVH to prevent nasty
418401 // dangling pointer issues within LazyValueInfo.
419402 LVI->eraseBlock(BB);
420 if (TryToSimplifyUncondBranchFromEmptyBlock(BB, DDT))
403 if (TryToSimplifyUncondBranchFromEmptyBlock(BB))
421404 Changed = true;
422405 }
423406 }
425408 } while (Changed);
426409
427410 LoopHeaders.clear();
428 DDT->flush();
429411 return EverChanged;
430412 }
431413
949931 bool JumpThreadingPass::ProcessBlock(BasicBlock *BB) {
950932 // If the block is trivially dead, just return and let the caller nuke it.
951933 // This simplifies other transformations.
952 if (DDT->pendingDeletedBB(BB) ||
953 (pred_empty(BB) && BB != &BB->getParent()->getEntryBlock()))
934 if (pred_empty(BB) &&
935 BB != &BB->getParent()->getEntryBlock())
954936 return false;
955937
956938 // If this block has a single predecessor, and if that pred has a single
966948 LoopHeaders.insert(BB);
967949
968950 LVI->eraseBlock(SinglePred);
969 MergeBasicBlockIntoOnlyPred(BB, nullptr, DDT);
951 MergeBasicBlockIntoOnlyPred(BB);
970952
971953 // Now that BB is merged into SinglePred (i.e. SinglePred Code followed by
972954 // BB code within one basic block `BB`), we need to invalidate the LVI
10491031 // successors to branch to. Let GetBestDestForJumpOnUndef decide.
10501032 if (isa(Condition)) {
10511033 unsigned BestSucc = GetBestDestForJumpOnUndef(BB);
1052 std::vector Updates;
10531034
10541035 // Fold the branch/switch.
10551036 TerminatorInst *BBTerm = BB->getTerminator();
1056 Updates.reserve(BBTerm->getNumSuccessors());
10571037 for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) {
10581038 if (i == BestSucc) continue;
1059 BasicBlock *Succ = BBTerm->getSuccessor(i);
1060 Succ->removePredecessor(BB, true);
1061 Updates.push_back({DominatorTree::Delete, BB, Succ});
1039 BBTerm->getSuccessor(i)->removePredecessor(BB, true);
10621040 }
10631041
10641042 DEBUG(dbgs() << " In block '" << BB->getName()
10651043 << "' folding undef terminator: " << *BBTerm << '\n');
10661044 BranchInst::Create(BBTerm->getSuccessor(BestSucc), BBTerm);
10671045 BBTerm->eraseFromParent();
1068 DDT->applyUpdates(Updates);
10691046 return true;
10701047 }
10711048
10761053 DEBUG(dbgs() << " In block '" << BB->getName()
10771054 << "' folding terminator: " << *BB->getTerminator() << '\n');
10781055 ++NumFolds;
1079 ConstantFoldTerminator(BB, true, nullptr, DDT);
1056 ConstantFoldTerminator(BB, true);
10801057 return true;
10811058 }
10821059
11091086 if (Ret != LazyValueInfo::Unknown) {
11101087 unsigned ToRemove = Ret == LazyValueInfo::True ? 1 : 0;
11111088 unsigned ToKeep = Ret == LazyValueInfo::True ? 0 : 1;
1112 BasicBlock *ToRemoveSucc = CondBr->getSuccessor(ToRemove);
1113 ToRemoveSucc->removePredecessor(BB, true);
1089 CondBr->getSuccessor(ToRemove)->removePredecessor(BB, true);
11141090 BranchInst::Create(CondBr->getSuccessor(ToKeep), CondBr);
11151091 CondBr->eraseFromParent();
11161092 if (CondCmp->use_empty())
11281104 ConstantInt::getFalse(CondCmp->getType());
11291105 ReplaceFoldableUses(CondCmp, CI);
11301106 }
1131 DDT->deleteEdge(BB, ToRemoveSucc);
11321107 return true;
11331108 }
11341109
12071182 Optional Implication =
12081183 isImpliedCondition(PBI->getCondition(), Cond, DL, CondIsTrue);
12091184 if (Implication) {
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);
1185 BI->getSuccessor(*Implication ? 1 : 0)->removePredecessor(BB);
1186 BranchInst::Create(BI->getSuccessor(*Implication ? 0 : 1), BI);
12141187 BI->eraseFromParent();
1215 DDT->deleteEdge(BB, RemoveSucc);
12161188 return true;
12171189 }
12181190 CurrentBB = CurrentPred;
16051577 if (PredWithKnownDest ==
16061578 (size_t)std::distance(pred_begin(BB), pred_end(BB))) {
16071579 bool SeenFirstBranchToOnlyDest = false;
1608 std::vector Updates;
1609 Updates.reserve(BB->getTerminator()->getNumSuccessors() - 1);
16101580 for (BasicBlock *SuccBB : successors(BB)) {
1611 if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest) {
1581 if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest)
16121582 SeenFirstBranchToOnlyDest = true; // Don't modify the first branch.
1613 } else {
1583 else
16141584 SuccBB->removePredecessor(BB, true); // This is unreachable successor.
1615 Updates.push_back({DominatorTree::Delete, BB, SuccBB});
1616 }
16171585 }
16181586
16191587 // Finally update the terminator.
16201588 TerminatorInst *Term = BB->getTerminator();
16211589 BranchInst::Create(OnlyDest, Term);
16221590 Term->eraseFromParent();
1623 DDT->applyUpdates(Updates);
16241591
16251592 // If the condition is now dead due to the removal of the old terminator,
16261593 // erase it.
19841951 PredTerm->setSuccessor(i, NewBB);
19851952 }
19861953
1987 DDT->applyUpdates({{DominatorTree::Insert, NewBB, SuccBB},
1988 {DominatorTree::Insert, PredBB, NewBB},
1989 {DominatorTree::Delete, PredBB, BB}});
1990
19911954 // At this point, the IR is fully up to date and consistent. Do a quick scan
19921955 // over the new instructions and zap any that are constants or dead. This
19931956 // frequently happens because of phi translation.
20071970 BasicBlock *JumpThreadingPass::SplitBlockPreds(BasicBlock *BB,
20081971 ArrayRef Preds,
20091972 const char *Suffix) {
2010 SmallVector NewBBs;
2011
20121973 // Collect the frequencies of all predecessors of BB, which will be used to
2013 // update the edge weight of the result of splitting predecessors.
2014 DenseMap FreqMap;
1974 // update the edge weight on BB->SuccBB.
1975 BlockFrequency PredBBFreq(0);
20151976 if (HasProfileData)
20161977 for (auto Pred : Preds)
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];
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;
20461987 }
20471988
20481989 bool JumpThreadingPass::doesBlockHaveProfileData(BasicBlock *BB) {
21862127 }
21872128
21882129 // And finally, do it! Start by factoring the predecessors if needed.
2189 std::vector Updates;
21902130 BasicBlock *PredBB;
21912131 if (PredBBs.size() == 1)
21922132 PredBB = PredBBs[0];
21952135 << " common predecessors.\n");
21962136 PredBB = SplitBlockPreds(BB, PredBBs, ".thr_comm");
21972137 }
2198 Updates.push_back({DominatorTree::Delete, PredBB, BB});
21992138
22002139 // Okay, we decided to do this! Clone all the instructions in BB onto the end
22012140 // of PredBB.
22082147 BranchInst *OldPredBranch = dyn_cast(PredBB->getTerminator());
22092148
22102149 if (!OldPredBranch || !OldPredBranch->isUnconditional()) {
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});
2150 PredBB = SplitEdge(PredBB, BB);
22162151 OldPredBranch = cast(PredBB->getTerminator());
22172152 }
22182153
22542189 // Otherwise, insert the new instruction into the block.
22552190 New->setName(BI->getName());
22562191 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});
22612192 }
22622193 }
22632194
23132244
23142245 // Remove the unconditional branch at the end of the PredBB block.
23152246 OldPredBranch->eraseFromParent();
2316 DDT->applyUpdates(Updates);
23172247
23182248 ++NumDupes;
23192249 return true;
23862316 // The select is now dead.
23872317 SI->eraseFromParent();
23882318
2389 DDT->applyUpdates({{DominatorTree::Insert, NewBB, BB},
2390 {DominatorTree::Insert, Pred, NewBB}});
23912319 // Update any other PHI nodes in BB.
23922320 for (BasicBlock::iterator BI = BB->begin();
23932321 PHINode *Phi = dyn_cast(BI); ++BI)
24662394 // Expand the select.
24672395 TerminatorInst *Term =
24682396 SplitBlockAndInsertIfThen(SI->getCondition(), SI, false);
2469 BasicBlock *SplitBB = SI->getParent();
2470 BasicBlock *NewBB = Term->getParent();
24712397 PHINode *NewPN = PHINode::Create(SI->getType(), 2, "", SI);
24722398 NewPN->addIncoming(SI->getTrueValue(), Term->getParent());
24732399 NewPN->addIncoming(SI->getFalseValue(), BB);
24742400 SI->replaceAllUsesWith(NewPN);
24752401 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);
24882402 return true;
24892403 }
24902404 return false;
25712485 if (!TrueDestIsSafe && !FalseDestIsSafe)
25722486 return false;
25732487
2574 BasicBlock *PredUnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
2575 BasicBlock *PredGuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
2488 BasicBlock *UnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
2489 BasicBlock *GuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
25762490
25772491 ValueToValueMapTy UnguardedMapping, GuardedMapping;
25782492 Instruction *AfterGuard = Guard->getNextNode();
25812495 return false;
25822496 // Duplicate all instructions before the guard and the guard itself to the
25832497 // branch where implication is not proved.
2584 BasicBlock *GuardedBlock = DuplicateInstructionsInSplitBetween(
2585 BB, PredGuardedBlock, AfterGuard, GuardedMapping);
2498 GuardedBlock = DuplicateInstructionsInSplitBetween(
2499 BB, GuardedBlock, AfterGuard, GuardedMapping);
25862500 assert(GuardedBlock && "Could not create the guarded block?");
25872501 // Duplicate all instructions before the guard in the unguarded branch.
25882502 // Since we have successfully duplicated the guarded block and this block
25892503 // has fewer instructions, we expect it to succeed.
2590 BasicBlock *UnguardedBlock = DuplicateInstructionsInSplitBetween(
2591 BB, PredUnguardedBlock, Guard, UnguardedMapping);
2504 UnguardedBlock = DuplicateInstructionsInSplitBetween(BB, UnguardedBlock,
2505 Guard, UnguardedMapping);
25922506 assert(UnguardedBlock && "Could not create the unguarded block?");
25932507 DEBUG(dbgs() << "Moved guard " << *Guard << " to block "
25942508 << GuardedBlock->getName() << "\n");
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}});
2509
26072510 // Some instructions before the guard may still have uses. For them, we need
26082511 // to create Phi nodes merging their copies in both guarded and unguarded
26092512 // branches. Those instructions that have no uses can be just removed.
4444
4545 using namespace llvm;
4646
47 void llvm::DeleteDeadBlock(BasicBlock *BB, DeferredDominance *DDT) {
47 void llvm::DeleteDeadBlock(BasicBlock *BB) {
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;
5352
5453 // Loop through all of our successors and make sure they know that one
5554 // of their predecessors is going away.
56 if (DDT)
57 Updates.reserve(BBTerm->getNumSuccessors());
58 for (BasicBlock *Succ : BBTerm->successors()) {
55 for (BasicBlock *Succ : BBTerm->successors())
5956 Succ->removePredecessor(BB);
60 if (DDT)
61 Updates.push_back({DominatorTree::Delete, BB, Succ});
62 }
6357
6458 // Zap all the instructions in the block.
6559 while (!BB->empty()) {
7468 BB->getInstList().pop_back();
7569 }
7670
77 if (DDT) {
78 DDT->applyUpdates(Updates);
79 DDT->deleteBB(BB); // Deferred deletion of BB.
80 } else {
81 BB->eraseFromParent(); // Zap the block!
82 }
71 // Zap the block!
72 BB->eraseFromParent();
8373 }
8474
8575 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,
103 DeferredDominance *DDT) {
102 const TargetLibraryInfo *TLI) {
104103 TerminatorInst *T = BB->getTerminator();
105104 IRBuilder<> Builder(T);
106105
127126 // Replace the conditional branch with an unconditional one.
128127 Builder.CreateBr(Destination);
129128 BI->eraseFromParent();
130 if (DDT)
131 DDT->deleteEdge(BB, OldDest);
132129 return true;
133130 }
134131
199196 createBranchWeights(Weights));
200197 }
201198 // Remove this entry.
202 BasicBlock *ParentBB = SI->getParent();
203 DefaultDest->removePredecessor(ParentBB);
199 DefaultDest->removePredecessor(SI->getParent());
204200 i = SI->removeCase(i);
205201 e = SI->case_end();
206 if (DDT)
207 DDT->deleteEdge(ParentBB, DefaultDest);
208202 continue;
209203 }
210204
230224 // Insert the new branch.
231225 Builder.CreateBr(TheOnlyDest);
232226 BasicBlock *BB = SI->getParent();
233 std::vector Updates;
234 if (DDT)
235 Updates.reserve(SI->getNumSuccessors() - 1);
236227
237228 // Remove entries from PHI nodes which we no longer branch to...
238229 for (BasicBlock *Succ : SI->successors()) {
239230 // Found case matching a constant operand?
240 if (Succ == TheOnlyDest) {
231 if (Succ == TheOnlyDest)
241232 TheOnlyDest = nullptr; // Don't modify the first branch to TheOnlyDest
242 } else {
233 else
243234 Succ->removePredecessor(BB);
244 if (DDT)
245 Updates.push_back({DominatorTree::Delete, BB, Succ});
246 }
247235 }
248236
249237 // Delete the old switch.
251239 SI->eraseFromParent();
252240 if (DeleteDeadConditions)
253241 RecursivelyDeleteTriviallyDeadInstructions(Cond, TLI);
254 if (DDT)
255 DDT->applyUpdates(Updates);
256242 return true;
257243 }
258244
298284 if (BlockAddress *BA =
299285 dyn_cast(IBI->getAddress()->stripPointerCasts())) {
300286 BasicBlock *TheOnlyDest = BA->getBasicBlock();
301 std::vector Updates;
302 if (DDT)
303 Updates.reserve(IBI->getNumDestinations() - 1);
304
305287 // Insert the new branch.
306288 Builder.CreateBr(TheOnlyDest);
307289
308290 for (unsigned i = 0, e = IBI->getNumDestinations(); i != e; ++i) {
309 if (IBI->getDestination(i) == TheOnlyDest) {
291 if (IBI->getDestination(i) == TheOnlyDest)
310292 TheOnlyDest = nullptr;
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 }
293 else
294 IBI->getDestination(i)->removePredecessor(IBI->getParent());
318295 }
319296 Value *Address = IBI->getAddress();
320297 IBI->eraseFromParent();
329306 new UnreachableInst(BB->getContext(), BB);
330307 }
331308
332 if (DDT)
333 DDT->applyUpdates(Updates);
334309 return true;
335310 }
336311 }
607582 ///
608583 /// .. and delete the predecessor corresponding to the '1', this will attempt to
609584 /// recursively fold the and to 0.
610 void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred,
611 DeferredDominance *DDT) {
585 void llvm::RemovePredecessorAndSimplify(BasicBlock *BB, BasicBlock *Pred) {
612586 // This only adjusts blocks with PHI nodes.
613587 if (!isa(BB->begin()))
614588 return;
631605 // of the block.
632606 if (PhiIt != OldPhiIt) PhiIt = &BB->front();
633607 }
634 if (DDT)
635 DDT->deleteEdge(Pred, BB);
636608 }
637609
638610 /// MergeBasicBlockIntoOnlyPred - DestBB is a block with one predecessor and its
639611 /// predecessor is known to have one successor (DestBB!). Eliminate the edge
640612 /// between them, moving the instructions in the predecessor into DestBB and
641613 /// deleting the predecessor block.
642 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, DominatorTree *DT,
643 DeferredDominance *DDT) {
644 assert(!(DT && DDT) && "Cannot call with both DT and DDT.");
645
614 void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB, DominatorTree *DT) {
646615 // If BB has single-entry PHI nodes, fold them.
647616 while (PHINode *PN = dyn_cast(DestBB->begin())) {
648617 Value *NewVal = PN->getIncomingValue(0);
654623
655624 BasicBlock *PredBB = DestBB->getSinglePredecessor();
656625 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 }
674626
675627 // Zap anything that took the address of DestBB. Not doing this will give the
676628 // address an invalid value.
692644
693645 // If the PredBB is the entry block of the function, move DestBB up to
694646 // become the entry block after we erase PredBB.
695 if (ReplaceEntryBB)
647 if (PredBB == &DestBB->getParent()->getEntryBlock())
696648 DestBB->moveAfter(PredBB);
697649
698650 if (DT) {
704656 DT->eraseNode(PredBB);
705657 }
706658 }
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 }
659 // Nuke BB.
660 PredBB->eraseFromParent();
720661 }
721662
722663 /// CanMergeValues - Return true if we can choose one of these values to use
923864 /// potential side-effect free intrinsics and the branch. If possible,
924865 /// eliminate BB by rewriting all the predecessors to branch to the successor
925866 /// block and return true. If we can't transform, return false.
926 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
927 DeferredDominance *DDT) {
867 bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB) {
928868 assert(BB != &BB->getParent()->getEntryBlock() &&
929869 "TryToSimplifyUncondBranchFromEmptyBlock called on entry block!");
930870
965905
966906 DEBUG(dbgs() << "Killing Trivial BB: \n" << *BB);
967907
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
979908 if (isa(Succ->begin())) {
980909 // If there is more than one pred of succ, and there are PHI nodes in
981910 // the successor, then we need to add incoming edges for the PHI nodes
1020949 // Everything that jumped to BB now goes to Succ.
1021950 BB->replaceAllUsesWith(Succ);
1022951 if (!Succ->hasName()) Succ->takeName(BB);
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 }
952 BB->eraseFromParent(); // Delete the old basic block.
1030953 return true;
1031954 }
1032955
15281451 }
15291452
15301453 unsigned llvm::changeToUnreachable(Instruction *I, bool UseLLVMTrap,
1531 bool PreserveLCSSA, DeferredDominance *DDT) {
1454 bool PreserveLCSSA) {
15321455 BasicBlock *BB = I->getParent();
1533 std::vector Updates;
1534
15351456 // Loop over all of the successors, removing BB's entry from any PHI
15361457 // nodes.
1537 if (DDT)
1538 Updates.reserve(BB->getTerminator()->getNumSuccessors());
1539 for (BasicBlock *Successor : successors(BB)) {
1458 for (BasicBlock *Successor : successors(BB))
15401459 Successor->removePredecessor(BB, PreserveLCSSA);
1541 if (DDT)
1542 Updates.push_back({DominatorTree::Delete, BB, Successor});
1543 }
1460
15441461 // Insert a call to llvm.trap right before this. This turns the undefined
15451462 // behavior into a hard fail instead of falling through into random code.
15461463 if (UseLLVMTrap) {
15601477 BB->getInstList().erase(BBI++);
15611478 ++NumInstrsRemoved;
15621479 }
1563 if (DDT)
1564 DDT->applyUpdates(Updates);
15651480 return NumInstrsRemoved;
15661481 }
15671482
15681483 /// changeToCall - Convert the specified invoke into a normal call.
1569 static void changeToCall(InvokeInst *II, DeferredDominance *DDT = nullptr) {
1484 static void changeToCall(InvokeInst *II) {
15701485 SmallVector Args(II->arg_begin(), II->arg_end());
15711486 SmallVector OpBundles;
15721487 II->getOperandBundlesAsDefs(OpBundles);
15791494 II->replaceAllUsesWith(NewCall);
15801495
15811496 // Follow the call by a branch to the normal destination.
1582 BasicBlock *NormalDestBB = II->getNormalDest();
1583 BranchInst::Create(NormalDestBB, II);
1497 BranchInst::Create(II->getNormalDest(), II);
15841498
15851499 // Update PHI nodes in the unwind destination
1586 BasicBlock *BB = II->getParent();
1587 BasicBlock *UnwindDestBB = II->getUnwindDest();
1588 UnwindDestBB->removePredecessor(BB);
1500 II->getUnwindDest()->removePredecessor(II->getParent());
15891501 II->eraseFromParent();
1590 if (DDT)
1591 DDT->deleteEdge(BB, UnwindDestBB);
15921502 }
15931503
15941504 BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
16291539 }
16301540
16311541 static bool markAliveBlocks(Function &F,
1632 SmallPtrSetImpl &Reachable,
1633 DeferredDominance *DDT = nullptr) {
1542 SmallPtrSetImpl &Reachable) {
16341543 SmallVector Worklist;
16351544 BasicBlock *BB = &F.front();
16361545 Worklist.push_back(BB);
16501559 if (II->getIntrinsicID() == Intrinsic::assume) {
16511560 if (match(II->getArgOperand(0), m_CombineOr(m_Zero(), m_Undef()))) {
16521561 // Don't insert a call to llvm.trap right before the unreachable.
1653 changeToUnreachable(II, false, false, DDT);
1562 changeToUnreachable(II, false);
16541563 Changed = true;
16551564 break;
16561565 }
16671576 // still be useful for widening.
16681577 if (match(II->getArgOperand(0), m_Zero()))
16691578 if (!isa(II->getNextNode())) {
1670 changeToUnreachable(II->getNextNode(), /*UseLLVMTrap=*/false,
1671 false, DDT);
1579 changeToUnreachable(II->getNextNode(), /*UseLLVMTrap=*/ false);
16721580 Changed = true;
16731581 break;
16741582 }
16781586 if (auto *CI = dyn_cast(&I)) {
16791587 Value *Callee = CI->getCalledValue();
16801588 if (isa(Callee) || isa(Callee)) {
1681 changeToUnreachable(CI, /*UseLLVMTrap=*/false, false, DDT);
1589 changeToUnreachable(CI, /*UseLLVMTrap=*/false);
16821590 Changed = true;
16831591 break;
16841592 }
16881596 // though.
16891597 if (!isa(CI->getNextNode())) {
16901598 // Don't insert a call to llvm.trap right before the unreachable.
1691 changeToUnreachable(CI->getNextNode(), false, false, DDT);
1599 changeToUnreachable(CI->getNextNode(), false);
16921600 Changed = true;
16931601 }
16941602 break;
17071615 if (isa(Ptr) ||
17081616 (isa(Ptr) &&
17091617 SI->getPointerAddressSpace() == 0)) {
1710 changeToUnreachable(SI, true, false, DDT);
1618 changeToUnreachable(SI, true);
17111619 Changed = true;
17121620 break;
17131621 }
17191627 // Turn invokes that call 'nounwind' functions into ordinary calls.
17201628 Value *Callee = II->getCalledValue();
17211629 if (isa(Callee) || isa(Callee)) {
1722 changeToUnreachable(II, true, false, DDT);
1630 changeToUnreachable(II, true);
17231631 Changed = true;
17241632 } else if (II->doesNotThrow() && canSimplifyInvokeNoUnwind(&F)) {
17251633 if (II->use_empty() && II->onlyReadsMemory()) {
17261634 // jump to the normal destination branch.
1727 BasicBlock *NormalDestBB = II->getNormalDest();
1728 BasicBlock *UnwindDestBB = II->getUnwindDest();
1729 BranchInst::Create(NormalDestBB, II);
1730 UnwindDestBB->removePredecessor(II->getParent());
1635 BranchInst::Create(II->getNormalDest(), II);
1636 II->getUnwindDest()->removePredecessor(II->getParent());
17311637 II->eraseFromParent();
1732 if (DDT)
1733 DDT->deleteEdge(BB, UnwindDestBB);
17341638 } else
1735 changeToCall(II, DDT);
1639 changeToCall(II);
17361640 Changed = true;
17371641 }
17381642 } else if (auto *CatchSwitch = dyn_cast(Terminator)) {
17781682 }
17791683 }
17801684
1781 Changed |= ConstantFoldTerminator(BB, true, nullptr, DDT);
1685 Changed |= ConstantFoldTerminator(BB, true);
17821686 for (BasicBlock *Successor : successors(BB))
17831687 if (Reachable.insert(Successor).second)
17841688 Worklist.push_back(Successor);
17861690 return Changed;
17871691 }
17881692
1789 void llvm::removeUnwindEdge(BasicBlock *BB, DeferredDominance *DDT) {
1693 void llvm::removeUnwindEdge(BasicBlock *BB) {
17901694 TerminatorInst *TI = BB->getTerminator();
17911695
17921696 if (auto *II = dyn_cast(TI)) {
1793 changeToCall(II, DDT);
1697 changeToCall(II);
17941698 return;
17951699 }
17961700
18181722 UnwindDest->removePredecessor(BB);
18191723 TI->replaceAllUsesWith(NewTI);
18201724 TI->eraseFromParent();
1821 if (DDT)
1822 DDT->deleteEdge(BB, UnwindDest);
18231725 }
18241726
18251727 /// removeUnreachableBlocks - Remove blocks that are not reachable, even
18261728 /// if they are in a dead cycle. Return true if a change was made, false
18271729 /// otherwise. If `LVI` is passed, this function preserves LazyValueInfo
18281730 /// after modifying the CFG.
1829 bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI,
1830 DeferredDominance *DDT) {
1731 bool llvm::removeUnreachableBlocks(Function &F, LazyValueInfo *LVI) {
18311732 SmallPtrSet Reachable;
1832 bool Changed = markAliveBlocks(F, Reachable, DDT);
1733 bool Changed = markAliveBlocks(F, Reachable);
18331734
18341735 // If there are unreachable blocks in the CFG...
18351736 if (Reachable.size() == F.size())
18391740 NumRemoved += F.size()-Reachable.size();
18401741
18411742 // Loop over all of the basic blocks that are not reachable, dropping all of
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))
1743 // their internal references...
1744 for (Function::iterator BB = ++F.begin(), E = F.end(); BB != E; ++BB) {
1745 if (Reachable.count(&*BB))
18471746 continue;
1848 for (BasicBlock *Successor : successors(BB)) {
1747
1748 for (BasicBlock *Successor : successors(&*BB))
18491749 if (Reachable.count(Successor))
1850 Successor->removePredecessor(BB);
1851 if (DDT)
1852 Updates.push_back({DominatorTree::Delete, BB, Successor});
1853 }
1750 Successor->removePredecessor(&*BB);
18541751 if (LVI)
1855 LVI->eraseBlock(BB);
1752 LVI->eraseBlock(&*BB);
18561753 BB->dropAllReferences();
18571754 }
18581755
1859 for (Function::iterator I = ++F.begin(); I != F.end();) {
1860 auto *BB = &*I;
1861 if (Reachable.count(BB)) {
1756 for (Function::iterator I = ++F.begin(); I != F.end();)
1757 if (!Reachable.count(&*I))
1758 I = F.getBasicBlockList().erase(I);
1759 else
18621760 ++I;
1863 continue;
1864 }
1865 if (DDT) {
1866 DDT->deleteBB(BB); // deferred deletion of BB.
1867 ++I;
1868 } else {
1869 I = F.getBasicBlockList().erase(I);
1870 }
1871 }
1872
1873 if (DDT)
1874 DDT->applyUpdates(Updates);
1761
18751762 return true;
18761763 }
18771764
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>
2221 ; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %iv.next, %backedge ]
2322 ; 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>
2523 ; CHECK-NEXT: %iv.next = add nsw i32 %iv, 1
2624 ; 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>
2825 ; CHECK-NEXT: %cont = icmp slt i32 %iv.next, 400
2926 ; CHECK-NOT: loop
3027 loop:
+0
-265
test/Transforms/JumpThreading/ddt-crash.ll less more
None ; 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
-50
test/Transforms/JumpThreading/lvi-tristate.ll less more
None ; 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 }