1*9880d681SAndroid Build Coastguard Worker //===---- llvm/Analysis/ScalarEvolutionExpander.h - SCEV Exprs --*- C++ -*-===// 2*9880d681SAndroid Build Coastguard Worker // 3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure 4*9880d681SAndroid Build Coastguard Worker // 5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source 6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details. 7*9880d681SAndroid Build Coastguard Worker // 8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 9*9880d681SAndroid Build Coastguard Worker // 10*9880d681SAndroid Build Coastguard Worker // This file defines the classes used to generate code from scalar expressions. 11*9880d681SAndroid Build Coastguard Worker // 12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===// 13*9880d681SAndroid Build Coastguard Worker 14*9880d681SAndroid Build Coastguard Worker #ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H 15*9880d681SAndroid Build Coastguard Worker #define LLVM_ANALYSIS_SCALAREVOLUTIONEXPANDER_H 16*9880d681SAndroid Build Coastguard Worker 17*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolutionExpressions.h" 18*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolutionNormalization.h" 19*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/TargetFolder.h" 20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/IRBuilder.h" 21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/ValueHandle.h" 22*9880d681SAndroid Build Coastguard Worker #include <set> 23*9880d681SAndroid Build Coastguard Worker 24*9880d681SAndroid Build Coastguard Worker namespace llvm { 25*9880d681SAndroid Build Coastguard Worker class TargetTransformInfo; 26*9880d681SAndroid Build Coastguard Worker 27*9880d681SAndroid Build Coastguard Worker /// Return true if the given expression is safe to expand in the sense that 28*9880d681SAndroid Build Coastguard Worker /// all materialized values are safe to speculate. 29*9880d681SAndroid Build Coastguard Worker bool isSafeToExpand(const SCEV *S, ScalarEvolution &SE); 30*9880d681SAndroid Build Coastguard Worker 31*9880d681SAndroid Build Coastguard Worker /// This class uses information about analyze scalars to 32*9880d681SAndroid Build Coastguard Worker /// rewrite expressions in canonical form. 33*9880d681SAndroid Build Coastguard Worker /// 34*9880d681SAndroid Build Coastguard Worker /// Clients should create an instance of this class when rewriting is needed, 35*9880d681SAndroid Build Coastguard Worker /// and destroy it when finished to allow the release of the associated 36*9880d681SAndroid Build Coastguard Worker /// memory. 37*9880d681SAndroid Build Coastguard Worker class SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> { 38*9880d681SAndroid Build Coastguard Worker ScalarEvolution &SE; 39*9880d681SAndroid Build Coastguard Worker const DataLayout &DL; 40*9880d681SAndroid Build Coastguard Worker 41*9880d681SAndroid Build Coastguard Worker // New instructions receive a name to identifies them with the current pass. 42*9880d681SAndroid Build Coastguard Worker const char* IVName; 43*9880d681SAndroid Build Coastguard Worker 44*9880d681SAndroid Build Coastguard Worker // InsertedExpressions caches Values for reuse, so must track RAUW. 45*9880d681SAndroid Build Coastguard Worker std::map<std::pair<const SCEV *, Instruction *>, TrackingVH<Value> > 46*9880d681SAndroid Build Coastguard Worker InsertedExpressions; 47*9880d681SAndroid Build Coastguard Worker // InsertedValues only flags inserted instructions so needs no RAUW. 48*9880d681SAndroid Build Coastguard Worker std::set<AssertingVH<Value> > InsertedValues; 49*9880d681SAndroid Build Coastguard Worker std::set<AssertingVH<Value> > InsertedPostIncValues; 50*9880d681SAndroid Build Coastguard Worker 51*9880d681SAndroid Build Coastguard Worker /// A memoization of the "relevant" loop for a given SCEV. 52*9880d681SAndroid Build Coastguard Worker DenseMap<const SCEV *, const Loop *> RelevantLoops; 53*9880d681SAndroid Build Coastguard Worker 54*9880d681SAndroid Build Coastguard Worker /// \brief Addrecs referring to any of the given loops are expanded 55*9880d681SAndroid Build Coastguard Worker /// in post-inc mode. For example, expanding {1,+,1}<L> in post-inc mode 56*9880d681SAndroid Build Coastguard Worker /// returns the add instruction that adds one to the phi for {0,+,1}<L>, 57*9880d681SAndroid Build Coastguard Worker /// as opposed to a new phi starting at 1. This is only supported in 58*9880d681SAndroid Build Coastguard Worker /// non-canonical mode. 59*9880d681SAndroid Build Coastguard Worker PostIncLoopSet PostIncLoops; 60*9880d681SAndroid Build Coastguard Worker 61*9880d681SAndroid Build Coastguard Worker /// \brief When this is non-null, addrecs expanded in the loop it indicates 62*9880d681SAndroid Build Coastguard Worker /// should be inserted with increments at IVIncInsertPos. 63*9880d681SAndroid Build Coastguard Worker const Loop *IVIncInsertLoop; 64*9880d681SAndroid Build Coastguard Worker 65*9880d681SAndroid Build Coastguard Worker /// \brief When expanding addrecs in the IVIncInsertLoop loop, insert the IV 66*9880d681SAndroid Build Coastguard Worker /// increment at this position. 67*9880d681SAndroid Build Coastguard Worker Instruction *IVIncInsertPos; 68*9880d681SAndroid Build Coastguard Worker 69*9880d681SAndroid Build Coastguard Worker /// \brief Phis that complete an IV chain. Reuse 70*9880d681SAndroid Build Coastguard Worker std::set<AssertingVH<PHINode> > ChainedPhis; 71*9880d681SAndroid Build Coastguard Worker 72*9880d681SAndroid Build Coastguard Worker /// \brief When true, expressions are expanded in "canonical" form. In 73*9880d681SAndroid Build Coastguard Worker /// particular, addrecs are expanded as arithmetic based on a canonical 74*9880d681SAndroid Build Coastguard Worker /// induction variable. When false, expression are expanded in a more 75*9880d681SAndroid Build Coastguard Worker /// literal form. 76*9880d681SAndroid Build Coastguard Worker bool CanonicalMode; 77*9880d681SAndroid Build Coastguard Worker 78*9880d681SAndroid Build Coastguard Worker /// \brief When invoked from LSR, the expander is in "strength reduction" 79*9880d681SAndroid Build Coastguard Worker /// mode. The only difference is that phi's are only reused if they are 80*9880d681SAndroid Build Coastguard Worker /// already in "expanded" form. 81*9880d681SAndroid Build Coastguard Worker bool LSRMode; 82*9880d681SAndroid Build Coastguard Worker 83*9880d681SAndroid Build Coastguard Worker typedef IRBuilder<TargetFolder> BuilderType; 84*9880d681SAndroid Build Coastguard Worker BuilderType Builder; 85*9880d681SAndroid Build Coastguard Worker 86*9880d681SAndroid Build Coastguard Worker // RAII object that stores the current insertion point and restores it when 87*9880d681SAndroid Build Coastguard Worker // the object is destroyed. This includes the debug location. Duplicated 88*9880d681SAndroid Build Coastguard Worker // from InsertPointGuard to add SetInsertPoint() which is used to updated 89*9880d681SAndroid Build Coastguard Worker // InsertPointGuards stack when insert points are moved during SCEV 90*9880d681SAndroid Build Coastguard Worker // expansion. 91*9880d681SAndroid Build Coastguard Worker class SCEVInsertPointGuard { 92*9880d681SAndroid Build Coastguard Worker IRBuilderBase &Builder; 93*9880d681SAndroid Build Coastguard Worker AssertingVH<BasicBlock> Block; 94*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator Point; 95*9880d681SAndroid Build Coastguard Worker DebugLoc DbgLoc; 96*9880d681SAndroid Build Coastguard Worker SCEVExpander *SE; 97*9880d681SAndroid Build Coastguard Worker 98*9880d681SAndroid Build Coastguard Worker SCEVInsertPointGuard(const SCEVInsertPointGuard &) = delete; 99*9880d681SAndroid Build Coastguard Worker SCEVInsertPointGuard &operator=(const SCEVInsertPointGuard &) = delete; 100*9880d681SAndroid Build Coastguard Worker 101*9880d681SAndroid Build Coastguard Worker public: SCEVInsertPointGuard(IRBuilderBase & B,SCEVExpander * SE)102*9880d681SAndroid Build Coastguard Worker SCEVInsertPointGuard(IRBuilderBase &B, SCEVExpander *SE) 103*9880d681SAndroid Build Coastguard Worker : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()), 104*9880d681SAndroid Build Coastguard Worker DbgLoc(B.getCurrentDebugLocation()), SE(SE) { 105*9880d681SAndroid Build Coastguard Worker SE->InsertPointGuards.push_back(this); 106*9880d681SAndroid Build Coastguard Worker } 107*9880d681SAndroid Build Coastguard Worker ~SCEVInsertPointGuard()108*9880d681SAndroid Build Coastguard Worker ~SCEVInsertPointGuard() { 109*9880d681SAndroid Build Coastguard Worker // These guards should always created/destroyed in FIFO order since they 110*9880d681SAndroid Build Coastguard Worker // are used to guard lexically scoped blocks of code in 111*9880d681SAndroid Build Coastguard Worker // ScalarEvolutionExpander. 112*9880d681SAndroid Build Coastguard Worker assert(SE->InsertPointGuards.back() == this); 113*9880d681SAndroid Build Coastguard Worker SE->InsertPointGuards.pop_back(); 114*9880d681SAndroid Build Coastguard Worker Builder.restoreIP(IRBuilderBase::InsertPoint(Block, Point)); 115*9880d681SAndroid Build Coastguard Worker Builder.SetCurrentDebugLocation(DbgLoc); 116*9880d681SAndroid Build Coastguard Worker } 117*9880d681SAndroid Build Coastguard Worker GetInsertPoint()118*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator GetInsertPoint() const { return Point; } SetInsertPoint(BasicBlock::iterator I)119*9880d681SAndroid Build Coastguard Worker void SetInsertPoint(BasicBlock::iterator I) { Point = I; } 120*9880d681SAndroid Build Coastguard Worker }; 121*9880d681SAndroid Build Coastguard Worker 122*9880d681SAndroid Build Coastguard Worker /// Stack of pointers to saved insert points, used to keep insert points 123*9880d681SAndroid Build Coastguard Worker /// consistent when instructions are moved. 124*9880d681SAndroid Build Coastguard Worker SmallVector<SCEVInsertPointGuard *, 8> InsertPointGuards; 125*9880d681SAndroid Build Coastguard Worker 126*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG 127*9880d681SAndroid Build Coastguard Worker const char *DebugType; 128*9880d681SAndroid Build Coastguard Worker #endif 129*9880d681SAndroid Build Coastguard Worker 130*9880d681SAndroid Build Coastguard Worker friend struct SCEVVisitor<SCEVExpander, Value*>; 131*9880d681SAndroid Build Coastguard Worker 132*9880d681SAndroid Build Coastguard Worker public: 133*9880d681SAndroid Build Coastguard Worker /// \brief Construct a SCEVExpander in "canonical" mode. 134*9880d681SAndroid Build Coastguard Worker explicit SCEVExpander(ScalarEvolution &se, const DataLayout &DL, 135*9880d681SAndroid Build Coastguard Worker const char *name) 136*9880d681SAndroid Build Coastguard Worker : SE(se), DL(DL), IVName(name), IVIncInsertLoop(nullptr), 137*9880d681SAndroid Build Coastguard Worker IVIncInsertPos(nullptr), CanonicalMode(true), LSRMode(false), 138*9880d681SAndroid Build Coastguard Worker Builder(se.getContext(), TargetFolder(DL)) { 139*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG 140*9880d681SAndroid Build Coastguard Worker DebugType = ""; 141*9880d681SAndroid Build Coastguard Worker #endif 142*9880d681SAndroid Build Coastguard Worker } 143*9880d681SAndroid Build Coastguard Worker 144*9880d681SAndroid Build Coastguard Worker ~SCEVExpander() { 145*9880d681SAndroid Build Coastguard Worker // Make sure the insert point guard stack is consistent. 146*9880d681SAndroid Build Coastguard Worker assert(InsertPointGuards.empty()); 147*9880d681SAndroid Build Coastguard Worker } 148*9880d681SAndroid Build Coastguard Worker 149*9880d681SAndroid Build Coastguard Worker #ifndef NDEBUG 150*9880d681SAndroid Build Coastguard Worker void setDebugType(const char* s) { DebugType = s; } 151*9880d681SAndroid Build Coastguard Worker #endif 152*9880d681SAndroid Build Coastguard Worker 153*9880d681SAndroid Build Coastguard Worker /// \brief Erase the contents of the InsertedExpressions map so that users 154*9880d681SAndroid Build Coastguard Worker /// trying to expand the same expression into multiple BasicBlocks or 155*9880d681SAndroid Build Coastguard Worker /// different places within the same BasicBlock can do so. 156*9880d681SAndroid Build Coastguard Worker void clear() { 157*9880d681SAndroid Build Coastguard Worker InsertedExpressions.clear(); 158*9880d681SAndroid Build Coastguard Worker InsertedValues.clear(); 159*9880d681SAndroid Build Coastguard Worker InsertedPostIncValues.clear(); 160*9880d681SAndroid Build Coastguard Worker ChainedPhis.clear(); 161*9880d681SAndroid Build Coastguard Worker } 162*9880d681SAndroid Build Coastguard Worker 163*9880d681SAndroid Build Coastguard Worker /// \brief Return true for expressions that may incur non-trivial cost to 164*9880d681SAndroid Build Coastguard Worker /// evaluate at runtime. 165*9880d681SAndroid Build Coastguard Worker /// 166*9880d681SAndroid Build Coastguard Worker /// At is an optional parameter which specifies point in code where user is 167*9880d681SAndroid Build Coastguard Worker /// going to expand this expression. Sometimes this knowledge can lead to a 168*9880d681SAndroid Build Coastguard Worker /// more accurate cost estimation. 169*9880d681SAndroid Build Coastguard Worker bool isHighCostExpansion(const SCEV *Expr, Loop *L, 170*9880d681SAndroid Build Coastguard Worker const Instruction *At = nullptr) { 171*9880d681SAndroid Build Coastguard Worker SmallPtrSet<const SCEV *, 8> Processed; 172*9880d681SAndroid Build Coastguard Worker return isHighCostExpansionHelper(Expr, L, At, Processed); 173*9880d681SAndroid Build Coastguard Worker } 174*9880d681SAndroid Build Coastguard Worker 175*9880d681SAndroid Build Coastguard Worker /// \brief This method returns the canonical induction variable of the 176*9880d681SAndroid Build Coastguard Worker /// specified type for the specified loop (inserting one if there is none). 177*9880d681SAndroid Build Coastguard Worker /// A canonical induction variable starts at zero and steps by one on each 178*9880d681SAndroid Build Coastguard Worker /// iteration. 179*9880d681SAndroid Build Coastguard Worker PHINode *getOrInsertCanonicalInductionVariable(const Loop *L, Type *Ty); 180*9880d681SAndroid Build Coastguard Worker 181*9880d681SAndroid Build Coastguard Worker /// \brief Return the induction variable increment's IV operand. 182*9880d681SAndroid Build Coastguard Worker Instruction *getIVIncOperand(Instruction *IncV, Instruction *InsertPos, 183*9880d681SAndroid Build Coastguard Worker bool allowScale); 184*9880d681SAndroid Build Coastguard Worker 185*9880d681SAndroid Build Coastguard Worker /// \brief Utility for hoisting an IV increment. 186*9880d681SAndroid Build Coastguard Worker bool hoistIVInc(Instruction *IncV, Instruction *InsertPos); 187*9880d681SAndroid Build Coastguard Worker 188*9880d681SAndroid Build Coastguard Worker /// \brief replace congruent phis with their most canonical 189*9880d681SAndroid Build Coastguard Worker /// representative. Return the number of phis eliminated. 190*9880d681SAndroid Build Coastguard Worker unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT, 191*9880d681SAndroid Build Coastguard Worker SmallVectorImpl<WeakVH> &DeadInsts, 192*9880d681SAndroid Build Coastguard Worker const TargetTransformInfo *TTI = nullptr); 193*9880d681SAndroid Build Coastguard Worker 194*9880d681SAndroid Build Coastguard Worker /// \brief Insert code to directly compute the specified SCEV expression 195*9880d681SAndroid Build Coastguard Worker /// into the program. The inserted code is inserted into the specified 196*9880d681SAndroid Build Coastguard Worker /// block. 197*9880d681SAndroid Build Coastguard Worker Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I); 198*9880d681SAndroid Build Coastguard Worker 199*9880d681SAndroid Build Coastguard Worker /// \brief Generates a code sequence that evaluates this predicate. 200*9880d681SAndroid Build Coastguard Worker /// The inserted instructions will be at position \p Loc. 201*9880d681SAndroid Build Coastguard Worker /// The result will be of type i1 and will have a value of 0 when the 202*9880d681SAndroid Build Coastguard Worker /// predicate is false and 1 otherwise. 203*9880d681SAndroid Build Coastguard Worker Value *expandCodeForPredicate(const SCEVPredicate *Pred, Instruction *Loc); 204*9880d681SAndroid Build Coastguard Worker 205*9880d681SAndroid Build Coastguard Worker /// \brief A specialized variant of expandCodeForPredicate, handling the 206*9880d681SAndroid Build Coastguard Worker /// case when we are expanding code for a SCEVEqualPredicate. 207*9880d681SAndroid Build Coastguard Worker Value *expandEqualPredicate(const SCEVEqualPredicate *Pred, 208*9880d681SAndroid Build Coastguard Worker Instruction *Loc); 209*9880d681SAndroid Build Coastguard Worker 210*9880d681SAndroid Build Coastguard Worker /// \brief Generates code that evaluates if the \p AR expression will 211*9880d681SAndroid Build Coastguard Worker /// overflow. 212*9880d681SAndroid Build Coastguard Worker Value *generateOverflowCheck(const SCEVAddRecExpr *AR, Instruction *Loc, 213*9880d681SAndroid Build Coastguard Worker bool Signed); 214*9880d681SAndroid Build Coastguard Worker 215*9880d681SAndroid Build Coastguard Worker /// \brief A specialized variant of expandCodeForPredicate, handling the 216*9880d681SAndroid Build Coastguard Worker /// case when we are expanding code for a SCEVWrapPredicate. 217*9880d681SAndroid Build Coastguard Worker Value *expandWrapPredicate(const SCEVWrapPredicate *P, Instruction *Loc); 218*9880d681SAndroid Build Coastguard Worker 219*9880d681SAndroid Build Coastguard Worker /// \brief A specialized variant of expandCodeForPredicate, handling the 220*9880d681SAndroid Build Coastguard Worker /// case when we are expanding code for a SCEVUnionPredicate. 221*9880d681SAndroid Build Coastguard Worker Value *expandUnionPredicate(const SCEVUnionPredicate *Pred, 222*9880d681SAndroid Build Coastguard Worker Instruction *Loc); 223*9880d681SAndroid Build Coastguard Worker 224*9880d681SAndroid Build Coastguard Worker /// \brief Set the current IV increment loop and position. 225*9880d681SAndroid Build Coastguard Worker void setIVIncInsertPos(const Loop *L, Instruction *Pos) { 226*9880d681SAndroid Build Coastguard Worker assert(!CanonicalMode && 227*9880d681SAndroid Build Coastguard Worker "IV increment positions are not supported in CanonicalMode"); 228*9880d681SAndroid Build Coastguard Worker IVIncInsertLoop = L; 229*9880d681SAndroid Build Coastguard Worker IVIncInsertPos = Pos; 230*9880d681SAndroid Build Coastguard Worker } 231*9880d681SAndroid Build Coastguard Worker 232*9880d681SAndroid Build Coastguard Worker /// \brief Enable post-inc expansion for addrecs referring to the given 233*9880d681SAndroid Build Coastguard Worker /// loops. Post-inc expansion is only supported in non-canonical mode. 234*9880d681SAndroid Build Coastguard Worker void setPostInc(const PostIncLoopSet &L) { 235*9880d681SAndroid Build Coastguard Worker assert(!CanonicalMode && 236*9880d681SAndroid Build Coastguard Worker "Post-inc expansion is not supported in CanonicalMode"); 237*9880d681SAndroid Build Coastguard Worker PostIncLoops = L; 238*9880d681SAndroid Build Coastguard Worker } 239*9880d681SAndroid Build Coastguard Worker 240*9880d681SAndroid Build Coastguard Worker /// \brief Disable all post-inc expansion. 241*9880d681SAndroid Build Coastguard Worker void clearPostInc() { 242*9880d681SAndroid Build Coastguard Worker PostIncLoops.clear(); 243*9880d681SAndroid Build Coastguard Worker 244*9880d681SAndroid Build Coastguard Worker // When we change the post-inc loop set, cached expansions may no 245*9880d681SAndroid Build Coastguard Worker // longer be valid. 246*9880d681SAndroid Build Coastguard Worker InsertedPostIncValues.clear(); 247*9880d681SAndroid Build Coastguard Worker } 248*9880d681SAndroid Build Coastguard Worker 249*9880d681SAndroid Build Coastguard Worker /// \brief Disable the behavior of expanding expressions in canonical form 250*9880d681SAndroid Build Coastguard Worker /// rather than in a more literal form. Non-canonical mode is useful for 251*9880d681SAndroid Build Coastguard Worker /// late optimization passes. 252*9880d681SAndroid Build Coastguard Worker void disableCanonicalMode() { CanonicalMode = false; } 253*9880d681SAndroid Build Coastguard Worker 254*9880d681SAndroid Build Coastguard Worker void enableLSRMode() { LSRMode = true; } 255*9880d681SAndroid Build Coastguard Worker 256*9880d681SAndroid Build Coastguard Worker /// \brief Clear the current insertion point. This is useful if the 257*9880d681SAndroid Build Coastguard Worker /// instruction that had been serving as the insertion point may have been 258*9880d681SAndroid Build Coastguard Worker /// deleted. 259*9880d681SAndroid Build Coastguard Worker void clearInsertPoint() { 260*9880d681SAndroid Build Coastguard Worker Builder.ClearInsertionPoint(); 261*9880d681SAndroid Build Coastguard Worker } 262*9880d681SAndroid Build Coastguard Worker 263*9880d681SAndroid Build Coastguard Worker /// \brief Return true if the specified instruction was inserted by the code 264*9880d681SAndroid Build Coastguard Worker /// rewriter. If so, the client should not modify the instruction. 265*9880d681SAndroid Build Coastguard Worker bool isInsertedInstruction(Instruction *I) const { 266*9880d681SAndroid Build Coastguard Worker return InsertedValues.count(I) || InsertedPostIncValues.count(I); 267*9880d681SAndroid Build Coastguard Worker } 268*9880d681SAndroid Build Coastguard Worker 269*9880d681SAndroid Build Coastguard Worker void setChainedPhi(PHINode *PN) { ChainedPhis.insert(PN); } 270*9880d681SAndroid Build Coastguard Worker 271*9880d681SAndroid Build Coastguard Worker /// \brief Try to find LLVM IR value for S available at the point At. 272*9880d681SAndroid Build Coastguard Worker /// 273*9880d681SAndroid Build Coastguard Worker /// L is a hint which tells in which loop to look for the suitable value. 274*9880d681SAndroid Build Coastguard Worker /// On success return value which is equivalent to the expanded S at point 275*9880d681SAndroid Build Coastguard Worker /// At. Return nullptr if value was not found. 276*9880d681SAndroid Build Coastguard Worker /// 277*9880d681SAndroid Build Coastguard Worker /// Note that this function does not perform an exhaustive search. I.e if it 278*9880d681SAndroid Build Coastguard Worker /// didn't find any value it does not mean that there is no such value. 279*9880d681SAndroid Build Coastguard Worker Value *findExistingExpansion(const SCEV *S, const Instruction *At, Loop *L); 280*9880d681SAndroid Build Coastguard Worker 281*9880d681SAndroid Build Coastguard Worker private: 282*9880d681SAndroid Build Coastguard Worker LLVMContext &getContext() const { return SE.getContext(); } 283*9880d681SAndroid Build Coastguard Worker 284*9880d681SAndroid Build Coastguard Worker /// \brief Recursive helper function for isHighCostExpansion. 285*9880d681SAndroid Build Coastguard Worker bool isHighCostExpansionHelper(const SCEV *S, Loop *L, 286*9880d681SAndroid Build Coastguard Worker const Instruction *At, 287*9880d681SAndroid Build Coastguard Worker SmallPtrSetImpl<const SCEV *> &Processed); 288*9880d681SAndroid Build Coastguard Worker 289*9880d681SAndroid Build Coastguard Worker /// \brief Insert the specified binary operator, doing a small amount 290*9880d681SAndroid Build Coastguard Worker /// of work to avoid inserting an obviously redundant operation. 291*9880d681SAndroid Build Coastguard Worker Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS, Value *RHS); 292*9880d681SAndroid Build Coastguard Worker 293*9880d681SAndroid Build Coastguard Worker /// \brief Arrange for there to be a cast of V to Ty at IP, reusing an 294*9880d681SAndroid Build Coastguard Worker /// existing cast if a suitable one exists, moving an existing cast if a 295*9880d681SAndroid Build Coastguard Worker /// suitable one exists but isn't in the right place, or or creating a new 296*9880d681SAndroid Build Coastguard Worker /// one. 297*9880d681SAndroid Build Coastguard Worker Value *ReuseOrCreateCast(Value *V, Type *Ty, 298*9880d681SAndroid Build Coastguard Worker Instruction::CastOps Op, 299*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator IP); 300*9880d681SAndroid Build Coastguard Worker 301*9880d681SAndroid Build Coastguard Worker /// \brief Insert a cast of V to the specified type, which must be possible 302*9880d681SAndroid Build Coastguard Worker /// with a noop cast, doing what we can to share the casts. 303*9880d681SAndroid Build Coastguard Worker Value *InsertNoopCastOfTo(Value *V, Type *Ty); 304*9880d681SAndroid Build Coastguard Worker 305*9880d681SAndroid Build Coastguard Worker /// \brief Expand a SCEVAddExpr with a pointer type into a GEP 306*9880d681SAndroid Build Coastguard Worker /// instead of using ptrtoint+arithmetic+inttoptr. 307*9880d681SAndroid Build Coastguard Worker Value *expandAddToGEP(const SCEV *const *op_begin, 308*9880d681SAndroid Build Coastguard Worker const SCEV *const *op_end, 309*9880d681SAndroid Build Coastguard Worker PointerType *PTy, Type *Ty, Value *V); 310*9880d681SAndroid Build Coastguard Worker 311*9880d681SAndroid Build Coastguard Worker /// \brief Find a previous Value in ExprValueMap for expand. 312*9880d681SAndroid Build Coastguard Worker Value *FindValueInExprValueMap(const SCEV *S, const Instruction *InsertPt); 313*9880d681SAndroid Build Coastguard Worker 314*9880d681SAndroid Build Coastguard Worker Value *expand(const SCEV *S); 315*9880d681SAndroid Build Coastguard Worker 316*9880d681SAndroid Build Coastguard Worker /// \brief Insert code to directly compute the specified SCEV expression 317*9880d681SAndroid Build Coastguard Worker /// into the program. The inserted code is inserted into the SCEVExpander's 318*9880d681SAndroid Build Coastguard Worker /// current insertion point. If a type is specified, the result will be 319*9880d681SAndroid Build Coastguard Worker /// expanded to have that type, with a cast if necessary. 320*9880d681SAndroid Build Coastguard Worker Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr); 321*9880d681SAndroid Build Coastguard Worker 322*9880d681SAndroid Build Coastguard Worker /// \brief Determine the most "relevant" loop for the given SCEV. 323*9880d681SAndroid Build Coastguard Worker const Loop *getRelevantLoop(const SCEV *); 324*9880d681SAndroid Build Coastguard Worker 325*9880d681SAndroid Build Coastguard Worker Value *visitConstant(const SCEVConstant *S) { 326*9880d681SAndroid Build Coastguard Worker return S->getValue(); 327*9880d681SAndroid Build Coastguard Worker } 328*9880d681SAndroid Build Coastguard Worker 329*9880d681SAndroid Build Coastguard Worker Value *visitTruncateExpr(const SCEVTruncateExpr *S); 330*9880d681SAndroid Build Coastguard Worker 331*9880d681SAndroid Build Coastguard Worker Value *visitZeroExtendExpr(const SCEVZeroExtendExpr *S); 332*9880d681SAndroid Build Coastguard Worker 333*9880d681SAndroid Build Coastguard Worker Value *visitSignExtendExpr(const SCEVSignExtendExpr *S); 334*9880d681SAndroid Build Coastguard Worker 335*9880d681SAndroid Build Coastguard Worker Value *visitAddExpr(const SCEVAddExpr *S); 336*9880d681SAndroid Build Coastguard Worker 337*9880d681SAndroid Build Coastguard Worker Value *visitMulExpr(const SCEVMulExpr *S); 338*9880d681SAndroid Build Coastguard Worker 339*9880d681SAndroid Build Coastguard Worker Value *visitUDivExpr(const SCEVUDivExpr *S); 340*9880d681SAndroid Build Coastguard Worker 341*9880d681SAndroid Build Coastguard Worker Value *visitAddRecExpr(const SCEVAddRecExpr *S); 342*9880d681SAndroid Build Coastguard Worker 343*9880d681SAndroid Build Coastguard Worker Value *visitSMaxExpr(const SCEVSMaxExpr *S); 344*9880d681SAndroid Build Coastguard Worker 345*9880d681SAndroid Build Coastguard Worker Value *visitUMaxExpr(const SCEVUMaxExpr *S); 346*9880d681SAndroid Build Coastguard Worker 347*9880d681SAndroid Build Coastguard Worker Value *visitUnknown(const SCEVUnknown *S) { 348*9880d681SAndroid Build Coastguard Worker return S->getValue(); 349*9880d681SAndroid Build Coastguard Worker } 350*9880d681SAndroid Build Coastguard Worker 351*9880d681SAndroid Build Coastguard Worker void rememberInstruction(Value *I); 352*9880d681SAndroid Build Coastguard Worker 353*9880d681SAndroid Build Coastguard Worker bool isNormalAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L); 354*9880d681SAndroid Build Coastguard Worker 355*9880d681SAndroid Build Coastguard Worker bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L); 356*9880d681SAndroid Build Coastguard Worker 357*9880d681SAndroid Build Coastguard Worker Value *expandAddRecExprLiterally(const SCEVAddRecExpr *); 358*9880d681SAndroid Build Coastguard Worker PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized, 359*9880d681SAndroid Build Coastguard Worker const Loop *L, 360*9880d681SAndroid Build Coastguard Worker Type *ExpandTy, 361*9880d681SAndroid Build Coastguard Worker Type *IntTy, 362*9880d681SAndroid Build Coastguard Worker Type *&TruncTy, 363*9880d681SAndroid Build Coastguard Worker bool &InvertStep); 364*9880d681SAndroid Build Coastguard Worker Value *expandIVInc(PHINode *PN, Value *StepV, const Loop *L, 365*9880d681SAndroid Build Coastguard Worker Type *ExpandTy, Type *IntTy, bool useSubtract); 366*9880d681SAndroid Build Coastguard Worker 367*9880d681SAndroid Build Coastguard Worker void hoistBeforePos(DominatorTree *DT, Instruction *InstToHoist, 368*9880d681SAndroid Build Coastguard Worker Instruction *Pos, PHINode *LoopPhi); 369*9880d681SAndroid Build Coastguard Worker 370*9880d681SAndroid Build Coastguard Worker void fixupInsertPoints(Instruction *I); 371*9880d681SAndroid Build Coastguard Worker }; 372*9880d681SAndroid Build Coastguard Worker } 373*9880d681SAndroid Build Coastguard Worker 374*9880d681SAndroid Build Coastguard Worker #endif 375