LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

IndVarSimplify.cpp

Go to the documentation of this file.
00001 //===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
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 transformation analyzes and transforms the induction variables (and
00011 // computations derived from them) into simpler forms suitable for subsequent
00012 // analysis and transformation.
00013 //
00014 // This transformation makes the following changes to each loop with an
00015 // identifiable induction variable:
00016 //   1. All loops are transformed to have a SINGLE canonical induction variable
00017 //      which starts at zero and steps by one.
00018 //   2. The canonical induction variable is guaranteed to be the first PHI node
00019 //      in the loop header block.
00020 //   3. Any pointer arithmetic recurrences are raised to use array subscripts.
00021 //
00022 // If the trip count of a loop is computable, this pass also makes the following
00023 // changes:
00024 //   1. The exit condition for the loop is canonicalized to compare the
00025 //      induction value against the exit value.  This turns loops like:
00026 //        'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
00027 //   2. Any use outside of the loop of an expression derived from the indvar
00028 //      is changed to compute the derived value outside of the loop, eliminating
00029 //      the dependence on the exit value of the induction variable.  If the only
00030 //      purpose of the loop is to compute the exit value of some derived
00031 //      expression, this transformation will make the loop dead.
00032 //
00033 // This transformation should be followed by strength reduction after all of the
00034 // desired loop transformations have been performed.  Additionally, on targets
00035 // where it is profitable, the loop could be transformed to count down to zero
00036 // (the "do loop" optimization).
00037 //
00038 //===----------------------------------------------------------------------===//
00039 
00040 #define DEBUG_TYPE "indvars"
00041 #include "llvm/Transforms/Scalar.h"
00042 #include "llvm/BasicBlock.h"
00043 #include "llvm/Constants.h"
00044 #include "llvm/Instructions.h"
00045 #include "llvm/Type.h"
00046 #include "llvm/Analysis/ScalarEvolutionExpander.h"
00047 #include "llvm/Analysis/LoopInfo.h"
00048 #include "llvm/Analysis/LoopPass.h"
00049 #include "llvm/Support/CFG.h"
00050 #include "llvm/Support/Compiler.h"
00051 #include "llvm/Support/Debug.h"
00052 #include "llvm/Support/GetElementPtrTypeIterator.h"
00053 #include "llvm/Transforms/Utils/Local.h"
00054 #include "llvm/Support/CommandLine.h"
00055 #include "llvm/ADT/SmallVector.h"
00056 #include "llvm/ADT/Statistic.h"
00057 using namespace llvm;
00058 
00059 STATISTIC(NumRemoved , "Number of aux indvars removed");
00060 STATISTIC(NumPointer , "Number of pointer indvars promoted");
00061 STATISTIC(NumInserted, "Number of canonical indvars added");
00062 STATISTIC(NumReplaced, "Number of exit values replaced");
00063 STATISTIC(NumLFTR    , "Number of loop exit tests replaced");
00064 
00065 namespace {
00066   class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass {
00067     LoopInfo        *LI;
00068     ScalarEvolution *SE;
00069     bool Changed;
00070   public:
00071 
00072    static char ID; // Pass identification, replacement for typeid
00073    IndVarSimplify() : LoopPass((intptr_t)&ID) {}
00074 
00075    bool runOnLoop(Loop *L, LPPassManager &LPM);
00076    bool doInitialization(Loop *L, LPPassManager &LPM);
00077    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00078      AU.addRequired<ScalarEvolution>();
00079      AU.addRequiredID(LCSSAID);
00080      AU.addRequiredID(LoopSimplifyID);
00081      AU.addRequired<LoopInfo>();
00082      AU.addPreservedID(LoopSimplifyID);
00083      AU.addPreservedID(LCSSAID);
00084      AU.setPreservesCFG();
00085    }
00086 
00087   private:
00088 
00089     void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader,
00090                                     std::set<Instruction*> &DeadInsts);
00091     Instruction *LinearFunctionTestReplace(Loop *L, SCEV *IterationCount,
00092                                            SCEVExpander &RW);
00093     void RewriteLoopExitValues(Loop *L, SCEV *IterationCount);
00094 
00095     void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
00096   };
00097 }
00098 
00099 char IndVarSimplify::ID = 0;
00100 static RegisterPass<IndVarSimplify>
00101 X("indvars", "Canonicalize Induction Variables");
00102 
00103 LoopPass *llvm::createIndVarSimplifyPass() {
00104   return new IndVarSimplify();
00105 }
00106 
00107 /// DeleteTriviallyDeadInstructions - If any of the instructions is the
00108 /// specified set are trivially dead, delete them and see if this makes any of
00109 /// their operands subsequently dead.
00110 void IndVarSimplify::
00111 DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
00112   while (!Insts.empty()) {
00113     Instruction *I = *Insts.begin();
00114     Insts.erase(Insts.begin());
00115     if (isInstructionTriviallyDead(I)) {
00116       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
00117         if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
00118           Insts.insert(U);
00119       SE->deleteValueFromRecords(I);
00120       DOUT << "INDVARS: Deleting: " << *I;
00121       I->eraseFromParent();
00122       Changed = true;
00123     }
00124   }
00125 }
00126 
00127 
00128 /// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer
00129 /// recurrence.  If so, change it into an integer recurrence, permitting
00130 /// analysis by the SCEV routines.
00131 void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN,
00132                                                 BasicBlock *Preheader,
00133                                             std::set<Instruction*> &DeadInsts) {
00134   assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!");
00135   unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader);
00136   unsigned BackedgeIdx = PreheaderIdx^1;
00137   if (GetElementPtrInst *GEPI =
00138           dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx)))
00139     if (GEPI->getOperand(0) == PN) {
00140       assert(GEPI->getNumOperands() == 2 && "GEP types must match!");
00141       DOUT << "INDVARS: Eliminating pointer recurrence: " << *GEPI;
00142       
00143       // Okay, we found a pointer recurrence.  Transform this pointer
00144       // recurrence into an integer recurrence.  Compute the value that gets
00145       // added to the pointer at every iteration.
00146       Value *AddedVal = GEPI->getOperand(1);
00147 
00148       // Insert a new integer PHI node into the top of the block.
00149       PHINode *NewPhi = PHINode::Create(AddedVal->getType(),
00150                                         PN->getName()+".rec", PN);
00151       NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader);
00152 
00153       // Create the new add instruction.
00154       Value *NewAdd = BinaryOperator::CreateAdd(NewPhi, AddedVal,
00155                                                 GEPI->getName()+".rec", GEPI);
00156       NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx));
00157 
00158       // Update the existing GEP to use the recurrence.
00159       GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx));
00160 
00161       // Update the GEP to use the new recurrence we just inserted.
00162       GEPI->setOperand(1, NewAdd);
00163 
00164       // If the incoming value is a constant expr GEP, try peeling out the array
00165       // 0 index if possible to make things simpler.
00166       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEPI->getOperand(0)))
00167         if (CE->getOpcode() == Instruction::GetElementPtr) {
00168           unsigned NumOps = CE->getNumOperands();
00169           assert(NumOps > 1 && "CE folding didn't work!");
00170           if (CE->getOperand(NumOps-1)->isNullValue()) {
00171             // Check to make sure the last index really is an array index.
00172             gep_type_iterator GTI = gep_type_begin(CE);
00173             for (unsigned i = 1, e = CE->getNumOperands()-1;
00174                  i != e; ++i, ++GTI)
00175               /*empty*/;
00176             if (isa<SequentialType>(*GTI)) {
00177               // Pull the last index out of the constant expr GEP.
00178               SmallVector<Value*, 8> CEIdxs(CE->op_begin()+1, CE->op_end()-1);
00179               Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0),
00180                                                              &CEIdxs[0],
00181                                                              CEIdxs.size());
00182               Value *Idx[2];
00183               Idx[0] = Constant::getNullValue(Type::Int32Ty);
00184               Idx[1] = NewAdd;
00185               GetElementPtrInst *NGEPI = GetElementPtrInst::Create(
00186                   NCE, Idx, Idx + 2, 
00187                   GEPI->getName(), GEPI);
00188               SE->deleteValueFromRecords(GEPI);
00189               GEPI->replaceAllUsesWith(NGEPI);
00190               GEPI->eraseFromParent();
00191               GEPI = NGEPI;
00192             }
00193           }
00194         }
00195 
00196 
00197       // Finally, if there are any other users of the PHI node, we must
00198       // insert a new GEP instruction that uses the pre-incremented version
00199       // of the induction amount.
00200       if (!PN->use_empty()) {
00201         BasicBlock::iterator InsertPos = PN; ++InsertPos;
00202         while (isa<PHINode>(InsertPos)) ++InsertPos;
00203         Value *PreInc =
00204           GetElementPtrInst::Create(PN->getIncomingValue(PreheaderIdx),
00205                                     NewPhi, "", InsertPos);
00206         PreInc->takeName(PN);
00207         PN->replaceAllUsesWith(PreInc);
00208       }
00209 
00210       // Delete the old PHI for sure, and the GEP if its otherwise unused.
00211       DeadInsts.insert(PN);
00212 
00213       ++NumPointer;
00214       Changed = true;
00215     }
00216 }
00217 
00218 /// LinearFunctionTestReplace - This method rewrites the exit condition of the
00219 /// loop to be a canonical != comparison against the incremented loop induction
00220 /// variable.  This pass is able to rewrite the exit tests of any loop where the
00221 /// SCEV analysis can determine a loop-invariant trip count of the loop, which
00222 /// is actually a much broader range than just linear tests.
00223 ///
00224 /// This method returns a "potentially dead" instruction whose computation chain
00225 /// should be deleted when convenient.
00226 Instruction *IndVarSimplify::LinearFunctionTestReplace(Loop *L,
00227                                                        SCEV *IterationCount,
00228                                                        SCEVExpander &RW) {
00229   // Find the exit block for the loop.  We can currently only handle loops with
00230   // a single exit.
00231   SmallVector<BasicBlock*, 8> ExitBlocks;
00232   L->getExitBlocks(ExitBlocks);
00233   if (ExitBlocks.size() != 1) return 0;
00234   BasicBlock *ExitBlock = ExitBlocks[0];
00235 
00236   // Make sure there is only one predecessor block in the loop.
00237   BasicBlock *ExitingBlock = 0;
00238   for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock);
00239        PI != PE; ++PI)
00240     if (L->contains(*PI)) {
00241       if (ExitingBlock == 0)
00242         ExitingBlock = *PI;
00243       else
00244         return 0;  // Multiple exits from loop to this block.
00245     }
00246   assert(ExitingBlock && "Loop info is broken");
00247 
00248   if (!isa<BranchInst>(ExitingBlock->getTerminator()))
00249     return 0;  // Can't rewrite non-branch yet
00250   BranchInst *BI = cast<BranchInst>(ExitingBlock->getTerminator());
00251   assert(BI->isConditional() && "Must be conditional to be part of loop!");
00252 
00253   Instruction *PotentiallyDeadInst = dyn_cast<Instruction>(BI->getCondition());
00254   
00255   // If the exiting block is not the same as the backedge block, we must compare
00256   // against the preincremented value, otherwise we prefer to compare against
00257   // the post-incremented value.
00258   BasicBlock *Header = L->getHeader();
00259   pred_iterator HPI = pred_begin(Header);
00260   assert(HPI != pred_end(Header) && "Loop with zero preds???");
00261   if (!L->contains(*HPI)) ++HPI;
00262   assert(HPI != pred_end(Header) && L->contains(*HPI) &&
00263          "No backedge in loop?");
00264 
00265   SCEVHandle TripCount = IterationCount;
00266   Value *IndVar;
00267   if (*HPI == ExitingBlock) {
00268     // The IterationCount expression contains the number of times that the
00269     // backedge actually branches to the loop header.  This is one less than the
00270     // number of times the loop executes, so add one to it.
00271     ConstantInt *OneC = ConstantInt::get(IterationCount->getType(), 1);
00272     TripCount = SE->getAddExpr(IterationCount, SE->getConstant(OneC));
00273     IndVar = L->getCanonicalInductionVariableIncrement();
00274   } else {
00275     // We have to use the preincremented value...
00276     IndVar = L->getCanonicalInductionVariable();
00277   }
00278   
00279   DOUT << "INDVARS: LFTR: TripCount = " << *TripCount
00280        << "  IndVar = " << *IndVar << "\n";
00281 
00282   // Expand the code for the iteration count into the preheader of the loop.
00283   BasicBlock *Preheader = L->getLoopPreheader();
00284   Value *ExitCnt = RW.expandCodeFor(TripCount, Preheader->getTerminator());
00285 
00286   // Insert a new icmp_ne or icmp_eq instruction before the branch.
00287   ICmpInst::Predicate Opcode;
00288   if (L->contains(BI->getSuccessor(0)))
00289     Opcode = ICmpInst::ICMP_NE;
00290   else
00291     Opcode = ICmpInst::ICMP_EQ;
00292 
00293   Value *Cond = new ICmpInst(Opcode, IndVar, ExitCnt, "exitcond", BI);
00294   BI->setCondition(Cond);
00295   ++NumLFTR;
00296   Changed = true;
00297   return PotentiallyDeadInst;
00298 }
00299 
00300 
00301 /// RewriteLoopExitValues - Check to see if this loop has a computable
00302 /// loop-invariant execution count.  If so, this means that we can compute the
00303 /// final value of any expressions that are recurrent in the loop, and
00304 /// substitute the exit values from the loop into any instructions outside of
00305 /// the loop that use the final values of the current expressions.
00306 void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *IterationCount) {
00307   BasicBlock *Preheader = L->getLoopPreheader();
00308 
00309   // Scan all of the instructions in the loop, looking at those that have
00310   // extra-loop users and which are recurrences.
00311   SCEVExpander Rewriter(*SE, *LI);
00312 
00313   // We insert the code into the preheader of the loop if the loop contains
00314   // multiple exit blocks, or in the exit block if there is exactly one.
00315   BasicBlock *BlockToInsertInto;
00316   SmallVector<BasicBlock*, 8> ExitBlocks;
00317   L->getUniqueExitBlocks(ExitBlocks);
00318   if (ExitBlocks.size() == 1)
00319     BlockToInsertInto = ExitBlocks[0];
00320   else
00321     BlockToInsertInto = Preheader;
00322   BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI();
00323 
00324   bool HasConstantItCount = isa<SCEVConstant>(IterationCount);
00325 
00326   std::set<Instruction*> InstructionsToDelete;
00327   std::map<Instruction*, Value*> ExitValues;
00328 
00329   // Find all values that are computed inside the loop, but used outside of it.
00330   // Because of LCSSA, these values will only occur in LCSSA PHI Nodes.  Scan
00331   // the exit blocks of the loop to find them.
00332   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
00333     BasicBlock *ExitBB = ExitBlocks[i];
00334     
00335     // If there are no PHI nodes in this exit block, then no values defined
00336     // inside the loop are used on this path, skip it.
00337     PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
00338     if (!PN) continue;
00339     
00340     unsigned NumPreds = PN->getNumIncomingValues();
00341     
00342     // Iterate over all of the PHI nodes.
00343     BasicBlock::iterator BBI = ExitBB->begin();
00344     while ((PN = dyn_cast<PHINode>(BBI++))) {
00345       
00346       // Iterate over all of the values in all the PHI nodes.
00347       for (unsigned i = 0; i != NumPreds; ++i) {
00348         // If the value being merged in is not integer or is not defined
00349         // in the loop, skip it.
00350         Value *InVal = PN->getIncomingValue(i);
00351         if (!isa<Instruction>(InVal) ||
00352             // SCEV only supports integer expressions for now.
00353             !isa<IntegerType>(InVal->getType()))
00354           continue;
00355 
00356         // If this pred is for a subloop, not L itself, skip it.
00357         if (LI->getLoopFor(PN->getIncomingBlock(i)) != L) 
00358           continue; // The Block is in a subloop, skip it.
00359 
00360         // Check that InVal is defined in the loop.
00361         Instruction *Inst = cast<Instruction>(InVal);
00362         if (!L->contains(Inst->getParent()))
00363           continue;
00364         
00365         // We require that this value either have a computable evolution or that
00366         // the loop have a constant iteration count.  In the case where the loop
00367         // has a constant iteration count, we can sometimes force evaluation of
00368         // the exit value through brute force.
00369         SCEVHandle SH = SE->getSCEV(Inst);
00370         if (!SH->hasComputableLoopEvolution(L) && !HasConstantItCount)
00371           continue;          // Cannot get exit evolution for the loop value.
00372         
00373         // Okay, this instruction has a user outside of the current loop
00374         // and varies predictably *inside* the loop.  Evaluate the value it
00375         // contains when the loop exits, if possible.
00376         SCEVHandle ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
00377         if (isa<SCEVCouldNotCompute>(ExitValue) ||
00378             !ExitValue->isLoopInvariant(L))
00379           continue;
00380 
00381         Changed = true;
00382         ++NumReplaced;
00383         
00384         // See if we already computed the exit value for the instruction, if so,
00385         // just reuse it.
00386         Value *&ExitVal = ExitValues[Inst];
00387         if (!ExitVal)
00388           ExitVal = Rewriter.expandCodeFor(ExitValue, InsertPt);
00389         
00390         DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal
00391              << "  LoopVal = " << *Inst << "\n";
00392 
00393         PN->setIncomingValue(i, ExitVal);
00394         
00395         // If this instruction is dead now, schedule it to be removed.
00396         if (Inst->use_empty())
00397           InstructionsToDelete.insert(Inst);
00398         
00399         // See if this is a single-entry LCSSA PHI node.  If so, we can (and
00400         // have to) remove
00401         // the PHI entirely.  This is safe, because the NewVal won't be variant
00402         // in the loop, so we don't need an LCSSA phi node anymore.
00403         if (NumPreds == 1) {
00404           SE->deleteValueFromRecords(PN);
00405           PN->replaceAllUsesWith(ExitVal);
00406           PN->eraseFromParent();
00407           break;
00408         }
00409       }
00410     }
00411   }
00412   
00413   DeleteTriviallyDeadInstructions(InstructionsToDelete);
00414 }
00415 
00416 bool IndVarSimplify::doInitialization(Loop *L, LPPassManager &LPM) {
00417 
00418   Changed = false;
00419   // First step.  Check to see if there are any trivial GEP pointer recurrences.
00420   // If there are, change them into integer recurrences, permitting analysis by
00421   // the SCEV routines.
00422   //
00423   BasicBlock *Header    = L->getHeader();
00424   BasicBlock *Preheader = L->getLoopPreheader();
00425   SE = &LPM.getAnalysis<ScalarEvolution>();
00426 
00427   std::set<Instruction*> DeadInsts;
00428   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
00429     PHINode *PN = cast<PHINode>(I);
00430     if (isa<PointerType>(PN->getType()))
00431       EliminatePointerRecurrence(PN, Preheader, DeadInsts);
00432   }
00433 
00434   if (!DeadInsts.empty())
00435     DeleteTriviallyDeadInstructions(DeadInsts);
00436 
00437   return Changed;
00438 }
00439 
00440 bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
00441 
00442 
00443   LI = &getAnalysis<LoopInfo>();
00444   SE = &getAnalysis<ScalarEvolution>();
00445 
00446   Changed = false;
00447   BasicBlock *Header    = L->getHeader();
00448   std::set<Instruction*> DeadInsts;
00449   
00450   // Verify the input to the pass in already in LCSSA form.
00451   assert(L->isLCSSAForm());
00452 
00453   // Check to see if this loop has a computable loop-invariant execution count.
00454   // If so, this means that we can compute the final value of any expressions
00455   // that are recurrent in the loop, and substitute the exit values from the
00456   // loop into any instructions outside of the loop that use the final values of
00457   // the current expressions.
00458   //
00459   SCEVHandle IterationCount = SE->getIterationCount(L);
00460   if (!isa<SCEVCouldNotCompute>(IterationCount))
00461     RewriteLoopExitValues(L, IterationCount);
00462 
00463   // Next, analyze all of the induction variables in the loop, canonicalizing
00464   // auxillary induction variables.
00465   std::vector<std::pair<PHINode*, SCEVHandle> > IndVars;
00466 
00467   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
00468     PHINode *PN = cast<PHINode>(I);
00469     if (PN->getType()->isInteger()) { // FIXME: when we have fast-math, enable!
00470       SCEVHandle SCEV = SE->getSCEV(PN);
00471       if (SCEV->hasComputableLoopEvolution(L))
00472         // FIXME: It is an extremely bad idea to indvar substitute anything more
00473         // complex than affine induction variables.  Doing so will put expensive
00474         // polynomial evaluations inside of the loop, and the str reduction pass
00475         // currently can only reduce affine polynomials.  For now just disable
00476         // indvar subst on anything more complex than an affine addrec.
00477         if (SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV))
00478           if (AR->isAffine())
00479             IndVars.push_back(std::make_pair(PN, SCEV));
00480     }
00481   }
00482 
00483   // If there are no induction variables in the loop, there is nothing more to
00484   // do.
00485   if (IndVars.empty()) {
00486     // Actually, if we know how many times the loop iterates, lets insert a
00487     // canonical induction variable to help subsequent passes.
00488     if (!isa<SCEVCouldNotCompute>(IterationCount)) {
00489       SCEVExpander Rewriter(*SE, *LI);
00490       Rewriter.getOrInsertCanonicalInductionVariable(L,
00491                                                      IterationCount->getType());
00492       if (Instruction *I = LinearFunctionTestReplace(L, IterationCount,
00493                                                      Rewriter)) {
00494         std::set<Instruction*> InstructionsToDelete;
00495         InstructionsToDelete.insert(I);
00496         DeleteTriviallyDeadInstructions(InstructionsToDelete);
00497       }
00498     }
00499     return Changed;
00500   }
00501 
00502   // Compute the type of the largest recurrence expression.
00503   //
00504   const Type *LargestType = IndVars[0].first->getType();
00505   bool DifferingSizes = false;
00506   for (unsigned i = 1, e = IndVars.size(); i != e; ++i) {
00507     const Type *Ty = IndVars[i].first->getType();
00508     DifferingSizes |= 
00509       Ty->getPrimitiveSizeInBits() != LargestType->getPrimitiveSizeInBits();
00510     if (Ty->getPrimitiveSizeInBits() > LargestType->getPrimitiveSizeInBits())
00511       LargestType = Ty;
00512   }
00513 
00514   // Create a rewriter object which we'll use to transform the code with.
00515   SCEVExpander Rewriter(*SE, *LI);
00516 
00517   // Now that we know the largest of of the induction variables in this loop,
00518   // insert a canonical induction variable of the largest size.
00519   Value *IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType);
00520   ++NumInserted;
00521   Changed = true;
00522   DOUT << "INDVARS: New CanIV: " << *IndVar;
00523 
00524   if (!isa<SCEVCouldNotCompute>(IterationCount)) {
00525     IterationCount = SE->getTruncateOrZeroExtend(IterationCount, LargestType);
00526     if (Instruction *DI = LinearFunctionTestReplace(L, IterationCount,Rewriter))
00527       DeadInsts.insert(DI);
00528   }
00529 
00530   // Now that we have a canonical induction variable, we can rewrite any
00531   // recurrences in terms of the induction variable.  Start with the auxillary
00532   // induction variables, and recursively rewrite any of their uses.
00533   BasicBlock::iterator InsertPt = Header->getFirstNonPHI();
00534 
00535   // If there were induction variables of other sizes, cast the primary
00536   // induction variable to the right size for them, avoiding the need for the
00537   // code evaluation methods to insert induction variables of different sizes.
00538   if (DifferingSizes) {
00539     SmallVector<unsigned,4> InsertedSizes;
00540     InsertedSizes.push_back(LargestType->getPrimitiveSizeInBits());
00541     for (unsigned i = 0, e = IndVars.size(); i != e; ++i) {
00542       unsigned ithSize = IndVars[i].first->getType()->getPrimitiveSizeInBits();
00543       if (std::find(InsertedSizes.begin(), InsertedSizes.end(), ithSize)
00544           == InsertedSizes.end()) {
00545         PHINode *PN = IndVars[i].first;
00546         InsertedSizes.push_back(ithSize);
00547         Instruction *New = new TruncInst(IndVar, PN->getType(), "indvar",
00548                                          InsertPt);
00549         Rewriter.addInsertedValue(New, SE->getSCEV(New));
00550         DOUT << "INDVARS: Made trunc IV for " << *PN
00551              << "   NewVal = " << *New << "\n";
00552       }
00553     }
00554   }
00555 
00556   // Rewrite all induction variables in terms of the canonical induction
00557   // variable.
00558   std::map<unsigned, Value*> InsertedSizes;
00559   while (!IndVars.empty()) {
00560     PHINode *PN = IndVars.back().first;
00561     Value *NewVal = Rewriter.expandCodeFor(IndVars.back().second, InsertPt);
00562     DOUT << "INDVARS: Rewrote IV '" << *IndVars.back().second << "' " << *PN
00563          << "   into = " << *NewVal << "\n";
00564     NewVal->takeName(PN);
00565 
00566     // Replace the old PHI Node with the inserted computation.
00567     PN->replaceAllUsesWith(NewVal);
00568     DeadInsts.insert(PN);
00569     IndVars.pop_back();
00570     ++NumRemoved;
00571     Changed = true;
00572   }
00573 
00574 #if 0
00575   // Now replace all derived expressions in the loop body with simpler
00576   // expressions.
00577   for (LoopInfo::block_iterator I = L->block_begin(), E = L->block_end();
00578        I != E; ++I) {
00579     BasicBlock *BB = *I;
00580     if (LI->getLoopFor(BB) == L) {  // Not in a subloop...
00581       for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
00582         if (I->getType()->isInteger() &&      // Is an integer instruction
00583             !I->use_empty() &&
00584             !Rewriter.isInsertedInstruction(I)) {
00585           SCEVHandle SH = SE->getSCEV(I);
00586           Value *V = Rewriter.expandCodeFor(SH, I, I->getType());
00587           if (V != I) {
00588             if (isa<Instruction>(V))
00589               V->takeName(I);
00590             I->replaceAllUsesWith(V);
00591             DeadInsts.insert(I);
00592             ++NumRemoved;
00593             Changed = true;
00594           }
00595         }
00596     }
00597   }
00598 #endif
00599 
00600   DeleteTriviallyDeadInstructions(DeadInsts);
00601   
00602   assert(L->isLCSSAForm());
00603   return Changed;
00604 }



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.