LLVM API Documentation

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 // If the trip count of a loop is computable, this pass also makes the following
00015 // changes:
00016 //   1. The exit condition for the loop is canonicalized to compare the
00017 //      induction value against the exit value.  This turns loops like:
00018 //        'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)'
00019 //   2. Any use outside of the loop of an expression derived from the indvar
00020 //      is changed to compute the derived value outside of the loop, eliminating
00021 //      the dependence on the exit value of the induction variable.  If the only
00022 //      purpose of the loop is to compute the exit value of some derived
00023 //      expression, this transformation will make the loop dead.
00024 //
00025 //===----------------------------------------------------------------------===//
00026 
00027 #include "llvm/Transforms/Scalar.h"
00028 #include "llvm/ADT/DenseMap.h"
00029 #include "llvm/ADT/SmallVector.h"
00030 #include "llvm/ADT/Statistic.h"
00031 #include "llvm/Analysis/LoopInfo.h"
00032 #include "llvm/Analysis/LoopPass.h"
00033 #include "llvm/Analysis/ScalarEvolutionExpander.h"
00034 #include "llvm/IR/BasicBlock.h"
00035 #include "llvm/IR/CFG.h"
00036 #include "llvm/IR/Constants.h"
00037 #include "llvm/IR/DataLayout.h"
00038 #include "llvm/IR/Dominators.h"
00039 #include "llvm/IR/Instructions.h"
00040 #include "llvm/IR/IntrinsicInst.h"
00041 #include "llvm/IR/LLVMContext.h"
00042 #include "llvm/IR/Type.h"
00043 #include "llvm/Support/CommandLine.h"
00044 #include "llvm/Support/Debug.h"
00045 #include "llvm/Support/raw_ostream.h"
00046 #include "llvm/Target/TargetLibraryInfo.h"
00047 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
00048 #include "llvm/Transforms/Utils/Local.h"
00049 #include "llvm/Transforms/Utils/SimplifyIndVar.h"
00050 using namespace llvm;
00051 
00052 #define DEBUG_TYPE "indvars"
00053 
00054 STATISTIC(NumWidened     , "Number of indvars widened");
00055 STATISTIC(NumReplaced    , "Number of exit values replaced");
00056 STATISTIC(NumLFTR        , "Number of loop exit tests replaced");
00057 STATISTIC(NumElimExt     , "Number of IV sign/zero extends eliminated");
00058 STATISTIC(NumElimIV      , "Number of congruent IVs eliminated");
00059 
00060 // Trip count verification can be enabled by default under NDEBUG if we
00061 // implement a strong expression equivalence checker in SCEV. Until then, we
00062 // use the verify-indvars flag, which may assert in some cases.
00063 static cl::opt<bool> VerifyIndvars(
00064   "verify-indvars", cl::Hidden,
00065   cl::desc("Verify the ScalarEvolution result after running indvars"));
00066 
00067 static cl::opt<bool> ReduceLiveIVs("liv-reduce", cl::Hidden,
00068   cl::desc("Reduce live induction variables."));
00069 
00070 namespace {
00071   class IndVarSimplify : public LoopPass {
00072     LoopInfo        *LI;
00073     ScalarEvolution *SE;
00074     DominatorTree   *DT;
00075     const DataLayout *DL;
00076     TargetLibraryInfo *TLI;
00077 
00078     SmallVector<WeakVH, 16> DeadInsts;
00079     bool Changed;
00080   public:
00081 
00082     static char ID; // Pass identification, replacement for typeid
00083     IndVarSimplify() : LoopPass(ID), LI(nullptr), SE(nullptr), DT(nullptr),
00084                        DL(nullptr), Changed(false) {
00085       initializeIndVarSimplifyPass(*PassRegistry::getPassRegistry());
00086     }
00087 
00088     bool runOnLoop(Loop *L, LPPassManager &LPM) override;
00089 
00090     void getAnalysisUsage(AnalysisUsage &AU) const override {
00091       AU.addRequired<DominatorTreeWrapperPass>();
00092       AU.addRequired<LoopInfo>();
00093       AU.addRequired<ScalarEvolution>();
00094       AU.addRequiredID(LoopSimplifyID);
00095       AU.addRequiredID(LCSSAID);
00096       AU.addPreserved<ScalarEvolution>();
00097       AU.addPreservedID(LoopSimplifyID);
00098       AU.addPreservedID(LCSSAID);
00099       AU.setPreservesCFG();
00100     }
00101 
00102   private:
00103     void releaseMemory() override {
00104       DeadInsts.clear();
00105     }
00106 
00107     bool isValidRewrite(Value *FromVal, Value *ToVal);
00108 
00109     void HandleFloatingPointIV(Loop *L, PHINode *PH);
00110     void RewriteNonIntegerIVs(Loop *L);
00111 
00112     void SimplifyAndExtend(Loop *L, SCEVExpander &Rewriter, LPPassManager &LPM);
00113 
00114     void RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter);
00115 
00116     Value *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount,
00117                                      PHINode *IndVar, SCEVExpander &Rewriter);
00118 
00119     void SinkUnusedInvariants(Loop *L);
00120   };
00121 }
00122 
00123 char IndVarSimplify::ID = 0;
00124 INITIALIZE_PASS_BEGIN(IndVarSimplify, "indvars",
00125                 "Induction Variable Simplification", false, false)
00126 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
00127 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
00128 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
00129 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
00130 INITIALIZE_PASS_DEPENDENCY(LCSSA)
00131 INITIALIZE_PASS_END(IndVarSimplify, "indvars",
00132                 "Induction Variable Simplification", false, false)
00133 
00134 Pass *llvm::createIndVarSimplifyPass() {
00135   return new IndVarSimplify();
00136 }
00137 
00138 /// isValidRewrite - Return true if the SCEV expansion generated by the
00139 /// rewriter can replace the original value. SCEV guarantees that it
00140 /// produces the same value, but the way it is produced may be illegal IR.
00141 /// Ideally, this function will only be called for verification.
00142 bool IndVarSimplify::isValidRewrite(Value *FromVal, Value *ToVal) {
00143   // If an SCEV expression subsumed multiple pointers, its expansion could
00144   // reassociate the GEP changing the base pointer. This is illegal because the
00145   // final address produced by a GEP chain must be inbounds relative to its
00146   // underlying object. Otherwise basic alias analysis, among other things,
00147   // could fail in a dangerous way. Ultimately, SCEV will be improved to avoid
00148   // producing an expression involving multiple pointers. Until then, we must
00149   // bail out here.
00150   //
00151   // Retrieve the pointer operand of the GEP. Don't use GetUnderlyingObject
00152   // because it understands lcssa phis while SCEV does not.
00153   Value *FromPtr = FromVal;
00154   Value *ToPtr = ToVal;
00155   if (GEPOperator *GEP = dyn_cast<GEPOperator>(FromVal)) {
00156     FromPtr = GEP->getPointerOperand();
00157   }
00158   if (GEPOperator *GEP = dyn_cast<GEPOperator>(ToVal)) {
00159     ToPtr = GEP->getPointerOperand();
00160   }
00161   if (FromPtr != FromVal || ToPtr != ToVal) {
00162     // Quickly check the common case
00163     if (FromPtr == ToPtr)
00164       return true;
00165 
00166     // SCEV may have rewritten an expression that produces the GEP's pointer
00167     // operand. That's ok as long as the pointer operand has the same base
00168     // pointer. Unlike GetUnderlyingObject(), getPointerBase() will find the
00169     // base of a recurrence. This handles the case in which SCEV expansion
00170     // converts a pointer type recurrence into a nonrecurrent pointer base
00171     // indexed by an integer recurrence.
00172 
00173     // If the GEP base pointer is a vector of pointers, abort.
00174     if (!FromPtr->getType()->isPointerTy() || !ToPtr->getType()->isPointerTy())
00175       return false;
00176 
00177     const SCEV *FromBase = SE->getPointerBase(SE->getSCEV(FromPtr));
00178     const SCEV *ToBase = SE->getPointerBase(SE->getSCEV(ToPtr));
00179     if (FromBase == ToBase)
00180       return true;
00181 
00182     DEBUG(dbgs() << "INDVARS: GEP rewrite bail out "
00183           << *FromBase << " != " << *ToBase << "\n");
00184 
00185     return false;
00186   }
00187   return true;
00188 }
00189 
00190 /// Determine the insertion point for this user. By default, insert immediately
00191 /// before the user. SCEVExpander or LICM will hoist loop invariants out of the
00192 /// loop. For PHI nodes, there may be multiple uses, so compute the nearest
00193 /// common dominator for the incoming blocks.
00194 static Instruction *getInsertPointForUses(Instruction *User, Value *Def,
00195                                           DominatorTree *DT) {
00196   PHINode *PHI = dyn_cast<PHINode>(User);
00197   if (!PHI)
00198     return User;
00199 
00200   Instruction *InsertPt = nullptr;
00201   for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) {
00202     if (PHI->getIncomingValue(i) != Def)
00203       continue;
00204 
00205     BasicBlock *InsertBB = PHI->getIncomingBlock(i);
00206     if (!InsertPt) {
00207       InsertPt = InsertBB->getTerminator();
00208       continue;
00209     }
00210     InsertBB = DT->findNearestCommonDominator(InsertPt->getParent(), InsertBB);
00211     InsertPt = InsertBB->getTerminator();
00212   }
00213   assert(InsertPt && "Missing phi operand");
00214   assert((!isa<Instruction>(Def) ||
00215           DT->dominates(cast<Instruction>(Def), InsertPt)) &&
00216          "def does not dominate all uses");
00217   return InsertPt;
00218 }
00219 
00220 //===----------------------------------------------------------------------===//
00221 // RewriteNonIntegerIVs and helpers. Prefer integer IVs.
00222 //===----------------------------------------------------------------------===//
00223 
00224 /// ConvertToSInt - Convert APF to an integer, if possible.
00225 static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal) {
00226   bool isExact = false;
00227   // See if we can convert this to an int64_t
00228   uint64_t UIntVal;
00229   if (APF.convertToInteger(&UIntVal, 64, true, APFloat::rmTowardZero,
00230                            &isExact) != APFloat::opOK || !isExact)
00231     return false;
00232   IntVal = UIntVal;
00233   return true;
00234 }
00235 
00236 /// HandleFloatingPointIV - If the loop has floating induction variable
00237 /// then insert corresponding integer induction variable if possible.
00238 /// For example,
00239 /// for(double i = 0; i < 10000; ++i)
00240 ///   bar(i)
00241 /// is converted into
00242 /// for(int i = 0; i < 10000; ++i)
00243 ///   bar((double)i);
00244 ///
00245 void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PN) {
00246   unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
00247   unsigned BackEdge     = IncomingEdge^1;
00248 
00249   // Check incoming value.
00250   ConstantFP *InitValueVal =
00251     dyn_cast<ConstantFP>(PN->getIncomingValue(IncomingEdge));
00252 
00253   int64_t InitValue;
00254   if (!InitValueVal || !ConvertToSInt(InitValueVal->getValueAPF(), InitValue))
00255     return;
00256 
00257   // Check IV increment. Reject this PN if increment operation is not
00258   // an add or increment value can not be represented by an integer.
00259   BinaryOperator *Incr =
00260     dyn_cast<BinaryOperator>(PN->getIncomingValue(BackEdge));
00261   if (Incr == nullptr || Incr->getOpcode() != Instruction::FAdd) return;
00262 
00263   // If this is not an add of the PHI with a constantfp, or if the constant fp
00264   // is not an integer, bail out.
00265   ConstantFP *IncValueVal = dyn_cast<ConstantFP>(Incr->getOperand(1));
00266   int64_t IncValue;
00267   if (IncValueVal == nullptr || Incr->getOperand(0) != PN ||
00268       !ConvertToSInt(IncValueVal->getValueAPF(), IncValue))
00269     return;
00270 
00271   // Check Incr uses. One user is PN and the other user is an exit condition
00272   // used by the conditional terminator.
00273   Value::user_iterator IncrUse = Incr->user_begin();
00274   Instruction *U1 = cast<Instruction>(*IncrUse++);
00275   if (IncrUse == Incr->user_end()) return;
00276   Instruction *U2 = cast<Instruction>(*IncrUse++);
00277   if (IncrUse != Incr->user_end()) return;
00278 
00279   // Find exit condition, which is an fcmp.  If it doesn't exist, or if it isn't
00280   // only used by a branch, we can't transform it.
00281   FCmpInst *Compare = dyn_cast<FCmpInst>(U1);
00282   if (!Compare)
00283     Compare = dyn_cast<FCmpInst>(U2);
00284   if (!Compare || !Compare->hasOneUse() ||
00285       !isa<BranchInst>(Compare->user_back()))
00286     return;
00287 
00288   BranchInst *TheBr = cast<BranchInst>(Compare->user_back());
00289 
00290   // We need to verify that the branch actually controls the iteration count
00291   // of the loop.  If not, the new IV can overflow and no one will notice.
00292   // The branch block must be in the loop and one of the successors must be out
00293   // of the loop.
00294   assert(TheBr->isConditional() && "Can't use fcmp if not conditional");
00295   if (!L->contains(TheBr->getParent()) ||
00296       (L->contains(TheBr->getSuccessor(0)) &&
00297        L->contains(TheBr->getSuccessor(1))))
00298     return;
00299 
00300 
00301   // If it isn't a comparison with an integer-as-fp (the exit value), we can't
00302   // transform it.
00303   ConstantFP *ExitValueVal = dyn_cast<ConstantFP>(Compare->getOperand(1));
00304   int64_t ExitValue;
00305   if (ExitValueVal == nullptr ||
00306       !ConvertToSInt(ExitValueVal->getValueAPF(), ExitValue))
00307     return;
00308 
00309   // Find new predicate for integer comparison.
00310   CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE;
00311   switch (Compare->getPredicate()) {
00312   default: return;  // Unknown comparison.
00313   case CmpInst::FCMP_OEQ:
00314   case CmpInst::FCMP_UEQ: NewPred = CmpInst::ICMP_EQ; break;
00315   case CmpInst::FCMP_ONE:
00316   case CmpInst::FCMP_UNE: NewPred = CmpInst::ICMP_NE; break;
00317   case CmpInst::FCMP_OGT:
00318   case CmpInst::FCMP_UGT: NewPred = CmpInst::ICMP_SGT; break;
00319   case CmpInst::FCMP_OGE:
00320   case CmpInst::FCMP_UGE: NewPred = CmpInst::ICMP_SGE; break;
00321   case CmpInst::FCMP_OLT:
00322   case CmpInst::FCMP_ULT: NewPred = CmpInst::ICMP_SLT; break;
00323   case CmpInst::FCMP_OLE:
00324   case CmpInst::FCMP_ULE: NewPred = CmpInst::ICMP_SLE; break;
00325   }
00326 
00327   // We convert the floating point induction variable to a signed i32 value if
00328   // we can.  This is only safe if the comparison will not overflow in a way
00329   // that won't be trapped by the integer equivalent operations.  Check for this
00330   // now.
00331   // TODO: We could use i64 if it is native and the range requires it.
00332 
00333   // The start/stride/exit values must all fit in signed i32.
00334   if (!isInt<32>(InitValue) || !isInt<32>(IncValue) || !isInt<32>(ExitValue))
00335     return;
00336 
00337   // If not actually striding (add x, 0.0), avoid touching the code.
00338   if (IncValue == 0)
00339     return;
00340 
00341   // Positive and negative strides have different safety conditions.
00342   if (IncValue > 0) {
00343     // If we have a positive stride, we require the init to be less than the
00344     // exit value.
00345     if (InitValue >= ExitValue)
00346       return;
00347 
00348     uint32_t Range = uint32_t(ExitValue-InitValue);
00349     // Check for infinite loop, either:
00350     // while (i <= Exit) or until (i > Exit)
00351     if (NewPred == CmpInst::ICMP_SLE || NewPred == CmpInst::ICMP_SGT) {
00352       if (++Range == 0) return;  // Range overflows.
00353     }
00354 
00355     unsigned Leftover = Range % uint32_t(IncValue);
00356 
00357     // If this is an equality comparison, we require that the strided value
00358     // exactly land on the exit value, otherwise the IV condition will wrap
00359     // around and do things the fp IV wouldn't.
00360     if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) &&
00361         Leftover != 0)
00362       return;
00363 
00364     // If the stride would wrap around the i32 before exiting, we can't
00365     // transform the IV.
00366     if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue)
00367       return;
00368 
00369   } else {
00370     // If we have a negative stride, we require the init to be greater than the
00371     // exit value.
00372     if (InitValue <= ExitValue)
00373       return;
00374 
00375     uint32_t Range = uint32_t(InitValue-ExitValue);
00376     // Check for infinite loop, either:
00377     // while (i >= Exit) or until (i < Exit)
00378     if (NewPred == CmpInst::ICMP_SGE || NewPred == CmpInst::ICMP_SLT) {
00379       if (++Range == 0) return;  // Range overflows.
00380     }
00381 
00382     unsigned Leftover = Range % uint32_t(-IncValue);
00383 
00384     // If this is an equality comparison, we require that the strided value
00385     // exactly land on the exit value, otherwise the IV condition will wrap
00386     // around and do things the fp IV wouldn't.
00387     if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) &&
00388         Leftover != 0)
00389       return;
00390 
00391     // If the stride would wrap around the i32 before exiting, we can't
00392     // transform the IV.
00393     if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue)
00394       return;
00395   }
00396 
00397   IntegerType *Int32Ty = Type::getInt32Ty(PN->getContext());
00398 
00399   // Insert new integer induction variable.
00400   PHINode *NewPHI = PHINode::Create(Int32Ty, 2, PN->getName()+".int", PN);
00401   NewPHI->addIncoming(ConstantInt::get(Int32Ty, InitValue),
00402                       PN->getIncomingBlock(IncomingEdge));
00403 
00404   Value *NewAdd =
00405     BinaryOperator::CreateAdd(NewPHI, ConstantInt::get(Int32Ty, IncValue),
00406                               Incr->getName()+".int", Incr);
00407   NewPHI->addIncoming(NewAdd, PN->getIncomingBlock(BackEdge));
00408 
00409   ICmpInst *NewCompare = new ICmpInst(TheBr, NewPred, NewAdd,
00410                                       ConstantInt::get(Int32Ty, ExitValue),
00411                                       Compare->getName());
00412 
00413   // In the following deletions, PN may become dead and may be deleted.
00414   // Use a WeakVH to observe whether this happens.
00415   WeakVH WeakPH = PN;
00416 
00417   // Delete the old floating point exit comparison.  The branch starts using the
00418   // new comparison.
00419   NewCompare->takeName(Compare);
00420   Compare->replaceAllUsesWith(NewCompare);
00421   RecursivelyDeleteTriviallyDeadInstructions(Compare, TLI);
00422 
00423   // Delete the old floating point increment.
00424   Incr->replaceAllUsesWith(UndefValue::get(Incr->getType()));
00425   RecursivelyDeleteTriviallyDeadInstructions(Incr, TLI);
00426 
00427   // If the FP induction variable still has uses, this is because something else
00428   // in the loop uses its value.  In order to canonicalize the induction
00429   // variable, we chose to eliminate the IV and rewrite it in terms of an
00430   // int->fp cast.
00431   //
00432   // We give preference to sitofp over uitofp because it is faster on most
00433   // platforms.
00434   if (WeakPH) {
00435     Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv",
00436                                  PN->getParent()->getFirstInsertionPt());
00437     PN->replaceAllUsesWith(Conv);
00438     RecursivelyDeleteTriviallyDeadInstructions(PN, TLI);
00439   }
00440   Changed = true;
00441 }
00442 
00443 void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
00444   // First step.  Check to see if there are any floating-point recurrences.
00445   // If there are, change them into integer recurrences, permitting analysis by
00446   // the SCEV routines.
00447   //
00448   BasicBlock *Header = L->getHeader();
00449 
00450   SmallVector<WeakVH, 8> PHIs;
00451   for (BasicBlock::iterator I = Header->begin();
00452        PHINode *PN = dyn_cast<PHINode>(I); ++I)
00453     PHIs.push_back(PN);
00454 
00455   for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
00456     if (PHINode *PN = dyn_cast_or_null<PHINode>(&*PHIs[i]))
00457       HandleFloatingPointIV(L, PN);
00458 
00459   // If the loop previously had floating-point IV, ScalarEvolution
00460   // may not have been able to compute a trip count. Now that we've done some
00461   // re-writing, the trip count may be computable.
00462   if (Changed)
00463     SE->forgetLoop(L);
00464 }
00465 
00466 //===----------------------------------------------------------------------===//
00467 // RewriteLoopExitValues - Optimize IV users outside the loop.
00468 // As a side effect, reduces the amount of IV processing within the loop.
00469 //===----------------------------------------------------------------------===//
00470 
00471 /// RewriteLoopExitValues - Check to see if this loop has a computable
00472 /// loop-invariant execution count.  If so, this means that we can compute the
00473 /// final value of any expressions that are recurrent in the loop, and
00474 /// substitute the exit values from the loop into any instructions outside of
00475 /// the loop that use the final values of the current expressions.
00476 ///
00477 /// This is mostly redundant with the regular IndVarSimplify activities that
00478 /// happen later, except that it's more powerful in some cases, because it's
00479 /// able to brute-force evaluate arbitrary instructions as long as they have
00480 /// constant operands at the beginning of the loop.
00481 void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter) {
00482   // Verify the input to the pass in already in LCSSA form.
00483   assert(L->isLCSSAForm(*DT));
00484 
00485   SmallVector<BasicBlock*, 8> ExitBlocks;
00486   L->getUniqueExitBlocks(ExitBlocks);
00487 
00488   // Find all values that are computed inside the loop, but used outside of it.
00489   // Because of LCSSA, these values will only occur in LCSSA PHI Nodes.  Scan
00490   // the exit blocks of the loop to find them.
00491   for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) {
00492     BasicBlock *ExitBB = ExitBlocks[i];
00493 
00494     // If there are no PHI nodes in this exit block, then no values defined
00495     // inside the loop are used on this path, skip it.
00496     PHINode *PN = dyn_cast<PHINode>(ExitBB->begin());
00497     if (!PN) continue;
00498 
00499     unsigned NumPreds = PN->getNumIncomingValues();
00500 
00501     // We would like to be able to RAUW single-incoming value PHI nodes. We
00502     // have to be certain this is safe even when this is an LCSSA PHI node.
00503     // While the computed exit value is no longer varying in *this* loop, the
00504     // exit block may be an exit block for an outer containing loop as well,
00505     // the exit value may be varying in the outer loop, and thus it may still
00506     // require an LCSSA PHI node. The safe case is when this is
00507     // single-predecessor PHI node (LCSSA) and the exit block containing it is
00508     // part of the enclosing loop, or this is the outer most loop of the nest.
00509     // In either case the exit value could (at most) be varying in the same
00510     // loop body as the phi node itself. Thus if it is in turn used outside of
00511     // an enclosing loop it will only be via a separate LCSSA node.
00512     bool LCSSASafePhiForRAUW =
00513         NumPreds == 1 &&
00514         (!L->getParentLoop() || L->getParentLoop() == LI->getLoopFor(ExitBB));
00515 
00516     // Iterate over all of the PHI nodes.
00517     BasicBlock::iterator BBI = ExitBB->begin();
00518     while ((PN = dyn_cast<PHINode>(BBI++))) {
00519       if (PN->use_empty())
00520         continue; // dead use, don't replace it
00521 
00522       // SCEV only supports integer expressions for now.
00523       if (!PN->getType()->isIntegerTy() && !PN->getType()->isPointerTy())
00524         continue;
00525 
00526       // It's necessary to tell ScalarEvolution about this explicitly so that
00527       // it can walk the def-use list and forget all SCEVs, as it may not be
00528       // watching the PHI itself. Once the new exit value is in place, there
00529       // may not be a def-use connection between the loop and every instruction
00530       // which got a SCEVAddRecExpr for that loop.
00531       SE->forgetValue(PN);
00532 
00533       // Iterate over all of the values in all the PHI nodes.
00534       for (unsigned i = 0; i != NumPreds; ++i) {
00535         // If the value being merged in is not integer or is not defined
00536         // in the loop, skip it.
00537         Value *InVal = PN->getIncomingValue(i);
00538         if (!isa<Instruction>(InVal))
00539           continue;
00540 
00541         // If this pred is for a subloop, not L itself, skip it.
00542         if (LI->getLoopFor(PN->getIncomingBlock(i)) != L)
00543           continue; // The Block is in a subloop, skip it.
00544 
00545         // Check that InVal is defined in the loop.
00546         Instruction *Inst = cast<Instruction>(InVal);
00547         if (!L->contains(Inst))
00548           continue;
00549 
00550         // Okay, this instruction has a user outside of the current loop
00551         // and varies predictably *inside* the loop.  Evaluate the value it
00552         // contains when the loop exits, if possible.
00553         const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop());
00554         if (!SE->isLoopInvariant(ExitValue, L) ||
00555             !isSafeToExpand(ExitValue, *SE))
00556           continue;
00557 
00558         // Computing the value outside of the loop brings no benefit if :
00559         //  - it is definitely used inside the loop in a way which can not be
00560         //    optimized away.
00561         //  - no use outside of the loop can take advantage of hoisting the
00562         //    computation out of the loop
00563         if (ExitValue->getSCEVType()>=scMulExpr) {
00564           unsigned NumHardInternalUses = 0;
00565           unsigned NumSoftExternalUses = 0;
00566           unsigned NumUses = 0;
00567           for (auto IB = Inst->user_begin(), IE = Inst->user_end();
00568                IB != IE && NumUses <= 6; ++IB) {
00569             Instruction *UseInstr = cast<Instruction>(*IB);
00570             unsigned Opc = UseInstr->getOpcode();
00571             NumUses++;
00572             if (L->contains(UseInstr)) {
00573               if (Opc == Instruction::Call || Opc == Instruction::Ret)
00574                 NumHardInternalUses++;
00575             } else {
00576               if (Opc == Instruction::PHI) {
00577                 // Do not count the Phi as a use. LCSSA may have inserted
00578                 // plenty of trivial ones.
00579                 NumUses--;
00580                 for (auto PB = UseInstr->user_begin(),
00581                           PE = UseInstr->user_end();
00582                      PB != PE && NumUses <= 6; ++PB, ++NumUses) {
00583                   unsigned PhiOpc = cast<Instruction>(*PB)->getOpcode();
00584                   if (PhiOpc != Instruction::Call && PhiOpc != Instruction::Ret)
00585                     NumSoftExternalUses++;
00586                 }
00587                 continue;
00588               }
00589               if (Opc != Instruction::Call && Opc != Instruction::Ret)
00590                 NumSoftExternalUses++;
00591             }
00592           }
00593           if (NumUses <= 6 && NumHardInternalUses && !NumSoftExternalUses)
00594             continue;
00595         }
00596 
00597         Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst);
00598 
00599         DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n'
00600                      << "  LoopVal = " << *Inst << "\n");
00601 
00602         if (!isValidRewrite(Inst, ExitVal)) {
00603           DeadInsts.push_back(ExitVal);
00604           continue;
00605         }
00606         Changed = true;
00607         ++NumReplaced;
00608 
00609         PN->setIncomingValue(i, ExitVal);
00610 
00611         // If this instruction is dead now, delete it. Don't do it now to avoid
00612         // invalidating iterators.
00613         if (isInstructionTriviallyDead(Inst, TLI))
00614           DeadInsts.push_back(Inst);
00615 
00616         // If we determined that this PHI is safe to replace even if an LCSSA
00617         // PHI, do so.
00618         if (LCSSASafePhiForRAUW) {
00619           PN->replaceAllUsesWith(ExitVal);
00620           PN->eraseFromParent();
00621         }
00622       }
00623 
00624       // If we were unable to completely replace the PHI node, clone the PHI
00625       // and delete the original one. This lets IVUsers and any other maps
00626       // purge the original user from their records.
00627       if (!LCSSASafePhiForRAUW) {
00628         PHINode *NewPN = cast<PHINode>(PN->clone());
00629         NewPN->takeName(PN);
00630         NewPN->insertBefore(PN);
00631         PN->replaceAllUsesWith(NewPN);
00632         PN->eraseFromParent();
00633       }
00634     }
00635   }
00636 
00637   // The insertion point instruction may have been deleted; clear it out
00638   // so that the rewriter doesn't trip over it later.
00639   Rewriter.clearInsertPoint();
00640 }
00641 
00642 //===----------------------------------------------------------------------===//
00643 //  IV Widening - Extend the width of an IV to cover its widest uses.
00644 //===----------------------------------------------------------------------===//
00645 
00646 namespace {
00647   // Collect information about induction variables that are used by sign/zero
00648   // extend operations. This information is recorded by CollectExtend and
00649   // provides the input to WidenIV.
00650   struct WideIVInfo {
00651     PHINode *NarrowIV;
00652     Type *WidestNativeType; // Widest integer type created [sz]ext
00653     bool IsSigned;          // Was an sext user seen before a zext?
00654 
00655     WideIVInfo() : NarrowIV(nullptr), WidestNativeType(nullptr),
00656                    IsSigned(false) {}
00657   };
00658 }
00659 
00660 /// visitCast - Update information about the induction variable that is
00661 /// extended by this sign or zero extend operation. This is used to determine
00662 /// the final width of the IV before actually widening it.
00663 static void visitIVCast(CastInst *Cast, WideIVInfo &WI, ScalarEvolution *SE,
00664                         const DataLayout *DL) {
00665   bool IsSigned = Cast->getOpcode() == Instruction::SExt;
00666   if (!IsSigned && Cast->getOpcode() != Instruction::ZExt)
00667     return;
00668 
00669   Type *Ty = Cast->getType();
00670   uint64_t Width = SE->getTypeSizeInBits(Ty);
00671   if (DL && !DL->isLegalInteger(Width))
00672     return;
00673 
00674   if (!WI.WidestNativeType) {
00675     WI.WidestNativeType = SE->getEffectiveSCEVType(Ty);
00676     WI.IsSigned = IsSigned;
00677     return;
00678   }
00679 
00680   // We extend the IV to satisfy the sign of its first user, arbitrarily.
00681   if (WI.IsSigned != IsSigned)
00682     return;
00683 
00684   if (Width > SE->getTypeSizeInBits(WI.WidestNativeType))
00685     WI.WidestNativeType = SE->getEffectiveSCEVType(Ty);
00686 }
00687 
00688 namespace {
00689 
00690 /// NarrowIVDefUse - Record a link in the Narrow IV def-use chain along with the
00691 /// WideIV that computes the same value as the Narrow IV def.  This avoids
00692 /// caching Use* pointers.
00693 struct NarrowIVDefUse {
00694   Instruction *NarrowDef;
00695   Instruction *NarrowUse;
00696   Instruction *WideDef;
00697 
00698   NarrowIVDefUse(): NarrowDef(nullptr), NarrowUse(nullptr), WideDef(nullptr) {}
00699 
00700   NarrowIVDefUse(Instruction *ND, Instruction *NU, Instruction *WD):
00701     NarrowDef(ND), NarrowUse(NU), WideDef(WD) {}
00702 };
00703 
00704 /// WidenIV - The goal of this transform is to remove sign and zero extends
00705 /// without creating any new induction variables. To do this, it creates a new
00706 /// phi of the wider type and redirects all users, either removing extends or
00707 /// inserting truncs whenever we stop propagating the type.
00708 ///
00709 class WidenIV {
00710   // Parameters
00711   PHINode *OrigPhi;
00712   Type *WideType;
00713   bool IsSigned;
00714 
00715   // Context
00716   LoopInfo        *LI;
00717   Loop            *L;
00718   ScalarEvolution *SE;
00719   DominatorTree   *DT;
00720 
00721   // Result
00722   PHINode *WidePhi;
00723   Instruction *WideInc;
00724   const SCEV *WideIncExpr;
00725   SmallVectorImpl<WeakVH> &DeadInsts;
00726 
00727   SmallPtrSet<Instruction*,16> Widened;
00728   SmallVector<NarrowIVDefUse, 8> NarrowIVUsers;
00729 
00730 public:
00731   WidenIV(const WideIVInfo &WI, LoopInfo *LInfo,
00732           ScalarEvolution *SEv, DominatorTree *DTree,
00733           SmallVectorImpl<WeakVH> &DI) :
00734     OrigPhi(WI.NarrowIV),
00735     WideType(WI.WidestNativeType),
00736     IsSigned(WI.IsSigned),
00737     LI(LInfo),
00738     L(LI->getLoopFor(OrigPhi->getParent())),
00739     SE(SEv),
00740     DT(DTree),
00741     WidePhi(nullptr),
00742     WideInc(nullptr),
00743     WideIncExpr(nullptr),
00744     DeadInsts(DI) {
00745     assert(L->getHeader() == OrigPhi->getParent() && "Phi must be an IV");
00746   }
00747 
00748   PHINode *CreateWideIV(SCEVExpander &Rewriter);
00749 
00750 protected:
00751   Value *getExtend(Value *NarrowOper, Type *WideType, bool IsSigned,
00752                    Instruction *Use);
00753 
00754   Instruction *CloneIVUser(NarrowIVDefUse DU);
00755 
00756   const SCEVAddRecExpr *GetWideRecurrence(Instruction *NarrowUse);
00757 
00758   const SCEVAddRecExpr* GetExtendedOperandRecurrence(NarrowIVDefUse DU);
00759 
00760   Instruction *WidenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter);
00761 
00762   void pushNarrowIVUsers(Instruction *NarrowDef, Instruction *WideDef);
00763 };
00764 } // anonymous namespace
00765 
00766 /// isLoopInvariant - Perform a quick domtree based check for loop invariance
00767 /// assuming that V is used within the loop. LoopInfo::isLoopInvariant() seems
00768 /// gratuitous for this purpose.
00769 static bool isLoopInvariant(Value *V, const Loop *L, const DominatorTree *DT) {
00770   Instruction *Inst = dyn_cast<Instruction>(V);
00771   if (!Inst)
00772     return true;
00773 
00774   return DT->properlyDominates(Inst->getParent(), L->getHeader());
00775 }
00776 
00777 Value *WidenIV::getExtend(Value *NarrowOper, Type *WideType, bool IsSigned,
00778                           Instruction *Use) {
00779   // Set the debug location and conservative insertion point.
00780   IRBuilder<> Builder(Use);
00781   // Hoist the insertion point into loop preheaders as far as possible.
00782   for (const Loop *L = LI->getLoopFor(Use->getParent());
00783        L && L->getLoopPreheader() && isLoopInvariant(NarrowOper, L, DT);
00784        L = L->getParentLoop())
00785     Builder.SetInsertPoint(L->getLoopPreheader()->getTerminator());
00786 
00787   return IsSigned ? Builder.CreateSExt(NarrowOper, WideType) :
00788                     Builder.CreateZExt(NarrowOper, WideType);
00789 }
00790 
00791 /// CloneIVUser - Instantiate a wide operation to replace a narrow
00792 /// operation. This only needs to handle operations that can evaluation to
00793 /// SCEVAddRec. It can safely return 0 for any operation we decide not to clone.
00794 Instruction *WidenIV::CloneIVUser(NarrowIVDefUse DU) {
00795   unsigned Opcode = DU.NarrowUse->getOpcode();
00796   switch (Opcode) {
00797   default:
00798     return nullptr;
00799   case Instruction::Add:
00800   case Instruction::Mul:
00801   case Instruction::UDiv:
00802   case Instruction::Sub:
00803   case Instruction::And:
00804   case Instruction::Or:
00805   case Instruction::Xor:
00806   case Instruction::Shl:
00807   case Instruction::LShr:
00808   case Instruction::AShr:
00809     DEBUG(dbgs() << "Cloning IVUser: " << *DU.NarrowUse << "\n");
00810 
00811     // Replace NarrowDef operands with WideDef. Otherwise, we don't know
00812     // anything about the narrow operand yet so must insert a [sz]ext. It is
00813     // probably loop invariant and will be folded or hoisted. If it actually
00814     // comes from a widened IV, it should be removed during a future call to
00815     // WidenIVUse.
00816     Value *LHS = (DU.NarrowUse->getOperand(0) == DU.NarrowDef) ? DU.WideDef :
00817       getExtend(DU.NarrowUse->getOperand(0), WideType, IsSigned, DU.NarrowUse);
00818     Value *RHS = (DU.NarrowUse->getOperand(1) == DU.NarrowDef) ? DU.WideDef :
00819       getExtend(DU.NarrowUse->getOperand(1), WideType, IsSigned, DU.NarrowUse);
00820 
00821     BinaryOperator *NarrowBO = cast<BinaryOperator>(DU.NarrowUse);
00822     BinaryOperator *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(),
00823                                                     LHS, RHS,
00824                                                     NarrowBO->getName());
00825     IRBuilder<> Builder(DU.NarrowUse);
00826     Builder.Insert(WideBO);
00827     if (const OverflowingBinaryOperator *OBO =
00828         dyn_cast<OverflowingBinaryOperator>(NarrowBO)) {
00829       if (OBO->hasNoUnsignedWrap()) WideBO->setHasNoUnsignedWrap();
00830       if (OBO->hasNoSignedWrap()) WideBO->setHasNoSignedWrap();
00831     }
00832     return WideBO;
00833   }
00834 }
00835 
00836 /// No-wrap operations can transfer sign extension of their result to their
00837 /// operands. Generate the SCEV value for the widened operation without
00838 /// actually modifying the IR yet. If the expression after extending the
00839 /// operands is an AddRec for this loop, return it.
00840 const SCEVAddRecExpr* WidenIV::GetExtendedOperandRecurrence(NarrowIVDefUse DU) {
00841   // Handle the common case of add<nsw/nuw>
00842   if (DU.NarrowUse->getOpcode() != Instruction::Add)
00843     return nullptr;
00844 
00845   // One operand (NarrowDef) has already been extended to WideDef. Now determine
00846   // if extending the other will lead to a recurrence.
00847   unsigned ExtendOperIdx = DU.NarrowUse->getOperand(0) == DU.NarrowDef ? 1 : 0;
00848   assert(DU.NarrowUse->getOperand(1-ExtendOperIdx) == DU.NarrowDef && "bad DU");
00849 
00850   const SCEV *ExtendOperExpr = nullptr;
00851   const OverflowingBinaryOperator *OBO =
00852     cast<OverflowingBinaryOperator>(DU.NarrowUse);
00853   if (IsSigned && OBO->hasNoSignedWrap())
00854     ExtendOperExpr = SE->getSignExtendExpr(
00855       SE->getSCEV(DU.NarrowUse->getOperand(ExtendOperIdx)), WideType);
00856   else if(!IsSigned && OBO->hasNoUnsignedWrap())
00857     ExtendOperExpr = SE->getZeroExtendExpr(
00858       SE->getSCEV(DU.NarrowUse->getOperand(ExtendOperIdx)), WideType);
00859   else
00860     return nullptr;
00861 
00862   // When creating this AddExpr, don't apply the current operations NSW or NUW
00863   // flags. This instruction may be guarded by control flow that the no-wrap
00864   // behavior depends on. Non-control-equivalent instructions can be mapped to
00865   // the same SCEV expression, and it would be incorrect to transfer NSW/NUW
00866   // semantics to those operations.
00867   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(
00868     SE->getAddExpr(SE->getSCEV(DU.WideDef), ExtendOperExpr));
00869 
00870   if (!AddRec || AddRec->getLoop() != L)
00871     return nullptr;
00872   return AddRec;
00873 }
00874 
00875 /// GetWideRecurrence - Is this instruction potentially interesting from
00876 /// IVUsers' perspective after widening it's type? In other words, can the
00877 /// extend be safely hoisted out of the loop with SCEV reducing the value to a
00878 /// recurrence on the same loop. If so, return the sign or zero extended
00879 /// recurrence. Otherwise return NULL.
00880 const SCEVAddRecExpr *WidenIV::GetWideRecurrence(Instruction *NarrowUse) {
00881   if (!SE->isSCEVable(NarrowUse->getType()))
00882     return nullptr;
00883 
00884   const SCEV *NarrowExpr = SE->getSCEV(NarrowUse);
00885   if (SE->getTypeSizeInBits(NarrowExpr->getType())
00886       >= SE->getTypeSizeInBits(WideType)) {
00887     // NarrowUse implicitly widens its operand. e.g. a gep with a narrow
00888     // index. So don't follow this use.
00889     return nullptr;
00890   }
00891 
00892   const SCEV *WideExpr = IsSigned ?
00893     SE->getSignExtendExpr(NarrowExpr, WideType) :
00894     SE->getZeroExtendExpr(NarrowExpr, WideType);
00895   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(WideExpr);
00896   if (!AddRec || AddRec->getLoop() != L)
00897     return nullptr;
00898   return AddRec;
00899 }
00900 
00901 /// This IV user cannot be widen. Replace this use of the original narrow IV
00902 /// with a truncation of the new wide IV to isolate and eliminate the narrow IV.
00903 static void truncateIVUse(NarrowIVDefUse DU, DominatorTree *DT) {
00904   DEBUG(dbgs() << "INDVARS: Truncate IV " << *DU.WideDef
00905         << " for user " << *DU.NarrowUse << "\n");
00906   IRBuilder<> Builder(getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT));
00907   Value *Trunc = Builder.CreateTrunc(DU.WideDef, DU.NarrowDef->getType());
00908   DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, Trunc);
00909 }
00910 
00911 /// WidenIVUse - Determine whether an individual user of the narrow IV can be
00912 /// widened. If so, return the wide clone of the user.
00913 Instruction *WidenIV::WidenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter) {
00914 
00915   // Stop traversing the def-use chain at inner-loop phis or post-loop phis.
00916   if (PHINode *UsePhi = dyn_cast<PHINode>(DU.NarrowUse)) {
00917     if (LI->getLoopFor(UsePhi->getParent()) != L) {
00918       // For LCSSA phis, sink the truncate outside the loop.
00919       // After SimplifyCFG most loop exit targets have a single predecessor.
00920       // Otherwise fall back to a truncate within the loop.
00921       if (UsePhi->getNumOperands() != 1)
00922         truncateIVUse(DU, DT);
00923       else {
00924         PHINode *WidePhi =
00925           PHINode::Create(DU.WideDef->getType(), 1, UsePhi->getName() + ".wide",
00926                           UsePhi);
00927         WidePhi->addIncoming(DU.WideDef, UsePhi->getIncomingBlock(0));
00928         IRBuilder<> Builder(WidePhi->getParent()->getFirstInsertionPt());
00929         Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType());
00930         UsePhi->replaceAllUsesWith(Trunc);
00931         DeadInsts.push_back(UsePhi);
00932         DEBUG(dbgs() << "INDVARS: Widen lcssa phi " << *UsePhi
00933               << " to " << *WidePhi << "\n");
00934       }
00935       return nullptr;
00936     }
00937   }
00938   // Our raison d'etre! Eliminate sign and zero extension.
00939   if (IsSigned ? isa<SExtInst>(DU.NarrowUse) : isa<ZExtInst>(DU.NarrowUse)) {
00940     Value *NewDef = DU.WideDef;
00941     if (DU.NarrowUse->getType() != WideType) {
00942       unsigned CastWidth = SE->getTypeSizeInBits(DU.NarrowUse->getType());
00943       unsigned IVWidth = SE->getTypeSizeInBits(WideType);
00944       if (CastWidth < IVWidth) {
00945         // The cast isn't as wide as the IV, so insert a Trunc.
00946         IRBuilder<> Builder(DU.NarrowUse);
00947         NewDef = Builder.CreateTrunc(DU.WideDef, DU.NarrowUse->getType());
00948       }
00949       else {
00950         // A wider extend was hidden behind a narrower one. This may induce
00951         // another round of IV widening in which the intermediate IV becomes
00952         // dead. It should be very rare.
00953         DEBUG(dbgs() << "INDVARS: New IV " << *WidePhi
00954               << " not wide enough to subsume " << *DU.NarrowUse << "\n");
00955         DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, DU.WideDef);
00956         NewDef = DU.NarrowUse;
00957       }
00958     }
00959     if (NewDef != DU.NarrowUse) {
00960       DEBUG(dbgs() << "INDVARS: eliminating " << *DU.NarrowUse
00961             << " replaced by " << *DU.WideDef << "\n");
00962       ++NumElimExt;
00963       DU.NarrowUse->replaceAllUsesWith(NewDef);
00964       DeadInsts.push_back(DU.NarrowUse);
00965     }
00966     // Now that the extend is gone, we want to expose it's uses for potential
00967     // further simplification. We don't need to directly inform SimplifyIVUsers
00968     // of the new users, because their parent IV will be processed later as a
00969     // new loop phi. If we preserved IVUsers analysis, we would also want to
00970     // push the uses of WideDef here.
00971 
00972     // No further widening is needed. The deceased [sz]ext had done it for us.
00973     return nullptr;
00974   }
00975 
00976   // Does this user itself evaluate to a recurrence after widening?
00977   const SCEVAddRecExpr *WideAddRec = GetWideRecurrence(DU.NarrowUse);
00978   if (!WideAddRec) {
00979       WideAddRec = GetExtendedOperandRecurrence(DU);
00980   }
00981   if (!WideAddRec) {
00982     // This user does not evaluate to a recurence after widening, so don't
00983     // follow it. Instead insert a Trunc to kill off the original use,
00984     // eventually isolating the original narrow IV so it can be removed.
00985     truncateIVUse(DU, DT);
00986     return nullptr;
00987   }
00988   // Assume block terminators cannot evaluate to a recurrence. We can't to
00989   // insert a Trunc after a terminator if there happens to be a critical edge.
00990   assert(DU.NarrowUse != DU.NarrowUse->getParent()->getTerminator() &&
00991          "SCEV is not expected to evaluate a block terminator");
00992 
00993   // Reuse the IV increment that SCEVExpander created as long as it dominates
00994   // NarrowUse.
00995   Instruction *WideUse = nullptr;
00996   if (WideAddRec == WideIncExpr
00997       && Rewriter.hoistIVInc(WideInc, DU.NarrowUse))
00998     WideUse = WideInc;
00999   else {
01000     WideUse = CloneIVUser(DU);
01001     if (!WideUse)
01002       return nullptr;
01003   }
01004   // Evaluation of WideAddRec ensured that the narrow expression could be
01005   // extended outside the loop without overflow. This suggests that the wide use
01006   // evaluates to the same expression as the extended narrow use, but doesn't
01007   // absolutely guarantee it. Hence the following failsafe check. In rare cases
01008   // where it fails, we simply throw away the newly created wide use.
01009   if (WideAddRec != SE->getSCEV(WideUse)) {
01010     DEBUG(dbgs() << "Wide use expression mismatch: " << *WideUse
01011           << ": " << *SE->getSCEV(WideUse) << " != " << *WideAddRec << "\n");
01012     DeadInsts.push_back(WideUse);
01013     return nullptr;
01014   }
01015 
01016   // Returning WideUse pushes it on the worklist.
01017   return WideUse;
01018 }
01019 
01020 /// pushNarrowIVUsers - Add eligible users of NarrowDef to NarrowIVUsers.
01021 ///
01022 void WidenIV::pushNarrowIVUsers(Instruction *NarrowDef, Instruction *WideDef) {
01023   for (User *U : NarrowDef->users()) {
01024     Instruction *NarrowUser = cast<Instruction>(U);
01025 
01026     // Handle data flow merges and bizarre phi cycles.
01027     if (!Widened.insert(NarrowUser))
01028       continue;
01029 
01030     NarrowIVUsers.push_back(NarrowIVDefUse(NarrowDef, NarrowUser, WideDef));
01031   }
01032 }
01033 
01034 /// CreateWideIV - Process a single induction variable. First use the
01035 /// SCEVExpander to create a wide induction variable that evaluates to the same
01036 /// recurrence as the original narrow IV. Then use a worklist to forward
01037 /// traverse the narrow IV's def-use chain. After WidenIVUse has processed all
01038 /// interesting IV users, the narrow IV will be isolated for removal by
01039 /// DeleteDeadPHIs.
01040 ///
01041 /// It would be simpler to delete uses as they are processed, but we must avoid
01042 /// invalidating SCEV expressions.
01043 ///
01044 PHINode *WidenIV::CreateWideIV(SCEVExpander &Rewriter) {
01045   // Is this phi an induction variable?
01046   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(OrigPhi));
01047   if (!AddRec)
01048     return nullptr;
01049 
01050   // Widen the induction variable expression.
01051   const SCEV *WideIVExpr = IsSigned ?
01052     SE->getSignExtendExpr(AddRec, WideType) :
01053     SE->getZeroExtendExpr(AddRec, WideType);
01054 
01055   assert(SE->getEffectiveSCEVType(WideIVExpr->getType()) == WideType &&
01056          "Expect the new IV expression to preserve its type");
01057 
01058   // Can the IV be extended outside the loop without overflow?
01059   AddRec = dyn_cast<SCEVAddRecExpr>(WideIVExpr);
01060   if (!AddRec || AddRec->getLoop() != L)
01061     return nullptr;
01062 
01063   // An AddRec must have loop-invariant operands. Since this AddRec is
01064   // materialized by a loop header phi, the expression cannot have any post-loop
01065   // operands, so they must dominate the loop header.
01066   assert(SE->properlyDominates(AddRec->getStart(), L->getHeader()) &&
01067          SE->properlyDominates(AddRec->getStepRecurrence(*SE), L->getHeader())
01068          && "Loop header phi recurrence inputs do not dominate the loop");
01069 
01070   // The rewriter provides a value for the desired IV expression. This may
01071   // either find an existing phi or materialize a new one. Either way, we
01072   // expect a well-formed cyclic phi-with-increments. i.e. any operand not part
01073   // of the phi-SCC dominates the loop entry.
01074   Instruction *InsertPt = L->getHeader()->begin();
01075   WidePhi = cast<PHINode>(Rewriter.expandCodeFor(AddRec, WideType, InsertPt));
01076 
01077   // Remembering the WideIV increment generated by SCEVExpander allows
01078   // WidenIVUse to reuse it when widening the narrow IV's increment. We don't
01079   // employ a general reuse mechanism because the call above is the only call to
01080   // SCEVExpander. Henceforth, we produce 1-to-1 narrow to wide uses.
01081   if (BasicBlock *LatchBlock = L->getLoopLatch()) {
01082     WideInc =
01083       cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock));
01084     WideIncExpr = SE->getSCEV(WideInc);
01085   }
01086 
01087   DEBUG(dbgs() << "Wide IV: " << *WidePhi << "\n");
01088   ++NumWidened;
01089 
01090   // Traverse the def-use chain using a worklist starting at the original IV.
01091   assert(Widened.empty() && NarrowIVUsers.empty() && "expect initial state" );
01092 
01093   Widened.insert(OrigPhi);
01094   pushNarrowIVUsers(OrigPhi, WidePhi);
01095 
01096   while (!NarrowIVUsers.empty()) {
01097     NarrowIVDefUse DU = NarrowIVUsers.pop_back_val();
01098 
01099     // Process a def-use edge. This may replace the use, so don't hold a
01100     // use_iterator across it.
01101     Instruction *WideUse = WidenIVUse(DU, Rewriter);
01102 
01103     // Follow all def-use edges from the previous narrow use.
01104     if (WideUse)
01105       pushNarrowIVUsers(DU.NarrowUse, WideUse);
01106 
01107     // WidenIVUse may have removed the def-use edge.
01108     if (DU.NarrowDef->use_empty())
01109       DeadInsts.push_back(DU.NarrowDef);
01110   }
01111   return WidePhi;
01112 }
01113 
01114 //===----------------------------------------------------------------------===//
01115 //  Live IV Reduction - Minimize IVs live across the loop.
01116 //===----------------------------------------------------------------------===//
01117 
01118 
01119 //===----------------------------------------------------------------------===//
01120 //  Simplification of IV users based on SCEV evaluation.
01121 //===----------------------------------------------------------------------===//
01122 
01123 namespace {
01124   class IndVarSimplifyVisitor : public IVVisitor {
01125     ScalarEvolution *SE;
01126     const DataLayout *DL;
01127     PHINode *IVPhi;
01128 
01129   public:
01130     WideIVInfo WI;
01131 
01132     IndVarSimplifyVisitor(PHINode *IV, ScalarEvolution *SCEV,
01133                           const DataLayout *DL, const DominatorTree *DTree):
01134       SE(SCEV), DL(DL), IVPhi(IV) {
01135       DT = DTree;
01136       WI.NarrowIV = IVPhi;
01137       if (ReduceLiveIVs)
01138         setSplitOverflowIntrinsics();
01139     }
01140 
01141     // Implement the interface used by simplifyUsersOfIV.
01142     void visitCast(CastInst *Cast) override { visitIVCast(Cast, WI, SE, DL); }
01143   };
01144 }
01145 
01146 /// SimplifyAndExtend - Iteratively perform simplification on a worklist of IV
01147 /// users. Each successive simplification may push more users which may
01148 /// themselves be candidates for simplification.
01149 ///
01150 /// Sign/Zero extend elimination is interleaved with IV simplification.
01151 ///
01152 void IndVarSimplify::SimplifyAndExtend(Loop *L,
01153                                        SCEVExpander &Rewriter,
01154                                        LPPassManager &LPM) {
01155   SmallVector<WideIVInfo, 8> WideIVs;
01156 
01157   SmallVector<PHINode*, 8> LoopPhis;
01158   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
01159     LoopPhis.push_back(cast<PHINode>(I));
01160   }
01161   // Each round of simplification iterates through the SimplifyIVUsers worklist
01162   // for all current phis, then determines whether any IVs can be
01163   // widened. Widening adds new phis to LoopPhis, inducing another round of
01164   // simplification on the wide IVs.
01165   while (!LoopPhis.empty()) {
01166     // Evaluate as many IV expressions as possible before widening any IVs. This
01167     // forces SCEV to set no-wrap flags before evaluating sign/zero
01168     // extension. The first time SCEV attempts to normalize sign/zero extension,
01169     // the result becomes final. So for the most predictable results, we delay
01170     // evaluation of sign/zero extend evaluation until needed, and avoid running
01171     // other SCEV based analysis prior to SimplifyAndExtend.
01172     do {
01173       PHINode *CurrIV = LoopPhis.pop_back_val();
01174 
01175       // Information about sign/zero extensions of CurrIV.
01176       IndVarSimplifyVisitor Visitor(CurrIV, SE, DL, DT);
01177 
01178       Changed |= simplifyUsersOfIV(CurrIV, SE, &LPM, DeadInsts, &Visitor);
01179 
01180       if (Visitor.WI.WidestNativeType) {
01181         WideIVs.push_back(Visitor.WI);
01182       }
01183     } while(!LoopPhis.empty());
01184 
01185     for (; !WideIVs.empty(); WideIVs.pop_back()) {
01186       WidenIV Widener(WideIVs.back(), LI, SE, DT, DeadInsts);
01187       if (PHINode *WidePhi = Widener.CreateWideIV(Rewriter)) {
01188         Changed = true;
01189         LoopPhis.push_back(WidePhi);
01190       }
01191     }
01192   }
01193 }
01194 
01195 //===----------------------------------------------------------------------===//
01196 //  LinearFunctionTestReplace and its kin. Rewrite the loop exit condition.
01197 //===----------------------------------------------------------------------===//
01198 
01199 /// Check for expressions that ScalarEvolution generates to compute
01200 /// BackedgeTakenInfo. If these expressions have not been reduced, then
01201 /// expanding them may incur additional cost (albeit in the loop preheader).
01202 static bool isHighCostExpansion(const SCEV *S, BranchInst *BI,
01203                                 SmallPtrSet<const SCEV*, 8> &Processed,
01204                                 ScalarEvolution *SE) {
01205   if (!Processed.insert(S))
01206     return false;
01207 
01208   // If the backedge-taken count is a UDiv, it's very likely a UDiv that
01209   // ScalarEvolution's HowFarToZero or HowManyLessThans produced to compute a
01210   // precise expression, rather than a UDiv from the user's code. If we can't
01211   // find a UDiv in the code with some simple searching, assume the former and
01212   // forego rewriting the loop.
01213   if (isa<SCEVUDivExpr>(S)) {
01214     ICmpInst *OrigCond = dyn_cast<ICmpInst>(BI->getCondition());
01215     if (!OrigCond) return true;
01216     const SCEV *R = SE->getSCEV(OrigCond->getOperand(1));
01217     R = SE->getMinusSCEV(R, SE->getConstant(R->getType(), 1));
01218     if (R != S) {
01219       const SCEV *L = SE->getSCEV(OrigCond->getOperand(0));
01220       L = SE->getMinusSCEV(L, SE->getConstant(L->getType(), 1));
01221       if (L != S)
01222         return true;
01223     }
01224   }
01225 
01226   // Recurse past add expressions, which commonly occur in the
01227   // BackedgeTakenCount. They may already exist in program code, and if not,
01228   // they are not too expensive rematerialize.
01229   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
01230     for (SCEVAddExpr::op_iterator I = Add->op_begin(), E = Add->op_end();
01231          I != E; ++I) {
01232       if (isHighCostExpansion(*I, BI, Processed, SE))
01233         return true;
01234     }
01235     return false;
01236   }
01237 
01238   // HowManyLessThans uses a Max expression whenever the loop is not guarded by
01239   // the exit condition.
01240   if (isa<SCEVSMaxExpr>(S) || isa<SCEVUMaxExpr>(S))
01241     return true;
01242 
01243   // If we haven't recognized an expensive SCEV pattern, assume it's an
01244   // expression produced by program code.
01245   return false;
01246 }
01247 
01248 /// canExpandBackedgeTakenCount - Return true if this loop's backedge taken
01249 /// count expression can be safely and cheaply expanded into an instruction
01250 /// sequence that can be used by LinearFunctionTestReplace.
01251 ///
01252 /// TODO: This fails for pointer-type loop counters with greater than one byte
01253 /// strides, consequently preventing LFTR from running. For the purpose of LFTR
01254 /// we could skip this check in the case that the LFTR loop counter (chosen by
01255 /// FindLoopCounter) is also pointer type. Instead, we could directly convert
01256 /// the loop test to an inequality test by checking the target data's alignment
01257 /// of element types (given that the initial pointer value originates from or is
01258 /// used by ABI constrained operation, as opposed to inttoptr/ptrtoint).
01259 /// However, we don't yet have a strong motivation for converting loop tests
01260 /// into inequality tests.
01261 static bool canExpandBackedgeTakenCount(Loop *L, ScalarEvolution *SE) {
01262   const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
01263   if (isa<SCEVCouldNotCompute>(BackedgeTakenCount) ||
01264       BackedgeTakenCount->isZero())
01265     return false;
01266 
01267   if (!L->getExitingBlock())
01268     return false;
01269 
01270   // Can't rewrite non-branch yet.
01271   BranchInst *BI = dyn_cast<BranchInst>(L->getExitingBlock()->getTerminator());
01272   if (!BI)
01273     return false;
01274 
01275   SmallPtrSet<const SCEV*, 8> Processed;
01276   if (isHighCostExpansion(BackedgeTakenCount, BI, Processed, SE))
01277     return false;
01278 
01279   return true;
01280 }
01281 
01282 /// getLoopPhiForCounter - Return the loop header phi IFF IncV adds a loop
01283 /// invariant value to the phi.
01284 static PHINode *getLoopPhiForCounter(Value *IncV, Loop *L, DominatorTree *DT) {
01285   Instruction *IncI = dyn_cast<Instruction>(IncV);
01286   if (!IncI)
01287     return nullptr;
01288 
01289   switch (IncI->getOpcode()) {
01290   case Instruction::Add:
01291   case Instruction::Sub:
01292     break;
01293   case Instruction::GetElementPtr:
01294     // An IV counter must preserve its type.
01295     if (IncI->getNumOperands() == 2)
01296       break;
01297   default:
01298     return nullptr;
01299   }
01300 
01301   PHINode *Phi = dyn_cast<PHINode>(IncI->getOperand(0));
01302   if (Phi && Phi->getParent() == L->getHeader()) {
01303     if (isLoopInvariant(IncI->getOperand(1), L, DT))
01304       return Phi;
01305     return nullptr;
01306   }
01307   if (IncI->getOpcode() == Instruction::GetElementPtr)
01308     return nullptr;
01309 
01310   // Allow add/sub to be commuted.
01311   Phi = dyn_cast<PHINode>(IncI->getOperand(1));
01312   if (Phi && Phi->getParent() == L->getHeader()) {
01313     if (isLoopInvariant(IncI->getOperand(0), L, DT))
01314       return Phi;
01315   }
01316   return nullptr;
01317 }
01318 
01319 /// Return the compare guarding the loop latch, or NULL for unrecognized tests.
01320 static ICmpInst *getLoopTest(Loop *L) {
01321   assert(L->getExitingBlock() && "expected loop exit");
01322 
01323   BasicBlock *LatchBlock = L->getLoopLatch();
01324   // Don't bother with LFTR if the loop is not properly simplified.
01325   if (!LatchBlock)
01326     return nullptr;
01327 
01328   BranchInst *BI = dyn_cast<BranchInst>(L->getExitingBlock()->getTerminator());
01329   assert(BI && "expected exit branch");
01330 
01331   return dyn_cast<ICmpInst>(BI->getCondition());
01332 }
01333 
01334 /// needsLFTR - LinearFunctionTestReplace policy. Return true unless we can show
01335 /// that the current exit test is already sufficiently canonical.
01336 static bool needsLFTR(Loop *L, DominatorTree *DT) {
01337   // Do LFTR to simplify the exit condition to an ICMP.
01338   ICmpInst *Cond = getLoopTest(L);
01339   if (!Cond)
01340     return true;
01341 
01342   // Do LFTR to simplify the exit ICMP to EQ/NE
01343   ICmpInst::Predicate Pred = Cond->getPredicate();
01344   if (Pred != ICmpInst::ICMP_NE && Pred != ICmpInst::ICMP_EQ)
01345     return true;
01346 
01347   // Look for a loop invariant RHS
01348   Value *LHS = Cond->getOperand(0);
01349   Value *RHS = Cond->getOperand(1);
01350   if (!isLoopInvariant(RHS, L, DT)) {
01351     if (!isLoopInvariant(LHS, L, DT))
01352       return true;
01353     std::swap(LHS, RHS);
01354   }
01355   // Look for a simple IV counter LHS
01356   PHINode *Phi = dyn_cast<PHINode>(LHS);
01357   if (!Phi)
01358     Phi = getLoopPhiForCounter(LHS, L, DT);
01359 
01360   if (!Phi)
01361     return true;
01362 
01363   // Do LFTR if PHI node is defined in the loop, but is *not* a counter.
01364   int Idx = Phi->getBasicBlockIndex(L->getLoopLatch());
01365   if (Idx < 0)
01366     return true;
01367 
01368   // Do LFTR if the exit condition's IV is *not* a simple counter.
01369   Value *IncV = Phi->getIncomingValue(Idx);
01370   return Phi != getLoopPhiForCounter(IncV, L, DT);
01371 }
01372 
01373 /// Recursive helper for hasConcreteDef(). Unfortunately, this currently boils
01374 /// down to checking that all operands are constant and listing instructions
01375 /// that may hide undef.
01376 static bool hasConcreteDefImpl(Value *V, SmallPtrSet<Value*, 8> &Visited,
01377                                unsigned Depth) {
01378   if (isa<Constant>(V))
01379     return !isa<UndefValue>(V);
01380 
01381   if (Depth >= 6)
01382     return false;
01383 
01384   // Conservatively handle non-constant non-instructions. For example, Arguments
01385   // may be undef.
01386   Instruction *I = dyn_cast<Instruction>(V);
01387   if (!I)
01388     return false;
01389 
01390   // Load and return values may be undef.
01391   if(I->mayReadFromMemory() || isa<CallInst>(I) || isa<InvokeInst>(I))
01392     return false;
01393 
01394   // Optimistically handle other instructions.
01395   for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) {
01396     if (!Visited.insert(*OI))
01397       continue;
01398     if (!hasConcreteDefImpl(*OI, Visited, Depth+1))
01399       return false;
01400   }
01401   return true;
01402 }
01403 
01404 /// Return true if the given value is concrete. We must prove that undef can
01405 /// never reach it.
01406 ///
01407 /// TODO: If we decide that this is a good approach to checking for undef, we
01408 /// may factor it into a common location.
01409 static bool hasConcreteDef(Value *V) {
01410   SmallPtrSet<Value*, 8> Visited;
01411   Visited.insert(V);
01412   return hasConcreteDefImpl(V, Visited, 0);
01413 }
01414 
01415 /// AlmostDeadIV - Return true if this IV has any uses other than the (soon to
01416 /// be rewritten) loop exit test.
01417 static bool AlmostDeadIV(PHINode *Phi, BasicBlock *LatchBlock, Value *Cond) {
01418   int LatchIdx = Phi->getBasicBlockIndex(LatchBlock);
01419   Value *IncV = Phi->getIncomingValue(LatchIdx);
01420 
01421   for (User *U : Phi->users())
01422     if (U != Cond && U != IncV) return false;
01423 
01424   for (User *U : IncV->users())
01425     if (U != Cond && U != Phi) return false;
01426   return true;
01427 }
01428 
01429 /// FindLoopCounter - Find an affine IV in canonical form.
01430 ///
01431 /// BECount may be an i8* pointer type. The pointer difference is already
01432 /// valid count without scaling the address stride, so it remains a pointer
01433 /// expression as far as SCEV is concerned.
01434 ///
01435 /// Currently only valid for LFTR. See the comments on hasConcreteDef below.
01436 ///
01437 /// FIXME: Accept -1 stride and set IVLimit = IVInit - BECount
01438 ///
01439 /// FIXME: Accept non-unit stride as long as SCEV can reduce BECount * Stride.
01440 /// This is difficult in general for SCEV because of potential overflow. But we
01441 /// could at least handle constant BECounts.
01442 static PHINode *
01443 FindLoopCounter(Loop *L, const SCEV *BECount,
01444                 ScalarEvolution *SE, DominatorTree *DT, const DataLayout *DL) {
01445   uint64_t BCWidth = SE->getTypeSizeInBits(BECount->getType());
01446 
01447   Value *Cond =
01448     cast<BranchInst>(L->getExitingBlock()->getTerminator())->getCondition();
01449 
01450   // Loop over all of the PHI nodes, looking for a simple counter.
01451   PHINode *BestPhi = nullptr;
01452   const SCEV *BestInit = nullptr;
01453   BasicBlock *LatchBlock = L->getLoopLatch();
01454   assert(LatchBlock && "needsLFTR should guarantee a loop latch");
01455 
01456   for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) {
01457     PHINode *Phi = cast<PHINode>(I);
01458     if (!SE->isSCEVable(Phi->getType()))
01459       continue;
01460 
01461     // Avoid comparing an integer IV against a pointer Limit.
01462     if (BECount->getType()->isPointerTy() && !Phi->getType()->isPointerTy())
01463       continue;
01464 
01465     const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Phi));
01466     if (!AR || AR->getLoop() != L || !AR->isAffine())
01467       continue;
01468 
01469     // AR may be a pointer type, while BECount is an integer type.
01470     // AR may be wider than BECount. With eq/ne tests overflow is immaterial.
01471     // AR may not be a narrower type, or we may never exit.
01472     uint64_t PhiWidth = SE->getTypeSizeInBits(AR->getType());
01473     if (PhiWidth < BCWidth || (DL && !DL->isLegalInteger(PhiWidth)))
01474       continue;
01475 
01476     const SCEV *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(*SE));
01477     if (!Step || !Step->isOne())
01478       continue;
01479 
01480     int LatchIdx = Phi->getBasicBlockIndex(LatchBlock);
01481     Value *IncV = Phi->getIncomingValue(LatchIdx);
01482     if (getLoopPhiForCounter(IncV, L, DT) != Phi)
01483       continue;
01484 
01485     // Avoid reusing a potentially undef value to compute other values that may
01486     // have originally had a concrete definition.
01487     if (!hasConcreteDef(Phi)) {
01488       // We explicitly allow unknown phis as long as they are already used by
01489       // the loop test. In this case we assume that performing LFTR could not
01490       // increase the number of undef users.
01491       if (ICmpInst *Cond = getLoopTest(L)) {
01492         if (Phi != getLoopPhiForCounter(Cond->getOperand(0), L, DT)
01493             && Phi != getLoopPhiForCounter(Cond->getOperand(1), L, DT)) {
01494           continue;
01495         }
01496       }
01497     }
01498     const SCEV *Init = AR->getStart();
01499 
01500     if (BestPhi && !AlmostDeadIV(BestPhi, LatchBlock, Cond)) {
01501       // Don't force a live loop counter if another IV can be used.
01502       if (AlmostDeadIV(Phi, LatchBlock, Cond))
01503         continue;
01504 
01505       // Prefer to count-from-zero. This is a more "canonical" counter form. It
01506       // also prefers integer to pointer IVs.
01507       if (BestInit->isZero() != Init->isZero()) {
01508         if (BestInit->isZero())
01509           continue;
01510       }
01511       // If two IVs both count from zero or both count from nonzero then the
01512       // narrower is likely a dead phi that has been widened. Use the wider phi
01513       // to allow the other to be eliminated.
01514       else if (PhiWidth <= SE->getTypeSizeInBits(BestPhi->getType()))
01515         continue;
01516     }
01517     BestPhi = Phi;
01518     BestInit = Init;
01519   }
01520   return BestPhi;
01521 }
01522 
01523 /// genLoopLimit - Help LinearFunctionTestReplace by generating a value that
01524 /// holds the RHS of the new loop test.
01525 static Value *genLoopLimit(PHINode *IndVar, const SCEV *IVCount, Loop *L,
01526                            SCEVExpander &Rewriter, ScalarEvolution *SE) {
01527   const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(IndVar));
01528   assert(AR && AR->getLoop() == L && AR->isAffine() && "bad loop counter");
01529   const SCEV *IVInit = AR->getStart();
01530 
01531   // IVInit may be a pointer while IVCount is an integer when FindLoopCounter
01532   // finds a valid pointer IV. Sign extend BECount in order to materialize a
01533   // GEP. Avoid running SCEVExpander on a new pointer value, instead reusing
01534   // the existing GEPs whenever possible.
01535   if (IndVar->getType()->isPointerTy()
01536       && !IVCount->getType()->isPointerTy()) {
01537 
01538     // IVOffset will be the new GEP offset that is interpreted by GEP as a
01539     // signed value. IVCount on the other hand represents the loop trip count,
01540     // which is an unsigned value. FindLoopCounter only allows induction
01541     // variables that have a positive unit stride of one. This means we don't
01542     // have to handle the case of negative offsets (yet) and just need to zero
01543     // extend IVCount.
01544     Type *OfsTy = SE->getEffectiveSCEVType(IVInit->getType());
01545     const SCEV *IVOffset = SE->getTruncateOrZeroExtend(IVCount, OfsTy);
01546 
01547     // Expand the code for the iteration count.
01548     assert(SE->isLoopInvariant(IVOffset, L) &&
01549            "Computed iteration count is not loop invariant!");
01550     BranchInst *BI = cast<BranchInst>(L->getExitingBlock()->getTerminator());
01551     Value *GEPOffset = Rewriter.expandCodeFor(IVOffset, OfsTy, BI);
01552 
01553     Value *GEPBase = IndVar->getIncomingValueForBlock(L->getLoopPreheader());
01554     assert(AR->getStart() == SE->getSCEV(GEPBase) && "bad loop counter");
01555     // We could handle pointer IVs other than i8*, but we need to compensate for
01556     // gep index scaling. See canExpandBackedgeTakenCount comments.
01557     assert(SE->getSizeOfExpr(IntegerType::getInt64Ty(IndVar->getContext()),
01558              cast<PointerType>(GEPBase->getType())->getElementType())->isOne()
01559            && "unit stride pointer IV must be i8*");
01560 
01561     IRBuilder<> Builder(L->getLoopPreheader()->getTerminator());
01562     return Builder.CreateGEP(GEPBase, GEPOffset, "lftr.limit");
01563   }
01564   else {
01565     // In any other case, convert both IVInit and IVCount to integers before
01566     // comparing. This may result in SCEV expension of pointers, but in practice
01567     // SCEV will fold the pointer arithmetic away as such:
01568     // BECount = (IVEnd - IVInit - 1) => IVLimit = IVInit (postinc).
01569     //
01570     // Valid Cases: (1) both integers is most common; (2) both may be pointers
01571     // for simple memset-style loops.
01572     //
01573     // IVInit integer and IVCount pointer would only occur if a canonical IV
01574     // were generated on top of case #2, which is not expected.
01575 
01576     const SCEV *IVLimit = nullptr;
01577     // For unit stride, IVCount = Start + BECount with 2's complement overflow.
01578     // For non-zero Start, compute IVCount here.
01579     if (AR->getStart()->isZero())
01580       IVLimit = IVCount;
01581     else {
01582       assert(AR->getStepRecurrence(*SE)->isOne() && "only handles unit stride");
01583       const SCEV *IVInit = AR->getStart();
01584 
01585       // For integer IVs, truncate the IV before computing IVInit + BECount.
01586       if (SE->getTypeSizeInBits(IVInit->getType())
01587           > SE->getTypeSizeInBits(IVCount->getType()))
01588         IVInit = SE->getTruncateExpr(IVInit, IVCount->getType());
01589 
01590       IVLimit = SE->getAddExpr(IVInit, IVCount);
01591     }
01592     // Expand the code for the iteration count.
01593     BranchInst *BI = cast<BranchInst>(L->getExitingBlock()->getTerminator());
01594     IRBuilder<> Builder(BI);
01595     assert(SE->isLoopInvariant(IVLimit, L) &&
01596            "Computed iteration count is not loop invariant!");
01597     // Ensure that we generate the same type as IndVar, or a smaller integer
01598     // type. In the presence of null pointer values, we have an integer type
01599     // SCEV expression (IVInit) for a pointer type IV value (IndVar).
01600     Type *LimitTy = IVCount->getType()->isPointerTy() ?
01601       IndVar->getType() : IVCount->getType();
01602     return Rewriter.expandCodeFor(IVLimit, LimitTy, BI);
01603   }
01604 }
01605 
01606 /// LinearFunctionTestReplace - This method rewrites the exit condition of the
01607 /// loop to be a canonical != comparison against the incremented loop induction
01608 /// variable.  This pass is able to rewrite the exit tests of any loop where the
01609 /// SCEV analysis can determine a loop-invariant trip count of the loop, which
01610 /// is actually a much broader range than just linear tests.
01611 Value *IndVarSimplify::
01612 LinearFunctionTestReplace(Loop *L,
01613                           const SCEV *BackedgeTakenCount,
01614                           PHINode *IndVar,
01615                           SCEVExpander &Rewriter) {
01616   assert(canExpandBackedgeTakenCount(L, SE) && "precondition");
01617 
01618   // Initialize CmpIndVar and IVCount to their preincremented values.
01619   Value *CmpIndVar = IndVar;
01620   const SCEV *IVCount = BackedgeTakenCount;
01621 
01622   // If the exiting block is the same as the backedge block, we prefer to
01623   // compare against the post-incremented value, otherwise we must compare
01624   // against the preincremented value.
01625   if (L->getExitingBlock() == L->getLoopLatch()) {
01626     // Add one to the "backedge-taken" count to get the trip count.
01627     // This addition may overflow, which is valid as long as the comparison is
01628     // truncated to BackedgeTakenCount->getType().
01629     IVCount = SE->getAddExpr(BackedgeTakenCount,
01630                              SE->getConstant(BackedgeTakenCount->getType(), 1));
01631     // The BackedgeTaken expression contains the number of times that the
01632     // backedge branches to the loop header.  This is one less than the
01633     // number of times the loop executes, so use the incremented indvar.
01634     CmpIndVar = IndVar->getIncomingValueForBlock(L->getExitingBlock());
01635   }
01636 
01637   Value *ExitCnt = genLoopLimit(IndVar, IVCount, L, Rewriter, SE);
01638   assert(ExitCnt->getType()->isPointerTy() == IndVar->getType()->isPointerTy()
01639          && "genLoopLimit missed a cast");
01640 
01641   // Insert a new icmp_ne or icmp_eq instruction before the branch.
01642   BranchInst *BI = cast<BranchInst>(L->getExitingBlock()->getTerminator());
01643   ICmpInst::Predicate P;
01644   if (L->contains(BI->getSuccessor(0)))
01645     P = ICmpInst::ICMP_NE;
01646   else
01647     P = ICmpInst::ICMP_EQ;
01648 
01649   DEBUG(dbgs() << "INDVARS: Rewriting loop exit condition to:\n"
01650                << "      LHS:" << *CmpIndVar << '\n'
01651                << "       op:\t"
01652                << (P == ICmpInst::ICMP_NE ? "!=" : "==") << "\n"
01653                << "      RHS:\t" << *ExitCnt << "\n"
01654                << "  IVCount:\t" << *IVCount << "\n");
01655 
01656   IRBuilder<> Builder(BI);
01657 
01658   // LFTR can ignore IV overflow and truncate to the width of
01659   // BECount. This avoids materializing the add(zext(add)) expression.
01660   unsigned CmpIndVarSize = SE->getTypeSizeInBits(CmpIndVar->getType());
01661   unsigned ExitCntSize = SE->getTypeSizeInBits(ExitCnt->getType());
01662   if (CmpIndVarSize > ExitCntSize) {
01663     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(SE->getSCEV(IndVar));
01664     const SCEV *ARStart = AR->getStart();
01665     const SCEV *ARStep = AR->getStepRecurrence(*SE);
01666     // For constant IVCount, avoid truncation.
01667     if (isa<SCEVConstant>(ARStart) && isa<SCEVConstant>(IVCount)) {
01668       const APInt &Start = cast<SCEVConstant>(ARStart)->getValue()->getValue();
01669       APInt Count = cast<SCEVConstant>(IVCount)->getValue()->getValue();
01670       // Note that the post-inc value of BackedgeTakenCount may have overflowed
01671       // above such that IVCount is now zero.
01672       if (IVCount != BackedgeTakenCount && Count == 0) {
01673         Count = APInt::getMaxValue(Count.getBitWidth()).zext(CmpIndVarSize);
01674         ++Count;
01675       }
01676       else
01677         Count = Count.zext(CmpIndVarSize);
01678       APInt NewLimit;
01679       if (cast<SCEVConstant>(ARStep)->getValue()->isNegative())
01680         NewLimit = Start - Count;
01681       else
01682         NewLimit = Start + Count;
01683       ExitCnt = ConstantInt::get(CmpIndVar->getType(), NewLimit);
01684 
01685       DEBUG(dbgs() << "  Widen RHS:\t" << *ExitCnt << "\n");
01686     } else {
01687       CmpIndVar = Builder.CreateTrunc(CmpIndVar, ExitCnt->getType(),
01688                                       "lftr.wideiv");
01689     }
01690   }
01691   Value *Cond = Builder.CreateICmp(P, CmpIndVar, ExitCnt, "exitcond");
01692   Value *OrigCond = BI->getCondition();
01693   // It's tempting to use replaceAllUsesWith here to fully replace the old
01694   // comparison, but that's not immediately safe, since users of the old
01695   // comparison may not be dominated by the new comparison. Instead, just
01696   // update the branch to use the new comparison; in the common case this
01697   // will make old comparison dead.
01698   BI->setCondition(Cond);
01699   DeadInsts.push_back(OrigCond);
01700 
01701   ++NumLFTR;
01702   Changed = true;
01703   return Cond;
01704 }
01705 
01706 //===----------------------------------------------------------------------===//
01707 //  SinkUnusedInvariants. A late subpass to cleanup loop preheaders.
01708 //===----------------------------------------------------------------------===//
01709 
01710 /// If there's a single exit block, sink any loop-invariant values that
01711 /// were defined in the preheader but not used inside the loop into the
01712 /// exit block to reduce register pressure in the loop.
01713 void IndVarSimplify::SinkUnusedInvariants(Loop *L) {
01714   BasicBlock *ExitBlock = L->getExitBlock();
01715   if (!ExitBlock) return;
01716 
01717   BasicBlock *Preheader = L->getLoopPreheader();
01718   if (!Preheader) return;
01719 
01720   Instruction *InsertPt = ExitBlock->getFirstInsertionPt();
01721   BasicBlock::iterator I = Preheader->getTerminator();
01722   while (I != Preheader->begin()) {
01723     --I;
01724     // New instructions were inserted at the end of the preheader.
01725     if (isa<PHINode>(I))
01726       break;
01727 
01728     // Don't move instructions which might have side effects, since the side
01729     // effects need to complete before instructions inside the loop.  Also don't
01730     // move instructions which might read memory, since the loop may modify
01731     // memory. Note that it's okay if the instruction might have undefined
01732     // behavior: LoopSimplify guarantees that the preheader dominates the exit
01733     // block.
01734     if (I->mayHaveSideEffects() || I->mayReadFromMemory())
01735       continue;
01736 
01737     // Skip debug info intrinsics.
01738     if (isa<DbgInfoIntrinsic>(I))
01739       continue;
01740 
01741     // Skip landingpad instructions.
01742     if (isa<LandingPadInst>(I))
01743       continue;
01744 
01745     // Don't sink alloca: we never want to sink static alloca's out of the
01746     // entry block, and correctly sinking dynamic alloca's requires
01747     // checks for stacksave/stackrestore intrinsics.
01748     // FIXME: Refactor this check somehow?
01749     if (isa<AllocaInst>(I))
01750       continue;
01751 
01752     // Determine if there is a use in or before the loop (direct or
01753     // otherwise).
01754     bool UsedInLoop = false;
01755     for (Use &U : I->uses()) {
01756       Instruction *User = cast<Instruction>(U.getUser());
01757       BasicBlock *UseBB = User->getParent();
01758       if (PHINode *P = dyn_cast<PHINode>(User)) {
01759         unsigned i =
01760           PHINode::getIncomingValueNumForOperand(U.getOperandNo());
01761         UseBB = P->getIncomingBlock(i);
01762       }
01763       if (UseBB == Preheader || L->contains(UseBB)) {
01764         UsedInLoop = true;
01765         break;
01766       }
01767     }
01768 
01769     // If there is, the def must remain in the preheader.
01770     if (UsedInLoop)
01771       continue;
01772 
01773     // Otherwise, sink it to the exit block.
01774     Instruction *ToMove = I;
01775     bool Done = false;
01776 
01777     if (I != Preheader->begin()) {
01778       // Skip debug info intrinsics.
01779       do {
01780         --I;
01781       } while (isa<DbgInfoIntrinsic>(I) && I != Preheader->begin());
01782 
01783       if (isa<DbgInfoIntrinsic>(I) && I == Preheader->begin())
01784         Done = true;
01785     } else {
01786       Done = true;
01787     }
01788 
01789     ToMove->moveBefore(InsertPt);
01790     if (Done) break;
01791     InsertPt = ToMove;
01792   }
01793 }
01794 
01795 //===----------------------------------------------------------------------===//
01796 //  IndVarSimplify driver. Manage several subpasses of IV simplification.
01797 //===----------------------------------------------------------------------===//
01798 
01799 bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
01800   if (skipOptnoneFunction(L))
01801     return false;
01802 
01803   // If LoopSimplify form is not available, stay out of trouble. Some notes:
01804   //  - LSR currently only supports LoopSimplify-form loops. Indvars'
01805   //    canonicalization can be a pessimization without LSR to "clean up"
01806   //    afterwards.
01807   //  - We depend on having a preheader; in particular,
01808   //    Loop::getCanonicalInductionVariable only supports loops with preheaders,
01809   //    and we're in trouble if we can't find the induction variable even when
01810   //    we've manually inserted one.
01811   if (!L->isLoopSimplifyForm())
01812     return false;
01813 
01814   LI = &getAnalysis<LoopInfo>();
01815   SE = &getAnalysis<ScalarEvolution>();
01816   DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
01817   DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
01818   DL = DLP ? &DLP->getDataLayout() : nullptr;
01819   TLI = getAnalysisIfAvailable<TargetLibraryInfo>();
01820 
01821   DeadInsts.clear();
01822   Changed = false;
01823 
01824   // If there are any floating-point recurrences, attempt to
01825   // transform them to use integer recurrences.
01826   RewriteNonIntegerIVs(L);
01827 
01828   const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L);
01829 
01830   // Create a rewriter object which we'll use to transform the code with.
01831   SCEVExpander Rewriter(*SE, "indvars");
01832 #ifndef NDEBUG
01833   Rewriter.setDebugType(DEBUG_TYPE);
01834 #endif
01835 
01836   // Eliminate redundant IV users.
01837   //
01838   // Simplification works best when run before other consumers of SCEV. We
01839   // attempt to avoid evaluating SCEVs for sign/zero extend operations until
01840   // other expressions involving loop IVs have been evaluated. This helps SCEV
01841   // set no-wrap flags before normalizing sign/zero extension.
01842   Rewriter.disableCanonicalMode();
01843   SimplifyAndExtend(L, Rewriter, LPM);
01844 
01845   // Check to see if this loop has a computable loop-invariant execution count.
01846   // If so, this means that we can compute the final value of any expressions
01847   // that are recurrent in the loop, and substitute the exit values from the
01848   // loop into any instructions outside of the loop that use the final values of
01849   // the current expressions.
01850   //
01851   if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount))
01852     RewriteLoopExitValues(L, Rewriter);
01853 
01854   // Eliminate redundant IV cycles.
01855   NumElimIV += Rewriter.replaceCongruentIVs(L, DT, DeadInsts);
01856 
01857   // If we have a trip count expression, rewrite the loop's exit condition
01858   // using it.  We can currently only handle loops with a single exit.
01859   if (canExpandBackedgeTakenCount(L, SE) && needsLFTR(L, DT)) {
01860     PHINode *IndVar = FindLoopCounter(L, BackedgeTakenCount, SE, DT, DL);
01861     if (IndVar) {
01862       // Check preconditions for proper SCEVExpander operation. SCEV does not
01863       // express SCEVExpander's dependencies, such as LoopSimplify. Instead any
01864       // pass that uses the SCEVExpander must do it. This does not work well for
01865       // loop passes because SCEVExpander makes assumptions about all loops,
01866       // while LoopPassManager only forces the current loop to be simplified.
01867       //
01868       // FIXME: SCEV expansion has no way to bail out, so the caller must
01869       // explicitly check any assumptions made by SCEV. Brittle.
01870       const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(BackedgeTakenCount);
01871       if (!AR || AR->getLoop()->getLoopPreheader())
01872         (void)LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar,
01873                                         Rewriter);
01874     }
01875   }
01876   // Clear the rewriter cache, because values that are in the rewriter's cache
01877   // can be deleted in the loop below, causing the AssertingVH in the cache to
01878   // trigger.
01879   Rewriter.clear();
01880 
01881   // Now that we're done iterating through lists, clean up any instructions
01882   // which are now dead.
01883   while (!DeadInsts.empty())
01884     if (Instruction *Inst =
01885           dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val()))
01886       RecursivelyDeleteTriviallyDeadInstructions(Inst, TLI);
01887 
01888   // The Rewriter may not be used from this point on.
01889 
01890   // Loop-invariant instructions in the preheader that aren't used in the
01891   // loop may be sunk below the loop to reduce register pressure.
01892   SinkUnusedInvariants(L);
01893 
01894   // Clean up dead instructions.
01895   Changed |= DeleteDeadPHIs(L->getHeader(), TLI);
01896   // Check a post-condition.
01897   assert(L->isLCSSAForm(*DT) &&
01898          "Indvars did not leave the loop in lcssa form!");
01899 
01900   // Verify that LFTR, and any other change have not interfered with SCEV's
01901   // ability to compute trip count.
01902 #ifndef NDEBUG
01903   if (VerifyIndvars && !isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
01904     SE->forgetLoop(L);
01905     const SCEV *NewBECount = SE->getBackedgeTakenCount(L);
01906     if (SE->getTypeSizeInBits(BackedgeTakenCount->getType()) <
01907         SE->getTypeSizeInBits(NewBECount->getType()))
01908       NewBECount = SE->getTruncateOrNoop(NewBECount,
01909                                          BackedgeTakenCount->getType());
01910     else
01911       BackedgeTakenCount = SE->getTruncateOrNoop(BackedgeTakenCount,
01912                                                  NewBECount->getType());
01913     assert(BackedgeTakenCount == NewBECount && "indvars must preserve SCEV");
01914   }
01915 #endif
01916 
01917   return Changed;
01918 }