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