LLVM API Documentation
00001 //===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements Loop Index Splitting Pass. This pass handles three 00011 // kinds of loops. 00012 // 00013 // [1] Loop is eliminated when loop body is executed only once. For example, 00014 // for (i = 0; i < N; ++i) { 00015 // if ( i == X) { 00016 // ... 00017 // } 00018 // } 00019 // 00020 // [2] Loop's iteration space is shrunk if loop body is executed for certain 00021 // range only. For example, 00022 // 00023 // for (i = 0; i < N; ++i) { 00024 // if ( i > A && i < B) { 00025 // ... 00026 // } 00027 // } 00028 // is trnasformed to iterators from A to B, if A > 0 and B < N. 00029 // 00030 // [3] Loop is split if the loop body is dominated by an branch. For example, 00031 // 00032 // for (i = LB; i < UB; ++i) { if (i < SV) A; else B; } 00033 // 00034 // is transformed into 00035 // AEV = BSV = SV 00036 // for (i = LB; i < min(UB, AEV); ++i) 00037 // A; 00038 // for (i = max(LB, BSV); i < UB; ++i); 00039 // B; 00040 //===----------------------------------------------------------------------===// 00041 00042 #define DEBUG_TYPE "loop-index-split" 00043 00044 #include "llvm/Transforms/Scalar.h" 00045 #include "llvm/Analysis/LoopPass.h" 00046 #include "llvm/Analysis/ScalarEvolutionExpander.h" 00047 #include "llvm/Analysis/Dominators.h" 00048 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 00049 #include "llvm/Transforms/Utils/Cloning.h" 00050 #include "llvm/Support/Compiler.h" 00051 #include "llvm/ADT/DepthFirstIterator.h" 00052 #include "llvm/ADT/Statistic.h" 00053 00054 using namespace llvm; 00055 00056 STATISTIC(NumIndexSplit, "Number of loop index split"); 00057 STATISTIC(NumIndexSplitRemoved, "Number of loops eliminated by loop index split"); 00058 STATISTIC(NumRestrictBounds, "Number of loop iteration space restricted"); 00059 00060 namespace { 00061 00062 class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass { 00063 00064 public: 00065 static char ID; // Pass ID, replacement for typeid 00066 LoopIndexSplit() : LoopPass(&ID) {} 00067 00068 // Index split Loop L. Return true if loop is split. 00069 bool runOnLoop(Loop *L, LPPassManager &LPM); 00070 00071 void getAnalysisUsage(AnalysisUsage &AU) const { 00072 AU.addRequired<ScalarEvolution>(); 00073 AU.addPreserved<ScalarEvolution>(); 00074 AU.addRequiredID(LCSSAID); 00075 AU.addPreservedID(LCSSAID); 00076 AU.addRequired<LoopInfo>(); 00077 AU.addPreserved<LoopInfo>(); 00078 AU.addRequiredID(LoopSimplifyID); 00079 AU.addPreservedID(LoopSimplifyID); 00080 AU.addRequired<DominatorTree>(); 00081 AU.addRequired<DominanceFrontier>(); 00082 AU.addPreserved<DominatorTree>(); 00083 AU.addPreserved<DominanceFrontier>(); 00084 } 00085 00086 private: 00087 /// processOneIterationLoop -- Eliminate loop if loop body is executed 00088 /// only once. For example, 00089 /// for (i = 0; i < N; ++i) { 00090 /// if ( i == X) { 00091 /// ... 00092 /// } 00093 /// } 00094 /// 00095 bool processOneIterationLoop(); 00096 00097 // -- Routines used by updateLoopIterationSpace(); 00098 00099 /// updateLoopIterationSpace -- Update loop's iteration space if loop 00100 /// body is executed for certain IV range only. For example, 00101 /// 00102 /// for (i = 0; i < N; ++i) { 00103 /// if ( i > A && i < B) { 00104 /// ... 00105 /// } 00106 /// } 00107 /// is transformed to iterators from A to B, if A > 0 and B < N. 00108 /// 00109 bool updateLoopIterationSpace(); 00110 00111 /// restrictLoopBound - Op dominates loop body. Op compares an IV based value 00112 /// with a loop invariant value. Update loop's lower and upper bound based on 00113 /// the loop invariant value. 00114 bool restrictLoopBound(ICmpInst &Op); 00115 00116 // --- Routines used by splitLoop(). --- / 00117 00118 bool splitLoop(); 00119 00120 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by 00121 /// DeadBB. This routine is used to remove split condition's dead branch, 00122 /// dominated by DeadBB. LiveBB dominates split conidition's other branch. 00123 void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB); 00124 00125 /// moveExitCondition - Move exit condition EC into split condition block. 00126 void moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB, 00127 BasicBlock *ExitBB, ICmpInst *EC, ICmpInst *SC, 00128 PHINode *IV, Instruction *IVAdd, Loop *LP, 00129 unsigned); 00130 00131 /// updatePHINodes - CFG has been changed. 00132 /// Before 00133 /// - ExitBB's single predecessor was Latch 00134 /// - Latch's second successor was Header 00135 /// Now 00136 /// - ExitBB's single predecessor was Header 00137 /// - Latch's one and only successor was Header 00138 /// 00139 /// Update ExitBB PHINodes' to reflect this change. 00140 void updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, 00141 BasicBlock *Header, 00142 PHINode *IV, Instruction *IVIncrement, Loop *LP); 00143 00144 // --- Utility routines --- / 00145 00146 /// cleanBlock - A block is considered clean if all non terminal 00147 /// instructions are either PHINodes or IV based values. 00148 bool cleanBlock(BasicBlock *BB); 00149 00150 /// IVisLT - If Op is comparing IV based value with an loop invariant and 00151 /// IV based value is less than the loop invariant then return the loop 00152 /// invariant. Otherwise return NULL. 00153 Value * IVisLT(ICmpInst &Op); 00154 00155 /// IVisLE - If Op is comparing IV based value with an loop invariant and 00156 /// IV based value is less than or equal to the loop invariant then 00157 /// return the loop invariant. Otherwise return NULL. 00158 Value * IVisLE(ICmpInst &Op); 00159 00160 /// IVisGT - If Op is comparing IV based value with an loop invariant and 00161 /// IV based value is greater than the loop invariant then return the loop 00162 /// invariant. Otherwise return NULL. 00163 Value * IVisGT(ICmpInst &Op); 00164 00165 /// IVisGE - If Op is comparing IV based value with an loop invariant and 00166 /// IV based value is greater than or equal to the loop invariant then 00167 /// return the loop invariant. Otherwise return NULL. 00168 Value * IVisGE(ICmpInst &Op); 00169 00170 private: 00171 00172 // Current Loop information. 00173 Loop *L; 00174 LPPassManager *LPM; 00175 LoopInfo *LI; 00176 ScalarEvolution *SE; 00177 DominatorTree *DT; 00178 DominanceFrontier *DF; 00179 00180 PHINode *IndVar; 00181 ICmpInst *ExitCondition; 00182 ICmpInst *SplitCondition; 00183 Value *IVStartValue; 00184 Value *IVExitValue; 00185 Instruction *IVIncrement; 00186 SmallPtrSet<Value *, 4> IVBasedValues; 00187 }; 00188 } 00189 00190 char LoopIndexSplit::ID = 0; 00191 static RegisterPass<LoopIndexSplit> 00192 X("loop-index-split", "Index Split Loops"); 00193 00194 Pass *llvm::createLoopIndexSplitPass() { 00195 return new LoopIndexSplit(); 00196 } 00197 00198 // Index split Loop L. Return true if loop is split. 00199 bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) { 00200 L = IncomingLoop; 00201 LPM = &LPM_Ref; 00202 00203 // FIXME - Nested loops make dominator info updates tricky. 00204 if (!L->getSubLoops().empty()) 00205 return false; 00206 00207 SE = &getAnalysis<ScalarEvolution>(); 00208 DT = &getAnalysis<DominatorTree>(); 00209 LI = &getAnalysis<LoopInfo>(); 00210 DF = &getAnalysis<DominanceFrontier>(); 00211 00212 // Initialize loop data. 00213 IndVar = L->getCanonicalInductionVariable(); 00214 if (!IndVar) return false; 00215 00216 bool P1InLoop = L->contains(IndVar->getIncomingBlock(1)); 00217 IVStartValue = IndVar->getIncomingValue(!P1InLoop); 00218 IVIncrement = dyn_cast<Instruction>(IndVar->getIncomingValue(P1InLoop)); 00219 if (!IVIncrement) return false; 00220 00221 IVBasedValues.clear(); 00222 IVBasedValues.insert(IndVar); 00223 IVBasedValues.insert(IVIncrement); 00224 for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); 00225 I != E; ++I) 00226 for(BasicBlock::iterator BI = (*I)->begin(), BE = (*I)->end(); 00227 BI != BE; ++BI) { 00228 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(BI)) 00229 if (BO != IVIncrement 00230 && (BO->getOpcode() == Instruction::Add 00231 || BO->getOpcode() == Instruction::Sub)) 00232 if (IVBasedValues.count(BO->getOperand(0)) 00233 && L->isLoopInvariant(BO->getOperand(1))) 00234 IVBasedValues.insert(BO); 00235 } 00236 00237 // Reject loop if loop exit condition is not suitable. 00238 SmallVector<BasicBlock *, 2> EBs; 00239 L->getExitingBlocks(EBs); 00240 if (EBs.size() != 1) 00241 return false; 00242 BranchInst *EBR = dyn_cast<BranchInst>(EBs[0]->getTerminator()); 00243 if (!EBR) return false; 00244 ExitCondition = dyn_cast<ICmpInst>(EBR->getCondition()); 00245 if (!ExitCondition) return false; 00246 if (EBs[0] != L->getLoopLatch()) return false; 00247 IVExitValue = ExitCondition->getOperand(1); 00248 if (!L->isLoopInvariant(IVExitValue)) 00249 IVExitValue = ExitCondition->getOperand(0); 00250 if (!L->isLoopInvariant(IVExitValue)) 00251 return false; 00252 00253 // If start value is more then exit value where induction variable 00254 // increments by 1 then we are potentially dealing with an infinite loop. 00255 // Do not index split this loop. 00256 if (ConstantInt *SV = dyn_cast<ConstantInt>(IVStartValue)) 00257 if (ConstantInt *EV = dyn_cast<ConstantInt>(IVExitValue)) 00258 if (SV->getSExtValue() > EV->getSExtValue()) 00259 return false; 00260 00261 if (processOneIterationLoop()) 00262 return true; 00263 00264 if (updateLoopIterationSpace()) 00265 return true; 00266 00267 if (splitLoop()) 00268 return true; 00269 00270 return false; 00271 } 00272 00273 // --- Helper routines --- 00274 // isUsedOutsideLoop - Returns true iff V is used outside the loop L. 00275 static bool isUsedOutsideLoop(Value *V, Loop *L) { 00276 for(Value::use_iterator UI = V->use_begin(), E = V->use_end(); UI != E; ++UI) 00277 if (!L->contains(cast<Instruction>(*UI)->getParent())) 00278 return true; 00279 return false; 00280 } 00281 00282 // Return V+1 00283 static Value *getPlusOne(Value *V, bool Sign, Instruction *InsertPt) { 00284 ConstantInt *One = ConstantInt::get(V->getType(), 1, Sign); 00285 return BinaryOperator::CreateAdd(V, One, "lsp", InsertPt); 00286 } 00287 00288 // Return V-1 00289 static Value *getMinusOne(Value *V, bool Sign, Instruction *InsertPt) { 00290 ConstantInt *One = ConstantInt::get(V->getType(), 1, Sign); 00291 return BinaryOperator::CreateSub(V, One, "lsp", InsertPt); 00292 } 00293 00294 // Return min(V1, V1) 00295 static Value *getMin(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) { 00296 00297 Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, 00298 V1, V2, "lsp", InsertPt); 00299 return SelectInst::Create(C, V1, V2, "lsp", InsertPt); 00300 } 00301 00302 // Return max(V1, V2) 00303 static Value *getMax(Value *V1, Value *V2, bool Sign, Instruction *InsertPt) { 00304 00305 Value *C = new ICmpInst(Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, 00306 V1, V2, "lsp", InsertPt); 00307 return SelectInst::Create(C, V2, V1, "lsp", InsertPt); 00308 } 00309 00310 /// processOneIterationLoop -- Eliminate loop if loop body is executed 00311 /// only once. For example, 00312 /// for (i = 0; i < N; ++i) { 00313 /// if ( i == X) { 00314 /// ... 00315 /// } 00316 /// } 00317 /// 00318 bool LoopIndexSplit::processOneIterationLoop() { 00319 SplitCondition = NULL; 00320 BasicBlock *Latch = L->getLoopLatch(); 00321 BasicBlock *Header = L->getHeader(); 00322 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator()); 00323 if (!BR) return false; 00324 if (!isa<BranchInst>(Latch->getTerminator())) return false; 00325 if (BR->isUnconditional()) return false; 00326 SplitCondition = dyn_cast<ICmpInst>(BR->getCondition()); 00327 if (!SplitCondition) return false; 00328 if (SplitCondition == ExitCondition) return false; 00329 if (SplitCondition->getPredicate() != ICmpInst::ICMP_EQ) return false; 00330 if (BR->getOperand(1) != Latch) return false; 00331 if (!IVBasedValues.count(SplitCondition->getOperand(0)) 00332 && !IVBasedValues.count(SplitCondition->getOperand(1))) 00333 return false; 00334 00335 // If IV is used outside the loop then this loop traversal is required. 00336 // FIXME: Calculate and use last IV value. 00337 if (isUsedOutsideLoop(IVIncrement, L)) 00338 return false; 00339 00340 // If BR operands are not IV or not loop invariants then skip this loop. 00341 Value *OPV = SplitCondition->getOperand(0); 00342 Value *SplitValue = SplitCondition->getOperand(1); 00343 if (!L->isLoopInvariant(SplitValue)) { 00344 Value *T = SplitValue; 00345 SplitValue = OPV; 00346 OPV = T; 00347 } 00348 if (!L->isLoopInvariant(SplitValue)) 00349 return false; 00350 Instruction *OPI = dyn_cast<Instruction>(OPV); 00351 if (!OPI) return false; 00352 if (OPI->getParent() != Header || isUsedOutsideLoop(OPI, L)) 00353 return false; 00354 00355 if (!cleanBlock(Header)) 00356 return false; 00357 00358 if (!cleanBlock(Latch)) 00359 return false; 00360 00361 // If the merge point for BR is not loop latch then skip this loop. 00362 if (BR->getSuccessor(0) != Latch) { 00363 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0)); 00364 assert (DF0 != DF->end() && "Unable to find dominance frontier"); 00365 if (!DF0->second.count(Latch)) 00366 return false; 00367 } 00368 00369 if (BR->getSuccessor(1) != Latch) { 00370 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1)); 00371 assert (DF1 != DF->end() && "Unable to find dominance frontier"); 00372 if (!DF1->second.count(Latch)) 00373 return false; 00374 } 00375 00376 // Now, Current loop L contains compare instruction 00377 // that compares induction variable, IndVar, against loop invariant. And 00378 // entire (i.e. meaningful) loop body is dominated by this compare 00379 // instruction. In such case eliminate 00380 // loop structure surrounding this loop body. For example, 00381 // for (int i = start; i < end; ++i) { 00382 // if ( i == somevalue) { 00383 // loop_body 00384 // } 00385 // } 00386 // can be transformed into 00387 // if (somevalue >= start && somevalue < end) { 00388 // i = somevalue; 00389 // loop_body 00390 // } 00391 00392 // Replace index variable with split value in loop body. Loop body is executed 00393 // only when index variable is equal to split value. 00394 IndVar->replaceAllUsesWith(SplitValue); 00395 00396 // Replace split condition in header. 00397 // Transform 00398 // SplitCondition : icmp eq i32 IndVar, SplitValue 00399 // into 00400 // c1 = icmp uge i32 SplitValue, StartValue 00401 // c2 = icmp ult i32 SplitValue, ExitValue 00402 // and i32 c1, c2 00403 Instruction *C1 = new ICmpInst(ExitCondition->isSignedPredicate() ? 00404 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, 00405 SplitValue, IVStartValue, "lisplit", BR); 00406 00407 CmpInst::Predicate C2P = ExitCondition->getPredicate(); 00408 BranchInst *LatchBR = cast<BranchInst>(Latch->getTerminator()); 00409 if (LatchBR->getOperand(0) != Header) 00410 C2P = CmpInst::getInversePredicate(C2P); 00411 Instruction *C2 = new ICmpInst(C2P, SplitValue, IVExitValue, "lisplit", BR); 00412 Instruction *NSplitCond = BinaryOperator::CreateAnd(C1, C2, "lisplit", BR); 00413 00414 SplitCondition->replaceAllUsesWith(NSplitCond); 00415 SplitCondition->eraseFromParent(); 00416 00417 // Remove Latch to Header edge. 00418 BasicBlock *LatchSucc = NULL; 00419 Header->removePredecessor(Latch); 00420 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch); 00421 SI != E; ++SI) { 00422 if (Header != *SI) 00423 LatchSucc = *SI; 00424 } 00425 LatchBR->setUnconditionalDest(LatchSucc); 00426 00427 // Remove IVIncrement 00428 IVIncrement->replaceAllUsesWith(UndefValue::get(IVIncrement->getType())); 00429 IVIncrement->eraseFromParent(); 00430 00431 LPM->deleteLoopFromQueue(L); 00432 00433 // Update Dominator Info. 00434 // Only CFG change done is to remove Latch to Header edge. This 00435 // does not change dominator tree because Latch did not dominate 00436 // Header. 00437 if (DF) { 00438 DominanceFrontier::iterator HeaderDF = DF->find(Header); 00439 if (HeaderDF != DF->end()) 00440 DF->removeFromFrontier(HeaderDF, Header); 00441 00442 DominanceFrontier::iterator LatchDF = DF->find(Latch); 00443 if (LatchDF != DF->end()) 00444 DF->removeFromFrontier(LatchDF, Header); 00445 } 00446 00447 ++NumIndexSplitRemoved; 00448 return true; 00449 } 00450 00451 /// restrictLoopBound - Op dominates loop body. Op compares an IV based value 00452 /// with a loop invariant value. Update loop's lower and upper bound based on 00453 /// the loop invariant value. 00454 bool LoopIndexSplit::restrictLoopBound(ICmpInst &Op) { 00455 bool Sign = Op.isSignedPredicate(); 00456 Instruction *PHTerm = L->getLoopPreheader()->getTerminator(); 00457 00458 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) { 00459 BranchInst *EBR = 00460 cast<BranchInst>(ExitCondition->getParent()->getTerminator()); 00461 ExitCondition->setPredicate(ExitCondition->getInversePredicate()); 00462 BasicBlock *T = EBR->getSuccessor(0); 00463 EBR->setSuccessor(0, EBR->getSuccessor(1)); 00464 EBR->setSuccessor(1, T); 00465 } 00466 00467 // New upper and lower bounds. 00468 Value *NLB = NULL; 00469 Value *NUB = NULL; 00470 if (Value *V = IVisLT(Op)) { 00471 // Restrict upper bound. 00472 if (IVisLE(*ExitCondition)) 00473 V = getMinusOne(V, Sign, PHTerm); 00474 NUB = getMin(V, IVExitValue, Sign, PHTerm); 00475 } else if (Value *V = IVisLE(Op)) { 00476 // Restrict upper bound. 00477 if (IVisLT(*ExitCondition)) 00478 V = getPlusOne(V, Sign, PHTerm); 00479 NUB = getMin(V, IVExitValue, Sign, PHTerm); 00480 } else if (Value *V = IVisGT(Op)) { 00481 // Restrict lower bound. 00482 V = getPlusOne(V, Sign, PHTerm); 00483 NLB = getMax(V, IVStartValue, Sign, PHTerm); 00484 } else if (Value *V = IVisGE(Op)) 00485 // Restrict lower bound. 00486 NLB = getMax(V, IVStartValue, Sign, PHTerm); 00487 00488 if (!NLB && !NUB) 00489 return false; 00490 00491 if (NLB) { 00492 unsigned i = IndVar->getBasicBlockIndex(L->getLoopPreheader()); 00493 IndVar->setIncomingValue(i, NLB); 00494 } 00495 00496 if (NUB) { 00497 unsigned i = (ExitCondition->getOperand(0) != IVExitValue); 00498 ExitCondition->setOperand(i, NUB); 00499 } 00500 return true; 00501 } 00502 00503 /// updateLoopIterationSpace -- Update loop's iteration space if loop 00504 /// body is executed for certain IV range only. For example, 00505 /// 00506 /// for (i = 0; i < N; ++i) { 00507 /// if ( i > A && i < B) { 00508 /// ... 00509 /// } 00510 /// } 00511 /// is transformed to iterators from A to B, if A > 0 and B < N. 00512 /// 00513 bool LoopIndexSplit::updateLoopIterationSpace() { 00514 SplitCondition = NULL; 00515 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE 00516 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ) 00517 return false; 00518 BasicBlock *Latch = L->getLoopLatch(); 00519 BasicBlock *Header = L->getHeader(); 00520 BranchInst *BR = dyn_cast<BranchInst>(Header->getTerminator()); 00521 if (!BR) return false; 00522 if (!isa<BranchInst>(Latch->getTerminator())) return false; 00523 if (BR->isUnconditional()) return false; 00524 BinaryOperator *AND = dyn_cast<BinaryOperator>(BR->getCondition()); 00525 if (!AND) return false; 00526 if (AND->getOpcode() != Instruction::And) return false; 00527 ICmpInst *Op0 = dyn_cast<ICmpInst>(AND->getOperand(0)); 00528 ICmpInst *Op1 = dyn_cast<ICmpInst>(AND->getOperand(1)); 00529 if (!Op0 || !Op1) 00530 return false; 00531 IVBasedValues.insert(AND); 00532 IVBasedValues.insert(Op0); 00533 IVBasedValues.insert(Op1); 00534 if (!cleanBlock(Header)) return false; 00535 BasicBlock *ExitingBlock = ExitCondition->getParent(); 00536 if (!cleanBlock(ExitingBlock)) return false; 00537 00538 // Verify that loop exiting block has only two predecessor, where one pred 00539 // is split condition block. The other predecessor will become exiting block's 00540 // dominator after CFG is updated. TODO : Handle CFG's where exiting block has 00541 // more then two predecessors. This requires extra work in updating dominator 00542 // information. 00543 BasicBlock *ExitingBBPred = NULL; 00544 for (pred_iterator PI = pred_begin(ExitingBlock), PE = pred_end(ExitingBlock); 00545 PI != PE; ++PI) { 00546 BasicBlock *BB = *PI; 00547 if (Header == BB) 00548 continue; 00549 if (ExitingBBPred) 00550 return false; 00551 else 00552 ExitingBBPred = BB; 00553 } 00554 00555 if (!restrictLoopBound(*Op0)) 00556 return false; 00557 00558 if (!restrictLoopBound(*Op1)) 00559 return false; 00560 00561 // Update CFG. 00562 if (BR->getSuccessor(0) == ExitingBlock) 00563 BR->setUnconditionalDest(BR->getSuccessor(1)); 00564 else 00565 BR->setUnconditionalDest(BR->getSuccessor(0)); 00566 00567 AND->eraseFromParent(); 00568 if (Op0->use_empty()) 00569 Op0->eraseFromParent(); 00570 if (Op1->use_empty()) 00571 Op1->eraseFromParent(); 00572 00573 // Update domiantor info. Now, ExitingBlock has only one predecessor, 00574 // ExitingBBPred, and it is ExitingBlock's immediate domiantor. 00575 DT->changeImmediateDominator(ExitingBlock, ExitingBBPred); 00576 00577 BasicBlock *ExitBlock = ExitingBlock->getTerminator()->getSuccessor(1); 00578 if (L->contains(ExitBlock)) 00579 ExitBlock = ExitingBlock->getTerminator()->getSuccessor(0); 00580 00581 // If ExitingBlock is a member of the loop basic blocks' DF list then 00582 // replace ExitingBlock with header and exit block in the DF list 00583 DominanceFrontier::iterator ExitingBlockDF = DF->find(ExitingBlock); 00584 for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); 00585 I != E; ++I) { 00586 BasicBlock *BB = *I; 00587 if (BB == Header || BB == ExitingBlock) 00588 continue; 00589 DominanceFrontier::iterator BBDF = DF->find(BB); 00590 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin(); 00591 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end(); 00592 while (DomSetI != DomSetE) { 00593 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI; 00594 ++DomSetI; 00595 BasicBlock *DFBB = *CurrentItr; 00596 if (DFBB == ExitingBlock) { 00597 BBDF->second.erase(DFBB); 00598 for (DominanceFrontier::DomSetType::iterator 00599 EBI = ExitingBlockDF->second.begin(), 00600 EBE = ExitingBlockDF->second.end(); EBI != EBE; ++EBI) 00601 BBDF->second.insert(*EBI); 00602 } 00603 } 00604 } 00605 NumRestrictBounds++; 00606 return true; 00607 } 00608 00609 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB. 00610 /// This routine is used to remove split condition's dead branch, dominated by 00611 /// DeadBB. LiveBB dominates split conidition's other branch. 00612 void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP, 00613 BasicBlock *LiveBB) { 00614 00615 // First update DeadBB's dominance frontier. 00616 SmallVector<BasicBlock *, 8> FrontierBBs; 00617 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB); 00618 if (DeadBBDF != DF->end()) { 00619 SmallVector<BasicBlock *, 8> PredBlocks; 00620 00621 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second; 00622 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(), 00623 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) 00624 { 00625 BasicBlock *FrontierBB = *DeadBBSetI; 00626 FrontierBBs.push_back(FrontierBB); 00627 00628 // Rremove any PHI incoming edge from blocks dominated by DeadBB. 00629 PredBlocks.clear(); 00630 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB); 00631 PI != PE; ++PI) { 00632 BasicBlock *P = *PI; 00633 if (P == DeadBB || DT->dominates(DeadBB, P)) 00634 PredBlocks.push_back(P); 00635 } 00636 00637 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end(); 00638 FBI != FBE; ++FBI) { 00639 if (PHINode *PN = dyn_cast<PHINode>(FBI)) { 00640 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(), 00641 PE = PredBlocks.end(); PI != PE; ++PI) { 00642 BasicBlock *P = *PI; 00643 PN->removeIncomingValue(P); 00644 } 00645 } 00646 else 00647 break; 00648 } 00649 } 00650 } 00651 00652 // Now remove DeadBB and all nodes dominated by DeadBB in df order. 00653 SmallVector<BasicBlock *, 32> WorkList; 00654 DomTreeNode *DN = DT->getNode(DeadBB); 00655 for (df_iterator<DomTreeNode*> DI = df_begin(DN), 00656 E = df_end(DN); DI != E; ++DI) { 00657 BasicBlock *BB = DI->getBlock(); 00658 WorkList.push_back(BB); 00659 BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy)); 00660 } 00661 00662 while (!WorkList.empty()) { 00663 BasicBlock *BB = WorkList.back(); WorkList.pop_back(); 00664 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end(); 00665 BBI != BBE; ) { 00666 Instruction *I = BBI; 00667 ++BBI; 00668 I->replaceAllUsesWith(UndefValue::get(I->getType())); 00669 I->eraseFromParent(); 00670 } 00671 LPM->deleteSimpleAnalysisValue(BB, LP); 00672 DT->eraseNode(BB); 00673 DF->removeBlock(BB); 00674 LI->removeBlock(BB); 00675 BB->eraseFromParent(); 00676 } 00677 00678 // Update Frontier BBs' dominator info. 00679 while (!FrontierBBs.empty()) { 00680 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back(); 00681 BasicBlock *NewDominator = FBB->getSinglePredecessor(); 00682 if (!NewDominator) { 00683 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB); 00684 NewDominator = *PI; 00685 ++PI; 00686 if (NewDominator != LiveBB) { 00687 for(; PI != PE; ++PI) { 00688 BasicBlock *P = *PI; 00689 if (P == LiveBB) { 00690 NewDominator = LiveBB; 00691 break; 00692 } 00693 NewDominator = DT->findNearestCommonDominator(NewDominator, P); 00694 } 00695 } 00696 } 00697 assert (NewDominator && "Unable to fix dominator info."); 00698 DT->changeImmediateDominator(FBB, NewDominator); 00699 DF->changeImmediateDominator(FBB, NewDominator, DT); 00700 } 00701 00702 } 00703 00704 // moveExitCondition - Move exit condition EC into split condition block CondBB. 00705 void LoopIndexSplit::moveExitCondition(BasicBlock *CondBB, BasicBlock *ActiveBB, 00706 BasicBlock *ExitBB, ICmpInst *EC, 00707 ICmpInst *SC, PHINode *IV, 00708 Instruction *IVAdd, Loop *LP, 00709 unsigned ExitValueNum) { 00710 00711 BasicBlock *ExitingBB = EC->getParent(); 00712 Instruction *CurrentBR = CondBB->getTerminator(); 00713 00714 // Move exit condition into split condition block. 00715 EC->moveBefore(CurrentBR); 00716 EC->setOperand(ExitValueNum == 0 ? 1 : 0, IV); 00717 00718 // Move exiting block's branch into split condition block. Update its branch 00719 // destination. 00720 BranchInst *ExitingBR = cast<BranchInst>(ExitingBB->getTerminator()); 00721 ExitingBR->moveBefore(CurrentBR); 00722 BasicBlock *OrigDestBB = NULL; 00723 if (ExitingBR->getSuccessor(0) == ExitBB) { 00724 OrigDestBB = ExitingBR->getSuccessor(1); 00725 ExitingBR->setSuccessor(1, ActiveBB); 00726 } 00727 else { 00728 OrigDestBB = ExitingBR->getSuccessor(0); 00729 ExitingBR->setSuccessor(0, ActiveBB); 00730 } 00731 00732 // Remove split condition and current split condition branch. 00733 SC->eraseFromParent(); 00734 CurrentBR->eraseFromParent(); 00735 00736 // Connect exiting block to original destination. 00737 BranchInst::Create(OrigDestBB, ExitingBB); 00738 00739 // Update PHINodes 00740 updatePHINodes(ExitBB, ExitingBB, CondBB, IV, IVAdd, LP); 00741 00742 // Fix dominator info. 00743 // ExitBB is now dominated by CondBB 00744 DT->changeImmediateDominator(ExitBB, CondBB); 00745 DF->changeImmediateDominator(ExitBB, CondBB, DT); 00746 00747 // Basicblocks dominated by ActiveBB may have ExitingBB or 00748 // a basic block outside the loop in their DF list. If so, 00749 // replace it with CondBB. 00750 DomTreeNode *Node = DT->getNode(ActiveBB); 00751 for (df_iterator<DomTreeNode *> DI = df_begin(Node), DE = df_end(Node); 00752 DI != DE; ++DI) { 00753 BasicBlock *BB = DI->getBlock(); 00754 DominanceFrontier::iterator BBDF = DF->find(BB); 00755 DominanceFrontier::DomSetType::iterator DomSetI = BBDF->second.begin(); 00756 DominanceFrontier::DomSetType::iterator DomSetE = BBDF->second.end(); 00757 while (DomSetI != DomSetE) { 00758 DominanceFrontier::DomSetType::iterator CurrentItr = DomSetI; 00759 ++DomSetI; 00760 BasicBlock *DFBB = *CurrentItr; 00761 if (DFBB == ExitingBB || !L->contains(DFBB)) { 00762 BBDF->second.erase(DFBB); 00763 BBDF->second.insert(CondBB); 00764 } 00765 } 00766 } 00767 } 00768 00769 /// updatePHINodes - CFG has been changed. 00770 /// Before 00771 /// - ExitBB's single predecessor was Latch 00772 /// - Latch's second successor was Header 00773 /// Now 00774 /// - ExitBB's single predecessor is Header 00775 /// - Latch's one and only successor is Header 00776 /// 00777 /// Update ExitBB PHINodes' to reflect this change. 00778 void LoopIndexSplit::updatePHINodes(BasicBlock *ExitBB, BasicBlock *Latch, 00779 BasicBlock *Header, 00780 PHINode *IV, Instruction *IVIncrement, 00781 Loop *LP) { 00782 00783 for (BasicBlock::iterator BI = ExitBB->begin(), BE = ExitBB->end(); 00784 BI != BE; ) { 00785 PHINode *PN = dyn_cast<PHINode>(BI); 00786 ++BI; 00787 if (!PN) 00788 break; 00789 00790 Value *V = PN->getIncomingValueForBlock(Latch); 00791 if (PHINode *PHV = dyn_cast<PHINode>(V)) { 00792 // PHV is in Latch. PHV has one use is in ExitBB PHINode. And one use 00793 // in Header which is new incoming value for PN. 00794 Value *NewV = NULL; 00795 for (Value::use_iterator UI = PHV->use_begin(), E = PHV->use_end(); 00796 UI != E; ++UI) 00797 if (PHINode *U = dyn_cast<PHINode>(*UI)) 00798 if (LP->contains(U->getParent())) { 00799 NewV = U; 00800 break; 00801 } 00802 00803 // Add incoming value from header only if PN has any use inside the loop. 00804 if (NewV) 00805 PN->addIncoming(NewV, Header); 00806 00807 } else if (Instruction *PHI = dyn_cast<Instruction>(V)) { 00808 // If this instruction is IVIncrement then IV is new incoming value 00809 // from header otherwise this instruction must be incoming value from 00810 // header because loop is in LCSSA form. 00811 if (PHI == IVIncrement) 00812 PN->addIncoming(IV, Header); 00813 else 00814 PN->addIncoming(V, Header); 00815 } else 00816 // Otherwise this is an incoming value from header because loop is in 00817 // LCSSA form. 00818 PN->addIncoming(V, Header); 00819 00820 // Remove incoming value from Latch. 00821 PN->removeIncomingValue(Latch); 00822 } 00823 } 00824 00825 bool LoopIndexSplit::splitLoop() { 00826 SplitCondition = NULL; 00827 if (ExitCondition->getPredicate() == ICmpInst::ICMP_NE 00828 || ExitCondition->getPredicate() == ICmpInst::ICMP_EQ) 00829 return false; 00830 BasicBlock *Header = L->getHeader(); 00831 BasicBlock *Latch = L->getLoopLatch(); 00832 BranchInst *SBR = NULL; // Split Condition Branch 00833 BranchInst *EBR = cast<BranchInst>(ExitCondition->getParent()->getTerminator()); 00834 // If Exiting block includes loop variant instructions then this 00835 // loop may not be split safely. 00836 BasicBlock *ExitingBlock = ExitCondition->getParent(); 00837 if (!cleanBlock(ExitingBlock)) return false; 00838 00839 for (Loop::block_iterator I = L->block_begin(), E = L->block_end(); 00840 I != E; ++I) { 00841 BranchInst *BR = dyn_cast<BranchInst>((*I)->getTerminator()); 00842 if (!BR || BR->isUnconditional()) continue; 00843 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition()); 00844 if (!CI || CI == ExitCondition 00845 || CI->getPredicate() == ICmpInst::ICMP_NE 00846 || CI->getPredicate() == ICmpInst::ICMP_EQ) 00847 continue; 00848 00849 // Unable to handle triangle loops at the moment. 00850 // In triangle loop, split condition is in header and one of the 00851 // the split destination is loop latch. If split condition is EQ 00852 // then such loops are already handle in processOneIterationLoop(). 00853 if (Header == (*I) 00854 && (Latch == BR->getSuccessor(0) || Latch == BR->getSuccessor(1))) 00855 continue; 00856 00857 // If the block does not dominate the latch then this is not a diamond. 00858 // Such loop may not benefit from index split. 00859 if (!DT->dominates((*I), Latch)) 00860 continue; 00861 00862 // If split condition branches heads do not have single predecessor, 00863 // SplitCondBlock, then is not possible to remove inactive branch. 00864 if (!BR->getSuccessor(0)->getSinglePredecessor() 00865 || !BR->getSuccessor(1)->getSinglePredecessor()) 00866 return false; 00867 00868 // If the merge point for BR is not loop latch then skip this condition. 00869 if (BR->getSuccessor(0) != Latch) { 00870 DominanceFrontier::iterator DF0 = DF->find(BR->getSuccessor(0)); 00871 assert (DF0 != DF->end() && "Unable to find dominance frontier"); 00872 if (!DF0->second.count(Latch)) 00873 continue; 00874 } 00875 00876 if (BR->getSuccessor(1) != Latch) { 00877 DominanceFrontier::iterator DF1 = DF->find(BR->getSuccessor(1)); 00878 assert (DF1 != DF->end() && "Unable to find dominance frontier"); 00879 if (!DF1->second.count(Latch)) 00880 continue; 00881 } 00882 SplitCondition = CI; 00883 SBR = BR; 00884 break; 00885 } 00886 00887 if (!SplitCondition) 00888 return false; 00889 00890 // If the predicate sign does not match then skip. 00891 if (ExitCondition->isSignedPredicate() != SplitCondition->isSignedPredicate()) 00892 return false; 00893 00894 unsigned EVOpNum = (ExitCondition->getOperand(1) == IVExitValue); 00895 unsigned SVOpNum = IVBasedValues.count(SplitCondition->getOperand(0)); 00896 Value *SplitValue = SplitCondition->getOperand(SVOpNum); 00897 if (!L->isLoopInvariant(SplitValue)) 00898 return false; 00899 if (!IVBasedValues.count(SplitCondition->getOperand(!SVOpNum))) 00900 return false; 00901 00902 // Normalize loop conditions so that it is easier to calculate new loop 00903 // bounds. 00904 if (IVisGT(*ExitCondition) || IVisGE(*ExitCondition)) { 00905 ExitCondition->setPredicate(ExitCondition->getInversePredicate()); 00906 BasicBlock *T = EBR->getSuccessor(0); 00907 EBR->setSuccessor(0, EBR->getSuccessor(1)); 00908 EBR->setSuccessor(1, T); 00909 } 00910 00911 if (IVisGT(*SplitCondition) || IVisGE(*SplitCondition)) { 00912 SplitCondition->setPredicate(SplitCondition->getInversePredicate()); 00913 BasicBlock *T = SBR->getSuccessor(0); 00914 SBR->setSuccessor(0, SBR->getSuccessor(1)); 00915 SBR->setSuccessor(1, T); 00916 } 00917 00918 //[*] Calculate new loop bounds. 00919 Value *AEV = SplitValue; 00920 Value *BSV = SplitValue; 00921 bool Sign = SplitCondition->isSignedPredicate(); 00922 Instruction *PHTerm = L->getLoopPreheader()->getTerminator(); 00923 00924 if (IVisLT(*ExitCondition)) { 00925 if (IVisLT(*SplitCondition)) { 00926 /* Do nothing */ 00927 } 00928 else if (IVisLE(*SplitCondition)) { 00929 AEV = getPlusOne(SplitValue, Sign, PHTerm); 00930 BSV = getPlusOne(SplitValue, Sign, PHTerm); 00931 } else { 00932 assert (0 && "Unexpected split condition!"); 00933 } 00934 } 00935 else if (IVisLE(*ExitCondition)) { 00936 if (IVisLT(*SplitCondition)) { 00937 AEV = getMinusOne(SplitValue, Sign, PHTerm); 00938 } 00939 else if (IVisLE(*SplitCondition)) { 00940 BSV = getPlusOne(SplitValue, Sign, PHTerm); 00941 } else { 00942 assert (0 && "Unexpected split condition!"); 00943 } 00944 } else { 00945 assert (0 && "Unexpected exit condition!"); 00946 } 00947 AEV = getMin(AEV, IVExitValue, Sign, PHTerm); 00948 BSV = getMax(BSV, IVStartValue, Sign, PHTerm); 00949 00950 // [*] Clone Loop 00951 DenseMap<const Value *, Value *> ValueMap; 00952 Loop *BLoop = CloneLoop(L, LPM, LI, ValueMap, this); 00953 Loop *ALoop = L; 00954 00955 // [*] ALoop's exiting edge enters BLoop's header. 00956 // ALoop's original exit block becomes BLoop's exit block. 00957 PHINode *B_IndVar = cast<PHINode>(ValueMap[IndVar]); 00958 BasicBlock *A_ExitingBlock = ExitCondition->getParent(); 00959 BranchInst *A_ExitInsn = 00960 dyn_cast<BranchInst>(A_ExitingBlock->getTerminator()); 00961 assert (A_ExitInsn && "Unable to find suitable loop exit branch"); 00962 BasicBlock *B_ExitBlock = A_ExitInsn->getSuccessor(1); 00963 BasicBlock *B_Header = BLoop->getHeader(); 00964 if (ALoop->contains(B_ExitBlock)) { 00965 B_ExitBlock = A_ExitInsn->getSuccessor(0); 00966 A_ExitInsn->setSuccessor(0, B_Header); 00967 } else 00968 A_ExitInsn->setSuccessor(1, B_Header); 00969 00970 // [*] Update ALoop's exit value using new exit value. 00971 ExitCondition->setOperand(EVOpNum, AEV); 00972 00973 // [*] Update BLoop's header phi nodes. Remove incoming PHINode's from 00974 // original loop's preheader. Add incoming PHINode values from 00975 // ALoop's exiting block. Update BLoop header's domiantor info. 00976 00977 // Collect inverse map of Header PHINodes. 00978 DenseMap<Value *, Value *> InverseMap; 00979 for (BasicBlock::iterator BI = ALoop->getHeader()->begin(), 00980 BE = ALoop->getHeader()->end(); BI != BE; ++BI) { 00981 if (PHINode *PN = dyn_cast<PHINode>(BI)) { 00982 PHINode *PNClone = cast<PHINode>(ValueMap[PN]); 00983 InverseMap[PNClone] = PN; 00984 } else 00985 break; 00986 } 00987 00988 BasicBlock *A_Preheader = ALoop->getLoopPreheader(); 00989 for (BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end(); 00990 BI != BE; ++BI) { 00991 if (PHINode *PN = dyn_cast<PHINode>(BI)) { 00992 // Remove incoming value from original preheader. 00993 PN->removeIncomingValue(A_Preheader); 00994 00995 // Add incoming value from A_ExitingBlock. 00996 if (PN == B_IndVar) 00997 PN->addIncoming(BSV, A_ExitingBlock); 00998 else { 00999 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]); 01000 Value *V2 = NULL; 01001 // If loop header is also loop exiting block then 01002 // OrigPN is incoming value for B loop header. 01003 if (A_ExitingBlock == ALoop->getHeader()) 01004 V2 = OrigPN; 01005 else 01006 V2 = OrigPN->getIncomingValueForBlock(A_ExitingBlock); 01007 PN->addIncoming(V2, A_ExitingBlock); 01008 } 01009 } else 01010 break; 01011 } 01012 01013 DT->changeImmediateDominator(B_Header, A_ExitingBlock); 01014 DF->changeImmediateDominator(B_Header, A_ExitingBlock, DT); 01015 01016 // [*] Update BLoop's exit block. Its new predecessor is BLoop's exit 01017 // block. Remove incoming PHINode values from ALoop's exiting block. 01018 // Add new incoming values from BLoop's incoming exiting value. 01019 // Update BLoop exit block's dominator info.. 01020 BasicBlock *B_ExitingBlock = cast<BasicBlock>(ValueMap[A_ExitingBlock]); 01021 for (BasicBlock::iterator BI = B_ExitBlock->begin(), BE = B_ExitBlock->end(); 01022 BI != BE; ++BI) { 01023 if (PHINode *PN = dyn_cast<PHINode>(BI)) { 01024 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(A_ExitingBlock)], 01025 B_ExitingBlock); 01026 PN->removeIncomingValue(A_ExitingBlock); 01027 } else 01028 break; 01029 } 01030 01031 DT->changeImmediateDominator(B_ExitBlock, B_ExitingBlock); 01032 DF->changeImmediateDominator(B_ExitBlock, B_ExitingBlock, DT); 01033 01034 //[*] Split ALoop's exit edge. This creates a new block which 01035 // serves two purposes. First one is to hold PHINode defnitions 01036 // to ensure that ALoop's LCSSA form. Second use it to act 01037 // as a preheader for BLoop. 01038 BasicBlock *A_ExitBlock = SplitEdge(A_ExitingBlock, B_Header, this); 01039 01040 //[*] Preserve ALoop's LCSSA form. Create new forwarding PHINodes 01041 // in A_ExitBlock to redefine outgoing PHI definitions from ALoop. 01042 for(BasicBlock::iterator BI = B_Header->begin(), BE = B_Header->end(); 01043 BI != BE; ++BI) { 01044 if (PHINode *PN = dyn_cast<PHINode>(BI)) { 01045 Value *V1 = PN->getIncomingValueForBlock(A_ExitBlock); 01046 PHINode *newPHI = PHINode::Create(PN->getType(), PN->getName()); 01047 newPHI->addIncoming(V1, A_ExitingBlock); 01048 A_ExitBlock->getInstList().push_front(newPHI); 01049 PN->removeIncomingValue(A_ExitBlock); 01050 PN->addIncoming(newPHI, A_ExitBlock); 01051 } else 01052 break; 01053 } 01054 01055 //[*] Eliminate split condition's inactive branch from ALoop. 01056 BasicBlock *A_SplitCondBlock = SplitCondition->getParent(); 01057 BranchInst *A_BR = cast<BranchInst>(A_SplitCondBlock->getTerminator()); 01058 BasicBlock *A_InactiveBranch = NULL; 01059 BasicBlock *A_ActiveBranch = NULL; 01060 A_ActiveBranch = A_BR->getSuccessor(0); 01061 A_InactiveBranch = A_BR->getSuccessor(1); 01062 A_BR->setUnconditionalDest(A_ActiveBranch); 01063 removeBlocks(A_InactiveBranch, L, A_ActiveBranch); 01064 01065 //[*] Eliminate split condition's inactive branch in from BLoop. 01066 BasicBlock *B_SplitCondBlock = cast<BasicBlock>(ValueMap[A_SplitCondBlock]); 01067 BranchInst *B_BR = cast<BranchInst>(B_SplitCondBlock->getTerminator()); 01068 BasicBlock *B_InactiveBranch = NULL; 01069 BasicBlock *B_ActiveBranch = NULL; 01070 B_ActiveBranch = B_BR->getSuccessor(1); 01071 B_InactiveBranch = B_BR->getSuccessor(0); 01072 B_BR->setUnconditionalDest(B_ActiveBranch); 01073 removeBlocks(B_InactiveBranch, BLoop, B_ActiveBranch); 01074 01075 BasicBlock *A_Header = ALoop->getHeader(); 01076 if (A_ExitingBlock == A_Header) 01077 return true; 01078 01079 //[*] Move exit condition into split condition block to avoid 01080 // executing dead loop iteration. 01081 ICmpInst *B_ExitCondition = cast<ICmpInst>(ValueMap[ExitCondition]); 01082 Instruction *B_IndVarIncrement = cast<Instruction>(ValueMap[IVIncrement]); 01083 ICmpInst *B_SplitCondition = cast<ICmpInst>(ValueMap[SplitCondition]); 01084 01085 moveExitCondition(A_SplitCondBlock, A_ActiveBranch, A_ExitBlock, ExitCondition, 01086 cast<ICmpInst>(SplitCondition), IndVar, IVIncrement, 01087 ALoop, EVOpNum); 01088 01089 moveExitCondition(B_SplitCondBlock, B_ActiveBranch, 01090 B_ExitBlock, B_ExitCondition, 01091 B_SplitCondition, B_IndVar, B_IndVarIncrement, 01092 BLoop, EVOpNum); 01093 01094 NumIndexSplit++; 01095 return true; 01096 } 01097 01098 /// cleanBlock - A block is considered clean if all non terminal instructions 01099 /// are either, PHINodes, IV based. 01100 bool LoopIndexSplit::cleanBlock(BasicBlock *BB) { 01101 Instruction *Terminator = BB->getTerminator(); 01102 for(BasicBlock::iterator BI = BB->begin(), BE = BB->end(); 01103 BI != BE; ++BI) { 01104 Instruction *I = BI; 01105 01106 if (isa<PHINode>(I) || I == Terminator || I == ExitCondition 01107 || I == SplitCondition || IVBasedValues.count(I)) 01108 continue; 01109 01110 if (I->mayWriteToMemory()) 01111 return false; 01112 01113 // I is used only inside this block then it is OK. 01114 bool usedOutsideBB = false; 01115 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); 01116 UI != UE; ++UI) { 01117 Instruction *U = cast<Instruction>(UI); 01118 if (U->getParent() != BB) 01119 usedOutsideBB = true; 01120 } 01121 if (!usedOutsideBB) 01122 continue; 01123 01124 // Otherwise we have a instruction that may not allow loop spliting. 01125 return false; 01126 } 01127 return true; 01128 } 01129 01130 /// IVisLT - If Op is comparing IV based value with an loop invariant and 01131 /// IV based value is less than the loop invariant then return the loop 01132 /// invariant. Otherwise return NULL. 01133 Value * LoopIndexSplit::IVisLT(ICmpInst &Op) { 01134 ICmpInst::Predicate P = Op.getPredicate(); 01135 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT) 01136 && IVBasedValues.count(Op.getOperand(0)) 01137 && L->isLoopInvariant(Op.getOperand(1))) 01138 return Op.getOperand(1); 01139 01140 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT) 01141 && IVBasedValues.count(Op.getOperand(1)) 01142 && L->isLoopInvariant(Op.getOperand(0))) 01143 return Op.getOperand(0); 01144 01145 return NULL; 01146 } 01147 01148 /// IVisLE - If Op is comparing IV based value with an loop invariant and 01149 /// IV based value is less than or equal to the loop invariant then 01150 /// return the loop invariant. Otherwise return NULL. 01151 Value * LoopIndexSplit::IVisLE(ICmpInst &Op) { 01152 ICmpInst::Predicate P = Op.getPredicate(); 01153 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE) 01154 && IVBasedValues.count(Op.getOperand(0)) 01155 && L->isLoopInvariant(Op.getOperand(1))) 01156 return Op.getOperand(1); 01157 01158 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE) 01159 && IVBasedValues.count(Op.getOperand(1)) 01160 && L->isLoopInvariant(Op.getOperand(0))) 01161 return Op.getOperand(0); 01162 01163 return NULL; 01164 } 01165 01166 /// IVisGT - If Op is comparing IV based value with an loop invariant and 01167 /// IV based value is greater than the loop invariant then return the loop 01168 /// invariant. Otherwise return NULL. 01169 Value * LoopIndexSplit::IVisGT(ICmpInst &Op) { 01170 ICmpInst::Predicate P = Op.getPredicate(); 01171 if ((P == ICmpInst::ICMP_SGT || P == ICmpInst::ICMP_UGT) 01172 && IVBasedValues.count(Op.getOperand(0)) 01173 && L->isLoopInvariant(Op.getOperand(1))) 01174 return Op.getOperand(1); 01175 01176 if ((P == ICmpInst::ICMP_SLT || P == ICmpInst::ICMP_ULT) 01177 && IVBasedValues.count(Op.getOperand(1)) 01178 && L->isLoopInvariant(Op.getOperand(0))) 01179 return Op.getOperand(0); 01180 01181 return NULL; 01182 } 01183 01184 /// IVisGE - If Op is comparing IV based value with an loop invariant and 01185 /// IV based value is greater than or equal to the loop invariant then 01186 /// return the loop invariant. Otherwise return NULL. 01187 Value * LoopIndexSplit::IVisGE(ICmpInst &Op) { 01188 ICmpInst::Predicate P = Op.getPredicate(); 01189 if ((P == ICmpInst::ICMP_SGE || P == ICmpInst::ICMP_UGE) 01190 && IVBasedValues.count(Op.getOperand(0)) 01191 && L->isLoopInvariant(Op.getOperand(1))) 01192 return Op.getOperand(1); 01193 01194 if ((P == ICmpInst::ICMP_SLE || P == ICmpInst::ICMP_ULE) 01195 && IVBasedValues.count(Op.getOperand(1)) 01196 && L->isLoopInvariant(Op.getOperand(0))) 01197 return Op.getOperand(0); 01198 01199 return NULL; 01200 } 01201