LoopStrengthReduce.cpp revision 37edbf0b21959e176672ae51f8b7e86ba1ef727d
1eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===// 2fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 3eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// The LLVM Compiler Infrastructure 4eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// 5eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// This file was developed by Nate Begeman and is distributed under the 6eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// University of Illinois Open Source License. See LICENSE.TXT for details. 7fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman// 8eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===----------------------------------------------------------------------===// 9eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// 10eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// This pass performs a strength reduction on array references inside loops that 11eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// have as one or more of their components the loop induction variable. This is 12eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// accomplished by creating a new Value to hold the initial value of the array 13eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// access for the first iteration, and then creating a new GEP instruction in 14eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// the loop to increment the value by the appropriate amount. 15eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman// 16eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman//===----------------------------------------------------------------------===// 17eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 18be3e5212e23edc9210f774fac18d801de252e906Chris Lattner#define DEBUG_TYPE "loop-reduce" 19eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Transforms/Scalar.h" 20eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Constants.h" 21eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Instructions.h" 22eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Type.h" 232f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen#include "llvm/DerivedTypes.h" 24eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Analysis/Dominators.h" 25eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Analysis/LoopInfo.h" 26169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Analysis/ScalarEvolutionExpander.h" 27eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Support/CFG.h" 28169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Support/GetElementPtrTypeIterator.h" 29e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner#include "llvm/Transforms/Utils/BasicBlockUtils.h" 30eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/Transforms/Utils/Local.h" 312f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen#include "llvm/Target/TargetData.h" 32eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include "llvm/ADT/Statistic.h" 33169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman#include "llvm/Support/Debug.h" 34cfb1d4235fe3291028341e6acf4139723b4749e3Jeff Cohen#include <algorithm> 35eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman#include <set> 36eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanusing namespace llvm; 37eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 38eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemannamespace { 39eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced"); 4026d91f16464db56283087176a73981048331dd2dChris Lattner Statistic<> NumInserted("loop-reduce", "Number of PHIs inserted"); 4150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner Statistic<> NumVariable("loop-reduce","Number of PHIs with variable strides"); 42eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 43ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// IVStrideUse - Keep track of one use of a strided induction variable, where 44ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// the stride is stored externally. The Offset member keeps track of the 45ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// offset from the IV, User is the actual user of the operand, and 'Operand' 46ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// is the operand # of the User that is the use. 47ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner struct IVStrideUse { 48ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner SCEVHandle Offset; 49ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner Instruction *User; 50ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner Value *OperandValToReplace; 51010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 52010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // isUseOfPostIncrementedValue - True if this should use the 53010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // post-incremented version of this IV, not the preincremented version. 54010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // This can only be set in special cases, such as the terminating setcc 55c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // instruction for a loop or uses dominated by the loop. 56010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner bool isUseOfPostIncrementedValue; 57ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner 58ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O) 59010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner : Offset(Offs), User(U), OperandValToReplace(O), 60010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner isUseOfPostIncrementedValue(false) {} 61ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner }; 62ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner 63ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// IVUsersOfOneStride - This structure keeps track of all instructions that 64ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// have an operand that is based on the trip count multiplied by some stride. 65ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// The stride for all of these users is common and kept external to this 66ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// structure. 67ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner struct IVUsersOfOneStride { 68169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// Users - Keep track of all of the users of this stride as well as the 69ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// initial value and the operand that uses the IV. 70ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner std::vector<IVStrideUse> Users; 71ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner 72ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) { 73ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner Users.push_back(IVStrideUse(Offset, User, Operand)); 74169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman } 75169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman }; 76169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 77169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 78eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman class LoopStrengthReduce : public FunctionPass { 79eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman LoopInfo *LI; 80eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman DominatorSet *DS; 81169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman ScalarEvolution *SE; 82169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman const TargetData *TD; 83169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman const Type *UIntPtrTy; 84eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman bool Changed; 857e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner 867e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner /// MaxTargetAMSize - This is the maximum power-of-two scale value that the 877e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner /// target can handle for free with its addressing modes. 882f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen unsigned MaxTargetAMSize; 89169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 90169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// IVUsesByStride - Keep track of all uses of induction variables that we 91169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// are interested in. The key of the map is the stride of the access. 9250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner std::map<SCEVHandle, IVUsersOfOneStride> IVUsesByStride; 93169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 9449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner /// CastedValues - As we need to cast values to uintptr_t, this keeps track 9549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner /// of the casted version of each value. This is accessed by 9649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner /// getCastedVersionOf. 9749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner std::map<Value*, Value*> CastedPointers; 98169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 99169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// DeadInsts - Keep track of instructions we may have made dead, so that 100169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// we can remove them after we are done working. 101169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman std::set<Instruction*> DeadInsts; 102eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman public: 1032f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen LoopStrengthReduce(unsigned MTAMS = 1) 1042f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen : MaxTargetAMSize(MTAMS) { 1052f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen } 1062f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen 107eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman virtual bool runOnFunction(Function &) { 108eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman LI = &getAnalysis<LoopInfo>(); 109eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman DS = &getAnalysis<DominatorSet>(); 110169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman SE = &getAnalysis<ScalarEvolution>(); 111169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman TD = &getAnalysis<TargetData>(); 112169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman UIntPtrTy = TD->getIntPtrType(); 113eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman Changed = false; 114eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 115eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) 116eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman runOnLoop(*I); 11749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner 118eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman return Changed; 119eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman } 120eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 121eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman virtual void getAnalysisUsage(AnalysisUsage &AU) const { 122aa96ae780afa5475e62df284855a971216289212Chris Lattner // We split critical edges, so we change the CFG. However, we do update 123aa96ae780afa5475e62df284855a971216289212Chris Lattner // many analyses if they are around. 124aa96ae780afa5475e62df284855a971216289212Chris Lattner AU.addPreservedID(LoopSimplifyID); 125aa96ae780afa5475e62df284855a971216289212Chris Lattner AU.addPreserved<LoopInfo>(); 126aa96ae780afa5475e62df284855a971216289212Chris Lattner AU.addPreserved<DominatorSet>(); 127aa96ae780afa5475e62df284855a971216289212Chris Lattner AU.addPreserved<ImmediateDominators>(); 128aa96ae780afa5475e62df284855a971216289212Chris Lattner AU.addPreserved<DominanceFrontier>(); 129aa96ae780afa5475e62df284855a971216289212Chris Lattner AU.addPreserved<DominatorTree>(); 130aa96ae780afa5475e62df284855a971216289212Chris Lattner 131f465db6c6a5a877aa791abfc3837d62c491dacd5Jeff Cohen AU.addRequiredID(LoopSimplifyID); 132eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman AU.addRequired<LoopInfo>(); 133eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman AU.addRequired<DominatorSet>(); 1342f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen AU.addRequired<TargetData>(); 135169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman AU.addRequired<ScalarEvolution>(); 136eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman } 13749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner 13849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner /// getCastedVersionOf - Return the specified value casted to uintptr_t. 13949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner /// 14049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner Value *getCastedVersionOf(Value *V); 14149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattnerprivate: 142eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman void runOnLoop(Loop *L); 1433416e5f645186a7e3321f927eab662d0ecef404bChris Lattner bool AddUsersIfInteresting(Instruction *I, Loop *L, 1443416e5f645186a7e3321f927eab662d0ecef404bChris Lattner std::set<Instruction*> &Processed); 1453416e5f645186a7e3321f927eab662d0ecef404bChris Lattner SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L); 1463416e5f645186a7e3321f927eab662d0ecef404bChris Lattner 147010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner void OptimizeIndvars(Loop *L); 148169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 14950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner void StrengthReduceStridedIVUsers(const SCEVHandle &Stride, 15050fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner IVUsersOfOneStride &Uses, 151ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner Loop *L, bool isOnlyStride); 152eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts); 153eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman }; 154fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman RegisterOpt<LoopStrengthReduce> X("loop-reduce", 155fe15830f962bb7fef046203a77d438d087772b34Chris Lattner "Loop Strength Reduction"); 156eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman} 157eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 1582f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff CohenFunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) { 1592f3c9b7562bcdc1795b2bd0ca28b283a8e972826Jeff Cohen return new LoopStrengthReduce(MaxTargetAMSize); 160eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman} 161eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 16249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner/// getCastedVersionOf - Return the specified value casted to uintptr_t. 16349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner/// 16449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris LattnerValue *LoopStrengthReduce::getCastedVersionOf(Value *V) { 16549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner if (V->getType() == UIntPtrTy) return V; 16649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner if (Constant *CB = dyn_cast<Constant>(V)) 16749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner return ConstantExpr::getCast(CB, UIntPtrTy); 16849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner 16949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner Value *&New = CastedPointers[V]; 17049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner if (New) return New; 17149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner 17249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner BasicBlock::iterator InsertPt; 17349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner if (Argument *Arg = dyn_cast<Argument>(V)) { 17449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner // Insert into the entry of the function, after any allocas. 17549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner InsertPt = Arg->getParent()->begin()->begin(); 17649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner while (isa<AllocaInst>(InsertPt)) ++InsertPt; 17749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner } else { 17849f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner if (InvokeInst *II = dyn_cast<InvokeInst>(V)) { 17949f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner InsertPt = II->getNormalDest()->begin(); 18049f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner } else { 18149f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner InsertPt = cast<Instruction>(V); 18249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner ++InsertPt; 18349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner } 18449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner 18549f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner // Do not insert casts into the middle of PHI node blocks. 18649f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner while (isa<PHINode>(InsertPt)) ++InsertPt; 18749f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner } 1887db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 1897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner New = new CastInst(V, UIntPtrTy, V->getName(), InsertPt); 1907db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner DeadInsts.insert(cast<Instruction>(New)); 1917db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner return New; 19249f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner} 19349f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner 19449f72e68cf6eb77b5791310513d94dc64dc6ea7dChris Lattner 195eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// DeleteTriviallyDeadInstructions - If any of the instructions is the 196eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// specified set are trivially dead, delete them and see if this makes any of 197eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman/// their operands subsequently dead. 198eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce:: 199eaa13851a7fe604363577350c5cf65c257c4d41aNate BegemanDeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) { 200eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman while (!Insts.empty()) { 201eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman Instruction *I = *Insts.begin(); 202eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman Insts.erase(Insts.begin()); 203eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman if (isInstructionTriviallyDead(I)) { 2040456e4a079de51087978c177b1de63343731f4fbJeff Cohen for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 2050456e4a079de51087978c177b1de63343731f4fbJeff Cohen if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) 2060456e4a079de51087978c177b1de63343731f4fbJeff Cohen Insts.insert(U); 20752d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner SE->deleteInstructionFromRecords(I); 20852d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner I->eraseFromParent(); 209eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman Changed = true; 210eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman } 211eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman } 212eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman} 213eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 214169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 2153416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// GetExpressionSCEV - Compute and return the SCEV for the specified 2163416e5f645186a7e3321f927eab662d0ecef404bChris Lattner/// instruction. 2173416e5f645186a7e3321f927eab662d0ecef404bChris LattnerSCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) { 21887265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions. 21987265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner // If this is a GEP that SE doesn't know about, compute it now and insert it. 22087265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner // If this is not a GEP, or if we have already done this computation, just let 22187265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner // SE figure it out. 2223416e5f645186a7e3321f927eab662d0ecef404bChris Lattner GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp); 22387265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner if (!GEP || SE->hasSCEV(GEP)) 2243416e5f645186a7e3321f927eab662d0ecef404bChris Lattner return SE->getSCEV(Exp); 2253416e5f645186a7e3321f927eab662d0ecef404bChris Lattner 226169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // Analyze all of the subscripts of this getelementptr instruction, looking 227169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // for uses that are determined by the trip count of L. First, skip all 228169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // operands the are not dependent on the IV. 229169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 230169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // Build up the base expression. Insert an LLVM cast of the pointer to 231169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // uintptr_t first. 2323416e5f645186a7e3321f927eab662d0ecef404bChris Lattner SCEVHandle GEPVal = SCEVUnknown::get(getCastedVersionOf(GEP->getOperand(0))); 233169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 234169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman gep_type_iterator GTI = gep_type_begin(GEP); 2353416e5f645186a7e3321f927eab662d0ecef404bChris Lattner 2363416e5f645186a7e3321f927eab662d0ecef404bChris Lattner for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) { 237169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // If this is a use of a recurrence that we can analyze, and it comes before 238169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // Op does in the GEP operand list, we will handle this when we process this 239169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // operand. 240169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman if (const StructType *STy = dyn_cast<StructType>(*GTI)) { 241169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman const StructLayout *SL = TD->getStructLayout(STy); 242169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman unsigned Idx = cast<ConstantUInt>(GEP->getOperand(i))->getValue(); 243169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman uint64_t Offset = SL->MemberOffsets[Idx]; 2443416e5f645186a7e3321f927eab662d0ecef404bChris Lattner GEPVal = SCEVAddExpr::get(GEPVal, 2453416e5f645186a7e3321f927eab662d0ecef404bChris Lattner SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy)); 2462f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner } else { 2477db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner Value *OpVal = getCastedVersionOf(GEP->getOperand(i)); 2487db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner SCEVHandle Idx = SE->getSCEV(OpVal); 2497db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 2503416e5f645186a7e3321f927eab662d0ecef404bChris Lattner uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType()); 2513416e5f645186a7e3321f927eab662d0ecef404bChris Lattner if (TypeSize != 1) 2523416e5f645186a7e3321f927eab662d0ecef404bChris Lattner Idx = SCEVMulExpr::get(Idx, 2533416e5f645186a7e3321f927eab662d0ecef404bChris Lattner SCEVConstant::get(ConstantUInt::get(UIntPtrTy, 2543416e5f645186a7e3321f927eab662d0ecef404bChris Lattner TypeSize))); 2553416e5f645186a7e3321f927eab662d0ecef404bChris Lattner GEPVal = SCEVAddExpr::get(GEPVal, Idx); 2562f62fdc9a71d790d381d7f17d16e7098e30217e3Chris Lattner } 257eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman } 258169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 25987265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner SE->setSCEV(GEP, GEPVal); 2603416e5f645186a7e3321f927eab662d0ecef404bChris Lattner return GEPVal; 261169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 262169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 2637db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// getSCEVStartAndStride - Compute the start and stride of this expression, 2647db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// returning false if the expression is not a start/stride pair, or true if it 2657db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// is. The stride must be a loop invariant expression, but the start may be 2667db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner/// a mix of loop invariant and loop variant expressions. 2677db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattnerstatic bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L, 26850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner SCEVHandle &Start, SCEVHandle &Stride) { 2697db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner SCEVHandle TheAddRec = Start; // Initialize to zero. 2707db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 2717db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner // If the outer level is an AddExpr, the operands are all start values except 2727db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner // for a nested AddRecExpr. 2737db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) { 2747db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i) 2757db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (SCEVAddRecExpr *AddRec = 2767db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) { 2777db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (AddRec->getLoop() == L) 2787db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner TheAddRec = SCEVAddExpr::get(AddRec, TheAddRec); 2797db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner else 2807db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner return false; // Nested IV of some sort? 2817db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner } else { 2827db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner Start = SCEVAddExpr::get(Start, AE->getOperand(i)); 2837db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner } 2847db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 2857db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner } else if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SH)) { 2867db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner TheAddRec = SH; 2877db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner } else { 2887db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner return false; // not analyzable. 2897db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner } 2907db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 2917db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec); 2927db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (!AddRec || AddRec->getLoop() != L) return false; 2937db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 2947db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner // FIXME: Generalize to non-affine IV's. 2957db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (!AddRec->isAffine()) return false; 2967db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 2977db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner Start = SCEVAddExpr::get(Start, AddRec->getOperand(0)); 2987db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 2997db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (!isa<SCEVConstant>(AddRec->getOperand(1))) 30050fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner DEBUG(std::cerr << "[" << L->getHeader()->getName() 30150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner << "] Variable stride: " << *AddRec << "\n"); 30250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner 30350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner Stride = AddRec->getOperand(1); 30450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner // Check that all constant strides are the unsigned type, we don't want to 30550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner // have two IV's one of signed stride 4 and one of unsigned stride 4 to not be 30650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner // merged. 30750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner assert((!isa<SCEVConstant>(Stride) || Stride->getType()->isUnsigned()) && 3087db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner "Constants should be canonicalized to unsigned!"); 30950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner 3107db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner return true; 3117db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner} 3127db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 313169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// AddUsersIfInteresting - Inspect the specified instruction. If it is a 314169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// reducible SCEV, recursively add its users to the IVUsesByStride set and 315169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// return true. Otherwise, return false. 3163416e5f645186a7e3321f927eab662d0ecef404bChris Lattnerbool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L, 3173416e5f645186a7e3321f927eab662d0ecef404bChris Lattner std::set<Instruction*> &Processed) { 318f84d5ab5df5900dcaf90df8d83c16b6cea22f087Nate Begeman if (I->getType() == Type::VoidTy) return false; 3193416e5f645186a7e3321f927eab662d0ecef404bChris Lattner if (!Processed.insert(I).second) 3203416e5f645186a7e3321f927eab662d0ecef404bChris Lattner return true; // Instruction already handled. 3213416e5f645186a7e3321f927eab662d0ecef404bChris Lattner 3227db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner // Get the symbolic expression for this instruction. 3233416e5f645186a7e3321f927eab662d0ecef404bChris Lattner SCEVHandle ISE = GetExpressionSCEV(I, L); 3247db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (isa<SCEVCouldNotCompute>(ISE)) return false; 3257db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner 3267db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner // Get the start and stride for this expression. 3277db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner SCEVHandle Start = SCEVUnknown::getIntegerSCEV(0, ISE->getType()); 32850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner SCEVHandle Stride = Start; 3297db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (!getSCEVStartAndStride(ISE, L, Start, Stride)) 3307db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner return false; // Non-reducible symbolic expression, bail out. 3313416e5f645186a7e3321f927eab662d0ecef404bChris Lattner 332169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;++UI){ 333169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman Instruction *User = cast<Instruction>(*UI); 334169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 335169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // Do not infinitely recurse on PHI nodes. 336396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner if (isa<PHINode>(User) && Processed.count(User)) 337169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman continue; 338169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 339169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // If this is an instruction defined in a nested loop, or outside this loop, 340f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner // don't recurse into it. 3417db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner bool AddUserToIVUsers = false; 342f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner if (LI->getLoopFor(User->getParent()) != L) { 343396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner DEBUG(std::cerr << "FOUND USER in other loop: " << *User 344f9186596f06f2c1c5357420d44e2fe2f38f98068Chris Lattner << " OF SCEV: " << *ISE << "\n"); 3457db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner AddUserToIVUsers = true; 3463416e5f645186a7e3321f927eab662d0ecef404bChris Lattner } else if (!AddUsersIfInteresting(User, L, Processed)) { 347a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner DEBUG(std::cerr << "FOUND USER: " << *User 348a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner << " OF SCEV: " << *ISE << "\n"); 3497db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner AddUserToIVUsers = true; 3507db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner } 351fd93908ae8b9684fe71c239e3c6cfe13ff6a2663Misha Brukman 3527db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (AddUserToIVUsers) { 353a4479ad25f7f184fc4600beb1d39fd1e71849c4dChris Lattner // Okay, we found a user that we cannot reduce. Analyze the instruction 354c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // and decide what to do with it. If we are a use inside of the loop, use 355c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // the value before incrementation, otherwise use it after incrementation. 35612b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner if (L->contains(User->getParent()) || 35712b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner // Alternatively, if we are a use outside of the loop, but is not 35812b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner // dominated by the latch block, we have to use the preincremented 35912b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner // value. 36012b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner !DS->dominates(L->getLoopLatch(), User->getParent())) { 361c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner IVUsesByStride[Stride].addUser(Start, User, I); 362c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner } else { 363c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // The value used will be incremented by the stride more than we are 364c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // expecting, so subtract this off. 365c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner SCEVHandle NewStart = SCEV::getMinusSCEV(Start, Stride); 366c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner IVUsesByStride[Stride].addUser(NewStart, User, I); 367c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner IVUsesByStride[Stride].Users.back().isUseOfPostIncrementedValue = true; 368c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner } 369169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman } 370169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman } 371169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman return true; 372169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 373169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 374169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemannamespace { 375169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// BasedUser - For a particular base value, keep information about how we've 376169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// partitioned the expression so far. 377169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman struct BasedUser { 378a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner /// Base - The Base value for the PHI node that needs to be inserted for 379a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner /// this use. As the use is processed, information gets moved from this 380a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner /// field to the Imm field (below). BasedUser values are sorted by this 381a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner /// field. 382a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner SCEVHandle Base; 383a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner 384169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// Inst - The instruction using the induction variable. 385169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman Instruction *Inst; 386169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 387ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// OperandValToReplace - The operand value of Inst to replace with the 388ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner /// EmittedBase. 389ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner Value *OperandValToReplace; 390169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 391169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// Imm - The immediate value that should be added to the base immediately 392169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// before Inst, because it will be folded into the imm field of the 393169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// instruction. 394169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman SCEVHandle Imm; 395169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 396169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// EmittedBase - The actual value* to use for the base value of this 397169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman /// operation. This is null if we should just use zero so far. 398169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman Value *EmittedBase; 399169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 400010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // isUseOfPostIncrementedValue - True if this should use the 401010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // post-incremented version of this IV, not the preincremented version. 402010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // This can only be set in special cases, such as the terminating setcc 403c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // instruction for a loop and uses outside the loop that are dominated by 404c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // the loop. 405010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner bool isUseOfPostIncrementedValue; 406a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner 407a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner BasedUser(IVStrideUse &IVSU) 408a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner : Base(IVSU.Offset), Inst(IVSU.User), 409a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner OperandValToReplace(IVSU.OperandValToReplace), 410a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner Imm(SCEVUnknown::getIntegerSCEV(0, Base->getType())), EmittedBase(0), 411a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {} 412169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 4132114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner // Once we rewrite the code to insert the new IVs we want, update the 4142114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner // operands of Inst to use the new expression 'NewBase', with 'Imm' added 4152114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner // to it. 4161bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase, 417c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner SCEVExpander &Rewriter, Loop *L, 418c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner Pass *P); 419169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 420a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner // Sort by the Base field. 421a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner bool operator<(const BasedUser &BU) const { return Base < BU.Base; } 422169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 423169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman void dump() const; 424169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman }; 425169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 426169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 427169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanvoid BasedUser::dump() const { 428a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner std::cerr << " Base=" << *Base; 429169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman std::cerr << " Imm=" << *Imm; 430169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman if (EmittedBase) 431169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman std::cerr << " EB=" << *EmittedBase; 432169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 433169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman std::cerr << " Inst: " << *Inst; 434169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 435169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 4362114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// Once we rewrite the code to insert the new IVs we want, update the 4372114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// operands of Inst to use the new expression 'NewBase', with 'Imm' added 4382114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner// to it. 4391bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnervoid BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase, 440e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner SCEVExpander &Rewriter, 441c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner Loop *L, Pass *P) { 4422114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner if (!isa<PHINode>(Inst)) { 4431bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SCEVHandle NewValSCEV = SCEVAddExpr::get(NewBase, Imm); 4442114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner Value *NewVal = Rewriter.expandCodeFor(NewValSCEV, Inst, 4452114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner OperandValToReplace->getType()); 4462114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner // Replace the use of the operand Value with the new Phi we just created. 4472114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner Inst->replaceUsesOfWith(OperandValToReplace, NewVal); 4482114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner DEBUG(std::cerr << " CHANGED: IMM =" << *Imm << " Inst = " << *Inst); 4492114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner return; 4502114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner } 4512114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner 4522114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner // PHI nodes are more complex. We have to insert one copy of the NewBase+Imm 453c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner // expression into each operand block that uses it. Note that PHI nodes can 454c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner // have multiple entries for the same predecessor. We use a map to make sure 455c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner // that a PHI node only has a single Value* for each predecessor (which also 456c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner // prevents us from inserting duplicate code in some blocks). 457c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner std::map<BasicBlock*, Value*> InsertedCode; 4582114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner PHINode *PN = cast<PHINode>(Inst); 4592114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 4602114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner if (PN->getIncomingValue(i) == OperandValToReplace) { 461e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner // If this is a critical edge, split the edge so that we do not insert the 462396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner // code on all predecessor/successor paths. We do this unless this is the 463396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner // canonical backedge for this loop, as this can make some inserted code 464396b2baf3ca0ee46e696713d34943035206d7a46Chris Lattner // be in an illegal position. 46537edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner BasicBlock *PHIPred = PN->getIncomingBlock(i); 46637edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 && 46737edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) { 468aa96ae780afa5475e62df284855a971216289212Chris Lattner 46937edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner 470aa96ae780afa5475e62df284855a971216289212Chris Lattner // First step, split the critical edge. 47137edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner SplitCriticalEdge(PHIPred, PN->getParent(), P); 472c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner 473aa96ae780afa5475e62df284855a971216289212Chris Lattner // Next step: move the basic block. In particular, if the PHI node 474aa96ae780afa5475e62df284855a971216289212Chris Lattner // is outside of the loop, and PredTI is in the loop, we want to 475aa96ae780afa5475e62df284855a971216289212Chris Lattner // move the block to be immediately before the PHI block, not 476aa96ae780afa5475e62df284855a971216289212Chris Lattner // immediately after PredTI. 47737edbf0b21959e176672ae51f8b7e86ba1ef727dChris Lattner if (L->contains(PHIPred) && !L->contains(PN->getParent())) { 478aa96ae780afa5475e62df284855a971216289212Chris Lattner BasicBlock *NewBB = PN->getIncomingBlock(i); 479aa96ae780afa5475e62df284855a971216289212Chris Lattner NewBB->moveBefore(PN->getParent()); 480e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner } 481aa96ae780afa5475e62df284855a971216289212Chris Lattner break; 482e0391beda88c6c441ce1aadbe223d6c0784061a2Chris Lattner } 4832114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner 484c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner Value *&Code = InsertedCode[PN->getIncomingBlock(i)]; 485c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner if (!Code) { 486c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner // Insert the code into the end of the predecessor block. 487c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner BasicBlock::iterator InsertPt =PN->getIncomingBlock(i)->getTerminator(); 4882114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner 489c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner SCEVHandle NewValSCEV = SCEVAddExpr::get(NewBase, Imm); 490c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner Code = Rewriter.expandCodeFor(NewValSCEV, InsertPt, 491c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner OperandValToReplace->getType()); 492c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner } 4932114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner 4942114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner // Replace the use of the operand Value with the new Phi we just created. 495c41e34520ab2f8c0bfe2f95546745826f6b34d59Chris Lattner PN->setIncomingValue(i, Code); 4962114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner Rewriter.clear(); 4972114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner } 4982114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner } 4992114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner DEBUG(std::cerr << " CHANGED: IMM =" << *Imm << " Inst = " << *Inst); 5002114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner} 5012114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner 5022114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner 503169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// isTargetConstant - Return true if the following can be referenced by the 504169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// immediate field of a target instruction. 505169974856781a1ce27af9ce6220c390b20c9e6ddNate Begemanstatic bool isTargetConstant(const SCEVHandle &V) { 506d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen 507169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // FIXME: Look at the target to decide if &GV is a legal constant immediate. 5083821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) { 5093821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner // PPC allows a sign-extended 16-bit immediate field. 5103821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner if ((int64_t)SC->getValue()->getRawValue() > -(1 << 16) && 5113821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner (int64_t)SC->getValue()->getRawValue() < (1 << 16)-1) 5123821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner return true; 5133821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner return false; 5143821e478a574b80d7f8bc96fa42731291cfccfe8Chris Lattner } 515d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen 516169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman return false; // ENABLE this for x86 517d29b6aa608d69f19b57ebd2ae630b040b1c4951dJeff Cohen 518169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) 519169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue())) 520169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman if (CE->getOpcode() == Instruction::Cast) 521169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman if (isa<GlobalValue>(CE->getOperand(0))) 522169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // FIXME: should check to see that the dest is uintptr_t! 523169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman return true; 524169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman return false; 525169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 526169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 52744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// MoveLoopVariantsToImediateField - Move any subexpressions from Val that are 52844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner/// loop varying to the Imm operand. 52944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattnerstatic void MoveLoopVariantsToImediateField(SCEVHandle &Val, SCEVHandle &Imm, 53044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner Loop *L) { 53144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner if (Val->isLoopInvariant(L)) return; // Nothing to do. 53244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 53344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { 53444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner std::vector<SCEVHandle> NewOps; 53544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner NewOps.reserve(SAE->getNumOperands()); 53644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 53744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner for (unsigned i = 0; i != SAE->getNumOperands(); ++i) 53844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner if (!SAE->getOperand(i)->isLoopInvariant(L)) { 53944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner // If this is a loop-variant expression, it must stay in the immediate 54044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner // field of the expression. 54144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i)); 54244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner } else { 54344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner NewOps.push_back(SAE->getOperand(i)); 54444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner } 54544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 54644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner if (NewOps.empty()) 54744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner Val = SCEVUnknown::getIntegerSCEV(0, Val->getType()); 54844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner else 54944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner Val = SCEVAddExpr::get(NewOps); 55044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { 55144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner // Try to pull immediates out of the start value of nested addrec's. 55244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner SCEVHandle Start = SARE->getStart(); 55344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner MoveLoopVariantsToImediateField(Start, Imm, L); 55444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 55544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); 55644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner Ops[0] = Start; 55744b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner Val = SCEVAddRecExpr::get(Ops, SARE->getLoop()); 55844b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner } else { 55944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner // Otherwise, all of Val is variant, move the whole thing over. 56044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner Imm = SCEVAddExpr::get(Imm, Val); 56144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner Val = SCEVUnknown::getIntegerSCEV(0, Val->getType()); 56244b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner } 56344b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner} 56444b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 56544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 56626d91f16464db56283087176a73981048331dd2dChris Lattner/// MoveImmediateValues - Look at Val, and pull out any additions of constants 567169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// that can fit into the immediate field of instructions in the target. 56826d91f16464db56283087176a73981048331dd2dChris Lattner/// Accumulate these immediate values into the Imm value. 56926d91f16464db56283087176a73981048331dd2dChris Lattnerstatic void MoveImmediateValues(SCEVHandle &Val, SCEVHandle &Imm, 57026d91f16464db56283087176a73981048331dd2dChris Lattner bool isAddress, Loop *L) { 5717a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) { 57226d91f16464db56283087176a73981048331dd2dChris Lattner std::vector<SCEVHandle> NewOps; 57326d91f16464db56283087176a73981048331dd2dChris Lattner NewOps.reserve(SAE->getNumOperands()); 57426d91f16464db56283087176a73981048331dd2dChris Lattner 57526d91f16464db56283087176a73981048331dd2dChris Lattner for (unsigned i = 0; i != SAE->getNumOperands(); ++i) 5767db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner if (isAddress && isTargetConstant(SAE->getOperand(i))) { 5777db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i)); 5787db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner } else if (!SAE->getOperand(i)->isLoopInvariant(L)) { 5797db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner // If this is a loop-variant expression, it must stay in the immediate 5807db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner // field of the expression. 5817db543f887c83aad2a95814a16d363a2313fc2a8Chris Lattner Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i)); 58226d91f16464db56283087176a73981048331dd2dChris Lattner } else { 58326d91f16464db56283087176a73981048331dd2dChris Lattner NewOps.push_back(SAE->getOperand(i)); 584169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman } 58526d91f16464db56283087176a73981048331dd2dChris Lattner 58626d91f16464db56283087176a73981048331dd2dChris Lattner if (NewOps.empty()) 58726d91f16464db56283087176a73981048331dd2dChris Lattner Val = SCEVUnknown::getIntegerSCEV(0, Val->getType()); 58826d91f16464db56283087176a73981048331dd2dChris Lattner else 58926d91f16464db56283087176a73981048331dd2dChris Lattner Val = SCEVAddExpr::get(NewOps); 59026d91f16464db56283087176a73981048331dd2dChris Lattner return; 5917a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) { 5927a65839f4118c99fb1636c3cbb41b0bf7ef27665Chris Lattner // Try to pull immediates out of the start value of nested addrec's. 59326d91f16464db56283087176a73981048331dd2dChris Lattner SCEVHandle Start = SARE->getStart(); 59426d91f16464db56283087176a73981048331dd2dChris Lattner MoveImmediateValues(Start, Imm, isAddress, L); 59526d91f16464db56283087176a73981048331dd2dChris Lattner 59626d91f16464db56283087176a73981048331dd2dChris Lattner if (Start != SARE->getStart()) { 59726d91f16464db56283087176a73981048331dd2dChris Lattner std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); 59826d91f16464db56283087176a73981048331dd2dChris Lattner Ops[0] = Start; 59926d91f16464db56283087176a73981048331dd2dChris Lattner Val = SCEVAddRecExpr::get(Ops, SARE->getLoop()); 60026d91f16464db56283087176a73981048331dd2dChris Lattner } 60126d91f16464db56283087176a73981048331dd2dChris Lattner return; 602169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman } 603169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 60426d91f16464db56283087176a73981048331dd2dChris Lattner // Loop-variant expressions must stay in the immediate field of the 60526d91f16464db56283087176a73981048331dd2dChris Lattner // expression. 60626d91f16464db56283087176a73981048331dd2dChris Lattner if ((isAddress && isTargetConstant(Val)) || 60726d91f16464db56283087176a73981048331dd2dChris Lattner !Val->isLoopInvariant(L)) { 60826d91f16464db56283087176a73981048331dd2dChris Lattner Imm = SCEVAddExpr::get(Imm, Val); 60926d91f16464db56283087176a73981048331dd2dChris Lattner Val = SCEVUnknown::getIntegerSCEV(0, Val->getType()); 61026d91f16464db56283087176a73981048331dd2dChris Lattner return; 6117a2ca56ef3bdda6874bcd4df2910fb5a33999f85Chris Lattner } 61226d91f16464db56283087176a73981048331dd2dChris Lattner 61326d91f16464db56283087176a73981048331dd2dChris Lattner // Otherwise, no immediates to move. 614169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman} 615169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 616934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 617934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// IncrementAddExprUses - Decompose the specified expression into its added 618934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// subexpressions, and increment SubExpressionUseCounts for each of these 619934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner/// decomposed parts. 620934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattnerstatic void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs, 621934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SCEVHandle Expr) { 622934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) { 623934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j) 624934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SeparateSubExprs(SubExprs, AE->getOperand(j)); 625934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) { 626934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Expr->getType()); 627934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner if (SARE->getOperand(0) == Zero) { 628934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SubExprs.push_back(Expr); 629934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } else { 630934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // Compute the addrec with zero as its base. 631934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end()); 632934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner Ops[0] = Zero; // Start with zero base. 633934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SubExprs.push_back(SCEVAddRecExpr::get(Ops, SARE->getLoop())); 634934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 635934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 636934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SeparateSubExprs(SubExprs, SARE->getOperand(0)); 637934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } 638934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } else if (!isa<SCEVConstant>(Expr) || 639934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner !cast<SCEVConstant>(Expr)->getValue()->isNullValue()) { 640934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // Do not add zero. 641934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SubExprs.push_back(Expr); 642934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } 643934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner} 644934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 645934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 6461bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// RemoveCommonExpressionsFromUseBases - Look through all of the uses in Bases, 6471bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// removing any common subexpressions from it. Anything truly common is 6481bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// removed, accumulated, and returned. This looks for things like (a+b+c) and 6491bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner/// (a+c+d) -> (a+c). The common expression is *removed* from the Bases. 6501bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattnerstatic SCEVHandle 6511bbae0cbf212d0356f564d12e8f728be455bdc47Chris LattnerRemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses) { 6521bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner unsigned NumUses = Uses.size(); 6531bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 6541bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Only one use? Use its base, regardless of what it is! 6551bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Uses[0].Base->getType()); 6561bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SCEVHandle Result = Zero; 6571bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner if (NumUses == 1) { 6581bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner std::swap(Result, Uses[0].Base); 6591bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner return Result; 6601bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner } 6611bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 6621bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // To find common subexpressions, count how many of Uses use each expression. 6631bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // If any subexpressions are used Uses.size() times, they are common. 6641bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner std::map<SCEVHandle, unsigned> SubExpressionUseCounts; 6651bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 666934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner std::vector<SCEVHandle> SubExprs; 667934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner for (unsigned i = 0; i != NumUses; ++i) { 668934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // If the base is zero (which is common), return zero now, there are no 669934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // CSEs we can find. 670934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner if (Uses[i].Base == Zero) return Zero; 671934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 672934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // Split the expression into subexprs. 673934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SeparateSubExprs(SubExprs, Uses[i].Base); 674934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // Add one to SubExpressionUseCounts for each subexpr present. 675934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) 676934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SubExpressionUseCounts[SubExprs[j]]++; 677934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SubExprs.clear(); 678934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } 679934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 680934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 6811bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Now that we know how many times each is used, build Result. 6821bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner for (std::map<SCEVHandle, unsigned>::iterator I = 6831bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SubExpressionUseCounts.begin(), E = SubExpressionUseCounts.end(); 6841bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner I != E; ) 6851bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner if (I->second == NumUses) { // Found CSE! 6861bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner Result = SCEVAddExpr::get(Result, I->first); 6871bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner ++I; 6881bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner } else { 6891bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Remove non-cse's from SubExpressionUseCounts. 6901bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SubExpressionUseCounts.erase(I++); 6911bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner } 6921bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 6931bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // If we found no CSE's, return now. 6941bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner if (Result == Zero) return Result; 6951bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 6961bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Otherwise, remove all of the CSE's we found from each of the base values. 697934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner for (unsigned i = 0; i != NumUses; ++i) { 698934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // Split the expression into subexprs. 699934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SeparateSubExprs(SubExprs, Uses[i].Base); 700934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 701934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // Remove any common subexpressions. 702934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner for (unsigned j = 0, e = SubExprs.size(); j != e; ++j) 703934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner if (SubExpressionUseCounts.count(SubExprs[j])) { 704934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner SubExprs.erase(SubExprs.begin()+j); 705934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner --j; --e; 706934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } 707934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner 708934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner // Finally, the non-shared expressions together. 709934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner if (SubExprs.empty()) 7101bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner Uses[i].Base = Zero; 711934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner else 712934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner Uses[i].Base = SCEVAddExpr::get(SubExprs); 71327e5142309946ca12c633b265673af0c24684427Chris Lattner SubExprs.clear(); 714934520a747722ecf94f35768e5b88eeaf44c3b24Chris Lattner } 7151bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 7161bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner return Result; 7171bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner} 7181bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 7191bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 720169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single 721169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// stride of IV. All of the users may have different starting values, and this 722169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman/// may not be the only stride (we know it is if isOnlyStride is true). 72350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattnervoid LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride, 724ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner IVUsersOfOneStride &Uses, 725ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner Loop *L, 726169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman bool isOnlyStride) { 727169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // Transform our list of users and offsets to a bit more complex table. In 728a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner // this new vector, each 'BasedUser' contains 'Base' the base of the 729a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner // strided accessas well as the old information from Uses. We progressively 730a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner // move information from the Base field to the Imm field, until we eventually 731a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner // have the full access expression to rewrite the use. 732a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner std::vector<BasedUser> UsersToProcess; 733169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman UsersToProcess.reserve(Uses.Users.size()); 734a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) { 735a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner UsersToProcess.push_back(Uses.Users[i]); 736a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner 737a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner // Move any loop invariant operands from the offset field to the immediate 738a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner // field of the use, so that we don't try to use something before it is 739a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner // computed. 740a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner MoveLoopVariantsToImediateField(UsersToProcess.back().Base, 741a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner UsersToProcess.back().Imm, L); 742a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner assert(UsersToProcess.back().Base->isLoopInvariant(L) && 74326d91f16464db56283087176a73981048331dd2dChris Lattner "Base value is not loop invariant!"); 7442461dff0700d0e34b9854d96a5cc03921b375525Chris Lattner } 74544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 7461bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // We now have a whole bunch of uses of like-strided induction variables, but 7471bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // they might all have different bases. We want to emit one PHI node for this 7481bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // stride which we fold as many common expressions (between the IVs) into as 7491bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // possible. Start by identifying the common expressions in the base values 7501bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find 7511bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // "A+B"), emit it to the preheader, then remove the expression from the 7521bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // UsersToProcess base values. 7531bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SCEVHandle CommonExprs = RemoveCommonExpressionsFromUseBases(UsersToProcess); 7541bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 75544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner // Next, figure out what we can represent in the immediate fields of 75644b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner // instructions. If we can represent anything there, move it to the imm 7571bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // fields of the BasedUsers. We do this so that it increases the commonality 7581bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // of the remaining uses. 75944b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) { 76080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner // If the user is not in the current loop, this means it is using the exit 76180b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner // value of the IV. Do not put anything in the base, make sure it's all in 76280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner // the immediate field to allow as much factoring as possible. 76380b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner if (!L->contains(UsersToProcess[i].Inst->getParent())) { 7648385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner UsersToProcess[i].Imm = SCEVAddExpr::get(UsersToProcess[i].Imm, 7658385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner UsersToProcess[i].Base); 7668385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner UsersToProcess[i].Base = 7678385e51e210fb5c1e7e48eae150b31679b3e137dChris Lattner SCEVUnknown::getIntegerSCEV(0, UsersToProcess[i].Base->getType()); 76880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner } else { 76980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner 77080b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner // Addressing modes can be folded into loads and stores. Be careful that 77180b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner // the store is through the expression, not of the expression though. 77280b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner bool isAddress = isa<LoadInst>(UsersToProcess[i].Inst); 77380b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].Inst)) 77480b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner if (SI->getOperand(1) == UsersToProcess[i].OperandValToReplace) 77580b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner isAddress = true; 77680b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner 77780b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner MoveImmediateValues(UsersToProcess[i].Base, UsersToProcess[i].Imm, 77880b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner isAddress, L); 77980b32b3aab369534a25cfab6d9b7447cc4a8ff1dChris Lattner } 78044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner } 78144b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 7821bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Now that we know what we need to do, insert the PHI node itself. 7831bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // 7841bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner DEBUG(std::cerr << "INSERTING IV of STRIDE " << *Stride << " and BASE " 7851bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner << *CommonExprs << " :\n"); 7861bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 7871bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SCEVExpander Rewriter(*SE, *LI); 7881bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SCEVExpander PreheaderRewriter(*SE, *LI); 7891bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 7901bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner BasicBlock *Preheader = L->getLoopPreheader(); 7911bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner Instruction *PreInsertPt = Preheader->getTerminator(); 7921bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner Instruction *PhiInsertBefore = L->getHeader()->begin(); 7931bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 79412b50410cd0a8cd81a7f528f862005e80921cf58Chris Lattner BasicBlock *LatchBlock = L->getLoopLatch(); 79544b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 7961bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Create a new Phi for this base, and stick it in the loop header. 7971bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner const Type *ReplacedTy = CommonExprs->getType(); 7981bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner PHINode *NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore); 7991bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner ++NumInserted; 80044b807e3c0b358d75f153066b2b7556710d9c7ccChris Lattner 80150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner // Insert the stride into the preheader. 80250fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner Value *StrideV = PreheaderRewriter.expandCodeFor(Stride, PreInsertPt, 80350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner ReplacedTy); 80450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner if (!isa<ConstantInt>(StrideV)) ++NumVariable; 80550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner 80650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner 8071bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Emit the initial base value into the loop preheader, and add it to the 8081bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Phi node. 8091bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner Value *PHIBaseV = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt, 8101bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner ReplacedTy); 8111bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner NewPHI->addIncoming(PHIBaseV, Preheader); 812be3e5212e23edc9210f774fac18d801de252e906Chris Lattner 8131bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Emit the increment of the base value before the terminator of the loop 8141bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // latch block, and add it to the Phi node. 8151bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SCEVHandle IncExp = SCEVAddExpr::get(SCEVUnknown::get(NewPHI), 81650fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner SCEVUnknown::get(StrideV)); 8171bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 8181bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner Value *IncV = Rewriter.expandCodeFor(IncExp, LatchBlock->getTerminator(), 8191bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner ReplacedTy); 8201bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner IncV->setName(NewPHI->getName()+".inc"); 8211bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner NewPHI->addIncoming(IncV, LatchBlock); 8221bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 8232351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner // Sort by the base value, so that all IVs with identical bases are next to 8241bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // each other. 8252351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner std::sort(UsersToProcess.begin(), UsersToProcess.end()); 826169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman while (!UsersToProcess.empty()) { 827a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner SCEVHandle Base = UsersToProcess.front().Base; 828be3e5212e23edc9210f774fac18d801de252e906Chris Lattner 8291bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner DEBUG(std::cerr << " INSERTING code for BASE = " << *Base << ":\n"); 830be3e5212e23edc9210f774fac18d801de252e906Chris Lattner 8311bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Emit the code for Base into the preheader. 8325272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt, 8335272f3c669c7a2d43dd4796aded314ecc00c66b8Chris Lattner ReplacedTy); 8341bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 8351bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // If BaseV is a constant other than 0, make sure that it gets inserted into 8361bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // the preheader, instead of being forward substituted into the uses. We do 8371bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // this by forcing a noop cast to be inserted into the preheader in this 8381bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // case. 8391bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner if (Constant *C = dyn_cast<Constant>(BaseV)) 8407259df3ab862d378427cf96cd69d1bc69aa2e5cbChris Lattner if (!C->isNullValue() && !isTargetConstant(Base)) { 8411bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // We want this constant emitted into the preheader! 8421bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner BaseV = new CastInst(BaseV, BaseV->getType(), "preheaderinsert", 8431bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner PreInsertPt); 8441bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner } 8451bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 846169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // Emit the code to add the immediate offset to the Phi value, just before 8472351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner // the instructions that we identified as using this stride and base. 848a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner while (!UsersToProcess.empty() && UsersToProcess.front().Base == Base) { 849a553b0cc014bc0fb0d5d4820ec2725e733c60003Chris Lattner BasedUser &User = UsersToProcess.front(); 8502351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner 851010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // If this instruction wants to use the post-incremented value, move it 852010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // after the post-inc and use its value instead of the PHI. 8531bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner Value *RewriteOp = NewPHI; 854010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner if (User.isUseOfPostIncrementedValue) { 8551bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner RewriteOp = IncV; 856c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner 857c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // If this user is in the loop, make sure it is the last thing in the 858c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner // loop to ensure it is dominated by the increment. 859c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner if (L->contains(User.Inst->getParent())) 860c6bae65b49f3b0bb457b6bdb60e29bd9a44e743aChris Lattner User.Inst->moveBefore(LatchBlock->getTerminator()); 861010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner } 8621bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp); 8631bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 8641bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Clear the SCEVExpander's expression map so that we are guaranteed 8651bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // to have the code emitted where we expect it. 8661bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner Rewriter.clear(); 8671bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 8681bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Now that we know what we need to do, insert code before User for the 8691bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // immediate and any loop-variant expressions. 8701bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isNullValue()) 8711bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Add BaseV to the PHI value if needed. 8721bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner RewriteExpr = SCEVAddExpr::get(RewriteExpr, SCEVUnknown::get(BaseV)); 8731bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner 874c60fb08f7ee00be0aa62c9bf92fd361a2d07c229Chris Lattner User.RewriteInstructionToUseNewBase(RewriteExpr, Rewriter, L, this); 8752351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner 8762351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner // Mark old value we replaced as possibly dead, so that it is elminated 8772351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner // if we just replaced the last use of that value. 8782114b273ef0520cec99c253ef6ddf717eaa7657aChris Lattner DeadInsts.insert(cast<Instruction>(User.OperandValToReplace)); 8792351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner 8802351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner UsersToProcess.erase(UsersToProcess.begin()); 8812351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner ++NumReduced; 8822351abaeab89ef0a0df5a715d0794ed5dec48bb3Chris Lattner } 883169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // TODO: Next, find out which base index is the most common, pull it out. 884169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman } 885169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 886169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but 887169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // different starting values, into different PHIs. 888eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman} 889eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 890010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar 891010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// uses in the loop, look to see if we can eliminate some, in favor of using 892010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner// common indvars for the different uses. 893010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattnervoid LoopStrengthReduce::OptimizeIndvars(Loop *L) { 894010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // TODO: implement optzns here. 895010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 896010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 897010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 898010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 899010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // Finally, get the terminating condition for the loop if possible. If we 900010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // can, we want to change it to use a post-incremented version of its 901010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // induction variable, to allow coallescing the live ranges for the IV into 902010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // one register value. 903010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin()); 904010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner BasicBlock *Preheader = L->getLoopPreheader(); 905010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner BasicBlock *LatchBlock = 906010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader); 907010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator()); 908010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner if (!TermBr || TermBr->isUnconditional() || 909010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner !isa<SetCondInst>(TermBr->getCondition())) 910010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner return; 911010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner SetCondInst *Cond = cast<SetCondInst>(TermBr->getCondition()); 912010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 913010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // Search IVUsesByStride to find Cond's IVUse if there is one. 914010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner IVStrideUse *CondUse = 0; 91550fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner const SCEVHandle *CondStride = 0; 916010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 91750fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner for (std::map<SCEVHandle, IVUsersOfOneStride>::iterator 91850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner I = IVUsesByStride.begin(), E = IVUsesByStride.end(); 91950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner I != E && !CondUse; ++I) 920010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner for (std::vector<IVStrideUse>::iterator UI = I->second.Users.begin(), 921010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner E = I->second.Users.end(); UI != E; ++UI) 922010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner if (UI->User == Cond) { 923010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner CondUse = &*UI; 92450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner CondStride = &I->first; 925010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // NOTE: we could handle setcc instructions with multiple uses here, but 926010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // InstCombine does it as well for simple uses, it's not clear that it 927010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // occurs enough in real life to handle. 928010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner break; 929010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner } 930010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner if (!CondUse) return; // setcc doesn't use the IV. 931010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 932010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // setcc stride is complex, don't mess with users. 93350fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner // FIXME: Evaluate whether this is a good idea or not. 93450fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner if (!isa<SCEVConstant>(*CondStride)) return; 935010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 936010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // It's possible for the setcc instruction to be anywhere in the loop, and 937010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // possible for it to have multiple users. If it is not immediately before 938010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // the latch block branch, move it. 939010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) { 940010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner if (Cond->hasOneUse()) { // Condition has a single use, just move it. 941010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner Cond->moveBefore(TermBr); 942010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner } else { 943010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // Otherwise, clone the terminating condition and insert into the loopend. 944010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner Cond = cast<SetCondInst>(Cond->clone()); 945010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner Cond->setName(L->getHeader()->getName() + ".termcond"); 946010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner LatchBlock->getInstList().insert(TermBr, Cond); 947010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 948010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // Clone the IVUse, as the old use still exists! 94950fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond, 950010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner CondUse->OperandValToReplace); 95150fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner CondUse = &IVUsesByStride[*CondStride].Users.back(); 952010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner } 953010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner } 954010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 955010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // If we get to here, we know that we can transform the setcc instruction to 956010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // use the post-incremented version of the IV, allowing us to coallesce the 957010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // live ranges for the IV correctly. 95850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner CondUse->Offset = SCEV::getMinusSCEV(CondUse->Offset, *CondStride); 959010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner CondUse->isUseOfPostIncrementedValue = true; 960010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner} 961169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 962eaa13851a7fe604363577350c5cf65c257c4d41aNate Begemanvoid LoopStrengthReduce::runOnLoop(Loop *L) { 963eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman // First step, transform all loops nesting inside of this loop. 964eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I) 965eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman runOnLoop(*I); 966eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 967169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // Next, find all uses of induction variables in this loop, and catagorize 968169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // them by stride. Start by finding all of the PHI nodes in the header for 969169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // this loop. If they are induction variables, inspect their uses. 9703416e5f645186a7e3321f927eab662d0ecef404bChris Lattner std::set<Instruction*> Processed; // Don't reprocess instructions. 971169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) 9723416e5f645186a7e3321f927eab662d0ecef404bChris Lattner AddUsersIfInteresting(I, L, Processed); 973169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 974169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // If we have nothing to do, return. 975010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner if (IVUsesByStride.empty()) return; 976010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 977010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // Optimize induction variables. Some indvar uses can be transformed to use 978010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // strides that will be needed for other purposes. A common example of this 979010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // is the exit test for the loop, which can often be rewritten to use the 980010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner // computation of some other indvar to decide when to terminate the loop. 981010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner OptimizeIndvars(L); 982010de25f42dabbb7e0a2fe8b42aecc4285362e0cChris Lattner 983169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 984169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // FIXME: We can widen subreg IV's here for RISC targets. e.g. instead of 985169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // doing computation in byte values, promote to 32-bit values if safe. 986169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 987169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // FIXME: Attempt to reuse values across multiple IV's. In particular, we 988169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be 989169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC. Need 990169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // to be careful that IV's are all the same type. Only works for intptr_t 991169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // indvars. 992169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 993169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // If we only have one stride, we can more aggressively eliminate some things. 994169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman bool HasOneStride = IVUsesByStride.size() == 1; 995169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 9961bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // Note: this processes each stride/type pair individually. All users passed 9971bbae0cbf212d0356f564d12e8f728be455bdc47Chris Lattner // into StrengthReduceStridedIVUsers have the same type AND stride. 99850fad70279fa982fcd6a72c919f4e18ad99829b9Chris Lattner for (std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI 999ec3fb63af27b6a20f4a9ee58bb63baad5640ea9cChris Lattner = IVUsesByStride.begin(), E = IVUsesByStride.end(); SI != E; ++SI) 1000169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride); 1001eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 1002eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman // Clean up after ourselves 1003eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman if (!DeadInsts.empty()) { 1004eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman DeleteTriviallyDeadInstructions(DeadInsts); 1005eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman 1006169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman BasicBlock::iterator I = L->getHeader()->begin(); 1007169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman PHINode *PN; 1008e9100c69cbfbcc9298b663d80ef4ddf31d7bba69Chris Lattner while ((PN = dyn_cast<PHINode>(I))) { 10091060e09fb207ed778581957671f8803d2df3a581Chris Lattner ++I; // Preincrement iterator to avoid invalidating it when deleting PN. 10101060e09fb207ed778581957671f8803d2df3a581Chris Lattner 101187265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner // At this point, we know that we have killed one or more GEP 101287265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner // instructions. It is worth checking to see if the cann indvar is also 101387265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner // dead, so that we can remove it as well. The requirements for the cann 101487265abffc4942bbcfb9c75de31b6c7dffb82be1Chris Lattner // indvar to be considered dead are: 1015169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // 1. the cann indvar has one use 1016169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // 2. the use is an add instruction 1017169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // 3. the add has one use 1018169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // 4. the add is used by the cann indvar 1019169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // If all four cases above are true, then we can remove both the add and 1020169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // the cann indvar. 1021169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // FIXME: this needs to eliminate an induction variable even if it's being 1022169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman // compared against some value to decide loop termination. 1023169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman if (PN->hasOneUse()) { 1024169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin())); 10257e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner if (BO && BO->hasOneUse()) { 10267e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner if (PN == *(BO->use_begin())) { 10277e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner DeadInsts.insert(BO); 10287e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner // Break the cycle, then delete the PHI. 10297e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner PN->replaceAllUsesWith(UndefValue::get(PN->getType())); 103052d83e66ee881f85d8a2ccac0183766a6386bfc9Chris Lattner SE->deleteInstructionFromRecords(PN); 10317e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner PN->eraseFromParent(); 1032eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman } 10337e608bbb5dfe4f827e64e91b0bb68a1d95d737aeChris Lattner } 1034169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman } 1035eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman } 1036169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman DeleteTriviallyDeadInstructions(DeadInsts); 1037eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman } 1038169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman 10399a59fbb89606aaefb27f6fe07dcb7c188bf1b3cdChris Lattner CastedPointers.clear(); 1040169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman IVUsesByStride.clear(); 1041169974856781a1ce27af9ce6220c390b20c9e6ddNate Begeman return; 1042eaa13851a7fe604363577350c5cf65c257c4d41aNate Begeman} 1043