LoopStrengthReduce.cpp revision 5272f3c669c7a2d43dd4796aded314ecc00c66b8
1eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//                     The LLVM Compiler Infrastructure
4eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//
5eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// This file was developed by Nate Begeman and is distributed under the
6eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// University of Illinois Open Source License. See LICENSE.TXT for details.
7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
8eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===----------------------------------------------------------------------===//
9eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//
10eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// This pass performs a strength reduction on array references inside loops that
11eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// have as one or more of their components the loop induction variable.  This is
12eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// accomplished by creating a new Value to hold the initial value of the array
13eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// access for the first iteration, and then creating a new GEP instruction in
14eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// the loop to increment the value by the appropriate amount.
15eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//
16eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===----------------------------------------------------------------------===//
17eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
18be3e5212e23edc9210f774fac18d801de252e906Chris Lattner#define DEBUG_TYPE "loop-reduce"
19eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Transforms/Scalar.h"
20eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Constants.h"
21eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Instructions.h"
22eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Type.h"
232f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen#include "llvm/DerivedTypes.h"
24eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Analysis/Dominators.h"
25eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Analysis/LoopInfo.h"
26169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Analysis/ScalarEvolutionExpander.h"
27eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Support/CFG.h"
28169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Support/GetElementPtrTypeIterator.h"
29eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Transforms/Utils/Local.h"
302f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen#include "llvm/Target/TargetData.h"
31eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/ADT/Statistic.h"
32169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Support/Debug.h"
33cfb1d4235fe3291028341e6acf4139723b4749e3Jeff Cohen#include <algorithm>
34eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include <set>
35eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanusing namespace llvm;
36eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
37eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemannamespace {
38eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced");
3926d91f16464db56283087176a73981048331dd2dChris Lattner  Statistic<> NumInserted("loop-reduce", "Number of PHIs inserted");
40eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
41ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// IVStrideUse - Keep track of one use of a strided induction variable, where
42ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// the stride is stored externally.  The Offset member keeps track of the
43ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// offset from the IV, User is the actual user of the operand, and 'Operand'
44ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// is the operand # of the User that is the use.
45ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  struct IVStrideUse {
46ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    SCEVHandle Offset;
47ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Instruction *User;
48ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
49010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
50010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
51010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
52010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
53010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // instruction for a loop.
54010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
55ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
56ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O)
57010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      : Offset(Offs), User(U), OperandValToReplace(O),
58010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        isUseOfPostIncrementedValue(false) {}
59ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  };
60ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
61ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// IVUsersOfOneStride - This structure keeps track of all instructions that
62ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// have an operand that is based on the trip count multiplied by some stride.
63ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// The stride for all of these users is common and kept external to this
64ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// structure.
65ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  struct IVUsersOfOneStride {
66169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Users - Keep track of all of the users of this stride as well as the
67ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// initial value and the operand that uses the IV.
68ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    std::vector<IVStrideUse> Users;
69ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
70ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) {
71ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner      Users.push_back(IVStrideUse(Offset, User, Operand));
72169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
73169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
74169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
75169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
76eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  class LoopStrengthReduce : public FunctionPass {
77eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    LoopInfo *LI;
78eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    DominatorSet *DS;
79169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    ScalarEvolution *SE;
80169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    const TargetData *TD;
81169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    const Type *UIntPtrTy;
82eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    bool Changed;
837e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner
847e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner    /// MaxTargetAMSize - This is the maximum power-of-two scale value that the
857e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner    /// target can handle for free with its addressing modes.
862f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen    unsigned MaxTargetAMSize;
87169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
88169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// IVUsesByStride - Keep track of all uses of induction variables that we
89169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// are interested in.  The key of the map is the stride of the access.
90ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    std::map<Value*, IVUsersOfOneStride> IVUsesByStride;
91169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
9249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// CastedValues - As we need to cast values to uintptr_t, this keeps track
9349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// of the casted version of each value.  This is accessed by
9449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf.
9549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    std::map<Value*, Value*> CastedPointers;
96169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
97169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// DeadInsts - Keep track of instructions we may have made dead, so that
98169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// we can remove them after we are done working.
99169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    std::set<Instruction*> DeadInsts;
100eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  public:
1012f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen    LoopStrengthReduce(unsigned MTAMS = 1)
1022f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen      : MaxTargetAMSize(MTAMS) {
1032f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen    }
1042f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen
105eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    virtual bool runOnFunction(Function &) {
106eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      LI = &getAnalysis<LoopInfo>();
107eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      DS = &getAnalysis<DominatorSet>();
108169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      SE = &getAnalysis<ScalarEvolution>();
109169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      TD = &getAnalysis<TargetData>();
110169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      UIntPtrTy = TD->getIntPtrType();
111eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      Changed = false;
112eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
113eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
114eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman        runOnLoop(*I);
11549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
116eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      return Changed;
117eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
118eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
119eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
120eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      AU.setPreservesCFG();
121f465db6c6a5a877aa791abfc3837d62c491dacd5Jeff Cohen      AU.addRequiredID(LoopSimplifyID);
122eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      AU.addRequired<LoopInfo>();
123eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      AU.addRequired<DominatorSet>();
1242f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen      AU.addRequired<TargetData>();
125169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      AU.addRequired<ScalarEvolution>();
126eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
12749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
12849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf - Return the specified value casted to uintptr_t.
12949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    ///
13049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    Value *getCastedVersionOf(Value *V);
13149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattnerprivate:
132eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    void runOnLoop(Loop *L);
1333416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    bool AddUsersIfInteresting(Instruction *I, Loop *L,
1343416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                               std::set<Instruction*> &Processed);
1353416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L);
1363416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
137010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    void OptimizeIndvars(Loop *L);
138169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
139ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    void StrengthReduceStridedIVUsers(Value *Stride, IVUsersOfOneStride &Uses,
140ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                      Loop *L, bool isOnlyStride);
141eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
142eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  };
143fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  RegisterOpt<LoopStrengthReduce> X("loop-reduce",
144eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman                                    "Strength Reduce GEP Uses of Ind. Vars");
145eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
146eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1472f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff CohenFunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) {
1482f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen  return new LoopStrengthReduce(MaxTargetAMSize);
149eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
150eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
15149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner/// getCastedVersionOf - Return the specified value casted to uintptr_t.
15249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner///
15349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris LattnerValue *LoopStrengthReduce::getCastedVersionOf(Value *V) {
15449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (V->getType() == UIntPtrTy) return V;
15549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (Constant *CB = dyn_cast<Constant>(V))
15649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    return ConstantExpr::getCast(CB, UIntPtrTy);
15749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
15849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  Value *&New = CastedPointers[V];
15949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (New) return New;
16049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
16149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  BasicBlock::iterator InsertPt;
16249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (Argument *Arg = dyn_cast<Argument>(V)) {
16349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    // Insert into the entry of the function, after any allocas.
16449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    InsertPt = Arg->getParent()->begin()->begin();
16549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    while (isa<AllocaInst>(InsertPt)) ++InsertPt;
16649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  } else {
16749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
16849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      InsertPt = II->getNormalDest()->begin();
16949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    } else {
17049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      InsertPt = cast<Instruction>(V);
17149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      ++InsertPt;
17249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    }
17349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
17449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    // Do not insert casts into the middle of PHI node blocks.
17549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    while (isa<PHINode>(InsertPt)) ++InsertPt;
17649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  }
1777db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
1787db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  New = new CastInst(V, UIntPtrTy, V->getName(), InsertPt);
1797db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  DeadInsts.insert(cast<Instruction>(New));
1807db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return New;
18149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner}
18249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
18349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
184eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// DeleteTriviallyDeadInstructions - If any of the instructions is the
185eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// specified set are trivially dead, delete them and see if this makes any of
186eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// their operands subsequently dead.
187eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::
188eaa13851a7fe604363577350c5cf65c257c4d41aNate BegemanDeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
189eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  while (!Insts.empty()) {
190eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    Instruction *I = *Insts.begin();
191eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    Insts.erase(Insts.begin());
192eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    if (isInstructionTriviallyDead(I)) {
1930456e4a079de51087978c177b1de63343731f4fbJeff Cohen      for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1940456e4a079de51087978c177b1de63343731f4fbJeff Cohen        if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
1950456e4a079de51087978c177b1de63343731f4fbJeff Cohen          Insts.insert(U);
19652d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner      SE->deleteInstructionFromRecords(I);
19752d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner      I->eraseFromParent();
198eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      Changed = true;
199eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
200eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
201eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
202eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
203169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2043416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// GetExpressionSCEV - Compute and return the SCEV for the specified
2053416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// instruction.
2063416e5f645186a7e3321f927eab662d0ecef404bChris LattnerSCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
2073416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp);
2083416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  if (!GEP)
2093416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return SE->getSCEV(Exp);
2103416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
211169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Analyze all of the subscripts of this getelementptr instruction, looking
212169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // for uses that are determined by the trip count of L.  First, skip all
213169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // operands the are not dependent on the IV.
214169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
215169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Build up the base expression.  Insert an LLVM cast of the pointer to
216169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // uintptr_t first.
2173416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0)));
218169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
219169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  gep_type_iterator GTI = gep_type_begin(GEP);
2203416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
2213416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
222169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is a use of a recurrence that we can analyze, and it comes before
223169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Op does in the GEP operand list, we will handle this when we process this
224169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // operand.
225169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
226169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      const StructLayout *SL = TD->getStructLayout(STy);
227169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      unsigned Idx = cast<ConstantUInt>(GEP->getOperand(i))->getValue();
228169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      uint64_t Offset = SL->MemberOffsets[Idx];
2293416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      GEPVal = SCEVAddExpr::get(GEPVal,
2303416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy));
2312f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    } else {
2327db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      Value *OpVal = getCastedVersionOf(GEP->getOperand(i));
2337db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      SCEVHandle Idx = SE->getSCEV(OpVal);
2347db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2353416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType());
2363416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      if (TypeSize != 1)
2373416e5f645186a7e3321f927eab662d0ecef404bChris Lattner        Idx = SCEVMulExpr::get(Idx,
2383416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                               SCEVConstant::get(ConstantUInt::get(UIntPtrTy,
2393416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                                                   TypeSize)));
2403416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      GEPVal = SCEVAddExpr::get(GEPVal, Idx);
2412f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    }
242eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
243169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2443416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  return GEPVal;
245169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
246169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2477db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// getSCEVStartAndStride - Compute the start and stride of this expression,
2487db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// returning false if the expression is not a start/stride pair, or true if it
2497db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// is.  The stride must be a loop invariant expression, but the start may be
2507db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// a mix of loop invariant and loop variant expressions.
2517db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattnerstatic bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
2527db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner                                  SCEVHandle &Start, Value *&Stride) {
2537db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle TheAddRec = Start;   // Initialize to zero.
2547db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2557db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // If the outer level is an AddExpr, the operands are all start values except
2567db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // for a nested AddRecExpr.
2577db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
2587db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
2597db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (SCEVAddRecExpr *AddRec =
2607db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner             dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
2617db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        if (AddRec->getLoop() == L)
2627db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          TheAddRec = SCEVAddExpr::get(AddRec, TheAddRec);
2637db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        else
2647db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          return false;  // Nested IV of some sort?
2657db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      } else {
2667db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Start = SCEVAddExpr::get(Start, AE->getOperand(i));
2677db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      }
2687db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2697db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SH)) {
2707db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    TheAddRec = SH;
2717db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else {
2727db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // not analyzable.
2737db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  }
2747db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2757db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
2767db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec || AddRec->getLoop() != L) return false;
2777db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2787db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // FIXME: Generalize to non-affine IV's.
2797db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec->isAffine()) return false;
2807db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2817db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  Start = SCEVAddExpr::get(Start, AddRec->getOperand(0));
2827db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2837db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // FIXME: generalize to IV's with more complex strides (must emit stride
2847db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // expression outside of loop!)
2857db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!isa<SCEVConstant>(AddRec->getOperand(1)))
2867db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;
2877db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2887db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVConstant *StrideC = cast<SCEVConstant>(AddRec->getOperand(1));
2897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  Stride = StrideC->getValue();
2907db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2917db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  assert(Stride->getType()->isUnsigned() &&
2927db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner         "Constants should be canonicalized to unsigned!");
2937db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return true;
2947db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner}
2957db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
296169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// AddUsersIfInteresting - Inspect the specified instruction.  If it is a
297169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// reducible SCEV, recursively add its users to the IVUsesByStride set and
298169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// return true.  Otherwise, return false.
2993416e5f645186a7e3321f927eab662d0ecef404bChris Lattnerbool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L,
3003416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                            std::set<Instruction*> &Processed) {
301f84d5ab5df5900dcaf90df8d83c16b6cea22f087Nate Begeman  if (I->getType() == Type::VoidTy) return false;
3023416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  if (!Processed.insert(I).second)
3033416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return true;    // Instruction already handled.
3043416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
3057db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the symbolic expression for this instruction.
3063416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  SCEVHandle ISE = GetExpressionSCEV(I, L);
3077db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (isa<SCEVCouldNotCompute>(ISE)) return false;
3087db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3097db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the start and stride for this expression.
3107db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle Start = SCEVUnknown::getIntegerSCEV(0, ISE->getType());
3117db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  Value *Stride = 0;
3127db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!getSCEVStartAndStride(ISE, L, Start, Stride))
3137db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // Non-reducible symbolic expression, bail out.
3143416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
315169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;++UI){
316169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *User = cast<Instruction>(*UI);
317169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
318169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Do not infinitely recurse on PHI nodes.
319169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (isa<PHINode>(User) && User->getParent() == L->getHeader())
320169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      continue;
321169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
322169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is an instruction defined in a nested loop, or outside this loop,
323f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    // don't recurse into it.
3247db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    bool AddUserToIVUsers = false;
325f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    if (LI->getLoopFor(User->getParent()) != L) {
326f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner      DEBUG(std::cerr << "FOUND USER in nested loop: " << *User
327f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner            << "   OF SCEV: " << *ISE << "\n");
3287db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
3293416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    } else if (!AddUsersIfInteresting(User, L, Processed)) {
330a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      DEBUG(std::cerr << "FOUND USER: " << *User
331a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner            << "   OF SCEV: " << *ISE << "\n");
3327db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
3337db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    }
334fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
3357db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    if (AddUserToIVUsers) {
336a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      // Okay, we found a user that we cannot reduce.  Analyze the instruction
337a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      // and decide what to do with it.
3387db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      IVUsesByStride[Stride].addUser(Start, User, I);
339169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
340169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
341169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return true;
342169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
343169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
344169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemannamespace {
345169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// BasedUser - For a particular base value, keep information about how we've
346169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// partitioned the expression so far.
347169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  struct BasedUser {
348169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Inst - The instruction using the induction variable.
349169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *Inst;
350169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
351ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// OperandValToReplace - The operand value of Inst to replace with the
352ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// EmittedBase.
353ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
354169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
355169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Imm - The immediate value that should be added to the base immediately
356169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// before Inst, because it will be folded into the imm field of the
357169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// instruction.
358169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    SCEVHandle Imm;
359169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
360169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// EmittedBase - The actual value* to use for the base value of this
361169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// operation.  This is null if we should just use zero so far.
362169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Value *EmittedBase;
363169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
364010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
365010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
366010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
367010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // instruction for a loop.
368010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
369010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
370010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    BasedUser(Instruction *I, Value *Op, const SCEVHandle &IMM, bool iUOPIV)
371010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      : Inst(I), OperandValToReplace(Op), Imm(IMM), EmittedBase(0),
372010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        isUseOfPostIncrementedValue(iUOPIV) {}
373169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
3742114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Once we rewrite the code to insert the new IVs we want, update the
3752114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // operands of Inst to use the new expression 'NewBase', with 'Imm' added
3762114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // to it.
3772114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    void RewriteInstructionToUseNewBase(Value *NewBase, SCEVExpander &Rewriter);
378169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
379169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // No need to compare these.
380169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    bool operator<(const BasedUser &BU) const { return 0; }
381169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
382169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    void dump() const;
383169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
384169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
385169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
386169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanvoid BasedUser::dump() const {
387169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::cerr << " Imm=" << *Imm;
388169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (EmittedBase)
389169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    std::cerr << "  EB=" << *EmittedBase;
390169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
391169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::cerr << "   Inst: " << *Inst;
392169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
393169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
3942114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// Once we rewrite the code to insert the new IVs we want, update the
3952114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// operands of Inst to use the new expression 'NewBase', with 'Imm' added
3962114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// to it.
3972114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattnervoid BasedUser::RewriteInstructionToUseNewBase(Value *NewBase,
3982114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner                                               SCEVExpander &Rewriter) {
3992114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  if (!isa<PHINode>(Inst)) {
4002114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    SCEVHandle NewValSCEV = SCEVAddExpr::get(SCEVUnknown::get(NewBase), Imm);
4012114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    Value *NewVal = Rewriter.expandCodeFor(NewValSCEV, Inst,
4022114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner                                           OperandValToReplace->getType());
4032114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
4042114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Replace the use of the operand Value with the new Phi we just created.
4052114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
4062114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    DEBUG(std::cerr << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst);
4072114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    return;
4082114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
4092114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
4102114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  // PHI nodes are more complex.  We have to insert one copy of the NewBase+Imm
4112114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  // expression into each operand block that uses it.
4122114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  PHINode *PN = cast<PHINode>(Inst);
4132114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
4142114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    if (PN->getIncomingValue(i) == OperandValToReplace) {
4152114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // FIXME: this should split any critical edges.
4162114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
4172114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // Insert the code into the end of the predecessor block.
4182114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      BasicBlock::iterator InsertPt = PN->getIncomingBlock(i)->getTerminator();
4192114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
4202114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      SCEVHandle NewValSCEV = SCEVAddExpr::get(SCEVUnknown::get(NewBase), Imm);
4212114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      Value *NewVal = Rewriter.expandCodeFor(NewValSCEV, InsertPt,
4222114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner                                             OperandValToReplace->getType());
4232114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
4242114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // Replace the use of the operand Value with the new Phi we just created.
4252114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      PN->setIncomingValue(i, NewVal);
4262114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      Rewriter.clear();
4272114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    }
4282114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
4292114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  DEBUG(std::cerr << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst);
4302114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner}
4312114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
4322114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
433169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// isTargetConstant - Return true if the following can be referenced by the
434169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// immediate field of a target instruction.
435169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanstatic bool isTargetConstant(const SCEVHandle &V) {
436d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
437169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: Look at the target to decide if &GV is a legal constant immediate.
438169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (isa<SCEVConstant>(V)) return true;
439d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
440169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return false;     // ENABLE this for x86
441d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
442169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
443169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
444169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      if (CE->getOpcode() == Instruction::Cast)
445169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman        if (isa<GlobalValue>(CE->getOperand(0)))
446169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman          // FIXME: should check to see that the dest is uintptr_t!
447169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman          return true;
448169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return false;
449169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
450169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
45126d91f16464db56283087176a73981048331dd2dChris Lattner/// MoveImmediateValues - Look at Val, and pull out any additions of constants
452169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// that can fit into the immediate field of instructions in the target.
45326d91f16464db56283087176a73981048331dd2dChris Lattner/// Accumulate these immediate values into the Imm value.
45426d91f16464db56283087176a73981048331dd2dChris Lattnerstatic void MoveImmediateValues(SCEVHandle &Val, SCEVHandle &Imm,
45526d91f16464db56283087176a73981048331dd2dChris Lattner                                bool isAddress, Loop *L) {
4567a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
45726d91f16464db56283087176a73981048331dd2dChris Lattner    std::vector<SCEVHandle> NewOps;
45826d91f16464db56283087176a73981048331dd2dChris Lattner    NewOps.reserve(SAE->getNumOperands());
45926d91f16464db56283087176a73981048331dd2dChris Lattner
46026d91f16464db56283087176a73981048331dd2dChris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
4617db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (isAddress && isTargetConstant(SAE->getOperand(i))) {
4627db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
4637db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      } else if (!SAE->getOperand(i)->isLoopInvariant(L)) {
4647db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // If this is a loop-variant expression, it must stay in the immediate
4657db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // field of the expression.
4667db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
46726d91f16464db56283087176a73981048331dd2dChris Lattner      } else {
46826d91f16464db56283087176a73981048331dd2dChris Lattner        NewOps.push_back(SAE->getOperand(i));
469169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
47026d91f16464db56283087176a73981048331dd2dChris Lattner
47126d91f16464db56283087176a73981048331dd2dChris Lattner    if (NewOps.empty())
47226d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
47326d91f16464db56283087176a73981048331dd2dChris Lattner    else
47426d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVAddExpr::get(NewOps);
47526d91f16464db56283087176a73981048331dd2dChris Lattner    return;
4767a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
4777a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner    // Try to pull immediates out of the start value of nested addrec's.
47826d91f16464db56283087176a73981048331dd2dChris Lattner    SCEVHandle Start = SARE->getStart();
47926d91f16464db56283087176a73981048331dd2dChris Lattner    MoveImmediateValues(Start, Imm, isAddress, L);
48026d91f16464db56283087176a73981048331dd2dChris Lattner
48126d91f16464db56283087176a73981048331dd2dChris Lattner    if (Start != SARE->getStart()) {
48226d91f16464db56283087176a73981048331dd2dChris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
48326d91f16464db56283087176a73981048331dd2dChris Lattner      Ops[0] = Start;
48426d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
48526d91f16464db56283087176a73981048331dd2dChris Lattner    }
48626d91f16464db56283087176a73981048331dd2dChris Lattner    return;
487169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
488169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
48926d91f16464db56283087176a73981048331dd2dChris Lattner  // Loop-variant expressions must stay in the immediate field of the
49026d91f16464db56283087176a73981048331dd2dChris Lattner  // expression.
49126d91f16464db56283087176a73981048331dd2dChris Lattner  if ((isAddress && isTargetConstant(Val)) ||
49226d91f16464db56283087176a73981048331dd2dChris Lattner      !Val->isLoopInvariant(L)) {
49326d91f16464db56283087176a73981048331dd2dChris Lattner    Imm = SCEVAddExpr::get(Imm, Val);
49426d91f16464db56283087176a73981048331dd2dChris Lattner    Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
49526d91f16464db56283087176a73981048331dd2dChris Lattner    return;
4967a2ca56ef3bdda6874bcd4df2910fb5a33999f85Chris Lattner  }
49726d91f16464db56283087176a73981048331dd2dChris Lattner
49826d91f16464db56283087176a73981048331dd2dChris Lattner  // Otherwise, no immediates to move.
499169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
500169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
501169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single
502169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// stride of IV.  All of the users may have different starting values, and this
503169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// may not be the only stride (we know it is if isOnlyStride is true).
504169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanvoid LoopStrengthReduce::StrengthReduceStridedIVUsers(Value *Stride,
505ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                                      IVUsersOfOneStride &Uses,
506ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                                      Loop *L,
507169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman                                                      bool isOnlyStride) {
508169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Transform our list of users and offsets to a bit more complex table.  In
509169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // this new vector, the first entry for each element is the base of the
510169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // strided access, and the second is the BasedUser object for the use.  We
511169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // progressively move information from the first to the second entry, until we
512169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // eventually emit the object.
513169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::vector<std::pair<SCEVHandle, BasedUser> > UsersToProcess;
514169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  UsersToProcess.reserve(Uses.Users.size());
515d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
516d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen  SCEVHandle ZeroBase = SCEVUnknown::getIntegerSCEV(0,
517ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                              Uses.Users[0].Offset->getType());
518169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
519169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i)
520ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    UsersToProcess.push_back(std::make_pair(Uses.Users[i].Offset,
521ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                            BasedUser(Uses.Users[i].User,
522ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                             Uses.Users[i].OperandValToReplace,
523010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner                                                      ZeroBase,
524010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner                                   Uses.Users[i].isUseOfPostIncrementedValue)));
525169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
526169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // First pass, figure out what we can represent in the immediate fields of
527169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // instructions.  If we can represent anything there, move it to the imm
528169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // fields of the BasedUsers.
529169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
5307db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    // Addressing modes can be folded into loads and stores.  Be careful that
5317db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    // the store is through the expression, not of the expression though.
5327db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    bool isAddress = isa<LoadInst>(UsersToProcess[i].second.Inst);
5337db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].second.Inst))
5347db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (SI->getOperand(1) == UsersToProcess[i].second.OperandValToReplace)
5357db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        isAddress = true;
5367db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
53726d91f16464db56283087176a73981048331dd2dChris Lattner    MoveImmediateValues(UsersToProcess[i].first, UsersToProcess[i].second.Imm,
53826d91f16464db56283087176a73981048331dd2dChris Lattner                        isAddress, L);
53926d91f16464db56283087176a73981048331dd2dChris Lattner
54026d91f16464db56283087176a73981048331dd2dChris Lattner    assert(UsersToProcess[i].first->isLoopInvariant(L) &&
54126d91f16464db56283087176a73981048331dd2dChris Lattner           "Base value is not loop invariant!");
5422461dff0700d0e34b9854d96a5cc03921b375525Chris Lattner  }
543fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
544169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  SCEVExpander Rewriter(*SE, *LI);
5455272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner  SCEVExpander PreheaderRewriter(*SE, *LI);
5465272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner
547169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  BasicBlock  *Preheader = L->getLoopPreheader();
548169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  Instruction *PreInsertPt = Preheader->getTerminator();
549169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  Instruction *PhiInsertBefore = L->getHeader()->begin();
550169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
551d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen  assert(isa<PHINode>(PhiInsertBefore) &&
552169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman         "How could this loop have IV's without any phis?");
553169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  PHINode *SomeLoopPHI = cast<PHINode>(PhiInsertBefore);
554169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  assert(SomeLoopPHI->getNumIncomingValues() == 2 &&
555169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman         "This loop isn't canonicalized right");
556169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  BasicBlock *LatchBlock =
557169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman   SomeLoopPHI->getIncomingBlock(SomeLoopPHI->getIncomingBlock(0) == Preheader);
558d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
559be3e5212e23edc9210f774fac18d801de252e906Chris Lattner  DEBUG(std::cerr << "INSERTING IVs of STRIDE " << *Stride << ":\n");
560be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
561169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: This loop needs increasing levels of intelligence.
5622351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner  // STAGE 0: just emit everything as its own base.
563169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // STAGE 1: factor out common vars from bases, and try and push resulting
5642351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner  //          constants into Imm field.  <-- We are here
565169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // STAGE 2: factor out large constants to try and make more constants
566169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  //          acceptable for target loads and stores.
567169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
5682351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner  // Sort by the base value, so that all IVs with identical bases are next to
5692351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner  // each other.
5702351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner  std::sort(UsersToProcess.begin(), UsersToProcess.end());
571169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  while (!UsersToProcess.empty()) {
5722351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    SCEVHandle Base = UsersToProcess.front().first;
573be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
574be3e5212e23edc9210f774fac18d801de252e906Chris Lattner    DEBUG(std::cerr << "  INSERTING PHI with BASE = " << *Base << ":\n");
575be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
576169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Create a new Phi for this base, and stick it in the loop header.
5772351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    const Type *ReplacedTy = Base->getType();
5782351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    PHINode *NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore);
57926d91f16464db56283087176a73981048331dd2dChris Lattner    ++NumInserted;
580169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
581d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen    // Emit the initial base value into the loop preheader, and add it to the
582169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Phi node.
5835272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner    Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt,
5845272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner                                                   ReplacedTy);
585169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    NewPHI->addIncoming(BaseV, Preheader);
586169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
587169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Emit the increment of the base value before the terminator of the loop
588169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // latch block, and add it to the Phi node.
589169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    SCEVHandle Inc = SCEVAddExpr::get(SCEVUnknown::get(NewPHI),
590169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman                                      SCEVUnknown::get(Stride));
591169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
592169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Value *IncV = Rewriter.expandCodeFor(Inc, LatchBlock->getTerminator(),
593169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman                                         ReplacedTy);
594169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    IncV->setName(NewPHI->getName()+".inc");
595169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    NewPHI->addIncoming(IncV, LatchBlock);
596169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
597169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Emit the code to add the immediate offset to the Phi value, just before
5982351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    // the instructions that we identified as using this stride and base.
5992351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    while (!UsersToProcess.empty() && UsersToProcess.front().first == Base) {
6002351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      BasedUser &User = UsersToProcess.front().second;
6012351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
6022351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // Clear the SCEVExpander's expression map so that we are guaranteed
6032351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // to have the code emitted where we expect it.
6042351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      Rewriter.clear();
6052114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
6062114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // Now that we know what we need to do, insert code before User for the
6072114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // immediate and any loop-variant expressions.
608010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Value *NewBase = NewPHI;
609010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
610010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // If this instruction wants to use the post-incremented value, move it
611010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // after the post-inc and use its value instead of the PHI.
612010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (User.isUseOfPostIncrementedValue) {
613010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        NewBase = IncV;
614010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        User.Inst->moveBefore(LatchBlock->getTerminator());
615010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
616010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      User.RewriteInstructionToUseNewBase(NewBase, Rewriter);
6172351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
6182351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // Mark old value we replaced as possibly dead, so that it is elminated
6192351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // if we just replaced the last use of that value.
6202114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      DeadInsts.insert(cast<Instruction>(User.OperandValToReplace));
6212351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
6222351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      UsersToProcess.erase(UsersToProcess.begin());
6232351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      ++NumReduced;
6242351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    }
625169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // TODO: Next, find out which base index is the most common, pull it out.
626169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
627169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
628169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but
629169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // different starting values, into different PHIs.
630eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
631eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
632010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar
633010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// uses in the loop, look to see if we can eliminate some, in favor of using
634010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// common indvars for the different uses.
635010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattnervoid LoopStrengthReduce::OptimizeIndvars(Loop *L) {
636010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // TODO: implement optzns here.
637010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
638010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
639010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
640010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
641010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Finally, get the terminating condition for the loop if possible.  If we
642010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // can, we want to change it to use a post-incremented version of its
643010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // induction variable, to allow coallescing the live ranges for the IV into
644010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // one register value.
645010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin());
646010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
647010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock *LatchBlock =
648010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner   SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader);
649010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator());
650010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!TermBr || TermBr->isUnconditional() ||
651010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      !isa<SetCondInst>(TermBr->getCondition()))
652010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    return;
653010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  SetCondInst *Cond = cast<SetCondInst>(TermBr->getCondition());
654010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
655010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Search IVUsesByStride to find Cond's IVUse if there is one.
656010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  IVStrideUse *CondUse = 0;
657010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  Value *CondStride = 0;
658010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
659010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  for (std::map<Value*, IVUsersOfOneStride>::iterator I =IVUsesByStride.begin(),
660010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner         E = IVUsesByStride.end(); I != E && !CondUse; ++I)
661010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    for (std::vector<IVStrideUse>::iterator UI = I->second.Users.begin(),
662010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner           E = I->second.Users.end(); UI != E; ++UI)
663010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (UI->User == Cond) {
664010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        CondUse = &*UI;
665010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        CondStride = I->first;
666010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // NOTE: we could handle setcc instructions with multiple uses here, but
667010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // InstCombine does it as well for simple uses, it's not clear that it
668010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // occurs enough in real life to handle.
669010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        break;
670010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
671010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!CondUse) return;  // setcc doesn't use the IV.
672010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
673010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // setcc stride is complex, don't mess with users.
674010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!isa<ConstantInt>(CondStride)) return;
675010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
676010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // It's possible for the setcc instruction to be anywhere in the loop, and
677010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // possible for it to have multiple users.  If it is not immediately before
678010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // the latch block branch, move it.
679010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) {
680010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    if (Cond->hasOneUse()) {   // Condition has a single use, just move it.
681010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->moveBefore(TermBr);
682010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    } else {
683010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Otherwise, clone the terminating condition and insert into the loopend.
684010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond = cast<SetCondInst>(Cond->clone());
685010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->setName(L->getHeader()->getName() + ".termcond");
686010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      LatchBlock->getInstList().insert(TermBr, Cond);
687010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
688010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Clone the IVUse, as the old use still exists!
689010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      IVUsesByStride[CondStride].addUser(CondUse->Offset, Cond,
690010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner                                         CondUse->OperandValToReplace);
691010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      CondUse = &IVUsesByStride[CondStride].Users.back();
692010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    }
693010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  }
694010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
695010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // If we get to here, we know that we can transform the setcc instruction to
696010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // use the post-incremented version of the IV, allowing us to coallesce the
697010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // live ranges for the IV correctly.
698010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  CondUse->Offset = SCEV::getMinusSCEV(CondUse->Offset,
699010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner                                       SCEVUnknown::get(CondStride));
700010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  CondUse->isUseOfPostIncrementedValue = true;
701010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner}
702169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
703eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::runOnLoop(Loop *L) {
704eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // First step, transform all loops nesting inside of this loop.
705eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
706eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    runOnLoop(*I);
707eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
708169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Next, find all uses of induction variables in this loop, and catagorize
709169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // them by stride.  Start by finding all of the PHI nodes in the header for
710169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // this loop.  If they are induction variables, inspect their uses.
7113416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  std::set<Instruction*> Processed;   // Don't reprocess instructions.
712169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
7133416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    AddUsersIfInteresting(I, L, Processed);
714169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
715169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // If we have nothing to do, return.
716010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (IVUsesByStride.empty()) return;
717010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
718010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Optimize induction variables.  Some indvar uses can be transformed to use
719010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // strides that will be needed for other purposes.  A common example of this
720010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // is the exit test for the loop, which can often be rewritten to use the
721010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // computation of some other indvar to decide when to terminate the loop.
722010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  OptimizeIndvars(L);
723010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
724169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
725169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: We can widen subreg IV's here for RISC targets.  e.g. instead of
726169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // doing computation in byte values, promote to 32-bit values if safe.
727169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
728169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: Attempt to reuse values across multiple IV's.  In particular, we
729169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be
730169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC.  Need
731169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // to be careful that IV's are all the same type.  Only works for intptr_t
732169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // indvars.
733169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
734169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // If we only have one stride, we can more aggressively eliminate some things.
735169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  bool HasOneStride = IVUsesByStride.size() == 1;
736169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
737ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  for (std::map<Value*, IVUsersOfOneStride>::iterator SI
738ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner        = IVUsesByStride.begin(), E = IVUsesByStride.end(); SI != E; ++SI)
739169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
740eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
741eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // Clean up after ourselves
742eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  if (!DeadInsts.empty()) {
743eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    DeleteTriviallyDeadInstructions(DeadInsts);
744eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
745169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    BasicBlock::iterator I = L->getHeader()->begin();
746169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    PHINode *PN;
747e9100c69cbfbcc9298b663d80ef4ddf31d7bba69Chris Lattner    while ((PN = dyn_cast<PHINode>(I))) {
7481060e09fb207ed778581957671f8803d2df3a581Chris Lattner      ++I;  // Preincrement iterator to avoid invalidating it when deleting PN.
7491060e09fb207ed778581957671f8803d2df3a581Chris Lattner
750169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // At this point, we know that we have killed one or more GEP instructions.
751169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // It is worth checking to see if the cann indvar is also dead, so that we
752169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // can remove it as well.  The requirements for the cann indvar to be
753169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // considered dead are:
754169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 1. the cann indvar has one use
755169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 2. the use is an add instruction
756169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 3. the add has one use
757169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 4. the add is used by the cann indvar
758169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // If all four cases above are true, then we can remove both the add and
759169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // the cann indvar.
760169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // FIXME: this needs to eliminate an induction variable even if it's being
761169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // compared against some value to decide loop termination.
762169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      if (PN->hasOneUse()) {
763169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman        BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin()));
7647e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        if (BO && BO->hasOneUse()) {
7657e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner          if (PN == *(BO->use_begin())) {
7667e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            DeadInsts.insert(BO);
7677e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            // Break the cycle, then delete the PHI.
7687e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
76952d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner            SE->deleteInstructionFromRecords(PN);
7707e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            PN->eraseFromParent();
771eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman          }
7727e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        }
773169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
774eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
775169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    DeleteTriviallyDeadInstructions(DeadInsts);
776eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
777169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
7789a59fbb89606aaefb27f6fe07dcb7c188bf1b3cdChris Lattner  CastedPointers.clear();
779169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  IVUsesByStride.clear();
780169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return;
781eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
782