Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 1 | //===- TailDuplication.cpp - Simplify CFG through tail duplication --------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass performs a limited form of tail duplication, intended to simplify |
| 11 | // CFGs by removing some unconditional branches. This pass is necessary to |
| 12 | // straighten out loops created by the C front-end, but also is capable of |
| 13 | // making other code nicer. After this pass is run, the CFG simplify pass |
| 14 | // should be run to clean up the mess. |
| 15 | // |
| 16 | // This pass could be enhanced in the future to use profile information to be |
| 17 | // more aggressive. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Chris Lattner | 7514437 | 2006-09-27 04:58:23 +0000 | [diff] [blame] | 21 | #define DEBUG_TYPE "tailduplicate" |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 22 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 3186d27 | 2003-08-31 21:17:44 +0000 | [diff] [blame] | 23 | #include "llvm/Constant.h" |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 24 | #include "llvm/Function.h" |
Misha Brukman | d8e1eea | 2004-07-29 17:05:13 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
Chris Lattner | b9df9b4 | 2004-11-22 17:23:57 +0000 | [diff] [blame] | 26 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 27 | #include "llvm/Pass.h" |
| 28 | #include "llvm/Type.h" |
Duncan Sands | b6133d1 | 2010-11-23 20:26:33 +0000 | [diff] [blame^] | 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/ADT/SmallPtrSet.h" |
| 31 | #include "llvm/Analysis/InstructionSimplify.h" |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CFG.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 35 | #include "llvm/Support/raw_ostream.h" |
Duncan Sands | b6133d1 | 2010-11-23 20:26:33 +0000 | [diff] [blame^] | 36 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | c9235d2 | 2008-03-21 23:51:57 +0000 | [diff] [blame] | 37 | #include <map> |
Chris Lattner | d745602 | 2004-01-09 06:02:20 +0000 | [diff] [blame] | 38 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 40 | STATISTIC(NumEliminated, "Number of unconditional branches eliminated"); |
| 41 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | static cl::opt<unsigned> |
Evan Cheng | 66ae104 | 2008-05-16 07:55:50 +0000 | [diff] [blame] | 43 | TailDupThreshold("taildup-threshold", |
| 44 | cl::desc("Max block size to tail duplicate"), |
| 45 | cl::init(1), cl::Hidden); |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 47 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 48 | class TailDup : public FunctionPass { |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 49 | bool runOnFunction(Function &F); |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 50 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 51 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 52 | TailDup() : FunctionPass(ID) { |
| 53 | initializeTailDupPass(*PassRegistry::getPassRegistry()); |
| 54 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 56 | private: |
Evan Cheng | 66ae104 | 2008-05-16 07:55:50 +0000 | [diff] [blame] | 57 | inline bool shouldEliminateUnconditionalBranch(TerminatorInst *, unsigned); |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 58 | inline void eliminateUnconditionalBranch(BranchInst *BI); |
Dale Johannesen | 72997fe | 2008-05-13 20:06:43 +0000 | [diff] [blame] | 59 | SmallPtrSet<BasicBlock*, 4> CycleDetector; |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 60 | }; |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 63 | char TailDup::ID = 0; |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 64 | INITIALIZE_PASS(TailDup, "tailduplicate", "Tail Duplication", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 65 | |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 66 | // Public interface to the Tail Duplication pass |
Chris Lattner | 4b50156 | 2004-09-20 04:43:15 +0000 | [diff] [blame] | 67 | FunctionPass *llvm::createTailDuplicationPass() { return new TailDup(); } |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 68 | |
| 69 | /// runOnFunction - Top level algorithm - Loop over each unconditional branch in |
Dale Johannesen | 72997fe | 2008-05-13 20:06:43 +0000 | [diff] [blame] | 70 | /// the function, eliminating it if it looks attractive enough. CycleDetector |
| 71 | /// prevents infinite loops by checking that we aren't redirecting a branch to |
| 72 | /// a place it already pointed to earlier; see PR 2323. |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 73 | bool TailDup::runOnFunction(Function &F) { |
| 74 | bool Changed = false; |
Dale Johannesen | 72997fe | 2008-05-13 20:06:43 +0000 | [diff] [blame] | 75 | CycleDetector.clear(); |
| 76 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ) { |
Evan Cheng | 66ae104 | 2008-05-16 07:55:50 +0000 | [diff] [blame] | 77 | if (shouldEliminateUnconditionalBranch(I->getTerminator(), |
| 78 | TailDupThreshold)) { |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 79 | eliminateUnconditionalBranch(cast<BranchInst>(I->getTerminator())); |
| 80 | Changed = true; |
| 81 | } else { |
| 82 | ++I; |
Dale Johannesen | 72997fe | 2008-05-13 20:06:43 +0000 | [diff] [blame] | 83 | CycleDetector.clear(); |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 84 | } |
Dale Johannesen | 72997fe | 2008-05-13 20:06:43 +0000 | [diff] [blame] | 85 | } |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 86 | return Changed; |
| 87 | } |
| 88 | |
| 89 | /// shouldEliminateUnconditionalBranch - Return true if this branch looks |
| 90 | /// attractive to eliminate. We eliminate the branch if the destination basic |
| 91 | /// block has <= 5 instructions in it, not counting PHI nodes. In practice, |
| 92 | /// since one of these is a terminator instruction, this means that we will add |
| 93 | /// up to 4 instructions to the new block. |
| 94 | /// |
| 95 | /// We don't count PHI nodes in the count since they will be removed when the |
| 96 | /// contents of the block are copied over. |
| 97 | /// |
Evan Cheng | 66ae104 | 2008-05-16 07:55:50 +0000 | [diff] [blame] | 98 | bool TailDup::shouldEliminateUnconditionalBranch(TerminatorInst *TI, |
| 99 | unsigned Threshold) { |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 100 | BranchInst *BI = dyn_cast<BranchInst>(TI); |
| 101 | if (!BI || !BI->isUnconditional()) return false; // Not an uncond branch! |
| 102 | |
| 103 | BasicBlock *Dest = BI->getSuccessor(0); |
| 104 | if (Dest == BI->getParent()) return false; // Do not loop infinitely! |
| 105 | |
Chris Lattner | 00f185f | 2003-07-23 03:32:41 +0000 | [diff] [blame] | 106 | // Do not inline a block if we will just get another branch to the same block! |
Chris Lattner | 4bebf08 | 2004-03-16 19:45:22 +0000 | [diff] [blame] | 107 | TerminatorInst *DTI = Dest->getTerminator(); |
| 108 | if (BranchInst *DBI = dyn_cast<BranchInst>(DTI)) |
Chris Lattner | 00f185f | 2003-07-23 03:32:41 +0000 | [diff] [blame] | 109 | if (DBI->isUnconditional() && DBI->getSuccessor(0) == Dest) |
| 110 | return false; // Do not loop infinitely! |
| 111 | |
Chris Lattner | 24ad00d | 2004-03-16 23:29:09 +0000 | [diff] [blame] | 112 | // FIXME: DemoteRegToStack cannot yet demote invoke instructions to the stack, |
| 113 | // because doing so would require breaking critical edges. This should be |
| 114 | // fixed eventually. |
| 115 | if (!DTI->use_empty()) |
| 116 | return false; |
| 117 | |
Devang Patel | 8dbd9ad | 2008-05-15 18:04:29 +0000 | [diff] [blame] | 118 | // Do not bother with blocks with only a single predecessor: simplify |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 119 | // CFG will fold these two blocks together! |
Devang Patel | 8dbd9ad | 2008-05-15 18:04:29 +0000 | [diff] [blame] | 120 | pred_iterator PI = pred_begin(Dest), PE = pred_end(Dest); |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 121 | ++PI; |
| 122 | if (PI == PE) return false; // Exactly one predecessor! |
| 123 | |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 124 | BasicBlock::iterator I = Dest->getFirstNonPHI(); |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 125 | |
Chris Lattner | b9df9b4 | 2004-11-22 17:23:57 +0000 | [diff] [blame] | 126 | for (unsigned Size = 0; I != Dest->end(); ++I) { |
| 127 | if (Size == Threshold) return false; // The block is too large. |
Chris Lattner | 0647ebf | 2007-11-04 06:37:55 +0000 | [diff] [blame] | 128 | |
| 129 | // Don't tail duplicate call instructions. They are very large compared to |
| 130 | // other instructions. |
| 131 | if (isa<CallInst>(I) || isa<InvokeInst>(I)) return false; |
Evan Cheng | 66ae104 | 2008-05-16 07:55:50 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 68b1bbb | 2009-09-27 21:31:39 +0000 | [diff] [blame] | 133 | // Also alloca and malloc. |
Victor Hernandez | 7b929da | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 134 | if (isa<AllocaInst>(I)) return false; |
Evan Cheng | 66ae104 | 2008-05-16 07:55:50 +0000 | [diff] [blame] | 135 | |
| 136 | // Some vector instructions can expand into a number of instructions. |
| 137 | if (isa<ShuffleVectorInst>(I) || isa<ExtractElementInst>(I) || |
| 138 | isa<InsertElementInst>(I)) return false; |
Chris Lattner | 0647ebf | 2007-11-04 06:37:55 +0000 | [diff] [blame] | 139 | |
Chris Lattner | b9df9b4 | 2004-11-22 17:23:57 +0000 | [diff] [blame] | 140 | // Only count instructions that are not debugger intrinsics. |
| 141 | if (!isa<DbgInfoIntrinsic>(I)) ++Size; |
| 142 | } |
Chris Lattner | 4bebf08 | 2004-03-16 19:45:22 +0000 | [diff] [blame] | 143 | |
| 144 | // Do not tail duplicate a block that has thousands of successors into a block |
| 145 | // with a single successor if the block has many other predecessors. This can |
| 146 | // cause an N^2 explosion in CFG edges (and PHI node entries), as seen in |
| 147 | // cases that have a large number of indirect gotos. |
Chris Lattner | 7e54a01 | 2004-11-01 07:05:07 +0000 | [diff] [blame] | 148 | unsigned NumSuccs = DTI->getNumSuccessors(); |
| 149 | if (NumSuccs > 8) { |
| 150 | unsigned TooMany = 128; |
| 151 | if (NumSuccs >= TooMany) return false; |
| 152 | TooMany = TooMany/NumSuccs; |
| 153 | for (; PI != PE; ++PI) |
| 154 | if (TooMany-- == 0) return false; |
| 155 | } |
Chris Lattner | e99c623 | 2006-09-07 21:30:15 +0000 | [diff] [blame] | 156 | |
Dale Johannesen | 72997fe | 2008-05-13 20:06:43 +0000 | [diff] [blame] | 157 | // If this unconditional branch is a fall-through, be careful about |
Chris Lattner | e99c623 | 2006-09-07 21:30:15 +0000 | [diff] [blame] | 158 | // tail duplicating it. In particular, we don't want to taildup it if the |
| 159 | // original block will still be there after taildup is completed: doing so |
| 160 | // would eliminate the fall-through, requiring unconditional branches. |
| 161 | Function::iterator DestI = Dest; |
| 162 | if (&*--DestI == BI->getParent()) { |
| 163 | // The uncond branch is a fall-through. Tail duplication of the block is |
| 164 | // will eliminate the fall-through-ness and end up cloning the terminator |
| 165 | // at the end of the Dest block. Since the original Dest block will |
| 166 | // continue to exist, this means that one or the other will not be able to |
| 167 | // fall through. One typical example that this helps with is code like: |
| 168 | // if (a) |
| 169 | // foo(); |
| 170 | // if (b) |
| 171 | // foo(); |
| 172 | // Cloning the 'if b' block into the end of the first foo block is messy. |
Chris Lattner | dfa1af0 | 2006-09-10 18:17:58 +0000 | [diff] [blame] | 173 | |
| 174 | // The messy case is when the fall-through block falls through to other |
| 175 | // blocks. This is what we would be preventing if we cloned the block. |
| 176 | DestI = Dest; |
| 177 | if (++DestI != Dest->getParent()->end()) { |
| 178 | BasicBlock *DestSucc = DestI; |
| 179 | // If any of Dest's successors are fall-throughs, don't do this xform. |
| 180 | for (succ_iterator SI = succ_begin(Dest), SE = succ_end(Dest); |
| 181 | SI != SE; ++SI) |
| 182 | if (*SI == DestSucc) |
| 183 | return false; |
| 184 | } |
Chris Lattner | e99c623 | 2006-09-07 21:30:15 +0000 | [diff] [blame] | 185 | } |
Chris Lattner | 4bebf08 | 2004-03-16 19:45:22 +0000 | [diff] [blame] | 186 | |
Dale Johannesen | 72997fe | 2008-05-13 20:06:43 +0000 | [diff] [blame] | 187 | // Finally, check that we haven't redirected to this target block earlier; |
| 188 | // there are cases where we loop forever if we don't check this (PR 2323). |
| 189 | if (!CycleDetector.insert(Dest)) |
| 190 | return false; |
| 191 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 192 | return true; |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 195 | /// FindObviousSharedDomOf - We know there is a branch from SrcBlock to |
| 196 | /// DestBlock, and that SrcBlock is not the only predecessor of DstBlock. If we |
| 197 | /// can find a predecessor of SrcBlock that is a dominator of both SrcBlock and |
| 198 | /// DstBlock, return it. |
| 199 | static BasicBlock *FindObviousSharedDomOf(BasicBlock *SrcBlock, |
| 200 | BasicBlock *DstBlock) { |
| 201 | // SrcBlock must have a single predecessor. |
| 202 | pred_iterator PI = pred_begin(SrcBlock), PE = pred_end(SrcBlock); |
| 203 | if (PI == PE || ++PI != PE) return 0; |
| 204 | |
| 205 | BasicBlock *SrcPred = *pred_begin(SrcBlock); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 206 | |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 207 | // Look at the predecessors of DstBlock. One of them will be SrcBlock. If |
| 208 | // there is only one other pred, get it, otherwise we can't handle it. |
| 209 | PI = pred_begin(DstBlock); PE = pred_end(DstBlock); |
| 210 | BasicBlock *DstOtherPred = 0; |
Gabor Greif | 639e9e4 | 2010-07-12 12:02:10 +0000 | [diff] [blame] | 211 | BasicBlock *P = *PI; |
| 212 | if (P == SrcBlock) { |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 213 | if (++PI == PE) return 0; |
Gabor Greif | e99e077 | 2010-07-12 11:32:39 +0000 | [diff] [blame] | 214 | DstOtherPred = *PI; |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 215 | if (++PI != PE) return 0; |
| 216 | } else { |
Gabor Greif | 639e9e4 | 2010-07-12 12:02:10 +0000 | [diff] [blame] | 217 | DstOtherPred = P; |
Gabor Greif | e99e077 | 2010-07-12 11:32:39 +0000 | [diff] [blame] | 218 | if (++PI == PE || *PI != SrcBlock || ++PI != PE) return 0; |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // We can handle two situations here: "if then" and "if then else" blocks. An |
| 222 | // 'if then' situation is just where DstOtherPred == SrcPred. |
| 223 | if (DstOtherPred == SrcPred) |
| 224 | return SrcPred; |
| 225 | |
| 226 | // Check to see if we have an "if then else" situation, which means that |
| 227 | // DstOtherPred will have a single predecessor and it will be SrcPred. |
| 228 | PI = pred_begin(DstOtherPred); PE = pred_end(DstOtherPred); |
| 229 | if (PI != PE && *PI == SrcPred) { |
| 230 | if (++PI != PE) return 0; // Not a single pred. |
| 231 | return SrcPred; // Otherwise, it's an "if then" situation. Return the if. |
| 232 | } |
| 233 | |
| 234 | // Otherwise, this is something we can't handle. |
| 235 | return 0; |
| 236 | } |
| 237 | |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 238 | |
| 239 | /// eliminateUnconditionalBranch - Clone the instructions from the destination |
| 240 | /// block into the source block, eliminating the specified unconditional branch. |
| 241 | /// If the destination block defines values used by successors of the dest |
| 242 | /// block, we may need to insert PHI nodes. |
| 243 | /// |
| 244 | void TailDup::eliminateUnconditionalBranch(BranchInst *Branch) { |
| 245 | BasicBlock *SourceBlock = Branch->getParent(); |
| 246 | BasicBlock *DestBlock = Branch->getSuccessor(0); |
| 247 | assert(SourceBlock != DestBlock && "Our predicate is broken!"); |
| 248 | |
David Greene | 7439239 | 2010-01-05 01:27:33 +0000 | [diff] [blame] | 249 | DEBUG(dbgs() << "TailDuplication[" << SourceBlock->getParent()->getName() |
Daniel Dunbar | ce63ffb | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 250 | << "]: Eliminating branch: " << *Branch); |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 251 | |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 252 | // See if we can avoid duplicating code by moving it up to a dominator of both |
| 253 | // blocks. |
| 254 | if (BasicBlock *DomBlock = FindObviousSharedDomOf(SourceBlock, DestBlock)) { |
David Greene | 7439239 | 2010-01-05 01:27:33 +0000 | [diff] [blame] | 255 | DEBUG(dbgs() << "Found shared dominator: " << DomBlock->getName() << "\n"); |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 256 | |
| 257 | // If there are non-phi instructions in DestBlock that have no operands |
| 258 | // defined in DestBlock, and if the instruction has no side effects, we can |
| 259 | // move the instruction to DomBlock instead of duplicating it. |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 260 | BasicBlock::iterator BBI = DestBlock->getFirstNonPHI(); |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 261 | while (!isa<TerminatorInst>(BBI)) { |
| 262 | Instruction *I = BBI++; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 263 | |
Eli Friedman | 0b79a77 | 2009-07-17 04:28:42 +0000 | [diff] [blame] | 264 | bool CanHoist = I->isSafeToSpeculativelyExecute() && |
| 265 | !I->mayReadFromMemory(); |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 266 | if (CanHoist) { |
| 267 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
| 268 | if (Instruction *OpI = dyn_cast<Instruction>(I->getOperand(op))) |
| 269 | if (OpI->getParent() == DestBlock || |
| 270 | (isa<InvokeInst>(OpI) && OpI->getParent() == DomBlock)) { |
| 271 | CanHoist = false; |
| 272 | break; |
| 273 | } |
| 274 | if (CanHoist) { |
| 275 | // Remove from DestBlock, move right before the term in DomBlock. |
| 276 | DestBlock->getInstList().remove(I); |
| 277 | DomBlock->getInstList().insert(DomBlock->getTerminator(), I); |
David Greene | 7439239 | 2010-01-05 01:27:33 +0000 | [diff] [blame] | 278 | DEBUG(dbgs() << "Hoisted: " << *I); |
Chris Lattner | c3e903f | 2004-10-06 03:27:37 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Chris Lattner | 24ad00d | 2004-03-16 23:29:09 +0000 | [diff] [blame] | 284 | // Tail duplication can not update SSA properties correctly if the values |
| 285 | // defined in the duplicated tail are used outside of the tail itself. For |
| 286 | // this reason, we spill all values that are used outside of the tail to the |
| 287 | // stack. |
| 288 | for (BasicBlock::iterator I = DestBlock->begin(); I != DestBlock->end(); ++I) |
Chris Lattner | 0cfe85b | 2008-04-20 22:18:22 +0000 | [diff] [blame] | 289 | if (I->isUsedOutsideOfBlock(DestBlock)) { |
| 290 | // We found a use outside of the tail. Create a new stack slot to |
| 291 | // break this inter-block usage pattern. |
Owen Anderson | 50dead0 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 292 | DemoteRegToStack(*I); |
Chris Lattner | 24ad00d | 2004-03-16 23:29:09 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 295 | // We are going to have to map operands from the original block B to the new |
| 296 | // copy of the block B'. If there are PHI nodes in the DestBlock, these PHI |
| 297 | // nodes also define part of this mapping. Loop over these PHI nodes, adding |
| 298 | // them to our mapping. |
Chris Lattner | ea635cd | 2003-06-22 20:25:27 +0000 | [diff] [blame] | 299 | // |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 300 | std::map<Value*, Value*> ValueMapping; |
| 301 | |
| 302 | BasicBlock::iterator BI = DestBlock->begin(); |
| 303 | bool HadPHINodes = isa<PHINode>(BI); |
| 304 | for (; PHINode *PN = dyn_cast<PHINode>(BI); ++BI) |
| 305 | ValueMapping[PN] = PN->getIncomingValueForBlock(SourceBlock); |
| 306 | |
| 307 | // Clone the non-phi instructions of the dest block into the source block, |
| 308 | // keeping track of the mapping... |
| 309 | // |
| 310 | for (; BI != DestBlock->end(); ++BI) { |
Nick Lewycky | 6776064 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 311 | Instruction *New = BI->clone(); |
Owen Anderson | 6bc41e8 | 2008-04-14 17:38:21 +0000 | [diff] [blame] | 312 | New->setName(BI->getName()); |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 313 | SourceBlock->getInstList().push_back(New); |
| 314 | ValueMapping[BI] = New; |
| 315 | } |
| 316 | |
| 317 | // Now that we have built the mapping information and cloned all of the |
| 318 | // instructions (giving us a new terminator, among other things), walk the new |
| 319 | // instructions, rewriting references of old instructions to use new |
| 320 | // instructions. |
| 321 | // |
| 322 | BI = Branch; ++BI; // Get an iterator to the first new instruction |
| 323 | for (; BI != SourceBlock->end(); ++BI) |
Dan Gohman | f530c92 | 2009-07-02 00:17:47 +0000 | [diff] [blame] | 324 | for (unsigned i = 0, e = BI->getNumOperands(); i != e; ++i) { |
| 325 | std::map<Value*, Value*>::const_iterator I = |
| 326 | ValueMapping.find(BI->getOperand(i)); |
| 327 | if (I != ValueMapping.end()) |
| 328 | BI->setOperand(i, I->second); |
| 329 | } |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 330 | |
| 331 | // Next we check to see if any of the successors of DestBlock had PHI nodes. |
| 332 | // If so, we need to add entries to the PHI nodes for SourceBlock now. |
| 333 | for (succ_iterator SI = succ_begin(DestBlock), SE = succ_end(DestBlock); |
| 334 | SI != SE; ++SI) { |
| 335 | BasicBlock *Succ = *SI; |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 336 | for (BasicBlock::iterator PNI = Succ->begin(); isa<PHINode>(PNI); ++PNI) { |
| 337 | PHINode *PN = cast<PHINode>(PNI); |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 338 | // Ok, we have a PHI node. Figure out what the incoming value was for the |
| 339 | // DestBlock. |
| 340 | Value *IV = PN->getIncomingValueForBlock(DestBlock); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 341 | |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 342 | // Remap the value if necessary... |
Dan Gohman | f530c92 | 2009-07-02 00:17:47 +0000 | [diff] [blame] | 343 | std::map<Value*, Value*>::const_iterator I = ValueMapping.find(IV); |
| 344 | if (I != ValueMapping.end()) |
| 345 | IV = I->second; |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 346 | PN->addIncoming(IV, SourceBlock); |
| 347 | } |
| 348 | } |
Chris Lattner | 24ad00d | 2004-03-16 23:29:09 +0000 | [diff] [blame] | 349 | |
| 350 | // Next, remove the old branch instruction, and any PHI node entries that we |
| 351 | // had. |
| 352 | BI = Branch; ++BI; // Get an iterator to the first new instruction |
| 353 | DestBlock->removePredecessor(SourceBlock); // Remove entries in PHI nodes... |
| 354 | SourceBlock->getInstList().erase(Branch); // Destroy the uncond branch... |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 355 | |
| 356 | // Final step: now that we have finished everything up, walk the cloned |
| 357 | // instructions one last time, constant propagating and DCE'ing them, because |
| 358 | // they may not be needed anymore. |
| 359 | // |
Chris Lattner | 1567853 | 2008-11-27 22:56:14 +0000 | [diff] [blame] | 360 | if (HadPHINodes) { |
| 361 | while (BI != SourceBlock->end()) { |
| 362 | Instruction *Inst = BI++; |
| 363 | if (isInstructionTriviallyDead(Inst)) |
| 364 | Inst->eraseFromParent(); |
Duncan Sands | b6133d1 | 2010-11-23 20:26:33 +0000 | [diff] [blame^] | 365 | else if (Value *V = SimplifyInstruction(Inst)) { |
| 366 | Inst->replaceAllUsesWith(V); |
Chris Lattner | 1567853 | 2008-11-27 22:56:14 +0000 | [diff] [blame] | 367 | Inst->eraseFromParent(); |
| 368 | } |
| 369 | } |
| 370 | } |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 7a7bef4 | 2003-06-22 20:10:28 +0000 | [diff] [blame] | 372 | ++NumEliminated; // We just killed a branch! |
| 373 | } |