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