LoopStrengthReduce.cpp revision 88cac3d2f70d01423f56363f24e172786428b3cf
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;
8088cac3d2f70d01423f56363f24e172786428b3cfChris Lattner    ETForest *EF;
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>();
11488cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      EF = &getAnalysis<ETForest>();
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>();
13288cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      AU.addPreserved<ETForest>();
133aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<ImmediateDominators>();
134aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominanceFrontier>();
135aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominatorTree>();
136aa96ae780afa5475e62df284855a971216289212Chris Lattner
137f465db6c6a5a877aa791abfc3837d62c491dacd5Jeff Cohen      AU.addRequiredID(LoopSimplifyID);
138eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      AU.addRequired<LoopInfo>();
13988cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      AU.addRequired<ETForest>();
1402f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen      AU.addRequired<TargetData>();
141169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      AU.addRequired<ScalarEvolution>();
142eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
14349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
14449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf - Return the specified value casted to uintptr_t.
14549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    ///
14649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    Value *getCastedVersionOf(Value *V);
14749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattnerprivate:
148eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    void runOnLoop(Loop *L);
1493416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    bool AddUsersIfInteresting(Instruction *I, Loop *L,
1503416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                               std::set<Instruction*> &Processed);
1513416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L);
1523416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
153010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    void OptimizeIndvars(Loop *L);
154169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
15550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    void StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
15650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                      IVUsersOfOneStride &Uses,
157ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                      Loop *L, bool isOnlyStride);
158eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
159eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  };
160fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  RegisterOpt<LoopStrengthReduce> X("loop-reduce",
161fe15830f962bb7fef046203a77d438d087772b34Chris Lattner                                    "Loop Strength Reduction");
162eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
163eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1642f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff CohenFunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) {
1652f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen  return new LoopStrengthReduce(MaxTargetAMSize);
166eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
167eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
16849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner/// getCastedVersionOf - Return the specified value casted to uintptr_t.
16949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner///
17049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris LattnerValue *LoopStrengthReduce::getCastedVersionOf(Value *V) {
17149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (V->getType() == UIntPtrTy) return V;
17249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (Constant *CB = dyn_cast<Constant>(V))
17349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    return ConstantExpr::getCast(CB, UIntPtrTy);
17449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
17549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  Value *&New = CastedPointers[V];
17649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (New) return New;
17749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
17849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  BasicBlock::iterator InsertPt;
17949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (Argument *Arg = dyn_cast<Argument>(V)) {
18049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    // Insert into the entry of the function, after any allocas.
18149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    InsertPt = Arg->getParent()->begin()->begin();
18249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    while (isa<AllocaInst>(InsertPt)) ++InsertPt;
18349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  } else {
18449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    if (InvokeInst *II = dyn_cast<InvokeInst>(V)) {
18549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      InsertPt = II->getNormalDest()->begin();
18649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    } else {
18749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      InsertPt = cast<Instruction>(V);
18849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner      ++InsertPt;
18949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    }
19049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
19149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    // Do not insert casts into the middle of PHI node blocks.
19249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    while (isa<PHINode>(InsertPt)) ++InsertPt;
19349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  }
1947db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
1957db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  New = new CastInst(V, UIntPtrTy, V->getName(), InsertPt);
1967db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  DeadInsts.insert(cast<Instruction>(New));
1977db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return New;
19849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner}
19949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
20049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
201eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// DeleteTriviallyDeadInstructions - If any of the instructions is the
202eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// specified set are trivially dead, delete them and see if this makes any of
203eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// their operands subsequently dead.
204eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::
205eaa13851a7fe604363577350c5cf65c257c4d41aNate BegemanDeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
206eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  while (!Insts.empty()) {
207eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    Instruction *I = *Insts.begin();
208eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    Insts.erase(Insts.begin());
209eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    if (isInstructionTriviallyDead(I)) {
2100456e4a079de51087978c177b1de63343731f4fbJeff Cohen      for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
2110456e4a079de51087978c177b1de63343731f4fbJeff Cohen        if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
2120456e4a079de51087978c177b1de63343731f4fbJeff Cohen          Insts.insert(U);
21352d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner      SE->deleteInstructionFromRecords(I);
21452d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner      I->eraseFromParent();
215eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      Changed = true;
216eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
217eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
218eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
219eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
220169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2213416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// GetExpressionSCEV - Compute and return the SCEV for the specified
2223416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// instruction.
2233416e5f645186a7e3321f927eab662d0ecef404bChris LattnerSCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
22487265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions.
22587265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // If this is a GEP that SE doesn't know about, compute it now and insert it.
22687265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // If this is not a GEP, or if we have already done this computation, just let
22787265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // SE figure it out.
2283416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp);
22987265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  if (!GEP || SE->hasSCEV(GEP))
2303416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return SE->getSCEV(Exp);
2313416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
232169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Analyze all of the subscripts of this getelementptr instruction, looking
233169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // for uses that are determined by the trip count of L.  First, skip all
234169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // operands the are not dependent on the IV.
235169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
236169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Build up the base expression.  Insert an LLVM cast of the pointer to
237169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // uintptr_t first.
2383416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0)));
239169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
240169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  gep_type_iterator GTI = gep_type_begin(GEP);
2413416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
2423416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
243169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is a use of a recurrence that we can analyze, and it comes before
244169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Op does in the GEP operand list, we will handle this when we process this
245169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // operand.
246169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
247169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      const StructLayout *SL = TD->getStructLayout(STy);
248169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      unsigned Idx = cast<ConstantUInt>(GEP->getOperand(i))->getValue();
249169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      uint64_t Offset = SL->MemberOffsets[Idx];
2503416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      GEPVal = SCEVAddExpr::get(GEPVal,
2513416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy));
2522f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    } else {
2537db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      Value *OpVal = getCastedVersionOf(GEP->getOperand(i));
2547db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      SCEVHandle Idx = SE->getSCEV(OpVal);
2557db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2563416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType());
2573416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      if (TypeSize != 1)
2583416e5f645186a7e3321f927eab662d0ecef404bChris Lattner        Idx = SCEVMulExpr::get(Idx,
2593416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                               SCEVConstant::get(ConstantUInt::get(UIntPtrTy,
2603416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                                                   TypeSize)));
2613416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      GEPVal = SCEVAddExpr::get(GEPVal, Idx);
2622f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    }
263eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
264169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
26587265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  SE->setSCEV(GEP, GEPVal);
2663416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  return GEPVal;
267169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
268169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2697db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// getSCEVStartAndStride - Compute the start and stride of this expression,
2707db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// returning false if the expression is not a start/stride pair, or true if it
2717db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// is.  The stride must be a loop invariant expression, but the start may be
2727db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// a mix of loop invariant and loop variant expressions.
2737db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattnerstatic bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
27450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                  SCEVHandle &Start, SCEVHandle &Stride) {
2757db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle TheAddRec = Start;   // Initialize to zero.
2767db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2777db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // If the outer level is an AddExpr, the operands are all start values except
2787db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // for a nested AddRecExpr.
2797db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
2807db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
2817db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (SCEVAddRecExpr *AddRec =
2827db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner             dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
2837db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        if (AddRec->getLoop() == L)
2847db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          TheAddRec = SCEVAddExpr::get(AddRec, TheAddRec);
2857db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        else
2867db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          return false;  // Nested IV of some sort?
2877db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      } else {
2887db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Start = SCEVAddExpr::get(Start, AE->getOperand(i));
2897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      }
2907db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2917db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SH)) {
2927db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    TheAddRec = SH;
2937db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else {
2947db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // not analyzable.
2957db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  }
2967db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2977db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
2987db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec || AddRec->getLoop() != L) return false;
2997db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3007db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // FIXME: Generalize to non-affine IV's.
3017db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec->isAffine()) return false;
3027db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3037db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  Start = SCEVAddExpr::get(Start, AddRec->getOperand(0));
3047db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3057db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!isa<SCEVConstant>(AddRec->getOperand(1)))
30650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    DEBUG(std::cerr << "[" << L->getHeader()->getName()
30750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                    << "] Variable stride: " << *AddRec << "\n");
30850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
30950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  Stride = AddRec->getOperand(1);
31050fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // Check that all constant strides are the unsigned type, we don't want to
31150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // have two IV's one of signed stride 4 and one of unsigned stride 4 to not be
31250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // merged.
31350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  assert((!isa<SCEVConstant>(Stride) || Stride->getType()->isUnsigned()) &&
3147db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner         "Constants should be canonicalized to unsigned!");
31550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
3167db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return true;
3177db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner}
3187db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3190ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression
3200ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// and now we need to decide whether the user should use the preinc or post-inc
3210ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// value.  If this user should use the post-inc version of the IV, return true.
3220ae33eb243417982fbdca792460bdd986108ac09Chris Lattner///
3230ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// Choosing wrong here can break dominance properties (if we choose to use the
3240ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// post-inc value when we cannot) or it can end up adding extra live-ranges to
3250ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we
3260ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// should use the post-inc value).
3270ae33eb243417982fbdca792460bdd986108ac09Chris Lattnerstatic bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV,
32888cac3d2f70d01423f56363f24e172786428b3cfChris Lattner                                       Loop *L, ETForest *EF, Pass *P) {
3290ae33eb243417982fbdca792460bdd986108ac09Chris Lattner  // If the user is in the loop, use the preinc value.
3300ae33eb243417982fbdca792460bdd986108ac09Chris Lattner  if (L->contains(User->getParent())) return false;
3310ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3325e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  BasicBlock *LatchBlock = L->getLoopLatch();
3335e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3345e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Ok, the user is outside of the loop.  If it is dominated by the latch
3355e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // block, use the post-inc value.
33688cac3d2f70d01423f56363f24e172786428b3cfChris Lattner  if (EF->dominates(LatchBlock, User->getParent()))
3375e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    return true;
3385e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3395e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // There is one case we have to be careful of: PHI nodes.  These little guys
3405e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // can live in blocks that do not dominate the latch block, but (since their
3415e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // uses occur in the predecessor block, not the block the PHI lives in) should
3425e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // still use the post-inc value.  Check for this case now.
3435e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  PHINode *PN = dyn_cast<PHINode>(User);
3445e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  if (!PN) return false;  // not a phi, not dominated by latch block.
3455e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3465e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Look at all of the uses of IV by the PHI node.  If any use corresponds to
3475e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // a block that is not dominated by the latch block, give up and use the
3485e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // preincremented value.
3495e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  unsigned NumUses = 0;
3505e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
3515e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    if (PN->getIncomingValue(i) == IV) {
3525e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      ++NumUses;
35388cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      if (!EF->dominates(LatchBlock, PN->getIncomingBlock(i)))
3545e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner        return false;
3555e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    }
3565e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3575e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Okay, all uses of IV by PN are in predecessor blocks that really are
3585e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // dominated by the latch block.  Split the critical edges and use the
3595e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // post-incremented value.
3605e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
3615e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    if (PN->getIncomingValue(i) == IV) {
3625e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      SplitCriticalEdge(PN->getIncomingBlock(i), PN->getParent(), P);
3635e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      if (--NumUses == 0) break;
3645e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    }
3655e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3665e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  return true;
3670ae33eb243417982fbdca792460bdd986108ac09Chris Lattner}
3680ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3690ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3700ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
371169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// AddUsersIfInteresting - Inspect the specified instruction.  If it is a
372169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// reducible SCEV, recursively add its users to the IVUsesByStride set and
373169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// return true.  Otherwise, return false.
3743416e5f645186a7e3321f927eab662d0ecef404bChris Lattnerbool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L,
3753416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                            std::set<Instruction*> &Processed) {
37663ad7963e4fc68a418cc48e21ba175a2d7093ec9Chris Lattner  if (!I->getType()->isInteger() && !isa<PointerType>(I->getType()))
37763ad7963e4fc68a418cc48e21ba175a2d7093ec9Chris Lattner      return false;   // Void and FP expressions cannot be reduced.
3783416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  if (!Processed.insert(I).second)
3793416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return true;    // Instruction already handled.
3803416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
3817db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the symbolic expression for this instruction.
3823416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  SCEVHandle ISE = GetExpressionSCEV(I, L);
3837db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (isa<SCEVCouldNotCompute>(ISE)) return false;
3847db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3857db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the start and stride for this expression.
3867db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle Start = SCEVUnknown::getIntegerSCEV(0, ISE->getType());
38750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  SCEVHandle Stride = Start;
3887db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!getSCEVStartAndStride(ISE, L, Start, Stride))
3897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // Non-reducible symbolic expression, bail out.
3903416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
391169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;++UI){
392169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *User = cast<Instruction>(*UI);
393169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
394169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Do not infinitely recurse on PHI nodes.
395396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner    if (isa<PHINode>(User) && Processed.count(User))
396169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      continue;
397169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
398169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is an instruction defined in a nested loop, or outside this loop,
399f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    // don't recurse into it.
4007db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    bool AddUserToIVUsers = false;
401f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    if (LI->getLoopFor(User->getParent()) != L) {
402396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      DEBUG(std::cerr << "FOUND USER in other loop: " << *User
403f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner            << "   OF SCEV: " << *ISE << "\n");
4047db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
4053416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    } else if (!AddUsersIfInteresting(User, L, Processed)) {
406a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      DEBUG(std::cerr << "FOUND USER: " << *User
407a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner            << "   OF SCEV: " << *ISE << "\n");
4087db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
4097db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    }
410fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4117db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    if (AddUserToIVUsers) {
4127305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      IVUsersOfOneStride &StrideUses = IVUsesByStride[Stride];
4137305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      if (StrideUses.Users.empty())     // First occurance of this stride?
4147305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideOrder.push_back(Stride);
4157305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
416a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      // Okay, we found a user that we cannot reduce.  Analyze the instruction
417c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      // and decide what to do with it.  If we are a use inside of the loop, use
418c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      // the value before incrementation, otherwise use it after incrementation.
41988cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      if (IVUseShouldUsePostIncValue(User, I, L, EF, this)) {
420c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // The value used will be incremented by the stride more than we are
421c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // expecting, so subtract this off.
422c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        SCEVHandle NewStart = SCEV::getMinusSCEV(Start, Stride);
4237305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.addUser(NewStart, User, I);
4247305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.Users.back().isUseOfPostIncrementedValue = true;
4255e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner        DEBUG(std::cerr << "   USING POSTINC SCEV, START=" << *NewStart<< "\n");
4260ae33eb243417982fbdca792460bdd986108ac09Chris Lattner      } else {
4277305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.addUser(Start, User, I);
428c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      }
429169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
430169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
431169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return true;
432169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
433169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
434169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemannamespace {
435169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// BasedUser - For a particular base value, keep information about how we've
436169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// partitioned the expression so far.
437169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  struct BasedUser {
438a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// Base - The Base value for the PHI node that needs to be inserted for
439a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// this use.  As the use is processed, information gets moved from this
440a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// field to the Imm field (below).  BasedUser values are sorted by this
441a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// field.
442a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    SCEVHandle Base;
443a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
444169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Inst - The instruction using the induction variable.
445169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *Inst;
446169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
447ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// OperandValToReplace - The operand value of Inst to replace with the
448ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// EmittedBase.
449ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
450169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
451169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Imm - The immediate value that should be added to the base immediately
452169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// before Inst, because it will be folded into the imm field of the
453169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// instruction.
454169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    SCEVHandle Imm;
455169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
456169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// EmittedBase - The actual value* to use for the base value of this
457169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// operation.  This is null if we should just use zero so far.
458169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Value *EmittedBase;
459169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
460010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
461010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
462010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
463c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // instruction for a loop and uses outside the loop that are dominated by
464c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // the loop.
465010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
466a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
467a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    BasedUser(IVStrideUse &IVSU)
468a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner      : Base(IVSU.Offset), Inst(IVSU.User),
469a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        OperandValToReplace(IVSU.OperandValToReplace),
470a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        Imm(SCEVUnknown::getIntegerSCEV(0, Base->getType())), EmittedBase(0),
471a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {}
472169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
4732114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Once we rewrite the code to insert the new IVs we want, update the
4742114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // operands of Inst to use the new expression 'NewBase', with 'Imm' added
4752114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // to it.
4761bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
477c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                        SCEVExpander &Rewriter, Loop *L,
478c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                        Pass *P);
479169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    void dump() const;
480169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
481169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
482169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
483169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanvoid BasedUser::dump() const {
484a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  std::cerr << " Base=" << *Base;
485169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::cerr << " Imm=" << *Imm;
486169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (EmittedBase)
487169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    std::cerr << "  EB=" << *EmittedBase;
488169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
489169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::cerr << "   Inst: " << *Inst;
490169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
491169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
4922114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// Once we rewrite the code to insert the new IVs we want, update the
4932114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// operands of Inst to use the new expression 'NewBase', with 'Imm' added
4942114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// to it.
4951bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnervoid BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
496e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner                                               SCEVExpander &Rewriter,
497c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                               Loop *L, Pass *P) {
4982114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  if (!isa<PHINode>(Inst)) {
4991bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    SCEVHandle NewValSCEV = SCEVAddExpr::get(NewBase, Imm);
5002114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    Value *NewVal = Rewriter.expandCodeFor(NewValSCEV, Inst,
5012114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner                                           OperandValToReplace->getType());
5022114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Replace the use of the operand Value with the new Phi we just created.
5032114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
5042114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    DEBUG(std::cerr << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst);
5052114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    return;
5062114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
5072114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
5082114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  // PHI nodes are more complex.  We have to insert one copy of the NewBase+Imm
509c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // expression into each operand block that uses it.  Note that PHI nodes can
510c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // have multiple entries for the same predecessor.  We use a map to make sure
511c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // that a PHI node only has a single Value* for each predecessor (which also
512c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // prevents us from inserting duplicate code in some blocks).
513c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  std::map<BasicBlock*, Value*> InsertedCode;
5142114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  PHINode *PN = cast<PHINode>(Inst);
5152114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5162114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    if (PN->getIncomingValue(i) == OperandValToReplace) {
517e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner      // If this is a critical edge, split the edge so that we do not insert the
518396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // code on all predecessor/successor paths.  We do this unless this is the
519396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // canonical backedge for this loop, as this can make some inserted code
520396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // be in an illegal position.
52137edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner      BasicBlock *PHIPred = PN->getIncomingBlock(i);
52237edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner      if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 &&
52337edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner          (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) {
52437edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner
525aa96ae780afa5475e62df284855a971216289212Chris Lattner        // First step, split the critical edge.
52637edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner        SplitCriticalEdge(PHIPred, PN->getParent(), P);
527c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner
528aa96ae780afa5475e62df284855a971216289212Chris Lattner        // Next step: move the basic block.  In particular, if the PHI node
529aa96ae780afa5475e62df284855a971216289212Chris Lattner        // is outside of the loop, and PredTI is in the loop, we want to
530aa96ae780afa5475e62df284855a971216289212Chris Lattner        // move the block to be immediately before the PHI block, not
531aa96ae780afa5475e62df284855a971216289212Chris Lattner        // immediately after PredTI.
53237edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner        if (L->contains(PHIPred) && !L->contains(PN->getParent())) {
533aa96ae780afa5475e62df284855a971216289212Chris Lattner          BasicBlock *NewBB = PN->getIncomingBlock(i);
534aa96ae780afa5475e62df284855a971216289212Chris Lattner          NewBB->moveBefore(PN->getParent());
535e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner        }
536e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner      }
5372114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
538c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      Value *&Code = InsertedCode[PN->getIncomingBlock(i)];
539c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      if (!Code) {
540c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        // Insert the code into the end of the predecessor block.
541c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        BasicBlock::iterator InsertPt =PN->getIncomingBlock(i)->getTerminator();
5422114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
543c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        SCEVHandle NewValSCEV = SCEVAddExpr::get(NewBase, Imm);
544c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        Code = Rewriter.expandCodeFor(NewValSCEV, InsertPt,
545c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner                                      OperandValToReplace->getType());
546c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      }
5472114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
5482114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // Replace the use of the operand Value with the new Phi we just created.
549c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      PN->setIncomingValue(i, Code);
5502114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      Rewriter.clear();
5512114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    }
5522114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
5532114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  DEBUG(std::cerr << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst);
5542114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner}
5552114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
5562114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
557169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// isTargetConstant - Return true if the following can be referenced by the
558169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// immediate field of a target instruction.
559169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanstatic bool isTargetConstant(const SCEVHandle &V) {
560d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
561169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: Look at the target to decide if &GV is a legal constant immediate.
5623821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
5633821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner    // PPC allows a sign-extended 16-bit immediate field.
564e08dc62b1a3b72e351165128ba63c2cdd2f41eb6Chris Lattner    int64_t V = SC->getValue()->getSExtValue();
565e08dc62b1a3b72e351165128ba63c2cdd2f41eb6Chris Lattner    if (V > -(1 << 16) && V < (1 << 16)-1)
566e08dc62b1a3b72e351165128ba63c2cdd2f41eb6Chris Lattner        return true;
5673821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner    return false;
5683821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner  }
569d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
570169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return false;     // ENABLE this for x86
571d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
572169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
573169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
574169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      if (CE->getOpcode() == Instruction::Cast)
575169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman        if (isa<GlobalValue>(CE->getOperand(0)))
576169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman          // FIXME: should check to see that the dest is uintptr_t!
577169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman          return true;
578169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return false;
579169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
580169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
58144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// MoveLoopVariantsToImediateField - Move any subexpressions from Val that are
58244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// loop varying to the Imm operand.
58344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattnerstatic void MoveLoopVariantsToImediateField(SCEVHandle &Val, SCEVHandle &Imm,
58444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner                                            Loop *L) {
58544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  if (Val->isLoopInvariant(L)) return;  // Nothing to do.
58644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
58744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
58844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    std::vector<SCEVHandle> NewOps;
58944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    NewOps.reserve(SAE->getNumOperands());
59044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
59144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
59244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      if (!SAE->getOperand(i)->isLoopInvariant(L)) {
59344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        // If this is a loop-variant expression, it must stay in the immediate
59444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        // field of the expression.
59544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
59644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      } else {
59744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        NewOps.push_back(SAE->getOperand(i));
59844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      }
59944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
60044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    if (NewOps.empty())
60144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
60244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    else
60344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      Val = SCEVAddExpr::get(NewOps);
60444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
60544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    // Try to pull immediates out of the start value of nested addrec's.
60644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    SCEVHandle Start = SARE->getStart();
60744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    MoveLoopVariantsToImediateField(Start, Imm, L);
60844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
60944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
61044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Ops[0] = Start;
61144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
61244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  } else {
61344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    // Otherwise, all of Val is variant, move the whole thing over.
61444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Imm = SCEVAddExpr::get(Imm, Val);
61544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
61644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  }
61744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner}
61844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
61944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
62026d91f16464db56283087176a73981048331dd2dChris Lattner/// MoveImmediateValues - Look at Val, and pull out any additions of constants
621169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// that can fit into the immediate field of instructions in the target.
62226d91f16464db56283087176a73981048331dd2dChris Lattner/// Accumulate these immediate values into the Imm value.
62326d91f16464db56283087176a73981048331dd2dChris Lattnerstatic void MoveImmediateValues(SCEVHandle &Val, SCEVHandle &Imm,
62426d91f16464db56283087176a73981048331dd2dChris Lattner                                bool isAddress, Loop *L) {
6257a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
62626d91f16464db56283087176a73981048331dd2dChris Lattner    std::vector<SCEVHandle> NewOps;
62726d91f16464db56283087176a73981048331dd2dChris Lattner    NewOps.reserve(SAE->getNumOperands());
62826d91f16464db56283087176a73981048331dd2dChris Lattner
62926d91f16464db56283087176a73981048331dd2dChris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
6307db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (isAddress && isTargetConstant(SAE->getOperand(i))) {
6317db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
6327db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      } else if (!SAE->getOperand(i)->isLoopInvariant(L)) {
6337db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // If this is a loop-variant expression, it must stay in the immediate
6347db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // field of the expression.
6357db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
63626d91f16464db56283087176a73981048331dd2dChris Lattner      } else {
63726d91f16464db56283087176a73981048331dd2dChris Lattner        NewOps.push_back(SAE->getOperand(i));
638169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
63926d91f16464db56283087176a73981048331dd2dChris Lattner
64026d91f16464db56283087176a73981048331dd2dChris Lattner    if (NewOps.empty())
64126d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
64226d91f16464db56283087176a73981048331dd2dChris Lattner    else
64326d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVAddExpr::get(NewOps);
64426d91f16464db56283087176a73981048331dd2dChris Lattner    return;
6457a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
6467a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner    // Try to pull immediates out of the start value of nested addrec's.
64726d91f16464db56283087176a73981048331dd2dChris Lattner    SCEVHandle Start = SARE->getStart();
64826d91f16464db56283087176a73981048331dd2dChris Lattner    MoveImmediateValues(Start, Imm, isAddress, L);
64926d91f16464db56283087176a73981048331dd2dChris Lattner
65026d91f16464db56283087176a73981048331dd2dChris Lattner    if (Start != SARE->getStart()) {
65126d91f16464db56283087176a73981048331dd2dChris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
65226d91f16464db56283087176a73981048331dd2dChris Lattner      Ops[0] = Start;
65326d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
65426d91f16464db56283087176a73981048331dd2dChris Lattner    }
65526d91f16464db56283087176a73981048331dd2dChris Lattner    return;
656169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
657169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
65826d91f16464db56283087176a73981048331dd2dChris Lattner  // Loop-variant expressions must stay in the immediate field of the
65926d91f16464db56283087176a73981048331dd2dChris Lattner  // expression.
66026d91f16464db56283087176a73981048331dd2dChris Lattner  if ((isAddress && isTargetConstant(Val)) ||
66126d91f16464db56283087176a73981048331dd2dChris Lattner      !Val->isLoopInvariant(L)) {
66226d91f16464db56283087176a73981048331dd2dChris Lattner    Imm = SCEVAddExpr::get(Imm, Val);
66326d91f16464db56283087176a73981048331dd2dChris Lattner    Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
66426d91f16464db56283087176a73981048331dd2dChris Lattner    return;
6657a2ca56ef3bdda6874bcd4df2910fb5a33999f85Chris Lattner  }
66626d91f16464db56283087176a73981048331dd2dChris Lattner
66726d91f16464db56283087176a73981048331dd2dChris Lattner  // Otherwise, no immediates to move.
668169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
669169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
670934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
671934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// IncrementAddExprUses - Decompose the specified expression into its added
672934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// subexpressions, and increment SubExpressionUseCounts for each of these
673934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// decomposed parts.
674934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattnerstatic void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs,
675934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner                             SCEVHandle Expr) {
676934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) {
677934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j)
678934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SeparateSubExprs(SubExprs, AE->getOperand(j));
679934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) {
680934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Expr->getType());
681934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (SARE->getOperand(0) == Zero) {
682934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SubExprs.push_back(Expr);
683934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    } else {
684934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      // Compute the addrec with zero as its base.
685934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
686934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      Ops[0] = Zero;   // Start with zero base.
687934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SubExprs.push_back(SCEVAddRecExpr::get(Ops, SARE->getLoop()));
688934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
689934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
690934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SeparateSubExprs(SubExprs, SARE->getOperand(0));
691934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    }
692934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  } else if (!isa<SCEVConstant>(Expr) ||
693934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner             !cast<SCEVConstant>(Expr)->getValue()->isNullValue()) {
694934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Do not add zero.
695934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SubExprs.push_back(Expr);
696934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
697934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner}
698934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
699934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
7001bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// RemoveCommonExpressionsFromUseBases - Look through all of the uses in Bases,
7011bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// removing any common subexpressions from it.  Anything truly common is
7021bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// removed, accumulated, and returned.  This looks for things like (a+b+c) and
7031bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// (a+c+d) -> (a+c).  The common expression is *removed* from the Bases.
7041bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnerstatic SCEVHandle
7051bbae0cbf212d0356f564d12e8f728be455bdc47Chris LattnerRemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses) {
7061bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  unsigned NumUses = Uses.size();
7071bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7081bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Only one use?  Use its base, regardless of what it is!
7091bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Uses[0].Base->getType());
7101bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle Result = Zero;
7111bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  if (NumUses == 1) {
7121bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    std::swap(Result, Uses[0].Base);
7131bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    return Result;
7141bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  }
7151bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7161bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // To find common subexpressions, count how many of Uses use each expression.
7171bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // If any subexpressions are used Uses.size() times, they are common.
7181bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  std::map<SCEVHandle, unsigned> SubExpressionUseCounts;
7191bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
720d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // UniqueSubExprs - Keep track of all of the subexpressions we see in the
721d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // order we see them.
722d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  std::vector<SCEVHandle> UniqueSubExprs;
723d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner
724934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  std::vector<SCEVHandle> SubExprs;
725934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  for (unsigned i = 0; i != NumUses; ++i) {
726934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // If the base is zero (which is common), return zero now, there are no
727934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // CSEs we can find.
728934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (Uses[i].Base == Zero) return Zero;
729934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
730934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Split the expression into subexprs.
731934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SeparateSubExprs(SubExprs, Uses[i].Base);
732934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Add one to SubExpressionUseCounts for each subexpr present.
733934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
734d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner      if (++SubExpressionUseCounts[SubExprs[j]] == 1)
735d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner        UniqueSubExprs.push_back(SubExprs[j]);
736934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SubExprs.clear();
737934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
738934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
739d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // Now that we know how many times each is used, build Result.  Iterate over
740d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // UniqueSubexprs so that we have a stable ordering.
741d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  for (unsigned i = 0, e = UniqueSubExprs.size(); i != e; ++i) {
742d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner    std::map<SCEVHandle, unsigned>::iterator I =
743d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner       SubExpressionUseCounts.find(UniqueSubExprs[i]);
744d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner    assert(I != SubExpressionUseCounts.end() && "Entry not found?");
7451bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    if (I->second == NumUses) {  // Found CSE!
7461bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Result = SCEVAddExpr::get(Result, I->first);
7471bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    } else {
7481bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Remove non-cse's from SubExpressionUseCounts.
749d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner      SubExpressionUseCounts.erase(I);
7501bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    }
751d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  }
7521bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7531bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // If we found no CSE's, return now.
7541bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  if (Result == Zero) return Result;
7551bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7561bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Otherwise, remove all of the CSE's we found from each of the base values.
757934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  for (unsigned i = 0; i != NumUses; ++i) {
758934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Split the expression into subexprs.
759934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SeparateSubExprs(SubExprs, Uses[i].Base);
760934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
761934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Remove any common subexpressions.
762934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
763934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      if (SubExpressionUseCounts.count(SubExprs[j])) {
764934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner        SubExprs.erase(SubExprs.begin()+j);
765934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner        --j; --e;
766934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      }
767934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
768934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Finally, the non-shared expressions together.
769934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (SubExprs.empty())
7701bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Uses[i].Base = Zero;
771934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    else
772934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      Uses[i].Base = SCEVAddExpr::get(SubExprs);
77327e5142309946ca12c633b265673af0c24684427Chris Lattner    SubExprs.clear();
774934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
7751bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7761bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  return Result;
7771bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner}
7781bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7791bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
780169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single
781169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// stride of IV.  All of the users may have different starting values, and this
782169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// may not be the only stride (we know it is if isOnlyStride is true).
78350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattnervoid LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
784ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                                      IVUsersOfOneStride &Uses,
785ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                                      Loop *L,
786169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman                                                      bool isOnlyStride) {
787169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Transform our list of users and offsets to a bit more complex table.  In
788a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // this new vector, each 'BasedUser' contains 'Base' the base of the
789a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // strided accessas well as the old information from Uses.  We progressively
790a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // move information from the Base field to the Imm field, until we eventually
791a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // have the full access expression to rewrite the use.
792a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  std::vector<BasedUser> UsersToProcess;
793169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  UsersToProcess.reserve(Uses.Users.size());
794a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) {
795a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    UsersToProcess.push_back(Uses.Users[i]);
796a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
797a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // Move any loop invariant operands from the offset field to the immediate
798a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // field of the use, so that we don't try to use something before it is
799a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // computed.
800a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    MoveLoopVariantsToImediateField(UsersToProcess.back().Base,
801a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner                                    UsersToProcess.back().Imm, L);
802a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    assert(UsersToProcess.back().Base->isLoopInvariant(L) &&
80326d91f16464db56283087176a73981048331dd2dChris Lattner           "Base value is not loop invariant!");
8042461dff0700d0e34b9854d96a5cc03921b375525Chris Lattner  }
80544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
8061bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // We now have a whole bunch of uses of like-strided induction variables, but
8071bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // they might all have different bases.  We want to emit one PHI node for this
8081bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // stride which we fold as many common expressions (between the IVs) into as
8091bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // possible.  Start by identifying the common expressions in the base values
8101bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find
8111bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // "A+B"), emit it to the preheader, then remove the expression from the
8121bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // UsersToProcess base values.
8131bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle CommonExprs = RemoveCommonExpressionsFromUseBases(UsersToProcess);
8141bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
81544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  // Next, figure out what we can represent in the immediate fields of
81644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  // instructions.  If we can represent anything there, move it to the imm
8171bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // fields of the BasedUsers.  We do this so that it increases the commonality
8181bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // of the remaining uses.
81944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
82080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // If the user is not in the current loop, this means it is using the exit
82180b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // value of the IV.  Do not put anything in the base, make sure it's all in
82280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // the immediate field to allow as much factoring as possible.
82380b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    if (!L->contains(UsersToProcess[i].Inst->getParent())) {
8248385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner      UsersToProcess[i].Imm = SCEVAddExpr::get(UsersToProcess[i].Imm,
8258385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner                                               UsersToProcess[i].Base);
8268385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner      UsersToProcess[i].Base =
8278385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner        SCEVUnknown::getIntegerSCEV(0, UsersToProcess[i].Base->getType());
82880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    } else {
82980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner
83080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      // Addressing modes can be folded into loads and stores.  Be careful that
83180b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      // the store is through the expression, not of the expression though.
83280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      bool isAddress = isa<LoadInst>(UsersToProcess[i].Inst);
83380b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].Inst))
83480b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner        if (SI->getOperand(1) == UsersToProcess[i].OperandValToReplace)
83580b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner          isAddress = true;
83680b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner
83780b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      MoveImmediateValues(UsersToProcess[i].Base, UsersToProcess[i].Imm,
83880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner                          isAddress, L);
83980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    }
84044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  }
84144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
8421bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Now that we know what we need to do, insert the PHI node itself.
8431bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  //
8441bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  DEBUG(std::cerr << "INSERTING IV of STRIDE " << *Stride << " and BASE "
8451bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        << *CommonExprs << " :\n");
8461bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8471bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVExpander Rewriter(*SE, *LI);
8481bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVExpander PreheaderRewriter(*SE, *LI);
8491bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8501bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
8511bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Instruction *PreInsertPt = Preheader->getTerminator();
8521bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Instruction *PhiInsertBefore = L->getHeader()->begin();
8531bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
85412b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner  BasicBlock *LatchBlock = L->getLoopLatch();
85544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
8561bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Create a new Phi for this base, and stick it in the loop header.
8571bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  const Type *ReplacedTy = CommonExprs->getType();
8581bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  PHINode *NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore);
8591bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  ++NumInserted;
86044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
86150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // Insert the stride into the preheader.
86250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  Value *StrideV = PreheaderRewriter.expandCodeFor(Stride, PreInsertPt,
86350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                                   ReplacedTy);
86450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  if (!isa<ConstantInt>(StrideV)) ++NumVariable;
86550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
86650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
8671bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Emit the initial base value into the loop preheader, and add it to the
8681bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Phi node.
8691bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Value *PHIBaseV = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt,
8701bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner                                                    ReplacedTy);
8711bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  NewPHI->addIncoming(PHIBaseV, Preheader);
872be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
8731bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Emit the increment of the base value before the terminator of the loop
8741bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // latch block, and add it to the Phi node.
8751bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle IncExp = SCEVAddExpr::get(SCEVUnknown::get(NewPHI),
87650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                       SCEVUnknown::get(StrideV));
8771bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8781bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Value *IncV = Rewriter.expandCodeFor(IncExp, LatchBlock->getTerminator(),
8791bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner                                       ReplacedTy);
8801bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  IncV->setName(NewPHI->getName()+".inc");
8811bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  NewPHI->addIncoming(IncV, LatchBlock);
8821bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8832351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner  // Sort by the base value, so that all IVs with identical bases are next to
8841bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // each other.
885169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  while (!UsersToProcess.empty()) {
8867b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    SCEVHandle Base = UsersToProcess.back().Base;
887be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
8881bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    DEBUG(std::cerr << "  INSERTING code for BASE = " << *Base << ":\n");
889be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
8901bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // Emit the code for Base into the preheader.
8915272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner    Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt,
8925272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner                                                   ReplacedTy);
8931bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8941bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // If BaseV is a constant other than 0, make sure that it gets inserted into
8951bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // the preheader, instead of being forward substituted into the uses.  We do
8961bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // this by forcing a noop cast to be inserted into the preheader in this
8971bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // case.
8981bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    if (Constant *C = dyn_cast<Constant>(BaseV))
8997259df3ab862d378427cf96cd69d1bc69aa2e5cbChris Lattner      if (!C->isNullValue() && !isTargetConstant(Base)) {
9001bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        // We want this constant emitted into the preheader!
9011bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        BaseV = new CastInst(BaseV, BaseV->getType(), "preheaderinsert",
9021bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner                             PreInsertPt);
9031bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      }
9041bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
905169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Emit the code to add the immediate offset to the Phi value, just before
9062351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    // the instructions that we identified as using this stride and base.
9077b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    unsigned ScanPos = 0;
9087b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    do {
9097b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      BasedUser &User = UsersToProcess.back();
9102351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
911010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // If this instruction wants to use the post-incremented value, move it
912010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // after the post-inc and use its value instead of the PHI.
9131bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Value *RewriteOp = NewPHI;
914010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (User.isUseOfPostIncrementedValue) {
9151bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        RewriteOp = IncV;
916c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner
917c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // If this user is in the loop, make sure it is the last thing in the
918c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // loop to ensure it is dominated by the increment.
919c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        if (L->contains(User.Inst->getParent()))
920c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner          User.Inst->moveBefore(LatchBlock->getTerminator());
921010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
9221bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp);
9231bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
9241bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Clear the SCEVExpander's expression map so that we are guaranteed
9251bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // to have the code emitted where we expect it.
9261bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Rewriter.clear();
9271bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
9281bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Now that we know what we need to do, insert code before User for the
9291bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // immediate and any loop-variant expressions.
9301bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isNullValue())
9311bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        // Add BaseV to the PHI value if needed.
9321bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        RewriteExpr = SCEVAddExpr::get(RewriteExpr, SCEVUnknown::get(BaseV));
9331bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
934c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner      User.RewriteInstructionToUseNewBase(RewriteExpr, Rewriter, L, this);
9352351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
9362351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // Mark old value we replaced as possibly dead, so that it is elminated
9372351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // if we just replaced the last use of that value.
9382114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      DeadInsts.insert(cast<Instruction>(User.OperandValToReplace));
9392351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
9407b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      UsersToProcess.pop_back();
9412351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      ++NumReduced;
9427b445c521bc191d0d25799b289e17b45f202a1afChris Lattner
9437b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      // If there are any more users to process with the same base, move one of
9447b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      // them to the end of the list so that we will process it.
9457b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      if (!UsersToProcess.empty()) {
9467b445c521bc191d0d25799b289e17b45f202a1afChris Lattner        for (unsigned e = UsersToProcess.size(); ScanPos != e; ++ScanPos)
9477b445c521bc191d0d25799b289e17b45f202a1afChris Lattner          if (UsersToProcess[ScanPos].Base == Base) {
9487b445c521bc191d0d25799b289e17b45f202a1afChris Lattner            std::swap(UsersToProcess[ScanPos], UsersToProcess.back());
9497b445c521bc191d0d25799b289e17b45f202a1afChris Lattner            break;
9507b445c521bc191d0d25799b289e17b45f202a1afChris Lattner          }
9517b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      }
9527b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    } while (!UsersToProcess.empty() && UsersToProcess.back().Base == Base);
953169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // TODO: Next, find out which base index is the most common, pull it out.
954169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
955169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
956169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but
957169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // different starting values, into different PHIs.
958eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
959eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
960010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar
961010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// uses in the loop, look to see if we can eliminate some, in favor of using
962010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// common indvars for the different uses.
963010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattnervoid LoopStrengthReduce::OptimizeIndvars(Loop *L) {
964010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // TODO: implement optzns here.
965010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
966010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
967010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
968010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
969010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Finally, get the terminating condition for the loop if possible.  If we
970010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // can, we want to change it to use a post-incremented version of its
971010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // induction variable, to allow coallescing the live ranges for the IV into
972010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // one register value.
973010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin());
974010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
975010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock *LatchBlock =
976010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner   SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader);
977010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator());
978010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!TermBr || TermBr->isUnconditional() ||
979010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      !isa<SetCondInst>(TermBr->getCondition()))
980010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    return;
981010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  SetCondInst *Cond = cast<SetCondInst>(TermBr->getCondition());
982010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
983010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Search IVUsesByStride to find Cond's IVUse if there is one.
984010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  IVStrideUse *CondUse = 0;
98550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  const SCEVHandle *CondStride = 0;
986010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
987b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner  for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e && !CondUse;
988b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner       ++Stride) {
989b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
990b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner      IVUsesByStride.find(StrideOrder[Stride]);
991b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
992b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner
993b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(),
994b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner           E = SI->second.Users.end(); UI != E; ++UI)
995010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (UI->User == Cond) {
996010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        CondUse = &*UI;
997b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner        CondStride = &SI->first;
998010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // NOTE: we could handle setcc instructions with multiple uses here, but
999010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // InstCombine does it as well for simple uses, it's not clear that it
1000010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // occurs enough in real life to handle.
1001010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        break;
1002010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
1003b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner  }
1004010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!CondUse) return;  // setcc doesn't use the IV.
1005010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1006010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // setcc stride is complex, don't mess with users.
100750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // FIXME: Evaluate whether this is a good idea or not.
100850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  if (!isa<SCEVConstant>(*CondStride)) return;
1009010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1010010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // It's possible for the setcc instruction to be anywhere in the loop, and
1011010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // possible for it to have multiple users.  If it is not immediately before
1012010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // the latch block branch, move it.
1013010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) {
1014010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    if (Cond->hasOneUse()) {   // Condition has a single use, just move it.
1015010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->moveBefore(TermBr);
1016010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    } else {
1017010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Otherwise, clone the terminating condition and insert into the loopend.
1018010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond = cast<SetCondInst>(Cond->clone());
1019010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->setName(L->getHeader()->getName() + ".termcond");
1020010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      LatchBlock->getInstList().insert(TermBr, Cond);
1021010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1022010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Clone the IVUse, as the old use still exists!
102350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner      IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond,
1024010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner                                         CondUse->OperandValToReplace);
102550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner      CondUse = &IVUsesByStride[*CondStride].Users.back();
1026010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    }
1027010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  }
1028010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1029010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // If we get to here, we know that we can transform the setcc instruction to
1030010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // use the post-incremented version of the IV, allowing us to coallesce the
1031010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // live ranges for the IV correctly.
103250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  CondUse->Offset = SCEV::getMinusSCEV(CondUse->Offset, *CondStride);
1033010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  CondUse->isUseOfPostIncrementedValue = true;
1034010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner}
1035169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1036eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::runOnLoop(Loop *L) {
1037eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // First step, transform all loops nesting inside of this loop.
1038eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
1039eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    runOnLoop(*I);
1040eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1041169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Next, find all uses of induction variables in this loop, and catagorize
1042169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // them by stride.  Start by finding all of the PHI nodes in the header for
1043169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // this loop.  If they are induction variables, inspect their uses.
10443416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  std::set<Instruction*> Processed;   // Don't reprocess instructions.
1045169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
10463416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    AddUsersIfInteresting(I, L, Processed);
1047169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1048169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // If we have nothing to do, return.
1049010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (IVUsesByStride.empty()) return;
1050010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1051010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Optimize induction variables.  Some indvar uses can be transformed to use
1052010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // strides that will be needed for other purposes.  A common example of this
1053010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // is the exit test for the loop, which can often be rewritten to use the
1054010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // computation of some other indvar to decide when to terminate the loop.
1055010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  OptimizeIndvars(L);
1056010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1057169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1058169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: We can widen subreg IV's here for RISC targets.  e.g. instead of
1059169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // doing computation in byte values, promote to 32-bit values if safe.
1060169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1061169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: Attempt to reuse values across multiple IV's.  In particular, we
1062169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be
1063169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC.  Need
1064169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // to be careful that IV's are all the same type.  Only works for intptr_t
1065169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // indvars.
1066169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1067169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // If we only have one stride, we can more aggressively eliminate some things.
1068169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  bool HasOneStride = IVUsesByStride.size() == 1;
10697305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
10701bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Note: this processes each stride/type pair individually.  All users passed
10717305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // into StrengthReduceStridedIVUsers have the same type AND stride.  Also,
10727305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // node that we iterate over IVUsesByStride indirectly by using StrideOrder.
10737305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // This extra layer of indirection makes the ordering of strides deterministic
10747305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // - not dependent on map order.
10757305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) {
10767305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
10777305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      IVUsesByStride.find(StrideOrder[Stride]);
10787305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
1079169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
10807305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  }
1081eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1082eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // Clean up after ourselves
1083eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  if (!DeadInsts.empty()) {
1084eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    DeleteTriviallyDeadInstructions(DeadInsts);
1085eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1086169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    BasicBlock::iterator I = L->getHeader()->begin();
1087169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    PHINode *PN;
1088e9100c69cbfbcc9298b663d80ef4ddf31d7bba69Chris Lattner    while ((PN = dyn_cast<PHINode>(I))) {
10891060e09fb207ed778581957671f8803d2df3a581Chris Lattner      ++I;  // Preincrement iterator to avoid invalidating it when deleting PN.
10901060e09fb207ed778581957671f8803d2df3a581Chris Lattner
109187265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // At this point, we know that we have killed one or more GEP
109287265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // instructions.  It is worth checking to see if the cann indvar is also
109387265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // dead, so that we can remove it as well.  The requirements for the cann
109487265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // indvar to be considered dead are:
1095169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 1. the cann indvar has one use
1096169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 2. the use is an add instruction
1097169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 3. the add has one use
1098169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 4. the add is used by the cann indvar
1099169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // If all four cases above are true, then we can remove both the add and
1100169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // the cann indvar.
1101169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // FIXME: this needs to eliminate an induction variable even if it's being
1102169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // compared against some value to decide loop termination.
1103169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      if (PN->hasOneUse()) {
1104169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman        BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin()));
11057e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        if (BO && BO->hasOneUse()) {
11067e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner          if (PN == *(BO->use_begin())) {
11077e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            DeadInsts.insert(BO);
11087e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            // Break the cycle, then delete the PHI.
11097e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
111052d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner            SE->deleteInstructionFromRecords(PN);
11117e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            PN->eraseFromParent();
1112eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman          }
11137e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        }
1114169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
1115eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
1116169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    DeleteTriviallyDeadInstructions(DeadInsts);
1117eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
1118169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
11199a59fbb89606aaefb27f6fe07dcb7c188bf1b3cdChris Lattner  CastedPointers.clear();
1120169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  IVUsesByStride.clear();
11217305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  StrideOrder.clear();
1122169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return;
1123eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
1124