1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===// 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 contains the implementation of the scalar evolution analysis 10 // engine, which is used primarily to analyze expressions involving induction 11 // variables in loops. 12 // 13 // There are several aspects to this library. First is the representation of 14 // scalar expressions, which are represented as subclasses of the SCEV class. 15 // These classes are used to represent certain types of subexpressions that we 16 // can handle. We only create one SCEV of a particular shape, so 17 // pointer-comparisons for equality are legal. 18 // 19 // One important aspect of the SCEV objects is that they are never cyclic, even 20 // if there is a cycle in the dataflow for an expression (ie, a PHI node). If 21 // the PHI node is one of the idioms that we can represent (e.g., a polynomial 22 // recurrence) then we represent it directly as a recurrence node, otherwise we 23 // represent it as a SCEVUnknown node. 24 // 25 // In addition to being able to represent expressions of various types, we also 26 // have folders that are used to build the *canonical* representation for a 27 // particular expression. These folders are capable of using a variety of 28 // rewrite rules to simplify the expressions. 29 // 30 // Once the folders are defined, we can implement the more interesting 31 // higher-level code, such as the code that recognizes PHI nodes of various 32 // types, computes the execution count of a loop, etc. 33 // 34 // TODO: We should use these routines and value representations to implement 35 // dependence analysis! 36 // 37 //===----------------------------------------------------------------------===// 38 // 39 // There are several good references for the techniques used in this analysis. 40 // 41 // Chains of recurrences -- a method to expedite the evaluation 42 // of closed-form functions 43 // Olaf Bachmann, Paul S. Wang, Eugene V. Zima 44 // 45 // On computational properties of chains of recurrences 46 // Eugene V. Zima 47 // 48 // Symbolic Evaluation of Chains of Recurrences for Loop Optimization 49 // Robert A. van Engelen 50 // 51 // Efficient Symbolic Analysis for Optimizing Compilers 52 // Robert A. van Engelen 53 // 54 // Using the chains of recurrences algebra for data dependence testing and 55 // induction variable substitution 56 // MS Thesis, Johnie Birch 57 // 58 //===----------------------------------------------------------------------===// 59 60 #include "llvm/Analysis/ScalarEvolution.h" 61 #include "llvm/ADT/APInt.h" 62 #include "llvm/ADT/ArrayRef.h" 63 #include "llvm/ADT/DenseMap.h" 64 #include "llvm/ADT/DepthFirstIterator.h" 65 #include "llvm/ADT/EquivalenceClasses.h" 66 #include "llvm/ADT/FoldingSet.h" 67 #include "llvm/ADT/None.h" 68 #include "llvm/ADT/Optional.h" 69 #include "llvm/ADT/STLExtras.h" 70 #include "llvm/ADT/ScopeExit.h" 71 #include "llvm/ADT/Sequence.h" 72 #include "llvm/ADT/SetVector.h" 73 #include "llvm/ADT/SmallPtrSet.h" 74 #include "llvm/ADT/SmallSet.h" 75 #include "llvm/ADT/SmallVector.h" 76 #include "llvm/ADT/Statistic.h" 77 #include "llvm/ADT/StringRef.h" 78 #include "llvm/Analysis/AssumptionCache.h" 79 #include "llvm/Analysis/ConstantFolding.h" 80 #include "llvm/Analysis/InstructionSimplify.h" 81 #include "llvm/Analysis/LoopInfo.h" 82 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 83 #include "llvm/Analysis/TargetLibraryInfo.h" 84 #include "llvm/Analysis/ValueTracking.h" 85 #include "llvm/Config/llvm-config.h" 86 #include "llvm/IR/Argument.h" 87 #include "llvm/IR/BasicBlock.h" 88 #include "llvm/IR/CFG.h" 89 #include "llvm/IR/Constant.h" 90 #include "llvm/IR/ConstantRange.h" 91 #include "llvm/IR/Constants.h" 92 #include "llvm/IR/DataLayout.h" 93 #include "llvm/IR/DerivedTypes.h" 94 #include "llvm/IR/Dominators.h" 95 #include "llvm/IR/Function.h" 96 #include "llvm/IR/GlobalAlias.h" 97 #include "llvm/IR/GlobalValue.h" 98 #include "llvm/IR/InstIterator.h" 99 #include "llvm/IR/InstrTypes.h" 100 #include "llvm/IR/Instruction.h" 101 #include "llvm/IR/Instructions.h" 102 #include "llvm/IR/IntrinsicInst.h" 103 #include "llvm/IR/Intrinsics.h" 104 #include "llvm/IR/LLVMContext.h" 105 #include "llvm/IR/Operator.h" 106 #include "llvm/IR/PatternMatch.h" 107 #include "llvm/IR/Type.h" 108 #include "llvm/IR/Use.h" 109 #include "llvm/IR/User.h" 110 #include "llvm/IR/Value.h" 111 #include "llvm/IR/Verifier.h" 112 #include "llvm/InitializePasses.h" 113 #include "llvm/Pass.h" 114 #include "llvm/Support/Casting.h" 115 #include "llvm/Support/CommandLine.h" 116 #include "llvm/Support/Compiler.h" 117 #include "llvm/Support/Debug.h" 118 #include "llvm/Support/ErrorHandling.h" 119 #include "llvm/Support/KnownBits.h" 120 #include "llvm/Support/SaveAndRestore.h" 121 #include "llvm/Support/raw_ostream.h" 122 #include <algorithm> 123 #include <cassert> 124 #include <climits> 125 #include <cstdint> 126 #include <cstdlib> 127 #include <map> 128 #include <memory> 129 #include <tuple> 130 #include <utility> 131 #include <vector> 132 133 using namespace llvm; 134 using namespace PatternMatch; 135 136 #define DEBUG_TYPE "scalar-evolution" 137 138 STATISTIC(NumTripCountsComputed, 139 "Number of loops with predictable loop counts"); 140 STATISTIC(NumTripCountsNotComputed, 141 "Number of loops without predictable loop counts"); 142 STATISTIC(NumBruteForceTripCountsComputed, 143 "Number of loops with trip counts computed by force"); 144 145 #ifdef EXPENSIVE_CHECKS 146 bool llvm::VerifySCEV = true; 147 #else 148 bool llvm::VerifySCEV = false; 149 #endif 150 151 static cl::opt<unsigned> 152 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, 153 cl::ZeroOrMore, 154 cl::desc("Maximum number of iterations SCEV will " 155 "symbolically execute a constant " 156 "derived loop"), 157 cl::init(100)); 158 159 static cl::opt<bool, true> VerifySCEVOpt( 160 "verify-scev", cl::Hidden, cl::location(VerifySCEV), 161 cl::desc("Verify ScalarEvolution's backedge taken counts (slow)")); 162 static cl::opt<bool> VerifySCEVStrict( 163 "verify-scev-strict", cl::Hidden, 164 cl::desc("Enable stricter verification with -verify-scev is passed")); 165 static cl::opt<bool> 166 VerifySCEVMap("verify-scev-maps", cl::Hidden, 167 cl::desc("Verify no dangling value in ScalarEvolution's " 168 "ExprValueMap (slow)")); 169 170 static cl::opt<bool> VerifyIR( 171 "scev-verify-ir", cl::Hidden, 172 cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"), 173 cl::init(false)); 174 175 static cl::opt<unsigned> MulOpsInlineThreshold( 176 "scev-mulops-inline-threshold", cl::Hidden, 177 cl::desc("Threshold for inlining multiplication operands into a SCEV"), 178 cl::init(32)); 179 180 static cl::opt<unsigned> AddOpsInlineThreshold( 181 "scev-addops-inline-threshold", cl::Hidden, 182 cl::desc("Threshold for inlining addition operands into a SCEV"), 183 cl::init(500)); 184 185 static cl::opt<unsigned> MaxSCEVCompareDepth( 186 "scalar-evolution-max-scev-compare-depth", cl::Hidden, 187 cl::desc("Maximum depth of recursive SCEV complexity comparisons"), 188 cl::init(32)); 189 190 static cl::opt<unsigned> MaxSCEVOperationsImplicationDepth( 191 "scalar-evolution-max-scev-operations-implication-depth", cl::Hidden, 192 cl::desc("Maximum depth of recursive SCEV operations implication analysis"), 193 cl::init(2)); 194 195 static cl::opt<unsigned> MaxValueCompareDepth( 196 "scalar-evolution-max-value-compare-depth", cl::Hidden, 197 cl::desc("Maximum depth of recursive value complexity comparisons"), 198 cl::init(2)); 199 200 static cl::opt<unsigned> 201 MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden, 202 cl::desc("Maximum depth of recursive arithmetics"), 203 cl::init(32)); 204 205 static cl::opt<unsigned> MaxConstantEvolvingDepth( 206 "scalar-evolution-max-constant-evolving-depth", cl::Hidden, 207 cl::desc("Maximum depth of recursive constant evolving"), cl::init(32)); 208 209 static cl::opt<unsigned> 210 MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden, 211 cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"), 212 cl::init(8)); 213 214 static cl::opt<unsigned> 215 MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden, 216 cl::desc("Max coefficients in AddRec during evolving"), 217 cl::init(8)); 218 219 static cl::opt<unsigned> 220 HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden, 221 cl::desc("Size of the expression which is considered huge"), 222 cl::init(4096)); 223 224 static cl::opt<bool> 225 ClassifyExpressions("scalar-evolution-classify-expressions", 226 cl::Hidden, cl::init(true), 227 cl::desc("When printing analysis, include information on every instruction")); 228 229 static cl::opt<bool> UseExpensiveRangeSharpening( 230 "scalar-evolution-use-expensive-range-sharpening", cl::Hidden, 231 cl::init(false), 232 cl::desc("Use more powerful methods of sharpening expression ranges. May " 233 "be costly in terms of compile time")); 234 235 static cl::opt<unsigned> MaxPhiSCCAnalysisSize( 236 "scalar-evolution-max-scc-analysis-depth", cl::Hidden, 237 cl::desc("Maximum amount of nodes to process while searching SCEVUnknown " 238 "Phi strongly connected components"), 239 cl::init(8)); 240 241 static cl::opt<bool> 242 EnableFiniteLoopControl("scalar-evolution-finite-loop", cl::Hidden, 243 cl::desc("Handle <= and >= in finite loops"), 244 cl::init(true)); 245 246 //===----------------------------------------------------------------------===// 247 // SCEV class definitions 248 //===----------------------------------------------------------------------===// 249 250 //===----------------------------------------------------------------------===// 251 // Implementation of the SCEV class. 252 // 253 254 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 255 LLVM_DUMP_METHOD void SCEV::dump() const { 256 print(dbgs()); 257 dbgs() << '\n'; 258 } 259 #endif 260 261 void SCEV::print(raw_ostream &OS) const { 262 switch (getSCEVType()) { 263 case scConstant: 264 cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false); 265 return; 266 case scPtrToInt: { 267 const SCEVPtrToIntExpr *PtrToInt = cast<SCEVPtrToIntExpr>(this); 268 const SCEV *Op = PtrToInt->getOperand(); 269 OS << "(ptrtoint " << *Op->getType() << " " << *Op << " to " 270 << *PtrToInt->getType() << ")"; 271 return; 272 } 273 case scTruncate: { 274 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this); 275 const SCEV *Op = Trunc->getOperand(); 276 OS << "(trunc " << *Op->getType() << " " << *Op << " to " 277 << *Trunc->getType() << ")"; 278 return; 279 } 280 case scZeroExtend: { 281 const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this); 282 const SCEV *Op = ZExt->getOperand(); 283 OS << "(zext " << *Op->getType() << " " << *Op << " to " 284 << *ZExt->getType() << ")"; 285 return; 286 } 287 case scSignExtend: { 288 const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this); 289 const SCEV *Op = SExt->getOperand(); 290 OS << "(sext " << *Op->getType() << " " << *Op << " to " 291 << *SExt->getType() << ")"; 292 return; 293 } 294 case scAddRecExpr: { 295 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this); 296 OS << "{" << *AR->getOperand(0); 297 for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i) 298 OS << ",+," << *AR->getOperand(i); 299 OS << "}<"; 300 if (AR->hasNoUnsignedWrap()) 301 OS << "nuw><"; 302 if (AR->hasNoSignedWrap()) 303 OS << "nsw><"; 304 if (AR->hasNoSelfWrap() && 305 !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW))) 306 OS << "nw><"; 307 AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false); 308 OS << ">"; 309 return; 310 } 311 case scAddExpr: 312 case scMulExpr: 313 case scUMaxExpr: 314 case scSMaxExpr: 315 case scUMinExpr: 316 case scSMinExpr: 317 case scSequentialUMinExpr: { 318 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this); 319 const char *OpStr = nullptr; 320 switch (NAry->getSCEVType()) { 321 case scAddExpr: OpStr = " + "; break; 322 case scMulExpr: OpStr = " * "; break; 323 case scUMaxExpr: OpStr = " umax "; break; 324 case scSMaxExpr: OpStr = " smax "; break; 325 case scUMinExpr: 326 OpStr = " umin "; 327 break; 328 case scSMinExpr: 329 OpStr = " smin "; 330 break; 331 case scSequentialUMinExpr: 332 OpStr = " umin_seq "; 333 break; 334 default: 335 llvm_unreachable("There are no other nary expression types."); 336 } 337 OS << "("; 338 ListSeparator LS(OpStr); 339 for (const SCEV *Op : NAry->operands()) 340 OS << LS << *Op; 341 OS << ")"; 342 switch (NAry->getSCEVType()) { 343 case scAddExpr: 344 case scMulExpr: 345 if (NAry->hasNoUnsignedWrap()) 346 OS << "<nuw>"; 347 if (NAry->hasNoSignedWrap()) 348 OS << "<nsw>"; 349 break; 350 default: 351 // Nothing to print for other nary expressions. 352 break; 353 } 354 return; 355 } 356 case scUDivExpr: { 357 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this); 358 OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")"; 359 return; 360 } 361 case scUnknown: { 362 const SCEVUnknown *U = cast<SCEVUnknown>(this); 363 Type *AllocTy; 364 if (U->isSizeOf(AllocTy)) { 365 OS << "sizeof(" << *AllocTy << ")"; 366 return; 367 } 368 if (U->isAlignOf(AllocTy)) { 369 OS << "alignof(" << *AllocTy << ")"; 370 return; 371 } 372 373 Type *CTy; 374 Constant *FieldNo; 375 if (U->isOffsetOf(CTy, FieldNo)) { 376 OS << "offsetof(" << *CTy << ", "; 377 FieldNo->printAsOperand(OS, false); 378 OS << ")"; 379 return; 380 } 381 382 // Otherwise just print it normally. 383 U->getValue()->printAsOperand(OS, false); 384 return; 385 } 386 case scCouldNotCompute: 387 OS << "***COULDNOTCOMPUTE***"; 388 return; 389 } 390 llvm_unreachable("Unknown SCEV kind!"); 391 } 392 393 Type *SCEV::getType() const { 394 switch (getSCEVType()) { 395 case scConstant: 396 return cast<SCEVConstant>(this)->getType(); 397 case scPtrToInt: 398 case scTruncate: 399 case scZeroExtend: 400 case scSignExtend: 401 return cast<SCEVCastExpr>(this)->getType(); 402 case scAddRecExpr: 403 return cast<SCEVAddRecExpr>(this)->getType(); 404 case scMulExpr: 405 return cast<SCEVMulExpr>(this)->getType(); 406 case scUMaxExpr: 407 case scSMaxExpr: 408 case scUMinExpr: 409 case scSMinExpr: 410 return cast<SCEVMinMaxExpr>(this)->getType(); 411 case scSequentialUMinExpr: 412 return cast<SCEVSequentialMinMaxExpr>(this)->getType(); 413 case scAddExpr: 414 return cast<SCEVAddExpr>(this)->getType(); 415 case scUDivExpr: 416 return cast<SCEVUDivExpr>(this)->getType(); 417 case scUnknown: 418 return cast<SCEVUnknown>(this)->getType(); 419 case scCouldNotCompute: 420 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); 421 } 422 llvm_unreachable("Unknown SCEV kind!"); 423 } 424 425 bool SCEV::isZero() const { 426 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 427 return SC->getValue()->isZero(); 428 return false; 429 } 430 431 bool SCEV::isOne() const { 432 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 433 return SC->getValue()->isOne(); 434 return false; 435 } 436 437 bool SCEV::isAllOnesValue() const { 438 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 439 return SC->getValue()->isMinusOne(); 440 return false; 441 } 442 443 bool SCEV::isNonConstantNegative() const { 444 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this); 445 if (!Mul) return false; 446 447 // If there is a constant factor, it will be first. 448 const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0)); 449 if (!SC) return false; 450 451 // Return true if the value is negative, this matches things like (-42 * V). 452 return SC->getAPInt().isNegative(); 453 } 454 455 SCEVCouldNotCompute::SCEVCouldNotCompute() : 456 SCEV(FoldingSetNodeIDRef(), scCouldNotCompute, 0) {} 457 458 bool SCEVCouldNotCompute::classof(const SCEV *S) { 459 return S->getSCEVType() == scCouldNotCompute; 460 } 461 462 const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { 463 FoldingSetNodeID ID; 464 ID.AddInteger(scConstant); 465 ID.AddPointer(V); 466 void *IP = nullptr; 467 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 468 SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V); 469 UniqueSCEVs.InsertNode(S, IP); 470 return S; 471 } 472 473 const SCEV *ScalarEvolution::getConstant(const APInt &Val) { 474 return getConstant(ConstantInt::get(getContext(), Val)); 475 } 476 477 const SCEV * 478 ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) { 479 IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); 480 return getConstant(ConstantInt::get(ITy, V, isSigned)); 481 } 482 483 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, 484 const SCEV *op, Type *ty) 485 : SCEV(ID, SCEVTy, computeExpressionSize(op)), Ty(ty) { 486 Operands[0] = op; 487 } 488 489 SCEVPtrToIntExpr::SCEVPtrToIntExpr(const FoldingSetNodeIDRef ID, const SCEV *Op, 490 Type *ITy) 491 : SCEVCastExpr(ID, scPtrToInt, Op, ITy) { 492 assert(getOperand()->getType()->isPointerTy() && Ty->isIntegerTy() && 493 "Must be a non-bit-width-changing pointer-to-integer cast!"); 494 } 495 496 SCEVIntegralCastExpr::SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID, 497 SCEVTypes SCEVTy, const SCEV *op, 498 Type *ty) 499 : SCEVCastExpr(ID, SCEVTy, op, ty) {} 500 501 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, const SCEV *op, 502 Type *ty) 503 : SCEVIntegralCastExpr(ID, scTruncate, op, ty) { 504 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 505 "Cannot truncate non-integer value!"); 506 } 507 508 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, 509 const SCEV *op, Type *ty) 510 : SCEVIntegralCastExpr(ID, scZeroExtend, op, ty) { 511 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 512 "Cannot zero extend non-integer value!"); 513 } 514 515 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, 516 const SCEV *op, Type *ty) 517 : SCEVIntegralCastExpr(ID, scSignExtend, op, ty) { 518 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 519 "Cannot sign extend non-integer value!"); 520 } 521 522 void SCEVUnknown::deleted() { 523 // Clear this SCEVUnknown from various maps. 524 SE->forgetMemoizedResults(this); 525 526 // Remove this SCEVUnknown from the uniquing map. 527 SE->UniqueSCEVs.RemoveNode(this); 528 529 // Release the value. 530 setValPtr(nullptr); 531 } 532 533 void SCEVUnknown::allUsesReplacedWith(Value *New) { 534 // Clear this SCEVUnknown from various maps. 535 SE->forgetMemoizedResults(this); 536 537 // Remove this SCEVUnknown from the uniquing map. 538 SE->UniqueSCEVs.RemoveNode(this); 539 540 // Replace the value pointer in case someone is still using this SCEVUnknown. 541 setValPtr(New); 542 } 543 544 bool SCEVUnknown::isSizeOf(Type *&AllocTy) const { 545 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) 546 if (VCE->getOpcode() == Instruction::PtrToInt) 547 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) 548 if (CE->getOpcode() == Instruction::GetElementPtr && 549 CE->getOperand(0)->isNullValue() && 550 CE->getNumOperands() == 2) 551 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1))) 552 if (CI->isOne()) { 553 AllocTy = cast<GEPOperator>(CE)->getSourceElementType(); 554 return true; 555 } 556 557 return false; 558 } 559 560 bool SCEVUnknown::isAlignOf(Type *&AllocTy) const { 561 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) 562 if (VCE->getOpcode() == Instruction::PtrToInt) 563 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) 564 if (CE->getOpcode() == Instruction::GetElementPtr && 565 CE->getOperand(0)->isNullValue()) { 566 Type *Ty = cast<GEPOperator>(CE)->getSourceElementType(); 567 if (StructType *STy = dyn_cast<StructType>(Ty)) 568 if (!STy->isPacked() && 569 CE->getNumOperands() == 3 && 570 CE->getOperand(1)->isNullValue()) { 571 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2))) 572 if (CI->isOne() && 573 STy->getNumElements() == 2 && 574 STy->getElementType(0)->isIntegerTy(1)) { 575 AllocTy = STy->getElementType(1); 576 return true; 577 } 578 } 579 } 580 581 return false; 582 } 583 584 bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const { 585 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) 586 if (VCE->getOpcode() == Instruction::PtrToInt) 587 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) 588 if (CE->getOpcode() == Instruction::GetElementPtr && 589 CE->getNumOperands() == 3 && 590 CE->getOperand(0)->isNullValue() && 591 CE->getOperand(1)->isNullValue()) { 592 Type *Ty = cast<GEPOperator>(CE)->getSourceElementType(); 593 // Ignore vector types here so that ScalarEvolutionExpander doesn't 594 // emit getelementptrs that index into vectors. 595 if (Ty->isStructTy() || Ty->isArrayTy()) { 596 CTy = Ty; 597 FieldNo = CE->getOperand(2); 598 return true; 599 } 600 } 601 602 return false; 603 } 604 605 //===----------------------------------------------------------------------===// 606 // SCEV Utilities 607 //===----------------------------------------------------------------------===// 608 609 /// Compare the two values \p LV and \p RV in terms of their "complexity" where 610 /// "complexity" is a partial (and somewhat ad-hoc) relation used to order 611 /// operands in SCEV expressions. \p EqCache is a set of pairs of values that 612 /// have been previously deemed to be "equally complex" by this routine. It is 613 /// intended to avoid exponential time complexity in cases like: 614 /// 615 /// %a = f(%x, %y) 616 /// %b = f(%a, %a) 617 /// %c = f(%b, %b) 618 /// 619 /// %d = f(%x, %y) 620 /// %e = f(%d, %d) 621 /// %f = f(%e, %e) 622 /// 623 /// CompareValueComplexity(%f, %c) 624 /// 625 /// Since we do not continue running this routine on expression trees once we 626 /// have seen unequal values, there is no need to track them in the cache. 627 static int 628 CompareValueComplexity(EquivalenceClasses<const Value *> &EqCacheValue, 629 const LoopInfo *const LI, Value *LV, Value *RV, 630 unsigned Depth) { 631 if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV)) 632 return 0; 633 634 // Order pointer values after integer values. This helps SCEVExpander form 635 // GEPs. 636 bool LIsPointer = LV->getType()->isPointerTy(), 637 RIsPointer = RV->getType()->isPointerTy(); 638 if (LIsPointer != RIsPointer) 639 return (int)LIsPointer - (int)RIsPointer; 640 641 // Compare getValueID values. 642 unsigned LID = LV->getValueID(), RID = RV->getValueID(); 643 if (LID != RID) 644 return (int)LID - (int)RID; 645 646 // Sort arguments by their position. 647 if (const auto *LA = dyn_cast<Argument>(LV)) { 648 const auto *RA = cast<Argument>(RV); 649 unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo(); 650 return (int)LArgNo - (int)RArgNo; 651 } 652 653 if (const auto *LGV = dyn_cast<GlobalValue>(LV)) { 654 const auto *RGV = cast<GlobalValue>(RV); 655 656 const auto IsGVNameSemantic = [&](const GlobalValue *GV) { 657 auto LT = GV->getLinkage(); 658 return !(GlobalValue::isPrivateLinkage(LT) || 659 GlobalValue::isInternalLinkage(LT)); 660 }; 661 662 // Use the names to distinguish the two values, but only if the 663 // names are semantically important. 664 if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV)) 665 return LGV->getName().compare(RGV->getName()); 666 } 667 668 // For instructions, compare their loop depth, and their operand count. This 669 // is pretty loose. 670 if (const auto *LInst = dyn_cast<Instruction>(LV)) { 671 const auto *RInst = cast<Instruction>(RV); 672 673 // Compare loop depths. 674 const BasicBlock *LParent = LInst->getParent(), 675 *RParent = RInst->getParent(); 676 if (LParent != RParent) { 677 unsigned LDepth = LI->getLoopDepth(LParent), 678 RDepth = LI->getLoopDepth(RParent); 679 if (LDepth != RDepth) 680 return (int)LDepth - (int)RDepth; 681 } 682 683 // Compare the number of operands. 684 unsigned LNumOps = LInst->getNumOperands(), 685 RNumOps = RInst->getNumOperands(); 686 if (LNumOps != RNumOps) 687 return (int)LNumOps - (int)RNumOps; 688 689 for (unsigned Idx : seq(0u, LNumOps)) { 690 int Result = 691 CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx), 692 RInst->getOperand(Idx), Depth + 1); 693 if (Result != 0) 694 return Result; 695 } 696 } 697 698 EqCacheValue.unionSets(LV, RV); 699 return 0; 700 } 701 702 // Return negative, zero, or positive, if LHS is less than, equal to, or greater 703 // than RHS, respectively. A three-way result allows recursive comparisons to be 704 // more efficient. 705 // If the max analysis depth was reached, return None, assuming we do not know 706 // if they are equivalent for sure. 707 static Optional<int> 708 CompareSCEVComplexity(EquivalenceClasses<const SCEV *> &EqCacheSCEV, 709 EquivalenceClasses<const Value *> &EqCacheValue, 710 const LoopInfo *const LI, const SCEV *LHS, 711 const SCEV *RHS, DominatorTree &DT, unsigned Depth = 0) { 712 // Fast-path: SCEVs are uniqued so we can do a quick equality check. 713 if (LHS == RHS) 714 return 0; 715 716 // Primarily, sort the SCEVs by their getSCEVType(). 717 SCEVTypes LType = LHS->getSCEVType(), RType = RHS->getSCEVType(); 718 if (LType != RType) 719 return (int)LType - (int)RType; 720 721 if (EqCacheSCEV.isEquivalent(LHS, RHS)) 722 return 0; 723 724 if (Depth > MaxSCEVCompareDepth) 725 return None; 726 727 // Aside from the getSCEVType() ordering, the particular ordering 728 // isn't very important except that it's beneficial to be consistent, 729 // so that (a + b) and (b + a) don't end up as different expressions. 730 switch (LType) { 731 case scUnknown: { 732 const SCEVUnknown *LU = cast<SCEVUnknown>(LHS); 733 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); 734 735 int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(), 736 RU->getValue(), Depth + 1); 737 if (X == 0) 738 EqCacheSCEV.unionSets(LHS, RHS); 739 return X; 740 } 741 742 case scConstant: { 743 const SCEVConstant *LC = cast<SCEVConstant>(LHS); 744 const SCEVConstant *RC = cast<SCEVConstant>(RHS); 745 746 // Compare constant values. 747 const APInt &LA = LC->getAPInt(); 748 const APInt &RA = RC->getAPInt(); 749 unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth(); 750 if (LBitWidth != RBitWidth) 751 return (int)LBitWidth - (int)RBitWidth; 752 return LA.ult(RA) ? -1 : 1; 753 } 754 755 case scAddRecExpr: { 756 const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS); 757 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); 758 759 // There is always a dominance between two recs that are used by one SCEV, 760 // so we can safely sort recs by loop header dominance. We require such 761 // order in getAddExpr. 762 const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop(); 763 if (LLoop != RLoop) { 764 const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader(); 765 assert(LHead != RHead && "Two loops share the same header?"); 766 if (DT.dominates(LHead, RHead)) 767 return 1; 768 else 769 assert(DT.dominates(RHead, LHead) && 770 "No dominance between recurrences used by one SCEV?"); 771 return -1; 772 } 773 774 // Addrec complexity grows with operand count. 775 unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands(); 776 if (LNumOps != RNumOps) 777 return (int)LNumOps - (int)RNumOps; 778 779 // Lexicographically compare. 780 for (unsigned i = 0; i != LNumOps; ++i) { 781 auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, 782 LA->getOperand(i), RA->getOperand(i), DT, 783 Depth + 1); 784 if (X != 0) 785 return X; 786 } 787 EqCacheSCEV.unionSets(LHS, RHS); 788 return 0; 789 } 790 791 case scAddExpr: 792 case scMulExpr: 793 case scSMaxExpr: 794 case scUMaxExpr: 795 case scSMinExpr: 796 case scUMinExpr: 797 case scSequentialUMinExpr: { 798 const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS); 799 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); 800 801 // Lexicographically compare n-ary expressions. 802 unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands(); 803 if (LNumOps != RNumOps) 804 return (int)LNumOps - (int)RNumOps; 805 806 for (unsigned i = 0; i != LNumOps; ++i) { 807 auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, 808 LC->getOperand(i), RC->getOperand(i), DT, 809 Depth + 1); 810 if (X != 0) 811 return X; 812 } 813 EqCacheSCEV.unionSets(LHS, RHS); 814 return 0; 815 } 816 817 case scUDivExpr: { 818 const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS); 819 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); 820 821 // Lexicographically compare udiv expressions. 822 auto X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getLHS(), 823 RC->getLHS(), DT, Depth + 1); 824 if (X != 0) 825 return X; 826 X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getRHS(), 827 RC->getRHS(), DT, Depth + 1); 828 if (X == 0) 829 EqCacheSCEV.unionSets(LHS, RHS); 830 return X; 831 } 832 833 case scPtrToInt: 834 case scTruncate: 835 case scZeroExtend: 836 case scSignExtend: { 837 const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS); 838 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); 839 840 // Compare cast expressions by operand. 841 auto X = 842 CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getOperand(), 843 RC->getOperand(), DT, Depth + 1); 844 if (X == 0) 845 EqCacheSCEV.unionSets(LHS, RHS); 846 return X; 847 } 848 849 case scCouldNotCompute: 850 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); 851 } 852 llvm_unreachable("Unknown SCEV kind!"); 853 } 854 855 /// Given a list of SCEV objects, order them by their complexity, and group 856 /// objects of the same complexity together by value. When this routine is 857 /// finished, we know that any duplicates in the vector are consecutive and that 858 /// complexity is monotonically increasing. 859 /// 860 /// Note that we go take special precautions to ensure that we get deterministic 861 /// results from this routine. In other words, we don't want the results of 862 /// this to depend on where the addresses of various SCEV objects happened to 863 /// land in memory. 864 static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, 865 LoopInfo *LI, DominatorTree &DT) { 866 if (Ops.size() < 2) return; // Noop 867 868 EquivalenceClasses<const SCEV *> EqCacheSCEV; 869 EquivalenceClasses<const Value *> EqCacheValue; 870 871 // Whether LHS has provably less complexity than RHS. 872 auto IsLessComplex = [&](const SCEV *LHS, const SCEV *RHS) { 873 auto Complexity = 874 CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LHS, RHS, DT); 875 return Complexity && *Complexity < 0; 876 }; 877 if (Ops.size() == 2) { 878 // This is the common case, which also happens to be trivially simple. 879 // Special case it. 880 const SCEV *&LHS = Ops[0], *&RHS = Ops[1]; 881 if (IsLessComplex(RHS, LHS)) 882 std::swap(LHS, RHS); 883 return; 884 } 885 886 // Do the rough sort by complexity. 887 llvm::stable_sort(Ops, [&](const SCEV *LHS, const SCEV *RHS) { 888 return IsLessComplex(LHS, RHS); 889 }); 890 891 // Now that we are sorted by complexity, group elements of the same 892 // complexity. Note that this is, at worst, N^2, but the vector is likely to 893 // be extremely short in practice. Note that we take this approach because we 894 // do not want to depend on the addresses of the objects we are grouping. 895 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { 896 const SCEV *S = Ops[i]; 897 unsigned Complexity = S->getSCEVType(); 898 899 // If there are any objects of the same complexity and same value as this 900 // one, group them. 901 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { 902 if (Ops[j] == S) { // Found a duplicate. 903 // Move it to immediately after i'th element. 904 std::swap(Ops[i+1], Ops[j]); 905 ++i; // no need to rescan it. 906 if (i == e-2) return; // Done! 907 } 908 } 909 } 910 } 911 912 /// Returns true if \p Ops contains a huge SCEV (the subtree of S contains at 913 /// least HugeExprThreshold nodes). 914 static bool hasHugeExpression(ArrayRef<const SCEV *> Ops) { 915 return any_of(Ops, [](const SCEV *S) { 916 return S->getExpressionSize() >= HugeExprThreshold; 917 }); 918 } 919 920 //===----------------------------------------------------------------------===// 921 // Simple SCEV method implementations 922 //===----------------------------------------------------------------------===// 923 924 /// Compute BC(It, K). The result has width W. Assume, K > 0. 925 static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, 926 ScalarEvolution &SE, 927 Type *ResultTy) { 928 // Handle the simplest case efficiently. 929 if (K == 1) 930 return SE.getTruncateOrZeroExtend(It, ResultTy); 931 932 // We are using the following formula for BC(It, K): 933 // 934 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! 935 // 936 // Suppose, W is the bitwidth of the return value. We must be prepared for 937 // overflow. Hence, we must assure that the result of our computation is 938 // equal to the accurate one modulo 2^W. Unfortunately, division isn't 939 // safe in modular arithmetic. 940 // 941 // However, this code doesn't use exactly that formula; the formula it uses 942 // is something like the following, where T is the number of factors of 2 in 943 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is 944 // exponentiation: 945 // 946 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) 947 // 948 // This formula is trivially equivalent to the previous formula. However, 949 // this formula can be implemented much more efficiently. The trick is that 950 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular 951 // arithmetic. To do exact division in modular arithmetic, all we have 952 // to do is multiply by the inverse. Therefore, this step can be done at 953 // width W. 954 // 955 // The next issue is how to safely do the division by 2^T. The way this 956 // is done is by doing the multiplication step at a width of at least W + T 957 // bits. This way, the bottom W+T bits of the product are accurate. Then, 958 // when we perform the division by 2^T (which is equivalent to a right shift 959 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get 960 // truncated out after the division by 2^T. 961 // 962 // In comparison to just directly using the first formula, this technique 963 // is much more efficient; using the first formula requires W * K bits, 964 // but this formula less than W + K bits. Also, the first formula requires 965 // a division step, whereas this formula only requires multiplies and shifts. 966 // 967 // It doesn't matter whether the subtraction step is done in the calculation 968 // width or the input iteration count's width; if the subtraction overflows, 969 // the result must be zero anyway. We prefer here to do it in the width of 970 // the induction variable because it helps a lot for certain cases; CodeGen 971 // isn't smart enough to ignore the overflow, which leads to much less 972 // efficient code if the width of the subtraction is wider than the native 973 // register width. 974 // 975 // (It's possible to not widen at all by pulling out factors of 2 before 976 // the multiplication; for example, K=2 can be calculated as 977 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires 978 // extra arithmetic, so it's not an obvious win, and it gets 979 // much more complicated for K > 3.) 980 981 // Protection from insane SCEVs; this bound is conservative, 982 // but it probably doesn't matter. 983 if (K > 1000) 984 return SE.getCouldNotCompute(); 985 986 unsigned W = SE.getTypeSizeInBits(ResultTy); 987 988 // Calculate K! / 2^T and T; we divide out the factors of two before 989 // multiplying for calculating K! / 2^T to avoid overflow. 990 // Other overflow doesn't matter because we only care about the bottom 991 // W bits of the result. 992 APInt OddFactorial(W, 1); 993 unsigned T = 1; 994 for (unsigned i = 3; i <= K; ++i) { 995 APInt Mult(W, i); 996 unsigned TwoFactors = Mult.countTrailingZeros(); 997 T += TwoFactors; 998 Mult.lshrInPlace(TwoFactors); 999 OddFactorial *= Mult; 1000 } 1001 1002 // We need at least W + T bits for the multiplication step 1003 unsigned CalculationBits = W + T; 1004 1005 // Calculate 2^T, at width T+W. 1006 APInt DivFactor = APInt::getOneBitSet(CalculationBits, T); 1007 1008 // Calculate the multiplicative inverse of K! / 2^T; 1009 // this multiplication factor will perform the exact division by 1010 // K! / 2^T. 1011 APInt Mod = APInt::getSignedMinValue(W+1); 1012 APInt MultiplyFactor = OddFactorial.zext(W+1); 1013 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); 1014 MultiplyFactor = MultiplyFactor.trunc(W); 1015 1016 // Calculate the product, at width T+W 1017 IntegerType *CalculationTy = IntegerType::get(SE.getContext(), 1018 CalculationBits); 1019 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); 1020 for (unsigned i = 1; i != K; ++i) { 1021 const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i)); 1022 Dividend = SE.getMulExpr(Dividend, 1023 SE.getTruncateOrZeroExtend(S, CalculationTy)); 1024 } 1025 1026 // Divide by 2^T 1027 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); 1028 1029 // Truncate the result, and divide by K! / 2^T. 1030 1031 return SE.getMulExpr(SE.getConstant(MultiplyFactor), 1032 SE.getTruncateOrZeroExtend(DivResult, ResultTy)); 1033 } 1034 1035 /// Return the value of this chain of recurrences at the specified iteration 1036 /// number. We can evaluate this recurrence by multiplying each element in the 1037 /// chain by the binomial coefficient corresponding to it. In other words, we 1038 /// can evaluate {A,+,B,+,C,+,D} as: 1039 /// 1040 /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) 1041 /// 1042 /// where BC(It, k) stands for binomial coefficient. 1043 const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, 1044 ScalarEvolution &SE) const { 1045 return evaluateAtIteration(makeArrayRef(op_begin(), op_end()), It, SE); 1046 } 1047 1048 const SCEV * 1049 SCEVAddRecExpr::evaluateAtIteration(ArrayRef<const SCEV *> Operands, 1050 const SCEV *It, ScalarEvolution &SE) { 1051 assert(Operands.size() > 0); 1052 const SCEV *Result = Operands[0]; 1053 for (unsigned i = 1, e = Operands.size(); i != e; ++i) { 1054 // The computation is correct in the face of overflow provided that the 1055 // multiplication is performed _after_ the evaluation of the binomial 1056 // coefficient. 1057 const SCEV *Coeff = BinomialCoefficient(It, i, SE, Result->getType()); 1058 if (isa<SCEVCouldNotCompute>(Coeff)) 1059 return Coeff; 1060 1061 Result = SE.getAddExpr(Result, SE.getMulExpr(Operands[i], Coeff)); 1062 } 1063 return Result; 1064 } 1065 1066 //===----------------------------------------------------------------------===// 1067 // SCEV Expression folder implementations 1068 //===----------------------------------------------------------------------===// 1069 1070 const SCEV *ScalarEvolution::getLosslessPtrToIntExpr(const SCEV *Op, 1071 unsigned Depth) { 1072 assert(Depth <= 1 && 1073 "getLosslessPtrToIntExpr() should self-recurse at most once."); 1074 1075 // We could be called with an integer-typed operands during SCEV rewrites. 1076 // Since the operand is an integer already, just perform zext/trunc/self cast. 1077 if (!Op->getType()->isPointerTy()) 1078 return Op; 1079 1080 // What would be an ID for such a SCEV cast expression? 1081 FoldingSetNodeID ID; 1082 ID.AddInteger(scPtrToInt); 1083 ID.AddPointer(Op); 1084 1085 void *IP = nullptr; 1086 1087 // Is there already an expression for such a cast? 1088 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) 1089 return S; 1090 1091 // It isn't legal for optimizations to construct new ptrtoint expressions 1092 // for non-integral pointers. 1093 if (getDataLayout().isNonIntegralPointerType(Op->getType())) 1094 return getCouldNotCompute(); 1095 1096 Type *IntPtrTy = getDataLayout().getIntPtrType(Op->getType()); 1097 1098 // We can only trivially model ptrtoint if SCEV's effective (integer) type 1099 // is sufficiently wide to represent all possible pointer values. 1100 // We could theoretically teach SCEV to truncate wider pointers, but 1101 // that isn't implemented for now. 1102 if (getDataLayout().getTypeSizeInBits(getEffectiveSCEVType(Op->getType())) != 1103 getDataLayout().getTypeSizeInBits(IntPtrTy)) 1104 return getCouldNotCompute(); 1105 1106 // If not, is this expression something we can't reduce any further? 1107 if (auto *U = dyn_cast<SCEVUnknown>(Op)) { 1108 // Perform some basic constant folding. If the operand of the ptr2int cast 1109 // is a null pointer, don't create a ptr2int SCEV expression (that will be 1110 // left as-is), but produce a zero constant. 1111 // NOTE: We could handle a more general case, but lack motivational cases. 1112 if (isa<ConstantPointerNull>(U->getValue())) 1113 return getZero(IntPtrTy); 1114 1115 // Create an explicit cast node. 1116 // We can reuse the existing insert position since if we get here, 1117 // we won't have made any changes which would invalidate it. 1118 SCEV *S = new (SCEVAllocator) 1119 SCEVPtrToIntExpr(ID.Intern(SCEVAllocator), Op, IntPtrTy); 1120 UniqueSCEVs.InsertNode(S, IP); 1121 registerUser(S, Op); 1122 return S; 1123 } 1124 1125 assert(Depth == 0 && "getLosslessPtrToIntExpr() should not self-recurse for " 1126 "non-SCEVUnknown's."); 1127 1128 // Otherwise, we've got some expression that is more complex than just a 1129 // single SCEVUnknown. But we don't want to have a SCEVPtrToIntExpr of an 1130 // arbitrary expression, we want to have SCEVPtrToIntExpr of an SCEVUnknown 1131 // only, and the expressions must otherwise be integer-typed. 1132 // So sink the cast down to the SCEVUnknown's. 1133 1134 /// The SCEVPtrToIntSinkingRewriter takes a scalar evolution expression, 1135 /// which computes a pointer-typed value, and rewrites the whole expression 1136 /// tree so that *all* the computations are done on integers, and the only 1137 /// pointer-typed operands in the expression are SCEVUnknown. 1138 class SCEVPtrToIntSinkingRewriter 1139 : public SCEVRewriteVisitor<SCEVPtrToIntSinkingRewriter> { 1140 using Base = SCEVRewriteVisitor<SCEVPtrToIntSinkingRewriter>; 1141 1142 public: 1143 SCEVPtrToIntSinkingRewriter(ScalarEvolution &SE) : SCEVRewriteVisitor(SE) {} 1144 1145 static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE) { 1146 SCEVPtrToIntSinkingRewriter Rewriter(SE); 1147 return Rewriter.visit(Scev); 1148 } 1149 1150 const SCEV *visit(const SCEV *S) { 1151 Type *STy = S->getType(); 1152 // If the expression is not pointer-typed, just keep it as-is. 1153 if (!STy->isPointerTy()) 1154 return S; 1155 // Else, recursively sink the cast down into it. 1156 return Base::visit(S); 1157 } 1158 1159 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) { 1160 SmallVector<const SCEV *, 2> Operands; 1161 bool Changed = false; 1162 for (auto *Op : Expr->operands()) { 1163 Operands.push_back(visit(Op)); 1164 Changed |= Op != Operands.back(); 1165 } 1166 return !Changed ? Expr : SE.getAddExpr(Operands, Expr->getNoWrapFlags()); 1167 } 1168 1169 const SCEV *visitMulExpr(const SCEVMulExpr *Expr) { 1170 SmallVector<const SCEV *, 2> Operands; 1171 bool Changed = false; 1172 for (auto *Op : Expr->operands()) { 1173 Operands.push_back(visit(Op)); 1174 Changed |= Op != Operands.back(); 1175 } 1176 return !Changed ? Expr : SE.getMulExpr(Operands, Expr->getNoWrapFlags()); 1177 } 1178 1179 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 1180 assert(Expr->getType()->isPointerTy() && 1181 "Should only reach pointer-typed SCEVUnknown's."); 1182 return SE.getLosslessPtrToIntExpr(Expr, /*Depth=*/1); 1183 } 1184 }; 1185 1186 // And actually perform the cast sinking. 1187 const SCEV *IntOp = SCEVPtrToIntSinkingRewriter::rewrite(Op, *this); 1188 assert(IntOp->getType()->isIntegerTy() && 1189 "We must have succeeded in sinking the cast, " 1190 "and ending up with an integer-typed expression!"); 1191 return IntOp; 1192 } 1193 1194 const SCEV *ScalarEvolution::getPtrToIntExpr(const SCEV *Op, Type *Ty) { 1195 assert(Ty->isIntegerTy() && "Target type must be an integer type!"); 1196 1197 const SCEV *IntOp = getLosslessPtrToIntExpr(Op); 1198 if (isa<SCEVCouldNotCompute>(IntOp)) 1199 return IntOp; 1200 1201 return getTruncateOrZeroExtend(IntOp, Ty); 1202 } 1203 1204 const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty, 1205 unsigned Depth) { 1206 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && 1207 "This is not a truncating conversion!"); 1208 assert(isSCEVable(Ty) && 1209 "This is not a conversion to a SCEVable type!"); 1210 assert(!Op->getType()->isPointerTy() && "Can't truncate pointer!"); 1211 Ty = getEffectiveSCEVType(Ty); 1212 1213 FoldingSetNodeID ID; 1214 ID.AddInteger(scTruncate); 1215 ID.AddPointer(Op); 1216 ID.AddPointer(Ty); 1217 void *IP = nullptr; 1218 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1219 1220 // Fold if the operand is constant. 1221 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 1222 return getConstant( 1223 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); 1224 1225 // trunc(trunc(x)) --> trunc(x) 1226 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) 1227 return getTruncateExpr(ST->getOperand(), Ty, Depth + 1); 1228 1229 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing 1230 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) 1231 return getTruncateOrSignExtend(SS->getOperand(), Ty, Depth + 1); 1232 1233 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing 1234 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) 1235 return getTruncateOrZeroExtend(SZ->getOperand(), Ty, Depth + 1); 1236 1237 if (Depth > MaxCastDepth) { 1238 SCEV *S = 1239 new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), Op, Ty); 1240 UniqueSCEVs.InsertNode(S, IP); 1241 registerUser(S, Op); 1242 return S; 1243 } 1244 1245 // trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and 1246 // trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN), 1247 // if after transforming we have at most one truncate, not counting truncates 1248 // that replace other casts. 1249 if (isa<SCEVAddExpr>(Op) || isa<SCEVMulExpr>(Op)) { 1250 auto *CommOp = cast<SCEVCommutativeExpr>(Op); 1251 SmallVector<const SCEV *, 4> Operands; 1252 unsigned numTruncs = 0; 1253 for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2; 1254 ++i) { 1255 const SCEV *S = getTruncateExpr(CommOp->getOperand(i), Ty, Depth + 1); 1256 if (!isa<SCEVIntegralCastExpr>(CommOp->getOperand(i)) && 1257 isa<SCEVTruncateExpr>(S)) 1258 numTruncs++; 1259 Operands.push_back(S); 1260 } 1261 if (numTruncs < 2) { 1262 if (isa<SCEVAddExpr>(Op)) 1263 return getAddExpr(Operands); 1264 else if (isa<SCEVMulExpr>(Op)) 1265 return getMulExpr(Operands); 1266 else 1267 llvm_unreachable("Unexpected SCEV type for Op."); 1268 } 1269 // Although we checked in the beginning that ID is not in the cache, it is 1270 // possible that during recursion and different modification ID was inserted 1271 // into the cache. So if we find it, just return it. 1272 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) 1273 return S; 1274 } 1275 1276 // If the input value is a chrec scev, truncate the chrec's operands. 1277 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { 1278 SmallVector<const SCEV *, 4> Operands; 1279 for (const SCEV *Op : AddRec->operands()) 1280 Operands.push_back(getTruncateExpr(Op, Ty, Depth + 1)); 1281 return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap); 1282 } 1283 1284 // Return zero if truncating to known zeros. 1285 uint32_t MinTrailingZeros = GetMinTrailingZeros(Op); 1286 if (MinTrailingZeros >= getTypeSizeInBits(Ty)) 1287 return getZero(Ty); 1288 1289 // The cast wasn't folded; create an explicit cast node. We can reuse 1290 // the existing insert position since if we get here, we won't have 1291 // made any changes which would invalidate it. 1292 SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), 1293 Op, Ty); 1294 UniqueSCEVs.InsertNode(S, IP); 1295 registerUser(S, Op); 1296 return S; 1297 } 1298 1299 // Get the limit of a recurrence such that incrementing by Step cannot cause 1300 // signed overflow as long as the value of the recurrence within the 1301 // loop does not exceed this limit before incrementing. 1302 static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step, 1303 ICmpInst::Predicate *Pred, 1304 ScalarEvolution *SE) { 1305 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); 1306 if (SE->isKnownPositive(Step)) { 1307 *Pred = ICmpInst::ICMP_SLT; 1308 return SE->getConstant(APInt::getSignedMinValue(BitWidth) - 1309 SE->getSignedRangeMax(Step)); 1310 } 1311 if (SE->isKnownNegative(Step)) { 1312 *Pred = ICmpInst::ICMP_SGT; 1313 return SE->getConstant(APInt::getSignedMaxValue(BitWidth) - 1314 SE->getSignedRangeMin(Step)); 1315 } 1316 return nullptr; 1317 } 1318 1319 // Get the limit of a recurrence such that incrementing by Step cannot cause 1320 // unsigned overflow as long as the value of the recurrence within the loop does 1321 // not exceed this limit before incrementing. 1322 static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step, 1323 ICmpInst::Predicate *Pred, 1324 ScalarEvolution *SE) { 1325 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); 1326 *Pred = ICmpInst::ICMP_ULT; 1327 1328 return SE->getConstant(APInt::getMinValue(BitWidth) - 1329 SE->getUnsignedRangeMax(Step)); 1330 } 1331 1332 namespace { 1333 1334 struct ExtendOpTraitsBase { 1335 typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *, 1336 unsigned); 1337 }; 1338 1339 // Used to make code generic over signed and unsigned overflow. 1340 template <typename ExtendOp> struct ExtendOpTraits { 1341 // Members present: 1342 // 1343 // static const SCEV::NoWrapFlags WrapType; 1344 // 1345 // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr; 1346 // 1347 // static const SCEV *getOverflowLimitForStep(const SCEV *Step, 1348 // ICmpInst::Predicate *Pred, 1349 // ScalarEvolution *SE); 1350 }; 1351 1352 template <> 1353 struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase { 1354 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW; 1355 1356 static const GetExtendExprTy GetExtendExpr; 1357 1358 static const SCEV *getOverflowLimitForStep(const SCEV *Step, 1359 ICmpInst::Predicate *Pred, 1360 ScalarEvolution *SE) { 1361 return getSignedOverflowLimitForStep(Step, Pred, SE); 1362 } 1363 }; 1364 1365 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits< 1366 SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr; 1367 1368 template <> 1369 struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase { 1370 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW; 1371 1372 static const GetExtendExprTy GetExtendExpr; 1373 1374 static const SCEV *getOverflowLimitForStep(const SCEV *Step, 1375 ICmpInst::Predicate *Pred, 1376 ScalarEvolution *SE) { 1377 return getUnsignedOverflowLimitForStep(Step, Pred, SE); 1378 } 1379 }; 1380 1381 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits< 1382 SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr; 1383 1384 } // end anonymous namespace 1385 1386 // The recurrence AR has been shown to have no signed/unsigned wrap or something 1387 // close to it. Typically, if we can prove NSW/NUW for AR, then we can just as 1388 // easily prove NSW/NUW for its preincrement or postincrement sibling. This 1389 // allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step + 1390 // Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the 1391 // expression "Step + sext/zext(PreIncAR)" is congruent with 1392 // "sext/zext(PostIncAR)" 1393 template <typename ExtendOpTy> 1394 static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty, 1395 ScalarEvolution *SE, unsigned Depth) { 1396 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType; 1397 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr; 1398 1399 const Loop *L = AR->getLoop(); 1400 const SCEV *Start = AR->getStart(); 1401 const SCEV *Step = AR->getStepRecurrence(*SE); 1402 1403 // Check for a simple looking step prior to loop entry. 1404 const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start); 1405 if (!SA) 1406 return nullptr; 1407 1408 // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV 1409 // subtraction is expensive. For this purpose, perform a quick and dirty 1410 // difference, by checking for Step in the operand list. 1411 SmallVector<const SCEV *, 4> DiffOps; 1412 for (const SCEV *Op : SA->operands()) 1413 if (Op != Step) 1414 DiffOps.push_back(Op); 1415 1416 if (DiffOps.size() == SA->getNumOperands()) 1417 return nullptr; 1418 1419 // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` + 1420 // `Step`: 1421 1422 // 1. NSW/NUW flags on the step increment. 1423 auto PreStartFlags = 1424 ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW); 1425 const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags); 1426 const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>( 1427 SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap)); 1428 1429 // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies 1430 // "S+X does not sign/unsign-overflow". 1431 // 1432 1433 const SCEV *BECount = SE->getBackedgeTakenCount(L); 1434 if (PreAR && PreAR->getNoWrapFlags(WrapType) && 1435 !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount)) 1436 return PreStart; 1437 1438 // 2. Direct overflow check on the step operation's expression. 1439 unsigned BitWidth = SE->getTypeSizeInBits(AR->getType()); 1440 Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2); 1441 const SCEV *OperandExtendedStart = 1442 SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth), 1443 (SE->*GetExtendExpr)(Step, WideTy, Depth)); 1444 if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) { 1445 if (PreAR && AR->getNoWrapFlags(WrapType)) { 1446 // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW 1447 // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then 1448 // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact. 1449 SE->setNoWrapFlags(const_cast<SCEVAddRecExpr *>(PreAR), WrapType); 1450 } 1451 return PreStart; 1452 } 1453 1454 // 3. Loop precondition. 1455 ICmpInst::Predicate Pred; 1456 const SCEV *OverflowLimit = 1457 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE); 1458 1459 if (OverflowLimit && 1460 SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit)) 1461 return PreStart; 1462 1463 return nullptr; 1464 } 1465 1466 // Get the normalized zero or sign extended expression for this AddRec's Start. 1467 template <typename ExtendOpTy> 1468 static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty, 1469 ScalarEvolution *SE, 1470 unsigned Depth) { 1471 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr; 1472 1473 const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE, Depth); 1474 if (!PreStart) 1475 return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth); 1476 1477 return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty, 1478 Depth), 1479 (SE->*GetExtendExpr)(PreStart, Ty, Depth)); 1480 } 1481 1482 // Try to prove away overflow by looking at "nearby" add recurrences. A 1483 // motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it 1484 // does not itself wrap then we can conclude that `{1,+,4}` is `nuw`. 1485 // 1486 // Formally: 1487 // 1488 // {S,+,X} == {S-T,+,X} + T 1489 // => Ext({S,+,X}) == Ext({S-T,+,X} + T) 1490 // 1491 // If ({S-T,+,X} + T) does not overflow ... (1) 1492 // 1493 // RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T) 1494 // 1495 // If {S-T,+,X} does not overflow ... (2) 1496 // 1497 // RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T) 1498 // == {Ext(S-T)+Ext(T),+,Ext(X)} 1499 // 1500 // If (S-T)+T does not overflow ... (3) 1501 // 1502 // RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)} 1503 // == {Ext(S),+,Ext(X)} == LHS 1504 // 1505 // Thus, if (1), (2) and (3) are true for some T, then 1506 // Ext({S,+,X}) == {Ext(S),+,Ext(X)} 1507 // 1508 // (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T) 1509 // does not overflow" restricted to the 0th iteration. Therefore we only need 1510 // to check for (1) and (2). 1511 // 1512 // In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T 1513 // is `Delta` (defined below). 1514 template <typename ExtendOpTy> 1515 bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start, 1516 const SCEV *Step, 1517 const Loop *L) { 1518 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType; 1519 1520 // We restrict `Start` to a constant to prevent SCEV from spending too much 1521 // time here. It is correct (but more expensive) to continue with a 1522 // non-constant `Start` and do a general SCEV subtraction to compute 1523 // `PreStart` below. 1524 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start); 1525 if (!StartC) 1526 return false; 1527 1528 APInt StartAI = StartC->getAPInt(); 1529 1530 for (unsigned Delta : {-2, -1, 1, 2}) { 1531 const SCEV *PreStart = getConstant(StartAI - Delta); 1532 1533 FoldingSetNodeID ID; 1534 ID.AddInteger(scAddRecExpr); 1535 ID.AddPointer(PreStart); 1536 ID.AddPointer(Step); 1537 ID.AddPointer(L); 1538 void *IP = nullptr; 1539 const auto *PreAR = 1540 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); 1541 1542 // Give up if we don't already have the add recurrence we need because 1543 // actually constructing an add recurrence is relatively expensive. 1544 if (PreAR && PreAR->getNoWrapFlags(WrapType)) { // proves (2) 1545 const SCEV *DeltaS = getConstant(StartC->getType(), Delta); 1546 ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE; 1547 const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep( 1548 DeltaS, &Pred, this); 1549 if (Limit && isKnownPredicate(Pred, PreAR, Limit)) // proves (1) 1550 return true; 1551 } 1552 } 1553 1554 return false; 1555 } 1556 1557 // Finds an integer D for an expression (C + x + y + ...) such that the top 1558 // level addition in (D + (C - D + x + y + ...)) would not wrap (signed or 1559 // unsigned) and the number of trailing zeros of (C - D + x + y + ...) is 1560 // maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and 1561 // the (C + x + y + ...) expression is \p WholeAddExpr. 1562 static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, 1563 const SCEVConstant *ConstantTerm, 1564 const SCEVAddExpr *WholeAddExpr) { 1565 const APInt &C = ConstantTerm->getAPInt(); 1566 const unsigned BitWidth = C.getBitWidth(); 1567 // Find number of trailing zeros of (x + y + ...) w/o the C first: 1568 uint32_t TZ = BitWidth; 1569 for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I) 1570 TZ = std::min(TZ, SE.GetMinTrailingZeros(WholeAddExpr->getOperand(I))); 1571 if (TZ) { 1572 // Set D to be as many least significant bits of C as possible while still 1573 // guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap: 1574 return TZ < BitWidth ? C.trunc(TZ).zext(BitWidth) : C; 1575 } 1576 return APInt(BitWidth, 0); 1577 } 1578 1579 // Finds an integer D for an affine AddRec expression {C,+,x} such that the top 1580 // level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the 1581 // number of trailing zeros of (C - D + x * n) is maximized, where C is the \p 1582 // ConstantStart, x is an arbitrary \p Step, and n is the loop trip count. 1583 static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, 1584 const APInt &ConstantStart, 1585 const SCEV *Step) { 1586 const unsigned BitWidth = ConstantStart.getBitWidth(); 1587 const uint32_t TZ = SE.GetMinTrailingZeros(Step); 1588 if (TZ) 1589 return TZ < BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth) 1590 : ConstantStart; 1591 return APInt(BitWidth, 0); 1592 } 1593 1594 const SCEV * 1595 ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { 1596 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 1597 "This is not an extending conversion!"); 1598 assert(isSCEVable(Ty) && 1599 "This is not a conversion to a SCEVable type!"); 1600 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); 1601 Ty = getEffectiveSCEVType(Ty); 1602 1603 // Fold if the operand is constant. 1604 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 1605 return getConstant( 1606 cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty))); 1607 1608 // zext(zext(x)) --> zext(x) 1609 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) 1610 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1); 1611 1612 // Before doing any expensive analysis, check to see if we've already 1613 // computed a SCEV for this Op and Ty. 1614 FoldingSetNodeID ID; 1615 ID.AddInteger(scZeroExtend); 1616 ID.AddPointer(Op); 1617 ID.AddPointer(Ty); 1618 void *IP = nullptr; 1619 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1620 if (Depth > MaxCastDepth) { 1621 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), 1622 Op, Ty); 1623 UniqueSCEVs.InsertNode(S, IP); 1624 registerUser(S, Op); 1625 return S; 1626 } 1627 1628 // zext(trunc(x)) --> zext(x) or x or trunc(x) 1629 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) { 1630 // It's possible the bits taken off by the truncate were all zero bits. If 1631 // so, we should be able to simplify this further. 1632 const SCEV *X = ST->getOperand(); 1633 ConstantRange CR = getUnsignedRange(X); 1634 unsigned TruncBits = getTypeSizeInBits(ST->getType()); 1635 unsigned NewBits = getTypeSizeInBits(Ty); 1636 if (CR.truncate(TruncBits).zeroExtend(NewBits).contains( 1637 CR.zextOrTrunc(NewBits))) 1638 return getTruncateOrZeroExtend(X, Ty, Depth); 1639 } 1640 1641 // If the input value is a chrec scev, and we can prove that the value 1642 // did not overflow the old, smaller, value, we can zero extend all of the 1643 // operands (often constants). This allows analysis of something like 1644 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } 1645 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) 1646 if (AR->isAffine()) { 1647 const SCEV *Start = AR->getStart(); 1648 const SCEV *Step = AR->getStepRecurrence(*this); 1649 unsigned BitWidth = getTypeSizeInBits(AR->getType()); 1650 const Loop *L = AR->getLoop(); 1651 1652 if (!AR->hasNoUnsignedWrap()) { 1653 auto NewFlags = proveNoWrapViaConstantRanges(AR); 1654 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags); 1655 } 1656 1657 // If we have special knowledge that this addrec won't overflow, 1658 // we don't need to do any further analysis. 1659 if (AR->hasNoUnsignedWrap()) { 1660 Start = 1661 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1); 1662 Step = getZeroExtendExpr(Step, Ty, Depth + 1); 1663 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 1664 } 1665 1666 // Check whether the backedge-taken count is SCEVCouldNotCompute. 1667 // Note that this serves two purposes: It filters out loops that are 1668 // simply not analyzable, and it covers the case where this code is 1669 // being called from within backedge-taken count analysis, such that 1670 // attempting to ask for the backedge-taken count would likely result 1671 // in infinite recursion. In the later case, the analysis code will 1672 // cope with a conservative value, and it will take care to purge 1673 // that value once it has finished. 1674 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); 1675 if (!isa<SCEVCouldNotCompute>(MaxBECount)) { 1676 // Manually compute the final value for AR, checking for overflow. 1677 1678 // Check whether the backedge-taken count can be losslessly casted to 1679 // the addrec's type. The count is always unsigned. 1680 const SCEV *CastedMaxBECount = 1681 getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth); 1682 const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend( 1683 CastedMaxBECount, MaxBECount->getType(), Depth); 1684 if (MaxBECount == RecastedMaxBECount) { 1685 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); 1686 // Check whether Start+Step*MaxBECount has no unsigned overflow. 1687 const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step, 1688 SCEV::FlagAnyWrap, Depth + 1); 1689 const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul, 1690 SCEV::FlagAnyWrap, 1691 Depth + 1), 1692 WideTy, Depth + 1); 1693 const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1); 1694 const SCEV *WideMaxBECount = 1695 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1); 1696 const SCEV *OperandExtendedAdd = 1697 getAddExpr(WideStart, 1698 getMulExpr(WideMaxBECount, 1699 getZeroExtendExpr(Step, WideTy, Depth + 1), 1700 SCEV::FlagAnyWrap, Depth + 1), 1701 SCEV::FlagAnyWrap, Depth + 1); 1702 if (ZAdd == OperandExtendedAdd) { 1703 // Cache knowledge of AR NUW, which is propagated to this AddRec. 1704 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNUW); 1705 // Return the expression with the addrec on the outside. 1706 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, 1707 Depth + 1); 1708 Step = getZeroExtendExpr(Step, Ty, Depth + 1); 1709 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 1710 } 1711 // Similar to above, only this time treat the step value as signed. 1712 // This covers loops that count down. 1713 OperandExtendedAdd = 1714 getAddExpr(WideStart, 1715 getMulExpr(WideMaxBECount, 1716 getSignExtendExpr(Step, WideTy, Depth + 1), 1717 SCEV::FlagAnyWrap, Depth + 1), 1718 SCEV::FlagAnyWrap, Depth + 1); 1719 if (ZAdd == OperandExtendedAdd) { 1720 // Cache knowledge of AR NW, which is propagated to this AddRec. 1721 // Negative step causes unsigned wrap, but it still can't self-wrap. 1722 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW); 1723 // Return the expression with the addrec on the outside. 1724 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, 1725 Depth + 1); 1726 Step = getSignExtendExpr(Step, Ty, Depth + 1); 1727 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 1728 } 1729 } 1730 } 1731 1732 // Normally, in the cases we can prove no-overflow via a 1733 // backedge guarding condition, we can also compute a backedge 1734 // taken count for the loop. The exceptions are assumptions and 1735 // guards present in the loop -- SCEV is not great at exploiting 1736 // these to compute max backedge taken counts, but can still use 1737 // these to prove lack of overflow. Use this fact to avoid 1738 // doing extra work that may not pay off. 1739 if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards || 1740 !AC.assumptions().empty()) { 1741 1742 auto NewFlags = proveNoUnsignedWrapViaInduction(AR); 1743 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags); 1744 if (AR->hasNoUnsignedWrap()) { 1745 // Same as nuw case above - duplicated here to avoid a compile time 1746 // issue. It's not clear that the order of checks does matter, but 1747 // it's one of two issue possible causes for a change which was 1748 // reverted. Be conservative for the moment. 1749 Start = 1750 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1); 1751 Step = getZeroExtendExpr(Step, Ty, Depth + 1); 1752 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 1753 } 1754 1755 // For a negative step, we can extend the operands iff doing so only 1756 // traverses values in the range zext([0,UINT_MAX]). 1757 if (isKnownNegative(Step)) { 1758 const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - 1759 getSignedRangeMin(Step)); 1760 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) || 1761 isKnownOnEveryIteration(ICmpInst::ICMP_UGT, AR, N)) { 1762 // Cache knowledge of AR NW, which is propagated to this 1763 // AddRec. Negative step causes unsigned wrap, but it 1764 // still can't self-wrap. 1765 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW); 1766 // Return the expression with the addrec on the outside. 1767 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, 1768 Depth + 1); 1769 Step = getSignExtendExpr(Step, Ty, Depth + 1); 1770 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 1771 } 1772 } 1773 } 1774 1775 // zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step}))<nuw><nsw> 1776 // if D + (C - D + Step * n) could be proven to not unsigned wrap 1777 // where D maximizes the number of trailing zeros of (C - D + Step * n) 1778 if (const auto *SC = dyn_cast<SCEVConstant>(Start)) { 1779 const APInt &C = SC->getAPInt(); 1780 const APInt &D = extractConstantWithoutWrapping(*this, C, Step); 1781 if (D != 0) { 1782 const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth); 1783 const SCEV *SResidual = 1784 getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags()); 1785 const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1); 1786 return getAddExpr(SZExtD, SZExtR, 1787 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), 1788 Depth + 1); 1789 } 1790 } 1791 1792 if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) { 1793 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNUW); 1794 Start = 1795 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1); 1796 Step = getZeroExtendExpr(Step, Ty, Depth + 1); 1797 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 1798 } 1799 } 1800 1801 // zext(A % B) --> zext(A) % zext(B) 1802 { 1803 const SCEV *LHS; 1804 const SCEV *RHS; 1805 if (matchURem(Op, LHS, RHS)) 1806 return getURemExpr(getZeroExtendExpr(LHS, Ty, Depth + 1), 1807 getZeroExtendExpr(RHS, Ty, Depth + 1)); 1808 } 1809 1810 // zext(A / B) --> zext(A) / zext(B). 1811 if (auto *Div = dyn_cast<SCEVUDivExpr>(Op)) 1812 return getUDivExpr(getZeroExtendExpr(Div->getLHS(), Ty, Depth + 1), 1813 getZeroExtendExpr(Div->getRHS(), Ty, Depth + 1)); 1814 1815 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) { 1816 // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw> 1817 if (SA->hasNoUnsignedWrap()) { 1818 // If the addition does not unsign overflow then we can, by definition, 1819 // commute the zero extension with the addition operation. 1820 SmallVector<const SCEV *, 4> Ops; 1821 for (const auto *Op : SA->operands()) 1822 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1)); 1823 return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1); 1824 } 1825 1826 // zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...)) 1827 // if D + (C - D + x + y + ...) could be proven to not unsigned wrap 1828 // where D maximizes the number of trailing zeros of (C - D + x + y + ...) 1829 // 1830 // Often address arithmetics contain expressions like 1831 // (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))). 1832 // This transformation is useful while proving that such expressions are 1833 // equal or differ by a small constant amount, see LoadStoreVectorizer pass. 1834 if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) { 1835 const APInt &D = extractConstantWithoutWrapping(*this, SC, SA); 1836 if (D != 0) { 1837 const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth); 1838 const SCEV *SResidual = 1839 getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth); 1840 const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1); 1841 return getAddExpr(SZExtD, SZExtR, 1842 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), 1843 Depth + 1); 1844 } 1845 } 1846 } 1847 1848 if (auto *SM = dyn_cast<SCEVMulExpr>(Op)) { 1849 // zext((A * B * ...)<nuw>) --> (zext(A) * zext(B) * ...)<nuw> 1850 if (SM->hasNoUnsignedWrap()) { 1851 // If the multiply does not unsign overflow then we can, by definition, 1852 // commute the zero extension with the multiply operation. 1853 SmallVector<const SCEV *, 4> Ops; 1854 for (const auto *Op : SM->operands()) 1855 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1)); 1856 return getMulExpr(Ops, SCEV::FlagNUW, Depth + 1); 1857 } 1858 1859 // zext(2^K * (trunc X to iN)) to iM -> 1860 // 2^K * (zext(trunc X to i{N-K}) to iM)<nuw> 1861 // 1862 // Proof: 1863 // 1864 // zext(2^K * (trunc X to iN)) to iM 1865 // = zext((trunc X to iN) << K) to iM 1866 // = zext((trunc X to i{N-K}) << K)<nuw> to iM 1867 // (because shl removes the top K bits) 1868 // = zext((2^K * (trunc X to i{N-K}))<nuw>) to iM 1869 // = (2^K * (zext(trunc X to i{N-K}) to iM))<nuw>. 1870 // 1871 if (SM->getNumOperands() == 2) 1872 if (auto *MulLHS = dyn_cast<SCEVConstant>(SM->getOperand(0))) 1873 if (MulLHS->getAPInt().isPowerOf2()) 1874 if (auto *TruncRHS = dyn_cast<SCEVTruncateExpr>(SM->getOperand(1))) { 1875 int NewTruncBits = getTypeSizeInBits(TruncRHS->getType()) - 1876 MulLHS->getAPInt().logBase2(); 1877 Type *NewTruncTy = IntegerType::get(getContext(), NewTruncBits); 1878 return getMulExpr( 1879 getZeroExtendExpr(MulLHS, Ty), 1880 getZeroExtendExpr( 1881 getTruncateExpr(TruncRHS->getOperand(), NewTruncTy), Ty), 1882 SCEV::FlagNUW, Depth + 1); 1883 } 1884 } 1885 1886 // The cast wasn't folded; create an explicit cast node. 1887 // Recompute the insert position, as it may have been invalidated. 1888 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1889 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), 1890 Op, Ty); 1891 UniqueSCEVs.InsertNode(S, IP); 1892 registerUser(S, Op); 1893 return S; 1894 } 1895 1896 const SCEV * 1897 ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) { 1898 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 1899 "This is not an extending conversion!"); 1900 assert(isSCEVable(Ty) && 1901 "This is not a conversion to a SCEVable type!"); 1902 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!"); 1903 Ty = getEffectiveSCEVType(Ty); 1904 1905 // Fold if the operand is constant. 1906 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 1907 return getConstant( 1908 cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty))); 1909 1910 // sext(sext(x)) --> sext(x) 1911 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) 1912 return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1); 1913 1914 // sext(zext(x)) --> zext(x) 1915 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) 1916 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1); 1917 1918 // Before doing any expensive analysis, check to see if we've already 1919 // computed a SCEV for this Op and Ty. 1920 FoldingSetNodeID ID; 1921 ID.AddInteger(scSignExtend); 1922 ID.AddPointer(Op); 1923 ID.AddPointer(Ty); 1924 void *IP = nullptr; 1925 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1926 // Limit recursion depth. 1927 if (Depth > MaxCastDepth) { 1928 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), 1929 Op, Ty); 1930 UniqueSCEVs.InsertNode(S, IP); 1931 registerUser(S, Op); 1932 return S; 1933 } 1934 1935 // sext(trunc(x)) --> sext(x) or x or trunc(x) 1936 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) { 1937 // It's possible the bits taken off by the truncate were all sign bits. If 1938 // so, we should be able to simplify this further. 1939 const SCEV *X = ST->getOperand(); 1940 ConstantRange CR = getSignedRange(X); 1941 unsigned TruncBits = getTypeSizeInBits(ST->getType()); 1942 unsigned NewBits = getTypeSizeInBits(Ty); 1943 if (CR.truncate(TruncBits).signExtend(NewBits).contains( 1944 CR.sextOrTrunc(NewBits))) 1945 return getTruncateOrSignExtend(X, Ty, Depth); 1946 } 1947 1948 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) { 1949 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw> 1950 if (SA->hasNoSignedWrap()) { 1951 // If the addition does not sign overflow then we can, by definition, 1952 // commute the sign extension with the addition operation. 1953 SmallVector<const SCEV *, 4> Ops; 1954 for (const auto *Op : SA->operands()) 1955 Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1)); 1956 return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1); 1957 } 1958 1959 // sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...)) 1960 // if D + (C - D + x + y + ...) could be proven to not signed wrap 1961 // where D maximizes the number of trailing zeros of (C - D + x + y + ...) 1962 // 1963 // For instance, this will bring two seemingly different expressions: 1964 // 1 + sext(5 + 20 * %x + 24 * %y) and 1965 // sext(6 + 20 * %x + 24 * %y) 1966 // to the same form: 1967 // 2 + sext(4 + 20 * %x + 24 * %y) 1968 if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) { 1969 const APInt &D = extractConstantWithoutWrapping(*this, SC, SA); 1970 if (D != 0) { 1971 const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth); 1972 const SCEV *SResidual = 1973 getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth); 1974 const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1); 1975 return getAddExpr(SSExtD, SSExtR, 1976 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), 1977 Depth + 1); 1978 } 1979 } 1980 } 1981 // If the input value is a chrec scev, and we can prove that the value 1982 // did not overflow the old, smaller, value, we can sign extend all of the 1983 // operands (often constants). This allows analysis of something like 1984 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } 1985 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) 1986 if (AR->isAffine()) { 1987 const SCEV *Start = AR->getStart(); 1988 const SCEV *Step = AR->getStepRecurrence(*this); 1989 unsigned BitWidth = getTypeSizeInBits(AR->getType()); 1990 const Loop *L = AR->getLoop(); 1991 1992 if (!AR->hasNoSignedWrap()) { 1993 auto NewFlags = proveNoWrapViaConstantRanges(AR); 1994 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags); 1995 } 1996 1997 // If we have special knowledge that this addrec won't overflow, 1998 // we don't need to do any further analysis. 1999 if (AR->hasNoSignedWrap()) { 2000 Start = 2001 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1); 2002 Step = getSignExtendExpr(Step, Ty, Depth + 1); 2003 return getAddRecExpr(Start, Step, L, SCEV::FlagNSW); 2004 } 2005 2006 // Check whether the backedge-taken count is SCEVCouldNotCompute. 2007 // Note that this serves two purposes: It filters out loops that are 2008 // simply not analyzable, and it covers the case where this code is 2009 // being called from within backedge-taken count analysis, such that 2010 // attempting to ask for the backedge-taken count would likely result 2011 // in infinite recursion. In the later case, the analysis code will 2012 // cope with a conservative value, and it will take care to purge 2013 // that value once it has finished. 2014 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); 2015 if (!isa<SCEVCouldNotCompute>(MaxBECount)) { 2016 // Manually compute the final value for AR, checking for 2017 // overflow. 2018 2019 // Check whether the backedge-taken count can be losslessly casted to 2020 // the addrec's type. The count is always unsigned. 2021 const SCEV *CastedMaxBECount = 2022 getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth); 2023 const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend( 2024 CastedMaxBECount, MaxBECount->getType(), Depth); 2025 if (MaxBECount == RecastedMaxBECount) { 2026 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); 2027 // Check whether Start+Step*MaxBECount has no signed overflow. 2028 const SCEV *SMul = getMulExpr(CastedMaxBECount, Step, 2029 SCEV::FlagAnyWrap, Depth + 1); 2030 const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul, 2031 SCEV::FlagAnyWrap, 2032 Depth + 1), 2033 WideTy, Depth + 1); 2034 const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1); 2035 const SCEV *WideMaxBECount = 2036 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1); 2037 const SCEV *OperandExtendedAdd = 2038 getAddExpr(WideStart, 2039 getMulExpr(WideMaxBECount, 2040 getSignExtendExpr(Step, WideTy, Depth + 1), 2041 SCEV::FlagAnyWrap, Depth + 1), 2042 SCEV::FlagAnyWrap, Depth + 1); 2043 if (SAdd == OperandExtendedAdd) { 2044 // Cache knowledge of AR NSW, which is propagated to this AddRec. 2045 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNSW); 2046 // Return the expression with the addrec on the outside. 2047 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, 2048 Depth + 1); 2049 Step = getSignExtendExpr(Step, Ty, Depth + 1); 2050 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 2051 } 2052 // Similar to above, only this time treat the step value as unsigned. 2053 // This covers loops that count up with an unsigned step. 2054 OperandExtendedAdd = 2055 getAddExpr(WideStart, 2056 getMulExpr(WideMaxBECount, 2057 getZeroExtendExpr(Step, WideTy, Depth + 1), 2058 SCEV::FlagAnyWrap, Depth + 1), 2059 SCEV::FlagAnyWrap, Depth + 1); 2060 if (SAdd == OperandExtendedAdd) { 2061 // If AR wraps around then 2062 // 2063 // abs(Step) * MaxBECount > unsigned-max(AR->getType()) 2064 // => SAdd != OperandExtendedAdd 2065 // 2066 // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=> 2067 // (SAdd == OperandExtendedAdd => AR is NW) 2068 2069 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW); 2070 2071 // Return the expression with the addrec on the outside. 2072 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, 2073 Depth + 1); 2074 Step = getZeroExtendExpr(Step, Ty, Depth + 1); 2075 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 2076 } 2077 } 2078 } 2079 2080 auto NewFlags = proveNoSignedWrapViaInduction(AR); 2081 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags); 2082 if (AR->hasNoSignedWrap()) { 2083 // Same as nsw case above - duplicated here to avoid a compile time 2084 // issue. It's not clear that the order of checks does matter, but 2085 // it's one of two issue possible causes for a change which was 2086 // reverted. Be conservative for the moment. 2087 Start = 2088 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1); 2089 Step = getSignExtendExpr(Step, Ty, Depth + 1); 2090 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 2091 } 2092 2093 // sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step}))<nuw><nsw> 2094 // if D + (C - D + Step * n) could be proven to not signed wrap 2095 // where D maximizes the number of trailing zeros of (C - D + Step * n) 2096 if (const auto *SC = dyn_cast<SCEVConstant>(Start)) { 2097 const APInt &C = SC->getAPInt(); 2098 const APInt &D = extractConstantWithoutWrapping(*this, C, Step); 2099 if (D != 0) { 2100 const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth); 2101 const SCEV *SResidual = 2102 getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags()); 2103 const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1); 2104 return getAddExpr(SSExtD, SSExtR, 2105 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW), 2106 Depth + 1); 2107 } 2108 } 2109 2110 if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) { 2111 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNSW); 2112 Start = 2113 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1); 2114 Step = getSignExtendExpr(Step, Ty, Depth + 1); 2115 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags()); 2116 } 2117 } 2118 2119 // If the input value is provably positive and we could not simplify 2120 // away the sext build a zext instead. 2121 if (isKnownNonNegative(Op)) 2122 return getZeroExtendExpr(Op, Ty, Depth + 1); 2123 2124 // The cast wasn't folded; create an explicit cast node. 2125 // Recompute the insert position, as it may have been invalidated. 2126 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 2127 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), 2128 Op, Ty); 2129 UniqueSCEVs.InsertNode(S, IP); 2130 registerUser(S, { Op }); 2131 return S; 2132 } 2133 2134 const SCEV *ScalarEvolution::getCastExpr(SCEVTypes Kind, const SCEV *Op, 2135 Type *Ty) { 2136 switch (Kind) { 2137 case scTruncate: 2138 return getTruncateExpr(Op, Ty); 2139 case scZeroExtend: 2140 return getZeroExtendExpr(Op, Ty); 2141 case scSignExtend: 2142 return getSignExtendExpr(Op, Ty); 2143 case scPtrToInt: 2144 return getPtrToIntExpr(Op, Ty); 2145 default: 2146 llvm_unreachable("Not a SCEV cast expression!"); 2147 } 2148 } 2149 2150 /// getAnyExtendExpr - Return a SCEV for the given operand extended with 2151 /// unspecified bits out to the given type. 2152 const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, 2153 Type *Ty) { 2154 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 2155 "This is not an extending conversion!"); 2156 assert(isSCEVable(Ty) && 2157 "This is not a conversion to a SCEVable type!"); 2158 Ty = getEffectiveSCEVType(Ty); 2159 2160 // Sign-extend negative constants. 2161 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 2162 if (SC->getAPInt().isNegative()) 2163 return getSignExtendExpr(Op, Ty); 2164 2165 // Peel off a truncate cast. 2166 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { 2167 const SCEV *NewOp = T->getOperand(); 2168 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) 2169 return getAnyExtendExpr(NewOp, Ty); 2170 return getTruncateOrNoop(NewOp, Ty); 2171 } 2172 2173 // Next try a zext cast. If the cast is folded, use it. 2174 const SCEV *ZExt = getZeroExtendExpr(Op, Ty); 2175 if (!isa<SCEVZeroExtendExpr>(ZExt)) 2176 return ZExt; 2177 2178 // Next try a sext cast. If the cast is folded, use it. 2179 const SCEV *SExt = getSignExtendExpr(Op, Ty); 2180 if (!isa<SCEVSignExtendExpr>(SExt)) 2181 return SExt; 2182 2183 // Force the cast to be folded into the operands of an addrec. 2184 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) { 2185 SmallVector<const SCEV *, 4> Ops; 2186 for (const SCEV *Op : AR->operands()) 2187 Ops.push_back(getAnyExtendExpr(Op, Ty)); 2188 return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW); 2189 } 2190 2191 // If the expression is obviously signed, use the sext cast value. 2192 if (isa<SCEVSMaxExpr>(Op)) 2193 return SExt; 2194 2195 // Absent any other information, use the zext cast value. 2196 return ZExt; 2197 } 2198 2199 /// Process the given Ops list, which is a list of operands to be added under 2200 /// the given scale, update the given map. This is a helper function for 2201 /// getAddRecExpr. As an example of what it does, given a sequence of operands 2202 /// that would form an add expression like this: 2203 /// 2204 /// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r) 2205 /// 2206 /// where A and B are constants, update the map with these values: 2207 /// 2208 /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) 2209 /// 2210 /// and add 13 + A*B*29 to AccumulatedConstant. 2211 /// This will allow getAddRecExpr to produce this: 2212 /// 2213 /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) 2214 /// 2215 /// This form often exposes folding opportunities that are hidden in 2216 /// the original operand list. 2217 /// 2218 /// Return true iff it appears that any interesting folding opportunities 2219 /// may be exposed. This helps getAddRecExpr short-circuit extra work in 2220 /// the common case where no interesting opportunities are present, and 2221 /// is also used as a check to avoid infinite recursion. 2222 static bool 2223 CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, 2224 SmallVectorImpl<const SCEV *> &NewOps, 2225 APInt &AccumulatedConstant, 2226 const SCEV *const *Ops, size_t NumOperands, 2227 const APInt &Scale, 2228 ScalarEvolution &SE) { 2229 bool Interesting = false; 2230 2231 // Iterate over the add operands. They are sorted, with constants first. 2232 unsigned i = 0; 2233 while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { 2234 ++i; 2235 // Pull a buried constant out to the outside. 2236 if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero()) 2237 Interesting = true; 2238 AccumulatedConstant += Scale * C->getAPInt(); 2239 } 2240 2241 // Next comes everything else. We're especially interested in multiplies 2242 // here, but they're in the middle, so just visit the rest with one loop. 2243 for (; i != NumOperands; ++i) { 2244 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); 2245 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { 2246 APInt NewScale = 2247 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt(); 2248 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { 2249 // A multiplication of a constant with another add; recurse. 2250 const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1)); 2251 Interesting |= 2252 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, 2253 Add->op_begin(), Add->getNumOperands(), 2254 NewScale, SE); 2255 } else { 2256 // A multiplication of a constant with some other value. Update 2257 // the map. 2258 SmallVector<const SCEV *, 4> MulOps(drop_begin(Mul->operands())); 2259 const SCEV *Key = SE.getMulExpr(MulOps); 2260 auto Pair = M.insert({Key, NewScale}); 2261 if (Pair.second) { 2262 NewOps.push_back(Pair.first->first); 2263 } else { 2264 Pair.first->second += NewScale; 2265 // The map already had an entry for this value, which may indicate 2266 // a folding opportunity. 2267 Interesting = true; 2268 } 2269 } 2270 } else { 2271 // An ordinary operand. Update the map. 2272 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = 2273 M.insert({Ops[i], Scale}); 2274 if (Pair.second) { 2275 NewOps.push_back(Pair.first->first); 2276 } else { 2277 Pair.first->second += Scale; 2278 // The map already had an entry for this value, which may indicate 2279 // a folding opportunity. 2280 Interesting = true; 2281 } 2282 } 2283 } 2284 2285 return Interesting; 2286 } 2287 2288 bool ScalarEvolution::willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, 2289 const SCEV *LHS, const SCEV *RHS) { 2290 const SCEV *(ScalarEvolution::*Operation)(const SCEV *, const SCEV *, 2291 SCEV::NoWrapFlags, unsigned); 2292 switch (BinOp) { 2293 default: 2294 llvm_unreachable("Unsupported binary op"); 2295 case Instruction::Add: 2296 Operation = &ScalarEvolution::getAddExpr; 2297 break; 2298 case Instruction::Sub: 2299 Operation = &ScalarEvolution::getMinusSCEV; 2300 break; 2301 case Instruction::Mul: 2302 Operation = &ScalarEvolution::getMulExpr; 2303 break; 2304 } 2305 2306 const SCEV *(ScalarEvolution::*Extension)(const SCEV *, Type *, unsigned) = 2307 Signed ? &ScalarEvolution::getSignExtendExpr 2308 : &ScalarEvolution::getZeroExtendExpr; 2309 2310 // Check ext(LHS op RHS) == ext(LHS) op ext(RHS) 2311 auto *NarrowTy = cast<IntegerType>(LHS->getType()); 2312 auto *WideTy = 2313 IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2); 2314 2315 const SCEV *A = (this->*Extension)( 2316 (this->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0), WideTy, 0); 2317 const SCEV *LHSB = (this->*Extension)(LHS, WideTy, 0); 2318 const SCEV *RHSB = (this->*Extension)(RHS, WideTy, 0); 2319 const SCEV *B = (this->*Operation)(LHSB, RHSB, SCEV::FlagAnyWrap, 0); 2320 return A == B; 2321 } 2322 2323 std::pair<SCEV::NoWrapFlags, bool /*Deduced*/> 2324 ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp( 2325 const OverflowingBinaryOperator *OBO) { 2326 SCEV::NoWrapFlags Flags = SCEV::NoWrapFlags::FlagAnyWrap; 2327 2328 if (OBO->hasNoUnsignedWrap()) 2329 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 2330 if (OBO->hasNoSignedWrap()) 2331 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); 2332 2333 bool Deduced = false; 2334 2335 if (OBO->hasNoUnsignedWrap() && OBO->hasNoSignedWrap()) 2336 return {Flags, Deduced}; 2337 2338 if (OBO->getOpcode() != Instruction::Add && 2339 OBO->getOpcode() != Instruction::Sub && 2340 OBO->getOpcode() != Instruction::Mul) 2341 return {Flags, Deduced}; 2342 2343 const SCEV *LHS = getSCEV(OBO->getOperand(0)); 2344 const SCEV *RHS = getSCEV(OBO->getOperand(1)); 2345 2346 if (!OBO->hasNoUnsignedWrap() && 2347 willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(), 2348 /* Signed */ false, LHS, RHS)) { 2349 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 2350 Deduced = true; 2351 } 2352 2353 if (!OBO->hasNoSignedWrap() && 2354 willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(), 2355 /* Signed */ true, LHS, RHS)) { 2356 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); 2357 Deduced = true; 2358 } 2359 2360 return {Flags, Deduced}; 2361 } 2362 2363 // We're trying to construct a SCEV of type `Type' with `Ops' as operands and 2364 // `OldFlags' as can't-wrap behavior. Infer a more aggressive set of 2365 // can't-overflow flags for the operation if possible. 2366 static SCEV::NoWrapFlags 2367 StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type, 2368 const ArrayRef<const SCEV *> Ops, 2369 SCEV::NoWrapFlags Flags) { 2370 using namespace std::placeholders; 2371 2372 using OBO = OverflowingBinaryOperator; 2373 2374 bool CanAnalyze = 2375 Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr; 2376 (void)CanAnalyze; 2377 assert(CanAnalyze && "don't call from other places!"); 2378 2379 int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW; 2380 SCEV::NoWrapFlags SignOrUnsignWrap = 2381 ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); 2382 2383 // If FlagNSW is true and all the operands are non-negative, infer FlagNUW. 2384 auto IsKnownNonNegative = [&](const SCEV *S) { 2385 return SE->isKnownNonNegative(S); 2386 }; 2387 2388 if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative)) 2389 Flags = 2390 ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask); 2391 2392 SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); 2393 2394 if (SignOrUnsignWrap != SignOrUnsignMask && 2395 (Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 && 2396 isa<SCEVConstant>(Ops[0])) { 2397 2398 auto Opcode = [&] { 2399 switch (Type) { 2400 case scAddExpr: 2401 return Instruction::Add; 2402 case scMulExpr: 2403 return Instruction::Mul; 2404 default: 2405 llvm_unreachable("Unexpected SCEV op."); 2406 } 2407 }(); 2408 2409 const APInt &C = cast<SCEVConstant>(Ops[0])->getAPInt(); 2410 2411 // (A <opcode> C) --> (A <opcode> C)<nsw> if the op doesn't sign overflow. 2412 if (!(SignOrUnsignWrap & SCEV::FlagNSW)) { 2413 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 2414 Opcode, C, OBO::NoSignedWrap); 2415 if (NSWRegion.contains(SE->getSignedRange(Ops[1]))) 2416 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); 2417 } 2418 2419 // (A <opcode> C) --> (A <opcode> C)<nuw> if the op doesn't unsign overflow. 2420 if (!(SignOrUnsignWrap & SCEV::FlagNUW)) { 2421 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 2422 Opcode, C, OBO::NoUnsignedWrap); 2423 if (NUWRegion.contains(SE->getUnsignedRange(Ops[1]))) 2424 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 2425 } 2426 } 2427 2428 // <0,+,nonnegative><nw> is also nuw 2429 // TODO: Add corresponding nsw case 2430 if (Type == scAddRecExpr && ScalarEvolution::hasFlags(Flags, SCEV::FlagNW) && 2431 !ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && Ops.size() == 2 && 2432 Ops[0]->isZero() && IsKnownNonNegative(Ops[1])) 2433 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 2434 2435 // both (udiv X, Y) * Y and Y * (udiv X, Y) are always NUW 2436 if (Type == scMulExpr && !ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && 2437 Ops.size() == 2) { 2438 if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Ops[0])) 2439 if (UDiv->getOperand(1) == Ops[1]) 2440 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 2441 if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Ops[1])) 2442 if (UDiv->getOperand(1) == Ops[0]) 2443 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 2444 } 2445 2446 return Flags; 2447 } 2448 2449 bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) { 2450 return isLoopInvariant(S, L) && properlyDominates(S, L->getHeader()); 2451 } 2452 2453 /// Get a canonical add expression, or something simpler if possible. 2454 const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops, 2455 SCEV::NoWrapFlags OrigFlags, 2456 unsigned Depth) { 2457 assert(!(OrigFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) && 2458 "only nuw or nsw allowed"); 2459 assert(!Ops.empty() && "Cannot get empty add!"); 2460 if (Ops.size() == 1) return Ops[0]; 2461 #ifndef NDEBUG 2462 Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); 2463 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 2464 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && 2465 "SCEVAddExpr operand types don't match!"); 2466 unsigned NumPtrs = count_if( 2467 Ops, [](const SCEV *Op) { return Op->getType()->isPointerTy(); }); 2468 assert(NumPtrs <= 1 && "add has at most one pointer operand"); 2469 #endif 2470 2471 // Sort by complexity, this groups all similar expression types together. 2472 GroupByComplexity(Ops, &LI, DT); 2473 2474 // If there are any constants, fold them together. 2475 unsigned Idx = 0; 2476 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 2477 ++Idx; 2478 assert(Idx < Ops.size()); 2479 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 2480 // We found two constants, fold them together! 2481 Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt()); 2482 if (Ops.size() == 2) return Ops[0]; 2483 Ops.erase(Ops.begin()+1); // Erase the folded element 2484 LHSC = cast<SCEVConstant>(Ops[0]); 2485 } 2486 2487 // If we are left with a constant zero being added, strip it off. 2488 if (LHSC->getValue()->isZero()) { 2489 Ops.erase(Ops.begin()); 2490 --Idx; 2491 } 2492 2493 if (Ops.size() == 1) return Ops[0]; 2494 } 2495 2496 // Delay expensive flag strengthening until necessary. 2497 auto ComputeFlags = [this, OrigFlags](const ArrayRef<const SCEV *> Ops) { 2498 return StrengthenNoWrapFlags(this, scAddExpr, Ops, OrigFlags); 2499 }; 2500 2501 // Limit recursion calls depth. 2502 if (Depth > MaxArithDepth || hasHugeExpression(Ops)) 2503 return getOrCreateAddExpr(Ops, ComputeFlags(Ops)); 2504 2505 if (SCEV *S = findExistingSCEVInCache(scAddExpr, Ops)) { 2506 // Don't strengthen flags if we have no new information. 2507 SCEVAddExpr *Add = static_cast<SCEVAddExpr *>(S); 2508 if (Add->getNoWrapFlags(OrigFlags) != OrigFlags) 2509 Add->setNoWrapFlags(ComputeFlags(Ops)); 2510 return S; 2511 } 2512 2513 // Okay, check to see if the same value occurs in the operand list more than 2514 // once. If so, merge them together into an multiply expression. Since we 2515 // sorted the list, these values are required to be adjacent. 2516 Type *Ty = Ops[0]->getType(); 2517 bool FoundMatch = false; 2518 for (unsigned i = 0, e = Ops.size(); i != e-1; ++i) 2519 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 2520 // Scan ahead to count how many equal operands there are. 2521 unsigned Count = 2; 2522 while (i+Count != e && Ops[i+Count] == Ops[i]) 2523 ++Count; 2524 // Merge the values into a multiply. 2525 const SCEV *Scale = getConstant(Ty, Count); 2526 const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1); 2527 if (Ops.size() == Count) 2528 return Mul; 2529 Ops[i] = Mul; 2530 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count); 2531 --i; e -= Count - 1; 2532 FoundMatch = true; 2533 } 2534 if (FoundMatch) 2535 return getAddExpr(Ops, OrigFlags, Depth + 1); 2536 2537 // Check for truncates. If all the operands are truncated from the same 2538 // type, see if factoring out the truncate would permit the result to be 2539 // folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y) 2540 // if the contents of the resulting outer trunc fold to something simple. 2541 auto FindTruncSrcType = [&]() -> Type * { 2542 // We're ultimately looking to fold an addrec of truncs and muls of only 2543 // constants and truncs, so if we find any other types of SCEV 2544 // as operands of the addrec then we bail and return nullptr here. 2545 // Otherwise, we return the type of the operand of a trunc that we find. 2546 if (auto *T = dyn_cast<SCEVTruncateExpr>(Ops[Idx])) 2547 return T->getOperand()->getType(); 2548 if (const auto *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { 2549 const auto *LastOp = Mul->getOperand(Mul->getNumOperands() - 1); 2550 if (const auto *T = dyn_cast<SCEVTruncateExpr>(LastOp)) 2551 return T->getOperand()->getType(); 2552 } 2553 return nullptr; 2554 }; 2555 if (auto *SrcType = FindTruncSrcType()) { 2556 SmallVector<const SCEV *, 8> LargeOps; 2557 bool Ok = true; 2558 // Check all the operands to see if they can be represented in the 2559 // source type of the truncate. 2560 for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 2561 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { 2562 if (T->getOperand()->getType() != SrcType) { 2563 Ok = false; 2564 break; 2565 } 2566 LargeOps.push_back(T->getOperand()); 2567 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { 2568 LargeOps.push_back(getAnyExtendExpr(C, SrcType)); 2569 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { 2570 SmallVector<const SCEV *, 8> LargeMulOps; 2571 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { 2572 if (const SCEVTruncateExpr *T = 2573 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { 2574 if (T->getOperand()->getType() != SrcType) { 2575 Ok = false; 2576 break; 2577 } 2578 LargeMulOps.push_back(T->getOperand()); 2579 } else if (const auto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) { 2580 LargeMulOps.push_back(getAnyExtendExpr(C, SrcType)); 2581 } else { 2582 Ok = false; 2583 break; 2584 } 2585 } 2586 if (Ok) 2587 LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1)); 2588 } else { 2589 Ok = false; 2590 break; 2591 } 2592 } 2593 if (Ok) { 2594 // Evaluate the expression in the larger type. 2595 const SCEV *Fold = getAddExpr(LargeOps, SCEV::FlagAnyWrap, Depth + 1); 2596 // If it folds to something simple, use it. Otherwise, don't. 2597 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) 2598 return getTruncateExpr(Fold, Ty); 2599 } 2600 } 2601 2602 if (Ops.size() == 2) { 2603 // Check if we have an expression of the form ((X + C1) - C2), where C1 and 2604 // C2 can be folded in a way that allows retaining wrapping flags of (X + 2605 // C1). 2606 const SCEV *A = Ops[0]; 2607 const SCEV *B = Ops[1]; 2608 auto *AddExpr = dyn_cast<SCEVAddExpr>(B); 2609 auto *C = dyn_cast<SCEVConstant>(A); 2610 if (AddExpr && C && isa<SCEVConstant>(AddExpr->getOperand(0))) { 2611 auto C1 = cast<SCEVConstant>(AddExpr->getOperand(0))->getAPInt(); 2612 auto C2 = C->getAPInt(); 2613 SCEV::NoWrapFlags PreservedFlags = SCEV::FlagAnyWrap; 2614 2615 APInt ConstAdd = C1 + C2; 2616 auto AddFlags = AddExpr->getNoWrapFlags(); 2617 // Adding a smaller constant is NUW if the original AddExpr was NUW. 2618 if (ScalarEvolution::hasFlags(AddFlags, SCEV::FlagNUW) && 2619 ConstAdd.ule(C1)) { 2620 PreservedFlags = 2621 ScalarEvolution::setFlags(PreservedFlags, SCEV::FlagNUW); 2622 } 2623 2624 // Adding a constant with the same sign and small magnitude is NSW, if the 2625 // original AddExpr was NSW. 2626 if (ScalarEvolution::hasFlags(AddFlags, SCEV::FlagNSW) && 2627 C1.isSignBitSet() == ConstAdd.isSignBitSet() && 2628 ConstAdd.abs().ule(C1.abs())) { 2629 PreservedFlags = 2630 ScalarEvolution::setFlags(PreservedFlags, SCEV::FlagNSW); 2631 } 2632 2633 if (PreservedFlags != SCEV::FlagAnyWrap) { 2634 SmallVector<const SCEV *, 4> NewOps(AddExpr->operands()); 2635 NewOps[0] = getConstant(ConstAdd); 2636 return getAddExpr(NewOps, PreservedFlags); 2637 } 2638 } 2639 } 2640 2641 // Canonicalize (-1 * urem X, Y) + X --> (Y * X/Y) 2642 if (Ops.size() == 2) { 2643 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[0]); 2644 if (Mul && Mul->getNumOperands() == 2 && 2645 Mul->getOperand(0)->isAllOnesValue()) { 2646 const SCEV *X; 2647 const SCEV *Y; 2648 if (matchURem(Mul->getOperand(1), X, Y) && X == Ops[1]) { 2649 return getMulExpr(Y, getUDivExpr(X, Y)); 2650 } 2651 } 2652 } 2653 2654 // Skip past any other cast SCEVs. 2655 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) 2656 ++Idx; 2657 2658 // If there are add operands they would be next. 2659 if (Idx < Ops.size()) { 2660 bool DeletedAdd = false; 2661 // If the original flags and all inlined SCEVAddExprs are NUW, use the 2662 // common NUW flag for expression after inlining. Other flags cannot be 2663 // preserved, because they may depend on the original order of operations. 2664 SCEV::NoWrapFlags CommonFlags = maskFlags(OrigFlags, SCEV::FlagNUW); 2665 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { 2666 if (Ops.size() > AddOpsInlineThreshold || 2667 Add->getNumOperands() > AddOpsInlineThreshold) 2668 break; 2669 // If we have an add, expand the add operands onto the end of the operands 2670 // list. 2671 Ops.erase(Ops.begin()+Idx); 2672 Ops.append(Add->op_begin(), Add->op_end()); 2673 DeletedAdd = true; 2674 CommonFlags = maskFlags(CommonFlags, Add->getNoWrapFlags()); 2675 } 2676 2677 // If we deleted at least one add, we added operands to the end of the list, 2678 // and they are not necessarily sorted. Recurse to resort and resimplify 2679 // any operands we just acquired. 2680 if (DeletedAdd) 2681 return getAddExpr(Ops, CommonFlags, Depth + 1); 2682 } 2683 2684 // Skip over the add expression until we get to a multiply. 2685 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) 2686 ++Idx; 2687 2688 // Check to see if there are any folding opportunities present with 2689 // operands multiplied by constant values. 2690 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { 2691 uint64_t BitWidth = getTypeSizeInBits(Ty); 2692 DenseMap<const SCEV *, APInt> M; 2693 SmallVector<const SCEV *, 8> NewOps; 2694 APInt AccumulatedConstant(BitWidth, 0); 2695 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, 2696 Ops.data(), Ops.size(), 2697 APInt(BitWidth, 1), *this)) { 2698 struct APIntCompare { 2699 bool operator()(const APInt &LHS, const APInt &RHS) const { 2700 return LHS.ult(RHS); 2701 } 2702 }; 2703 2704 // Some interesting folding opportunity is present, so its worthwhile to 2705 // re-generate the operands list. Group the operands by constant scale, 2706 // to avoid multiplying by the same constant scale multiple times. 2707 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; 2708 for (const SCEV *NewOp : NewOps) 2709 MulOpLists[M.find(NewOp)->second].push_back(NewOp); 2710 // Re-generate the operands list. 2711 Ops.clear(); 2712 if (AccumulatedConstant != 0) 2713 Ops.push_back(getConstant(AccumulatedConstant)); 2714 for (auto &MulOp : MulOpLists) { 2715 if (MulOp.first == 1) { 2716 Ops.push_back(getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1)); 2717 } else if (MulOp.first != 0) { 2718 Ops.push_back(getMulExpr( 2719 getConstant(MulOp.first), 2720 getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1), 2721 SCEV::FlagAnyWrap, Depth + 1)); 2722 } 2723 } 2724 if (Ops.empty()) 2725 return getZero(Ty); 2726 if (Ops.size() == 1) 2727 return Ops[0]; 2728 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); 2729 } 2730 } 2731 2732 // If we are adding something to a multiply expression, make sure the 2733 // something is not already an operand of the multiply. If so, merge it into 2734 // the multiply. 2735 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { 2736 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); 2737 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { 2738 const SCEV *MulOpSCEV = Mul->getOperand(MulOp); 2739 if (isa<SCEVConstant>(MulOpSCEV)) 2740 continue; 2741 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) 2742 if (MulOpSCEV == Ops[AddOp]) { 2743 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) 2744 const SCEV *InnerMul = Mul->getOperand(MulOp == 0); 2745 if (Mul->getNumOperands() != 2) { 2746 // If the multiply has more than two operands, we must get the 2747 // Y*Z term. 2748 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), 2749 Mul->op_begin()+MulOp); 2750 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); 2751 InnerMul = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); 2752 } 2753 SmallVector<const SCEV *, 2> TwoOps = {getOne(Ty), InnerMul}; 2754 const SCEV *AddOne = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); 2755 const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV, 2756 SCEV::FlagAnyWrap, Depth + 1); 2757 if (Ops.size() == 2) return OuterMul; 2758 if (AddOp < Idx) { 2759 Ops.erase(Ops.begin()+AddOp); 2760 Ops.erase(Ops.begin()+Idx-1); 2761 } else { 2762 Ops.erase(Ops.begin()+Idx); 2763 Ops.erase(Ops.begin()+AddOp-1); 2764 } 2765 Ops.push_back(OuterMul); 2766 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); 2767 } 2768 2769 // Check this multiply against other multiplies being added together. 2770 for (unsigned OtherMulIdx = Idx+1; 2771 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); 2772 ++OtherMulIdx) { 2773 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); 2774 // If MulOp occurs in OtherMul, we can fold the two multiplies 2775 // together. 2776 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); 2777 OMulOp != e; ++OMulOp) 2778 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { 2779 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) 2780 const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); 2781 if (Mul->getNumOperands() != 2) { 2782 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), 2783 Mul->op_begin()+MulOp); 2784 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); 2785 InnerMul1 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); 2786 } 2787 const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); 2788 if (OtherMul->getNumOperands() != 2) { 2789 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), 2790 OtherMul->op_begin()+OMulOp); 2791 MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end()); 2792 InnerMul2 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1); 2793 } 2794 SmallVector<const SCEV *, 2> TwoOps = {InnerMul1, InnerMul2}; 2795 const SCEV *InnerMulSum = 2796 getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); 2797 const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum, 2798 SCEV::FlagAnyWrap, Depth + 1); 2799 if (Ops.size() == 2) return OuterMul; 2800 Ops.erase(Ops.begin()+Idx); 2801 Ops.erase(Ops.begin()+OtherMulIdx-1); 2802 Ops.push_back(OuterMul); 2803 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); 2804 } 2805 } 2806 } 2807 } 2808 2809 // If there are any add recurrences in the operands list, see if any other 2810 // added values are loop invariant. If so, we can fold them into the 2811 // recurrence. 2812 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) 2813 ++Idx; 2814 2815 // Scan over all recurrences, trying to fold loop invariants into them. 2816 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { 2817 // Scan all of the other operands to this add and add them to the vector if 2818 // they are loop invariant w.r.t. the recurrence. 2819 SmallVector<const SCEV *, 8> LIOps; 2820 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); 2821 const Loop *AddRecLoop = AddRec->getLoop(); 2822 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 2823 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) { 2824 LIOps.push_back(Ops[i]); 2825 Ops.erase(Ops.begin()+i); 2826 --i; --e; 2827 } 2828 2829 // If we found some loop invariants, fold them into the recurrence. 2830 if (!LIOps.empty()) { 2831 // Compute nowrap flags for the addition of the loop-invariant ops and 2832 // the addrec. Temporarily push it as an operand for that purpose. These 2833 // flags are valid in the scope of the addrec only. 2834 LIOps.push_back(AddRec); 2835 SCEV::NoWrapFlags Flags = ComputeFlags(LIOps); 2836 LIOps.pop_back(); 2837 2838 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} 2839 LIOps.push_back(AddRec->getStart()); 2840 2841 SmallVector<const SCEV *, 4> AddRecOps(AddRec->operands()); 2842 2843 // It is not in general safe to propagate flags valid on an add within 2844 // the addrec scope to one outside it. We must prove that the inner 2845 // scope is guaranteed to execute if the outer one does to be able to 2846 // safely propagate. We know the program is undefined if poison is 2847 // produced on the inner scoped addrec. We also know that *for this use* 2848 // the outer scoped add can't overflow (because of the flags we just 2849 // computed for the inner scoped add) without the program being undefined. 2850 // Proving that entry to the outer scope neccesitates entry to the inner 2851 // scope, thus proves the program undefined if the flags would be violated 2852 // in the outer scope. 2853 SCEV::NoWrapFlags AddFlags = Flags; 2854 if (AddFlags != SCEV::FlagAnyWrap) { 2855 auto *DefI = getDefiningScopeBound(LIOps); 2856 auto *ReachI = &*AddRecLoop->getHeader()->begin(); 2857 if (!isGuaranteedToTransferExecutionTo(DefI, ReachI)) 2858 AddFlags = SCEV::FlagAnyWrap; 2859 } 2860 AddRecOps[0] = getAddExpr(LIOps, AddFlags, Depth + 1); 2861 2862 // Build the new addrec. Propagate the NUW and NSW flags if both the 2863 // outer add and the inner addrec are guaranteed to have no overflow. 2864 // Always propagate NW. 2865 Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW)); 2866 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags); 2867 2868 // If all of the other operands were loop invariant, we are done. 2869 if (Ops.size() == 1) return NewRec; 2870 2871 // Otherwise, add the folded AddRec by the non-invariant parts. 2872 for (unsigned i = 0;; ++i) 2873 if (Ops[i] == AddRec) { 2874 Ops[i] = NewRec; 2875 break; 2876 } 2877 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); 2878 } 2879 2880 // Okay, if there weren't any loop invariants to be folded, check to see if 2881 // there are multiple AddRec's with the same loop induction variable being 2882 // added together. If so, we can fold them. 2883 for (unsigned OtherIdx = Idx+1; 2884 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); 2885 ++OtherIdx) { 2886 // We expect the AddRecExpr's to be sorted in reverse dominance order, 2887 // so that the 1st found AddRecExpr is dominated by all others. 2888 assert(DT.dominates( 2889 cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(), 2890 AddRec->getLoop()->getHeader()) && 2891 "AddRecExprs are not sorted in reverse dominance order?"); 2892 if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) { 2893 // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L> 2894 SmallVector<const SCEV *, 4> AddRecOps(AddRec->operands()); 2895 for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); 2896 ++OtherIdx) { 2897 const auto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); 2898 if (OtherAddRec->getLoop() == AddRecLoop) { 2899 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); 2900 i != e; ++i) { 2901 if (i >= AddRecOps.size()) { 2902 AddRecOps.append(OtherAddRec->op_begin()+i, 2903 OtherAddRec->op_end()); 2904 break; 2905 } 2906 SmallVector<const SCEV *, 2> TwoOps = { 2907 AddRecOps[i], OtherAddRec->getOperand(i)}; 2908 AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1); 2909 } 2910 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; 2911 } 2912 } 2913 // Step size has changed, so we cannot guarantee no self-wraparound. 2914 Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap); 2915 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); 2916 } 2917 } 2918 2919 // Otherwise couldn't fold anything into this recurrence. Move onto the 2920 // next one. 2921 } 2922 2923 // Okay, it looks like we really DO need an add expr. Check to see if we 2924 // already have one, otherwise create a new one. 2925 return getOrCreateAddExpr(Ops, ComputeFlags(Ops)); 2926 } 2927 2928 const SCEV * 2929 ScalarEvolution::getOrCreateAddExpr(ArrayRef<const SCEV *> Ops, 2930 SCEV::NoWrapFlags Flags) { 2931 FoldingSetNodeID ID; 2932 ID.AddInteger(scAddExpr); 2933 for (const SCEV *Op : Ops) 2934 ID.AddPointer(Op); 2935 void *IP = nullptr; 2936 SCEVAddExpr *S = 2937 static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); 2938 if (!S) { 2939 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 2940 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 2941 S = new (SCEVAllocator) 2942 SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size()); 2943 UniqueSCEVs.InsertNode(S, IP); 2944 registerUser(S, Ops); 2945 } 2946 S->setNoWrapFlags(Flags); 2947 return S; 2948 } 2949 2950 const SCEV * 2951 ScalarEvolution::getOrCreateAddRecExpr(ArrayRef<const SCEV *> Ops, 2952 const Loop *L, SCEV::NoWrapFlags Flags) { 2953 FoldingSetNodeID ID; 2954 ID.AddInteger(scAddRecExpr); 2955 for (const SCEV *Op : Ops) 2956 ID.AddPointer(Op); 2957 ID.AddPointer(L); 2958 void *IP = nullptr; 2959 SCEVAddRecExpr *S = 2960 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); 2961 if (!S) { 2962 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 2963 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 2964 S = new (SCEVAllocator) 2965 SCEVAddRecExpr(ID.Intern(SCEVAllocator), O, Ops.size(), L); 2966 UniqueSCEVs.InsertNode(S, IP); 2967 LoopUsers[L].push_back(S); 2968 registerUser(S, Ops); 2969 } 2970 setNoWrapFlags(S, Flags); 2971 return S; 2972 } 2973 2974 const SCEV * 2975 ScalarEvolution::getOrCreateMulExpr(ArrayRef<const SCEV *> Ops, 2976 SCEV::NoWrapFlags Flags) { 2977 FoldingSetNodeID ID; 2978 ID.AddInteger(scMulExpr); 2979 for (const SCEV *Op : Ops) 2980 ID.AddPointer(Op); 2981 void *IP = nullptr; 2982 SCEVMulExpr *S = 2983 static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); 2984 if (!S) { 2985 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 2986 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 2987 S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator), 2988 O, Ops.size()); 2989 UniqueSCEVs.InsertNode(S, IP); 2990 registerUser(S, Ops); 2991 } 2992 S->setNoWrapFlags(Flags); 2993 return S; 2994 } 2995 2996 static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) { 2997 uint64_t k = i*j; 2998 if (j > 1 && k / j != i) Overflow = true; 2999 return k; 3000 } 3001 3002 /// Compute the result of "n choose k", the binomial coefficient. If an 3003 /// intermediate computation overflows, Overflow will be set and the return will 3004 /// be garbage. Overflow is not cleared on absence of overflow. 3005 static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) { 3006 // We use the multiplicative formula: 3007 // n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 . 3008 // At each iteration, we take the n-th term of the numeral and divide by the 3009 // (k-n)th term of the denominator. This division will always produce an 3010 // integral result, and helps reduce the chance of overflow in the 3011 // intermediate computations. However, we can still overflow even when the 3012 // final result would fit. 3013 3014 if (n == 0 || n == k) return 1; 3015 if (k > n) return 0; 3016 3017 if (k > n/2) 3018 k = n-k; 3019 3020 uint64_t r = 1; 3021 for (uint64_t i = 1; i <= k; ++i) { 3022 r = umul_ov(r, n-(i-1), Overflow); 3023 r /= i; 3024 } 3025 return r; 3026 } 3027 3028 /// Determine if any of the operands in this SCEV are a constant or if 3029 /// any of the add or multiply expressions in this SCEV contain a constant. 3030 static bool containsConstantInAddMulChain(const SCEV *StartExpr) { 3031 struct FindConstantInAddMulChain { 3032 bool FoundConstant = false; 3033 3034 bool follow(const SCEV *S) { 3035 FoundConstant |= isa<SCEVConstant>(S); 3036 return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S); 3037 } 3038 3039 bool isDone() const { 3040 return FoundConstant; 3041 } 3042 }; 3043 3044 FindConstantInAddMulChain F; 3045 SCEVTraversal<FindConstantInAddMulChain> ST(F); 3046 ST.visitAll(StartExpr); 3047 return F.FoundConstant; 3048 } 3049 3050 /// Get a canonical multiply expression, or something simpler if possible. 3051 const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, 3052 SCEV::NoWrapFlags OrigFlags, 3053 unsigned Depth) { 3054 assert(OrigFlags == maskFlags(OrigFlags, SCEV::FlagNUW | SCEV::FlagNSW) && 3055 "only nuw or nsw allowed"); 3056 assert(!Ops.empty() && "Cannot get empty mul!"); 3057 if (Ops.size() == 1) return Ops[0]; 3058 #ifndef NDEBUG 3059 Type *ETy = Ops[0]->getType(); 3060 assert(!ETy->isPointerTy()); 3061 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 3062 assert(Ops[i]->getType() == ETy && 3063 "SCEVMulExpr operand types don't match!"); 3064 #endif 3065 3066 // Sort by complexity, this groups all similar expression types together. 3067 GroupByComplexity(Ops, &LI, DT); 3068 3069 // If there are any constants, fold them together. 3070 unsigned Idx = 0; 3071 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 3072 ++Idx; 3073 assert(Idx < Ops.size()); 3074 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 3075 // We found two constants, fold them together! 3076 Ops[0] = getConstant(LHSC->getAPInt() * RHSC->getAPInt()); 3077 if (Ops.size() == 2) return Ops[0]; 3078 Ops.erase(Ops.begin()+1); // Erase the folded element 3079 LHSC = cast<SCEVConstant>(Ops[0]); 3080 } 3081 3082 // If we have a multiply of zero, it will always be zero. 3083 if (LHSC->getValue()->isZero()) 3084 return LHSC; 3085 3086 // If we are left with a constant one being multiplied, strip it off. 3087 if (LHSC->getValue()->isOne()) { 3088 Ops.erase(Ops.begin()); 3089 --Idx; 3090 } 3091 3092 if (Ops.size() == 1) 3093 return Ops[0]; 3094 } 3095 3096 // Delay expensive flag strengthening until necessary. 3097 auto ComputeFlags = [this, OrigFlags](const ArrayRef<const SCEV *> Ops) { 3098 return StrengthenNoWrapFlags(this, scMulExpr, Ops, OrigFlags); 3099 }; 3100 3101 // Limit recursion calls depth. 3102 if (Depth > MaxArithDepth || hasHugeExpression(Ops)) 3103 return getOrCreateMulExpr(Ops, ComputeFlags(Ops)); 3104 3105 if (SCEV *S = findExistingSCEVInCache(scMulExpr, Ops)) { 3106 // Don't strengthen flags if we have no new information. 3107 SCEVMulExpr *Mul = static_cast<SCEVMulExpr *>(S); 3108 if (Mul->getNoWrapFlags(OrigFlags) != OrigFlags) 3109 Mul->setNoWrapFlags(ComputeFlags(Ops)); 3110 return S; 3111 } 3112 3113 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 3114 if (Ops.size() == 2) { 3115 // C1*(C2+V) -> C1*C2 + C1*V 3116 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) 3117 // If any of Add's ops are Adds or Muls with a constant, apply this 3118 // transformation as well. 3119 // 3120 // TODO: There are some cases where this transformation is not 3121 // profitable; for example, Add = (C0 + X) * Y + Z. Maybe the scope of 3122 // this transformation should be narrowed down. 3123 if (Add->getNumOperands() == 2 && containsConstantInAddMulChain(Add)) { 3124 const SCEV *LHS = getMulExpr(LHSC, Add->getOperand(0), 3125 SCEV::FlagAnyWrap, Depth + 1); 3126 const SCEV *RHS = getMulExpr(LHSC, Add->getOperand(1), 3127 SCEV::FlagAnyWrap, Depth + 1); 3128 return getAddExpr(LHS, RHS, SCEV::FlagAnyWrap, Depth + 1); 3129 } 3130 3131 if (Ops[0]->isAllOnesValue()) { 3132 // If we have a mul by -1 of an add, try distributing the -1 among the 3133 // add operands. 3134 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) { 3135 SmallVector<const SCEV *, 4> NewOps; 3136 bool AnyFolded = false; 3137 for (const SCEV *AddOp : Add->operands()) { 3138 const SCEV *Mul = getMulExpr(Ops[0], AddOp, SCEV::FlagAnyWrap, 3139 Depth + 1); 3140 if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true; 3141 NewOps.push_back(Mul); 3142 } 3143 if (AnyFolded) 3144 return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1); 3145 } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) { 3146 // Negation preserves a recurrence's no self-wrap property. 3147 SmallVector<const SCEV *, 4> Operands; 3148 for (const SCEV *AddRecOp : AddRec->operands()) 3149 Operands.push_back(getMulExpr(Ops[0], AddRecOp, SCEV::FlagAnyWrap, 3150 Depth + 1)); 3151 3152 return getAddRecExpr(Operands, AddRec->getLoop(), 3153 AddRec->getNoWrapFlags(SCEV::FlagNW)); 3154 } 3155 } 3156 } 3157 } 3158 3159 // Skip over the add expression until we get to a multiply. 3160 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) 3161 ++Idx; 3162 3163 // If there are mul operands inline them all into this expression. 3164 if (Idx < Ops.size()) { 3165 bool DeletedMul = false; 3166 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { 3167 if (Ops.size() > MulOpsInlineThreshold) 3168 break; 3169 // If we have an mul, expand the mul operands onto the end of the 3170 // operands list. 3171 Ops.erase(Ops.begin()+Idx); 3172 Ops.append(Mul->op_begin(), Mul->op_end()); 3173 DeletedMul = true; 3174 } 3175 3176 // If we deleted at least one mul, we added operands to the end of the 3177 // list, and they are not necessarily sorted. Recurse to resort and 3178 // resimplify any operands we just acquired. 3179 if (DeletedMul) 3180 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); 3181 } 3182 3183 // If there are any add recurrences in the operands list, see if any other 3184 // added values are loop invariant. If so, we can fold them into the 3185 // recurrence. 3186 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) 3187 ++Idx; 3188 3189 // Scan over all recurrences, trying to fold loop invariants into them. 3190 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { 3191 // Scan all of the other operands to this mul and add them to the vector 3192 // if they are loop invariant w.r.t. the recurrence. 3193 SmallVector<const SCEV *, 8> LIOps; 3194 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); 3195 const Loop *AddRecLoop = AddRec->getLoop(); 3196 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 3197 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) { 3198 LIOps.push_back(Ops[i]); 3199 Ops.erase(Ops.begin()+i); 3200 --i; --e; 3201 } 3202 3203 // If we found some loop invariants, fold them into the recurrence. 3204 if (!LIOps.empty()) { 3205 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} 3206 SmallVector<const SCEV *, 4> NewOps; 3207 NewOps.reserve(AddRec->getNumOperands()); 3208 const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1); 3209 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) 3210 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i), 3211 SCEV::FlagAnyWrap, Depth + 1)); 3212 3213 // Build the new addrec. Propagate the NUW and NSW flags if both the 3214 // outer mul and the inner addrec are guaranteed to have no overflow. 3215 // 3216 // No self-wrap cannot be guaranteed after changing the step size, but 3217 // will be inferred if either NUW or NSW is true. 3218 SCEV::NoWrapFlags Flags = ComputeFlags({Scale, AddRec}); 3219 const SCEV *NewRec = getAddRecExpr( 3220 NewOps, AddRecLoop, AddRec->getNoWrapFlags(Flags)); 3221 3222 // If all of the other operands were loop invariant, we are done. 3223 if (Ops.size() == 1) return NewRec; 3224 3225 // Otherwise, multiply the folded AddRec by the non-invariant parts. 3226 for (unsigned i = 0;; ++i) 3227 if (Ops[i] == AddRec) { 3228 Ops[i] = NewRec; 3229 break; 3230 } 3231 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); 3232 } 3233 3234 // Okay, if there weren't any loop invariants to be folded, check to see 3235 // if there are multiple AddRec's with the same loop induction variable 3236 // being multiplied together. If so, we can fold them. 3237 3238 // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L> 3239 // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [ 3240 // choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z 3241 // ]]],+,...up to x=2n}. 3242 // Note that the arguments to choose() are always integers with values 3243 // known at compile time, never SCEV objects. 3244 // 3245 // The implementation avoids pointless extra computations when the two 3246 // addrec's are of different length (mathematically, it's equivalent to 3247 // an infinite stream of zeros on the right). 3248 bool OpsModified = false; 3249 for (unsigned OtherIdx = Idx+1; 3250 OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); 3251 ++OtherIdx) { 3252 const SCEVAddRecExpr *OtherAddRec = 3253 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]); 3254 if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop) 3255 continue; 3256 3257 // Limit max number of arguments to avoid creation of unreasonably big 3258 // SCEVAddRecs with very complex operands. 3259 if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 > 3260 MaxAddRecSize || hasHugeExpression({AddRec, OtherAddRec})) 3261 continue; 3262 3263 bool Overflow = false; 3264 Type *Ty = AddRec->getType(); 3265 bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64; 3266 SmallVector<const SCEV*, 7> AddRecOps; 3267 for (int x = 0, xe = AddRec->getNumOperands() + 3268 OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) { 3269 SmallVector <const SCEV *, 7> SumOps; 3270 for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) { 3271 uint64_t Coeff1 = Choose(x, 2*x - y, Overflow); 3272 for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1), 3273 ze = std::min(x+1, (int)OtherAddRec->getNumOperands()); 3274 z < ze && !Overflow; ++z) { 3275 uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow); 3276 uint64_t Coeff; 3277 if (LargerThan64Bits) 3278 Coeff = umul_ov(Coeff1, Coeff2, Overflow); 3279 else 3280 Coeff = Coeff1*Coeff2; 3281 const SCEV *CoeffTerm = getConstant(Ty, Coeff); 3282 const SCEV *Term1 = AddRec->getOperand(y-z); 3283 const SCEV *Term2 = OtherAddRec->getOperand(z); 3284 SumOps.push_back(getMulExpr(CoeffTerm, Term1, Term2, 3285 SCEV::FlagAnyWrap, Depth + 1)); 3286 } 3287 } 3288 if (SumOps.empty()) 3289 SumOps.push_back(getZero(Ty)); 3290 AddRecOps.push_back(getAddExpr(SumOps, SCEV::FlagAnyWrap, Depth + 1)); 3291 } 3292 if (!Overflow) { 3293 const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRecLoop, 3294 SCEV::FlagAnyWrap); 3295 if (Ops.size() == 2) return NewAddRec; 3296 Ops[Idx] = NewAddRec; 3297 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; 3298 OpsModified = true; 3299 AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec); 3300 if (!AddRec) 3301 break; 3302 } 3303 } 3304 if (OpsModified) 3305 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1); 3306 3307 // Otherwise couldn't fold anything into this recurrence. Move onto the 3308 // next one. 3309 } 3310 3311 // Okay, it looks like we really DO need an mul expr. Check to see if we 3312 // already have one, otherwise create a new one. 3313 return getOrCreateMulExpr(Ops, ComputeFlags(Ops)); 3314 } 3315 3316 /// Represents an unsigned remainder expression based on unsigned division. 3317 const SCEV *ScalarEvolution::getURemExpr(const SCEV *LHS, 3318 const SCEV *RHS) { 3319 assert(getEffectiveSCEVType(LHS->getType()) == 3320 getEffectiveSCEVType(RHS->getType()) && 3321 "SCEVURemExpr operand types don't match!"); 3322 3323 // Short-circuit easy cases 3324 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { 3325 // If constant is one, the result is trivial 3326 if (RHSC->getValue()->isOne()) 3327 return getZero(LHS->getType()); // X urem 1 --> 0 3328 3329 // If constant is a power of two, fold into a zext(trunc(LHS)). 3330 if (RHSC->getAPInt().isPowerOf2()) { 3331 Type *FullTy = LHS->getType(); 3332 Type *TruncTy = 3333 IntegerType::get(getContext(), RHSC->getAPInt().logBase2()); 3334 return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy); 3335 } 3336 } 3337 3338 // Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y) 3339 const SCEV *UDiv = getUDivExpr(LHS, RHS); 3340 const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW); 3341 return getMinusSCEV(LHS, Mult, SCEV::FlagNUW); 3342 } 3343 3344 /// Get a canonical unsigned division expression, or something simpler if 3345 /// possible. 3346 const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, 3347 const SCEV *RHS) { 3348 assert(!LHS->getType()->isPointerTy() && 3349 "SCEVUDivExpr operand can't be pointer!"); 3350 assert(LHS->getType() == RHS->getType() && 3351 "SCEVUDivExpr operand types don't match!"); 3352 3353 FoldingSetNodeID ID; 3354 ID.AddInteger(scUDivExpr); 3355 ID.AddPointer(LHS); 3356 ID.AddPointer(RHS); 3357 void *IP = nullptr; 3358 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) 3359 return S; 3360 3361 // 0 udiv Y == 0 3362 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) 3363 if (LHSC->getValue()->isZero()) 3364 return LHS; 3365 3366 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { 3367 if (RHSC->getValue()->isOne()) 3368 return LHS; // X udiv 1 --> x 3369 // If the denominator is zero, the result of the udiv is undefined. Don't 3370 // try to analyze it, because the resolution chosen here may differ from 3371 // the resolution chosen in other parts of the compiler. 3372 if (!RHSC->getValue()->isZero()) { 3373 // Determine if the division can be folded into the operands of 3374 // its operands. 3375 // TODO: Generalize this to non-constants by using known-bits information. 3376 Type *Ty = LHS->getType(); 3377 unsigned LZ = RHSC->getAPInt().countLeadingZeros(); 3378 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1; 3379 // For non-power-of-two values, effectively round the value up to the 3380 // nearest power of two. 3381 if (!RHSC->getAPInt().isPowerOf2()) 3382 ++MaxShiftAmt; 3383 IntegerType *ExtTy = 3384 IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); 3385 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) 3386 if (const SCEVConstant *Step = 3387 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) { 3388 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. 3389 const APInt &StepInt = Step->getAPInt(); 3390 const APInt &DivInt = RHSC->getAPInt(); 3391 if (!StepInt.urem(DivInt) && 3392 getZeroExtendExpr(AR, ExtTy) == 3393 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), 3394 getZeroExtendExpr(Step, ExtTy), 3395 AR->getLoop(), SCEV::FlagAnyWrap)) { 3396 SmallVector<const SCEV *, 4> Operands; 3397 for (const SCEV *Op : AR->operands()) 3398 Operands.push_back(getUDivExpr(Op, RHS)); 3399 return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW); 3400 } 3401 /// Get a canonical UDivExpr for a recurrence. 3402 /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0. 3403 // We can currently only fold X%N if X is constant. 3404 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart()); 3405 if (StartC && !DivInt.urem(StepInt) && 3406 getZeroExtendExpr(AR, ExtTy) == 3407 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), 3408 getZeroExtendExpr(Step, ExtTy), 3409 AR->getLoop(), SCEV::FlagAnyWrap)) { 3410 const APInt &StartInt = StartC->getAPInt(); 3411 const APInt &StartRem = StartInt.urem(StepInt); 3412 if (StartRem != 0) { 3413 const SCEV *NewLHS = 3414 getAddRecExpr(getConstant(StartInt - StartRem), Step, 3415 AR->getLoop(), SCEV::FlagNW); 3416 if (LHS != NewLHS) { 3417 LHS = NewLHS; 3418 3419 // Reset the ID to include the new LHS, and check if it is 3420 // already cached. 3421 ID.clear(); 3422 ID.AddInteger(scUDivExpr); 3423 ID.AddPointer(LHS); 3424 ID.AddPointer(RHS); 3425 IP = nullptr; 3426 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) 3427 return S; 3428 } 3429 } 3430 } 3431 } 3432 // (A*B)/C --> A*(B/C) if safe and B/C can be folded. 3433 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { 3434 SmallVector<const SCEV *, 4> Operands; 3435 for (const SCEV *Op : M->operands()) 3436 Operands.push_back(getZeroExtendExpr(Op, ExtTy)); 3437 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) 3438 // Find an operand that's safely divisible. 3439 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { 3440 const SCEV *Op = M->getOperand(i); 3441 const SCEV *Div = getUDivExpr(Op, RHSC); 3442 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { 3443 Operands = SmallVector<const SCEV *, 4>(M->operands()); 3444 Operands[i] = Div; 3445 return getMulExpr(Operands); 3446 } 3447 } 3448 } 3449 3450 // (A/B)/C --> A/(B*C) if safe and B*C can be folded. 3451 if (const SCEVUDivExpr *OtherDiv = dyn_cast<SCEVUDivExpr>(LHS)) { 3452 if (auto *DivisorConstant = 3453 dyn_cast<SCEVConstant>(OtherDiv->getRHS())) { 3454 bool Overflow = false; 3455 APInt NewRHS = 3456 DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow); 3457 if (Overflow) { 3458 return getConstant(RHSC->getType(), 0, false); 3459 } 3460 return getUDivExpr(OtherDiv->getLHS(), getConstant(NewRHS)); 3461 } 3462 } 3463 3464 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. 3465 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) { 3466 SmallVector<const SCEV *, 4> Operands; 3467 for (const SCEV *Op : A->operands()) 3468 Operands.push_back(getZeroExtendExpr(Op, ExtTy)); 3469 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { 3470 Operands.clear(); 3471 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { 3472 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); 3473 if (isa<SCEVUDivExpr>(Op) || 3474 getMulExpr(Op, RHS) != A->getOperand(i)) 3475 break; 3476 Operands.push_back(Op); 3477 } 3478 if (Operands.size() == A->getNumOperands()) 3479 return getAddExpr(Operands); 3480 } 3481 } 3482 3483 // Fold if both operands are constant. 3484 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { 3485 Constant *LHSCV = LHSC->getValue(); 3486 Constant *RHSCV = RHSC->getValue(); 3487 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, 3488 RHSCV))); 3489 } 3490 } 3491 } 3492 3493 // The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs 3494 // changes). Make sure we get a new one. 3495 IP = nullptr; 3496 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 3497 SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator), 3498 LHS, RHS); 3499 UniqueSCEVs.InsertNode(S, IP); 3500 registerUser(S, {LHS, RHS}); 3501 return S; 3502 } 3503 3504 APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) { 3505 APInt A = C1->getAPInt().abs(); 3506 APInt B = C2->getAPInt().abs(); 3507 uint32_t ABW = A.getBitWidth(); 3508 uint32_t BBW = B.getBitWidth(); 3509 3510 if (ABW > BBW) 3511 B = B.zext(ABW); 3512 else if (ABW < BBW) 3513 A = A.zext(BBW); 3514 3515 return APIntOps::GreatestCommonDivisor(std::move(A), std::move(B)); 3516 } 3517 3518 /// Get a canonical unsigned division expression, or something simpler if 3519 /// possible. There is no representation for an exact udiv in SCEV IR, but we 3520 /// can attempt to remove factors from the LHS and RHS. We can't do this when 3521 /// it's not exact because the udiv may be clearing bits. 3522 const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS, 3523 const SCEV *RHS) { 3524 // TODO: we could try to find factors in all sorts of things, but for now we 3525 // just deal with u/exact (multiply, constant). See SCEVDivision towards the 3526 // end of this file for inspiration. 3527 3528 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS); 3529 if (!Mul || !Mul->hasNoUnsignedWrap()) 3530 return getUDivExpr(LHS, RHS); 3531 3532 if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) { 3533 // If the mulexpr multiplies by a constant, then that constant must be the 3534 // first element of the mulexpr. 3535 if (const auto *LHSCst = dyn_cast<SCEVConstant>(Mul->getOperand(0))) { 3536 if (LHSCst == RHSCst) { 3537 SmallVector<const SCEV *, 2> Operands(drop_begin(Mul->operands())); 3538 return getMulExpr(Operands); 3539 } 3540 3541 // We can't just assume that LHSCst divides RHSCst cleanly, it could be 3542 // that there's a factor provided by one of the other terms. We need to 3543 // check. 3544 APInt Factor = gcd(LHSCst, RHSCst); 3545 if (!Factor.isIntN(1)) { 3546 LHSCst = 3547 cast<SCEVConstant>(getConstant(LHSCst->getAPInt().udiv(Factor))); 3548 RHSCst = 3549 cast<SCEVConstant>(getConstant(RHSCst->getAPInt().udiv(Factor))); 3550 SmallVector<const SCEV *, 2> Operands; 3551 Operands.push_back(LHSCst); 3552 Operands.append(Mul->op_begin() + 1, Mul->op_end()); 3553 LHS = getMulExpr(Operands); 3554 RHS = RHSCst; 3555 Mul = dyn_cast<SCEVMulExpr>(LHS); 3556 if (!Mul) 3557 return getUDivExactExpr(LHS, RHS); 3558 } 3559 } 3560 } 3561 3562 for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) { 3563 if (Mul->getOperand(i) == RHS) { 3564 SmallVector<const SCEV *, 2> Operands; 3565 Operands.append(Mul->op_begin(), Mul->op_begin() + i); 3566 Operands.append(Mul->op_begin() + i + 1, Mul->op_end()); 3567 return getMulExpr(Operands); 3568 } 3569 } 3570 3571 return getUDivExpr(LHS, RHS); 3572 } 3573 3574 /// Get an add recurrence expression for the specified loop. Simplify the 3575 /// expression as much as possible. 3576 const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step, 3577 const Loop *L, 3578 SCEV::NoWrapFlags Flags) { 3579 SmallVector<const SCEV *, 4> Operands; 3580 Operands.push_back(Start); 3581 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) 3582 if (StepChrec->getLoop() == L) { 3583 Operands.append(StepChrec->op_begin(), StepChrec->op_end()); 3584 return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW)); 3585 } 3586 3587 Operands.push_back(Step); 3588 return getAddRecExpr(Operands, L, Flags); 3589 } 3590 3591 /// Get an add recurrence expression for the specified loop. Simplify the 3592 /// expression as much as possible. 3593 const SCEV * 3594 ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, 3595 const Loop *L, SCEV::NoWrapFlags Flags) { 3596 if (Operands.size() == 1) return Operands[0]; 3597 #ifndef NDEBUG 3598 Type *ETy = getEffectiveSCEVType(Operands[0]->getType()); 3599 for (unsigned i = 1, e = Operands.size(); i != e; ++i) { 3600 assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy && 3601 "SCEVAddRecExpr operand types don't match!"); 3602 assert(!Operands[i]->getType()->isPointerTy() && "Step must be integer"); 3603 } 3604 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 3605 assert(isLoopInvariant(Operands[i], L) && 3606 "SCEVAddRecExpr operand is not loop-invariant!"); 3607 #endif 3608 3609 if (Operands.back()->isZero()) { 3610 Operands.pop_back(); 3611 return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X 3612 } 3613 3614 // It's tempting to want to call getConstantMaxBackedgeTakenCount count here and 3615 // use that information to infer NUW and NSW flags. However, computing a 3616 // BE count requires calling getAddRecExpr, so we may not yet have a 3617 // meaningful BE count at this point (and if we don't, we'd be stuck 3618 // with a SCEVCouldNotCompute as the cached BE count). 3619 3620 Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags); 3621 3622 // Canonicalize nested AddRecs in by nesting them in order of loop depth. 3623 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { 3624 const Loop *NestedLoop = NestedAR->getLoop(); 3625 if (L->contains(NestedLoop) 3626 ? (L->getLoopDepth() < NestedLoop->getLoopDepth()) 3627 : (!NestedLoop->contains(L) && 3628 DT.dominates(L->getHeader(), NestedLoop->getHeader()))) { 3629 SmallVector<const SCEV *, 4> NestedOperands(NestedAR->operands()); 3630 Operands[0] = NestedAR->getStart(); 3631 // AddRecs require their operands be loop-invariant with respect to their 3632 // loops. Don't perform this transformation if it would break this 3633 // requirement. 3634 bool AllInvariant = all_of( 3635 Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); }); 3636 3637 if (AllInvariant) { 3638 // Create a recurrence for the outer loop with the same step size. 3639 // 3640 // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the 3641 // inner recurrence has the same property. 3642 SCEV::NoWrapFlags OuterFlags = 3643 maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags()); 3644 3645 NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags); 3646 AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) { 3647 return isLoopInvariant(Op, NestedLoop); 3648 }); 3649 3650 if (AllInvariant) { 3651 // Ok, both add recurrences are valid after the transformation. 3652 // 3653 // The inner recurrence keeps its NW flag but only keeps NUW/NSW if 3654 // the outer recurrence has the same property. 3655 SCEV::NoWrapFlags InnerFlags = 3656 maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags); 3657 return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags); 3658 } 3659 } 3660 // Reset Operands to its original state. 3661 Operands[0] = NestedAR; 3662 } 3663 } 3664 3665 // Okay, it looks like we really DO need an addrec expr. Check to see if we 3666 // already have one, otherwise create a new one. 3667 return getOrCreateAddRecExpr(Operands, L, Flags); 3668 } 3669 3670 const SCEV * 3671 ScalarEvolution::getGEPExpr(GEPOperator *GEP, 3672 const SmallVectorImpl<const SCEV *> &IndexExprs) { 3673 const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand()); 3674 // getSCEV(Base)->getType() has the same address space as Base->getType() 3675 // because SCEV::getType() preserves the address space. 3676 Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType()); 3677 const bool AssumeInBoundsFlags = [&]() { 3678 if (!GEP->isInBounds()) 3679 return false; 3680 3681 // We'd like to propagate flags from the IR to the corresponding SCEV nodes, 3682 // but to do that, we have to ensure that said flag is valid in the entire 3683 // defined scope of the SCEV. 3684 auto *GEPI = dyn_cast<Instruction>(GEP); 3685 // TODO: non-instructions have global scope. We might be able to prove 3686 // some global scope cases 3687 return GEPI && isSCEVExprNeverPoison(GEPI); 3688 }(); 3689 3690 SCEV::NoWrapFlags OffsetWrap = 3691 AssumeInBoundsFlags ? SCEV::FlagNSW : SCEV::FlagAnyWrap; 3692 3693 Type *CurTy = GEP->getType(); 3694 bool FirstIter = true; 3695 SmallVector<const SCEV *, 4> Offsets; 3696 for (const SCEV *IndexExpr : IndexExprs) { 3697 // Compute the (potentially symbolic) offset in bytes for this index. 3698 if (StructType *STy = dyn_cast<StructType>(CurTy)) { 3699 // For a struct, add the member offset. 3700 ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue(); 3701 unsigned FieldNo = Index->getZExtValue(); 3702 const SCEV *FieldOffset = getOffsetOfExpr(IntIdxTy, STy, FieldNo); 3703 Offsets.push_back(FieldOffset); 3704 3705 // Update CurTy to the type of the field at Index. 3706 CurTy = STy->getTypeAtIndex(Index); 3707 } else { 3708 // Update CurTy to its element type. 3709 if (FirstIter) { 3710 assert(isa<PointerType>(CurTy) && 3711 "The first index of a GEP indexes a pointer"); 3712 CurTy = GEP->getSourceElementType(); 3713 FirstIter = false; 3714 } else { 3715 CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0); 3716 } 3717 // For an array, add the element offset, explicitly scaled. 3718 const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy); 3719 // Getelementptr indices are signed. 3720 IndexExpr = getTruncateOrSignExtend(IndexExpr, IntIdxTy); 3721 3722 // Multiply the index by the element size to compute the element offset. 3723 const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, OffsetWrap); 3724 Offsets.push_back(LocalOffset); 3725 } 3726 } 3727 3728 // Handle degenerate case of GEP without offsets. 3729 if (Offsets.empty()) 3730 return BaseExpr; 3731 3732 // Add the offsets together, assuming nsw if inbounds. 3733 const SCEV *Offset = getAddExpr(Offsets, OffsetWrap); 3734 // Add the base address and the offset. We cannot use the nsw flag, as the 3735 // base address is unsigned. However, if we know that the offset is 3736 // non-negative, we can use nuw. 3737 SCEV::NoWrapFlags BaseWrap = AssumeInBoundsFlags && isKnownNonNegative(Offset) 3738 ? SCEV::FlagNUW : SCEV::FlagAnyWrap; 3739 auto *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap); 3740 assert(BaseExpr->getType() == GEPExpr->getType() && 3741 "GEP should not change type mid-flight."); 3742 return GEPExpr; 3743 } 3744 3745 SCEV *ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType, 3746 ArrayRef<const SCEV *> Ops) { 3747 FoldingSetNodeID ID; 3748 ID.AddInteger(SCEVType); 3749 for (const SCEV *Op : Ops) 3750 ID.AddPointer(Op); 3751 void *IP = nullptr; 3752 return UniqueSCEVs.FindNodeOrInsertPos(ID, IP); 3753 } 3754 3755 const SCEV *ScalarEvolution::getAbsExpr(const SCEV *Op, bool IsNSW) { 3756 SCEV::NoWrapFlags Flags = IsNSW ? SCEV::FlagNSW : SCEV::FlagAnyWrap; 3757 return getSMaxExpr(Op, getNegativeSCEV(Op, Flags)); 3758 } 3759 3760 const SCEV *ScalarEvolution::getMinMaxExpr(SCEVTypes Kind, 3761 SmallVectorImpl<const SCEV *> &Ops) { 3762 assert(SCEVMinMaxExpr::isMinMaxType(Kind) && "Not a SCEVMinMaxExpr!"); 3763 assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!"); 3764 if (Ops.size() == 1) return Ops[0]; 3765 #ifndef NDEBUG 3766 Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); 3767 for (unsigned i = 1, e = Ops.size(); i != e; ++i) { 3768 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && 3769 "Operand types don't match!"); 3770 assert(Ops[0]->getType()->isPointerTy() == 3771 Ops[i]->getType()->isPointerTy() && 3772 "min/max should be consistently pointerish"); 3773 } 3774 #endif 3775 3776 bool IsSigned = Kind == scSMaxExpr || Kind == scSMinExpr; 3777 bool IsMax = Kind == scSMaxExpr || Kind == scUMaxExpr; 3778 3779 // Sort by complexity, this groups all similar expression types together. 3780 GroupByComplexity(Ops, &LI, DT); 3781 3782 // Check if we have created the same expression before. 3783 if (const SCEV *S = findExistingSCEVInCache(Kind, Ops)) { 3784 return S; 3785 } 3786 3787 // If there are any constants, fold them together. 3788 unsigned Idx = 0; 3789 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 3790 ++Idx; 3791 assert(Idx < Ops.size()); 3792 auto FoldOp = [&](const APInt &LHS, const APInt &RHS) { 3793 if (Kind == scSMaxExpr) 3794 return APIntOps::smax(LHS, RHS); 3795 else if (Kind == scSMinExpr) 3796 return APIntOps::smin(LHS, RHS); 3797 else if (Kind == scUMaxExpr) 3798 return APIntOps::umax(LHS, RHS); 3799 else if (Kind == scUMinExpr) 3800 return APIntOps::umin(LHS, RHS); 3801 llvm_unreachable("Unknown SCEV min/max opcode"); 3802 }; 3803 3804 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 3805 // We found two constants, fold them together! 3806 ConstantInt *Fold = ConstantInt::get( 3807 getContext(), FoldOp(LHSC->getAPInt(), RHSC->getAPInt())); 3808 Ops[0] = getConstant(Fold); 3809 Ops.erase(Ops.begin()+1); // Erase the folded element 3810 if (Ops.size() == 1) return Ops[0]; 3811 LHSC = cast<SCEVConstant>(Ops[0]); 3812 } 3813 3814 bool IsMinV = LHSC->getValue()->isMinValue(IsSigned); 3815 bool IsMaxV = LHSC->getValue()->isMaxValue(IsSigned); 3816 3817 if (IsMax ? IsMinV : IsMaxV) { 3818 // If we are left with a constant minimum(/maximum)-int, strip it off. 3819 Ops.erase(Ops.begin()); 3820 --Idx; 3821 } else if (IsMax ? IsMaxV : IsMinV) { 3822 // If we have a max(/min) with a constant maximum(/minimum)-int, 3823 // it will always be the extremum. 3824 return LHSC; 3825 } 3826 3827 if (Ops.size() == 1) return Ops[0]; 3828 } 3829 3830 // Find the first operation of the same kind 3831 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < Kind) 3832 ++Idx; 3833 3834 // Check to see if one of the operands is of the same kind. If so, expand its 3835 // operands onto our operand list, and recurse to simplify. 3836 if (Idx < Ops.size()) { 3837 bool DeletedAny = false; 3838 while (Ops[Idx]->getSCEVType() == Kind) { 3839 const SCEVMinMaxExpr *SMME = cast<SCEVMinMaxExpr>(Ops[Idx]); 3840 Ops.erase(Ops.begin()+Idx); 3841 Ops.append(SMME->op_begin(), SMME->op_end()); 3842 DeletedAny = true; 3843 } 3844 3845 if (DeletedAny) 3846 return getMinMaxExpr(Kind, Ops); 3847 } 3848 3849 // Okay, check to see if the same value occurs in the operand list twice. If 3850 // so, delete one. Since we sorted the list, these values are required to 3851 // be adjacent. 3852 llvm::CmpInst::Predicate GEPred = 3853 IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; 3854 llvm::CmpInst::Predicate LEPred = 3855 IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; 3856 llvm::CmpInst::Predicate FirstPred = IsMax ? GEPred : LEPred; 3857 llvm::CmpInst::Predicate SecondPred = IsMax ? LEPred : GEPred; 3858 for (unsigned i = 0, e = Ops.size() - 1; i != e; ++i) { 3859 if (Ops[i] == Ops[i + 1] || 3860 isKnownViaNonRecursiveReasoning(FirstPred, Ops[i], Ops[i + 1])) { 3861 // X op Y op Y --> X op Y 3862 // X op Y --> X, if we know X, Y are ordered appropriately 3863 Ops.erase(Ops.begin() + i + 1, Ops.begin() + i + 2); 3864 --i; 3865 --e; 3866 } else if (isKnownViaNonRecursiveReasoning(SecondPred, Ops[i], 3867 Ops[i + 1])) { 3868 // X op Y --> Y, if we know X, Y are ordered appropriately 3869 Ops.erase(Ops.begin() + i, Ops.begin() + i + 1); 3870 --i; 3871 --e; 3872 } 3873 } 3874 3875 if (Ops.size() == 1) return Ops[0]; 3876 3877 assert(!Ops.empty() && "Reduced smax down to nothing!"); 3878 3879 // Okay, it looks like we really DO need an expr. Check to see if we 3880 // already have one, otherwise create a new one. 3881 FoldingSetNodeID ID; 3882 ID.AddInteger(Kind); 3883 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 3884 ID.AddPointer(Ops[i]); 3885 void *IP = nullptr; 3886 const SCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, IP); 3887 if (ExistingSCEV) 3888 return ExistingSCEV; 3889 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 3890 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 3891 SCEV *S = new (SCEVAllocator) 3892 SCEVMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size()); 3893 3894 UniqueSCEVs.InsertNode(S, IP); 3895 registerUser(S, Ops); 3896 return S; 3897 } 3898 3899 namespace { 3900 3901 class SCEVSequentialMinMaxDeduplicatingVisitor final 3902 : public SCEVVisitor<SCEVSequentialMinMaxDeduplicatingVisitor, 3903 Optional<const SCEV *>> { 3904 using RetVal = Optional<const SCEV *>; 3905 using Base = SCEVVisitor<SCEVSequentialMinMaxDeduplicatingVisitor, RetVal>; 3906 3907 ScalarEvolution &SE; 3908 const SCEVTypes RootKind; // Must be a sequential min/max expression. 3909 const SCEVTypes NonSequentialRootKind; // Non-sequential variant of RootKind. 3910 SmallPtrSet<const SCEV *, 16> SeenOps; 3911 3912 bool canRecurseInto(SCEVTypes Kind) const { 3913 // We can only recurse into the SCEV expression of the same effective type 3914 // as the type of our root SCEV expression. 3915 return RootKind == Kind || NonSequentialRootKind == Kind; 3916 }; 3917 3918 RetVal visitAnyMinMaxExpr(const SCEV *S) { 3919 assert((isa<SCEVMinMaxExpr>(S) || isa<SCEVSequentialMinMaxExpr>(S)) && 3920 "Only for min/max expressions."); 3921 SCEVTypes Kind = S->getSCEVType(); 3922 3923 if (!canRecurseInto(Kind)) 3924 return S; 3925 3926 auto *NAry = cast<SCEVNAryExpr>(S); 3927 SmallVector<const SCEV *> NewOps; 3928 bool Changed = 3929 visit(Kind, makeArrayRef(NAry->op_begin(), NAry->op_end()), NewOps); 3930 3931 if (!Changed) 3932 return S; 3933 if (NewOps.empty()) 3934 return None; 3935 3936 return isa<SCEVSequentialMinMaxExpr>(S) 3937 ? SE.getSequentialMinMaxExpr(Kind, NewOps) 3938 : SE.getMinMaxExpr(Kind, NewOps); 3939 } 3940 3941 RetVal visit(const SCEV *S) { 3942 // Has the whole operand been seen already? 3943 if (!SeenOps.insert(S).second) 3944 return None; 3945 return Base::visit(S); 3946 } 3947 3948 public: 3949 SCEVSequentialMinMaxDeduplicatingVisitor(ScalarEvolution &SE, 3950 SCEVTypes RootKind) 3951 : SE(SE), RootKind(RootKind), 3952 NonSequentialRootKind( 3953 SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType( 3954 RootKind)) {} 3955 3956 bool /*Changed*/ visit(SCEVTypes Kind, ArrayRef<const SCEV *> OrigOps, 3957 SmallVectorImpl<const SCEV *> &NewOps) { 3958 bool Changed = false; 3959 SmallVector<const SCEV *> Ops; 3960 Ops.reserve(OrigOps.size()); 3961 3962 for (const SCEV *Op : OrigOps) { 3963 RetVal NewOp = visit(Op); 3964 if (NewOp != Op) 3965 Changed = true; 3966 if (NewOp) 3967 Ops.emplace_back(*NewOp); 3968 } 3969 3970 if (Changed) 3971 NewOps = std::move(Ops); 3972 return Changed; 3973 } 3974 3975 RetVal visitConstant(const SCEVConstant *Constant) { return Constant; } 3976 3977 RetVal visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) { return Expr; } 3978 3979 RetVal visitTruncateExpr(const SCEVTruncateExpr *Expr) { return Expr; } 3980 3981 RetVal visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { return Expr; } 3982 3983 RetVal visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { return Expr; } 3984 3985 RetVal visitAddExpr(const SCEVAddExpr *Expr) { return Expr; } 3986 3987 RetVal visitMulExpr(const SCEVMulExpr *Expr) { return Expr; } 3988 3989 RetVal visitUDivExpr(const SCEVUDivExpr *Expr) { return Expr; } 3990 3991 RetVal visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; } 3992 3993 RetVal visitSMaxExpr(const SCEVSMaxExpr *Expr) { 3994 return visitAnyMinMaxExpr(Expr); 3995 } 3996 3997 RetVal visitUMaxExpr(const SCEVUMaxExpr *Expr) { 3998 return visitAnyMinMaxExpr(Expr); 3999 } 4000 4001 RetVal visitSMinExpr(const SCEVSMinExpr *Expr) { 4002 return visitAnyMinMaxExpr(Expr); 4003 } 4004 4005 RetVal visitUMinExpr(const SCEVUMinExpr *Expr) { 4006 return visitAnyMinMaxExpr(Expr); 4007 } 4008 4009 RetVal visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Expr) { 4010 return visitAnyMinMaxExpr(Expr); 4011 } 4012 4013 RetVal visitUnknown(const SCEVUnknown *Expr) { return Expr; } 4014 4015 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { return Expr; } 4016 }; 4017 4018 } // namespace 4019 4020 /// Return true if V is poison given that AssumedPoison is already poison. 4021 static bool impliesPoison(const SCEV *AssumedPoison, const SCEV *S) { 4022 // The only way poison may be introduced in a SCEV expression is from a 4023 // poison SCEVUnknown (ConstantExprs are also represented as SCEVUnknown, 4024 // not SCEVConstant). Notably, nowrap flags in SCEV nodes can *not* 4025 // introduce poison -- they encode guaranteed, non-speculated knowledge. 4026 // 4027 // Additionally, all SCEV nodes propagate poison from inputs to outputs, 4028 // with the notable exception of umin_seq, where only poison from the first 4029 // operand is (unconditionally) propagated. 4030 struct SCEVPoisonCollector { 4031 bool LookThroughSeq; 4032 SmallPtrSet<const SCEV *, 4> MaybePoison; 4033 SCEVPoisonCollector(bool LookThroughSeq) : LookThroughSeq(LookThroughSeq) {} 4034 4035 bool follow(const SCEV *S) { 4036 // TODO: We can always follow the first operand, but the SCEVTraversal 4037 // API doesn't support this. 4038 if (!LookThroughSeq && isa<SCEVSequentialMinMaxExpr>(S)) 4039 return false; 4040 4041 if (auto *SU = dyn_cast<SCEVUnknown>(S)) { 4042 if (!isGuaranteedNotToBePoison(SU->getValue())) 4043 MaybePoison.insert(S); 4044 } 4045 return true; 4046 } 4047 bool isDone() const { return false; } 4048 }; 4049 4050 // First collect all SCEVs that might result in AssumedPoison to be poison. 4051 // We need to look through umin_seq here, because we want to find all SCEVs 4052 // that *might* result in poison, not only those that are *required* to. 4053 SCEVPoisonCollector PC1(/* LookThroughSeq */ true); 4054 visitAll(AssumedPoison, PC1); 4055 4056 // AssumedPoison is never poison. As the assumption is false, the implication 4057 // is true. Don't bother walking the other SCEV in this case. 4058 if (PC1.MaybePoison.empty()) 4059 return true; 4060 4061 // Collect all SCEVs in S that, if poison, *will* result in S being poison 4062 // as well. We cannot look through umin_seq here, as its argument only *may* 4063 // make the result poison. 4064 SCEVPoisonCollector PC2(/* LookThroughSeq */ false); 4065 visitAll(S, PC2); 4066 4067 // Make sure that no matter which SCEV in PC1.MaybePoison is actually poison, 4068 // it will also make S poison by being part of PC2.MaybePoison. 4069 return all_of(PC1.MaybePoison, 4070 [&](const SCEV *S) { return PC2.MaybePoison.contains(S); }); 4071 } 4072 4073 const SCEV * 4074 ScalarEvolution::getSequentialMinMaxExpr(SCEVTypes Kind, 4075 SmallVectorImpl<const SCEV *> &Ops) { 4076 assert(SCEVSequentialMinMaxExpr::isSequentialMinMaxType(Kind) && 4077 "Not a SCEVSequentialMinMaxExpr!"); 4078 assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!"); 4079 if (Ops.size() == 1) 4080 return Ops[0]; 4081 #ifndef NDEBUG 4082 Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); 4083 for (unsigned i = 1, e = Ops.size(); i != e; ++i) { 4084 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && 4085 "Operand types don't match!"); 4086 assert(Ops[0]->getType()->isPointerTy() == 4087 Ops[i]->getType()->isPointerTy() && 4088 "min/max should be consistently pointerish"); 4089 } 4090 #endif 4091 4092 // Note that SCEVSequentialMinMaxExpr is *NOT* commutative, 4093 // so we can *NOT* do any kind of sorting of the expressions! 4094 4095 // Check if we have created the same expression before. 4096 if (const SCEV *S = findExistingSCEVInCache(Kind, Ops)) 4097 return S; 4098 4099 // FIXME: there are *some* simplifications that we can do here. 4100 4101 // Keep only the first instance of an operand. 4102 { 4103 SCEVSequentialMinMaxDeduplicatingVisitor Deduplicator(*this, Kind); 4104 bool Changed = Deduplicator.visit(Kind, Ops, Ops); 4105 if (Changed) 4106 return getSequentialMinMaxExpr(Kind, Ops); 4107 } 4108 4109 // Check to see if one of the operands is of the same kind. If so, expand its 4110 // operands onto our operand list, and recurse to simplify. 4111 { 4112 unsigned Idx = 0; 4113 bool DeletedAny = false; 4114 while (Idx < Ops.size()) { 4115 if (Ops[Idx]->getSCEVType() != Kind) { 4116 ++Idx; 4117 continue; 4118 } 4119 const auto *SMME = cast<SCEVSequentialMinMaxExpr>(Ops[Idx]); 4120 Ops.erase(Ops.begin() + Idx); 4121 Ops.insert(Ops.begin() + Idx, SMME->op_begin(), SMME->op_end()); 4122 DeletedAny = true; 4123 } 4124 4125 if (DeletedAny) 4126 return getSequentialMinMaxExpr(Kind, Ops); 4127 } 4128 4129 const SCEV *SaturationPoint; 4130 ICmpInst::Predicate Pred; 4131 switch (Kind) { 4132 case scSequentialUMinExpr: 4133 SaturationPoint = getZero(Ops[0]->getType()); 4134 Pred = ICmpInst::ICMP_ULE; 4135 break; 4136 default: 4137 llvm_unreachable("Not a sequential min/max type."); 4138 } 4139 4140 for (unsigned i = 1, e = Ops.size(); i != e; ++i) { 4141 // We can replace %x umin_seq %y with %x umin %y if either: 4142 // * %y being poison implies %x is also poison. 4143 // * %x cannot be the saturating value (e.g. zero for umin). 4144 if (::impliesPoison(Ops[i], Ops[i - 1]) || 4145 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_NE, Ops[i - 1], 4146 SaturationPoint)) { 4147 SmallVector<const SCEV *> SeqOps = {Ops[i - 1], Ops[i]}; 4148 Ops[i - 1] = getMinMaxExpr( 4149 SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(Kind), 4150 SeqOps); 4151 Ops.erase(Ops.begin() + i); 4152 return getSequentialMinMaxExpr(Kind, Ops); 4153 } 4154 // Fold %x umin_seq %y to %x if %x ule %y. 4155 // TODO: We might be able to prove the predicate for a later operand. 4156 if (isKnownViaNonRecursiveReasoning(Pred, Ops[i - 1], Ops[i])) { 4157 Ops.erase(Ops.begin() + i); 4158 return getSequentialMinMaxExpr(Kind, Ops); 4159 } 4160 } 4161 4162 // Okay, it looks like we really DO need an expr. Check to see if we 4163 // already have one, otherwise create a new one. 4164 FoldingSetNodeID ID; 4165 ID.AddInteger(Kind); 4166 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 4167 ID.AddPointer(Ops[i]); 4168 void *IP = nullptr; 4169 const SCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, IP); 4170 if (ExistingSCEV) 4171 return ExistingSCEV; 4172 4173 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 4174 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 4175 SCEV *S = new (SCEVAllocator) 4176 SCEVSequentialMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size()); 4177 4178 UniqueSCEVs.InsertNode(S, IP); 4179 registerUser(S, Ops); 4180 return S; 4181 } 4182 4183 const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, const SCEV *RHS) { 4184 SmallVector<const SCEV *, 2> Ops = {LHS, RHS}; 4185 return getSMaxExpr(Ops); 4186 } 4187 4188 const SCEV *ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { 4189 return getMinMaxExpr(scSMaxExpr, Ops); 4190 } 4191 4192 const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, const SCEV *RHS) { 4193 SmallVector<const SCEV *, 2> Ops = {LHS, RHS}; 4194 return getUMaxExpr(Ops); 4195 } 4196 4197 const SCEV *ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { 4198 return getMinMaxExpr(scUMaxExpr, Ops); 4199 } 4200 4201 const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, 4202 const SCEV *RHS) { 4203 SmallVector<const SCEV *, 2> Ops = { LHS, RHS }; 4204 return getSMinExpr(Ops); 4205 } 4206 4207 const SCEV *ScalarEvolution::getSMinExpr(SmallVectorImpl<const SCEV *> &Ops) { 4208 return getMinMaxExpr(scSMinExpr, Ops); 4209 } 4210 4211 const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, const SCEV *RHS, 4212 bool Sequential) { 4213 SmallVector<const SCEV *, 2> Ops = { LHS, RHS }; 4214 return getUMinExpr(Ops, Sequential); 4215 } 4216 4217 const SCEV *ScalarEvolution::getUMinExpr(SmallVectorImpl<const SCEV *> &Ops, 4218 bool Sequential) { 4219 return Sequential ? getSequentialMinMaxExpr(scSequentialUMinExpr, Ops) 4220 : getMinMaxExpr(scUMinExpr, Ops); 4221 } 4222 4223 const SCEV * 4224 ScalarEvolution::getSizeOfScalableVectorExpr(Type *IntTy, 4225 ScalableVectorType *ScalableTy) { 4226 Constant *NullPtr = Constant::getNullValue(ScalableTy->getPointerTo()); 4227 Constant *One = ConstantInt::get(IntTy, 1); 4228 Constant *GEP = ConstantExpr::getGetElementPtr(ScalableTy, NullPtr, One); 4229 // Note that the expression we created is the final expression, we don't 4230 // want to simplify it any further Also, if we call a normal getSCEV(), 4231 // we'll end up in an endless recursion. So just create an SCEVUnknown. 4232 return getUnknown(ConstantExpr::getPtrToInt(GEP, IntTy)); 4233 } 4234 4235 const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) { 4236 if (auto *ScalableAllocTy = dyn_cast<ScalableVectorType>(AllocTy)) 4237 return getSizeOfScalableVectorExpr(IntTy, ScalableAllocTy); 4238 // We can bypass creating a target-independent constant expression and then 4239 // folding it back into a ConstantInt. This is just a compile-time 4240 // optimization. 4241 return getConstant(IntTy, getDataLayout().getTypeAllocSize(AllocTy)); 4242 } 4243 4244 const SCEV *ScalarEvolution::getStoreSizeOfExpr(Type *IntTy, Type *StoreTy) { 4245 if (auto *ScalableStoreTy = dyn_cast<ScalableVectorType>(StoreTy)) 4246 return getSizeOfScalableVectorExpr(IntTy, ScalableStoreTy); 4247 // We can bypass creating a target-independent constant expression and then 4248 // folding it back into a ConstantInt. This is just a compile-time 4249 // optimization. 4250 return getConstant(IntTy, getDataLayout().getTypeStoreSize(StoreTy)); 4251 } 4252 4253 const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy, 4254 StructType *STy, 4255 unsigned FieldNo) { 4256 // We can bypass creating a target-independent constant expression and then 4257 // folding it back into a ConstantInt. This is just a compile-time 4258 // optimization. 4259 return getConstant( 4260 IntTy, getDataLayout().getStructLayout(STy)->getElementOffset(FieldNo)); 4261 } 4262 4263 const SCEV *ScalarEvolution::getUnknown(Value *V) { 4264 // Don't attempt to do anything other than create a SCEVUnknown object 4265 // here. createSCEV only calls getUnknown after checking for all other 4266 // interesting possibilities, and any other code that calls getUnknown 4267 // is doing so in order to hide a value from SCEV canonicalization. 4268 4269 FoldingSetNodeID ID; 4270 ID.AddInteger(scUnknown); 4271 ID.AddPointer(V); 4272 void *IP = nullptr; 4273 if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) { 4274 assert(cast<SCEVUnknown>(S)->getValue() == V && 4275 "Stale SCEVUnknown in uniquing map!"); 4276 return S; 4277 } 4278 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this, 4279 FirstUnknown); 4280 FirstUnknown = cast<SCEVUnknown>(S); 4281 UniqueSCEVs.InsertNode(S, IP); 4282 return S; 4283 } 4284 4285 //===----------------------------------------------------------------------===// 4286 // Basic SCEV Analysis and PHI Idiom Recognition Code 4287 // 4288 4289 /// Test if values of the given type are analyzable within the SCEV 4290 /// framework. This primarily includes integer types, and it can optionally 4291 /// include pointer types if the ScalarEvolution class has access to 4292 /// target-specific information. 4293 bool ScalarEvolution::isSCEVable(Type *Ty) const { 4294 // Integers and pointers are always SCEVable. 4295 return Ty->isIntOrPtrTy(); 4296 } 4297 4298 /// Return the size in bits of the specified type, for which isSCEVable must 4299 /// return true. 4300 uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const { 4301 assert(isSCEVable(Ty) && "Type is not SCEVable!"); 4302 if (Ty->isPointerTy()) 4303 return getDataLayout().getIndexTypeSizeInBits(Ty); 4304 return getDataLayout().getTypeSizeInBits(Ty); 4305 } 4306 4307 /// Return a type with the same bitwidth as the given type and which represents 4308 /// how SCEV will treat the given type, for which isSCEVable must return 4309 /// true. For pointer types, this is the pointer index sized integer type. 4310 Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const { 4311 assert(isSCEVable(Ty) && "Type is not SCEVable!"); 4312 4313 if (Ty->isIntegerTy()) 4314 return Ty; 4315 4316 // The only other support type is pointer. 4317 assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!"); 4318 return getDataLayout().getIndexType(Ty); 4319 } 4320 4321 Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const { 4322 return getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2; 4323 } 4324 4325 bool ScalarEvolution::instructionCouldExistWitthOperands(const SCEV *A, 4326 const SCEV *B) { 4327 /// For a valid use point to exist, the defining scope of one operand 4328 /// must dominate the other. 4329 bool PreciseA, PreciseB; 4330 auto *ScopeA = getDefiningScopeBound({A}, PreciseA); 4331 auto *ScopeB = getDefiningScopeBound({B}, PreciseB); 4332 if (!PreciseA || !PreciseB) 4333 // Can't tell. 4334 return false; 4335 return (ScopeA == ScopeB) || DT.dominates(ScopeA, ScopeB) || 4336 DT.dominates(ScopeB, ScopeA); 4337 } 4338 4339 4340 const SCEV *ScalarEvolution::getCouldNotCompute() { 4341 return CouldNotCompute.get(); 4342 } 4343 4344 bool ScalarEvolution::checkValidity(const SCEV *S) const { 4345 bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) { 4346 auto *SU = dyn_cast<SCEVUnknown>(S); 4347 return SU && SU->getValue() == nullptr; 4348 }); 4349 4350 return !ContainsNulls; 4351 } 4352 4353 bool ScalarEvolution::containsAddRecurrence(const SCEV *S) { 4354 HasRecMapType::iterator I = HasRecMap.find(S); 4355 if (I != HasRecMap.end()) 4356 return I->second; 4357 4358 bool FoundAddRec = 4359 SCEVExprContains(S, [](const SCEV *S) { return isa<SCEVAddRecExpr>(S); }); 4360 HasRecMap.insert({S, FoundAddRec}); 4361 return FoundAddRec; 4362 } 4363 4364 /// Return the ValueOffsetPair set for \p S. \p S can be represented 4365 /// by the value and offset from any ValueOffsetPair in the set. 4366 ArrayRef<Value *> ScalarEvolution::getSCEVValues(const SCEV *S) { 4367 ExprValueMapType::iterator SI = ExprValueMap.find_as(S); 4368 if (SI == ExprValueMap.end()) 4369 return None; 4370 #ifndef NDEBUG 4371 if (VerifySCEVMap) { 4372 // Check there is no dangling Value in the set returned. 4373 for (Value *V : SI->second) 4374 assert(ValueExprMap.count(V)); 4375 } 4376 #endif 4377 return SI->second.getArrayRef(); 4378 } 4379 4380 /// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V) 4381 /// cannot be used separately. eraseValueFromMap should be used to remove 4382 /// V from ValueExprMap and ExprValueMap at the same time. 4383 void ScalarEvolution::eraseValueFromMap(Value *V) { 4384 ValueExprMapType::iterator I = ValueExprMap.find_as(V); 4385 if (I != ValueExprMap.end()) { 4386 auto EVIt = ExprValueMap.find(I->second); 4387 bool Removed = EVIt->second.remove(V); 4388 (void) Removed; 4389 assert(Removed && "Value not in ExprValueMap?"); 4390 ValueExprMap.erase(I); 4391 } 4392 } 4393 4394 void ScalarEvolution::insertValueToMap(Value *V, const SCEV *S) { 4395 // A recursive query may have already computed the SCEV. It should be 4396 // equivalent, but may not necessarily be exactly the same, e.g. due to lazily 4397 // inferred nowrap flags. 4398 auto It = ValueExprMap.find_as(V); 4399 if (It == ValueExprMap.end()) { 4400 ValueExprMap.insert({SCEVCallbackVH(V, this), S}); 4401 ExprValueMap[S].insert(V); 4402 } 4403 } 4404 4405 /// Return an existing SCEV if it exists, otherwise analyze the expression and 4406 /// create a new one. 4407 const SCEV *ScalarEvolution::getSCEV(Value *V) { 4408 assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); 4409 4410 const SCEV *S = getExistingSCEV(V); 4411 if (S == nullptr) { 4412 S = createSCEV(V); 4413 // During PHI resolution, it is possible to create two SCEVs for the same 4414 // V, so it is needed to double check whether V->S is inserted into 4415 // ValueExprMap before insert S->{V, 0} into ExprValueMap. 4416 std::pair<ValueExprMapType::iterator, bool> Pair = 4417 ValueExprMap.insert({SCEVCallbackVH(V, this), S}); 4418 if (Pair.second) 4419 ExprValueMap[S].insert(V); 4420 } 4421 return S; 4422 } 4423 4424 const SCEV *ScalarEvolution::getExistingSCEV(Value *V) { 4425 assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); 4426 4427 ValueExprMapType::iterator I = ValueExprMap.find_as(V); 4428 if (I != ValueExprMap.end()) { 4429 const SCEV *S = I->second; 4430 assert(checkValidity(S) && 4431 "existing SCEV has not been properly invalidated"); 4432 return S; 4433 } 4434 return nullptr; 4435 } 4436 4437 /// Return a SCEV corresponding to -V = -1*V 4438 const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V, 4439 SCEV::NoWrapFlags Flags) { 4440 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) 4441 return getConstant( 4442 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); 4443 4444 Type *Ty = V->getType(); 4445 Ty = getEffectiveSCEVType(Ty); 4446 return getMulExpr(V, getMinusOne(Ty), Flags); 4447 } 4448 4449 /// If Expr computes ~A, return A else return nullptr 4450 static const SCEV *MatchNotExpr(const SCEV *Expr) { 4451 const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr); 4452 if (!Add || Add->getNumOperands() != 2 || 4453 !Add->getOperand(0)->isAllOnesValue()) 4454 return nullptr; 4455 4456 const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1)); 4457 if (!AddRHS || AddRHS->getNumOperands() != 2 || 4458 !AddRHS->getOperand(0)->isAllOnesValue()) 4459 return nullptr; 4460 4461 return AddRHS->getOperand(1); 4462 } 4463 4464 /// Return a SCEV corresponding to ~V = -1-V 4465 const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { 4466 assert(!V->getType()->isPointerTy() && "Can't negate pointer"); 4467 4468 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) 4469 return getConstant( 4470 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); 4471 4472 // Fold ~(u|s)(min|max)(~x, ~y) to (u|s)(max|min)(x, y) 4473 if (const SCEVMinMaxExpr *MME = dyn_cast<SCEVMinMaxExpr>(V)) { 4474 auto MatchMinMaxNegation = [&](const SCEVMinMaxExpr *MME) { 4475 SmallVector<const SCEV *, 2> MatchedOperands; 4476 for (const SCEV *Operand : MME->operands()) { 4477 const SCEV *Matched = MatchNotExpr(Operand); 4478 if (!Matched) 4479 return (const SCEV *)nullptr; 4480 MatchedOperands.push_back(Matched); 4481 } 4482 return getMinMaxExpr(SCEVMinMaxExpr::negate(MME->getSCEVType()), 4483 MatchedOperands); 4484 }; 4485 if (const SCEV *Replaced = MatchMinMaxNegation(MME)) 4486 return Replaced; 4487 } 4488 4489 Type *Ty = V->getType(); 4490 Ty = getEffectiveSCEVType(Ty); 4491 return getMinusSCEV(getMinusOne(Ty), V); 4492 } 4493 4494 const SCEV *ScalarEvolution::removePointerBase(const SCEV *P) { 4495 assert(P->getType()->isPointerTy()); 4496 4497 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(P)) { 4498 // The base of an AddRec is the first operand. 4499 SmallVector<const SCEV *> Ops{AddRec->operands()}; 4500 Ops[0] = removePointerBase(Ops[0]); 4501 // Don't try to transfer nowrap flags for now. We could in some cases 4502 // (for example, if pointer operand of the AddRec is a SCEVUnknown). 4503 return getAddRecExpr(Ops, AddRec->getLoop(), SCEV::FlagAnyWrap); 4504 } 4505 if (auto *Add = dyn_cast<SCEVAddExpr>(P)) { 4506 // The base of an Add is the pointer operand. 4507 SmallVector<const SCEV *> Ops{Add->operands()}; 4508 const SCEV **PtrOp = nullptr; 4509 for (const SCEV *&AddOp : Ops) { 4510 if (AddOp->getType()->isPointerTy()) { 4511 assert(!PtrOp && "Cannot have multiple pointer ops"); 4512 PtrOp = &AddOp; 4513 } 4514 } 4515 *PtrOp = removePointerBase(*PtrOp); 4516 // Don't try to transfer nowrap flags for now. We could in some cases 4517 // (for example, if the pointer operand of the Add is a SCEVUnknown). 4518 return getAddExpr(Ops); 4519 } 4520 // Any other expression must be a pointer base. 4521 return getZero(P->getType()); 4522 } 4523 4524 const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS, 4525 SCEV::NoWrapFlags Flags, 4526 unsigned Depth) { 4527 // Fast path: X - X --> 0. 4528 if (LHS == RHS) 4529 return getZero(LHS->getType()); 4530 4531 // If we subtract two pointers with different pointer bases, bail. 4532 // Eventually, we're going to add an assertion to getMulExpr that we 4533 // can't multiply by a pointer. 4534 if (RHS->getType()->isPointerTy()) { 4535 if (!LHS->getType()->isPointerTy() || 4536 getPointerBase(LHS) != getPointerBase(RHS)) 4537 return getCouldNotCompute(); 4538 LHS = removePointerBase(LHS); 4539 RHS = removePointerBase(RHS); 4540 } 4541 4542 // We represent LHS - RHS as LHS + (-1)*RHS. This transformation 4543 // makes it so that we cannot make much use of NUW. 4544 auto AddFlags = SCEV::FlagAnyWrap; 4545 const bool RHSIsNotMinSigned = 4546 !getSignedRangeMin(RHS).isMinSignedValue(); 4547 if (hasFlags(Flags, SCEV::FlagNSW)) { 4548 // Let M be the minimum representable signed value. Then (-1)*RHS 4549 // signed-wraps if and only if RHS is M. That can happen even for 4550 // a NSW subtraction because e.g. (-1)*M signed-wraps even though 4551 // -1 - M does not. So to transfer NSW from LHS - RHS to LHS + 4552 // (-1)*RHS, we need to prove that RHS != M. 4553 // 4554 // If LHS is non-negative and we know that LHS - RHS does not 4555 // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap 4556 // either by proving that RHS > M or that LHS >= 0. 4557 if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) { 4558 AddFlags = SCEV::FlagNSW; 4559 } 4560 } 4561 4562 // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS - 4563 // RHS is NSW and LHS >= 0. 4564 // 4565 // The difficulty here is that the NSW flag may have been proven 4566 // relative to a loop that is to be found in a recurrence in LHS and 4567 // not in RHS. Applying NSW to (-1)*M may then let the NSW have a 4568 // larger scope than intended. 4569 auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap; 4570 4571 return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth); 4572 } 4573 4574 const SCEV *ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty, 4575 unsigned Depth) { 4576 Type *SrcTy = V->getType(); 4577 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 4578 "Cannot truncate or zero extend with non-integer arguments!"); 4579 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 4580 return V; // No conversion 4581 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) 4582 return getTruncateExpr(V, Ty, Depth); 4583 return getZeroExtendExpr(V, Ty, Depth); 4584 } 4585 4586 const SCEV *ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, Type *Ty, 4587 unsigned Depth) { 4588 Type *SrcTy = V->getType(); 4589 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 4590 "Cannot truncate or zero extend with non-integer arguments!"); 4591 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 4592 return V; // No conversion 4593 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) 4594 return getTruncateExpr(V, Ty, Depth); 4595 return getSignExtendExpr(V, Ty, Depth); 4596 } 4597 4598 const SCEV * 4599 ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) { 4600 Type *SrcTy = V->getType(); 4601 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 4602 "Cannot noop or zero extend with non-integer arguments!"); 4603 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 4604 "getNoopOrZeroExtend cannot truncate!"); 4605 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 4606 return V; // No conversion 4607 return getZeroExtendExpr(V, Ty); 4608 } 4609 4610 const SCEV * 4611 ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) { 4612 Type *SrcTy = V->getType(); 4613 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 4614 "Cannot noop or sign extend with non-integer arguments!"); 4615 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 4616 "getNoopOrSignExtend cannot truncate!"); 4617 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 4618 return V; // No conversion 4619 return getSignExtendExpr(V, Ty); 4620 } 4621 4622 const SCEV * 4623 ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) { 4624 Type *SrcTy = V->getType(); 4625 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 4626 "Cannot noop or any extend with non-integer arguments!"); 4627 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 4628 "getNoopOrAnyExtend cannot truncate!"); 4629 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 4630 return V; // No conversion 4631 return getAnyExtendExpr(V, Ty); 4632 } 4633 4634 const SCEV * 4635 ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) { 4636 Type *SrcTy = V->getType(); 4637 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() && 4638 "Cannot truncate or noop with non-integer arguments!"); 4639 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && 4640 "getTruncateOrNoop cannot extend!"); 4641 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 4642 return V; // No conversion 4643 return getTruncateExpr(V, Ty); 4644 } 4645 4646 const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, 4647 const SCEV *RHS) { 4648 const SCEV *PromotedLHS = LHS; 4649 const SCEV *PromotedRHS = RHS; 4650 4651 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) 4652 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); 4653 else 4654 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); 4655 4656 return getUMaxExpr(PromotedLHS, PromotedRHS); 4657 } 4658 4659 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, 4660 const SCEV *RHS, 4661 bool Sequential) { 4662 SmallVector<const SCEV *, 2> Ops = { LHS, RHS }; 4663 return getUMinFromMismatchedTypes(Ops, Sequential); 4664 } 4665 4666 const SCEV * 4667 ScalarEvolution::getUMinFromMismatchedTypes(SmallVectorImpl<const SCEV *> &Ops, 4668 bool Sequential) { 4669 assert(!Ops.empty() && "At least one operand must be!"); 4670 // Trivial case. 4671 if (Ops.size() == 1) 4672 return Ops[0]; 4673 4674 // Find the max type first. 4675 Type *MaxType = nullptr; 4676 for (auto *S : Ops) 4677 if (MaxType) 4678 MaxType = getWiderType(MaxType, S->getType()); 4679 else 4680 MaxType = S->getType(); 4681 assert(MaxType && "Failed to find maximum type!"); 4682 4683 // Extend all ops to max type. 4684 SmallVector<const SCEV *, 2> PromotedOps; 4685 for (auto *S : Ops) 4686 PromotedOps.push_back(getNoopOrZeroExtend(S, MaxType)); 4687 4688 // Generate umin. 4689 return getUMinExpr(PromotedOps, Sequential); 4690 } 4691 4692 const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) { 4693 // A pointer operand may evaluate to a nonpointer expression, such as null. 4694 if (!V->getType()->isPointerTy()) 4695 return V; 4696 4697 while (true) { 4698 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { 4699 V = AddRec->getStart(); 4700 } else if (auto *Add = dyn_cast<SCEVAddExpr>(V)) { 4701 const SCEV *PtrOp = nullptr; 4702 for (const SCEV *AddOp : Add->operands()) { 4703 if (AddOp->getType()->isPointerTy()) { 4704 assert(!PtrOp && "Cannot have multiple pointer ops"); 4705 PtrOp = AddOp; 4706 } 4707 } 4708 assert(PtrOp && "Must have pointer op"); 4709 V = PtrOp; 4710 } else // Not something we can look further into. 4711 return V; 4712 } 4713 } 4714 4715 /// Push users of the given Instruction onto the given Worklist. 4716 static void PushDefUseChildren(Instruction *I, 4717 SmallVectorImpl<Instruction *> &Worklist, 4718 SmallPtrSetImpl<Instruction *> &Visited) { 4719 // Push the def-use children onto the Worklist stack. 4720 for (User *U : I->users()) { 4721 auto *UserInsn = cast<Instruction>(U); 4722 if (Visited.insert(UserInsn).second) 4723 Worklist.push_back(UserInsn); 4724 } 4725 } 4726 4727 namespace { 4728 4729 /// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start 4730 /// expression in case its Loop is L. If it is not L then 4731 /// if IgnoreOtherLoops is true then use AddRec itself 4732 /// otherwise rewrite cannot be done. 4733 /// If SCEV contains non-invariant unknown SCEV rewrite cannot be done. 4734 class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> { 4735 public: 4736 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE, 4737 bool IgnoreOtherLoops = true) { 4738 SCEVInitRewriter Rewriter(L, SE); 4739 const SCEV *Result = Rewriter.visit(S); 4740 if (Rewriter.hasSeenLoopVariantSCEVUnknown()) 4741 return SE.getCouldNotCompute(); 4742 return Rewriter.hasSeenOtherLoops() && !IgnoreOtherLoops 4743 ? SE.getCouldNotCompute() 4744 : Result; 4745 } 4746 4747 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 4748 if (!SE.isLoopInvariant(Expr, L)) 4749 SeenLoopVariantSCEVUnknown = true; 4750 return Expr; 4751 } 4752 4753 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { 4754 // Only re-write AddRecExprs for this loop. 4755 if (Expr->getLoop() == L) 4756 return Expr->getStart(); 4757 SeenOtherLoops = true; 4758 return Expr; 4759 } 4760 4761 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; } 4762 4763 bool hasSeenOtherLoops() { return SeenOtherLoops; } 4764 4765 private: 4766 explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE) 4767 : SCEVRewriteVisitor(SE), L(L) {} 4768 4769 const Loop *L; 4770 bool SeenLoopVariantSCEVUnknown = false; 4771 bool SeenOtherLoops = false; 4772 }; 4773 4774 /// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post 4775 /// increment expression in case its Loop is L. If it is not L then 4776 /// use AddRec itself. 4777 /// If SCEV contains non-invariant unknown SCEV rewrite cannot be done. 4778 class SCEVPostIncRewriter : public SCEVRewriteVisitor<SCEVPostIncRewriter> { 4779 public: 4780 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE) { 4781 SCEVPostIncRewriter Rewriter(L, SE); 4782 const SCEV *Result = Rewriter.visit(S); 4783 return Rewriter.hasSeenLoopVariantSCEVUnknown() 4784 ? SE.getCouldNotCompute() 4785 : Result; 4786 } 4787 4788 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 4789 if (!SE.isLoopInvariant(Expr, L)) 4790 SeenLoopVariantSCEVUnknown = true; 4791 return Expr; 4792 } 4793 4794 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { 4795 // Only re-write AddRecExprs for this loop. 4796 if (Expr->getLoop() == L) 4797 return Expr->getPostIncExpr(SE); 4798 SeenOtherLoops = true; 4799 return Expr; 4800 } 4801 4802 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; } 4803 4804 bool hasSeenOtherLoops() { return SeenOtherLoops; } 4805 4806 private: 4807 explicit SCEVPostIncRewriter(const Loop *L, ScalarEvolution &SE) 4808 : SCEVRewriteVisitor(SE), L(L) {} 4809 4810 const Loop *L; 4811 bool SeenLoopVariantSCEVUnknown = false; 4812 bool SeenOtherLoops = false; 4813 }; 4814 4815 /// This class evaluates the compare condition by matching it against the 4816 /// condition of loop latch. If there is a match we assume a true value 4817 /// for the condition while building SCEV nodes. 4818 class SCEVBackedgeConditionFolder 4819 : public SCEVRewriteVisitor<SCEVBackedgeConditionFolder> { 4820 public: 4821 static const SCEV *rewrite(const SCEV *S, const Loop *L, 4822 ScalarEvolution &SE) { 4823 bool IsPosBECond = false; 4824 Value *BECond = nullptr; 4825 if (BasicBlock *Latch = L->getLoopLatch()) { 4826 BranchInst *BI = dyn_cast<BranchInst>(Latch->getTerminator()); 4827 if (BI && BI->isConditional()) { 4828 assert(BI->getSuccessor(0) != BI->getSuccessor(1) && 4829 "Both outgoing branches should not target same header!"); 4830 BECond = BI->getCondition(); 4831 IsPosBECond = BI->getSuccessor(0) == L->getHeader(); 4832 } else { 4833 return S; 4834 } 4835 } 4836 SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE); 4837 return Rewriter.visit(S); 4838 } 4839 4840 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 4841 const SCEV *Result = Expr; 4842 bool InvariantF = SE.isLoopInvariant(Expr, L); 4843 4844 if (!InvariantF) { 4845 Instruction *I = cast<Instruction>(Expr->getValue()); 4846 switch (I->getOpcode()) { 4847 case Instruction::Select: { 4848 SelectInst *SI = cast<SelectInst>(I); 4849 Optional<const SCEV *> Res = 4850 compareWithBackedgeCondition(SI->getCondition()); 4851 if (Res.hasValue()) { 4852 bool IsOne = cast<SCEVConstant>(Res.getValue())->getValue()->isOne(); 4853 Result = SE.getSCEV(IsOne ? SI->getTrueValue() : SI->getFalseValue()); 4854 } 4855 break; 4856 } 4857 default: { 4858 Optional<const SCEV *> Res = compareWithBackedgeCondition(I); 4859 if (Res.hasValue()) 4860 Result = Res.getValue(); 4861 break; 4862 } 4863 } 4864 } 4865 return Result; 4866 } 4867 4868 private: 4869 explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond, 4870 bool IsPosBECond, ScalarEvolution &SE) 4871 : SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond), 4872 IsPositiveBECond(IsPosBECond) {} 4873 4874 Optional<const SCEV *> compareWithBackedgeCondition(Value *IC); 4875 4876 const Loop *L; 4877 /// Loop back condition. 4878 Value *BackedgeCond = nullptr; 4879 /// Set to true if loop back is on positive branch condition. 4880 bool IsPositiveBECond; 4881 }; 4882 4883 Optional<const SCEV *> 4884 SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) { 4885 4886 // If value matches the backedge condition for loop latch, 4887 // then return a constant evolution node based on loopback 4888 // branch taken. 4889 if (BackedgeCond == IC) 4890 return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext())) 4891 : SE.getZero(Type::getInt1Ty(SE.getContext())); 4892 return None; 4893 } 4894 4895 class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> { 4896 public: 4897 static const SCEV *rewrite(const SCEV *S, const Loop *L, 4898 ScalarEvolution &SE) { 4899 SCEVShiftRewriter Rewriter(L, SE); 4900 const SCEV *Result = Rewriter.visit(S); 4901 return Rewriter.isValid() ? Result : SE.getCouldNotCompute(); 4902 } 4903 4904 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 4905 // Only allow AddRecExprs for this loop. 4906 if (!SE.isLoopInvariant(Expr, L)) 4907 Valid = false; 4908 return Expr; 4909 } 4910 4911 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { 4912 if (Expr->getLoop() == L && Expr->isAffine()) 4913 return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE)); 4914 Valid = false; 4915 return Expr; 4916 } 4917 4918 bool isValid() { return Valid; } 4919 4920 private: 4921 explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE) 4922 : SCEVRewriteVisitor(SE), L(L) {} 4923 4924 const Loop *L; 4925 bool Valid = true; 4926 }; 4927 4928 } // end anonymous namespace 4929 4930 SCEV::NoWrapFlags 4931 ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) { 4932 if (!AR->isAffine()) 4933 return SCEV::FlagAnyWrap; 4934 4935 using OBO = OverflowingBinaryOperator; 4936 4937 SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap; 4938 4939 if (!AR->hasNoSignedWrap()) { 4940 ConstantRange AddRecRange = getSignedRange(AR); 4941 ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this)); 4942 4943 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 4944 Instruction::Add, IncRange, OBO::NoSignedWrap); 4945 if (NSWRegion.contains(AddRecRange)) 4946 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW); 4947 } 4948 4949 if (!AR->hasNoUnsignedWrap()) { 4950 ConstantRange AddRecRange = getUnsignedRange(AR); 4951 ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this)); 4952 4953 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 4954 Instruction::Add, IncRange, OBO::NoUnsignedWrap); 4955 if (NUWRegion.contains(AddRecRange)) 4956 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW); 4957 } 4958 4959 return Result; 4960 } 4961 4962 SCEV::NoWrapFlags 4963 ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) { 4964 SCEV::NoWrapFlags Result = AR->getNoWrapFlags(); 4965 4966 if (AR->hasNoSignedWrap()) 4967 return Result; 4968 4969 if (!AR->isAffine()) 4970 return Result; 4971 4972 const SCEV *Step = AR->getStepRecurrence(*this); 4973 const Loop *L = AR->getLoop(); 4974 4975 // Check whether the backedge-taken count is SCEVCouldNotCompute. 4976 // Note that this serves two purposes: It filters out loops that are 4977 // simply not analyzable, and it covers the case where this code is 4978 // being called from within backedge-taken count analysis, such that 4979 // attempting to ask for the backedge-taken count would likely result 4980 // in infinite recursion. In the later case, the analysis code will 4981 // cope with a conservative value, and it will take care to purge 4982 // that value once it has finished. 4983 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); 4984 4985 // Normally, in the cases we can prove no-overflow via a 4986 // backedge guarding condition, we can also compute a backedge 4987 // taken count for the loop. The exceptions are assumptions and 4988 // guards present in the loop -- SCEV is not great at exploiting 4989 // these to compute max backedge taken counts, but can still use 4990 // these to prove lack of overflow. Use this fact to avoid 4991 // doing extra work that may not pay off. 4992 4993 if (isa<SCEVCouldNotCompute>(MaxBECount) && !HasGuards && 4994 AC.assumptions().empty()) 4995 return Result; 4996 4997 // If the backedge is guarded by a comparison with the pre-inc value the 4998 // addrec is safe. Also, if the entry is guarded by a comparison with the 4999 // start value and the backedge is guarded by a comparison with the post-inc 5000 // value, the addrec is safe. 5001 ICmpInst::Predicate Pred; 5002 const SCEV *OverflowLimit = 5003 getSignedOverflowLimitForStep(Step, &Pred, this); 5004 if (OverflowLimit && 5005 (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) || 5006 isKnownOnEveryIteration(Pred, AR, OverflowLimit))) { 5007 Result = setFlags(Result, SCEV::FlagNSW); 5008 } 5009 return Result; 5010 } 5011 SCEV::NoWrapFlags 5012 ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) { 5013 SCEV::NoWrapFlags Result = AR->getNoWrapFlags(); 5014 5015 if (AR->hasNoUnsignedWrap()) 5016 return Result; 5017 5018 if (!AR->isAffine()) 5019 return Result; 5020 5021 const SCEV *Step = AR->getStepRecurrence(*this); 5022 unsigned BitWidth = getTypeSizeInBits(AR->getType()); 5023 const Loop *L = AR->getLoop(); 5024 5025 // Check whether the backedge-taken count is SCEVCouldNotCompute. 5026 // Note that this serves two purposes: It filters out loops that are 5027 // simply not analyzable, and it covers the case where this code is 5028 // being called from within backedge-taken count analysis, such that 5029 // attempting to ask for the backedge-taken count would likely result 5030 // in infinite recursion. In the later case, the analysis code will 5031 // cope with a conservative value, and it will take care to purge 5032 // that value once it has finished. 5033 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L); 5034 5035 // Normally, in the cases we can prove no-overflow via a 5036 // backedge guarding condition, we can also compute a backedge 5037 // taken count for the loop. The exceptions are assumptions and 5038 // guards present in the loop -- SCEV is not great at exploiting 5039 // these to compute max backedge taken counts, but can still use 5040 // these to prove lack of overflow. Use this fact to avoid 5041 // doing extra work that may not pay off. 5042 5043 if (isa<SCEVCouldNotCompute>(MaxBECount) && !HasGuards && 5044 AC.assumptions().empty()) 5045 return Result; 5046 5047 // If the backedge is guarded by a comparison with the pre-inc value the 5048 // addrec is safe. Also, if the entry is guarded by a comparison with the 5049 // start value and the backedge is guarded by a comparison with the post-inc 5050 // value, the addrec is safe. 5051 if (isKnownPositive(Step)) { 5052 const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - 5053 getUnsignedRangeMax(Step)); 5054 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || 5055 isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR, N)) { 5056 Result = setFlags(Result, SCEV::FlagNUW); 5057 } 5058 } 5059 5060 return Result; 5061 } 5062 5063 namespace { 5064 5065 /// Represents an abstract binary operation. This may exist as a 5066 /// normal instruction or constant expression, or may have been 5067 /// derived from an expression tree. 5068 struct BinaryOp { 5069 unsigned Opcode; 5070 Value *LHS; 5071 Value *RHS; 5072 bool IsNSW = false; 5073 bool IsNUW = false; 5074 5075 /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or 5076 /// constant expression. 5077 Operator *Op = nullptr; 5078 5079 explicit BinaryOp(Operator *Op) 5080 : Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)), 5081 Op(Op) { 5082 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) { 5083 IsNSW = OBO->hasNoSignedWrap(); 5084 IsNUW = OBO->hasNoUnsignedWrap(); 5085 } 5086 } 5087 5088 explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false, 5089 bool IsNUW = false) 5090 : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {} 5091 }; 5092 5093 } // end anonymous namespace 5094 5095 /// Try to map \p V into a BinaryOp, and return \c None on failure. 5096 static Optional<BinaryOp> MatchBinaryOp(Value *V, DominatorTree &DT) { 5097 auto *Op = dyn_cast<Operator>(V); 5098 if (!Op) 5099 return None; 5100 5101 // Implementation detail: all the cleverness here should happen without 5102 // creating new SCEV expressions -- our caller knowns tricks to avoid creating 5103 // SCEV expressions when possible, and we should not break that. 5104 5105 switch (Op->getOpcode()) { 5106 case Instruction::Add: 5107 case Instruction::Sub: 5108 case Instruction::Mul: 5109 case Instruction::UDiv: 5110 case Instruction::URem: 5111 case Instruction::And: 5112 case Instruction::Or: 5113 case Instruction::AShr: 5114 case Instruction::Shl: 5115 return BinaryOp(Op); 5116 5117 case Instruction::Xor: 5118 if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1))) 5119 // If the RHS of the xor is a signmask, then this is just an add. 5120 // Instcombine turns add of signmask into xor as a strength reduction step. 5121 if (RHSC->getValue().isSignMask()) 5122 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1)); 5123 // Binary `xor` is a bit-wise `add`. 5124 if (V->getType()->isIntegerTy(1)) 5125 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1)); 5126 return BinaryOp(Op); 5127 5128 case Instruction::LShr: 5129 // Turn logical shift right of a constant into a unsigned divide. 5130 if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) { 5131 uint32_t BitWidth = cast<IntegerType>(Op->getType())->getBitWidth(); 5132 5133 // If the shift count is not less than the bitwidth, the result of 5134 // the shift is undefined. Don't try to analyze it, because the 5135 // resolution chosen here may differ from the resolution chosen in 5136 // other parts of the compiler. 5137 if (SA->getValue().ult(BitWidth)) { 5138 Constant *X = 5139 ConstantInt::get(SA->getContext(), 5140 APInt::getOneBitSet(BitWidth, SA->getZExtValue())); 5141 return BinaryOp(Instruction::UDiv, Op->getOperand(0), X); 5142 } 5143 } 5144 return BinaryOp(Op); 5145 5146 case Instruction::ExtractValue: { 5147 auto *EVI = cast<ExtractValueInst>(Op); 5148 if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0) 5149 break; 5150 5151 auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand()); 5152 if (!WO) 5153 break; 5154 5155 Instruction::BinaryOps BinOp = WO->getBinaryOp(); 5156 bool Signed = WO->isSigned(); 5157 // TODO: Should add nuw/nsw flags for mul as well. 5158 if (BinOp == Instruction::Mul || !isOverflowIntrinsicNoWrap(WO, DT)) 5159 return BinaryOp(BinOp, WO->getLHS(), WO->getRHS()); 5160 5161 // Now that we know that all uses of the arithmetic-result component of 5162 // CI are guarded by the overflow check, we can go ahead and pretend 5163 // that the arithmetic is non-overflowing. 5164 return BinaryOp(BinOp, WO->getLHS(), WO->getRHS(), 5165 /* IsNSW = */ Signed, /* IsNUW = */ !Signed); 5166 } 5167 5168 default: 5169 break; 5170 } 5171 5172 // Recognise intrinsic loop.decrement.reg, and as this has exactly the same 5173 // semantics as a Sub, return a binary sub expression. 5174 if (auto *II = dyn_cast<IntrinsicInst>(V)) 5175 if (II->getIntrinsicID() == Intrinsic::loop_decrement_reg) 5176 return BinaryOp(Instruction::Sub, II->getOperand(0), II->getOperand(1)); 5177 5178 return None; 5179 } 5180 5181 /// Helper function to createAddRecFromPHIWithCasts. We have a phi 5182 /// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via 5183 /// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the 5184 /// way. This function checks if \p Op, an operand of this SCEVAddExpr, 5185 /// follows one of the following patterns: 5186 /// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) 5187 /// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) 5188 /// If the SCEV expression of \p Op conforms with one of the expected patterns 5189 /// we return the type of the truncation operation, and indicate whether the 5190 /// truncated type should be treated as signed/unsigned by setting 5191 /// \p Signed to true/false, respectively. 5192 static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI, 5193 bool &Signed, ScalarEvolution &SE) { 5194 // The case where Op == SymbolicPHI (that is, with no type conversions on 5195 // the way) is handled by the regular add recurrence creating logic and 5196 // would have already been triggered in createAddRecForPHI. Reaching it here 5197 // means that createAddRecFromPHI had failed for this PHI before (e.g., 5198 // because one of the other operands of the SCEVAddExpr updating this PHI is 5199 // not invariant). 5200 // 5201 // Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in 5202 // this case predicates that allow us to prove that Op == SymbolicPHI will 5203 // be added. 5204 if (Op == SymbolicPHI) 5205 return nullptr; 5206 5207 unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType()); 5208 unsigned NewBits = SE.getTypeSizeInBits(Op->getType()); 5209 if (SourceBits != NewBits) 5210 return nullptr; 5211 5212 const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Op); 5213 const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Op); 5214 if (!SExt && !ZExt) 5215 return nullptr; 5216 const SCEVTruncateExpr *Trunc = 5217 SExt ? dyn_cast<SCEVTruncateExpr>(SExt->getOperand()) 5218 : dyn_cast<SCEVTruncateExpr>(ZExt->getOperand()); 5219 if (!Trunc) 5220 return nullptr; 5221 const SCEV *X = Trunc->getOperand(); 5222 if (X != SymbolicPHI) 5223 return nullptr; 5224 Signed = SExt != nullptr; 5225 return Trunc->getType(); 5226 } 5227 5228 static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) { 5229 if (!PN->getType()->isIntegerTy()) 5230 return nullptr; 5231 const Loop *L = LI.getLoopFor(PN->getParent()); 5232 if (!L || L->getHeader() != PN->getParent()) 5233 return nullptr; 5234 return L; 5235 } 5236 5237 // Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the 5238 // computation that updates the phi follows the following pattern: 5239 // (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum 5240 // which correspond to a phi->trunc->sext/zext->add->phi update chain. 5241 // If so, try to see if it can be rewritten as an AddRecExpr under some 5242 // Predicates. If successful, return them as a pair. Also cache the results 5243 // of the analysis. 5244 // 5245 // Example usage scenario: 5246 // Say the Rewriter is called for the following SCEV: 5247 // 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step) 5248 // where: 5249 // %X = phi i64 (%Start, %BEValue) 5250 // It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X), 5251 // and call this function with %SymbolicPHI = %X. 5252 // 5253 // The analysis will find that the value coming around the backedge has 5254 // the following SCEV: 5255 // BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step) 5256 // Upon concluding that this matches the desired pattern, the function 5257 // will return the pair {NewAddRec, SmallPredsVec} where: 5258 // NewAddRec = {%Start,+,%Step} 5259 // SmallPredsVec = {P1, P2, P3} as follows: 5260 // P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw> 5261 // P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64) 5262 // P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64) 5263 // The returned pair means that SymbolicPHI can be rewritten into NewAddRec 5264 // under the predicates {P1,P2,P3}. 5265 // This predicated rewrite will be cached in PredicatedSCEVRewrites: 5266 // PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)} 5267 // 5268 // TODO's: 5269 // 5270 // 1) Extend the Induction descriptor to also support inductions that involve 5271 // casts: When needed (namely, when we are called in the context of the 5272 // vectorizer induction analysis), a Set of cast instructions will be 5273 // populated by this method, and provided back to isInductionPHI. This is 5274 // needed to allow the vectorizer to properly record them to be ignored by 5275 // the cost model and to avoid vectorizing them (otherwise these casts, 5276 // which are redundant under the runtime overflow checks, will be 5277 // vectorized, which can be costly). 5278 // 5279 // 2) Support additional induction/PHISCEV patterns: We also want to support 5280 // inductions where the sext-trunc / zext-trunc operations (partly) occur 5281 // after the induction update operation (the induction increment): 5282 // 5283 // (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix) 5284 // which correspond to a phi->add->trunc->sext/zext->phi update chain. 5285 // 5286 // (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix) 5287 // which correspond to a phi->trunc->add->sext/zext->phi update chain. 5288 // 5289 // 3) Outline common code with createAddRecFromPHI to avoid duplication. 5290 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>> 5291 ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) { 5292 SmallVector<const SCEVPredicate *, 3> Predicates; 5293 5294 // *** Part1: Analyze if we have a phi-with-cast pattern for which we can 5295 // return an AddRec expression under some predicate. 5296 5297 auto *PN = cast<PHINode>(SymbolicPHI->getValue()); 5298 const Loop *L = isIntegerLoopHeaderPHI(PN, LI); 5299 assert(L && "Expecting an integer loop header phi"); 5300 5301 // The loop may have multiple entrances or multiple exits; we can analyze 5302 // this phi as an addrec if it has a unique entry value and a unique 5303 // backedge value. 5304 Value *BEValueV = nullptr, *StartValueV = nullptr; 5305 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 5306 Value *V = PN->getIncomingValue(i); 5307 if (L->contains(PN->getIncomingBlock(i))) { 5308 if (!BEValueV) { 5309 BEValueV = V; 5310 } else if (BEValueV != V) { 5311 BEValueV = nullptr; 5312 break; 5313 } 5314 } else if (!StartValueV) { 5315 StartValueV = V; 5316 } else if (StartValueV != V) { 5317 StartValueV = nullptr; 5318 break; 5319 } 5320 } 5321 if (!BEValueV || !StartValueV) 5322 return None; 5323 5324 const SCEV *BEValue = getSCEV(BEValueV); 5325 5326 // If the value coming around the backedge is an add with the symbolic 5327 // value we just inserted, possibly with casts that we can ignore under 5328 // an appropriate runtime guard, then we found a simple induction variable! 5329 const auto *Add = dyn_cast<SCEVAddExpr>(BEValue); 5330 if (!Add) 5331 return None; 5332 5333 // If there is a single occurrence of the symbolic value, possibly 5334 // casted, replace it with a recurrence. 5335 unsigned FoundIndex = Add->getNumOperands(); 5336 Type *TruncTy = nullptr; 5337 bool Signed; 5338 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) 5339 if ((TruncTy = 5340 isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this))) 5341 if (FoundIndex == e) { 5342 FoundIndex = i; 5343 break; 5344 } 5345 5346 if (FoundIndex == Add->getNumOperands()) 5347 return None; 5348 5349 // Create an add with everything but the specified operand. 5350 SmallVector<const SCEV *, 8> Ops; 5351 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) 5352 if (i != FoundIndex) 5353 Ops.push_back(Add->getOperand(i)); 5354 const SCEV *Accum = getAddExpr(Ops); 5355 5356 // The runtime checks will not be valid if the step amount is 5357 // varying inside the loop. 5358 if (!isLoopInvariant(Accum, L)) 5359 return None; 5360 5361 // *** Part2: Create the predicates 5362 5363 // Analysis was successful: we have a phi-with-cast pattern for which we 5364 // can return an AddRec expression under the following predicates: 5365 // 5366 // P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum) 5367 // fits within the truncated type (does not overflow) for i = 0 to n-1. 5368 // P2: An Equal predicate that guarantees that 5369 // Start = (Ext ix (Trunc iy (Start) to ix) to iy) 5370 // P3: An Equal predicate that guarantees that 5371 // Accum = (Ext ix (Trunc iy (Accum) to ix) to iy) 5372 // 5373 // As we next prove, the above predicates guarantee that: 5374 // Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy) 5375 // 5376 // 5377 // More formally, we want to prove that: 5378 // Expr(i+1) = Start + (i+1) * Accum 5379 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum 5380 // 5381 // Given that: 5382 // 1) Expr(0) = Start 5383 // 2) Expr(1) = Start + Accum 5384 // = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2 5385 // 3) Induction hypothesis (step i): 5386 // Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum 5387 // 5388 // Proof: 5389 // Expr(i+1) = 5390 // = Start + (i+1)*Accum 5391 // = (Start + i*Accum) + Accum 5392 // = Expr(i) + Accum 5393 // = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum 5394 // :: from step i 5395 // 5396 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum 5397 // 5398 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) 5399 // + (Ext ix (Trunc iy (Accum) to ix) to iy) 5400 // + Accum :: from P3 5401 // 5402 // = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy) 5403 // + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y) 5404 // 5405 // = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum 5406 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum 5407 // 5408 // By induction, the same applies to all iterations 1<=i<n: 5409 // 5410 5411 // Create a truncated addrec for which we will add a no overflow check (P1). 5412 const SCEV *StartVal = getSCEV(StartValueV); 5413 const SCEV *PHISCEV = 5414 getAddRecExpr(getTruncateExpr(StartVal, TruncTy), 5415 getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap); 5416 5417 // PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr. 5418 // ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV 5419 // will be constant. 5420 // 5421 // If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't 5422 // add P1. 5423 if (const auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) { 5424 SCEVWrapPredicate::IncrementWrapFlags AddedFlags = 5425 Signed ? SCEVWrapPredicate::IncrementNSSW 5426 : SCEVWrapPredicate::IncrementNUSW; 5427 const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags); 5428 Predicates.push_back(AddRecPred); 5429 } 5430 5431 // Create the Equal Predicates P2,P3: 5432 5433 // It is possible that the predicates P2 and/or P3 are computable at 5434 // compile time due to StartVal and/or Accum being constants. 5435 // If either one is, then we can check that now and escape if either P2 5436 // or P3 is false. 5437 5438 // Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy) 5439 // for each of StartVal and Accum 5440 auto getExtendedExpr = [&](const SCEV *Expr, 5441 bool CreateSignExtend) -> const SCEV * { 5442 assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant"); 5443 const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy); 5444 const SCEV *ExtendedExpr = 5445 CreateSignExtend ? getSignExtendExpr(TruncatedExpr, Expr->getType()) 5446 : getZeroExtendExpr(TruncatedExpr, Expr->getType()); 5447 return ExtendedExpr; 5448 }; 5449 5450 // Given: 5451 // ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy 5452 // = getExtendedExpr(Expr) 5453 // Determine whether the predicate P: Expr == ExtendedExpr 5454 // is known to be false at compile time 5455 auto PredIsKnownFalse = [&](const SCEV *Expr, 5456 const SCEV *ExtendedExpr) -> bool { 5457 return Expr != ExtendedExpr && 5458 isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr); 5459 }; 5460 5461 const SCEV *StartExtended = getExtendedExpr(StartVal, Signed); 5462 if (PredIsKnownFalse(StartVal, StartExtended)) { 5463 LLVM_DEBUG(dbgs() << "P2 is compile-time false\n";); 5464 return None; 5465 } 5466 5467 // The Step is always Signed (because the overflow checks are either 5468 // NSSW or NUSW) 5469 const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true); 5470 if (PredIsKnownFalse(Accum, AccumExtended)) { 5471 LLVM_DEBUG(dbgs() << "P3 is compile-time false\n";); 5472 return None; 5473 } 5474 5475 auto AppendPredicate = [&](const SCEV *Expr, 5476 const SCEV *ExtendedExpr) -> void { 5477 if (Expr != ExtendedExpr && 5478 !isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) { 5479 const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr); 5480 LLVM_DEBUG(dbgs() << "Added Predicate: " << *Pred); 5481 Predicates.push_back(Pred); 5482 } 5483 }; 5484 5485 AppendPredicate(StartVal, StartExtended); 5486 AppendPredicate(Accum, AccumExtended); 5487 5488 // *** Part3: Predicates are ready. Now go ahead and create the new addrec in 5489 // which the casts had been folded away. The caller can rewrite SymbolicPHI 5490 // into NewAR if it will also add the runtime overflow checks specified in 5491 // Predicates. 5492 auto *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap); 5493 5494 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite = 5495 std::make_pair(NewAR, Predicates); 5496 // Remember the result of the analysis for this SCEV at this locayyytion. 5497 PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite; 5498 return PredRewrite; 5499 } 5500 5501 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>> 5502 ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) { 5503 auto *PN = cast<PHINode>(SymbolicPHI->getValue()); 5504 const Loop *L = isIntegerLoopHeaderPHI(PN, LI); 5505 if (!L) 5506 return None; 5507 5508 // Check to see if we already analyzed this PHI. 5509 auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L}); 5510 if (I != PredicatedSCEVRewrites.end()) { 5511 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite = 5512 I->second; 5513 // Analysis was done before and failed to create an AddRec: 5514 if (Rewrite.first == SymbolicPHI) 5515 return None; 5516 // Analysis was done before and succeeded to create an AddRec under 5517 // a predicate: 5518 assert(isa<SCEVAddRecExpr>(Rewrite.first) && "Expected an AddRec"); 5519 assert(!(Rewrite.second).empty() && "Expected to find Predicates"); 5520 return Rewrite; 5521 } 5522 5523 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>> 5524 Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI); 5525 5526 // Record in the cache that the analysis failed 5527 if (!Rewrite) { 5528 SmallVector<const SCEVPredicate *, 3> Predicates; 5529 PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates}; 5530 return None; 5531 } 5532 5533 return Rewrite; 5534 } 5535 5536 // FIXME: This utility is currently required because the Rewriter currently 5537 // does not rewrite this expression: 5538 // {0, +, (sext ix (trunc iy to ix) to iy)} 5539 // into {0, +, %step}, 5540 // even when the following Equal predicate exists: 5541 // "%step == (sext ix (trunc iy to ix) to iy)". 5542 bool PredicatedScalarEvolution::areAddRecsEqualWithPreds( 5543 const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const { 5544 if (AR1 == AR2) 5545 return true; 5546 5547 auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool { 5548 if (Expr1 != Expr2 && !Preds->implies(SE.getEqualPredicate(Expr1, Expr2)) && 5549 !Preds->implies(SE.getEqualPredicate(Expr2, Expr1))) 5550 return false; 5551 return true; 5552 }; 5553 5554 if (!areExprsEqual(AR1->getStart(), AR2->getStart()) || 5555 !areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE))) 5556 return false; 5557 return true; 5558 } 5559 5560 /// A helper function for createAddRecFromPHI to handle simple cases. 5561 /// 5562 /// This function tries to find an AddRec expression for the simplest (yet most 5563 /// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)). 5564 /// If it fails, createAddRecFromPHI will use a more general, but slow, 5565 /// technique for finding the AddRec expression. 5566 const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN, 5567 Value *BEValueV, 5568 Value *StartValueV) { 5569 const Loop *L = LI.getLoopFor(PN->getParent()); 5570 assert(L && L->getHeader() == PN->getParent()); 5571 assert(BEValueV && StartValueV); 5572 5573 auto BO = MatchBinaryOp(BEValueV, DT); 5574 if (!BO) 5575 return nullptr; 5576 5577 if (BO->Opcode != Instruction::Add) 5578 return nullptr; 5579 5580 const SCEV *Accum = nullptr; 5581 if (BO->LHS == PN && L->isLoopInvariant(BO->RHS)) 5582 Accum = getSCEV(BO->RHS); 5583 else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS)) 5584 Accum = getSCEV(BO->LHS); 5585 5586 if (!Accum) 5587 return nullptr; 5588 5589 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; 5590 if (BO->IsNUW) 5591 Flags = setFlags(Flags, SCEV::FlagNUW); 5592 if (BO->IsNSW) 5593 Flags = setFlags(Flags, SCEV::FlagNSW); 5594 5595 const SCEV *StartVal = getSCEV(StartValueV); 5596 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags); 5597 insertValueToMap(PN, PHISCEV); 5598 5599 // We can add Flags to the post-inc expression only if we 5600 // know that it is *undefined behavior* for BEValueV to 5601 // overflow. 5602 if (auto *BEInst = dyn_cast<Instruction>(BEValueV)) { 5603 assert(isLoopInvariant(Accum, L) && 5604 "Accum is defined outside L, but is not invariant?"); 5605 if (isAddRecNeverPoison(BEInst, L)) 5606 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags); 5607 } 5608 5609 return PHISCEV; 5610 } 5611 5612 const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) { 5613 const Loop *L = LI.getLoopFor(PN->getParent()); 5614 if (!L || L->getHeader() != PN->getParent()) 5615 return nullptr; 5616 5617 // The loop may have multiple entrances or multiple exits; we can analyze 5618 // this phi as an addrec if it has a unique entry value and a unique 5619 // backedge value. 5620 Value *BEValueV = nullptr, *StartValueV = nullptr; 5621 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 5622 Value *V = PN->getIncomingValue(i); 5623 if (L->contains(PN->getIncomingBlock(i))) { 5624 if (!BEValueV) { 5625 BEValueV = V; 5626 } else if (BEValueV != V) { 5627 BEValueV = nullptr; 5628 break; 5629 } 5630 } else if (!StartValueV) { 5631 StartValueV = V; 5632 } else if (StartValueV != V) { 5633 StartValueV = nullptr; 5634 break; 5635 } 5636 } 5637 if (!BEValueV || !StartValueV) 5638 return nullptr; 5639 5640 assert(ValueExprMap.find_as(PN) == ValueExprMap.end() && 5641 "PHI node already processed?"); 5642 5643 // First, try to find AddRec expression without creating a fictituos symbolic 5644 // value for PN. 5645 if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV)) 5646 return S; 5647 5648 // Handle PHI node value symbolically. 5649 const SCEV *SymbolicName = getUnknown(PN); 5650 insertValueToMap(PN, SymbolicName); 5651 5652 // Using this symbolic name for the PHI, analyze the value coming around 5653 // the back-edge. 5654 const SCEV *BEValue = getSCEV(BEValueV); 5655 5656 // NOTE: If BEValue is loop invariant, we know that the PHI node just 5657 // has a special value for the first iteration of the loop. 5658 5659 // If the value coming around the backedge is an add with the symbolic 5660 // value we just inserted, then we found a simple induction variable! 5661 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { 5662 // If there is a single occurrence of the symbolic value, replace it 5663 // with a recurrence. 5664 unsigned FoundIndex = Add->getNumOperands(); 5665 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) 5666 if (Add->getOperand(i) == SymbolicName) 5667 if (FoundIndex == e) { 5668 FoundIndex = i; 5669 break; 5670 } 5671 5672 if (FoundIndex != Add->getNumOperands()) { 5673 // Create an add with everything but the specified operand. 5674 SmallVector<const SCEV *, 8> Ops; 5675 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) 5676 if (i != FoundIndex) 5677 Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i), 5678 L, *this)); 5679 const SCEV *Accum = getAddExpr(Ops); 5680 5681 // This is not a valid addrec if the step amount is varying each 5682 // loop iteration, but is not itself an addrec in this loop. 5683 if (isLoopInvariant(Accum, L) || 5684 (isa<SCEVAddRecExpr>(Accum) && 5685 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { 5686 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; 5687 5688 if (auto BO = MatchBinaryOp(BEValueV, DT)) { 5689 if (BO->Opcode == Instruction::Add && BO->LHS == PN) { 5690 if (BO->IsNUW) 5691 Flags = setFlags(Flags, SCEV::FlagNUW); 5692 if (BO->IsNSW) 5693 Flags = setFlags(Flags, SCEV::FlagNSW); 5694 } 5695 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) { 5696 // If the increment is an inbounds GEP, then we know the address 5697 // space cannot be wrapped around. We cannot make any guarantee 5698 // about signed or unsigned overflow because pointers are 5699 // unsigned but we may have a negative index from the base 5700 // pointer. We can guarantee that no unsigned wrap occurs if the 5701 // indices form a positive value. 5702 if (GEP->isInBounds() && GEP->getOperand(0) == PN) { 5703 Flags = setFlags(Flags, SCEV::FlagNW); 5704 5705 const SCEV *Ptr = getSCEV(GEP->getPointerOperand()); 5706 if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr))) 5707 Flags = setFlags(Flags, SCEV::FlagNUW); 5708 } 5709 5710 // We cannot transfer nuw and nsw flags from subtraction 5711 // operations -- sub nuw X, Y is not the same as add nuw X, -Y 5712 // for instance. 5713 } 5714 5715 const SCEV *StartVal = getSCEV(StartValueV); 5716 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags); 5717 5718 // Okay, for the entire analysis of this edge we assumed the PHI 5719 // to be symbolic. We now need to go back and purge all of the 5720 // entries for the scalars that use the symbolic expression. 5721 forgetMemoizedResults(SymbolicName); 5722 insertValueToMap(PN, PHISCEV); 5723 5724 // We can add Flags to the post-inc expression only if we 5725 // know that it is *undefined behavior* for BEValueV to 5726 // overflow. 5727 if (auto *BEInst = dyn_cast<Instruction>(BEValueV)) 5728 if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L)) 5729 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags); 5730 5731 return PHISCEV; 5732 } 5733 } 5734 } else { 5735 // Otherwise, this could be a loop like this: 5736 // i = 0; for (j = 1; ..; ++j) { .... i = j; } 5737 // In this case, j = {1,+,1} and BEValue is j. 5738 // Because the other in-value of i (0) fits the evolution of BEValue 5739 // i really is an addrec evolution. 5740 // 5741 // We can generalize this saying that i is the shifted value of BEValue 5742 // by one iteration: 5743 // PHI(f(0), f({1,+,1})) --> f({0,+,1}) 5744 const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this); 5745 const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false); 5746 if (Shifted != getCouldNotCompute() && 5747 Start != getCouldNotCompute()) { 5748 const SCEV *StartVal = getSCEV(StartValueV); 5749 if (Start == StartVal) { 5750 // Okay, for the entire analysis of this edge we assumed the PHI 5751 // to be symbolic. We now need to go back and purge all of the 5752 // entries for the scalars that use the symbolic expression. 5753 forgetMemoizedResults(SymbolicName); 5754 insertValueToMap(PN, Shifted); 5755 return Shifted; 5756 } 5757 } 5758 } 5759 5760 // Remove the temporary PHI node SCEV that has been inserted while intending 5761 // to create an AddRecExpr for this PHI node. We can not keep this temporary 5762 // as it will prevent later (possibly simpler) SCEV expressions to be added 5763 // to the ValueExprMap. 5764 eraseValueFromMap(PN); 5765 5766 return nullptr; 5767 } 5768 5769 // Checks if the SCEV S is available at BB. S is considered available at BB 5770 // if S can be materialized at BB without introducing a fault. 5771 static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S, 5772 BasicBlock *BB) { 5773 struct CheckAvailable { 5774 bool TraversalDone = false; 5775 bool Available = true; 5776 5777 const Loop *L = nullptr; // The loop BB is in (can be nullptr) 5778 BasicBlock *BB = nullptr; 5779 DominatorTree &DT; 5780 5781 CheckAvailable(const Loop *L, BasicBlock *BB, DominatorTree &DT) 5782 : L(L), BB(BB), DT(DT) {} 5783 5784 bool setUnavailable() { 5785 TraversalDone = true; 5786 Available = false; 5787 return false; 5788 } 5789 5790 bool follow(const SCEV *S) { 5791 switch (S->getSCEVType()) { 5792 case scConstant: 5793 case scPtrToInt: 5794 case scTruncate: 5795 case scZeroExtend: 5796 case scSignExtend: 5797 case scAddExpr: 5798 case scMulExpr: 5799 case scUMaxExpr: 5800 case scSMaxExpr: 5801 case scUMinExpr: 5802 case scSMinExpr: 5803 case scSequentialUMinExpr: 5804 // These expressions are available if their operand(s) is/are. 5805 return true; 5806 5807 case scAddRecExpr: { 5808 // We allow add recurrences that are on the loop BB is in, or some 5809 // outer loop. This guarantees availability because the value of the 5810 // add recurrence at BB is simply the "current" value of the induction 5811 // variable. We can relax this in the future; for instance an add 5812 // recurrence on a sibling dominating loop is also available at BB. 5813 const auto *ARLoop = cast<SCEVAddRecExpr>(S)->getLoop(); 5814 if (L && (ARLoop == L || ARLoop->contains(L))) 5815 return true; 5816 5817 return setUnavailable(); 5818 } 5819 5820 case scUnknown: { 5821 // For SCEVUnknown, we check for simple dominance. 5822 const auto *SU = cast<SCEVUnknown>(S); 5823 Value *V = SU->getValue(); 5824 5825 if (isa<Argument>(V)) 5826 return false; 5827 5828 if (isa<Instruction>(V) && DT.dominates(cast<Instruction>(V), BB)) 5829 return false; 5830 5831 return setUnavailable(); 5832 } 5833 5834 case scUDivExpr: 5835 case scCouldNotCompute: 5836 // We do not try to smart about these at all. 5837 return setUnavailable(); 5838 } 5839 llvm_unreachable("Unknown SCEV kind!"); 5840 } 5841 5842 bool isDone() { return TraversalDone; } 5843 }; 5844 5845 CheckAvailable CA(L, BB, DT); 5846 SCEVTraversal<CheckAvailable> ST(CA); 5847 5848 ST.visitAll(S); 5849 return CA.Available; 5850 } 5851 5852 // Try to match a control flow sequence that branches out at BI and merges back 5853 // at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful 5854 // match. 5855 static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge, 5856 Value *&C, Value *&LHS, Value *&RHS) { 5857 C = BI->getCondition(); 5858 5859 BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0)); 5860 BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1)); 5861 5862 if (!LeftEdge.isSingleEdge()) 5863 return false; 5864 5865 assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()"); 5866 5867 Use &LeftUse = Merge->getOperandUse(0); 5868 Use &RightUse = Merge->getOperandUse(1); 5869 5870 if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) { 5871 LHS = LeftUse; 5872 RHS = RightUse; 5873 return true; 5874 } 5875 5876 if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) { 5877 LHS = RightUse; 5878 RHS = LeftUse; 5879 return true; 5880 } 5881 5882 return false; 5883 } 5884 5885 const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) { 5886 auto IsReachable = 5887 [&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); }; 5888 if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) { 5889 const Loop *L = LI.getLoopFor(PN->getParent()); 5890 5891 // We don't want to break LCSSA, even in a SCEV expression tree. 5892 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 5893 if (LI.getLoopFor(PN->getIncomingBlock(i)) != L) 5894 return nullptr; 5895 5896 // Try to match 5897 // 5898 // br %cond, label %left, label %right 5899 // left: 5900 // br label %merge 5901 // right: 5902 // br label %merge 5903 // merge: 5904 // V = phi [ %x, %left ], [ %y, %right ] 5905 // 5906 // as "select %cond, %x, %y" 5907 5908 BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock(); 5909 assert(IDom && "At least the entry block should dominate PN"); 5910 5911 auto *BI = dyn_cast<BranchInst>(IDom->getTerminator()); 5912 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr; 5913 5914 if (BI && BI->isConditional() && 5915 BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) && 5916 IsAvailableOnEntry(L, DT, getSCEV(LHS), PN->getParent()) && 5917 IsAvailableOnEntry(L, DT, getSCEV(RHS), PN->getParent())) 5918 return createNodeForSelectOrPHI(PN, Cond, LHS, RHS); 5919 } 5920 5921 return nullptr; 5922 } 5923 5924 const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { 5925 if (const SCEV *S = createAddRecFromPHI(PN)) 5926 return S; 5927 5928 if (const SCEV *S = createNodeFromSelectLikePHI(PN)) 5929 return S; 5930 5931 // If the PHI has a single incoming value, follow that value, unless the 5932 // PHI's incoming blocks are in a different loop, in which case doing so 5933 // risks breaking LCSSA form. Instcombine would normally zap these, but 5934 // it doesn't have DominatorTree information, so it may miss cases. 5935 if (Value *V = SimplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC})) 5936 if (LI.replacementPreservesLCSSAForm(PN, V)) 5937 return getSCEV(V); 5938 5939 // If it's not a loop phi, we can't handle it yet. 5940 return getUnknown(PN); 5941 } 5942 5943 bool SCEVMinMaxExprContains(const SCEV *Root, const SCEV *OperandToFind, 5944 SCEVTypes RootKind) { 5945 struct FindClosure { 5946 const SCEV *OperandToFind; 5947 const SCEVTypes RootKind; // Must be a sequential min/max expression. 5948 const SCEVTypes NonSequentialRootKind; // Non-seq variant of RootKind. 5949 5950 bool Found = false; 5951 5952 bool canRecurseInto(SCEVTypes Kind) const { 5953 // We can only recurse into the SCEV expression of the same effective type 5954 // as the type of our root SCEV expression, and into zero-extensions. 5955 return RootKind == Kind || NonSequentialRootKind == Kind || 5956 scZeroExtend == Kind; 5957 }; 5958 5959 FindClosure(const SCEV *OperandToFind, SCEVTypes RootKind) 5960 : OperandToFind(OperandToFind), RootKind(RootKind), 5961 NonSequentialRootKind( 5962 SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType( 5963 RootKind)) {} 5964 5965 bool follow(const SCEV *S) { 5966 Found = S == OperandToFind; 5967 5968 return !isDone() && canRecurseInto(S->getSCEVType()); 5969 } 5970 5971 bool isDone() const { return Found; } 5972 }; 5973 5974 FindClosure FC(OperandToFind, RootKind); 5975 visitAll(Root, FC); 5976 return FC.Found; 5977 } 5978 5979 const SCEV *ScalarEvolution::createNodeForSelectOrPHIInstWithICmpInstCond( 5980 Instruction *I, ICmpInst *Cond, Value *TrueVal, Value *FalseVal) { 5981 // Try to match some simple smax or umax patterns. 5982 auto *ICI = Cond; 5983 5984 Value *LHS = ICI->getOperand(0); 5985 Value *RHS = ICI->getOperand(1); 5986 5987 switch (ICI->getPredicate()) { 5988 case ICmpInst::ICMP_SLT: 5989 case ICmpInst::ICMP_SLE: 5990 case ICmpInst::ICMP_ULT: 5991 case ICmpInst::ICMP_ULE: 5992 std::swap(LHS, RHS); 5993 LLVM_FALLTHROUGH; 5994 case ICmpInst::ICMP_SGT: 5995 case ICmpInst::ICMP_SGE: 5996 case ICmpInst::ICMP_UGT: 5997 case ICmpInst::ICMP_UGE: 5998 // a > b ? a+x : b+x -> max(a, b)+x 5999 // a > b ? b+x : a+x -> min(a, b)+x 6000 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) { 6001 bool Signed = ICI->isSigned(); 6002 const SCEV *LA = getSCEV(TrueVal); 6003 const SCEV *RA = getSCEV(FalseVal); 6004 const SCEV *LS = getSCEV(LHS); 6005 const SCEV *RS = getSCEV(RHS); 6006 if (LA->getType()->isPointerTy()) { 6007 // FIXME: Handle cases where LS/RS are pointers not equal to LA/RA. 6008 // Need to make sure we can't produce weird expressions involving 6009 // negated pointers. 6010 if (LA == LS && RA == RS) 6011 return Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS); 6012 if (LA == RS && RA == LS) 6013 return Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS); 6014 } 6015 auto CoerceOperand = [&](const SCEV *Op) -> const SCEV * { 6016 if (Op->getType()->isPointerTy()) { 6017 Op = getLosslessPtrToIntExpr(Op); 6018 if (isa<SCEVCouldNotCompute>(Op)) 6019 return Op; 6020 } 6021 if (Signed) 6022 Op = getNoopOrSignExtend(Op, I->getType()); 6023 else 6024 Op = getNoopOrZeroExtend(Op, I->getType()); 6025 return Op; 6026 }; 6027 LS = CoerceOperand(LS); 6028 RS = CoerceOperand(RS); 6029 if (isa<SCEVCouldNotCompute>(LS) || isa<SCEVCouldNotCompute>(RS)) 6030 break; 6031 const SCEV *LDiff = getMinusSCEV(LA, LS); 6032 const SCEV *RDiff = getMinusSCEV(RA, RS); 6033 if (LDiff == RDiff) 6034 return getAddExpr(Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS), 6035 LDiff); 6036 LDiff = getMinusSCEV(LA, RS); 6037 RDiff = getMinusSCEV(RA, LS); 6038 if (LDiff == RDiff) 6039 return getAddExpr(Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS), 6040 LDiff); 6041 } 6042 break; 6043 case ICmpInst::ICMP_NE: 6044 // x != 0 ? x+y : C+y -> x == 0 ? C+y : x+y 6045 std::swap(TrueVal, FalseVal); 6046 LLVM_FALLTHROUGH; 6047 case ICmpInst::ICMP_EQ: 6048 // x == 0 ? C+y : x+y -> umax(x, C)+y iff C u<= 1 6049 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) && 6050 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) { 6051 const SCEV *X = getNoopOrZeroExtend(getSCEV(LHS), I->getType()); 6052 const SCEV *TrueValExpr = getSCEV(TrueVal); // C+y 6053 const SCEV *FalseValExpr = getSCEV(FalseVal); // x+y 6054 const SCEV *Y = getMinusSCEV(FalseValExpr, X); // y = (x+y)-x 6055 const SCEV *C = getMinusSCEV(TrueValExpr, Y); // C = (C+y)-y 6056 if (isa<SCEVConstant>(C) && cast<SCEVConstant>(C)->getAPInt().ule(1)) 6057 return getAddExpr(getUMaxExpr(X, C), Y); 6058 } 6059 // x == 0 ? 0 : umin (..., x, ...) -> umin_seq(x, umin (...)) 6060 // x == 0 ? 0 : umin_seq(..., x, ...) -> umin_seq(x, umin_seq(...)) 6061 // x == 0 ? 0 : umin (..., umin_seq(..., x, ...), ...) 6062 // -> umin_seq(x, umin (..., umin_seq(...), ...)) 6063 if (isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero() && 6064 isa<ConstantInt>(TrueVal) && cast<ConstantInt>(TrueVal)->isZero()) { 6065 const SCEV *X = getSCEV(LHS); 6066 while (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(X)) 6067 X = ZExt->getOperand(); 6068 if (getTypeSizeInBits(X->getType()) <= getTypeSizeInBits(I->getType())) { 6069 const SCEV *FalseValExpr = getSCEV(FalseVal); 6070 if (SCEVMinMaxExprContains(FalseValExpr, X, scSequentialUMinExpr)) 6071 return getUMinExpr(getNoopOrZeroExtend(X, I->getType()), FalseValExpr, 6072 /*Sequential=*/true); 6073 } 6074 } 6075 break; 6076 default: 6077 break; 6078 } 6079 6080 return getUnknown(I); 6081 } 6082 6083 static Optional<const SCEV *> 6084 createNodeForSelectViaUMinSeq(ScalarEvolution *SE, const SCEV *CondExpr, 6085 const SCEV *TrueExpr, const SCEV *FalseExpr) { 6086 assert(CondExpr->getType()->isIntegerTy(1) && 6087 TrueExpr->getType() == FalseExpr->getType() && 6088 TrueExpr->getType()->isIntegerTy(1) && 6089 "Unexpected operands of a select."); 6090 6091 // i1 cond ? i1 x : i1 C --> C + (i1 cond ? (i1 x - i1 C) : i1 0) 6092 // --> C + (umin_seq cond, x - C) 6093 // 6094 // i1 cond ? i1 C : i1 x --> C + (i1 cond ? i1 0 : (i1 x - i1 C)) 6095 // --> C + (i1 ~cond ? (i1 x - i1 C) : i1 0) 6096 // --> C + (umin_seq ~cond, x - C) 6097 6098 // FIXME: while we can't legally model the case where both of the hands 6099 // are fully variable, we only require that the *difference* is constant. 6100 if (!isa<SCEVConstant>(TrueExpr) && !isa<SCEVConstant>(FalseExpr)) 6101 return None; 6102 6103 const SCEV *X, *C; 6104 if (isa<SCEVConstant>(TrueExpr)) { 6105 CondExpr = SE->getNotSCEV(CondExpr); 6106 X = FalseExpr; 6107 C = TrueExpr; 6108 } else { 6109 X = TrueExpr; 6110 C = FalseExpr; 6111 } 6112 return SE->getAddExpr(C, SE->getUMinExpr(CondExpr, SE->getMinusSCEV(X, C), 6113 /*Sequential=*/true)); 6114 } 6115 6116 static Optional<const SCEV *> createNodeForSelectViaUMinSeq(ScalarEvolution *SE, 6117 Value *Cond, 6118 Value *TrueVal, 6119 Value *FalseVal) { 6120 if (!isa<ConstantInt>(TrueVal) && !isa<ConstantInt>(FalseVal)) 6121 return None; 6122 6123 const auto *SECond = SE->getSCEV(Cond); 6124 const auto *SETrue = SE->getSCEV(TrueVal); 6125 const auto *SEFalse = SE->getSCEV(FalseVal); 6126 return createNodeForSelectViaUMinSeq(SE, SECond, SETrue, SEFalse); 6127 } 6128 6129 const SCEV *ScalarEvolution::createNodeForSelectOrPHIViaUMinSeq( 6130 Value *V, Value *Cond, Value *TrueVal, Value *FalseVal) { 6131 assert(Cond->getType()->isIntegerTy(1) && "Select condition is not an i1?"); 6132 assert(TrueVal->getType() == FalseVal->getType() && 6133 V->getType() == TrueVal->getType() && 6134 "Types of select hands and of the result must match."); 6135 6136 // For now, only deal with i1-typed `select`s. 6137 if (!V->getType()->isIntegerTy(1)) 6138 return getUnknown(V); 6139 6140 if (Optional<const SCEV *> S = 6141 createNodeForSelectViaUMinSeq(this, Cond, TrueVal, FalseVal)) 6142 return *S; 6143 6144 return getUnknown(V); 6145 } 6146 6147 const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Value *V, Value *Cond, 6148 Value *TrueVal, 6149 Value *FalseVal) { 6150 // Handle "constant" branch or select. This can occur for instance when a 6151 // loop pass transforms an inner loop and moves on to process the outer loop. 6152 if (auto *CI = dyn_cast<ConstantInt>(Cond)) 6153 return getSCEV(CI->isOne() ? TrueVal : FalseVal); 6154 6155 if (auto *I = dyn_cast<Instruction>(V)) { 6156 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) { 6157 const SCEV *S = createNodeForSelectOrPHIInstWithICmpInstCond( 6158 I, ICI, TrueVal, FalseVal); 6159 if (!isa<SCEVUnknown>(S)) 6160 return S; 6161 } 6162 } 6163 6164 return createNodeForSelectOrPHIViaUMinSeq(V, Cond, TrueVal, FalseVal); 6165 } 6166 6167 /// Expand GEP instructions into add and multiply operations. This allows them 6168 /// to be analyzed by regular SCEV code. 6169 const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) { 6170 // Don't attempt to analyze GEPs over unsized objects. 6171 if (!GEP->getSourceElementType()->isSized()) 6172 return getUnknown(GEP); 6173 6174 SmallVector<const SCEV *, 4> IndexExprs; 6175 for (Value *Index : GEP->indices()) 6176 IndexExprs.push_back(getSCEV(Index)); 6177 return getGEPExpr(GEP, IndexExprs); 6178 } 6179 6180 uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) { 6181 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) 6182 return C->getAPInt().countTrailingZeros(); 6183 6184 if (const SCEVPtrToIntExpr *I = dyn_cast<SCEVPtrToIntExpr>(S)) 6185 return GetMinTrailingZeros(I->getOperand()); 6186 6187 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) 6188 return std::min(GetMinTrailingZeros(T->getOperand()), 6189 (uint32_t)getTypeSizeInBits(T->getType())); 6190 6191 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { 6192 uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); 6193 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) 6194 ? getTypeSizeInBits(E->getType()) 6195 : OpRes; 6196 } 6197 6198 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { 6199 uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); 6200 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) 6201 ? getTypeSizeInBits(E->getType()) 6202 : OpRes; 6203 } 6204 6205 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { 6206 // The result is the min of all operands results. 6207 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); 6208 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) 6209 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); 6210 return MinOpRes; 6211 } 6212 6213 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { 6214 // The result is the sum of all operands results. 6215 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); 6216 uint32_t BitWidth = getTypeSizeInBits(M->getType()); 6217 for (unsigned i = 1, e = M->getNumOperands(); 6218 SumOpRes != BitWidth && i != e; ++i) 6219 SumOpRes = 6220 std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), BitWidth); 6221 return SumOpRes; 6222 } 6223 6224 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { 6225 // The result is the min of all operands results. 6226 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); 6227 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) 6228 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); 6229 return MinOpRes; 6230 } 6231 6232 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { 6233 // The result is the min of all operands results. 6234 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); 6235 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) 6236 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); 6237 return MinOpRes; 6238 } 6239 6240 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { 6241 // The result is the min of all operands results. 6242 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); 6243 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) 6244 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); 6245 return MinOpRes; 6246 } 6247 6248 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 6249 // For a SCEVUnknown, ask ValueTracking. 6250 KnownBits Known = computeKnownBits(U->getValue(), getDataLayout(), 0, &AC, nullptr, &DT); 6251 return Known.countMinTrailingZeros(); 6252 } 6253 6254 // SCEVUDivExpr 6255 return 0; 6256 } 6257 6258 uint32_t ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { 6259 auto I = MinTrailingZerosCache.find(S); 6260 if (I != MinTrailingZerosCache.end()) 6261 return I->second; 6262 6263 uint32_t Result = GetMinTrailingZerosImpl(S); 6264 auto InsertPair = MinTrailingZerosCache.insert({S, Result}); 6265 assert(InsertPair.second && "Should insert a new key"); 6266 return InsertPair.first->second; 6267 } 6268 6269 /// Helper method to assign a range to V from metadata present in the IR. 6270 static Optional<ConstantRange> GetRangeFromMetadata(Value *V) { 6271 if (Instruction *I = dyn_cast<Instruction>(V)) 6272 if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) 6273 return getConstantRangeFromMetadata(*MD); 6274 6275 return None; 6276 } 6277 6278 void ScalarEvolution::setNoWrapFlags(SCEVAddRecExpr *AddRec, 6279 SCEV::NoWrapFlags Flags) { 6280 if (AddRec->getNoWrapFlags(Flags) != Flags) { 6281 AddRec->setNoWrapFlags(Flags); 6282 UnsignedRanges.erase(AddRec); 6283 SignedRanges.erase(AddRec); 6284 } 6285 } 6286 6287 ConstantRange ScalarEvolution:: 6288 getRangeForUnknownRecurrence(const SCEVUnknown *U) { 6289 const DataLayout &DL = getDataLayout(); 6290 6291 unsigned BitWidth = getTypeSizeInBits(U->getType()); 6292 const ConstantRange FullSet(BitWidth, /*isFullSet=*/true); 6293 6294 // Match a simple recurrence of the form: <start, ShiftOp, Step>, and then 6295 // use information about the trip count to improve our available range. Note 6296 // that the trip count independent cases are already handled by known bits. 6297 // WARNING: The definition of recurrence used here is subtly different than 6298 // the one used by AddRec (and thus most of this file). Step is allowed to 6299 // be arbitrarily loop varying here, where AddRec allows only loop invariant 6300 // and other addrecs in the same loop (for non-affine addrecs). The code 6301 // below intentionally handles the case where step is not loop invariant. 6302 auto *P = dyn_cast<PHINode>(U->getValue()); 6303 if (!P) 6304 return FullSet; 6305 6306 // Make sure that no Phi input comes from an unreachable block. Otherwise, 6307 // even the values that are not available in these blocks may come from them, 6308 // and this leads to false-positive recurrence test. 6309 for (auto *Pred : predecessors(P->getParent())) 6310 if (!DT.isReachableFromEntry(Pred)) 6311 return FullSet; 6312 6313 BinaryOperator *BO; 6314 Value *Start, *Step; 6315 if (!matchSimpleRecurrence(P, BO, Start, Step)) 6316 return FullSet; 6317 6318 // If we found a recurrence in reachable code, we must be in a loop. Note 6319 // that BO might be in some subloop of L, and that's completely okay. 6320 auto *L = LI.getLoopFor(P->getParent()); 6321 assert(L && L->getHeader() == P->getParent()); 6322 if (!L->contains(BO->getParent())) 6323 // NOTE: This bailout should be an assert instead. However, asserting 6324 // the condition here exposes a case where LoopFusion is querying SCEV 6325 // with malformed loop information during the midst of the transform. 6326 // There doesn't appear to be an obvious fix, so for the moment bailout 6327 // until the caller issue can be fixed. PR49566 tracks the bug. 6328 return FullSet; 6329 6330 // TODO: Extend to other opcodes such as mul, and div 6331 switch (BO->getOpcode()) { 6332 default: 6333 return FullSet; 6334 case Instruction::AShr: 6335 case Instruction::LShr: 6336 case Instruction::Shl: 6337 break; 6338 }; 6339 6340 if (BO->getOperand(0) != P) 6341 // TODO: Handle the power function forms some day. 6342 return FullSet; 6343 6344 unsigned TC = getSmallConstantMaxTripCount(L); 6345 if (!TC || TC >= BitWidth) 6346 return FullSet; 6347 6348 auto KnownStart = computeKnownBits(Start, DL, 0, &AC, nullptr, &DT); 6349 auto KnownStep = computeKnownBits(Step, DL, 0, &AC, nullptr, &DT); 6350 assert(KnownStart.getBitWidth() == BitWidth && 6351 KnownStep.getBitWidth() == BitWidth); 6352 6353 // Compute total shift amount, being careful of overflow and bitwidths. 6354 auto MaxShiftAmt = KnownStep.getMaxValue(); 6355 APInt TCAP(BitWidth, TC-1); 6356 bool Overflow = false; 6357 auto TotalShift = MaxShiftAmt.umul_ov(TCAP, Overflow); 6358 if (Overflow) 6359 return FullSet; 6360 6361 switch (BO->getOpcode()) { 6362 default: 6363 llvm_unreachable("filtered out above"); 6364 case Instruction::AShr: { 6365 // For each ashr, three cases: 6366 // shift = 0 => unchanged value 6367 // saturation => 0 or -1 6368 // other => a value closer to zero (of the same sign) 6369 // Thus, the end value is closer to zero than the start. 6370 auto KnownEnd = KnownBits::ashr(KnownStart, 6371 KnownBits::makeConstant(TotalShift)); 6372 if (KnownStart.isNonNegative()) 6373 // Analogous to lshr (simply not yet canonicalized) 6374 return ConstantRange::getNonEmpty(KnownEnd.getMinValue(), 6375 KnownStart.getMaxValue() + 1); 6376 if (KnownStart.isNegative()) 6377 // End >=u Start && End <=s Start 6378 return ConstantRange::getNonEmpty(KnownStart.getMinValue(), 6379 KnownEnd.getMaxValue() + 1); 6380 break; 6381 } 6382 case Instruction::LShr: { 6383 // For each lshr, three cases: 6384 // shift = 0 => unchanged value 6385 // saturation => 0 6386 // other => a smaller positive number 6387 // Thus, the low end of the unsigned range is the last value produced. 6388 auto KnownEnd = KnownBits::lshr(KnownStart, 6389 KnownBits::makeConstant(TotalShift)); 6390 return ConstantRange::getNonEmpty(KnownEnd.getMinValue(), 6391 KnownStart.getMaxValue() + 1); 6392 } 6393 case Instruction::Shl: { 6394 // Iff no bits are shifted out, value increases on every shift. 6395 auto KnownEnd = KnownBits::shl(KnownStart, 6396 KnownBits::makeConstant(TotalShift)); 6397 if (TotalShift.ult(KnownStart.countMinLeadingZeros())) 6398 return ConstantRange(KnownStart.getMinValue(), 6399 KnownEnd.getMaxValue() + 1); 6400 break; 6401 } 6402 }; 6403 return FullSet; 6404 } 6405 6406 /// Determine the range for a particular SCEV. If SignHint is 6407 /// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges 6408 /// with a "cleaner" unsigned (resp. signed) representation. 6409 const ConstantRange & 6410 ScalarEvolution::getRangeRef(const SCEV *S, 6411 ScalarEvolution::RangeSignHint SignHint) { 6412 DenseMap<const SCEV *, ConstantRange> &Cache = 6413 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges 6414 : SignedRanges; 6415 ConstantRange::PreferredRangeType RangeType = 6416 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED 6417 ? ConstantRange::Unsigned : ConstantRange::Signed; 6418 6419 // See if we've computed this range already. 6420 DenseMap<const SCEV *, ConstantRange>::iterator I = Cache.find(S); 6421 if (I != Cache.end()) 6422 return I->second; 6423 6424 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) 6425 return setRange(C, SignHint, ConstantRange(C->getAPInt())); 6426 6427 unsigned BitWidth = getTypeSizeInBits(S->getType()); 6428 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); 6429 using OBO = OverflowingBinaryOperator; 6430 6431 // If the value has known zeros, the maximum value will have those known zeros 6432 // as well. 6433 uint32_t TZ = GetMinTrailingZeros(S); 6434 if (TZ != 0) { 6435 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) 6436 ConservativeResult = 6437 ConstantRange(APInt::getMinValue(BitWidth), 6438 APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1); 6439 else 6440 ConservativeResult = ConstantRange( 6441 APInt::getSignedMinValue(BitWidth), 6442 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1); 6443 } 6444 6445 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { 6446 ConstantRange X = getRangeRef(Add->getOperand(0), SignHint); 6447 unsigned WrapType = OBO::AnyWrap; 6448 if (Add->hasNoSignedWrap()) 6449 WrapType |= OBO::NoSignedWrap; 6450 if (Add->hasNoUnsignedWrap()) 6451 WrapType |= OBO::NoUnsignedWrap; 6452 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) 6453 X = X.addWithNoWrap(getRangeRef(Add->getOperand(i), SignHint), 6454 WrapType, RangeType); 6455 return setRange(Add, SignHint, 6456 ConservativeResult.intersectWith(X, RangeType)); 6457 } 6458 6459 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { 6460 ConstantRange X = getRangeRef(Mul->getOperand(0), SignHint); 6461 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) 6462 X = X.multiply(getRangeRef(Mul->getOperand(i), SignHint)); 6463 return setRange(Mul, SignHint, 6464 ConservativeResult.intersectWith(X, RangeType)); 6465 } 6466 6467 if (isa<SCEVMinMaxExpr>(S) || isa<SCEVSequentialMinMaxExpr>(S)) { 6468 Intrinsic::ID ID; 6469 switch (S->getSCEVType()) { 6470 case scUMaxExpr: 6471 ID = Intrinsic::umax; 6472 break; 6473 case scSMaxExpr: 6474 ID = Intrinsic::smax; 6475 break; 6476 case scUMinExpr: 6477 case scSequentialUMinExpr: 6478 ID = Intrinsic::umin; 6479 break; 6480 case scSMinExpr: 6481 ID = Intrinsic::smin; 6482 break; 6483 default: 6484 llvm_unreachable("Unknown SCEVMinMaxExpr/SCEVSequentialMinMaxExpr."); 6485 } 6486 6487 const auto *NAry = cast<SCEVNAryExpr>(S); 6488 ConstantRange X = getRangeRef(NAry->getOperand(0), SignHint); 6489 for (unsigned i = 1, e = NAry->getNumOperands(); i != e; ++i) 6490 X = X.intrinsic(ID, {X, getRangeRef(NAry->getOperand(i), SignHint)}); 6491 return setRange(S, SignHint, 6492 ConservativeResult.intersectWith(X, RangeType)); 6493 } 6494 6495 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { 6496 ConstantRange X = getRangeRef(UDiv->getLHS(), SignHint); 6497 ConstantRange Y = getRangeRef(UDiv->getRHS(), SignHint); 6498 return setRange(UDiv, SignHint, 6499 ConservativeResult.intersectWith(X.udiv(Y), RangeType)); 6500 } 6501 6502 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { 6503 ConstantRange X = getRangeRef(ZExt->getOperand(), SignHint); 6504 return setRange(ZExt, SignHint, 6505 ConservativeResult.intersectWith(X.zeroExtend(BitWidth), 6506 RangeType)); 6507 } 6508 6509 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { 6510 ConstantRange X = getRangeRef(SExt->getOperand(), SignHint); 6511 return setRange(SExt, SignHint, 6512 ConservativeResult.intersectWith(X.signExtend(BitWidth), 6513 RangeType)); 6514 } 6515 6516 if (const SCEVPtrToIntExpr *PtrToInt = dyn_cast<SCEVPtrToIntExpr>(S)) { 6517 ConstantRange X = getRangeRef(PtrToInt->getOperand(), SignHint); 6518 return setRange(PtrToInt, SignHint, X); 6519 } 6520 6521 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { 6522 ConstantRange X = getRangeRef(Trunc->getOperand(), SignHint); 6523 return setRange(Trunc, SignHint, 6524 ConservativeResult.intersectWith(X.truncate(BitWidth), 6525 RangeType)); 6526 } 6527 6528 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { 6529 // If there's no unsigned wrap, the value will never be less than its 6530 // initial value. 6531 if (AddRec->hasNoUnsignedWrap()) { 6532 APInt UnsignedMinValue = getUnsignedRangeMin(AddRec->getStart()); 6533 if (!UnsignedMinValue.isZero()) 6534 ConservativeResult = ConservativeResult.intersectWith( 6535 ConstantRange(UnsignedMinValue, APInt(BitWidth, 0)), RangeType); 6536 } 6537 6538 // If there's no signed wrap, and all the operands except initial value have 6539 // the same sign or zero, the value won't ever be: 6540 // 1: smaller than initial value if operands are non negative, 6541 // 2: bigger than initial value if operands are non positive. 6542 // For both cases, value can not cross signed min/max boundary. 6543 if (AddRec->hasNoSignedWrap()) { 6544 bool AllNonNeg = true; 6545 bool AllNonPos = true; 6546 for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) { 6547 if (!isKnownNonNegative(AddRec->getOperand(i))) 6548 AllNonNeg = false; 6549 if (!isKnownNonPositive(AddRec->getOperand(i))) 6550 AllNonPos = false; 6551 } 6552 if (AllNonNeg) 6553 ConservativeResult = ConservativeResult.intersectWith( 6554 ConstantRange::getNonEmpty(getSignedRangeMin(AddRec->getStart()), 6555 APInt::getSignedMinValue(BitWidth)), 6556 RangeType); 6557 else if (AllNonPos) 6558 ConservativeResult = ConservativeResult.intersectWith( 6559 ConstantRange::getNonEmpty( 6560 APInt::getSignedMinValue(BitWidth), 6561 getSignedRangeMax(AddRec->getStart()) + 1), 6562 RangeType); 6563 } 6564 6565 // TODO: non-affine addrec 6566 if (AddRec->isAffine()) { 6567 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(AddRec->getLoop()); 6568 if (!isa<SCEVCouldNotCompute>(MaxBECount) && 6569 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) { 6570 auto RangeFromAffine = getRangeForAffineAR( 6571 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount, 6572 BitWidth); 6573 ConservativeResult = 6574 ConservativeResult.intersectWith(RangeFromAffine, RangeType); 6575 6576 auto RangeFromFactoring = getRangeViaFactoring( 6577 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount, 6578 BitWidth); 6579 ConservativeResult = 6580 ConservativeResult.intersectWith(RangeFromFactoring, RangeType); 6581 } 6582 6583 // Now try symbolic BE count and more powerful methods. 6584 if (UseExpensiveRangeSharpening) { 6585 const SCEV *SymbolicMaxBECount = 6586 getSymbolicMaxBackedgeTakenCount(AddRec->getLoop()); 6587 if (!isa<SCEVCouldNotCompute>(SymbolicMaxBECount) && 6588 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth && 6589 AddRec->hasNoSelfWrap()) { 6590 auto RangeFromAffineNew = getRangeForAffineNoSelfWrappingAR( 6591 AddRec, SymbolicMaxBECount, BitWidth, SignHint); 6592 ConservativeResult = 6593 ConservativeResult.intersectWith(RangeFromAffineNew, RangeType); 6594 } 6595 } 6596 } 6597 6598 return setRange(AddRec, SignHint, std::move(ConservativeResult)); 6599 } 6600 6601 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 6602 6603 // Check if the IR explicitly contains !range metadata. 6604 Optional<ConstantRange> MDRange = GetRangeFromMetadata(U->getValue()); 6605 if (MDRange.hasValue()) 6606 ConservativeResult = ConservativeResult.intersectWith(MDRange.getValue(), 6607 RangeType); 6608 6609 // Use facts about recurrences in the underlying IR. Note that add 6610 // recurrences are AddRecExprs and thus don't hit this path. This 6611 // primarily handles shift recurrences. 6612 auto CR = getRangeForUnknownRecurrence(U); 6613 ConservativeResult = ConservativeResult.intersectWith(CR); 6614 6615 // See if ValueTracking can give us a useful range. 6616 const DataLayout &DL = getDataLayout(); 6617 KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT); 6618 if (Known.getBitWidth() != BitWidth) 6619 Known = Known.zextOrTrunc(BitWidth); 6620 6621 // ValueTracking may be able to compute a tighter result for the number of 6622 // sign bits than for the value of those sign bits. 6623 unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT); 6624 if (U->getType()->isPointerTy()) { 6625 // If the pointer size is larger than the index size type, this can cause 6626 // NS to be larger than BitWidth. So compensate for this. 6627 unsigned ptrSize = DL.getPointerTypeSizeInBits(U->getType()); 6628 int ptrIdxDiff = ptrSize - BitWidth; 6629 if (ptrIdxDiff > 0 && ptrSize > BitWidth && NS > (unsigned)ptrIdxDiff) 6630 NS -= ptrIdxDiff; 6631 } 6632 6633 if (NS > 1) { 6634 // If we know any of the sign bits, we know all of the sign bits. 6635 if (!Known.Zero.getHiBits(NS).isZero()) 6636 Known.Zero.setHighBits(NS); 6637 if (!Known.One.getHiBits(NS).isZero()) 6638 Known.One.setHighBits(NS); 6639 } 6640 6641 if (Known.getMinValue() != Known.getMaxValue() + 1) 6642 ConservativeResult = ConservativeResult.intersectWith( 6643 ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1), 6644 RangeType); 6645 if (NS > 1) 6646 ConservativeResult = ConservativeResult.intersectWith( 6647 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), 6648 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1), 6649 RangeType); 6650 6651 // A range of Phi is a subset of union of all ranges of its input. 6652 if (const PHINode *Phi = dyn_cast<PHINode>(U->getValue())) { 6653 // Make sure that we do not run over cycled Phis. 6654 if (PendingPhiRanges.insert(Phi).second) { 6655 ConstantRange RangeFromOps(BitWidth, /*isFullSet=*/false); 6656 for (auto &Op : Phi->operands()) { 6657 auto OpRange = getRangeRef(getSCEV(Op), SignHint); 6658 RangeFromOps = RangeFromOps.unionWith(OpRange); 6659 // No point to continue if we already have a full set. 6660 if (RangeFromOps.isFullSet()) 6661 break; 6662 } 6663 ConservativeResult = 6664 ConservativeResult.intersectWith(RangeFromOps, RangeType); 6665 bool Erased = PendingPhiRanges.erase(Phi); 6666 assert(Erased && "Failed to erase Phi properly?"); 6667 (void) Erased; 6668 } 6669 } 6670 6671 return setRange(U, SignHint, std::move(ConservativeResult)); 6672 } 6673 6674 return setRange(S, SignHint, std::move(ConservativeResult)); 6675 } 6676 6677 // Given a StartRange, Step and MaxBECount for an expression compute a range of 6678 // values that the expression can take. Initially, the expression has a value 6679 // from StartRange and then is changed by Step up to MaxBECount times. Signed 6680 // argument defines if we treat Step as signed or unsigned. 6681 static ConstantRange getRangeForAffineARHelper(APInt Step, 6682 const ConstantRange &StartRange, 6683 const APInt &MaxBECount, 6684 unsigned BitWidth, bool Signed) { 6685 // If either Step or MaxBECount is 0, then the expression won't change, and we 6686 // just need to return the initial range. 6687 if (Step == 0 || MaxBECount == 0) 6688 return StartRange; 6689 6690 // If we don't know anything about the initial value (i.e. StartRange is 6691 // FullRange), then we don't know anything about the final range either. 6692 // Return FullRange. 6693 if (StartRange.isFullSet()) 6694 return ConstantRange::getFull(BitWidth); 6695 6696 // If Step is signed and negative, then we use its absolute value, but we also 6697 // note that we're moving in the opposite direction. 6698 bool Descending = Signed && Step.isNegative(); 6699 6700 if (Signed) 6701 // This is correct even for INT_SMIN. Let's look at i8 to illustrate this: 6702 // abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128. 6703 // This equations hold true due to the well-defined wrap-around behavior of 6704 // APInt. 6705 Step = Step.abs(); 6706 6707 // Check if Offset is more than full span of BitWidth. If it is, the 6708 // expression is guaranteed to overflow. 6709 if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount)) 6710 return ConstantRange::getFull(BitWidth); 6711 6712 // Offset is by how much the expression can change. Checks above guarantee no 6713 // overflow here. 6714 APInt Offset = Step * MaxBECount; 6715 6716 // Minimum value of the final range will match the minimal value of StartRange 6717 // if the expression is increasing and will be decreased by Offset otherwise. 6718 // Maximum value of the final range will match the maximal value of StartRange 6719 // if the expression is decreasing and will be increased by Offset otherwise. 6720 APInt StartLower = StartRange.getLower(); 6721 APInt StartUpper = StartRange.getUpper() - 1; 6722 APInt MovedBoundary = Descending ? (StartLower - std::move(Offset)) 6723 : (StartUpper + std::move(Offset)); 6724 6725 // It's possible that the new minimum/maximum value will fall into the initial 6726 // range (due to wrap around). This means that the expression can take any 6727 // value in this bitwidth, and we have to return full range. 6728 if (StartRange.contains(MovedBoundary)) 6729 return ConstantRange::getFull(BitWidth); 6730 6731 APInt NewLower = 6732 Descending ? std::move(MovedBoundary) : std::move(StartLower); 6733 APInt NewUpper = 6734 Descending ? std::move(StartUpper) : std::move(MovedBoundary); 6735 NewUpper += 1; 6736 6737 // No overflow detected, return [StartLower, StartUpper + Offset + 1) range. 6738 return ConstantRange::getNonEmpty(std::move(NewLower), std::move(NewUpper)); 6739 } 6740 6741 ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start, 6742 const SCEV *Step, 6743 const SCEV *MaxBECount, 6744 unsigned BitWidth) { 6745 assert(!isa<SCEVCouldNotCompute>(MaxBECount) && 6746 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth && 6747 "Precondition!"); 6748 6749 MaxBECount = getNoopOrZeroExtend(MaxBECount, Start->getType()); 6750 APInt MaxBECountValue = getUnsignedRangeMax(MaxBECount); 6751 6752 // First, consider step signed. 6753 ConstantRange StartSRange = getSignedRange(Start); 6754 ConstantRange StepSRange = getSignedRange(Step); 6755 6756 // If Step can be both positive and negative, we need to find ranges for the 6757 // maximum absolute step values in both directions and union them. 6758 ConstantRange SR = 6759 getRangeForAffineARHelper(StepSRange.getSignedMin(), StartSRange, 6760 MaxBECountValue, BitWidth, /* Signed = */ true); 6761 SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(), 6762 StartSRange, MaxBECountValue, 6763 BitWidth, /* Signed = */ true)); 6764 6765 // Next, consider step unsigned. 6766 ConstantRange UR = getRangeForAffineARHelper( 6767 getUnsignedRangeMax(Step), getUnsignedRange(Start), 6768 MaxBECountValue, BitWidth, /* Signed = */ false); 6769 6770 // Finally, intersect signed and unsigned ranges. 6771 return SR.intersectWith(UR, ConstantRange::Smallest); 6772 } 6773 6774 ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR( 6775 const SCEVAddRecExpr *AddRec, const SCEV *MaxBECount, unsigned BitWidth, 6776 ScalarEvolution::RangeSignHint SignHint) { 6777 assert(AddRec->isAffine() && "Non-affine AddRecs are not suppored!\n"); 6778 assert(AddRec->hasNoSelfWrap() && 6779 "This only works for non-self-wrapping AddRecs!"); 6780 const bool IsSigned = SignHint == HINT_RANGE_SIGNED; 6781 const SCEV *Step = AddRec->getStepRecurrence(*this); 6782 // Only deal with constant step to save compile time. 6783 if (!isa<SCEVConstant>(Step)) 6784 return ConstantRange::getFull(BitWidth); 6785 // Let's make sure that we can prove that we do not self-wrap during 6786 // MaxBECount iterations. We need this because MaxBECount is a maximum 6787 // iteration count estimate, and we might infer nw from some exit for which we 6788 // do not know max exit count (or any other side reasoning). 6789 // TODO: Turn into assert at some point. 6790 if (getTypeSizeInBits(MaxBECount->getType()) > 6791 getTypeSizeInBits(AddRec->getType())) 6792 return ConstantRange::getFull(BitWidth); 6793 MaxBECount = getNoopOrZeroExtend(MaxBECount, AddRec->getType()); 6794 const SCEV *RangeWidth = getMinusOne(AddRec->getType()); 6795 const SCEV *StepAbs = getUMinExpr(Step, getNegativeSCEV(Step)); 6796 const SCEV *MaxItersWithoutWrap = getUDivExpr(RangeWidth, StepAbs); 6797 if (!isKnownPredicateViaConstantRanges(ICmpInst::ICMP_ULE, MaxBECount, 6798 MaxItersWithoutWrap)) 6799 return ConstantRange::getFull(BitWidth); 6800 6801 ICmpInst::Predicate LEPred = 6802 IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; 6803 ICmpInst::Predicate GEPred = 6804 IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; 6805 const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this); 6806 6807 // We know that there is no self-wrap. Let's take Start and End values and 6808 // look at all intermediate values V1, V2, ..., Vn that IndVar takes during 6809 // the iteration. They either lie inside the range [Min(Start, End), 6810 // Max(Start, End)] or outside it: 6811 // 6812 // Case 1: RangeMin ... Start V1 ... VN End ... RangeMax; 6813 // Case 2: RangeMin Vk ... V1 Start ... End Vn ... Vk + 1 RangeMax; 6814 // 6815 // No self wrap flag guarantees that the intermediate values cannot be BOTH 6816 // outside and inside the range [Min(Start, End), Max(Start, End)]. Using that 6817 // knowledge, let's try to prove that we are dealing with Case 1. It is so if 6818 // Start <= End and step is positive, or Start >= End and step is negative. 6819 const SCEV *Start = AddRec->getStart(); 6820 ConstantRange StartRange = getRangeRef(Start, SignHint); 6821 ConstantRange EndRange = getRangeRef(End, SignHint); 6822 ConstantRange RangeBetween = StartRange.unionWith(EndRange); 6823 // If they already cover full iteration space, we will know nothing useful 6824 // even if we prove what we want to prove. 6825 if (RangeBetween.isFullSet()) 6826 return RangeBetween; 6827 // Only deal with ranges that do not wrap (i.e. RangeMin < RangeMax). 6828 bool IsWrappedSet = IsSigned ? RangeBetween.isSignWrappedSet() 6829 : RangeBetween.isWrappedSet(); 6830 if (IsWrappedSet) 6831 return ConstantRange::getFull(BitWidth); 6832 6833 if (isKnownPositive(Step) && 6834 isKnownPredicateViaConstantRanges(LEPred, Start, End)) 6835 return RangeBetween; 6836 else if (isKnownNegative(Step) && 6837 isKnownPredicateViaConstantRanges(GEPred, Start, End)) 6838 return RangeBetween; 6839 return ConstantRange::getFull(BitWidth); 6840 } 6841 6842 ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start, 6843 const SCEV *Step, 6844 const SCEV *MaxBECount, 6845 unsigned BitWidth) { 6846 // RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q}) 6847 // == RangeOf({A,+,P}) union RangeOf({B,+,Q}) 6848 6849 struct SelectPattern { 6850 Value *Condition = nullptr; 6851 APInt TrueValue; 6852 APInt FalseValue; 6853 6854 explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth, 6855 const SCEV *S) { 6856 Optional<unsigned> CastOp; 6857 APInt Offset(BitWidth, 0); 6858 6859 assert(SE.getTypeSizeInBits(S->getType()) == BitWidth && 6860 "Should be!"); 6861 6862 // Peel off a constant offset: 6863 if (auto *SA = dyn_cast<SCEVAddExpr>(S)) { 6864 // In the future we could consider being smarter here and handle 6865 // {Start+Step,+,Step} too. 6866 if (SA->getNumOperands() != 2 || !isa<SCEVConstant>(SA->getOperand(0))) 6867 return; 6868 6869 Offset = cast<SCEVConstant>(SA->getOperand(0))->getAPInt(); 6870 S = SA->getOperand(1); 6871 } 6872 6873 // Peel off a cast operation 6874 if (auto *SCast = dyn_cast<SCEVIntegralCastExpr>(S)) { 6875 CastOp = SCast->getSCEVType(); 6876 S = SCast->getOperand(); 6877 } 6878 6879 using namespace llvm::PatternMatch; 6880 6881 auto *SU = dyn_cast<SCEVUnknown>(S); 6882 const APInt *TrueVal, *FalseVal; 6883 if (!SU || 6884 !match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal), 6885 m_APInt(FalseVal)))) { 6886 Condition = nullptr; 6887 return; 6888 } 6889 6890 TrueValue = *TrueVal; 6891 FalseValue = *FalseVal; 6892 6893 // Re-apply the cast we peeled off earlier 6894 if (CastOp.hasValue()) 6895 switch (*CastOp) { 6896 default: 6897 llvm_unreachable("Unknown SCEV cast type!"); 6898 6899 case scTruncate: 6900 TrueValue = TrueValue.trunc(BitWidth); 6901 FalseValue = FalseValue.trunc(BitWidth); 6902 break; 6903 case scZeroExtend: 6904 TrueValue = TrueValue.zext(BitWidth); 6905 FalseValue = FalseValue.zext(BitWidth); 6906 break; 6907 case scSignExtend: 6908 TrueValue = TrueValue.sext(BitWidth); 6909 FalseValue = FalseValue.sext(BitWidth); 6910 break; 6911 } 6912 6913 // Re-apply the constant offset we peeled off earlier 6914 TrueValue += Offset; 6915 FalseValue += Offset; 6916 } 6917 6918 bool isRecognized() { return Condition != nullptr; } 6919 }; 6920 6921 SelectPattern StartPattern(*this, BitWidth, Start); 6922 if (!StartPattern.isRecognized()) 6923 return ConstantRange::getFull(BitWidth); 6924 6925 SelectPattern StepPattern(*this, BitWidth, Step); 6926 if (!StepPattern.isRecognized()) 6927 return ConstantRange::getFull(BitWidth); 6928 6929 if (StartPattern.Condition != StepPattern.Condition) { 6930 // We don't handle this case today; but we could, by considering four 6931 // possibilities below instead of two. I'm not sure if there are cases where 6932 // that will help over what getRange already does, though. 6933 return ConstantRange::getFull(BitWidth); 6934 } 6935 6936 // NB! Calling ScalarEvolution::getConstant is fine, but we should not try to 6937 // construct arbitrary general SCEV expressions here. This function is called 6938 // from deep in the call stack, and calling getSCEV (on a sext instruction, 6939 // say) can end up caching a suboptimal value. 6940 6941 // FIXME: without the explicit `this` receiver below, MSVC errors out with 6942 // C2352 and C2512 (otherwise it isn't needed). 6943 6944 const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue); 6945 const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue); 6946 const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue); 6947 const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue); 6948 6949 ConstantRange TrueRange = 6950 this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount, BitWidth); 6951 ConstantRange FalseRange = 6952 this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount, BitWidth); 6953 6954 return TrueRange.unionWith(FalseRange); 6955 } 6956 6957 SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) { 6958 if (isa<ConstantExpr>(V)) return SCEV::FlagAnyWrap; 6959 const BinaryOperator *BinOp = cast<BinaryOperator>(V); 6960 6961 // Return early if there are no flags to propagate to the SCEV. 6962 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; 6963 if (BinOp->hasNoUnsignedWrap()) 6964 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 6965 if (BinOp->hasNoSignedWrap()) 6966 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); 6967 if (Flags == SCEV::FlagAnyWrap) 6968 return SCEV::FlagAnyWrap; 6969 6970 return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap; 6971 } 6972 6973 const Instruction * 6974 ScalarEvolution::getNonTrivialDefiningScopeBound(const SCEV *S) { 6975 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(S)) 6976 return &*AddRec->getLoop()->getHeader()->begin(); 6977 if (auto *U = dyn_cast<SCEVUnknown>(S)) 6978 if (auto *I = dyn_cast<Instruction>(U->getValue())) 6979 return I; 6980 return nullptr; 6981 } 6982 6983 /// Fills \p Ops with unique operands of \p S, if it has operands. If not, 6984 /// \p Ops remains unmodified. 6985 static void collectUniqueOps(const SCEV *S, 6986 SmallVectorImpl<const SCEV *> &Ops) { 6987 SmallPtrSet<const SCEV *, 4> Unique; 6988 auto InsertUnique = [&](const SCEV *S) { 6989 if (Unique.insert(S).second) 6990 Ops.push_back(S); 6991 }; 6992 if (auto *S2 = dyn_cast<SCEVCastExpr>(S)) 6993 for (auto *Op : S2->operands()) 6994 InsertUnique(Op); 6995 else if (auto *S2 = dyn_cast<SCEVNAryExpr>(S)) 6996 for (auto *Op : S2->operands()) 6997 InsertUnique(Op); 6998 else if (auto *S2 = dyn_cast<SCEVUDivExpr>(S)) 6999 for (auto *Op : S2->operands()) 7000 InsertUnique(Op); 7001 } 7002 7003 const Instruction * 7004 ScalarEvolution::getDefiningScopeBound(ArrayRef<const SCEV *> Ops, 7005 bool &Precise) { 7006 Precise = true; 7007 // Do a bounded search of the def relation of the requested SCEVs. 7008 SmallSet<const SCEV *, 16> Visited; 7009 SmallVector<const SCEV *> Worklist; 7010 auto pushOp = [&](const SCEV *S) { 7011 if (!Visited.insert(S).second) 7012 return; 7013 // Threshold of 30 here is arbitrary. 7014 if (Visited.size() > 30) { 7015 Precise = false; 7016 return; 7017 } 7018 Worklist.push_back(S); 7019 }; 7020 7021 for (auto *S : Ops) 7022 pushOp(S); 7023 7024 const Instruction *Bound = nullptr; 7025 while (!Worklist.empty()) { 7026 auto *S = Worklist.pop_back_val(); 7027 if (auto *DefI = getNonTrivialDefiningScopeBound(S)) { 7028 if (!Bound || DT.dominates(Bound, DefI)) 7029 Bound = DefI; 7030 } else { 7031 SmallVector<const SCEV *, 4> Ops; 7032 collectUniqueOps(S, Ops); 7033 for (auto *Op : Ops) 7034 pushOp(Op); 7035 } 7036 } 7037 return Bound ? Bound : &*F.getEntryBlock().begin(); 7038 } 7039 7040 const Instruction * 7041 ScalarEvolution::getDefiningScopeBound(ArrayRef<const SCEV *> Ops) { 7042 bool Discard; 7043 return getDefiningScopeBound(Ops, Discard); 7044 } 7045 7046 bool ScalarEvolution::isGuaranteedToTransferExecutionTo(const Instruction *A, 7047 const Instruction *B) { 7048 if (A->getParent() == B->getParent() && 7049 isGuaranteedToTransferExecutionToSuccessor(A->getIterator(), 7050 B->getIterator())) 7051 return true; 7052 7053 auto *BLoop = LI.getLoopFor(B->getParent()); 7054 if (BLoop && BLoop->getHeader() == B->getParent() && 7055 BLoop->getLoopPreheader() == A->getParent() && 7056 isGuaranteedToTransferExecutionToSuccessor(A->getIterator(), 7057 A->getParent()->end()) && 7058 isGuaranteedToTransferExecutionToSuccessor(B->getParent()->begin(), 7059 B->getIterator())) 7060 return true; 7061 return false; 7062 } 7063 7064 7065 bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) { 7066 // Only proceed if we can prove that I does not yield poison. 7067 if (!programUndefinedIfPoison(I)) 7068 return false; 7069 7070 // At this point we know that if I is executed, then it does not wrap 7071 // according to at least one of NSW or NUW. If I is not executed, then we do 7072 // not know if the calculation that I represents would wrap. Multiple 7073 // instructions can map to the same SCEV. If we apply NSW or NUW from I to 7074 // the SCEV, we must guarantee no wrapping for that SCEV also when it is 7075 // derived from other instructions that map to the same SCEV. We cannot make 7076 // that guarantee for cases where I is not executed. So we need to find a 7077 // upper bound on the defining scope for the SCEV, and prove that I is 7078 // executed every time we enter that scope. When the bounding scope is a 7079 // loop (the common case), this is equivalent to proving I executes on every 7080 // iteration of that loop. 7081 SmallVector<const SCEV *> SCEVOps; 7082 for (const Use &Op : I->operands()) { 7083 // I could be an extractvalue from a call to an overflow intrinsic. 7084 // TODO: We can do better here in some cases. 7085 if (isSCEVable(Op->getType())) 7086 SCEVOps.push_back(getSCEV(Op)); 7087 } 7088 auto *DefI = getDefiningScopeBound(SCEVOps); 7089 return isGuaranteedToTransferExecutionTo(DefI, I); 7090 } 7091 7092 bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) { 7093 // If we know that \c I can never be poison period, then that's enough. 7094 if (isSCEVExprNeverPoison(I)) 7095 return true; 7096 7097 // For an add recurrence specifically, we assume that infinite loops without 7098 // side effects are undefined behavior, and then reason as follows: 7099 // 7100 // If the add recurrence is poison in any iteration, it is poison on all 7101 // future iterations (since incrementing poison yields poison). If the result 7102 // of the add recurrence is fed into the loop latch condition and the loop 7103 // does not contain any throws or exiting blocks other than the latch, we now 7104 // have the ability to "choose" whether the backedge is taken or not (by 7105 // choosing a sufficiently evil value for the poison feeding into the branch) 7106 // for every iteration including and after the one in which \p I first became 7107 // poison. There are two possibilities (let's call the iteration in which \p 7108 // I first became poison as K): 7109 // 7110 // 1. In the set of iterations including and after K, the loop body executes 7111 // no side effects. In this case executing the backege an infinte number 7112 // of times will yield undefined behavior. 7113 // 7114 // 2. In the set of iterations including and after K, the loop body executes 7115 // at least one side effect. In this case, that specific instance of side 7116 // effect is control dependent on poison, which also yields undefined 7117 // behavior. 7118 7119 auto *ExitingBB = L->getExitingBlock(); 7120 auto *LatchBB = L->getLoopLatch(); 7121 if (!ExitingBB || !LatchBB || ExitingBB != LatchBB) 7122 return false; 7123 7124 SmallPtrSet<const Instruction *, 16> Pushed; 7125 SmallVector<const Instruction *, 8> PoisonStack; 7126 7127 // We start by assuming \c I, the post-inc add recurrence, is poison. Only 7128 // things that are known to be poison under that assumption go on the 7129 // PoisonStack. 7130 Pushed.insert(I); 7131 PoisonStack.push_back(I); 7132 7133 bool LatchControlDependentOnPoison = false; 7134 while (!PoisonStack.empty() && !LatchControlDependentOnPoison) { 7135 const Instruction *Poison = PoisonStack.pop_back_val(); 7136 7137 for (auto *PoisonUser : Poison->users()) { 7138 if (propagatesPoison(cast<Operator>(PoisonUser))) { 7139 if (Pushed.insert(cast<Instruction>(PoisonUser)).second) 7140 PoisonStack.push_back(cast<Instruction>(PoisonUser)); 7141 } else if (auto *BI = dyn_cast<BranchInst>(PoisonUser)) { 7142 assert(BI->isConditional() && "Only possibility!"); 7143 if (BI->getParent() == LatchBB) { 7144 LatchControlDependentOnPoison = true; 7145 break; 7146 } 7147 } 7148 } 7149 } 7150 7151 return LatchControlDependentOnPoison && loopHasNoAbnormalExits(L); 7152 } 7153 7154 ScalarEvolution::LoopProperties 7155 ScalarEvolution::getLoopProperties(const Loop *L) { 7156 using LoopProperties = ScalarEvolution::LoopProperties; 7157 7158 auto Itr = LoopPropertiesCache.find(L); 7159 if (Itr == LoopPropertiesCache.end()) { 7160 auto HasSideEffects = [](Instruction *I) { 7161 if (auto *SI = dyn_cast<StoreInst>(I)) 7162 return !SI->isSimple(); 7163 7164 return I->mayThrow() || I->mayWriteToMemory(); 7165 }; 7166 7167 LoopProperties LP = {/* HasNoAbnormalExits */ true, 7168 /*HasNoSideEffects*/ true}; 7169 7170 for (auto *BB : L->getBlocks()) 7171 for (auto &I : *BB) { 7172 if (!isGuaranteedToTransferExecutionToSuccessor(&I)) 7173 LP.HasNoAbnormalExits = false; 7174 if (HasSideEffects(&I)) 7175 LP.HasNoSideEffects = false; 7176 if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects) 7177 break; // We're already as pessimistic as we can get. 7178 } 7179 7180 auto InsertPair = LoopPropertiesCache.insert({L, LP}); 7181 assert(InsertPair.second && "We just checked!"); 7182 Itr = InsertPair.first; 7183 } 7184 7185 return Itr->second; 7186 } 7187 7188 bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) { 7189 // A mustprogress loop without side effects must be finite. 7190 // TODO: The check used here is very conservative. It's only *specific* 7191 // side effects which are well defined in infinite loops. 7192 return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L)); 7193 } 7194 7195 const SCEV *ScalarEvolution::createSCEV(Value *V) { 7196 if (!isSCEVable(V->getType())) 7197 return getUnknown(V); 7198 7199 if (Instruction *I = dyn_cast<Instruction>(V)) { 7200 // Don't attempt to analyze instructions in blocks that aren't 7201 // reachable. Such instructions don't matter, and they aren't required 7202 // to obey basic rules for definitions dominating uses which this 7203 // analysis depends on. 7204 if (!DT.isReachableFromEntry(I->getParent())) 7205 return getUnknown(UndefValue::get(V->getType())); 7206 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) 7207 return getConstant(CI); 7208 else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) 7209 return GA->isInterposable() ? getUnknown(V) : getSCEV(GA->getAliasee()); 7210 else if (!isa<ConstantExpr>(V)) 7211 return getUnknown(V); 7212 7213 const SCEV *LHS; 7214 const SCEV *RHS; 7215 7216 Operator *U = cast<Operator>(V); 7217 if (auto BO = MatchBinaryOp(U, DT)) { 7218 switch (BO->Opcode) { 7219 case Instruction::Add: { 7220 // The simple thing to do would be to just call getSCEV on both operands 7221 // and call getAddExpr with the result. However if we're looking at a 7222 // bunch of things all added together, this can be quite inefficient, 7223 // because it leads to N-1 getAddExpr calls for N ultimate operands. 7224 // Instead, gather up all the operands and make a single getAddExpr call. 7225 // LLVM IR canonical form means we need only traverse the left operands. 7226 SmallVector<const SCEV *, 4> AddOps; 7227 do { 7228 if (BO->Op) { 7229 if (auto *OpSCEV = getExistingSCEV(BO->Op)) { 7230 AddOps.push_back(OpSCEV); 7231 break; 7232 } 7233 7234 // If a NUW or NSW flag can be applied to the SCEV for this 7235 // addition, then compute the SCEV for this addition by itself 7236 // with a separate call to getAddExpr. We need to do that 7237 // instead of pushing the operands of the addition onto AddOps, 7238 // since the flags are only known to apply to this particular 7239 // addition - they may not apply to other additions that can be 7240 // formed with operands from AddOps. 7241 const SCEV *RHS = getSCEV(BO->RHS); 7242 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op); 7243 if (Flags != SCEV::FlagAnyWrap) { 7244 const SCEV *LHS = getSCEV(BO->LHS); 7245 if (BO->Opcode == Instruction::Sub) 7246 AddOps.push_back(getMinusSCEV(LHS, RHS, Flags)); 7247 else 7248 AddOps.push_back(getAddExpr(LHS, RHS, Flags)); 7249 break; 7250 } 7251 } 7252 7253 if (BO->Opcode == Instruction::Sub) 7254 AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS))); 7255 else 7256 AddOps.push_back(getSCEV(BO->RHS)); 7257 7258 auto NewBO = MatchBinaryOp(BO->LHS, DT); 7259 if (!NewBO || (NewBO->Opcode != Instruction::Add && 7260 NewBO->Opcode != Instruction::Sub)) { 7261 AddOps.push_back(getSCEV(BO->LHS)); 7262 break; 7263 } 7264 BO = NewBO; 7265 } while (true); 7266 7267 return getAddExpr(AddOps); 7268 } 7269 7270 case Instruction::Mul: { 7271 SmallVector<const SCEV *, 4> MulOps; 7272 do { 7273 if (BO->Op) { 7274 if (auto *OpSCEV = getExistingSCEV(BO->Op)) { 7275 MulOps.push_back(OpSCEV); 7276 break; 7277 } 7278 7279 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op); 7280 if (Flags != SCEV::FlagAnyWrap) { 7281 LHS = getSCEV(BO->LHS); 7282 RHS = getSCEV(BO->RHS); 7283 MulOps.push_back(getMulExpr(LHS, RHS, Flags)); 7284 break; 7285 } 7286 } 7287 7288 MulOps.push_back(getSCEV(BO->RHS)); 7289 auto NewBO = MatchBinaryOp(BO->LHS, DT); 7290 if (!NewBO || NewBO->Opcode != Instruction::Mul) { 7291 MulOps.push_back(getSCEV(BO->LHS)); 7292 break; 7293 } 7294 BO = NewBO; 7295 } while (true); 7296 7297 return getMulExpr(MulOps); 7298 } 7299 case Instruction::UDiv: 7300 LHS = getSCEV(BO->LHS); 7301 RHS = getSCEV(BO->RHS); 7302 return getUDivExpr(LHS, RHS); 7303 case Instruction::URem: 7304 LHS = getSCEV(BO->LHS); 7305 RHS = getSCEV(BO->RHS); 7306 return getURemExpr(LHS, RHS); 7307 case Instruction::Sub: { 7308 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; 7309 if (BO->Op) 7310 Flags = getNoWrapFlagsFromUB(BO->Op); 7311 LHS = getSCEV(BO->LHS); 7312 RHS = getSCEV(BO->RHS); 7313 return getMinusSCEV(LHS, RHS, Flags); 7314 } 7315 case Instruction::And: 7316 // For an expression like x&255 that merely masks off the high bits, 7317 // use zext(trunc(x)) as the SCEV expression. 7318 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) { 7319 if (CI->isZero()) 7320 return getSCEV(BO->RHS); 7321 if (CI->isMinusOne()) 7322 return getSCEV(BO->LHS); 7323 const APInt &A = CI->getValue(); 7324 7325 // Instcombine's ShrinkDemandedConstant may strip bits out of 7326 // constants, obscuring what would otherwise be a low-bits mask. 7327 // Use computeKnownBits to compute what ShrinkDemandedConstant 7328 // knew about to reconstruct a low-bits mask value. 7329 unsigned LZ = A.countLeadingZeros(); 7330 unsigned TZ = A.countTrailingZeros(); 7331 unsigned BitWidth = A.getBitWidth(); 7332 KnownBits Known(BitWidth); 7333 computeKnownBits(BO->LHS, Known, getDataLayout(), 7334 0, &AC, nullptr, &DT); 7335 7336 APInt EffectiveMask = 7337 APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ); 7338 if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) { 7339 const SCEV *MulCount = getConstant(APInt::getOneBitSet(BitWidth, TZ)); 7340 const SCEV *LHS = getSCEV(BO->LHS); 7341 const SCEV *ShiftedLHS = nullptr; 7342 if (auto *LHSMul = dyn_cast<SCEVMulExpr>(LHS)) { 7343 if (auto *OpC = dyn_cast<SCEVConstant>(LHSMul->getOperand(0))) { 7344 // For an expression like (x * 8) & 8, simplify the multiply. 7345 unsigned MulZeros = OpC->getAPInt().countTrailingZeros(); 7346 unsigned GCD = std::min(MulZeros, TZ); 7347 APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD); 7348 SmallVector<const SCEV*, 4> MulOps; 7349 MulOps.push_back(getConstant(OpC->getAPInt().lshr(GCD))); 7350 MulOps.append(LHSMul->op_begin() + 1, LHSMul->op_end()); 7351 auto *NewMul = getMulExpr(MulOps, LHSMul->getNoWrapFlags()); 7352 ShiftedLHS = getUDivExpr(NewMul, getConstant(DivAmt)); 7353 } 7354 } 7355 if (!ShiftedLHS) 7356 ShiftedLHS = getUDivExpr(LHS, MulCount); 7357 return getMulExpr( 7358 getZeroExtendExpr( 7359 getTruncateExpr(ShiftedLHS, 7360 IntegerType::get(getContext(), BitWidth - LZ - TZ)), 7361 BO->LHS->getType()), 7362 MulCount); 7363 } 7364 } 7365 // Binary `and` is a bit-wise `umin`. 7366 if (BO->LHS->getType()->isIntegerTy(1)) { 7367 LHS = getSCEV(BO->LHS); 7368 RHS = getSCEV(BO->RHS); 7369 return getUMinExpr(LHS, RHS); 7370 } 7371 break; 7372 7373 case Instruction::Or: 7374 // If the RHS of the Or is a constant, we may have something like: 7375 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop 7376 // optimizations will transparently handle this case. 7377 // 7378 // In order for this transformation to be safe, the LHS must be of the 7379 // form X*(2^n) and the Or constant must be less than 2^n. 7380 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) { 7381 const SCEV *LHS = getSCEV(BO->LHS); 7382 const APInt &CIVal = CI->getValue(); 7383 if (GetMinTrailingZeros(LHS) >= 7384 (CIVal.getBitWidth() - CIVal.countLeadingZeros())) { 7385 // Build a plain add SCEV. 7386 return getAddExpr(LHS, getSCEV(CI), 7387 (SCEV::NoWrapFlags)(SCEV::FlagNUW | SCEV::FlagNSW)); 7388 } 7389 } 7390 // Binary `or` is a bit-wise `umax`. 7391 if (BO->LHS->getType()->isIntegerTy(1)) { 7392 LHS = getSCEV(BO->LHS); 7393 RHS = getSCEV(BO->RHS); 7394 return getUMaxExpr(LHS, RHS); 7395 } 7396 break; 7397 7398 case Instruction::Xor: 7399 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) { 7400 // If the RHS of xor is -1, then this is a not operation. 7401 if (CI->isMinusOne()) 7402 return getNotSCEV(getSCEV(BO->LHS)); 7403 7404 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. 7405 // This is a variant of the check for xor with -1, and it handles 7406 // the case where instcombine has trimmed non-demanded bits out 7407 // of an xor with -1. 7408 if (auto *LBO = dyn_cast<BinaryOperator>(BO->LHS)) 7409 if (ConstantInt *LCI = dyn_cast<ConstantInt>(LBO->getOperand(1))) 7410 if (LBO->getOpcode() == Instruction::And && 7411 LCI->getValue() == CI->getValue()) 7412 if (const SCEVZeroExtendExpr *Z = 7413 dyn_cast<SCEVZeroExtendExpr>(getSCEV(BO->LHS))) { 7414 Type *UTy = BO->LHS->getType(); 7415 const SCEV *Z0 = Z->getOperand(); 7416 Type *Z0Ty = Z0->getType(); 7417 unsigned Z0TySize = getTypeSizeInBits(Z0Ty); 7418 7419 // If C is a low-bits mask, the zero extend is serving to 7420 // mask off the high bits. Complement the operand and 7421 // re-apply the zext. 7422 if (CI->getValue().isMask(Z0TySize)) 7423 return getZeroExtendExpr(getNotSCEV(Z0), UTy); 7424 7425 // If C is a single bit, it may be in the sign-bit position 7426 // before the zero-extend. In this case, represent the xor 7427 // using an add, which is equivalent, and re-apply the zext. 7428 APInt Trunc = CI->getValue().trunc(Z0TySize); 7429 if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() && 7430 Trunc.isSignMask()) 7431 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), 7432 UTy); 7433 } 7434 } 7435 break; 7436 7437 case Instruction::Shl: 7438 // Turn shift left of a constant amount into a multiply. 7439 if (ConstantInt *SA = dyn_cast<ConstantInt>(BO->RHS)) { 7440 uint32_t BitWidth = cast<IntegerType>(SA->getType())->getBitWidth(); 7441 7442 // If the shift count is not less than the bitwidth, the result of 7443 // the shift is undefined. Don't try to analyze it, because the 7444 // resolution chosen here may differ from the resolution chosen in 7445 // other parts of the compiler. 7446 if (SA->getValue().uge(BitWidth)) 7447 break; 7448 7449 // We can safely preserve the nuw flag in all cases. It's also safe to 7450 // turn a nuw nsw shl into a nuw nsw mul. However, nsw in isolation 7451 // requires special handling. It can be preserved as long as we're not 7452 // left shifting by bitwidth - 1. 7453 auto Flags = SCEV::FlagAnyWrap; 7454 if (BO->Op) { 7455 auto MulFlags = getNoWrapFlagsFromUB(BO->Op); 7456 if ((MulFlags & SCEV::FlagNSW) && 7457 ((MulFlags & SCEV::FlagNUW) || SA->getValue().ult(BitWidth - 1))) 7458 Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNSW); 7459 if (MulFlags & SCEV::FlagNUW) 7460 Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNUW); 7461 } 7462 7463 ConstantInt *X = ConstantInt::get( 7464 getContext(), APInt::getOneBitSet(BitWidth, SA->getZExtValue())); 7465 return getMulExpr(getSCEV(BO->LHS), getConstant(X), Flags); 7466 } 7467 break; 7468 7469 case Instruction::AShr: { 7470 // AShr X, C, where C is a constant. 7471 ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS); 7472 if (!CI) 7473 break; 7474 7475 Type *OuterTy = BO->LHS->getType(); 7476 uint64_t BitWidth = getTypeSizeInBits(OuterTy); 7477 // If the shift count is not less than the bitwidth, the result of 7478 // the shift is undefined. Don't try to analyze it, because the 7479 // resolution chosen here may differ from the resolution chosen in 7480 // other parts of the compiler. 7481 if (CI->getValue().uge(BitWidth)) 7482 break; 7483 7484 if (CI->isZero()) 7485 return getSCEV(BO->LHS); // shift by zero --> noop 7486 7487 uint64_t AShrAmt = CI->getZExtValue(); 7488 Type *TruncTy = IntegerType::get(getContext(), BitWidth - AShrAmt); 7489 7490 Operator *L = dyn_cast<Operator>(BO->LHS); 7491 if (L && L->getOpcode() == Instruction::Shl) { 7492 // X = Shl A, n 7493 // Y = AShr X, m 7494 // Both n and m are constant. 7495 7496 const SCEV *ShlOp0SCEV = getSCEV(L->getOperand(0)); 7497 if (L->getOperand(1) == BO->RHS) 7498 // For a two-shift sext-inreg, i.e. n = m, 7499 // use sext(trunc(x)) as the SCEV expression. 7500 return getSignExtendExpr( 7501 getTruncateExpr(ShlOp0SCEV, TruncTy), OuterTy); 7502 7503 ConstantInt *ShlAmtCI = dyn_cast<ConstantInt>(L->getOperand(1)); 7504 if (ShlAmtCI && ShlAmtCI->getValue().ult(BitWidth)) { 7505 uint64_t ShlAmt = ShlAmtCI->getZExtValue(); 7506 if (ShlAmt > AShrAmt) { 7507 // When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV 7508 // expression. We already checked that ShlAmt < BitWidth, so 7509 // the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as 7510 // ShlAmt - AShrAmt < Amt. 7511 APInt Mul = APInt::getOneBitSet(BitWidth - AShrAmt, 7512 ShlAmt - AShrAmt); 7513 return getSignExtendExpr( 7514 getMulExpr(getTruncateExpr(ShlOp0SCEV, TruncTy), 7515 getConstant(Mul)), OuterTy); 7516 } 7517 } 7518 } 7519 break; 7520 } 7521 } 7522 } 7523 7524 switch (U->getOpcode()) { 7525 case Instruction::Trunc: 7526 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); 7527 7528 case Instruction::ZExt: 7529 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); 7530 7531 case Instruction::SExt: 7532 if (auto BO = MatchBinaryOp(U->getOperand(0), DT)) { 7533 // The NSW flag of a subtract does not always survive the conversion to 7534 // A + (-1)*B. By pushing sign extension onto its operands we are much 7535 // more likely to preserve NSW and allow later AddRec optimisations. 7536 // 7537 // NOTE: This is effectively duplicating this logic from getSignExtend: 7538 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw> 7539 // but by that point the NSW information has potentially been lost. 7540 if (BO->Opcode == Instruction::Sub && BO->IsNSW) { 7541 Type *Ty = U->getType(); 7542 auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty); 7543 auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty); 7544 return getMinusSCEV(V1, V2, SCEV::FlagNSW); 7545 } 7546 } 7547 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); 7548 7549 case Instruction::BitCast: 7550 // BitCasts are no-op casts so we just eliminate the cast. 7551 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) 7552 return getSCEV(U->getOperand(0)); 7553 break; 7554 7555 case Instruction::PtrToInt: { 7556 // Pointer to integer cast is straight-forward, so do model it. 7557 const SCEV *Op = getSCEV(U->getOperand(0)); 7558 Type *DstIntTy = U->getType(); 7559 // But only if effective SCEV (integer) type is wide enough to represent 7560 // all possible pointer values. 7561 const SCEV *IntOp = getPtrToIntExpr(Op, DstIntTy); 7562 if (isa<SCEVCouldNotCompute>(IntOp)) 7563 return getUnknown(V); 7564 return IntOp; 7565 } 7566 case Instruction::IntToPtr: 7567 // Just don't deal with inttoptr casts. 7568 return getUnknown(V); 7569 7570 case Instruction::SDiv: 7571 // If both operands are non-negative, this is just an udiv. 7572 if (isKnownNonNegative(getSCEV(U->getOperand(0))) && 7573 isKnownNonNegative(getSCEV(U->getOperand(1)))) 7574 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1))); 7575 break; 7576 7577 case Instruction::SRem: 7578 // If both operands are non-negative, this is just an urem. 7579 if (isKnownNonNegative(getSCEV(U->getOperand(0))) && 7580 isKnownNonNegative(getSCEV(U->getOperand(1)))) 7581 return getURemExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1))); 7582 break; 7583 7584 case Instruction::GetElementPtr: 7585 return createNodeForGEP(cast<GEPOperator>(U)); 7586 7587 case Instruction::PHI: 7588 return createNodeForPHI(cast<PHINode>(U)); 7589 7590 case Instruction::Select: 7591 return createNodeForSelectOrPHI(U, U->getOperand(0), U->getOperand(1), 7592 U->getOperand(2)); 7593 7594 case Instruction::Call: 7595 case Instruction::Invoke: 7596 if (Value *RV = cast<CallBase>(U)->getReturnedArgOperand()) 7597 return getSCEV(RV); 7598 7599 if (auto *II = dyn_cast<IntrinsicInst>(U)) { 7600 switch (II->getIntrinsicID()) { 7601 case Intrinsic::abs: 7602 return getAbsExpr( 7603 getSCEV(II->getArgOperand(0)), 7604 /*IsNSW=*/cast<ConstantInt>(II->getArgOperand(1))->isOne()); 7605 case Intrinsic::umax: 7606 LHS = getSCEV(II->getArgOperand(0)); 7607 RHS = getSCEV(II->getArgOperand(1)); 7608 return getUMaxExpr(LHS, RHS); 7609 case Intrinsic::umin: 7610 LHS = getSCEV(II->getArgOperand(0)); 7611 RHS = getSCEV(II->getArgOperand(1)); 7612 return getUMinExpr(LHS, RHS); 7613 case Intrinsic::smax: 7614 LHS = getSCEV(II->getArgOperand(0)); 7615 RHS = getSCEV(II->getArgOperand(1)); 7616 return getSMaxExpr(LHS, RHS); 7617 case Intrinsic::smin: 7618 LHS = getSCEV(II->getArgOperand(0)); 7619 RHS = getSCEV(II->getArgOperand(1)); 7620 return getSMinExpr(LHS, RHS); 7621 case Intrinsic::usub_sat: { 7622 const SCEV *X = getSCEV(II->getArgOperand(0)); 7623 const SCEV *Y = getSCEV(II->getArgOperand(1)); 7624 const SCEV *ClampedY = getUMinExpr(X, Y); 7625 return getMinusSCEV(X, ClampedY, SCEV::FlagNUW); 7626 } 7627 case Intrinsic::uadd_sat: { 7628 const SCEV *X = getSCEV(II->getArgOperand(0)); 7629 const SCEV *Y = getSCEV(II->getArgOperand(1)); 7630 const SCEV *ClampedX = getUMinExpr(X, getNotSCEV(Y)); 7631 return getAddExpr(ClampedX, Y, SCEV::FlagNUW); 7632 } 7633 case Intrinsic::start_loop_iterations: 7634 // A start_loop_iterations is just equivalent to the first operand for 7635 // SCEV purposes. 7636 return getSCEV(II->getArgOperand(0)); 7637 default: 7638 break; 7639 } 7640 } 7641 break; 7642 } 7643 7644 return getUnknown(V); 7645 } 7646 7647 //===----------------------------------------------------------------------===// 7648 // Iteration Count Computation Code 7649 // 7650 7651 const SCEV *ScalarEvolution::getTripCountFromExitCount(const SCEV *ExitCount, 7652 bool Extend) { 7653 if (isa<SCEVCouldNotCompute>(ExitCount)) 7654 return getCouldNotCompute(); 7655 7656 auto *ExitCountType = ExitCount->getType(); 7657 assert(ExitCountType->isIntegerTy()); 7658 7659 if (!Extend) 7660 return getAddExpr(ExitCount, getOne(ExitCountType)); 7661 7662 auto *WiderType = Type::getIntNTy(ExitCountType->getContext(), 7663 1 + ExitCountType->getScalarSizeInBits()); 7664 return getAddExpr(getNoopOrZeroExtend(ExitCount, WiderType), 7665 getOne(WiderType)); 7666 } 7667 7668 static unsigned getConstantTripCount(const SCEVConstant *ExitCount) { 7669 if (!ExitCount) 7670 return 0; 7671 7672 ConstantInt *ExitConst = ExitCount->getValue(); 7673 7674 // Guard against huge trip counts. 7675 if (ExitConst->getValue().getActiveBits() > 32) 7676 return 0; 7677 7678 // In case of integer overflow, this returns 0, which is correct. 7679 return ((unsigned)ExitConst->getZExtValue()) + 1; 7680 } 7681 7682 unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L) { 7683 auto *ExitCount = dyn_cast<SCEVConstant>(getBackedgeTakenCount(L, Exact)); 7684 return getConstantTripCount(ExitCount); 7685 } 7686 7687 unsigned 7688 ScalarEvolution::getSmallConstantTripCount(const Loop *L, 7689 const BasicBlock *ExitingBlock) { 7690 assert(ExitingBlock && "Must pass a non-null exiting block!"); 7691 assert(L->isLoopExiting(ExitingBlock) && 7692 "Exiting block must actually branch out of the loop!"); 7693 const SCEVConstant *ExitCount = 7694 dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock)); 7695 return getConstantTripCount(ExitCount); 7696 } 7697 7698 unsigned ScalarEvolution::getSmallConstantMaxTripCount(const Loop *L) { 7699 const auto *MaxExitCount = 7700 dyn_cast<SCEVConstant>(getConstantMaxBackedgeTakenCount(L)); 7701 return getConstantTripCount(MaxExitCount); 7702 } 7703 7704 const SCEV *ScalarEvolution::getConstantMaxTripCountFromArray(const Loop *L) { 7705 // We can't infer from Array in Irregular Loop. 7706 // FIXME: It's hard to infer loop bound from array operated in Nested Loop. 7707 if (!L->isLoopSimplifyForm() || !L->isInnermost()) 7708 return getCouldNotCompute(); 7709 7710 // FIXME: To make the scene more typical, we only analysis loops that have 7711 // one exiting block and that block must be the latch. To make it easier to 7712 // capture loops that have memory access and memory access will be executed 7713 // in each iteration. 7714 const BasicBlock *LoopLatch = L->getLoopLatch(); 7715 assert(LoopLatch && "See defination of simplify form loop."); 7716 if (L->getExitingBlock() != LoopLatch) 7717 return getCouldNotCompute(); 7718 7719 const DataLayout &DL = getDataLayout(); 7720 SmallVector<const SCEV *> InferCountColl; 7721 for (auto *BB : L->getBlocks()) { 7722 // Go here, we can know that Loop is a single exiting and simplified form 7723 // loop. Make sure that infer from Memory Operation in those BBs must be 7724 // executed in loop. First step, we can make sure that max execution time 7725 // of MemAccessBB in loop represents latch max excution time. 7726 // If MemAccessBB does not dom Latch, skip. 7727 // Entry 7728 // │ 7729 // ┌─────▼─────┐ 7730 // │Loop Header◄─────┐ 7731 // └──┬──────┬─┘ │ 7732 // │ │ │ 7733 // ┌────────▼──┐ ┌─▼─────┐ │ 7734 // │MemAccessBB│ │OtherBB│ │ 7735 // └────────┬──┘ └─┬─────┘ │ 7736 // │ │ │ 7737 // ┌─▼──────▼─┐ │ 7738 // │Loop Latch├─────┘ 7739 // └────┬─────┘ 7740 // ▼ 7741 // Exit 7742 if (!DT.dominates(BB, LoopLatch)) 7743 continue; 7744 7745 for (Instruction &Inst : *BB) { 7746 // Find Memory Operation Instruction. 7747 auto *GEP = getLoadStorePointerOperand(&Inst); 7748 if (!GEP) 7749 continue; 7750 7751 auto *ElemSize = dyn_cast<SCEVConstant>(getElementSize(&Inst)); 7752 // Do not infer from scalar type, eg."ElemSize = sizeof()". 7753 if (!ElemSize) 7754 continue; 7755 7756 // Use a existing polynomial recurrence on the trip count. 7757 auto *AddRec = dyn_cast<SCEVAddRecExpr>(getSCEV(GEP)); 7758 if (!AddRec) 7759 continue; 7760 auto *ArrBase = dyn_cast<SCEVUnknown>(getPointerBase(AddRec)); 7761 auto *Step = dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(*this)); 7762 if (!ArrBase || !Step) 7763 continue; 7764 assert(isLoopInvariant(ArrBase, L) && "See addrec definition"); 7765 7766 // Only handle { %array + step }, 7767 // FIXME: {(SCEVAddRecExpr) + step } could not be analysed here. 7768 if (AddRec->getStart() != ArrBase) 7769 continue; 7770 7771 // Memory operation pattern which have gaps. 7772 // Or repeat memory opreation. 7773 // And index of GEP wraps arround. 7774 if (Step->getAPInt().getActiveBits() > 32 || 7775 Step->getAPInt().getZExtValue() != 7776 ElemSize->getAPInt().getZExtValue() || 7777 Step->isZero() || Step->getAPInt().isNegative()) 7778 continue; 7779 7780 // Only infer from stack array which has certain size. 7781 // Make sure alloca instruction is not excuted in loop. 7782 AllocaInst *AllocateInst = dyn_cast<AllocaInst>(ArrBase->getValue()); 7783 if (!AllocateInst || L->contains(AllocateInst->getParent())) 7784 continue; 7785 7786 // Make sure only handle normal array. 7787 auto *Ty = dyn_cast<ArrayType>(AllocateInst->getAllocatedType()); 7788 auto *ArrSize = dyn_cast<ConstantInt>(AllocateInst->getArraySize()); 7789 if (!Ty || !ArrSize || !ArrSize->isOne()) 7790 continue; 7791 7792 // FIXME: Since gep indices are silently zext to the indexing type, 7793 // we will have a narrow gep index which wraps around rather than 7794 // increasing strictly, we shoule ensure that step is increasing 7795 // strictly by the loop iteration. 7796 // Now we can infer a max execution time by MemLength/StepLength. 7797 const SCEV *MemSize = 7798 getConstant(Step->getType(), DL.getTypeAllocSize(Ty)); 7799 auto *MaxExeCount = 7800 dyn_cast<SCEVConstant>(getUDivCeilSCEV(MemSize, Step)); 7801 if (!MaxExeCount || MaxExeCount->getAPInt().getActiveBits() > 32) 7802 continue; 7803 7804 // If the loop reaches the maximum number of executions, we can not 7805 // access bytes starting outside the statically allocated size without 7806 // being immediate UB. But it is allowed to enter loop header one more 7807 // time. 7808 auto *InferCount = dyn_cast<SCEVConstant>( 7809 getAddExpr(MaxExeCount, getOne(MaxExeCount->getType()))); 7810 // Discard the maximum number of execution times under 32bits. 7811 if (!InferCount || InferCount->getAPInt().getActiveBits() > 32) 7812 continue; 7813 7814 InferCountColl.push_back(InferCount); 7815 } 7816 } 7817 7818 if (InferCountColl.size() == 0) 7819 return getCouldNotCompute(); 7820 7821 return getUMinFromMismatchedTypes(InferCountColl); 7822 } 7823 7824 unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) { 7825 SmallVector<BasicBlock *, 8> ExitingBlocks; 7826 L->getExitingBlocks(ExitingBlocks); 7827 7828 Optional<unsigned> Res = None; 7829 for (auto *ExitingBB : ExitingBlocks) { 7830 unsigned Multiple = getSmallConstantTripMultiple(L, ExitingBB); 7831 if (!Res) 7832 Res = Multiple; 7833 Res = (unsigned)GreatestCommonDivisor64(*Res, Multiple); 7834 } 7835 return Res.getValueOr(1); 7836 } 7837 7838 unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L, 7839 const SCEV *ExitCount) { 7840 if (ExitCount == getCouldNotCompute()) 7841 return 1; 7842 7843 // Get the trip count 7844 const SCEV *TCExpr = getTripCountFromExitCount(ExitCount); 7845 7846 const SCEVConstant *TC = dyn_cast<SCEVConstant>(TCExpr); 7847 if (!TC) 7848 // Attempt to factor more general cases. Returns the greatest power of 7849 // two divisor. If overflow happens, the trip count expression is still 7850 // divisible by the greatest power of 2 divisor returned. 7851 return 1U << std::min((uint32_t)31, 7852 GetMinTrailingZeros(applyLoopGuards(TCExpr, L))); 7853 7854 ConstantInt *Result = TC->getValue(); 7855 7856 // Guard against huge trip counts (this requires checking 7857 // for zero to handle the case where the trip count == -1 and the 7858 // addition wraps). 7859 if (!Result || Result->getValue().getActiveBits() > 32 || 7860 Result->getValue().getActiveBits() == 0) 7861 return 1; 7862 7863 return (unsigned)Result->getZExtValue(); 7864 } 7865 7866 /// Returns the largest constant divisor of the trip count of this loop as a 7867 /// normal unsigned value, if possible. This means that the actual trip count is 7868 /// always a multiple of the returned value (don't forget the trip count could 7869 /// very well be zero as well!). 7870 /// 7871 /// Returns 1 if the trip count is unknown or not guaranteed to be the 7872 /// multiple of a constant (which is also the case if the trip count is simply 7873 /// constant, use getSmallConstantTripCount for that case), Will also return 1 7874 /// if the trip count is very large (>= 2^32). 7875 /// 7876 /// As explained in the comments for getSmallConstantTripCount, this assumes 7877 /// that control exits the loop via ExitingBlock. 7878 unsigned 7879 ScalarEvolution::getSmallConstantTripMultiple(const Loop *L, 7880 const BasicBlock *ExitingBlock) { 7881 assert(ExitingBlock && "Must pass a non-null exiting block!"); 7882 assert(L->isLoopExiting(ExitingBlock) && 7883 "Exiting block must actually branch out of the loop!"); 7884 const SCEV *ExitCount = getExitCount(L, ExitingBlock); 7885 return getSmallConstantTripMultiple(L, ExitCount); 7886 } 7887 7888 const SCEV *ScalarEvolution::getExitCount(const Loop *L, 7889 const BasicBlock *ExitingBlock, 7890 ExitCountKind Kind) { 7891 switch (Kind) { 7892 case Exact: 7893 case SymbolicMaximum: 7894 return getBackedgeTakenInfo(L).getExact(ExitingBlock, this); 7895 case ConstantMaximum: 7896 return getBackedgeTakenInfo(L).getConstantMax(ExitingBlock, this); 7897 }; 7898 llvm_unreachable("Invalid ExitCountKind!"); 7899 } 7900 7901 const SCEV * 7902 ScalarEvolution::getPredicatedBackedgeTakenCount(const Loop *L, 7903 SmallVector<const SCEVPredicate *, 4> &Preds) { 7904 return getPredicatedBackedgeTakenInfo(L).getExact(L, this, &Preds); 7905 } 7906 7907 const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L, 7908 ExitCountKind Kind) { 7909 switch (Kind) { 7910 case Exact: 7911 return getBackedgeTakenInfo(L).getExact(L, this); 7912 case ConstantMaximum: 7913 return getBackedgeTakenInfo(L).getConstantMax(this); 7914 case SymbolicMaximum: 7915 return getBackedgeTakenInfo(L).getSymbolicMax(L, this); 7916 }; 7917 llvm_unreachable("Invalid ExitCountKind!"); 7918 } 7919 7920 bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) { 7921 return getBackedgeTakenInfo(L).isConstantMaxOrZero(this); 7922 } 7923 7924 /// Push PHI nodes in the header of the given loop onto the given Worklist. 7925 static void PushLoopPHIs(const Loop *L, 7926 SmallVectorImpl<Instruction *> &Worklist, 7927 SmallPtrSetImpl<Instruction *> &Visited) { 7928 BasicBlock *Header = L->getHeader(); 7929 7930 // Push all Loop-header PHIs onto the Worklist stack. 7931 for (PHINode &PN : Header->phis()) 7932 if (Visited.insert(&PN).second) 7933 Worklist.push_back(&PN); 7934 } 7935 7936 const ScalarEvolution::BackedgeTakenInfo & 7937 ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) { 7938 auto &BTI = getBackedgeTakenInfo(L); 7939 if (BTI.hasFullInfo()) 7940 return BTI; 7941 7942 auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()}); 7943 7944 if (!Pair.second) 7945 return Pair.first->second; 7946 7947 BackedgeTakenInfo Result = 7948 computeBackedgeTakenCount(L, /*AllowPredicates=*/true); 7949 7950 return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result); 7951 } 7952 7953 ScalarEvolution::BackedgeTakenInfo & 7954 ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { 7955 // Initially insert an invalid entry for this loop. If the insertion 7956 // succeeds, proceed to actually compute a backedge-taken count and 7957 // update the value. The temporary CouldNotCompute value tells SCEV 7958 // code elsewhere that it shouldn't attempt to request a new 7959 // backedge-taken count, which could result in infinite recursion. 7960 std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair = 7961 BackedgeTakenCounts.insert({L, BackedgeTakenInfo()}); 7962 if (!Pair.second) 7963 return Pair.first->second; 7964 7965 // computeBackedgeTakenCount may allocate memory for its result. Inserting it 7966 // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result 7967 // must be cleared in this scope. 7968 BackedgeTakenInfo Result = computeBackedgeTakenCount(L); 7969 7970 // In product build, there are no usage of statistic. 7971 (void)NumTripCountsComputed; 7972 (void)NumTripCountsNotComputed; 7973 #if LLVM_ENABLE_STATS || !defined(NDEBUG) 7974 const SCEV *BEExact = Result.getExact(L, this); 7975 if (BEExact != getCouldNotCompute()) { 7976 assert(isLoopInvariant(BEExact, L) && 7977 isLoopInvariant(Result.getConstantMax(this), L) && 7978 "Computed backedge-taken count isn't loop invariant for loop!"); 7979 ++NumTripCountsComputed; 7980 } else if (Result.getConstantMax(this) == getCouldNotCompute() && 7981 isa<PHINode>(L->getHeader()->begin())) { 7982 // Only count loops that have phi nodes as not being computable. 7983 ++NumTripCountsNotComputed; 7984 } 7985 #endif // LLVM_ENABLE_STATS || !defined(NDEBUG) 7986 7987 // Now that we know more about the trip count for this loop, forget any 7988 // existing SCEV values for PHI nodes in this loop since they are only 7989 // conservative estimates made without the benefit of trip count 7990 // information. This invalidation is not necessary for correctness, and is 7991 // only done to produce more precise results. 7992 if (Result.hasAnyInfo()) { 7993 // Invalidate any expression using an addrec in this loop. 7994 SmallVector<const SCEV *, 8> ToForget; 7995 auto LoopUsersIt = LoopUsers.find(L); 7996 if (LoopUsersIt != LoopUsers.end()) 7997 append_range(ToForget, LoopUsersIt->second); 7998 forgetMemoizedResults(ToForget); 7999 8000 // Invalidate constant-evolved loop header phis. 8001 for (PHINode &PN : L->getHeader()->phis()) 8002 ConstantEvolutionLoopExitValue.erase(&PN); 8003 } 8004 8005 // Re-lookup the insert position, since the call to 8006 // computeBackedgeTakenCount above could result in a 8007 // recusive call to getBackedgeTakenInfo (on a different 8008 // loop), which would invalidate the iterator computed 8009 // earlier. 8010 return BackedgeTakenCounts.find(L)->second = std::move(Result); 8011 } 8012 8013 void ScalarEvolution::forgetAllLoops() { 8014 // This method is intended to forget all info about loops. It should 8015 // invalidate caches as if the following happened: 8016 // - The trip counts of all loops have changed arbitrarily 8017 // - Every llvm::Value has been updated in place to produce a different 8018 // result. 8019 BackedgeTakenCounts.clear(); 8020 PredicatedBackedgeTakenCounts.clear(); 8021 BECountUsers.clear(); 8022 LoopPropertiesCache.clear(); 8023 ConstantEvolutionLoopExitValue.clear(); 8024 ValueExprMap.clear(); 8025 ValuesAtScopes.clear(); 8026 ValuesAtScopesUsers.clear(); 8027 LoopDispositions.clear(); 8028 BlockDispositions.clear(); 8029 UnsignedRanges.clear(); 8030 SignedRanges.clear(); 8031 ExprValueMap.clear(); 8032 HasRecMap.clear(); 8033 MinTrailingZerosCache.clear(); 8034 PredicatedSCEVRewrites.clear(); 8035 } 8036 8037 void ScalarEvolution::forgetLoop(const Loop *L) { 8038 SmallVector<const Loop *, 16> LoopWorklist(1, L); 8039 SmallVector<Instruction *, 32> Worklist; 8040 SmallPtrSet<Instruction *, 16> Visited; 8041 SmallVector<const SCEV *, 16> ToForget; 8042 8043 // Iterate over all the loops and sub-loops to drop SCEV information. 8044 while (!LoopWorklist.empty()) { 8045 auto *CurrL = LoopWorklist.pop_back_val(); 8046 8047 // Drop any stored trip count value. 8048 forgetBackedgeTakenCounts(CurrL, /* Predicated */ false); 8049 forgetBackedgeTakenCounts(CurrL, /* Predicated */ true); 8050 8051 // Drop information about predicated SCEV rewrites for this loop. 8052 for (auto I = PredicatedSCEVRewrites.begin(); 8053 I != PredicatedSCEVRewrites.end();) { 8054 std::pair<const SCEV *, const Loop *> Entry = I->first; 8055 if (Entry.second == CurrL) 8056 PredicatedSCEVRewrites.erase(I++); 8057 else 8058 ++I; 8059 } 8060 8061 auto LoopUsersItr = LoopUsers.find(CurrL); 8062 if (LoopUsersItr != LoopUsers.end()) { 8063 ToForget.insert(ToForget.end(), LoopUsersItr->second.begin(), 8064 LoopUsersItr->second.end()); 8065 } 8066 8067 // Drop information about expressions based on loop-header PHIs. 8068 PushLoopPHIs(CurrL, Worklist, Visited); 8069 8070 while (!Worklist.empty()) { 8071 Instruction *I = Worklist.pop_back_val(); 8072 8073 ValueExprMapType::iterator It = 8074 ValueExprMap.find_as(static_cast<Value *>(I)); 8075 if (It != ValueExprMap.end()) { 8076 eraseValueFromMap(It->first); 8077 ToForget.push_back(It->second); 8078 if (PHINode *PN = dyn_cast<PHINode>(I)) 8079 ConstantEvolutionLoopExitValue.erase(PN); 8080 } 8081 8082 PushDefUseChildren(I, Worklist, Visited); 8083 } 8084 8085 LoopPropertiesCache.erase(CurrL); 8086 // Forget all contained loops too, to avoid dangling entries in the 8087 // ValuesAtScopes map. 8088 LoopWorklist.append(CurrL->begin(), CurrL->end()); 8089 } 8090 forgetMemoizedResults(ToForget); 8091 } 8092 8093 void ScalarEvolution::forgetTopmostLoop(const Loop *L) { 8094 while (Loop *Parent = L->getParentLoop()) 8095 L = Parent; 8096 forgetLoop(L); 8097 } 8098 8099 void ScalarEvolution::forgetValue(Value *V) { 8100 Instruction *I = dyn_cast<Instruction>(V); 8101 if (!I) return; 8102 8103 // Drop information about expressions based on loop-header PHIs. 8104 SmallVector<Instruction *, 16> Worklist; 8105 SmallPtrSet<Instruction *, 8> Visited; 8106 SmallVector<const SCEV *, 8> ToForget; 8107 Worklist.push_back(I); 8108 Visited.insert(I); 8109 8110 while (!Worklist.empty()) { 8111 I = Worklist.pop_back_val(); 8112 ValueExprMapType::iterator It = 8113 ValueExprMap.find_as(static_cast<Value *>(I)); 8114 if (It != ValueExprMap.end()) { 8115 eraseValueFromMap(It->first); 8116 ToForget.push_back(It->second); 8117 if (PHINode *PN = dyn_cast<PHINode>(I)) 8118 ConstantEvolutionLoopExitValue.erase(PN); 8119 } 8120 8121 PushDefUseChildren(I, Worklist, Visited); 8122 } 8123 forgetMemoizedResults(ToForget); 8124 } 8125 8126 void ScalarEvolution::forgetLoopDispositions(const Loop *L) { 8127 LoopDispositions.clear(); 8128 } 8129 8130 /// Get the exact loop backedge taken count considering all loop exits. A 8131 /// computable result can only be returned for loops with all exiting blocks 8132 /// dominating the latch. howFarToZero assumes that the limit of each loop test 8133 /// is never skipped. This is a valid assumption as long as the loop exits via 8134 /// that test. For precise results, it is the caller's responsibility to specify 8135 /// the relevant loop exiting block using getExact(ExitingBlock, SE). 8136 const SCEV * 8137 ScalarEvolution::BackedgeTakenInfo::getExact(const Loop *L, ScalarEvolution *SE, 8138 SmallVector<const SCEVPredicate *, 4> *Preds) const { 8139 // If any exits were not computable, the loop is not computable. 8140 if (!isComplete() || ExitNotTaken.empty()) 8141 return SE->getCouldNotCompute(); 8142 8143 const BasicBlock *Latch = L->getLoopLatch(); 8144 // All exiting blocks we have collected must dominate the only backedge. 8145 if (!Latch) 8146 return SE->getCouldNotCompute(); 8147 8148 // All exiting blocks we have gathered dominate loop's latch, so exact trip 8149 // count is simply a minimum out of all these calculated exit counts. 8150 SmallVector<const SCEV *, 2> Ops; 8151 for (auto &ENT : ExitNotTaken) { 8152 const SCEV *BECount = ENT.ExactNotTaken; 8153 assert(BECount != SE->getCouldNotCompute() && "Bad exit SCEV!"); 8154 assert(SE->DT.dominates(ENT.ExitingBlock, Latch) && 8155 "We should only have known counts for exiting blocks that dominate " 8156 "latch!"); 8157 8158 Ops.push_back(BECount); 8159 8160 if (Preds) 8161 for (auto *P : ENT.Predicates) 8162 Preds->push_back(P); 8163 8164 assert((Preds || ENT.hasAlwaysTruePredicate()) && 8165 "Predicate should be always true!"); 8166 } 8167 8168 // If an earlier exit exits on the first iteration (exit count zero), then 8169 // a later poison exit count should not propagate into the result. This are 8170 // exactly the semantics provided by umin_seq. 8171 return SE->getUMinFromMismatchedTypes(Ops, /* Sequential */ true); 8172 } 8173 8174 /// Get the exact not taken count for this loop exit. 8175 const SCEV * 8176 ScalarEvolution::BackedgeTakenInfo::getExact(const BasicBlock *ExitingBlock, 8177 ScalarEvolution *SE) const { 8178 for (auto &ENT : ExitNotTaken) 8179 if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate()) 8180 return ENT.ExactNotTaken; 8181 8182 return SE->getCouldNotCompute(); 8183 } 8184 8185 const SCEV *ScalarEvolution::BackedgeTakenInfo::getConstantMax( 8186 const BasicBlock *ExitingBlock, ScalarEvolution *SE) const { 8187 for (auto &ENT : ExitNotTaken) 8188 if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate()) 8189 return ENT.MaxNotTaken; 8190 8191 return SE->getCouldNotCompute(); 8192 } 8193 8194 /// getConstantMax - Get the constant max backedge taken count for the loop. 8195 const SCEV * 8196 ScalarEvolution::BackedgeTakenInfo::getConstantMax(ScalarEvolution *SE) const { 8197 auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) { 8198 return !ENT.hasAlwaysTruePredicate(); 8199 }; 8200 8201 if (!getConstantMax() || any_of(ExitNotTaken, PredicateNotAlwaysTrue)) 8202 return SE->getCouldNotCompute(); 8203 8204 assert((isa<SCEVCouldNotCompute>(getConstantMax()) || 8205 isa<SCEVConstant>(getConstantMax())) && 8206 "No point in having a non-constant max backedge taken count!"); 8207 return getConstantMax(); 8208 } 8209 8210 const SCEV * 8211 ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(const Loop *L, 8212 ScalarEvolution *SE) { 8213 if (!SymbolicMax) 8214 SymbolicMax = SE->computeSymbolicMaxBackedgeTakenCount(L); 8215 return SymbolicMax; 8216 } 8217 8218 bool ScalarEvolution::BackedgeTakenInfo::isConstantMaxOrZero( 8219 ScalarEvolution *SE) const { 8220 auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) { 8221 return !ENT.hasAlwaysTruePredicate(); 8222 }; 8223 return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue); 8224 } 8225 8226 ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E) 8227 : ExitLimit(E, E, false, None) { 8228 } 8229 8230 ScalarEvolution::ExitLimit::ExitLimit( 8231 const SCEV *E, const SCEV *M, bool MaxOrZero, 8232 ArrayRef<const SmallPtrSetImpl<const SCEVPredicate *> *> PredSetList) 8233 : ExactNotTaken(E), MaxNotTaken(M), MaxOrZero(MaxOrZero) { 8234 // If we prove the max count is zero, so is the symbolic bound. This happens 8235 // in practice due to differences in a) how context sensitive we've chosen 8236 // to be and b) how we reason about bounds impied by UB. 8237 if (MaxNotTaken->isZero()) 8238 ExactNotTaken = MaxNotTaken; 8239 8240 assert((isa<SCEVCouldNotCompute>(ExactNotTaken) || 8241 !isa<SCEVCouldNotCompute>(MaxNotTaken)) && 8242 "Exact is not allowed to be less precise than Max"); 8243 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) || 8244 isa<SCEVConstant>(MaxNotTaken)) && 8245 "No point in having a non-constant max backedge taken count!"); 8246 for (auto *PredSet : PredSetList) 8247 for (auto *P : *PredSet) 8248 addPredicate(P); 8249 assert((isa<SCEVCouldNotCompute>(E) || !E->getType()->isPointerTy()) && 8250 "Backedge count should be int"); 8251 assert((isa<SCEVCouldNotCompute>(M) || !M->getType()->isPointerTy()) && 8252 "Max backedge count should be int"); 8253 } 8254 8255 ScalarEvolution::ExitLimit::ExitLimit( 8256 const SCEV *E, const SCEV *M, bool MaxOrZero, 8257 const SmallPtrSetImpl<const SCEVPredicate *> &PredSet) 8258 : ExitLimit(E, M, MaxOrZero, {&PredSet}) { 8259 } 8260 8261 ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E, const SCEV *M, 8262 bool MaxOrZero) 8263 : ExitLimit(E, M, MaxOrZero, None) { 8264 } 8265 8266 /// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each 8267 /// computable exit into a persistent ExitNotTakenInfo array. 8268 ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo( 8269 ArrayRef<ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo> ExitCounts, 8270 bool IsComplete, const SCEV *ConstantMax, bool MaxOrZero) 8271 : ConstantMax(ConstantMax), IsComplete(IsComplete), MaxOrZero(MaxOrZero) { 8272 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo; 8273 8274 ExitNotTaken.reserve(ExitCounts.size()); 8275 std::transform( 8276 ExitCounts.begin(), ExitCounts.end(), std::back_inserter(ExitNotTaken), 8277 [&](const EdgeExitInfo &EEI) { 8278 BasicBlock *ExitBB = EEI.first; 8279 const ExitLimit &EL = EEI.second; 8280 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, EL.MaxNotTaken, 8281 EL.Predicates); 8282 }); 8283 assert((isa<SCEVCouldNotCompute>(ConstantMax) || 8284 isa<SCEVConstant>(ConstantMax)) && 8285 "No point in having a non-constant max backedge taken count!"); 8286 } 8287 8288 /// Compute the number of times the backedge of the specified loop will execute. 8289 ScalarEvolution::BackedgeTakenInfo 8290 ScalarEvolution::computeBackedgeTakenCount(const Loop *L, 8291 bool AllowPredicates) { 8292 SmallVector<BasicBlock *, 8> ExitingBlocks; 8293 L->getExitingBlocks(ExitingBlocks); 8294 8295 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo; 8296 8297 SmallVector<EdgeExitInfo, 4> ExitCounts; 8298 bool CouldComputeBECount = true; 8299 BasicBlock *Latch = L->getLoopLatch(); // may be NULL. 8300 const SCEV *MustExitMaxBECount = nullptr; 8301 const SCEV *MayExitMaxBECount = nullptr; 8302 bool MustExitMaxOrZero = false; 8303 8304 // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts 8305 // and compute maxBECount. 8306 // Do a union of all the predicates here. 8307 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { 8308 BasicBlock *ExitBB = ExitingBlocks[i]; 8309 8310 // We canonicalize untaken exits to br (constant), ignore them so that 8311 // proving an exit untaken doesn't negatively impact our ability to reason 8312 // about the loop as whole. 8313 if (auto *BI = dyn_cast<BranchInst>(ExitBB->getTerminator())) 8314 if (auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) { 8315 bool ExitIfTrue = !L->contains(BI->getSuccessor(0)); 8316 if (ExitIfTrue == CI->isZero()) 8317 continue; 8318 } 8319 8320 ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates); 8321 8322 assert((AllowPredicates || EL.Predicates.empty()) && 8323 "Predicated exit limit when predicates are not allowed!"); 8324 8325 // 1. For each exit that can be computed, add an entry to ExitCounts. 8326 // CouldComputeBECount is true only if all exits can be computed. 8327 if (EL.ExactNotTaken == getCouldNotCompute()) 8328 // We couldn't compute an exact value for this exit, so 8329 // we won't be able to compute an exact value for the loop. 8330 CouldComputeBECount = false; 8331 else 8332 ExitCounts.emplace_back(ExitBB, EL); 8333 8334 // 2. Derive the loop's MaxBECount from each exit's max number of 8335 // non-exiting iterations. Partition the loop exits into two kinds: 8336 // LoopMustExits and LoopMayExits. 8337 // 8338 // If the exit dominates the loop latch, it is a LoopMustExit otherwise it 8339 // is a LoopMayExit. If any computable LoopMustExit is found, then 8340 // MaxBECount is the minimum EL.MaxNotTaken of computable 8341 // LoopMustExits. Otherwise, MaxBECount is conservatively the maximum 8342 // EL.MaxNotTaken, where CouldNotCompute is considered greater than any 8343 // computable EL.MaxNotTaken. 8344 if (EL.MaxNotTaken != getCouldNotCompute() && Latch && 8345 DT.dominates(ExitBB, Latch)) { 8346 if (!MustExitMaxBECount) { 8347 MustExitMaxBECount = EL.MaxNotTaken; 8348 MustExitMaxOrZero = EL.MaxOrZero; 8349 } else { 8350 MustExitMaxBECount = 8351 getUMinFromMismatchedTypes(MustExitMaxBECount, EL.MaxNotTaken); 8352 } 8353 } else if (MayExitMaxBECount != getCouldNotCompute()) { 8354 if (!MayExitMaxBECount || EL.MaxNotTaken == getCouldNotCompute()) 8355 MayExitMaxBECount = EL.MaxNotTaken; 8356 else { 8357 MayExitMaxBECount = 8358 getUMaxFromMismatchedTypes(MayExitMaxBECount, EL.MaxNotTaken); 8359 } 8360 } 8361 } 8362 const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount : 8363 (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute()); 8364 // The loop backedge will be taken the maximum or zero times if there's 8365 // a single exit that must be taken the maximum or zero times. 8366 bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1); 8367 8368 // Remember which SCEVs are used in exit limits for invalidation purposes. 8369 // We only care about non-constant SCEVs here, so we can ignore EL.MaxNotTaken 8370 // and MaxBECount, which must be SCEVConstant. 8371 for (const auto &Pair : ExitCounts) 8372 if (!isa<SCEVConstant>(Pair.second.ExactNotTaken)) 8373 BECountUsers[Pair.second.ExactNotTaken].insert({L, AllowPredicates}); 8374 return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount, 8375 MaxBECount, MaxOrZero); 8376 } 8377 8378 ScalarEvolution::ExitLimit 8379 ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock, 8380 bool AllowPredicates) { 8381 assert(L->contains(ExitingBlock) && "Exit count for non-loop block?"); 8382 // If our exiting block does not dominate the latch, then its connection with 8383 // loop's exit limit may be far from trivial. 8384 const BasicBlock *Latch = L->getLoopLatch(); 8385 if (!Latch || !DT.dominates(ExitingBlock, Latch)) 8386 return getCouldNotCompute(); 8387 8388 bool IsOnlyExit = (L->getExitingBlock() != nullptr); 8389 Instruction *Term = ExitingBlock->getTerminator(); 8390 if (BranchInst *BI = dyn_cast<BranchInst>(Term)) { 8391 assert(BI->isConditional() && "If unconditional, it can't be in loop!"); 8392 bool ExitIfTrue = !L->contains(BI->getSuccessor(0)); 8393 assert(ExitIfTrue == L->contains(BI->getSuccessor(1)) && 8394 "It should have one successor in loop and one exit block!"); 8395 // Proceed to the next level to examine the exit condition expression. 8396 return computeExitLimitFromCond( 8397 L, BI->getCondition(), ExitIfTrue, 8398 /*ControlsExit=*/IsOnlyExit, AllowPredicates); 8399 } 8400 8401 if (SwitchInst *SI = dyn_cast<SwitchInst>(Term)) { 8402 // For switch, make sure that there is a single exit from the loop. 8403 BasicBlock *Exit = nullptr; 8404 for (auto *SBB : successors(ExitingBlock)) 8405 if (!L->contains(SBB)) { 8406 if (Exit) // Multiple exit successors. 8407 return getCouldNotCompute(); 8408 Exit = SBB; 8409 } 8410 assert(Exit && "Exiting block must have at least one exit"); 8411 return computeExitLimitFromSingleExitSwitch(L, SI, Exit, 8412 /*ControlsExit=*/IsOnlyExit); 8413 } 8414 8415 return getCouldNotCompute(); 8416 } 8417 8418 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond( 8419 const Loop *L, Value *ExitCond, bool ExitIfTrue, 8420 bool ControlsExit, bool AllowPredicates) { 8421 ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates); 8422 return computeExitLimitFromCondCached(Cache, L, ExitCond, ExitIfTrue, 8423 ControlsExit, AllowPredicates); 8424 } 8425 8426 Optional<ScalarEvolution::ExitLimit> 8427 ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond, 8428 bool ExitIfTrue, bool ControlsExit, 8429 bool AllowPredicates) { 8430 (void)this->L; 8431 (void)this->ExitIfTrue; 8432 (void)this->AllowPredicates; 8433 8434 assert(this->L == L && this->ExitIfTrue == ExitIfTrue && 8435 this->AllowPredicates == AllowPredicates && 8436 "Variance in assumed invariant key components!"); 8437 auto Itr = TripCountMap.find({ExitCond, ControlsExit}); 8438 if (Itr == TripCountMap.end()) 8439 return None; 8440 return Itr->second; 8441 } 8442 8443 void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond, 8444 bool ExitIfTrue, 8445 bool ControlsExit, 8446 bool AllowPredicates, 8447 const ExitLimit &EL) { 8448 assert(this->L == L && this->ExitIfTrue == ExitIfTrue && 8449 this->AllowPredicates == AllowPredicates && 8450 "Variance in assumed invariant key components!"); 8451 8452 auto InsertResult = TripCountMap.insert({{ExitCond, ControlsExit}, EL}); 8453 assert(InsertResult.second && "Expected successful insertion!"); 8454 (void)InsertResult; 8455 (void)ExitIfTrue; 8456 } 8457 8458 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached( 8459 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, 8460 bool ControlsExit, bool AllowPredicates) { 8461 8462 if (auto MaybeEL = 8463 Cache.find(L, ExitCond, ExitIfTrue, ControlsExit, AllowPredicates)) 8464 return *MaybeEL; 8465 8466 ExitLimit EL = computeExitLimitFromCondImpl(Cache, L, ExitCond, ExitIfTrue, 8467 ControlsExit, AllowPredicates); 8468 Cache.insert(L, ExitCond, ExitIfTrue, ControlsExit, AllowPredicates, EL); 8469 return EL; 8470 } 8471 8472 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl( 8473 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, 8474 bool ControlsExit, bool AllowPredicates) { 8475 // Handle BinOp conditions (And, Or). 8476 if (auto LimitFromBinOp = computeExitLimitFromCondFromBinOp( 8477 Cache, L, ExitCond, ExitIfTrue, ControlsExit, AllowPredicates)) 8478 return *LimitFromBinOp; 8479 8480 // With an icmp, it may be feasible to compute an exact backedge-taken count. 8481 // Proceed to the next level to examine the icmp. 8482 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) { 8483 ExitLimit EL = 8484 computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsExit); 8485 if (EL.hasFullInfo() || !AllowPredicates) 8486 return EL; 8487 8488 // Try again, but use SCEV predicates this time. 8489 return computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsExit, 8490 /*AllowPredicates=*/true); 8491 } 8492 8493 // Check for a constant condition. These are normally stripped out by 8494 // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to 8495 // preserve the CFG and is temporarily leaving constant conditions 8496 // in place. 8497 if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) { 8498 if (ExitIfTrue == !CI->getZExtValue()) 8499 // The backedge is always taken. 8500 return getCouldNotCompute(); 8501 else 8502 // The backedge is never taken. 8503 return getZero(CI->getType()); 8504 } 8505 8506 // If we're exiting based on the overflow flag of an x.with.overflow intrinsic 8507 // with a constant step, we can form an equivalent icmp predicate and figure 8508 // out how many iterations will be taken before we exit. 8509 const WithOverflowInst *WO; 8510 const APInt *C; 8511 if (match(ExitCond, m_ExtractValue<1>(m_WithOverflowInst(WO))) && 8512 match(WO->getRHS(), m_APInt(C))) { 8513 ConstantRange NWR = 8514 ConstantRange::makeExactNoWrapRegion(WO->getBinaryOp(), *C, 8515 WO->getNoWrapKind()); 8516 CmpInst::Predicate Pred; 8517 APInt NewRHSC, Offset; 8518 NWR.getEquivalentICmp(Pred, NewRHSC, Offset); 8519 if (!ExitIfTrue) 8520 Pred = ICmpInst::getInversePredicate(Pred); 8521 auto *LHS = getSCEV(WO->getLHS()); 8522 if (Offset != 0) 8523 LHS = getAddExpr(LHS, getConstant(Offset)); 8524 auto EL = computeExitLimitFromICmp(L, Pred, LHS, getConstant(NewRHSC), 8525 ControlsExit, AllowPredicates); 8526 if (EL.hasAnyInfo()) return EL; 8527 } 8528 8529 // If it's not an integer or pointer comparison then compute it the hard way. 8530 return computeExitCountExhaustively(L, ExitCond, ExitIfTrue); 8531 } 8532 8533 Optional<ScalarEvolution::ExitLimit> 8534 ScalarEvolution::computeExitLimitFromCondFromBinOp( 8535 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue, 8536 bool ControlsExit, bool AllowPredicates) { 8537 // Check if the controlling expression for this loop is an And or Or. 8538 Value *Op0, *Op1; 8539 bool IsAnd = false; 8540 if (match(ExitCond, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) 8541 IsAnd = true; 8542 else if (match(ExitCond, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) 8543 IsAnd = false; 8544 else 8545 return None; 8546 8547 // EitherMayExit is true in these two cases: 8548 // br (and Op0 Op1), loop, exit 8549 // br (or Op0 Op1), exit, loop 8550 bool EitherMayExit = IsAnd ^ ExitIfTrue; 8551 ExitLimit EL0 = computeExitLimitFromCondCached(Cache, L, Op0, ExitIfTrue, 8552 ControlsExit && !EitherMayExit, 8553 AllowPredicates); 8554 ExitLimit EL1 = computeExitLimitFromCondCached(Cache, L, Op1, ExitIfTrue, 8555 ControlsExit && !EitherMayExit, 8556 AllowPredicates); 8557 8558 // Be robust against unsimplified IR for the form "op i1 X, NeutralElement" 8559 const Constant *NeutralElement = ConstantInt::get(ExitCond->getType(), IsAnd); 8560 if (isa<ConstantInt>(Op1)) 8561 return Op1 == NeutralElement ? EL0 : EL1; 8562 if (isa<ConstantInt>(Op0)) 8563 return Op0 == NeutralElement ? EL1 : EL0; 8564 8565 const SCEV *BECount = getCouldNotCompute(); 8566 const SCEV *MaxBECount = getCouldNotCompute(); 8567 if (EitherMayExit) { 8568 // Both conditions must be same for the loop to continue executing. 8569 // Choose the less conservative count. 8570 if (EL0.ExactNotTaken != getCouldNotCompute() && 8571 EL1.ExactNotTaken != getCouldNotCompute()) { 8572 BECount = getUMinFromMismatchedTypes( 8573 EL0.ExactNotTaken, EL1.ExactNotTaken, 8574 /*Sequential=*/!isa<BinaryOperator>(ExitCond)); 8575 } 8576 if (EL0.MaxNotTaken == getCouldNotCompute()) 8577 MaxBECount = EL1.MaxNotTaken; 8578 else if (EL1.MaxNotTaken == getCouldNotCompute()) 8579 MaxBECount = EL0.MaxNotTaken; 8580 else 8581 MaxBECount = getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken); 8582 } else { 8583 // Both conditions must be same at the same time for the loop to exit. 8584 // For now, be conservative. 8585 if (EL0.ExactNotTaken == EL1.ExactNotTaken) 8586 BECount = EL0.ExactNotTaken; 8587 } 8588 8589 // There are cases (e.g. PR26207) where computeExitLimitFromCond is able 8590 // to be more aggressive when computing BECount than when computing 8591 // MaxBECount. In these cases it is possible for EL0.ExactNotTaken and 8592 // EL1.ExactNotTaken to match, but for EL0.MaxNotTaken and EL1.MaxNotTaken 8593 // to not. 8594 if (isa<SCEVCouldNotCompute>(MaxBECount) && 8595 !isa<SCEVCouldNotCompute>(BECount)) 8596 MaxBECount = getConstant(getUnsignedRangeMax(BECount)); 8597 8598 return ExitLimit(BECount, MaxBECount, false, 8599 { &EL0.Predicates, &EL1.Predicates }); 8600 } 8601 8602 ScalarEvolution::ExitLimit 8603 ScalarEvolution::computeExitLimitFromICmp(const Loop *L, 8604 ICmpInst *ExitCond, 8605 bool ExitIfTrue, 8606 bool ControlsExit, 8607 bool AllowPredicates) { 8608 // If the condition was exit on true, convert the condition to exit on false 8609 ICmpInst::Predicate Pred; 8610 if (!ExitIfTrue) 8611 Pred = ExitCond->getPredicate(); 8612 else 8613 Pred = ExitCond->getInversePredicate(); 8614 const ICmpInst::Predicate OriginalPred = Pred; 8615 8616 const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); 8617 const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); 8618 8619 ExitLimit EL = computeExitLimitFromICmp(L, Pred, LHS, RHS, ControlsExit, 8620 AllowPredicates); 8621 if (EL.hasAnyInfo()) return EL; 8622 8623 auto *ExhaustiveCount = 8624 computeExitCountExhaustively(L, ExitCond, ExitIfTrue); 8625 8626 if (!isa<SCEVCouldNotCompute>(ExhaustiveCount)) 8627 return ExhaustiveCount; 8628 8629 return computeShiftCompareExitLimit(ExitCond->getOperand(0), 8630 ExitCond->getOperand(1), L, OriginalPred); 8631 } 8632 ScalarEvolution::ExitLimit 8633 ScalarEvolution::computeExitLimitFromICmp(const Loop *L, 8634 ICmpInst::Predicate Pred, 8635 const SCEV *LHS, const SCEV *RHS, 8636 bool ControlsExit, 8637 bool AllowPredicates) { 8638 8639 // Try to evaluate any dependencies out of the loop. 8640 LHS = getSCEVAtScope(LHS, L); 8641 RHS = getSCEVAtScope(RHS, L); 8642 8643 // At this point, we would like to compute how many iterations of the 8644 // loop the predicate will return true for these inputs. 8645 if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) { 8646 // If there is a loop-invariant, force it into the RHS. 8647 std::swap(LHS, RHS); 8648 Pred = ICmpInst::getSwappedPredicate(Pred); 8649 } 8650 8651 bool ControllingFiniteLoop = 8652 ControlsExit && loopHasNoAbnormalExits(L) && loopIsFiniteByAssumption(L); 8653 // Simplify the operands before analyzing them. 8654 (void)SimplifyICmpOperands(Pred, LHS, RHS, /*Depth=*/0, 8655 (EnableFiniteLoopControl ? ControllingFiniteLoop 8656 : false)); 8657 8658 // If we have a comparison of a chrec against a constant, try to use value 8659 // ranges to answer this query. 8660 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) 8661 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) 8662 if (AddRec->getLoop() == L) { 8663 // Form the constant range. 8664 ConstantRange CompRange = 8665 ConstantRange::makeExactICmpRegion(Pred, RHSC->getAPInt()); 8666 8667 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); 8668 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; 8669 } 8670 8671 // If this loop must exit based on this condition (or execute undefined 8672 // behaviour), and we can prove the test sequence produced must repeat 8673 // the same values on self-wrap of the IV, then we can infer that IV 8674 // doesn't self wrap because if it did, we'd have an infinite (undefined) 8675 // loop. 8676 if (ControllingFiniteLoop && isLoopInvariant(RHS, L)) { 8677 // TODO: We can peel off any functions which are invertible *in L*. Loop 8678 // invariant terms are effectively constants for our purposes here. 8679 auto *InnerLHS = LHS; 8680 if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS)) 8681 InnerLHS = ZExt->getOperand(); 8682 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(InnerLHS)) { 8683 auto *StrideC = dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)); 8684 if (!AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() && 8685 StrideC && StrideC->getAPInt().isPowerOf2()) { 8686 auto Flags = AR->getNoWrapFlags(); 8687 Flags = setFlags(Flags, SCEV::FlagNW); 8688 SmallVector<const SCEV*> Operands{AR->operands()}; 8689 Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags); 8690 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags); 8691 } 8692 } 8693 } 8694 8695 switch (Pred) { 8696 case ICmpInst::ICMP_NE: { // while (X != Y) 8697 // Convert to: while (X-Y != 0) 8698 if (LHS->getType()->isPointerTy()) { 8699 LHS = getLosslessPtrToIntExpr(LHS); 8700 if (isa<SCEVCouldNotCompute>(LHS)) 8701 return LHS; 8702 } 8703 if (RHS->getType()->isPointerTy()) { 8704 RHS = getLosslessPtrToIntExpr(RHS); 8705 if (isa<SCEVCouldNotCompute>(RHS)) 8706 return RHS; 8707 } 8708 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit, 8709 AllowPredicates); 8710 if (EL.hasAnyInfo()) return EL; 8711 break; 8712 } 8713 case ICmpInst::ICMP_EQ: { // while (X == Y) 8714 // Convert to: while (X-Y == 0) 8715 if (LHS->getType()->isPointerTy()) { 8716 LHS = getLosslessPtrToIntExpr(LHS); 8717 if (isa<SCEVCouldNotCompute>(LHS)) 8718 return LHS; 8719 } 8720 if (RHS->getType()->isPointerTy()) { 8721 RHS = getLosslessPtrToIntExpr(RHS); 8722 if (isa<SCEVCouldNotCompute>(RHS)) 8723 return RHS; 8724 } 8725 ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L); 8726 if (EL.hasAnyInfo()) return EL; 8727 break; 8728 } 8729 case ICmpInst::ICMP_SLT: 8730 case ICmpInst::ICMP_ULT: { // while (X < Y) 8731 bool IsSigned = Pred == ICmpInst::ICMP_SLT; 8732 ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsExit, 8733 AllowPredicates); 8734 if (EL.hasAnyInfo()) return EL; 8735 break; 8736 } 8737 case ICmpInst::ICMP_SGT: 8738 case ICmpInst::ICMP_UGT: { // while (X > Y) 8739 bool IsSigned = Pred == ICmpInst::ICMP_SGT; 8740 ExitLimit EL = 8741 howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsExit, 8742 AllowPredicates); 8743 if (EL.hasAnyInfo()) return EL; 8744 break; 8745 } 8746 default: 8747 break; 8748 } 8749 8750 return getCouldNotCompute(); 8751 } 8752 8753 ScalarEvolution::ExitLimit 8754 ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L, 8755 SwitchInst *Switch, 8756 BasicBlock *ExitingBlock, 8757 bool ControlsExit) { 8758 assert(!L->contains(ExitingBlock) && "Not an exiting block!"); 8759 8760 // Give up if the exit is the default dest of a switch. 8761 if (Switch->getDefaultDest() == ExitingBlock) 8762 return getCouldNotCompute(); 8763 8764 assert(L->contains(Switch->getDefaultDest()) && 8765 "Default case must not exit the loop!"); 8766 const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L); 8767 const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock)); 8768 8769 // while (X != Y) --> while (X-Y != 0) 8770 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit); 8771 if (EL.hasAnyInfo()) 8772 return EL; 8773 8774 return getCouldNotCompute(); 8775 } 8776 8777 static ConstantInt * 8778 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, 8779 ScalarEvolution &SE) { 8780 const SCEV *InVal = SE.getConstant(C); 8781 const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); 8782 assert(isa<SCEVConstant>(Val) && 8783 "Evaluation of SCEV at constant didn't fold correctly?"); 8784 return cast<SCEVConstant>(Val)->getValue(); 8785 } 8786 8787 ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit( 8788 Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) { 8789 ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV); 8790 if (!RHS) 8791 return getCouldNotCompute(); 8792 8793 const BasicBlock *Latch = L->getLoopLatch(); 8794 if (!Latch) 8795 return getCouldNotCompute(); 8796 8797 const BasicBlock *Predecessor = L->getLoopPredecessor(); 8798 if (!Predecessor) 8799 return getCouldNotCompute(); 8800 8801 // Return true if V is of the form "LHS `shift_op` <positive constant>". 8802 // Return LHS in OutLHS and shift_opt in OutOpCode. 8803 auto MatchPositiveShift = 8804 [](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) { 8805 8806 using namespace PatternMatch; 8807 8808 ConstantInt *ShiftAmt; 8809 if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) 8810 OutOpCode = Instruction::LShr; 8811 else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) 8812 OutOpCode = Instruction::AShr; 8813 else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) 8814 OutOpCode = Instruction::Shl; 8815 else 8816 return false; 8817 8818 return ShiftAmt->getValue().isStrictlyPositive(); 8819 }; 8820 8821 // Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in 8822 // 8823 // loop: 8824 // %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ] 8825 // %iv.shifted = lshr i32 %iv, <positive constant> 8826 // 8827 // Return true on a successful match. Return the corresponding PHI node (%iv 8828 // above) in PNOut and the opcode of the shift operation in OpCodeOut. 8829 auto MatchShiftRecurrence = 8830 [&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) { 8831 Optional<Instruction::BinaryOps> PostShiftOpCode; 8832 8833 { 8834 Instruction::BinaryOps OpC; 8835 Value *V; 8836 8837 // If we encounter a shift instruction, "peel off" the shift operation, 8838 // and remember that we did so. Later when we inspect %iv's backedge 8839 // value, we will make sure that the backedge value uses the same 8840 // operation. 8841 // 8842 // Note: the peeled shift operation does not have to be the same 8843 // instruction as the one feeding into the PHI's backedge value. We only 8844 // really care about it being the same *kind* of shift instruction -- 8845 // that's all that is required for our later inferences to hold. 8846 if (MatchPositiveShift(LHS, V, OpC)) { 8847 PostShiftOpCode = OpC; 8848 LHS = V; 8849 } 8850 } 8851 8852 PNOut = dyn_cast<PHINode>(LHS); 8853 if (!PNOut || PNOut->getParent() != L->getHeader()) 8854 return false; 8855 8856 Value *BEValue = PNOut->getIncomingValueForBlock(Latch); 8857 Value *OpLHS; 8858 8859 return 8860 // The backedge value for the PHI node must be a shift by a positive 8861 // amount 8862 MatchPositiveShift(BEValue, OpLHS, OpCodeOut) && 8863 8864 // of the PHI node itself 8865 OpLHS == PNOut && 8866 8867 // and the kind of shift should be match the kind of shift we peeled 8868 // off, if any. 8869 (!PostShiftOpCode.hasValue() || *PostShiftOpCode == OpCodeOut); 8870 }; 8871 8872 PHINode *PN; 8873 Instruction::BinaryOps OpCode; 8874 if (!MatchShiftRecurrence(LHS, PN, OpCode)) 8875 return getCouldNotCompute(); 8876 8877 const DataLayout &DL = getDataLayout(); 8878 8879 // The key rationale for this optimization is that for some kinds of shift 8880 // recurrences, the value of the recurrence "stabilizes" to either 0 or -1 8881 // within a finite number of iterations. If the condition guarding the 8882 // backedge (in the sense that the backedge is taken if the condition is true) 8883 // is false for the value the shift recurrence stabilizes to, then we know 8884 // that the backedge is taken only a finite number of times. 8885 8886 ConstantInt *StableValue = nullptr; 8887 switch (OpCode) { 8888 default: 8889 llvm_unreachable("Impossible case!"); 8890 8891 case Instruction::AShr: { 8892 // {K,ashr,<positive-constant>} stabilizes to signum(K) in at most 8893 // bitwidth(K) iterations. 8894 Value *FirstValue = PN->getIncomingValueForBlock(Predecessor); 8895 KnownBits Known = computeKnownBits(FirstValue, DL, 0, &AC, 8896 Predecessor->getTerminator(), &DT); 8897 auto *Ty = cast<IntegerType>(RHS->getType()); 8898 if (Known.isNonNegative()) 8899 StableValue = ConstantInt::get(Ty, 0); 8900 else if (Known.isNegative()) 8901 StableValue = ConstantInt::get(Ty, -1, true); 8902 else 8903 return getCouldNotCompute(); 8904 8905 break; 8906 } 8907 case Instruction::LShr: 8908 case Instruction::Shl: 8909 // Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>} 8910 // stabilize to 0 in at most bitwidth(K) iterations. 8911 StableValue = ConstantInt::get(cast<IntegerType>(RHS->getType()), 0); 8912 break; 8913 } 8914 8915 auto *Result = 8916 ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI); 8917 assert(Result->getType()->isIntegerTy(1) && 8918 "Otherwise cannot be an operand to a branch instruction"); 8919 8920 if (Result->isZeroValue()) { 8921 unsigned BitWidth = getTypeSizeInBits(RHS->getType()); 8922 const SCEV *UpperBound = 8923 getConstant(getEffectiveSCEVType(RHS->getType()), BitWidth); 8924 return ExitLimit(getCouldNotCompute(), UpperBound, false); 8925 } 8926 8927 return getCouldNotCompute(); 8928 } 8929 8930 /// Return true if we can constant fold an instruction of the specified type, 8931 /// assuming that all operands were constants. 8932 static bool CanConstantFold(const Instruction *I) { 8933 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || 8934 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) || 8935 isa<LoadInst>(I) || isa<ExtractValueInst>(I)) 8936 return true; 8937 8938 if (const CallInst *CI = dyn_cast<CallInst>(I)) 8939 if (const Function *F = CI->getCalledFunction()) 8940 return canConstantFoldCallTo(CI, F); 8941 return false; 8942 } 8943 8944 /// Determine whether this instruction can constant evolve within this loop 8945 /// assuming its operands can all constant evolve. 8946 static bool canConstantEvolve(Instruction *I, const Loop *L) { 8947 // An instruction outside of the loop can't be derived from a loop PHI. 8948 if (!L->contains(I)) return false; 8949 8950 if (isa<PHINode>(I)) { 8951 // We don't currently keep track of the control flow needed to evaluate 8952 // PHIs, so we cannot handle PHIs inside of loops. 8953 return L->getHeader() == I->getParent(); 8954 } 8955 8956 // If we won't be able to constant fold this expression even if the operands 8957 // are constants, bail early. 8958 return CanConstantFold(I); 8959 } 8960 8961 /// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by 8962 /// recursing through each instruction operand until reaching a loop header phi. 8963 static PHINode * 8964 getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L, 8965 DenseMap<Instruction *, PHINode *> &PHIMap, 8966 unsigned Depth) { 8967 if (Depth > MaxConstantEvolvingDepth) 8968 return nullptr; 8969 8970 // Otherwise, we can evaluate this instruction if all of its operands are 8971 // constant or derived from a PHI node themselves. 8972 PHINode *PHI = nullptr; 8973 for (Value *Op : UseInst->operands()) { 8974 if (isa<Constant>(Op)) continue; 8975 8976 Instruction *OpInst = dyn_cast<Instruction>(Op); 8977 if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr; 8978 8979 PHINode *P = dyn_cast<PHINode>(OpInst); 8980 if (!P) 8981 // If this operand is already visited, reuse the prior result. 8982 // We may have P != PHI if this is the deepest point at which the 8983 // inconsistent paths meet. 8984 P = PHIMap.lookup(OpInst); 8985 if (!P) { 8986 // Recurse and memoize the results, whether a phi is found or not. 8987 // This recursive call invalidates pointers into PHIMap. 8988 P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1); 8989 PHIMap[OpInst] = P; 8990 } 8991 if (!P) 8992 return nullptr; // Not evolving from PHI 8993 if (PHI && PHI != P) 8994 return nullptr; // Evolving from multiple different PHIs. 8995 PHI = P; 8996 } 8997 // This is a expression evolving from a constant PHI! 8998 return PHI; 8999 } 9000 9001 /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node 9002 /// in the loop that V is derived from. We allow arbitrary operations along the 9003 /// way, but the operands of an operation must either be constants or a value 9004 /// derived from a constant PHI. If this expression does not fit with these 9005 /// constraints, return null. 9006 static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { 9007 Instruction *I = dyn_cast<Instruction>(V); 9008 if (!I || !canConstantEvolve(I, L)) return nullptr; 9009 9010 if (PHINode *PN = dyn_cast<PHINode>(I)) 9011 return PN; 9012 9013 // Record non-constant instructions contained by the loop. 9014 DenseMap<Instruction *, PHINode *> PHIMap; 9015 return getConstantEvolvingPHIOperands(I, L, PHIMap, 0); 9016 } 9017 9018 /// EvaluateExpression - Given an expression that passes the 9019 /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node 9020 /// in the loop has the value PHIVal. If we can't fold this expression for some 9021 /// reason, return null. 9022 static Constant *EvaluateExpression(Value *V, const Loop *L, 9023 DenseMap<Instruction *, Constant *> &Vals, 9024 const DataLayout &DL, 9025 const TargetLibraryInfo *TLI) { 9026 // Convenient constant check, but redundant for recursive calls. 9027 if (Constant *C = dyn_cast<Constant>(V)) return C; 9028 Instruction *I = dyn_cast<Instruction>(V); 9029 if (!I) return nullptr; 9030 9031 if (Constant *C = Vals.lookup(I)) return C; 9032 9033 // An instruction inside the loop depends on a value outside the loop that we 9034 // weren't given a mapping for, or a value such as a call inside the loop. 9035 if (!canConstantEvolve(I, L)) return nullptr; 9036 9037 // An unmapped PHI can be due to a branch or another loop inside this loop, 9038 // or due to this not being the initial iteration through a loop where we 9039 // couldn't compute the evolution of this particular PHI last time. 9040 if (isa<PHINode>(I)) return nullptr; 9041 9042 std::vector<Constant*> Operands(I->getNumOperands()); 9043 9044 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 9045 Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i)); 9046 if (!Operand) { 9047 Operands[i] = dyn_cast<Constant>(I->getOperand(i)); 9048 if (!Operands[i]) return nullptr; 9049 continue; 9050 } 9051 Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI); 9052 Vals[Operand] = C; 9053 if (!C) return nullptr; 9054 Operands[i] = C; 9055 } 9056 9057 if (CmpInst *CI = dyn_cast<CmpInst>(I)) 9058 return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], 9059 Operands[1], DL, TLI); 9060 if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 9061 if (!LI->isVolatile()) 9062 return ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL); 9063 } 9064 return ConstantFoldInstOperands(I, Operands, DL, TLI); 9065 } 9066 9067 9068 // If every incoming value to PN except the one for BB is a specific Constant, 9069 // return that, else return nullptr. 9070 static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) { 9071 Constant *IncomingVal = nullptr; 9072 9073 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 9074 if (PN->getIncomingBlock(i) == BB) 9075 continue; 9076 9077 auto *CurrentVal = dyn_cast<Constant>(PN->getIncomingValue(i)); 9078 if (!CurrentVal) 9079 return nullptr; 9080 9081 if (IncomingVal != CurrentVal) { 9082 if (IncomingVal) 9083 return nullptr; 9084 IncomingVal = CurrentVal; 9085 } 9086 } 9087 9088 return IncomingVal; 9089 } 9090 9091 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is 9092 /// in the header of its containing loop, we know the loop executes a 9093 /// constant number of times, and the PHI node is just a recurrence 9094 /// involving constants, fold it. 9095 Constant * 9096 ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, 9097 const APInt &BEs, 9098 const Loop *L) { 9099 auto I = ConstantEvolutionLoopExitValue.find(PN); 9100 if (I != ConstantEvolutionLoopExitValue.end()) 9101 return I->second; 9102 9103 if (BEs.ugt(MaxBruteForceIterations)) 9104 return ConstantEvolutionLoopExitValue[PN] = nullptr; // Not going to evaluate it. 9105 9106 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; 9107 9108 DenseMap<Instruction *, Constant *> CurrentIterVals; 9109 BasicBlock *Header = L->getHeader(); 9110 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!"); 9111 9112 BasicBlock *Latch = L->getLoopLatch(); 9113 if (!Latch) 9114 return nullptr; 9115 9116 for (PHINode &PHI : Header->phis()) { 9117 if (auto *StartCST = getOtherIncomingValue(&PHI, Latch)) 9118 CurrentIterVals[&PHI] = StartCST; 9119 } 9120 if (!CurrentIterVals.count(PN)) 9121 return RetVal = nullptr; 9122 9123 Value *BEValue = PN->getIncomingValueForBlock(Latch); 9124 9125 // Execute the loop symbolically to determine the exit value. 9126 assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) && 9127 "BEs is <= MaxBruteForceIterations which is an 'unsigned'!"); 9128 9129 unsigned NumIterations = BEs.getZExtValue(); // must be in range 9130 unsigned IterationNum = 0; 9131 const DataLayout &DL = getDataLayout(); 9132 for (; ; ++IterationNum) { 9133 if (IterationNum == NumIterations) 9134 return RetVal = CurrentIterVals[PN]; // Got exit value! 9135 9136 // Compute the value of the PHIs for the next iteration. 9137 // EvaluateExpression adds non-phi values to the CurrentIterVals map. 9138 DenseMap<Instruction *, Constant *> NextIterVals; 9139 Constant *NextPHI = 9140 EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); 9141 if (!NextPHI) 9142 return nullptr; // Couldn't evaluate! 9143 NextIterVals[PN] = NextPHI; 9144 9145 bool StoppedEvolving = NextPHI == CurrentIterVals[PN]; 9146 9147 // Also evaluate the other PHI nodes. However, we don't get to stop if we 9148 // cease to be able to evaluate one of them or if they stop evolving, 9149 // because that doesn't necessarily prevent us from computing PN. 9150 SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute; 9151 for (const auto &I : CurrentIterVals) { 9152 PHINode *PHI = dyn_cast<PHINode>(I.first); 9153 if (!PHI || PHI == PN || PHI->getParent() != Header) continue; 9154 PHIsToCompute.emplace_back(PHI, I.second); 9155 } 9156 // We use two distinct loops because EvaluateExpression may invalidate any 9157 // iterators into CurrentIterVals. 9158 for (const auto &I : PHIsToCompute) { 9159 PHINode *PHI = I.first; 9160 Constant *&NextPHI = NextIterVals[PHI]; 9161 if (!NextPHI) { // Not already computed. 9162 Value *BEValue = PHI->getIncomingValueForBlock(Latch); 9163 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); 9164 } 9165 if (NextPHI != I.second) 9166 StoppedEvolving = false; 9167 } 9168 9169 // If all entries in CurrentIterVals == NextIterVals then we can stop 9170 // iterating, the loop can't continue to change. 9171 if (StoppedEvolving) 9172 return RetVal = CurrentIterVals[PN]; 9173 9174 CurrentIterVals.swap(NextIterVals); 9175 } 9176 } 9177 9178 const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L, 9179 Value *Cond, 9180 bool ExitWhen) { 9181 PHINode *PN = getConstantEvolvingPHI(Cond, L); 9182 if (!PN) return getCouldNotCompute(); 9183 9184 // If the loop is canonicalized, the PHI will have exactly two entries. 9185 // That's the only form we support here. 9186 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute(); 9187 9188 DenseMap<Instruction *, Constant *> CurrentIterVals; 9189 BasicBlock *Header = L->getHeader(); 9190 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!"); 9191 9192 BasicBlock *Latch = L->getLoopLatch(); 9193 assert(Latch && "Should follow from NumIncomingValues == 2!"); 9194 9195 for (PHINode &PHI : Header->phis()) { 9196 if (auto *StartCST = getOtherIncomingValue(&PHI, Latch)) 9197 CurrentIterVals[&PHI] = StartCST; 9198 } 9199 if (!CurrentIterVals.count(PN)) 9200 return getCouldNotCompute(); 9201 9202 // Okay, we find a PHI node that defines the trip count of this loop. Execute 9203 // the loop symbolically to determine when the condition gets a value of 9204 // "ExitWhen". 9205 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. 9206 const DataLayout &DL = getDataLayout(); 9207 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){ 9208 auto *CondVal = dyn_cast_or_null<ConstantInt>( 9209 EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI)); 9210 9211 // Couldn't symbolically evaluate. 9212 if (!CondVal) return getCouldNotCompute(); 9213 9214 if (CondVal->getValue() == uint64_t(ExitWhen)) { 9215 ++NumBruteForceTripCountsComputed; 9216 return getConstant(Type::getInt32Ty(getContext()), IterationNum); 9217 } 9218 9219 // Update all the PHI nodes for the next iteration. 9220 DenseMap<Instruction *, Constant *> NextIterVals; 9221 9222 // Create a list of which PHIs we need to compute. We want to do this before 9223 // calling EvaluateExpression on them because that may invalidate iterators 9224 // into CurrentIterVals. 9225 SmallVector<PHINode *, 8> PHIsToCompute; 9226 for (const auto &I : CurrentIterVals) { 9227 PHINode *PHI = dyn_cast<PHINode>(I.first); 9228 if (!PHI || PHI->getParent() != Header) continue; 9229 PHIsToCompute.push_back(PHI); 9230 } 9231 for (PHINode *PHI : PHIsToCompute) { 9232 Constant *&NextPHI = NextIterVals[PHI]; 9233 if (NextPHI) continue; // Already computed! 9234 9235 Value *BEValue = PHI->getIncomingValueForBlock(Latch); 9236 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); 9237 } 9238 CurrentIterVals.swap(NextIterVals); 9239 } 9240 9241 // Too many iterations were needed to evaluate. 9242 return getCouldNotCompute(); 9243 } 9244 9245 const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { 9246 SmallVector<std::pair<const Loop *, const SCEV *>, 2> &Values = 9247 ValuesAtScopes[V]; 9248 // Check to see if we've folded this expression at this loop before. 9249 for (auto &LS : Values) 9250 if (LS.first == L) 9251 return LS.second ? LS.second : V; 9252 9253 Values.emplace_back(L, nullptr); 9254 9255 // Otherwise compute it. 9256 const SCEV *C = computeSCEVAtScope(V, L); 9257 for (auto &LS : reverse(ValuesAtScopes[V])) 9258 if (LS.first == L) { 9259 LS.second = C; 9260 if (!isa<SCEVConstant>(C)) 9261 ValuesAtScopesUsers[C].push_back({L, V}); 9262 break; 9263 } 9264 return C; 9265 } 9266 9267 /// This builds up a Constant using the ConstantExpr interface. That way, we 9268 /// will return Constants for objects which aren't represented by a 9269 /// SCEVConstant, because SCEVConstant is restricted to ConstantInt. 9270 /// Returns NULL if the SCEV isn't representable as a Constant. 9271 static Constant *BuildConstantFromSCEV(const SCEV *V) { 9272 switch (V->getSCEVType()) { 9273 case scCouldNotCompute: 9274 case scAddRecExpr: 9275 return nullptr; 9276 case scConstant: 9277 return cast<SCEVConstant>(V)->getValue(); 9278 case scUnknown: 9279 return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue()); 9280 case scSignExtend: { 9281 const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V); 9282 if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand())) 9283 return ConstantExpr::getSExt(CastOp, SS->getType()); 9284 return nullptr; 9285 } 9286 case scZeroExtend: { 9287 const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V); 9288 if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand())) 9289 return ConstantExpr::getZExt(CastOp, SZ->getType()); 9290 return nullptr; 9291 } 9292 case scPtrToInt: { 9293 const SCEVPtrToIntExpr *P2I = cast<SCEVPtrToIntExpr>(V); 9294 if (Constant *CastOp = BuildConstantFromSCEV(P2I->getOperand())) 9295 return ConstantExpr::getPtrToInt(CastOp, P2I->getType()); 9296 9297 return nullptr; 9298 } 9299 case scTruncate: { 9300 const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V); 9301 if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand())) 9302 return ConstantExpr::getTrunc(CastOp, ST->getType()); 9303 return nullptr; 9304 } 9305 case scAddExpr: { 9306 const SCEVAddExpr *SA = cast<SCEVAddExpr>(V); 9307 if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) { 9308 if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) { 9309 unsigned AS = PTy->getAddressSpace(); 9310 Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS); 9311 C = ConstantExpr::getBitCast(C, DestPtrTy); 9312 } 9313 for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) { 9314 Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i)); 9315 if (!C2) 9316 return nullptr; 9317 9318 // First pointer! 9319 if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) { 9320 unsigned AS = C2->getType()->getPointerAddressSpace(); 9321 std::swap(C, C2); 9322 Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS); 9323 // The offsets have been converted to bytes. We can add bytes to an 9324 // i8* by GEP with the byte count in the first index. 9325 C = ConstantExpr::getBitCast(C, DestPtrTy); 9326 } 9327 9328 // Don't bother trying to sum two pointers. We probably can't 9329 // statically compute a load that results from it anyway. 9330 if (C2->getType()->isPointerTy()) 9331 return nullptr; 9332 9333 if (C->getType()->isPointerTy()) { 9334 C = ConstantExpr::getGetElementPtr(Type::getInt8Ty(C->getContext()), 9335 C, C2); 9336 } else { 9337 C = ConstantExpr::getAdd(C, C2); 9338 } 9339 } 9340 return C; 9341 } 9342 return nullptr; 9343 } 9344 case scMulExpr: { 9345 const SCEVMulExpr *SM = cast<SCEVMulExpr>(V); 9346 if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) { 9347 // Don't bother with pointers at all. 9348 if (C->getType()->isPointerTy()) 9349 return nullptr; 9350 for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) { 9351 Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i)); 9352 if (!C2 || C2->getType()->isPointerTy()) 9353 return nullptr; 9354 C = ConstantExpr::getMul(C, C2); 9355 } 9356 return C; 9357 } 9358 return nullptr; 9359 } 9360 case scUDivExpr: { 9361 const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V); 9362 if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS())) 9363 if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS())) 9364 if (LHS->getType() == RHS->getType()) 9365 return ConstantExpr::getUDiv(LHS, RHS); 9366 return nullptr; 9367 } 9368 case scSMaxExpr: 9369 case scUMaxExpr: 9370 case scSMinExpr: 9371 case scUMinExpr: 9372 case scSequentialUMinExpr: 9373 return nullptr; // TODO: smax, umax, smin, umax, umin_seq. 9374 } 9375 llvm_unreachable("Unknown SCEV kind!"); 9376 } 9377 9378 const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { 9379 if (isa<SCEVConstant>(V)) return V; 9380 9381 // If this instruction is evolved from a constant-evolving PHI, compute the 9382 // exit value from the loop without using SCEVs. 9383 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { 9384 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { 9385 if (PHINode *PN = dyn_cast<PHINode>(I)) { 9386 const Loop *CurrLoop = this->LI[I->getParent()]; 9387 // Looking for loop exit value. 9388 if (CurrLoop && CurrLoop->getParentLoop() == L && 9389 PN->getParent() == CurrLoop->getHeader()) { 9390 // Okay, there is no closed form solution for the PHI node. Check 9391 // to see if the loop that contains it has a known backedge-taken 9392 // count. If so, we may be able to force computation of the exit 9393 // value. 9394 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(CurrLoop); 9395 // This trivial case can show up in some degenerate cases where 9396 // the incoming IR has not yet been fully simplified. 9397 if (BackedgeTakenCount->isZero()) { 9398 Value *InitValue = nullptr; 9399 bool MultipleInitValues = false; 9400 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) { 9401 if (!CurrLoop->contains(PN->getIncomingBlock(i))) { 9402 if (!InitValue) 9403 InitValue = PN->getIncomingValue(i); 9404 else if (InitValue != PN->getIncomingValue(i)) { 9405 MultipleInitValues = true; 9406 break; 9407 } 9408 } 9409 } 9410 if (!MultipleInitValues && InitValue) 9411 return getSCEV(InitValue); 9412 } 9413 // Do we have a loop invariant value flowing around the backedge 9414 // for a loop which must execute the backedge? 9415 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && 9416 isKnownPositive(BackedgeTakenCount) && 9417 PN->getNumIncomingValues() == 2) { 9418 9419 unsigned InLoopPred = 9420 CurrLoop->contains(PN->getIncomingBlock(0)) ? 0 : 1; 9421 Value *BackedgeVal = PN->getIncomingValue(InLoopPred); 9422 if (CurrLoop->isLoopInvariant(BackedgeVal)) 9423 return getSCEV(BackedgeVal); 9424 } 9425 if (auto *BTCC = dyn_cast<SCEVConstant>(BackedgeTakenCount)) { 9426 // Okay, we know how many times the containing loop executes. If 9427 // this is a constant evolving PHI node, get the final value at 9428 // the specified iteration number. 9429 Constant *RV = getConstantEvolutionLoopExitValue( 9430 PN, BTCC->getAPInt(), CurrLoop); 9431 if (RV) return getSCEV(RV); 9432 } 9433 } 9434 9435 // If there is a single-input Phi, evaluate it at our scope. If we can 9436 // prove that this replacement does not break LCSSA form, use new value. 9437 if (PN->getNumOperands() == 1) { 9438 const SCEV *Input = getSCEV(PN->getOperand(0)); 9439 const SCEV *InputAtScope = getSCEVAtScope(Input, L); 9440 // TODO: We can generalize it using LI.replacementPreservesLCSSAForm, 9441 // for the simplest case just support constants. 9442 if (isa<SCEVConstant>(InputAtScope)) return InputAtScope; 9443 } 9444 } 9445 9446 // Okay, this is an expression that we cannot symbolically evaluate 9447 // into a SCEV. Check to see if it's possible to symbolically evaluate 9448 // the arguments into constants, and if so, try to constant propagate the 9449 // result. This is particularly useful for computing loop exit values. 9450 if (CanConstantFold(I)) { 9451 SmallVector<Constant *, 4> Operands; 9452 bool MadeImprovement = false; 9453 for (Value *Op : I->operands()) { 9454 if (Constant *C = dyn_cast<Constant>(Op)) { 9455 Operands.push_back(C); 9456 continue; 9457 } 9458 9459 // If any of the operands is non-constant and if they are 9460 // non-integer and non-pointer, don't even try to analyze them 9461 // with scev techniques. 9462 if (!isSCEVable(Op->getType())) 9463 return V; 9464 9465 const SCEV *OrigV = getSCEV(Op); 9466 const SCEV *OpV = getSCEVAtScope(OrigV, L); 9467 MadeImprovement |= OrigV != OpV; 9468 9469 Constant *C = BuildConstantFromSCEV(OpV); 9470 if (!C) return V; 9471 if (C->getType() != Op->getType()) 9472 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 9473 Op->getType(), 9474 false), 9475 C, Op->getType()); 9476 Operands.push_back(C); 9477 } 9478 9479 // Check to see if getSCEVAtScope actually made an improvement. 9480 if (MadeImprovement) { 9481 Constant *C = nullptr; 9482 const DataLayout &DL = getDataLayout(); 9483 if (const CmpInst *CI = dyn_cast<CmpInst>(I)) 9484 C = ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], 9485 Operands[1], DL, &TLI); 9486 else if (const LoadInst *Load = dyn_cast<LoadInst>(I)) { 9487 if (!Load->isVolatile()) 9488 C = ConstantFoldLoadFromConstPtr(Operands[0], Load->getType(), 9489 DL); 9490 } else 9491 C = ConstantFoldInstOperands(I, Operands, DL, &TLI); 9492 if (!C) return V; 9493 return getSCEV(C); 9494 } 9495 } 9496 } 9497 9498 // This is some other type of SCEVUnknown, just return it. 9499 return V; 9500 } 9501 9502 if (isa<SCEVCommutativeExpr>(V) || isa<SCEVSequentialMinMaxExpr>(V)) { 9503 const auto *Comm = cast<SCEVNAryExpr>(V); 9504 // Avoid performing the look-up in the common case where the specified 9505 // expression has no loop-variant portions. 9506 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { 9507 const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); 9508 if (OpAtScope != Comm->getOperand(i)) { 9509 // Okay, at least one of these operands is loop variant but might be 9510 // foldable. Build a new instance of the folded commutative expression. 9511 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), 9512 Comm->op_begin()+i); 9513 NewOps.push_back(OpAtScope); 9514 9515 for (++i; i != e; ++i) { 9516 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); 9517 NewOps.push_back(OpAtScope); 9518 } 9519 if (isa<SCEVAddExpr>(Comm)) 9520 return getAddExpr(NewOps, Comm->getNoWrapFlags()); 9521 if (isa<SCEVMulExpr>(Comm)) 9522 return getMulExpr(NewOps, Comm->getNoWrapFlags()); 9523 if (isa<SCEVMinMaxExpr>(Comm)) 9524 return getMinMaxExpr(Comm->getSCEVType(), NewOps); 9525 if (isa<SCEVSequentialMinMaxExpr>(Comm)) 9526 return getSequentialMinMaxExpr(Comm->getSCEVType(), NewOps); 9527 llvm_unreachable("Unknown commutative / sequential min/max SCEV type!"); 9528 } 9529 } 9530 // If we got here, all operands are loop invariant. 9531 return Comm; 9532 } 9533 9534 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { 9535 const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); 9536 const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); 9537 if (LHS == Div->getLHS() && RHS == Div->getRHS()) 9538 return Div; // must be loop invariant 9539 return getUDivExpr(LHS, RHS); 9540 } 9541 9542 // If this is a loop recurrence for a loop that does not contain L, then we 9543 // are dealing with the final value computed by the loop. 9544 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { 9545 // First, attempt to evaluate each operand. 9546 // Avoid performing the look-up in the common case where the specified 9547 // expression has no loop-variant portions. 9548 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { 9549 const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L); 9550 if (OpAtScope == AddRec->getOperand(i)) 9551 continue; 9552 9553 // Okay, at least one of these operands is loop variant but might be 9554 // foldable. Build a new instance of the folded commutative expression. 9555 SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(), 9556 AddRec->op_begin()+i); 9557 NewOps.push_back(OpAtScope); 9558 for (++i; i != e; ++i) 9559 NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L)); 9560 9561 const SCEV *FoldedRec = 9562 getAddRecExpr(NewOps, AddRec->getLoop(), 9563 AddRec->getNoWrapFlags(SCEV::FlagNW)); 9564 AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec); 9565 // The addrec may be folded to a nonrecurrence, for example, if the 9566 // induction variable is multiplied by zero after constant folding. Go 9567 // ahead and return the folded value. 9568 if (!AddRec) 9569 return FoldedRec; 9570 break; 9571 } 9572 9573 // If the scope is outside the addrec's loop, evaluate it by using the 9574 // loop exit value of the addrec. 9575 if (!AddRec->getLoop()->contains(L)) { 9576 // To evaluate this recurrence, we need to know how many times the AddRec 9577 // loop iterates. Compute this now. 9578 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); 9579 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; 9580 9581 // Then, evaluate the AddRec. 9582 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); 9583 } 9584 9585 return AddRec; 9586 } 9587 9588 if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) { 9589 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); 9590 if (Op == Cast->getOperand()) 9591 return Cast; // must be loop invariant 9592 return getCastExpr(Cast->getSCEVType(), Op, Cast->getType()); 9593 } 9594 9595 llvm_unreachable("Unknown SCEV type!"); 9596 } 9597 9598 const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { 9599 return getSCEVAtScope(getSCEV(V), L); 9600 } 9601 9602 const SCEV *ScalarEvolution::stripInjectiveFunctions(const SCEV *S) const { 9603 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) 9604 return stripInjectiveFunctions(ZExt->getOperand()); 9605 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) 9606 return stripInjectiveFunctions(SExt->getOperand()); 9607 return S; 9608 } 9609 9610 /// Finds the minimum unsigned root of the following equation: 9611 /// 9612 /// A * X = B (mod N) 9613 /// 9614 /// where N = 2^BW and BW is the common bit width of A and B. The signedness of 9615 /// A and B isn't important. 9616 /// 9617 /// If the equation does not have a solution, SCEVCouldNotCompute is returned. 9618 static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const SCEV *B, 9619 ScalarEvolution &SE) { 9620 uint32_t BW = A.getBitWidth(); 9621 assert(BW == SE.getTypeSizeInBits(B->getType())); 9622 assert(A != 0 && "A must be non-zero."); 9623 9624 // 1. D = gcd(A, N) 9625 // 9626 // The gcd of A and N may have only one prime factor: 2. The number of 9627 // trailing zeros in A is its multiplicity 9628 uint32_t Mult2 = A.countTrailingZeros(); 9629 // D = 2^Mult2 9630 9631 // 2. Check if B is divisible by D. 9632 // 9633 // B is divisible by D if and only if the multiplicity of prime factor 2 for B 9634 // is not less than multiplicity of this prime factor for D. 9635 if (SE.GetMinTrailingZeros(B) < Mult2) 9636 return SE.getCouldNotCompute(); 9637 9638 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic 9639 // modulo (N / D). 9640 // 9641 // If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent 9642 // (N / D) in general. The inverse itself always fits into BW bits, though, 9643 // so we immediately truncate it. 9644 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D 9645 APInt Mod(BW + 1, 0); 9646 Mod.setBit(BW - Mult2); // Mod = N / D 9647 APInt I = AD.multiplicativeInverse(Mod).trunc(BW); 9648 9649 // 4. Compute the minimum unsigned root of the equation: 9650 // I * (B / D) mod (N / D) 9651 // To simplify the computation, we factor out the divide by D: 9652 // (I * B mod N) / D 9653 const SCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2)); 9654 return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D); 9655 } 9656 9657 /// For a given quadratic addrec, generate coefficients of the corresponding 9658 /// quadratic equation, multiplied by a common value to ensure that they are 9659 /// integers. 9660 /// The returned value is a tuple { A, B, C, M, BitWidth }, where 9661 /// Ax^2 + Bx + C is the quadratic function, M is the value that A, B and C 9662 /// were multiplied by, and BitWidth is the bit width of the original addrec 9663 /// coefficients. 9664 /// This function returns None if the addrec coefficients are not compile- 9665 /// time constants. 9666 static Optional<std::tuple<APInt, APInt, APInt, APInt, unsigned>> 9667 GetQuadraticEquation(const SCEVAddRecExpr *AddRec) { 9668 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); 9669 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); 9670 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); 9671 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); 9672 LLVM_DEBUG(dbgs() << __func__ << ": analyzing quadratic addrec: " 9673 << *AddRec << '\n'); 9674 9675 // We currently can only solve this if the coefficients are constants. 9676 if (!LC || !MC || !NC) { 9677 LLVM_DEBUG(dbgs() << __func__ << ": coefficients are not constant\n"); 9678 return None; 9679 } 9680 9681 APInt L = LC->getAPInt(); 9682 APInt M = MC->getAPInt(); 9683 APInt N = NC->getAPInt(); 9684 assert(!N.isZero() && "This is not a quadratic addrec"); 9685 9686 unsigned BitWidth = LC->getAPInt().getBitWidth(); 9687 unsigned NewWidth = BitWidth + 1; 9688 LLVM_DEBUG(dbgs() << __func__ << ": addrec coeff bw: " 9689 << BitWidth << '\n'); 9690 // The sign-extension (as opposed to a zero-extension) here matches the 9691 // extension used in SolveQuadraticEquationWrap (with the same motivation). 9692 N = N.sext(NewWidth); 9693 M = M.sext(NewWidth); 9694 L = L.sext(NewWidth); 9695 9696 // The increments are M, M+N, M+2N, ..., so the accumulated values are 9697 // L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is, 9698 // L+M, L+2M+N, L+3M+3N, ... 9699 // After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N. 9700 // 9701 // The equation Acc = 0 is then 9702 // L + nM + n(n-1)/2 N = 0, or 2L + 2M n + n(n-1) N = 0. 9703 // In a quadratic form it becomes: 9704 // N n^2 + (2M-N) n + 2L = 0. 9705 9706 APInt A = N; 9707 APInt B = 2 * M - A; 9708 APInt C = 2 * L; 9709 APInt T = APInt(NewWidth, 2); 9710 LLVM_DEBUG(dbgs() << __func__ << ": equation " << A << "x^2 + " << B 9711 << "x + " << C << ", coeff bw: " << NewWidth 9712 << ", multiplied by " << T << '\n'); 9713 return std::make_tuple(A, B, C, T, BitWidth); 9714 } 9715 9716 /// Helper function to compare optional APInts: 9717 /// (a) if X and Y both exist, return min(X, Y), 9718 /// (b) if neither X nor Y exist, return None, 9719 /// (c) if exactly one of X and Y exists, return that value. 9720 static Optional<APInt> MinOptional(Optional<APInt> X, Optional<APInt> Y) { 9721 if (X.hasValue() && Y.hasValue()) { 9722 unsigned W = std::max(X->getBitWidth(), Y->getBitWidth()); 9723 APInt XW = X->sext(W); 9724 APInt YW = Y->sext(W); 9725 return XW.slt(YW) ? *X : *Y; 9726 } 9727 if (!X.hasValue() && !Y.hasValue()) 9728 return None; 9729 return X.hasValue() ? *X : *Y; 9730 } 9731 9732 /// Helper function to truncate an optional APInt to a given BitWidth. 9733 /// When solving addrec-related equations, it is preferable to return a value 9734 /// that has the same bit width as the original addrec's coefficients. If the 9735 /// solution fits in the original bit width, truncate it (except for i1). 9736 /// Returning a value of a different bit width may inhibit some optimizations. 9737 /// 9738 /// In general, a solution to a quadratic equation generated from an addrec 9739 /// may require BW+1 bits, where BW is the bit width of the addrec's 9740 /// coefficients. The reason is that the coefficients of the quadratic 9741 /// equation are BW+1 bits wide (to avoid truncation when converting from 9742 /// the addrec to the equation). 9743 static Optional<APInt> TruncIfPossible(Optional<APInt> X, unsigned BitWidth) { 9744 if (!X.hasValue()) 9745 return None; 9746 unsigned W = X->getBitWidth(); 9747 if (BitWidth > 1 && BitWidth < W && X->isIntN(BitWidth)) 9748 return X->trunc(BitWidth); 9749 return X; 9750 } 9751 9752 /// Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n 9753 /// iterations. The values L, M, N are assumed to be signed, and they 9754 /// should all have the same bit widths. 9755 /// Find the least n >= 0 such that c(n) = 0 in the arithmetic modulo 2^BW, 9756 /// where BW is the bit width of the addrec's coefficients. 9757 /// If the calculated value is a BW-bit integer (for BW > 1), it will be 9758 /// returned as such, otherwise the bit width of the returned value may 9759 /// be greater than BW. 9760 /// 9761 /// This function returns None if 9762 /// (a) the addrec coefficients are not constant, or 9763 /// (b) SolveQuadraticEquationWrap was unable to find a solution. For cases 9764 /// like x^2 = 5, no integer solutions exist, in other cases an integer 9765 /// solution may exist, but SolveQuadraticEquationWrap may fail to find it. 9766 static Optional<APInt> 9767 SolveQuadraticAddRecExact(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { 9768 APInt A, B, C, M; 9769 unsigned BitWidth; 9770 auto T = GetQuadraticEquation(AddRec); 9771 if (!T.hasValue()) 9772 return None; 9773 9774 std::tie(A, B, C, M, BitWidth) = *T; 9775 LLVM_DEBUG(dbgs() << __func__ << ": solving for unsigned overflow\n"); 9776 Optional<APInt> X = APIntOps::SolveQuadraticEquationWrap(A, B, C, BitWidth+1); 9777 if (!X.hasValue()) 9778 return None; 9779 9780 ConstantInt *CX = ConstantInt::get(SE.getContext(), *X); 9781 ConstantInt *V = EvaluateConstantChrecAtConstant(AddRec, CX, SE); 9782 if (!V->isZero()) 9783 return None; 9784 9785 return TruncIfPossible(X, BitWidth); 9786 } 9787 9788 /// Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n 9789 /// iterations. The values M, N are assumed to be signed, and they 9790 /// should all have the same bit widths. 9791 /// Find the least n such that c(n) does not belong to the given range, 9792 /// while c(n-1) does. 9793 /// 9794 /// This function returns None if 9795 /// (a) the addrec coefficients are not constant, or 9796 /// (b) SolveQuadraticEquationWrap was unable to find a solution for the 9797 /// bounds of the range. 9798 static Optional<APInt> 9799 SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec, 9800 const ConstantRange &Range, ScalarEvolution &SE) { 9801 assert(AddRec->getOperand(0)->isZero() && 9802 "Starting value of addrec should be 0"); 9803 LLVM_DEBUG(dbgs() << __func__ << ": solving boundary crossing for range " 9804 << Range << ", addrec " << *AddRec << '\n'); 9805 // This case is handled in getNumIterationsInRange. Here we can assume that 9806 // we start in the range. 9807 assert(Range.contains(APInt(SE.getTypeSizeInBits(AddRec->getType()), 0)) && 9808 "Addrec's initial value should be in range"); 9809 9810 APInt A, B, C, M; 9811 unsigned BitWidth; 9812 auto T = GetQuadraticEquation(AddRec); 9813 if (!T.hasValue()) 9814 return None; 9815 9816 // Be careful about the return value: there can be two reasons for not 9817 // returning an actual number. First, if no solutions to the equations 9818 // were found, and second, if the solutions don't leave the given range. 9819 // The first case means that the actual solution is "unknown", the second 9820 // means that it's known, but not valid. If the solution is unknown, we 9821 // cannot make any conclusions. 9822 // Return a pair: the optional solution and a flag indicating if the 9823 // solution was found. 9824 auto SolveForBoundary = [&](APInt Bound) -> std::pair<Optional<APInt>,bool> { 9825 // Solve for signed overflow and unsigned overflow, pick the lower 9826 // solution. 9827 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: checking boundary " 9828 << Bound << " (before multiplying by " << M << ")\n"); 9829 Bound *= M; // The quadratic equation multiplier. 9830 9831 Optional<APInt> SO = None; 9832 if (BitWidth > 1) { 9833 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for " 9834 "signed overflow\n"); 9835 SO = APIntOps::SolveQuadraticEquationWrap(A, B, -Bound, BitWidth); 9836 } 9837 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for " 9838 "unsigned overflow\n"); 9839 Optional<APInt> UO = APIntOps::SolveQuadraticEquationWrap(A, B, -Bound, 9840 BitWidth+1); 9841 9842 auto LeavesRange = [&] (const APInt &X) { 9843 ConstantInt *C0 = ConstantInt::get(SE.getContext(), X); 9844 ConstantInt *V0 = EvaluateConstantChrecAtConstant(AddRec, C0, SE); 9845 if (Range.contains(V0->getValue())) 9846 return false; 9847 // X should be at least 1, so X-1 is non-negative. 9848 ConstantInt *C1 = ConstantInt::get(SE.getContext(), X-1); 9849 ConstantInt *V1 = EvaluateConstantChrecAtConstant(AddRec, C1, SE); 9850 if (Range.contains(V1->getValue())) 9851 return true; 9852 return false; 9853 }; 9854 9855 // If SolveQuadraticEquationWrap returns None, it means that there can 9856 // be a solution, but the function failed to find it. We cannot treat it 9857 // as "no solution". 9858 if (!SO.hasValue() || !UO.hasValue()) 9859 return { None, false }; 9860 9861 // Check the smaller value first to see if it leaves the range. 9862 // At this point, both SO and UO must have values. 9863 Optional<APInt> Min = MinOptional(SO, UO); 9864 if (LeavesRange(*Min)) 9865 return { Min, true }; 9866 Optional<APInt> Max = Min == SO ? UO : SO; 9867 if (LeavesRange(*Max)) 9868 return { Max, true }; 9869 9870 // Solutions were found, but were eliminated, hence the "true". 9871 return { None, true }; 9872 }; 9873 9874 std::tie(A, B, C, M, BitWidth) = *T; 9875 // Lower bound is inclusive, subtract 1 to represent the exiting value. 9876 APInt Lower = Range.getLower().sext(A.getBitWidth()) - 1; 9877 APInt Upper = Range.getUpper().sext(A.getBitWidth()); 9878 auto SL = SolveForBoundary(Lower); 9879 auto SU = SolveForBoundary(Upper); 9880 // If any of the solutions was unknown, no meaninigful conclusions can 9881 // be made. 9882 if (!SL.second || !SU.second) 9883 return None; 9884 9885 // Claim: The correct solution is not some value between Min and Max. 9886 // 9887 // Justification: Assuming that Min and Max are different values, one of 9888 // them is when the first signed overflow happens, the other is when the 9889 // first unsigned overflow happens. Crossing the range boundary is only 9890 // possible via an overflow (treating 0 as a special case of it, modeling 9891 // an overflow as crossing k*2^W for some k). 9892 // 9893 // The interesting case here is when Min was eliminated as an invalid 9894 // solution, but Max was not. The argument is that if there was another 9895 // overflow between Min and Max, it would also have been eliminated if 9896 // it was considered. 9897 // 9898 // For a given boundary, it is possible to have two overflows of the same 9899 // type (signed/unsigned) without having the other type in between: this 9900 // can happen when the vertex of the parabola is between the iterations 9901 // corresponding to the overflows. This is only possible when the two 9902 // overflows cross k*2^W for the same k. In such case, if the second one 9903 // left the range (and was the first one to do so), the first overflow 9904 // would have to enter the range, which would mean that either we had left 9905 // the range before or that we started outside of it. Both of these cases 9906 // are contradictions. 9907 // 9908 // Claim: In the case where SolveForBoundary returns None, the correct 9909 // solution is not some value between the Max for this boundary and the 9910 // Min of the other boundary. 9911 // 9912 // Justification: Assume that we had such Max_A and Min_B corresponding 9913 // to range boundaries A and B and such that Max_A < Min_B. If there was 9914 // a solution between Max_A and Min_B, it would have to be caused by an 9915 // overflow corresponding to either A or B. It cannot correspond to B, 9916 // since Min_B is the first occurrence of such an overflow. If it 9917 // corresponded to A, it would have to be either a signed or an unsigned 9918 // overflow that is larger than both eliminated overflows for A. But 9919 // between the eliminated overflows and this overflow, the values would 9920 // cover the entire value space, thus crossing the other boundary, which 9921 // is a contradiction. 9922 9923 return TruncIfPossible(MinOptional(SL.first, SU.first), BitWidth); 9924 } 9925 9926 ScalarEvolution::ExitLimit 9927 ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit, 9928 bool AllowPredicates) { 9929 9930 // This is only used for loops with a "x != y" exit test. The exit condition 9931 // is now expressed as a single expression, V = x-y. So the exit test is 9932 // effectively V != 0. We know and take advantage of the fact that this 9933 // expression only being used in a comparison by zero context. 9934 9935 SmallPtrSet<const SCEVPredicate *, 4> Predicates; 9936 // If the value is a constant 9937 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { 9938 // If the value is already zero, the branch will execute zero times. 9939 if (C->getValue()->isZero()) return C; 9940 return getCouldNotCompute(); // Otherwise it will loop infinitely. 9941 } 9942 9943 const SCEVAddRecExpr *AddRec = 9944 dyn_cast<SCEVAddRecExpr>(stripInjectiveFunctions(V)); 9945 9946 if (!AddRec && AllowPredicates) 9947 // Try to make this an AddRec using runtime tests, in the first X 9948 // iterations of this loop, where X is the SCEV expression found by the 9949 // algorithm below. 9950 AddRec = convertSCEVToAddRecWithPredicates(V, L, Predicates); 9951 9952 if (!AddRec || AddRec->getLoop() != L) 9953 return getCouldNotCompute(); 9954 9955 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of 9956 // the quadratic equation to solve it. 9957 if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) { 9958 // We can only use this value if the chrec ends up with an exact zero 9959 // value at this index. When solving for "X*X != 5", for example, we 9960 // should not accept a root of 2. 9961 if (auto S = SolveQuadraticAddRecExact(AddRec, *this)) { 9962 const auto *R = cast<SCEVConstant>(getConstant(S.getValue())); 9963 return ExitLimit(R, R, false, Predicates); 9964 } 9965 return getCouldNotCompute(); 9966 } 9967 9968 // Otherwise we can only handle this if it is affine. 9969 if (!AddRec->isAffine()) 9970 return getCouldNotCompute(); 9971 9972 // If this is an affine expression, the execution count of this branch is 9973 // the minimum unsigned root of the following equation: 9974 // 9975 // Start + Step*N = 0 (mod 2^BW) 9976 // 9977 // equivalent to: 9978 // 9979 // Step*N = -Start (mod 2^BW) 9980 // 9981 // where BW is the common bit width of Start and Step. 9982 9983 // Get the initial value for the loop. 9984 const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop()); 9985 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop()); 9986 9987 // For now we handle only constant steps. 9988 // 9989 // TODO: Handle a nonconstant Step given AddRec<NUW>. If the 9990 // AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap 9991 // to 0, it must be counting down to equal 0. Consequently, N = Start / -Step. 9992 // We have not yet seen any such cases. 9993 const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step); 9994 if (!StepC || StepC->getValue()->isZero()) 9995 return getCouldNotCompute(); 9996 9997 // For positive steps (counting up until unsigned overflow): 9998 // N = -Start/Step (as unsigned) 9999 // For negative steps (counting down to zero): 10000 // N = Start/-Step 10001 // First compute the unsigned distance from zero in the direction of Step. 10002 bool CountDown = StepC->getAPInt().isNegative(); 10003 const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start); 10004 10005 // Handle unitary steps, which cannot wraparound. 10006 // 1*N = -Start; -1*N = Start (mod 2^BW), so: 10007 // N = Distance (as unsigned) 10008 if (StepC->getValue()->isOne() || StepC->getValue()->isMinusOne()) { 10009 APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, L)); 10010 MaxBECount = APIntOps::umin(MaxBECount, getUnsignedRangeMax(Distance)); 10011 10012 // When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated, 10013 // we end up with a loop whose backedge-taken count is n - 1. Detect this 10014 // case, and see if we can improve the bound. 10015 // 10016 // Explicitly handling this here is necessary because getUnsignedRange 10017 // isn't context-sensitive; it doesn't know that we only care about the 10018 // range inside the loop. 10019 const SCEV *Zero = getZero(Distance->getType()); 10020 const SCEV *One = getOne(Distance->getType()); 10021 const SCEV *DistancePlusOne = getAddExpr(Distance, One); 10022 if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) { 10023 // If Distance + 1 doesn't overflow, we can compute the maximum distance 10024 // as "unsigned_max(Distance + 1) - 1". 10025 ConstantRange CR = getUnsignedRange(DistancePlusOne); 10026 MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1); 10027 } 10028 return ExitLimit(Distance, getConstant(MaxBECount), false, Predicates); 10029 } 10030 10031 // If the condition controls loop exit (the loop exits only if the expression 10032 // is true) and the addition is no-wrap we can use unsigned divide to 10033 // compute the backedge count. In this case, the step may not divide the 10034 // distance, but we don't care because if the condition is "missed" the loop 10035 // will have undefined behavior due to wrapping. 10036 if (ControlsExit && AddRec->hasNoSelfWrap() && 10037 loopHasNoAbnormalExits(AddRec->getLoop())) { 10038 const SCEV *Exact = 10039 getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step); 10040 const SCEV *Max = getCouldNotCompute(); 10041 if (Exact != getCouldNotCompute()) { 10042 APInt MaxInt = getUnsignedRangeMax(applyLoopGuards(Exact, L)); 10043 Max = getConstant(APIntOps::umin(MaxInt, getUnsignedRangeMax(Exact))); 10044 } 10045 return ExitLimit(Exact, Max, false, Predicates); 10046 } 10047 10048 // Solve the general equation. 10049 const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(), 10050 getNegativeSCEV(Start), *this); 10051 10052 const SCEV *M = E; 10053 if (E != getCouldNotCompute()) { 10054 APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, L)); 10055 M = getConstant(APIntOps::umin(MaxWithGuards, getUnsignedRangeMax(E))); 10056 } 10057 return ExitLimit(E, M, false, Predicates); 10058 } 10059 10060 ScalarEvolution::ExitLimit 10061 ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) { 10062 // Loops that look like: while (X == 0) are very strange indeed. We don't 10063 // handle them yet except for the trivial case. This could be expanded in the 10064 // future as needed. 10065 10066 // If the value is a constant, check to see if it is known to be non-zero 10067 // already. If so, the backedge will execute zero times. 10068 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { 10069 if (!C->getValue()->isZero()) 10070 return getZero(C->getType()); 10071 return getCouldNotCompute(); // Otherwise it will loop infinitely. 10072 } 10073 10074 // We could implement others, but I really doubt anyone writes loops like 10075 // this, and if they did, they would already be constant folded. 10076 return getCouldNotCompute(); 10077 } 10078 10079 std::pair<const BasicBlock *, const BasicBlock *> 10080 ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB) 10081 const { 10082 // If the block has a unique predecessor, then there is no path from the 10083 // predecessor to the block that does not go through the direct edge 10084 // from the predecessor to the block. 10085 if (const BasicBlock *Pred = BB->getSinglePredecessor()) 10086 return {Pred, BB}; 10087 10088 // A loop's header is defined to be a block that dominates the loop. 10089 // If the header has a unique predecessor outside the loop, it must be 10090 // a block that has exactly one successor that can reach the loop. 10091 if (const Loop *L = LI.getLoopFor(BB)) 10092 return {L->getLoopPredecessor(), L->getHeader()}; 10093 10094 return {nullptr, nullptr}; 10095 } 10096 10097 /// SCEV structural equivalence is usually sufficient for testing whether two 10098 /// expressions are equal, however for the purposes of looking for a condition 10099 /// guarding a loop, it can be useful to be a little more general, since a 10100 /// front-end may have replicated the controlling expression. 10101 static bool HasSameValue(const SCEV *A, const SCEV *B) { 10102 // Quick check to see if they are the same SCEV. 10103 if (A == B) return true; 10104 10105 auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) { 10106 // Not all instructions that are "identical" compute the same value. For 10107 // instance, two distinct alloca instructions allocating the same type are 10108 // identical and do not read memory; but compute distinct values. 10109 return A->isIdenticalTo(B) && (isa<BinaryOperator>(A) || isa<GetElementPtrInst>(A)); 10110 }; 10111 10112 // Otherwise, if they're both SCEVUnknown, it's possible that they hold 10113 // two different instructions with the same value. Check for this case. 10114 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) 10115 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) 10116 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) 10117 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) 10118 if (ComputesEqualValues(AI, BI)) 10119 return true; 10120 10121 // Otherwise assume they may have a different value. 10122 return false; 10123 } 10124 10125 bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred, 10126 const SCEV *&LHS, const SCEV *&RHS, 10127 unsigned Depth, 10128 bool ControllingFiniteLoop) { 10129 bool Changed = false; 10130 // Simplifies ICMP to trivial true or false by turning it into '0 == 0' or 10131 // '0 != 0'. 10132 auto TrivialCase = [&](bool TriviallyTrue) { 10133 LHS = RHS = getConstant(ConstantInt::getFalse(getContext())); 10134 Pred = TriviallyTrue ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE; 10135 return true; 10136 }; 10137 // If we hit the max recursion limit bail out. 10138 if (Depth >= 3) 10139 return false; 10140 10141 // Canonicalize a constant to the right side. 10142 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { 10143 // Check for both operands constant. 10144 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { 10145 if (ConstantExpr::getICmp(Pred, 10146 LHSC->getValue(), 10147 RHSC->getValue())->isNullValue()) 10148 return TrivialCase(false); 10149 else 10150 return TrivialCase(true); 10151 } 10152 // Otherwise swap the operands to put the constant on the right. 10153 std::swap(LHS, RHS); 10154 Pred = ICmpInst::getSwappedPredicate(Pred); 10155 Changed = true; 10156 } 10157 10158 // If we're comparing an addrec with a value which is loop-invariant in the 10159 // addrec's loop, put the addrec on the left. Also make a dominance check, 10160 // as both operands could be addrecs loop-invariant in each other's loop. 10161 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) { 10162 const Loop *L = AR->getLoop(); 10163 if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) { 10164 std::swap(LHS, RHS); 10165 Pred = ICmpInst::getSwappedPredicate(Pred); 10166 Changed = true; 10167 } 10168 } 10169 10170 // If there's a constant operand, canonicalize comparisons with boundary 10171 // cases, and canonicalize *-or-equal comparisons to regular comparisons. 10172 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { 10173 const APInt &RA = RC->getAPInt(); 10174 10175 bool SimplifiedByConstantRange = false; 10176 10177 if (!ICmpInst::isEquality(Pred)) { 10178 ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, RA); 10179 if (ExactCR.isFullSet()) 10180 return TrivialCase(true); 10181 else if (ExactCR.isEmptySet()) 10182 return TrivialCase(false); 10183 10184 APInt NewRHS; 10185 CmpInst::Predicate NewPred; 10186 if (ExactCR.getEquivalentICmp(NewPred, NewRHS) && 10187 ICmpInst::isEquality(NewPred)) { 10188 // We were able to convert an inequality to an equality. 10189 Pred = NewPred; 10190 RHS = getConstant(NewRHS); 10191 Changed = SimplifiedByConstantRange = true; 10192 } 10193 } 10194 10195 if (!SimplifiedByConstantRange) { 10196 switch (Pred) { 10197 default: 10198 break; 10199 case ICmpInst::ICMP_EQ: 10200 case ICmpInst::ICMP_NE: 10201 // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b. 10202 if (!RA) 10203 if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS)) 10204 if (const SCEVMulExpr *ME = 10205 dyn_cast<SCEVMulExpr>(AE->getOperand(0))) 10206 if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 && 10207 ME->getOperand(0)->isAllOnesValue()) { 10208 RHS = AE->getOperand(1); 10209 LHS = ME->getOperand(1); 10210 Changed = true; 10211 } 10212 break; 10213 10214 10215 // The "Should have been caught earlier!" messages refer to the fact 10216 // that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above 10217 // should have fired on the corresponding cases, and canonicalized the 10218 // check to trivial case. 10219 10220 case ICmpInst::ICMP_UGE: 10221 assert(!RA.isMinValue() && "Should have been caught earlier!"); 10222 Pred = ICmpInst::ICMP_UGT; 10223 RHS = getConstant(RA - 1); 10224 Changed = true; 10225 break; 10226 case ICmpInst::ICMP_ULE: 10227 assert(!RA.isMaxValue() && "Should have been caught earlier!"); 10228 Pred = ICmpInst::ICMP_ULT; 10229 RHS = getConstant(RA + 1); 10230 Changed = true; 10231 break; 10232 case ICmpInst::ICMP_SGE: 10233 assert(!RA.isMinSignedValue() && "Should have been caught earlier!"); 10234 Pred = ICmpInst::ICMP_SGT; 10235 RHS = getConstant(RA - 1); 10236 Changed = true; 10237 break; 10238 case ICmpInst::ICMP_SLE: 10239 assert(!RA.isMaxSignedValue() && "Should have been caught earlier!"); 10240 Pred = ICmpInst::ICMP_SLT; 10241 RHS = getConstant(RA + 1); 10242 Changed = true; 10243 break; 10244 } 10245 } 10246 } 10247 10248 // Check for obvious equality. 10249 if (HasSameValue(LHS, RHS)) { 10250 if (ICmpInst::isTrueWhenEqual(Pred)) 10251 return TrivialCase(true); 10252 if (ICmpInst::isFalseWhenEqual(Pred)) 10253 return TrivialCase(false); 10254 } 10255 10256 // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by 10257 // adding or subtracting 1 from one of the operands. This can be done for 10258 // one of two reasons: 10259 // 1) The range of the RHS does not include the (signed/unsigned) boundaries 10260 // 2) The loop is finite, with this comparison controlling the exit. Since the 10261 // loop is finite, the bound cannot include the corresponding boundary 10262 // (otherwise it would loop forever). 10263 switch (Pred) { 10264 case ICmpInst::ICMP_SLE: 10265 if (ControllingFiniteLoop || !getSignedRangeMax(RHS).isMaxSignedValue()) { 10266 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, 10267 SCEV::FlagNSW); 10268 Pred = ICmpInst::ICMP_SLT; 10269 Changed = true; 10270 } else if (!getSignedRangeMin(LHS).isMinSignedValue()) { 10271 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, 10272 SCEV::FlagNSW); 10273 Pred = ICmpInst::ICMP_SLT; 10274 Changed = true; 10275 } 10276 break; 10277 case ICmpInst::ICMP_SGE: 10278 if (ControllingFiniteLoop || !getSignedRangeMin(RHS).isMinSignedValue()) { 10279 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, 10280 SCEV::FlagNSW); 10281 Pred = ICmpInst::ICMP_SGT; 10282 Changed = true; 10283 } else if (!getSignedRangeMax(LHS).isMaxSignedValue()) { 10284 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, 10285 SCEV::FlagNSW); 10286 Pred = ICmpInst::ICMP_SGT; 10287 Changed = true; 10288 } 10289 break; 10290 case ICmpInst::ICMP_ULE: 10291 if (ControllingFiniteLoop || !getUnsignedRangeMax(RHS).isMaxValue()) { 10292 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, 10293 SCEV::FlagNUW); 10294 Pred = ICmpInst::ICMP_ULT; 10295 Changed = true; 10296 } else if (!getUnsignedRangeMin(LHS).isMinValue()) { 10297 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS); 10298 Pred = ICmpInst::ICMP_ULT; 10299 Changed = true; 10300 } 10301 break; 10302 case ICmpInst::ICMP_UGE: 10303 if (ControllingFiniteLoop || !getUnsignedRangeMin(RHS).isMinValue()) { 10304 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS); 10305 Pred = ICmpInst::ICMP_UGT; 10306 Changed = true; 10307 } else if (!getUnsignedRangeMax(LHS).isMaxValue()) { 10308 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, 10309 SCEV::FlagNUW); 10310 Pred = ICmpInst::ICMP_UGT; 10311 Changed = true; 10312 } 10313 break; 10314 default: 10315 break; 10316 } 10317 10318 // TODO: More simplifications are possible here. 10319 10320 // Recursively simplify until we either hit a recursion limit or nothing 10321 // changes. 10322 if (Changed) 10323 return SimplifyICmpOperands(Pred, LHS, RHS, Depth + 1, 10324 ControllingFiniteLoop); 10325 10326 return Changed; 10327 } 10328 10329 bool ScalarEvolution::isKnownNegative(const SCEV *S) { 10330 return getSignedRangeMax(S).isNegative(); 10331 } 10332 10333 bool ScalarEvolution::isKnownPositive(const SCEV *S) { 10334 return getSignedRangeMin(S).isStrictlyPositive(); 10335 } 10336 10337 bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { 10338 return !getSignedRangeMin(S).isNegative(); 10339 } 10340 10341 bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { 10342 return !getSignedRangeMax(S).isStrictlyPositive(); 10343 } 10344 10345 bool ScalarEvolution::isKnownNonZero(const SCEV *S) { 10346 return getUnsignedRangeMin(S) != 0; 10347 } 10348 10349 std::pair<const SCEV *, const SCEV *> 10350 ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) { 10351 // Compute SCEV on entry of loop L. 10352 const SCEV *Start = SCEVInitRewriter::rewrite(S, L, *this); 10353 if (Start == getCouldNotCompute()) 10354 return { Start, Start }; 10355 // Compute post increment SCEV for loop L. 10356 const SCEV *PostInc = SCEVPostIncRewriter::rewrite(S, L, *this); 10357 assert(PostInc != getCouldNotCompute() && "Unexpected could not compute"); 10358 return { Start, PostInc }; 10359 } 10360 10361 bool ScalarEvolution::isKnownViaInduction(ICmpInst::Predicate Pred, 10362 const SCEV *LHS, const SCEV *RHS) { 10363 // First collect all loops. 10364 SmallPtrSet<const Loop *, 8> LoopsUsed; 10365 getUsedLoops(LHS, LoopsUsed); 10366 getUsedLoops(RHS, LoopsUsed); 10367 10368 if (LoopsUsed.empty()) 10369 return false; 10370 10371 // Domination relationship must be a linear order on collected loops. 10372 #ifndef NDEBUG 10373 for (auto *L1 : LoopsUsed) 10374 for (auto *L2 : LoopsUsed) 10375 assert((DT.dominates(L1->getHeader(), L2->getHeader()) || 10376 DT.dominates(L2->getHeader(), L1->getHeader())) && 10377 "Domination relationship is not a linear order"); 10378 #endif 10379 10380 const Loop *MDL = 10381 *std::max_element(LoopsUsed.begin(), LoopsUsed.end(), 10382 [&](const Loop *L1, const Loop *L2) { 10383 return DT.properlyDominates(L1->getHeader(), L2->getHeader()); 10384 }); 10385 10386 // Get init and post increment value for LHS. 10387 auto SplitLHS = SplitIntoInitAndPostInc(MDL, LHS); 10388 // if LHS contains unknown non-invariant SCEV then bail out. 10389 if (SplitLHS.first == getCouldNotCompute()) 10390 return false; 10391 assert (SplitLHS.second != getCouldNotCompute() && "Unexpected CNC"); 10392 // Get init and post increment value for RHS. 10393 auto SplitRHS = SplitIntoInitAndPostInc(MDL, RHS); 10394 // if RHS contains unknown non-invariant SCEV then bail out. 10395 if (SplitRHS.first == getCouldNotCompute()) 10396 return false; 10397 assert (SplitRHS.second != getCouldNotCompute() && "Unexpected CNC"); 10398 // It is possible that init SCEV contains an invariant load but it does 10399 // not dominate MDL and is not available at MDL loop entry, so we should 10400 // check it here. 10401 if (!isAvailableAtLoopEntry(SplitLHS.first, MDL) || 10402 !isAvailableAtLoopEntry(SplitRHS.first, MDL)) 10403 return false; 10404 10405 // It seems backedge guard check is faster than entry one so in some cases 10406 // it can speed up whole estimation by short circuit 10407 return isLoopBackedgeGuardedByCond(MDL, Pred, SplitLHS.second, 10408 SplitRHS.second) && 10409 isLoopEntryGuardedByCond(MDL, Pred, SplitLHS.first, SplitRHS.first); 10410 } 10411 10412 bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, 10413 const SCEV *LHS, const SCEV *RHS) { 10414 // Canonicalize the inputs first. 10415 (void)SimplifyICmpOperands(Pred, LHS, RHS); 10416 10417 if (isKnownViaInduction(Pred, LHS, RHS)) 10418 return true; 10419 10420 if (isKnownPredicateViaSplitting(Pred, LHS, RHS)) 10421 return true; 10422 10423 // Otherwise see what can be done with some simple reasoning. 10424 return isKnownViaNonRecursiveReasoning(Pred, LHS, RHS); 10425 } 10426 10427 Optional<bool> ScalarEvolution::evaluatePredicate(ICmpInst::Predicate Pred, 10428 const SCEV *LHS, 10429 const SCEV *RHS) { 10430 if (isKnownPredicate(Pred, LHS, RHS)) 10431 return true; 10432 else if (isKnownPredicate(ICmpInst::getInversePredicate(Pred), LHS, RHS)) 10433 return false; 10434 return None; 10435 } 10436 10437 bool ScalarEvolution::isKnownPredicateAt(ICmpInst::Predicate Pred, 10438 const SCEV *LHS, const SCEV *RHS, 10439 const Instruction *CtxI) { 10440 // TODO: Analyze guards and assumes from Context's block. 10441 return isKnownPredicate(Pred, LHS, RHS) || 10442 isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred, LHS, RHS); 10443 } 10444 10445 Optional<bool> ScalarEvolution::evaluatePredicateAt(ICmpInst::Predicate Pred, 10446 const SCEV *LHS, 10447 const SCEV *RHS, 10448 const Instruction *CtxI) { 10449 Optional<bool> KnownWithoutContext = evaluatePredicate(Pred, LHS, RHS); 10450 if (KnownWithoutContext) 10451 return KnownWithoutContext; 10452 10453 if (isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred, LHS, RHS)) 10454 return true; 10455 else if (isBasicBlockEntryGuardedByCond(CtxI->getParent(), 10456 ICmpInst::getInversePredicate(Pred), 10457 LHS, RHS)) 10458 return false; 10459 return None; 10460 } 10461 10462 bool ScalarEvolution::isKnownOnEveryIteration(ICmpInst::Predicate Pred, 10463 const SCEVAddRecExpr *LHS, 10464 const SCEV *RHS) { 10465 const Loop *L = LHS->getLoop(); 10466 return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) && 10467 isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this), RHS); 10468 } 10469 10470 Optional<ScalarEvolution::MonotonicPredicateType> 10471 ScalarEvolution::getMonotonicPredicateType(const SCEVAddRecExpr *LHS, 10472 ICmpInst::Predicate Pred) { 10473 auto Result = getMonotonicPredicateTypeImpl(LHS, Pred); 10474 10475 #ifndef NDEBUG 10476 // Verify an invariant: inverting the predicate should turn a monotonically 10477 // increasing change to a monotonically decreasing one, and vice versa. 10478 if (Result) { 10479 auto ResultSwapped = 10480 getMonotonicPredicateTypeImpl(LHS, ICmpInst::getSwappedPredicate(Pred)); 10481 10482 assert(ResultSwapped.hasValue() && "should be able to analyze both!"); 10483 assert(ResultSwapped.getValue() != Result.getValue() && 10484 "monotonicity should flip as we flip the predicate"); 10485 } 10486 #endif 10487 10488 return Result; 10489 } 10490 10491 Optional<ScalarEvolution::MonotonicPredicateType> 10492 ScalarEvolution::getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS, 10493 ICmpInst::Predicate Pred) { 10494 // A zero step value for LHS means the induction variable is essentially a 10495 // loop invariant value. We don't really depend on the predicate actually 10496 // flipping from false to true (for increasing predicates, and the other way 10497 // around for decreasing predicates), all we care about is that *if* the 10498 // predicate changes then it only changes from false to true. 10499 // 10500 // A zero step value in itself is not very useful, but there may be places 10501 // where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be 10502 // as general as possible. 10503 10504 // Only handle LE/LT/GE/GT predicates. 10505 if (!ICmpInst::isRelational(Pred)) 10506 return None; 10507 10508 bool IsGreater = ICmpInst::isGE(Pred) || ICmpInst::isGT(Pred); 10509 assert((IsGreater || ICmpInst::isLE(Pred) || ICmpInst::isLT(Pred)) && 10510 "Should be greater or less!"); 10511 10512 // Check that AR does not wrap. 10513 if (ICmpInst::isUnsigned(Pred)) { 10514 if (!LHS->hasNoUnsignedWrap()) 10515 return None; 10516 return IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; 10517 } else { 10518 assert(ICmpInst::isSigned(Pred) && 10519 "Relational predicate is either signed or unsigned!"); 10520 if (!LHS->hasNoSignedWrap()) 10521 return None; 10522 10523 const SCEV *Step = LHS->getStepRecurrence(*this); 10524 10525 if (isKnownNonNegative(Step)) 10526 return IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; 10527 10528 if (isKnownNonPositive(Step)) 10529 return !IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing; 10530 10531 return None; 10532 } 10533 } 10534 10535 Optional<ScalarEvolution::LoopInvariantPredicate> 10536 ScalarEvolution::getLoopInvariantPredicate(ICmpInst::Predicate Pred, 10537 const SCEV *LHS, const SCEV *RHS, 10538 const Loop *L) { 10539 10540 // If there is a loop-invariant, force it into the RHS, otherwise bail out. 10541 if (!isLoopInvariant(RHS, L)) { 10542 if (!isLoopInvariant(LHS, L)) 10543 return None; 10544 10545 std::swap(LHS, RHS); 10546 Pred = ICmpInst::getSwappedPredicate(Pred); 10547 } 10548 10549 const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS); 10550 if (!ArLHS || ArLHS->getLoop() != L) 10551 return None; 10552 10553 auto MonotonicType = getMonotonicPredicateType(ArLHS, Pred); 10554 if (!MonotonicType) 10555 return None; 10556 // If the predicate "ArLHS `Pred` RHS" monotonically increases from false to 10557 // true as the loop iterates, and the backedge is control dependent on 10558 // "ArLHS `Pred` RHS" == true then we can reason as follows: 10559 // 10560 // * if the predicate was false in the first iteration then the predicate 10561 // is never evaluated again, since the loop exits without taking the 10562 // backedge. 10563 // * if the predicate was true in the first iteration then it will 10564 // continue to be true for all future iterations since it is 10565 // monotonically increasing. 10566 // 10567 // For both the above possibilities, we can replace the loop varying 10568 // predicate with its value on the first iteration of the loop (which is 10569 // loop invariant). 10570 // 10571 // A similar reasoning applies for a monotonically decreasing predicate, by 10572 // replacing true with false and false with true in the above two bullets. 10573 bool Increasing = *MonotonicType == ScalarEvolution::MonotonicallyIncreasing; 10574 auto P = Increasing ? Pred : ICmpInst::getInversePredicate(Pred); 10575 10576 if (!isLoopBackedgeGuardedByCond(L, P, LHS, RHS)) 10577 return None; 10578 10579 return ScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(), RHS); 10580 } 10581 10582 Optional<ScalarEvolution::LoopInvariantPredicate> 10583 ScalarEvolution::getLoopInvariantExitCondDuringFirstIterations( 10584 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, 10585 const Instruction *CtxI, const SCEV *MaxIter) { 10586 // Try to prove the following set of facts: 10587 // - The predicate is monotonic in the iteration space. 10588 // - If the check does not fail on the 1st iteration: 10589 // - No overflow will happen during first MaxIter iterations; 10590 // - It will not fail on the MaxIter'th iteration. 10591 // If the check does fail on the 1st iteration, we leave the loop and no 10592 // other checks matter. 10593 10594 // If there is a loop-invariant, force it into the RHS, otherwise bail out. 10595 if (!isLoopInvariant(RHS, L)) { 10596 if (!isLoopInvariant(LHS, L)) 10597 return None; 10598 10599 std::swap(LHS, RHS); 10600 Pred = ICmpInst::getSwappedPredicate(Pred); 10601 } 10602 10603 auto *AR = dyn_cast<SCEVAddRecExpr>(LHS); 10604 if (!AR || AR->getLoop() != L) 10605 return None; 10606 10607 // The predicate must be relational (i.e. <, <=, >=, >). 10608 if (!ICmpInst::isRelational(Pred)) 10609 return None; 10610 10611 // TODO: Support steps other than +/- 1. 10612 const SCEV *Step = AR->getStepRecurrence(*this); 10613 auto *One = getOne(Step->getType()); 10614 auto *MinusOne = getNegativeSCEV(One); 10615 if (Step != One && Step != MinusOne) 10616 return None; 10617 10618 // Type mismatch here means that MaxIter is potentially larger than max 10619 // unsigned value in start type, which mean we cannot prove no wrap for the 10620 // indvar. 10621 if (AR->getType() != MaxIter->getType()) 10622 return None; 10623 10624 // Value of IV on suggested last iteration. 10625 const SCEV *Last = AR->evaluateAtIteration(MaxIter, *this); 10626 // Does it still meet the requirement? 10627 if (!isLoopBackedgeGuardedByCond(L, Pred, Last, RHS)) 10628 return None; 10629 // Because step is +/- 1 and MaxIter has same type as Start (i.e. it does 10630 // not exceed max unsigned value of this type), this effectively proves 10631 // that there is no wrap during the iteration. To prove that there is no 10632 // signed/unsigned wrap, we need to check that 10633 // Start <= Last for step = 1 or Start >= Last for step = -1. 10634 ICmpInst::Predicate NoOverflowPred = 10635 CmpInst::isSigned(Pred) ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; 10636 if (Step == MinusOne) 10637 NoOverflowPred = CmpInst::getSwappedPredicate(NoOverflowPred); 10638 const SCEV *Start = AR->getStart(); 10639 if (!isKnownPredicateAt(NoOverflowPred, Start, Last, CtxI)) 10640 return None; 10641 10642 // Everything is fine. 10643 return ScalarEvolution::LoopInvariantPredicate(Pred, Start, RHS); 10644 } 10645 10646 bool ScalarEvolution::isKnownPredicateViaConstantRanges( 10647 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) { 10648 if (HasSameValue(LHS, RHS)) 10649 return ICmpInst::isTrueWhenEqual(Pred); 10650 10651 // This code is split out from isKnownPredicate because it is called from 10652 // within isLoopEntryGuardedByCond. 10653 10654 auto CheckRanges = [&](const ConstantRange &RangeLHS, 10655 const ConstantRange &RangeRHS) { 10656 return RangeLHS.icmp(Pred, RangeRHS); 10657 }; 10658 10659 // The check at the top of the function catches the case where the values are 10660 // known to be equal. 10661 if (Pred == CmpInst::ICMP_EQ) 10662 return false; 10663 10664 if (Pred == CmpInst::ICMP_NE) { 10665 auto SL = getSignedRange(LHS); 10666 auto SR = getSignedRange(RHS); 10667 if (CheckRanges(SL, SR)) 10668 return true; 10669 auto UL = getUnsignedRange(LHS); 10670 auto UR = getUnsignedRange(RHS); 10671 if (CheckRanges(UL, UR)) 10672 return true; 10673 auto *Diff = getMinusSCEV(LHS, RHS); 10674 return !isa<SCEVCouldNotCompute>(Diff) && isKnownNonZero(Diff); 10675 } 10676 10677 if (CmpInst::isSigned(Pred)) { 10678 auto SL = getSignedRange(LHS); 10679 auto SR = getSignedRange(RHS); 10680 return CheckRanges(SL, SR); 10681 } 10682 10683 auto UL = getUnsignedRange(LHS); 10684 auto UR = getUnsignedRange(RHS); 10685 return CheckRanges(UL, UR); 10686 } 10687 10688 bool ScalarEvolution::isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred, 10689 const SCEV *LHS, 10690 const SCEV *RHS) { 10691 // Match X to (A + C1)<ExpectedFlags> and Y to (A + C2)<ExpectedFlags>, where 10692 // C1 and C2 are constant integers. If either X or Y are not add expressions, 10693 // consider them as X + 0 and Y + 0 respectively. C1 and C2 are returned via 10694 // OutC1 and OutC2. 10695 auto MatchBinaryAddToConst = [this](const SCEV *X, const SCEV *Y, 10696 APInt &OutC1, APInt &OutC2, 10697 SCEV::NoWrapFlags ExpectedFlags) { 10698 const SCEV *XNonConstOp, *XConstOp; 10699 const SCEV *YNonConstOp, *YConstOp; 10700 SCEV::NoWrapFlags XFlagsPresent; 10701 SCEV::NoWrapFlags YFlagsPresent; 10702 10703 if (!splitBinaryAdd(X, XConstOp, XNonConstOp, XFlagsPresent)) { 10704 XConstOp = getZero(X->getType()); 10705 XNonConstOp = X; 10706 XFlagsPresent = ExpectedFlags; 10707 } 10708 if (!isa<SCEVConstant>(XConstOp) || 10709 (XFlagsPresent & ExpectedFlags) != ExpectedFlags) 10710 return false; 10711 10712 if (!splitBinaryAdd(Y, YConstOp, YNonConstOp, YFlagsPresent)) { 10713 YConstOp = getZero(Y->getType()); 10714 YNonConstOp = Y; 10715 YFlagsPresent = ExpectedFlags; 10716 } 10717 10718 if (!isa<SCEVConstant>(YConstOp) || 10719 (YFlagsPresent & ExpectedFlags) != ExpectedFlags) 10720 return false; 10721 10722 if (YNonConstOp != XNonConstOp) 10723 return false; 10724 10725 OutC1 = cast<SCEVConstant>(XConstOp)->getAPInt(); 10726 OutC2 = cast<SCEVConstant>(YConstOp)->getAPInt(); 10727 10728 return true; 10729 }; 10730 10731 APInt C1; 10732 APInt C2; 10733 10734 switch (Pred) { 10735 default: 10736 break; 10737 10738 case ICmpInst::ICMP_SGE: 10739 std::swap(LHS, RHS); 10740 LLVM_FALLTHROUGH; 10741 case ICmpInst::ICMP_SLE: 10742 // (X + C1)<nsw> s<= (X + C2)<nsw> if C1 s<= C2. 10743 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.sle(C2)) 10744 return true; 10745 10746 break; 10747 10748 case ICmpInst::ICMP_SGT: 10749 std::swap(LHS, RHS); 10750 LLVM_FALLTHROUGH; 10751 case ICmpInst::ICMP_SLT: 10752 // (X + C1)<nsw> s< (X + C2)<nsw> if C1 s< C2. 10753 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.slt(C2)) 10754 return true; 10755 10756 break; 10757 10758 case ICmpInst::ICMP_UGE: 10759 std::swap(LHS, RHS); 10760 LLVM_FALLTHROUGH; 10761 case ICmpInst::ICMP_ULE: 10762 // (X + C1)<nuw> u<= (X + C2)<nuw> for C1 u<= C2. 10763 if (MatchBinaryAddToConst(RHS, LHS, C2, C1, SCEV::FlagNUW) && C1.ule(C2)) 10764 return true; 10765 10766 break; 10767 10768 case ICmpInst::ICMP_UGT: 10769 std::swap(LHS, RHS); 10770 LLVM_FALLTHROUGH; 10771 case ICmpInst::ICMP_ULT: 10772 // (X + C1)<nuw> u< (X + C2)<nuw> if C1 u< C2. 10773 if (MatchBinaryAddToConst(RHS, LHS, C2, C1, SCEV::FlagNUW) && C1.ult(C2)) 10774 return true; 10775 break; 10776 } 10777 10778 return false; 10779 } 10780 10781 bool ScalarEvolution::isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, 10782 const SCEV *LHS, 10783 const SCEV *RHS) { 10784 if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate) 10785 return false; 10786 10787 // Allowing arbitrary number of activations of isKnownPredicateViaSplitting on 10788 // the stack can result in exponential time complexity. 10789 SaveAndRestore<bool> Restore(ProvingSplitPredicate, true); 10790 10791 // If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L 10792 // 10793 // To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use 10794 // isKnownPredicate. isKnownPredicate is more powerful, but also more 10795 // expensive; and using isKnownNonNegative(RHS) is sufficient for most of the 10796 // interesting cases seen in practice. We can consider "upgrading" L >= 0 to 10797 // use isKnownPredicate later if needed. 10798 return isKnownNonNegative(RHS) && 10799 isKnownPredicate(CmpInst::ICMP_SGE, LHS, getZero(LHS->getType())) && 10800 isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS); 10801 } 10802 10803 bool ScalarEvolution::isImpliedViaGuard(const BasicBlock *BB, 10804 ICmpInst::Predicate Pred, 10805 const SCEV *LHS, const SCEV *RHS) { 10806 // No need to even try if we know the module has no guards. 10807 if (!HasGuards) 10808 return false; 10809 10810 return any_of(*BB, [&](const Instruction &I) { 10811 using namespace llvm::PatternMatch; 10812 10813 Value *Condition; 10814 return match(&I, m_Intrinsic<Intrinsic::experimental_guard>( 10815 m_Value(Condition))) && 10816 isImpliedCond(Pred, LHS, RHS, Condition, false); 10817 }); 10818 } 10819 10820 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is 10821 /// protected by a conditional between LHS and RHS. This is used to 10822 /// to eliminate casts. 10823 bool 10824 ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, 10825 ICmpInst::Predicate Pred, 10826 const SCEV *LHS, const SCEV *RHS) { 10827 // Interpret a null as meaning no loop, where there is obviously no guard 10828 // (interprocedural conditions notwithstanding). 10829 if (!L) return true; 10830 10831 if (VerifyIR) 10832 assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) && 10833 "This cannot be done on broken IR!"); 10834 10835 10836 if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS)) 10837 return true; 10838 10839 BasicBlock *Latch = L->getLoopLatch(); 10840 if (!Latch) 10841 return false; 10842 10843 BranchInst *LoopContinuePredicate = 10844 dyn_cast<BranchInst>(Latch->getTerminator()); 10845 if (LoopContinuePredicate && LoopContinuePredicate->isConditional() && 10846 isImpliedCond(Pred, LHS, RHS, 10847 LoopContinuePredicate->getCondition(), 10848 LoopContinuePredicate->getSuccessor(0) != L->getHeader())) 10849 return true; 10850 10851 // We don't want more than one activation of the following loops on the stack 10852 // -- that can lead to O(n!) time complexity. 10853 if (WalkingBEDominatingConds) 10854 return false; 10855 10856 SaveAndRestore<bool> ClearOnExit(WalkingBEDominatingConds, true); 10857 10858 // See if we can exploit a trip count to prove the predicate. 10859 const auto &BETakenInfo = getBackedgeTakenInfo(L); 10860 const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this); 10861 if (LatchBECount != getCouldNotCompute()) { 10862 // We know that Latch branches back to the loop header exactly 10863 // LatchBECount times. This means the backdege condition at Latch is 10864 // equivalent to "{0,+,1} u< LatchBECount". 10865 Type *Ty = LatchBECount->getType(); 10866 auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW); 10867 const SCEV *LoopCounter = 10868 getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags); 10869 if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter, 10870 LatchBECount)) 10871 return true; 10872 } 10873 10874 // Check conditions due to any @llvm.assume intrinsics. 10875 for (auto &AssumeVH : AC.assumptions()) { 10876 if (!AssumeVH) 10877 continue; 10878 auto *CI = cast<CallInst>(AssumeVH); 10879 if (!DT.dominates(CI, Latch->getTerminator())) 10880 continue; 10881 10882 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false)) 10883 return true; 10884 } 10885 10886 // If the loop is not reachable from the entry block, we risk running into an 10887 // infinite loop as we walk up into the dom tree. These loops do not matter 10888 // anyway, so we just return a conservative answer when we see them. 10889 if (!DT.isReachableFromEntry(L->getHeader())) 10890 return false; 10891 10892 if (isImpliedViaGuard(Latch, Pred, LHS, RHS)) 10893 return true; 10894 10895 for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()]; 10896 DTN != HeaderDTN; DTN = DTN->getIDom()) { 10897 assert(DTN && "should reach the loop header before reaching the root!"); 10898 10899 BasicBlock *BB = DTN->getBlock(); 10900 if (isImpliedViaGuard(BB, Pred, LHS, RHS)) 10901 return true; 10902 10903 BasicBlock *PBB = BB->getSinglePredecessor(); 10904 if (!PBB) 10905 continue; 10906 10907 BranchInst *ContinuePredicate = dyn_cast<BranchInst>(PBB->getTerminator()); 10908 if (!ContinuePredicate || !ContinuePredicate->isConditional()) 10909 continue; 10910 10911 Value *Condition = ContinuePredicate->getCondition(); 10912 10913 // If we have an edge `E` within the loop body that dominates the only 10914 // latch, the condition guarding `E` also guards the backedge. This 10915 // reasoning works only for loops with a single latch. 10916 10917 BasicBlockEdge DominatingEdge(PBB, BB); 10918 if (DominatingEdge.isSingleEdge()) { 10919 // We're constructively (and conservatively) enumerating edges within the 10920 // loop body that dominate the latch. The dominator tree better agree 10921 // with us on this: 10922 assert(DT.dominates(DominatingEdge, Latch) && "should be!"); 10923 10924 if (isImpliedCond(Pred, LHS, RHS, Condition, 10925 BB != ContinuePredicate->getSuccessor(0))) 10926 return true; 10927 } 10928 } 10929 10930 return false; 10931 } 10932 10933 bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB, 10934 ICmpInst::Predicate Pred, 10935 const SCEV *LHS, 10936 const SCEV *RHS) { 10937 if (VerifyIR) 10938 assert(!verifyFunction(*BB->getParent(), &dbgs()) && 10939 "This cannot be done on broken IR!"); 10940 10941 // If we cannot prove strict comparison (e.g. a > b), maybe we can prove 10942 // the facts (a >= b && a != b) separately. A typical situation is when the 10943 // non-strict comparison is known from ranges and non-equality is known from 10944 // dominating predicates. If we are proving strict comparison, we always try 10945 // to prove non-equality and non-strict comparison separately. 10946 auto NonStrictPredicate = ICmpInst::getNonStrictPredicate(Pred); 10947 const bool ProvingStrictComparison = (Pred != NonStrictPredicate); 10948 bool ProvedNonStrictComparison = false; 10949 bool ProvedNonEquality = false; 10950 10951 auto SplitAndProve = 10952 [&](std::function<bool(ICmpInst::Predicate)> Fn) -> bool { 10953 if (!ProvedNonStrictComparison) 10954 ProvedNonStrictComparison = Fn(NonStrictPredicate); 10955 if (!ProvedNonEquality) 10956 ProvedNonEquality = Fn(ICmpInst::ICMP_NE); 10957 if (ProvedNonStrictComparison && ProvedNonEquality) 10958 return true; 10959 return false; 10960 }; 10961 10962 if (ProvingStrictComparison) { 10963 auto ProofFn = [&](ICmpInst::Predicate P) { 10964 return isKnownViaNonRecursiveReasoning(P, LHS, RHS); 10965 }; 10966 if (SplitAndProve(ProofFn)) 10967 return true; 10968 } 10969 10970 // Try to prove (Pred, LHS, RHS) using isImpliedViaGuard. 10971 auto ProveViaGuard = [&](const BasicBlock *Block) { 10972 if (isImpliedViaGuard(Block, Pred, LHS, RHS)) 10973 return true; 10974 if (ProvingStrictComparison) { 10975 auto ProofFn = [&](ICmpInst::Predicate P) { 10976 return isImpliedViaGuard(Block, P, LHS, RHS); 10977 }; 10978 if (SplitAndProve(ProofFn)) 10979 return true; 10980 } 10981 return false; 10982 }; 10983 10984 // Try to prove (Pred, LHS, RHS) using isImpliedCond. 10985 auto ProveViaCond = [&](const Value *Condition, bool Inverse) { 10986 const Instruction *CtxI = &BB->front(); 10987 if (isImpliedCond(Pred, LHS, RHS, Condition, Inverse, CtxI)) 10988 return true; 10989 if (ProvingStrictComparison) { 10990 auto ProofFn = [&](ICmpInst::Predicate P) { 10991 return isImpliedCond(P, LHS, RHS, Condition, Inverse, CtxI); 10992 }; 10993 if (SplitAndProve(ProofFn)) 10994 return true; 10995 } 10996 return false; 10997 }; 10998 10999 // Starting at the block's predecessor, climb up the predecessor chain, as long 11000 // as there are predecessors that can be found that have unique successors 11001 // leading to the original block. 11002 const Loop *ContainingLoop = LI.getLoopFor(BB); 11003 const BasicBlock *PredBB; 11004 if (ContainingLoop && ContainingLoop->getHeader() == BB) 11005 PredBB = ContainingLoop->getLoopPredecessor(); 11006 else 11007 PredBB = BB->getSinglePredecessor(); 11008 for (std::pair<const BasicBlock *, const BasicBlock *> Pair(PredBB, BB); 11009 Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { 11010 if (ProveViaGuard(Pair.first)) 11011 return true; 11012 11013 const BranchInst *LoopEntryPredicate = 11014 dyn_cast<BranchInst>(Pair.first->getTerminator()); 11015 if (!LoopEntryPredicate || 11016 LoopEntryPredicate->isUnconditional()) 11017 continue; 11018 11019 if (ProveViaCond(LoopEntryPredicate->getCondition(), 11020 LoopEntryPredicate->getSuccessor(0) != Pair.second)) 11021 return true; 11022 } 11023 11024 // Check conditions due to any @llvm.assume intrinsics. 11025 for (auto &AssumeVH : AC.assumptions()) { 11026 if (!AssumeVH) 11027 continue; 11028 auto *CI = cast<CallInst>(AssumeVH); 11029 if (!DT.dominates(CI, BB)) 11030 continue; 11031 11032 if (ProveViaCond(CI->getArgOperand(0), false)) 11033 return true; 11034 } 11035 11036 return false; 11037 } 11038 11039 bool ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L, 11040 ICmpInst::Predicate Pred, 11041 const SCEV *LHS, 11042 const SCEV *RHS) { 11043 // Interpret a null as meaning no loop, where there is obviously no guard 11044 // (interprocedural conditions notwithstanding). 11045 if (!L) 11046 return false; 11047 11048 // Both LHS and RHS must be available at loop entry. 11049 assert(isAvailableAtLoopEntry(LHS, L) && 11050 "LHS is not available at Loop Entry"); 11051 assert(isAvailableAtLoopEntry(RHS, L) && 11052 "RHS is not available at Loop Entry"); 11053 11054 if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS)) 11055 return true; 11056 11057 return isBasicBlockEntryGuardedByCond(L->getHeader(), Pred, LHS, RHS); 11058 } 11059 11060 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, 11061 const SCEV *RHS, 11062 const Value *FoundCondValue, bool Inverse, 11063 const Instruction *CtxI) { 11064 // False conditions implies anything. Do not bother analyzing it further. 11065 if (FoundCondValue == 11066 ConstantInt::getBool(FoundCondValue->getContext(), Inverse)) 11067 return true; 11068 11069 if (!PendingLoopPredicates.insert(FoundCondValue).second) 11070 return false; 11071 11072 auto ClearOnExit = 11073 make_scope_exit([&]() { PendingLoopPredicates.erase(FoundCondValue); }); 11074 11075 // Recursively handle And and Or conditions. 11076 const Value *Op0, *Op1; 11077 if (match(FoundCondValue, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) { 11078 if (!Inverse) 11079 return isImpliedCond(Pred, LHS, RHS, Op0, Inverse, CtxI) || 11080 isImpliedCond(Pred, LHS, RHS, Op1, Inverse, CtxI); 11081 } else if (match(FoundCondValue, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) { 11082 if (Inverse) 11083 return isImpliedCond(Pred, LHS, RHS, Op0, Inverse, CtxI) || 11084 isImpliedCond(Pred, LHS, RHS, Op1, Inverse, CtxI); 11085 } 11086 11087 const ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue); 11088 if (!ICI) return false; 11089 11090 // Now that we found a conditional branch that dominates the loop or controls 11091 // the loop latch. Check to see if it is the comparison we are looking for. 11092 ICmpInst::Predicate FoundPred; 11093 if (Inverse) 11094 FoundPred = ICI->getInversePredicate(); 11095 else 11096 FoundPred = ICI->getPredicate(); 11097 11098 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); 11099 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); 11100 11101 return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS, CtxI); 11102 } 11103 11104 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, 11105 const SCEV *RHS, 11106 ICmpInst::Predicate FoundPred, 11107 const SCEV *FoundLHS, const SCEV *FoundRHS, 11108 const Instruction *CtxI) { 11109 // Balance the types. 11110 if (getTypeSizeInBits(LHS->getType()) < 11111 getTypeSizeInBits(FoundLHS->getType())) { 11112 // For unsigned and equality predicates, try to prove that both found 11113 // operands fit into narrow unsigned range. If so, try to prove facts in 11114 // narrow types. 11115 if (!CmpInst::isSigned(FoundPred) && !FoundLHS->getType()->isPointerTy() && 11116 !FoundRHS->getType()->isPointerTy()) { 11117 auto *NarrowType = LHS->getType(); 11118 auto *WideType = FoundLHS->getType(); 11119 auto BitWidth = getTypeSizeInBits(NarrowType); 11120 const SCEV *MaxValue = getZeroExtendExpr( 11121 getConstant(APInt::getMaxValue(BitWidth)), WideType); 11122 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundLHS, 11123 MaxValue) && 11124 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundRHS, 11125 MaxValue)) { 11126 const SCEV *TruncFoundLHS = getTruncateExpr(FoundLHS, NarrowType); 11127 const SCEV *TruncFoundRHS = getTruncateExpr(FoundRHS, NarrowType); 11128 if (isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, TruncFoundLHS, 11129 TruncFoundRHS, CtxI)) 11130 return true; 11131 } 11132 } 11133 11134 if (LHS->getType()->isPointerTy() || RHS->getType()->isPointerTy()) 11135 return false; 11136 if (CmpInst::isSigned(Pred)) { 11137 LHS = getSignExtendExpr(LHS, FoundLHS->getType()); 11138 RHS = getSignExtendExpr(RHS, FoundLHS->getType()); 11139 } else { 11140 LHS = getZeroExtendExpr(LHS, FoundLHS->getType()); 11141 RHS = getZeroExtendExpr(RHS, FoundLHS->getType()); 11142 } 11143 } else if (getTypeSizeInBits(LHS->getType()) > 11144 getTypeSizeInBits(FoundLHS->getType())) { 11145 if (FoundLHS->getType()->isPointerTy() || FoundRHS->getType()->isPointerTy()) 11146 return false; 11147 if (CmpInst::isSigned(FoundPred)) { 11148 FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); 11149 FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); 11150 } else { 11151 FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); 11152 FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); 11153 } 11154 } 11155 return isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, FoundLHS, 11156 FoundRHS, CtxI); 11157 } 11158 11159 bool ScalarEvolution::isImpliedCondBalancedTypes( 11160 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, 11161 ICmpInst::Predicate FoundPred, const SCEV *FoundLHS, const SCEV *FoundRHS, 11162 const Instruction *CtxI) { 11163 assert(getTypeSizeInBits(LHS->getType()) == 11164 getTypeSizeInBits(FoundLHS->getType()) && 11165 "Types should be balanced!"); 11166 // Canonicalize the query to match the way instcombine will have 11167 // canonicalized the comparison. 11168 if (SimplifyICmpOperands(Pred, LHS, RHS)) 11169 if (LHS == RHS) 11170 return CmpInst::isTrueWhenEqual(Pred); 11171 if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS)) 11172 if (FoundLHS == FoundRHS) 11173 return CmpInst::isFalseWhenEqual(FoundPred); 11174 11175 // Check to see if we can make the LHS or RHS match. 11176 if (LHS == FoundRHS || RHS == FoundLHS) { 11177 if (isa<SCEVConstant>(RHS)) { 11178 std::swap(FoundLHS, FoundRHS); 11179 FoundPred = ICmpInst::getSwappedPredicate(FoundPred); 11180 } else { 11181 std::swap(LHS, RHS); 11182 Pred = ICmpInst::getSwappedPredicate(Pred); 11183 } 11184 } 11185 11186 // Check whether the found predicate is the same as the desired predicate. 11187 if (FoundPred == Pred) 11188 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI); 11189 11190 // Check whether swapping the found predicate makes it the same as the 11191 // desired predicate. 11192 if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { 11193 // We can write the implication 11194 // 0. LHS Pred RHS <- FoundLHS SwapPred FoundRHS 11195 // using one of the following ways: 11196 // 1. LHS Pred RHS <- FoundRHS Pred FoundLHS 11197 // 2. RHS SwapPred LHS <- FoundLHS SwapPred FoundRHS 11198 // 3. LHS Pred RHS <- ~FoundLHS Pred ~FoundRHS 11199 // 4. ~LHS SwapPred ~RHS <- FoundLHS SwapPred FoundRHS 11200 // Forms 1. and 2. require swapping the operands of one condition. Don't 11201 // do this if it would break canonical constant/addrec ordering. 11202 if (!isa<SCEVConstant>(RHS) && !isa<SCEVAddRecExpr>(LHS)) 11203 return isImpliedCondOperands(FoundPred, RHS, LHS, FoundLHS, FoundRHS, 11204 CtxI); 11205 if (!isa<SCEVConstant>(FoundRHS) && !isa<SCEVAddRecExpr>(FoundLHS)) 11206 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS, CtxI); 11207 11208 // There's no clear preference between forms 3. and 4., try both. Avoid 11209 // forming getNotSCEV of pointer values as the resulting subtract is 11210 // not legal. 11211 if (!LHS->getType()->isPointerTy() && !RHS->getType()->isPointerTy() && 11212 isImpliedCondOperands(FoundPred, getNotSCEV(LHS), getNotSCEV(RHS), 11213 FoundLHS, FoundRHS, CtxI)) 11214 return true; 11215 11216 if (!FoundLHS->getType()->isPointerTy() && 11217 !FoundRHS->getType()->isPointerTy() && 11218 isImpliedCondOperands(Pred, LHS, RHS, getNotSCEV(FoundLHS), 11219 getNotSCEV(FoundRHS), CtxI)) 11220 return true; 11221 11222 return false; 11223 } 11224 11225 auto IsSignFlippedPredicate = [](CmpInst::Predicate P1, 11226 CmpInst::Predicate P2) { 11227 assert(P1 != P2 && "Handled earlier!"); 11228 return CmpInst::isRelational(P2) && 11229 P1 == CmpInst::getFlippedSignednessPredicate(P2); 11230 }; 11231 if (IsSignFlippedPredicate(Pred, FoundPred)) { 11232 // Unsigned comparison is the same as signed comparison when both the 11233 // operands are non-negative or negative. 11234 if ((isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) || 11235 (isKnownNegative(FoundLHS) && isKnownNegative(FoundRHS))) 11236 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI); 11237 // Create local copies that we can freely swap and canonicalize our 11238 // conditions to "le/lt". 11239 ICmpInst::Predicate CanonicalPred = Pred, CanonicalFoundPred = FoundPred; 11240 const SCEV *CanonicalLHS = LHS, *CanonicalRHS = RHS, 11241 *CanonicalFoundLHS = FoundLHS, *CanonicalFoundRHS = FoundRHS; 11242 if (ICmpInst::isGT(CanonicalPred) || ICmpInst::isGE(CanonicalPred)) { 11243 CanonicalPred = ICmpInst::getSwappedPredicate(CanonicalPred); 11244 CanonicalFoundPred = ICmpInst::getSwappedPredicate(CanonicalFoundPred); 11245 std::swap(CanonicalLHS, CanonicalRHS); 11246 std::swap(CanonicalFoundLHS, CanonicalFoundRHS); 11247 } 11248 assert((ICmpInst::isLT(CanonicalPred) || ICmpInst::isLE(CanonicalPred)) && 11249 "Must be!"); 11250 assert((ICmpInst::isLT(CanonicalFoundPred) || 11251 ICmpInst::isLE(CanonicalFoundPred)) && 11252 "Must be!"); 11253 if (ICmpInst::isSigned(CanonicalPred) && isKnownNonNegative(CanonicalRHS)) 11254 // Use implication: 11255 // x <u y && y >=s 0 --> x <s y. 11256 // If we can prove the left part, the right part is also proven. 11257 return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS, 11258 CanonicalRHS, CanonicalFoundLHS, 11259 CanonicalFoundRHS); 11260 if (ICmpInst::isUnsigned(CanonicalPred) && isKnownNegative(CanonicalRHS)) 11261 // Use implication: 11262 // x <s y && y <s 0 --> x <u y. 11263 // If we can prove the left part, the right part is also proven. 11264 return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS, 11265 CanonicalRHS, CanonicalFoundLHS, 11266 CanonicalFoundRHS); 11267 } 11268 11269 // Check if we can make progress by sharpening ranges. 11270 if (FoundPred == ICmpInst::ICMP_NE && 11271 (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) { 11272 11273 const SCEVConstant *C = nullptr; 11274 const SCEV *V = nullptr; 11275 11276 if (isa<SCEVConstant>(FoundLHS)) { 11277 C = cast<SCEVConstant>(FoundLHS); 11278 V = FoundRHS; 11279 } else { 11280 C = cast<SCEVConstant>(FoundRHS); 11281 V = FoundLHS; 11282 } 11283 11284 // The guarding predicate tells us that C != V. If the known range 11285 // of V is [C, t), we can sharpen the range to [C + 1, t). The 11286 // range we consider has to correspond to same signedness as the 11287 // predicate we're interested in folding. 11288 11289 APInt Min = ICmpInst::isSigned(Pred) ? 11290 getSignedRangeMin(V) : getUnsignedRangeMin(V); 11291 11292 if (Min == C->getAPInt()) { 11293 // Given (V >= Min && V != Min) we conclude V >= (Min + 1). 11294 // This is true even if (Min + 1) wraps around -- in case of 11295 // wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)). 11296 11297 APInt SharperMin = Min + 1; 11298 11299 switch (Pred) { 11300 case ICmpInst::ICMP_SGE: 11301 case ICmpInst::ICMP_UGE: 11302 // We know V `Pred` SharperMin. If this implies LHS `Pred` 11303 // RHS, we're done. 11304 if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(SharperMin), 11305 CtxI)) 11306 return true; 11307 LLVM_FALLTHROUGH; 11308 11309 case ICmpInst::ICMP_SGT: 11310 case ICmpInst::ICMP_UGT: 11311 // We know from the range information that (V `Pred` Min || 11312 // V == Min). We know from the guarding condition that !(V 11313 // == Min). This gives us 11314 // 11315 // V `Pred` Min || V == Min && !(V == Min) 11316 // => V `Pred` Min 11317 // 11318 // If V `Pred` Min implies LHS `Pred` RHS, we're done. 11319 11320 if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min), CtxI)) 11321 return true; 11322 break; 11323 11324 // `LHS < RHS` and `LHS <= RHS` are handled in the same way as `RHS > LHS` and `RHS >= LHS` respectively. 11325 case ICmpInst::ICMP_SLE: 11326 case ICmpInst::ICMP_ULE: 11327 if (isImpliedCondOperands(CmpInst::getSwappedPredicate(Pred), RHS, 11328 LHS, V, getConstant(SharperMin), CtxI)) 11329 return true; 11330 LLVM_FALLTHROUGH; 11331 11332 case ICmpInst::ICMP_SLT: 11333 case ICmpInst::ICMP_ULT: 11334 if (isImpliedCondOperands(CmpInst::getSwappedPredicate(Pred), RHS, 11335 LHS, V, getConstant(Min), CtxI)) 11336 return true; 11337 break; 11338 11339 default: 11340 // No change 11341 break; 11342 } 11343 } 11344 } 11345 11346 // Check whether the actual condition is beyond sufficient. 11347 if (FoundPred == ICmpInst::ICMP_EQ) 11348 if (ICmpInst::isTrueWhenEqual(Pred)) 11349 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI)) 11350 return true; 11351 if (Pred == ICmpInst::ICMP_NE) 11352 if (!ICmpInst::isTrueWhenEqual(FoundPred)) 11353 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS, CtxI)) 11354 return true; 11355 11356 // Otherwise assume the worst. 11357 return false; 11358 } 11359 11360 bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr, 11361 const SCEV *&L, const SCEV *&R, 11362 SCEV::NoWrapFlags &Flags) { 11363 const auto *AE = dyn_cast<SCEVAddExpr>(Expr); 11364 if (!AE || AE->getNumOperands() != 2) 11365 return false; 11366 11367 L = AE->getOperand(0); 11368 R = AE->getOperand(1); 11369 Flags = AE->getNoWrapFlags(); 11370 return true; 11371 } 11372 11373 Optional<APInt> ScalarEvolution::computeConstantDifference(const SCEV *More, 11374 const SCEV *Less) { 11375 // We avoid subtracting expressions here because this function is usually 11376 // fairly deep in the call stack (i.e. is called many times). 11377 11378 // X - X = 0. 11379 if (More == Less) 11380 return APInt(getTypeSizeInBits(More->getType()), 0); 11381 11382 if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) { 11383 const auto *LAR = cast<SCEVAddRecExpr>(Less); 11384 const auto *MAR = cast<SCEVAddRecExpr>(More); 11385 11386 if (LAR->getLoop() != MAR->getLoop()) 11387 return None; 11388 11389 // We look at affine expressions only; not for correctness but to keep 11390 // getStepRecurrence cheap. 11391 if (!LAR->isAffine() || !MAR->isAffine()) 11392 return None; 11393 11394 if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this)) 11395 return None; 11396 11397 Less = LAR->getStart(); 11398 More = MAR->getStart(); 11399 11400 // fall through 11401 } 11402 11403 if (isa<SCEVConstant>(Less) && isa<SCEVConstant>(More)) { 11404 const auto &M = cast<SCEVConstant>(More)->getAPInt(); 11405 const auto &L = cast<SCEVConstant>(Less)->getAPInt(); 11406 return M - L; 11407 } 11408 11409 SCEV::NoWrapFlags Flags; 11410 const SCEV *LLess = nullptr, *RLess = nullptr; 11411 const SCEV *LMore = nullptr, *RMore = nullptr; 11412 const SCEVConstant *C1 = nullptr, *C2 = nullptr; 11413 // Compare (X + C1) vs X. 11414 if (splitBinaryAdd(Less, LLess, RLess, Flags)) 11415 if ((C1 = dyn_cast<SCEVConstant>(LLess))) 11416 if (RLess == More) 11417 return -(C1->getAPInt()); 11418 11419 // Compare X vs (X + C2). 11420 if (splitBinaryAdd(More, LMore, RMore, Flags)) 11421 if ((C2 = dyn_cast<SCEVConstant>(LMore))) 11422 if (RMore == Less) 11423 return C2->getAPInt(); 11424 11425 // Compare (X + C1) vs (X + C2). 11426 if (C1 && C2 && RLess == RMore) 11427 return C2->getAPInt() - C1->getAPInt(); 11428 11429 return None; 11430 } 11431 11432 bool ScalarEvolution::isImpliedCondOperandsViaAddRecStart( 11433 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, 11434 const SCEV *FoundLHS, const SCEV *FoundRHS, const Instruction *CtxI) { 11435 // Try to recognize the following pattern: 11436 // 11437 // FoundRHS = ... 11438 // ... 11439 // loop: 11440 // FoundLHS = {Start,+,W} 11441 // context_bb: // Basic block from the same loop 11442 // known(Pred, FoundLHS, FoundRHS) 11443 // 11444 // If some predicate is known in the context of a loop, it is also known on 11445 // each iteration of this loop, including the first iteration. Therefore, in 11446 // this case, `FoundLHS Pred FoundRHS` implies `Start Pred FoundRHS`. Try to 11447 // prove the original pred using this fact. 11448 if (!CtxI) 11449 return false; 11450 const BasicBlock *ContextBB = CtxI->getParent(); 11451 // Make sure AR varies in the context block. 11452 if (auto *AR = dyn_cast<SCEVAddRecExpr>(FoundLHS)) { 11453 const Loop *L = AR->getLoop(); 11454 // Make sure that context belongs to the loop and executes on 1st iteration 11455 // (if it ever executes at all). 11456 if (!L->contains(ContextBB) || !DT.dominates(ContextBB, L->getLoopLatch())) 11457 return false; 11458 if (!isAvailableAtLoopEntry(FoundRHS, AR->getLoop())) 11459 return false; 11460 return isImpliedCondOperands(Pred, LHS, RHS, AR->getStart(), FoundRHS); 11461 } 11462 11463 if (auto *AR = dyn_cast<SCEVAddRecExpr>(FoundRHS)) { 11464 const Loop *L = AR->getLoop(); 11465 // Make sure that context belongs to the loop and executes on 1st iteration 11466 // (if it ever executes at all). 11467 if (!L->contains(ContextBB) || !DT.dominates(ContextBB, L->getLoopLatch())) 11468 return false; 11469 if (!isAvailableAtLoopEntry(FoundLHS, AR->getLoop())) 11470 return false; 11471 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, AR->getStart()); 11472 } 11473 11474 return false; 11475 } 11476 11477 bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow( 11478 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, 11479 const SCEV *FoundLHS, const SCEV *FoundRHS) { 11480 if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT) 11481 return false; 11482 11483 const auto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(LHS); 11484 if (!AddRecLHS) 11485 return false; 11486 11487 const auto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(FoundLHS); 11488 if (!AddRecFoundLHS) 11489 return false; 11490 11491 // We'd like to let SCEV reason about control dependencies, so we constrain 11492 // both the inequalities to be about add recurrences on the same loop. This 11493 // way we can use isLoopEntryGuardedByCond later. 11494 11495 const Loop *L = AddRecFoundLHS->getLoop(); 11496 if (L != AddRecLHS->getLoop()) 11497 return false; 11498 11499 // FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1) 11500 // 11501 // FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C) 11502 // ... (2) 11503 // 11504 // Informal proof for (2), assuming (1) [*]: 11505 // 11506 // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**] 11507 // 11508 // Then 11509 // 11510 // FoundLHS s< FoundRHS s< INT_MIN - C 11511 // <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ] 11512 // <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ] 11513 // <=> (FoundLHS + INT_MIN + C + INT_MIN) s< 11514 // (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ] 11515 // <=> FoundLHS + C s< FoundRHS + C 11516 // 11517 // [*]: (1) can be proved by ruling out overflow. 11518 // 11519 // [**]: This can be proved by analyzing all the four possibilities: 11520 // (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and 11521 // (A s>= 0, B s>= 0). 11522 // 11523 // Note: 11524 // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C" 11525 // will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS 11526 // = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS 11527 // s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is 11528 // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS + 11529 // C)". 11530 11531 Optional<APInt> LDiff = computeConstantDifference(LHS, FoundLHS); 11532 Optional<APInt> RDiff = computeConstantDifference(RHS, FoundRHS); 11533 if (!LDiff || !RDiff || *LDiff != *RDiff) 11534 return false; 11535 11536 if (LDiff->isMinValue()) 11537 return true; 11538 11539 APInt FoundRHSLimit; 11540 11541 if (Pred == CmpInst::ICMP_ULT) { 11542 FoundRHSLimit = -(*RDiff); 11543 } else { 11544 assert(Pred == CmpInst::ICMP_SLT && "Checked above!"); 11545 FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff; 11546 } 11547 11548 // Try to prove (1) or (2), as needed. 11549 return isAvailableAtLoopEntry(FoundRHS, L) && 11550 isLoopEntryGuardedByCond(L, Pred, FoundRHS, 11551 getConstant(FoundRHSLimit)); 11552 } 11553 11554 bool ScalarEvolution::isImpliedViaMerge(ICmpInst::Predicate Pred, 11555 const SCEV *LHS, const SCEV *RHS, 11556 const SCEV *FoundLHS, 11557 const SCEV *FoundRHS, unsigned Depth) { 11558 const PHINode *LPhi = nullptr, *RPhi = nullptr; 11559 11560 auto ClearOnExit = make_scope_exit([&]() { 11561 if (LPhi) { 11562 bool Erased = PendingMerges.erase(LPhi); 11563 assert(Erased && "Failed to erase LPhi!"); 11564 (void)Erased; 11565 } 11566 if (RPhi) { 11567 bool Erased = PendingMerges.erase(RPhi); 11568 assert(Erased && "Failed to erase RPhi!"); 11569 (void)Erased; 11570 } 11571 }); 11572 11573 // Find respective Phis and check that they are not being pending. 11574 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) 11575 if (auto *Phi = dyn_cast<PHINode>(LU->getValue())) { 11576 if (!PendingMerges.insert(Phi).second) 11577 return false; 11578 LPhi = Phi; 11579 } 11580 if (const SCEVUnknown *RU = dyn_cast<SCEVUnknown>(RHS)) 11581 if (auto *Phi = dyn_cast<PHINode>(RU->getValue())) { 11582 // If we detect a loop of Phi nodes being processed by this method, for 11583 // example: 11584 // 11585 // %a = phi i32 [ %some1, %preheader ], [ %b, %latch ] 11586 // %b = phi i32 [ %some2, %preheader ], [ %a, %latch ] 11587 // 11588 // we don't want to deal with a case that complex, so return conservative 11589 // answer false. 11590 if (!PendingMerges.insert(Phi).second) 11591 return false; 11592 RPhi = Phi; 11593 } 11594 11595 // If none of LHS, RHS is a Phi, nothing to do here. 11596 if (!LPhi && !RPhi) 11597 return false; 11598 11599 // If there is a SCEVUnknown Phi we are interested in, make it left. 11600 if (!LPhi) { 11601 std::swap(LHS, RHS); 11602 std::swap(FoundLHS, FoundRHS); 11603 std::swap(LPhi, RPhi); 11604 Pred = ICmpInst::getSwappedPredicate(Pred); 11605 } 11606 11607 assert(LPhi && "LPhi should definitely be a SCEVUnknown Phi!"); 11608 const BasicBlock *LBB = LPhi->getParent(); 11609 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS); 11610 11611 auto ProvedEasily = [&](const SCEV *S1, const SCEV *S2) { 11612 return isKnownViaNonRecursiveReasoning(Pred, S1, S2) || 11613 isImpliedCondOperandsViaRanges(Pred, S1, S2, FoundLHS, FoundRHS) || 11614 isImpliedViaOperations(Pred, S1, S2, FoundLHS, FoundRHS, Depth); 11615 }; 11616 11617 if (RPhi && RPhi->getParent() == LBB) { 11618 // Case one: RHS is also a SCEVUnknown Phi from the same basic block. 11619 // If we compare two Phis from the same block, and for each entry block 11620 // the predicate is true for incoming values from this block, then the 11621 // predicate is also true for the Phis. 11622 for (const BasicBlock *IncBB : predecessors(LBB)) { 11623 const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB)); 11624 const SCEV *R = getSCEV(RPhi->getIncomingValueForBlock(IncBB)); 11625 if (!ProvedEasily(L, R)) 11626 return false; 11627 } 11628 } else if (RAR && RAR->getLoop()->getHeader() == LBB) { 11629 // Case two: RHS is also a Phi from the same basic block, and it is an 11630 // AddRec. It means that there is a loop which has both AddRec and Unknown 11631 // PHIs, for it we can compare incoming values of AddRec from above the loop 11632 // and latch with their respective incoming values of LPhi. 11633 // TODO: Generalize to handle loops with many inputs in a header. 11634 if (LPhi->getNumIncomingValues() != 2) return false; 11635 11636 auto *RLoop = RAR->getLoop(); 11637 auto *Predecessor = RLoop->getLoopPredecessor(); 11638 assert(Predecessor && "Loop with AddRec with no predecessor?"); 11639 const SCEV *L1 = getSCEV(LPhi->getIncomingValueForBlock(Predecessor)); 11640 if (!ProvedEasily(L1, RAR->getStart())) 11641 return false; 11642 auto *Latch = RLoop->getLoopLatch(); 11643 assert(Latch && "Loop with AddRec with no latch?"); 11644 const SCEV *L2 = getSCEV(LPhi->getIncomingValueForBlock(Latch)); 11645 if (!ProvedEasily(L2, RAR->getPostIncExpr(*this))) 11646 return false; 11647 } else { 11648 // In all other cases go over inputs of LHS and compare each of them to RHS, 11649 // the predicate is true for (LHS, RHS) if it is true for all such pairs. 11650 // At this point RHS is either a non-Phi, or it is a Phi from some block 11651 // different from LBB. 11652 for (const BasicBlock *IncBB : predecessors(LBB)) { 11653 // Check that RHS is available in this block. 11654 if (!dominates(RHS, IncBB)) 11655 return false; 11656 const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB)); 11657 // Make sure L does not refer to a value from a potentially previous 11658 // iteration of a loop. 11659 if (!properlyDominates(L, IncBB)) 11660 return false; 11661 if (!ProvedEasily(L, RHS)) 11662 return false; 11663 } 11664 } 11665 return true; 11666 } 11667 11668 bool ScalarEvolution::isImpliedCondOperandsViaShift(ICmpInst::Predicate Pred, 11669 const SCEV *LHS, 11670 const SCEV *RHS, 11671 const SCEV *FoundLHS, 11672 const SCEV *FoundRHS) { 11673 // We want to imply LHS < RHS from LHS < (RHS >> shiftvalue). First, make 11674 // sure that we are dealing with same LHS. 11675 if (RHS == FoundRHS) { 11676 std::swap(LHS, RHS); 11677 std::swap(FoundLHS, FoundRHS); 11678 Pred = ICmpInst::getSwappedPredicate(Pred); 11679 } 11680 if (LHS != FoundLHS) 11681 return false; 11682 11683 auto *SUFoundRHS = dyn_cast<SCEVUnknown>(FoundRHS); 11684 if (!SUFoundRHS) 11685 return false; 11686 11687 Value *Shiftee, *ShiftValue; 11688 11689 using namespace PatternMatch; 11690 if (match(SUFoundRHS->getValue(), 11691 m_LShr(m_Value(Shiftee), m_Value(ShiftValue)))) { 11692 auto *ShifteeS = getSCEV(Shiftee); 11693 // Prove one of the following: 11694 // LHS <u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <u RHS 11695 // LHS <=u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <=u RHS 11696 // LHS <s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0 11697 // ---> LHS <s RHS 11698 // LHS <=s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0 11699 // ---> LHS <=s RHS 11700 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) 11701 return isKnownPredicate(ICmpInst::ICMP_ULE, ShifteeS, RHS); 11702 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) 11703 if (isKnownNonNegative(ShifteeS)) 11704 return isKnownPredicate(ICmpInst::ICMP_SLE, ShifteeS, RHS); 11705 } 11706 11707 return false; 11708 } 11709 11710 bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, 11711 const SCEV *LHS, const SCEV *RHS, 11712 const SCEV *FoundLHS, 11713 const SCEV *FoundRHS, 11714 const Instruction *CtxI) { 11715 if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS)) 11716 return true; 11717 11718 if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS)) 11719 return true; 11720 11721 if (isImpliedCondOperandsViaShift(Pred, LHS, RHS, FoundLHS, FoundRHS)) 11722 return true; 11723 11724 if (isImpliedCondOperandsViaAddRecStart(Pred, LHS, RHS, FoundLHS, FoundRHS, 11725 CtxI)) 11726 return true; 11727 11728 return isImpliedCondOperandsHelper(Pred, LHS, RHS, 11729 FoundLHS, FoundRHS); 11730 } 11731 11732 /// Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values? 11733 template <typename MinMaxExprType> 11734 static bool IsMinMaxConsistingOf(const SCEV *MaybeMinMaxExpr, 11735 const SCEV *Candidate) { 11736 const MinMaxExprType *MinMaxExpr = dyn_cast<MinMaxExprType>(MaybeMinMaxExpr); 11737 if (!MinMaxExpr) 11738 return false; 11739 11740 return is_contained(MinMaxExpr->operands(), Candidate); 11741 } 11742 11743 static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE, 11744 ICmpInst::Predicate Pred, 11745 const SCEV *LHS, const SCEV *RHS) { 11746 // If both sides are affine addrecs for the same loop, with equal 11747 // steps, and we know the recurrences don't wrap, then we only 11748 // need to check the predicate on the starting values. 11749 11750 if (!ICmpInst::isRelational(Pred)) 11751 return false; 11752 11753 const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS); 11754 if (!LAR) 11755 return false; 11756 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS); 11757 if (!RAR) 11758 return false; 11759 if (LAR->getLoop() != RAR->getLoop()) 11760 return false; 11761 if (!LAR->isAffine() || !RAR->isAffine()) 11762 return false; 11763 11764 if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE)) 11765 return false; 11766 11767 SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ? 11768 SCEV::FlagNSW : SCEV::FlagNUW; 11769 if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW)) 11770 return false; 11771 11772 return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart()); 11773 } 11774 11775 /// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max 11776 /// expression? 11777 static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE, 11778 ICmpInst::Predicate Pred, 11779 const SCEV *LHS, const SCEV *RHS) { 11780 switch (Pred) { 11781 default: 11782 return false; 11783 11784 case ICmpInst::ICMP_SGE: 11785 std::swap(LHS, RHS); 11786 LLVM_FALLTHROUGH; 11787 case ICmpInst::ICMP_SLE: 11788 return 11789 // min(A, ...) <= A 11790 IsMinMaxConsistingOf<SCEVSMinExpr>(LHS, RHS) || 11791 // A <= max(A, ...) 11792 IsMinMaxConsistingOf<SCEVSMaxExpr>(RHS, LHS); 11793 11794 case ICmpInst::ICMP_UGE: 11795 std::swap(LHS, RHS); 11796 LLVM_FALLTHROUGH; 11797 case ICmpInst::ICMP_ULE: 11798 return 11799 // min(A, ...) <= A 11800 // FIXME: what about umin_seq? 11801 IsMinMaxConsistingOf<SCEVUMinExpr>(LHS, RHS) || 11802 // A <= max(A, ...) 11803 IsMinMaxConsistingOf<SCEVUMaxExpr>(RHS, LHS); 11804 } 11805 11806 llvm_unreachable("covered switch fell through?!"); 11807 } 11808 11809 bool ScalarEvolution::isImpliedViaOperations(ICmpInst::Predicate Pred, 11810 const SCEV *LHS, const SCEV *RHS, 11811 const SCEV *FoundLHS, 11812 const SCEV *FoundRHS, 11813 unsigned Depth) { 11814 assert(getTypeSizeInBits(LHS->getType()) == 11815 getTypeSizeInBits(RHS->getType()) && 11816 "LHS and RHS have different sizes?"); 11817 assert(getTypeSizeInBits(FoundLHS->getType()) == 11818 getTypeSizeInBits(FoundRHS->getType()) && 11819 "FoundLHS and FoundRHS have different sizes?"); 11820 // We want to avoid hurting the compile time with analysis of too big trees. 11821 if (Depth > MaxSCEVOperationsImplicationDepth) 11822 return false; 11823 11824 // We only want to work with GT comparison so far. 11825 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_SLT) { 11826 Pred = CmpInst::getSwappedPredicate(Pred); 11827 std::swap(LHS, RHS); 11828 std::swap(FoundLHS, FoundRHS); 11829 } 11830 11831 // For unsigned, try to reduce it to corresponding signed comparison. 11832 if (Pred == ICmpInst::ICMP_UGT) 11833 // We can replace unsigned predicate with its signed counterpart if all 11834 // involved values are non-negative. 11835 // TODO: We could have better support for unsigned. 11836 if (isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) { 11837 // Knowing that both FoundLHS and FoundRHS are non-negative, and knowing 11838 // FoundLHS >u FoundRHS, we also know that FoundLHS >s FoundRHS. Let us 11839 // use this fact to prove that LHS and RHS are non-negative. 11840 const SCEV *MinusOne = getMinusOne(LHS->getType()); 11841 if (isImpliedCondOperands(ICmpInst::ICMP_SGT, LHS, MinusOne, FoundLHS, 11842 FoundRHS) && 11843 isImpliedCondOperands(ICmpInst::ICMP_SGT, RHS, MinusOne, FoundLHS, 11844 FoundRHS)) 11845 Pred = ICmpInst::ICMP_SGT; 11846 } 11847 11848 if (Pred != ICmpInst::ICMP_SGT) 11849 return false; 11850 11851 auto GetOpFromSExt = [&](const SCEV *S) { 11852 if (auto *Ext = dyn_cast<SCEVSignExtendExpr>(S)) 11853 return Ext->getOperand(); 11854 // TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off 11855 // the constant in some cases. 11856 return S; 11857 }; 11858 11859 // Acquire values from extensions. 11860 auto *OrigLHS = LHS; 11861 auto *OrigFoundLHS = FoundLHS; 11862 LHS = GetOpFromSExt(LHS); 11863 FoundLHS = GetOpFromSExt(FoundLHS); 11864 11865 // Is the SGT predicate can be proved trivially or using the found context. 11866 auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) { 11867 return isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGT, S1, S2) || 11868 isImpliedViaOperations(ICmpInst::ICMP_SGT, S1, S2, OrigFoundLHS, 11869 FoundRHS, Depth + 1); 11870 }; 11871 11872 if (auto *LHSAddExpr = dyn_cast<SCEVAddExpr>(LHS)) { 11873 // We want to avoid creation of any new non-constant SCEV. Since we are 11874 // going to compare the operands to RHS, we should be certain that we don't 11875 // need any size extensions for this. So let's decline all cases when the 11876 // sizes of types of LHS and RHS do not match. 11877 // TODO: Maybe try to get RHS from sext to catch more cases? 11878 if (getTypeSizeInBits(LHS->getType()) != getTypeSizeInBits(RHS->getType())) 11879 return false; 11880 11881 // Should not overflow. 11882 if (!LHSAddExpr->hasNoSignedWrap()) 11883 return false; 11884 11885 auto *LL = LHSAddExpr->getOperand(0); 11886 auto *LR = LHSAddExpr->getOperand(1); 11887 auto *MinusOne = getMinusOne(RHS->getType()); 11888 11889 // Checks that S1 >= 0 && S2 > RHS, trivially or using the found context. 11890 auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) { 11891 return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS); 11892 }; 11893 // Try to prove the following rule: 11894 // (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS). 11895 // (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS). 11896 if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL)) 11897 return true; 11898 } else if (auto *LHSUnknownExpr = dyn_cast<SCEVUnknown>(LHS)) { 11899 Value *LL, *LR; 11900 // FIXME: Once we have SDiv implemented, we can get rid of this matching. 11901 11902 using namespace llvm::PatternMatch; 11903 11904 if (match(LHSUnknownExpr->getValue(), m_SDiv(m_Value(LL), m_Value(LR)))) { 11905 // Rules for division. 11906 // We are going to perform some comparisons with Denominator and its 11907 // derivative expressions. In general case, creating a SCEV for it may 11908 // lead to a complex analysis of the entire graph, and in particular it 11909 // can request trip count recalculation for the same loop. This would 11910 // cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid 11911 // this, we only want to create SCEVs that are constants in this section. 11912 // So we bail if Denominator is not a constant. 11913 if (!isa<ConstantInt>(LR)) 11914 return false; 11915 11916 auto *Denominator = cast<SCEVConstant>(getSCEV(LR)); 11917 11918 // We want to make sure that LHS = FoundLHS / Denominator. If it is so, 11919 // then a SCEV for the numerator already exists and matches with FoundLHS. 11920 auto *Numerator = getExistingSCEV(LL); 11921 if (!Numerator || Numerator->getType() != FoundLHS->getType()) 11922 return false; 11923 11924 // Make sure that the numerator matches with FoundLHS and the denominator 11925 // is positive. 11926 if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator)) 11927 return false; 11928 11929 auto *DTy = Denominator->getType(); 11930 auto *FRHSTy = FoundRHS->getType(); 11931 if (DTy->isPointerTy() != FRHSTy->isPointerTy()) 11932 // One of types is a pointer and another one is not. We cannot extend 11933 // them properly to a wider type, so let us just reject this case. 11934 // TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help 11935 // to avoid this check. 11936 return false; 11937 11938 // Given that: 11939 // FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0. 11940 auto *WTy = getWiderType(DTy, FRHSTy); 11941 auto *DenominatorExt = getNoopOrSignExtend(Denominator, WTy); 11942 auto *FoundRHSExt = getNoopOrSignExtend(FoundRHS, WTy); 11943 11944 // Try to prove the following rule: 11945 // (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS). 11946 // For example, given that FoundLHS > 2. It means that FoundLHS is at 11947 // least 3. If we divide it by Denominator < 4, we will have at least 1. 11948 auto *DenomMinusTwo = getMinusSCEV(DenominatorExt, getConstant(WTy, 2)); 11949 if (isKnownNonPositive(RHS) && 11950 IsSGTViaContext(FoundRHSExt, DenomMinusTwo)) 11951 return true; 11952 11953 // Try to prove the following rule: 11954 // (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS). 11955 // For example, given that FoundLHS > -3. Then FoundLHS is at least -2. 11956 // If we divide it by Denominator > 2, then: 11957 // 1. If FoundLHS is negative, then the result is 0. 11958 // 2. If FoundLHS is non-negative, then the result is non-negative. 11959 // Anyways, the result is non-negative. 11960 auto *MinusOne = getMinusOne(WTy); 11961 auto *NegDenomMinusOne = getMinusSCEV(MinusOne, DenominatorExt); 11962 if (isKnownNegative(RHS) && 11963 IsSGTViaContext(FoundRHSExt, NegDenomMinusOne)) 11964 return true; 11965 } 11966 } 11967 11968 // If our expression contained SCEVUnknown Phis, and we split it down and now 11969 // need to prove something for them, try to prove the predicate for every 11970 // possible incoming values of those Phis. 11971 if (isImpliedViaMerge(Pred, OrigLHS, RHS, OrigFoundLHS, FoundRHS, Depth + 1)) 11972 return true; 11973 11974 return false; 11975 } 11976 11977 static bool isKnownPredicateExtendIdiom(ICmpInst::Predicate Pred, 11978 const SCEV *LHS, const SCEV *RHS) { 11979 // zext x u<= sext x, sext x s<= zext x 11980 switch (Pred) { 11981 case ICmpInst::ICMP_SGE: 11982 std::swap(LHS, RHS); 11983 LLVM_FALLTHROUGH; 11984 case ICmpInst::ICMP_SLE: { 11985 // If operand >=s 0 then ZExt == SExt. If operand <s 0 then SExt <s ZExt. 11986 const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(LHS); 11987 const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(RHS); 11988 if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand()) 11989 return true; 11990 break; 11991 } 11992 case ICmpInst::ICMP_UGE: 11993 std::swap(LHS, RHS); 11994 LLVM_FALLTHROUGH; 11995 case ICmpInst::ICMP_ULE: { 11996 // If operand >=s 0 then ZExt == SExt. If operand <s 0 then ZExt <u SExt. 11997 const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS); 11998 const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(RHS); 11999 if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand()) 12000 return true; 12001 break; 12002 } 12003 default: 12004 break; 12005 }; 12006 return false; 12007 } 12008 12009 bool 12010 ScalarEvolution::isKnownViaNonRecursiveReasoning(ICmpInst::Predicate Pred, 12011 const SCEV *LHS, const SCEV *RHS) { 12012 return isKnownPredicateExtendIdiom(Pred, LHS, RHS) || 12013 isKnownPredicateViaConstantRanges(Pred, LHS, RHS) || 12014 IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) || 12015 IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) || 12016 isKnownPredicateViaNoOverflow(Pred, LHS, RHS); 12017 } 12018 12019 bool 12020 ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, 12021 const SCEV *LHS, const SCEV *RHS, 12022 const SCEV *FoundLHS, 12023 const SCEV *FoundRHS) { 12024 switch (Pred) { 12025 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); 12026 case ICmpInst::ICMP_EQ: 12027 case ICmpInst::ICMP_NE: 12028 if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) 12029 return true; 12030 break; 12031 case ICmpInst::ICMP_SLT: 12032 case ICmpInst::ICMP_SLE: 12033 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) && 12034 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS)) 12035 return true; 12036 break; 12037 case ICmpInst::ICMP_SGT: 12038 case ICmpInst::ICMP_SGE: 12039 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) && 12040 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS)) 12041 return true; 12042 break; 12043 case ICmpInst::ICMP_ULT: 12044 case ICmpInst::ICMP_ULE: 12045 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) && 12046 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS)) 12047 return true; 12048 break; 12049 case ICmpInst::ICMP_UGT: 12050 case ICmpInst::ICMP_UGE: 12051 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) && 12052 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS)) 12053 return true; 12054 break; 12055 } 12056 12057 // Maybe it can be proved via operations? 12058 if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS)) 12059 return true; 12060 12061 return false; 12062 } 12063 12064 bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred, 12065 const SCEV *LHS, 12066 const SCEV *RHS, 12067 const SCEV *FoundLHS, 12068 const SCEV *FoundRHS) { 12069 if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS)) 12070 // The restriction on `FoundRHS` be lifted easily -- it exists only to 12071 // reduce the compile time impact of this optimization. 12072 return false; 12073 12074 Optional<APInt> Addend = computeConstantDifference(LHS, FoundLHS); 12075 if (!Addend) 12076 return false; 12077 12078 const APInt &ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getAPInt(); 12079 12080 // `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the 12081 // antecedent "`FoundLHS` `Pred` `FoundRHS`". 12082 ConstantRange FoundLHSRange = 12083 ConstantRange::makeExactICmpRegion(Pred, ConstFoundRHS); 12084 12085 // Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`: 12086 ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend)); 12087 12088 // We can also compute the range of values for `LHS` that satisfy the 12089 // consequent, "`LHS` `Pred` `RHS`": 12090 const APInt &ConstRHS = cast<SCEVConstant>(RHS)->getAPInt(); 12091 // The antecedent implies the consequent if every value of `LHS` that 12092 // satisfies the antecedent also satisfies the consequent. 12093 return LHSRange.icmp(Pred, ConstRHS); 12094 } 12095 12096 bool ScalarEvolution::canIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, 12097 bool IsSigned) { 12098 assert(isKnownPositive(Stride) && "Positive stride expected!"); 12099 12100 unsigned BitWidth = getTypeSizeInBits(RHS->getType()); 12101 const SCEV *One = getOne(Stride->getType()); 12102 12103 if (IsSigned) { 12104 APInt MaxRHS = getSignedRangeMax(RHS); 12105 APInt MaxValue = APInt::getSignedMaxValue(BitWidth); 12106 APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One)); 12107 12108 // SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow! 12109 return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS); 12110 } 12111 12112 APInt MaxRHS = getUnsignedRangeMax(RHS); 12113 APInt MaxValue = APInt::getMaxValue(BitWidth); 12114 APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One)); 12115 12116 // UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow! 12117 return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS); 12118 } 12119 12120 bool ScalarEvolution::canIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, 12121 bool IsSigned) { 12122 12123 unsigned BitWidth = getTypeSizeInBits(RHS->getType()); 12124 const SCEV *One = getOne(Stride->getType()); 12125 12126 if (IsSigned) { 12127 APInt MinRHS = getSignedRangeMin(RHS); 12128 APInt MinValue = APInt::getSignedMinValue(BitWidth); 12129 APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One)); 12130 12131 // SMinRHS - SMaxStrideMinusOne < SMinValue => overflow! 12132 return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS); 12133 } 12134 12135 APInt MinRHS = getUnsignedRangeMin(RHS); 12136 APInt MinValue = APInt::getMinValue(BitWidth); 12137 APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One)); 12138 12139 // UMinRHS - UMaxStrideMinusOne < UMinValue => overflow! 12140 return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS); 12141 } 12142 12143 const SCEV *ScalarEvolution::getUDivCeilSCEV(const SCEV *N, const SCEV *D) { 12144 // umin(N, 1) + floor((N - umin(N, 1)) / D) 12145 // This is equivalent to "1 + floor((N - 1) / D)" for N != 0. The umin 12146 // expression fixes the case of N=0. 12147 const SCEV *MinNOne = getUMinExpr(N, getOne(N->getType())); 12148 const SCEV *NMinusOne = getMinusSCEV(N, MinNOne); 12149 return getAddExpr(MinNOne, getUDivExpr(NMinusOne, D)); 12150 } 12151 12152 const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start, 12153 const SCEV *Stride, 12154 const SCEV *End, 12155 unsigned BitWidth, 12156 bool IsSigned) { 12157 // The logic in this function assumes we can represent a positive stride. 12158 // If we can't, the backedge-taken count must be zero. 12159 if (IsSigned && BitWidth == 1) 12160 return getZero(Stride->getType()); 12161 12162 // This code has only been closely audited for negative strides in the 12163 // unsigned comparison case, it may be correct for signed comparison, but 12164 // that needs to be established. 12165 assert((!IsSigned || !isKnownNonPositive(Stride)) && 12166 "Stride is expected strictly positive for signed case!"); 12167 12168 // Calculate the maximum backedge count based on the range of values 12169 // permitted by Start, End, and Stride. 12170 APInt MinStart = 12171 IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start); 12172 12173 APInt MinStride = 12174 IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride); 12175 12176 // We assume either the stride is positive, or the backedge-taken count 12177 // is zero. So force StrideForMaxBECount to be at least one. 12178 APInt One(BitWidth, 1); 12179 APInt StrideForMaxBECount = IsSigned ? APIntOps::smax(One, MinStride) 12180 : APIntOps::umax(One, MinStride); 12181 12182 APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth) 12183 : APInt::getMaxValue(BitWidth); 12184 APInt Limit = MaxValue - (StrideForMaxBECount - 1); 12185 12186 // Although End can be a MAX expression we estimate MaxEnd considering only 12187 // the case End = RHS of the loop termination condition. This is safe because 12188 // in the other case (End - Start) is zero, leading to a zero maximum backedge 12189 // taken count. 12190 APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit) 12191 : APIntOps::umin(getUnsignedRangeMax(End), Limit); 12192 12193 // MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride) 12194 MaxEnd = IsSigned ? APIntOps::smax(MaxEnd, MinStart) 12195 : APIntOps::umax(MaxEnd, MinStart); 12196 12197 return getUDivCeilSCEV(getConstant(MaxEnd - MinStart) /* Delta */, 12198 getConstant(StrideForMaxBECount) /* Step */); 12199 } 12200 12201 ScalarEvolution::ExitLimit 12202 ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS, 12203 const Loop *L, bool IsSigned, 12204 bool ControlsExit, bool AllowPredicates) { 12205 SmallPtrSet<const SCEVPredicate *, 4> Predicates; 12206 12207 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS); 12208 bool PredicatedIV = false; 12209 12210 auto canAssumeNoSelfWrap = [&](const SCEVAddRecExpr *AR) { 12211 // Can we prove this loop *must* be UB if overflow of IV occurs? 12212 // Reasoning goes as follows: 12213 // * Suppose the IV did self wrap. 12214 // * If Stride evenly divides the iteration space, then once wrap 12215 // occurs, the loop must revisit the same values. 12216 // * We know that RHS is invariant, and that none of those values 12217 // caused this exit to be taken previously. Thus, this exit is 12218 // dynamically dead. 12219 // * If this is the sole exit, then a dead exit implies the loop 12220 // must be infinite if there are no abnormal exits. 12221 // * If the loop were infinite, then it must either not be mustprogress 12222 // or have side effects. Otherwise, it must be UB. 12223 // * It can't (by assumption), be UB so we have contradicted our 12224 // premise and can conclude the IV did not in fact self-wrap. 12225 if (!isLoopInvariant(RHS, L)) 12226 return false; 12227 12228 auto *StrideC = dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)); 12229 if (!StrideC || !StrideC->getAPInt().isPowerOf2()) 12230 return false; 12231 12232 if (!ControlsExit || !loopHasNoAbnormalExits(L)) 12233 return false; 12234 12235 return loopIsFiniteByAssumption(L); 12236 }; 12237 12238 if (!IV) { 12239 if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS)) { 12240 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(ZExt->getOperand()); 12241 if (AR && AR->getLoop() == L && AR->isAffine()) { 12242 auto canProveNUW = [&]() { 12243 if (!isLoopInvariant(RHS, L)) 12244 return false; 12245 12246 if (!isKnownNonZero(AR->getStepRecurrence(*this))) 12247 // We need the sequence defined by AR to strictly increase in the 12248 // unsigned integer domain for the logic below to hold. 12249 return false; 12250 12251 const unsigned InnerBitWidth = getTypeSizeInBits(AR->getType()); 12252 const unsigned OuterBitWidth = getTypeSizeInBits(RHS->getType()); 12253 // If RHS <=u Limit, then there must exist a value V in the sequence 12254 // defined by AR (e.g. {Start,+,Step}) such that V >u RHS, and 12255 // V <=u UINT_MAX. Thus, we must exit the loop before unsigned 12256 // overflow occurs. This limit also implies that a signed comparison 12257 // (in the wide bitwidth) is equivalent to an unsigned comparison as 12258 // the high bits on both sides must be zero. 12259 APInt StrideMax = getUnsignedRangeMax(AR->getStepRecurrence(*this)); 12260 APInt Limit = APInt::getMaxValue(InnerBitWidth) - (StrideMax - 1); 12261 Limit = Limit.zext(OuterBitWidth); 12262 return getUnsignedRangeMax(applyLoopGuards(RHS, L)).ule(Limit); 12263 }; 12264 auto Flags = AR->getNoWrapFlags(); 12265 if (!hasFlags(Flags, SCEV::FlagNUW) && canProveNUW()) 12266 Flags = setFlags(Flags, SCEV::FlagNUW); 12267 12268 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags); 12269 if (AR->hasNoUnsignedWrap()) { 12270 // Emulate what getZeroExtendExpr would have done during construction 12271 // if we'd been able to infer the fact just above at that time. 12272 const SCEV *Step = AR->getStepRecurrence(*this); 12273 Type *Ty = ZExt->getType(); 12274 auto *S = getAddRecExpr( 12275 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, 0), 12276 getZeroExtendExpr(Step, Ty, 0), L, AR->getNoWrapFlags()); 12277 IV = dyn_cast<SCEVAddRecExpr>(S); 12278 } 12279 } 12280 } 12281 } 12282 12283 12284 if (!IV && AllowPredicates) { 12285 // Try to make this an AddRec using runtime tests, in the first X 12286 // iterations of this loop, where X is the SCEV expression found by the 12287 // algorithm below. 12288 IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates); 12289 PredicatedIV = true; 12290 } 12291 12292 // Avoid weird loops 12293 if (!IV || IV->getLoop() != L || !IV->isAffine()) 12294 return getCouldNotCompute(); 12295 12296 // A precondition of this method is that the condition being analyzed 12297 // reaches an exiting branch which dominates the latch. Given that, we can 12298 // assume that an increment which violates the nowrap specification and 12299 // produces poison must cause undefined behavior when the resulting poison 12300 // value is branched upon and thus we can conclude that the backedge is 12301 // taken no more often than would be required to produce that poison value. 12302 // Note that a well defined loop can exit on the iteration which violates 12303 // the nowrap specification if there is another exit (either explicit or 12304 // implicit/exceptional) which causes the loop to execute before the 12305 // exiting instruction we're analyzing would trigger UB. 12306 auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW; 12307 bool NoWrap = ControlsExit && IV->getNoWrapFlags(WrapType); 12308 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; 12309 12310 const SCEV *Stride = IV->getStepRecurrence(*this); 12311 12312 bool PositiveStride = isKnownPositive(Stride); 12313 12314 // Avoid negative or zero stride values. 12315 if (!PositiveStride) { 12316 // We can compute the correct backedge taken count for loops with unknown 12317 // strides if we can prove that the loop is not an infinite loop with side 12318 // effects. Here's the loop structure we are trying to handle - 12319 // 12320 // i = start 12321 // do { 12322 // A[i] = i; 12323 // i += s; 12324 // } while (i < end); 12325 // 12326 // The backedge taken count for such loops is evaluated as - 12327 // (max(end, start + stride) - start - 1) /u stride 12328 // 12329 // The additional preconditions that we need to check to prove correctness 12330 // of the above formula is as follows - 12331 // 12332 // a) IV is either nuw or nsw depending upon signedness (indicated by the 12333 // NoWrap flag). 12334 // b) the loop is guaranteed to be finite (e.g. is mustprogress and has 12335 // no side effects within the loop) 12336 // c) loop has a single static exit (with no abnormal exits) 12337 // 12338 // Precondition a) implies that if the stride is negative, this is a single 12339 // trip loop. The backedge taken count formula reduces to zero in this case. 12340 // 12341 // Precondition b) and c) combine to imply that if rhs is invariant in L, 12342 // then a zero stride means the backedge can't be taken without executing 12343 // undefined behavior. 12344 // 12345 // The positive stride case is the same as isKnownPositive(Stride) returning 12346 // true (original behavior of the function). 12347 // 12348 if (PredicatedIV || !NoWrap || !loopIsFiniteByAssumption(L) || 12349 !loopHasNoAbnormalExits(L)) 12350 return getCouldNotCompute(); 12351 12352 // This bailout is protecting the logic in computeMaxBECountForLT which 12353 // has not yet been sufficiently auditted or tested with negative strides. 12354 // We used to filter out all known-non-positive cases here, we're in the 12355 // process of being less restrictive bit by bit. 12356 if (IsSigned && isKnownNonPositive(Stride)) 12357 return getCouldNotCompute(); 12358 12359 if (!isKnownNonZero(Stride)) { 12360 // If we have a step of zero, and RHS isn't invariant in L, we don't know 12361 // if it might eventually be greater than start and if so, on which 12362 // iteration. We can't even produce a useful upper bound. 12363 if (!isLoopInvariant(RHS, L)) 12364 return getCouldNotCompute(); 12365 12366 // We allow a potentially zero stride, but we need to divide by stride 12367 // below. Since the loop can't be infinite and this check must control 12368 // the sole exit, we can infer the exit must be taken on the first 12369 // iteration (e.g. backedge count = 0) if the stride is zero. Given that, 12370 // we know the numerator in the divides below must be zero, so we can 12371 // pick an arbitrary non-zero value for the denominator (e.g. stride) 12372 // and produce the right result. 12373 // FIXME: Handle the case where Stride is poison? 12374 auto wouldZeroStrideBeUB = [&]() { 12375 // Proof by contradiction. Suppose the stride were zero. If we can 12376 // prove that the backedge *is* taken on the first iteration, then since 12377 // we know this condition controls the sole exit, we must have an 12378 // infinite loop. We can't have a (well defined) infinite loop per 12379 // check just above. 12380 // Note: The (Start - Stride) term is used to get the start' term from 12381 // (start' + stride,+,stride). Remember that we only care about the 12382 // result of this expression when stride == 0 at runtime. 12383 auto *StartIfZero = getMinusSCEV(IV->getStart(), Stride); 12384 return isLoopEntryGuardedByCond(L, Cond, StartIfZero, RHS); 12385 }; 12386 if (!wouldZeroStrideBeUB()) { 12387 Stride = getUMaxExpr(Stride, getOne(Stride->getType())); 12388 } 12389 } 12390 } else if (!Stride->isOne() && !NoWrap) { 12391 auto isUBOnWrap = [&]() { 12392 // From no-self-wrap, we need to then prove no-(un)signed-wrap. This 12393 // follows trivially from the fact that every (un)signed-wrapped, but 12394 // not self-wrapped value must be LT than the last value before 12395 // (un)signed wrap. Since we know that last value didn't exit, nor 12396 // will any smaller one. 12397 return canAssumeNoSelfWrap(IV); 12398 }; 12399 12400 // Avoid proven overflow cases: this will ensure that the backedge taken 12401 // count will not generate any unsigned overflow. Relaxed no-overflow 12402 // conditions exploit NoWrapFlags, allowing to optimize in presence of 12403 // undefined behaviors like the case of C language. 12404 if (canIVOverflowOnLT(RHS, Stride, IsSigned) && !isUBOnWrap()) 12405 return getCouldNotCompute(); 12406 } 12407 12408 // On all paths just preceeding, we established the following invariant: 12409 // IV can be assumed not to overflow up to and including the exiting 12410 // iteration. We proved this in one of two ways: 12411 // 1) We can show overflow doesn't occur before the exiting iteration 12412 // 1a) canIVOverflowOnLT, and b) step of one 12413 // 2) We can show that if overflow occurs, the loop must execute UB 12414 // before any possible exit. 12415 // Note that we have not yet proved RHS invariant (in general). 12416 12417 const SCEV *Start = IV->getStart(); 12418 12419 // Preserve pointer-typed Start/RHS to pass to isLoopEntryGuardedByCond. 12420 // If we convert to integers, isLoopEntryGuardedByCond will miss some cases. 12421 // Use integer-typed versions for actual computation; we can't subtract 12422 // pointers in general. 12423 const SCEV *OrigStart = Start; 12424 const SCEV *OrigRHS = RHS; 12425 if (Start->getType()->isPointerTy()) { 12426 Start = getLosslessPtrToIntExpr(Start); 12427 if (isa<SCEVCouldNotCompute>(Start)) 12428 return Start; 12429 } 12430 if (RHS->getType()->isPointerTy()) { 12431 RHS = getLosslessPtrToIntExpr(RHS); 12432 if (isa<SCEVCouldNotCompute>(RHS)) 12433 return RHS; 12434 } 12435 12436 // When the RHS is not invariant, we do not know the end bound of the loop and 12437 // cannot calculate the ExactBECount needed by ExitLimit. However, we can 12438 // calculate the MaxBECount, given the start, stride and max value for the end 12439 // bound of the loop (RHS), and the fact that IV does not overflow (which is 12440 // checked above). 12441 if (!isLoopInvariant(RHS, L)) { 12442 const SCEV *MaxBECount = computeMaxBECountForLT( 12443 Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned); 12444 return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount, 12445 false /*MaxOrZero*/, Predicates); 12446 } 12447 12448 // We use the expression (max(End,Start)-Start)/Stride to describe the 12449 // backedge count, as if the backedge is taken at least once max(End,Start) 12450 // is End and so the result is as above, and if not max(End,Start) is Start 12451 // so we get a backedge count of zero. 12452 const SCEV *BECount = nullptr; 12453 auto *OrigStartMinusStride = getMinusSCEV(OrigStart, Stride); 12454 assert(isAvailableAtLoopEntry(OrigStartMinusStride, L) && "Must be!"); 12455 assert(isAvailableAtLoopEntry(OrigStart, L) && "Must be!"); 12456 assert(isAvailableAtLoopEntry(OrigRHS, L) && "Must be!"); 12457 // Can we prove (max(RHS,Start) > Start - Stride? 12458 if (isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigStart) && 12459 isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigRHS)) { 12460 // In this case, we can use a refined formula for computing backedge taken 12461 // count. The general formula remains: 12462 // "End-Start /uceiling Stride" where "End = max(RHS,Start)" 12463 // We want to use the alternate formula: 12464 // "((End - 1) - (Start - Stride)) /u Stride" 12465 // Let's do a quick case analysis to show these are equivalent under 12466 // our precondition that max(RHS,Start) > Start - Stride. 12467 // * For RHS <= Start, the backedge-taken count must be zero. 12468 // "((End - 1) - (Start - Stride)) /u Stride" reduces to 12469 // "((Start - 1) - (Start - Stride)) /u Stride" which simplies to 12470 // "Stride - 1 /u Stride" which is indeed zero for all non-zero values 12471 // of Stride. For 0 stride, we've use umin(1,Stride) above, reducing 12472 // this to the stride of 1 case. 12473 // * For RHS >= Start, the backedge count must be "RHS-Start /uceil Stride". 12474 // "((End - 1) - (Start - Stride)) /u Stride" reduces to 12475 // "((RHS - 1) - (Start - Stride)) /u Stride" reassociates to 12476 // "((RHS - (Start - Stride) - 1) /u Stride". 12477 // Our preconditions trivially imply no overflow in that form. 12478 const SCEV *MinusOne = getMinusOne(Stride->getType()); 12479 const SCEV *Numerator = 12480 getMinusSCEV(getAddExpr(RHS, MinusOne), getMinusSCEV(Start, Stride)); 12481 BECount = getUDivExpr(Numerator, Stride); 12482 } 12483 12484 const SCEV *BECountIfBackedgeTaken = nullptr; 12485 if (!BECount) { 12486 auto canProveRHSGreaterThanEqualStart = [&]() { 12487 auto CondGE = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; 12488 if (isLoopEntryGuardedByCond(L, CondGE, OrigRHS, OrigStart)) 12489 return true; 12490 12491 // (RHS > Start - 1) implies RHS >= Start. 12492 // * "RHS >= Start" is trivially equivalent to "RHS > Start - 1" if 12493 // "Start - 1" doesn't overflow. 12494 // * For signed comparison, if Start - 1 does overflow, it's equal 12495 // to INT_MAX, and "RHS >s INT_MAX" is trivially false. 12496 // * For unsigned comparison, if Start - 1 does overflow, it's equal 12497 // to UINT_MAX, and "RHS >u UINT_MAX" is trivially false. 12498 // 12499 // FIXME: Should isLoopEntryGuardedByCond do this for us? 12500 auto CondGT = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 12501 auto *StartMinusOne = getAddExpr(OrigStart, 12502 getMinusOne(OrigStart->getType())); 12503 return isLoopEntryGuardedByCond(L, CondGT, OrigRHS, StartMinusOne); 12504 }; 12505 12506 // If we know that RHS >= Start in the context of loop, then we know that 12507 // max(RHS, Start) = RHS at this point. 12508 const SCEV *End; 12509 if (canProveRHSGreaterThanEqualStart()) { 12510 End = RHS; 12511 } else { 12512 // If RHS < Start, the backedge will be taken zero times. So in 12513 // general, we can write the backedge-taken count as: 12514 // 12515 // RHS >= Start ? ceil(RHS - Start) / Stride : 0 12516 // 12517 // We convert it to the following to make it more convenient for SCEV: 12518 // 12519 // ceil(max(RHS, Start) - Start) / Stride 12520 End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start); 12521 12522 // See what would happen if we assume the backedge is taken. This is 12523 // used to compute MaxBECount. 12524 BECountIfBackedgeTaken = getUDivCeilSCEV(getMinusSCEV(RHS, Start), Stride); 12525 } 12526 12527 // At this point, we know: 12528 // 12529 // 1. If IsSigned, Start <=s End; otherwise, Start <=u End 12530 // 2. The index variable doesn't overflow. 12531 // 12532 // Therefore, we know N exists such that 12533 // (Start + Stride * N) >= End, and computing "(Start + Stride * N)" 12534 // doesn't overflow. 12535 // 12536 // Using this information, try to prove whether the addition in 12537 // "(Start - End) + (Stride - 1)" has unsigned overflow. 12538 const SCEV *One = getOne(Stride->getType()); 12539 bool MayAddOverflow = [&] { 12540 if (auto *StrideC = dyn_cast<SCEVConstant>(Stride)) { 12541 if (StrideC->getAPInt().isPowerOf2()) { 12542 // Suppose Stride is a power of two, and Start/End are unsigned 12543 // integers. Let UMAX be the largest representable unsigned 12544 // integer. 12545 // 12546 // By the preconditions of this function, we know 12547 // "(Start + Stride * N) >= End", and this doesn't overflow. 12548 // As a formula: 12549 // 12550 // End <= (Start + Stride * N) <= UMAX 12551 // 12552 // Subtracting Start from all the terms: 12553 // 12554 // End - Start <= Stride * N <= UMAX - Start 12555 // 12556 // Since Start is unsigned, UMAX - Start <= UMAX. Therefore: 12557 // 12558 // End - Start <= Stride * N <= UMAX 12559 // 12560 // Stride * N is a multiple of Stride. Therefore, 12561 // 12562 // End - Start <= Stride * N <= UMAX - (UMAX mod Stride) 12563 // 12564 // Since Stride is a power of two, UMAX + 1 is divisible by Stride. 12565 // Therefore, UMAX mod Stride == Stride - 1. So we can write: 12566 // 12567 // End - Start <= Stride * N <= UMAX - Stride - 1 12568 // 12569 // Dropping the middle term: 12570 // 12571 // End - Start <= UMAX - Stride - 1 12572 // 12573 // Adding Stride - 1 to both sides: 12574 // 12575 // (End - Start) + (Stride - 1) <= UMAX 12576 // 12577 // In other words, the addition doesn't have unsigned overflow. 12578 // 12579 // A similar proof works if we treat Start/End as signed values. 12580 // Just rewrite steps before "End - Start <= Stride * N <= UMAX" to 12581 // use signed max instead of unsigned max. Note that we're trying 12582 // to prove a lack of unsigned overflow in either case. 12583 return false; 12584 } 12585 } 12586 if (Start == Stride || Start == getMinusSCEV(Stride, One)) { 12587 // If Start is equal to Stride, (End - Start) + (Stride - 1) == End - 1. 12588 // If !IsSigned, 0 <u Stride == Start <=u End; so 0 <u End - 1 <u End. 12589 // If IsSigned, 0 <s Stride == Start <=s End; so 0 <s End - 1 <s End. 12590 // 12591 // If Start is equal to Stride - 1, (End - Start) + Stride - 1 == End. 12592 return false; 12593 } 12594 return true; 12595 }(); 12596 12597 const SCEV *Delta = getMinusSCEV(End, Start); 12598 if (!MayAddOverflow) { 12599 // floor((D + (S - 1)) / S) 12600 // We prefer this formulation if it's legal because it's fewer operations. 12601 BECount = 12602 getUDivExpr(getAddExpr(Delta, getMinusSCEV(Stride, One)), Stride); 12603 } else { 12604 BECount = getUDivCeilSCEV(Delta, Stride); 12605 } 12606 } 12607 12608 const SCEV *MaxBECount; 12609 bool MaxOrZero = false; 12610 if (isa<SCEVConstant>(BECount)) { 12611 MaxBECount = BECount; 12612 } else if (BECountIfBackedgeTaken && 12613 isa<SCEVConstant>(BECountIfBackedgeTaken)) { 12614 // If we know exactly how many times the backedge will be taken if it's 12615 // taken at least once, then the backedge count will either be that or 12616 // zero. 12617 MaxBECount = BECountIfBackedgeTaken; 12618 MaxOrZero = true; 12619 } else { 12620 MaxBECount = computeMaxBECountForLT( 12621 Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned); 12622 } 12623 12624 if (isa<SCEVCouldNotCompute>(MaxBECount) && 12625 !isa<SCEVCouldNotCompute>(BECount)) 12626 MaxBECount = getConstant(getUnsignedRangeMax(BECount)); 12627 12628 return ExitLimit(BECount, MaxBECount, MaxOrZero, Predicates); 12629 } 12630 12631 ScalarEvolution::ExitLimit 12632 ScalarEvolution::howManyGreaterThans(const SCEV *LHS, const SCEV *RHS, 12633 const Loop *L, bool IsSigned, 12634 bool ControlsExit, bool AllowPredicates) { 12635 SmallPtrSet<const SCEVPredicate *, 4> Predicates; 12636 // We handle only IV > Invariant 12637 if (!isLoopInvariant(RHS, L)) 12638 return getCouldNotCompute(); 12639 12640 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS); 12641 if (!IV && AllowPredicates) 12642 // Try to make this an AddRec using runtime tests, in the first X 12643 // iterations of this loop, where X is the SCEV expression found by the 12644 // algorithm below. 12645 IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates); 12646 12647 // Avoid weird loops 12648 if (!IV || IV->getLoop() != L || !IV->isAffine()) 12649 return getCouldNotCompute(); 12650 12651 auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW; 12652 bool NoWrap = ControlsExit && IV->getNoWrapFlags(WrapType); 12653 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; 12654 12655 const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this)); 12656 12657 // Avoid negative or zero stride values 12658 if (!isKnownPositive(Stride)) 12659 return getCouldNotCompute(); 12660 12661 // Avoid proven overflow cases: this will ensure that the backedge taken count 12662 // will not generate any unsigned overflow. Relaxed no-overflow conditions 12663 // exploit NoWrapFlags, allowing to optimize in presence of undefined 12664 // behaviors like the case of C language. 12665 if (!Stride->isOne() && !NoWrap) 12666 if (canIVOverflowOnGT(RHS, Stride, IsSigned)) 12667 return getCouldNotCompute(); 12668 12669 const SCEV *Start = IV->getStart(); 12670 const SCEV *End = RHS; 12671 if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS)) { 12672 // If we know that Start >= RHS in the context of loop, then we know that 12673 // min(RHS, Start) = RHS at this point. 12674 if (isLoopEntryGuardedByCond( 12675 L, IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, Start, RHS)) 12676 End = RHS; 12677 else 12678 End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start); 12679 } 12680 12681 if (Start->getType()->isPointerTy()) { 12682 Start = getLosslessPtrToIntExpr(Start); 12683 if (isa<SCEVCouldNotCompute>(Start)) 12684 return Start; 12685 } 12686 if (End->getType()->isPointerTy()) { 12687 End = getLosslessPtrToIntExpr(End); 12688 if (isa<SCEVCouldNotCompute>(End)) 12689 return End; 12690 } 12691 12692 // Compute ((Start - End) + (Stride - 1)) / Stride. 12693 // FIXME: This can overflow. Holding off on fixing this for now; 12694 // howManyGreaterThans will hopefully be gone soon. 12695 const SCEV *One = getOne(Stride->getType()); 12696 const SCEV *BECount = getUDivExpr( 12697 getAddExpr(getMinusSCEV(Start, End), getMinusSCEV(Stride, One)), Stride); 12698 12699 APInt MaxStart = IsSigned ? getSignedRangeMax(Start) 12700 : getUnsignedRangeMax(Start); 12701 12702 APInt MinStride = IsSigned ? getSignedRangeMin(Stride) 12703 : getUnsignedRangeMin(Stride); 12704 12705 unsigned BitWidth = getTypeSizeInBits(LHS->getType()); 12706 APInt Limit = IsSigned ? APInt::getSignedMinValue(BitWidth) + (MinStride - 1) 12707 : APInt::getMinValue(BitWidth) + (MinStride - 1); 12708 12709 // Although End can be a MIN expression we estimate MinEnd considering only 12710 // the case End = RHS. This is safe because in the other case (Start - End) 12711 // is zero, leading to a zero maximum backedge taken count. 12712 APInt MinEnd = 12713 IsSigned ? APIntOps::smax(getSignedRangeMin(RHS), Limit) 12714 : APIntOps::umax(getUnsignedRangeMin(RHS), Limit); 12715 12716 const SCEV *MaxBECount = isa<SCEVConstant>(BECount) 12717 ? BECount 12718 : getUDivCeilSCEV(getConstant(MaxStart - MinEnd), 12719 getConstant(MinStride)); 12720 12721 if (isa<SCEVCouldNotCompute>(MaxBECount)) 12722 MaxBECount = BECount; 12723 12724 return ExitLimit(BECount, MaxBECount, false, Predicates); 12725 } 12726 12727 const SCEV *SCEVAddRecExpr::getNumIterationsInRange(const ConstantRange &Range, 12728 ScalarEvolution &SE) const { 12729 if (Range.isFullSet()) // Infinite loop. 12730 return SE.getCouldNotCompute(); 12731 12732 // If the start is a non-zero constant, shift the range to simplify things. 12733 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) 12734 if (!SC->getValue()->isZero()) { 12735 SmallVector<const SCEV *, 4> Operands(operands()); 12736 Operands[0] = SE.getZero(SC->getType()); 12737 const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(), 12738 getNoWrapFlags(FlagNW)); 12739 if (const auto *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted)) 12740 return ShiftedAddRec->getNumIterationsInRange( 12741 Range.subtract(SC->getAPInt()), SE); 12742 // This is strange and shouldn't happen. 12743 return SE.getCouldNotCompute(); 12744 } 12745 12746 // The only time we can solve this is when we have all constant indices. 12747 // Otherwise, we cannot determine the overflow conditions. 12748 if (any_of(operands(), [](const SCEV *Op) { return !isa<SCEVConstant>(Op); })) 12749 return SE.getCouldNotCompute(); 12750 12751 // Okay at this point we know that all elements of the chrec are constants and 12752 // that the start element is zero. 12753 12754 // First check to see if the range contains zero. If not, the first 12755 // iteration exits. 12756 unsigned BitWidth = SE.getTypeSizeInBits(getType()); 12757 if (!Range.contains(APInt(BitWidth, 0))) 12758 return SE.getZero(getType()); 12759 12760 if (isAffine()) { 12761 // If this is an affine expression then we have this situation: 12762 // Solve {0,+,A} in Range === Ax in Range 12763 12764 // We know that zero is in the range. If A is positive then we know that 12765 // the upper value of the range must be the first possible exit value. 12766 // If A is negative then the lower of the range is the last possible loop 12767 // value. Also note that we already checked for a full range. 12768 APInt A = cast<SCEVConstant>(getOperand(1))->getAPInt(); 12769 APInt End = A.sge(1) ? (Range.getUpper() - 1) : Range.getLower(); 12770 12771 // The exit value should be (End+A)/A. 12772 APInt ExitVal = (End + A).udiv(A); 12773 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); 12774 12775 // Evaluate at the exit value. If we really did fall out of the valid 12776 // range, then we computed our trip count, otherwise wrap around or other 12777 // things must have happened. 12778 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); 12779 if (Range.contains(Val->getValue())) 12780 return SE.getCouldNotCompute(); // Something strange happened 12781 12782 // Ensure that the previous value is in the range. 12783 assert(Range.contains( 12784 EvaluateConstantChrecAtConstant(this, 12785 ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) && 12786 "Linear scev computation is off in a bad way!"); 12787 return SE.getConstant(ExitValue); 12788 } 12789 12790 if (isQuadratic()) { 12791 if (auto S = SolveQuadraticAddRecRange(this, Range, SE)) 12792 return SE.getConstant(S.getValue()); 12793 } 12794 12795 return SE.getCouldNotCompute(); 12796 } 12797 12798 const SCEVAddRecExpr * 12799 SCEVAddRecExpr::getPostIncExpr(ScalarEvolution &SE) const { 12800 assert(getNumOperands() > 1 && "AddRec with zero step?"); 12801 // There is a temptation to just call getAddExpr(this, getStepRecurrence(SE)), 12802 // but in this case we cannot guarantee that the value returned will be an 12803 // AddRec because SCEV does not have a fixed point where it stops 12804 // simplification: it is legal to return ({rec1} + {rec2}). For example, it 12805 // may happen if we reach arithmetic depth limit while simplifying. So we 12806 // construct the returned value explicitly. 12807 SmallVector<const SCEV *, 3> Ops; 12808 // If this is {A,+,B,+,C,...,+,N}, then its step is {B,+,C,+,...,+,N}, and 12809 // (this + Step) is {A+B,+,B+C,+...,+,N}. 12810 for (unsigned i = 0, e = getNumOperands() - 1; i < e; ++i) 12811 Ops.push_back(SE.getAddExpr(getOperand(i), getOperand(i + 1))); 12812 // We know that the last operand is not a constant zero (otherwise it would 12813 // have been popped out earlier). This guarantees us that if the result has 12814 // the same last operand, then it will also not be popped out, meaning that 12815 // the returned value will be an AddRec. 12816 const SCEV *Last = getOperand(getNumOperands() - 1); 12817 assert(!Last->isZero() && "Recurrency with zero step?"); 12818 Ops.push_back(Last); 12819 return cast<SCEVAddRecExpr>(SE.getAddRecExpr(Ops, getLoop(), 12820 SCEV::FlagAnyWrap)); 12821 } 12822 12823 // Return true when S contains at least an undef value. 12824 bool ScalarEvolution::containsUndefs(const SCEV *S) const { 12825 return SCEVExprContains(S, [](const SCEV *S) { 12826 if (const auto *SU = dyn_cast<SCEVUnknown>(S)) 12827 return isa<UndefValue>(SU->getValue()); 12828 return false; 12829 }); 12830 } 12831 12832 // Return true when S contains a value that is a nullptr. 12833 bool ScalarEvolution::containsErasedValue(const SCEV *S) const { 12834 return SCEVExprContains(S, [](const SCEV *S) { 12835 if (const auto *SU = dyn_cast<SCEVUnknown>(S)) 12836 return SU->getValue() == nullptr; 12837 return false; 12838 }); 12839 } 12840 12841 /// Return the size of an element read or written by Inst. 12842 const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) { 12843 Type *Ty; 12844 if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) 12845 Ty = Store->getValueOperand()->getType(); 12846 else if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) 12847 Ty = Load->getType(); 12848 else 12849 return nullptr; 12850 12851 Type *ETy = getEffectiveSCEVType(PointerType::getUnqual(Ty)); 12852 return getSizeOfExpr(ETy, Ty); 12853 } 12854 12855 //===----------------------------------------------------------------------===// 12856 // SCEVCallbackVH Class Implementation 12857 //===----------------------------------------------------------------------===// 12858 12859 void ScalarEvolution::SCEVCallbackVH::deleted() { 12860 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); 12861 if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) 12862 SE->ConstantEvolutionLoopExitValue.erase(PN); 12863 SE->eraseValueFromMap(getValPtr()); 12864 // this now dangles! 12865 } 12866 12867 void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) { 12868 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); 12869 12870 // Forget all the expressions associated with users of the old value, 12871 // so that future queries will recompute the expressions using the new 12872 // value. 12873 Value *Old = getValPtr(); 12874 SmallVector<User *, 16> Worklist(Old->users()); 12875 SmallPtrSet<User *, 8> Visited; 12876 while (!Worklist.empty()) { 12877 User *U = Worklist.pop_back_val(); 12878 // Deleting the Old value will cause this to dangle. Postpone 12879 // that until everything else is done. 12880 if (U == Old) 12881 continue; 12882 if (!Visited.insert(U).second) 12883 continue; 12884 if (PHINode *PN = dyn_cast<PHINode>(U)) 12885 SE->ConstantEvolutionLoopExitValue.erase(PN); 12886 SE->eraseValueFromMap(U); 12887 llvm::append_range(Worklist, U->users()); 12888 } 12889 // Delete the Old value. 12890 if (PHINode *PN = dyn_cast<PHINode>(Old)) 12891 SE->ConstantEvolutionLoopExitValue.erase(PN); 12892 SE->eraseValueFromMap(Old); 12893 // this now dangles! 12894 } 12895 12896 ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) 12897 : CallbackVH(V), SE(se) {} 12898 12899 //===----------------------------------------------------------------------===// 12900 // ScalarEvolution Class Implementation 12901 //===----------------------------------------------------------------------===// 12902 12903 ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI, 12904 AssumptionCache &AC, DominatorTree &DT, 12905 LoopInfo &LI) 12906 : F(F), TLI(TLI), AC(AC), DT(DT), LI(LI), 12907 CouldNotCompute(new SCEVCouldNotCompute()), ValuesAtScopes(64), 12908 LoopDispositions(64), BlockDispositions(64) { 12909 // To use guards for proving predicates, we need to scan every instruction in 12910 // relevant basic blocks, and not just terminators. Doing this is a waste of 12911 // time if the IR does not actually contain any calls to 12912 // @llvm.experimental.guard, so do a quick check and remember this beforehand. 12913 // 12914 // This pessimizes the case where a pass that preserves ScalarEvolution wants 12915 // to _add_ guards to the module when there weren't any before, and wants 12916 // ScalarEvolution to optimize based on those guards. For now we prefer to be 12917 // efficient in lieu of being smart in that rather obscure case. 12918 12919 auto *GuardDecl = F.getParent()->getFunction( 12920 Intrinsic::getName(Intrinsic::experimental_guard)); 12921 HasGuards = GuardDecl && !GuardDecl->use_empty(); 12922 } 12923 12924 ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg) 12925 : F(Arg.F), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT), 12926 LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)), 12927 ValueExprMap(std::move(Arg.ValueExprMap)), 12928 PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)), 12929 PendingPhiRanges(std::move(Arg.PendingPhiRanges)), 12930 PendingMerges(std::move(Arg.PendingMerges)), 12931 MinTrailingZerosCache(std::move(Arg.MinTrailingZerosCache)), 12932 BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)), 12933 PredicatedBackedgeTakenCounts( 12934 std::move(Arg.PredicatedBackedgeTakenCounts)), 12935 BECountUsers(std::move(Arg.BECountUsers)), 12936 ConstantEvolutionLoopExitValue( 12937 std::move(Arg.ConstantEvolutionLoopExitValue)), 12938 ValuesAtScopes(std::move(Arg.ValuesAtScopes)), 12939 ValuesAtScopesUsers(std::move(Arg.ValuesAtScopesUsers)), 12940 LoopDispositions(std::move(Arg.LoopDispositions)), 12941 LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)), 12942 BlockDispositions(std::move(Arg.BlockDispositions)), 12943 SCEVUsers(std::move(Arg.SCEVUsers)), 12944 UnsignedRanges(std::move(Arg.UnsignedRanges)), 12945 SignedRanges(std::move(Arg.SignedRanges)), 12946 UniqueSCEVs(std::move(Arg.UniqueSCEVs)), 12947 UniquePreds(std::move(Arg.UniquePreds)), 12948 SCEVAllocator(std::move(Arg.SCEVAllocator)), 12949 LoopUsers(std::move(Arg.LoopUsers)), 12950 PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)), 12951 FirstUnknown(Arg.FirstUnknown) { 12952 Arg.FirstUnknown = nullptr; 12953 } 12954 12955 ScalarEvolution::~ScalarEvolution() { 12956 // Iterate through all the SCEVUnknown instances and call their 12957 // destructors, so that they release their references to their values. 12958 for (SCEVUnknown *U = FirstUnknown; U;) { 12959 SCEVUnknown *Tmp = U; 12960 U = U->Next; 12961 Tmp->~SCEVUnknown(); 12962 } 12963 FirstUnknown = nullptr; 12964 12965 ExprValueMap.clear(); 12966 ValueExprMap.clear(); 12967 HasRecMap.clear(); 12968 BackedgeTakenCounts.clear(); 12969 PredicatedBackedgeTakenCounts.clear(); 12970 12971 assert(PendingLoopPredicates.empty() && "isImpliedCond garbage"); 12972 assert(PendingPhiRanges.empty() && "getRangeRef garbage"); 12973 assert(PendingMerges.empty() && "isImpliedViaMerge garbage"); 12974 assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!"); 12975 assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!"); 12976 } 12977 12978 bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { 12979 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); 12980 } 12981 12982 static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, 12983 const Loop *L) { 12984 // Print all inner loops first 12985 for (Loop *I : *L) 12986 PrintLoopInfo(OS, SE, I); 12987 12988 OS << "Loop "; 12989 L->getHeader()->printAsOperand(OS, /*PrintType=*/false); 12990 OS << ": "; 12991 12992 SmallVector<BasicBlock *, 8> ExitingBlocks; 12993 L->getExitingBlocks(ExitingBlocks); 12994 if (ExitingBlocks.size() != 1) 12995 OS << "<multiple exits> "; 12996 12997 if (SE->hasLoopInvariantBackedgeTakenCount(L)) 12998 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L) << "\n"; 12999 else 13000 OS << "Unpredictable backedge-taken count.\n"; 13001 13002 if (ExitingBlocks.size() > 1) 13003 for (BasicBlock *ExitingBlock : ExitingBlocks) { 13004 OS << " exit count for " << ExitingBlock->getName() << ": " 13005 << *SE->getExitCount(L, ExitingBlock) << "\n"; 13006 } 13007 13008 OS << "Loop "; 13009 L->getHeader()->printAsOperand(OS, /*PrintType=*/false); 13010 OS << ": "; 13011 13012 if (!isa<SCEVCouldNotCompute>(SE->getConstantMaxBackedgeTakenCount(L))) { 13013 OS << "max backedge-taken count is " << *SE->getConstantMaxBackedgeTakenCount(L); 13014 if (SE->isBackedgeTakenCountMaxOrZero(L)) 13015 OS << ", actual taken count either this or zero."; 13016 } else { 13017 OS << "Unpredictable max backedge-taken count. "; 13018 } 13019 13020 OS << "\n" 13021 "Loop "; 13022 L->getHeader()->printAsOperand(OS, /*PrintType=*/false); 13023 OS << ": "; 13024 13025 SmallVector<const SCEVPredicate *, 4> Preds; 13026 auto PBT = SE->getPredicatedBackedgeTakenCount(L, Preds); 13027 if (!isa<SCEVCouldNotCompute>(PBT)) { 13028 OS << "Predicated backedge-taken count is " << *PBT << "\n"; 13029 OS << " Predicates:\n"; 13030 for (auto *P : Preds) 13031 P->print(OS, 4); 13032 } else { 13033 OS << "Unpredictable predicated backedge-taken count. "; 13034 } 13035 OS << "\n"; 13036 13037 if (SE->hasLoopInvariantBackedgeTakenCount(L)) { 13038 OS << "Loop "; 13039 L->getHeader()->printAsOperand(OS, /*PrintType=*/false); 13040 OS << ": "; 13041 OS << "Trip multiple is " << SE->getSmallConstantTripMultiple(L) << "\n"; 13042 } 13043 } 13044 13045 static StringRef loopDispositionToStr(ScalarEvolution::LoopDisposition LD) { 13046 switch (LD) { 13047 case ScalarEvolution::LoopVariant: 13048 return "Variant"; 13049 case ScalarEvolution::LoopInvariant: 13050 return "Invariant"; 13051 case ScalarEvolution::LoopComputable: 13052 return "Computable"; 13053 } 13054 llvm_unreachable("Unknown ScalarEvolution::LoopDisposition kind!"); 13055 } 13056 13057 void ScalarEvolution::print(raw_ostream &OS) const { 13058 // ScalarEvolution's implementation of the print method is to print 13059 // out SCEV values of all instructions that are interesting. Doing 13060 // this potentially causes it to create new SCEV objects though, 13061 // which technically conflicts with the const qualifier. This isn't 13062 // observable from outside the class though, so casting away the 13063 // const isn't dangerous. 13064 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); 13065 13066 if (ClassifyExpressions) { 13067 OS << "Classifying expressions for: "; 13068 F.printAsOperand(OS, /*PrintType=*/false); 13069 OS << "\n"; 13070 for (Instruction &I : instructions(F)) 13071 if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) { 13072 OS << I << '\n'; 13073 OS << " --> "; 13074 const SCEV *SV = SE.getSCEV(&I); 13075 SV->print(OS); 13076 if (!isa<SCEVCouldNotCompute>(SV)) { 13077 OS << " U: "; 13078 SE.getUnsignedRange(SV).print(OS); 13079 OS << " S: "; 13080 SE.getSignedRange(SV).print(OS); 13081 } 13082 13083 const Loop *L = LI.getLoopFor(I.getParent()); 13084 13085 const SCEV *AtUse = SE.getSCEVAtScope(SV, L); 13086 if (AtUse != SV) { 13087 OS << " --> "; 13088 AtUse->print(OS); 13089 if (!isa<SCEVCouldNotCompute>(AtUse)) { 13090 OS << " U: "; 13091 SE.getUnsignedRange(AtUse).print(OS); 13092 OS << " S: "; 13093 SE.getSignedRange(AtUse).print(OS); 13094 } 13095 } 13096 13097 if (L) { 13098 OS << "\t\t" "Exits: "; 13099 const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); 13100 if (!SE.isLoopInvariant(ExitValue, L)) { 13101 OS << "<<Unknown>>"; 13102 } else { 13103 OS << *ExitValue; 13104 } 13105 13106 bool First = true; 13107 for (auto *Iter = L; Iter; Iter = Iter->getParentLoop()) { 13108 if (First) { 13109 OS << "\t\t" "LoopDispositions: { "; 13110 First = false; 13111 } else { 13112 OS << ", "; 13113 } 13114 13115 Iter->getHeader()->printAsOperand(OS, /*PrintType=*/false); 13116 OS << ": " << loopDispositionToStr(SE.getLoopDisposition(SV, Iter)); 13117 } 13118 13119 for (auto *InnerL : depth_first(L)) { 13120 if (InnerL == L) 13121 continue; 13122 if (First) { 13123 OS << "\t\t" "LoopDispositions: { "; 13124 First = false; 13125 } else { 13126 OS << ", "; 13127 } 13128 13129 InnerL->getHeader()->printAsOperand(OS, /*PrintType=*/false); 13130 OS << ": " << loopDispositionToStr(SE.getLoopDisposition(SV, InnerL)); 13131 } 13132 13133 OS << " }"; 13134 } 13135 13136 OS << "\n"; 13137 } 13138 } 13139 13140 OS << "Determining loop execution counts for: "; 13141 F.printAsOperand(OS, /*PrintType=*/false); 13142 OS << "\n"; 13143 for (Loop *I : LI) 13144 PrintLoopInfo(OS, &SE, I); 13145 } 13146 13147 ScalarEvolution::LoopDisposition 13148 ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) { 13149 auto &Values = LoopDispositions[S]; 13150 for (auto &V : Values) { 13151 if (V.getPointer() == L) 13152 return V.getInt(); 13153 } 13154 Values.emplace_back(L, LoopVariant); 13155 LoopDisposition D = computeLoopDisposition(S, L); 13156 auto &Values2 = LoopDispositions[S]; 13157 for (auto &V : llvm::reverse(Values2)) { 13158 if (V.getPointer() == L) { 13159 V.setInt(D); 13160 break; 13161 } 13162 } 13163 return D; 13164 } 13165 13166 ScalarEvolution::LoopDisposition 13167 ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) { 13168 switch (S->getSCEVType()) { 13169 case scConstant: 13170 return LoopInvariant; 13171 case scPtrToInt: 13172 case scTruncate: 13173 case scZeroExtend: 13174 case scSignExtend: 13175 return getLoopDisposition(cast<SCEVCastExpr>(S)->getOperand(), L); 13176 case scAddRecExpr: { 13177 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); 13178 13179 // If L is the addrec's loop, it's computable. 13180 if (AR->getLoop() == L) 13181 return LoopComputable; 13182 13183 // Add recurrences are never invariant in the function-body (null loop). 13184 if (!L) 13185 return LoopVariant; 13186 13187 // Everything that is not defined at loop entry is variant. 13188 if (DT.dominates(L->getHeader(), AR->getLoop()->getHeader())) 13189 return LoopVariant; 13190 assert(!L->contains(AR->getLoop()) && "Containing loop's header does not" 13191 " dominate the contained loop's header?"); 13192 13193 // This recurrence is invariant w.r.t. L if AR's loop contains L. 13194 if (AR->getLoop()->contains(L)) 13195 return LoopInvariant; 13196 13197 // This recurrence is variant w.r.t. L if any of its operands 13198 // are variant. 13199 for (auto *Op : AR->operands()) 13200 if (!isLoopInvariant(Op, L)) 13201 return LoopVariant; 13202 13203 // Otherwise it's loop-invariant. 13204 return LoopInvariant; 13205 } 13206 case scAddExpr: 13207 case scMulExpr: 13208 case scUMaxExpr: 13209 case scSMaxExpr: 13210 case scUMinExpr: 13211 case scSMinExpr: 13212 case scSequentialUMinExpr: { 13213 bool HasVarying = false; 13214 for (auto *Op : cast<SCEVNAryExpr>(S)->operands()) { 13215 LoopDisposition D = getLoopDisposition(Op, L); 13216 if (D == LoopVariant) 13217 return LoopVariant; 13218 if (D == LoopComputable) 13219 HasVarying = true; 13220 } 13221 return HasVarying ? LoopComputable : LoopInvariant; 13222 } 13223 case scUDivExpr: { 13224 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); 13225 LoopDisposition LD = getLoopDisposition(UDiv->getLHS(), L); 13226 if (LD == LoopVariant) 13227 return LoopVariant; 13228 LoopDisposition RD = getLoopDisposition(UDiv->getRHS(), L); 13229 if (RD == LoopVariant) 13230 return LoopVariant; 13231 return (LD == LoopInvariant && RD == LoopInvariant) ? 13232 LoopInvariant : LoopComputable; 13233 } 13234 case scUnknown: 13235 // All non-instruction values are loop invariant. All instructions are loop 13236 // invariant if they are not contained in the specified loop. 13237 // Instructions are never considered invariant in the function body 13238 // (null loop) because they are defined within the "loop". 13239 if (auto *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) 13240 return (L && !L->contains(I)) ? LoopInvariant : LoopVariant; 13241 return LoopInvariant; 13242 case scCouldNotCompute: 13243 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); 13244 } 13245 llvm_unreachable("Unknown SCEV kind!"); 13246 } 13247 13248 bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) { 13249 return getLoopDisposition(S, L) == LoopInvariant; 13250 } 13251 13252 bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) { 13253 return getLoopDisposition(S, L) == LoopComputable; 13254 } 13255 13256 ScalarEvolution::BlockDisposition 13257 ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) { 13258 auto &Values = BlockDispositions[S]; 13259 for (auto &V : Values) { 13260 if (V.getPointer() == BB) 13261 return V.getInt(); 13262 } 13263 Values.emplace_back(BB, DoesNotDominateBlock); 13264 BlockDisposition D = computeBlockDisposition(S, BB); 13265 auto &Values2 = BlockDispositions[S]; 13266 for (auto &V : llvm::reverse(Values2)) { 13267 if (V.getPointer() == BB) { 13268 V.setInt(D); 13269 break; 13270 } 13271 } 13272 return D; 13273 } 13274 13275 ScalarEvolution::BlockDisposition 13276 ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) { 13277 switch (S->getSCEVType()) { 13278 case scConstant: 13279 return ProperlyDominatesBlock; 13280 case scPtrToInt: 13281 case scTruncate: 13282 case scZeroExtend: 13283 case scSignExtend: 13284 return getBlockDisposition(cast<SCEVCastExpr>(S)->getOperand(), BB); 13285 case scAddRecExpr: { 13286 // This uses a "dominates" query instead of "properly dominates" query 13287 // to test for proper dominance too, because the instruction which 13288 // produces the addrec's value is a PHI, and a PHI effectively properly 13289 // dominates its entire containing block. 13290 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); 13291 if (!DT.dominates(AR->getLoop()->getHeader(), BB)) 13292 return DoesNotDominateBlock; 13293 13294 // Fall through into SCEVNAryExpr handling. 13295 LLVM_FALLTHROUGH; 13296 } 13297 case scAddExpr: 13298 case scMulExpr: 13299 case scUMaxExpr: 13300 case scSMaxExpr: 13301 case scUMinExpr: 13302 case scSMinExpr: 13303 case scSequentialUMinExpr: { 13304 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); 13305 bool Proper = true; 13306 for (const SCEV *NAryOp : NAry->operands()) { 13307 BlockDisposition D = getBlockDisposition(NAryOp, BB); 13308 if (D == DoesNotDominateBlock) 13309 return DoesNotDominateBlock; 13310 if (D == DominatesBlock) 13311 Proper = false; 13312 } 13313 return Proper ? ProperlyDominatesBlock : DominatesBlock; 13314 } 13315 case scUDivExpr: { 13316 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); 13317 const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS(); 13318 BlockDisposition LD = getBlockDisposition(LHS, BB); 13319 if (LD == DoesNotDominateBlock) 13320 return DoesNotDominateBlock; 13321 BlockDisposition RD = getBlockDisposition(RHS, BB); 13322 if (RD == DoesNotDominateBlock) 13323 return DoesNotDominateBlock; 13324 return (LD == ProperlyDominatesBlock && RD == ProperlyDominatesBlock) ? 13325 ProperlyDominatesBlock : DominatesBlock; 13326 } 13327 case scUnknown: 13328 if (Instruction *I = 13329 dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) { 13330 if (I->getParent() == BB) 13331 return DominatesBlock; 13332 if (DT.properlyDominates(I->getParent(), BB)) 13333 return ProperlyDominatesBlock; 13334 return DoesNotDominateBlock; 13335 } 13336 return ProperlyDominatesBlock; 13337 case scCouldNotCompute: 13338 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); 13339 } 13340 llvm_unreachable("Unknown SCEV kind!"); 13341 } 13342 13343 bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) { 13344 return getBlockDisposition(S, BB) >= DominatesBlock; 13345 } 13346 13347 bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) { 13348 return getBlockDisposition(S, BB) == ProperlyDominatesBlock; 13349 } 13350 13351 bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const { 13352 return SCEVExprContains(S, [&](const SCEV *Expr) { return Expr == Op; }); 13353 } 13354 13355 void ScalarEvolution::forgetBackedgeTakenCounts(const Loop *L, 13356 bool Predicated) { 13357 auto &BECounts = 13358 Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts; 13359 auto It = BECounts.find(L); 13360 if (It != BECounts.end()) { 13361 for (const ExitNotTakenInfo &ENT : It->second.ExitNotTaken) { 13362 if (!isa<SCEVConstant>(ENT.ExactNotTaken)) { 13363 auto UserIt = BECountUsers.find(ENT.ExactNotTaken); 13364 assert(UserIt != BECountUsers.end()); 13365 UserIt->second.erase({L, Predicated}); 13366 } 13367 } 13368 BECounts.erase(It); 13369 } 13370 } 13371 13372 void ScalarEvolution::forgetMemoizedResults(ArrayRef<const SCEV *> SCEVs) { 13373 SmallPtrSet<const SCEV *, 8> ToForget(SCEVs.begin(), SCEVs.end()); 13374 SmallVector<const SCEV *, 8> Worklist(ToForget.begin(), ToForget.end()); 13375 13376 while (!Worklist.empty()) { 13377 const SCEV *Curr = Worklist.pop_back_val(); 13378 auto Users = SCEVUsers.find(Curr); 13379 if (Users != SCEVUsers.end()) 13380 for (auto *User : Users->second) 13381 if (ToForget.insert(User).second) 13382 Worklist.push_back(User); 13383 } 13384 13385 for (auto *S : ToForget) 13386 forgetMemoizedResultsImpl(S); 13387 13388 for (auto I = PredicatedSCEVRewrites.begin(); 13389 I != PredicatedSCEVRewrites.end();) { 13390 std::pair<const SCEV *, const Loop *> Entry = I->first; 13391 if (ToForget.count(Entry.first)) 13392 PredicatedSCEVRewrites.erase(I++); 13393 else 13394 ++I; 13395 } 13396 } 13397 13398 void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) { 13399 LoopDispositions.erase(S); 13400 BlockDispositions.erase(S); 13401 UnsignedRanges.erase(S); 13402 SignedRanges.erase(S); 13403 HasRecMap.erase(S); 13404 MinTrailingZerosCache.erase(S); 13405 13406 auto ExprIt = ExprValueMap.find(S); 13407 if (ExprIt != ExprValueMap.end()) { 13408 for (Value *V : ExprIt->second) { 13409 auto ValueIt = ValueExprMap.find_as(V); 13410 if (ValueIt != ValueExprMap.end()) 13411 ValueExprMap.erase(ValueIt); 13412 } 13413 ExprValueMap.erase(ExprIt); 13414 } 13415 13416 auto ScopeIt = ValuesAtScopes.find(S); 13417 if (ScopeIt != ValuesAtScopes.end()) { 13418 for (const auto &Pair : ScopeIt->second) 13419 if (!isa_and_nonnull<SCEVConstant>(Pair.second)) 13420 erase_value(ValuesAtScopesUsers[Pair.second], 13421 std::make_pair(Pair.first, S)); 13422 ValuesAtScopes.erase(ScopeIt); 13423 } 13424 13425 auto ScopeUserIt = ValuesAtScopesUsers.find(S); 13426 if (ScopeUserIt != ValuesAtScopesUsers.end()) { 13427 for (const auto &Pair : ScopeUserIt->second) 13428 erase_value(ValuesAtScopes[Pair.second], std::make_pair(Pair.first, S)); 13429 ValuesAtScopesUsers.erase(ScopeUserIt); 13430 } 13431 13432 auto BEUsersIt = BECountUsers.find(S); 13433 if (BEUsersIt != BECountUsers.end()) { 13434 // Work on a copy, as forgetBackedgeTakenCounts() will modify the original. 13435 auto Copy = BEUsersIt->second; 13436 for (const auto &Pair : Copy) 13437 forgetBackedgeTakenCounts(Pair.getPointer(), Pair.getInt()); 13438 BECountUsers.erase(BEUsersIt); 13439 } 13440 } 13441 13442 void 13443 ScalarEvolution::getUsedLoops(const SCEV *S, 13444 SmallPtrSetImpl<const Loop *> &LoopsUsed) { 13445 struct FindUsedLoops { 13446 FindUsedLoops(SmallPtrSetImpl<const Loop *> &LoopsUsed) 13447 : LoopsUsed(LoopsUsed) {} 13448 SmallPtrSetImpl<const Loop *> &LoopsUsed; 13449 bool follow(const SCEV *S) { 13450 if (auto *AR = dyn_cast<SCEVAddRecExpr>(S)) 13451 LoopsUsed.insert(AR->getLoop()); 13452 return true; 13453 } 13454 13455 bool isDone() const { return false; } 13456 }; 13457 13458 FindUsedLoops F(LoopsUsed); 13459 SCEVTraversal<FindUsedLoops>(F).visitAll(S); 13460 } 13461 13462 void ScalarEvolution::getReachableBlocks( 13463 SmallPtrSetImpl<BasicBlock *> &Reachable, Function &F) { 13464 SmallVector<BasicBlock *> Worklist; 13465 Worklist.push_back(&F.getEntryBlock()); 13466 while (!Worklist.empty()) { 13467 BasicBlock *BB = Worklist.pop_back_val(); 13468 if (!Reachable.insert(BB).second) 13469 continue; 13470 13471 Value *Cond; 13472 BasicBlock *TrueBB, *FalseBB; 13473 if (match(BB->getTerminator(), m_Br(m_Value(Cond), m_BasicBlock(TrueBB), 13474 m_BasicBlock(FalseBB)))) { 13475 if (auto *C = dyn_cast<ConstantInt>(Cond)) { 13476 Worklist.push_back(C->isOne() ? TrueBB : FalseBB); 13477 continue; 13478 } 13479 13480 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) { 13481 const SCEV *L = getSCEV(Cmp->getOperand(0)); 13482 const SCEV *R = getSCEV(Cmp->getOperand(1)); 13483 if (isKnownPredicateViaConstantRanges(Cmp->getPredicate(), L, R)) { 13484 Worklist.push_back(TrueBB); 13485 continue; 13486 } 13487 if (isKnownPredicateViaConstantRanges(Cmp->getInversePredicate(), L, 13488 R)) { 13489 Worklist.push_back(FalseBB); 13490 continue; 13491 } 13492 } 13493 } 13494 13495 append_range(Worklist, successors(BB)); 13496 } 13497 } 13498 13499 void ScalarEvolution::verify() const { 13500 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); 13501 ScalarEvolution SE2(F, TLI, AC, DT, LI); 13502 13503 SmallVector<Loop *, 8> LoopStack(LI.begin(), LI.end()); 13504 13505 // Map's SCEV expressions from one ScalarEvolution "universe" to another. 13506 struct SCEVMapper : public SCEVRewriteVisitor<SCEVMapper> { 13507 SCEVMapper(ScalarEvolution &SE) : SCEVRewriteVisitor<SCEVMapper>(SE) {} 13508 13509 const SCEV *visitConstant(const SCEVConstant *Constant) { 13510 return SE.getConstant(Constant->getAPInt()); 13511 } 13512 13513 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 13514 return SE.getUnknown(Expr->getValue()); 13515 } 13516 13517 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { 13518 return SE.getCouldNotCompute(); 13519 } 13520 }; 13521 13522 SCEVMapper SCM(SE2); 13523 SmallPtrSet<BasicBlock *, 16> ReachableBlocks; 13524 SE2.getReachableBlocks(ReachableBlocks, F); 13525 13526 auto GetDelta = [&](const SCEV *Old, const SCEV *New) -> const SCEV * { 13527 if (containsUndefs(Old) || containsUndefs(New)) { 13528 // SCEV treats "undef" as an unknown but consistent value (i.e. it does 13529 // not propagate undef aggressively). This means we can (and do) fail 13530 // verification in cases where a transform makes a value go from "undef" 13531 // to "undef+1" (say). The transform is fine, since in both cases the 13532 // result is "undef", but SCEV thinks the value increased by 1. 13533 return nullptr; 13534 } 13535 13536 // Unless VerifySCEVStrict is set, we only compare constant deltas. 13537 const SCEV *Delta = SE2.getMinusSCEV(Old, New); 13538 if (!VerifySCEVStrict && !isa<SCEVConstant>(Delta)) 13539 return nullptr; 13540 13541 return Delta; 13542 }; 13543 13544 while (!LoopStack.empty()) { 13545 auto *L = LoopStack.pop_back_val(); 13546 llvm::append_range(LoopStack, *L); 13547 13548 // Only verify BECounts in reachable loops. For an unreachable loop, 13549 // any BECount is legal. 13550 if (!ReachableBlocks.contains(L->getHeader())) 13551 continue; 13552 13553 // Only verify cached BECounts. Computing new BECounts may change the 13554 // results of subsequent SCEV uses. 13555 auto It = BackedgeTakenCounts.find(L); 13556 if (It == BackedgeTakenCounts.end()) 13557 continue; 13558 13559 auto *CurBECount = 13560 SCM.visit(It->second.getExact(L, const_cast<ScalarEvolution *>(this))); 13561 auto *NewBECount = SE2.getBackedgeTakenCount(L); 13562 13563 if (CurBECount == SE2.getCouldNotCompute() || 13564 NewBECount == SE2.getCouldNotCompute()) { 13565 // NB! This situation is legal, but is very suspicious -- whatever pass 13566 // change the loop to make a trip count go from could not compute to 13567 // computable or vice-versa *should have* invalidated SCEV. However, we 13568 // choose not to assert here (for now) since we don't want false 13569 // positives. 13570 continue; 13571 } 13572 13573 if (SE.getTypeSizeInBits(CurBECount->getType()) > 13574 SE.getTypeSizeInBits(NewBECount->getType())) 13575 NewBECount = SE2.getZeroExtendExpr(NewBECount, CurBECount->getType()); 13576 else if (SE.getTypeSizeInBits(CurBECount->getType()) < 13577 SE.getTypeSizeInBits(NewBECount->getType())) 13578 CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType()); 13579 13580 const SCEV *Delta = GetDelta(CurBECount, NewBECount); 13581 if (Delta && !Delta->isZero()) { 13582 dbgs() << "Trip Count for " << *L << " Changed!\n"; 13583 dbgs() << "Old: " << *CurBECount << "\n"; 13584 dbgs() << "New: " << *NewBECount << "\n"; 13585 dbgs() << "Delta: " << *Delta << "\n"; 13586 std::abort(); 13587 } 13588 } 13589 13590 // Collect all valid loops currently in LoopInfo. 13591 SmallPtrSet<Loop *, 32> ValidLoops; 13592 SmallVector<Loop *, 32> Worklist(LI.begin(), LI.end()); 13593 while (!Worklist.empty()) { 13594 Loop *L = Worklist.pop_back_val(); 13595 if (ValidLoops.insert(L).second) 13596 Worklist.append(L->begin(), L->end()); 13597 } 13598 for (auto &KV : ValueExprMap) { 13599 #ifndef NDEBUG 13600 // Check for SCEV expressions referencing invalid/deleted loops. 13601 if (auto *AR = dyn_cast<SCEVAddRecExpr>(KV.second)) { 13602 assert(ValidLoops.contains(AR->getLoop()) && 13603 "AddRec references invalid loop"); 13604 } 13605 #endif 13606 13607 // Check that the value is also part of the reverse map. 13608 auto It = ExprValueMap.find(KV.second); 13609 if (It == ExprValueMap.end() || !It->second.contains(KV.first)) { 13610 dbgs() << "Value " << *KV.first 13611 << " is in ValueExprMap but not in ExprValueMap\n"; 13612 std::abort(); 13613 } 13614 13615 if (auto *I = dyn_cast<Instruction>(&*KV.first)) { 13616 if (!ReachableBlocks.contains(I->getParent())) 13617 continue; 13618 const SCEV *OldSCEV = SCM.visit(KV.second); 13619 const SCEV *NewSCEV = SE2.getSCEV(I); 13620 const SCEV *Delta = GetDelta(OldSCEV, NewSCEV); 13621 if (Delta && !Delta->isZero()) { 13622 dbgs() << "SCEV for value " << *I << " changed!\n" 13623 << "Old: " << *OldSCEV << "\n" 13624 << "New: " << *NewSCEV << "\n" 13625 << "Delta: " << *Delta << "\n"; 13626 std::abort(); 13627 } 13628 } 13629 } 13630 13631 for (const auto &KV : ExprValueMap) { 13632 for (Value *V : KV.second) { 13633 auto It = ValueExprMap.find_as(V); 13634 if (It == ValueExprMap.end()) { 13635 dbgs() << "Value " << *V 13636 << " is in ExprValueMap but not in ValueExprMap\n"; 13637 std::abort(); 13638 } 13639 if (It->second != KV.first) { 13640 dbgs() << "Value " << *V << " mapped to " << *It->second 13641 << " rather than " << *KV.first << "\n"; 13642 std::abort(); 13643 } 13644 } 13645 } 13646 13647 // Verify integrity of SCEV users. 13648 for (const auto &S : UniqueSCEVs) { 13649 SmallVector<const SCEV *, 4> Ops; 13650 collectUniqueOps(&S, Ops); 13651 for (const auto *Op : Ops) { 13652 // We do not store dependencies of constants. 13653 if (isa<SCEVConstant>(Op)) 13654 continue; 13655 auto It = SCEVUsers.find(Op); 13656 if (It != SCEVUsers.end() && It->second.count(&S)) 13657 continue; 13658 dbgs() << "Use of operand " << *Op << " by user " << S 13659 << " is not being tracked!\n"; 13660 std::abort(); 13661 } 13662 } 13663 13664 // Verify integrity of ValuesAtScopes users. 13665 for (const auto &ValueAndVec : ValuesAtScopes) { 13666 const SCEV *Value = ValueAndVec.first; 13667 for (const auto &LoopAndValueAtScope : ValueAndVec.second) { 13668 const Loop *L = LoopAndValueAtScope.first; 13669 const SCEV *ValueAtScope = LoopAndValueAtScope.second; 13670 if (!isa<SCEVConstant>(ValueAtScope)) { 13671 auto It = ValuesAtScopesUsers.find(ValueAtScope); 13672 if (It != ValuesAtScopesUsers.end() && 13673 is_contained(It->second, std::make_pair(L, Value))) 13674 continue; 13675 dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: " 13676 << *ValueAtScope << " missing in ValuesAtScopesUsers\n"; 13677 std::abort(); 13678 } 13679 } 13680 } 13681 13682 for (const auto &ValueAtScopeAndVec : ValuesAtScopesUsers) { 13683 const SCEV *ValueAtScope = ValueAtScopeAndVec.first; 13684 for (const auto &LoopAndValue : ValueAtScopeAndVec.second) { 13685 const Loop *L = LoopAndValue.first; 13686 const SCEV *Value = LoopAndValue.second; 13687 assert(!isa<SCEVConstant>(Value)); 13688 auto It = ValuesAtScopes.find(Value); 13689 if (It != ValuesAtScopes.end() && 13690 is_contained(It->second, std::make_pair(L, ValueAtScope))) 13691 continue; 13692 dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: " 13693 << *ValueAtScope << " missing in ValuesAtScopes\n"; 13694 std::abort(); 13695 } 13696 } 13697 13698 // Verify integrity of BECountUsers. 13699 auto VerifyBECountUsers = [&](bool Predicated) { 13700 auto &BECounts = 13701 Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts; 13702 for (const auto &LoopAndBEInfo : BECounts) { 13703 for (const ExitNotTakenInfo &ENT : LoopAndBEInfo.second.ExitNotTaken) { 13704 if (!isa<SCEVConstant>(ENT.ExactNotTaken)) { 13705 auto UserIt = BECountUsers.find(ENT.ExactNotTaken); 13706 if (UserIt != BECountUsers.end() && 13707 UserIt->second.contains({ LoopAndBEInfo.first, Predicated })) 13708 continue; 13709 dbgs() << "Value " << *ENT.ExactNotTaken << " for loop " 13710 << *LoopAndBEInfo.first << " missing from BECountUsers\n"; 13711 std::abort(); 13712 } 13713 } 13714 } 13715 }; 13716 VerifyBECountUsers(/* Predicated */ false); 13717 VerifyBECountUsers(/* Predicated */ true); 13718 } 13719 13720 bool ScalarEvolution::invalidate( 13721 Function &F, const PreservedAnalyses &PA, 13722 FunctionAnalysisManager::Invalidator &Inv) { 13723 // Invalidate the ScalarEvolution object whenever it isn't preserved or one 13724 // of its dependencies is invalidated. 13725 auto PAC = PA.getChecker<ScalarEvolutionAnalysis>(); 13726 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) || 13727 Inv.invalidate<AssumptionAnalysis>(F, PA) || 13728 Inv.invalidate<DominatorTreeAnalysis>(F, PA) || 13729 Inv.invalidate<LoopAnalysis>(F, PA); 13730 } 13731 13732 AnalysisKey ScalarEvolutionAnalysis::Key; 13733 13734 ScalarEvolution ScalarEvolutionAnalysis::run(Function &F, 13735 FunctionAnalysisManager &AM) { 13736 return ScalarEvolution(F, AM.getResult<TargetLibraryAnalysis>(F), 13737 AM.getResult<AssumptionAnalysis>(F), 13738 AM.getResult<DominatorTreeAnalysis>(F), 13739 AM.getResult<LoopAnalysis>(F)); 13740 } 13741 13742 PreservedAnalyses 13743 ScalarEvolutionVerifierPass::run(Function &F, FunctionAnalysisManager &AM) { 13744 AM.getResult<ScalarEvolutionAnalysis>(F).verify(); 13745 return PreservedAnalyses::all(); 13746 } 13747 13748 PreservedAnalyses 13749 ScalarEvolutionPrinterPass::run(Function &F, FunctionAnalysisManager &AM) { 13750 // For compatibility with opt's -analyze feature under legacy pass manager 13751 // which was not ported to NPM. This keeps tests using 13752 // update_analyze_test_checks.py working. 13753 OS << "Printing analysis 'Scalar Evolution Analysis' for function '" 13754 << F.getName() << "':\n"; 13755 AM.getResult<ScalarEvolutionAnalysis>(F).print(OS); 13756 return PreservedAnalyses::all(); 13757 } 13758 13759 INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution", 13760 "Scalar Evolution Analysis", false, true) 13761 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 13762 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 13763 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 13764 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 13765 INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution", 13766 "Scalar Evolution Analysis", false, true) 13767 13768 char ScalarEvolutionWrapperPass::ID = 0; 13769 13770 ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() : FunctionPass(ID) { 13771 initializeScalarEvolutionWrapperPassPass(*PassRegistry::getPassRegistry()); 13772 } 13773 13774 bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) { 13775 SE.reset(new ScalarEvolution( 13776 F, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F), 13777 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F), 13778 getAnalysis<DominatorTreeWrapperPass>().getDomTree(), 13779 getAnalysis<LoopInfoWrapperPass>().getLoopInfo())); 13780 return false; 13781 } 13782 13783 void ScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); } 13784 13785 void ScalarEvolutionWrapperPass::print(raw_ostream &OS, const Module *) const { 13786 SE->print(OS); 13787 } 13788 13789 void ScalarEvolutionWrapperPass::verifyAnalysis() const { 13790 if (!VerifySCEV) 13791 return; 13792 13793 SE->verify(); 13794 } 13795 13796 void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 13797 AU.setPreservesAll(); 13798 AU.addRequiredTransitive<AssumptionCacheTracker>(); 13799 AU.addRequiredTransitive<LoopInfoWrapperPass>(); 13800 AU.addRequiredTransitive<DominatorTreeWrapperPass>(); 13801 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>(); 13802 } 13803 13804 const SCEVPredicate *ScalarEvolution::getEqualPredicate(const SCEV *LHS, 13805 const SCEV *RHS) { 13806 return getComparePredicate(ICmpInst::ICMP_EQ, LHS, RHS); 13807 } 13808 13809 const SCEVPredicate * 13810 ScalarEvolution::getComparePredicate(const ICmpInst::Predicate Pred, 13811 const SCEV *LHS, const SCEV *RHS) { 13812 FoldingSetNodeID ID; 13813 assert(LHS->getType() == RHS->getType() && 13814 "Type mismatch between LHS and RHS"); 13815 // Unique this node based on the arguments 13816 ID.AddInteger(SCEVPredicate::P_Compare); 13817 ID.AddInteger(Pred); 13818 ID.AddPointer(LHS); 13819 ID.AddPointer(RHS); 13820 void *IP = nullptr; 13821 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP)) 13822 return S; 13823 SCEVComparePredicate *Eq = new (SCEVAllocator) 13824 SCEVComparePredicate(ID.Intern(SCEVAllocator), Pred, LHS, RHS); 13825 UniquePreds.InsertNode(Eq, IP); 13826 return Eq; 13827 } 13828 13829 const SCEVPredicate *ScalarEvolution::getWrapPredicate( 13830 const SCEVAddRecExpr *AR, 13831 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) { 13832 FoldingSetNodeID ID; 13833 // Unique this node based on the arguments 13834 ID.AddInteger(SCEVPredicate::P_Wrap); 13835 ID.AddPointer(AR); 13836 ID.AddInteger(AddedFlags); 13837 void *IP = nullptr; 13838 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP)) 13839 return S; 13840 auto *OF = new (SCEVAllocator) 13841 SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags); 13842 UniquePreds.InsertNode(OF, IP); 13843 return OF; 13844 } 13845 13846 namespace { 13847 13848 class SCEVPredicateRewriter : public SCEVRewriteVisitor<SCEVPredicateRewriter> { 13849 public: 13850 13851 /// Rewrites \p S in the context of a loop L and the SCEV predication 13852 /// infrastructure. 13853 /// 13854 /// If \p Pred is non-null, the SCEV expression is rewritten to respect the 13855 /// equivalences present in \p Pred. 13856 /// 13857 /// If \p NewPreds is non-null, rewrite is free to add further predicates to 13858 /// \p NewPreds such that the result will be an AddRecExpr. 13859 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE, 13860 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds, 13861 const SCEVPredicate *Pred) { 13862 SCEVPredicateRewriter Rewriter(L, SE, NewPreds, Pred); 13863 return Rewriter.visit(S); 13864 } 13865 13866 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 13867 if (Pred) { 13868 if (auto *U = dyn_cast<SCEVUnionPredicate>(Pred)) { 13869 for (auto *Pred : U->getPredicates()) 13870 if (const auto *IPred = dyn_cast<SCEVComparePredicate>(Pred)) 13871 if (IPred->getLHS() == Expr && 13872 IPred->getPredicate() == ICmpInst::ICMP_EQ) 13873 return IPred->getRHS(); 13874 } else if (const auto *IPred = dyn_cast<SCEVComparePredicate>(Pred)) { 13875 if (IPred->getLHS() == Expr && 13876 IPred->getPredicate() == ICmpInst::ICMP_EQ) 13877 return IPred->getRHS(); 13878 } 13879 } 13880 return convertToAddRecWithPreds(Expr); 13881 } 13882 13883 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { 13884 const SCEV *Operand = visit(Expr->getOperand()); 13885 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand); 13886 if (AR && AR->getLoop() == L && AR->isAffine()) { 13887 // This couldn't be folded because the operand didn't have the nuw 13888 // flag. Add the nusw flag as an assumption that we could make. 13889 const SCEV *Step = AR->getStepRecurrence(SE); 13890 Type *Ty = Expr->getType(); 13891 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNUSW)) 13892 return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty), 13893 SE.getSignExtendExpr(Step, Ty), L, 13894 AR->getNoWrapFlags()); 13895 } 13896 return SE.getZeroExtendExpr(Operand, Expr->getType()); 13897 } 13898 13899 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { 13900 const SCEV *Operand = visit(Expr->getOperand()); 13901 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand); 13902 if (AR && AR->getLoop() == L && AR->isAffine()) { 13903 // This couldn't be folded because the operand didn't have the nsw 13904 // flag. Add the nssw flag as an assumption that we could make. 13905 const SCEV *Step = AR->getStepRecurrence(SE); 13906 Type *Ty = Expr->getType(); 13907 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNSSW)) 13908 return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty), 13909 SE.getSignExtendExpr(Step, Ty), L, 13910 AR->getNoWrapFlags()); 13911 } 13912 return SE.getSignExtendExpr(Operand, Expr->getType()); 13913 } 13914 13915 private: 13916 explicit SCEVPredicateRewriter(const Loop *L, ScalarEvolution &SE, 13917 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds, 13918 const SCEVPredicate *Pred) 13919 : SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred), L(L) {} 13920 13921 bool addOverflowAssumption(const SCEVPredicate *P) { 13922 if (!NewPreds) { 13923 // Check if we've already made this assumption. 13924 return Pred && Pred->implies(P); 13925 } 13926 NewPreds->insert(P); 13927 return true; 13928 } 13929 13930 bool addOverflowAssumption(const SCEVAddRecExpr *AR, 13931 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) { 13932 auto *A = SE.getWrapPredicate(AR, AddedFlags); 13933 return addOverflowAssumption(A); 13934 } 13935 13936 // If \p Expr represents a PHINode, we try to see if it can be represented 13937 // as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible 13938 // to add this predicate as a runtime overflow check, we return the AddRec. 13939 // If \p Expr does not meet these conditions (is not a PHI node, or we 13940 // couldn't create an AddRec for it, or couldn't add the predicate), we just 13941 // return \p Expr. 13942 const SCEV *convertToAddRecWithPreds(const SCEVUnknown *Expr) { 13943 if (!isa<PHINode>(Expr->getValue())) 13944 return Expr; 13945 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>> 13946 PredicatedRewrite = SE.createAddRecFromPHIWithCasts(Expr); 13947 if (!PredicatedRewrite) 13948 return Expr; 13949 for (auto *P : PredicatedRewrite->second){ 13950 // Wrap predicates from outer loops are not supported. 13951 if (auto *WP = dyn_cast<const SCEVWrapPredicate>(P)) { 13952 if (L != WP->getExpr()->getLoop()) 13953 return Expr; 13954 } 13955 if (!addOverflowAssumption(P)) 13956 return Expr; 13957 } 13958 return PredicatedRewrite->first; 13959 } 13960 13961 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds; 13962 const SCEVPredicate *Pred; 13963 const Loop *L; 13964 }; 13965 13966 } // end anonymous namespace 13967 13968 const SCEV * 13969 ScalarEvolution::rewriteUsingPredicate(const SCEV *S, const Loop *L, 13970 const SCEVPredicate &Preds) { 13971 return SCEVPredicateRewriter::rewrite(S, L, *this, nullptr, &Preds); 13972 } 13973 13974 const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates( 13975 const SCEV *S, const Loop *L, 13976 SmallPtrSetImpl<const SCEVPredicate *> &Preds) { 13977 SmallPtrSet<const SCEVPredicate *, 4> TransformPreds; 13978 S = SCEVPredicateRewriter::rewrite(S, L, *this, &TransformPreds, nullptr); 13979 auto *AddRec = dyn_cast<SCEVAddRecExpr>(S); 13980 13981 if (!AddRec) 13982 return nullptr; 13983 13984 // Since the transformation was successful, we can now transfer the SCEV 13985 // predicates. 13986 for (auto *P : TransformPreds) 13987 Preds.insert(P); 13988 13989 return AddRec; 13990 } 13991 13992 /// SCEV predicates 13993 SCEVPredicate::SCEVPredicate(const FoldingSetNodeIDRef ID, 13994 SCEVPredicateKind Kind) 13995 : FastID(ID), Kind(Kind) {} 13996 13997 SCEVComparePredicate::SCEVComparePredicate(const FoldingSetNodeIDRef ID, 13998 const ICmpInst::Predicate Pred, 13999 const SCEV *LHS, const SCEV *RHS) 14000 : SCEVPredicate(ID, P_Compare), Pred(Pred), LHS(LHS), RHS(RHS) { 14001 assert(LHS->getType() == RHS->getType() && "LHS and RHS types don't match"); 14002 assert(LHS != RHS && "LHS and RHS are the same SCEV"); 14003 } 14004 14005 bool SCEVComparePredicate::implies(const SCEVPredicate *N) const { 14006 const auto *Op = dyn_cast<SCEVComparePredicate>(N); 14007 14008 if (!Op) 14009 return false; 14010 14011 if (Pred != ICmpInst::ICMP_EQ) 14012 return false; 14013 14014 return Op->LHS == LHS && Op->RHS == RHS; 14015 } 14016 14017 bool SCEVComparePredicate::isAlwaysTrue() const { return false; } 14018 14019 void SCEVComparePredicate::print(raw_ostream &OS, unsigned Depth) const { 14020 if (Pred == ICmpInst::ICMP_EQ) 14021 OS.indent(Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n"; 14022 else 14023 OS.indent(Depth) << "Compare predicate: " << *LHS 14024 << " " << CmpInst::getPredicateName(Pred) << ") " 14025 << *RHS << "\n"; 14026 14027 } 14028 14029 SCEVWrapPredicate::SCEVWrapPredicate(const FoldingSetNodeIDRef ID, 14030 const SCEVAddRecExpr *AR, 14031 IncrementWrapFlags Flags) 14032 : SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {} 14033 14034 const SCEVAddRecExpr *SCEVWrapPredicate::getExpr() const { return AR; } 14035 14036 bool SCEVWrapPredicate::implies(const SCEVPredicate *N) const { 14037 const auto *Op = dyn_cast<SCEVWrapPredicate>(N); 14038 14039 return Op && Op->AR == AR && setFlags(Flags, Op->Flags) == Flags; 14040 } 14041 14042 bool SCEVWrapPredicate::isAlwaysTrue() const { 14043 SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags(); 14044 IncrementWrapFlags IFlags = Flags; 14045 14046 if (ScalarEvolution::setFlags(ScevFlags, SCEV::FlagNSW) == ScevFlags) 14047 IFlags = clearFlags(IFlags, IncrementNSSW); 14048 14049 return IFlags == IncrementAnyWrap; 14050 } 14051 14052 void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const { 14053 OS.indent(Depth) << *getExpr() << " Added Flags: "; 14054 if (SCEVWrapPredicate::IncrementNUSW & getFlags()) 14055 OS << "<nusw>"; 14056 if (SCEVWrapPredicate::IncrementNSSW & getFlags()) 14057 OS << "<nssw>"; 14058 OS << "\n"; 14059 } 14060 14061 SCEVWrapPredicate::IncrementWrapFlags 14062 SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR, 14063 ScalarEvolution &SE) { 14064 IncrementWrapFlags ImpliedFlags = IncrementAnyWrap; 14065 SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags(); 14066 14067 // We can safely transfer the NSW flag as NSSW. 14068 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNSW) == StaticFlags) 14069 ImpliedFlags = IncrementNSSW; 14070 14071 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNUW) == StaticFlags) { 14072 // If the increment is positive, the SCEV NUW flag will also imply the 14073 // WrapPredicate NUSW flag. 14074 if (const auto *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE))) 14075 if (Step->getValue()->getValue().isNonNegative()) 14076 ImpliedFlags = setFlags(ImpliedFlags, IncrementNUSW); 14077 } 14078 14079 return ImpliedFlags; 14080 } 14081 14082 /// Union predicates don't get cached so create a dummy set ID for it. 14083 SCEVUnionPredicate::SCEVUnionPredicate(ArrayRef<const SCEVPredicate *> Preds) 14084 : SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) { 14085 for (auto *P : Preds) 14086 add(P); 14087 } 14088 14089 bool SCEVUnionPredicate::isAlwaysTrue() const { 14090 return all_of(Preds, 14091 [](const SCEVPredicate *I) { return I->isAlwaysTrue(); }); 14092 } 14093 14094 bool SCEVUnionPredicate::implies(const SCEVPredicate *N) const { 14095 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N)) 14096 return all_of(Set->Preds, 14097 [this](const SCEVPredicate *I) { return this->implies(I); }); 14098 14099 return any_of(Preds, 14100 [N](const SCEVPredicate *I) { return I->implies(N); }); 14101 } 14102 14103 void SCEVUnionPredicate::print(raw_ostream &OS, unsigned Depth) const { 14104 for (auto Pred : Preds) 14105 Pred->print(OS, Depth); 14106 } 14107 14108 void SCEVUnionPredicate::add(const SCEVPredicate *N) { 14109 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N)) { 14110 for (auto Pred : Set->Preds) 14111 add(Pred); 14112 return; 14113 } 14114 14115 Preds.push_back(N); 14116 } 14117 14118 PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE, 14119 Loop &L) 14120 : SE(SE), L(L) { 14121 SmallVector<const SCEVPredicate*, 4> Empty; 14122 Preds = std::make_unique<SCEVUnionPredicate>(Empty); 14123 } 14124 14125 void ScalarEvolution::registerUser(const SCEV *User, 14126 ArrayRef<const SCEV *> Ops) { 14127 for (auto *Op : Ops) 14128 // We do not expect that forgetting cached data for SCEVConstants will ever 14129 // open any prospects for sharpening or introduce any correctness issues, 14130 // so we don't bother storing their dependencies. 14131 if (!isa<SCEVConstant>(Op)) 14132 SCEVUsers[Op].insert(User); 14133 } 14134 14135 const SCEV *PredicatedScalarEvolution::getSCEV(Value *V) { 14136 const SCEV *Expr = SE.getSCEV(V); 14137 RewriteEntry &Entry = RewriteMap[Expr]; 14138 14139 // If we already have an entry and the version matches, return it. 14140 if (Entry.second && Generation == Entry.first) 14141 return Entry.second; 14142 14143 // We found an entry but it's stale. Rewrite the stale entry 14144 // according to the current predicate. 14145 if (Entry.second) 14146 Expr = Entry.second; 14147 14148 const SCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, *Preds); 14149 Entry = {Generation, NewSCEV}; 14150 14151 return NewSCEV; 14152 } 14153 14154 const SCEV *PredicatedScalarEvolution::getBackedgeTakenCount() { 14155 if (!BackedgeCount) { 14156 SmallVector<const SCEVPredicate *, 4> Preds; 14157 BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, Preds); 14158 for (auto *P : Preds) 14159 addPredicate(*P); 14160 } 14161 return BackedgeCount; 14162 } 14163 14164 void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) { 14165 if (Preds->implies(&Pred)) 14166 return; 14167 14168 auto &OldPreds = Preds->getPredicates(); 14169 SmallVector<const SCEVPredicate*, 4> NewPreds(OldPreds.begin(), OldPreds.end()); 14170 NewPreds.push_back(&Pred); 14171 Preds = std::make_unique<SCEVUnionPredicate>(NewPreds); 14172 updateGeneration(); 14173 } 14174 14175 const SCEVPredicate &PredicatedScalarEvolution::getPredicate() const { 14176 return *Preds; 14177 } 14178 14179 void PredicatedScalarEvolution::updateGeneration() { 14180 // If the generation number wrapped recompute everything. 14181 if (++Generation == 0) { 14182 for (auto &II : RewriteMap) { 14183 const SCEV *Rewritten = II.second.second; 14184 II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, *Preds)}; 14185 } 14186 } 14187 } 14188 14189 void PredicatedScalarEvolution::setNoOverflow( 14190 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) { 14191 const SCEV *Expr = getSCEV(V); 14192 const auto *AR = cast<SCEVAddRecExpr>(Expr); 14193 14194 auto ImpliedFlags = SCEVWrapPredicate::getImpliedFlags(AR, SE); 14195 14196 // Clear the statically implied flags. 14197 Flags = SCEVWrapPredicate::clearFlags(Flags, ImpliedFlags); 14198 addPredicate(*SE.getWrapPredicate(AR, Flags)); 14199 14200 auto II = FlagsMap.insert({V, Flags}); 14201 if (!II.second) 14202 II.first->second = SCEVWrapPredicate::setFlags(Flags, II.first->second); 14203 } 14204 14205 bool PredicatedScalarEvolution::hasNoOverflow( 14206 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) { 14207 const SCEV *Expr = getSCEV(V); 14208 const auto *AR = cast<SCEVAddRecExpr>(Expr); 14209 14210 Flags = SCEVWrapPredicate::clearFlags( 14211 Flags, SCEVWrapPredicate::getImpliedFlags(AR, SE)); 14212 14213 auto II = FlagsMap.find(V); 14214 14215 if (II != FlagsMap.end()) 14216 Flags = SCEVWrapPredicate::clearFlags(Flags, II->second); 14217 14218 return Flags == SCEVWrapPredicate::IncrementAnyWrap; 14219 } 14220 14221 const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) { 14222 const SCEV *Expr = this->getSCEV(V); 14223 SmallPtrSet<const SCEVPredicate *, 4> NewPreds; 14224 auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, NewPreds); 14225 14226 if (!New) 14227 return nullptr; 14228 14229 for (auto *P : NewPreds) 14230 addPredicate(*P); 14231 14232 RewriteMap[SE.getSCEV(V)] = {Generation, New}; 14233 return New; 14234 } 14235 14236 PredicatedScalarEvolution::PredicatedScalarEvolution( 14237 const PredicatedScalarEvolution &Init) 14238 : RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L), 14239 Preds(std::make_unique<SCEVUnionPredicate>(Init.Preds->getPredicates())), 14240 Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) { 14241 for (auto I : Init.FlagsMap) 14242 FlagsMap.insert(I); 14243 } 14244 14245 void PredicatedScalarEvolution::print(raw_ostream &OS, unsigned Depth) const { 14246 // For each block. 14247 for (auto *BB : L.getBlocks()) 14248 for (auto &I : *BB) { 14249 if (!SE.isSCEVable(I.getType())) 14250 continue; 14251 14252 auto *Expr = SE.getSCEV(&I); 14253 auto II = RewriteMap.find(Expr); 14254 14255 if (II == RewriteMap.end()) 14256 continue; 14257 14258 // Don't print things that are not interesting. 14259 if (II->second.second == Expr) 14260 continue; 14261 14262 OS.indent(Depth) << "[PSE]" << I << ":\n"; 14263 OS.indent(Depth + 2) << *Expr << "\n"; 14264 OS.indent(Depth + 2) << "--> " << *II->second.second << "\n"; 14265 } 14266 } 14267 14268 // Match the mathematical pattern A - (A / B) * B, where A and B can be 14269 // arbitrary expressions. Also match zext (trunc A to iB) to iY, which is used 14270 // for URem with constant power-of-2 second operands. 14271 // It's not always easy, as A and B can be folded (imagine A is X / 2, and B is 14272 // 4, A / B becomes X / 8). 14273 bool ScalarEvolution::matchURem(const SCEV *Expr, const SCEV *&LHS, 14274 const SCEV *&RHS) { 14275 // Try to match 'zext (trunc A to iB) to iY', which is used 14276 // for URem with constant power-of-2 second operands. Make sure the size of 14277 // the operand A matches the size of the whole expressions. 14278 if (const auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(Expr)) 14279 if (const auto *Trunc = dyn_cast<SCEVTruncateExpr>(ZExt->getOperand(0))) { 14280 LHS = Trunc->getOperand(); 14281 // Bail out if the type of the LHS is larger than the type of the 14282 // expression for now. 14283 if (getTypeSizeInBits(LHS->getType()) > 14284 getTypeSizeInBits(Expr->getType())) 14285 return false; 14286 if (LHS->getType() != Expr->getType()) 14287 LHS = getZeroExtendExpr(LHS, Expr->getType()); 14288 RHS = getConstant(APInt(getTypeSizeInBits(Expr->getType()), 1) 14289 << getTypeSizeInBits(Trunc->getType())); 14290 return true; 14291 } 14292 const auto *Add = dyn_cast<SCEVAddExpr>(Expr); 14293 if (Add == nullptr || Add->getNumOperands() != 2) 14294 return false; 14295 14296 const SCEV *A = Add->getOperand(1); 14297 const auto *Mul = dyn_cast<SCEVMulExpr>(Add->getOperand(0)); 14298 14299 if (Mul == nullptr) 14300 return false; 14301 14302 const auto MatchURemWithDivisor = [&](const SCEV *B) { 14303 // (SomeExpr + (-(SomeExpr / B) * B)). 14304 if (Expr == getURemExpr(A, B)) { 14305 LHS = A; 14306 RHS = B; 14307 return true; 14308 } 14309 return false; 14310 }; 14311 14312 // (SomeExpr + (-1 * (SomeExpr / B) * B)). 14313 if (Mul->getNumOperands() == 3 && isa<SCEVConstant>(Mul->getOperand(0))) 14314 return MatchURemWithDivisor(Mul->getOperand(1)) || 14315 MatchURemWithDivisor(Mul->getOperand(2)); 14316 14317 // (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)). 14318 if (Mul->getNumOperands() == 2) 14319 return MatchURemWithDivisor(Mul->getOperand(1)) || 14320 MatchURemWithDivisor(Mul->getOperand(0)) || 14321 MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(1))) || 14322 MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(0))); 14323 return false; 14324 } 14325 14326 const SCEV * 14327 ScalarEvolution::computeSymbolicMaxBackedgeTakenCount(const Loop *L) { 14328 SmallVector<BasicBlock*, 16> ExitingBlocks; 14329 L->getExitingBlocks(ExitingBlocks); 14330 14331 // Form an expression for the maximum exit count possible for this loop. We 14332 // merge the max and exact information to approximate a version of 14333 // getConstantMaxBackedgeTakenCount which isn't restricted to just constants. 14334 SmallVector<const SCEV*, 4> ExitCounts; 14335 for (BasicBlock *ExitingBB : ExitingBlocks) { 14336 const SCEV *ExitCount = getExitCount(L, ExitingBB); 14337 if (isa<SCEVCouldNotCompute>(ExitCount)) 14338 ExitCount = getExitCount(L, ExitingBB, 14339 ScalarEvolution::ConstantMaximum); 14340 if (!isa<SCEVCouldNotCompute>(ExitCount)) { 14341 assert(DT.dominates(ExitingBB, L->getLoopLatch()) && 14342 "We should only have known counts for exiting blocks that " 14343 "dominate latch!"); 14344 ExitCounts.push_back(ExitCount); 14345 } 14346 } 14347 if (ExitCounts.empty()) 14348 return getCouldNotCompute(); 14349 return getUMinFromMismatchedTypes(ExitCounts); 14350 } 14351 14352 /// A rewriter to replace SCEV expressions in Map with the corresponding entry 14353 /// in the map. It skips AddRecExpr because we cannot guarantee that the 14354 /// replacement is loop invariant in the loop of the AddRec. 14355 /// 14356 /// At the moment only rewriting SCEVUnknown and SCEVZeroExtendExpr is 14357 /// supported. 14358 class SCEVLoopGuardRewriter : public SCEVRewriteVisitor<SCEVLoopGuardRewriter> { 14359 const DenseMap<const SCEV *, const SCEV *> ⤅ 14360 14361 public: 14362 SCEVLoopGuardRewriter(ScalarEvolution &SE, 14363 DenseMap<const SCEV *, const SCEV *> &M) 14364 : SCEVRewriteVisitor(SE), Map(M) {} 14365 14366 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; } 14367 14368 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 14369 auto I = Map.find(Expr); 14370 if (I == Map.end()) 14371 return Expr; 14372 return I->second; 14373 } 14374 14375 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { 14376 auto I = Map.find(Expr); 14377 if (I == Map.end()) 14378 return SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitZeroExtendExpr( 14379 Expr); 14380 return I->second; 14381 } 14382 }; 14383 14384 const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) { 14385 SmallVector<const SCEV *> ExprsToRewrite; 14386 auto CollectCondition = [&](ICmpInst::Predicate Predicate, const SCEV *LHS, 14387 const SCEV *RHS, 14388 DenseMap<const SCEV *, const SCEV *> 14389 &RewriteMap) { 14390 // WARNING: It is generally unsound to apply any wrap flags to the proposed 14391 // replacement SCEV which isn't directly implied by the structure of that 14392 // SCEV. In particular, using contextual facts to imply flags is *NOT* 14393 // legal. See the scoping rules for flags in the header to understand why. 14394 14395 // If LHS is a constant, apply information to the other expression. 14396 if (isa<SCEVConstant>(LHS)) { 14397 std::swap(LHS, RHS); 14398 Predicate = CmpInst::getSwappedPredicate(Predicate); 14399 } 14400 14401 // Check for a condition of the form (-C1 + X < C2). InstCombine will 14402 // create this form when combining two checks of the form (X u< C2 + C1) and 14403 // (X >=u C1). 14404 auto MatchRangeCheckIdiom = [this, Predicate, LHS, RHS, &RewriteMap, 14405 &ExprsToRewrite]() { 14406 auto *AddExpr = dyn_cast<SCEVAddExpr>(LHS); 14407 if (!AddExpr || AddExpr->getNumOperands() != 2) 14408 return false; 14409 14410 auto *C1 = dyn_cast<SCEVConstant>(AddExpr->getOperand(0)); 14411 auto *LHSUnknown = dyn_cast<SCEVUnknown>(AddExpr->getOperand(1)); 14412 auto *C2 = dyn_cast<SCEVConstant>(RHS); 14413 if (!C1 || !C2 || !LHSUnknown) 14414 return false; 14415 14416 auto ExactRegion = 14417 ConstantRange::makeExactICmpRegion(Predicate, C2->getAPInt()) 14418 .sub(C1->getAPInt()); 14419 14420 // Bail out, unless we have a non-wrapping, monotonic range. 14421 if (ExactRegion.isWrappedSet() || ExactRegion.isFullSet()) 14422 return false; 14423 auto I = RewriteMap.find(LHSUnknown); 14424 const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHSUnknown; 14425 RewriteMap[LHSUnknown] = getUMaxExpr( 14426 getConstant(ExactRegion.getUnsignedMin()), 14427 getUMinExpr(RewrittenLHS, getConstant(ExactRegion.getUnsignedMax()))); 14428 ExprsToRewrite.push_back(LHSUnknown); 14429 return true; 14430 }; 14431 if (MatchRangeCheckIdiom()) 14432 return; 14433 14434 // If we have LHS == 0, check if LHS is computing a property of some unknown 14435 // SCEV %v which we can rewrite %v to express explicitly. 14436 const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS); 14437 if (Predicate == CmpInst::ICMP_EQ && RHSC && 14438 RHSC->getValue()->isNullValue()) { 14439 // If LHS is A % B, i.e. A % B == 0, rewrite A to (A /u B) * B to 14440 // explicitly express that. 14441 const SCEV *URemLHS = nullptr; 14442 const SCEV *URemRHS = nullptr; 14443 if (matchURem(LHS, URemLHS, URemRHS)) { 14444 if (const SCEVUnknown *LHSUnknown = dyn_cast<SCEVUnknown>(URemLHS)) { 14445 auto Multiple = getMulExpr(getUDivExpr(URemLHS, URemRHS), URemRHS); 14446 RewriteMap[LHSUnknown] = Multiple; 14447 ExprsToRewrite.push_back(LHSUnknown); 14448 return; 14449 } 14450 } 14451 } 14452 14453 // Do not apply information for constants or if RHS contains an AddRec. 14454 if (isa<SCEVConstant>(LHS) || containsAddRecurrence(RHS)) 14455 return; 14456 14457 // If RHS is SCEVUnknown, make sure the information is applied to it. 14458 if (!isa<SCEVUnknown>(LHS) && isa<SCEVUnknown>(RHS)) { 14459 std::swap(LHS, RHS); 14460 Predicate = CmpInst::getSwappedPredicate(Predicate); 14461 } 14462 14463 // Limit to expressions that can be rewritten. 14464 if (!isa<SCEVUnknown>(LHS) && !isa<SCEVZeroExtendExpr>(LHS)) 14465 return; 14466 14467 // Check whether LHS has already been rewritten. In that case we want to 14468 // chain further rewrites onto the already rewritten value. 14469 auto I = RewriteMap.find(LHS); 14470 const SCEV *RewrittenLHS = I != RewriteMap.end() ? I->second : LHS; 14471 14472 const SCEV *RewrittenRHS = nullptr; 14473 switch (Predicate) { 14474 case CmpInst::ICMP_ULT: 14475 RewrittenRHS = 14476 getUMinExpr(RewrittenLHS, getMinusSCEV(RHS, getOne(RHS->getType()))); 14477 break; 14478 case CmpInst::ICMP_SLT: 14479 RewrittenRHS = 14480 getSMinExpr(RewrittenLHS, getMinusSCEV(RHS, getOne(RHS->getType()))); 14481 break; 14482 case CmpInst::ICMP_ULE: 14483 RewrittenRHS = getUMinExpr(RewrittenLHS, RHS); 14484 break; 14485 case CmpInst::ICMP_SLE: 14486 RewrittenRHS = getSMinExpr(RewrittenLHS, RHS); 14487 break; 14488 case CmpInst::ICMP_UGT: 14489 RewrittenRHS = 14490 getUMaxExpr(RewrittenLHS, getAddExpr(RHS, getOne(RHS->getType()))); 14491 break; 14492 case CmpInst::ICMP_SGT: 14493 RewrittenRHS = 14494 getSMaxExpr(RewrittenLHS, getAddExpr(RHS, getOne(RHS->getType()))); 14495 break; 14496 case CmpInst::ICMP_UGE: 14497 RewrittenRHS = getUMaxExpr(RewrittenLHS, RHS); 14498 break; 14499 case CmpInst::ICMP_SGE: 14500 RewrittenRHS = getSMaxExpr(RewrittenLHS, RHS); 14501 break; 14502 case CmpInst::ICMP_EQ: 14503 if (isa<SCEVConstant>(RHS)) 14504 RewrittenRHS = RHS; 14505 break; 14506 case CmpInst::ICMP_NE: 14507 if (isa<SCEVConstant>(RHS) && 14508 cast<SCEVConstant>(RHS)->getValue()->isNullValue()) 14509 RewrittenRHS = getUMaxExpr(RewrittenLHS, getOne(RHS->getType())); 14510 break; 14511 default: 14512 break; 14513 } 14514 14515 if (RewrittenRHS) { 14516 RewriteMap[LHS] = RewrittenRHS; 14517 if (LHS == RewrittenLHS) 14518 ExprsToRewrite.push_back(LHS); 14519 } 14520 }; 14521 14522 SmallVector<std::pair<Value *, bool>> Terms; 14523 // First, collect information from assumptions dominating the loop. 14524 for (auto &AssumeVH : AC.assumptions()) { 14525 if (!AssumeVH) 14526 continue; 14527 auto *AssumeI = cast<CallInst>(AssumeVH); 14528 if (!DT.dominates(AssumeI, L->getHeader())) 14529 continue; 14530 Terms.emplace_back(AssumeI->getOperand(0), true); 14531 } 14532 14533 // Second, collect conditions from dominating branches. Starting at the loop 14534 // predecessor, climb up the predecessor chain, as long as there are 14535 // predecessors that can be found that have unique successors leading to the 14536 // original header. 14537 // TODO: share this logic with isLoopEntryGuardedByCond. 14538 for (std::pair<const BasicBlock *, const BasicBlock *> Pair( 14539 L->getLoopPredecessor(), L->getHeader()); 14540 Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { 14541 14542 const BranchInst *LoopEntryPredicate = 14543 dyn_cast<BranchInst>(Pair.first->getTerminator()); 14544 if (!LoopEntryPredicate || LoopEntryPredicate->isUnconditional()) 14545 continue; 14546 14547 Terms.emplace_back(LoopEntryPredicate->getCondition(), 14548 LoopEntryPredicate->getSuccessor(0) == Pair.second); 14549 } 14550 14551 // Now apply the information from the collected conditions to RewriteMap. 14552 // Conditions are processed in reverse order, so the earliest conditions is 14553 // processed first. This ensures the SCEVs with the shortest dependency chains 14554 // are constructed first. 14555 DenseMap<const SCEV *, const SCEV *> RewriteMap; 14556 for (auto &E : reverse(Terms)) { 14557 bool EnterIfTrue = E.second; 14558 SmallVector<Value *, 8> Worklist; 14559 SmallPtrSet<Value *, 8> Visited; 14560 Worklist.push_back(E.first); 14561 while (!Worklist.empty()) { 14562 Value *Cond = Worklist.pop_back_val(); 14563 if (!Visited.insert(Cond).second) 14564 continue; 14565 14566 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) { 14567 auto Predicate = 14568 EnterIfTrue ? Cmp->getPredicate() : Cmp->getInversePredicate(); 14569 const auto *LHS = getSCEV(Cmp->getOperand(0)); 14570 const auto *RHS = getSCEV(Cmp->getOperand(1)); 14571 CollectCondition(Predicate, LHS, RHS, RewriteMap); 14572 continue; 14573 } 14574 14575 Value *L, *R; 14576 if (EnterIfTrue ? match(Cond, m_LogicalAnd(m_Value(L), m_Value(R))) 14577 : match(Cond, m_LogicalOr(m_Value(L), m_Value(R)))) { 14578 Worklist.push_back(L); 14579 Worklist.push_back(R); 14580 } 14581 } 14582 } 14583 14584 if (RewriteMap.empty()) 14585 return Expr; 14586 14587 // Now that all rewrite information is collect, rewrite the collected 14588 // expressions with the information in the map. This applies information to 14589 // sub-expressions. 14590 if (ExprsToRewrite.size() > 1) { 14591 for (const SCEV *Expr : ExprsToRewrite) { 14592 const SCEV *RewriteTo = RewriteMap[Expr]; 14593 RewriteMap.erase(Expr); 14594 SCEVLoopGuardRewriter Rewriter(*this, RewriteMap); 14595 RewriteMap.insert({Expr, Rewriter.visit(RewriteTo)}); 14596 } 14597 } 14598 14599 SCEVLoopGuardRewriter Rewriter(*this, RewriteMap); 14600 return Rewriter.visit(Expr); 14601 } 14602