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