LoopStrengthReduce.cpp revision 7e79b3898ddd919170d367a516f51296017146c2
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"
349525528a7dc5462b6374d38c81ba5c07b11741feChris Lattner#include "llvm/Support/Visibility.h"
35d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng#include "llvm/Target/TargetLowering.h"
36cfb1d4235fe3291028341e6acf4139723b4749e3Jeff Cohen#include <algorithm>
37dac58ad983c62b49629e1f2969f4e0a621167d63Chris Lattner#include <iostream>
38eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include <set>
39eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanusing namespace llvm;
40eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
41eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemannamespace {
42eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced");
4326d91f16464db56283087176a73981048331dd2dChris Lattner  Statistic<> NumInserted("loop-reduce", "Number of PHIs inserted");
4450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  Statistic<> NumVariable("loop-reduce","Number of PHIs with variable strides");
45eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
46ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// IVStrideUse - Keep track of one use of a strided induction variable, where
47ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// the stride is stored externally.  The Offset member keeps track of the
48ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// offset from the IV, User is the actual user of the operand, and 'Operand'
49ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// is the operand # of the User that is the use.
50ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  struct IVStrideUse {
51ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    SCEVHandle Offset;
52ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Instruction *User;
53ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
54010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
55010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
56010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
57010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
58c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // instruction for a loop or uses dominated by the loop.
59010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
60ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
61ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O)
62010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      : Offset(Offs), User(U), OperandValToReplace(O),
63010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        isUseOfPostIncrementedValue(false) {}
64ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  };
65ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
66ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// IVUsersOfOneStride - This structure keeps track of all instructions that
67ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// have an operand that is based on the trip count multiplied by some stride.
68ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// The stride for all of these users is common and kept external to this
69ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// structure.
70ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  struct IVUsersOfOneStride {
71169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Users - Keep track of all of the users of this stride as well as the
72ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// initial value and the operand that uses the IV.
73ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    std::vector<IVStrideUse> Users;
74ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
75ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) {
76ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner      Users.push_back(IVStrideUse(Offset, User, Operand));
77169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
78169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
79169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
80d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  /// IVInfo - This structure keeps track of one IV expression inserted during
8121495775e710d37003e100862cdc647cbdc3b999Evan Cheng  /// StrengthReduceStridedIVUsers. It contains the stride, the common base, as
8221495775e710d37003e100862cdc647cbdc3b999Evan Cheng  /// well as the PHI node and increment value created for rewrite.
83d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  struct IVExpr {
8421495775e710d37003e100862cdc647cbdc3b999Evan Cheng    SCEVHandle  Stride;
85d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    SCEVHandle  Base;
86d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    PHINode    *PHI;
87d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    Value      *IncV;
88d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
8921495775e710d37003e100862cdc647cbdc3b999Evan Cheng    IVExpr()
9021495775e710d37003e100862cdc647cbdc3b999Evan Cheng      : Stride(SCEVUnknown::getIntegerSCEV(0, Type::UIntTy)),
9121495775e710d37003e100862cdc647cbdc3b999Evan Cheng        Base  (SCEVUnknown::getIntegerSCEV(0, Type::UIntTy)) {}
9221495775e710d37003e100862cdc647cbdc3b999Evan Cheng    IVExpr(const SCEVHandle &stride, const SCEVHandle &base, PHINode *phi,
9321495775e710d37003e100862cdc647cbdc3b999Evan Cheng           Value *incv)
9421495775e710d37003e100862cdc647cbdc3b999Evan Cheng      : Stride(stride), Base(base), PHI(phi), IncV(incv) {}
95d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  };
96d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
97d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  /// IVsOfOneStride - This structure keeps track of all IV expression inserted
98d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  /// during StrengthReduceStridedIVUsers for a particular stride of the IV.
99d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  struct IVsOfOneStride {
100d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    std::vector<IVExpr> IVs;
101d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
10221495775e710d37003e100862cdc647cbdc3b999Evan Cheng    void addIV(const SCEVHandle &Stride, const SCEVHandle &Base, PHINode *PHI,
10321495775e710d37003e100862cdc647cbdc3b999Evan Cheng               Value *IncV) {
10421495775e710d37003e100862cdc647cbdc3b999Evan Cheng      IVs.push_back(IVExpr(Stride, Base, PHI, IncV));
105d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    }
106d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  };
107169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1089525528a7dc5462b6374d38c81ba5c07b11741feChris Lattner  class VISIBILITY_HIDDEN LoopStrengthReduce : public FunctionPass {
109eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    LoopInfo *LI;
11088cac3d2f70d01423f56363f24e172786428b3cfChris Lattner    ETForest *EF;
111169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    ScalarEvolution *SE;
112169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    const TargetData *TD;
113169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    const Type *UIntPtrTy;
114eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    bool Changed;
1157e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner
116169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// IVUsesByStride - Keep track of all uses of induction variables that we
117169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// are interested in.  The key of the map is the stride of the access.
11850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    std::map<SCEVHandle, IVUsersOfOneStride> IVUsesByStride;
119169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
120d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    /// IVsByStride - Keep track of all IVs that have been inserted for a
121d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    /// particular stride.
122d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    std::map<SCEVHandle, IVsOfOneStride> IVsByStride;
123d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1247305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// StrideOrder - An ordering of the keys in IVUsesByStride that is stable:
1257305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// We use this to iterate over the IVUsesByStride collection without being
1267305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// dependent on random ordering of pointers in the process.
1277305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    std::vector<SCEVHandle> StrideOrder;
1287305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
12949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// CastedValues - As we need to cast values to uintptr_t, this keeps track
13049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// of the casted version of each value.  This is accessed by
13149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf.
13249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    std::map<Value*, Value*> CastedPointers;
133169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
134169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// DeadInsts - Keep track of instructions we may have made dead, so that
135169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// we can remove them after we are done working.
136169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    std::set<Instruction*> DeadInsts;
137d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng
138d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    /// TLI - Keep a pointer of a TargetLowering to consult for determining
139d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    /// transformation profitability.
140d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    const TargetLowering *TLI;
141d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng
142eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  public:
143d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    LoopStrengthReduce(const TargetLowering *tli = NULL)
144d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng      : TLI(tli) {
1452f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen    }
1462f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen
147eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    virtual bool runOnFunction(Function &) {
148eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      LI = &getAnalysis<LoopInfo>();
14988cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      EF = &getAnalysis<ETForest>();
150169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      SE = &getAnalysis<ScalarEvolution>();
151169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      TD = &getAnalysis<TargetData>();
152169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      UIntPtrTy = TD->getIntPtrType();
153eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      Changed = false;
154eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
155eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
156eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman        runOnLoop(*I);
15749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
158eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      return Changed;
159eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
160eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
161eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
162aa96ae780afa5475e62df284855a971216289212Chris Lattner      // We split critical edges, so we change the CFG.  However, we do update
163aa96ae780afa5475e62df284855a971216289212Chris Lattner      // many analyses if they are around.
164aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreservedID(LoopSimplifyID);
165aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<LoopInfo>();
166aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominatorSet>();
16788cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      AU.addPreserved<ETForest>();
168aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<ImmediateDominators>();
169aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominanceFrontier>();
170aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominatorTree>();
171aa96ae780afa5475e62df284855a971216289212Chris Lattner
172f465db6c6a5a877aa791abfc3837d62c491dacd5Jeff Cohen      AU.addRequiredID(LoopSimplifyID);
173eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      AU.addRequired<LoopInfo>();
17488cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      AU.addRequired<ETForest>();
1752f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen      AU.addRequired<TargetData>();
176169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      AU.addRequired<ScalarEvolution>();
177eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
17849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
17949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf - Return the specified value casted to uintptr_t.
18049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    ///
18149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    Value *getCastedVersionOf(Value *V);
18249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattnerprivate:
183eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    void runOnLoop(Loop *L);
1843416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    bool AddUsersIfInteresting(Instruction *I, Loop *L,
1853416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                               std::set<Instruction*> &Processed);
1863416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L);
1873416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
188010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    void OptimizeIndvars(Loop *L);
189169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
19031e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng    unsigned CheckForIVReuse(const SCEVHandle&, IVExpr&, const Type*);
191eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
19250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    void StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
19350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                      IVUsersOfOneStride &Uses,
194ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                      Loop *L, bool isOnlyStride);
195eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
196eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  };
197fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman  RegisterOpt<LoopStrengthReduce> X("loop-reduce",
198fe15830f962bb7fef046203a77d438d087772b34Chris Lattner                                    "Loop Strength Reduction");
199eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
200eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
201d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan ChengFunctionPass *llvm::createLoopStrengthReducePass(const TargetLowering *TLI) {
202d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  return new LoopStrengthReduce(TLI);
203eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
204eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
20549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner/// getCastedVersionOf - Return the specified value casted to uintptr_t.
20649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner///
20749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris LattnerValue *LoopStrengthReduce::getCastedVersionOf(Value *V) {
20849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (V->getType() == UIntPtrTy) return V;
20949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (Constant *CB = dyn_cast<Constant>(V))
21049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    return ConstantExpr::getCast(CB, UIntPtrTy);
21149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
21249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  Value *&New = CastedPointers[V];
21349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (New) return New;
21449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
2150a70f219f45077dfaa7e37ad1ed6f760f1f7a1e8Chris Lattner  New = SCEVExpander::InsertCastOfTo(V, UIntPtrTy);
2167db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  DeadInsts.insert(cast<Instruction>(New));
2177db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return New;
21849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner}
21949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
22049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
221eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// DeleteTriviallyDeadInstructions - If any of the instructions is the
222eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// specified set are trivially dead, delete them and see if this makes any of
223eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// their operands subsequently dead.
224eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::
225eaa13851a7fe604363577350c5cf65c257c4d41aNate BegemanDeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
226eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  while (!Insts.empty()) {
227eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    Instruction *I = *Insts.begin();
228eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    Insts.erase(Insts.begin());
229eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    if (isInstructionTriviallyDead(I)) {
2300456e4a079de51087978c177b1de63343731f4fbJeff Cohen      for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
2310456e4a079de51087978c177b1de63343731f4fbJeff Cohen        if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
2320456e4a079de51087978c177b1de63343731f4fbJeff Cohen          Insts.insert(U);
23352d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner      SE->deleteInstructionFromRecords(I);
23452d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner      I->eraseFromParent();
235eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      Changed = true;
236eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
237eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
238eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
239eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
240169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2413416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// GetExpressionSCEV - Compute and return the SCEV for the specified
2423416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// instruction.
2433416e5f645186a7e3321f927eab662d0ecef404bChris LattnerSCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
24487265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions.
24587265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // If this is a GEP that SE doesn't know about, compute it now and insert it.
24687265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // If this is not a GEP, or if we have already done this computation, just let
24787265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // SE figure it out.
2483416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp);
24987265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  if (!GEP || SE->hasSCEV(GEP))
2503416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return SE->getSCEV(Exp);
2513416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
252169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Analyze all of the subscripts of this getelementptr instruction, looking
253169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // for uses that are determined by the trip count of L.  First, skip all
254169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // operands the are not dependent on the IV.
255169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
256169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Build up the base expression.  Insert an LLVM cast of the pointer to
257169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // uintptr_t first.
2583416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0)));
259169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
260169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  gep_type_iterator GTI = gep_type_begin(GEP);
2613416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
2623416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
263169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is a use of a recurrence that we can analyze, and it comes before
264169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Op does in the GEP operand list, we will handle this when we process this
265169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // operand.
266169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
267169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      const StructLayout *SL = TD->getStructLayout(STy);
268169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      unsigned Idx = cast<ConstantUInt>(GEP->getOperand(i))->getValue();
269169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      uint64_t Offset = SL->MemberOffsets[Idx];
2703416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      GEPVal = SCEVAddExpr::get(GEPVal,
2713416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy));
2722f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    } else {
2737db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      Value *OpVal = getCastedVersionOf(GEP->getOperand(i));
2747db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      SCEVHandle Idx = SE->getSCEV(OpVal);
2757db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2763416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType());
2773416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      if (TypeSize != 1)
2783416e5f645186a7e3321f927eab662d0ecef404bChris Lattner        Idx = SCEVMulExpr::get(Idx,
2793416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                               SCEVConstant::get(ConstantUInt::get(UIntPtrTy,
2803416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                                                   TypeSize)));
2813416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      GEPVal = SCEVAddExpr::get(GEPVal, Idx);
2822f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    }
283eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
284169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
28587265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  SE->setSCEV(GEP, GEPVal);
2863416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  return GEPVal;
287169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
288169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// getSCEVStartAndStride - Compute the start and stride of this expression,
2907db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// returning false if the expression is not a start/stride pair, or true if it
2917db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// is.  The stride must be a loop invariant expression, but the start may be
2927db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// a mix of loop invariant and loop variant expressions.
2937db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattnerstatic bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
29450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                  SCEVHandle &Start, SCEVHandle &Stride) {
2957db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle TheAddRec = Start;   // Initialize to zero.
2967db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
2977db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // If the outer level is an AddExpr, the operands are all start values except
2987db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // for a nested AddRecExpr.
2997db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
3007db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
3017db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (SCEVAddRecExpr *AddRec =
3027db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner             dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
3037db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        if (AddRec->getLoop() == L)
3047db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          TheAddRec = SCEVAddExpr::get(AddRec, TheAddRec);
3057db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        else
3067db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          return false;  // Nested IV of some sort?
3077db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      } else {
3087db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        Start = SCEVAddExpr::get(Start, AE->getOperand(i));
3097db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      }
3107db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3117db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SH)) {
3127db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    TheAddRec = SH;
3137db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else {
3147db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // not analyzable.
3157db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  }
3167db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3177db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
3187db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec || AddRec->getLoop() != L) return false;
3197db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3207db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // FIXME: Generalize to non-affine IV's.
3217db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec->isAffine()) return false;
3227db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3237db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  Start = SCEVAddExpr::get(Start, AddRec->getOperand(0));
3247db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3257db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!isa<SCEVConstant>(AddRec->getOperand(1)))
32650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    DEBUG(std::cerr << "[" << L->getHeader()->getName()
32750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                    << "] Variable stride: " << *AddRec << "\n");
32850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
32950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  Stride = AddRec->getOperand(1);
33050fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // Check that all constant strides are the unsigned type, we don't want to
33150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // have two IV's one of signed stride 4 and one of unsigned stride 4 to not be
33250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // merged.
33350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  assert((!isa<SCEVConstant>(Stride) || Stride->getType()->isUnsigned()) &&
3347db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner         "Constants should be canonicalized to unsigned!");
33550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
3367db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return true;
3377db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner}
3387db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3390ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression
3400ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// and now we need to decide whether the user should use the preinc or post-inc
3410ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// value.  If this user should use the post-inc version of the IV, return true.
3420ae33eb243417982fbdca792460bdd986108ac09Chris Lattner///
3430ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// Choosing wrong here can break dominance properties (if we choose to use the
3440ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// post-inc value when we cannot) or it can end up adding extra live-ranges to
3450ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we
3460ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// should use the post-inc value).
3470ae33eb243417982fbdca792460bdd986108ac09Chris Lattnerstatic bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV,
34888cac3d2f70d01423f56363f24e172786428b3cfChris Lattner                                       Loop *L, ETForest *EF, Pass *P) {
3490ae33eb243417982fbdca792460bdd986108ac09Chris Lattner  // If the user is in the loop, use the preinc value.
3500ae33eb243417982fbdca792460bdd986108ac09Chris Lattner  if (L->contains(User->getParent())) return false;
3510ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3525e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  BasicBlock *LatchBlock = L->getLoopLatch();
3535e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3545e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Ok, the user is outside of the loop.  If it is dominated by the latch
3555e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // block, use the post-inc value.
35688cac3d2f70d01423f56363f24e172786428b3cfChris Lattner  if (EF->dominates(LatchBlock, User->getParent()))
3575e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    return true;
3585e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3595e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // There is one case we have to be careful of: PHI nodes.  These little guys
3605e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // can live in blocks that do not dominate the latch block, but (since their
3615e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // uses occur in the predecessor block, not the block the PHI lives in) should
3625e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // still use the post-inc value.  Check for this case now.
3635e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  PHINode *PN = dyn_cast<PHINode>(User);
3645e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  if (!PN) return false;  // not a phi, not dominated by latch block.
3655e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3665e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Look at all of the uses of IV by the PHI node.  If any use corresponds to
3675e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // a block that is not dominated by the latch block, give up and use the
3685e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // preincremented value.
3695e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  unsigned NumUses = 0;
3705e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
3715e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    if (PN->getIncomingValue(i) == IV) {
3725e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      ++NumUses;
37388cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      if (!EF->dominates(LatchBlock, PN->getIncomingBlock(i)))
3745e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner        return false;
3755e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    }
3765e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3775e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Okay, all uses of IV by PN are in predecessor blocks that really are
3785e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // dominated by the latch block.  Split the critical edges and use the
3795e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // post-incremented value.
3805e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
3815e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    if (PN->getIncomingValue(i) == IV) {
3825e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      SplitCriticalEdge(PN->getIncomingBlock(i), PN->getParent(), P);
3835e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      if (--NumUses == 0) break;
3845e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    }
3855e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
3865e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  return true;
3870ae33eb243417982fbdca792460bdd986108ac09Chris Lattner}
3880ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3890ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
3900ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
391169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// AddUsersIfInteresting - Inspect the specified instruction.  If it is a
392169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// reducible SCEV, recursively add its users to the IVUsesByStride set and
393169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// return true.  Otherwise, return false.
3943416e5f645186a7e3321f927eab662d0ecef404bChris Lattnerbool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L,
3953416e5f645186a7e3321f927eab662d0ecef404bChris Lattner                                            std::set<Instruction*> &Processed) {
39663ad7963e4fc68a418cc48e21ba175a2d7093ec9Chris Lattner  if (!I->getType()->isInteger() && !isa<PointerType>(I->getType()))
39763ad7963e4fc68a418cc48e21ba175a2d7093ec9Chris Lattner      return false;   // Void and FP expressions cannot be reduced.
3983416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  if (!Processed.insert(I).second)
3993416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return true;    // Instruction already handled.
4003416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
4017db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the symbolic expression for this instruction.
4023416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  SCEVHandle ISE = GetExpressionSCEV(I, L);
4037db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (isa<SCEVCouldNotCompute>(ISE)) return false;
4047db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
4057db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the start and stride for this expression.
4067db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle Start = SCEVUnknown::getIntegerSCEV(0, ISE->getType());
40750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  SCEVHandle Stride = Start;
4087db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!getSCEVStartAndStride(ISE, L, Start, Stride))
4097db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // Non-reducible symbolic expression, bail out.
4103416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
411169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;++UI){
412169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *User = cast<Instruction>(*UI);
413169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
414169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Do not infinitely recurse on PHI nodes.
415396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner    if (isa<PHINode>(User) && Processed.count(User))
416169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      continue;
417169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
418169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is an instruction defined in a nested loop, or outside this loop,
419f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    // don't recurse into it.
4207db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    bool AddUserToIVUsers = false;
421f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    if (LI->getLoopFor(User->getParent()) != L) {
422396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      DEBUG(std::cerr << "FOUND USER in other loop: " << *User
423f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner            << "   OF SCEV: " << *ISE << "\n");
4247db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
4253416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    } else if (!AddUsersIfInteresting(User, L, Processed)) {
426a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      DEBUG(std::cerr << "FOUND USER: " << *User
427a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner            << "   OF SCEV: " << *ISE << "\n");
4287db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
4297db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    }
430fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
4317db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    if (AddUserToIVUsers) {
4327305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      IVUsersOfOneStride &StrideUses = IVUsesByStride[Stride];
4337305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      if (StrideUses.Users.empty())     // First occurance of this stride?
4347305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideOrder.push_back(Stride);
4357305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
436a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      // Okay, we found a user that we cannot reduce.  Analyze the instruction
437c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      // and decide what to do with it.  If we are a use inside of the loop, use
438c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      // the value before incrementation, otherwise use it after incrementation.
43988cac3d2f70d01423f56363f24e172786428b3cfChris Lattner      if (IVUseShouldUsePostIncValue(User, I, L, EF, this)) {
440c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // The value used will be incremented by the stride more than we are
441c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // expecting, so subtract this off.
442c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        SCEVHandle NewStart = SCEV::getMinusSCEV(Start, Stride);
4437305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.addUser(NewStart, User, I);
4447305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.Users.back().isUseOfPostIncrementedValue = true;
4455e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner        DEBUG(std::cerr << "   USING POSTINC SCEV, START=" << *NewStart<< "\n");
4460ae33eb243417982fbdca792460bdd986108ac09Chris Lattner      } else {
4477305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.addUser(Start, User, I);
448c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      }
449169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
450169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
451169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return true;
452169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
453169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
454169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemannamespace {
455169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// BasedUser - For a particular base value, keep information about how we've
456169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// partitioned the expression so far.
457169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  struct BasedUser {
458a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// Base - The Base value for the PHI node that needs to be inserted for
459a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// this use.  As the use is processed, information gets moved from this
460a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// field to the Imm field (below).  BasedUser values are sorted by this
461a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// field.
462a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    SCEVHandle Base;
463a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
464169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Inst - The instruction using the induction variable.
465169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *Inst;
466169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
467ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// OperandValToReplace - The operand value of Inst to replace with the
468ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// EmittedBase.
469ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
470169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
471169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Imm - The immediate value that should be added to the base immediately
472169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// before Inst, because it will be folded into the imm field of the
473169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// instruction.
474169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    SCEVHandle Imm;
475169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
476169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// EmittedBase - The actual value* to use for the base value of this
477169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// operation.  This is null if we should just use zero so far.
478169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Value *EmittedBase;
479169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
480010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
481010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
482010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
483c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // instruction for a loop and uses outside the loop that are dominated by
484c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // the loop.
485010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
486a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
487a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    BasedUser(IVStrideUse &IVSU)
488a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner      : Base(IVSU.Offset), Inst(IVSU.User),
489a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        OperandValToReplace(IVSU.OperandValToReplace),
490a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        Imm(SCEVUnknown::getIntegerSCEV(0, Base->getType())), EmittedBase(0),
491a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {}
492169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
4932114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Once we rewrite the code to insert the new IVs we want, update the
4942114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // operands of Inst to use the new expression 'NewBase', with 'Imm' added
4952114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // to it.
4961bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
497c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                        SCEVExpander &Rewriter, Loop *L,
498c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                        Pass *P);
499221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
500221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    Value *InsertCodeForBaseAtPosition(const SCEVHandle &NewBase,
501221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                       SCEVExpander &Rewriter,
502221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                       Instruction *IP, Loop *L);
503169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    void dump() const;
504169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
505169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
506169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
507169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanvoid BasedUser::dump() const {
508a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  std::cerr << " Base=" << *Base;
509169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::cerr << " Imm=" << *Imm;
510169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (EmittedBase)
511169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    std::cerr << "  EB=" << *EmittedBase;
512169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
513169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  std::cerr << "   Inst: " << *Inst;
514169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
515169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
516221fc3c6d69bd3854e9121f51e3283492c222ab7Chris LattnerValue *BasedUser::InsertCodeForBaseAtPosition(const SCEVHandle &NewBase,
517221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                              SCEVExpander &Rewriter,
518221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                              Instruction *IP, Loop *L) {
519221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // Figure out where we *really* want to insert this code.  In particular, if
520221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // the user is inside of a loop that is nested inside of L, we really don't
521221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // want to insert this expression before the user, we'd rather pull it out as
522221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // many loops as possible.
523221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  LoopInfo &LI = Rewriter.getLoopInfo();
524221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  Instruction *BaseInsertPt = IP;
525221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
526221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // Figure out the most-nested loop that IP is in.
527221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  Loop *InsertLoop = LI.getLoopFor(IP->getParent());
528221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
529221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // If InsertLoop is not L, and InsertLoop is nested inside of L, figure out
530221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // the preheader of the outer-most loop where NewBase is not loop invariant.
531221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  while (InsertLoop && NewBase->isLoopInvariant(InsertLoop)) {
532221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    BaseInsertPt = InsertLoop->getLoopPreheader()->getTerminator();
533221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    InsertLoop = InsertLoop->getParentLoop();
534221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  }
535221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
536221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // If there is no immediate value, skip the next part.
537221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Imm))
538221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    if (SC->getValue()->isNullValue())
539221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      return Rewriter.expandCodeFor(NewBase, BaseInsertPt,
540221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                    OperandValToReplace->getType());
541221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
542221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  Value *Base = Rewriter.expandCodeFor(NewBase, BaseInsertPt);
543221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
544221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // Always emit the immediate (if non-zero) into the same block as the user.
545221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  SCEVHandle NewValSCEV = SCEVAddExpr::get(SCEVUnknown::get(Base), Imm);
546221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  return Rewriter.expandCodeFor(NewValSCEV, IP,
547221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                OperandValToReplace->getType());
548221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner}
549221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
550221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
5512114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// Once we rewrite the code to insert the new IVs we want, update the
5522114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// operands of Inst to use the new expression 'NewBase', with 'Imm' added
5532114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// to it.
5541bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnervoid BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
555e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner                                               SCEVExpander &Rewriter,
556c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner                                               Loop *L, Pass *P) {
5572114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  if (!isa<PHINode>(Inst)) {
558221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    Value *NewVal = InsertCodeForBaseAtPosition(NewBase, Rewriter, Inst, L);
5592114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Replace the use of the operand Value with the new Phi we just created.
5602114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
5612114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    DEBUG(std::cerr << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst);
5622114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    return;
5632114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
5642114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
5652114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  // PHI nodes are more complex.  We have to insert one copy of the NewBase+Imm
566c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // expression into each operand block that uses it.  Note that PHI nodes can
567c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // have multiple entries for the same predecessor.  We use a map to make sure
568c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // that a PHI node only has a single Value* for each predecessor (which also
569c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // prevents us from inserting duplicate code in some blocks).
570c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  std::map<BasicBlock*, Value*> InsertedCode;
5712114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  PHINode *PN = cast<PHINode>(Inst);
5722114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5732114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    if (PN->getIncomingValue(i) == OperandValToReplace) {
574e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner      // If this is a critical edge, split the edge so that we do not insert the
575396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // code on all predecessor/successor paths.  We do this unless this is the
576396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // canonical backedge for this loop, as this can make some inserted code
577396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // be in an illegal position.
57837edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner      BasicBlock *PHIPred = PN->getIncomingBlock(i);
57937edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner      if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 &&
58037edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner          (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) {
58137edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner
582aa96ae780afa5475e62df284855a971216289212Chris Lattner        // First step, split the critical edge.
58337edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner        SplitCriticalEdge(PHIPred, PN->getParent(), P);
584c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner
585aa96ae780afa5475e62df284855a971216289212Chris Lattner        // Next step: move the basic block.  In particular, if the PHI node
586aa96ae780afa5475e62df284855a971216289212Chris Lattner        // is outside of the loop, and PredTI is in the loop, we want to
587aa96ae780afa5475e62df284855a971216289212Chris Lattner        // move the block to be immediately before the PHI block, not
588aa96ae780afa5475e62df284855a971216289212Chris Lattner        // immediately after PredTI.
58937edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner        if (L->contains(PHIPred) && !L->contains(PN->getParent())) {
590aa96ae780afa5475e62df284855a971216289212Chris Lattner          BasicBlock *NewBB = PN->getIncomingBlock(i);
591aa96ae780afa5475e62df284855a971216289212Chris Lattner          NewBB->moveBefore(PN->getParent());
592e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner        }
593e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner      }
5942114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
595c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      Value *&Code = InsertedCode[PN->getIncomingBlock(i)];
596c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      if (!Code) {
597c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        // Insert the code into the end of the predecessor block.
598221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        Instruction *InsertPt = PN->getIncomingBlock(i)->getTerminator();
599221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        Code = InsertCodeForBaseAtPosition(NewBase, Rewriter, InsertPt, L);
600c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      }
6012114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
6022114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // Replace the use of the operand Value with the new Phi we just created.
603c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      PN->setIncomingValue(i, Code);
6042114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      Rewriter.clear();
6052114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    }
6062114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
6072114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  DEBUG(std::cerr << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst);
6082114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner}
6092114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
6102114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
611169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// isTargetConstant - Return true if the following can be referenced by the
612169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// immediate field of a target instruction.
613d277f2c66914aecb619c12855f6afae4c7ef883bEvan Chengstatic bool isTargetConstant(const SCEVHandle &V, const TargetLowering *TLI) {
6143821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
615e08dc62b1a3b72e351165128ba63c2cdd2f41eb6Chris Lattner    int64_t V = SC->getValue()->getSExtValue();
616d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    if (TLI)
617d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      return TLI->isLegalAddressImmediate(V);
618d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    else
619d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      // Defaults to PPC. PPC allows a sign-extended 16-bit immediate field.
620d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      return (V > -(1 << 16) && V < (1 << 16)-1);
6213821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner  }
622d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
623169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
624169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
625d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      if (CE->getOpcode() == Instruction::Cast) {
626d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng        Constant *Op0 = CE->getOperand(0);
627d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng        if (isa<GlobalValue>(Op0) &&
628d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng            TLI &&
629d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng            TLI->isLegalAddressImmediate(cast<GlobalValue>(Op0)))
630169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman          return true;
631d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      }
632169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return false;
633169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
634169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
63544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// MoveLoopVariantsToImediateField - Move any subexpressions from Val that are
63644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// loop varying to the Imm operand.
63744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattnerstatic void MoveLoopVariantsToImediateField(SCEVHandle &Val, SCEVHandle &Imm,
63844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner                                            Loop *L) {
63944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  if (Val->isLoopInvariant(L)) return;  // Nothing to do.
64044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
64144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
64244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    std::vector<SCEVHandle> NewOps;
64344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    NewOps.reserve(SAE->getNumOperands());
64444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
64544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
64644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      if (!SAE->getOperand(i)->isLoopInvariant(L)) {
64744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        // If this is a loop-variant expression, it must stay in the immediate
64844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        // field of the expression.
64944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
65044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      } else {
65144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        NewOps.push_back(SAE->getOperand(i));
65244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      }
65344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
65444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    if (NewOps.empty())
65544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
65644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    else
65744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      Val = SCEVAddExpr::get(NewOps);
65844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
65944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    // Try to pull immediates out of the start value of nested addrec's.
66044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    SCEVHandle Start = SARE->getStart();
66144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    MoveLoopVariantsToImediateField(Start, Imm, L);
66244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
66344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
66444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Ops[0] = Start;
66544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
66644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  } else {
66744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    // Otherwise, all of Val is variant, move the whole thing over.
66844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Imm = SCEVAddExpr::get(Imm, Val);
66944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
67044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  }
67144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner}
67244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
67344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
67426d91f16464db56283087176a73981048331dd2dChris Lattner/// MoveImmediateValues - Look at Val, and pull out any additions of constants
675169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// that can fit into the immediate field of instructions in the target.
67626d91f16464db56283087176a73981048331dd2dChris Lattner/// Accumulate these immediate values into the Imm value.
677d277f2c66914aecb619c12855f6afae4c7ef883bEvan Chengstatic void MoveImmediateValues(const TargetLowering *TLI,
678d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng                                SCEVHandle &Val, SCEVHandle &Imm,
67926d91f16464db56283087176a73981048331dd2dChris Lattner                                bool isAddress, Loop *L) {
6807a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
68126d91f16464db56283087176a73981048331dd2dChris Lattner    std::vector<SCEVHandle> NewOps;
68226d91f16464db56283087176a73981048331dd2dChris Lattner    NewOps.reserve(SAE->getNumOperands());
68326d91f16464db56283087176a73981048331dd2dChris Lattner
684221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i) {
685221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      SCEVHandle NewOp = SAE->getOperand(i);
686d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      MoveImmediateValues(TLI, NewOp, Imm, isAddress, L);
687221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
688221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      if (!NewOp->isLoopInvariant(L)) {
6897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // If this is a loop-variant expression, it must stay in the immediate
6907db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // field of the expression.
691221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        Imm = SCEVAddExpr::get(Imm, NewOp);
69226d91f16464db56283087176a73981048331dd2dChris Lattner      } else {
693221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        NewOps.push_back(NewOp);
694169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
695221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    }
69626d91f16464db56283087176a73981048331dd2dChris Lattner
69726d91f16464db56283087176a73981048331dd2dChris Lattner    if (NewOps.empty())
69826d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
69926d91f16464db56283087176a73981048331dd2dChris Lattner    else
70026d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVAddExpr::get(NewOps);
70126d91f16464db56283087176a73981048331dd2dChris Lattner    return;
7027a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
7037a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner    // Try to pull immediates out of the start value of nested addrec's.
70426d91f16464db56283087176a73981048331dd2dChris Lattner    SCEVHandle Start = SARE->getStart();
705d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    MoveImmediateValues(TLI, Start, Imm, isAddress, L);
70626d91f16464db56283087176a73981048331dd2dChris Lattner
70726d91f16464db56283087176a73981048331dd2dChris Lattner    if (Start != SARE->getStart()) {
70826d91f16464db56283087176a73981048331dd2dChris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
70926d91f16464db56283087176a73981048331dd2dChris Lattner      Ops[0] = Start;
71026d91f16464db56283087176a73981048331dd2dChris Lattner      Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
71126d91f16464db56283087176a73981048331dd2dChris Lattner    }
71226d91f16464db56283087176a73981048331dd2dChris Lattner    return;
713221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  } else if (SCEVMulExpr *SME = dyn_cast<SCEVMulExpr>(Val)) {
714221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    // Transform "8 * (4 + v)" -> "32 + 8*V" if "32" fits in the immed field.
715d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    if (isAddress && isTargetConstant(SME->getOperand(0), TLI) &&
716221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        SME->getNumOperands() == 2 && SME->isLoopInvariant(L)) {
717221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
718221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      SCEVHandle SubImm = SCEVUnknown::getIntegerSCEV(0, Val->getType());
719221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      SCEVHandle NewOp = SME->getOperand(1);
720d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      MoveImmediateValues(TLI, NewOp, SubImm, isAddress, L);
721221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
722221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      // If we extracted something out of the subexpressions, see if we can
723221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      // simplify this!
724221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      if (NewOp != SME->getOperand(1)) {
725221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        // Scale SubImm up by "8".  If the result is a target constant, we are
726221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        // good.
727221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        SubImm = SCEVMulExpr::get(SubImm, SME->getOperand(0));
728d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng        if (isTargetConstant(SubImm, TLI)) {
729221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner          // Accumulate the immediate.
730221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner          Imm = SCEVAddExpr::get(Imm, SubImm);
731221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
732221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner          // Update what is left of 'Val'.
733221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner          Val = SCEVMulExpr::get(SME->getOperand(0), NewOp);
734221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner          return;
735221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        }
736221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      }
737221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    }
738169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
739169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
74026d91f16464db56283087176a73981048331dd2dChris Lattner  // Loop-variant expressions must stay in the immediate field of the
74126d91f16464db56283087176a73981048331dd2dChris Lattner  // expression.
742d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng  if ((isAddress && isTargetConstant(Val, TLI)) ||
74326d91f16464db56283087176a73981048331dd2dChris Lattner      !Val->isLoopInvariant(L)) {
74426d91f16464db56283087176a73981048331dd2dChris Lattner    Imm = SCEVAddExpr::get(Imm, Val);
74526d91f16464db56283087176a73981048331dd2dChris Lattner    Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
74626d91f16464db56283087176a73981048331dd2dChris Lattner    return;
7477a2ca56ef3bdda6874bcd4df2910fb5a33999f85Chris Lattner  }
74826d91f16464db56283087176a73981048331dd2dChris Lattner
74926d91f16464db56283087176a73981048331dd2dChris Lattner  // Otherwise, no immediates to move.
750169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
751169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
752934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
7537e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// SeparateSubExprs - Decompose Expr into all of the subexpressions that are
7547e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// added together.  This is used to reassociate common addition subexprs
7557e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// together for maximal sharing when rewriting bases.
756934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattnerstatic void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs,
757934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner                             SCEVHandle Expr) {
758934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) {
759934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j)
760934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SeparateSubExprs(SubExprs, AE->getOperand(j));
761934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) {
762934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Expr->getType());
763934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (SARE->getOperand(0) == Zero) {
764934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SubExprs.push_back(Expr);
765934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    } else {
766934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      // Compute the addrec with zero as its base.
767934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
768934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      Ops[0] = Zero;   // Start with zero base.
769934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SubExprs.push_back(SCEVAddRecExpr::get(Ops, SARE->getLoop()));
770934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
771934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
772934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SeparateSubExprs(SubExprs, SARE->getOperand(0));
773934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    }
774934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  } else if (!isa<SCEVConstant>(Expr) ||
775934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner             !cast<SCEVConstant>(Expr)->getValue()->isNullValue()) {
776934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Do not add zero.
777934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SubExprs.push_back(Expr);
778934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
779934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner}
780934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
781934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
7821bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// RemoveCommonExpressionsFromUseBases - Look through all of the uses in Bases,
7831bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// removing any common subexpressions from it.  Anything truly common is
7841bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// removed, accumulated, and returned.  This looks for things like (a+b+c) and
7851bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// (a+c+d) -> (a+c).  The common expression is *removed* from the Bases.
7861bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnerstatic SCEVHandle
7871bbae0cbf212d0356f564d12e8f728be455bdc47Chris LattnerRemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses) {
7881bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  unsigned NumUses = Uses.size();
7891bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7901bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Only one use?  Use its base, regardless of what it is!
7911bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Uses[0].Base->getType());
7921bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle Result = Zero;
7931bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  if (NumUses == 1) {
7941bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    std::swap(Result, Uses[0].Base);
7951bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    return Result;
7961bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  }
7971bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
7981bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // To find common subexpressions, count how many of Uses use each expression.
7991bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // If any subexpressions are used Uses.size() times, they are common.
8001bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  std::map<SCEVHandle, unsigned> SubExpressionUseCounts;
8011bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
802d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // UniqueSubExprs - Keep track of all of the subexpressions we see in the
803d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // order we see them.
804d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  std::vector<SCEVHandle> UniqueSubExprs;
805d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner
806934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  std::vector<SCEVHandle> SubExprs;
807934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  for (unsigned i = 0; i != NumUses; ++i) {
808934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // If the base is zero (which is common), return zero now, there are no
809934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // CSEs we can find.
810934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (Uses[i].Base == Zero) return Zero;
811934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
812934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Split the expression into subexprs.
813934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SeparateSubExprs(SubExprs, Uses[i].Base);
814934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Add one to SubExpressionUseCounts for each subexpr present.
815934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
816d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner      if (++SubExpressionUseCounts[SubExprs[j]] == 1)
817d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner        UniqueSubExprs.push_back(SubExprs[j]);
818934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SubExprs.clear();
819934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
820934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
821d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // Now that we know how many times each is used, build Result.  Iterate over
822d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // UniqueSubexprs so that we have a stable ordering.
823d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  for (unsigned i = 0, e = UniqueSubExprs.size(); i != e; ++i) {
824d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner    std::map<SCEVHandle, unsigned>::iterator I =
825d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner       SubExpressionUseCounts.find(UniqueSubExprs[i]);
826d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner    assert(I != SubExpressionUseCounts.end() && "Entry not found?");
8271bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    if (I->second == NumUses) {  // Found CSE!
8281bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Result = SCEVAddExpr::get(Result, I->first);
8291bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    } else {
8301bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Remove non-cse's from SubExpressionUseCounts.
831d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner      SubExpressionUseCounts.erase(I);
8321bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    }
833d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  }
8341bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8351bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // If we found no CSE's, return now.
8361bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  if (Result == Zero) return Result;
8371bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8381bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Otherwise, remove all of the CSE's we found from each of the base values.
839934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  for (unsigned i = 0; i != NumUses; ++i) {
840934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Split the expression into subexprs.
841934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SeparateSubExprs(SubExprs, Uses[i].Base);
842934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
843934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Remove any common subexpressions.
844934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
845934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      if (SubExpressionUseCounts.count(SubExprs[j])) {
846934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner        SubExprs.erase(SubExprs.begin()+j);
847934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner        --j; --e;
848934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      }
849934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
850934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Finally, the non-shared expressions together.
851934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (SubExprs.empty())
8521bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Uses[i].Base = Zero;
853934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    else
854934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      Uses[i].Base = SCEVAddExpr::get(SubExprs);
85527e5142309946ca12c633b265673af0c24684427Chris Lattner    SubExprs.clear();
856934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
8571bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
8581bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  return Result;
8591bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner}
8601bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
861d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng/// isZero - returns true if the scalar evolution expression is zero.
862d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng///
863d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Chengstatic bool isZero(SCEVHandle &V) {
864d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V))
865d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    return SC->getValue()->getRawValue() == 0;
866d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  return false;
867d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng}
868d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
8691bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
870eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng/// CheckForIVReuse - Returns the multiple if the stride is the multiple
871eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng/// of a previous stride and it is a legal value for the target addressing
872eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng/// mode scale component. This allows the users of this stride to be rewritten
87321495775e710d37003e100862cdc647cbdc3b999Evan Cheng/// as prev iv * factor. It returns 0 if no reuse is possible.
874eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Chengunsigned LoopStrengthReduce::CheckForIVReuse(const SCEVHandle &Stride,
87531e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng                                             IVExpr &IV, const Type *Ty) {
87621495775e710d37003e100862cdc647cbdc3b999Evan Cheng  if (!TLI) return 0;
877eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
878eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Stride)) {
87921495775e710d37003e100862cdc647cbdc3b999Evan Cheng    int64_t SInt = SC->getValue()->getSExtValue();
88021495775e710d37003e100862cdc647cbdc3b999Evan Cheng    if (SInt == 1) return 0;
881eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
882eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    for (TargetLowering::legal_am_scale_iterator
883eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng           I = TLI->legal_am_scale_begin(), E = TLI->legal_am_scale_end();
884eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng         I != E; ++I) {
885eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng      unsigned Scale = *I;
886ad2072643ac2ea2c13f991474e500b28a160bb46Reid Spencer      if (unsigned(abs(SInt)) < Scale || (SInt % Scale) != 0)
887eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        continue;
888eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng      std::map<SCEVHandle, IVsOfOneStride>::iterator SI =
889eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        IVsByStride.find(SCEVUnknown::getIntegerSCEV(SInt/Scale, Type::UIntTy));
890eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng      if (SI == IVsByStride.end())
891eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        continue;
892eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng      for (std::vector<IVExpr>::iterator II = SI->second.IVs.begin(),
893eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng             IE = SI->second.IVs.end(); II != IE; ++II)
894eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        // FIXME: Only handle base == 0 for now.
89531e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng        // Only reuse previous IV if it would not require a type conversion.
89631e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng        if (isZero(II->Base) &&
89731e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng            II->Base->getType()->isLosslesslyConvertibleTo(Ty)) {
898eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng          IV = *II;
899eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng          return Scale;
900eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        }
901eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    }
902eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  }
903eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
90421495775e710d37003e100862cdc647cbdc3b999Evan Cheng  return 0;
905eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng}
906eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
9077e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// PartitionByIsUseOfPostIncrementedValue - Simple boolean predicate that
9087e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// returns true if Val's isUseOfPostIncrementedValue is true.
9097e79b3898ddd919170d367a516f51296017146c2Chris Lattnerstatic bool PartitionByIsUseOfPostIncrementedValue(const BasedUser &Val) {
9107e79b3898ddd919170d367a516f51296017146c2Chris Lattner  return Val.isUseOfPostIncrementedValue;
9117e79b3898ddd919170d367a516f51296017146c2Chris Lattner}
912eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
913169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single
914169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// stride of IV.  All of the users may have different starting values, and this
915169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// may not be the only stride (we know it is if isOnlyStride is true).
91650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattnervoid LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
917ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                                      IVUsersOfOneStride &Uses,
918ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                                      Loop *L,
919169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman                                                      bool isOnlyStride) {
920169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Transform our list of users and offsets to a bit more complex table.  In
921a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // this new vector, each 'BasedUser' contains 'Base' the base of the
922a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // strided accessas well as the old information from Uses.  We progressively
923a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // move information from the Base field to the Imm field, until we eventually
924a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  // have the full access expression to rewrite the use.
925a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  std::vector<BasedUser> UsersToProcess;
926169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  UsersToProcess.reserve(Uses.Users.size());
927a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) {
928a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    UsersToProcess.push_back(Uses.Users[i]);
929a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
930a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // Move any loop invariant operands from the offset field to the immediate
931a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // field of the use, so that we don't try to use something before it is
932a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // computed.
933a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    MoveLoopVariantsToImediateField(UsersToProcess.back().Base,
934a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner                                    UsersToProcess.back().Imm, L);
935a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    assert(UsersToProcess.back().Base->isLoopInvariant(L) &&
93626d91f16464db56283087176a73981048331dd2dChris Lattner           "Base value is not loop invariant!");
9372461dff0700d0e34b9854d96a5cc03921b375525Chris Lattner  }
938eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
93931e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // We now have a whole bunch of uses of like-strided induction variables, but
94031e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // they might all have different bases.  We want to emit one PHI node for this
94131e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // stride which we fold as many common expressions (between the IVs) into as
94231e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // possible.  Start by identifying the common expressions in the base values
94331e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find
94431e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // "A+B"), emit it to the preheader, then remove the expression from the
94531e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // UsersToProcess base values.
94631e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  SCEVHandle CommonExprs =
94731e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng    RemoveCommonExpressionsFromUseBases(UsersToProcess);
94831e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng
949eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  // Check if it is possible to reuse a IV with stride that is factor of this
950eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  // stride. And the multiple is a number that can be encoded in the scale
951eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  // field of the target addressing mode.
952eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  PHINode *NewPHI = NULL;
953eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  Value   *IncV   = NULL;
95421495775e710d37003e100862cdc647cbdc3b999Evan Cheng  IVExpr   ReuseIV;
95531e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  unsigned RewriteFactor = CheckForIVReuse(Stride, ReuseIV,
95631e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng                                           CommonExprs->getType());
95721495775e710d37003e100862cdc647cbdc3b999Evan Cheng  if (RewriteFactor != 0) {
95821495775e710d37003e100862cdc647cbdc3b999Evan Cheng    DEBUG(std::cerr << "BASED ON IV of STRIDE " << *ReuseIV.Stride
95921495775e710d37003e100862cdc647cbdc3b999Evan Cheng          << " and BASE " << *ReuseIV.Base << " :\n");
960eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    NewPHI = ReuseIV.PHI;
961eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    IncV   = ReuseIV.IncV;
962eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  }
963eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
96444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  // Next, figure out what we can represent in the immediate fields of
96544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  // instructions.  If we can represent anything there, move it to the imm
9661bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // fields of the BasedUsers.  We do this so that it increases the commonality
9671bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // of the remaining uses.
96844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
96980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // If the user is not in the current loop, this means it is using the exit
97080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // value of the IV.  Do not put anything in the base, make sure it's all in
97180b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // the immediate field to allow as much factoring as possible.
97280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    if (!L->contains(UsersToProcess[i].Inst->getParent())) {
9738385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner      UsersToProcess[i].Imm = SCEVAddExpr::get(UsersToProcess[i].Imm,
9748385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner                                               UsersToProcess[i].Base);
9758385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner      UsersToProcess[i].Base =
9768385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner        SCEVUnknown::getIntegerSCEV(0, UsersToProcess[i].Base->getType());
97780b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    } else {
97880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner
97980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      // Addressing modes can be folded into loads and stores.  Be careful that
98080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      // the store is through the expression, not of the expression though.
98180b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      bool isAddress = isa<LoadInst>(UsersToProcess[i].Inst);
98280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].Inst))
98380b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner        if (SI->getOperand(1) == UsersToProcess[i].OperandValToReplace)
98480b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner          isAddress = true;
98580b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner
986d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      MoveImmediateValues(TLI, UsersToProcess[i].Base, UsersToProcess[i].Imm,
98780b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner                          isAddress, L);
98880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    }
98944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  }
990d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
9911bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Now that we know what we need to do, insert the PHI node itself.
9921bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  //
9931bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  DEBUG(std::cerr << "INSERTING IV of STRIDE " << *Stride << " and BASE "
9941bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        << *CommonExprs << " :\n");
995d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
9961bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVExpander Rewriter(*SE, *LI);
9971bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVExpander PreheaderRewriter(*SE, *LI);
9981bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
9991bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
10001bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Instruction *PreInsertPt = Preheader->getTerminator();
10011bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Instruction *PhiInsertBefore = L->getHeader()->begin();
10021bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
100312b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner  BasicBlock *LatchBlock = L->getLoopLatch();
1004d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
10051bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  const Type *ReplacedTy = CommonExprs->getType();
1006eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
1007eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  // Emit the initial base value into the loop preheader.
1008eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  Value *CommonBaseV
1009eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt,
1010eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng                                      ReplacedTy);
1011eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
101221495775e710d37003e100862cdc647cbdc3b999Evan Cheng  if (RewriteFactor == 0) {
1013d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    // Create a new Phi for this base, and stick it in the loop header.
1014d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore);
1015d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    ++NumInserted;
101644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
1017eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    // Add common base to the new Phi node.
1018eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    NewPHI->addIncoming(CommonBaseV, Preheader);
1019eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
1020d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    // Insert the stride into the preheader.
1021d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    Value *StrideV = PreheaderRewriter.expandCodeFor(Stride, PreInsertPt,
1022d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng                                                     ReplacedTy);
1023d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    if (!isa<ConstantInt>(StrideV)) ++NumVariable;
102450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
1025d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    // Emit the increment of the base value before the terminator of the loop
1026d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    // latch block, and add it to the Phi node.
1027d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    SCEVHandle IncExp = SCEVAddExpr::get(SCEVUnknown::get(NewPHI),
1028d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng                                         SCEVUnknown::get(StrideV));
10291bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
1030d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    IncV = Rewriter.expandCodeFor(IncExp, LatchBlock->getTerminator(),
1031d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng                                  ReplacedTy);
1032d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    IncV->setName(NewPHI->getName()+".inc");
1033d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    NewPHI->addIncoming(IncV, LatchBlock);
1034d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1035eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    // Remember this in case a later stride is multiple of this.
103621495775e710d37003e100862cdc647cbdc3b999Evan Cheng    IVsByStride[Stride].addIV(Stride, CommonExprs, NewPHI, IncV);
1037eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  } else {
1038eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    Constant *C = dyn_cast<Constant>(CommonBaseV);
1039eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    if (!C ||
1040eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        (!C->isNullValue() &&
1041eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng         !isTargetConstant(SCEVUnknown::get(CommonBaseV), TLI)))
1042eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng      // We want the common base emitted into the preheader!
1043eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng      CommonBaseV = new CastInst(CommonBaseV, CommonBaseV->getType(),
1044eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng                                 "commonbase", PreInsertPt);
1045d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  }
10461bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
10477e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // We want to emit code for users inside the loop first.  To do this, we
10487e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // rearrange BasedUser so that the entries at the end have
10497e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // isUseOfPostIncrementedValue = false, because we pop off the end of the
10507e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // vector (so we handle them first).
10517e79b3898ddd919170d367a516f51296017146c2Chris Lattner  std::partition(UsersToProcess.begin(), UsersToProcess.end(),
10527e79b3898ddd919170d367a516f51296017146c2Chris Lattner                 PartitionByIsUseOfPostIncrementedValue);
10537e79b3898ddd919170d367a516f51296017146c2Chris Lattner
10547e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // Sort this by base, so that things with the same base are handled
10557e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // together.  By partitioning first and stable-sorting later, we are
10567e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // guaranteed that within each base we will pop off users from within the
10577e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // loop before users outside of the loop with a particular base.
10587e79b3898ddd919170d367a516f51296017146c2Chris Lattner  //
10597e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // We would like to use stable_sort here, but we can't.  The problem is that
10607e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // SCEVHandle's don't have a deterministic ordering w.r.t to each other, so
10617e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // we don't have anything to do a '<' comparison on.  Because we think the
10627e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // number of uses is small, do a horrible bubble sort which just relies on
10637e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // ==.
10647e79b3898ddd919170d367a516f51296017146c2Chris Lattner  for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
10657e79b3898ddd919170d367a516f51296017146c2Chris Lattner    // Get a base value.
10667e79b3898ddd919170d367a516f51296017146c2Chris Lattner    SCEVHandle Base = UsersToProcess[i].Base;
10677e79b3898ddd919170d367a516f51296017146c2Chris Lattner
10687e79b3898ddd919170d367a516f51296017146c2Chris Lattner    // Compact everything with this base to be consequetive with this one.
10697e79b3898ddd919170d367a516f51296017146c2Chris Lattner    for (unsigned j = i+1; j != e; ++j) {
10707e79b3898ddd919170d367a516f51296017146c2Chris Lattner      if (UsersToProcess[j].Base == Base) {
10717e79b3898ddd919170d367a516f51296017146c2Chris Lattner        std::swap(UsersToProcess[i+1], UsersToProcess[j]);
10727e79b3898ddd919170d367a516f51296017146c2Chris Lattner        ++i;
10737e79b3898ddd919170d367a516f51296017146c2Chris Lattner      }
10747e79b3898ddd919170d367a516f51296017146c2Chris Lattner    }
10757e79b3898ddd919170d367a516f51296017146c2Chris Lattner  }
10767e79b3898ddd919170d367a516f51296017146c2Chris Lattner
10777e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // Process all the users now.  This outer loop handles all bases, the inner
10787e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // loop handles all users of a particular base.
1079169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  while (!UsersToProcess.empty()) {
10807b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    SCEVHandle Base = UsersToProcess.back().Base;
1081be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
10821bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    DEBUG(std::cerr << "  INSERTING code for BASE = " << *Base << ":\n");
1083be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
10841bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // Emit the code for Base into the preheader.
10855272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner    Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt,
10865272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner                                                   ReplacedTy);
10871bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
10881bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // If BaseV is a constant other than 0, make sure that it gets inserted into
10891bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // the preheader, instead of being forward substituted into the uses.  We do
10901bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // this by forcing a noop cast to be inserted into the preheader in this
10911bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // case.
10927e79b3898ddd919170d367a516f51296017146c2Chris Lattner    if (Constant *C = dyn_cast<Constant>(BaseV)) {
1093d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      if (!C->isNullValue() && !isTargetConstant(Base, TLI)) {
10941bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        // We want this constant emitted into the preheader!
10951bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        BaseV = new CastInst(BaseV, BaseV->getType(), "preheaderinsert",
10961bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner                             PreInsertPt);
10971bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      }
10987e79b3898ddd919170d367a516f51296017146c2Chris Lattner    }
10997e79b3898ddd919170d367a516f51296017146c2Chris Lattner
1100169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Emit the code to add the immediate offset to the Phi value, just before
11012351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    // the instructions that we identified as using this stride and base.
11027b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    do {
11037e79b3898ddd919170d367a516f51296017146c2Chris Lattner      // FIXME: Use emitted users to emit other users.
11047b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      BasedUser &User = UsersToProcess.back();
11052351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
1106010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // If this instruction wants to use the post-incremented value, move it
1107010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // after the post-inc and use its value instead of the PHI.
11081bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Value *RewriteOp = NewPHI;
1109010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (User.isUseOfPostIncrementedValue) {
11101bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        RewriteOp = IncV;
1111c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner
1112c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // If this user is in the loop, make sure it is the last thing in the
1113c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // loop to ensure it is dominated by the increment.
1114c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        if (L->contains(User.Inst->getParent()))
1115c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner          User.Inst->moveBefore(LatchBlock->getTerminator());
1116010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
111786c75d31133319a88216c1b1cd26a789e4023000Evan Cheng      if (RewriteOp->getType() != ReplacedTy)
111886c75d31133319a88216c1b1cd26a789e4023000Evan Cheng        RewriteOp = SCEVExpander::InsertCastOfTo(RewriteOp, ReplacedTy);
111986c75d31133319a88216c1b1cd26a789e4023000Evan Cheng
11201bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp);
11211bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
11221bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Clear the SCEVExpander's expression map so that we are guaranteed
11231bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // to have the code emitted where we expect it.
11241bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Rewriter.clear();
1125d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1126d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng      // If we are reusing the iv, then it must be multiplied by a constant
1127d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng      // factor take advantage of addressing mode scale component.
112821495775e710d37003e100862cdc647cbdc3b999Evan Cheng      if (RewriteFactor != 0) {
1129d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng        RewriteExpr =
1130d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng          SCEVMulExpr::get(SCEVUnknown::getIntegerSCEV(RewriteFactor,
1131eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng                                                       RewriteExpr->getType()),
1132eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng                           RewriteExpr);
1133eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
1134eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        // The common base is emitted in the loop preheader. But since we
1135eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        // are reusing an IV, it has not been used to initialize the PHI node.
1136eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        // Add it to the expression used to rewrite the uses.
1137eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        if (!isa<ConstantInt>(CommonBaseV) ||
1138eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng            !cast<ConstantInt>(CommonBaseV)->isNullValue())
1139eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng          RewriteExpr = SCEVAddExpr::get(RewriteExpr,
1140eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng                                         SCEVUnknown::get(CommonBaseV));
1141eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng      }
1142d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
11431bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Now that we know what we need to do, insert code before User for the
11441bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // immediate and any loop-variant expressions.
11451bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isNullValue())
11461bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        // Add BaseV to the PHI value if needed.
11471bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        RewriteExpr = SCEVAddExpr::get(RewriteExpr, SCEVUnknown::get(BaseV));
1148d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1149c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner      User.RewriteInstructionToUseNewBase(RewriteExpr, Rewriter, L, this);
11502351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
11512351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // Mark old value we replaced as possibly dead, so that it is elminated
11522351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // if we just replaced the last use of that value.
11532114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      DeadInsts.insert(cast<Instruction>(User.OperandValToReplace));
11542351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
11557b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      UsersToProcess.pop_back();
11562351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      ++NumReduced;
11577b445c521bc191d0d25799b289e17b45f202a1afChris Lattner
11587e79b3898ddd919170d367a516f51296017146c2Chris Lattner      // If there are any more users to process with the same base, process them
11597e79b3898ddd919170d367a516f51296017146c2Chris Lattner      // now.  We sorted by base above, so we just have to check the last elt.
11607b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    } while (!UsersToProcess.empty() && UsersToProcess.back().Base == Base);
1161169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // TODO: Next, find out which base index is the most common, pull it out.
1162169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
1163169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1164169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but
1165169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // different starting values, into different PHIs.
1166eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
1167eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1168010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar
1169010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// uses in the loop, look to see if we can eliminate some, in favor of using
1170010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// common indvars for the different uses.
1171010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattnervoid LoopStrengthReduce::OptimizeIndvars(Loop *L) {
1172010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // TODO: implement optzns here.
1173010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1174010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1175010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1176010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1177010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Finally, get the terminating condition for the loop if possible.  If we
1178010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // can, we want to change it to use a post-incremented version of its
117998d9811db263ee040d9a6a69ef9e1c4fdc8c219dChris Lattner  // induction variable, to allow coalescing the live ranges for the IV into
1180010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // one register value.
1181010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin());
1182010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
1183010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock *LatchBlock =
1184010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner   SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader);
1185010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator());
1186010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!TermBr || TermBr->isUnconditional() ||
1187010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      !isa<SetCondInst>(TermBr->getCondition()))
1188010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    return;
1189010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  SetCondInst *Cond = cast<SetCondInst>(TermBr->getCondition());
1190010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1191010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Search IVUsesByStride to find Cond's IVUse if there is one.
1192010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  IVStrideUse *CondUse = 0;
119350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  const SCEVHandle *CondStride = 0;
1194010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1195b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner  for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e && !CondUse;
1196b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner       ++Stride) {
1197b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
1198b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner      IVUsesByStride.find(StrideOrder[Stride]);
1199b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
1200b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner
1201b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner    for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(),
1202b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner           E = SI->second.Users.end(); UI != E; ++UI)
1203010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (UI->User == Cond) {
1204010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        CondUse = &*UI;
1205b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner        CondStride = &SI->first;
1206010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // NOTE: we could handle setcc instructions with multiple uses here, but
1207010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // InstCombine does it as well for simple uses, it's not clear that it
1208010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        // occurs enough in real life to handle.
1209010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        break;
1210010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
1211b4dd1b86fab8f5f60e06f54a7416c9d0f3e0016fChris Lattner  }
1212010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (!CondUse) return;  // setcc doesn't use the IV.
1213010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1214010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // setcc stride is complex, don't mess with users.
121550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  // FIXME: Evaluate whether this is a good idea or not.
121650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  if (!isa<SCEVConstant>(*CondStride)) return;
1217010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1218010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // It's possible for the setcc instruction to be anywhere in the loop, and
1219010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // possible for it to have multiple users.  If it is not immediately before
1220010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // the latch block branch, move it.
1221010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) {
1222010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    if (Cond->hasOneUse()) {   // Condition has a single use, just move it.
1223010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->moveBefore(TermBr);
1224010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    } else {
1225010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Otherwise, clone the terminating condition and insert into the loopend.
1226010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond = cast<SetCondInst>(Cond->clone());
1227010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->setName(L->getHeader()->getName() + ".termcond");
1228010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      LatchBlock->getInstList().insert(TermBr, Cond);
1229010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1230010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Clone the IVUse, as the old use still exists!
123150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner      IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond,
1232010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner                                         CondUse->OperandValToReplace);
123350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner      CondUse = &IVUsesByStride[*CondStride].Users.back();
1234010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    }
1235010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  }
1236010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1237010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // If we get to here, we know that we can transform the setcc instruction to
123898d9811db263ee040d9a6a69ef9e1c4fdc8c219dChris Lattner  // use the post-incremented version of the IV, allowing us to coalesce the
1239010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // live ranges for the IV correctly.
124050fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  CondUse->Offset = SCEV::getMinusSCEV(CondUse->Offset, *CondStride);
1241010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  CondUse->isUseOfPostIncrementedValue = true;
1242010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner}
1243169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
12444496a50deb95527508070eb649e9b56a02b588c3Evan Chengnamespace {
12454496a50deb95527508070eb649e9b56a02b588c3Evan Cheng  // Constant strides come first which in turns are sorted by their absolute
12464496a50deb95527508070eb649e9b56a02b588c3Evan Cheng  // values. If absolute values are the same, then positive strides comes first.
12474496a50deb95527508070eb649e9b56a02b588c3Evan Cheng  // e.g.
12484496a50deb95527508070eb649e9b56a02b588c3Evan Cheng  // 4, -1, X, 1, 2 ==> 1, -1, 2, 4, X
12494496a50deb95527508070eb649e9b56a02b588c3Evan Cheng  struct StrideCompare {
12504496a50deb95527508070eb649e9b56a02b588c3Evan Cheng    bool operator()(const SCEVHandle &LHS, const SCEVHandle &RHS) {
12514496a50deb95527508070eb649e9b56a02b588c3Evan Cheng      SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS);
12524496a50deb95527508070eb649e9b56a02b588c3Evan Cheng      SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS);
12534496a50deb95527508070eb649e9b56a02b588c3Evan Cheng      if (LHSC && RHSC) {
12544496a50deb95527508070eb649e9b56a02b588c3Evan Cheng        int64_t  LV = LHSC->getValue()->getSExtValue();
12554496a50deb95527508070eb649e9b56a02b588c3Evan Cheng        int64_t  RV = RHSC->getValue()->getSExtValue();
12564496a50deb95527508070eb649e9b56a02b588c3Evan Cheng        uint64_t ALV = (LV < 0) ? -LV : LV;
12574496a50deb95527508070eb649e9b56a02b588c3Evan Cheng        uint64_t ARV = (RV < 0) ? -RV : RV;
12584496a50deb95527508070eb649e9b56a02b588c3Evan Cheng        if (ALV == ARV)
12594496a50deb95527508070eb649e9b56a02b588c3Evan Cheng          return LV > RV;
12604496a50deb95527508070eb649e9b56a02b588c3Evan Cheng        else
12614496a50deb95527508070eb649e9b56a02b588c3Evan Cheng          return ALV < ARV;
1262035c6a23564a0ff4384a67d6e20d258a6a2dac95Chris Lattner      }
1263035c6a23564a0ff4384a67d6e20d258a6a2dac95Chris Lattner      return (LHSC && !RHSC);
12644496a50deb95527508070eb649e9b56a02b588c3Evan Cheng    }
12654496a50deb95527508070eb649e9b56a02b588c3Evan Cheng  };
12664496a50deb95527508070eb649e9b56a02b588c3Evan Cheng}
12674496a50deb95527508070eb649e9b56a02b588c3Evan Cheng
1268eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::runOnLoop(Loop *L) {
1269eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // First step, transform all loops nesting inside of this loop.
1270eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
1271eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    runOnLoop(*I);
1272eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1273169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Next, find all uses of induction variables in this loop, and catagorize
1274169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // them by stride.  Start by finding all of the PHI nodes in the header for
1275169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // this loop.  If they are induction variables, inspect their uses.
12763416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  std::set<Instruction*> Processed;   // Don't reprocess instructions.
1277169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
12783416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    AddUsersIfInteresting(I, L, Processed);
1279169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1280169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // If we have nothing to do, return.
1281010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (IVUsesByStride.empty()) return;
1282010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1283010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Optimize induction variables.  Some indvar uses can be transformed to use
1284010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // strides that will be needed for other purposes.  A common example of this
1285010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // is the exit test for the loop, which can often be rewritten to use the
1286010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // computation of some other indvar to decide when to terminate the loop.
1287010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  OptimizeIndvars(L);
1288010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
1289169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1290169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: We can widen subreg IV's here for RISC targets.  e.g. instead of
1291169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // doing computation in byte values, promote to 32-bit values if safe.
1292169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1293169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // FIXME: Attempt to reuse values across multiple IV's.  In particular, we
1294169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be
1295169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC.  Need
1296169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // to be careful that IV's are all the same type.  Only works for intptr_t
1297169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // indvars.
1298169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1299169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // If we only have one stride, we can more aggressively eliminate some things.
1300169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  bool HasOneStride = IVUsesByStride.size() == 1;
1301d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1302d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng#ifndef NDEBUG
1303d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  DEBUG(std::cerr << "\nLSR on ");
1304d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  DEBUG(L->dump());
1305d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng#endif
1306d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1307d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  // IVsByStride keeps IVs for one particular loop.
1308d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  IVsByStride.clear();
1309d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
13104496a50deb95527508070eb649e9b56a02b588c3Evan Cheng  // Sort the StrideOrder so we process larger strides first.
13114496a50deb95527508070eb649e9b56a02b588c3Evan Cheng  std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare());
13124496a50deb95527508070eb649e9b56a02b588c3Evan Cheng
13131bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Note: this processes each stride/type pair individually.  All users passed
13147305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // into StrengthReduceStridedIVUsers have the same type AND stride.  Also,
13157305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // node that we iterate over IVUsesByStride indirectly by using StrideOrder.
13167305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // This extra layer of indirection makes the ordering of strides deterministic
13177305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  // - not dependent on map order.
13187305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) {
13197305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
13207305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      IVUsesByStride.find(StrideOrder[Stride]);
13217305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
1322169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
13237305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  }
1324eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1325eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // Clean up after ourselves
1326eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  if (!DeadInsts.empty()) {
1327eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    DeleteTriviallyDeadInstructions(DeadInsts);
1328eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1329169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    BasicBlock::iterator I = L->getHeader()->begin();
1330169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    PHINode *PN;
1331e9100c69cbfbcc9298b663d80ef4ddf31d7bba69Chris Lattner    while ((PN = dyn_cast<PHINode>(I))) {
13321060e09fb207ed778581957671f8803d2df3a581Chris Lattner      ++I;  // Preincrement iterator to avoid invalidating it when deleting PN.
13331060e09fb207ed778581957671f8803d2df3a581Chris Lattner
133487265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // At this point, we know that we have killed one or more GEP
133587265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // instructions.  It is worth checking to see if the cann indvar is also
133687265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // dead, so that we can remove it as well.  The requirements for the cann
133787265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner      // indvar to be considered dead are:
1338169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 1. the cann indvar has one use
1339169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 2. the use is an add instruction
1340169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 3. the add has one use
1341169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // 4. the add is used by the cann indvar
1342169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // If all four cases above are true, then we can remove both the add and
1343169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // the cann indvar.
1344169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // FIXME: this needs to eliminate an induction variable even if it's being
1345169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // compared against some value to decide loop termination.
1346169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      if (PN->hasOneUse()) {
1347169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman        BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin()));
13487e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        if (BO && BO->hasOneUse()) {
13497e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner          if (PN == *(BO->use_begin())) {
13507e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            DeadInsts.insert(BO);
13517e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            // Break the cycle, then delete the PHI.
13527e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
135352d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner            SE->deleteInstructionFromRecords(PN);
13547e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner            PN->eraseFromParent();
1355eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman          }
13567e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        }
1357169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
1358eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
1359169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    DeleteTriviallyDeadInstructions(DeadInsts);
1360eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
1361169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
13629a59fbb89606aaefb27f6fe07dcb7c188bf1b3cdChris Lattner  CastedPointers.clear();
1363169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  IVUsesByStride.clear();
13647305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  StrideOrder.clear();
1365169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return;
1366eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
1367