ScalarEvolutionExpander.cpp revision 203954
11541Srgrimes//===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===//
21541Srgrimes//
31541Srgrimes//                     The LLVM Compiler Infrastructure
485895Srwatson//
51541Srgrimes// This file is distributed under the University of Illinois Open Source
685895Srwatson// License. See LICENSE.TXT for details.
71541Srgrimes//
81541Srgrimes//===----------------------------------------------------------------------===//
91541Srgrimes//
101541Srgrimes// This file contains the implementation of the scalar evolution expander,
111541Srgrimes// which is used to generate the code corresponding to a given scalar evolution
121541Srgrimes// expression.
131541Srgrimes//
141541Srgrimes//===----------------------------------------------------------------------===//
151541Srgrimes
161541Srgrimes#include "llvm/Analysis/ScalarEvolutionExpander.h"
171541Srgrimes#include "llvm/Analysis/LoopInfo.h"
181541Srgrimes#include "llvm/LLVMContext.h"
191541Srgrimes#include "llvm/Target/TargetData.h"
201541Srgrimes#include "llvm/ADT/STLExtras.h"
211541Srgrimesusing namespace llvm;
221541Srgrimes
231541Srgrimes/// InsertNoopCastOfTo - Insert a cast of V to the specified type,
241541Srgrimes/// which must be possible with a noop cast, doing what we can to share
251541Srgrimes/// the casts.
261541SrgrimesValue *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) {
271541Srgrimes  Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false);
281541Srgrimes  assert((Op == Instruction::BitCast ||
291541Srgrimes          Op == Instruction::PtrToInt ||
301541Srgrimes          Op == Instruction::IntToPtr) &&
311541Srgrimes         "InsertNoopCastOfTo cannot perform non-noop casts!");
321541Srgrimes  assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
331541Srgrimes         "InsertNoopCastOfTo cannot change sizes!");
341541Srgrimes
351541Srgrimes  // Short-circuit unnecessary bitcasts.
361541Srgrimes  if (Op == Instruction::BitCast && V->getType() == Ty)
371541Srgrimes    return V;
381541Srgrimes
391541Srgrimes  // Short-circuit unnecessary inttoptr<->ptrtoint casts.
401541Srgrimes  if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) &&
4150477Speter      SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) {
421541Srgrimes    if (CastInst *CI = dyn_cast<CastInst>(V))
431541Srgrimes      if ((CI->getOpcode() == Instruction::PtrToInt ||
441541Srgrimes           CI->getOpcode() == Instruction::IntToPtr) &&
451541Srgrimes          SE.getTypeSizeInBits(CI->getType()) ==
461541Srgrimes          SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
471541Srgrimes        return CI->getOperand(0);
4831778Seivind    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
4975426Srwatson      if ((CE->getOpcode() == Instruction::PtrToInt ||
5031778Seivind           CE->getOpcode() == Instruction::IntToPtr) &&
511541Srgrimes          SE.getTypeSizeInBits(CE->getType()) ==
5276166Smarkm          SE.getTypeSizeInBits(CE->getOperand(0)->getType()))
531541Srgrimes        return CE->getOperand(0);
5441059Speter  }
5570317Sjake
5676166Smarkm  if (Constant *C = dyn_cast<Constant>(V))
571541Srgrimes    return ConstantExpr::getCast(Op, C, Ty);
5886304Sjhb
5976166Smarkm  if (Argument *A = dyn_cast<Argument>(V)) {
601541Srgrimes    // Check to see if there is already a cast!
6131891Ssef    for (Value::use_iterator UI = A->use_begin(), E = A->use_end();
6265495Struckman         UI != E; ++UI)
6361287Srwatson      if ((*UI)->getType() == Ty)
6472786Srwatson        if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
651541Srgrimes          if (CI->getOpcode() == Op) {
6630354Sphk            // If the cast isn't the first instruction of the function, move it.
6730354Sphk            if (BasicBlock::iterator(CI) !=
6880735Srwatson                A->getParent()->getEntryBlock().begin()) {
6980735Srwatson              // Recreate the cast at the beginning of the entry block.
7080735Srwatson              // The old cast is left in place in case it is being used
7187138Srwatson              // as an insert point.
7287138Srwatson              Instruction *NewCI =
7387138Srwatson                CastInst::Create(Op, V, Ty, "",
7412221Sbde                                 A->getParent()->getEntryBlock().begin());
7511332Sswallace              NewCI->takeName(CI);
761541Srgrimes              CI->replaceAllUsesWith(NewCI);
771541Srgrimes              return NewCI;
7812221Sbde            }
791541Srgrimes            return CI;
8058717Sdillon          }
8182749Sdillon
8258717Sdillon    Instruction *I = CastInst::Create(Op, V, Ty, V->getName(),
8370317Sjake                                      A->getParent()->getEntryBlock().begin());
8482749Sdillon    rememberInstruction(I);
8582749Sdillon    return I;
8682749Sdillon  }
871541Srgrimes
881549Srgrimes  Instruction *I = cast<Instruction>(V);
8983366Sjulian
9083366Sjulian  // Check to see if there is already a cast.  If there is, use it.
9111332Sswallace  for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
921541Srgrimes       UI != E; ++UI) {
9383366Sjulian    if ((*UI)->getType() == Ty)
9485564Sdillon      if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
951541Srgrimes        if (CI->getOpcode() == Op) {
9685564Sdillon          BasicBlock::iterator It = I; ++It;
9783366Sjulian          if (isa<InvokeInst>(I))
981541Srgrimes            It = cast<InvokeInst>(I)->getNormalDest()->begin();
9974728Sjhb          while (isa<PHINode>(It)) ++It;
10083366Sjulian          if (It != BasicBlock::iterator(CI)) {
10174728Sjhb            // Recreate the cast after the user.
1021541Srgrimes            // The old cast is left in place in case it is being used
10385564Sdillon            // as an insert point.
1041541Srgrimes            Instruction *NewCI = CastInst::Create(Op, V, Ty, "", It);
1051541Srgrimes            NewCI->takeName(CI);
1061541Srgrimes            CI->replaceAllUsesWith(NewCI);
10770317Sjake            rememberInstruction(NewCI);
10882749Sdillon            return NewCI;
10970317Sjake          }
11070317Sjake          rememberInstruction(CI);
11112221Sbde          return CI;
11211332Sswallace        }
11311332Sswallace  }
11411332Sswallace  BasicBlock::iterator IP = I; ++IP;
11512221Sbde  if (InvokeInst *II = dyn_cast<InvokeInst>(I))
11682749Sdillon    IP = II->getNormalDest()->begin();
11782749Sdillon  while (isa<PHINode>(IP)) ++IP;
11882749Sdillon  Instruction *CI = CastInst::Create(Op, V, Ty, V->getName(), IP);
1191541Srgrimes  rememberInstruction(CI);
1201549Srgrimes  return CI;
12183366Sjulian}
12283366Sjulian
12311332Sswallace/// InsertBinop - Insert the specified binary operator, doing a small amount
1241541Srgrimes/// of work to avoid inserting an obviously redundant operation.
12583366SjulianValue *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
12685564Sdillon                                 Value *LHS, Value *RHS) {
1271541Srgrimes  // Fold a binop with constant operands.
12885564Sdillon  if (Constant *CLHS = dyn_cast<Constant>(LHS))
12974728Sjhb    if (Constant *CRHS = dyn_cast<Constant>(RHS))
13083366Sjulian      return ConstantExpr::get(Opcode, CLHS, CRHS);
13174728Sjhb
13285564Sdillon  // Do a quick scan to see if we have this binop nearby.  If so, reuse it.
1331541Srgrimes  unsigned ScanLimit = 6;
1341541Srgrimes  BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
1351541Srgrimes  // Scanning starts from the last instruction before the insertion point.
13658717Sdillon  BasicBlock::iterator IP = Builder.GetInsertPoint();
13758717Sdillon  if (IP != BlockBegin) {
13858717Sdillon    --IP;
13958717Sdillon    for (; ScanLimit; --IP, --ScanLimit) {
14058717Sdillon      if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS &&
14112221Sbde          IP->getOperand(1) == RHS)
14211332Sswallace        return IP;
14311332Sswallace      if (IP == BlockBegin) break;
14411332Sswallace    }
14512221Sbde  }
14682749Sdillon
14782749Sdillon  // If we haven't found this binop, insert it.
14882749Sdillon  Value *BO = Builder.CreateBinOp(Opcode, LHS, RHS, "tmp");
1491549Srgrimes  rememberInstruction(BO);
15083366Sjulian  return BO;
15183366Sjulian}
15211332Sswallace
1531541Srgrimes/// FactorOutConstant - Test if S is divisible by Factor, using signed
15483366Sjulian/// division. If so, update S with Factor divided out and return true.
1551541Srgrimes/// S need not be evenly divisble if a reasonable remainder can be
15682749Sdillon/// computed.
15783366Sjulian/// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made
15882749Sdillon/// unnecessary; in its place, just signed-divide Ops[i] by the scale and
1591541Srgrimes/// check to see if the divide was folded.
1601541Srgrimesstatic bool FactorOutConstant(const SCEV *&S,
1611541Srgrimes                              const SCEV *&Remainder,
16228401Speter                              const SCEV *Factor,
16312221Sbde                              ScalarEvolution &SE,
16428401Speter                              const TargetData *TD) {
16528401Speter  // Everything is divisible by one.
16628401Speter  if (Factor->isOne())
16728401Speter    return true;
16828401Speter
16982749Sdillon  // x/x == 1.
17082749Sdillon  if (S == Factor) {
17182749Sdillon    S = SE.getIntegerSCEV(1, S->getType());
17228401Speter    return true;
17383366Sjulian  }
17483366Sjulian
17528401Speter  // For a Constant, check for a multiple of the given factor.
17628401Speter  if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
17783366Sjulian    // 0/x == 0.
17841726Struckman    if (C->isZero())
17982749Sdillon      return true;
18085564Sdillon    // Check for divisibility.
18141726Struckman    if (const SCEVConstant *FC = dyn_cast<SCEVConstant>(Factor)) {
18285564Sdillon      ConstantInt *CI =
18328401Speter        ConstantInt::get(SE.getContext(),
18483366Sjulian                         C->getValue()->getValue().sdiv(
18584825Sjhb                                                   FC->getValue()->getValue()));
18684825Sjhb      // If the quotient is zero and the remainder is non-zero, reject
18775893Sjhb      // the value at this scale. It will be considered for subsequent
18884825Sjhb      // smaller scales.
18984825Sjhb      if (!CI->isZero()) {
19084825Sjhb        const SCEV *Div = SE.getConstant(CI);
19175893Sjhb        S = Div;
19275893Sjhb        Remainder =
19385564Sdillon          SE.getAddExpr(Remainder,
19482749Sdillon                        SE.getConstant(C->getValue()->getValue().srem(
19528401Speter                                                  FC->getValue()->getValue())));
19628401Speter        return true;
19728401Speter      }
19828401Speter    }
19928401Speter  }
20028401Speter
20128401Speter  // In a Mul, check if there is a constant operand which is a multiple
20228401Speter  // of the given factor.
20328401Speter  if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
20428401Speter    if (TD) {
20528401Speter      // With TargetData, the size is known. Check if there is a constant
20682749Sdillon      // operand which is a multiple of the given factor. If so, we can
20782749Sdillon      // factor it.
20882749Sdillon      const SCEVConstant *FC = cast<SCEVConstant>(Factor);
20928401Speter      if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0)))
21083366Sjulian        if (!C->getValue()->getValue().srem(FC->getValue()->getValue())) {
21183366Sjulian          const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands();
21228401Speter          SmallVector<const SCEV *, 4> NewMulOps(MOperands.begin(),
21328401Speter                                                 MOperands.end());
21483366Sjulian          NewMulOps[0] =
21541726Struckman            SE.getConstant(C->getValue()->getValue().sdiv(
21682749Sdillon                                                   FC->getValue()->getValue()));
21741726Struckman          S = SE.getMulExpr(NewMulOps);
21882749Sdillon          return true;
21984825Sjhb        }
22083366Sjulian    } else {
22184825Sjhb      // Without TargetData, check if Factor can be factored out of any of the
22284825Sjhb      // Mul's operands. If so, we can just remove it.
22384825Sjhb      for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
22484825Sjhb        const SCEV *SOp = M->getOperand(i);
22584825Sjhb        const SCEV *Remainder = SE.getIntegerSCEV(0, SOp->getType());
22684825Sjhb        if (FactorOutConstant(SOp, Remainder, Factor, SE, TD) &&
22775893Sjhb            Remainder->isZero()) {
22875893Sjhb          const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands();
22982749Sdillon          SmallVector<const SCEV *, 4> NewMulOps(MOperands.begin(),
23082749Sdillon                                                 MOperands.end());
23128401Speter          NewMulOps[i] = SOp;
23228401Speter          S = SE.getMulExpr(NewMulOps);
23328401Speter          return true;
23458941Sdillon        }
23558941Sdillon      }
23658941Sdillon    }
23728401Speter  }
23811332Sswallace
23911332Sswallace  // In an AddRec, check if both start and step are divisible.
24011332Sswallace  if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
24112221Sbde    const SCEV *Step = A->getStepRecurrence(SE);
24211332Sswallace    const SCEV *StepRem = SE.getIntegerSCEV(0, Step->getType());
24382749Sdillon    if (!FactorOutConstant(Step, StepRem, Factor, SE, TD))
24482749Sdillon      return false;
24582749Sdillon    if (!StepRem->isZero())
2461541Srgrimes      return false;
2471549Srgrimes    const SCEV *Start = A->getStart();
24883366Sjulian    if (!FactorOutConstant(Start, Remainder, Factor, SE, TD))
24983366Sjulian      return false;
25011332Sswallace    S = SE.getAddRecExpr(Start, Step, A->getLoop());
2511541Srgrimes    return true;
25283366Sjulian  }
2531541Srgrimes
25482749Sdillon  return false;
25583366Sjulian}
2561541Srgrimes
25783366Sjulian/// SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs
2581541Srgrimes/// is the number of SCEVAddRecExprs present, which are kept at the end of
25982749Sdillon/// the list.
2601541Srgrimes///
2611541Srgrimesstatic void SimplifyAddOperands(SmallVectorImpl<const SCEV *> &Ops,
2621541Srgrimes                                const Type *Ty,
26358941Sdillon                                ScalarEvolution &SE) {
26458941Sdillon  unsigned NumAddRecs = 0;
26558941Sdillon  for (unsigned i = Ops.size(); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1]); --i)
26612221Sbde    ++NumAddRecs;
26711332Sswallace  // Group Ops into non-addrecs and addrecs.
26811332Sswallace  SmallVector<const SCEV *, 8> NoAddRecs(Ops.begin(), Ops.end() - NumAddRecs);
26911332Sswallace  SmallVector<const SCEV *, 8> AddRecs(Ops.end() - NumAddRecs, Ops.end());
27012221Sbde  // Let ScalarEvolution sort and simplify the non-addrecs list.
27111332Sswallace  const SCEV *Sum = NoAddRecs.empty() ?
2721541Srgrimes                    SE.getIntegerSCEV(0, Ty) :
2731549Srgrimes                    SE.getAddExpr(NoAddRecs);
27483366Sjulian  // If it returned an add, use the operands. Otherwise it simplified
27583366Sjulian  // the sum into a single value, so just use that.
27611332Sswallace  if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum))
2771541Srgrimes    Ops = Add->getOperands();
27882749Sdillon  else {
27983366Sjulian    Ops.clear();
28082749Sdillon    if (!Sum->isZero())
2811541Srgrimes      Ops.push_back(Sum);
2821541Srgrimes  }
2831541Srgrimes  // Then append the addrecs.
28458941Sdillon  Ops.insert(Ops.end(), AddRecs.begin(), AddRecs.end());
28558941Sdillon}
28658941Sdillon
28712221Sbde/// SplitAddRecs - Flatten a list of add operands, moving addrec start values
28811332Sswallace/// out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}.
28911332Sswallace/// This helps expose more opportunities for folding parts of the expressions
29011332Sswallace/// into GEP indices.
29112221Sbde///
29211332Sswallacestatic void SplitAddRecs(SmallVectorImpl<const SCEV *> &Ops,
29382749Sdillon                         const Type *Ty,
29482749Sdillon                         ScalarEvolution &SE) {
29582749Sdillon  // Find the addrecs.
2961541Srgrimes  SmallVector<const SCEV *, 8> AddRecs;
2971549Srgrimes  for (unsigned i = 0, e = Ops.size(); i != e; ++i)
29883366Sjulian    while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) {
29983366Sjulian      const SCEV *Start = A->getStart();
30011332Sswallace      if (Start->isZero()) break;
3011541Srgrimes      const SCEV *Zero = SE.getIntegerSCEV(0, Ty);
30283366Sjulian      AddRecs.push_back(SE.getAddRecExpr(Zero,
3031541Srgrimes                                         A->getStepRecurrence(SE),
30482749Sdillon                                         A->getLoop()));
30583366Sjulian      if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) {
3061541Srgrimes        Ops[i] = Zero;
30783366Sjulian        Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
3081541Srgrimes        e += Add->getNumOperands();
30982749Sdillon      } else {
3101541Srgrimes        Ops[i] = Start;
3111541Srgrimes      }
3121541Srgrimes    }
3131541Srgrimes  if (!AddRecs.empty()) {
3141541Srgrimes    // Add the addrecs onto the end of the list.
3151541Srgrimes    Ops.insert(Ops.end(), AddRecs.begin(), AddRecs.end());
3161541Srgrimes    // Resort the operand list, moving any constants to the front.
3171541Srgrimes    SimplifyAddOperands(Ops, Ty, SE);
31812221Sbde  }
31911332Sswallace}
32011332Sswallace
32111332Sswallace/// expandAddToGEP - Expand an addition expression with a pointer type into
32212221Sbde/// a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps
32311332Sswallace/// BasicAliasAnalysis and other passes analyze the result. See the rules
32482749Sdillon/// for getelementptr vs. inttoptr in
32582749Sdillon/// http://llvm.org/docs/LangRef.html#pointeraliasing
32682749Sdillon/// for details.
3271541Srgrimes///
3281549Srgrimes/// Design note: The correctness of using getelementptr here depends on
32983366Sjulian/// ScalarEvolution not recognizing inttoptr and ptrtoint operators, as
33083366Sjulian/// they may introduce pointer arithmetic which may not be safely converted
33111332Sswallace/// into getelementptr.
3321541Srgrimes///
33383366Sjulian/// Design note: It might seem desirable for this function to be more
3341541Srgrimes/// loop-aware. If some of the indices are loop-invariant while others
33582749Sdillon/// aren't, it might seem desirable to emit multiple GEPs, keeping the
33683366Sjulian/// loop-invariant portions of the overall computation outside the loop.
33782749Sdillon/// However, there are a few reasons this is not done here. Hoisting simple
3381541Srgrimes/// arithmetic is a low-level optimization that often isn't very
3391541Srgrimes/// important until late in the optimization process. In fact, passes
3401541Srgrimes/// like InstructionCombining will combine GEPs, even if it means
34112221Sbde/// pushing loop-invariant computation down into loops, so even if the
3421541Srgrimes/// GEPs were split here, the work would quickly be undone. The
3431541Srgrimes/// LoopStrengthReduction pass, which is usually run quite late (and
3441541Srgrimes/// after the last InstructionCombining pass), takes care of hoisting
3451541Srgrimes/// loop-invariant portions of expressions, after considering what
34612221Sbde/// can be folded using target addressing modes.
34782749Sdillon///
34882749SdillonValue *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin,
34982749Sdillon                                    const SCEV *const *op_end,
3501549Srgrimes                                    const PointerType *PTy,
35183366Sjulian                                    const Type *Ty,
35283366Sjulian                                    Value *V) {
3531541Srgrimes  const Type *ElTy = PTy->getElementType();
3541541Srgrimes  SmallVector<Value *, 4> GepIndices;
35582749Sdillon  SmallVector<const SCEV *, 8> Ops(op_begin, op_end);
35683366Sjulian  bool AnyNonZeroIndices = false;
35777183Srwatson
35882749Sdillon  // Split AddRecs up into parts as either of the parts may be usable
3591541Srgrimes  // without the other.
36082749Sdillon  SplitAddRecs(Ops, Ty, SE);
36182749Sdillon
3621541Srgrimes  // Descend down the pointer's type and attempt to convert the other
36383366Sjulian  // operands into GEP indices, at each level. The first index in a GEP
36482749Sdillon  // indexes into the array implied by the pointer operand; the rest of
36582749Sdillon  // the indices index into the element or field type selected by the
3661541Srgrimes  // preceding index.
36782749Sdillon  for (;;) {
36882749Sdillon    // If the scale size is not 0, attempt to factor out a scale for
36982749Sdillon    // array indexing.
37082749Sdillon    SmallVector<const SCEV *, 8> ScaledOps;
37177183Srwatson    if (ElTy->isSized()) {
37277183Srwatson      const SCEV *ElSize = SE.getSizeOfExpr(ElTy);
37382749Sdillon      if (!ElSize->isZero()) {
37482749Sdillon        SmallVector<const SCEV *, 8> NewOps;
37582749Sdillon        for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
37683366Sjulian          const SCEV *Op = Ops[i];
37782749Sdillon          const SCEV *Remainder = SE.getIntegerSCEV(0, Ty);
37882749Sdillon          if (FactorOutConstant(Op, Remainder, ElSize, SE, SE.TD)) {
37982749Sdillon            // Op now has ElSize factored out.
3801541Srgrimes            ScaledOps.push_back(Op);
3811541Srgrimes            if (!Remainder->isZero())
38212221Sbde              NewOps.push_back(Remainder);
38312207Sbde            AnyNonZeroIndices = true;
38411332Sswallace          } else {
38511332Sswallace            // The operand was not divisible, so add it to the list of operands
38612221Sbde            // we'll scan next iteration.
38711332Sswallace            NewOps.push_back(Ops[i]);
38882749Sdillon          }
38982749Sdillon        }
39082749Sdillon        // If we made any changes, update Ops.
3911541Srgrimes        if (!ScaledOps.empty()) {
3921549Srgrimes          Ops = NewOps;
39383366Sjulian          SimplifyAddOperands(Ops, Ty, SE);
39483366Sjulian        }
39512207Sbde      }
3961541Srgrimes    }
39782749Sdillon
39883366Sjulian    // Record the scaled array index for this level of the type. If
3991541Srgrimes    // we didn't find any operands that could be factored, tentatively
40082749Sdillon    // assume that element zero was selected (since the zero offset
4011541Srgrimes    // would obviously be folded away).
40282749Sdillon    Value *Scaled = ScaledOps.empty() ?
4031541Srgrimes                    Constant::getNullValue(Ty) :
4041541Srgrimes                    expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
40583366Sjulian    GepIndices.push_back(Scaled);
40682749Sdillon
4071541Srgrimes    // Collect struct field index operands.
40882749Sdillon    while (const StructType *STy = dyn_cast<StructType>(ElTy)) {
40982749Sdillon      bool FoundFieldNo = false;
4101541Srgrimes      // An empty struct has no fields.
4111541Srgrimes      if (STy->getNumElements() == 0) break;
4121541Srgrimes      if (SE.TD) {
4131541Srgrimes        // With TargetData, field offsets are known. See if a constant offset
4141541Srgrimes        // falls within any of the struct fields.
4151541Srgrimes        if (Ops.empty()) break;
4161541Srgrimes        if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0]))
4171541Srgrimes          if (SE.getTypeSizeInBits(C->getType()) <= 64) {
4181541Srgrimes            const StructLayout &SL = *SE.TD->getStructLayout(STy);
4191541Srgrimes            uint64_t FullOffset = C->getValue()->getZExtValue();
4201541Srgrimes            if (FullOffset < SL.getSizeInBytes()) {
4211541Srgrimes              unsigned ElIdx = SL.getElementContainingOffset(FullOffset);
4221541Srgrimes              GepIndices.push_back(
4231541Srgrimes                  ConstantInt::get(Type::getInt32Ty(Ty->getContext()), ElIdx));
4241541Srgrimes              ElTy = STy->getTypeAtIndex(ElIdx);
42512221Sbde              Ops[0] =
4261541Srgrimes                SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
4271541Srgrimes              AnyNonZeroIndices = true;
4281541Srgrimes              FoundFieldNo = true;
4291541Srgrimes            }
43012221Sbde          }
43182749Sdillon      } else {
43282749Sdillon        // Without TargetData, just check for an offsetof expression of the
43382749Sdillon        // appropriate struct type.
4341541Srgrimes        for (unsigned i = 0, e = Ops.size(); i != e; ++i)
4351549Srgrimes          if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(Ops[i])) {
43683366Sjulian            const Type *CTy;
43783366Sjulian            Constant *FieldNo;
4381541Srgrimes            if (U->isOffsetOf(CTy, FieldNo) && CTy == STy) {
4391541Srgrimes              GepIndices.push_back(FieldNo);
44083366Sjulian              ElTy =
4411541Srgrimes                STy->getTypeAtIndex(cast<ConstantInt>(FieldNo)->getZExtValue());
4421541Srgrimes              Ops[i] = SE.getConstant(Ty, 0);
44375448Srwatson              AnyNonZeroIndices = true;
4441541Srgrimes              FoundFieldNo = true;
44520677Sbde              break;
44620677Sbde            }
44782749Sdillon          }
44882749Sdillon      }
44982749Sdillon      // If no struct field offsets were found, tentatively assume that
45086304Sjhb      // field zero was selected (since the zero offset would obviously
4511541Srgrimes      // be folded away).
45275893Sjhb      if (!FoundFieldNo) {
45375893Sjhb        ElTy = STy->getTypeAtIndex(0u);
45475893Sjhb        GepIndices.push_back(
45582749Sdillon          Constant::getNullValue(Type::getInt32Ty(Ty->getContext())));
45682749Sdillon      }
45775893Sjhb    }
45879335Srwatson
45975893Sjhb    if (const ArrayType *ATy = dyn_cast<ArrayType>(ElTy))
46082749Sdillon      ElTy = ATy->getElementType();
46175893Sjhb    else
46275893Sjhb      break;
46375893Sjhb  }
46475893Sjhb
46582749Sdillon  // If none of the operands were convertable to proper GEP indices, cast
46682749Sdillon  // the base to i8* and do an ugly getelementptr with that. It's still
46775893Sjhb  // better than ptrtoint+arithmetic+inttoptr at least.
46875893Sjhb  if (!AnyNonZeroIndices) {
46975893Sjhb    // Cast the base to i8*.
47082749Sdillon    V = InsertNoopCastOfTo(V,
47182749Sdillon       Type::getInt8PtrTy(Ty->getContext(), PTy->getAddressSpace()));
47275893Sjhb
47375893Sjhb    // Expand the operands for a plain byte offset.
4741541Srgrimes    Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty);
47575893Sjhb
47675893Sjhb    // Fold a GEP with constant operands.
47775893Sjhb    if (Constant *CLHS = dyn_cast<Constant>(V))
47875893Sjhb      if (Constant *CRHS = dyn_cast<Constant>(Idx))
47982749Sdillon        return ConstantExpr::getGetElementPtr(CLHS, &CRHS, 1);
48082749Sdillon
48175893Sjhb    // Do a quick scan to see if we have this GEP nearby.  If so, reuse it.
48282749Sdillon    unsigned ScanLimit = 6;
4831541Srgrimes    BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
48482749Sdillon    // Scanning starts from the last instruction before the insertion point.
4851541Srgrimes    BasicBlock::iterator IP = Builder.GetInsertPoint();
48675893Sjhb    if (IP != BlockBegin) {
48775893Sjhb      --IP;
48882749Sdillon      for (; ScanLimit; --IP, --ScanLimit) {
48982749Sdillon        if (IP->getOpcode() == Instruction::GetElementPtr &&
49075893Sjhb            IP->getOperand(0) == V && IP->getOperand(1) == Idx)
49182749Sdillon          return IP;
49275893Sjhb        if (IP == BlockBegin) break;
49375893Sjhb      }
49482749Sdillon    }
49582749Sdillon
49686304Sjhb    // Emit a GEP.
49782749Sdillon    Value *GEP = Builder.CreateGEP(V, Idx, "uglygep");
49882749Sdillon    rememberInstruction(GEP);
4991541Srgrimes    return GEP;
5001541Srgrimes  }
50124448Speter
50224448Speter  // Insert a pretty getelementptr. Note that this GEP is not marked inbounds,
50372093Sasmodai  // because ScalarEvolution may have changed the address arithmetic to
50424448Speter  // compute a value which is beyond the end of the allocated object.
50524448Speter  Value *Casted = V;
50624448Speter  if (V->getType() != PTy)
50724448Speter    Casted = InsertNoopCastOfTo(Casted, PTy);
50824448Speter  Value *GEP = Builder.CreateGEP(Casted,
50924448Speter                                 GepIndices.begin(),
51024448Speter                                 GepIndices.end(),
51124448Speter                                 "scevgep");
51224448Speter  Ops.push_back(SE.getUnknown(GEP));
51312221Sbde  rememberInstruction(GEP);
5141541Srgrimes  return expand(SE.getAddExpr(Ops));
5151541Srgrimes}
5161541Srgrimes
51712221Sbde/// isNonConstantNegative - Return true if the specified scev is negated, but
51882749Sdillon/// not a constant.
51982749Sdillonstatic bool isNonConstantNegative(const SCEV *F) {
52082749Sdillon  const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(F);
5211541Srgrimes  if (!Mul) return false;
5221549Srgrimes
52383366Sjulian  // If there is a constant factor, it will be first.
52483366Sjulian  const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
5251541Srgrimes  if (!SC) return false;
5261541Srgrimes
52783366Sjulian  // Return true if the value is negative, this matches things like (-42 * V).
52877183Srwatson  return SC->getValue()->getValue().isNegative();
52977183Srwatson}
53082749Sdillon
5311541SrgrimesValue *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
53277183Srwatson  int NumOperands = S->getNumOperands();
53377183Srwatson  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
53482749Sdillon
53582749Sdillon  // Find the index of an operand to start with. Choose the operand with
53624448Speter  // pointer type, if there is one, or the last operand otherwise.
53724448Speter  int PIdx = 0;
53824448Speter  for (; PIdx != NumOperands - 1; ++PIdx)
53924448Speter    if (isa<PointerType>(S->getOperand(PIdx)->getType())) break;
54024448Speter
54172093Sasmodai  // Expand code for the operand that we chose.
54224448Speter  Value *V = expand(S->getOperand(PIdx));
54324448Speter
54424448Speter  // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
54524448Speter  // comments on expandAddToGEP for details.
54624448Speter  if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) {
54724448Speter    // Take the operand at PIdx out of the list.
54824448Speter    const SmallVectorImpl<const SCEV *> &Ops = S->getOperands();
54924448Speter    SmallVector<const SCEV *, 8> NewOps;
55024448Speter    NewOps.insert(NewOps.end(), Ops.begin(), Ops.begin() + PIdx);
55124448Speter    NewOps.insert(NewOps.end(), Ops.begin() + PIdx + 1, Ops.end());
55224448Speter    // Make a GEP.
55377183Srwatson    return expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, V);
55417994Sache  }
55577183Srwatson
55617994Sache  // Otherwise, we'll expand the rest of the SCEVAddExpr as plain integer
55724448Speter  // arithmetic.
55877183Srwatson  V = InsertNoopCastOfTo(V, Ty);
55924448Speter
56077183Srwatson  // Emit a bunch of add instructions
56182749Sdillon  for (int i = NumOperands-1; i >= 0; --i) {
56224448Speter    if (i == PIdx) continue;
56377183Srwatson    const SCEV *Op = S->getOperand(i);
56424448Speter    if (isNonConstantNegative(Op)) {
5651541Srgrimes      Value *W = expandCodeFor(SE.getNegativeSCEV(Op), Ty);
56624448Speter      V = InsertBinop(Instruction::Sub, V, W);
56724448Speter    } else {
5681541Srgrimes      Value *W = expandCodeFor(Op, Ty);
56917994Sache      V = InsertBinop(Instruction::Add, V, W);
57024448Speter    }
57177183Srwatson  }
57217994Sache  return V;
57377183Srwatson}
57417994Sache
57524448SpeterValue *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
57624448Speter  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
57765495Struckman  int FirstOp = 0;  // Set if we should emit a subtract.
57824448Speter  if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
57977183Srwatson    if (SC->getValue()->isAllOnesValue())
58077183Srwatson      FirstOp = 1;
58165495Struckman
58224448Speter  int i = S->getNumOperands()-2;
58324448Speter  Value *V = expandCodeFor(S->getOperand(i+1), Ty);
58424448Speter
58524448Speter  // Emit a bunch of multiply instructions
58624448Speter  for (; i >= FirstOp; --i) {
58724448Speter    Value *W = expandCodeFor(S->getOperand(i), Ty);
58824448Speter    V = InsertBinop(Instruction::Mul, V, W);
58924448Speter  }
59077183Srwatson
59177183Srwatson  // -1 * ...  --->  0 - ...
59231891Ssef  if (FirstOp == 1)
59324448Speter    V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V);
5948141Sache  return V;
59524448Speter}
59624448Speter
59724448SpeterValue *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
59824448Speter  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
59924448Speter
60077183Srwatson  Value *LHS = expandCodeFor(S->getLHS(), Ty);
60177183Srwatson  if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
60231891Ssef    const APInt &RHS = SC->getValue()->getValue();
60324448Speter    if (RHS.isPowerOf2())
60477183Srwatson      return InsertBinop(Instruction::LShr, LHS,
60577183Srwatson                         ConstantInt::get(Ty, RHS.logBase2()));
60682749Sdillon  }
60782749Sdillon
60882749Sdillon  Value *RHS = expandCodeFor(S->getRHS(), Ty);
6091541Srgrimes  return InsertBinop(Instruction::UDiv, LHS, RHS);
6101541Srgrimes}
61112221Sbde
6121541Srgrimes/// Move parts of Base into Rest to leave Base with the minimal
6131541Srgrimes/// expression that provides a pointer operand suitable for a
6141541Srgrimes/// GEP expansion.
61512221Sbdestatic void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest,
61682749Sdillon                              ScalarEvolution &SE) {
61782749Sdillon  while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) {
61882749Sdillon    Base = A->getStart();
6191541Srgrimes    Rest = SE.getAddExpr(Rest,
6201549Srgrimes                         SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()),
62183366Sjulian                                          A->getStepRecurrence(SE),
62283366Sjulian                                          A->getLoop()));
6231541Srgrimes  }
6241541Srgrimes  if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) {
62583366Sjulian    Base = A->getOperand(A->getNumOperands()-1);
62677183Srwatson    SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end());
62777183Srwatson    NewAddOps.back() = Rest;
62882749Sdillon    Rest = SE.getAddExpr(NewAddOps);
6291541Srgrimes    ExposePointerBase(Base, Rest, SE);
6301541Srgrimes  }
63182749Sdillon}
63282749Sdillon
63377183Srwatson/// getAddRecExprPHILiterally - Helper for expandAddRecExprLiterally. Expand
63477183Srwatson/// the base addrec, which is the addrec without any non-loop-dominating
63577183Srwatson/// values, and return the PHI.
63682749SdillonPHINode *
63782749SdillonSCEVExpander::getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
63882749Sdillon                                        const Loop *L,
6391541Srgrimes                                        const Type *ExpandTy,
6401541Srgrimes                                        const Type *IntTy) {
6411541Srgrimes  // Reuse a previously-inserted PHI, if present.
6421541Srgrimes  for (BasicBlock::iterator I = L->getHeader()->begin();
64377183Srwatson       PHINode *PN = dyn_cast<PHINode>(I); ++I)
64477183Srwatson    if (SE.isSCEVable(PN->getType()) &&
64577183Srwatson        (SE.getEffectiveSCEVType(PN->getType()) ==
64631891Ssef         SE.getEffectiveSCEVType(Normalized->getType())) &&
64724449Speter        SE.getSCEV(PN) == Normalized)
64877183Srwatson      if (BasicBlock *LatchBlock = L->getLoopLatch()) {
64977183Srwatson        Instruction *IncV =
65082749Sdillon          cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock));
65182749Sdillon
65282749Sdillon        // Determine if this is a well-behaved chain of instructions leading
6531541Srgrimes        // back to the PHI. It probably will be, if we're scanning an inner
6541541Srgrimes        // loop already visited by LSR for example, but it wouldn't have
65512221Sbde        // to be.
6561541Srgrimes        do {
6571541Srgrimes          if (IncV->getNumOperands() == 0 || isa<PHINode>(IncV)) {
6581541Srgrimes            IncV = 0;
65912221Sbde            break;
66082749Sdillon          }
66182749Sdillon          IncV = dyn_cast<Instruction>(IncV->getOperand(0));
66282749Sdillon          if (!IncV)
6631541Srgrimes            break;
6641549Srgrimes          if (IncV->mayHaveSideEffects()) {
66583366Sjulian            IncV = 0;
66683366Sjulian            break;
6671541Srgrimes          }
6681541Srgrimes        } while (IncV != PN);
66983366Sjulian
67077183Srwatson        if (IncV) {
67177183Srwatson          // Ok, the add recurrence looks usable.
67282749Sdillon          // Remember this PHI, even in post-inc mode.
6731541Srgrimes          InsertedValues.insert(PN);
67477183Srwatson          // Remember the increment.
67582749Sdillon          IncV = cast<Instruction>(PN->getIncomingValueForBlock(LatchBlock));
67682749Sdillon          rememberInstruction(IncV);
67777183Srwatson          if (L == IVIncInsertLoop)
67824448Speter            do {
67924448Speter              if (SE.DT->dominates(IncV, IVIncInsertPos))
68024448Speter                break;
68124448Speter              // Make sure the increment is where we want it. But don't move it
68224448Speter              // down past a potential existing post-inc user.
68372093Sasmodai              IncV->moveBefore(IVIncInsertPos);
68424448Speter              IVIncInsertPos = IncV;
68524448Speter              IncV = cast<Instruction>(IncV->getOperand(0));
68624448Speter            } while (IncV != PN);
68724448Speter          return PN;
68824448Speter        }
68977183Srwatson      }
69017994Sache
69177183Srwatson  // Save the original insertion point so we can restore it when we're done.
69217994Sache  BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
69324448Speter  BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
69477183Srwatson
69524448Speter  // Expand code for the start value.
69682749Sdillon  Value *StartV = expandCodeFor(Normalized->getStart(), ExpandTy,
69782749Sdillon                                L->getHeader()->begin());
69882749Sdillon
69924448Speter  // Expand code for the step value. Insert instructions right before the
70077183Srwatson  // terminator corresponding to the back-edge. Do this before creating the PHI
70117994Sache  // so that PHI reuse code doesn't see an incomplete PHI. If the stride is
70224448Speter  // negative, insert a sub instead of an add for the increment (unless it's a
70324448Speter  // constant, because subtracts of constants are canonicalized to adds).
70424448Speter  const SCEV *Step = Normalized->getStepRecurrence(SE);
70524448Speter  bool isPointer = isa<PointerType>(ExpandTy);
70624448Speter  bool isNegative = !isPointer && isNonConstantNegative(Step);
70724448Speter  if (isNegative)
70877183Srwatson    Step = SE.getNegativeSCEV(Step);
70917994Sache  Value *StepV = expandCodeFor(Step, IntTy, L->getHeader()->begin());
71077183Srwatson
71124448Speter  // Create the PHI.
71224448Speter  Builder.SetInsertPoint(L->getHeader(), L->getHeader()->begin());
71324448Speter  PHINode *PN = Builder.CreatePHI(ExpandTy, "lsr.iv");
71424448Speter  rememberInstruction(PN);
71524448Speter
71677183Srwatson  // Create the step instructions and populate the PHI.
71777183Srwatson  BasicBlock *Header = L->getHeader();
71831891Ssef  for (pred_iterator HPI = pred_begin(Header), HPE = pred_end(Header);
71924448Speter       HPI != HPE; ++HPI) {
72024448Speter    BasicBlock *Pred = *HPI;
72124448Speter
72224448Speter    // Add a start value.
72324448Speter    if (!L->contains(Pred)) {
72424448Speter      PN->addIncoming(StartV, Pred);
72524448Speter      continue;
72624448Speter    }
72777183Srwatson
72877183Srwatson    // Create a step value and add it to the PHI. If IVIncInsertLoop is
72931891Ssef    // non-null and equal to the addrec's loop, insert the instructions
73024448Speter    // at IVIncInsertPos.
7318141Sache    Instruction *InsertPos = L == IVIncInsertLoop ?
73224448Speter      IVIncInsertPos : Pred->getTerminator();
73324448Speter    Builder.SetInsertPoint(InsertPos->getParent(), InsertPos);
73424448Speter    Value *IncV;
73524448Speter    // If the PHI is a pointer, use a GEP, otherwise use an add or sub.
73677183Srwatson    if (isPointer) {
73777183Srwatson      const PointerType *GEPPtrTy = cast<PointerType>(ExpandTy);
73831891Ssef      // If the step isn't constant, don't use an implicitly scaled GEP, because
73924448Speter      // that would require a multiply inside the loop.
74077183Srwatson      if (!isa<ConstantInt>(StepV))
74177183Srwatson        GEPPtrTy = PointerType::get(Type::getInt1Ty(SE.getContext()),
74282749Sdillon                                    GEPPtrTy->getAddressSpace());
74382749Sdillon      const SCEV *const StepArray[1] = { SE.getSCEV(StepV) };
74482749Sdillon      IncV = expandAddToGEP(StepArray, StepArray+1, GEPPtrTy, IntTy, PN);
7451541Srgrimes      if (IncV->getType() != PN->getType()) {
7461541Srgrimes        IncV = Builder.CreateBitCast(IncV, PN->getType(), "tmp");
74712221Sbde        rememberInstruction(IncV);
7481541Srgrimes      }
7491541Srgrimes    } else {
7501541Srgrimes      IncV = isNegative ?
75112221Sbde        Builder.CreateSub(PN, StepV, "lsr.iv.next") :
75282749Sdillon        Builder.CreateAdd(PN, StepV, "lsr.iv.next");
75382749Sdillon      rememberInstruction(IncV);
75482749Sdillon    }
7551541Srgrimes    PN->addIncoming(IncV, Pred);
7561549Srgrimes  }
75783366Sjulian
75883366Sjulian  // Restore the original insert point.
7591541Srgrimes  if (SaveInsertBB)
7601541Srgrimes    restoreInsertPoint(SaveInsertBB, SaveInsertPt);
76183366Sjulian
76277183Srwatson  // Remember this PHI, even in post-inc mode.
76377183Srwatson  InsertedValues.insert(PN);
76482749Sdillon
7651541Srgrimes  return PN;
7661541Srgrimes}
76782749Sdillon
76882749SdillonValue *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
76977183Srwatson  const Type *STy = S->getType();
77077183Srwatson  const Type *IntTy = SE.getEffectiveSCEVType(STy);
77177183Srwatson  const Loop *L = S->getLoop();
77282749Sdillon
77382749Sdillon  // Determine a normalized form of this expression, which is the expression
77482749Sdillon  // before any post-inc adjustment is made.
77577183Srwatson  const SCEVAddRecExpr *Normalized = S;
77677183Srwatson  if (L == PostIncLoop) {
77777183Srwatson    const SCEV *Step = S->getStepRecurrence(SE);
77831891Ssef    Normalized = cast<SCEVAddRecExpr>(SE.getMinusSCEV(S, Step));
77924449Speter  }
78077183Srwatson
78177183Srwatson  // Strip off any non-loop-dominating component from the addrec start.
78282749Sdillon  const SCEV *Start = Normalized->getStart();
78382749Sdillon  const SCEV *PostLoopOffset = 0;
78482749Sdillon  if (!Start->properlyDominates(L->getHeader(), SE.DT)) {
7851541Srgrimes    PostLoopOffset = Start;
7861541Srgrimes    Start = SE.getIntegerSCEV(0, Normalized->getType());
78712221Sbde    Normalized =
7881541Srgrimes      cast<SCEVAddRecExpr>(SE.getAddRecExpr(Start,
7891541Srgrimes                                            Normalized->getStepRecurrence(SE),
7901541Srgrimes                                            Normalized->getLoop()));
7911541Srgrimes  }
79212221Sbde
79382749Sdillon  // Strip off any non-loop-dominating component from the addrec step.
79482749Sdillon  const SCEV *Step = Normalized->getStepRecurrence(SE);
79582749Sdillon  const SCEV *PostLoopScale = 0;
7961541Srgrimes  if (!Step->hasComputableLoopEvolution(L) &&
7971549Srgrimes      !Step->dominates(L->getHeader(), SE.DT)) {
79883366Sjulian    PostLoopScale = Step;
79983366Sjulian    Step = SE.getIntegerSCEV(1, Normalized->getType());
8001541Srgrimes    Normalized =
8011541Srgrimes      cast<SCEVAddRecExpr>(SE.getAddRecExpr(Start, Step,
80283366Sjulian                                            Normalized->getLoop()));
80377183Srwatson  }
80477183Srwatson
8051541Srgrimes  // Expand the core addrec. If we need post-loop scaling, force it to
8061541Srgrimes  // expand to an integer type to avoid the need for additional casting.
80782749Sdillon  const Type *ExpandTy = PostLoopScale ? IntTy : STy;
80882749Sdillon  PHINode *PN = getAddRecExprPHILiterally(Normalized, L, ExpandTy, IntTy);
80977183Srwatson
81077183Srwatson  // Accomodate post-inc mode, if necessary.
81177183Srwatson  Value *Result;
81282749Sdillon  if (L != PostIncLoop)
81382749Sdillon    Result = PN;
81482749Sdillon  else {
81582749Sdillon    // In PostInc mode, use the post-incremented value.
81682749Sdillon    BasicBlock *LatchBlock = L->getLoopLatch();
81724447Speter    assert(LatchBlock && "PostInc mode requires a unique loop latch!");
81824447Speter    Result = PN->getIncomingValueForBlock(LatchBlock);
81924447Speter  }
82024447Speter
82177183Srwatson  // Re-apply any non-loop-dominating scale.
82224447Speter  if (PostLoopScale) {
82324447Speter    Result = InsertNoopCastOfTo(Result, IntTy);
82424447Speter    Result = Builder.CreateMul(Result,
82524447Speter                               expandCodeFor(PostLoopScale, IntTy));
82624447Speter    rememberInstruction(Result);
82724447Speter  }
82824447Speter
82977183Srwatson  // Re-apply any non-loop-dominating offset.
83024447Speter  if (PostLoopOffset) {
83124447Speter    if (const PointerType *PTy = dyn_cast<PointerType>(ExpandTy)) {
83277183Srwatson      const SCEV *const OffsetArray[1] = { PostLoopOffset };
83377183Srwatson      Result = expandAddToGEP(OffsetArray, OffsetArray+1, PTy, IntTy, Result);
83482749Sdillon    } else {
83577183Srwatson      Result = InsertNoopCastOfTo(Result, IntTy);
83677183Srwatson      Result = Builder.CreateAdd(Result,
83724447Speter                                 expandCodeFor(PostLoopOffset, IntTy));
83831891Ssef      rememberInstruction(Result);
83977183Srwatson    }
84077183Srwatson  }
84182749Sdillon
84282749Sdillon  return Result;
84382749Sdillon}
8441541Srgrimes
8451541SrgrimesValue *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
84612221Sbde  if (!CanonicalMode) return expandAddRecExprLiterally(S);
8471541Srgrimes
8489238Sache  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
8499238Sache  const Loop *L = S->getLoop();
8501541Srgrimes
85112221Sbde  // First check for an existing canonical IV in a suitable type.
85282749Sdillon  PHINode *CanonicalIV = 0;
85382749Sdillon  if (PHINode *PN = L->getCanonicalInductionVariable())
85482749Sdillon    if (SE.isSCEVable(PN->getType()) &&
8551541Srgrimes        isa<IntegerType>(SE.getEffectiveSCEVType(PN->getType())) &&
8561549Srgrimes        SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty))
85783366Sjulian      CanonicalIV = PN;
85883366Sjulian
8591541Srgrimes  // Rewrite an AddRec in terms of the canonical induction variable, if
8601541Srgrimes  // its type is more narrow.
86183366Sjulian  if (CanonicalIV &&
86277183Srwatson      SE.getTypeSizeInBits(CanonicalIV->getType()) >
86377183Srwatson      SE.getTypeSizeInBits(Ty)) {
86482749Sdillon    const SmallVectorImpl<const SCEV *> &Ops = S->getOperands();
8651541Srgrimes    SmallVector<const SCEV *, 4> NewOps(Ops.size());
8669238Sache    for (unsigned i = 0, e = Ops.size(); i != e; ++i)
8679238Sache      NewOps[i] = SE.getAnyExtendExpr(Ops[i], CanonicalIV->getType());
86882749Sdillon    Value *V = expand(SE.getAddRecExpr(NewOps, S->getLoop()));
86982749Sdillon    BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
87082749Sdillon    BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
87177183Srwatson    BasicBlock::iterator NewInsertPt =
87277183Srwatson      llvm::next(BasicBlock::iterator(cast<Instruction>(V)));
87377183Srwatson    while (isa<PHINode>(NewInsertPt)) ++NewInsertPt;
87477183Srwatson    V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0,
87577183Srwatson                      NewInsertPt);
87682749Sdillon    restoreInsertPoint(SaveInsertBB, SaveInsertPt);
87782749Sdillon    return V;
87882749Sdillon  }
87977183Srwatson
88077183Srwatson  // {X,+,F} --> X + {0,+,F}
88177183Srwatson  if (!S->getStart()->isZero()) {
88231891Ssef    const SmallVectorImpl<const SCEV *> &SOperands = S->getOperands();
88324450Speter    SmallVector<const SCEV *, 4> NewOps(SOperands.begin(), SOperands.end());
88477183Srwatson    NewOps[0] = SE.getIntegerSCEV(0, Ty);
88577183Srwatson    const SCEV *Rest = SE.getAddRecExpr(NewOps, L);
88631891Ssef
8878135Sache    // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
88877183Srwatson    // comments on expandAddToGEP for details.
88977183Srwatson    const SCEV *Base = S->getStart();
89077183Srwatson    const SCEV *RestArray[1] = { Rest };
89131891Ssef    // Dig into the expression to find the pointer base for a GEP.
89224450Speter    ExposePointerBase(Base, RestArray[0], SE);
89377183Srwatson    // If we found a pointer, expand the AddRec with a GEP.
89477183Srwatson    if (const PointerType *PTy = dyn_cast<PointerType>(Base->getType())) {
89582749Sdillon      // Make sure the Base isn't something exotic, such as a multiplied
89682749Sdillon      // or divided pointer value. In those cases, the result type isn't
89782749Sdillon      // actually a pointer type.
8981541Srgrimes      if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) {
8991541Srgrimes        Value *StartV = expand(Base);
90012221Sbde        assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!");
9011541Srgrimes        return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV);
9029238Sache      }
9039238Sache    }
9041541Srgrimes
90512221Sbde    // Just do a normal add. Pre-expand the operands to suppress folding.
90682749Sdillon    return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())),
90782749Sdillon                                SE.getUnknown(expand(Rest))));
90882749Sdillon  }
9091541Srgrimes
9101549Srgrimes  // {0,+,1} --> Insert a canonical induction variable into the loop!
91183366Sjulian  if (S->isAffine() &&
91283366Sjulian      S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) {
9131541Srgrimes    // If there's a canonical IV, just use it.
9141541Srgrimes    if (CanonicalIV) {
91583366Sjulian      assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) &&
91677183Srwatson             "IVs with types different from the canonical IV should "
91777183Srwatson             "already have been handled!");
91882749Sdillon      return CanonicalIV;
9191541Srgrimes    }
9209238Sache
9219238Sache    // Create and insert the PHI node for the induction variable in the
92282749Sdillon    // specified loop.
92382749Sdillon    BasicBlock *Header = L->getHeader();
92482749Sdillon    PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
92577183Srwatson    rememberInstruction(PN);
92677183Srwatson
92777183Srwatson    Constant *One = ConstantInt::get(Ty, 1);
92877183Srwatson    for (pred_iterator HPI = pred_begin(Header), HPE = pred_end(Header);
92977183Srwatson         HPI != HPE; ++HPI)
93082749Sdillon      if (L->contains(*HPI)) {
93182749Sdillon        // Insert a unit add instruction right before the terminator
93282749Sdillon        // corresponding to the back-edge.
9339238Sache        Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
93477183Srwatson                                                     (*HPI)->getTerminator());
93577183Srwatson        rememberInstruction(Add);
93677183Srwatson        PN->addIncoming(Add, *HPI);
93731891Ssef      } else {
93824450Speter        PN->addIncoming(Constant::getNullValue(Ty), *HPI);
93977183Srwatson      }
94077183Srwatson  }
94131891Ssef
94224450Speter  // {0,+,F} --> {0,+,1} * F
94377183Srwatson  // Get the canonical induction variable I for this loop.
94477183Srwatson  Value *I = CanonicalIV ?
94577183Srwatson             CanonicalIV :
94631891Ssef             getOrInsertCanonicalInductionVariable(L, Ty);
94724450Speter
94877812Sru  // If this is a simple linear addrec, emit it now as a special case.
94977812Sru  if (S->isAffine())    // {0,+,F} --> i*F
95082749Sdillon    return
95182749Sdillon      expand(SE.getTruncateOrNoop(
95282749Sdillon        SE.getMulExpr(SE.getUnknown(I),
9531541Srgrimes                      SE.getNoopOrAnyExtend(S->getOperand(1),
9541541Srgrimes                                            I->getType())),
95556115Speter        Ty));
95656115Speter
95756115Speter  // If this is a chain of recurrences, turn it into a closed form, using the
95856115Speter  // folders, then expandCodeFor the closed form.  This allows the folders to
95956115Speter  // simplify the expression without having to build a bunch of special code
96024453Speter  // into this folder.
96156115Speter  const SCEV *IH = SE.getUnknown(I);   // Get I as a "symbolic" SCEV.
96256115Speter
96356115Speter  // Promote S up to the canonical IV type, if the cast is foldable.
96456115Speter  const SCEV *NewS = S;
96556115Speter  const SCEV *Ext = SE.getNoopOrAnyExtend(S, I->getType());
96656115Speter  if (isa<SCEVAddRecExpr>(Ext))
96782749Sdillon    NewS = Ext;
96882749Sdillon
96982749Sdillon  const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE);
97056115Speter  //cerr << "Evaluated: " << *this << "\n     to: " << *V << "\n";
97156115Speter
97283366Sjulian  // Truncate the result down to the original type, if needed.
97383366Sjulian  const SCEV *T = SE.getTruncateOrNoop(V, Ty);
97456115Speter  return expand(T);
97556115Speter}
97683366Sjulian
97777183SrwatsonValue *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
97877183Srwatson  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
97956115Speter  Value *V = expandCodeFor(S->getOperand(),
98056115Speter                           SE.getEffectiveSCEVType(S->getOperand()->getType()));
98156115Speter  Value *I = Builder.CreateTrunc(V, Ty, "tmp");
98256115Speter  rememberInstruction(I);
98356115Speter  return I;
98482749Sdillon}
98582749Sdillon
98677183SrwatsonValue *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
98777183Srwatson  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
98877183Srwatson  Value *V = expandCodeFor(S->getOperand(),
98977183Srwatson                           SE.getEffectiveSCEVType(S->getOperand()->getType()));
99077183Srwatson  Value *I = Builder.CreateZExt(V, Ty, "tmp");
99177183Srwatson  rememberInstruction(I);
99277183Srwatson  return I;
99377183Srwatson}
99477183Srwatson
99577183SrwatsonValue *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
99682749Sdillon  const Type *Ty = SE.getEffectiveSCEVType(S->getType());
99782749Sdillon  Value *V = expandCodeFor(S->getOperand(),
99882749Sdillon                           SE.getEffectiveSCEVType(S->getOperand()->getType()));
99977183Srwatson  Value *I = Builder.CreateSExt(V, Ty, "tmp");
100077183Srwatson  rememberInstruction(I);
100177183Srwatson  return I;
100277183Srwatson}
100356115Speter
100456115SpeterValue *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
100577183Srwatson  Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
100677183Srwatson  const Type *Ty = LHS->getType();
100756115Speter  for (int i = S->getNumOperands()-2; i >= 0; --i) {
100856115Speter    // In the case of mixed integer and pointer types, do the
100977183Srwatson    // rest of the comparisons as integer.
101077183Srwatson    if (S->getOperand(i)->getType() != Ty) {
101156115Speter      Ty = SE.getEffectiveSCEVType(Ty);
101256115Speter      LHS = InsertNoopCastOfTo(LHS, Ty);
101377183Srwatson    }
101477183Srwatson    Value *RHS = expandCodeFor(S->getOperand(i), Ty);
101582749Sdillon    Value *ICmp = Builder.CreateICmpSGT(LHS, RHS, "tmp");
101682749Sdillon    rememberInstruction(ICmp);
101782749Sdillon    Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax");
101882749Sdillon    rememberInstruction(Sel);
101956115Speter    LHS = Sel;
102056115Speter  }
102156115Speter  // In the case of mixed integer and pointer types, cast the
102256115Speter  // final result back to the pointer type.
102356115Speter  if (LHS->getType() != S->getType())
102456115Speter    LHS = InsertNoopCastOfTo(LHS, S->getType());
102556115Speter  return LHS;
102656115Speter}
102756115Speter
102856115SpeterValue *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
102956115Speter  Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
103056115Speter  const Type *Ty = LHS->getType();
103156115Speter  for (int i = S->getNumOperands()-2; i >= 0; --i) {
103256115Speter    // In the case of mixed integer and pointer types, do the
103382749Sdillon    // rest of the comparisons as integer.
103482749Sdillon    if (S->getOperand(i)->getType() != Ty) {
103582749Sdillon      Ty = SE.getEffectiveSCEVType(Ty);
103656115Speter      LHS = InsertNoopCastOfTo(LHS, Ty);
103756115Speter    }
103883366Sjulian    Value *RHS = expandCodeFor(S->getOperand(i), Ty);
103983366Sjulian    Value *ICmp = Builder.CreateICmpUGT(LHS, RHS, "tmp");
104056115Speter    rememberInstruction(ICmp);
104156115Speter    Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax");
104283366Sjulian    rememberInstruction(Sel);
104377183Srwatson    LHS = Sel;
104477183Srwatson  }
104556115Speter  // In the case of mixed integer and pointer types, cast the
104656115Speter  // final result back to the pointer type.
104756115Speter  if (LHS->getType() != S->getType())
104856115Speter    LHS = InsertNoopCastOfTo(LHS, S->getType());
104956115Speter  return LHS;
105082749Sdillon}
105182749Sdillon
105277183SrwatsonValue *SCEVExpander::expandCodeFor(const SCEV *SH, const Type *Ty) {
105377183Srwatson  // Expand the code for this SCEV.
105477183Srwatson  Value *V = expand(SH);
105577183Srwatson  if (Ty) {
105677183Srwatson    assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
105777183Srwatson           "non-trivial casts should be done with the SCEVs directly!");
105877183Srwatson    V = InsertNoopCastOfTo(V, Ty);
105977183Srwatson  }
106077183Srwatson  return V;
106177183Srwatson}
106282749Sdillon
106382749SdillonValue *SCEVExpander::expand(const SCEV *S) {
106482749Sdillon  // Compute an insertion point for this SCEV object. Hoist the instructions
106577183Srwatson  // as far out in the loop nest as possible.
106677183Srwatson  Instruction *InsertPt = Builder.GetInsertPoint();
106777183Srwatson  for (Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock()); ;
106856115Speter       L = L->getParentLoop())
106956115Speter    if (S->isLoopInvariant(L)) {
107077183Srwatson      if (!L) break;
107177183Srwatson      if (BasicBlock *Preheader = L->getLoopPreheader())
107256115Speter        InsertPt = Preheader->getTerminator();
107356115Speter    } else {
107477183Srwatson      // If the SCEV is computable at this level, insert it into the header
107577183Srwatson      // after the PHIs (and after any other instructions that we've inserted
107656115Speter      // there) so that it is guaranteed to dominate any user inside the loop.
107756115Speter      if (L && S->hasComputableLoopEvolution(L))
107877183Srwatson        InsertPt = L->getHeader()->getFirstNonPHI();
107977183Srwatson      while (isInsertedInstruction(InsertPt))
108082749Sdillon        InsertPt = llvm::next(BasicBlock::iterator(InsertPt));
108182749Sdillon      break;
108282749Sdillon    }
108382749Sdillon
108456115Speter  // Check to see if we already expanded this here.
108556115Speter  std::map<std::pair<const SCEV *, Instruction *>,
108656115Speter           AssertingVH<Value> >::iterator I =
108756115Speter    InsertedExpressions.find(std::make_pair(S, InsertPt));
108856115Speter  if (I != InsertedExpressions.end())
108956115Speter    return I->second;
109056115Speter
109156115Speter  BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
109256115Speter  BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
109382749Sdillon  Builder.SetInsertPoint(InsertPt->getParent(), InsertPt);
109482749Sdillon
109582749Sdillon  // Expand the expression into instructions.
109656115Speter  Value *V = visit(S);
109756115Speter
109883366Sjulian  // Remember the expanded value for this SCEV at this location.
109983366Sjulian  if (!PostIncLoop)
110056115Speter    InsertedExpressions[std::make_pair(S, InsertPt)] = V;
110156115Speter
110282749Sdillon  restoreInsertPoint(SaveInsertBB, SaveInsertPt);
110383366Sjulian  return V;
110456115Speter}
110556115Speter
110682749Sdillonvoid SCEVExpander::rememberInstruction(Value *I) {
110782749Sdillon  if (!PostIncLoop)
110882749Sdillon    InsertedValues.insert(I);
110956115Speter
111077183Srwatson  // If we just claimed an existing instruction and that instruction had
111177183Srwatson  // been the insert point, adjust the insert point forward so that
111256115Speter  // subsequently inserted code will be dominated.
111377183Srwatson  if (Builder.GetInsertPoint() == I) {
111477183Srwatson    BasicBlock::iterator It = cast<Instruction>(I);
111556115Speter    do { ++It; } while (isInsertedInstruction(It));
111677183Srwatson    Builder.SetInsertPoint(Builder.GetInsertBlock(), It);
111777183Srwatson  }
111882749Sdillon}
111956115Speter
112056115Spetervoid SCEVExpander::restoreInsertPoint(BasicBlock *BB, BasicBlock::iterator I) {
112156115Speter  // If we aquired more instructions since the old insert point was saved,
112256115Speter  // advance past them.
112356115Speter  while (isInsertedInstruction(I)) ++I;
112456115Speter
112556115Speter  Builder.SetInsertPoint(BB, I);
112656115Speter}
112756115Speter
112856115Speter/// getOrInsertCanonicalInductionVariable - This method returns the
112982749Sdillon/// canonical induction variable of the specified type for the specified
113082749Sdillon/// loop (inserting one if there is none).  A canonical induction variable
113182749Sdillon/// starts at zero and steps by one on each iteration.
113256115SpeterValue *
113356115SpeterSCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
113483366Sjulian                                                    const Type *Ty) {
113583366Sjulian  assert(Ty->isIntegerTy() && "Can only insert integer induction variables!");
113656115Speter  const SCEV *H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
113756115Speter                                   SE.getIntegerSCEV(1, Ty), L);
113882749Sdillon  BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
113983366Sjulian  BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
114056115Speter  Value *V = expandCodeFor(H, 0, L->getHeader()->begin());
114156115Speter  if (SaveInsertBB)
114282749Sdillon    restoreInsertPoint(SaveInsertBB, SaveInsertPt);
114382749Sdillon  return V;
114482749Sdillon}
114556115Speter