Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 1 | //===-- LoopUnswitch.cpp - Hoist loop-invariant conditionals in loop ------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass transforms loops that contain branches on loop-invariant conditions |
| 11 | // to have multiple loops. For example, it turns the left into the right code: |
| 12 | // |
| 13 | // for (...) if (lic) |
| 14 | // A for (...) |
| 15 | // if (lic) A; B; C |
| 16 | // B else |
| 17 | // C for (...) |
| 18 | // A; C |
| 19 | // |
| 20 | // This can increase the size of the code exponentially (doubling it every time |
| 21 | // a loop is unswitched) so we only unswitch if the resultant code will be |
| 22 | // smaller than a threshold. |
| 23 | // |
| 24 | // This pass expects LICM to be run before it to hoist invariant conditions out |
| 25 | // of the loop, to make the unswitching opportunity obvious. |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #define DEBUG_TYPE "loop-unswitch" |
| 30 | #include "llvm/Transforms/Scalar.h" |
| 31 | #include "llvm/Constants.h" |
| 32 | #include "llvm/Function.h" |
| 33 | #include "llvm/Instructions.h" |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/LoopInfo.h" |
| 35 | #include "llvm/Transforms/Utils/Cloning.h" |
| 36 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 81be2e9 | 2006-02-10 19:08:15 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | e487abb | 2006-02-09 20:15:48 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Debug.h" |
| 40 | #include "llvm/Support/CommandLine.h" |
Alkis Evlogimenos | 20aa474 | 2004-09-03 18:19:51 +0000 | [diff] [blame] | 41 | #include <algorithm> |
Chris Lattner | dac58ad | 2006-01-22 23:32:06 +0000 | [diff] [blame] | 42 | #include <iostream> |
Chris Lattner | 2f4b898 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 43 | #include <set> |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | |
| 46 | namespace { |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 47 | Statistic<> NumBranches("loop-unswitch", "Number of branches unswitched"); |
| 48 | Statistic<> NumSwitches("loop-unswitch", "Number of switches unswitched"); |
| 49 | Statistic<> NumSelects ("loop-unswitch", "Number of selects unswitched"); |
| 50 | Statistic<> NumTrivial ("loop-unswitch", |
| 51 | "Number of unswitches that are trivial"); |
Chris Lattner | e487abb | 2006-02-09 20:15:48 +0000 | [diff] [blame] | 52 | cl::opt<unsigned> |
| 53 | Threshold("loop-unswitch-threshold", cl::desc("Max loop size to unswitch"), |
| 54 | cl::init(10), cl::Hidden); |
| 55 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 56 | class LoopUnswitch : public FunctionPass { |
| 57 | LoopInfo *LI; // Loop information |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 58 | public: |
| 59 | virtual bool runOnFunction(Function &F); |
| 60 | bool visitLoop(Loop *L); |
| 61 | |
| 62 | /// This transformation requires natural loop information & requires that |
| 63 | /// loop preheaders be inserted into the CFG... |
| 64 | /// |
| 65 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 66 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | f4f5f4e | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 67 | AU.addPreservedID(LoopSimplifyID); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 68 | AU.addRequired<LoopInfo>(); |
| 69 | AU.addPreserved<LoopInfo>(); |
| 70 | } |
| 71 | |
| 72 | private: |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 73 | bool UnswitchIfProfitable(Value *LoopCond, Constant *Val,Loop *L); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 74 | unsigned getLoopUnswitchCost(Loop *L, Value *LIC); |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 75 | void VersionLoop(Value *LIC, Constant *OnVal, |
| 76 | Loop *L, Loop *&Out1, Loop *&Out2); |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 77 | BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To); |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 78 | void RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC,Constant *Val, |
| 79 | bool isEqual); |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 80 | void UnswitchTrivialCondition(Loop *L, Value *Cond, Constant *Val, |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 81 | BasicBlock *ExitBlock); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 82 | }; |
| 83 | RegisterOpt<LoopUnswitch> X("loop-unswitch", "Unswitch loops"); |
| 84 | } |
| 85 | |
Jeff Cohen | f5e58f8 | 2005-01-06 05:47:18 +0000 | [diff] [blame] | 86 | FunctionPass *llvm::createLoopUnswitchPass() { return new LoopUnswitch(); } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 87 | |
| 88 | bool LoopUnswitch::runOnFunction(Function &F) { |
| 89 | bool Changed = false; |
| 90 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 91 | |
| 92 | // Transform all the top-level loops. Copy the loop list so that the child |
| 93 | // can update the loop tree if it needs to delete the loop. |
| 94 | std::vector<Loop*> SubLoops(LI->begin(), LI->end()); |
| 95 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 96 | Changed |= visitLoop(SubLoops[i]); |
| 97 | |
| 98 | return Changed; |
| 99 | } |
| 100 | |
Chris Lattner | 2f4b898 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 102 | /// LoopValuesUsedOutsideLoop - Return true if there are any values defined in |
| 103 | /// the loop that are used by instructions outside of it. |
Chris Lattner | 2f4b898 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 104 | static bool LoopValuesUsedOutsideLoop(Loop *L) { |
| 105 | // We will be doing lots of "loop contains block" queries. Loop::contains is |
| 106 | // linear time, use a set to speed this up. |
| 107 | std::set<BasicBlock*> LoopBlocks; |
| 108 | |
| 109 | for (Loop::block_iterator BB = L->block_begin(), E = L->block_end(); |
| 110 | BB != E; ++BB) |
| 111 | LoopBlocks.insert(*BB); |
| 112 | |
| 113 | for (Loop::block_iterator BB = L->block_begin(), E = L->block_end(); |
| 114 | BB != E; ++BB) { |
| 115 | for (BasicBlock::iterator I = (*BB)->begin(), E = (*BB)->end(); I != E; ++I) |
| 116 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; |
| 117 | ++UI) { |
| 118 | BasicBlock *UserBB = cast<Instruction>(*UI)->getParent(); |
| 119 | if (!LoopBlocks.count(UserBB)) |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 126 | /// FindTrivialLoopExitBlock - We know that we have a branch from the loop |
| 127 | /// header to the specified latch block. See if one of the successors of the |
| 128 | /// latch block is an exit, and if so what block it is. |
| 129 | static BasicBlock *FindTrivialLoopExitBlock(Loop *L, BasicBlock *Latch) { |
| 130 | BasicBlock *Header = L->getHeader(); |
| 131 | BranchInst *LatchBranch = dyn_cast<BranchInst>(Latch->getTerminator()); |
| 132 | if (!LatchBranch || !LatchBranch->isConditional()) return 0; |
| 133 | |
| 134 | // Simple case, the latch block is a conditional branch. The target that |
| 135 | // doesn't go to the loop header is our block if it is not in the loop. |
| 136 | if (LatchBranch->getSuccessor(0) == Header) { |
| 137 | if (L->contains(LatchBranch->getSuccessor(1))) return false; |
| 138 | return LatchBranch->getSuccessor(1); |
| 139 | } else { |
| 140 | assert(LatchBranch->getSuccessor(1) == Header); |
| 141 | if (L->contains(LatchBranch->getSuccessor(0))) return false; |
| 142 | return LatchBranch->getSuccessor(0); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 147 | /// IsTrivialUnswitchCondition - Check to see if this unswitch condition is |
| 148 | /// trivial: that is, that the condition controls whether or not the loop does |
| 149 | /// anything at all. If this is a trivial condition, unswitching produces no |
| 150 | /// code duplications (equivalently, it produces a simpler loop and a new empty |
| 151 | /// loop, which gets deleted). |
| 152 | /// |
| 153 | /// If this is a trivial condition, return ConstantBool::True if the loop body |
| 154 | /// runs when the condition is true, False if the loop body executes when the |
| 155 | /// condition is false. Otherwise, return null to indicate a complex condition. |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 156 | static bool IsTrivialUnswitchCondition(Loop *L, Value *Cond, |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 157 | Constant **Val = 0, |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 158 | BasicBlock **LoopExit = 0) { |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 159 | BasicBlock *Header = L->getHeader(); |
| 160 | BranchInst *HeaderTerm = dyn_cast<BranchInst>(Header->getTerminator()); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 161 | |
| 162 | // If the header block doesn't end with a conditional branch on Cond, we can't |
| 163 | // handle it. |
| 164 | if (!HeaderTerm || !HeaderTerm->isConditional() || |
| 165 | HeaderTerm->getCondition() != Cond) |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 166 | return false; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 167 | |
| 168 | // Check to see if the conditional branch goes to the latch block. If not, |
| 169 | // it's not trivial. This also determines the value of Cond that will execute |
| 170 | // the loop. |
| 171 | BasicBlock *Latch = L->getLoopLatch(); |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 172 | if (HeaderTerm->getSuccessor(1) == Latch) { |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 173 | if (Val) *Val = ConstantBool::True; |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 174 | } else if (HeaderTerm->getSuccessor(0) == Latch) |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 175 | if (Val) *Val = ConstantBool::False; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 176 | else |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 177 | return false; // Doesn't branch to latch block. |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 178 | |
| 179 | // The latch block must end with a conditional branch where one edge goes to |
| 180 | // the header (this much we know) and one edge goes OUT of the loop. |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 181 | BasicBlock *LoopExitBlock = FindTrivialLoopExitBlock(L, Latch); |
| 182 | if (!LoopExitBlock) return 0; |
| 183 | if (LoopExit) *LoopExit = LoopExitBlock; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 184 | |
| 185 | // We already know that nothing uses any scalar values defined inside of this |
| 186 | // loop. As such, we just have to check to see if this loop will execute any |
| 187 | // side-effecting instructions (e.g. stores, calls, volatile loads) in the |
| 188 | // part of the loop that the code *would* execute. |
| 189 | for (BasicBlock::iterator I = Header->begin(), E = Header->end(); I != E; ++I) |
| 190 | if (I->mayWriteToMemory()) |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 191 | return false; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 192 | for (BasicBlock::iterator I = Latch->begin(), E = Latch->end(); I != E; ++I) |
| 193 | if (I->mayWriteToMemory()) |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 194 | return false; |
| 195 | return true; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /// getLoopUnswitchCost - Return the cost (code size growth) that will happen if |
| 199 | /// we choose to unswitch the specified loop on the specified value. |
| 200 | /// |
| 201 | unsigned LoopUnswitch::getLoopUnswitchCost(Loop *L, Value *LIC) { |
| 202 | // If the condition is trivial, always unswitch. There is no code growth for |
| 203 | // this case. |
| 204 | if (IsTrivialUnswitchCondition(L, LIC)) |
| 205 | return 0; |
| 206 | |
| 207 | unsigned Cost = 0; |
| 208 | // FIXME: this is brain dead. It should take into consideration code |
| 209 | // shrinkage. |
| 210 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 211 | I != E; ++I) { |
| 212 | BasicBlock *BB = *I; |
| 213 | // Do not include empty blocks in the cost calculation. This happen due to |
| 214 | // loop canonicalization and will be removed. |
| 215 | if (BB->begin() == BasicBlock::iterator(BB->getTerminator())) |
| 216 | continue; |
| 217 | |
| 218 | // Count basic blocks. |
| 219 | ++Cost; |
| 220 | } |
| 221 | |
| 222 | return Cost; |
| 223 | } |
| 224 | |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 225 | /// FindLIVLoopCondition - Cond is a condition that occurs in L. If it is |
| 226 | /// invariant in the loop, or has an invariant piece, return the invariant. |
| 227 | /// Otherwise, return null. |
| 228 | static Value *FindLIVLoopCondition(Value *Cond, Loop *L, bool &Changed) { |
| 229 | // Constants should be folded, not unswitched on! |
| 230 | if (isa<Constant>(Cond)) return false; |
| 231 | |
| 232 | // TODO: Handle: br (VARIANT|INVARIANT). |
| 233 | // TODO: Hoist simple expressions out of loops. |
| 234 | if (L->isLoopInvariant(Cond)) return Cond; |
| 235 | |
| 236 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Cond)) |
| 237 | if (BO->getOpcode() == Instruction::And || |
| 238 | BO->getOpcode() == Instruction::Or) { |
| 239 | // If either the left or right side is invariant, we can unswitch on this, |
| 240 | // which will cause the branch to go away in one loop and the condition to |
| 241 | // simplify in the other one. |
| 242 | if (Value *LHS = FindLIVLoopCondition(BO->getOperand(0), L, Changed)) |
| 243 | return LHS; |
| 244 | if (Value *RHS = FindLIVLoopCondition(BO->getOperand(1), L, Changed)) |
| 245 | return RHS; |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 251 | bool LoopUnswitch::visitLoop(Loop *L) { |
| 252 | bool Changed = false; |
| 253 | |
| 254 | // Recurse through all subloops before we process this loop. Copy the loop |
| 255 | // list so that the child can update the loop tree if it needs to delete the |
| 256 | // loop. |
| 257 | std::vector<Loop*> SubLoops(L->begin(), L->end()); |
| 258 | for (unsigned i = 0, e = SubLoops.size(); i != e; ++i) |
| 259 | Changed |= visitLoop(SubLoops[i]); |
| 260 | |
| 261 | // Loop over all of the basic blocks in the loop. If we find an interior |
| 262 | // block that is branching on a loop-invariant condition, we can unswitch this |
| 263 | // loop. |
| 264 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 265 | I != E; ++I) { |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 266 | TerminatorInst *TI = (*I)->getTerminator(); |
| 267 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 268 | // If this isn't branching on an invariant condition, we can't unswitch |
| 269 | // it. |
| 270 | if (BI->isConditional()) { |
| 271 | // See if this, or some part of it, is loop invariant. If so, we can |
| 272 | // unswitch on it if we desire. |
| 273 | Value *LoopCond = FindLIVLoopCondition(BI->getCondition(), L, Changed); |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 274 | if (LoopCond && UnswitchIfProfitable(LoopCond, ConstantBool::True, L)) { |
| 275 | ++NumBranches; |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 276 | return true; |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 277 | } |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 278 | } |
| 279 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 280 | Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), L, Changed); |
| 281 | if (LoopCond && SI->getNumCases() > 1) { |
| 282 | // Find a value to unswitch on: |
| 283 | // FIXME: this should chose the most expensive case! |
| 284 | Constant *UnswitchVal = SI->getCaseValue(1); |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 285 | if (UnswitchIfProfitable(LoopCond, UnswitchVal, L)) { |
| 286 | ++NumSwitches; |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 287 | return true; |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 288 | } |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | // Scan the instructions to check for unswitchable values. |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 293 | for (BasicBlock::iterator BBI = (*I)->begin(), E = (*I)->end(); |
| 294 | BBI != E; ++BBI) |
| 295 | if (SelectInst *SI = dyn_cast<SelectInst>(BBI)) { |
| 296 | Value *LoopCond = FindLIVLoopCondition(SI->getCondition(), L, Changed); |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 297 | if (LoopCond && UnswitchIfProfitable(LoopCond, ConstantBool::True, L)) { |
| 298 | ++NumSelects; |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 299 | return true; |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 300 | } |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 301 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 302 | } |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 304 | return Changed; |
| 305 | } |
| 306 | |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 307 | /// UnswitchIfProfitable - We have found that we can unswitch L when |
| 308 | /// LoopCond == Val to simplify the loop. If we decide that this is profitable, |
| 309 | /// unswitch the loop, reprocess the pieces, then return true. |
| 310 | bool LoopUnswitch::UnswitchIfProfitable(Value *LoopCond, Constant *Val,Loop *L){ |
| 311 | // Check to see if it would be profitable to unswitch this loop. |
| 312 | if (getLoopUnswitchCost(L, LoopCond) > Threshold) { |
| 313 | // FIXME: this should estimate growth by the amount of code shared by the |
| 314 | // resultant unswitched loops. |
| 315 | // |
| 316 | DEBUG(std::cerr << "NOT unswitching loop %" |
| 317 | << L->getHeader()->getName() << ", cost too high: " |
| 318 | << L->getBlocks().size() << "\n"); |
| 319 | return false; |
| 320 | } |
| 321 | |
| 322 | // If this loop has live-out values, we can't unswitch it. We need something |
| 323 | // like loop-closed SSA form in order to know how to insert PHI nodes for |
| 324 | // these values. |
| 325 | if (LoopValuesUsedOutsideLoop(L)) { |
| 326 | DEBUG(std::cerr << "NOT unswitching loop %" << L->getHeader()->getName() |
| 327 | << ", a loop value is used outside loop!\n"); |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | //std::cerr << "BEFORE:\n"; LI->dump(); |
| 332 | Loop *NewLoop1 = 0, *NewLoop2 = 0; |
| 333 | |
| 334 | // If this is a trivial condition to unswitch (which results in no code |
| 335 | // duplication), do it now. |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 336 | Constant *CondVal; |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 337 | BasicBlock *ExitBlock; |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 338 | if (IsTrivialUnswitchCondition(L, LoopCond, &CondVal, &ExitBlock)){ |
| 339 | UnswitchTrivialCondition(L, LoopCond, CondVal, ExitBlock); |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 340 | NewLoop1 = L; |
| 341 | } else { |
| 342 | VersionLoop(LoopCond, Val, L, NewLoop1, NewLoop2); |
| 343 | } |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 344 | |
| 345 | //std::cerr << "AFTER:\n"; LI->dump(); |
| 346 | |
| 347 | // Try to unswitch each of our new loops now! |
| 348 | if (NewLoop1) visitLoop(NewLoop1); |
| 349 | if (NewLoop2) visitLoop(NewLoop2); |
| 350 | return true; |
| 351 | } |
| 352 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 353 | BasicBlock *LoopUnswitch::SplitEdge(BasicBlock *BB, BasicBlock *Succ) { |
| 354 | TerminatorInst *LatchTerm = BB->getTerminator(); |
| 355 | unsigned SuccNum = 0; |
| 356 | for (unsigned i = 0, e = LatchTerm->getNumSuccessors(); ; ++i) { |
| 357 | assert(i != e && "Didn't find edge?"); |
| 358 | if (LatchTerm->getSuccessor(i) == Succ) { |
| 359 | SuccNum = i; |
| 360 | break; |
| 361 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 362 | } |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 363 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 364 | // If this is a critical edge, let SplitCriticalEdge do it. |
| 365 | if (SplitCriticalEdge(BB->getTerminator(), SuccNum, this)) |
| 366 | return LatchTerm->getSuccessor(SuccNum); |
| 367 | |
| 368 | // If the edge isn't critical, then BB has a single successor or Succ has a |
| 369 | // single pred. Split the block. |
| 370 | BasicBlock *BlockToSplit; |
| 371 | BasicBlock::iterator SplitPoint; |
| 372 | if (BasicBlock *SP = Succ->getSinglePredecessor()) { |
| 373 | // If the successor only has a single pred, split the top of the successor |
| 374 | // block. |
| 375 | assert(SP == BB && "CFG broken"); |
| 376 | BlockToSplit = Succ; |
| 377 | SplitPoint = Succ->begin(); |
| 378 | } else { |
| 379 | // Otherwise, if BB has a single successor, split it at the bottom of the |
| 380 | // block. |
| 381 | assert(BB->getTerminator()->getNumSuccessors() == 1 && |
| 382 | "Should have a single succ!"); |
| 383 | BlockToSplit = BB; |
| 384 | SplitPoint = BB->getTerminator(); |
| 385 | } |
| 386 | |
| 387 | BasicBlock *New = |
| 388 | BlockToSplit->splitBasicBlock(SplitPoint, |
| 389 | BlockToSplit->getName()+".tail"); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 390 | // New now lives in whichever loop that BB used to. |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 391 | if (Loop *L = LI->getLoopFor(BlockToSplit)) |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 392 | L->addBasicBlockToLoop(New, *LI); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 393 | return New; |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 394 | } |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 396 | |
| 397 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 398 | // RemapInstruction - Convert the instruction operands from referencing the |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 399 | // current values into those specified by ValueMap. |
| 400 | // |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 401 | static inline void RemapInstruction(Instruction *I, |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 402 | std::map<const Value *, Value*> &ValueMap) { |
| 403 | for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { |
| 404 | Value *Op = I->getOperand(op); |
| 405 | std::map<const Value *, Value*>::iterator It = ValueMap.find(Op); |
| 406 | if (It != ValueMap.end()) Op = It->second; |
| 407 | I->setOperand(op, Op); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /// CloneLoop - Recursively clone the specified loop and all of its children, |
| 412 | /// mapping the blocks with the specified map. |
| 413 | static Loop *CloneLoop(Loop *L, Loop *PL, std::map<const Value*, Value*> &VM, |
| 414 | LoopInfo *LI) { |
| 415 | Loop *New = new Loop(); |
| 416 | |
| 417 | if (PL) |
| 418 | PL->addChildLoop(New); |
| 419 | else |
| 420 | LI->addTopLevelLoop(New); |
| 421 | |
| 422 | // Add all of the blocks in L to the new loop. |
| 423 | for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); |
| 424 | I != E; ++I) |
| 425 | if (LI->getLoopFor(*I) == L) |
| 426 | New->addBasicBlockToLoop(cast<BasicBlock>(VM[*I]), *LI); |
| 427 | |
| 428 | // Add all of the subloops to the new loop. |
| 429 | for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) |
| 430 | CloneLoop(*I, New, VM, LI); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 432 | return New; |
| 433 | } |
| 434 | |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 435 | /// EmitPreheaderBranchOnCondition - Emit a conditional branch on two values |
| 436 | /// if LIC == Val, branch to TrueDst, otherwise branch to FalseDest. Insert the |
| 437 | /// code immediately before InsertPt. |
| 438 | static void EmitPreheaderBranchOnCondition(Value *LIC, Constant *Val, |
| 439 | BasicBlock *TrueDest, |
| 440 | BasicBlock *FalseDest, |
| 441 | Instruction *InsertPt) { |
| 442 | // Insert a conditional branch on LIC to the two preheaders. The original |
| 443 | // code is the true version and the new code is the false version. |
| 444 | Value *BranchVal = LIC; |
Chris Lattner | 3fdde11 | 2006-02-15 19:05:52 +0000 | [diff] [blame^] | 445 | if (!isa<ConstantBool>(Val)) { |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 446 | BranchVal = BinaryOperator::createSetEQ(LIC, Val, "tmp", InsertPt); |
| 447 | } else if (Val != ConstantBool::True) { |
| 448 | // We want to enter the new loop when the condition is true. |
| 449 | std::swap(TrueDest, FalseDest); |
| 450 | } |
| 451 | |
| 452 | // Insert the new branch. |
| 453 | new BranchInst(TrueDest, FalseDest, BranchVal, InsertPt); |
| 454 | } |
| 455 | |
| 456 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 457 | /// UnswitchTrivialCondition - Given a loop that has a trivial unswitchable |
| 458 | /// condition in it (a cond branch from its header block to its latch block, |
| 459 | /// where the path through the loop that doesn't execute its body has no |
| 460 | /// side-effects), unswitch it. This doesn't involve any code duplication, just |
| 461 | /// moving the conditional branch outside of the loop and updating loop info. |
| 462 | void LoopUnswitch::UnswitchTrivialCondition(Loop *L, Value *Cond, |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 463 | Constant *Val, |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 464 | BasicBlock *ExitBlock) { |
Chris Lattner | 4d1ca94 | 2006-02-10 01:36:35 +0000 | [diff] [blame] | 465 | DEBUG(std::cerr << "loop-unswitch: Trivial-Unswitch loop %" |
| 466 | << L->getHeader()->getName() << " [" << L->getBlocks().size() |
| 467 | << " blocks] in Function " << L->getHeader()->getParent()->getName() |
| 468 | << " on cond:" << *Cond << "\n"); |
| 469 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 470 | // First step, split the preheader, so that we know that there is a safe place |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 471 | // to insert the conditional branch. We will change 'OrigPH' to have a |
| 472 | // conditional branch on Cond. |
| 473 | BasicBlock *OrigPH = L->getLoopPreheader(); |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 474 | BasicBlock *NewPH = SplitEdge(OrigPH, L->getHeader()); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 475 | |
| 476 | // Now that we have a place to insert the conditional branch, create a place |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 477 | // to branch to: this is the exit block out of the loop that we should |
| 478 | // short-circuit to. |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 479 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 480 | // Split this edge now, so that the loop maintains its exit block. |
Chris Lattner | dd3ee6d | 2006-02-10 02:01:22 +0000 | [diff] [blame] | 481 | assert(!L->contains(ExitBlock) && "Exit block is in the loop?"); |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 482 | BasicBlock *NewExit = SplitEdge(L->getLoopLatch(), ExitBlock); |
| 483 | assert(NewExit != ExitBlock && "Edge not split!"); |
| 484 | |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 485 | // Okay, now we have a position to branch from and a position to branch to, |
| 486 | // insert the new conditional branch. |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 487 | EmitPreheaderBranchOnCondition(Cond, Val, NewPH, NewExit, |
| 488 | OrigPH->getTerminator()); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 489 | OrigPH->getTerminator()->eraseFromParent(); |
| 490 | |
| 491 | // Now that we know that the loop is never entered when this condition is a |
| 492 | // particular value, rewrite the loop with this info. We know that this will |
| 493 | // at least eliminate the old branch. |
Chris Lattner | 6d9d13d | 2006-02-15 01:44:42 +0000 | [diff] [blame] | 494 | RewriteLoopBodyWithConditionConstant(L, Cond, Val, true); |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 495 | ++NumTrivial; |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 496 | } |
| 497 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 498 | |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 499 | /// VersionLoop - We determined that the loop is profitable to unswitch when LIC |
| 500 | /// equal Val. Split it into loop versions and test the condition outside of |
| 501 | /// either loop. Return the loops created as Out1/Out2. |
| 502 | void LoopUnswitch::VersionLoop(Value *LIC, Constant *Val, Loop *L, |
| 503 | Loop *&Out1, Loop *&Out2) { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 504 | Function *F = L->getHeader()->getParent(); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 506 | DEBUG(std::cerr << "loop-unswitch: Unswitching loop %" |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 507 | << L->getHeader()->getName() << " [" << L->getBlocks().size() |
| 508 | << " blocks] in Function " << F->getName() |
| 509 | << " when '" << *Val << "' == " << *LIC << "\n"); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 510 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 511 | // LoopBlocks contains all of the basic blocks of the loop, including the |
| 512 | // preheader of the loop, the body of the loop, and the exit blocks of the |
| 513 | // loop, in that order. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 514 | std::vector<BasicBlock*> LoopBlocks; |
| 515 | |
| 516 | // First step, split the preheader and exit blocks, and add these blocks to |
| 517 | // the LoopBlocks list. |
| 518 | BasicBlock *OrigPreheader = L->getLoopPreheader(); |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 519 | LoopBlocks.push_back(SplitEdge(OrigPreheader, L->getHeader())); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 520 | |
| 521 | // We want the loop to come after the preheader, but before the exit blocks. |
| 522 | LoopBlocks.insert(LoopBlocks.end(), L->block_begin(), L->block_end()); |
| 523 | |
| 524 | std::vector<BasicBlock*> ExitBlocks; |
| 525 | L->getExitBlocks(ExitBlocks); |
| 526 | std::sort(ExitBlocks.begin(), ExitBlocks.end()); |
| 527 | ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()), |
| 528 | ExitBlocks.end()); |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 529 | |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 530 | // Split all of the edges from inside the loop to their exit blocks. This |
| 531 | // unswitching trivial: no phi nodes to update. |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 532 | unsigned NumBlocks = L->getBlocks().size(); |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 533 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 534 | BasicBlock *ExitBlock = ExitBlocks[i]; |
| 535 | std::vector<BasicBlock*> Preds(pred_begin(ExitBlock), pred_end(ExitBlock)); |
| 536 | |
| 537 | for (unsigned j = 0, e = Preds.size(); j != e; ++j) { |
| 538 | assert(L->contains(Preds[j]) && |
| 539 | "All preds of loop exit blocks must be the same loop!"); |
| 540 | SplitEdge(Preds[j], ExitBlock); |
| 541 | } |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 542 | } |
Chris Lattner | b2bc315 | 2006-02-10 23:16:39 +0000 | [diff] [blame] | 543 | |
| 544 | // The exit blocks may have been changed due to edge splitting, recompute. |
| 545 | ExitBlocks.clear(); |
| 546 | L->getExitBlocks(ExitBlocks); |
| 547 | std::sort(ExitBlocks.begin(), ExitBlocks.end()); |
| 548 | ExitBlocks.erase(std::unique(ExitBlocks.begin(), ExitBlocks.end()), |
| 549 | ExitBlocks.end()); |
| 550 | |
| 551 | // Add exit blocks to the loop blocks. |
| 552 | LoopBlocks.insert(LoopBlocks.end(), ExitBlocks.begin(), ExitBlocks.end()); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 553 | |
| 554 | // Next step, clone all of the basic blocks that make up the loop (including |
| 555 | // the loop preheader and exit blocks), keeping track of the mapping between |
| 556 | // the instructions and blocks. |
| 557 | std::vector<BasicBlock*> NewBlocks; |
| 558 | NewBlocks.reserve(LoopBlocks.size()); |
| 559 | std::map<const Value*, Value*> ValueMap; |
| 560 | for (unsigned i = 0, e = LoopBlocks.size(); i != e; ++i) { |
Chris Lattner | 3dd4c40 | 2006-02-14 01:01:41 +0000 | [diff] [blame] | 561 | BasicBlock *New = CloneBasicBlock(LoopBlocks[i], ValueMap, ".us", F); |
| 562 | NewBlocks.push_back(New); |
| 563 | ValueMap[LoopBlocks[i]] = New; // Keep the BB mapping. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | // Splice the newly inserted blocks into the function right before the |
| 567 | // original preheader. |
| 568 | F->getBasicBlockList().splice(LoopBlocks[0], F->getBasicBlockList(), |
| 569 | NewBlocks[0], F->end()); |
| 570 | |
| 571 | // Now we create the new Loop object for the versioned loop. |
| 572 | Loop *NewLoop = CloneLoop(L, L->getParentLoop(), ValueMap, LI); |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 573 | Loop *ParentLoop = L->getParentLoop(); |
| 574 | if (ParentLoop) { |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 575 | // Make sure to add the cloned preheader and exit blocks to the parent loop |
| 576 | // as well. |
Chris Lattner | e825593 | 2006-02-10 23:26:14 +0000 | [diff] [blame] | 577 | ParentLoop->addBasicBlockToLoop(NewBlocks[0], *LI); |
| 578 | } |
| 579 | |
| 580 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 581 | BasicBlock *NewExit = cast<BasicBlock>(ValueMap[ExitBlocks[i]]); |
| 582 | if (ParentLoop) |
| 583 | ParentLoop->addBasicBlockToLoop(cast<BasicBlock>(NewExit), *LI); |
| 584 | |
| 585 | assert(NewExit->getTerminator()->getNumSuccessors() == 1 && |
| 586 | "Exit block should have been split to have one successor!"); |
| 587 | BasicBlock *ExitSucc = NewExit->getTerminator()->getSuccessor(0); |
| 588 | |
| 589 | // If the successor of the exit block had PHI nodes, add an entry for |
| 590 | // NewExit. |
| 591 | PHINode *PN; |
| 592 | for (BasicBlock::iterator I = ExitSucc->begin(); |
| 593 | (PN = dyn_cast<PHINode>(I)); ++I) { |
| 594 | Value *V = PN->getIncomingValueForBlock(ExitBlocks[i]); |
| 595 | std::map<const Value *, Value*>::iterator It = ValueMap.find(V); |
| 596 | if (It != ValueMap.end()) V = It->second; |
| 597 | PN->addIncoming(V, NewExit); |
| 598 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | // Rewrite the code to refer to itself. |
| 602 | for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i) |
| 603 | for (BasicBlock::iterator I = NewBlocks[i]->begin(), |
| 604 | E = NewBlocks[i]->end(); I != E; ++I) |
| 605 | RemapInstruction(I, ValueMap); |
Chris Lattner | 2f4b898 | 2006-02-09 19:14:52 +0000 | [diff] [blame] | 606 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 607 | // Rewrite the original preheader to select between versions of the loop. |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 608 | BranchInst *OldBR = cast<BranchInst>(OrigPreheader->getTerminator()); |
| 609 | assert(OldBR->isUnconditional() && OldBR->getSuccessor(0) == LoopBlocks[0] && |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 610 | "Preheader splitting did not work correctly!"); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 611 | |
Chris Lattner | fed5d9d | 2006-02-15 00:07:43 +0000 | [diff] [blame] | 612 | // Emit the new branch that selects between the two versions of this loop. |
| 613 | EmitPreheaderBranchOnCondition(LIC, Val, NewBlocks[0], LoopBlocks[0], OldBR); |
| 614 | OldBR->eraseFromParent(); |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 615 | |
| 616 | // Now we rewrite the original code to know that the condition is true and the |
| 617 | // new code to know that the condition is false. |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 618 | RewriteLoopBodyWithConditionConstant(L, LIC, Val, false); |
| 619 | RewriteLoopBodyWithConditionConstant(NewLoop, LIC, Val, true); |
Chris Lattner | f4f5f4e | 2006-02-09 22:15:42 +0000 | [diff] [blame] | 620 | Out1 = L; |
| 621 | Out2 = NewLoop; |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 624 | // RewriteLoopBodyWithConditionConstant - We know either that the value LIC has |
| 625 | // the value specified by Val in the specified loop, or we know it does NOT have |
| 626 | // that value. Rewrite any uses of LIC or of properties correlated to it. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 627 | void LoopUnswitch::RewriteLoopBodyWithConditionConstant(Loop *L, Value *LIC, |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 628 | Constant *Val, |
| 629 | bool IsEqual) { |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 630 | assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?"); |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 631 | |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 632 | // FIXME: Support correlated properties, like: |
| 633 | // for (...) |
| 634 | // if (li1 < li2) |
| 635 | // ... |
| 636 | // if (li1 > li2) |
| 637 | // ... |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 638 | |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 639 | // NotVal - If Val is a bool, this contains its inverse. |
| 640 | Constant *NotVal = 0; |
| 641 | if (ConstantBool *CB = dyn_cast<ConstantBool>(Val)) |
| 642 | NotVal = ConstantBool::get(!CB->getValue()); |
| 643 | |
Chris Lattner | 708e1a5 | 2006-02-10 02:30:37 +0000 | [diff] [blame] | 644 | // FOLD boolean conditions (X|LIC), (X&LIC). Fold conditional branches, |
| 645 | // selects, switches. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 646 | std::vector<User*> Users(LIC->use_begin(), LIC->use_end()); |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 647 | |
| 648 | // Haha, this loop could be unswitched. Get it? The unswitch pass could |
| 649 | // unswitch itself. Amazing. |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 650 | for (unsigned i = 0, e = Users.size(); i != e; ++i) |
Chris Lattner | 4c41d49 | 2006-02-10 01:24:09 +0000 | [diff] [blame] | 651 | if (Instruction *U = cast<Instruction>(Users[i])) |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 652 | if (L->contains(U->getParent())) |
Chris Lattner | c235809 | 2006-02-11 00:43:37 +0000 | [diff] [blame] | 653 | if (IsEqual) { |
| 654 | U->replaceUsesOfWith(LIC, Val); |
| 655 | } else if (NotVal) { |
| 656 | U->replaceUsesOfWith(LIC, NotVal); |
| 657 | } else { |
| 658 | // If we know that LIC is not Val, use this info to simplify code. |
| 659 | if (SwitchInst *SI = dyn_cast<SwitchInst>(U)) { |
| 660 | for (unsigned i = 1, e = SI->getNumCases(); i != e; ++i) { |
| 661 | if (SI->getCaseValue(i) == Val) { |
| 662 | // Found a dead case value. Don't remove PHI nodes in the |
| 663 | // successor if they become single-entry, those PHI nodes may |
| 664 | // be in the Users list. |
| 665 | SI->getSuccessor(i)->removePredecessor(SI->getParent(), true); |
| 666 | SI->removeCase(i); |
| 667 | break; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // TODO: We could simplify stuff like X == C. |
| 673 | } |
Chris Lattner | 18f1609 | 2004-04-19 18:07:02 +0000 | [diff] [blame] | 674 | } |