LoopStrengthReduce.cpp revision 203af58aea3ae341d38e5c2c5b390b0c31d25557
1eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===//
2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman//
3eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//                     The LLVM Compiler Infrastructure
4eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//
54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source
64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// 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"
22e5b01bea7b9b7dce7c24484d2d915b0c118d4d07Dan Gohman#include "llvm/IntrinsicInst.h"
23eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Type.h"
242f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen#include "llvm/DerivedTypes.h"
25eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Analysis/Dominators.h"
26eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Analysis/LoopInfo.h"
270f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel#include "llvm/Analysis/LoopPass.h"
28169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Analysis/ScalarEvolutionExpander.h"
29eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Support/CFG.h"
30169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Support/GetElementPtrTypeIterator.h"
31e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h"
32eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Transforms/Utils/Local.h"
332f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen#include "llvm/Target/TargetData.h"
34168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng#include "llvm/ADT/SmallPtrSet.h"
35eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/ADT/Statistic.h"
36169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Support/Debug.h"
37a4f0b3a084d120cfc5b5bb06f64b222f5cb72740Chris Lattner#include "llvm/Support/Compiler.h"
38d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng#include "llvm/Target/TargetLowering.h"
39cfb1d4235fe3291028341e6acf4139723b4749e3Jeff Cohen#include <algorithm>
40eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include <set>
41eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanusing namespace llvm;
42eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
43cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan ChengSTATISTIC(NumReduced ,    "Number of GEPs strength reduced");
44cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan ChengSTATISTIC(NumInserted,    "Number of PHIs inserted");
45cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan ChengSTATISTIC(NumVariable,    "Number of PHIs with variable strides");
46541532724e29203e28c2fe0136cf6eabd49d4532Devang PatelSTATISTIC(NumEliminated,  "Number of strides eliminated");
47541532724e29203e28c2fe0136cf6eabd49d4532Devang PatelSTATISTIC(NumShadow,      "Number of Shadow IVs optimized");
48eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
490e5f499638c8d277b9dc4a4385712177c53b5681Chris Lattnernamespace {
50dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen
51c01a53007a4f4f9a601f1cc83ff4e2935405b905Jeff Cohen  struct BasedUser;
52dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen
53ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// IVStrideUse - Keep track of one use of a strided induction variable, where
54ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// the stride is stored externally.  The Offset member keeps track of the
559330c3af45453d0a3dd9caef40910a9a053e4de5Dan Gohman  /// offset from the IV, User is the actual user of the operand, and
569330c3af45453d0a3dd9caef40910a9a053e4de5Dan Gohman  /// 'OperandValToReplace' is the operand of the User that is the use.
579133fe28954d498fc4de13064c7d65bd811de02cReid Spencer  struct VISIBILITY_HIDDEN IVStrideUse {
58ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    SCEVHandle Offset;
59ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Instruction *User;
60ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
61010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
62010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
63010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
64010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
65c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // instruction for a loop or uses dominated by the loop.
66010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
67ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
68ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O)
69010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      : Offset(Offs), User(U), OperandValToReplace(O),
70010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner        isUseOfPostIncrementedValue(false) {}
71ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  };
72ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
73ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// IVUsersOfOneStride - This structure keeps track of all instructions that
74ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// have an operand that is based on the trip count multiplied by some stride.
75ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// The stride for all of these users is common and kept external to this
76ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner  /// structure.
779133fe28954d498fc4de13064c7d65bd811de02cReid Spencer  struct VISIBILITY_HIDDEN IVUsersOfOneStride {
78169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Users - Keep track of all of the users of this stride as well as the
79ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// initial value and the operand that uses the IV.
80ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    std::vector<IVStrideUse> Users;
81ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner
82ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) {
83ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner      Users.push_back(IVStrideUse(Offset, User, Operand));
84169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
85169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
86169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
87d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  /// IVInfo - This structure keeps track of one IV expression inserted during
8821495775e710d37003e100862cdc647cbdc3b999Evan Cheng  /// StrengthReduceStridedIVUsers. It contains the stride, the common base, as
8921495775e710d37003e100862cdc647cbdc3b999Evan Cheng  /// well as the PHI node and increment value created for rewrite.
909133fe28954d498fc4de13064c7d65bd811de02cReid Spencer  struct VISIBILITY_HIDDEN IVExpr {
9121495775e710d37003e100862cdc647cbdc3b999Evan Cheng    SCEVHandle  Stride;
92d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    SCEVHandle  Base;
93d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    PHINode    *PHI;
94d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    Value      *IncV;
95d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
9621495775e710d37003e100862cdc647cbdc3b999Evan Cheng    IVExpr(const SCEVHandle &stride, const SCEVHandle &base, PHINode *phi,
9721495775e710d37003e100862cdc647cbdc3b999Evan Cheng           Value *incv)
9821495775e710d37003e100862cdc647cbdc3b999Evan Cheng      : Stride(stride), Base(base), PHI(phi), IncV(incv) {}
99d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  };
100d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
101d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  /// IVsOfOneStride - This structure keeps track of all IV expression inserted
102d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  /// during StrengthReduceStridedIVUsers for a particular stride of the IV.
1039133fe28954d498fc4de13064c7d65bd811de02cReid Spencer  struct VISIBILITY_HIDDEN IVsOfOneStride {
104d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    std::vector<IVExpr> IVs;
105d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
10621495775e710d37003e100862cdc647cbdc3b999Evan Cheng    void addIV(const SCEVHandle &Stride, const SCEVHandle &Base, PHINode *PHI,
10721495775e710d37003e100862cdc647cbdc3b999Evan Cheng               Value *IncV) {
10821495775e710d37003e100862cdc647cbdc3b999Evan Cheng      IVs.push_back(IVExpr(Stride, Base, PHI, IncV));
109d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    }
110d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  };
111169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1120f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel  class VISIBILITY_HIDDEN LoopStrengthReduce : public LoopPass {
113eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    LoopInfo *LI;
114b7d9dfc7ba4ae1ae9482eee62b1912b40dc64f42Devang Patel    DominatorTree *DT;
115169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    ScalarEvolution *SE;
116169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    const TargetData *TD;
117169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    const Type *UIntPtrTy;
118eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    bool Changed;
1197e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner
120169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// IVUsesByStride - Keep track of all uses of induction variables that we
121169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// are interested in.  The key of the map is the stride of the access.
12250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    std::map<SCEVHandle, IVUsersOfOneStride> IVUsesByStride;
123169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
124d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    /// IVsByStride - Keep track of all IVs that have been inserted for a
125d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    /// particular stride.
126d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    std::map<SCEVHandle, IVsOfOneStride> IVsByStride;
127d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1287305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// StrideOrder - An ordering of the keys in IVUsesByStride that is stable:
1297305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// We use this to iterate over the IVUsesByStride collection without being
1307305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner    /// dependent on random ordering of pointers in the process.
1318392772727ed9105c92fe4514d53dab74c333edcEvan Cheng    SmallVector<SCEVHandle, 16> StrideOrder;
1327305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
13349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// CastedValues - As we need to cast values to uintptr_t, this keeps track
13449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// of the casted version of each value.  This is accessed by
13549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf.
1368392772727ed9105c92fe4514d53dab74c333edcEvan Cheng    DenseMap<Value*, Value*> CastedPointers;
137169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
138169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// DeadInsts - Keep track of instructions we may have made dead, so that
139169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// we can remove them after we are done working.
14009fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner    SmallVector<Instruction*, 16> DeadInsts;
141d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng
142d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    /// TLI - Keep a pointer of a TargetLowering to consult for determining
143d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    /// transformation profitability.
144d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng    const TargetLowering *TLI;
145d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng
146eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  public:
1471997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel    static char ID; // Pass ID, replacement for typeid
148c2bbfc18e9adbbdcf5b3375d8d25e2452f7df7f1Dan Gohman    explicit LoopStrengthReduce(const TargetLowering *tli = NULL) :
149ae73dc1448d25b02cabc7c64c86c64371453dda8Dan Gohman      LoopPass(&ID), TLI(tli) {
1502f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen    }
1512f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen
1520f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel    bool runOnLoop(Loop *L, LPPassManager &LPM);
153eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
154eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
155aa96ae780afa5475e62df284855a971216289212Chris Lattner      // We split critical edges, so we change the CFG.  However, we do update
156aa96ae780afa5475e62df284855a971216289212Chris Lattner      // many analyses if they are around.
157aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreservedID(LoopSimplifyID);
158aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<LoopInfo>();
159aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominanceFrontier>();
160aa96ae780afa5475e62df284855a971216289212Chris Lattner      AU.addPreserved<DominatorTree>();
161aa96ae780afa5475e62df284855a971216289212Chris Lattner
162f465db6c6a5a877aa791abfc3837d62c491dacd5Jeff Cohen      AU.addRequiredID(LoopSimplifyID);
163eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman      AU.addRequired<LoopInfo>();
164b7d9dfc7ba4ae1ae9482eee62b1912b40dc64f42Devang Patel      AU.addRequired<DominatorTree>();
1652f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen      AU.addRequired<TargetData>();
166169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      AU.addRequired<ScalarEvolution>();
167a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      AU.addPreserved<ScalarEvolution>();
168eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
16949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
17049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    /// getCastedVersionOf - Return the specified value casted to uintptr_t.
17149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner    ///
1723ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer    Value *getCastedVersionOf(Instruction::CastOps opcode, Value *V);
17349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattnerprivate:
1743416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    bool AddUsersIfInteresting(Instruction *I, Loop *L,
175168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng                               SmallPtrSet<Instruction*,16> &Processed);
1768480bc5b5b0d75c2cfa08d71c29dc98c20e5882cDan Gohman    SCEVHandle GetExpressionSCEV(Instruction *E);
177cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    ICmpInst *ChangeCompareStride(Loop *L, ICmpInst *Cond,
178cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng                                  IVStrideUse* &CondUse,
179cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng                                  const SCEVHandle* &CondStride);
180010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    void OptimizeIndvars(Loop *L);
181a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
182a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    /// OptimizeShadowIV - If IV is used in a int-to-float cast
183a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    /// inside the loop then try to eliminate the cast opeation.
184a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    void OptimizeShadowIV(Loop *L);
185a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
186ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    /// OptimizeSMax - Rewrite the loop's terminating condition
187ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    /// if it uses an smax computation.
188ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    ICmpInst *OptimizeSMax(Loop *L, ICmpInst *Cond,
189ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman                           IVStrideUse* &CondUse);
190ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
191c677de2713646ab6d8200cd71613f6b4ae9885fbDevang Patel    bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse,
192a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel                           const SCEVHandle *&CondStride);
1935f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng    bool RequiresTypeConversion(const Type *Ty, const Type *NewTy);
1942bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng    unsigned CheckForIVReuse(bool, bool, const SCEVHandle&,
19502e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman                             IVExpr&, const Type*,
196dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen                             const std::vector<BasedUser>& UsersToProcess);
19702e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman    bool ValidStride(bool, int64_t,
19802e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman                     const std::vector<BasedUser>& UsersToProcess);
1995f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng    SCEVHandle CollectIVUsers(const SCEVHandle &Stride,
2005f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                              IVUsersOfOneStride &Uses,
2015f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                              Loop *L,
2025f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                              bool &AllUsesAreAddresses,
2035f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                              std::vector<BasedUser> &UsersToProcess);
20450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner    void StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
20550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner                                      IVUsersOfOneStride &Uses,
206ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner                                      Loop *L, bool isOnlyStride);
207a68d4ca73e9cd0b19b2a48a2943e16cc0f89da27Chris Lattner    void DeleteTriviallyDeadInstructions();
208eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  };
209eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
210eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
211844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanchar LoopStrengthReduce::ID = 0;
212844731a7f1909f55935e3514c9e713a62d67662eDan Gohmanstatic RegisterPass<LoopStrengthReduce>
213844731a7f1909f55935e3514c9e713a62d67662eDan GohmanX("loop-reduce", "Loop Strength Reduction");
214844731a7f1909f55935e3514c9e713a62d67662eDan Gohman
215394f0441e06dafca29f0752cf400990a5b8fe4b1Daniel DunbarPass *llvm::createLoopStrengthReducePass(const TargetLowering *TLI) {
216d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  return new LoopStrengthReduce(TLI);
217eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
218eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
2194da49122f3f3c8da68a52723d846b88c72166a68Reid Spencer/// getCastedVersionOf - Return the specified value casted to uintptr_t. This
2204da49122f3f3c8da68a52723d846b88c72166a68Reid Spencer/// assumes that the Value* V is of integer or pointer type only.
22149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner///
2223ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid SpencerValue *LoopStrengthReduce::getCastedVersionOf(Instruction::CastOps opcode,
2233ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer                                              Value *V) {
22449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (V->getType() == UIntPtrTy) return V;
22549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (Constant *CB = dyn_cast<Constant>(V))
2263ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer    return ConstantExpr::getCast(opcode, CB, UIntPtrTy);
22749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
22849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  Value *&New = CastedPointers[V];
22949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner  if (New) return New;
23049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
2313ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer  New = SCEVExpander::InsertCastOfTo(opcode, V, UIntPtrTy);
23209fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  DeadInsts.push_back(cast<Instruction>(New));
2337db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return New;
23449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner}
23549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
23649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner
237eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// DeleteTriviallyDeadInstructions - If any of the instructions is the
238eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// specified set are trivially dead, delete them and see if this makes any of
239eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// their operands subsequently dead.
240a68d4ca73e9cd0b19b2a48a2943e16cc0f89da27Chris Lattnervoid LoopStrengthReduce::DeleteTriviallyDeadInstructions() {
24109fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  if (DeadInsts.empty()) return;
24209fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner
24309fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  // Sort the deadinsts list so that we can trivially eliminate duplicates as we
24409fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  // go.  The code below never adds a non-dead instruction to the worklist, but
24509fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  // callers may not be so careful.
24699d0015735f8e2aee1a4b99e39ffdaadc8a1dba8Chris Lattner  array_pod_sort(DeadInsts.begin(), DeadInsts.end());
24709fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner
24809fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  // Drop duplicate instructions and those with uses.
24909fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  for (unsigned i = 0, e = DeadInsts.size()-1; i < e; ++i) {
25009fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner    Instruction *I = DeadInsts[i];
25109fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner    if (!I->use_empty()) DeadInsts[i] = 0;
25209fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner    while (DeadInsts[i+1] == I && i != e)
25309fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner      DeadInsts[++i] = 0;
25409fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  }
25509fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner
256a68d4ca73e9cd0b19b2a48a2943e16cc0f89da27Chris Lattner  while (!DeadInsts.empty()) {
257a68d4ca73e9cd0b19b2a48a2943e16cc0f89da27Chris Lattner    Instruction *I = DeadInsts.back();
258a68d4ca73e9cd0b19b2a48a2943e16cc0f89da27Chris Lattner    DeadInsts.pop_back();
25909fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner
26009fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner    if (I == 0 || !isInstructionTriviallyDead(I))
261bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner      continue;
262bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner
263bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner    SE->deleteValueFromRecords(I);
264411052bb96fb4a30035de6cafa492fa4f598e824Bill Wendling
26509fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner    for (User::op_iterator OI = I->op_begin(), E = I->op_end(); OI != E; ++OI) {
26609fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner      if (Instruction *U = dyn_cast<Instruction>(*OI)) {
26709fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner        *OI = 0;
268bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner        if (U->use_empty())
26909fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner          DeadInsts.push_back(U);
270bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner      }
271eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
272bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner
273bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner    I->eraseFromParent();
274bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner    Changed = true;
275eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
276eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
277eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
278169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
2793416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// GetExpressionSCEV - Compute and return the SCEV for the specified
2803416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// instruction.
2818480bc5b5b0d75c2cfa08d71c29dc98c20e5882cDan GohmanSCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp) {
282da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen  // Pointer to pointer bitcast instructions return the same value as their
283da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen  // operand.
284da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen  if (BitCastInst *BCI = dyn_cast<BitCastInst>(Exp)) {
285da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen    if (SE->hasSCEV(BCI) || !isa<Instruction>(BCI->getOperand(0)))
286da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen      return SE->getSCEV(BCI);
2878480bc5b5b0d75c2cfa08d71c29dc98c20e5882cDan Gohman    SCEVHandle R = GetExpressionSCEV(cast<Instruction>(BCI->getOperand(0)));
288da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen    SE->setSCEV(BCI, R);
289da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen    return R;
290da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen  }
291da91f49751b9b55a41d315b9fbdbb14614587b03Dale Johannesen
29287265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions.
29387265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // If this is a GEP that SE doesn't know about, compute it now and insert it.
29487265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // If this is not a GEP, or if we have already done this computation, just let
29587265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  // SE figure it out.
2963416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp);
29787265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  if (!GEP || SE->hasSCEV(GEP))
2983416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return SE->getSCEV(Exp);
2993416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
300169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Analyze all of the subscripts of this getelementptr instruction, looking
3018480bc5b5b0d75c2cfa08d71c29dc98c20e5882cDan Gohman  // for uses that are determined by the trip count of the loop.  First, skip
3028480bc5b5b0d75c2cfa08d71c29dc98c20e5882cDan Gohman  // all operands the are not dependent on the IV.
303169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
304169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // Build up the base expression.  Insert an LLVM cast of the pointer to
305169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // uintptr_t first.
306246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  SCEVHandle GEPVal = SE->getUnknown(
3073ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer      getCastedVersionOf(Instruction::PtrToInt, GEP->getOperand(0)));
308169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
309169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  gep_type_iterator GTI = gep_type_begin(GEP);
3103416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
3116725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif  for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
3126725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif       i != e; ++i, ++GTI) {
313169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is a use of a recurrence that we can analyze, and it comes before
314169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Op does in the GEP operand list, we will handle this when we process this
315169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // operand.
316169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
317169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      const StructLayout *SL = TD->getStructLayout(STy);
3186725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif      unsigned Idx = cast<ConstantInt>(*i)->getZExtValue();
319b1919e2f08ecb37140af676fd2916f8d5ed7df3dChris Lattner      uint64_t Offset = SL->getElementOffset(Idx);
320246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      GEPVal = SE->getAddExpr(GEPVal,
321246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                             SE->getIntegerSCEV(Offset, UIntPtrTy));
3222f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    } else {
3233ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer      unsigned GEPOpiBits =
3246725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif        (*i)->getType()->getPrimitiveSizeInBits();
3253ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer      unsigned IntPtrBits = UIntPtrTy->getPrimitiveSizeInBits();
3263ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer      Instruction::CastOps opcode = (GEPOpiBits < IntPtrBits ?
3273ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer          Instruction::SExt : (GEPOpiBits > IntPtrBits ? Instruction::Trunc :
3283ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer            Instruction::BitCast));
3296725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif      Value *OpVal = getCastedVersionOf(opcode, *i);
3307db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      SCEVHandle Idx = SE->getSCEV(OpVal);
3317db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
332a7ac2bd4076096bb3a9986dd5a44f20c7f715518Dale Johannesen      uint64_t TypeSize = TD->getABITypeSize(GTI.getIndexedType());
3333416e5f645186a7e3321f927eab662d0ecef404bChris Lattner      if (TypeSize != 1)
334246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman        Idx = SE->getMulExpr(Idx,
335246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                            SE->getConstant(ConstantInt::get(UIntPtrTy,
336246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                                             TypeSize)));
337246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      GEPVal = SE->getAddExpr(GEPVal, Idx);
3382f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner    }
339eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
340169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
34187265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner  SE->setSCEV(GEP, GEPVal);
3423416e5f645186a7e3321f927eab662d0ecef404bChris Lattner  return GEPVal;
343169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
344169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
3457db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// getSCEVStartAndStride - Compute the start and stride of this expression,
3467db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// returning false if the expression is not a start/stride pair, or true if it
3477db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// is.  The stride must be a loop invariant expression, but the start may be
3487db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// a mix of loop invariant and loop variant expressions.
3497db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattnerstatic bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
350246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                  SCEVHandle &Start, SCEVHandle &Stride,
351246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                  ScalarEvolution *SE) {
3527db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVHandle TheAddRec = Start;   // Initialize to zero.
3537db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3547db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // If the outer level is an AddExpr, the operands are all start values except
3557db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // for a nested AddRecExpr.
3567db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
3577db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
3587db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      if (SCEVAddRecExpr *AddRec =
3597db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner             dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
3607db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        if (AddRec->getLoop() == L)
361246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman          TheAddRec = SE->getAddExpr(AddRec, TheAddRec);
3627db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        else
3637db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner          return false;  // Nested IV of some sort?
3647db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      } else {
365246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman        Start = SE->getAddExpr(Start, AE->getOperand(i));
3667db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      }
3677db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3683ed469ccd7b028a030b550d84b7336d146f5d8faReid Spencer  } else if (isa<SCEVAddRecExpr>(SH)) {
3697db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    TheAddRec = SH;
3707db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  } else {
3717db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // not analyzable.
3727db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  }
3737db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3747db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
3757db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec || AddRec->getLoop() != L) return false;
3767db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3777db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // FIXME: Generalize to non-affine IV's.
3787db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!AddRec->isAffine()) return false;
3797db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
380246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  Start = SE->getAddExpr(Start, AddRec->getOperand(0));
3817db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3827db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (!isa<SCEVConstant>(AddRec->getOperand(1)))
383b7427031372337e6d67f9573ec6c722ab5ea913eBill Wendling    DOUT << "[" << L->getHeader()->getName()
384b7427031372337e6d67f9573ec6c722ab5ea913eBill Wendling         << "] Variable stride: " << *AddRec << "\n";
38550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
38650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  Stride = AddRec->getOperand(1);
3877db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  return true;
3887db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner}
3897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
3900ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression
3910ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// and now we need to decide whether the user should use the preinc or post-inc
3920ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// value.  If this user should use the post-inc version of the IV, return true.
3930ae33eb243417982fbdca792460bdd986108ac09Chris Lattner///
3940ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// Choosing wrong here can break dominance properties (if we choose to use the
3950ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// post-inc value when we cannot) or it can end up adding extra live-ranges to
3960ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we
3970ae33eb243417982fbdca792460bdd986108ac09Chris Lattner/// should use the post-inc value).
3980ae33eb243417982fbdca792460bdd986108ac09Chris Lattnerstatic bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV,
3990e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng                                       Loop *L, DominatorTree *DT, Pass *P,
40009fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner                                      SmallVectorImpl<Instruction*> &DeadInsts){
4010ae33eb243417982fbdca792460bdd986108ac09Chris Lattner  // If the user is in the loop, use the preinc value.
4020ae33eb243417982fbdca792460bdd986108ac09Chris Lattner  if (L->contains(User->getParent())) return false;
4030ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
4045e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  BasicBlock *LatchBlock = L->getLoopLatch();
4055e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
4065e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Ok, the user is outside of the loop.  If it is dominated by the latch
4075e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // block, use the post-inc value.
408b7d9dfc7ba4ae1ae9482eee62b1912b40dc64f42Devang Patel  if (DT->dominates(LatchBlock, User->getParent()))
4095e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    return true;
4105e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
4115e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // There is one case we have to be careful of: PHI nodes.  These little guys
4125e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // can live in blocks that do not dominate the latch block, but (since their
4135e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // uses occur in the predecessor block, not the block the PHI lives in) should
4145e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // still use the post-inc value.  Check for this case now.
4155e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  PHINode *PN = dyn_cast<PHINode>(User);
4165e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  if (!PN) return false;  // not a phi, not dominated by latch block.
4175e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
4185e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Look at all of the uses of IV by the PHI node.  If any use corresponds to
4195e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // a block that is not dominated by the latch block, give up and use the
4205e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // preincremented value.
4215e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  unsigned NumUses = 0;
4225e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
4235e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    if (PN->getIncomingValue(i) == IV) {
4245e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      ++NumUses;
425b7d9dfc7ba4ae1ae9482eee62b1912b40dc64f42Devang Patel      if (!DT->dominates(LatchBlock, PN->getIncomingBlock(i)))
4265e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner        return false;
4275e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    }
4285e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
4295e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // Okay, all uses of IV by PN are in predecessor blocks that really are
4305e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // dominated by the latch block.  Split the critical edges and use the
4315e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  // post-incremented value.
4325e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
4335e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    if (PN->getIncomingValue(i) == IV) {
4348392772727ed9105c92fe4514d53dab74c333edcEvan Cheng      SplitCriticalEdge(PN->getIncomingBlock(i), PN->getParent(), P, false);
4351b9c8e73b5d620cf2d9a8e150b179fe95ddb8c4eChris Lattner      // Splitting the critical edge can reduce the number of entries in this
4361b9c8e73b5d620cf2d9a8e150b179fe95ddb8c4eChris Lattner      // PHI.
4371b9c8e73b5d620cf2d9a8e150b179fe95ddb8c4eChris Lattner      e = PN->getNumIncomingValues();
4385e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner      if (--NumUses == 0) break;
4395e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner    }
4400e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng
4410e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng  // PHI node might have become a constant value after SplitCriticalEdge.
44209fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  DeadInsts.push_back(User);
4435e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner
4445e8ca66914fc9da9848cf6b1a29dd5075f4c721fChris Lattner  return true;
4450ae33eb243417982fbdca792460bdd986108ac09Chris Lattner}
4460ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
447203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// isAddress - Returns true if the specified instruction is using the
448203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// specified value as an address.
449203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesenstatic bool isAddressUse(Instruction *Inst, Value *OperandVal) {
450203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  bool isAddress = isa<LoadInst>(Inst);
451203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
452203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    if (SI->getOperand(1) == OperandVal)
453203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      isAddress = true;
454203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  } else if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {
455203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // Addressing modes can also be folded into prefetches and a variety
456203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // of intrinsics.
457203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    switch (II->getIntrinsicID()) {
458203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      default: break;
459203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      case Intrinsic::prefetch:
460203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      case Intrinsic::x86_sse2_loadu_dq:
461203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      case Intrinsic::x86_sse2_loadu_pd:
462203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      case Intrinsic::x86_sse_loadu_ps:
463203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      case Intrinsic::x86_sse_storeu_ps:
464203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      case Intrinsic::x86_sse2_storeu_pd:
465203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      case Intrinsic::x86_sse2_storeu_dq:
466203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      case Intrinsic::x86_sse2_storel_dq:
467203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        if (II->getOperand(1) == OperandVal)
468203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen          isAddress = true;
469203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        break;
470203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    }
471203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  }
472203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  return isAddress;
473203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen}
4740ae33eb243417982fbdca792460bdd986108ac09Chris Lattner
475169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// AddUsersIfInteresting - Inspect the specified instruction.  If it is a
476169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// reducible SCEV, recursively add its users to the IVUsesByStride set and
477169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// return true.  Otherwise, return false.
4783416e5f645186a7e3321f927eab662d0ecef404bChris Lattnerbool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L,
479168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng                                      SmallPtrSet<Instruction*,16> &Processed) {
48042a75517250017a52afb03a0ade03cbd49559fe5Chris Lattner  if (!I->getType()->isInteger() && !isa<PointerType>(I->getType()))
4814a9a3e53746e3cc752d8a242ddc887a106cf5021Dan Gohman    return false;   // Void and FP expressions cannot be reduced.
482168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng  if (!Processed.insert(I))
4833416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    return true;    // Instruction already handled.
4843416e5f645186a7e3321f927eab662d0ecef404bChris Lattner
4857db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the symbolic expression for this instruction.
4868480bc5b5b0d75c2cfa08d71c29dc98c20e5882cDan Gohman  SCEVHandle ISE = GetExpressionSCEV(I);
4877db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  if (isa<SCEVCouldNotCompute>(ISE)) return false;
4887db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner
4897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner  // Get the start and stride for this expression.
490246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  SCEVHandle Start = SE->getIntegerSCEV(0, ISE->getType());
49150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  SCEVHandle Stride = Start;
492246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  if (!getSCEVStartAndStride(ISE, L, Start, Stride, SE))
4937db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    return false;  // Non-reducible symbolic expression, bail out.
4944fe26582c09ec19873753cb4e9bb2ac3cdace88aDevang Patel
4952a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel  std::vector<Instruction *> IUsers;
4962a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel  // Collect all I uses now because IVUseShouldUsePostIncValue may
4972a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel  // invalidate use_iterator.
4982a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel  for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
4992a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel    IUsers.push_back(cast<Instruction>(*UI));
500169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
5012a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel  for (unsigned iused_index = 0, iused_size = IUsers.size();
5022a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel       iused_index != iused_size; ++iused_index) {
5032a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel
5042a5fa189972c1ecc29f3682a732d15f94b9498f1Devang Patel    Instruction *User = IUsers[iused_index];
5054fe26582c09ec19873753cb4e9bb2ac3cdace88aDevang Patel
506169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Do not infinitely recurse on PHI nodes.
507396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner    if (isa<PHINode>(User) && Processed.count(User))
508169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      continue;
509169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
510169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // If this is an instruction defined in a nested loop, or outside this loop,
511f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    // don't recurse into it.
5127db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    bool AddUserToIVUsers = false;
513f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner    if (LI->getLoopFor(User->getParent()) != L) {
514b7427031372337e6d67f9573ec6c722ab5ea913eBill Wendling      DOUT << "FOUND USER in other loop: " << *User
515b7427031372337e6d67f9573ec6c722ab5ea913eBill Wendling           << "   OF SCEV: " << *ISE << "\n";
5167db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
5173416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    } else if (!AddUsersIfInteresting(User, L, Processed)) {
518b7427031372337e6d67f9573ec6c722ab5ea913eBill Wendling      DOUT << "FOUND USER: " << *User
519b7427031372337e6d67f9573ec6c722ab5ea913eBill Wendling           << "   OF SCEV: " << *ISE << "\n";
5207db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner      AddUserToIVUsers = true;
5217db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    }
522fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman
5237db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner    if (AddUserToIVUsers) {
5247305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      IVUsersOfOneStride &StrideUses = IVUsesByStride[Stride];
5257305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner      if (StrideUses.Users.empty())     // First occurance of this stride?
5267305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideOrder.push_back(Stride);
5277305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner
528a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner      // Okay, we found a user that we cannot reduce.  Analyze the instruction
529c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      // and decide what to do with it.  If we are a use inside of the loop, use
530c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      // the value before incrementation, otherwise use it after incrementation.
5310e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng      if (IVUseShouldUsePostIncValue(User, I, L, DT, this, DeadInsts)) {
532c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // The value used will be incremented by the stride more than we are
533c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // expecting, so subtract this off.
534246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman        SCEVHandle NewStart = SE->getMinusSCEV(Start, Stride);
5357305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.addUser(NewStart, User, I);
5367305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.Users.back().isUseOfPostIncrementedValue = true;
537b7427031372337e6d67f9573ec6c722ab5ea913eBill Wendling        DOUT << "   USING POSTINC SCEV, START=" << *NewStart<< "\n";
5380ae33eb243417982fbdca792460bdd986108ac09Chris Lattner      } else {
5397305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner        StrideUses.addUser(Start, User, I);
540c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner      }
541169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    }
542169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
543169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return true;
544169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
545169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
546169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemannamespace {
547169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// BasedUser - For a particular base value, keep information about how we've
548169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  /// partitioned the expression so far.
549169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  struct BasedUser {
550246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    /// SE - The current ScalarEvolution object.
551246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    ScalarEvolution *SE;
552246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman
553a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// Base - The Base value for the PHI node that needs to be inserted for
554a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// this use.  As the use is processed, information gets moved from this
555a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// field to the Imm field (below).  BasedUser values are sorted by this
556a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    /// field.
557a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    SCEVHandle Base;
558a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
559169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Inst - The instruction using the induction variable.
560169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    Instruction *Inst;
561169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
562ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// OperandValToReplace - The operand value of Inst to replace with the
563ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    /// EmittedBase.
564ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner    Value *OperandValToReplace;
565169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
566169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// Imm - The immediate value that should be added to the base immediately
567169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// before Inst, because it will be folded into the imm field of the
568169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    /// instruction.
569169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    SCEVHandle Imm;
570169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
571010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // isUseOfPostIncrementedValue - True if this should use the
572010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // post-incremented version of this IV, not the preincremented version.
573010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    // This can only be set in special cases, such as the terminating setcc
574c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // instruction for a loop and uses outside the loop that are dominated by
575c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner    // the loop.
576010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    bool isUseOfPostIncrementedValue;
577a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
578246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    BasedUser(IVStrideUse &IVSU, ScalarEvolution *se)
579246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      : SE(se), Base(IVSU.Offset), Inst(IVSU.User),
580a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        OperandValToReplace(IVSU.OperandValToReplace),
581308f24d4525a6365f8d65ba821786189c080c0ceDale Johannesen        Imm(SE->getIntegerSCEV(0, Base->getType())),
582a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner        isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {}
583169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
5842114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Once we rewrite the code to insert the new IVs we want, update the
5852114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // operands of Inst to use the new expression 'NewBase', with 'Imm' added
5862114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // to it.
5871bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
588f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman                                        Instruction *InsertPt,
5890e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng                                       SCEVExpander &Rewriter, Loop *L, Pass *P,
59009fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner                                      SmallVectorImpl<Instruction*> &DeadInsts);
591221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
592221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    Value *InsertCodeForBaseAtPosition(const SCEVHandle &NewBase,
593221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                       SCEVExpander &Rewriter,
594221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                       Instruction *IP, Loop *L);
595169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    void dump() const;
596169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  };
597169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
598169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
599169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanvoid BasedUser::dump() const {
600e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling  cerr << " Base=" << *Base;
601e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling  cerr << " Imm=" << *Imm;
602e81561909d128c6e2d8033cb5465a49b2596b26aBill Wendling  cerr << "   Inst: " << *Inst;
603169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
604169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
605221fc3c6d69bd3854e9121f51e3283492c222ab7Chris LattnerValue *BasedUser::InsertCodeForBaseAtPosition(const SCEVHandle &NewBase,
606221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                              SCEVExpander &Rewriter,
607221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner                                              Instruction *IP, Loop *L) {
608221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // Figure out where we *really* want to insert this code.  In particular, if
609221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // the user is inside of a loop that is nested inside of L, we really don't
610221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // want to insert this expression before the user, we'd rather pull it out as
611221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // many loops as possible.
612221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  LoopInfo &LI = Rewriter.getLoopInfo();
613221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  Instruction *BaseInsertPt = IP;
614221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
615221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // Figure out the most-nested loop that IP is in.
616221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  Loop *InsertLoop = LI.getLoopFor(IP->getParent());
617221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
618221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // If InsertLoop is not L, and InsertLoop is nested inside of L, figure out
619221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // the preheader of the outer-most loop where NewBase is not loop invariant.
620eccdd08d4c1a81ede221bbccf3045fe6f11e1003Dale Johannesen  if (L->contains(IP->getParent()))
621eccdd08d4c1a81ede221bbccf3045fe6f11e1003Dale Johannesen    while (InsertLoop && NewBase->isLoopInvariant(InsertLoop)) {
622eccdd08d4c1a81ede221bbccf3045fe6f11e1003Dale Johannesen      BaseInsertPt = InsertLoop->getLoopPreheader()->getTerminator();
623eccdd08d4c1a81ede221bbccf3045fe6f11e1003Dale Johannesen      InsertLoop = InsertLoop->getParentLoop();
624eccdd08d4c1a81ede221bbccf3045fe6f11e1003Dale Johannesen    }
625221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
626221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // If there is no immediate value, skip the next part.
627cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman  if (Imm->isZero())
628cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman    return Rewriter.expandCodeFor(NewBase, BaseInsertPt);
629221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
630221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  Value *Base = Rewriter.expandCodeFor(NewBase, BaseInsertPt);
631b47f6124f4893c00573d1a5be56a6e4cf877faebChris Lattner
632b47f6124f4893c00573d1a5be56a6e4cf877faebChris Lattner  // If we are inserting the base and imm values in the same block, make sure to
633b47f6124f4893c00573d1a5be56a6e4cf877faebChris Lattner  // adjust the IP position if insertion reused a result.
634b47f6124f4893c00573d1a5be56a6e4cf877faebChris Lattner  if (IP == BaseInsertPt)
635b47f6124f4893c00573d1a5be56a6e4cf877faebChris Lattner    IP = Rewriter.getInsertionPoint();
636221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
637221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  // Always emit the immediate (if non-zero) into the same block as the user.
638246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  SCEVHandle NewValSCEV = SE->getAddExpr(SE->getUnknown(Base), Imm);
639d19534add90a2a894af61523b830887097bb780bDan Gohman  return Rewriter.expandCodeFor(NewValSCEV, IP);
640b47f6124f4893c00573d1a5be56a6e4cf877faebChris Lattner
641221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner}
642221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
643221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
6442114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// Once we rewrite the code to insert the new IVs we want, update the
6452114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// operands of Inst to use the new expression 'NewBase', with 'Imm' added
646f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman// to it. NewBasePt is the last instruction which contributes to the
647f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman// value of NewBase in the case that it's a diffferent instruction from
648f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman// the PHI that NewBase is computed from, or null otherwise.
649f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman//
6501bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnervoid BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
651f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman                                               Instruction *NewBasePt,
6520e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng                                      SCEVExpander &Rewriter, Loop *L, Pass *P,
65309fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner                                      SmallVectorImpl<Instruction*> &DeadInsts){
6542114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  if (!isa<PHINode>(Inst)) {
655c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // By default, insert code at the user instruction.
656c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    BasicBlock::iterator InsertPt = Inst;
657c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner
658c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // However, if the Operand is itself an instruction, the (potentially
659c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // complex) inserted code may be shared by many users.  Because of this, we
660c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // want to emit code for the computation of the operand right before its old
661c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // computation.  This is usually safe, because we obviously used to use the
662c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // computation when it was computed in its current block.  However, in some
663c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // cases (e.g. use of a post-incremented induction variable) the NewBase
664c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // value will be pinned to live somewhere after the original computation.
665c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    // In this case, we have to back off.
666f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    //
667f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // If this is a use outside the loop (which means after, since it is based
668f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // on a loop indvar) we use the post-incremented value, so that we don't
669f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // artificially make the preinc value live out the bottom of the loop.
670589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen    if (!isUseOfPostIncrementedValue && L->contains(Inst->getParent())) {
671ca756ae886f1d39053ffd049123fdcc7e5c2aed3Dan Gohman      if (NewBasePt && isa<PHINode>(OperandValToReplace)) {
672f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman        InsertPt = NewBasePt;
673f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman        ++InsertPt;
6746725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif      } else if (Instruction *OpInst
6756725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif                 = dyn_cast<Instruction>(OperandValToReplace)) {
676c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner        InsertPt = OpInst;
677c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner        while (isa<PHINode>(InsertPt)) ++InsertPt;
678c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner      }
679c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    }
680c5494af8a90f398046c45bc2b7549ab9004c01d9Chris Lattner    Value *NewVal = InsertCodeForBaseAtPosition(NewBase, Rewriter, InsertPt, L);
681a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman    // Adjust the type back to match the Inst. Note that we can't use InsertPt
682a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman    // here because the SCEVExpander may have inserted the instructions after
683a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman    // that point, in its efforts to avoid inserting redundant expressions.
684d19534add90a2a894af61523b830887097bb780bDan Gohman    if (isa<PointerType>(OperandValToReplace->getType())) {
685a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman      NewVal = SCEVExpander::InsertCastOfTo(Instruction::IntToPtr,
686a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman                                            NewVal,
687a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman                                            OperandValToReplace->getType());
688d19534add90a2a894af61523b830887097bb780bDan Gohman    }
6892114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    // Replace the use of the operand Value with the new Phi we just created.
6902114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
6917d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner    DOUT << "    CHANGED: IMM =" << *Imm;
6927d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner    DOUT << "  \tNEWBASE =" << *NewBase;
6937d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner    DOUT << "  \tInst = " << *Inst;
6942114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    return;
6952114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
6962114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
6972114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  // PHI nodes are more complex.  We have to insert one copy of the NewBase+Imm
698c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // expression into each operand block that uses it.  Note that PHI nodes can
699c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // have multiple entries for the same predecessor.  We use a map to make sure
700c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // that a PHI node only has a single Value* for each predecessor (which also
701c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner  // prevents us from inserting duplicate code in some blocks).
7028392772727ed9105c92fe4514d53dab74c333edcEvan Cheng  DenseMap<BasicBlock*, Value*> InsertedCode;
7032114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  PHINode *PN = cast<PHINode>(Inst);
7042114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
7052114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    if (PN->getIncomingValue(i) == OperandValToReplace) {
706e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner      // If this is a critical edge, split the edge so that we do not insert the
707396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // code on all predecessor/successor paths.  We do this unless this is the
708396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // canonical backedge for this loop, as this can make some inserted code
709396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner      // be in an illegal position.
71037edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner      BasicBlock *PHIPred = PN->getIncomingBlock(i);
71137edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner      if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 &&
71237edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner          (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) {
71337edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner
714aa96ae780afa5475e62df284855a971216289212Chris Lattner        // First step, split the critical edge.
7158392772727ed9105c92fe4514d53dab74c333edcEvan Cheng        SplitCriticalEdge(PHIPred, PN->getParent(), P, false);
716c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner
717aa96ae780afa5475e62df284855a971216289212Chris Lattner        // Next step: move the basic block.  In particular, if the PHI node
718aa96ae780afa5475e62df284855a971216289212Chris Lattner        // is outside of the loop, and PredTI is in the loop, we want to
719aa96ae780afa5475e62df284855a971216289212Chris Lattner        // move the block to be immediately before the PHI block, not
720aa96ae780afa5475e62df284855a971216289212Chris Lattner        // immediately after PredTI.
72137edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner        if (L->contains(PHIPred) && !L->contains(PN->getParent())) {
722aa96ae780afa5475e62df284855a971216289212Chris Lattner          BasicBlock *NewBB = PN->getIncomingBlock(i);
723aa96ae780afa5475e62df284855a971216289212Chris Lattner          NewBB->moveBefore(PN->getParent());
724e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner        }
7251b9c8e73b5d620cf2d9a8e150b179fe95ddb8c4eChris Lattner
7261b9c8e73b5d620cf2d9a8e150b179fe95ddb8c4eChris Lattner        // Splitting the edge can reduce the number of PHI entries we have.
7271b9c8e73b5d620cf2d9a8e150b179fe95ddb8c4eChris Lattner        e = PN->getNumIncomingValues();
728e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner      }
7292114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
730c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      Value *&Code = InsertedCode[PN->getIncomingBlock(i)];
731c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      if (!Code) {
732c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner        // Insert the code into the end of the predecessor block.
733221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        Instruction *InsertPt = PN->getIncomingBlock(i)->getTerminator();
734221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        Code = InsertCodeForBaseAtPosition(NewBase, Rewriter, InsertPt, L);
735d19534add90a2a894af61523b830887097bb780bDan Gohman
736684b22df79c51114a12289e10a4063d5f02259a9Chris Lattner        // Adjust the type back to match the PHI. Note that we can't use
737684b22df79c51114a12289e10a4063d5f02259a9Chris Lattner        // InsertPt here because the SCEVExpander may have inserted its
738684b22df79c51114a12289e10a4063d5f02259a9Chris Lattner        // instructions after that point, in its efforts to avoid inserting
739684b22df79c51114a12289e10a4063d5f02259a9Chris Lattner        // redundant expressions.
740d19534add90a2a894af61523b830887097bb780bDan Gohman        if (isa<PointerType>(PN->getType())) {
741a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman          Code = SCEVExpander::InsertCastOfTo(Instruction::IntToPtr,
742a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman                                              Code,
743a9cfed77b6bc4930ffb07cbd2877cef5c44d93b2Dan Gohman                                              PN->getType());
744d19534add90a2a894af61523b830887097bb780bDan Gohman        }
745c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      }
7462114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
7472114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      // Replace the use of the operand Value with the new Phi we just created.
748c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner      PN->setIncomingValue(i, Code);
7492114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner      Rewriter.clear();
7502114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner    }
7512114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner  }
7520e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng
7530e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng  // PHI node might have become a constant value after SplitCriticalEdge.
75409fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner  DeadInsts.push_back(Inst);
7550e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng
756b7427031372337e6d67f9573ec6c722ab5ea913eBill Wendling  DOUT << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst;
7572114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner}
7582114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
7592114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner
760203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// fitsInAddressMode - Return true if V can be subsumed within an addressing
761203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// mode, and does not need to be put in a register first.
762203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesenstatic bool fitsInAddressMode(const SCEVHandle &V, const Type *UseTy,
763203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen                             const TargetLowering *TLI, bool HasBaseReg) {
7643821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
7655eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng    int64_t VC = SC->getValue()->getSExtValue();
766579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner    if (TLI) {
767579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner      TargetLowering::AddrMode AM;
768579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner      AM.BaseOffs = VC;
769203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      AM.HasBaseReg = HasBaseReg;
770579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner      return TLI->isLegalAddressingMode(AM, UseTy);
771579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner    } else {
772d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      // Defaults to PPC. PPC allows a sign-extended 16-bit immediate field.
7735eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng      return (VC > -(1 << 16) && VC < (1 << 16)-1);
774579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner    }
7753821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner  }
776d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen
777169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
778169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
779579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner      if (TLI && CE->getOpcode() == Instruction::PtrToInt) {
780d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng        Constant *Op0 = CE->getOperand(0);
781579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner        if (GlobalValue *GV = dyn_cast<GlobalValue>(Op0)) {
782579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner          TargetLowering::AddrMode AM;
783579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner          AM.BaseGV = GV;
784203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen          AM.HasBaseReg = HasBaseReg;
785579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner          return TLI->isLegalAddressingMode(AM, UseTy);
786579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner        }
787d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng      }
788169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  return false;
789169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
790169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
791544e0d0e52e68e8bb0c4ce18bd6584602353ce1cDale Johannesen/// MoveLoopVariantsToImmediateField - Move any subexpressions from Val that are
79244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// loop varying to the Imm operand.
793544e0d0e52e68e8bb0c4ce18bd6584602353ce1cDale Johannesenstatic void MoveLoopVariantsToImmediateField(SCEVHandle &Val, SCEVHandle &Imm,
794246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                            Loop *L, ScalarEvolution *SE) {
79544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  if (Val->isLoopInvariant(L)) return;  // Nothing to do.
79644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
79744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
79844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    std::vector<SCEVHandle> NewOps;
79944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    NewOps.reserve(SAE->getNumOperands());
80044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
80144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
80244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      if (!SAE->getOperand(i)->isLoopInvariant(L)) {
80344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        // If this is a loop-variant expression, it must stay in the immediate
80444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        // field of the expression.
805246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman        Imm = SE->getAddExpr(Imm, SAE->getOperand(i));
80644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      } else {
80744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner        NewOps.push_back(SAE->getOperand(i));
80844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner      }
80944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
81044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    if (NewOps.empty())
811246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Val = SE->getIntegerSCEV(0, Val->getType());
81244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    else
813246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Val = SE->getAddExpr(NewOps);
81444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
81544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    // Try to pull immediates out of the start value of nested addrec's.
81644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    SCEVHandle Start = SARE->getStart();
817544e0d0e52e68e8bb0c4ce18bd6584602353ce1cDale Johannesen    MoveLoopVariantsToImmediateField(Start, Imm, L, SE);
81844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
81944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
82044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    Ops[0] = Start;
821246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    Val = SE->getAddRecExpr(Ops, SARE->getLoop());
82244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  } else {
82344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner    // Otherwise, all of Val is variant, move the whole thing over.
824246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    Imm = SE->getAddExpr(Imm, Val);
825246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    Val = SE->getIntegerSCEV(0, Val->getType());
82644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  }
82744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner}
82844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
82944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
83026d91f16464db56283087176a73981048331dd2dChris Lattner/// MoveImmediateValues - Look at Val, and pull out any additions of constants
831169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// that can fit into the immediate field of instructions in the target.
83226d91f16464db56283087176a73981048331dd2dChris Lattner/// Accumulate these immediate values into the Imm value.
833d277f2c66914aecb619c12855f6afae4c7ef883bEvan Chengstatic void MoveImmediateValues(const TargetLowering *TLI,
8341d95816db537242b3ba0e43a0ec96342ad696bf2Evan Cheng                                Instruction *User,
835d277f2c66914aecb619c12855f6afae4c7ef883bEvan Cheng                                SCEVHandle &Val, SCEVHandle &Imm,
836246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                bool isAddress, Loop *L,
837246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                ScalarEvolution *SE) {
8381d95816db537242b3ba0e43a0ec96342ad696bf2Evan Cheng  const Type *UseTy = User->getType();
8391d95816db537242b3ba0e43a0ec96342ad696bf2Evan Cheng  if (StoreInst *SI = dyn_cast<StoreInst>(User))
8401d95816db537242b3ba0e43a0ec96342ad696bf2Evan Cheng    UseTy = SI->getOperand(0)->getType();
8411d95816db537242b3ba0e43a0ec96342ad696bf2Evan Cheng
8427a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
84326d91f16464db56283087176a73981048331dd2dChris Lattner    std::vector<SCEVHandle> NewOps;
84426d91f16464db56283087176a73981048331dd2dChris Lattner    NewOps.reserve(SAE->getNumOperands());
84526d91f16464db56283087176a73981048331dd2dChris Lattner
846221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    for (unsigned i = 0; i != SAE->getNumOperands(); ++i) {
847221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      SCEVHandle NewOp = SAE->getOperand(i);
848246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      MoveImmediateValues(TLI, User, NewOp, Imm, isAddress, L, SE);
849221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
850221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      if (!NewOp->isLoopInvariant(L)) {
8517db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // If this is a loop-variant expression, it must stay in the immediate
8527db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner        // field of the expression.
853246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman        Imm = SE->getAddExpr(Imm, NewOp);
85426d91f16464db56283087176a73981048331dd2dChris Lattner      } else {
855221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        NewOps.push_back(NewOp);
856169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
857221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    }
85826d91f16464db56283087176a73981048331dd2dChris Lattner
85926d91f16464db56283087176a73981048331dd2dChris Lattner    if (NewOps.empty())
860246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Val = SE->getIntegerSCEV(0, Val->getType());
86126d91f16464db56283087176a73981048331dd2dChris Lattner    else
862246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Val = SE->getAddExpr(NewOps);
86326d91f16464db56283087176a73981048331dd2dChris Lattner    return;
8647a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
8657a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner    // Try to pull immediates out of the start value of nested addrec's.
86626d91f16464db56283087176a73981048331dd2dChris Lattner    SCEVHandle Start = SARE->getStart();
867246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    MoveImmediateValues(TLI, User, Start, Imm, isAddress, L, SE);
86826d91f16464db56283087176a73981048331dd2dChris Lattner
86926d91f16464db56283087176a73981048331dd2dChris Lattner    if (Start != SARE->getStart()) {
87026d91f16464db56283087176a73981048331dd2dChris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
87126d91f16464db56283087176a73981048331dd2dChris Lattner      Ops[0] = Start;
872246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Val = SE->getAddRecExpr(Ops, SARE->getLoop());
87326d91f16464db56283087176a73981048331dd2dChris Lattner    }
87426d91f16464db56283087176a73981048331dd2dChris Lattner    return;
875221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner  } else if (SCEVMulExpr *SME = dyn_cast<SCEVMulExpr>(Val)) {
876221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    // Transform "8 * (4 + v)" -> "32 + 8*V" if "32" fits in the immed field.
877203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    if (isAddress && fitsInAddressMode(SME->getOperand(0), UseTy, TLI, false) &&
878221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        SME->getNumOperands() == 2 && SME->isLoopInvariant(L)) {
879221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
880246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      SCEVHandle SubImm = SE->getIntegerSCEV(0, Val->getType());
881221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      SCEVHandle NewOp = SME->getOperand(1);
882246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      MoveImmediateValues(TLI, User, NewOp, SubImm, isAddress, L, SE);
883221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
884221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      // If we extracted something out of the subexpressions, see if we can
885221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      // simplify this!
886221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      if (NewOp != SME->getOperand(1)) {
887221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        // Scale SubImm up by "8".  If the result is a target constant, we are
888221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        // good.
889246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman        SubImm = SE->getMulExpr(SubImm, SME->getOperand(0));
890203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        if (fitsInAddressMode(SubImm, UseTy, TLI, false)) {
891221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner          // Accumulate the immediate.
892246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman          Imm = SE->getAddExpr(Imm, SubImm);
893221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner
894221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner          // Update what is left of 'Val'.
895246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman          Val = SE->getMulExpr(SME->getOperand(0), NewOp);
896221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner          return;
897221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner        }
898221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner      }
899221fc3c6d69bd3854e9121f51e3283492c222ab7Chris Lattner    }
900169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
901169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
90226d91f16464db56283087176a73981048331dd2dChris Lattner  // Loop-variant expressions must stay in the immediate field of the
90326d91f16464db56283087176a73981048331dd2dChris Lattner  // expression.
904203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  if ((isAddress && fitsInAddressMode(Val, UseTy, TLI, false)) ||
90526d91f16464db56283087176a73981048331dd2dChris Lattner      !Val->isLoopInvariant(L)) {
906246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    Imm = SE->getAddExpr(Imm, Val);
907246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    Val = SE->getIntegerSCEV(0, Val->getType());
90826d91f16464db56283087176a73981048331dd2dChris Lattner    return;
9097a2ca56ef3bdda6874bcd4df2910fb5a33999f85Chris Lattner  }
91026d91f16464db56283087176a73981048331dd2dChris Lattner
91126d91f16464db56283087176a73981048331dd2dChris Lattner  // Otherwise, no immediates to move.
912169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman}
913169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
914934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
9157e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// SeparateSubExprs - Decompose Expr into all of the subexpressions that are
9167e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// added together.  This is used to reassociate common addition subexprs
9177e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// together for maximal sharing when rewriting bases.
918934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattnerstatic void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs,
919246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                             SCEVHandle Expr,
920246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                             ScalarEvolution *SE) {
921934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) {
922934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j)
923246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      SeparateSubExprs(SubExprs, AE->getOperand(j), SE);
924934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) {
925246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    SCEVHandle Zero = SE->getIntegerSCEV(0, Expr->getType());
926934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (SARE->getOperand(0) == Zero) {
927934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      SubExprs.push_back(Expr);
928934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    } else {
929934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      // Compute the addrec with zero as its base.
930934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
931934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      Ops[0] = Zero;   // Start with zero base.
932246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      SubExprs.push_back(SE->getAddRecExpr(Ops, SARE->getLoop()));
933934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
934934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
935246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      SeparateSubExprs(SubExprs, SARE->getOperand(0), SE);
936934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    }
937cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman  } else if (!Expr->isZero()) {
938934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Do not add zero.
939934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SubExprs.push_back(Expr);
940934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
941934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner}
942934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
943203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen// This is logically local to the following function, but C++ says we have
944203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen// to make it file scope.
945203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesenstruct SubExprUseData { unsigned Count; bool notAllUsesAreFree; };
946934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
947203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// RemoveCommonExpressionsFromUseBases - Look through all of the Bases of all
948203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// the Uses, removing any common subexpressions, except that if all such
949203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// subexpressions can be folded into an addressing mode for all uses inside
950203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// the loop (this case is referred to as "free" in comments herein) we do
951203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen/// not remove anything.  This looks for things like (a+b+c) and
952f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner/// (a+c+d) and computes the common (a+c) subexpression.  The common expression
953f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner/// is *removed* from the Bases and returned.
9541bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnerstatic SCEVHandle
955246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan GohmanRemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses,
956203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen                                    ScalarEvolution *SE, Loop *L,
957203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen                                    const TargetLowering *TLI) {
9581bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  unsigned NumUses = Uses.size();
9591bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
960f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner  // Only one use?  This is a very common case, so we handle it specially and
961f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner  // cheaply.
962246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  SCEVHandle Zero = SE->getIntegerSCEV(0, Uses[0].Base->getType());
9631bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVHandle Result = Zero;
964203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  SCEVHandle FreeResult = Zero;
9651bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  if (NumUses == 1) {
966f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // If the use is inside the loop, use its base, regardless of what it is:
967f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // it is clearly shared across all the IV's.  If the use is outside the loop
968f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // (which means after it) we don't want to factor anything *into* the loop,
969f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // so just use 0 as the base.
970589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen    if (L->contains(Uses[0].Inst->getParent()))
971589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen      std::swap(Result, Uses[0].Base);
9721bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    return Result;
9731bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  }
9741bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
9751bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // To find common subexpressions, count how many of Uses use each expression.
9761bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // If any subexpressions are used Uses.size() times, they are common.
977203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  // Also track whether all uses of each expression can be moved into an
978203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  // an addressing mode "for free"; such expressions are left within the loop.
979203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  // struct SubExprUseData { unsigned Count; bool notAllUsesAreFree; };
980203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  std::map<SCEVHandle, SubExprUseData> SubExpressionUseData;
9811bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
982d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // UniqueSubExprs - Keep track of all of the subexpressions we see in the
983d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // order we see them.
984d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  std::vector<SCEVHandle> UniqueSubExprs;
985d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner
986934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  std::vector<SCEVHandle> SubExprs;
987f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner  unsigned NumUsesInsideLoop = 0;
988934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  for (unsigned i = 0; i != NumUses; ++i) {
989f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // If the user is outside the loop, just ignore it for base computation.
990f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // Since the user is outside the loop, it must be *after* the loop (if it
991f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // were before, it could not be based on the loop IV).  We don't want users
992f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // after the loop to affect base computation of values *inside* the loop,
993f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // because we can always add their offsets to the result IV after the loop
994f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // is done, ensuring we get good code inside the loop.
995589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen    if (!L->contains(Uses[i].Inst->getParent()))
996589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen      continue;
997589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen    NumUsesInsideLoop++;
998589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen
999934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // If the base is zero (which is common), return zero now, there are no
1000934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // CSEs we can find.
1001934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (Uses[i].Base == Zero) return Zero;
1002934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
1003203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // If this use is as an address we may be able to put CSEs in the addressing
1004203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // mode rather than hoisting them.
1005203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    bool isAddrUse = isAddressUse(Uses[i].Inst, Uses[i].OperandValToReplace);
1006203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // We may need the UseTy below, but only when isAddrUse, so compute it
1007203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // only in that case.
1008203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    const Type *UseTy = 0;
1009203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    if (isAddrUse) {
1010203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      UseTy  = Uses[i].Inst->getType();
1011203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (StoreInst *SI = dyn_cast<StoreInst>(Uses[i].Inst))
1012203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        UseTy = SI->getOperand(0)->getType();
1013203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    }
1014203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen
1015934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Split the expression into subexprs.
1016246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    SeparateSubExprs(SubExprs, Uses[i].Base, SE);
1017203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // Add one to SubExpressionUseData.Count for each subexpr present, and
1018203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // if the subexpr is not a valid immediate within an addressing mode use,
1019203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // set SubExpressionUseData.notAllUsesAreFree.  We definitely want to
1020203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // hoist these out of the loop (if they are common to all uses).
1021203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) {
1022203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (++SubExpressionUseData[SubExprs[j]].Count == 1)
1023d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner        UniqueSubExprs.push_back(SubExprs[j]);
1024203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (!isAddrUse || !fitsInAddressMode(SubExprs[j], UseTy, TLI, false))
1025203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        SubExpressionUseData[SubExprs[j]].notAllUsesAreFree = true;
1026203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    }
1027934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    SubExprs.clear();
1028934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
1029934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
1030d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // Now that we know how many times each is used, build Result.  Iterate over
1031d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  // UniqueSubexprs so that we have a stable ordering.
1032d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  for (unsigned i = 0, e = UniqueSubExprs.size(); i != e; ++i) {
1033203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    std::map<SCEVHandle, SubExprUseData>::iterator I =
1034203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen       SubExpressionUseData.find(UniqueSubExprs[i]);
1035203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    assert(I != SubExpressionUseData.end() && "Entry not found?");
1036203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    if (I->second.Count == NumUsesInsideLoop) { // Found CSE!
1037203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (I->second.notAllUsesAreFree)
1038203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        Result = SE->getAddExpr(Result, I->first);
1039203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      else
1040203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        FreeResult = SE->getAddExpr(FreeResult, I->first);
1041203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    } else
1042203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      // Remove non-cse's from SubExpressionUseData.
1043203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      SubExpressionUseData.erase(I);
1044d6155e96f78a9f4344f5e697f7dd74d2f2325092Chris Lattner  }
1045203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen
1046203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  if (FreeResult != Zero) {
1047203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // We have some subexpressions that can be subsumed into addressing
1048203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // modes in every use inside the loop.  However, it's possible that
1049203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // there are so many of them that the combined FreeResult cannot
1050203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // be subsumed, or that the target cannot handle both a FreeResult
1051203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // and a Result in the same instruction (for example because it would
1052203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    // require too many registers).  Check this.
1053203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    for (unsigned i=0; i<NumUses; ++i) {
1054203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (!L->contains(Uses[i].Inst->getParent()))
1055203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        continue;
1056203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      // We know this is an addressing mode use; if there are any uses that
1057203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      // are not, FreeResult would be Zero.
1058203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      const Type *UseTy = Uses[i].Inst->getType();
1059203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (StoreInst *SI = dyn_cast<StoreInst>(Uses[i].Inst))
1060203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        UseTy = SI->getOperand(0)->getType();
1061203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (!fitsInAddressMode(FreeResult, UseTy, TLI, Result!=Zero)) {
1062203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        // FIXME:  could split up FreeResult into pieces here, some hoisted
1063203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        // and some not.  Doesn't seem worth it for now.
1064203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        Result = SE->getAddExpr(Result, FreeResult);
1065203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        FreeResult = Zero;
1066203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen        break;
1067203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      }
1068203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    }
1069203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  }
1070203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen
10711bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // If we found no CSE's, return now.
10721bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  if (Result == Zero) return Result;
10731bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
1074203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  // If we still have a FreeResult, remove its subexpressions from
1075203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  // SubExpressionUseData.  This means they will remain in the use Bases.
1076203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  if (FreeResult != Zero) {
1077203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    SeparateSubExprs(SubExprs, FreeResult, SE);
1078203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) {
1079203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      std::map<SCEVHandle, SubExprUseData>::iterator I =
1080203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen         SubExpressionUseData.find(SubExprs[j]);
1081203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      SubExpressionUseData.erase(I);
1082203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    }
1083203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    SubExprs.clear();
1084203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen  }
1085203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen
10861bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Otherwise, remove all of the CSE's we found from each of the base values.
1087934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  for (unsigned i = 0; i != NumUses; ++i) {
1088fb10cd490195128d4d36ed9d963f173a6d6ca46eDale Johannesen    // Uses outside the loop don't necessarily include the common base, but
1089fb10cd490195128d4d36ed9d963f173a6d6ca46eDale Johannesen    // the final IV value coming into those uses does.  Instead of trying to
1090fb10cd490195128d4d36ed9d963f173a6d6ca46eDale Johannesen    // remove the pieces of the common base, which might not be there,
1091fb10cd490195128d4d36ed9d963f173a6d6ca46eDale Johannesen    // subtract off the base to compensate for this.
1092fb10cd490195128d4d36ed9d963f173a6d6ca46eDale Johannesen    if (!L->contains(Uses[i].Inst->getParent())) {
1093fb10cd490195128d4d36ed9d963f173a6d6ca46eDale Johannesen      Uses[i].Base = SE->getMinusSCEV(Uses[i].Base, Result);
1094589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen      continue;
1095fb10cd490195128d4d36ed9d963f173a6d6ca46eDale Johannesen    }
1096589bf0865ccd10d36f406d622c0160be249343e1Dale Johannesen
1097934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Split the expression into subexprs.
1098246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    SeparateSubExprs(SubExprs, Uses[i].Base, SE);
1099934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
1100934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    // Remove any common subexpressions.
1101934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
1102203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (SubExpressionUseData.count(SubExprs[j])) {
1103934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner        SubExprs.erase(SubExprs.begin()+j);
1104934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner        --j; --e;
1105934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner      }
1106934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner
1107f8828eb41b2f8f0356ff6889c9924eade654e39aChris Lattner    // Finally, add the non-shared expressions together.
1108934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    if (SubExprs.empty())
11091bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Uses[i].Base = Zero;
1110934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner    else
1111246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      Uses[i].Base = SE->getAddExpr(SubExprs);
111227e5142309946ca12c633b265673af0c24684427Chris Lattner    SubExprs.clear();
1113934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner  }
11141bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
11151bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  return Result;
11161bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner}
11171bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
1118dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen/// ValidStride - Check whether the given Scale is valid for all loads and
1119579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner/// stores in UsersToProcess.
1120dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen///
112102e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohmanbool LoopStrengthReduce::ValidStride(bool HasBaseReg,
112202e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman                               int64_t Scale,
1123dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen                               const std::vector<BasedUser>& UsersToProcess) {
1124d6b62a572210aff965a55626cf36a68821838844Evan Cheng  if (!TLI)
1125d6b62a572210aff965a55626cf36a68821838844Evan Cheng    return true;
1126d6b62a572210aff965a55626cf36a68821838844Evan Cheng
11278e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen  for (unsigned i=0, e = UsersToProcess.size(); i!=e; ++i) {
11281ebd89eb6b4f5df8d8171ee654a73ecdf3f66580Chris Lattner    // If this is a load or other access, pass the type of the access in.
11291ebd89eb6b4f5df8d8171ee654a73ecdf3f66580Chris Lattner    const Type *AccessTy = Type::VoidTy;
11301ebd89eb6b4f5df8d8171ee654a73ecdf3f66580Chris Lattner    if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].Inst))
11311ebd89eb6b4f5df8d8171ee654a73ecdf3f66580Chris Lattner      AccessTy = SI->getOperand(0)->getType();
11321ebd89eb6b4f5df8d8171ee654a73ecdf3f66580Chris Lattner    else if (LoadInst *LI = dyn_cast<LoadInst>(UsersToProcess[i].Inst))
11331ebd89eb6b4f5df8d8171ee654a73ecdf3f66580Chris Lattner      AccessTy = LI->getType();
113455e641b766a18878b51551d626d5a566102e487eEvan Cheng    else if (isa<PHINode>(UsersToProcess[i].Inst))
113555e641b766a18878b51551d626d5a566102e487eEvan Cheng      continue;
11361ebd89eb6b4f5df8d8171ee654a73ecdf3f66580Chris Lattner
1137579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner    TargetLowering::AddrMode AM;
1138579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner    if (SCEVConstant *SC = dyn_cast<SCEVConstant>(UsersToProcess[i].Imm))
1139579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner      AM.BaseOffs = SC->getValue()->getSExtValue();
1140cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman    AM.HasBaseReg = HasBaseReg || !UsersToProcess[i].Base->isZero();
1141579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner    AM.Scale = Scale;
1142579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner
1143579633cd1006f6add1b022e9c2bc96f7f0e65777Chris Lattner    // If load[imm+r*scale] is illegal, bail out.
1144d6b62a572210aff965a55626cf36a68821838844Evan Cheng    if (!TLI->isLegalAddressingMode(AM, AccessTy))
1145dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen      return false;
11468e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen  }
1147dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen  return true;
1148dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen}
11491bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
11505f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng/// RequiresTypeConversion - Returns true if converting Ty to NewTy is not
11515f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng/// a nop.
11522bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Chengbool LoopStrengthReduce::RequiresTypeConversion(const Type *Ty1,
11532bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng                                                const Type *Ty2) {
11542bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng  if (Ty1 == Ty2)
11555f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng    return false;
11562bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng  if (TLI && TLI->isTruncateFree(Ty1, Ty2))
11572bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng    return false;
11582bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng  return (!Ty1->canLosslesslyBitCastTo(Ty2) &&
11592bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng          !(isa<PointerType>(Ty2) &&
11602bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng            Ty1->canLosslesslyBitCastTo(UIntPtrTy)) &&
11612bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng          !(isa<PointerType>(Ty1) &&
11622bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng            Ty2->canLosslesslyBitCastTo(UIntPtrTy)));
11635f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng}
11645f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng
1165eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng/// CheckForIVReuse - Returns the multiple if the stride is the multiple
1166eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng/// of a previous stride and it is a legal value for the target addressing
116702e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman/// mode scale component and optional base reg. This allows the users of
116802e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman/// this stride to be rewritten as prev iv * factor. It returns 0 if no
116902e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman/// reuse is possible.
117002e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohmanunsigned LoopStrengthReduce::CheckForIVReuse(bool HasBaseReg,
11712bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng                                bool AllUsesAreAddresses,
117202e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman                                const SCEVHandle &Stride,
1173dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen                                IVExpr &IV, const Type *Ty,
1174dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen                                const std::vector<BasedUser>& UsersToProcess) {
1175eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Stride)) {
1176502db93a8ab376730164db43ca3ce8032b72bd59Reid Spencer    int64_t SInt = SC->getValue()->getSExtValue();
1177b51b4b5fdfdd7a85ea66776d7900214f8b24f826Dale Johannesen    for (unsigned NewStride = 0, e = StrideOrder.size(); NewStride != e;
1178b51b4b5fdfdd7a85ea66776d7900214f8b24f826Dale Johannesen         ++NewStride) {
1179b51b4b5fdfdd7a85ea66776d7900214f8b24f826Dale Johannesen      std::map<SCEVHandle, IVsOfOneStride>::iterator SI =
1180b51b4b5fdfdd7a85ea66776d7900214f8b24f826Dale Johannesen                IVsByStride.find(StrideOrder[NewStride]);
1181b51b4b5fdfdd7a85ea66776d7900214f8b24f826Dale Johannesen      if (SI == IVsByStride.end())
1182b51b4b5fdfdd7a85ea66776d7900214f8b24f826Dale Johannesen        continue;
11835eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng      int64_t SSInt = cast<SCEVConstant>(SI->first)->getValue()->getSExtValue();
11842bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng      if (SI->first != Stride &&
11851d31290634eccc3b360c427282d59780d76b9169Chris Lattner          (unsigned(abs(SInt)) < SSInt || (SInt % SSInt) != 0))
1186eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        continue;
11875eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng      int64_t Scale = SInt / SSInt;
1188dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen      // Check that this stride is valid for all the types used for loads and
1189dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen      // stores; if it can be used for some and not others, we might as well use
1190dc42f48ea90132509e678028e7dbab5544ef0794Dale Johannesen      // the original stride everywhere, since we have to create the IV for it
1191aa34331e7832dc1139231626516e9587eeecb0ceDan Gohman      // anyway. If the scale is 1, then we don't need to worry about folding
1192aa34331e7832dc1139231626516e9587eeecb0ceDan Gohman      // multiplications.
1193aa34331e7832dc1139231626516e9587eeecb0ceDan Gohman      if (Scale == 1 ||
1194aa34331e7832dc1139231626516e9587eeecb0ceDan Gohman          (AllUsesAreAddresses &&
1195aa34331e7832dc1139231626516e9587eeecb0ceDan Gohman           ValidStride(HasBaseReg, Scale, UsersToProcess)))
11965eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng        for (std::vector<IVExpr>::iterator II = SI->second.IVs.begin(),
11975eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng               IE = SI->second.IVs.end(); II != IE; ++II)
11985eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng          // FIXME: Only handle base == 0 for now.
11995eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng          // Only reuse previous IV if it would not require a type conversion.
1200cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman          if (II->Base->isZero() &&
12012bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng              !RequiresTypeConversion(II->Base->getType(), Ty)) {
12025eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng            IV = *II;
12035eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng            return Scale;
12045eef2d21a0c1cc4560a49e4dcdec9c00946a86c2Evan Cheng          }
1205eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    }
1206eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  }
120721495775e710d37003e100862cdc647cbdc3b999Evan Cheng  return 0;
1208eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng}
1209eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
12107e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// PartitionByIsUseOfPostIncrementedValue - Simple boolean predicate that
12117e79b3898ddd919170d367a516f51296017146c2Chris Lattner/// returns true if Val's isUseOfPostIncrementedValue is true.
12127e79b3898ddd919170d367a516f51296017146c2Chris Lattnerstatic bool PartitionByIsUseOfPostIncrementedValue(const BasedUser &Val) {
12137e79b3898ddd919170d367a516f51296017146c2Chris Lattner  return Val.isUseOfPostIncrementedValue;
12147e79b3898ddd919170d367a516f51296017146c2Chris Lattner}
1215eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
12164a9a3e53746e3cc752d8a242ddc887a106cf5021Dan Gohman/// isNonConstantNegative - Return true if the specified scev is negated, but
1217fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner/// not a constant.
1218fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattnerstatic bool isNonConstantNegative(const SCEVHandle &Expr) {
1219fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner  SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Expr);
1220fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner  if (!Mul) return false;
1221fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner
1222fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner  // If there is a constant factor, it will be first.
1223fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner  SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
1224fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner  if (!SC) return false;
1225fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner
1226fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner  // Return true if the value is negative, this matches things like (-42 * V).
1227fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner  return SC->getValue()->getValue().isNegative();
1228fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner}
1229fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner
12305f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng// CollectIVUsers - Transform our list of users and offsets to a bit more
123173b43b9b549a75fb0015c825df68abd95705a67cDan Gohman// complex table. In this new vector, each 'BasedUser' contains 'Base', the base
123273b43b9b549a75fb0015c825df68abd95705a67cDan Gohman// of the strided accesses, as well as the old information from Uses. We
12335f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng// progressively move information from the Base field to the Imm field, until
12345f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng// we eventually have the full access expression to rewrite the use.
12355f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan ChengSCEVHandle LoopStrengthReduce::CollectIVUsers(const SCEVHandle &Stride,
12365f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                              IVUsersOfOneStride &Uses,
12375f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                              Loop *L,
12385f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                              bool &AllUsesAreAddresses,
12395f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                       std::vector<BasedUser> &UsersToProcess) {
1240169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  UsersToProcess.reserve(Uses.Users.size());
1241a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner  for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) {
1242246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    UsersToProcess.push_back(BasedUser(Uses.Users[i], SE));
1243a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner
124467c79892949332568b082f124d9598971fa3277fDale Johannesen    // Move any loop variant operands from the offset field to the immediate
1245a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // field of the use, so that we don't try to use something before it is
1246a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    // computed.
1247544e0d0e52e68e8bb0c4ce18bd6584602353ce1cDale Johannesen    MoveLoopVariantsToImmediateField(UsersToProcess.back().Base,
1248246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                    UsersToProcess.back().Imm, L, SE);
1249a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner    assert(UsersToProcess.back().Base->isLoopInvariant(L) &&
125026d91f16464db56283087176a73981048331dd2dChris Lattner           "Base value is not loop invariant!");
12512461dff0700d0e34b9854d96a5cc03921b375525Chris Lattner  }
1252eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
125331e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // We now have a whole bunch of uses of like-strided induction variables, but
125431e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // they might all have different bases.  We want to emit one PHI node for this
125531e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // stride which we fold as many common expressions (between the IVs) into as
125631e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // possible.  Start by identifying the common expressions in the base values
125731e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find
125831e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // "A+B"), emit it to the preheader, then remove the expression from the
125931e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  // UsersToProcess base values.
126031e773147bf13cb11c2bf343db5706fe3e2923d7Evan Cheng  SCEVHandle CommonExprs =
1261203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen    RemoveCommonExpressionsFromUseBases(UsersToProcess, SE, L, TLI);
126202e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman
126344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  // Next, figure out what we can represent in the immediate fields of
126444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  // instructions.  If we can represent anything there, move it to the imm
12651bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // fields of the BasedUsers.  We do this so that it increases the commonality
12661bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // of the remaining uses.
126732e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng  unsigned NumPHI = 0;
126844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
126980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // If the user is not in the current loop, this means it is using the exit
127080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // value of the IV.  Do not put anything in the base, make sure it's all in
127180b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    // the immediate field to allow as much factoring as possible.
127280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    if (!L->contains(UsersToProcess[i].Inst->getParent())) {
1273246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      UsersToProcess[i].Imm = SE->getAddExpr(UsersToProcess[i].Imm,
1274246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                             UsersToProcess[i].Base);
12758385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner      UsersToProcess[i].Base =
1276246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman        SE->getIntegerSCEV(0, UsersToProcess[i].Base->getType());
127780b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    } else {
127880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner
127980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      // Addressing modes can be folded into loads and stores.  Be careful that
128080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner      // the store is through the expression, not of the expression though.
128132e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng      bool isPHI = false;
1282d6b62a572210aff965a55626cf36a68821838844Evan Cheng      bool isAddress = isAddressUse(UsersToProcess[i].Inst,
1283d6b62a572210aff965a55626cf36a68821838844Evan Cheng                                    UsersToProcess[i].OperandValToReplace);
1284d6b62a572210aff965a55626cf36a68821838844Evan Cheng      if (isa<PHINode>(UsersToProcess[i].Inst)) {
128532e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng        isPHI = true;
128632e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng        ++NumPHI;
12872acc7601650654d03cd53faeece8d7685a203105Dan Gohman      }
128802e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman
128902e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman      // If this use isn't an address, then not all uses are addresses.
129055e641b766a18878b51551d626d5a566102e487eEvan Cheng      if (!isAddress && !isPHI)
129102e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman        AllUsesAreAddresses = false;
129280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner
12931d95816db537242b3ba0e43a0ec96342ad696bf2Evan Cheng      MoveImmediateValues(TLI, UsersToProcess[i].Inst, UsersToProcess[i].Base,
1294246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                          UsersToProcess[i].Imm, isAddress, L, SE);
129580b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner    }
129644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner  }
1297d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
129832e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng  // If one of the use if a PHI node and all other uses are addresses, still
129932e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng  // allow iv reuse. Essentially we are trading one constant multiplication
130032e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng  // for one fewer iv.
130132e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng  if (NumPHI > 1)
130232e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng    AllUsesAreAddresses = false;
130332e4c7c486084cdbed07925be4a0e9f3ab6caedeEvan Cheng
13045f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  return CommonExprs;
13055f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng}
13065f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng
13075f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng/// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single
13085f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng/// stride of IV.  All of the users may have different starting values, and this
13095f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng/// may not be the only stride (we know it is if isOnlyStride is true).
13105f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Chengvoid LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
13115f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                                      IVUsersOfOneStride &Uses,
13125f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                                      Loop *L,
13135f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                                      bool isOnlyStride) {
13145f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // If all the users are moved to another stride, then there is nothing to do.
1315303595942502f17c087fa28874c2b89117148c45Dan Gohman  if (Uses.Users.empty())
13165f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng    return;
13175f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng
13185f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // Keep track if every use in UsersToProcess is an address. If they all are,
13195f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // we may be able to rewrite the entire collection of them in terms of a
13205f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // smaller-stride IV.
13215f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  bool AllUsesAreAddresses = true;
13225f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng
13235f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // Transform our list of users and offsets to a bit more complex table.  In
13245f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // this new vector, each 'BasedUser' contains 'Base' the base of the
13255f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // strided accessas well as the old information from Uses.  We progressively
13265f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // move information from the Base field to the Imm field, until we eventually
13275f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // have the full access expression to rewrite the use.
13285f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  std::vector<BasedUser> UsersToProcess;
13295f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  SCEVHandle CommonExprs = CollectIVUsers(Stride, Uses, L, AllUsesAreAddresses,
13305f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                          UsersToProcess);
13315f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng
13325f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // If we managed to find some expressions in common, we'll need to carry
13335f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // their value in a register and add it in for each use. This will take up
13345f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // a register operand, which potentially restricts what stride values are
13355f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng  // valid.
1336cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman  bool HaveCommonExprs = !CommonExprs->isZero();
13375f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng
133802e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman  // If all uses are addresses, check if it is possible to reuse an IV with a
133902e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman  // stride that is a factor of this stride. And that the multiple is a number
134002e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman  // that can be encoded in the scale field of the target addressing mode. And
134102e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman  // that we will have a valid instruction after this substition, including the
134202e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman  // immediate field, if any.
13438e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen  PHINode *NewPHI = NULL;
13448e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen  Value   *IncV   = NULL;
1345246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  IVExpr   ReuseIV(SE->getIntegerSCEV(0, Type::Int32Ty),
1346246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                   SE->getIntegerSCEV(0, Type::Int32Ty),
1347246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                   0, 0);
134802e4fa7d5fb4edd2ce9c7ede29c74d50cb126d7dDan Gohman  unsigned RewriteFactor = 0;
13492bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng  RewriteFactor = CheckForIVReuse(HaveCommonExprs, AllUsesAreAddresses,
13502bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng                                  Stride, ReuseIV, CommonExprs->getType(),
13512bd122c4d934a70e031dc0ca5171719bac66c2c9Evan Cheng                                  UsersToProcess);
13528e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen  if (RewriteFactor != 0) {
13538e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen    DOUT << "BASED ON IV of STRIDE " << *ReuseIV.Stride
13548e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen         << " and BASE " << *ReuseIV.Base << " :\n";
13558e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen    NewPHI = ReuseIV.PHI;
13568e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen    IncV   = ReuseIV.IncV;
13578e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen  }
13588e59e163db8cd3e7b4c96e438fbedf78bff06707Dale Johannesen
1359fe35555d09ecdad4b42da1138a1d40ea4b83898eChris Lattner  const Type *ReplacedTy = CommonExprs->getType();
1360fe35555d09ecdad4b42da1138a1d40ea4b83898eChris Lattner
13611bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  // Now that we know what we need to do, insert the PHI node itself.
13621bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  //
1363fe35555d09ecdad4b42da1138a1d40ea4b83898eChris Lattner  DOUT << "INSERTING IV of TYPE " << *ReplacedTy << " of STRIDE "
13647d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner       << *Stride << " and BASE " << *CommonExprs << ": ";
1365d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
13661bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVExpander Rewriter(*SE, *LI);
13671bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  SCEVExpander PreheaderRewriter(*SE, *LI);
13681bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
13691bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
13701bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Instruction *PreInsertPt = Preheader->getTerminator();
13711bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner  Instruction *PhiInsertBefore = L->getHeader()->begin();
13721bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
137312b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner  BasicBlock *LatchBlock = L->getLoopLatch();
1374d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1375eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
1376eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  // Emit the initial base value into the loop preheader.
1377eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  Value *CommonBaseV
1378d19534add90a2a894af61523b830887097bb780bDan Gohman    = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt);
1379eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
138021495775e710d37003e100862cdc647cbdc3b999Evan Cheng  if (RewriteFactor == 0) {
1381d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    // Create a new Phi for this base, and stick it in the loop header.
1382051a950000e21935165db56695e35bade668193bGabor Greif    NewPHI = PHINode::Create(ReplacedTy, "iv.", PhiInsertBefore);
1383d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    ++NumInserted;
138444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner
1385eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    // Add common base to the new Phi node.
1386eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    NewPHI->addIncoming(CommonBaseV, Preheader);
1387eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
1388fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner    // If the stride is negative, insert a sub instead of an add for the
1389fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner    // increment.
1390fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner    bool isNegative = isNonConstantNegative(Stride);
1391fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner    SCEVHandle IncAmount = Stride;
1392fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner    if (isNegative)
1393246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      IncAmount = SE->getNegativeSCEV(Stride);
1394fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner
1395d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    // Insert the stride into the preheader.
1396d19534add90a2a894af61523b830887097bb780bDan Gohman    Value *StrideV = PreheaderRewriter.expandCodeFor(IncAmount, PreInsertPt);
1397d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    if (!isa<ConstantInt>(StrideV)) ++NumVariable;
139850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner
1399d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    // Emit the increment of the base value before the terminator of the loop
1400d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    // latch block, and add it to the Phi node.
1401246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    SCEVHandle IncExp = SE->getUnknown(StrideV);
1402fb3e1190fc33c93a7185695051d5aeeaddbae0adChris Lattner    if (isNegative)
1403246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      IncExp = SE->getNegativeSCEV(IncExp);
1404246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman    IncExp = SE->getAddExpr(SE->getUnknown(NewPHI), IncExp);
14051bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
1406d19534add90a2a894af61523b830887097bb780bDan Gohman    IncV = Rewriter.expandCodeFor(IncExp, LatchBlock->getTerminator());
1407d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    IncV->setName(NewPHI->getName()+".inc");
1408d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng    NewPHI->addIncoming(IncV, LatchBlock);
1409d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1410eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    // Remember this in case a later stride is multiple of this.
141121495775e710d37003e100862cdc647cbdc3b999Evan Cheng    IVsByStride[Stride].addIV(Stride, CommonExprs, NewPHI, IncV);
14127d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner
14137d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner    DOUT << " IV=%" << NewPHI->getNameStr() << " INC=%" << IncV->getNameStr();
1414eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng  } else {
1415eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    Constant *C = dyn_cast<Constant>(CommonBaseV);
1416eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng    if (!C ||
1417eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        (!C->isNullValue() &&
1418203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen         !fitsInAddressMode(SE->getUnknown(CommonBaseV), ReplacedTy,
1419203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen                           TLI, false)))
14203da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer      // We want the common base emitted into the preheader! This is just
14213da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer      // using cast as a copy so BitCast (no-op cast) is appropriate
14223da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer      CommonBaseV = new BitCastInst(CommonBaseV, CommonBaseV->getType(),
14233da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer                                    "commonbase", PreInsertPt);
1424d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng  }
14257d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner  DOUT << "\n";
14261bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
14277e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // We want to emit code for users inside the loop first.  To do this, we
14287e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // rearrange BasedUser so that the entries at the end have
14297e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // isUseOfPostIncrementedValue = false, because we pop off the end of the
14307e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // vector (so we handle them first).
14317e79b3898ddd919170d367a516f51296017146c2Chris Lattner  std::partition(UsersToProcess.begin(), UsersToProcess.end(),
14327e79b3898ddd919170d367a516f51296017146c2Chris Lattner                 PartitionByIsUseOfPostIncrementedValue);
14337e79b3898ddd919170d367a516f51296017146c2Chris Lattner
14347e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // Sort this by base, so that things with the same base are handled
14357e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // together.  By partitioning first and stable-sorting later, we are
14367e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // guaranteed that within each base we will pop off users from within the
14377e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // loop before users outside of the loop with a particular base.
14387e79b3898ddd919170d367a516f51296017146c2Chris Lattner  //
14397e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // We would like to use stable_sort here, but we can't.  The problem is that
14407e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // SCEVHandle's don't have a deterministic ordering w.r.t to each other, so
14417e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // we don't have anything to do a '<' comparison on.  Because we think the
14427e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // number of uses is small, do a horrible bubble sort which just relies on
14437e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // ==.
14447e79b3898ddd919170d367a516f51296017146c2Chris Lattner  for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
14457e79b3898ddd919170d367a516f51296017146c2Chris Lattner    // Get a base value.
14467e79b3898ddd919170d367a516f51296017146c2Chris Lattner    SCEVHandle Base = UsersToProcess[i].Base;
14477e79b3898ddd919170d367a516f51296017146c2Chris Lattner
14488392772727ed9105c92fe4514d53dab74c333edcEvan Cheng    // Compact everything with this base to be consequtive with this one.
14497e79b3898ddd919170d367a516f51296017146c2Chris Lattner    for (unsigned j = i+1; j != e; ++j) {
14507e79b3898ddd919170d367a516f51296017146c2Chris Lattner      if (UsersToProcess[j].Base == Base) {
14517e79b3898ddd919170d367a516f51296017146c2Chris Lattner        std::swap(UsersToProcess[i+1], UsersToProcess[j]);
14527e79b3898ddd919170d367a516f51296017146c2Chris Lattner        ++i;
14537e79b3898ddd919170d367a516f51296017146c2Chris Lattner      }
14547e79b3898ddd919170d367a516f51296017146c2Chris Lattner    }
14557e79b3898ddd919170d367a516f51296017146c2Chris Lattner  }
14567e79b3898ddd919170d367a516f51296017146c2Chris Lattner
14577e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // Process all the users now.  This outer loop handles all bases, the inner
14587e79b3898ddd919170d367a516f51296017146c2Chris Lattner  // loop handles all users of a particular base.
1459169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  while (!UsersToProcess.empty()) {
14607b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    SCEVHandle Base = UsersToProcess.back().Base;
1461be3e5212e23edc9210f774fac18d801de252e906Chris Lattner
14621bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // Emit the code for Base into the preheader.
1463d19534add90a2a894af61523b830887097bb780bDan Gohman    Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt);
14647d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner
14657d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner    DOUT << "  INSERTING code for BASE = " << *Base << ":";
14667d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner    if (BaseV->hasName())
14677d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner      DOUT << " Result value name = %" << BaseV->getNameStr();
14687d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner    DOUT << "\n";
14697d8ed8a94118ace05962e88689a4089c2f49366dChris Lattner
14701bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // If BaseV is a constant other than 0, make sure that it gets inserted into
14711bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner    // the preheader, instead of being forward substituted into the uses.  We do
14723da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    // this by forcing a BitCast (noop cast) to be inserted into the preheader
14733da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer    // in this case.
14747e79b3898ddd919170d367a516f51296017146c2Chris Lattner    if (Constant *C = dyn_cast<Constant>(BaseV)) {
1475203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen      if (!C->isNullValue() && !fitsInAddressMode(Base, ReplacedTy,
1476203af58aea3ae341d38e5c2c5b390b0c31d25557Dale Johannesen                                                 TLI, false)) {
14773da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer        // We want this constant emitted into the preheader! This is just
14783da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer        // using cast as a copy so BitCast (no-op cast) is appropriate
14793da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer        BaseV = new BitCastInst(BaseV, BaseV->getType(), "preheaderinsert",
14804a9a3e53746e3cc752d8a242ddc887a106cf5021Dan Gohman                                PreInsertPt);
14811bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      }
14827e79b3898ddd919170d367a516f51296017146c2Chris Lattner    }
14837e79b3898ddd919170d367a516f51296017146c2Chris Lattner
1484169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // Emit the code to add the immediate offset to the Phi value, just before
14852351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner    // the instructions that we identified as using this stride and base.
14867b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    do {
14877e79b3898ddd919170d367a516f51296017146c2Chris Lattner      // FIXME: Use emitted users to emit other users.
14887b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      BasedUser &User = UsersToProcess.back();
14892351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
1490010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // If this instruction wants to use the post-incremented value, move it
1491010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // after the post-inc and use its value instead of the PHI.
14921bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Value *RewriteOp = NewPHI;
1493010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      if (User.isUseOfPostIncrementedValue) {
14941bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        RewriteOp = IncV;
1495c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner
1496c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // If this user is in the loop, make sure it is the last thing in the
1497c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        // loop to ensure it is dominated by the increment.
1498c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner        if (L->contains(User.Inst->getParent()))
1499c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner          User.Inst->moveBefore(LatchBlock->getTerminator());
1500010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      }
15013ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer      if (RewriteOp->getType() != ReplacedTy) {
15023ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer        Instruction::CastOps opcode = Instruction::Trunc;
15033ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer        if (ReplacedTy->getPrimitiveSizeInBits() ==
15043ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer            RewriteOp->getType()->getPrimitiveSizeInBits())
15053ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer          opcode = Instruction::BitCast;
15063ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer        RewriteOp = SCEVExpander::InsertCastOfTo(opcode, RewriteOp, ReplacedTy);
15073ba68b9eef2851dae8a9d1b18928c6fa2e3c5f87Reid Spencer      }
150886c75d31133319a88216c1b1cd26a789e4023000Evan Cheng
1509246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman      SCEVHandle RewriteExpr = SE->getUnknown(RewriteOp);
15101bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner
1511f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman      // If we had to insert new instrutions for RewriteOp, we have to
1512f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman      // consider that they may not have been able to end up immediately
1513f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman      // next to RewriteOp, because non-PHI instructions may never precede
1514f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman      // PHI instructions in a block. In this case, remember where the last
1515ca756ae886f1d39053ffd049123fdcc7e5c2aed3Dan Gohman      // instruction was inserted so that if we're replacing a different
1516ca756ae886f1d39053ffd049123fdcc7e5c2aed3Dan Gohman      // PHI node, we can use the later point to expand the final
1517ca756ae886f1d39053ffd049123fdcc7e5c2aed3Dan Gohman      // RewriteExpr.
1518f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman      Instruction *NewBasePt = dyn_cast<Instruction>(RewriteOp);
1519f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman      if (RewriteOp == NewPHI) NewBasePt = 0;
1520f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman
15211bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Clear the SCEVExpander's expression map so that we are guaranteed
15221bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // to have the code emitted where we expect it.
15231bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      Rewriter.clear();
1524d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1525d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng      // If we are reusing the iv, then it must be multiplied by a constant
1526d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng      // factor take advantage of addressing mode scale component.
152721495775e710d37003e100862cdc647cbdc3b999Evan Cheng      if (RewriteFactor != 0) {
15288392772727ed9105c92fe4514d53dab74c333edcEvan Cheng        RewriteExpr = SE->getMulExpr(SE->getIntegerSCEV(RewriteFactor,
15298392772727ed9105c92fe4514d53dab74c333edcEvan Cheng                                                        RewriteExpr->getType()),
15308392772727ed9105c92fe4514d53dab74c333edcEvan Cheng                                     RewriteExpr);
1531eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng
1532eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        // The common base is emitted in the loop preheader. But since we
1533eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        // are reusing an IV, it has not been used to initialize the PHI node.
1534eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        // Add it to the expression used to rewrite the uses.
1535eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng        if (!isa<ConstantInt>(CommonBaseV) ||
1536bee0f663d842f5aa41286c22815446e2d22de95aReid Spencer            !cast<ConstantInt>(CommonBaseV)->isZero())
1537246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman          RewriteExpr = SE->getAddExpr(RewriteExpr,
1538246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman                                      SE->getUnknown(CommonBaseV));
1539eb8f9e229740a0f292f5e8f0975bd10b7889b194Evan Cheng      }
1540d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
15411bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // Now that we know what we need to do, insert code before User for the
15421bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner      // immediate and any loop-variant expressions.
1543bee0f663d842f5aa41286c22815446e2d22de95aReid Spencer      if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isZero())
15441bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner        // Add BaseV to the PHI value if needed.
1545246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman        RewriteExpr = SE->getAddExpr(RewriteExpr, SE->getUnknown(BaseV));
1546d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
1547f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman      User.RewriteInstructionToUseNewBase(RewriteExpr, NewBasePt,
1548f20d70d57e1723d70b48b3f19868e17b9282bbfcDan Gohman                                          Rewriter, L, this,
15490e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng                                          DeadInsts);
15502351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
1551a68d4ca73e9cd0b19b2a48a2943e16cc0f89da27Chris Lattner      // Mark old value we replaced as possibly dead, so that it is eliminated
15522351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      // if we just replaced the last use of that value.
155309fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner      DeadInsts.push_back(cast<Instruction>(User.OperandValToReplace));
15542351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner
15557b445c521bc191d0d25799b289e17b45f202a1afChris Lattner      UsersToProcess.pop_back();
15562351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner      ++NumReduced;
15577b445c521bc191d0d25799b289e17b45f202a1afChris Lattner
15587e79b3898ddd919170d367a516f51296017146c2Chris Lattner      // If there are any more users to process with the same base, process them
15597e79b3898ddd919170d367a516f51296017146c2Chris Lattner      // now.  We sorted by base above, so we just have to check the last elt.
15607b445c521bc191d0d25799b289e17b45f202a1afChris Lattner    } while (!UsersToProcess.empty() && UsersToProcess.back().Base == Base);
1561169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    // TODO: Next, find out which base index is the most common, pull it out.
1562169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  }
1563169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
1564169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but
1565169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // different starting values, into different PHIs.
1566eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
1567eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
1568c677de2713646ab6d8200cd71613f6b4ae9885fbDevang Patel/// FindIVUserForCond - If Cond has an operand that is an expression of an IV,
1569aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner/// set the IV user and stride information and return true, otherwise return
1570aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner/// false.
1571c677de2713646ab6d8200cd71613f6b4ae9885fbDevang Patelbool LoopStrengthReduce::FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse,
1572aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner                                       const SCEVHandle *&CondStride) {
1573aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner  for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e && !CondUse;
1574aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner       ++Stride) {
1575aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
1576aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner    IVUsesByStride.find(StrideOrder[Stride]);
1577aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner    assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
1578aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner
1579aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner    for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(),
1580aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner         E = SI->second.Users.end(); UI != E; ++UI)
1581aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner      if (UI->User == Cond) {
1582aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner        // NOTE: we could handle setcc instructions with multiple uses here, but
1583aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner        // InstCombine does it as well for simple uses, it's not clear that it
1584aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner        // occurs enough in real life to handle.
1585aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner        CondUse = &*UI;
1586aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner        CondStride = &SI->first;
1587aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner        return true;
1588aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner      }
1589aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner  }
1590aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner  return false;
1591aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner}
1592aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner
1593cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Chengnamespace {
1594cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  // Constant strides come first which in turns are sorted by their absolute
1595cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  // values. If absolute values are the same, then positive strides comes first.
1596cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  // e.g.
1597cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  // 4, -1, X, 1, 2 ==> 1, -1, 2, 4, X
1598cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  struct StrideCompare {
1599cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    bool operator()(const SCEVHandle &LHS, const SCEVHandle &RHS) {
1600cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS);
1601cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS);
1602cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      if (LHSC && RHSC) {
1603cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        int64_t  LV = LHSC->getValue()->getSExtValue();
1604cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        int64_t  RV = RHSC->getValue()->getSExtValue();
1605cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        uint64_t ALV = (LV < 0) ? -LV : LV;
1606cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        uint64_t ARV = (RV < 0) ? -RV : RV;
1607cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        if (ALV == ARV)
1608cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng          return LV > RV;
1609cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        else
1610cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng          return ALV < ARV;
1611cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      }
1612cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      return (LHSC && !RHSC);
1613cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    }
1614cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  };
1615cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng}
1616cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1617cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// ChangeCompareStride - If a loop termination compare instruction is the
1618cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// only use of its stride, and the compaison is against a constant value,
1619cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// try eliminate the stride by moving the compare instruction to another
1620cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// stride and change its constant operand accordingly. e.g.
1621cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng///
1622cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// loop:
1623cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// ...
1624cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// v1 = v1 + 3
1625cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// v2 = v2 + 1
1626cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// if (v2 < 10) goto loop
1627cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// =>
1628cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// loop:
1629cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// ...
1630cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// v1 = v1 + 3
1631cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng/// if (v1 < 30) goto loop
1632cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan ChengICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond,
16330e0014d0499d6ec6402e07b71cf24af992a9d297Evan Cheng                                                IVStrideUse* &CondUse,
1634cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng                                                const SCEVHandle* &CondStride) {
1635cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  if (StrideOrder.size() < 2 ||
1636cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      IVUsesByStride[*CondStride].Users.size() != 1)
1637cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    return Cond;
1638cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  const SCEVConstant *SC = dyn_cast<SCEVConstant>(*CondStride);
1639cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  if (!SC) return Cond;
1640cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  ConstantInt *C = dyn_cast<ConstantInt>(Cond->getOperand(1));
1641cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  if (!C) return Cond;
1642cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1643cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  ICmpInst::Predicate Predicate = Cond->getPredicate();
1644cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  int64_t CmpSSInt = SC->getValue()->getSExtValue();
1645cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  int64_t CmpVal = C->getValue().getSExtValue();
1646168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng  unsigned BitWidth = C->getValue().getBitWidth();
1647168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng  uint64_t SignBit = 1ULL << (BitWidth-1);
1648168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng  const Type *CmpTy = C->getType();
1649168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng  const Type *NewCmpTy = NULL;
1650af62c09437675369afcc481e88ed9cd5806491feEvan Cheng  unsigned TyBits = CmpTy->getPrimitiveSizeInBits();
1651af62c09437675369afcc481e88ed9cd5806491feEvan Cheng  unsigned NewTyBits = 0;
1652cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  int64_t NewCmpVal = CmpVal;
1653cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  SCEVHandle *NewStride = NULL;
1654cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  Value *NewIncV = NULL;
1655cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  int64_t Scale = 1;
1656cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1657d16aba22c9eca0b2576a5fe5507f3c8ab378942eDevang Patel  // Check stride constant and the comparision constant signs to detect
1658d16aba22c9eca0b2576a5fe5507f3c8ab378942eDevang Patel  // overflow.
16594b3f08bac7def05de67f5c0e9f15c53d0870c7c1Devang Patel  if ((CmpVal & SignBit) != (CmpSSInt & SignBit))
1660d16aba22c9eca0b2576a5fe5507f3c8ab378942eDevang Patel    return Cond;
1661d16aba22c9eca0b2576a5fe5507f3c8ab378942eDevang Patel
1662cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  // Look for a suitable stride / iv as replacement.
1663cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare());
1664cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  for (unsigned i = 0, e = StrideOrder.size(); i != e; ++i) {
1665cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
1666cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      IVUsesByStride.find(StrideOrder[i]);
1667cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    if (!isa<SCEVConstant>(SI->first))
1668cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      continue;
1669cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    int64_t SSInt = cast<SCEVConstant>(SI->first)->getValue()->getSExtValue();
1670168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    if (abs(SSInt) <= abs(CmpSSInt) || (SSInt % CmpSSInt) != 0)
1671cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      continue;
1672cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1673168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    Scale = SSInt / CmpSSInt;
1674168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    NewCmpVal = CmpVal * Scale;
1675168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    APInt Mul = APInt(BitWidth, NewCmpVal);
1676168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    // Check for overflow.
1677168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    if (Mul.getSExtValue() != NewCmpVal) {
1678168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng      NewCmpVal = CmpVal;
1679168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng      continue;
1680168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    }
1681168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng
1682cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    // Watch out for overflow.
1683168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    if (ICmpInst::isSignedPredicate(Predicate) &&
1684168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng        (CmpVal & SignBit) != (NewCmpVal & SignBit))
1685cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      NewCmpVal = CmpVal;
1686168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng
1687cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    if (NewCmpVal != CmpVal) {
1688cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      // Pick the best iv to use trying to avoid a cast.
1689cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      NewIncV = NULL;
1690cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(),
1691cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng             E = SI->second.Users.end(); UI != E; ++UI) {
1692cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        NewIncV = UI->OperandValToReplace;
1693cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        if (NewIncV->getType() == CmpTy)
1694cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng          break;
1695cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      }
1696cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      if (!NewIncV) {
1697cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        NewCmpVal = CmpVal;
1698cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        continue;
1699cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      }
1700cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1701cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      NewCmpTy = NewIncV->getType();
1702af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      NewTyBits = isa<PointerType>(NewCmpTy)
1703af62c09437675369afcc481e88ed9cd5806491feEvan Cheng        ? UIntPtrTy->getPrimitiveSizeInBits()
1704af62c09437675369afcc481e88ed9cd5806491feEvan Cheng        : NewCmpTy->getPrimitiveSizeInBits();
1705af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      if (RequiresTypeConversion(NewCmpTy, CmpTy)) {
17066725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif        // Check if it is possible to rewrite it using
17076725cb5f1c1b59cbd71dc221623c7b4cabafafd0Gabor Greif        // an iv / stride of a smaller integer type.
1708af62c09437675369afcc481e88ed9cd5806491feEvan Cheng        bool TruncOk = false;
1709af62c09437675369afcc481e88ed9cd5806491feEvan Cheng        if (NewCmpTy->isInteger()) {
1710af62c09437675369afcc481e88ed9cd5806491feEvan Cheng          unsigned Bits = NewTyBits;
1711af62c09437675369afcc481e88ed9cd5806491feEvan Cheng          if (ICmpInst::isSignedPredicate(Predicate))
1712af62c09437675369afcc481e88ed9cd5806491feEvan Cheng            --Bits;
1713af62c09437675369afcc481e88ed9cd5806491feEvan Cheng          uint64_t Mask = (1ULL << Bits) - 1;
1714af62c09437675369afcc481e88ed9cd5806491feEvan Cheng          if (((uint64_t)NewCmpVal & Mask) == (uint64_t)NewCmpVal)
1715af62c09437675369afcc481e88ed9cd5806491feEvan Cheng            TruncOk = true;
1716af62c09437675369afcc481e88ed9cd5806491feEvan Cheng        }
1717af62c09437675369afcc481e88ed9cd5806491feEvan Cheng        if (!TruncOk) {
1718af62c09437675369afcc481e88ed9cd5806491feEvan Cheng          NewCmpVal = CmpVal;
1719af62c09437675369afcc481e88ed9cd5806491feEvan Cheng          continue;
1720af62c09437675369afcc481e88ed9cd5806491feEvan Cheng        }
1721af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      }
1722af62c09437675369afcc481e88ed9cd5806491feEvan Cheng
1723af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      // Don't rewrite if use offset is non-constant and the new type is
1724af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      // of a different type.
1725af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      // FIXME: too conservative?
1726af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      if (NewTyBits != TyBits && !isa<SCEVConstant>(CondUse->Offset)) {
17275f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng        NewCmpVal = CmpVal;
17285f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng        continue;
17295f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng      }
17305f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng
17315f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng      bool AllUsesAreAddresses = true;
17325f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng      std::vector<BasedUser> UsersToProcess;
17335f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng      SCEVHandle CommonExprs = CollectIVUsers(SI->first, SI->second, L,
17345f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                              AllUsesAreAddresses,
17355f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng                                              UsersToProcess);
17365f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng      // Avoid rewriting the compare instruction with an iv of new stride
17375f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng      // if it's likely the new stride uses will be rewritten using the
17385f8ebaa5c0c216b7dbb16b781895e4af36bfa39aEvan Cheng      if (AllUsesAreAddresses &&
1739cfeb6a450632f2a6cd05302633c8c2b8c90cfdfdDan Gohman          ValidStride(!CommonExprs->isZero(), Scale, UsersToProcess)) {
1740cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        NewCmpVal = CmpVal;
1741cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng        continue;
1742cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      }
1743cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1744f5e25f32c7605569390f5e81ebb582c96d74a1d2Evan Cheng      // If scale is negative, use swapped predicate unless it's testing
1745cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      // for equality.
1746cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      if (Scale < 0 && !Cond->isEquality())
1747f5e25f32c7605569390f5e81ebb582c96d74a1d2Evan Cheng        Predicate = ICmpInst::getSwappedPredicate(Predicate);
1748cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1749cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      NewStride = &StrideOrder[i];
1750cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng      break;
1751cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    }
1752cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  }
1753cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
17549b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman  // Forgo this transformation if it the increment happens to be
17559b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman  // unfortunately positioned after the condition, and the condition
17569b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman  // has multiple uses which prevent it from being moved immediately
17579b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman  // before the branch. See
17589b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman  // test/Transforms/LoopStrengthReduce/change-compare-stride-trickiness-*.ll
17599b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman  // for an example of this situation.
1760d16aba22c9eca0b2576a5fe5507f3c8ab378942eDevang Patel  if (!Cond->hasOneUse()) {
17619b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman    for (BasicBlock::iterator I = Cond, E = Cond->getParent()->end();
17629b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman         I != E; ++I)
17639b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman      if (I == NewIncV)
17649b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman        return Cond;
1765d16aba22c9eca0b2576a5fe5507f3c8ab378942eDevang Patel  }
17669b93dd1f1ab3532a101a432d7000477adfe6401dDan Gohman
1767cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  if (NewCmpVal != CmpVal) {
1768cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    // Create a new compare instruction using new stride / iv.
1769cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    ICmpInst *OldCond = Cond;
1770af62c09437675369afcc481e88ed9cd5806491feEvan Cheng    Value *RHS;
1771af62c09437675369afcc481e88ed9cd5806491feEvan Cheng    if (!isa<PointerType>(NewCmpTy))
1772af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      RHS = ConstantInt::get(NewCmpTy, NewCmpVal);
1773af62c09437675369afcc481e88ed9cd5806491feEvan Cheng    else {
1774af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      RHS = ConstantInt::get(UIntPtrTy, NewCmpVal);
1775af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      RHS = SCEVExpander::InsertCastOfTo(Instruction::IntToPtr, RHS, NewCmpTy);
1776cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    }
1777168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    // Insert new compare instruction.
1778e562b1725ee068ff525082d1e9ba885c8928c72eDan Gohman    Cond = new ICmpInst(Predicate, NewIncV, RHS,
1779e562b1725ee068ff525082d1e9ba885c8928c72eDan Gohman                        L->getHeader()->getName() + ".termcond",
1780e562b1725ee068ff525082d1e9ba885c8928c72eDan Gohman                        OldCond);
1781168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng
1782168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    // Remove the old compare instruction. The old indvar is probably dead too.
178309fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner    DeadInsts.push_back(cast<Instruction>(CondUse->OperandValToReplace));
1784168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng    SE->deleteValueFromRecords(OldCond);
1785010ee2d95516fe13a574bce5d682a8f8997ab60bDan Gohman    OldCond->replaceAllUsesWith(Cond);
1786cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    OldCond->eraseFromParent();
1787168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng
1788cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    IVUsesByStride[*CondStride].Users.pop_back();
1789af62c09437675369afcc481e88ed9cd5806491feEvan Cheng    SCEVHandle NewOffset = TyBits == NewTyBits
1790af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      ? SE->getMulExpr(CondUse->Offset,
1791af62c09437675369afcc481e88ed9cd5806491feEvan Cheng                       SE->getConstant(ConstantInt::get(CmpTy, Scale)))
1792af62c09437675369afcc481e88ed9cd5806491feEvan Cheng      : SE->getConstant(ConstantInt::get(NewCmpTy,
1793af62c09437675369afcc481e88ed9cd5806491feEvan Cheng        cast<SCEVConstant>(CondUse->Offset)->getValue()->getSExtValue()*Scale));
1794cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    IVUsesByStride[*NewStride].addUser(NewOffset, Cond, NewIncV);
1795cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    CondUse = &IVUsesByStride[*NewStride].Users.back();
1796cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    CondStride = NewStride;
1797cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng    ++NumEliminated;
1798cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  }
1799cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1800cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  return Cond;
1801cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng}
1802cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
1803ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// OptimizeSMax - Rewrite the loop's terminating condition if it uses
1804ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// an smax computation.
1805ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1806ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// This is a narrow solution to a specific, but acute, problem. For loops
1807ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// like this:
1808ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1809ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   i = 0;
1810ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   do {
1811ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///     p[i] = 0.0;
1812ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   } while (++i < n);
1813ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1814ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// where the comparison is signed, the trip count isn't just 'n', because
1815ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// 'n' could be negative. And unfortunately this can come up even for loops
1816ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// where the user didn't use a C do-while loop. For example, seemingly
1817ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// well-behaved top-test loops will commonly be lowered like this:
1818ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman//
1819ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   if (n > 0) {
1820ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///     i = 0;
1821ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///     do {
1822ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///       p[i] = 0.0;
1823ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///     } while (++i < n);
1824ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   }
1825ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1826ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// and then it's possible for subsequent optimization to obscure the if
1827ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// test in such a way that indvars can't find it.
1828ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1829ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// When indvars can't find the if test in loops like this, it creates a
1830ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// signed-max expression, which allows it to give the loop a canonical
1831ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// induction variable:
1832ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1833ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   i = 0;
1834ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   smax = n < 1 ? 1 : n;
1835ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   do {
1836ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///     p[i] = 0.0;
1837ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///   } while (++i != smax);
1838ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1839ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// Canonical induction variables are necessary because the loop passes
1840ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// are designed around them. The most obvious example of this is the
1841ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// LoopInfo analysis, which doesn't remember trip count values. It
1842ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// expects to be able to rediscover the trip count each time it is
1843ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// needed, and it does this using a simple analyis that only succeeds if
1844ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// the loop has a canonical induction variable.
1845ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1846ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// However, when it comes time to generate code, the maximum operation
1847ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// can be quite costly, especially if it's inside of an outer loop.
1848ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1849ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// This function solves this problem by detecting this type of loop and
1850ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// rewriting their conditions from ICMP_NE back to ICMP_SLT, and deleting
1851ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman/// the instructions for the maximum computation.
1852ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman///
1853ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan GohmanICmpInst *LoopStrengthReduce::OptimizeSMax(Loop *L, ICmpInst *Cond,
1854ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman                                           IVStrideUse* &CondUse) {
1855ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // Check that the loop matches the pattern we're looking for.
1856ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (Cond->getPredicate() != CmpInst::ICMP_EQ &&
1857ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman      Cond->getPredicate() != CmpInst::ICMP_NE)
1858ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    return Cond;
1859ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1860ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SelectInst *Sel = dyn_cast<SelectInst>(Cond->getOperand(1));
1861ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (!Sel || !Sel->hasOneUse()) return Cond;
1862ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1863ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SCEVHandle IterationCount = SE->getIterationCount(L);
1864ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (isa<SCEVCouldNotCompute>(IterationCount))
1865ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    return Cond;
1866ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SCEVHandle One = SE->getIntegerSCEV(1, IterationCount->getType());
1867ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1868ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // Adjust for an annoying getIterationCount quirk.
1869ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  IterationCount = SE->getAddExpr(IterationCount, One);
1870ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1871ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // Check for a max calculation that matches the pattern.
1872ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(IterationCount);
1873ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (!SMax || SMax != SE->getSCEV(Sel)) return Cond;
1874ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1875ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SCEVHandle SMaxLHS = SMax->getOperand(0);
1876ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SCEVHandle SMaxRHS = SMax->getOperand(1);
1877ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (!SMaxLHS || SMaxLHS != One) return Cond;
1878ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1879ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // Check the relevant induction variable for conformance to
1880ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // the pattern.
1881ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SCEVHandle IV = SE->getSCEV(Cond->getOperand(0));
1882ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(IV);
1883ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (!AR || !AR->isAffine() ||
1884ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman      AR->getStart() != One ||
1885ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman      AR->getStepRecurrence(*SE) != One)
1886ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    return Cond;
1887ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1888ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // Check the right operand of the select, and remember it, as it will
1889ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // be used in the new comparison instruction.
1890ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  Value *NewRHS = 0;
1891ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (SE->getSCEV(Sel->getOperand(1)) == SMaxRHS)
1892ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    NewRHS = Sel->getOperand(1);
1893ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  else if (SE->getSCEV(Sel->getOperand(2)) == SMaxRHS)
1894ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    NewRHS = Sel->getOperand(2);
1895ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (!NewRHS) return Cond;
1896ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1897ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // Ok, everything looks ok to change the condition into an SLT or SGE and
1898ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // delete the max calculation.
1899ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  ICmpInst *NewCond =
1900ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    new ICmpInst(Cond->getPredicate() == CmpInst::ICMP_NE ?
1901ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman                   CmpInst::ICMP_SLT :
1902ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman                   CmpInst::ICMP_SGE,
1903ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman                 Cond->getOperand(0), NewRHS, "scmp", Cond);
1904ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1905ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // Delete the max calculation instructions.
1906586b7b75479990475ed5a03fae72d44e0b5ca8e7Dan Gohman  SE->deleteValueFromRecords(Cond);
1907ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  Cond->replaceAllUsesWith(NewCond);
1908ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  Cond->eraseFromParent();
1909ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  Instruction *Cmp = cast<Instruction>(Sel->getOperand(0));
1910ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  SE->deleteValueFromRecords(Sel);
1911586b7b75479990475ed5a03fae72d44e0b5ca8e7Dan Gohman  Sel->eraseFromParent();
1912ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  if (Cmp->use_empty()) {
1913ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman    SE->deleteValueFromRecords(Cmp);
1914586b7b75479990475ed5a03fae72d44e0b5ca8e7Dan Gohman    Cmp->eraseFromParent();
1915ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  }
1916ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  CondUse->User = NewCond;
1917ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  return NewCond;
1918ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman}
1919ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
1920a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel/// OptimizeShadowIV - If IV is used in a int-to-float cast
1921a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel/// inside the loop then try to eliminate the cast opeation.
1922a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patelvoid LoopStrengthReduce::OptimizeShadowIV(Loop *L) {
1923a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1924a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel  SCEVHandle IterationCount = SE->getIterationCount(L);
1925a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel  if (isa<SCEVCouldNotCompute>(IterationCount))
1926a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    return;
1927a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1928a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel  for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e;
1929a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel       ++Stride) {
1930a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
1931a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      IVUsesByStride.find(StrideOrder[Stride]);
1932a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
1933a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    if (!isa<SCEVConstant>(SI->first))
1934a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      continue;
1935a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1936a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(),
1937a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel           E = SI->second.Users.end(); UI != E; /* empty */) {
1938a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      std::vector<IVStrideUse>::iterator CandidateUI = UI;
1939541532724e29203e28c2fe0136cf6eabd49d4532Devang Patel      ++UI;
1940a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      Instruction *ShadowUse = CandidateUI->User;
1941a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      const Type *DestTy = NULL;
1942a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1943a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      /* If shadow use is a int->float cast then insert a second IV
1944541532724e29203e28c2fe0136cf6eabd49d4532Devang Patel         to eliminate this cast.
1945a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1946a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel           for (unsigned i = 0; i < n; ++i)
1947a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel             foo((double)i);
1948a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1949541532724e29203e28c2fe0136cf6eabd49d4532Devang Patel         is transformed into
1950a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1951a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel           double d = 0.0;
1952a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel           for (unsigned i = 0; i < n; ++i, ++d)
1953a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel             foo(d);
1954a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      */
1955541532724e29203e28c2fe0136cf6eabd49d4532Devang Patel      if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->User))
1956a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        DestTy = UCast->getDestTy();
1957541532724e29203e28c2fe0136cf6eabd49d4532Devang Patel      else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->User))
1958a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        DestTy = SCast->getDestTy();
195918bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel      if (!DestTy) continue;
196018bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel
196118bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel      if (TLI) {
196218bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel        /* If target does not support DestTy natively then do not apply
196318bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel           this transformation. */
196418bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel        MVT DVT = TLI->getValueType(DestTy);
196518bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel        if (!TLI->isTypeLegal(DVT)) continue;
196618bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel      }
196718bb2788a0edc0ec1c373465429743892c8d5fbeDevang Patel
1968a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      PHINode *PH = dyn_cast<PHINode>(ShadowUse->getOperand(0));
1969a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (!PH) continue;
1970a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (PH->getNumIncomingValues() != 2) continue;
1971a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1972a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      const Type *SrcTy = PH->getType();
1973a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      int Mantissa = DestTy->getFPMantissaWidth();
1974a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (Mantissa == -1) continue;
1975a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if ((int)TD->getTypeSizeInBits(SrcTy) > Mantissa)
1976a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        continue;
1977a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1978a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      unsigned Entry, Latch;
1979a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (PH->getIncomingBlock(0) == L->getLoopPreheader()) {
1980a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        Entry = 0;
1981a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        Latch = 1;
1982a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      } else {
1983a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        Entry = 1;
1984a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        Latch = 0;
1985a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      }
1986a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1987a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      ConstantInt *Init = dyn_cast<ConstantInt>(PH->getIncomingValue(Entry));
1988a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (!Init) continue;
1989a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      ConstantFP *NewInit = ConstantFP::get(DestTy, Init->getZExtValue());
1990a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1991a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      BinaryOperator *Incr =
1992a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        dyn_cast<BinaryOperator>(PH->getIncomingValue(Latch));
1993a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (!Incr) continue;
1994a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (Incr->getOpcode() != Instruction::Add
1995a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel          && Incr->getOpcode() != Instruction::Sub)
1996a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        continue;
1997a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
1998a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      /* Initialize new IV, double d = 0.0 in above example. */
1999a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      ConstantInt *C = NULL;
2000a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (Incr->getOperand(0) == PH)
2001a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        C = dyn_cast<ConstantInt>(Incr->getOperand(1));
2002a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      else if (Incr->getOperand(1) == PH)
2003a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        C = dyn_cast<ConstantInt>(Incr->getOperand(0));
2004a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      else
2005a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        continue;
2006a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
2007a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      if (!C) continue;
2008a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
2009a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      /* Add new PHINode. */
2010a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      PHINode *NewPH = PHINode::Create(DestTy, "IV.S.", PH);
2011a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
2012541532724e29203e28c2fe0136cf6eabd49d4532Devang Patel      /* create new increment. '++d' in above example. */
2013a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      ConstantFP *CFP = ConstantFP::get(DestTy, C->getZExtValue());
2014a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      BinaryOperator *NewIncr =
2015a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel        BinaryOperator::Create(Incr->getOpcode(),
2016a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel                               NewPH, CFP, "IV.S.next.", Incr);
2017a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
2018a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry));
2019a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch));
2020a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
2021a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      /* Remove cast operation */
2022a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      SE->deleteValueFromRecords(ShadowUse);
2023a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      ShadowUse->replaceAllUsesWith(NewPH);
2024a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      ShadowUse->eraseFromParent();
2025a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      SI->second.Users.erase(CandidateUI);
2026a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      NumShadow++;
2027a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel      break;
2028a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel    }
2029a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel  }
2030a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel}
2031a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
2032010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar
2033010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// uses in the loop, look to see if we can eliminate some, in favor of using
2034010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// common indvars for the different uses.
2035010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattnervoid LoopStrengthReduce::OptimizeIndvars(Loop *L) {
2036010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // TODO: implement optzns here.
2037010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
2038a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel  OptimizeShadowIV(L);
2039a0b3909d432e9f2c79aee8ec3133f9b9ec71dc1aDevang Patel
2040010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Finally, get the terminating condition for the loop if possible.  If we
2041010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // can, we want to change it to use a post-incremented version of its
204298d9811db263ee040d9a6a69ef9e1c4fdc8c219dChris Lattner  // induction variable, to allow coalescing the live ranges for the IV into
2043010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // one register value.
2044010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin());
2045010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock  *Preheader = L->getLoopPreheader();
2046010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BasicBlock *LatchBlock =
2047010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner   SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader);
2048010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator());
2049e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  if (!TermBr || TermBr->isUnconditional() ||
2050e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer      !isa<ICmpInst>(TermBr->getCondition()))
2051010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    return;
2052e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer  ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition());
2053010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
2054010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // Search IVUsesByStride to find Cond's IVUse if there is one.
2055010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  IVStrideUse *CondUse = 0;
205650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner  const SCEVHandle *CondStride = 0;
2057010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
2058c677de2713646ab6d8200cd71613f6b4ae9885fbDevang Patel  if (!FindIVUserForCond(Cond, CondUse, CondStride))
2059aed01d19315132daf68414ace410ec725b4b6d30Chris Lattner    return; // setcc doesn't use the IV.
2060cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng
2061ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // If the trip count is computed in terms of an smax (due to ScalarEvolution
2062ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // being unable to find a sufficient guard, for example), change the loop
2063ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  // comparison to use SLT instead of NE.
2064ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman  Cond = OptimizeSMax(L, Cond, CondUse);
2065ad7321f58a485ae80fe3da1f400aa695e250b1a8Dan Gohman
2066cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  // If possible, change stride and operands of the compare instruction to
2067cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  // eliminate one stride.
2068cdf43b1fadac74ecf1cc858e72bde877a10ceca1Evan Cheng  Cond = ChangeCompareStride(L, Cond, CondUse, CondStride);
2069010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
2070010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // It's possible for the setcc instruction to be anywhere in the loop, and
2071010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // possible for it to have multiple users.  If it is not immediately before
2072010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // the latch block branch, move it.
2073010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) {
2074010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    if (Cond->hasOneUse()) {   // Condition has a single use, just move it.
2075010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->moveBefore(TermBr);
2076010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    } else {
2077010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Otherwise, clone the terminating condition and insert into the loopend.
2078e4d87aa2de6e52952dca73716386db09aad5a8fdReid Spencer      Cond = cast<ICmpInst>(Cond->clone());
2079010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      Cond->setName(L->getHeader()->getName() + ".termcond");
2080010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      LatchBlock->getInstList().insert(TermBr, Cond);
2081010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
2082010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner      // Clone the IVUse, as the old use still exists!
208350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner      IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond,
2084010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner                                         CondUse->OperandValToReplace);
208550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner      CondUse = &IVUsesByStride[*CondStride].Users.back();
2086010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner    }
2087010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  }
2088010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
2089010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // If we get to here, we know that we can transform the setcc instruction to
209098d9811db263ee040d9a6a69ef9e1c4fdc8c219dChris Lattner  // use the post-incremented version of the IV, allowing us to coalesce the
2091010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  // live ranges for the IV correctly.
2092246b2564d3bbbafe06ebf6a67745cd24141b5cb4Dan Gohman  CondUse->Offset = SE->getMinusSCEV(CondUse->Offset, *CondStride);
2093010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner  CondUse->isUseOfPostIncrementedValue = true;
20941ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng  Changed = true;
2095010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner}
2096169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
20970f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patelbool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager &LPM) {
2098eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
20990f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel  LI = &getAnalysis<LoopInfo>();
2100b7d9dfc7ba4ae1ae9482eee62b1912b40dc64f42Devang Patel  DT = &getAnalysis<DominatorTree>();
21010f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel  SE = &getAnalysis<ScalarEvolution>();
21020f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel  TD = &getAnalysis<TargetData>();
21030f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel  UIntPtrTy = TD->getIntPtrType();
21043fea643fb427e72907b7a40e940d7949d1d57e76Dan Gohman  Changed = false;
21050f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel
21060f54dcbf07c69e41ecaa6b4fbf0d94956d8e9ff5Devang Patel  // Find all uses of induction variables in this loop, and catagorize
2107169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // them by stride.  Start by finding all of the PHI nodes in the header for
2108169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  // this loop.  If they are induction variables, inspect their uses.
2109168a66b21e71e4c3301c9d4152d4c683a4bef889Evan Cheng  SmallPtrSet<Instruction*,16> Processed;   // Don't reprocess instructions.
2110169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman  for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
21113416e5f645186a7e3321f927eab662d0ecef404bChris Lattner    AddUsersIfInteresting(I, L, Processed);
2112169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
21131ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng  if (!IVUsesByStride.empty()) {
21141ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // Optimize induction variables.  Some indvar uses can be transformed to use
21151ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // strides that will be needed for other purposes.  A common example of this
21161ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // is the exit test for the loop, which can often be rewritten to use the
21171ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // computation of some other indvar to decide when to terminate the loop.
21181ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    OptimizeIndvars(L);
2119010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
21201ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // FIXME: We can widen subreg IV's here for RISC targets.  e.g. instead of
21211ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // doing computation in byte values, promote to 32-bit values if safe.
2122010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner
21231ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // FIXME: Attempt to reuse values across multiple IV's.  In particular, we
21241ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // could have something like "for(i) { foo(i*8); bar(i*16) }", which should
21251ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // be codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC.
21261ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // Need to be careful that IV's are all the same type.  Only works for
21271ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // intptr_t indvars.
2128169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman
21291ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // If we only have one stride, we can more aggressively eliminate some
21301ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // things.
21311ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    bool HasOneStride = IVUsesByStride.size() == 1;
2132d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
2133d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng#ifndef NDEBUG
21341ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    DOUT << "\nLSR on ";
21351ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    DEBUG(L->dump());
2136d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng#endif
2137d1d6b5cce260808deeac0227b00f6f81a20b2c6fEvan Cheng
21381ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // IVsByStride keeps IVs for one particular loop.
21391ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    assert(IVsByStride.empty() && "Stale entries in IVsByStride?");
21401ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng
21411ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // Sort the StrideOrder so we process larger strides first.
21421ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare());
21431ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng
21441ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // Note: this processes each stride/type pair individually.  All users
21451ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // passed into StrengthReduceStridedIVUsers have the same type AND stride.
21461ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // Also, note that we iterate over IVUsesByStride indirectly by using
21471ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // StrideOrder. This extra layer of indirection makes the ordering of
21481ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    // strides deterministic - not dependent on map order.
21491ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) {
21501ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng      std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
21511ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng        IVUsesByStride.find(StrideOrder[Stride]);
21521ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng      assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
21531ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng      StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
21541ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng    }
21557305ae28df07e0b9c9a8a020a9f9596de25077aaChris Lattner  }
2156eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
2157010ee2d95516fe13a574bce5d682a8f8997ab60bDan Gohman  // We're done analyzing this loop; release all the state we built up for it.
2158010ee2d95516fe13a574bce5d682a8f8997ab60bDan Gohman  CastedPointers.clear();
2159010ee2d95516fe13a574bce5d682a8f8997ab60bDan Gohman  IVUsesByStride.clear();
2160010ee2d95516fe13a574bce5d682a8f8997ab60bDan Gohman  IVsByStride.clear();
2161010ee2d95516fe13a574bce5d682a8f8997ab60bDan Gohman  StrideOrder.clear();
2162010ee2d95516fe13a574bce5d682a8f8997ab60bDan Gohman
2163eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  // Clean up after ourselves
2164eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  if (!DeadInsts.empty()) {
2165a68d4ca73e9cd0b19b2a48a2943e16cc0f89da27Chris Lattner    DeleteTriviallyDeadInstructions();
2166eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman
2167169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman    BasicBlock::iterator I = L->getHeader()->begin();
2168cbfe5bbe88f5f2ee03a388585112f7609c8151adDan Gohman    while (PHINode *PN = dyn_cast<PHINode>(I++)) {
2169cbfe5bbe88f5f2ee03a388585112f7609c8151adDan Gohman      // At this point, we know that we have killed one or more IV users.
2170bfcee36cd747bf9f791ba7aa3e9e8ac3671c6822Chris Lattner      // It is worth checking to see if the cannonical indvar is also
2171cbfe5bbe88f5f2ee03a388585112f7609c8151adDan Gohman      // dead, so that we can remove it as well.
2172cbfe5bbe88f5f2ee03a388585112f7609c8151adDan Gohman      //
2173cbfe5bbe88f5f2ee03a388585112f7609c8151adDan Gohman      // We can remove a PHI if it is on a cycle in the def-use graph
2174cbfe5bbe88f5f2ee03a388585112f7609c8151adDan Gohman      // where each node in the cycle has degree one, i.e. only one use,
2175cbfe5bbe88f5f2ee03a388585112f7609c8151adDan Gohman      // and is an instruction with no side effects.
2176cbfe5bbe88f5f2ee03a388585112f7609c8151adDan Gohman      //
2177169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // FIXME: this needs to eliminate an induction variable even if it's being
2178169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      // compared against some value to decide loop termination.
2179a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner      if (!PN->hasOneUse())
2180a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner        continue;
2181a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner
2182a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner      SmallPtrSet<PHINode *, 4> PHIs;
2183a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner      for (Instruction *J = dyn_cast<Instruction>(*PN->use_begin());
2184a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner           J && J->hasOneUse() && !J->mayWriteToMemory();
2185a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner           J = dyn_cast<Instruction>(*J->use_begin())) {
2186a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner        // If we find the original PHI, we've discovered a cycle.
2187a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner        if (J == PN) {
2188a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner          // Break the cycle and mark the PHI for deletion.
2189a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner          SE->deleteValueFromRecords(PN);
2190a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner          PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
219109fb7dadf1f8f2efaae6a803c63fb29d06105df3Chris Lattner          DeadInsts.push_back(PN);
2192a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner          Changed = true;
2193a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner          break;
21947e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner        }
2195a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner        // If we find a PHI more than once, we're on a cycle that
2196a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner        // won't prove fruitful.
2197a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner        if (isa<PHINode>(J) && !PHIs.insert(cast<PHINode>(J)))
2198a0d4486073f6303fbf33b50ac3fbae4601ea0229Chris Lattner          break;
2199169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman      }
2200eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman    }
2201a68d4ca73e9cd0b19b2a48a2943e16cc0f89da27Chris Lattner    DeleteTriviallyDeadInstructions();
2202eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman  }
22031ce75dcbbcb6a67904a23b4ec701d1e994767c7eEvan Cheng  return Changed;
2204eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman}
2205