Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 1 | //===- JumpThreading.cpp - Thread control through conditional blocks ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 10 | // This file implements the Jump Threading pass. |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "jump-threading" |
| 15 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 16 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | 1ff50b3 | 2009-07-03 00:54:20 +0000 | [diff] [blame] | 17 | #include "llvm/LLVMContext.h" |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
Chris Lattner | ef0c674 | 2008-12-01 04:48:07 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/ConstantFolding.h" |
Chris Lattner | 2cc6751 | 2008-04-21 02:57:57 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | ef0c674 | 2008-12-01 04:48:07 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetData.h" |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseMap.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/ADT/STLExtras.h" |
| 26 | #include "llvm/ADT/SmallPtrSet.h" |
| 27 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
| 29 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Debug.h" |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ValueHandle.h" |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 32 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 33 | using namespace llvm; |
| 34 | |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 35 | STATISTIC(NumThreads, "Number of jumps threaded"); |
| 36 | STATISTIC(NumFolds, "Number of terminators folded"); |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 38 | static cl::opt<unsigned> |
| 39 | Threshold("jump-threading-threshold", |
| 40 | cl::desc("Max block size to duplicate for jump threading"), |
| 41 | cl::init(6), cl::Hidden); |
| 42 | |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 43 | namespace { |
Chris Lattner | 94019f8 | 2008-05-09 04:43:13 +0000 | [diff] [blame] | 44 | /// This pass performs 'jump threading', which looks at blocks that have |
| 45 | /// multiple predecessors and multiple successors. If one or more of the |
| 46 | /// predecessors of the block can be proven to always jump to one of the |
| 47 | /// successors, we forward the edge from the predecessor to the successor by |
| 48 | /// duplicating the contents of this block. |
| 49 | /// |
| 50 | /// An example of when this can occur is code like this: |
| 51 | /// |
| 52 | /// if () { ... |
| 53 | /// X = 4; |
| 54 | /// } |
| 55 | /// if (X < 3) { |
| 56 | /// |
| 57 | /// In this case, the unconditional branch at the end of the first if can be |
| 58 | /// revectored to the false side of the second if. |
| 59 | /// |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 60 | class VISIBILITY_HIDDEN JumpThreading : public FunctionPass { |
Chris Lattner | ef0c674 | 2008-12-01 04:48:07 +0000 | [diff] [blame] | 61 | TargetData *TD; |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 62 | #ifdef NDEBUG |
| 63 | SmallPtrSet<BasicBlock*, 16> LoopHeaders; |
| 64 | #else |
| 65 | SmallSet<AssertingVH<BasicBlock>, 16> LoopHeaders; |
| 66 | #endif |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 67 | public: |
| 68 | static char ID; // Pass identification |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 69 | JumpThreading() : FunctionPass(&ID) {} |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 70 | |
Chris Lattner | ef0c674 | 2008-12-01 04:48:07 +0000 | [diff] [blame] | 71 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | ef0c674 | 2008-12-01 04:48:07 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 74 | bool runOnFunction(Function &F); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 75 | void FindLoopHeaders(Function &F); |
| 76 | |
Chris Lattner | c7bcbf6 | 2008-11-27 07:20:04 +0000 | [diff] [blame] | 77 | bool ProcessBlock(BasicBlock *BB); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 78 | bool ThreadEdge(BasicBlock *BB, BasicBlock *PredBB, BasicBlock *SuccBB, |
| 79 | unsigned JumpThreadCost); |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 80 | BasicBlock *FactorCommonPHIPreds(PHINode *PN, Value *Val); |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 81 | bool ProcessBranchOnDuplicateCond(BasicBlock *PredBB, BasicBlock *DestBB); |
Chris Lattner | 3cda3cd | 2008-12-04 06:31:07 +0000 | [diff] [blame] | 82 | bool ProcessSwitchOnDuplicateCond(BasicBlock *PredBB, BasicBlock *DestBB); |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 83 | |
Chris Lattner | d38c14e | 2008-04-22 06:36:15 +0000 | [diff] [blame] | 84 | bool ProcessJumpOnPHI(PHINode *PN); |
Chris Lattner | ae65b3c | 2008-04-22 20:46:09 +0000 | [diff] [blame] | 85 | bool ProcessBranchOnLogical(Value *V, BasicBlock *BB, bool isAnd); |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 86 | bool ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB); |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 87 | |
| 88 | bool SimplifyPartiallyRedundantLoad(LoadInst *LI); |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 89 | }; |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 92 | char JumpThreading::ID = 0; |
| 93 | static RegisterPass<JumpThreading> |
| 94 | X("jump-threading", "Jump Threading"); |
| 95 | |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 96 | // Public interface to the Jump Threading pass |
| 97 | FunctionPass *llvm::createJumpThreadingPass() { return new JumpThreading(); } |
| 98 | |
| 99 | /// runOnFunction - Top level algorithm. |
| 100 | /// |
| 101 | bool JumpThreading::runOnFunction(Function &F) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 102 | DEBUG(errs() << "Jump threading on function '" << F.getName() << "'\n"); |
Dan Gohman | 02a436c | 2009-07-24 18:13:53 +0000 | [diff] [blame] | 103 | TD = getAnalysisIfAvailable<TargetData>(); |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 104 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 105 | FindLoopHeaders(F); |
| 106 | |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 107 | bool AnotherIteration = true, EverChanged = false; |
| 108 | while (AnotherIteration) { |
| 109 | AnotherIteration = false; |
| 110 | bool Changed = false; |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 111 | for (Function::iterator I = F.begin(), E = F.end(); I != E;) { |
| 112 | BasicBlock *BB = I; |
| 113 | while (ProcessBlock(BB)) |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 114 | Changed = true; |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 115 | |
| 116 | ++I; |
| 117 | |
| 118 | // If the block is trivially dead, zap it. This eliminates the successor |
| 119 | // edges which simplifies the CFG. |
| 120 | if (pred_begin(BB) == pred_end(BB) && |
Chris Lattner | 20fa76e | 2008-12-08 22:44:07 +0000 | [diff] [blame] | 121 | BB != &BB->getParent()->getEntryBlock()) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 122 | DEBUG(errs() << " JT: Deleting dead block '" << BB->getName() |
| 123 | << "' with terminator: " << *BB->getTerminator()); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 124 | LoopHeaders.erase(BB); |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 125 | DeleteDeadBlock(BB); |
| 126 | Changed = true; |
| 127 | } |
| 128 | } |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 129 | AnotherIteration = Changed; |
| 130 | EverChanged |= Changed; |
| 131 | } |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 132 | |
| 133 | LoopHeaders.clear(); |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 134 | return EverChanged; |
Chris Lattner | 8383a7b | 2008-04-20 20:35:01 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 136 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 137 | /// FindLoopHeaders - We do not want jump threading to turn proper loop |
| 138 | /// structures into irreducible loops. Doing this breaks up the loop nesting |
| 139 | /// hierarchy and pessimizes later transformations. To prevent this from |
| 140 | /// happening, we first have to find the loop headers. Here we approximate this |
| 141 | /// by finding targets of backedges in the CFG. |
| 142 | /// |
| 143 | /// Note that there definitely are cases when we want to allow threading of |
| 144 | /// edges across a loop header. For example, threading a jump from outside the |
| 145 | /// loop (the preheader) to an exit block of the loop is definitely profitable. |
| 146 | /// It is also almost always profitable to thread backedges from within the loop |
| 147 | /// to exit blocks, and is often profitable to thread backedges to other blocks |
| 148 | /// within the loop (forming a nested loop). This simple analysis is not rich |
| 149 | /// enough to track all of these properties and keep it up-to-date as the CFG |
| 150 | /// mutates, so we don't allow any of these transformations. |
| 151 | /// |
| 152 | void JumpThreading::FindLoopHeaders(Function &F) { |
| 153 | SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges; |
| 154 | FindFunctionBackedges(F, Edges); |
| 155 | |
| 156 | for (unsigned i = 0, e = Edges.size(); i != e; ++i) |
| 157 | LoopHeaders.insert(const_cast<BasicBlock*>(Edges[i].second)); |
| 158 | } |
| 159 | |
| 160 | |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 161 | /// FactorCommonPHIPreds - If there are multiple preds with the same incoming |
| 162 | /// value for the PHI, factor them together so we get one block to thread for |
| 163 | /// the whole group. |
| 164 | /// This is important for things like "phi i1 [true, true, false, true, x]" |
| 165 | /// where we only need to clone the block for the true blocks once. |
| 166 | /// |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 167 | BasicBlock *JumpThreading::FactorCommonPHIPreds(PHINode *PN, Value *Val) { |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 168 | SmallVector<BasicBlock*, 16> CommonPreds; |
| 169 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 170 | if (PN->getIncomingValue(i) == Val) |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 171 | CommonPreds.push_back(PN->getIncomingBlock(i)); |
| 172 | |
| 173 | if (CommonPreds.size() == 1) |
| 174 | return CommonPreds[0]; |
| 175 | |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 176 | DEBUG(errs() << " Factoring out " << CommonPreds.size() |
| 177 | << " common predecessors.\n"); |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 178 | return SplitBlockPredecessors(PN->getParent(), |
| 179 | &CommonPreds[0], CommonPreds.size(), |
| 180 | ".thr_comm", this); |
| 181 | } |
| 182 | |
| 183 | |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 184 | /// getJumpThreadDuplicationCost - Return the cost of duplicating this block to |
| 185 | /// thread across it. |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 186 | static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB) { |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 187 | /// Ignore PHI nodes, these will be flattened when duplication happens. |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 188 | BasicBlock::const_iterator I = BB->getFirstNonPHI(); |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 189 | |
| 190 | // Sum up the cost of each instruction until we get to the terminator. Don't |
| 191 | // include the terminator because the copy won't include it. |
| 192 | unsigned Size = 0; |
| 193 | for (; !isa<TerminatorInst>(I); ++I) { |
| 194 | // Debugger intrinsics don't incur code size. |
| 195 | if (isa<DbgInfoIntrinsic>(I)) continue; |
| 196 | |
| 197 | // If this is a pointer->pointer bitcast, it is free. |
| 198 | if (isa<BitCastInst>(I) && isa<PointerType>(I->getType())) |
| 199 | continue; |
| 200 | |
| 201 | // All other instructions count for at least one unit. |
| 202 | ++Size; |
| 203 | |
| 204 | // Calls are more expensive. If they are non-intrinsic calls, we model them |
| 205 | // as having cost of 4. If they are a non-vector intrinsic, we model them |
| 206 | // as having cost of 2 total, and if they are a vector intrinsic, we model |
| 207 | // them as having cost 1. |
| 208 | if (const CallInst *CI = dyn_cast<CallInst>(I)) { |
| 209 | if (!isa<IntrinsicInst>(CI)) |
| 210 | Size += 3; |
Chris Lattner | 62c762f | 2009-07-02 15:39:39 +0000 | [diff] [blame] | 211 | else if (!isa<VectorType>(CI->getType())) |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 212 | Size += 1; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | // Threading through a switch statement is particularly profitable. If this |
| 217 | // block ends in a switch, decrease its cost to make it more likely to happen. |
| 218 | if (isa<SwitchInst>(I)) |
| 219 | Size = Size > 6 ? Size-6 : 0; |
| 220 | |
| 221 | return Size; |
| 222 | } |
| 223 | |
Chris Lattner | c7bcbf6 | 2008-11-27 07:20:04 +0000 | [diff] [blame] | 224 | /// ProcessBlock - If there are any predecessors whose control can be threaded |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 225 | /// through to a successor, transform them now. |
Chris Lattner | c7bcbf6 | 2008-11-27 07:20:04 +0000 | [diff] [blame] | 226 | bool JumpThreading::ProcessBlock(BasicBlock *BB) { |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 227 | // If this block has a single predecessor, and if that pred has a single |
| 228 | // successor, merge the blocks. This encourages recursive jump threading |
| 229 | // because now the condition in this block can be threaded through |
| 230 | // predecessors of our predecessor block. |
| 231 | if (BasicBlock *SinglePred = BB->getSinglePredecessor()) |
Chris Lattner | f5102a0 | 2008-11-28 19:54:49 +0000 | [diff] [blame] | 232 | if (SinglePred->getTerminator()->getNumSuccessors() == 1 && |
| 233 | SinglePred != BB) { |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 234 | // If SinglePred was a loop header, BB becomes one. |
| 235 | if (LoopHeaders.erase(SinglePred)) |
| 236 | LoopHeaders.insert(BB); |
| 237 | |
Chris Lattner | 3d86d24 | 2008-11-27 19:25:19 +0000 | [diff] [blame] | 238 | // Remember if SinglePred was the entry block of the function. If so, we |
| 239 | // will need to move BB back to the entry position. |
| 240 | bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock(); |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 241 | MergeBasicBlockIntoOnlyPred(BB); |
Chris Lattner | 3d86d24 | 2008-11-27 19:25:19 +0000 | [diff] [blame] | 242 | |
| 243 | if (isEntry && BB != &BB->getParent()->getEntryBlock()) |
| 244 | BB->moveBefore(&BB->getParent()->getEntryBlock()); |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 245 | return true; |
| 246 | } |
| 247 | |
Matthijs Kooijman | 6e7b322 | 2008-05-20 07:26:45 +0000 | [diff] [blame] | 248 | // See if this block ends with a branch or switch. If so, see if the |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 249 | // condition is a phi node. If so, and if an entry of the phi node is a |
| 250 | // constant, we can thread the block. |
| 251 | Value *Condition; |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 252 | if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) { |
| 253 | // Can't thread an unconditional jump. |
| 254 | if (BI->isUnconditional()) return false; |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 255 | Condition = BI->getCondition(); |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 256 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB->getTerminator())) |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 257 | Condition = SI->getCondition(); |
| 258 | else |
| 259 | return false; // Must be an invoke. |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 260 | |
| 261 | // If the terminator of this block is branching on a constant, simplify the |
Chris Lattner | 037c781 | 2008-04-21 18:25:01 +0000 | [diff] [blame] | 262 | // terminator to an unconditional branch. This can occur due to threading in |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 263 | // other blocks. |
| 264 | if (isa<ConstantInt>(Condition)) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 265 | DEBUG(errs() << " In block '" << BB->getName() |
| 266 | << "' folding terminator: " << *BB->getTerminator()); |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 267 | ++NumFolds; |
| 268 | ConstantFoldTerminator(BB); |
| 269 | return true; |
| 270 | } |
| 271 | |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 272 | // If the terminator is branching on an undef, we can pick any of the |
| 273 | // successors to branch to. Since this is arbitrary, we pick the successor |
| 274 | // with the fewest predecessors. This should reduce the in-degree of the |
| 275 | // others. |
| 276 | if (isa<UndefValue>(Condition)) { |
| 277 | TerminatorInst *BBTerm = BB->getTerminator(); |
| 278 | unsigned MinSucc = 0; |
| 279 | BasicBlock *TestBB = BBTerm->getSuccessor(MinSucc); |
| 280 | // Compute the successor with the minimum number of predecessors. |
| 281 | unsigned MinNumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB)); |
| 282 | for (unsigned i = 1, e = BBTerm->getNumSuccessors(); i != e; ++i) { |
| 283 | TestBB = BBTerm->getSuccessor(i); |
| 284 | unsigned NumPreds = std::distance(pred_begin(TestBB), pred_end(TestBB)); |
| 285 | if (NumPreds < MinNumPreds) |
| 286 | MinSucc = i; |
| 287 | } |
| 288 | |
| 289 | // Fold the branch/switch. |
| 290 | for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i) { |
| 291 | if (i == MinSucc) continue; |
| 292 | BBTerm->getSuccessor(i)->removePredecessor(BB); |
| 293 | } |
| 294 | |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 295 | DEBUG(errs() << " In block '" << BB->getName() |
| 296 | << "' folding undef terminator: " << *BBTerm); |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 297 | BranchInst::Create(BBTerm->getSuccessor(MinSucc), BBTerm); |
| 298 | BBTerm->eraseFromParent(); |
| 299 | return true; |
| 300 | } |
| 301 | |
| 302 | Instruction *CondInst = dyn_cast<Instruction>(Condition); |
| 303 | |
| 304 | // If the condition is an instruction defined in another block, see if a |
| 305 | // predecessor has the same condition: |
| 306 | // br COND, BBX, BBY |
| 307 | // BBX: |
| 308 | // br COND, BBZ, BBW |
| 309 | if (!Condition->hasOneUse() && // Multiple uses. |
| 310 | (CondInst == 0 || CondInst->getParent() != BB)) { // Non-local definition. |
| 311 | pred_iterator PI = pred_begin(BB), E = pred_end(BB); |
| 312 | if (isa<BranchInst>(BB->getTerminator())) { |
| 313 | for (; PI != E; ++PI) |
| 314 | if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) |
| 315 | if (PBI->isConditional() && PBI->getCondition() == Condition && |
| 316 | ProcessBranchOnDuplicateCond(*PI, BB)) |
| 317 | return true; |
Chris Lattner | 3cda3cd | 2008-12-04 06:31:07 +0000 | [diff] [blame] | 318 | } else { |
| 319 | assert(isa<SwitchInst>(BB->getTerminator()) && "Unknown jump terminator"); |
| 320 | for (; PI != E; ++PI) |
| 321 | if (SwitchInst *PSI = dyn_cast<SwitchInst>((*PI)->getTerminator())) |
| 322 | if (PSI->getCondition() == Condition && |
| 323 | ProcessSwitchOnDuplicateCond(*PI, BB)) |
| 324 | return true; |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 328 | // All the rest of our checks depend on the condition being an instruction. |
| 329 | if (CondInst == 0) |
| 330 | return false; |
| 331 | |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 332 | // See if this is a phi node in the current block. |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 333 | if (PHINode *PN = dyn_cast<PHINode>(CondInst)) |
| 334 | if (PN->getParent() == BB) |
| 335 | return ProcessJumpOnPHI(PN); |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 336 | |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 337 | // If this is a conditional branch whose condition is and/or of a phi, try to |
| 338 | // simplify it. |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 339 | if ((CondInst->getOpcode() == Instruction::And || |
| 340 | CondInst->getOpcode() == Instruction::Or) && |
| 341 | isa<BranchInst>(BB->getTerminator()) && |
| 342 | ProcessBranchOnLogical(CondInst, BB, |
| 343 | CondInst->getOpcode() == Instruction::And)) |
| 344 | return true; |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 345 | |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 346 | if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) { |
| 347 | if (isa<PHINode>(CondCmp->getOperand(0))) { |
| 348 | // If we have "br (phi != 42)" and the phi node has any constant values |
| 349 | // as operands, we can thread through this block. |
| 350 | // |
| 351 | // If we have "br (cmp phi, x)" and the phi node contains x such that the |
| 352 | // comparison uniquely identifies the branch target, we can thread |
| 353 | // through this block. |
| 354 | |
| 355 | if (ProcessBranchOnCompare(CondCmp, BB)) |
| 356 | return true; |
| 357 | } |
Chris Lattner | 79c740f | 2009-06-19 16:27:56 +0000 | [diff] [blame] | 358 | |
| 359 | // If we have a comparison, loop over the predecessors to see if there is |
| 360 | // a condition with the same value. |
| 361 | pred_iterator PI = pred_begin(BB), E = pred_end(BB); |
| 362 | for (; PI != E; ++PI) |
| 363 | if (BranchInst *PBI = dyn_cast<BranchInst>((*PI)->getTerminator())) |
| 364 | if (PBI->isConditional() && *PI != BB) { |
| 365 | if (CmpInst *CI = dyn_cast<CmpInst>(PBI->getCondition())) { |
| 366 | if (CI->getOperand(0) == CondCmp->getOperand(0) && |
| 367 | CI->getOperand(1) == CondCmp->getOperand(1) && |
| 368 | CI->getPredicate() == CondCmp->getPredicate()) { |
| 369 | // TODO: Could handle things like (x != 4) --> (x == 17) |
| 370 | if (ProcessBranchOnDuplicateCond(*PI, BB)) |
| 371 | return true; |
| 372 | } |
| 373 | } |
| 374 | } |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 375 | } |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 376 | |
| 377 | // Check for some cases that are worth simplifying. Right now we want to look |
| 378 | // for loads that are used by a switch or by the condition for the branch. If |
| 379 | // we see one, check to see if it's partially redundant. If so, insert a PHI |
| 380 | // which can then be used to thread the values. |
| 381 | // |
| 382 | // This is particularly important because reg2mem inserts loads and stores all |
| 383 | // over the place, and this blocks jump threading if we don't zap them. |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 384 | Value *SimplifyValue = CondInst; |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 385 | if (CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue)) |
| 386 | if (isa<Constant>(CondCmp->getOperand(1))) |
| 387 | SimplifyValue = CondCmp->getOperand(0); |
| 388 | |
| 389 | if (LoadInst *LI = dyn_cast<LoadInst>(SimplifyValue)) |
| 390 | if (SimplifyPartiallyRedundantLoad(LI)) |
| 391 | return true; |
| 392 | |
| 393 | // TODO: If we have: "br (X > 0)" and we have a predecessor where we know |
| 394 | // "(X == 4)" thread through this block. |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 395 | |
Chris Lattner | d38c14e | 2008-04-22 06:36:15 +0000 | [diff] [blame] | 396 | return false; |
| 397 | } |
| 398 | |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 399 | /// ProcessBranchOnDuplicateCond - We found a block and a predecessor of that |
| 400 | /// block that jump on exactly the same condition. This means that we almost |
| 401 | /// always know the direction of the edge in the DESTBB: |
| 402 | /// PREDBB: |
| 403 | /// br COND, DESTBB, BBY |
| 404 | /// DESTBB: |
| 405 | /// br COND, BBZ, BBW |
| 406 | /// |
| 407 | /// If DESTBB has multiple predecessors, we can't just constant fold the branch |
| 408 | /// in DESTBB, we have to thread over it. |
| 409 | bool JumpThreading::ProcessBranchOnDuplicateCond(BasicBlock *PredBB, |
| 410 | BasicBlock *BB) { |
| 411 | BranchInst *PredBI = cast<BranchInst>(PredBB->getTerminator()); |
| 412 | |
| 413 | // If both successors of PredBB go to DESTBB, we don't know anything. We can |
| 414 | // fold the branch to an unconditional one, which allows other recursive |
| 415 | // simplifications. |
| 416 | bool BranchDir; |
| 417 | if (PredBI->getSuccessor(1) != BB) |
| 418 | BranchDir = true; |
| 419 | else if (PredBI->getSuccessor(0) != BB) |
| 420 | BranchDir = false; |
| 421 | else { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 422 | DEBUG(errs() << " In block '" << PredBB->getName() |
| 423 | << "' folding terminator: " << *PredBB->getTerminator()); |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 424 | ++NumFolds; |
| 425 | ConstantFoldTerminator(PredBB); |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | BranchInst *DestBI = cast<BranchInst>(BB->getTerminator()); |
| 430 | |
| 431 | // If the dest block has one predecessor, just fix the branch condition to a |
| 432 | // constant and fold it. |
| 433 | if (BB->getSinglePredecessor()) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 434 | DEBUG(errs() << " In block '" << BB->getName() |
| 435 | << "' folding condition to '" << BranchDir << "': " |
| 436 | << *BB->getTerminator()); |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 437 | ++NumFolds; |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 438 | DestBI->setCondition(ConstantInt::get(Type::Int1Ty, BranchDir)); |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 439 | ConstantFoldTerminator(BB); |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | // Otherwise we need to thread from PredBB to DestBB's successor which |
| 444 | // involves code duplication. Check to see if it is worth it. |
| 445 | unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB); |
| 446 | if (JumpThreadCost > Threshold) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 447 | DEBUG(errs() << " Not threading BB '" << BB->getName() |
| 448 | << "' - Cost is too high: " << JumpThreadCost << "\n"); |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
| 452 | // Next, figure out which successor we are threading to. |
| 453 | BasicBlock *SuccBB = DestBI->getSuccessor(!BranchDir); |
| 454 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 455 | // Ok, try to thread it! |
| 456 | return ThreadEdge(BB, PredBB, SuccBB, JumpThreadCost); |
Chris Lattner | 421fa9e | 2008-12-03 07:48:08 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Chris Lattner | 3cda3cd | 2008-12-04 06:31:07 +0000 | [diff] [blame] | 459 | /// ProcessSwitchOnDuplicateCond - We found a block and a predecessor of that |
| 460 | /// block that switch on exactly the same condition. This means that we almost |
| 461 | /// always know the direction of the edge in the DESTBB: |
| 462 | /// PREDBB: |
| 463 | /// switch COND [... DESTBB, BBY ... ] |
| 464 | /// DESTBB: |
| 465 | /// switch COND [... BBZ, BBW ] |
| 466 | /// |
| 467 | /// Optimizing switches like this is very important, because simplifycfg builds |
| 468 | /// switches out of repeated 'if' conditions. |
| 469 | bool JumpThreading::ProcessSwitchOnDuplicateCond(BasicBlock *PredBB, |
| 470 | BasicBlock *DestBB) { |
Chris Lattner | 2c7ed11 | 2009-01-19 21:20:34 +0000 | [diff] [blame] | 471 | // Can't thread edge to self. |
| 472 | if (PredBB == DestBB) |
| 473 | return false; |
| 474 | |
| 475 | |
Chris Lattner | 3cda3cd | 2008-12-04 06:31:07 +0000 | [diff] [blame] | 476 | SwitchInst *PredSI = cast<SwitchInst>(PredBB->getTerminator()); |
| 477 | SwitchInst *DestSI = cast<SwitchInst>(DestBB->getTerminator()); |
| 478 | |
| 479 | // There are a variety of optimizations that we can potentially do on these |
| 480 | // blocks: we order them from most to least preferable. |
| 481 | |
| 482 | // If DESTBB *just* contains the switch, then we can forward edges from PREDBB |
| 483 | // directly to their destination. This does not introduce *any* code size |
Dale Johannesen | 6b23339 | 2009-03-17 00:38:24 +0000 | [diff] [blame] | 484 | // growth. Skip debug info first. |
| 485 | BasicBlock::iterator BBI = DestBB->begin(); |
| 486 | while (isa<DbgInfoIntrinsic>(BBI)) |
| 487 | BBI++; |
Chris Lattner | 3cda3cd | 2008-12-04 06:31:07 +0000 | [diff] [blame] | 488 | |
| 489 | // FIXME: Thread if it just contains a PHI. |
Dale Johannesen | 6b23339 | 2009-03-17 00:38:24 +0000 | [diff] [blame] | 490 | if (isa<SwitchInst>(BBI)) { |
Chris Lattner | 3cda3cd | 2008-12-04 06:31:07 +0000 | [diff] [blame] | 491 | bool MadeChange = false; |
| 492 | // Ignore the default edge for now. |
| 493 | for (unsigned i = 1, e = DestSI->getNumSuccessors(); i != e; ++i) { |
| 494 | ConstantInt *DestVal = DestSI->getCaseValue(i); |
| 495 | BasicBlock *DestSucc = DestSI->getSuccessor(i); |
| 496 | |
| 497 | // Okay, DestSI has a case for 'DestVal' that goes to 'DestSucc'. See if |
| 498 | // PredSI has an explicit case for it. If so, forward. If it is covered |
| 499 | // by the default case, we can't update PredSI. |
| 500 | unsigned PredCase = PredSI->findCaseValue(DestVal); |
| 501 | if (PredCase == 0) continue; |
| 502 | |
| 503 | // If PredSI doesn't go to DestBB on this value, then it won't reach the |
| 504 | // case on this condition. |
| 505 | if (PredSI->getSuccessor(PredCase) != DestBB && |
| 506 | DestSI->getSuccessor(i) != DestBB) |
| 507 | continue; |
| 508 | |
| 509 | // Otherwise, we're safe to make the change. Make sure that the edge from |
| 510 | // DestSI to DestSucc is not critical and has no PHI nodes. |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 511 | DEBUG(errs() << "FORWARDING EDGE " << *DestVal << " FROM: " << *PredSI); |
| 512 | DEBUG(errs() << "THROUGH: " << *DestSI); |
Chris Lattner | 3cda3cd | 2008-12-04 06:31:07 +0000 | [diff] [blame] | 513 | |
| 514 | // If the destination has PHI nodes, just split the edge for updating |
| 515 | // simplicity. |
| 516 | if (isa<PHINode>(DestSucc->begin()) && !DestSucc->getSinglePredecessor()){ |
| 517 | SplitCriticalEdge(DestSI, i, this); |
| 518 | DestSucc = DestSI->getSuccessor(i); |
| 519 | } |
| 520 | FoldSingleEntryPHINodes(DestSucc); |
| 521 | PredSI->setSuccessor(PredCase, DestSucc); |
| 522 | MadeChange = true; |
| 523 | } |
| 524 | |
| 525 | if (MadeChange) |
| 526 | return true; |
| 527 | } |
| 528 | |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 533 | /// SimplifyPartiallyRedundantLoad - If LI is an obviously partially redundant |
| 534 | /// load instruction, eliminate it by replacing it with a PHI node. This is an |
| 535 | /// important optimization that encourages jump threading, and needs to be run |
| 536 | /// interlaced with other jump threading tasks. |
| 537 | bool JumpThreading::SimplifyPartiallyRedundantLoad(LoadInst *LI) { |
| 538 | // Don't hack volatile loads. |
| 539 | if (LI->isVolatile()) return false; |
| 540 | |
| 541 | // If the load is defined in a block with exactly one predecessor, it can't be |
| 542 | // partially redundant. |
| 543 | BasicBlock *LoadBB = LI->getParent(); |
| 544 | if (LoadBB->getSinglePredecessor()) |
| 545 | return false; |
| 546 | |
| 547 | Value *LoadedPtr = LI->getOperand(0); |
| 548 | |
| 549 | // If the loaded operand is defined in the LoadBB, it can't be available. |
| 550 | // FIXME: Could do PHI translation, that would be fun :) |
| 551 | if (Instruction *PtrOp = dyn_cast<Instruction>(LoadedPtr)) |
| 552 | if (PtrOp->getParent() == LoadBB) |
| 553 | return false; |
| 554 | |
| 555 | // Scan a few instructions up from the load, to see if it is obviously live at |
| 556 | // the entry to its block. |
| 557 | BasicBlock::iterator BBIt = LI; |
| 558 | |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 559 | if (Value *AvailableVal = FindAvailableLoadedValue(LoadedPtr, LoadBB, |
| 560 | BBIt, 6)) { |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 561 | // If the value if the load is locally available within the block, just use |
| 562 | // it. This frequently occurs for reg2mem'd allocas. |
| 563 | //cerr << "LOAD ELIMINATED:\n" << *BBIt << *LI << "\n"; |
Chris Lattner | 2a99b48 | 2009-01-09 06:08:12 +0000 | [diff] [blame] | 564 | |
| 565 | // If the returned value is the load itself, replace with an undef. This can |
| 566 | // only happen in dead loops. |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 567 | if (AvailableVal == LI) AvailableVal = |
| 568 | AvailableVal->getContext().getUndef(LI->getType()); |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 569 | LI->replaceAllUsesWith(AvailableVal); |
| 570 | LI->eraseFromParent(); |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | // Otherwise, if we scanned the whole block and got to the top of the block, |
| 575 | // we know the block is locally transparent to the load. If not, something |
| 576 | // might clobber its value. |
| 577 | if (BBIt != LoadBB->begin()) |
| 578 | return false; |
| 579 | |
| 580 | |
| 581 | SmallPtrSet<BasicBlock*, 8> PredsScanned; |
| 582 | typedef SmallVector<std::pair<BasicBlock*, Value*>, 8> AvailablePredsTy; |
| 583 | AvailablePredsTy AvailablePreds; |
| 584 | BasicBlock *OneUnavailablePred = 0; |
| 585 | |
| 586 | // If we got here, the loaded value is transparent through to the start of the |
| 587 | // block. Check to see if it is available in any of the predecessor blocks. |
| 588 | for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB); |
| 589 | PI != PE; ++PI) { |
| 590 | BasicBlock *PredBB = *PI; |
| 591 | |
| 592 | // If we already scanned this predecessor, skip it. |
| 593 | if (!PredsScanned.insert(PredBB)) |
| 594 | continue; |
| 595 | |
| 596 | // Scan the predecessor to see if the value is available in the pred. |
| 597 | BBIt = PredBB->end(); |
Chris Lattner | 52c9585 | 2008-11-27 08:10:05 +0000 | [diff] [blame] | 598 | Value *PredAvailable = FindAvailableLoadedValue(LoadedPtr, PredBB, BBIt, 6); |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 599 | if (!PredAvailable) { |
| 600 | OneUnavailablePred = PredBB; |
| 601 | continue; |
| 602 | } |
| 603 | |
| 604 | // If so, this load is partially redundant. Remember this info so that we |
| 605 | // can create a PHI node. |
| 606 | AvailablePreds.push_back(std::make_pair(PredBB, PredAvailable)); |
| 607 | } |
| 608 | |
| 609 | // If the loaded value isn't available in any predecessor, it isn't partially |
| 610 | // redundant. |
| 611 | if (AvailablePreds.empty()) return false; |
| 612 | |
| 613 | // Okay, the loaded value is available in at least one (and maybe all!) |
| 614 | // predecessors. If the value is unavailable in more than one unique |
| 615 | // predecessor, we want to insert a merge block for those common predecessors. |
| 616 | // This ensures that we only have to insert one reload, thus not increasing |
| 617 | // code size. |
| 618 | BasicBlock *UnavailablePred = 0; |
| 619 | |
| 620 | // If there is exactly one predecessor where the value is unavailable, the |
| 621 | // already computed 'OneUnavailablePred' block is it. If it ends in an |
| 622 | // unconditional branch, we know that it isn't a critical edge. |
| 623 | if (PredsScanned.size() == AvailablePreds.size()+1 && |
| 624 | OneUnavailablePred->getTerminator()->getNumSuccessors() == 1) { |
| 625 | UnavailablePred = OneUnavailablePred; |
| 626 | } else if (PredsScanned.size() != AvailablePreds.size()) { |
| 627 | // Otherwise, we had multiple unavailable predecessors or we had a critical |
| 628 | // edge from the one. |
| 629 | SmallVector<BasicBlock*, 8> PredsToSplit; |
| 630 | SmallPtrSet<BasicBlock*, 8> AvailablePredSet; |
| 631 | |
| 632 | for (unsigned i = 0, e = AvailablePreds.size(); i != e; ++i) |
| 633 | AvailablePredSet.insert(AvailablePreds[i].first); |
| 634 | |
| 635 | // Add all the unavailable predecessors to the PredsToSplit list. |
| 636 | for (pred_iterator PI = pred_begin(LoadBB), PE = pred_end(LoadBB); |
| 637 | PI != PE; ++PI) |
| 638 | if (!AvailablePredSet.count(*PI)) |
| 639 | PredsToSplit.push_back(*PI); |
| 640 | |
| 641 | // Split them out to their own block. |
| 642 | UnavailablePred = |
| 643 | SplitBlockPredecessors(LoadBB, &PredsToSplit[0], PredsToSplit.size(), |
| 644 | "thread-split", this); |
| 645 | } |
| 646 | |
| 647 | // If the value isn't available in all predecessors, then there will be |
| 648 | // exactly one where it isn't available. Insert a load on that edge and add |
| 649 | // it to the AvailablePreds list. |
| 650 | if (UnavailablePred) { |
| 651 | assert(UnavailablePred->getTerminator()->getNumSuccessors() == 1 && |
| 652 | "Can't handle critical edge here!"); |
| 653 | Value *NewVal = new LoadInst(LoadedPtr, LI->getName()+".pr", |
| 654 | UnavailablePred->getTerminator()); |
| 655 | AvailablePreds.push_back(std::make_pair(UnavailablePred, NewVal)); |
| 656 | } |
| 657 | |
| 658 | // Now we know that each predecessor of this block has a value in |
| 659 | // AvailablePreds, sort them for efficient access as we're walking the preds. |
Chris Lattner | a352200 | 2008-12-01 06:52:57 +0000 | [diff] [blame] | 660 | array_pod_sort(AvailablePreds.begin(), AvailablePreds.end()); |
Chris Lattner | 69e067f | 2008-11-27 05:07:53 +0000 | [diff] [blame] | 661 | |
| 662 | // Create a PHI node at the start of the block for the PRE'd load value. |
| 663 | PHINode *PN = PHINode::Create(LI->getType(), "", LoadBB->begin()); |
| 664 | PN->takeName(LI); |
| 665 | |
| 666 | // Insert new entries into the PHI for each predecessor. A single block may |
| 667 | // have multiple entries here. |
| 668 | for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB); PI != E; |
| 669 | ++PI) { |
| 670 | AvailablePredsTy::iterator I = |
| 671 | std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(), |
| 672 | std::make_pair(*PI, (Value*)0)); |
| 673 | |
| 674 | assert(I != AvailablePreds.end() && I->first == *PI && |
| 675 | "Didn't find entry for predecessor!"); |
| 676 | |
| 677 | PN->addIncoming(I->second, I->first); |
| 678 | } |
| 679 | |
| 680 | //cerr << "PRE: " << *LI << *PN << "\n"; |
| 681 | |
| 682 | LI->replaceAllUsesWith(PN); |
| 683 | LI->eraseFromParent(); |
| 684 | |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | |
Chris Lattner | d38c14e | 2008-04-22 06:36:15 +0000 | [diff] [blame] | 689 | /// ProcessJumpOnPHI - We have a conditional branch of switch on a PHI node in |
| 690 | /// the current block. See if there are any simplifications we can do based on |
| 691 | /// inputs to the phi node. |
| 692 | /// |
| 693 | bool JumpThreading::ProcessJumpOnPHI(PHINode *PN) { |
Chris Lattner | f9065a9 | 2008-04-20 21:18:09 +0000 | [diff] [blame] | 694 | // See if the phi node has any constant values. If so, we can determine where |
| 695 | // the corresponding predecessor will branch. |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 696 | ConstantInt *PredCst = 0; |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 697 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 698 | if ((PredCst = dyn_cast<ConstantInt>(PN->getIncomingValue(i)))) |
Chris Lattner | f9065a9 | 2008-04-20 21:18:09 +0000 | [diff] [blame] | 699 | break; |
Chris Lattner | f9065a9 | 2008-04-20 21:18:09 +0000 | [diff] [blame] | 700 | |
| 701 | // If no incoming value has a constant, we don't know the destination of any |
| 702 | // predecessors. |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 703 | if (PredCst == 0) |
Chris Lattner | f9065a9 | 2008-04-20 21:18:09 +0000 | [diff] [blame] | 704 | return false; |
| 705 | |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 706 | // See if the cost of duplicating this block is low enough. |
Chris Lattner | d38c14e | 2008-04-22 06:36:15 +0000 | [diff] [blame] | 707 | BasicBlock *BB = PN->getParent(); |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 708 | unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB); |
| 709 | if (JumpThreadCost > Threshold) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 710 | DEBUG(errs() << " Not threading BB '" << BB->getName() |
| 711 | << "' - Cost is too high: " << JumpThreadCost << "\n"); |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 712 | return false; |
| 713 | } |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 714 | |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 715 | // If so, we can actually do this threading. Merge any common predecessors |
| 716 | // that will act the same. |
| 717 | BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredCst); |
| 718 | |
| 719 | // Next, figure out which successor we are threading to. |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 720 | BasicBlock *SuccBB; |
| 721 | if (BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator())) |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 722 | SuccBB = BI->getSuccessor(PredCst == PredBB->getContext().getFalse()); |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 723 | else { |
| 724 | SwitchInst *SI = cast<SwitchInst>(BB->getTerminator()); |
| 725 | SuccBB = SI->getSuccessor(SI->findCaseValue(PredCst)); |
| 726 | } |
| 727 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 728 | // Ok, try to thread it! |
| 729 | return ThreadEdge(BB, PredBB, SuccBB, JumpThreadCost); |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 732 | /// ProcessJumpOnLogicalPHI - PN's basic block contains a conditional branch |
| 733 | /// whose condition is an AND/OR where one side is PN. If PN has constant |
| 734 | /// operands that permit us to evaluate the condition for some operand, thread |
| 735 | /// through the block. For example with: |
| 736 | /// br (and X, phi(Y, Z, false)) |
| 737 | /// the predecessor corresponding to the 'false' will always jump to the false |
| 738 | /// destination of the branch. |
| 739 | /// |
Chris Lattner | ae65b3c | 2008-04-22 20:46:09 +0000 | [diff] [blame] | 740 | bool JumpThreading::ProcessBranchOnLogical(Value *V, BasicBlock *BB, |
| 741 | bool isAnd) { |
| 742 | // If this is a binary operator tree of the same AND/OR opcode, check the |
| 743 | // LHS/RHS. |
| 744 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(V)) |
Duncan Sands | 43e2a03 | 2008-05-27 11:50:51 +0000 | [diff] [blame] | 745 | if ((isAnd && BO->getOpcode() == Instruction::And) || |
| 746 | (!isAnd && BO->getOpcode() == Instruction::Or)) { |
Chris Lattner | ae65b3c | 2008-04-22 20:46:09 +0000 | [diff] [blame] | 747 | if (ProcessBranchOnLogical(BO->getOperand(0), BB, isAnd)) |
| 748 | return true; |
| 749 | if (ProcessBranchOnLogical(BO->getOperand(1), BB, isAnd)) |
| 750 | return true; |
| 751 | } |
| 752 | |
| 753 | // If this isn't a PHI node, we can't handle it. |
| 754 | PHINode *PN = dyn_cast<PHINode>(V); |
| 755 | if (!PN || PN->getParent() != BB) return false; |
| 756 | |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 757 | // We can only do the simplification for phi nodes of 'false' with AND or |
| 758 | // 'true' with OR. See if we have any entries in the phi for this. |
| 759 | unsigned PredNo = ~0U; |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 760 | ConstantInt *PredCst = ConstantInt::get(Type::Int1Ty, !isAnd); |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 761 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 762 | if (PN->getIncomingValue(i) == PredCst) { |
| 763 | PredNo = i; |
| 764 | break; |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | // If no match, bail out. |
| 769 | if (PredNo == ~0U) |
| 770 | return false; |
| 771 | |
| 772 | // See if the cost of duplicating this block is low enough. |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 773 | unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB); |
| 774 | if (JumpThreadCost > Threshold) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 775 | DEBUG(errs() << " Not threading BB '" << BB->getName() |
| 776 | << "' - Cost is too high: " << JumpThreadCost << "\n"); |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 777 | return false; |
| 778 | } |
| 779 | |
| 780 | // If so, we can actually do this threading. Merge any common predecessors |
| 781 | // that will act the same. |
| 782 | BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredCst); |
| 783 | |
| 784 | // Next, figure out which successor we are threading to. If this was an AND, |
| 785 | // the constant must be FALSE, and we must be targeting the 'false' block. |
| 786 | // If this is an OR, the constant must be TRUE, and we must be targeting the |
| 787 | // 'true' block. |
| 788 | BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(isAnd); |
| 789 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 790 | // Ok, try to thread it! |
| 791 | return ThreadEdge(BB, PredBB, SuccBB, JumpThreadCost); |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 794 | /// GetResultOfComparison - Given an icmp/fcmp predicate and the left and right |
| 795 | /// hand sides of the compare instruction, try to determine the result. If the |
| 796 | /// result can not be determined, a null pointer is returned. |
| 797 | static Constant *GetResultOfComparison(CmpInst::Predicate pred, |
Owen Anderson | 1ff50b3 | 2009-07-03 00:54:20 +0000 | [diff] [blame] | 798 | Value *LHS, Value *RHS, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 799 | LLVMContext &Context) { |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 800 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 801 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 802 | return Context.getConstantExprCompare(pred, CLHS, CRHS); |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 803 | |
| 804 | if (LHS == RHS) |
| 805 | if (isa<IntegerType>(LHS->getType()) || isa<PointerType>(LHS->getType())) |
| 806 | return ICmpInst::isTrueWhenEqual(pred) ? |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 807 | Context.getTrue() : Context.getFalse(); |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 812 | /// ProcessBranchOnCompare - We found a branch on a comparison between a phi |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 813 | /// node and a value. If we can identify when the comparison is true between |
| 814 | /// the phi inputs and the value, we can fold the compare for that edge and |
| 815 | /// thread through it. |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 816 | bool JumpThreading::ProcessBranchOnCompare(CmpInst *Cmp, BasicBlock *BB) { |
| 817 | PHINode *PN = cast<PHINode>(Cmp->getOperand(0)); |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 818 | Value *RHS = Cmp->getOperand(1); |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 819 | |
| 820 | // If the phi isn't in the current block, an incoming edge to this block |
| 821 | // doesn't control the destination. |
| 822 | if (PN->getParent() != BB) |
| 823 | return false; |
| 824 | |
| 825 | // We can do this simplification if any comparisons fold to true or false. |
| 826 | // See if any do. |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 827 | Value *PredVal = 0; |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 828 | bool TrueDirection = false; |
| 829 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 830 | PredVal = PN->getIncomingValue(i); |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 831 | |
Owen Anderson | 1ff50b3 | 2009-07-03 00:54:20 +0000 | [diff] [blame] | 832 | Constant *Res = GetResultOfComparison(Cmp->getPredicate(), PredVal, |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 833 | RHS, Cmp->getContext()); |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 834 | if (!Res) { |
| 835 | PredVal = 0; |
| 836 | continue; |
| 837 | } |
| 838 | |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 839 | // If this folded to a constant expr, we can't do anything. |
| 840 | if (ConstantInt *ResC = dyn_cast<ConstantInt>(Res)) { |
| 841 | TrueDirection = ResC->getZExtValue(); |
| 842 | break; |
| 843 | } |
| 844 | // If this folded to undef, just go the false way. |
| 845 | if (isa<UndefValue>(Res)) { |
| 846 | TrueDirection = false; |
| 847 | break; |
| 848 | } |
| 849 | |
| 850 | // Otherwise, we can't fold this input. |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 851 | PredVal = 0; |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | // If no match, bail out. |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 855 | if (PredVal == 0) |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 856 | return false; |
| 857 | |
| 858 | // See if the cost of duplicating this block is low enough. |
| 859 | unsigned JumpThreadCost = getJumpThreadDuplicationCost(BB); |
| 860 | if (JumpThreadCost > Threshold) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 861 | DEBUG(errs() << " Not threading BB '" << BB->getNameStart() |
| 862 | << "' - Cost is too high: " << JumpThreadCost << "\n"); |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 863 | return false; |
| 864 | } |
| 865 | |
| 866 | // If so, we can actually do this threading. Merge any common predecessors |
| 867 | // that will act the same. |
Nick Lewycky | 9683f18 | 2009-06-19 04:56:29 +0000 | [diff] [blame] | 868 | BasicBlock *PredBB = FactorCommonPHIPreds(PN, PredVal); |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 869 | |
| 870 | // Next, get our successor. |
| 871 | BasicBlock *SuccBB = BB->getTerminator()->getSuccessor(!TrueDirection); |
| 872 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 873 | // Ok, try to thread it! |
| 874 | return ThreadEdge(BB, PredBB, SuccBB, JumpThreadCost); |
Chris Lattner | a5ddb59 | 2008-04-22 21:40:39 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Chris Lattner | 6bf7750 | 2008-04-22 07:05:46 +0000 | [diff] [blame] | 877 | |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 878 | /// ThreadEdge - We have decided that it is safe and profitable to thread an |
| 879 | /// edge from PredBB to SuccBB across BB. Transform the IR to reflect this |
| 880 | /// change. |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 881 | bool JumpThreading::ThreadEdge(BasicBlock *BB, BasicBlock *PredBB, |
| 882 | BasicBlock *SuccBB, unsigned JumpThreadCost) { |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 883 | |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 884 | // If threading to the same block as we come from, we would infinite loop. |
| 885 | if (SuccBB == BB) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 886 | DEBUG(errs() << " Not threading across BB '" << BB->getName() |
| 887 | << "' - would thread to self!\n"); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 888 | return false; |
| 889 | } |
| 890 | |
| 891 | // If threading this would thread across a loop header, don't thread the edge. |
| 892 | // See the comments above FindLoopHeaders for justifications and caveats. |
| 893 | if (LoopHeaders.count(BB)) { |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 894 | DEBUG(errs() << " Not threading from '" << PredBB->getName() |
| 895 | << "' across loop header BB '" << BB->getName() |
| 896 | << "' to dest BB '" << SuccBB->getName() |
| 897 | << "' - it might create an irreducible loop!\n"); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 898 | return false; |
| 899 | } |
| 900 | |
| 901 | // And finally, do it! |
Daniel Dunbar | 93b67e4 | 2009-07-26 07:49:05 +0000 | [diff] [blame^] | 902 | DEBUG(errs() << " Threading edge from '" << PredBB->getName() << "' to '" |
| 903 | << SuccBB->getNameStart() << "' with cost: " << JumpThreadCost |
| 904 | << ", across block:\n " |
| 905 | << *BB << "\n"); |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 906 | |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 907 | // Jump Threading can not update SSA properties correctly if the values |
| 908 | // defined in the duplicated block are used outside of the block itself. For |
| 909 | // this reason, we spill all values that are used outside of BB to the stack. |
Chris Lattner | 8554cc2 | 2008-05-05 20:21:22 +0000 | [diff] [blame] | 910 | for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) { |
| 911 | if (!I->isUsedOutsideOfBlock(BB)) |
| 912 | continue; |
| 913 | |
| 914 | // We found a use of I outside of BB. Create a new stack slot to |
| 915 | // break this inter-block usage pattern. |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 916 | DemoteRegToStack(*I); |
Chris Lattner | 8554cc2 | 2008-05-05 20:21:22 +0000 | [diff] [blame] | 917 | } |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 918 | |
| 919 | // We are going to have to map operands from the original BB block to the new |
| 920 | // copy of the block 'NewBB'. If there are PHI nodes in BB, evaluate them to |
| 921 | // account for entry from PredBB. |
| 922 | DenseMap<Instruction*, Value*> ValueMapping; |
| 923 | |
| 924 | BasicBlock *NewBB = |
| 925 | BasicBlock::Create(BB->getName()+".thread", BB->getParent(), BB); |
| 926 | NewBB->moveAfter(PredBB); |
| 927 | |
| 928 | BasicBlock::iterator BI = BB->begin(); |
| 929 | for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) |
| 930 | ValueMapping[PN] = PN->getIncomingValueForBlock(PredBB); |
| 931 | |
| 932 | // Clone the non-phi instructions of BB into NewBB, keeping track of the |
| 933 | // mapping and using it to remap operands in the cloned instructions. |
| 934 | for (; !isa<TerminatorInst>(BI); ++BI) { |
Owen Anderson | e922c02 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 935 | Instruction *New = BI->clone(BI->getContext()); |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 936 | New->setName(BI->getNameStart()); |
| 937 | NewBB->getInstList().push_back(New); |
| 938 | ValueMapping[BI] = New; |
| 939 | |
| 940 | // Remap operands to patch up intra-block references. |
| 941 | for (unsigned i = 0, e = New->getNumOperands(); i != e; ++i) |
Dan Gohman | f530c92 | 2009-07-02 00:17:47 +0000 | [diff] [blame] | 942 | if (Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) { |
| 943 | DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst); |
| 944 | if (I != ValueMapping.end()) |
| 945 | New->setOperand(i, I->second); |
| 946 | } |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 947 | } |
| 948 | |
| 949 | // We didn't copy the terminator from BB over to NewBB, because there is now |
| 950 | // an unconditional jump to SuccBB. Insert the unconditional jump. |
| 951 | BranchInst::Create(SuccBB, NewBB); |
| 952 | |
| 953 | // Check to see if SuccBB has PHI nodes. If so, we need to add entries to the |
| 954 | // PHI nodes for NewBB now. |
| 955 | for (BasicBlock::iterator PNI = SuccBB->begin(); isa<PHINode>(PNI); ++PNI) { |
| 956 | PHINode *PN = cast<PHINode>(PNI); |
| 957 | // Ok, we have a PHI node. Figure out what the incoming value was for the |
| 958 | // DestBlock. |
| 959 | Value *IV = PN->getIncomingValueForBlock(BB); |
| 960 | |
| 961 | // Remap the value if necessary. |
Dan Gohman | f530c92 | 2009-07-02 00:17:47 +0000 | [diff] [blame] | 962 | if (Instruction *Inst = dyn_cast<Instruction>(IV)) { |
| 963 | DenseMap<Instruction*, Value*>::iterator I = ValueMapping.find(Inst); |
| 964 | if (I != ValueMapping.end()) |
| 965 | IV = I->second; |
| 966 | } |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 967 | PN->addIncoming(IV, NewBB); |
| 968 | } |
| 969 | |
Chris Lattner | ef0c674 | 2008-12-01 04:48:07 +0000 | [diff] [blame] | 970 | // Ok, NewBB is good to go. Update the terminator of PredBB to jump to |
Chris Lattner | bd3401f | 2008-04-20 22:39:42 +0000 | [diff] [blame] | 971 | // NewBB instead of BB. This eliminates predecessors from BB, which requires |
| 972 | // us to simplify any PHI nodes in BB. |
| 973 | TerminatorInst *PredTerm = PredBB->getTerminator(); |
| 974 | for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) |
| 975 | if (PredTerm->getSuccessor(i) == BB) { |
| 976 | BB->removePredecessor(PredBB); |
| 977 | PredTerm->setSuccessor(i, NewBB); |
| 978 | } |
Chris Lattner | ef0c674 | 2008-12-01 04:48:07 +0000 | [diff] [blame] | 979 | |
| 980 | // At this point, the IR is fully up to date and consistent. Do a quick scan |
| 981 | // over the new instructions and zap any that are constants or dead. This |
| 982 | // frequently happens because of phi translation. |
| 983 | BI = NewBB->begin(); |
| 984 | for (BasicBlock::iterator E = NewBB->end(); BI != E; ) { |
| 985 | Instruction *Inst = BI++; |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 986 | if (Constant *C = ConstantFoldInstruction(Inst, BB->getContext(), TD)) { |
Chris Lattner | ef0c674 | 2008-12-01 04:48:07 +0000 | [diff] [blame] | 987 | Inst->replaceAllUsesWith(C); |
| 988 | Inst->eraseFromParent(); |
| 989 | continue; |
| 990 | } |
| 991 | |
| 992 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
| 993 | } |
Mike Stump | fe095f3 | 2009-05-04 18:40:41 +0000 | [diff] [blame] | 994 | |
| 995 | // Threaded an edge! |
| 996 | ++NumThreads; |
| 997 | return true; |
Chris Lattner | 177480b | 2008-04-20 21:13:06 +0000 | [diff] [blame] | 998 | } |