LoopStrengthReduce.cpp revision b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016f
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"
29e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h"
30eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Transforms/Utils/Local.h"
312f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen#include "llvm/Target/TargetData.h"
32eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/ADT/Statistic.h"
33169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Support/Debug.h"
34cfb1d4235fe3291028341e6acf4139723b4749e3Jeff Cohen#include <algorithm>
35eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include <set>
36eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanusing namespace llvm;
37eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
38eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemannamespace {
39eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced");
4026d91f16464db56283087176a73981048331dd2dChris Lattner  Statistic<> NumInserted("loop-reduce", "Number of PHIs inserted");
4150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  Statistic<> NumVariable("loop-reduce","Number of PHIs with variable strides");
42eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
43ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// IVStrideUse - Keep track of one use of a strided induction variable, where
44ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// the stride is stored externally.  The Offset member keeps track of the
45ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// offset from the IV, User is the actual user of the operand, and 'Operand'
46ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// is the operand # of the User that is the use.
47ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  struct IVStrideUse {
48ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    SCEVHandle Offset;
49ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Instruction *User;
50ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
51010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
52010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
53010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
54010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
55c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // instruction for a loop or uses dominated by the loop.
56010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
57ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
58ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O)
59010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      : Offset(Offs), User(U), OperandValToReplace(O),
60010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        isUseOfPostIncrementedValue(false) {}
61ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  };
62ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
63ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// IVUsersOfOneStride - This structure keeps track of all instructions that
64ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// have an operand that is based on the trip count multiplied by some stride.
65ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// The stride for all of these users is common and kept external to this
66ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// structure.
67ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  struct IVUsersOfOneStride {
68169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Users - Keep track of all of the users of this stride as well as the
69ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// initial value and the operand that uses the IV.
70ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    std::vector<IVStrideUse> Users;
71ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
72ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) {
73ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner      Users.push_back(IVStrideUse(Offset, User, Operand));
74169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
75169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
76169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
77169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
78eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  class LoopStrengthReduce : public FunctionPass {
79eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    LoopInfo *LI;
80eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    DominatorSet *DS;
81169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    ScalarEvolution *SE;
82169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    const TargetData *TD;
83169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    const Type *UIntPtrTy;
84eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    bool Changed;
857e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner
867e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner    /// MaxTargetAMSize - This is the maximum power-of-two scale value that the
877e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner    /// target can handle for free with its addressing modes.
882f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen    unsigned MaxTargetAMSize;
89169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
90169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// IVUsesByStride - Keep track of all uses of induction variables that we
91169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// are interested in.  The key of the map is the stride of the access.
9250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    std::map<SCEVHandle, IVUsersOfOneStride> IVUsesByStride;
93169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
947305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// StrideOrder - An ordering of the keys in IVUsesByStride that is stable:
957305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// We use this to iterate over the IVUsesByStride collection without being
967305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// dependent on random ordering of pointers in the process.
977305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    std::vector<SCEVHandle> StrideOrder;
987305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
9949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// CastedValues - As we need to cast values to uintptr_t, this keeps track
10049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// of the casted version of each value.  This is accessed by
10149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf.
10249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    std::map<Value*, Value*> CastedPointers;
103169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
104169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// DeadInsts - Keep track of instructions we may have made dead, so that
105169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// we can remove them after we are done working.
106169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    std::set<Instruction*> DeadInsts;
107eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  public:
1082f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen    LoopStrengthReduce(unsigned MTAMS = 1)
1092f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen      : MaxTargetAMSize(MTAMS) {
1102f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen    }
1112f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen
112eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    virtual bool runOnFunction(Function &) {
113eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      LI = &getAnalysis<LoopInfo>();
114eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      DS = &getAnalysis<DominatorSet>();
115169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      SE = &getAnalysis<ScalarEvolution>();
116169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      TD = &getAnalysis<TargetData>();
117169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      UIntPtrTy = TD->getIntPtrType();
118eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      Changed = false;
119eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
120eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
121eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman        runOnLoop(*I);
12249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
123eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      return Changed;
124eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
125eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
126eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
127aa96ae780afa5475e62df284855a971216289212Chris Lattner      // We split critical edges, so we change the CFG.  However, we do update
128aa96ae780afa5475e62df284855a971216289212Chris Lattner      // many analyses if they are around.
129aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreservedID(LoopSimplifyID);
130aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<LoopInfo>();
131aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominatorSet>();
132aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<ImmediateDominators>();
133aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominanceFrontier>();
134aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominatorTree>();
135aa96ae780afa5475e62df284855a971216289212Chris Lattner
136f465db6c6a5a877aa791abfc3837d62c491dacd5Jeff Cohen      AU.addRequiredID(LoopSimplifyID);
137eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      AU.addRequired<LoopInfo>();
138eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      AU.addRequired<DominatorSet>();
1392f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen      AU.addRequired<TargetData>();
140169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      AU.addRequired<ScalarEvolution>();
141eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
14249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
14349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf - Return the specified value casted to uintptr_t.
14449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    ///
14549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    Value *getCastedVersionOf(Value *V);
14649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattnerprivate:
147eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    void runOnLoop(Loop *L);
1483416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    bool AddUsersIfInteresting(Instruction *I, Loop *L,
1493416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                               std::set<Instruction*> &Processed);
1503416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L);
1513416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
152010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    void OptimizeIndvars(Loop *L);
153169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
15450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    void StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
15550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                      IVUsersOfOneStride &Uses,
156ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                      Loop *L, bool isOnlyStride);
157eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
158eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  };
159fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  RegisterOpt<LoopStrengthReduce> X("loop-reduce",
160fe15830f962bb7fef046203a77d438d087772b34Chris Lattner                                    "Loop Strength Reduction");
161eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
162eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1632f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff CohenFunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) {
1642f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen  return new LoopStrengthReduce(MaxTargetAMSize);
165eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
166eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
16749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner/// getCastedVersionOf - Return the specified value casted to uintptr_t.
16849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner///
16949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris LattnerValue *LoopStrengthReduce::getCastedVersionOf(Value *V) {
17049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (V->getType() == UIntPtrTy) return V;
17149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (Constant *CB = dyn_cast<Constant>(V))
17249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    return ConstantExpr::getCast(CB, UIntPtrTy);
17349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
17449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  Value *&New = CastedPointers[V];
17549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (New) return New;
17649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
17749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  BasicBlock::iterator InsertPt;
17849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (Argument *Arg = dyn_cast<Argument>(V)) {
17949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    // Insert into the entry of the function, after any allocas.
18049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    InsertPt = Arg->getParent()->begin()->begin();
18149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    while (isa<AllocaInst>(InsertPt)) ++InsertPt;
18249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  } else {
18349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
18449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      InsertPt = II->getNormalDest()->begin();
18549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    } else {
18649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      InsertPt = cast<Instruction>(V);
18749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      ++InsertPt;
18849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    }
18949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
19049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    // Do not insert casts into the middle of PHI node blocks.
19149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    while (isa<PHINode>(InsertPt)) ++InsertPt;
19249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  }
1937db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
1947db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  New = new CastInst(V, UIntPtrTy, V->getName(), InsertPt);
1957db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  DeadInsts.insert(cast<Instruction>(New));
1967db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return New;
19749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner}
19849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
19949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
200eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// DeleteTriviallyDeadInstructions - If any of the instructions is the
201eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// specified set are trivially dead, delete them and see if this makes any of
202eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// their operands subsequently dead.
203eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::
204eaa13851a7fe604363577350c5cf65c257c4d41aNate BegemanDeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
205eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  while (!Insts.empty()) {
206eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    Instruction *I = *Insts.begin();
207eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    Insts.erase(Insts.begin());
208eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    if (isInstructionTriviallyDead(I)) {
2090456e4a079de51087978c177b1de63343731f4fbJeff Cohen      for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
2100456e4a079de51087978c177b1de63343731f4fbJeff Cohen        if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
2110456e4a079de51087978c177b1de63343731f4fbJeff Cohen          Insts.insert(U);
21252d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner      SE->deleteInstructionFromRecords(I);
21352d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner      I->eraseFromParent();
214eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      Changed = true;
215eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
216eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
217eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
218eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
219169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2203416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// GetExpressionSCEV - Compute and return the SCEV for the specified
2213416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// instruction.
2223416e5f645186a7e3321f927eab662d0ecef404bChris LattnerSCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
22387265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions.
22487265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // If this is a GEP that SE doesn't know about, compute it now and insert it.
22587265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // If this is not a GEP, or if we have already done this computation, just let
22687265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // SE figure it out.
2273416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp);
22887265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  if (!GEP || SE->hasSCEV(GEP))
2293416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return SE->getSCEV(Exp);
2303416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
231169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Analyze all of the subscripts of this getelementptr instruction, looking
232169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // for uses that are determined by the trip count of L.  First, skip all
233169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // operands the are not dependent on the IV.
234169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
235169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Build up the base expression.  Insert an LLVM cast of the pointer to
236169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // uintptr_t first.
2373416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0)));
238169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
239169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  gep_type_iterator GTI = gep_type_begin(GEP);
2403416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
2413416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
242169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is a use of a recurrence that we can analyze, and it comes before
243169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Op does in the GEP operand list, we will handle this when we process this
244169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // operand.
245169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
246169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      const StructLayout *SL = TD->getStructLayout(STy);
247169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      unsigned Idx = cast<ConstantUInt>(GEP->getOperand(i))->getValue();
248169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      uint64_t Offset = SL->MemberOffsets[Idx];
2493416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      GEPVal = SCEVAddExpr::get(GEPVal,
2503416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy));
2512f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    } else {
2527db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      Value *OpVal = getCastedVersionOf(GEP->getOperand(i));
2537db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      SCEVHandle Idx = SE->getSCEV(OpVal);
2547db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2553416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType());
2563416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      if (TypeSize != 1)
2573416e5f645186a7e3321f927eab662d0ecef404bChris Lattner        Idx = SCEVMulExpr::get(Idx,
2583416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                               SCEVConstant::get(ConstantUInt::get(UIntPtrTy,
2593416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                                                   TypeSize)));
2603416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      GEPVal = SCEVAddExpr::get(GEPVal, Idx);
2612f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    }
262eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
263169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
26487265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  SE->setSCEV(GEP, GEPVal);
2653416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  return GEPVal;
266169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
267169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2687db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// getSCEVStartAndStride - Compute the start and stride of this expression,
2697db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// returning false if the expression is not a start/stride pair, or true if it
2707db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// is.  The stride must be a loop invariant expression, but the start may be
2717db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// a mix of loop invariant and loop variant expressions.
2727db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattnerstatic bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
27350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                  SCEVHandle &Start, SCEVHandle &Stride) {
2747db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle TheAddRec = Start;   // Initialize to zero.
2757db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2767db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // If the outer level is an AddExpr, the operands are all start values except
2777db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // for a nested AddRecExpr.
2787db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
2797db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
2807db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (SCEVAddRecExpr *AddRec =
2817db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner             dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
2827db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        if (AddRec->getLoop() == L)
2837db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          TheAddRec = SCEVAddExpr::get(AddRec, TheAddRec);
2847db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        else
2857db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          return false;  // Nested IV of some sort?
2867db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      } else {
2877db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Start = SCEVAddExpr::get(Start, AE->getOperand(i));
2887db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      }
2897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2907db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SH)) {
2917db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    TheAddRec = SH;
2927db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else {
2937db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // not analyzable.
2947db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  }
2957db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2967db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
2977db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec || AddRec->getLoop() != L) return false;
2987db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2997db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // FIXME: Generalize to non-affine IV's.
3007db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec->isAffine()) return false;
3017db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3027db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  Start = SCEVAddExpr::get(Start, AddRec->getOperand(0));
3037db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3047db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!isa<SCEVConstant>(AddRec->getOperand(1)))
30550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    DEBUG(std::cerr << "[" << L->getHeader()->getName()
30650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                    << "] Variable stride: " << *AddRec << "\n");
30750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
30850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  Stride = AddRec->getOperand(1);
30950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // Check that all constant strides are the unsigned type, we don't want to
31050fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // have two IV's one of signed stride 4 and one of unsigned stride 4 to not be
31150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // merged.
31250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  assert((!isa<SCEVConstant>(Stride) || Stride->getType()->isUnsigned()) &&
3137db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner         "Constants should be canonicalized to unsigned!");
31450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
3157db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return true;
3167db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner}
3177db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3180ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression
3190ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// and now we need to decide whether the user should use the preinc or post-inc
3200ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// value.  If this user should use the post-inc version of the IV, return true.
3210ae33eb243417982fbdca792460bdd986108ac09Chris Lattner///
3220ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// Choosing wrong here can break dominance properties (if we choose to use the
3230ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// post-inc value when we cannot) or it can end up adding extra live-ranges to
3240ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we
3250ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// should use the post-inc value).
3260ae33eb243417982fbdca792460bdd986108ac09Chris Lattnerstatic bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV,
3275e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner                                       Loop *L, DominatorSet *DS, Pass *P) {
3280ae33eb243417982fbdca792460bdd986108ac09Chris Lattner  // If the user is in the loop, use the preinc value.
3290ae33eb243417982fbdca792460bdd986108ac09Chris Lattner  if (L->contains(User->getParent())) return false;
3300ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3315e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  BasicBlock *LatchBlock = L->getLoopLatch();
3325e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3335e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Ok, the user is outside of the loop.  If it is dominated by the latch
3345e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // block, use the post-inc value.
3355e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  if (DS->dominates(LatchBlock, User->getParent()))
3365e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    return true;
3375e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3385e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // There is one case we have to be careful of: PHI nodes.  These little guys
3395e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // can live in blocks that do not dominate the latch block, but (since their
3405e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // uses occur in the predecessor block, not the block the PHI lives in) should
3415e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // still use the post-inc value.  Check for this case now.
3425e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  PHINode *PN = dyn_cast<PHINode>(User);
3435e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  if (!PN) return false;  // not a phi, not dominated by latch block.
3445e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3455e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Look at all of the uses of IV by the PHI node.  If any use corresponds to
3465e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // a block that is not dominated by the latch block, give up and use the
3475e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // preincremented value.
3485e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  unsigned NumUses = 0;
3495e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
3505e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    if (PN->getIncomingValue(i) == IV) {
3515e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      ++NumUses;
3525e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      if (!DS->dominates(LatchBlock, PN->getIncomingBlock(i)))
3535e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner        return false;
3545e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    }
3555e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3565e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Okay, all uses of IV by PN are in predecessor blocks that really are
3575e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // dominated by the latch block.  Split the critical edges and use the
3585e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // post-incremented value.
3595e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
3605e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    if (PN->getIncomingValue(i) == IV) {
3615e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      SplitCriticalEdge(PN->getIncomingBlock(i), PN->getParent(), P);
3625e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      if (--NumUses == 0) break;
3635e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    }
3645e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3655e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  return true;
3660ae33eb243417982fbdca792460bdd986108ac09Chris Lattner}
3670ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3680ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3690ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
370169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// AddUsersIfInteresting - Inspect the specified instruction.  If it is a
371169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// reducible SCEV, recursively add its users to the IVUsesByStride set and
372169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// return true.  Otherwise, return false.
3733416e5f645186a7e3321f927eab662d0ecef404bChris Lattnerbool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L,
3743416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                            std::set<Instruction*> &Processed) {
375f84d5ab5df5900dcaf90df8d83c16b6cea22f087Nate Begeman  if (I->getType() == Type::VoidTy) return false;
3763416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  if (!Processed.insert(I).second)
3773416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return true;    // Instruction already handled.
3783416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
3797db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the symbolic expression for this instruction.
3803416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  SCEVHandle ISE = GetExpressionSCEV(I, L);
3817db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (isa<SCEVCouldNotCompute>(ISE)) return false;
3827db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3837db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the start and stride for this expression.
3847db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle Start = SCEVUnknown::getIntegerSCEV(0, ISE->getType());
38550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  SCEVHandle Stride = Start;
3867db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!getSCEVStartAndStride(ISE, L, Start, Stride))
3877db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // Non-reducible symbolic expression, bail out.
3883416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
389169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;++UI){
390169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *User = cast<Instruction>(*UI);
391169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
392169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Do not infinitely recurse on PHI nodes.
393396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner    if (isa<PHINode>(User) && Processed.count(User))
394169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      continue;
395169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
396169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is an instruction defined in a nested loop, or outside this loop,
397f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    // don't recurse into it.
3987db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    bool AddUserToIVUsers = false;
399f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    if (LI->getLoopFor(User->getParent()) != L) {
400396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      DEBUG(std::cerr << "FOUND USER in other loop: " << *User
401f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner            << "   OF SCEV: " << *ISE << "\n");
4027db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
4033416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    } else if (!AddUsersIfInteresting(User, L, Processed)) {
404a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      DEBUG(std::cerr << "FOUND USER: " << *User
405a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner            << "   OF SCEV: " << *ISE << "\n");
4067db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
4077db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    }
408fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4097db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    if (AddUserToIVUsers) {
4107305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      IVUsersOfOneStride &StrideUses = IVUsesByStride[Stride];
4117305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      if (StrideUses.Users.empty())     // First occurance of this stride?
4127305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideOrder.push_back(Stride);
4137305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
414a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      // Okay, we found a user that we cannot reduce.  Analyze the instruction
415c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      // and decide what to do with it.  If we are a use inside of the loop, use
416c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      // the value before incrementation, otherwise use it after incrementation.
4175e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      if (IVUseShouldUsePostIncValue(User, I, L, DS, this)) {
418c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // The value used will be incremented by the stride more than we are
419c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // expecting, so subtract this off.
420c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        SCEVHandle NewStart = SCEV::getMinusSCEV(Start, Stride);
4217305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.addUser(NewStart, User, I);
4227305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.Users.back().isUseOfPostIncrementedValue = true;
4235e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner        DEBUG(std::cerr << "   USING POSTINC SCEV, START=" << *NewStart<< "\n");
4240ae33eb243417982fbdca792460bdd986108ac09Chris Lattner      } else {
4257305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.addUser(Start, User, I);
426c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      }
427169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
428169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
429169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return true;
430169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
431169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
432169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemannamespace {
433169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// BasedUser - For a particular base value, keep information about how we've
434169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// partitioned the expression so far.
435169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  struct BasedUser {
436a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// Base - The Base value for the PHI node that needs to be inserted for
437a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// this use.  As the use is processed, information gets moved from this
438a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// field to the Imm field (below).  BasedUser values are sorted by this
439a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// field.
440a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    SCEVHandle Base;
441a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
442169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Inst - The instruction using the induction variable.
443169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *Inst;
444169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
445ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// OperandValToReplace - The operand value of Inst to replace with the
446ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// EmittedBase.
447ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
448169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
449169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Imm - The immediate value that should be added to the base immediately
450169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// before Inst, because it will be folded into the imm field of the
451169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// instruction.
452169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    SCEVHandle Imm;
453169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
454169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// EmittedBase - The actual value* to use for the base value of this
455169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// operation.  This is null if we should just use zero so far.
456169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Value *EmittedBase;
457169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
458010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
459010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
460010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
461c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // instruction for a loop and uses outside the loop that are dominated by
462c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // the loop.
463010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
464a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
465a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    BasedUser(IVStrideUse &IVSU)
466a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner      : Base(IVSU.Offset), Inst(IVSU.User),
467a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        OperandValToReplace(IVSU.OperandValToReplace),
468a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        Imm(SCEVUnknown::getIntegerSCEV(0, Base->getType())), EmittedBase(0),
469a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {}
470169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
4712114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Once we rewrite the code to insert the new IVs we want, update the
4722114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // operands of Inst to use the new expression 'NewBase', with 'Imm' added
4732114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // to it.
4741bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
475c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                        SCEVExpander &Rewriter, Loop *L,
476c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                        Pass *P);
477169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
478a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // Sort by the Base field.
479a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    bool operator<(const BasedUser &BU) const { return Base < BU.Base; }
480169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
481169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    void dump() const;
482169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
483169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
484169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
485169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanvoid BasedUser::dump() const {
486a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  std::cerr << " Base=" << *Base;
487169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::cerr << " Imm=" << *Imm;
488169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (EmittedBase)
489169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    std::cerr << "  EB=" << *EmittedBase;
490169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
491169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::cerr << "   Inst: " << *Inst;
492169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
493169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
4942114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// Once we rewrite the code to insert the new IVs we want, update the
4952114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// operands of Inst to use the new expression 'NewBase', with 'Imm' added
4962114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// to it.
4971bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnervoid BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
498e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner                                               SCEVExpander &Rewriter,
499c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                               Loop *L, Pass *P) {
5002114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  if (!isa<PHINode>(Inst)) {
5011bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    SCEVHandle NewValSCEV = SCEVAddExpr::get(NewBase, Imm);
5022114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    Value *NewVal = Rewriter.expandCodeFor(NewValSCEV, Inst,
5032114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner                                           OperandValToReplace->getType());
5042114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Replace the use of the operand Value with the new Phi we just created.
5052114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
5062114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    DEBUG(std::cerr << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst);
5072114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    return;
5082114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
5092114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
5102114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  // PHI nodes are more complex.  We have to insert one copy of the NewBase+Imm
511c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // expression into each operand block that uses it.  Note that PHI nodes can
512c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // have multiple entries for the same predecessor.  We use a map to make sure
513c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // that a PHI node only has a single Value* for each predecessor (which also
514c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // prevents us from inserting duplicate code in some blocks).
515c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  std::map<BasicBlock*, Value*> InsertedCode;
5162114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  PHINode *PN = cast<PHINode>(Inst);
5172114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5182114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    if (PN->getIncomingValue(i) == OperandValToReplace) {
519e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner      // If this is a critical edge, split the edge so that we do not insert the
520396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // code on all predecessor/successor paths.  We do this unless this is the
521396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // canonical backedge for this loop, as this can make some inserted code
522396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // be in an illegal position.
52337edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner      BasicBlock *PHIPred = PN->getIncomingBlock(i);
52437edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner      if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 &&
52537edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner          (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) {
52637edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner
527aa96ae780afa5475e62df284855a971216289212Chris Lattner        // First step, split the critical edge.
52837edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner        SplitCriticalEdge(PHIPred, PN->getParent(), P);
529c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner
530aa96ae780afa5475e62df284855a971216289212Chris Lattner        // Next step: move the basic block.  In particular, if the PHI node
531aa96ae780afa5475e62df284855a971216289212Chris Lattner        // is outside of the loop, and PredTI is in the loop, we want to
532aa96ae780afa5475e62df284855a971216289212Chris Lattner        // move the block to be immediately before the PHI block, not
533aa96ae780afa5475e62df284855a971216289212Chris Lattner        // immediately after PredTI.
53437edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner        if (L->contains(PHIPred) && !L->contains(PN->getParent())) {
535aa96ae780afa5475e62df284855a971216289212Chris Lattner          BasicBlock *NewBB = PN->getIncomingBlock(i);
536aa96ae780afa5475e62df284855a971216289212Chris Lattner          NewBB->moveBefore(PN->getParent());
537e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner        }
538e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner      }
5392114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
540c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      Value *&Code = InsertedCode[PN->getIncomingBlock(i)];
541c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      if (!Code) {
542c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        // Insert the code into the end of the predecessor block.
543c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        BasicBlock::iterator InsertPt =PN->getIncomingBlock(i)->getTerminator();
5442114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
545c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        SCEVHandle NewValSCEV = SCEVAddExpr::get(NewBase, Imm);
546c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        Code = Rewriter.expandCodeFor(NewValSCEV, InsertPt,
547c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner                                      OperandValToReplace->getType());
548c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      }
5492114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
5502114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // Replace the use of the operand Value with the new Phi we just created.
551c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      PN->setIncomingValue(i, Code);
5522114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      Rewriter.clear();
5532114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    }
5542114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
5552114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  DEBUG(std::cerr << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst);
5562114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner}
5572114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
5582114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
559169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// isTargetConstant - Return true if the following can be referenced by the
560169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// immediate field of a target instruction.
561169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanstatic bool isTargetConstant(const SCEVHandle &V) {
562d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
563169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: Look at the target to decide if &GV is a legal constant immediate.
5643821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
5653821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner    // PPC allows a sign-extended 16-bit immediate field.
5663821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner    if ((int64_t)SC->getValue()->getRawValue() > -(1 << 16) &&
5673821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner        (int64_t)SC->getValue()->getRawValue() < (1 << 16)-1)
5683821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner      return true;
5693821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner    return false;
5703821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner  }
571d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
572169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return false;     // ENABLE this for x86
573d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
574169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
575169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
576169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      if (CE->getOpcode() == Instruction::Cast)
577169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman        if (isa<GlobalValue>(CE->getOperand(0)))
578169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman          // FIXME: should check to see that the dest is uintptr_t!
579169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman          return true;
580169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return false;
581169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
582169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
58344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// MoveLoopVariantsToImediateField - Move any subexpressions from Val that are
58444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// loop varying to the Imm operand.
58544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattnerstatic void MoveLoopVariantsToImediateField(SCEVHandle &Val, SCEVHandle &Imm,
58644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner                                            Loop *L) {
58744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  if (Val->isLoopInvariant(L)) return;  // Nothing to do.
58844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
58944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
59044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    std::vector<SCEVHandle> NewOps;
59144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    NewOps.reserve(SAE->getNumOperands());
59244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
59344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
59444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      if (!SAE->getOperand(i)->isLoopInvariant(L)) {
59544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        // If this is a loop-variant expression, it must stay in the immediate
59644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        // field of the expression.
59744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
59844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      } else {
59944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        NewOps.push_back(SAE->getOperand(i));
60044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      }
60144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
60244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    if (NewOps.empty())
60344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
60444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    else
60544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      Val = SCEVAddExpr::get(NewOps);
60644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
60744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    // Try to pull immediates out of the start value of nested addrec's.
60844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    SCEVHandle Start = SARE->getStart();
60944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    MoveLoopVariantsToImediateField(Start, Imm, L);
61044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
61144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
61244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Ops[0] = Start;
61344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
61444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  } else {
61544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    // Otherwise, all of Val is variant, move the whole thing over.
61644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Imm = SCEVAddExpr::get(Imm, Val);
61744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
61844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  }
61944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner}
62044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
62144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
62226d91f16464db56283087176a73981048331dd2dChris Lattner/// MoveImmediateValues - Look at Val, and pull out any additions of constants
623169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// that can fit into the immediate field of instructions in the target.
62426d91f16464db56283087176a73981048331dd2dChris Lattner/// Accumulate these immediate values into the Imm value.
62526d91f16464db56283087176a73981048331dd2dChris Lattnerstatic void MoveImmediateValues(SCEVHandle &Val, SCEVHandle &Imm,
62626d91f16464db56283087176a73981048331dd2dChris Lattner                                bool isAddress, Loop *L) {
6277a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
62826d91f16464db56283087176a73981048331dd2dChris Lattner    std::vector<SCEVHandle> NewOps;
62926d91f16464db56283087176a73981048331dd2dChris Lattner    NewOps.reserve(SAE->getNumOperands());
63026d91f16464db56283087176a73981048331dd2dChris Lattner
63126d91f16464db56283087176a73981048331dd2dChris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
6327db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (isAddress && isTargetConstant(SAE->getOperand(i))) {
6337db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
6347db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      } else if (!SAE->getOperand(i)->isLoopInvariant(L)) {
6357db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // If this is a loop-variant expression, it must stay in the immediate
6367db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // field of the expression.
6377db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
63826d91f16464db56283087176a73981048331dd2dChris Lattner      } else {
63926d91f16464db56283087176a73981048331dd2dChris Lattner        NewOps.push_back(SAE->getOperand(i));
640169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
64126d91f16464db56283087176a73981048331dd2dChris Lattner
64226d91f16464db56283087176a73981048331dd2dChris Lattner    if (NewOps.empty())
64326d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
64426d91f16464db56283087176a73981048331dd2dChris Lattner    else
64526d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVAddExpr::get(NewOps);
64626d91f16464db56283087176a73981048331dd2dChris Lattner    return;
6477a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
6487a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner    // Try to pull immediates out of the start value of nested addrec's.
64926d91f16464db56283087176a73981048331dd2dChris Lattner    SCEVHandle Start = SARE->getStart();
65026d91f16464db56283087176a73981048331dd2dChris Lattner    MoveImmediateValues(Start, Imm, isAddress, L);
65126d91f16464db56283087176a73981048331dd2dChris Lattner
65226d91f16464db56283087176a73981048331dd2dChris Lattner    if (Start != SARE->getStart()) {
65326d91f16464db56283087176a73981048331dd2dChris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
65426d91f16464db56283087176a73981048331dd2dChris Lattner      Ops[0] = Start;
65526d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
65626d91f16464db56283087176a73981048331dd2dChris Lattner    }
65726d91f16464db56283087176a73981048331dd2dChris Lattner    return;
658169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
659169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
66026d91f16464db56283087176a73981048331dd2dChris Lattner  // Loop-variant expressions must stay in the immediate field of the
66126d91f16464db56283087176a73981048331dd2dChris Lattner  // expression.
66226d91f16464db56283087176a73981048331dd2dChris Lattner  if ((isAddress && isTargetConstant(Val)) ||
66326d91f16464db56283087176a73981048331dd2dChris Lattner      !Val->isLoopInvariant(L)) {
66426d91f16464db56283087176a73981048331dd2dChris Lattner    Imm = SCEVAddExpr::get(Imm, Val);
66526d91f16464db56283087176a73981048331dd2dChris Lattner    Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
66626d91f16464db56283087176a73981048331dd2dChris Lattner    return;
6677a2ca56ef3bdda6874bcd4df2910fb5a33999f85Chris Lattner  }
66826d91f16464db56283087176a73981048331dd2dChris Lattner
66926d91f16464db56283087176a73981048331dd2dChris Lattner  // Otherwise, no immediates to move.
670169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
671169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
672934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
673934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// IncrementAddExprUses - Decompose the specified expression into its added
674934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// subexpressions, and increment SubExpressionUseCounts for each of these
675934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// decomposed parts.
676934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattnerstatic void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs,
677934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner                             SCEVHandle Expr) {
678934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) {
679934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j)
680934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SeparateSubExprs(SubExprs, AE->getOperand(j));
681934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) {
682934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Expr->getType());
683934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (SARE->getOperand(0) == Zero) {
684934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SubExprs.push_back(Expr);
685934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    } else {
686934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      // Compute the addrec with zero as its base.
687934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
688934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      Ops[0] = Zero;   // Start with zero base.
689934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SubExprs.push_back(SCEVAddRecExpr::get(Ops, SARE->getLoop()));
690934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
691934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
692934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SeparateSubExprs(SubExprs, SARE->getOperand(0));
693934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    }
694934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  } else if (!isa<SCEVConstant>(Expr) ||
695934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner             !cast<SCEVConstant>(Expr)->getValue()->isNullValue()) {
696934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Do not add zero.
697934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SubExprs.push_back(Expr);
698934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
699934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner}
700934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
701934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
7021bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// RemoveCommonExpressionsFromUseBases - Look through all of the uses in Bases,
7031bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// removing any common subexpressions from it.  Anything truly common is
7041bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// removed, accumulated, and returned.  This looks for things like (a+b+c) and
7051bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// (a+c+d) -> (a+c).  The common expression is *removed* from the Bases.
7061bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnerstatic SCEVHandle
7071bbae0cbf212d0356f564d12e8f728be455bdc47Chris LattnerRemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses) {
7081bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  unsigned NumUses = Uses.size();
7091bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7101bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Only one use?  Use its base, regardless of what it is!
7111bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Uses[0].Base->getType());
7121bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle Result = Zero;
7131bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  if (NumUses == 1) {
7141bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    std::swap(Result, Uses[0].Base);
7151bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    return Result;
7161bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  }
7171bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7181bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // To find common subexpressions, count how many of Uses use each expression.
7191bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // If any subexpressions are used Uses.size() times, they are common.
7201bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  std::map<SCEVHandle, unsigned> SubExpressionUseCounts;
7211bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
722934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  std::vector<SCEVHandle> SubExprs;
723934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  for (unsigned i = 0; i != NumUses; ++i) {
724934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // If the base is zero (which is common), return zero now, there are no
725934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // CSEs we can find.
726934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (Uses[i].Base == Zero) return Zero;
727934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
728934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Split the expression into subexprs.
729934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SeparateSubExprs(SubExprs, Uses[i].Base);
730934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Add one to SubExpressionUseCounts for each subexpr present.
731934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
732934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SubExpressionUseCounts[SubExprs[j]]++;
733934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SubExprs.clear();
734934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
735934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
736934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
7371bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Now that we know how many times each is used, build Result.
7381bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  for (std::map<SCEVHandle, unsigned>::iterator I =
7391bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner       SubExpressionUseCounts.begin(), E = SubExpressionUseCounts.end();
7401bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner       I != E; )
7411bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    if (I->second == NumUses) {  // Found CSE!
7421bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Result = SCEVAddExpr::get(Result, I->first);
7431bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      ++I;
7441bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    } else {
7451bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Remove non-cse's from SubExpressionUseCounts.
7461bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      SubExpressionUseCounts.erase(I++);
7471bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    }
7481bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7491bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // If we found no CSE's, return now.
7501bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  if (Result == Zero) return Result;
7511bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7521bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Otherwise, remove all of the CSE's we found from each of the base values.
753934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  for (unsigned i = 0; i != NumUses; ++i) {
754934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Split the expression into subexprs.
755934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SeparateSubExprs(SubExprs, Uses[i].Base);
756934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
757934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Remove any common subexpressions.
758934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
759934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      if (SubExpressionUseCounts.count(SubExprs[j])) {
760934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner        SubExprs.erase(SubExprs.begin()+j);
761934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner        --j; --e;
762934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      }
763934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
764934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Finally, the non-shared expressions together.
765934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (SubExprs.empty())
7661bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Uses[i].Base = Zero;
767934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    else
768934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      Uses[i].Base = SCEVAddExpr::get(SubExprs);
76927e5142309946ca12c633b265673af0c24684427Chris Lattner    SubExprs.clear();
770934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
7711bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7721bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  return Result;
7731bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner}
7741bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7751bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
776169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single
777169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// stride of IV.  All of the users may have different starting values, and this
778169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// may not be the only stride (we know it is if isOnlyStride is true).
77950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattnervoid LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
780ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                                      IVUsersOfOneStride &Uses,
781ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                                      Loop *L,
782169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman                                                      bool isOnlyStride) {
783169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Transform our list of users and offsets to a bit more complex table.  In
784a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // this new vector, each 'BasedUser' contains 'Base' the base of the
785a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // strided accessas well as the old information from Uses.  We progressively
786a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // move information from the Base field to the Imm field, until we eventually
787a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // have the full access expression to rewrite the use.
788a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  std::vector<BasedUser> UsersToProcess;
789169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  UsersToProcess.reserve(Uses.Users.size());
790a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) {
791a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    UsersToProcess.push_back(Uses.Users[i]);
792a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
793a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // Move any loop invariant operands from the offset field to the immediate
794a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // field of the use, so that we don't try to use something before it is
795a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // computed.
796a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    MoveLoopVariantsToImediateField(UsersToProcess.back().Base,
797a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner                                    UsersToProcess.back().Imm, L);
798a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    assert(UsersToProcess.back().Base->isLoopInvariant(L) &&
79926d91f16464db56283087176a73981048331dd2dChris Lattner           "Base value is not loop invariant!");
8002461dff0700d0e34b9854d96a5cc03921b375525Chris Lattner  }
80144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
8021bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // We now have a whole bunch of uses of like-strided induction variables, but
8031bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // they might all have different bases.  We want to emit one PHI node for this
8041bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // stride which we fold as many common expressions (between the IVs) into as
8051bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // possible.  Start by identifying the common expressions in the base values
8061bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find
8071bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // "A+B"), emit it to the preheader, then remove the expression from the
8081bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // UsersToProcess base values.
8091bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle CommonExprs = RemoveCommonExpressionsFromUseBases(UsersToProcess);
8101bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
81144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  // Next, figure out what we can represent in the immediate fields of
81244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  // instructions.  If we can represent anything there, move it to the imm
8131bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // fields of the BasedUsers.  We do this so that it increases the commonality
8141bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // of the remaining uses.
81544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
81680b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // If the user is not in the current loop, this means it is using the exit
81780b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // value of the IV.  Do not put anything in the base, make sure it's all in
81880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // the immediate field to allow as much factoring as possible.
81980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    if (!L->contains(UsersToProcess[i].Inst->getParent())) {
8208385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner      UsersToProcess[i].Imm = SCEVAddExpr::get(UsersToProcess[i].Imm,
8218385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner                                               UsersToProcess[i].Base);
8228385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner      UsersToProcess[i].Base =
8238385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner        SCEVUnknown::getIntegerSCEV(0, UsersToProcess[i].Base->getType());
82480b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    } else {
82580b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner
82680b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      // Addressing modes can be folded into loads and stores.  Be careful that
82780b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      // the store is through the expression, not of the expression though.
82880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      bool isAddress = isa<LoadInst>(UsersToProcess[i].Inst);
82980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].Inst))
83080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner        if (SI->getOperand(1) == UsersToProcess[i].OperandValToReplace)
83180b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner          isAddress = true;
83280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner
83380b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      MoveImmediateValues(UsersToProcess[i].Base, UsersToProcess[i].Imm,
83480b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner                          isAddress, L);
83580b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    }
83644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  }
83744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
8381bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Now that we know what we need to do, insert the PHI node itself.
8391bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  //
8401bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  DEBUG(std::cerr << "INSERTING IV of STRIDE " << *Stride << " and BASE "
8411bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        << *CommonExprs << " :\n");
8421bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8431bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVExpander Rewriter(*SE, *LI);
8441bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVExpander PreheaderRewriter(*SE, *LI);
8451bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8461bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
8471bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Instruction *PreInsertPt = Preheader->getTerminator();
8481bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Instruction *PhiInsertBefore = L->getHeader()->begin();
8491bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
85012b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner  BasicBlock *LatchBlock = L->getLoopLatch();
85144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
8521bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Create a new Phi for this base, and stick it in the loop header.
8531bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  const Type *ReplacedTy = CommonExprs->getType();
8541bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  PHINode *NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore);
8551bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  ++NumInserted;
85644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
85750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // Insert the stride into the preheader.
85850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  Value *StrideV = PreheaderRewriter.expandCodeFor(Stride, PreInsertPt,
85950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                                   ReplacedTy);
86050fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  if (!isa<ConstantInt>(StrideV)) ++NumVariable;
86150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
86250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
8631bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Emit the initial base value into the loop preheader, and add it to the
8641bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Phi node.
8651bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Value *PHIBaseV = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt,
8661bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner                                                    ReplacedTy);
8671bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  NewPHI->addIncoming(PHIBaseV, Preheader);
868be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
8691bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Emit the increment of the base value before the terminator of the loop
8701bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // latch block, and add it to the Phi node.
8711bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle IncExp = SCEVAddExpr::get(SCEVUnknown::get(NewPHI),
87250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                       SCEVUnknown::get(StrideV));
8731bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8741bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Value *IncV = Rewriter.expandCodeFor(IncExp, LatchBlock->getTerminator(),
8751bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner                                       ReplacedTy);
8761bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  IncV->setName(NewPHI->getName()+".inc");
8771bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  NewPHI->addIncoming(IncV, LatchBlock);
8781bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8792351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner  // Sort by the base value, so that all IVs with identical bases are next to
8801bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // each other.
8812351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner  std::sort(UsersToProcess.begin(), UsersToProcess.end());
882169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  while (!UsersToProcess.empty()) {
883a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    SCEVHandle Base = UsersToProcess.front().Base;
884be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
8851bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    DEBUG(std::cerr << "  INSERTING code for BASE = " << *Base << ":\n");
886be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
8871bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // Emit the code for Base into the preheader.
8885272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner    Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt,
8895272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner                                                   ReplacedTy);
8901bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8911bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // If BaseV is a constant other than 0, make sure that it gets inserted into
8921bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // the preheader, instead of being forward substituted into the uses.  We do
8931bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // this by forcing a noop cast to be inserted into the preheader in this
8941bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // case.
8951bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    if (Constant *C = dyn_cast<Constant>(BaseV))
8967259df3ab862d378427cf96cd69d1bc69aa2e5cbChris Lattner      if (!C->isNullValue() && !isTargetConstant(Base)) {
8971bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        // We want this constant emitted into the preheader!
8981bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        BaseV = new CastInst(BaseV, BaseV->getType(), "preheaderinsert",
8991bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner                             PreInsertPt);
9001bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      }
9011bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
902169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Emit the code to add the immediate offset to the Phi value, just before
9032351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    // the instructions that we identified as using this stride and base.
904a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    while (!UsersToProcess.empty() && UsersToProcess.front().Base == Base) {
905a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner      BasedUser &User = UsersToProcess.front();
9062351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
907010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // If this instruction wants to use the post-incremented value, move it
908010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // after the post-inc and use its value instead of the PHI.
9091bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Value *RewriteOp = NewPHI;
910010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (User.isUseOfPostIncrementedValue) {
9111bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        RewriteOp = IncV;
912c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner
913c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // If this user is in the loop, make sure it is the last thing in the
914c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // loop to ensure it is dominated by the increment.
915c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        if (L->contains(User.Inst->getParent()))
916c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner          User.Inst->moveBefore(LatchBlock->getTerminator());
917010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
9181bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp);
9191bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
9201bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Clear the SCEVExpander's expression map so that we are guaranteed
9211bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // to have the code emitted where we expect it.
9221bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Rewriter.clear();
9231bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
9241bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Now that we know what we need to do, insert code before User for the
9251bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // immediate and any loop-variant expressions.
9261bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isNullValue())
9271bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        // Add BaseV to the PHI value if needed.
9281bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        RewriteExpr = SCEVAddExpr::get(RewriteExpr, SCEVUnknown::get(BaseV));
9291bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
930c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner      User.RewriteInstructionToUseNewBase(RewriteExpr, Rewriter, L, this);
9312351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
9322351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // Mark old value we replaced as possibly dead, so that it is elminated
9332351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // if we just replaced the last use of that value.
9342114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      DeadInsts.insert(cast<Instruction>(User.OperandValToReplace));
9352351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
9362351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      UsersToProcess.erase(UsersToProcess.begin());
9372351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      ++NumReduced;
9382351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    }
939169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // TODO: Next, find out which base index is the most common, pull it out.
940169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
941169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
942169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but
943169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // different starting values, into different PHIs.
944eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
945eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
946010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar
947010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// uses in the loop, look to see if we can eliminate some, in favor of using
948010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// common indvars for the different uses.
949010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattnervoid LoopStrengthReduce::OptimizeIndvars(Loop *L) {
950010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // TODO: implement optzns here.
951010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
952010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
953010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
954010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
955010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Finally, get the terminating condition for the loop if possible.  If we
956010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // can, we want to change it to use a post-incremented version of its
957010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // induction variable, to allow coallescing the live ranges for the IV into
958010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // one register value.
959010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin());
960010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
961010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock *LatchBlock =
962010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner   SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader);
963010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator());
964010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!TermBr || TermBr->isUnconditional() ||
965010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      !isa<SetCondInst>(TermBr->getCondition()))
966010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    return;
967010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  SetCondInst *Cond = cast<SetCondInst>(TermBr->getCondition());
968010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
969010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Search IVUsesByStride to find Cond's IVUse if there is one.
970010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  IVStrideUse *CondUse = 0;
97150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  const SCEVHandle *CondStride = 0;
972010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
973b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner  for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e && !CondUse;
974b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner       ++Stride) {
975b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
976b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner      IVUsesByStride.find(StrideOrder[Stride]);
977b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
978b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner
979b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(),
980b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner           E = SI->second.Users.end(); UI != E; ++UI)
981010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (UI->User == Cond) {
982010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        CondUse = &*UI;
983b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner        CondStride = &SI->first;
984010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // NOTE: we could handle setcc instructions with multiple uses here, but
985010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // InstCombine does it as well for simple uses, it's not clear that it
986010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // occurs enough in real life to handle.
987010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        break;
988010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
989b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner  }
990010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!CondUse) return;  // setcc doesn't use the IV.
991010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
992010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // setcc stride is complex, don't mess with users.
99350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // FIXME: Evaluate whether this is a good idea or not.
99450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  if (!isa<SCEVConstant>(*CondStride)) return;
995010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
996010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // It's possible for the setcc instruction to be anywhere in the loop, and
997010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // possible for it to have multiple users.  If it is not immediately before
998010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // the latch block branch, move it.
999010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) {
1000010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    if (Cond->hasOneUse()) {   // Condition has a single use, just move it.
1001010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->moveBefore(TermBr);
1002010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    } else {
1003010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Otherwise, clone the terminating condition and insert into the loopend.
1004010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond = cast<SetCondInst>(Cond->clone());
1005010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->setName(L->getHeader()->getName() + ".termcond");
1006010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      LatchBlock->getInstList().insert(TermBr, Cond);
1007010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1008010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Clone the IVUse, as the old use still exists!
100950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner      IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond,
1010010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner                                         CondUse->OperandValToReplace);
101150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner      CondUse = &IVUsesByStride[*CondStride].Users.back();
1012010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    }
1013010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  }
1014010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1015010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // If we get to here, we know that we can transform the setcc instruction to
1016010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // use the post-incremented version of the IV, allowing us to coallesce the
1017010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // live ranges for the IV correctly.
101850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  CondUse->Offset = SCEV::getMinusSCEV(CondUse->Offset, *CondStride);
1019010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  CondUse->isUseOfPostIncrementedValue = true;
1020010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner}
1021169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1022eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::runOnLoop(Loop *L) {
1023eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // First step, transform all loops nesting inside of this loop.
1024eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
1025eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    runOnLoop(*I);
1026eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1027169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Next, find all uses of induction variables in this loop, and catagorize
1028169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // them by stride.  Start by finding all of the PHI nodes in the header for
1029169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // this loop.  If they are induction variables, inspect their uses.
10303416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  std::set<Instruction*> Processed;   // Don't reprocess instructions.
1031169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
10323416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    AddUsersIfInteresting(I, L, Processed);
1033169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1034169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // If we have nothing to do, return.
1035010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (IVUsesByStride.empty()) return;
1036010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1037010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Optimize induction variables.  Some indvar uses can be transformed to use
1038010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // strides that will be needed for other purposes.  A common example of this
1039010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // is the exit test for the loop, which can often be rewritten to use the
1040010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // computation of some other indvar to decide when to terminate the loop.
1041010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  OptimizeIndvars(L);
1042010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1043169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1044169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: We can widen subreg IV's here for RISC targets.  e.g. instead of
1045169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // doing computation in byte values, promote to 32-bit values if safe.
1046169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1047169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: Attempt to reuse values across multiple IV's.  In particular, we
1048169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be
1049169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC.  Need
1050169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // to be careful that IV's are all the same type.  Only works for intptr_t
1051169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // indvars.
1052169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1053169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // If we only have one stride, we can more aggressively eliminate some things.
1054169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  bool HasOneStride = IVUsesByStride.size() == 1;
10557305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
10561bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Note: this processes each stride/type pair individually.  All users passed
10577305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // into StrengthReduceStridedIVUsers have the same type AND stride.  Also,
10587305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // node that we iterate over IVUsesByStride indirectly by using StrideOrder.
10597305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // This extra layer of indirection makes the ordering of strides deterministic
10607305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // - not dependent on map order.
10617305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) {
10627305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
10637305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      IVUsesByStride.find(StrideOrder[Stride]);
10647305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
1065169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
10667305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  }
1067eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1068eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // Clean up after ourselves
1069eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  if (!DeadInsts.empty()) {
1070eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    DeleteTriviallyDeadInstructions(DeadInsts);
1071eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1072169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    BasicBlock::iterator I = L->getHeader()->begin();
1073169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    PHINode *PN;
1074e9100c69cbfbcc9298b663d80ef4ddf31d7bba69Chris Lattner    while ((PN = dyn_cast<PHINode>(I))) {
10751060e09fb207ed778581957671f8803d2df3a581Chris Lattner      ++I;  // Preincrement iterator to avoid invalidating it when deleting PN.
10761060e09fb207ed778581957671f8803d2df3a581Chris Lattner
107787265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // At this point, we know that we have killed one or more GEP
107887265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // instructions.  It is worth checking to see if the cann indvar is also
107987265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // dead, so that we can remove it as well.  The requirements for the cann
108087265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // indvar to be considered dead are:
1081169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 1. the cann indvar has one use
1082169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 2. the use is an add instruction
1083169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 3. the add has one use
1084169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 4. the add is used by the cann indvar
1085169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // If all four cases above are true, then we can remove both the add and
1086169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // the cann indvar.
1087169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // FIXME: this needs to eliminate an induction variable even if it's being
1088169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // compared against some value to decide loop termination.
1089169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      if (PN->hasOneUse()) {
1090169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman        BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin()));
10917e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        if (BO && BO->hasOneUse()) {
10927e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner          if (PN == *(BO->use_begin())) {
10937e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            DeadInsts.insert(BO);
10947e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            // Break the cycle, then delete the PHI.
10957e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
109652d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner            SE->deleteInstructionFromRecords(PN);
10977e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            PN->eraseFromParent();
1098eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman          }
10997e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        }
1100169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
1101eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
1102169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    DeleteTriviallyDeadInstructions(DeadInsts);
1103eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
1104169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
11059a59fbb89606aaefb27f6fe07dcb7c188bf1b3cdChris Lattner  CastedPointers.clear();
1106169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  IVUsesByStride.clear();
11077305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  StrideOrder.clear();
1108169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return;
1109eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
1110