1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains the implementation of the scalar evolution analysis 11 // engine, which is used primarily to analyze expressions involving induction 12 // variables in loops. 13 // 14 // There are several aspects to this library. First is the representation of 15 // scalar expressions, which are represented as subclasses of the SCEV class. 16 // These classes are used to represent certain types of subexpressions that we 17 // can handle. We only create one SCEV of a particular shape, so 18 // pointer-comparisons for equality are legal. 19 // 20 // One important aspect of the SCEV objects is that they are never cyclic, even 21 // if there is a cycle in the dataflow for an expression (ie, a PHI node). If 22 // the PHI node is one of the idioms that we can represent (e.g., a polynomial 23 // recurrence) then we represent it directly as a recurrence node, otherwise we 24 // represent it as a SCEVUnknown node. 25 // 26 // In addition to being able to represent expressions of various types, we also 27 // have folders that are used to build the *canonical* representation for a 28 // particular expression. These folders are capable of using a variety of 29 // rewrite rules to simplify the expressions. 30 // 31 // Once the folders are defined, we can implement the more interesting 32 // higher-level code, such as the code that recognizes PHI nodes of various 33 // types, computes the execution count of a loop, etc. 34 // 35 // TODO: We should use these routines and value representations to implement 36 // dependence analysis! 37 // 38 //===----------------------------------------------------------------------===// 39 // 40 // There are several good references for the techniques used in this analysis. 41 // 42 // Chains of recurrences -- a method to expedite the evaluation 43 // of closed-form functions 44 // Olaf Bachmann, Paul S. Wang, Eugene V. Zima 45 // 46 // On computational properties of chains of recurrences 47 // Eugene V. Zima 48 // 49 // Symbolic Evaluation of Chains of Recurrences for Loop Optimization 50 // Robert A. van Engelen 51 // 52 // Efficient Symbolic Analysis for Optimizing Compilers 53 // Robert A. van Engelen 54 // 55 // Using the chains of recurrences algebra for data dependence testing and 56 // induction variable substitution 57 // MS Thesis, Johnie Birch 58 // 59 //===----------------------------------------------------------------------===// 60 61 #include "llvm/Analysis/ScalarEvolution.h" 62 #include "llvm/ADT/Optional.h" 63 #include "llvm/ADT/STLExtras.h" 64 #include "llvm/ADT/SmallPtrSet.h" 65 #include "llvm/ADT/Statistic.h" 66 #include "llvm/Analysis/AssumptionCache.h" 67 #include "llvm/Analysis/ConstantFolding.h" 68 #include "llvm/Analysis/InstructionSimplify.h" 69 #include "llvm/Analysis/LoopInfo.h" 70 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 71 #include "llvm/Analysis/TargetLibraryInfo.h" 72 #include "llvm/Analysis/ValueTracking.h" 73 #include "llvm/IR/ConstantRange.h" 74 #include "llvm/IR/Constants.h" 75 #include "llvm/IR/DataLayout.h" 76 #include "llvm/IR/DerivedTypes.h" 77 #include "llvm/IR/Dominators.h" 78 #include "llvm/IR/GetElementPtrTypeIterator.h" 79 #include "llvm/IR/GlobalAlias.h" 80 #include "llvm/IR/GlobalVariable.h" 81 #include "llvm/IR/InstIterator.h" 82 #include "llvm/IR/Instructions.h" 83 #include "llvm/IR/LLVMContext.h" 84 #include "llvm/IR/Metadata.h" 85 #include "llvm/IR/Operator.h" 86 #include "llvm/IR/PatternMatch.h" 87 #include "llvm/Support/CommandLine.h" 88 #include "llvm/Support/Debug.h" 89 #include "llvm/Support/ErrorHandling.h" 90 #include "llvm/Support/MathExtras.h" 91 #include "llvm/Support/raw_ostream.h" 92 #include "llvm/Support/SaveAndRestore.h" 93 #include <algorithm> 94 using namespace llvm; 95 96 #define DEBUG_TYPE "scalar-evolution" 97 98 STATISTIC(NumArrayLenItCounts, 99 "Number of trip counts computed with array length"); 100 STATISTIC(NumTripCountsComputed, 101 "Number of loops with predictable loop counts"); 102 STATISTIC(NumTripCountsNotComputed, 103 "Number of loops without predictable loop counts"); 104 STATISTIC(NumBruteForceTripCountsComputed, 105 "Number of loops with trip counts computed by force"); 106 107 static cl::opt<unsigned> 108 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, 109 cl::desc("Maximum number of iterations SCEV will " 110 "symbolically execute a constant " 111 "derived loop"), 112 cl::init(100)); 113 114 // FIXME: Enable this with XDEBUG when the test suite is clean. 115 static cl::opt<bool> 116 VerifySCEV("verify-scev", 117 cl::desc("Verify ScalarEvolution's backedge taken counts (slow)")); 118 static cl::opt<bool> 119 VerifySCEVMap("verify-scev-maps", 120 cl::desc("Verify no dangling value in ScalarEvolution's" 121 "ExprValueMap (slow)")); 122 123 //===----------------------------------------------------------------------===// 124 // SCEV class definitions 125 //===----------------------------------------------------------------------===// 126 127 //===----------------------------------------------------------------------===// 128 // Implementation of the SCEV class. 129 // 130 131 LLVM_DUMP_METHOD 132 void SCEV::dump() const { 133 print(dbgs()); 134 dbgs() << '\n'; 135 } 136 137 void SCEV::print(raw_ostream &OS) const { 138 switch (static_cast<SCEVTypes>(getSCEVType())) { 139 case scConstant: 140 cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false); 141 return; 142 case scTruncate: { 143 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this); 144 const SCEV *Op = Trunc->getOperand(); 145 OS << "(trunc " << *Op->getType() << " " << *Op << " to " 146 << *Trunc->getType() << ")"; 147 return; 148 } 149 case scZeroExtend: { 150 const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this); 151 const SCEV *Op = ZExt->getOperand(); 152 OS << "(zext " << *Op->getType() << " " << *Op << " to " 153 << *ZExt->getType() << ")"; 154 return; 155 } 156 case scSignExtend: { 157 const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this); 158 const SCEV *Op = SExt->getOperand(); 159 OS << "(sext " << *Op->getType() << " " << *Op << " to " 160 << *SExt->getType() << ")"; 161 return; 162 } 163 case scAddRecExpr: { 164 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this); 165 OS << "{" << *AR->getOperand(0); 166 for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i) 167 OS << ",+," << *AR->getOperand(i); 168 OS << "}<"; 169 if (AR->hasNoUnsignedWrap()) 170 OS << "nuw><"; 171 if (AR->hasNoSignedWrap()) 172 OS << "nsw><"; 173 if (AR->hasNoSelfWrap() && 174 !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW))) 175 OS << "nw><"; 176 AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false); 177 OS << ">"; 178 return; 179 } 180 case scAddExpr: 181 case scMulExpr: 182 case scUMaxExpr: 183 case scSMaxExpr: { 184 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this); 185 const char *OpStr = nullptr; 186 switch (NAry->getSCEVType()) { 187 case scAddExpr: OpStr = " + "; break; 188 case scMulExpr: OpStr = " * "; break; 189 case scUMaxExpr: OpStr = " umax "; break; 190 case scSMaxExpr: OpStr = " smax "; break; 191 } 192 OS << "("; 193 for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end(); 194 I != E; ++I) { 195 OS << **I; 196 if (std::next(I) != E) 197 OS << OpStr; 198 } 199 OS << ")"; 200 switch (NAry->getSCEVType()) { 201 case scAddExpr: 202 case scMulExpr: 203 if (NAry->hasNoUnsignedWrap()) 204 OS << "<nuw>"; 205 if (NAry->hasNoSignedWrap()) 206 OS << "<nsw>"; 207 } 208 return; 209 } 210 case scUDivExpr: { 211 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this); 212 OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")"; 213 return; 214 } 215 case scUnknown: { 216 const SCEVUnknown *U = cast<SCEVUnknown>(this); 217 Type *AllocTy; 218 if (U->isSizeOf(AllocTy)) { 219 OS << "sizeof(" << *AllocTy << ")"; 220 return; 221 } 222 if (U->isAlignOf(AllocTy)) { 223 OS << "alignof(" << *AllocTy << ")"; 224 return; 225 } 226 227 Type *CTy; 228 Constant *FieldNo; 229 if (U->isOffsetOf(CTy, FieldNo)) { 230 OS << "offsetof(" << *CTy << ", "; 231 FieldNo->printAsOperand(OS, false); 232 OS << ")"; 233 return; 234 } 235 236 // Otherwise just print it normally. 237 U->getValue()->printAsOperand(OS, false); 238 return; 239 } 240 case scCouldNotCompute: 241 OS << "***COULDNOTCOMPUTE***"; 242 return; 243 } 244 llvm_unreachable("Unknown SCEV kind!"); 245 } 246 247 Type *SCEV::getType() const { 248 switch (static_cast<SCEVTypes>(getSCEVType())) { 249 case scConstant: 250 return cast<SCEVConstant>(this)->getType(); 251 case scTruncate: 252 case scZeroExtend: 253 case scSignExtend: 254 return cast<SCEVCastExpr>(this)->getType(); 255 case scAddRecExpr: 256 case scMulExpr: 257 case scUMaxExpr: 258 case scSMaxExpr: 259 return cast<SCEVNAryExpr>(this)->getType(); 260 case scAddExpr: 261 return cast<SCEVAddExpr>(this)->getType(); 262 case scUDivExpr: 263 return cast<SCEVUDivExpr>(this)->getType(); 264 case scUnknown: 265 return cast<SCEVUnknown>(this)->getType(); 266 case scCouldNotCompute: 267 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); 268 } 269 llvm_unreachable("Unknown SCEV kind!"); 270 } 271 272 bool SCEV::isZero() const { 273 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 274 return SC->getValue()->isZero(); 275 return false; 276 } 277 278 bool SCEV::isOne() const { 279 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 280 return SC->getValue()->isOne(); 281 return false; 282 } 283 284 bool SCEV::isAllOnesValue() const { 285 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 286 return SC->getValue()->isAllOnesValue(); 287 return false; 288 } 289 290 /// isNonConstantNegative - Return true if the specified scev is negated, but 291 /// not a constant. 292 bool SCEV::isNonConstantNegative() const { 293 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this); 294 if (!Mul) return false; 295 296 // If there is a constant factor, it will be first. 297 const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0)); 298 if (!SC) return false; 299 300 // Return true if the value is negative, this matches things like (-42 * V). 301 return SC->getAPInt().isNegative(); 302 } 303 304 SCEVCouldNotCompute::SCEVCouldNotCompute() : 305 SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {} 306 307 bool SCEVCouldNotCompute::classof(const SCEV *S) { 308 return S->getSCEVType() == scCouldNotCompute; 309 } 310 311 const SCEV *ScalarEvolution::getConstant(ConstantInt *V) { 312 FoldingSetNodeID ID; 313 ID.AddInteger(scConstant); 314 ID.AddPointer(V); 315 void *IP = nullptr; 316 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 317 SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V); 318 UniqueSCEVs.InsertNode(S, IP); 319 return S; 320 } 321 322 const SCEV *ScalarEvolution::getConstant(const APInt &Val) { 323 return getConstant(ConstantInt::get(getContext(), Val)); 324 } 325 326 const SCEV * 327 ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) { 328 IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); 329 return getConstant(ConstantInt::get(ITy, V, isSigned)); 330 } 331 332 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, 333 unsigned SCEVTy, const SCEV *op, Type *ty) 334 : SCEV(ID, SCEVTy), Op(op), Ty(ty) {} 335 336 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, 337 const SCEV *op, Type *ty) 338 : SCEVCastExpr(ID, scTruncate, op, ty) { 339 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && 340 (Ty->isIntegerTy() || Ty->isPointerTy()) && 341 "Cannot truncate non-integer value!"); 342 } 343 344 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, 345 const SCEV *op, Type *ty) 346 : SCEVCastExpr(ID, scZeroExtend, op, ty) { 347 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && 348 (Ty->isIntegerTy() || Ty->isPointerTy()) && 349 "Cannot zero extend non-integer value!"); 350 } 351 352 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, 353 const SCEV *op, Type *ty) 354 : SCEVCastExpr(ID, scSignExtend, op, ty) { 355 assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) && 356 (Ty->isIntegerTy() || Ty->isPointerTy()) && 357 "Cannot sign extend non-integer value!"); 358 } 359 360 void SCEVUnknown::deleted() { 361 // Clear this SCEVUnknown from various maps. 362 SE->forgetMemoizedResults(this); 363 364 // Remove this SCEVUnknown from the uniquing map. 365 SE->UniqueSCEVs.RemoveNode(this); 366 367 // Release the value. 368 setValPtr(nullptr); 369 } 370 371 void SCEVUnknown::allUsesReplacedWith(Value *New) { 372 // Clear this SCEVUnknown from various maps. 373 SE->forgetMemoizedResults(this); 374 375 // Remove this SCEVUnknown from the uniquing map. 376 SE->UniqueSCEVs.RemoveNode(this); 377 378 // Update this SCEVUnknown to point to the new value. This is needed 379 // because there may still be outstanding SCEVs which still point to 380 // this SCEVUnknown. 381 setValPtr(New); 382 } 383 384 bool SCEVUnknown::isSizeOf(Type *&AllocTy) const { 385 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) 386 if (VCE->getOpcode() == Instruction::PtrToInt) 387 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) 388 if (CE->getOpcode() == Instruction::GetElementPtr && 389 CE->getOperand(0)->isNullValue() && 390 CE->getNumOperands() == 2) 391 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1))) 392 if (CI->isOne()) { 393 AllocTy = cast<PointerType>(CE->getOperand(0)->getType()) 394 ->getElementType(); 395 return true; 396 } 397 398 return false; 399 } 400 401 bool SCEVUnknown::isAlignOf(Type *&AllocTy) const { 402 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) 403 if (VCE->getOpcode() == Instruction::PtrToInt) 404 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) 405 if (CE->getOpcode() == Instruction::GetElementPtr && 406 CE->getOperand(0)->isNullValue()) { 407 Type *Ty = 408 cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); 409 if (StructType *STy = dyn_cast<StructType>(Ty)) 410 if (!STy->isPacked() && 411 CE->getNumOperands() == 3 && 412 CE->getOperand(1)->isNullValue()) { 413 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2))) 414 if (CI->isOne() && 415 STy->getNumElements() == 2 && 416 STy->getElementType(0)->isIntegerTy(1)) { 417 AllocTy = STy->getElementType(1); 418 return true; 419 } 420 } 421 } 422 423 return false; 424 } 425 426 bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const { 427 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue())) 428 if (VCE->getOpcode() == Instruction::PtrToInt) 429 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0))) 430 if (CE->getOpcode() == Instruction::GetElementPtr && 431 CE->getNumOperands() == 3 && 432 CE->getOperand(0)->isNullValue() && 433 CE->getOperand(1)->isNullValue()) { 434 Type *Ty = 435 cast<PointerType>(CE->getOperand(0)->getType())->getElementType(); 436 // Ignore vector types here so that ScalarEvolutionExpander doesn't 437 // emit getelementptrs that index into vectors. 438 if (Ty->isStructTy() || Ty->isArrayTy()) { 439 CTy = Ty; 440 FieldNo = CE->getOperand(2); 441 return true; 442 } 443 } 444 445 return false; 446 } 447 448 //===----------------------------------------------------------------------===// 449 // SCEV Utilities 450 //===----------------------------------------------------------------------===// 451 452 namespace { 453 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less 454 /// than the complexity of the RHS. This comparator is used to canonicalize 455 /// expressions. 456 class SCEVComplexityCompare { 457 const LoopInfo *const LI; 458 public: 459 explicit SCEVComplexityCompare(const LoopInfo *li) : LI(li) {} 460 461 // Return true or false if LHS is less than, or at least RHS, respectively. 462 bool operator()(const SCEV *LHS, const SCEV *RHS) const { 463 return compare(LHS, RHS) < 0; 464 } 465 466 // Return negative, zero, or positive, if LHS is less than, equal to, or 467 // greater than RHS, respectively. A three-way result allows recursive 468 // comparisons to be more efficient. 469 int compare(const SCEV *LHS, const SCEV *RHS) const { 470 // Fast-path: SCEVs are uniqued so we can do a quick equality check. 471 if (LHS == RHS) 472 return 0; 473 474 // Primarily, sort the SCEVs by their getSCEVType(). 475 unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType(); 476 if (LType != RType) 477 return (int)LType - (int)RType; 478 479 // Aside from the getSCEVType() ordering, the particular ordering 480 // isn't very important except that it's beneficial to be consistent, 481 // so that (a + b) and (b + a) don't end up as different expressions. 482 switch (static_cast<SCEVTypes>(LType)) { 483 case scUnknown: { 484 const SCEVUnknown *LU = cast<SCEVUnknown>(LHS); 485 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); 486 487 // Sort SCEVUnknown values with some loose heuristics. TODO: This is 488 // not as complete as it could be. 489 const Value *LV = LU->getValue(), *RV = RU->getValue(); 490 491 // Order pointer values after integer values. This helps SCEVExpander 492 // form GEPs. 493 bool LIsPointer = LV->getType()->isPointerTy(), 494 RIsPointer = RV->getType()->isPointerTy(); 495 if (LIsPointer != RIsPointer) 496 return (int)LIsPointer - (int)RIsPointer; 497 498 // Compare getValueID values. 499 unsigned LID = LV->getValueID(), 500 RID = RV->getValueID(); 501 if (LID != RID) 502 return (int)LID - (int)RID; 503 504 // Sort arguments by their position. 505 if (const Argument *LA = dyn_cast<Argument>(LV)) { 506 const Argument *RA = cast<Argument>(RV); 507 unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo(); 508 return (int)LArgNo - (int)RArgNo; 509 } 510 511 // For instructions, compare their loop depth, and their operand 512 // count. This is pretty loose. 513 if (const Instruction *LInst = dyn_cast<Instruction>(LV)) { 514 const Instruction *RInst = cast<Instruction>(RV); 515 516 // Compare loop depths. 517 const BasicBlock *LParent = LInst->getParent(), 518 *RParent = RInst->getParent(); 519 if (LParent != RParent) { 520 unsigned LDepth = LI->getLoopDepth(LParent), 521 RDepth = LI->getLoopDepth(RParent); 522 if (LDepth != RDepth) 523 return (int)LDepth - (int)RDepth; 524 } 525 526 // Compare the number of operands. 527 unsigned LNumOps = LInst->getNumOperands(), 528 RNumOps = RInst->getNumOperands(); 529 return (int)LNumOps - (int)RNumOps; 530 } 531 532 return 0; 533 } 534 535 case scConstant: { 536 const SCEVConstant *LC = cast<SCEVConstant>(LHS); 537 const SCEVConstant *RC = cast<SCEVConstant>(RHS); 538 539 // Compare constant values. 540 const APInt &LA = LC->getAPInt(); 541 const APInt &RA = RC->getAPInt(); 542 unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth(); 543 if (LBitWidth != RBitWidth) 544 return (int)LBitWidth - (int)RBitWidth; 545 return LA.ult(RA) ? -1 : 1; 546 } 547 548 case scAddRecExpr: { 549 const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS); 550 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); 551 552 // Compare addrec loop depths. 553 const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop(); 554 if (LLoop != RLoop) { 555 unsigned LDepth = LLoop->getLoopDepth(), 556 RDepth = RLoop->getLoopDepth(); 557 if (LDepth != RDepth) 558 return (int)LDepth - (int)RDepth; 559 } 560 561 // Addrec complexity grows with operand count. 562 unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands(); 563 if (LNumOps != RNumOps) 564 return (int)LNumOps - (int)RNumOps; 565 566 // Lexicographically compare. 567 for (unsigned i = 0; i != LNumOps; ++i) { 568 long X = compare(LA->getOperand(i), RA->getOperand(i)); 569 if (X != 0) 570 return X; 571 } 572 573 return 0; 574 } 575 576 case scAddExpr: 577 case scMulExpr: 578 case scSMaxExpr: 579 case scUMaxExpr: { 580 const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS); 581 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); 582 583 // Lexicographically compare n-ary expressions. 584 unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands(); 585 if (LNumOps != RNumOps) 586 return (int)LNumOps - (int)RNumOps; 587 588 for (unsigned i = 0; i != LNumOps; ++i) { 589 if (i >= RNumOps) 590 return 1; 591 long X = compare(LC->getOperand(i), RC->getOperand(i)); 592 if (X != 0) 593 return X; 594 } 595 return (int)LNumOps - (int)RNumOps; 596 } 597 598 case scUDivExpr: { 599 const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS); 600 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); 601 602 // Lexicographically compare udiv expressions. 603 long X = compare(LC->getLHS(), RC->getLHS()); 604 if (X != 0) 605 return X; 606 return compare(LC->getRHS(), RC->getRHS()); 607 } 608 609 case scTruncate: 610 case scZeroExtend: 611 case scSignExtend: { 612 const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS); 613 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); 614 615 // Compare cast expressions by operand. 616 return compare(LC->getOperand(), RC->getOperand()); 617 } 618 619 case scCouldNotCompute: 620 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); 621 } 622 llvm_unreachable("Unknown SCEV kind!"); 623 } 624 }; 625 } // end anonymous namespace 626 627 /// GroupByComplexity - Given a list of SCEV objects, order them by their 628 /// complexity, and group objects of the same complexity together by value. 629 /// When this routine is finished, we know that any duplicates in the vector are 630 /// consecutive and that complexity is monotonically increasing. 631 /// 632 /// Note that we go take special precautions to ensure that we get deterministic 633 /// results from this routine. In other words, we don't want the results of 634 /// this to depend on where the addresses of various SCEV objects happened to 635 /// land in memory. 636 /// 637 static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops, 638 LoopInfo *LI) { 639 if (Ops.size() < 2) return; // Noop 640 if (Ops.size() == 2) { 641 // This is the common case, which also happens to be trivially simple. 642 // Special case it. 643 const SCEV *&LHS = Ops[0], *&RHS = Ops[1]; 644 if (SCEVComplexityCompare(LI)(RHS, LHS)) 645 std::swap(LHS, RHS); 646 return; 647 } 648 649 // Do the rough sort by complexity. 650 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); 651 652 // Now that we are sorted by complexity, group elements of the same 653 // complexity. Note that this is, at worst, N^2, but the vector is likely to 654 // be extremely short in practice. Note that we take this approach because we 655 // do not want to depend on the addresses of the objects we are grouping. 656 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { 657 const SCEV *S = Ops[i]; 658 unsigned Complexity = S->getSCEVType(); 659 660 // If there are any objects of the same complexity and same value as this 661 // one, group them. 662 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { 663 if (Ops[j] == S) { // Found a duplicate. 664 // Move it to immediately after i'th element. 665 std::swap(Ops[i+1], Ops[j]); 666 ++i; // no need to rescan it. 667 if (i == e-2) return; // Done! 668 } 669 } 670 } 671 } 672 673 // Returns the size of the SCEV S. 674 static inline int sizeOfSCEV(const SCEV *S) { 675 struct FindSCEVSize { 676 int Size; 677 FindSCEVSize() : Size(0) {} 678 679 bool follow(const SCEV *S) { 680 ++Size; 681 // Keep looking at all operands of S. 682 return true; 683 } 684 bool isDone() const { 685 return false; 686 } 687 }; 688 689 FindSCEVSize F; 690 SCEVTraversal<FindSCEVSize> ST(F); 691 ST.visitAll(S); 692 return F.Size; 693 } 694 695 namespace { 696 697 struct SCEVDivision : public SCEVVisitor<SCEVDivision, void> { 698 public: 699 // Computes the Quotient and Remainder of the division of Numerator by 700 // Denominator. 701 static void divide(ScalarEvolution &SE, const SCEV *Numerator, 702 const SCEV *Denominator, const SCEV **Quotient, 703 const SCEV **Remainder) { 704 assert(Numerator && Denominator && "Uninitialized SCEV"); 705 706 SCEVDivision D(SE, Numerator, Denominator); 707 708 // Check for the trivial case here to avoid having to check for it in the 709 // rest of the code. 710 if (Numerator == Denominator) { 711 *Quotient = D.One; 712 *Remainder = D.Zero; 713 return; 714 } 715 716 if (Numerator->isZero()) { 717 *Quotient = D.Zero; 718 *Remainder = D.Zero; 719 return; 720 } 721 722 // A simple case when N/1. The quotient is N. 723 if (Denominator->isOne()) { 724 *Quotient = Numerator; 725 *Remainder = D.Zero; 726 return; 727 } 728 729 // Split the Denominator when it is a product. 730 if (const SCEVMulExpr *T = dyn_cast<const SCEVMulExpr>(Denominator)) { 731 const SCEV *Q, *R; 732 *Quotient = Numerator; 733 for (const SCEV *Op : T->operands()) { 734 divide(SE, *Quotient, Op, &Q, &R); 735 *Quotient = Q; 736 737 // Bail out when the Numerator is not divisible by one of the terms of 738 // the Denominator. 739 if (!R->isZero()) { 740 *Quotient = D.Zero; 741 *Remainder = Numerator; 742 return; 743 } 744 } 745 *Remainder = D.Zero; 746 return; 747 } 748 749 D.visit(Numerator); 750 *Quotient = D.Quotient; 751 *Remainder = D.Remainder; 752 } 753 754 // Except in the trivial case described above, we do not know how to divide 755 // Expr by Denominator for the following functions with empty implementation. 756 void visitTruncateExpr(const SCEVTruncateExpr *Numerator) {} 757 void visitZeroExtendExpr(const SCEVZeroExtendExpr *Numerator) {} 758 void visitSignExtendExpr(const SCEVSignExtendExpr *Numerator) {} 759 void visitUDivExpr(const SCEVUDivExpr *Numerator) {} 760 void visitSMaxExpr(const SCEVSMaxExpr *Numerator) {} 761 void visitUMaxExpr(const SCEVUMaxExpr *Numerator) {} 762 void visitUnknown(const SCEVUnknown *Numerator) {} 763 void visitCouldNotCompute(const SCEVCouldNotCompute *Numerator) {} 764 765 void visitConstant(const SCEVConstant *Numerator) { 766 if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) { 767 APInt NumeratorVal = Numerator->getAPInt(); 768 APInt DenominatorVal = D->getAPInt(); 769 uint32_t NumeratorBW = NumeratorVal.getBitWidth(); 770 uint32_t DenominatorBW = DenominatorVal.getBitWidth(); 771 772 if (NumeratorBW > DenominatorBW) 773 DenominatorVal = DenominatorVal.sext(NumeratorBW); 774 else if (NumeratorBW < DenominatorBW) 775 NumeratorVal = NumeratorVal.sext(DenominatorBW); 776 777 APInt QuotientVal(NumeratorVal.getBitWidth(), 0); 778 APInt RemainderVal(NumeratorVal.getBitWidth(), 0); 779 APInt::sdivrem(NumeratorVal, DenominatorVal, QuotientVal, RemainderVal); 780 Quotient = SE.getConstant(QuotientVal); 781 Remainder = SE.getConstant(RemainderVal); 782 return; 783 } 784 } 785 786 void visitAddRecExpr(const SCEVAddRecExpr *Numerator) { 787 const SCEV *StartQ, *StartR, *StepQ, *StepR; 788 if (!Numerator->isAffine()) 789 return cannotDivide(Numerator); 790 divide(SE, Numerator->getStart(), Denominator, &StartQ, &StartR); 791 divide(SE, Numerator->getStepRecurrence(SE), Denominator, &StepQ, &StepR); 792 // Bail out if the types do not match. 793 Type *Ty = Denominator->getType(); 794 if (Ty != StartQ->getType() || Ty != StartR->getType() || 795 Ty != StepQ->getType() || Ty != StepR->getType()) 796 return cannotDivide(Numerator); 797 Quotient = SE.getAddRecExpr(StartQ, StepQ, Numerator->getLoop(), 798 Numerator->getNoWrapFlags()); 799 Remainder = SE.getAddRecExpr(StartR, StepR, Numerator->getLoop(), 800 Numerator->getNoWrapFlags()); 801 } 802 803 void visitAddExpr(const SCEVAddExpr *Numerator) { 804 SmallVector<const SCEV *, 2> Qs, Rs; 805 Type *Ty = Denominator->getType(); 806 807 for (const SCEV *Op : Numerator->operands()) { 808 const SCEV *Q, *R; 809 divide(SE, Op, Denominator, &Q, &R); 810 811 // Bail out if types do not match. 812 if (Ty != Q->getType() || Ty != R->getType()) 813 return cannotDivide(Numerator); 814 815 Qs.push_back(Q); 816 Rs.push_back(R); 817 } 818 819 if (Qs.size() == 1) { 820 Quotient = Qs[0]; 821 Remainder = Rs[0]; 822 return; 823 } 824 825 Quotient = SE.getAddExpr(Qs); 826 Remainder = SE.getAddExpr(Rs); 827 } 828 829 void visitMulExpr(const SCEVMulExpr *Numerator) { 830 SmallVector<const SCEV *, 2> Qs; 831 Type *Ty = Denominator->getType(); 832 833 bool FoundDenominatorTerm = false; 834 for (const SCEV *Op : Numerator->operands()) { 835 // Bail out if types do not match. 836 if (Ty != Op->getType()) 837 return cannotDivide(Numerator); 838 839 if (FoundDenominatorTerm) { 840 Qs.push_back(Op); 841 continue; 842 } 843 844 // Check whether Denominator divides one of the product operands. 845 const SCEV *Q, *R; 846 divide(SE, Op, Denominator, &Q, &R); 847 if (!R->isZero()) { 848 Qs.push_back(Op); 849 continue; 850 } 851 852 // Bail out if types do not match. 853 if (Ty != Q->getType()) 854 return cannotDivide(Numerator); 855 856 FoundDenominatorTerm = true; 857 Qs.push_back(Q); 858 } 859 860 if (FoundDenominatorTerm) { 861 Remainder = Zero; 862 if (Qs.size() == 1) 863 Quotient = Qs[0]; 864 else 865 Quotient = SE.getMulExpr(Qs); 866 return; 867 } 868 869 if (!isa<SCEVUnknown>(Denominator)) 870 return cannotDivide(Numerator); 871 872 // The Remainder is obtained by replacing Denominator by 0 in Numerator. 873 ValueToValueMap RewriteMap; 874 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] = 875 cast<SCEVConstant>(Zero)->getValue(); 876 Remainder = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true); 877 878 if (Remainder->isZero()) { 879 // The Quotient is obtained by replacing Denominator by 1 in Numerator. 880 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] = 881 cast<SCEVConstant>(One)->getValue(); 882 Quotient = 883 SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true); 884 return; 885 } 886 887 // Quotient is (Numerator - Remainder) divided by Denominator. 888 const SCEV *Q, *R; 889 const SCEV *Diff = SE.getMinusSCEV(Numerator, Remainder); 890 // This SCEV does not seem to simplify: fail the division here. 891 if (sizeOfSCEV(Diff) > sizeOfSCEV(Numerator)) 892 return cannotDivide(Numerator); 893 divide(SE, Diff, Denominator, &Q, &R); 894 if (R != Zero) 895 return cannotDivide(Numerator); 896 Quotient = Q; 897 } 898 899 private: 900 SCEVDivision(ScalarEvolution &S, const SCEV *Numerator, 901 const SCEV *Denominator) 902 : SE(S), Denominator(Denominator) { 903 Zero = SE.getZero(Denominator->getType()); 904 One = SE.getOne(Denominator->getType()); 905 906 // We generally do not know how to divide Expr by Denominator. We 907 // initialize the division to a "cannot divide" state to simplify the rest 908 // of the code. 909 cannotDivide(Numerator); 910 } 911 912 // Convenience function for giving up on the division. We set the quotient to 913 // be equal to zero and the remainder to be equal to the numerator. 914 void cannotDivide(const SCEV *Numerator) { 915 Quotient = Zero; 916 Remainder = Numerator; 917 } 918 919 ScalarEvolution &SE; 920 const SCEV *Denominator, *Quotient, *Remainder, *Zero, *One; 921 }; 922 923 } 924 925 //===----------------------------------------------------------------------===// 926 // Simple SCEV method implementations 927 //===----------------------------------------------------------------------===// 928 929 /// BinomialCoefficient - Compute BC(It, K). The result has width W. 930 /// Assume, K > 0. 931 static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K, 932 ScalarEvolution &SE, 933 Type *ResultTy) { 934 // Handle the simplest case efficiently. 935 if (K == 1) 936 return SE.getTruncateOrZeroExtend(It, ResultTy); 937 938 // We are using the following formula for BC(It, K): 939 // 940 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! 941 // 942 // Suppose, W is the bitwidth of the return value. We must be prepared for 943 // overflow. Hence, we must assure that the result of our computation is 944 // equal to the accurate one modulo 2^W. Unfortunately, division isn't 945 // safe in modular arithmetic. 946 // 947 // However, this code doesn't use exactly that formula; the formula it uses 948 // is something like the following, where T is the number of factors of 2 in 949 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is 950 // exponentiation: 951 // 952 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) 953 // 954 // This formula is trivially equivalent to the previous formula. However, 955 // this formula can be implemented much more efficiently. The trick is that 956 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular 957 // arithmetic. To do exact division in modular arithmetic, all we have 958 // to do is multiply by the inverse. Therefore, this step can be done at 959 // width W. 960 // 961 // The next issue is how to safely do the division by 2^T. The way this 962 // is done is by doing the multiplication step at a width of at least W + T 963 // bits. This way, the bottom W+T bits of the product are accurate. Then, 964 // when we perform the division by 2^T (which is equivalent to a right shift 965 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get 966 // truncated out after the division by 2^T. 967 // 968 // In comparison to just directly using the first formula, this technique 969 // is much more efficient; using the first formula requires W * K bits, 970 // but this formula less than W + K bits. Also, the first formula requires 971 // a division step, whereas this formula only requires multiplies and shifts. 972 // 973 // It doesn't matter whether the subtraction step is done in the calculation 974 // width or the input iteration count's width; if the subtraction overflows, 975 // the result must be zero anyway. We prefer here to do it in the width of 976 // the induction variable because it helps a lot for certain cases; CodeGen 977 // isn't smart enough to ignore the overflow, which leads to much less 978 // efficient code if the width of the subtraction is wider than the native 979 // register width. 980 // 981 // (It's possible to not widen at all by pulling out factors of 2 before 982 // the multiplication; for example, K=2 can be calculated as 983 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires 984 // extra arithmetic, so it's not an obvious win, and it gets 985 // much more complicated for K > 3.) 986 987 // Protection from insane SCEVs; this bound is conservative, 988 // but it probably doesn't matter. 989 if (K > 1000) 990 return SE.getCouldNotCompute(); 991 992 unsigned W = SE.getTypeSizeInBits(ResultTy); 993 994 // Calculate K! / 2^T and T; we divide out the factors of two before 995 // multiplying for calculating K! / 2^T to avoid overflow. 996 // Other overflow doesn't matter because we only care about the bottom 997 // W bits of the result. 998 APInt OddFactorial(W, 1); 999 unsigned T = 1; 1000 for (unsigned i = 3; i <= K; ++i) { 1001 APInt Mult(W, i); 1002 unsigned TwoFactors = Mult.countTrailingZeros(); 1003 T += TwoFactors; 1004 Mult = Mult.lshr(TwoFactors); 1005 OddFactorial *= Mult; 1006 } 1007 1008 // We need at least W + T bits for the multiplication step 1009 unsigned CalculationBits = W + T; 1010 1011 // Calculate 2^T, at width T+W. 1012 APInt DivFactor = APInt::getOneBitSet(CalculationBits, T); 1013 1014 // Calculate the multiplicative inverse of K! / 2^T; 1015 // this multiplication factor will perform the exact division by 1016 // K! / 2^T. 1017 APInt Mod = APInt::getSignedMinValue(W+1); 1018 APInt MultiplyFactor = OddFactorial.zext(W+1); 1019 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); 1020 MultiplyFactor = MultiplyFactor.trunc(W); 1021 1022 // Calculate the product, at width T+W 1023 IntegerType *CalculationTy = IntegerType::get(SE.getContext(), 1024 CalculationBits); 1025 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); 1026 for (unsigned i = 1; i != K; ++i) { 1027 const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i)); 1028 Dividend = SE.getMulExpr(Dividend, 1029 SE.getTruncateOrZeroExtend(S, CalculationTy)); 1030 } 1031 1032 // Divide by 2^T 1033 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); 1034 1035 // Truncate the result, and divide by K! / 2^T. 1036 1037 return SE.getMulExpr(SE.getConstant(MultiplyFactor), 1038 SE.getTruncateOrZeroExtend(DivResult, ResultTy)); 1039 } 1040 1041 /// evaluateAtIteration - Return the value of this chain of recurrences at 1042 /// the specified iteration number. We can evaluate this recurrence by 1043 /// multiplying each element in the chain by the binomial coefficient 1044 /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: 1045 /// 1046 /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) 1047 /// 1048 /// where BC(It, k) stands for binomial coefficient. 1049 /// 1050 const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It, 1051 ScalarEvolution &SE) const { 1052 const SCEV *Result = getStart(); 1053 for (unsigned i = 1, e = getNumOperands(); 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, getType()); 1058 if (isa<SCEVCouldNotCompute>(Coeff)) 1059 return Coeff; 1060 1061 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); 1062 } 1063 return Result; 1064 } 1065 1066 //===----------------------------------------------------------------------===// 1067 // SCEV Expression folder implementations 1068 //===----------------------------------------------------------------------===// 1069 1070 const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, 1071 Type *Ty) { 1072 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && 1073 "This is not a truncating conversion!"); 1074 assert(isSCEVable(Ty) && 1075 "This is not a conversion to a SCEVable type!"); 1076 Ty = getEffectiveSCEVType(Ty); 1077 1078 FoldingSetNodeID ID; 1079 ID.AddInteger(scTruncate); 1080 ID.AddPointer(Op); 1081 ID.AddPointer(Ty); 1082 void *IP = nullptr; 1083 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1084 1085 // Fold if the operand is constant. 1086 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 1087 return getConstant( 1088 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); 1089 1090 // trunc(trunc(x)) --> trunc(x) 1091 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) 1092 return getTruncateExpr(ST->getOperand(), Ty); 1093 1094 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing 1095 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) 1096 return getTruncateOrSignExtend(SS->getOperand(), Ty); 1097 1098 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing 1099 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) 1100 return getTruncateOrZeroExtend(SZ->getOperand(), Ty); 1101 1102 // trunc(x1+x2+...+xN) --> trunc(x1)+trunc(x2)+...+trunc(xN) if we can 1103 // eliminate all the truncates, or we replace other casts with truncates. 1104 if (const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Op)) { 1105 SmallVector<const SCEV *, 4> Operands; 1106 bool hasTrunc = false; 1107 for (unsigned i = 0, e = SA->getNumOperands(); i != e && !hasTrunc; ++i) { 1108 const SCEV *S = getTruncateExpr(SA->getOperand(i), Ty); 1109 if (!isa<SCEVCastExpr>(SA->getOperand(i))) 1110 hasTrunc = isa<SCEVTruncateExpr>(S); 1111 Operands.push_back(S); 1112 } 1113 if (!hasTrunc) 1114 return getAddExpr(Operands); 1115 UniqueSCEVs.FindNodeOrInsertPos(ID, IP); // Mutates IP, returns NULL. 1116 } 1117 1118 // trunc(x1*x2*...*xN) --> trunc(x1)*trunc(x2)*...*trunc(xN) if we can 1119 // eliminate all the truncates, or we replace other casts with truncates. 1120 if (const SCEVMulExpr *SM = dyn_cast<SCEVMulExpr>(Op)) { 1121 SmallVector<const SCEV *, 4> Operands; 1122 bool hasTrunc = false; 1123 for (unsigned i = 0, e = SM->getNumOperands(); i != e && !hasTrunc; ++i) { 1124 const SCEV *S = getTruncateExpr(SM->getOperand(i), Ty); 1125 if (!isa<SCEVCastExpr>(SM->getOperand(i))) 1126 hasTrunc = isa<SCEVTruncateExpr>(S); 1127 Operands.push_back(S); 1128 } 1129 if (!hasTrunc) 1130 return getMulExpr(Operands); 1131 UniqueSCEVs.FindNodeOrInsertPos(ID, IP); // Mutates IP, returns NULL. 1132 } 1133 1134 // If the input value is a chrec scev, truncate the chrec's operands. 1135 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { 1136 SmallVector<const SCEV *, 4> Operands; 1137 for (const SCEV *Op : AddRec->operands()) 1138 Operands.push_back(getTruncateExpr(Op, Ty)); 1139 return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap); 1140 } 1141 1142 // The cast wasn't folded; create an explicit cast node. We can reuse 1143 // the existing insert position since if we get here, we won't have 1144 // made any changes which would invalidate it. 1145 SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), 1146 Op, Ty); 1147 UniqueSCEVs.InsertNode(S, IP); 1148 return S; 1149 } 1150 1151 // Get the limit of a recurrence such that incrementing by Step cannot cause 1152 // signed overflow as long as the value of the recurrence within the 1153 // loop does not exceed this limit before incrementing. 1154 static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step, 1155 ICmpInst::Predicate *Pred, 1156 ScalarEvolution *SE) { 1157 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); 1158 if (SE->isKnownPositive(Step)) { 1159 *Pred = ICmpInst::ICMP_SLT; 1160 return SE->getConstant(APInt::getSignedMinValue(BitWidth) - 1161 SE->getSignedRange(Step).getSignedMax()); 1162 } 1163 if (SE->isKnownNegative(Step)) { 1164 *Pred = ICmpInst::ICMP_SGT; 1165 return SE->getConstant(APInt::getSignedMaxValue(BitWidth) - 1166 SE->getSignedRange(Step).getSignedMin()); 1167 } 1168 return nullptr; 1169 } 1170 1171 // Get the limit of a recurrence such that incrementing by Step cannot cause 1172 // unsigned overflow as long as the value of the recurrence within the loop does 1173 // not exceed this limit before incrementing. 1174 static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step, 1175 ICmpInst::Predicate *Pred, 1176 ScalarEvolution *SE) { 1177 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType()); 1178 *Pred = ICmpInst::ICMP_ULT; 1179 1180 return SE->getConstant(APInt::getMinValue(BitWidth) - 1181 SE->getUnsignedRange(Step).getUnsignedMax()); 1182 } 1183 1184 namespace { 1185 1186 struct ExtendOpTraitsBase { 1187 typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *); 1188 }; 1189 1190 // Used to make code generic over signed and unsigned overflow. 1191 template <typename ExtendOp> struct ExtendOpTraits { 1192 // Members present: 1193 // 1194 // static const SCEV::NoWrapFlags WrapType; 1195 // 1196 // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr; 1197 // 1198 // static const SCEV *getOverflowLimitForStep(const SCEV *Step, 1199 // ICmpInst::Predicate *Pred, 1200 // ScalarEvolution *SE); 1201 }; 1202 1203 template <> 1204 struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase { 1205 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW; 1206 1207 static const GetExtendExprTy GetExtendExpr; 1208 1209 static const SCEV *getOverflowLimitForStep(const SCEV *Step, 1210 ICmpInst::Predicate *Pred, 1211 ScalarEvolution *SE) { 1212 return getSignedOverflowLimitForStep(Step, Pred, SE); 1213 } 1214 }; 1215 1216 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits< 1217 SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr; 1218 1219 template <> 1220 struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase { 1221 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW; 1222 1223 static const GetExtendExprTy GetExtendExpr; 1224 1225 static const SCEV *getOverflowLimitForStep(const SCEV *Step, 1226 ICmpInst::Predicate *Pred, 1227 ScalarEvolution *SE) { 1228 return getUnsignedOverflowLimitForStep(Step, Pred, SE); 1229 } 1230 }; 1231 1232 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits< 1233 SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr; 1234 } 1235 1236 // The recurrence AR has been shown to have no signed/unsigned wrap or something 1237 // close to it. Typically, if we can prove NSW/NUW for AR, then we can just as 1238 // easily prove NSW/NUW for its preincrement or postincrement sibling. This 1239 // allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step + 1240 // Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the 1241 // expression "Step + sext/zext(PreIncAR)" is congruent with 1242 // "sext/zext(PostIncAR)" 1243 template <typename ExtendOpTy> 1244 static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty, 1245 ScalarEvolution *SE) { 1246 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType; 1247 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr; 1248 1249 const Loop *L = AR->getLoop(); 1250 const SCEV *Start = AR->getStart(); 1251 const SCEV *Step = AR->getStepRecurrence(*SE); 1252 1253 // Check for a simple looking step prior to loop entry. 1254 const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start); 1255 if (!SA) 1256 return nullptr; 1257 1258 // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV 1259 // subtraction is expensive. For this purpose, perform a quick and dirty 1260 // difference, by checking for Step in the operand list. 1261 SmallVector<const SCEV *, 4> DiffOps; 1262 for (const SCEV *Op : SA->operands()) 1263 if (Op != Step) 1264 DiffOps.push_back(Op); 1265 1266 if (DiffOps.size() == SA->getNumOperands()) 1267 return nullptr; 1268 1269 // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` + 1270 // `Step`: 1271 1272 // 1. NSW/NUW flags on the step increment. 1273 auto PreStartFlags = 1274 ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW); 1275 const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags); 1276 const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>( 1277 SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap)); 1278 1279 // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies 1280 // "S+X does not sign/unsign-overflow". 1281 // 1282 1283 const SCEV *BECount = SE->getBackedgeTakenCount(L); 1284 if (PreAR && PreAR->getNoWrapFlags(WrapType) && 1285 !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount)) 1286 return PreStart; 1287 1288 // 2. Direct overflow check on the step operation's expression. 1289 unsigned BitWidth = SE->getTypeSizeInBits(AR->getType()); 1290 Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2); 1291 const SCEV *OperandExtendedStart = 1292 SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy), 1293 (SE->*GetExtendExpr)(Step, WideTy)); 1294 if ((SE->*GetExtendExpr)(Start, WideTy) == OperandExtendedStart) { 1295 if (PreAR && AR->getNoWrapFlags(WrapType)) { 1296 // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW 1297 // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then 1298 // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact. 1299 const_cast<SCEVAddRecExpr *>(PreAR)->setNoWrapFlags(WrapType); 1300 } 1301 return PreStart; 1302 } 1303 1304 // 3. Loop precondition. 1305 ICmpInst::Predicate Pred; 1306 const SCEV *OverflowLimit = 1307 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE); 1308 1309 if (OverflowLimit && 1310 SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit)) 1311 return PreStart; 1312 1313 return nullptr; 1314 } 1315 1316 // Get the normalized zero or sign extended expression for this AddRec's Start. 1317 template <typename ExtendOpTy> 1318 static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty, 1319 ScalarEvolution *SE) { 1320 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr; 1321 1322 const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE); 1323 if (!PreStart) 1324 return (SE->*GetExtendExpr)(AR->getStart(), Ty); 1325 1326 return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty), 1327 (SE->*GetExtendExpr)(PreStart, Ty)); 1328 } 1329 1330 // Try to prove away overflow by looking at "nearby" add recurrences. A 1331 // motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it 1332 // does not itself wrap then we can conclude that `{1,+,4}` is `nuw`. 1333 // 1334 // Formally: 1335 // 1336 // {S,+,X} == {S-T,+,X} + T 1337 // => Ext({S,+,X}) == Ext({S-T,+,X} + T) 1338 // 1339 // If ({S-T,+,X} + T) does not overflow ... (1) 1340 // 1341 // RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T) 1342 // 1343 // If {S-T,+,X} does not overflow ... (2) 1344 // 1345 // RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T) 1346 // == {Ext(S-T)+Ext(T),+,Ext(X)} 1347 // 1348 // If (S-T)+T does not overflow ... (3) 1349 // 1350 // RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)} 1351 // == {Ext(S),+,Ext(X)} == LHS 1352 // 1353 // Thus, if (1), (2) and (3) are true for some T, then 1354 // Ext({S,+,X}) == {Ext(S),+,Ext(X)} 1355 // 1356 // (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T) 1357 // does not overflow" restricted to the 0th iteration. Therefore we only need 1358 // to check for (1) and (2). 1359 // 1360 // In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T 1361 // is `Delta` (defined below). 1362 // 1363 template <typename ExtendOpTy> 1364 bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start, 1365 const SCEV *Step, 1366 const Loop *L) { 1367 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType; 1368 1369 // We restrict `Start` to a constant to prevent SCEV from spending too much 1370 // time here. It is correct (but more expensive) to continue with a 1371 // non-constant `Start` and do a general SCEV subtraction to compute 1372 // `PreStart` below. 1373 // 1374 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start); 1375 if (!StartC) 1376 return false; 1377 1378 APInt StartAI = StartC->getAPInt(); 1379 1380 for (unsigned Delta : {-2, -1, 1, 2}) { 1381 const SCEV *PreStart = getConstant(StartAI - Delta); 1382 1383 FoldingSetNodeID ID; 1384 ID.AddInteger(scAddRecExpr); 1385 ID.AddPointer(PreStart); 1386 ID.AddPointer(Step); 1387 ID.AddPointer(L); 1388 void *IP = nullptr; 1389 const auto *PreAR = 1390 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); 1391 1392 // Give up if we don't already have the add recurrence we need because 1393 // actually constructing an add recurrence is relatively expensive. 1394 if (PreAR && PreAR->getNoWrapFlags(WrapType)) { // proves (2) 1395 const SCEV *DeltaS = getConstant(StartC->getType(), Delta); 1396 ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE; 1397 const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep( 1398 DeltaS, &Pred, this); 1399 if (Limit && isKnownPredicate(Pred, PreAR, Limit)) // proves (1) 1400 return true; 1401 } 1402 } 1403 1404 return false; 1405 } 1406 1407 const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op, 1408 Type *Ty) { 1409 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 1410 "This is not an extending conversion!"); 1411 assert(isSCEVable(Ty) && 1412 "This is not a conversion to a SCEVable type!"); 1413 Ty = getEffectiveSCEVType(Ty); 1414 1415 // Fold if the operand is constant. 1416 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 1417 return getConstant( 1418 cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty))); 1419 1420 // zext(zext(x)) --> zext(x) 1421 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) 1422 return getZeroExtendExpr(SZ->getOperand(), Ty); 1423 1424 // Before doing any expensive analysis, check to see if we've already 1425 // computed a SCEV for this Op and Ty. 1426 FoldingSetNodeID ID; 1427 ID.AddInteger(scZeroExtend); 1428 ID.AddPointer(Op); 1429 ID.AddPointer(Ty); 1430 void *IP = nullptr; 1431 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1432 1433 // zext(trunc(x)) --> zext(x) or x or trunc(x) 1434 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) { 1435 // It's possible the bits taken off by the truncate were all zero bits. If 1436 // so, we should be able to simplify this further. 1437 const SCEV *X = ST->getOperand(); 1438 ConstantRange CR = getUnsignedRange(X); 1439 unsigned TruncBits = getTypeSizeInBits(ST->getType()); 1440 unsigned NewBits = getTypeSizeInBits(Ty); 1441 if (CR.truncate(TruncBits).zeroExtend(NewBits).contains( 1442 CR.zextOrTrunc(NewBits))) 1443 return getTruncateOrZeroExtend(X, Ty); 1444 } 1445 1446 // If the input value is a chrec scev, and we can prove that the value 1447 // did not overflow the old, smaller, value, we can zero extend all of the 1448 // operands (often constants). This allows analysis of something like 1449 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } 1450 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) 1451 if (AR->isAffine()) { 1452 const SCEV *Start = AR->getStart(); 1453 const SCEV *Step = AR->getStepRecurrence(*this); 1454 unsigned BitWidth = getTypeSizeInBits(AR->getType()); 1455 const Loop *L = AR->getLoop(); 1456 1457 if (!AR->hasNoUnsignedWrap()) { 1458 auto NewFlags = proveNoWrapViaConstantRanges(AR); 1459 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags); 1460 } 1461 1462 // If we have special knowledge that this addrec won't overflow, 1463 // we don't need to do any further analysis. 1464 if (AR->hasNoUnsignedWrap()) 1465 return getAddRecExpr( 1466 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this), 1467 getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1468 1469 // Check whether the backedge-taken count is SCEVCouldNotCompute. 1470 // Note that this serves two purposes: It filters out loops that are 1471 // simply not analyzable, and it covers the case where this code is 1472 // being called from within backedge-taken count analysis, such that 1473 // attempting to ask for the backedge-taken count would likely result 1474 // in infinite recursion. In the later case, the analysis code will 1475 // cope with a conservative value, and it will take care to purge 1476 // that value once it has finished. 1477 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); 1478 if (!isa<SCEVCouldNotCompute>(MaxBECount)) { 1479 // Manually compute the final value for AR, checking for 1480 // overflow. 1481 1482 // Check whether the backedge-taken count can be losslessly casted to 1483 // the addrec's type. The count is always unsigned. 1484 const SCEV *CastedMaxBECount = 1485 getTruncateOrZeroExtend(MaxBECount, Start->getType()); 1486 const SCEV *RecastedMaxBECount = 1487 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); 1488 if (MaxBECount == RecastedMaxBECount) { 1489 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); 1490 // Check whether Start+Step*MaxBECount has no unsigned overflow. 1491 const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step); 1492 const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul), WideTy); 1493 const SCEV *WideStart = getZeroExtendExpr(Start, WideTy); 1494 const SCEV *WideMaxBECount = 1495 getZeroExtendExpr(CastedMaxBECount, WideTy); 1496 const SCEV *OperandExtendedAdd = 1497 getAddExpr(WideStart, 1498 getMulExpr(WideMaxBECount, 1499 getZeroExtendExpr(Step, WideTy))); 1500 if (ZAdd == OperandExtendedAdd) { 1501 // Cache knowledge of AR NUW, which is propagated to this AddRec. 1502 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW); 1503 // Return the expression with the addrec on the outside. 1504 return getAddRecExpr( 1505 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this), 1506 getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1507 } 1508 // Similar to above, only this time treat the step value as signed. 1509 // This covers loops that count down. 1510 OperandExtendedAdd = 1511 getAddExpr(WideStart, 1512 getMulExpr(WideMaxBECount, 1513 getSignExtendExpr(Step, WideTy))); 1514 if (ZAdd == OperandExtendedAdd) { 1515 // Cache knowledge of AR NW, which is propagated to this AddRec. 1516 // Negative step causes unsigned wrap, but it still can't self-wrap. 1517 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW); 1518 // Return the expression with the addrec on the outside. 1519 return getAddRecExpr( 1520 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this), 1521 getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1522 } 1523 } 1524 1525 // If the backedge is guarded by a comparison with the pre-inc value 1526 // the addrec is safe. Also, if the entry is guarded by a comparison 1527 // with the start value and the backedge is guarded by a comparison 1528 // with the post-inc value, the addrec is safe. 1529 if (isKnownPositive(Step)) { 1530 const SCEV *N = getConstant(APInt::getMinValue(BitWidth) - 1531 getUnsignedRange(Step).getUnsignedMax()); 1532 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) || 1533 (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) && 1534 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, 1535 AR->getPostIncExpr(*this), N))) { 1536 // Cache knowledge of AR NUW, which is propagated to this AddRec. 1537 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW); 1538 // Return the expression with the addrec on the outside. 1539 return getAddRecExpr( 1540 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this), 1541 getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1542 } 1543 } else if (isKnownNegative(Step)) { 1544 const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) - 1545 getSignedRange(Step).getSignedMin()); 1546 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) || 1547 (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) && 1548 isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, 1549 AR->getPostIncExpr(*this), N))) { 1550 // Cache knowledge of AR NW, which is propagated to this AddRec. 1551 // Negative step causes unsigned wrap, but it still can't self-wrap. 1552 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW); 1553 // Return the expression with the addrec on the outside. 1554 return getAddRecExpr( 1555 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this), 1556 getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1557 } 1558 } 1559 } 1560 1561 if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) { 1562 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW); 1563 return getAddRecExpr( 1564 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this), 1565 getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1566 } 1567 } 1568 1569 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) { 1570 // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw> 1571 if (SA->hasNoUnsignedWrap()) { 1572 // If the addition does not unsign overflow then we can, by definition, 1573 // commute the zero extension with the addition operation. 1574 SmallVector<const SCEV *, 4> Ops; 1575 for (const auto *Op : SA->operands()) 1576 Ops.push_back(getZeroExtendExpr(Op, Ty)); 1577 return getAddExpr(Ops, SCEV::FlagNUW); 1578 } 1579 } 1580 1581 // The cast wasn't folded; create an explicit cast node. 1582 // Recompute the insert position, as it may have been invalidated. 1583 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1584 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator), 1585 Op, Ty); 1586 UniqueSCEVs.InsertNode(S, IP); 1587 return S; 1588 } 1589 1590 const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op, 1591 Type *Ty) { 1592 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 1593 "This is not an extending conversion!"); 1594 assert(isSCEVable(Ty) && 1595 "This is not a conversion to a SCEVable type!"); 1596 Ty = getEffectiveSCEVType(Ty); 1597 1598 // Fold if the operand is constant. 1599 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 1600 return getConstant( 1601 cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty))); 1602 1603 // sext(sext(x)) --> sext(x) 1604 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) 1605 return getSignExtendExpr(SS->getOperand(), Ty); 1606 1607 // sext(zext(x)) --> zext(x) 1608 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) 1609 return getZeroExtendExpr(SZ->getOperand(), Ty); 1610 1611 // Before doing any expensive analysis, check to see if we've already 1612 // computed a SCEV for this Op and Ty. 1613 FoldingSetNodeID ID; 1614 ID.AddInteger(scSignExtend); 1615 ID.AddPointer(Op); 1616 ID.AddPointer(Ty); 1617 void *IP = nullptr; 1618 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1619 1620 // sext(trunc(x)) --> sext(x) or x or trunc(x) 1621 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) { 1622 // It's possible the bits taken off by the truncate were all sign bits. If 1623 // so, we should be able to simplify this further. 1624 const SCEV *X = ST->getOperand(); 1625 ConstantRange CR = getSignedRange(X); 1626 unsigned TruncBits = getTypeSizeInBits(ST->getType()); 1627 unsigned NewBits = getTypeSizeInBits(Ty); 1628 if (CR.truncate(TruncBits).signExtend(NewBits).contains( 1629 CR.sextOrTrunc(NewBits))) 1630 return getTruncateOrSignExtend(X, Ty); 1631 } 1632 1633 // sext(C1 + (C2 * x)) --> C1 + sext(C2 * x) if C1 < C2 1634 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) { 1635 if (SA->getNumOperands() == 2) { 1636 auto *SC1 = dyn_cast<SCEVConstant>(SA->getOperand(0)); 1637 auto *SMul = dyn_cast<SCEVMulExpr>(SA->getOperand(1)); 1638 if (SMul && SC1) { 1639 if (auto *SC2 = dyn_cast<SCEVConstant>(SMul->getOperand(0))) { 1640 const APInt &C1 = SC1->getAPInt(); 1641 const APInt &C2 = SC2->getAPInt(); 1642 if (C1.isStrictlyPositive() && C2.isStrictlyPositive() && 1643 C2.ugt(C1) && C2.isPowerOf2()) 1644 return getAddExpr(getSignExtendExpr(SC1, Ty), 1645 getSignExtendExpr(SMul, Ty)); 1646 } 1647 } 1648 } 1649 1650 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw> 1651 if (SA->hasNoSignedWrap()) { 1652 // If the addition does not sign overflow then we can, by definition, 1653 // commute the sign extension with the addition operation. 1654 SmallVector<const SCEV *, 4> Ops; 1655 for (const auto *Op : SA->operands()) 1656 Ops.push_back(getSignExtendExpr(Op, Ty)); 1657 return getAddExpr(Ops, SCEV::FlagNSW); 1658 } 1659 } 1660 // If the input value is a chrec scev, and we can prove that the value 1661 // did not overflow the old, smaller, value, we can sign extend all of the 1662 // operands (often constants). This allows analysis of something like 1663 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } 1664 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) 1665 if (AR->isAffine()) { 1666 const SCEV *Start = AR->getStart(); 1667 const SCEV *Step = AR->getStepRecurrence(*this); 1668 unsigned BitWidth = getTypeSizeInBits(AR->getType()); 1669 const Loop *L = AR->getLoop(); 1670 1671 if (!AR->hasNoSignedWrap()) { 1672 auto NewFlags = proveNoWrapViaConstantRanges(AR); 1673 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags); 1674 } 1675 1676 // If we have special knowledge that this addrec won't overflow, 1677 // we don't need to do any further analysis. 1678 if (AR->hasNoSignedWrap()) 1679 return getAddRecExpr( 1680 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this), 1681 getSignExtendExpr(Step, Ty), L, SCEV::FlagNSW); 1682 1683 // Check whether the backedge-taken count is SCEVCouldNotCompute. 1684 // Note that this serves two purposes: It filters out loops that are 1685 // simply not analyzable, and it covers the case where this code is 1686 // being called from within backedge-taken count analysis, such that 1687 // attempting to ask for the backedge-taken count would likely result 1688 // in infinite recursion. In the later case, the analysis code will 1689 // cope with a conservative value, and it will take care to purge 1690 // that value once it has finished. 1691 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L); 1692 if (!isa<SCEVCouldNotCompute>(MaxBECount)) { 1693 // Manually compute the final value for AR, checking for 1694 // overflow. 1695 1696 // Check whether the backedge-taken count can be losslessly casted to 1697 // the addrec's type. The count is always unsigned. 1698 const SCEV *CastedMaxBECount = 1699 getTruncateOrZeroExtend(MaxBECount, Start->getType()); 1700 const SCEV *RecastedMaxBECount = 1701 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); 1702 if (MaxBECount == RecastedMaxBECount) { 1703 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2); 1704 // Check whether Start+Step*MaxBECount has no signed overflow. 1705 const SCEV *SMul = getMulExpr(CastedMaxBECount, Step); 1706 const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul), WideTy); 1707 const SCEV *WideStart = getSignExtendExpr(Start, WideTy); 1708 const SCEV *WideMaxBECount = 1709 getZeroExtendExpr(CastedMaxBECount, WideTy); 1710 const SCEV *OperandExtendedAdd = 1711 getAddExpr(WideStart, 1712 getMulExpr(WideMaxBECount, 1713 getSignExtendExpr(Step, WideTy))); 1714 if (SAdd == OperandExtendedAdd) { 1715 // Cache knowledge of AR NSW, which is propagated to this AddRec. 1716 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW); 1717 // Return the expression with the addrec on the outside. 1718 return getAddRecExpr( 1719 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this), 1720 getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1721 } 1722 // Similar to above, only this time treat the step value as unsigned. 1723 // This covers loops that count up with an unsigned step. 1724 OperandExtendedAdd = 1725 getAddExpr(WideStart, 1726 getMulExpr(WideMaxBECount, 1727 getZeroExtendExpr(Step, WideTy))); 1728 if (SAdd == OperandExtendedAdd) { 1729 // If AR wraps around then 1730 // 1731 // abs(Step) * MaxBECount > unsigned-max(AR->getType()) 1732 // => SAdd != OperandExtendedAdd 1733 // 1734 // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=> 1735 // (SAdd == OperandExtendedAdd => AR is NW) 1736 1737 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW); 1738 1739 // Return the expression with the addrec on the outside. 1740 return getAddRecExpr( 1741 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this), 1742 getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1743 } 1744 } 1745 1746 // If the backedge is guarded by a comparison with the pre-inc value 1747 // the addrec is safe. Also, if the entry is guarded by a comparison 1748 // with the start value and the backedge is guarded by a comparison 1749 // with the post-inc value, the addrec is safe. 1750 ICmpInst::Predicate Pred; 1751 const SCEV *OverflowLimit = 1752 getSignedOverflowLimitForStep(Step, &Pred, this); 1753 if (OverflowLimit && 1754 (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) || 1755 (isLoopEntryGuardedByCond(L, Pred, Start, OverflowLimit) && 1756 isLoopBackedgeGuardedByCond(L, Pred, AR->getPostIncExpr(*this), 1757 OverflowLimit)))) { 1758 // Cache knowledge of AR NSW, then propagate NSW to the wide AddRec. 1759 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW); 1760 return getAddRecExpr( 1761 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this), 1762 getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1763 } 1764 } 1765 // If Start and Step are constants, check if we can apply this 1766 // transformation: 1767 // sext{C1,+,C2} --> C1 + sext{0,+,C2} if C1 < C2 1768 auto *SC1 = dyn_cast<SCEVConstant>(Start); 1769 auto *SC2 = dyn_cast<SCEVConstant>(Step); 1770 if (SC1 && SC2) { 1771 const APInt &C1 = SC1->getAPInt(); 1772 const APInt &C2 = SC2->getAPInt(); 1773 if (C1.isStrictlyPositive() && C2.isStrictlyPositive() && C2.ugt(C1) && 1774 C2.isPowerOf2()) { 1775 Start = getSignExtendExpr(Start, Ty); 1776 const SCEV *NewAR = getAddRecExpr(getZero(AR->getType()), Step, L, 1777 AR->getNoWrapFlags()); 1778 return getAddExpr(Start, getSignExtendExpr(NewAR, Ty)); 1779 } 1780 } 1781 1782 if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) { 1783 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW); 1784 return getAddRecExpr( 1785 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this), 1786 getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags()); 1787 } 1788 } 1789 1790 // If the input value is provably positive and we could not simplify 1791 // away the sext build a zext instead. 1792 if (isKnownNonNegative(Op)) 1793 return getZeroExtendExpr(Op, Ty); 1794 1795 // The cast wasn't folded; create an explicit cast node. 1796 // Recompute the insert position, as it may have been invalidated. 1797 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1798 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator), 1799 Op, Ty); 1800 UniqueSCEVs.InsertNode(S, IP); 1801 return S; 1802 } 1803 1804 /// getAnyExtendExpr - Return a SCEV for the given operand extended with 1805 /// unspecified bits out to the given type. 1806 /// 1807 const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op, 1808 Type *Ty) { 1809 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 1810 "This is not an extending conversion!"); 1811 assert(isSCEVable(Ty) && 1812 "This is not a conversion to a SCEVable type!"); 1813 Ty = getEffectiveSCEVType(Ty); 1814 1815 // Sign-extend negative constants. 1816 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 1817 if (SC->getAPInt().isNegative()) 1818 return getSignExtendExpr(Op, Ty); 1819 1820 // Peel off a truncate cast. 1821 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { 1822 const SCEV *NewOp = T->getOperand(); 1823 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) 1824 return getAnyExtendExpr(NewOp, Ty); 1825 return getTruncateOrNoop(NewOp, Ty); 1826 } 1827 1828 // Next try a zext cast. If the cast is folded, use it. 1829 const SCEV *ZExt = getZeroExtendExpr(Op, Ty); 1830 if (!isa<SCEVZeroExtendExpr>(ZExt)) 1831 return ZExt; 1832 1833 // Next try a sext cast. If the cast is folded, use it. 1834 const SCEV *SExt = getSignExtendExpr(Op, Ty); 1835 if (!isa<SCEVSignExtendExpr>(SExt)) 1836 return SExt; 1837 1838 // Force the cast to be folded into the operands of an addrec. 1839 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) { 1840 SmallVector<const SCEV *, 4> Ops; 1841 for (const SCEV *Op : AR->operands()) 1842 Ops.push_back(getAnyExtendExpr(Op, Ty)); 1843 return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW); 1844 } 1845 1846 // If the expression is obviously signed, use the sext cast value. 1847 if (isa<SCEVSMaxExpr>(Op)) 1848 return SExt; 1849 1850 // Absent any other information, use the zext cast value. 1851 return ZExt; 1852 } 1853 1854 /// CollectAddOperandsWithScales - Process the given Ops list, which is 1855 /// a list of operands to be added under the given scale, update the given 1856 /// map. This is a helper function for getAddRecExpr. As an example of 1857 /// what it does, given a sequence of operands that would form an add 1858 /// expression like this: 1859 /// 1860 /// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r) 1861 /// 1862 /// where A and B are constants, update the map with these values: 1863 /// 1864 /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) 1865 /// 1866 /// and add 13 + A*B*29 to AccumulatedConstant. 1867 /// This will allow getAddRecExpr to produce this: 1868 /// 1869 /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) 1870 /// 1871 /// This form often exposes folding opportunities that are hidden in 1872 /// the original operand list. 1873 /// 1874 /// Return true iff it appears that any interesting folding opportunities 1875 /// may be exposed. This helps getAddRecExpr short-circuit extra work in 1876 /// the common case where no interesting opportunities are present, and 1877 /// is also used as a check to avoid infinite recursion. 1878 /// 1879 static bool 1880 CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M, 1881 SmallVectorImpl<const SCEV *> &NewOps, 1882 APInt &AccumulatedConstant, 1883 const SCEV *const *Ops, size_t NumOperands, 1884 const APInt &Scale, 1885 ScalarEvolution &SE) { 1886 bool Interesting = false; 1887 1888 // Iterate over the add operands. They are sorted, with constants first. 1889 unsigned i = 0; 1890 while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { 1891 ++i; 1892 // Pull a buried constant out to the outside. 1893 if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero()) 1894 Interesting = true; 1895 AccumulatedConstant += Scale * C->getAPInt(); 1896 } 1897 1898 // Next comes everything else. We're especially interested in multiplies 1899 // here, but they're in the middle, so just visit the rest with one loop. 1900 for (; i != NumOperands; ++i) { 1901 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); 1902 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { 1903 APInt NewScale = 1904 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt(); 1905 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { 1906 // A multiplication of a constant with another add; recurse. 1907 const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1)); 1908 Interesting |= 1909 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, 1910 Add->op_begin(), Add->getNumOperands(), 1911 NewScale, SE); 1912 } else { 1913 // A multiplication of a constant with some other value. Update 1914 // the map. 1915 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); 1916 const SCEV *Key = SE.getMulExpr(MulOps); 1917 auto Pair = M.insert({Key, NewScale}); 1918 if (Pair.second) { 1919 NewOps.push_back(Pair.first->first); 1920 } else { 1921 Pair.first->second += NewScale; 1922 // The map already had an entry for this value, which may indicate 1923 // a folding opportunity. 1924 Interesting = true; 1925 } 1926 } 1927 } else { 1928 // An ordinary operand. Update the map. 1929 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair = 1930 M.insert({Ops[i], Scale}); 1931 if (Pair.second) { 1932 NewOps.push_back(Pair.first->first); 1933 } else { 1934 Pair.first->second += Scale; 1935 // The map already had an entry for this value, which may indicate 1936 // a folding opportunity. 1937 Interesting = true; 1938 } 1939 } 1940 } 1941 1942 return Interesting; 1943 } 1944 1945 // We're trying to construct a SCEV of type `Type' with `Ops' as operands and 1946 // `OldFlags' as can't-wrap behavior. Infer a more aggressive set of 1947 // can't-overflow flags for the operation if possible. 1948 static SCEV::NoWrapFlags 1949 StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type, 1950 const SmallVectorImpl<const SCEV *> &Ops, 1951 SCEV::NoWrapFlags Flags) { 1952 using namespace std::placeholders; 1953 typedef OverflowingBinaryOperator OBO; 1954 1955 bool CanAnalyze = 1956 Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr; 1957 (void)CanAnalyze; 1958 assert(CanAnalyze && "don't call from other places!"); 1959 1960 int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW; 1961 SCEV::NoWrapFlags SignOrUnsignWrap = 1962 ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); 1963 1964 // If FlagNSW is true and all the operands are non-negative, infer FlagNUW. 1965 auto IsKnownNonNegative = [&](const SCEV *S) { 1966 return SE->isKnownNonNegative(S); 1967 }; 1968 1969 if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative)) 1970 Flags = 1971 ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask); 1972 1973 SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask); 1974 1975 if (SignOrUnsignWrap != SignOrUnsignMask && Type == scAddExpr && 1976 Ops.size() == 2 && isa<SCEVConstant>(Ops[0])) { 1977 1978 // (A + C) --> (A + C)<nsw> if the addition does not sign overflow 1979 // (A + C) --> (A + C)<nuw> if the addition does not unsign overflow 1980 1981 const APInt &C = cast<SCEVConstant>(Ops[0])->getAPInt(); 1982 if (!(SignOrUnsignWrap & SCEV::FlagNSW)) { 1983 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 1984 Instruction::Add, C, OBO::NoSignedWrap); 1985 if (NSWRegion.contains(SE->getSignedRange(Ops[1]))) 1986 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); 1987 } 1988 if (!(SignOrUnsignWrap & SCEV::FlagNUW)) { 1989 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 1990 Instruction::Add, C, OBO::NoUnsignedWrap); 1991 if (NUWRegion.contains(SE->getUnsignedRange(Ops[1]))) 1992 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 1993 } 1994 } 1995 1996 return Flags; 1997 } 1998 1999 /// getAddExpr - Get a canonical add expression, or something simpler if 2000 /// possible. 2001 const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops, 2002 SCEV::NoWrapFlags Flags) { 2003 assert(!(Flags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) && 2004 "only nuw or nsw allowed"); 2005 assert(!Ops.empty() && "Cannot get empty add!"); 2006 if (Ops.size() == 1) return Ops[0]; 2007 #ifndef NDEBUG 2008 Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); 2009 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 2010 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && 2011 "SCEVAddExpr operand types don't match!"); 2012 #endif 2013 2014 // Sort by complexity, this groups all similar expression types together. 2015 GroupByComplexity(Ops, &LI); 2016 2017 Flags = StrengthenNoWrapFlags(this, scAddExpr, Ops, Flags); 2018 2019 // If there are any constants, fold them together. 2020 unsigned Idx = 0; 2021 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 2022 ++Idx; 2023 assert(Idx < Ops.size()); 2024 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 2025 // We found two constants, fold them together! 2026 Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt()); 2027 if (Ops.size() == 2) return Ops[0]; 2028 Ops.erase(Ops.begin()+1); // Erase the folded element 2029 LHSC = cast<SCEVConstant>(Ops[0]); 2030 } 2031 2032 // If we are left with a constant zero being added, strip it off. 2033 if (LHSC->getValue()->isZero()) { 2034 Ops.erase(Ops.begin()); 2035 --Idx; 2036 } 2037 2038 if (Ops.size() == 1) return Ops[0]; 2039 } 2040 2041 // Okay, check to see if the same value occurs in the operand list more than 2042 // once. If so, merge them together into an multiply expression. Since we 2043 // sorted the list, these values are required to be adjacent. 2044 Type *Ty = Ops[0]->getType(); 2045 bool FoundMatch = false; 2046 for (unsigned i = 0, e = Ops.size(); i != e-1; ++i) 2047 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 2048 // Scan ahead to count how many equal operands there are. 2049 unsigned Count = 2; 2050 while (i+Count != e && Ops[i+Count] == Ops[i]) 2051 ++Count; 2052 // Merge the values into a multiply. 2053 const SCEV *Scale = getConstant(Ty, Count); 2054 const SCEV *Mul = getMulExpr(Scale, Ops[i]); 2055 if (Ops.size() == Count) 2056 return Mul; 2057 Ops[i] = Mul; 2058 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count); 2059 --i; e -= Count - 1; 2060 FoundMatch = true; 2061 } 2062 if (FoundMatch) 2063 return getAddExpr(Ops, Flags); 2064 2065 // Check for truncates. If all the operands are truncated from the same 2066 // type, see if factoring out the truncate would permit the result to be 2067 // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) 2068 // if the contents of the resulting outer trunc fold to something simple. 2069 for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { 2070 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); 2071 Type *DstType = Trunc->getType(); 2072 Type *SrcType = Trunc->getOperand()->getType(); 2073 SmallVector<const SCEV *, 8> LargeOps; 2074 bool Ok = true; 2075 // Check all the operands to see if they can be represented in the 2076 // source type of the truncate. 2077 for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 2078 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { 2079 if (T->getOperand()->getType() != SrcType) { 2080 Ok = false; 2081 break; 2082 } 2083 LargeOps.push_back(T->getOperand()); 2084 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { 2085 LargeOps.push_back(getAnyExtendExpr(C, SrcType)); 2086 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { 2087 SmallVector<const SCEV *, 8> LargeMulOps; 2088 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { 2089 if (const SCEVTruncateExpr *T = 2090 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { 2091 if (T->getOperand()->getType() != SrcType) { 2092 Ok = false; 2093 break; 2094 } 2095 LargeMulOps.push_back(T->getOperand()); 2096 } else if (const auto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) { 2097 LargeMulOps.push_back(getAnyExtendExpr(C, SrcType)); 2098 } else { 2099 Ok = false; 2100 break; 2101 } 2102 } 2103 if (Ok) 2104 LargeOps.push_back(getMulExpr(LargeMulOps)); 2105 } else { 2106 Ok = false; 2107 break; 2108 } 2109 } 2110 if (Ok) { 2111 // Evaluate the expression in the larger type. 2112 const SCEV *Fold = getAddExpr(LargeOps, Flags); 2113 // If it folds to something simple, use it. Otherwise, don't. 2114 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) 2115 return getTruncateExpr(Fold, DstType); 2116 } 2117 } 2118 2119 // Skip past any other cast SCEVs. 2120 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) 2121 ++Idx; 2122 2123 // If there are add operands they would be next. 2124 if (Idx < Ops.size()) { 2125 bool DeletedAdd = false; 2126 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { 2127 // If we have an add, expand the add operands onto the end of the operands 2128 // list. 2129 Ops.erase(Ops.begin()+Idx); 2130 Ops.append(Add->op_begin(), Add->op_end()); 2131 DeletedAdd = true; 2132 } 2133 2134 // If we deleted at least one add, we added operands to the end of the list, 2135 // and they are not necessarily sorted. Recurse to resort and resimplify 2136 // any operands we just acquired. 2137 if (DeletedAdd) 2138 return getAddExpr(Ops); 2139 } 2140 2141 // Skip over the add expression until we get to a multiply. 2142 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) 2143 ++Idx; 2144 2145 // Check to see if there are any folding opportunities present with 2146 // operands multiplied by constant values. 2147 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { 2148 uint64_t BitWidth = getTypeSizeInBits(Ty); 2149 DenseMap<const SCEV *, APInt> M; 2150 SmallVector<const SCEV *, 8> NewOps; 2151 APInt AccumulatedConstant(BitWidth, 0); 2152 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, 2153 Ops.data(), Ops.size(), 2154 APInt(BitWidth, 1), *this)) { 2155 struct APIntCompare { 2156 bool operator()(const APInt &LHS, const APInt &RHS) const { 2157 return LHS.ult(RHS); 2158 } 2159 }; 2160 2161 // Some interesting folding opportunity is present, so its worthwhile to 2162 // re-generate the operands list. Group the operands by constant scale, 2163 // to avoid multiplying by the same constant scale multiple times. 2164 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists; 2165 for (const SCEV *NewOp : NewOps) 2166 MulOpLists[M.find(NewOp)->second].push_back(NewOp); 2167 // Re-generate the operands list. 2168 Ops.clear(); 2169 if (AccumulatedConstant != 0) 2170 Ops.push_back(getConstant(AccumulatedConstant)); 2171 for (auto &MulOp : MulOpLists) 2172 if (MulOp.first != 0) 2173 Ops.push_back(getMulExpr(getConstant(MulOp.first), 2174 getAddExpr(MulOp.second))); 2175 if (Ops.empty()) 2176 return getZero(Ty); 2177 if (Ops.size() == 1) 2178 return Ops[0]; 2179 return getAddExpr(Ops); 2180 } 2181 } 2182 2183 // If we are adding something to a multiply expression, make sure the 2184 // something is not already an operand of the multiply. If so, merge it into 2185 // the multiply. 2186 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { 2187 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); 2188 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { 2189 const SCEV *MulOpSCEV = Mul->getOperand(MulOp); 2190 if (isa<SCEVConstant>(MulOpSCEV)) 2191 continue; 2192 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) 2193 if (MulOpSCEV == Ops[AddOp]) { 2194 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) 2195 const SCEV *InnerMul = Mul->getOperand(MulOp == 0); 2196 if (Mul->getNumOperands() != 2) { 2197 // If the multiply has more than two operands, we must get the 2198 // Y*Z term. 2199 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), 2200 Mul->op_begin()+MulOp); 2201 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); 2202 InnerMul = getMulExpr(MulOps); 2203 } 2204 const SCEV *One = getOne(Ty); 2205 const SCEV *AddOne = getAddExpr(One, InnerMul); 2206 const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV); 2207 if (Ops.size() == 2) return OuterMul; 2208 if (AddOp < Idx) { 2209 Ops.erase(Ops.begin()+AddOp); 2210 Ops.erase(Ops.begin()+Idx-1); 2211 } else { 2212 Ops.erase(Ops.begin()+Idx); 2213 Ops.erase(Ops.begin()+AddOp-1); 2214 } 2215 Ops.push_back(OuterMul); 2216 return getAddExpr(Ops); 2217 } 2218 2219 // Check this multiply against other multiplies being added together. 2220 for (unsigned OtherMulIdx = Idx+1; 2221 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); 2222 ++OtherMulIdx) { 2223 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); 2224 // If MulOp occurs in OtherMul, we can fold the two multiplies 2225 // together. 2226 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); 2227 OMulOp != e; ++OMulOp) 2228 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { 2229 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) 2230 const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0); 2231 if (Mul->getNumOperands() != 2) { 2232 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), 2233 Mul->op_begin()+MulOp); 2234 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end()); 2235 InnerMul1 = getMulExpr(MulOps); 2236 } 2237 const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0); 2238 if (OtherMul->getNumOperands() != 2) { 2239 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), 2240 OtherMul->op_begin()+OMulOp); 2241 MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end()); 2242 InnerMul2 = getMulExpr(MulOps); 2243 } 2244 const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2); 2245 const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); 2246 if (Ops.size() == 2) return OuterMul; 2247 Ops.erase(Ops.begin()+Idx); 2248 Ops.erase(Ops.begin()+OtherMulIdx-1); 2249 Ops.push_back(OuterMul); 2250 return getAddExpr(Ops); 2251 } 2252 } 2253 } 2254 } 2255 2256 // If there are any add recurrences in the operands list, see if any other 2257 // added values are loop invariant. If so, we can fold them into the 2258 // recurrence. 2259 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) 2260 ++Idx; 2261 2262 // Scan over all recurrences, trying to fold loop invariants into them. 2263 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { 2264 // Scan all of the other operands to this add and add them to the vector if 2265 // they are loop invariant w.r.t. the recurrence. 2266 SmallVector<const SCEV *, 8> LIOps; 2267 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); 2268 const Loop *AddRecLoop = AddRec->getLoop(); 2269 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 2270 if (isLoopInvariant(Ops[i], AddRecLoop)) { 2271 LIOps.push_back(Ops[i]); 2272 Ops.erase(Ops.begin()+i); 2273 --i; --e; 2274 } 2275 2276 // If we found some loop invariants, fold them into the recurrence. 2277 if (!LIOps.empty()) { 2278 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} 2279 LIOps.push_back(AddRec->getStart()); 2280 2281 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), 2282 AddRec->op_end()); 2283 AddRecOps[0] = getAddExpr(LIOps); 2284 2285 // Build the new addrec. Propagate the NUW and NSW flags if both the 2286 // outer add and the inner addrec are guaranteed to have no overflow. 2287 // Always propagate NW. 2288 Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW)); 2289 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags); 2290 2291 // If all of the other operands were loop invariant, we are done. 2292 if (Ops.size() == 1) return NewRec; 2293 2294 // Otherwise, add the folded AddRec by the non-invariant parts. 2295 for (unsigned i = 0;; ++i) 2296 if (Ops[i] == AddRec) { 2297 Ops[i] = NewRec; 2298 break; 2299 } 2300 return getAddExpr(Ops); 2301 } 2302 2303 // Okay, if there weren't any loop invariants to be folded, check to see if 2304 // there are multiple AddRec's with the same loop induction variable being 2305 // added together. If so, we can fold them. 2306 for (unsigned OtherIdx = Idx+1; 2307 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); 2308 ++OtherIdx) 2309 if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) { 2310 // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L> 2311 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(), 2312 AddRec->op_end()); 2313 for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); 2314 ++OtherIdx) 2315 if (const auto *OtherAddRec = dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx])) 2316 if (OtherAddRec->getLoop() == AddRecLoop) { 2317 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); 2318 i != e; ++i) { 2319 if (i >= AddRecOps.size()) { 2320 AddRecOps.append(OtherAddRec->op_begin()+i, 2321 OtherAddRec->op_end()); 2322 break; 2323 } 2324 AddRecOps[i] = getAddExpr(AddRecOps[i], 2325 OtherAddRec->getOperand(i)); 2326 } 2327 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; 2328 } 2329 // Step size has changed, so we cannot guarantee no self-wraparound. 2330 Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap); 2331 return getAddExpr(Ops); 2332 } 2333 2334 // Otherwise couldn't fold anything into this recurrence. Move onto the 2335 // next one. 2336 } 2337 2338 // Okay, it looks like we really DO need an add expr. Check to see if we 2339 // already have one, otherwise create a new one. 2340 FoldingSetNodeID ID; 2341 ID.AddInteger(scAddExpr); 2342 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 2343 ID.AddPointer(Ops[i]); 2344 void *IP = nullptr; 2345 SCEVAddExpr *S = 2346 static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); 2347 if (!S) { 2348 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 2349 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 2350 S = new (SCEVAllocator) SCEVAddExpr(ID.Intern(SCEVAllocator), 2351 O, Ops.size()); 2352 UniqueSCEVs.InsertNode(S, IP); 2353 } 2354 S->setNoWrapFlags(Flags); 2355 return S; 2356 } 2357 2358 static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) { 2359 uint64_t k = i*j; 2360 if (j > 1 && k / j != i) Overflow = true; 2361 return k; 2362 } 2363 2364 /// Compute the result of "n choose k", the binomial coefficient. If an 2365 /// intermediate computation overflows, Overflow will be set and the return will 2366 /// be garbage. Overflow is not cleared on absence of overflow. 2367 static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) { 2368 // We use the multiplicative formula: 2369 // n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 . 2370 // At each iteration, we take the n-th term of the numeral and divide by the 2371 // (k-n)th term of the denominator. This division will always produce an 2372 // integral result, and helps reduce the chance of overflow in the 2373 // intermediate computations. However, we can still overflow even when the 2374 // final result would fit. 2375 2376 if (n == 0 || n == k) return 1; 2377 if (k > n) return 0; 2378 2379 if (k > n/2) 2380 k = n-k; 2381 2382 uint64_t r = 1; 2383 for (uint64_t i = 1; i <= k; ++i) { 2384 r = umul_ov(r, n-(i-1), Overflow); 2385 r /= i; 2386 } 2387 return r; 2388 } 2389 2390 /// Determine if any of the operands in this SCEV are a constant or if 2391 /// any of the add or multiply expressions in this SCEV contain a constant. 2392 static bool containsConstantSomewhere(const SCEV *StartExpr) { 2393 SmallVector<const SCEV *, 4> Ops; 2394 Ops.push_back(StartExpr); 2395 while (!Ops.empty()) { 2396 const SCEV *CurrentExpr = Ops.pop_back_val(); 2397 if (isa<SCEVConstant>(*CurrentExpr)) 2398 return true; 2399 2400 if (isa<SCEVAddExpr>(*CurrentExpr) || isa<SCEVMulExpr>(*CurrentExpr)) { 2401 const auto *CurrentNAry = cast<SCEVNAryExpr>(CurrentExpr); 2402 Ops.append(CurrentNAry->op_begin(), CurrentNAry->op_end()); 2403 } 2404 } 2405 return false; 2406 } 2407 2408 /// getMulExpr - Get a canonical multiply expression, or something simpler if 2409 /// possible. 2410 const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops, 2411 SCEV::NoWrapFlags Flags) { 2412 assert(Flags == maskFlags(Flags, SCEV::FlagNUW | SCEV::FlagNSW) && 2413 "only nuw or nsw allowed"); 2414 assert(!Ops.empty() && "Cannot get empty mul!"); 2415 if (Ops.size() == 1) return Ops[0]; 2416 #ifndef NDEBUG 2417 Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); 2418 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 2419 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && 2420 "SCEVMulExpr operand types don't match!"); 2421 #endif 2422 2423 // Sort by complexity, this groups all similar expression types together. 2424 GroupByComplexity(Ops, &LI); 2425 2426 Flags = StrengthenNoWrapFlags(this, scMulExpr, Ops, Flags); 2427 2428 // If there are any constants, fold them together. 2429 unsigned Idx = 0; 2430 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 2431 2432 // C1*(C2+V) -> C1*C2 + C1*V 2433 if (Ops.size() == 2) 2434 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) 2435 // If any of Add's ops are Adds or Muls with a constant, 2436 // apply this transformation as well. 2437 if (Add->getNumOperands() == 2) 2438 if (containsConstantSomewhere(Add)) 2439 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), 2440 getMulExpr(LHSC, Add->getOperand(1))); 2441 2442 ++Idx; 2443 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 2444 // We found two constants, fold them together! 2445 ConstantInt *Fold = 2446 ConstantInt::get(getContext(), LHSC->getAPInt() * RHSC->getAPInt()); 2447 Ops[0] = getConstant(Fold); 2448 Ops.erase(Ops.begin()+1); // Erase the folded element 2449 if (Ops.size() == 1) return Ops[0]; 2450 LHSC = cast<SCEVConstant>(Ops[0]); 2451 } 2452 2453 // If we are left with a constant one being multiplied, strip it off. 2454 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { 2455 Ops.erase(Ops.begin()); 2456 --Idx; 2457 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { 2458 // If we have a multiply of zero, it will always be zero. 2459 return Ops[0]; 2460 } else if (Ops[0]->isAllOnesValue()) { 2461 // If we have a mul by -1 of an add, try distributing the -1 among the 2462 // add operands. 2463 if (Ops.size() == 2) { 2464 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) { 2465 SmallVector<const SCEV *, 4> NewOps; 2466 bool AnyFolded = false; 2467 for (const SCEV *AddOp : Add->operands()) { 2468 const SCEV *Mul = getMulExpr(Ops[0], AddOp); 2469 if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true; 2470 NewOps.push_back(Mul); 2471 } 2472 if (AnyFolded) 2473 return getAddExpr(NewOps); 2474 } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) { 2475 // Negation preserves a recurrence's no self-wrap property. 2476 SmallVector<const SCEV *, 4> Operands; 2477 for (const SCEV *AddRecOp : AddRec->operands()) 2478 Operands.push_back(getMulExpr(Ops[0], AddRecOp)); 2479 2480 return getAddRecExpr(Operands, AddRec->getLoop(), 2481 AddRec->getNoWrapFlags(SCEV::FlagNW)); 2482 } 2483 } 2484 } 2485 2486 if (Ops.size() == 1) 2487 return Ops[0]; 2488 } 2489 2490 // Skip over the add expression until we get to a multiply. 2491 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) 2492 ++Idx; 2493 2494 // If there are mul operands inline them all into this expression. 2495 if (Idx < Ops.size()) { 2496 bool DeletedMul = false; 2497 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { 2498 // If we have an mul, expand the mul operands onto the end of the operands 2499 // list. 2500 Ops.erase(Ops.begin()+Idx); 2501 Ops.append(Mul->op_begin(), Mul->op_end()); 2502 DeletedMul = true; 2503 } 2504 2505 // If we deleted at least one mul, we added operands to the end of the list, 2506 // and they are not necessarily sorted. Recurse to resort and resimplify 2507 // any operands we just acquired. 2508 if (DeletedMul) 2509 return getMulExpr(Ops); 2510 } 2511 2512 // If there are any add recurrences in the operands list, see if any other 2513 // added values are loop invariant. If so, we can fold them into the 2514 // recurrence. 2515 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) 2516 ++Idx; 2517 2518 // Scan over all recurrences, trying to fold loop invariants into them. 2519 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { 2520 // Scan all of the other operands to this mul and add them to the vector if 2521 // they are loop invariant w.r.t. the recurrence. 2522 SmallVector<const SCEV *, 8> LIOps; 2523 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); 2524 const Loop *AddRecLoop = AddRec->getLoop(); 2525 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 2526 if (isLoopInvariant(Ops[i], AddRecLoop)) { 2527 LIOps.push_back(Ops[i]); 2528 Ops.erase(Ops.begin()+i); 2529 --i; --e; 2530 } 2531 2532 // If we found some loop invariants, fold them into the recurrence. 2533 if (!LIOps.empty()) { 2534 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} 2535 SmallVector<const SCEV *, 4> NewOps; 2536 NewOps.reserve(AddRec->getNumOperands()); 2537 const SCEV *Scale = getMulExpr(LIOps); 2538 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) 2539 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); 2540 2541 // Build the new addrec. Propagate the NUW and NSW flags if both the 2542 // outer mul and the inner addrec are guaranteed to have no overflow. 2543 // 2544 // No self-wrap cannot be guaranteed after changing the step size, but 2545 // will be inferred if either NUW or NSW is true. 2546 Flags = AddRec->getNoWrapFlags(clearFlags(Flags, SCEV::FlagNW)); 2547 const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, Flags); 2548 2549 // If all of the other operands were loop invariant, we are done. 2550 if (Ops.size() == 1) return NewRec; 2551 2552 // Otherwise, multiply the folded AddRec by the non-invariant parts. 2553 for (unsigned i = 0;; ++i) 2554 if (Ops[i] == AddRec) { 2555 Ops[i] = NewRec; 2556 break; 2557 } 2558 return getMulExpr(Ops); 2559 } 2560 2561 // Okay, if there weren't any loop invariants to be folded, check to see if 2562 // there are multiple AddRec's with the same loop induction variable being 2563 // multiplied together. If so, we can fold them. 2564 2565 // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L> 2566 // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [ 2567 // choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z 2568 // ]]],+,...up to x=2n}. 2569 // Note that the arguments to choose() are always integers with values 2570 // known at compile time, never SCEV objects. 2571 // 2572 // The implementation avoids pointless extra computations when the two 2573 // addrec's are of different length (mathematically, it's equivalent to 2574 // an infinite stream of zeros on the right). 2575 bool OpsModified = false; 2576 for (unsigned OtherIdx = Idx+1; 2577 OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]); 2578 ++OtherIdx) { 2579 const SCEVAddRecExpr *OtherAddRec = 2580 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]); 2581 if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop) 2582 continue; 2583 2584 bool Overflow = false; 2585 Type *Ty = AddRec->getType(); 2586 bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64; 2587 SmallVector<const SCEV*, 7> AddRecOps; 2588 for (int x = 0, xe = AddRec->getNumOperands() + 2589 OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) { 2590 const SCEV *Term = getZero(Ty); 2591 for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) { 2592 uint64_t Coeff1 = Choose(x, 2*x - y, Overflow); 2593 for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1), 2594 ze = std::min(x+1, (int)OtherAddRec->getNumOperands()); 2595 z < ze && !Overflow; ++z) { 2596 uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow); 2597 uint64_t Coeff; 2598 if (LargerThan64Bits) 2599 Coeff = umul_ov(Coeff1, Coeff2, Overflow); 2600 else 2601 Coeff = Coeff1*Coeff2; 2602 const SCEV *CoeffTerm = getConstant(Ty, Coeff); 2603 const SCEV *Term1 = AddRec->getOperand(y-z); 2604 const SCEV *Term2 = OtherAddRec->getOperand(z); 2605 Term = getAddExpr(Term, getMulExpr(CoeffTerm, Term1,Term2)); 2606 } 2607 } 2608 AddRecOps.push_back(Term); 2609 } 2610 if (!Overflow) { 2611 const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(), 2612 SCEV::FlagAnyWrap); 2613 if (Ops.size() == 2) return NewAddRec; 2614 Ops[Idx] = NewAddRec; 2615 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx; 2616 OpsModified = true; 2617 AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec); 2618 if (!AddRec) 2619 break; 2620 } 2621 } 2622 if (OpsModified) 2623 return getMulExpr(Ops); 2624 2625 // Otherwise couldn't fold anything into this recurrence. Move onto the 2626 // next one. 2627 } 2628 2629 // Okay, it looks like we really DO need an mul expr. Check to see if we 2630 // already have one, otherwise create a new one. 2631 FoldingSetNodeID ID; 2632 ID.AddInteger(scMulExpr); 2633 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 2634 ID.AddPointer(Ops[i]); 2635 void *IP = nullptr; 2636 SCEVMulExpr *S = 2637 static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); 2638 if (!S) { 2639 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 2640 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 2641 S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator), 2642 O, Ops.size()); 2643 UniqueSCEVs.InsertNode(S, IP); 2644 } 2645 S->setNoWrapFlags(Flags); 2646 return S; 2647 } 2648 2649 /// getUDivExpr - Get a canonical unsigned division expression, or something 2650 /// simpler if possible. 2651 const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, 2652 const SCEV *RHS) { 2653 assert(getEffectiveSCEVType(LHS->getType()) == 2654 getEffectiveSCEVType(RHS->getType()) && 2655 "SCEVUDivExpr operand types don't match!"); 2656 2657 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { 2658 if (RHSC->getValue()->equalsInt(1)) 2659 return LHS; // X udiv 1 --> x 2660 // If the denominator is zero, the result of the udiv is undefined. Don't 2661 // try to analyze it, because the resolution chosen here may differ from 2662 // the resolution chosen in other parts of the compiler. 2663 if (!RHSC->getValue()->isZero()) { 2664 // Determine if the division can be folded into the operands of 2665 // its operands. 2666 // TODO: Generalize this to non-constants by using known-bits information. 2667 Type *Ty = LHS->getType(); 2668 unsigned LZ = RHSC->getAPInt().countLeadingZeros(); 2669 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1; 2670 // For non-power-of-two values, effectively round the value up to the 2671 // nearest power of two. 2672 if (!RHSC->getAPInt().isPowerOf2()) 2673 ++MaxShiftAmt; 2674 IntegerType *ExtTy = 2675 IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt); 2676 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) 2677 if (const SCEVConstant *Step = 2678 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) { 2679 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. 2680 const APInt &StepInt = Step->getAPInt(); 2681 const APInt &DivInt = RHSC->getAPInt(); 2682 if (!StepInt.urem(DivInt) && 2683 getZeroExtendExpr(AR, ExtTy) == 2684 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), 2685 getZeroExtendExpr(Step, ExtTy), 2686 AR->getLoop(), SCEV::FlagAnyWrap)) { 2687 SmallVector<const SCEV *, 4> Operands; 2688 for (const SCEV *Op : AR->operands()) 2689 Operands.push_back(getUDivExpr(Op, RHS)); 2690 return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW); 2691 } 2692 /// Get a canonical UDivExpr for a recurrence. 2693 /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0. 2694 // We can currently only fold X%N if X is constant. 2695 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart()); 2696 if (StartC && !DivInt.urem(StepInt) && 2697 getZeroExtendExpr(AR, ExtTy) == 2698 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), 2699 getZeroExtendExpr(Step, ExtTy), 2700 AR->getLoop(), SCEV::FlagAnyWrap)) { 2701 const APInt &StartInt = StartC->getAPInt(); 2702 const APInt &StartRem = StartInt.urem(StepInt); 2703 if (StartRem != 0) 2704 LHS = getAddRecExpr(getConstant(StartInt - StartRem), Step, 2705 AR->getLoop(), SCEV::FlagNW); 2706 } 2707 } 2708 // (A*B)/C --> A*(B/C) if safe and B/C can be folded. 2709 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { 2710 SmallVector<const SCEV *, 4> Operands; 2711 for (const SCEV *Op : M->operands()) 2712 Operands.push_back(getZeroExtendExpr(Op, ExtTy)); 2713 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) 2714 // Find an operand that's safely divisible. 2715 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { 2716 const SCEV *Op = M->getOperand(i); 2717 const SCEV *Div = getUDivExpr(Op, RHSC); 2718 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { 2719 Operands = SmallVector<const SCEV *, 4>(M->op_begin(), 2720 M->op_end()); 2721 Operands[i] = Div; 2722 return getMulExpr(Operands); 2723 } 2724 } 2725 } 2726 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. 2727 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) { 2728 SmallVector<const SCEV *, 4> Operands; 2729 for (const SCEV *Op : A->operands()) 2730 Operands.push_back(getZeroExtendExpr(Op, ExtTy)); 2731 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { 2732 Operands.clear(); 2733 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { 2734 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS); 2735 if (isa<SCEVUDivExpr>(Op) || 2736 getMulExpr(Op, RHS) != A->getOperand(i)) 2737 break; 2738 Operands.push_back(Op); 2739 } 2740 if (Operands.size() == A->getNumOperands()) 2741 return getAddExpr(Operands); 2742 } 2743 } 2744 2745 // Fold if both operands are constant. 2746 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { 2747 Constant *LHSCV = LHSC->getValue(); 2748 Constant *RHSCV = RHSC->getValue(); 2749 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, 2750 RHSCV))); 2751 } 2752 } 2753 } 2754 2755 FoldingSetNodeID ID; 2756 ID.AddInteger(scUDivExpr); 2757 ID.AddPointer(LHS); 2758 ID.AddPointer(RHS); 2759 void *IP = nullptr; 2760 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 2761 SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator), 2762 LHS, RHS); 2763 UniqueSCEVs.InsertNode(S, IP); 2764 return S; 2765 } 2766 2767 static const APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) { 2768 APInt A = C1->getAPInt().abs(); 2769 APInt B = C2->getAPInt().abs(); 2770 uint32_t ABW = A.getBitWidth(); 2771 uint32_t BBW = B.getBitWidth(); 2772 2773 if (ABW > BBW) 2774 B = B.zext(ABW); 2775 else if (ABW < BBW) 2776 A = A.zext(BBW); 2777 2778 return APIntOps::GreatestCommonDivisor(A, B); 2779 } 2780 2781 /// getUDivExactExpr - Get a canonical unsigned division expression, or 2782 /// something simpler if possible. There is no representation for an exact udiv 2783 /// in SCEV IR, but we can attempt to remove factors from the LHS and RHS. 2784 /// We can't do this when it's not exact because the udiv may be clearing bits. 2785 const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS, 2786 const SCEV *RHS) { 2787 // TODO: we could try to find factors in all sorts of things, but for now we 2788 // just deal with u/exact (multiply, constant). See SCEVDivision towards the 2789 // end of this file for inspiration. 2790 2791 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS); 2792 if (!Mul) 2793 return getUDivExpr(LHS, RHS); 2794 2795 if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) { 2796 // If the mulexpr multiplies by a constant, then that constant must be the 2797 // first element of the mulexpr. 2798 if (const auto *LHSCst = dyn_cast<SCEVConstant>(Mul->getOperand(0))) { 2799 if (LHSCst == RHSCst) { 2800 SmallVector<const SCEV *, 2> Operands; 2801 Operands.append(Mul->op_begin() + 1, Mul->op_end()); 2802 return getMulExpr(Operands); 2803 } 2804 2805 // We can't just assume that LHSCst divides RHSCst cleanly, it could be 2806 // that there's a factor provided by one of the other terms. We need to 2807 // check. 2808 APInt Factor = gcd(LHSCst, RHSCst); 2809 if (!Factor.isIntN(1)) { 2810 LHSCst = 2811 cast<SCEVConstant>(getConstant(LHSCst->getAPInt().udiv(Factor))); 2812 RHSCst = 2813 cast<SCEVConstant>(getConstant(RHSCst->getAPInt().udiv(Factor))); 2814 SmallVector<const SCEV *, 2> Operands; 2815 Operands.push_back(LHSCst); 2816 Operands.append(Mul->op_begin() + 1, Mul->op_end()); 2817 LHS = getMulExpr(Operands); 2818 RHS = RHSCst; 2819 Mul = dyn_cast<SCEVMulExpr>(LHS); 2820 if (!Mul) 2821 return getUDivExactExpr(LHS, RHS); 2822 } 2823 } 2824 } 2825 2826 for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) { 2827 if (Mul->getOperand(i) == RHS) { 2828 SmallVector<const SCEV *, 2> Operands; 2829 Operands.append(Mul->op_begin(), Mul->op_begin() + i); 2830 Operands.append(Mul->op_begin() + i + 1, Mul->op_end()); 2831 return getMulExpr(Operands); 2832 } 2833 } 2834 2835 return getUDivExpr(LHS, RHS); 2836 } 2837 2838 /// getAddRecExpr - Get an add recurrence expression for the specified loop. 2839 /// Simplify the expression as much as possible. 2840 const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step, 2841 const Loop *L, 2842 SCEV::NoWrapFlags Flags) { 2843 SmallVector<const SCEV *, 4> Operands; 2844 Operands.push_back(Start); 2845 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) 2846 if (StepChrec->getLoop() == L) { 2847 Operands.append(StepChrec->op_begin(), StepChrec->op_end()); 2848 return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW)); 2849 } 2850 2851 Operands.push_back(Step); 2852 return getAddRecExpr(Operands, L, Flags); 2853 } 2854 2855 /// getAddRecExpr - Get an add recurrence expression for the specified loop. 2856 /// Simplify the expression as much as possible. 2857 const SCEV * 2858 ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands, 2859 const Loop *L, SCEV::NoWrapFlags Flags) { 2860 if (Operands.size() == 1) return Operands[0]; 2861 #ifndef NDEBUG 2862 Type *ETy = getEffectiveSCEVType(Operands[0]->getType()); 2863 for (unsigned i = 1, e = Operands.size(); i != e; ++i) 2864 assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy && 2865 "SCEVAddRecExpr operand types don't match!"); 2866 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 2867 assert(isLoopInvariant(Operands[i], L) && 2868 "SCEVAddRecExpr operand is not loop-invariant!"); 2869 #endif 2870 2871 if (Operands.back()->isZero()) { 2872 Operands.pop_back(); 2873 return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X 2874 } 2875 2876 // It's tempting to want to call getMaxBackedgeTakenCount count here and 2877 // use that information to infer NUW and NSW flags. However, computing a 2878 // BE count requires calling getAddRecExpr, so we may not yet have a 2879 // meaningful BE count at this point (and if we don't, we'd be stuck 2880 // with a SCEVCouldNotCompute as the cached BE count). 2881 2882 Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags); 2883 2884 // Canonicalize nested AddRecs in by nesting them in order of loop depth. 2885 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { 2886 const Loop *NestedLoop = NestedAR->getLoop(); 2887 if (L->contains(NestedLoop) 2888 ? (L->getLoopDepth() < NestedLoop->getLoopDepth()) 2889 : (!NestedLoop->contains(L) && 2890 DT.dominates(L->getHeader(), NestedLoop->getHeader()))) { 2891 SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(), 2892 NestedAR->op_end()); 2893 Operands[0] = NestedAR->getStart(); 2894 // AddRecs require their operands be loop-invariant with respect to their 2895 // loops. Don't perform this transformation if it would break this 2896 // requirement. 2897 bool AllInvariant = all_of( 2898 Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); }); 2899 2900 if (AllInvariant) { 2901 // Create a recurrence for the outer loop with the same step size. 2902 // 2903 // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the 2904 // inner recurrence has the same property. 2905 SCEV::NoWrapFlags OuterFlags = 2906 maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags()); 2907 2908 NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags); 2909 AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) { 2910 return isLoopInvariant(Op, NestedLoop); 2911 }); 2912 2913 if (AllInvariant) { 2914 // Ok, both add recurrences are valid after the transformation. 2915 // 2916 // The inner recurrence keeps its NW flag but only keeps NUW/NSW if 2917 // the outer recurrence has the same property. 2918 SCEV::NoWrapFlags InnerFlags = 2919 maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags); 2920 return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags); 2921 } 2922 } 2923 // Reset Operands to its original state. 2924 Operands[0] = NestedAR; 2925 } 2926 } 2927 2928 // Okay, it looks like we really DO need an addrec expr. Check to see if we 2929 // already have one, otherwise create a new one. 2930 FoldingSetNodeID ID; 2931 ID.AddInteger(scAddRecExpr); 2932 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 2933 ID.AddPointer(Operands[i]); 2934 ID.AddPointer(L); 2935 void *IP = nullptr; 2936 SCEVAddRecExpr *S = 2937 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP)); 2938 if (!S) { 2939 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Operands.size()); 2940 std::uninitialized_copy(Operands.begin(), Operands.end(), O); 2941 S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator), 2942 O, Operands.size(), L); 2943 UniqueSCEVs.InsertNode(S, IP); 2944 } 2945 S->setNoWrapFlags(Flags); 2946 return S; 2947 } 2948 2949 const SCEV * 2950 ScalarEvolution::getGEPExpr(Type *PointeeType, const SCEV *BaseExpr, 2951 const SmallVectorImpl<const SCEV *> &IndexExprs, 2952 bool InBounds) { 2953 // getSCEV(Base)->getType() has the same address space as Base->getType() 2954 // because SCEV::getType() preserves the address space. 2955 Type *IntPtrTy = getEffectiveSCEVType(BaseExpr->getType()); 2956 // FIXME(PR23527): Don't blindly transfer the inbounds flag from the GEP 2957 // instruction to its SCEV, because the Instruction may be guarded by control 2958 // flow and the no-overflow bits may not be valid for the expression in any 2959 // context. This can be fixed similarly to how these flags are handled for 2960 // adds. 2961 SCEV::NoWrapFlags Wrap = InBounds ? SCEV::FlagNSW : SCEV::FlagAnyWrap; 2962 2963 const SCEV *TotalOffset = getZero(IntPtrTy); 2964 // The address space is unimportant. The first thing we do on CurTy is getting 2965 // its element type. 2966 Type *CurTy = PointerType::getUnqual(PointeeType); 2967 for (const SCEV *IndexExpr : IndexExprs) { 2968 // Compute the (potentially symbolic) offset in bytes for this index. 2969 if (StructType *STy = dyn_cast<StructType>(CurTy)) { 2970 // For a struct, add the member offset. 2971 ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue(); 2972 unsigned FieldNo = Index->getZExtValue(); 2973 const SCEV *FieldOffset = getOffsetOfExpr(IntPtrTy, STy, FieldNo); 2974 2975 // Add the field offset to the running total offset. 2976 TotalOffset = getAddExpr(TotalOffset, FieldOffset); 2977 2978 // Update CurTy to the type of the field at Index. 2979 CurTy = STy->getTypeAtIndex(Index); 2980 } else { 2981 // Update CurTy to its element type. 2982 CurTy = cast<SequentialType>(CurTy)->getElementType(); 2983 // For an array, add the element offset, explicitly scaled. 2984 const SCEV *ElementSize = getSizeOfExpr(IntPtrTy, CurTy); 2985 // Getelementptr indices are signed. 2986 IndexExpr = getTruncateOrSignExtend(IndexExpr, IntPtrTy); 2987 2988 // Multiply the index by the element size to compute the element offset. 2989 const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, Wrap); 2990 2991 // Add the element offset to the running total offset. 2992 TotalOffset = getAddExpr(TotalOffset, LocalOffset); 2993 } 2994 } 2995 2996 // Add the total offset from all the GEP indices to the base. 2997 return getAddExpr(BaseExpr, TotalOffset, Wrap); 2998 } 2999 3000 const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, 3001 const SCEV *RHS) { 3002 SmallVector<const SCEV *, 2> Ops; 3003 Ops.push_back(LHS); 3004 Ops.push_back(RHS); 3005 return getSMaxExpr(Ops); 3006 } 3007 3008 const SCEV * 3009 ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { 3010 assert(!Ops.empty() && "Cannot get empty smax!"); 3011 if (Ops.size() == 1) return Ops[0]; 3012 #ifndef NDEBUG 3013 Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); 3014 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 3015 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && 3016 "SCEVSMaxExpr operand types don't match!"); 3017 #endif 3018 3019 // Sort by complexity, this groups all similar expression types together. 3020 GroupByComplexity(Ops, &LI); 3021 3022 // If there are any constants, fold them together. 3023 unsigned Idx = 0; 3024 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 3025 ++Idx; 3026 assert(Idx < Ops.size()); 3027 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 3028 // We found two constants, fold them together! 3029 ConstantInt *Fold = ConstantInt::get( 3030 getContext(), APIntOps::smax(LHSC->getAPInt(), RHSC->getAPInt())); 3031 Ops[0] = getConstant(Fold); 3032 Ops.erase(Ops.begin()+1); // Erase the folded element 3033 if (Ops.size() == 1) return Ops[0]; 3034 LHSC = cast<SCEVConstant>(Ops[0]); 3035 } 3036 3037 // If we are left with a constant minimum-int, strip it off. 3038 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { 3039 Ops.erase(Ops.begin()); 3040 --Idx; 3041 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { 3042 // If we have an smax with a constant maximum-int, it will always be 3043 // maximum-int. 3044 return Ops[0]; 3045 } 3046 3047 if (Ops.size() == 1) return Ops[0]; 3048 } 3049 3050 // Find the first SMax 3051 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) 3052 ++Idx; 3053 3054 // Check to see if one of the operands is an SMax. If so, expand its operands 3055 // onto our operand list, and recurse to simplify. 3056 if (Idx < Ops.size()) { 3057 bool DeletedSMax = false; 3058 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { 3059 Ops.erase(Ops.begin()+Idx); 3060 Ops.append(SMax->op_begin(), SMax->op_end()); 3061 DeletedSMax = true; 3062 } 3063 3064 if (DeletedSMax) 3065 return getSMaxExpr(Ops); 3066 } 3067 3068 // Okay, check to see if the same value occurs in the operand list twice. If 3069 // so, delete one. Since we sorted the list, these values are required to 3070 // be adjacent. 3071 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) 3072 // X smax Y smax Y --> X smax Y 3073 // X smax Y --> X, if X is always greater than Y 3074 if (Ops[i] == Ops[i+1] || 3075 isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) { 3076 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2); 3077 --i; --e; 3078 } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) { 3079 Ops.erase(Ops.begin()+i, Ops.begin()+i+1); 3080 --i; --e; 3081 } 3082 3083 if (Ops.size() == 1) return Ops[0]; 3084 3085 assert(!Ops.empty() && "Reduced smax down to nothing!"); 3086 3087 // Okay, it looks like we really DO need an smax expr. Check to see if we 3088 // already have one, otherwise create a new one. 3089 FoldingSetNodeID ID; 3090 ID.AddInteger(scSMaxExpr); 3091 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 3092 ID.AddPointer(Ops[i]); 3093 void *IP = nullptr; 3094 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 3095 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 3096 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 3097 SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator), 3098 O, Ops.size()); 3099 UniqueSCEVs.InsertNode(S, IP); 3100 return S; 3101 } 3102 3103 const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, 3104 const SCEV *RHS) { 3105 SmallVector<const SCEV *, 2> Ops; 3106 Ops.push_back(LHS); 3107 Ops.push_back(RHS); 3108 return getUMaxExpr(Ops); 3109 } 3110 3111 const SCEV * 3112 ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) { 3113 assert(!Ops.empty() && "Cannot get empty umax!"); 3114 if (Ops.size() == 1) return Ops[0]; 3115 #ifndef NDEBUG 3116 Type *ETy = getEffectiveSCEVType(Ops[0]->getType()); 3117 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 3118 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy && 3119 "SCEVUMaxExpr operand types don't match!"); 3120 #endif 3121 3122 // Sort by complexity, this groups all similar expression types together. 3123 GroupByComplexity(Ops, &LI); 3124 3125 // If there are any constants, fold them together. 3126 unsigned Idx = 0; 3127 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 3128 ++Idx; 3129 assert(Idx < Ops.size()); 3130 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 3131 // We found two constants, fold them together! 3132 ConstantInt *Fold = ConstantInt::get( 3133 getContext(), APIntOps::umax(LHSC->getAPInt(), RHSC->getAPInt())); 3134 Ops[0] = getConstant(Fold); 3135 Ops.erase(Ops.begin()+1); // Erase the folded element 3136 if (Ops.size() == 1) return Ops[0]; 3137 LHSC = cast<SCEVConstant>(Ops[0]); 3138 } 3139 3140 // If we are left with a constant minimum-int, strip it off. 3141 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { 3142 Ops.erase(Ops.begin()); 3143 --Idx; 3144 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { 3145 // If we have an umax with a constant maximum-int, it will always be 3146 // maximum-int. 3147 return Ops[0]; 3148 } 3149 3150 if (Ops.size() == 1) return Ops[0]; 3151 } 3152 3153 // Find the first UMax 3154 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) 3155 ++Idx; 3156 3157 // Check to see if one of the operands is a UMax. If so, expand its operands 3158 // onto our operand list, and recurse to simplify. 3159 if (Idx < Ops.size()) { 3160 bool DeletedUMax = false; 3161 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { 3162 Ops.erase(Ops.begin()+Idx); 3163 Ops.append(UMax->op_begin(), UMax->op_end()); 3164 DeletedUMax = true; 3165 } 3166 3167 if (DeletedUMax) 3168 return getUMaxExpr(Ops); 3169 } 3170 3171 // Okay, check to see if the same value occurs in the operand list twice. If 3172 // so, delete one. Since we sorted the list, these values are required to 3173 // be adjacent. 3174 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) 3175 // X umax Y umax Y --> X umax Y 3176 // X umax Y --> X, if X is always greater than Y 3177 if (Ops[i] == Ops[i+1] || 3178 isKnownPredicate(ICmpInst::ICMP_UGE, Ops[i], Ops[i+1])) { 3179 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2); 3180 --i; --e; 3181 } else if (isKnownPredicate(ICmpInst::ICMP_ULE, Ops[i], Ops[i+1])) { 3182 Ops.erase(Ops.begin()+i, Ops.begin()+i+1); 3183 --i; --e; 3184 } 3185 3186 if (Ops.size() == 1) return Ops[0]; 3187 3188 assert(!Ops.empty() && "Reduced umax down to nothing!"); 3189 3190 // Okay, it looks like we really DO need a umax expr. Check to see if we 3191 // already have one, otherwise create a new one. 3192 FoldingSetNodeID ID; 3193 ID.AddInteger(scUMaxExpr); 3194 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 3195 ID.AddPointer(Ops[i]); 3196 void *IP = nullptr; 3197 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 3198 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size()); 3199 std::uninitialized_copy(Ops.begin(), Ops.end(), O); 3200 SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator), 3201 O, Ops.size()); 3202 UniqueSCEVs.InsertNode(S, IP); 3203 return S; 3204 } 3205 3206 const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, 3207 const SCEV *RHS) { 3208 // ~smax(~x, ~y) == smin(x, y). 3209 return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); 3210 } 3211 3212 const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, 3213 const SCEV *RHS) { 3214 // ~umax(~x, ~y) == umin(x, y) 3215 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); 3216 } 3217 3218 const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) { 3219 // We can bypass creating a target-independent 3220 // constant expression and then folding it back into a ConstantInt. 3221 // This is just a compile-time optimization. 3222 return getConstant(IntTy, getDataLayout().getTypeAllocSize(AllocTy)); 3223 } 3224 3225 const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy, 3226 StructType *STy, 3227 unsigned FieldNo) { 3228 // We can bypass creating a target-independent 3229 // constant expression and then folding it back into a ConstantInt. 3230 // This is just a compile-time optimization. 3231 return getConstant( 3232 IntTy, getDataLayout().getStructLayout(STy)->getElementOffset(FieldNo)); 3233 } 3234 3235 const SCEV *ScalarEvolution::getUnknown(Value *V) { 3236 // Don't attempt to do anything other than create a SCEVUnknown object 3237 // here. createSCEV only calls getUnknown after checking for all other 3238 // interesting possibilities, and any other code that calls getUnknown 3239 // is doing so in order to hide a value from SCEV canonicalization. 3240 3241 FoldingSetNodeID ID; 3242 ID.AddInteger(scUnknown); 3243 ID.AddPointer(V); 3244 void *IP = nullptr; 3245 if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) { 3246 assert(cast<SCEVUnknown>(S)->getValue() == V && 3247 "Stale SCEVUnknown in uniquing map!"); 3248 return S; 3249 } 3250 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this, 3251 FirstUnknown); 3252 FirstUnknown = cast<SCEVUnknown>(S); 3253 UniqueSCEVs.InsertNode(S, IP); 3254 return S; 3255 } 3256 3257 //===----------------------------------------------------------------------===// 3258 // Basic SCEV Analysis and PHI Idiom Recognition Code 3259 // 3260 3261 /// isSCEVable - Test if values of the given type are analyzable within 3262 /// the SCEV framework. This primarily includes integer types, and it 3263 /// can optionally include pointer types if the ScalarEvolution class 3264 /// has access to target-specific information. 3265 bool ScalarEvolution::isSCEVable(Type *Ty) const { 3266 // Integers and pointers are always SCEVable. 3267 return Ty->isIntegerTy() || Ty->isPointerTy(); 3268 } 3269 3270 /// getTypeSizeInBits - Return the size in bits of the specified type, 3271 /// for which isSCEVable must return true. 3272 uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const { 3273 assert(isSCEVable(Ty) && "Type is not SCEVable!"); 3274 return getDataLayout().getTypeSizeInBits(Ty); 3275 } 3276 3277 /// getEffectiveSCEVType - Return a type with the same bitwidth as 3278 /// the given type and which represents how SCEV will treat the given 3279 /// type, for which isSCEVable must return true. For pointer types, 3280 /// this is the pointer-sized integer type. 3281 Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const { 3282 assert(isSCEVable(Ty) && "Type is not SCEVable!"); 3283 3284 if (Ty->isIntegerTy()) 3285 return Ty; 3286 3287 // The only other support type is pointer. 3288 assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!"); 3289 return getDataLayout().getIntPtrType(Ty); 3290 } 3291 3292 const SCEV *ScalarEvolution::getCouldNotCompute() { 3293 return CouldNotCompute.get(); 3294 } 3295 3296 3297 bool ScalarEvolution::checkValidity(const SCEV *S) const { 3298 // Helper class working with SCEVTraversal to figure out if a SCEV contains 3299 // a SCEVUnknown with null value-pointer. FindInvalidSCEVUnknown::FindOne 3300 // is set iff if find such SCEVUnknown. 3301 // 3302 struct FindInvalidSCEVUnknown { 3303 bool FindOne; 3304 FindInvalidSCEVUnknown() { FindOne = false; } 3305 bool follow(const SCEV *S) { 3306 switch (static_cast<SCEVTypes>(S->getSCEVType())) { 3307 case scConstant: 3308 return false; 3309 case scUnknown: 3310 if (!cast<SCEVUnknown>(S)->getValue()) 3311 FindOne = true; 3312 return false; 3313 default: 3314 return true; 3315 } 3316 } 3317 bool isDone() const { return FindOne; } 3318 }; 3319 3320 FindInvalidSCEVUnknown F; 3321 SCEVTraversal<FindInvalidSCEVUnknown> ST(F); 3322 ST.visitAll(S); 3323 3324 return !F.FindOne; 3325 } 3326 3327 namespace { 3328 // Helper class working with SCEVTraversal to figure out if a SCEV contains 3329 // a sub SCEV of scAddRecExpr type. FindInvalidSCEVUnknown::FoundOne is set 3330 // iff if such sub scAddRecExpr type SCEV is found. 3331 struct FindAddRecurrence { 3332 bool FoundOne; 3333 FindAddRecurrence() : FoundOne(false) {} 3334 3335 bool follow(const SCEV *S) { 3336 switch (static_cast<SCEVTypes>(S->getSCEVType())) { 3337 case scAddRecExpr: 3338 FoundOne = true; 3339 case scConstant: 3340 case scUnknown: 3341 case scCouldNotCompute: 3342 return false; 3343 default: 3344 return true; 3345 } 3346 } 3347 bool isDone() const { return FoundOne; } 3348 }; 3349 } 3350 3351 bool ScalarEvolution::containsAddRecurrence(const SCEV *S) { 3352 HasRecMapType::iterator I = HasRecMap.find_as(S); 3353 if (I != HasRecMap.end()) 3354 return I->second; 3355 3356 FindAddRecurrence F; 3357 SCEVTraversal<FindAddRecurrence> ST(F); 3358 ST.visitAll(S); 3359 HasRecMap.insert({S, F.FoundOne}); 3360 return F.FoundOne; 3361 } 3362 3363 /// getSCEVValues - Return the Value set from S. 3364 SetVector<Value *> *ScalarEvolution::getSCEVValues(const SCEV *S) { 3365 ExprValueMapType::iterator SI = ExprValueMap.find_as(S); 3366 if (SI == ExprValueMap.end()) 3367 return nullptr; 3368 #ifndef NDEBUG 3369 if (VerifySCEVMap) { 3370 // Check there is no dangling Value in the set returned. 3371 for (const auto &VE : SI->second) 3372 assert(ValueExprMap.count(VE)); 3373 } 3374 #endif 3375 return &SI->second; 3376 } 3377 3378 /// eraseValueFromMap - Erase Value from ValueExprMap and ExprValueMap. 3379 /// If ValueExprMap.erase(V) is not used together with forgetMemoizedResults(S), 3380 /// eraseValueFromMap should be used instead to ensure whenever V->S is removed 3381 /// from ValueExprMap, V is also removed from the set of ExprValueMap[S]. 3382 void ScalarEvolution::eraseValueFromMap(Value *V) { 3383 ValueExprMapType::iterator I = ValueExprMap.find_as(V); 3384 if (I != ValueExprMap.end()) { 3385 const SCEV *S = I->second; 3386 SetVector<Value *> *SV = getSCEVValues(S); 3387 // Remove V from the set of ExprValueMap[S] 3388 if (SV) 3389 SV->remove(V); 3390 ValueExprMap.erase(V); 3391 } 3392 } 3393 3394 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the 3395 /// expression and create a new one. 3396 const SCEV *ScalarEvolution::getSCEV(Value *V) { 3397 assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); 3398 3399 const SCEV *S = getExistingSCEV(V); 3400 if (S == nullptr) { 3401 S = createSCEV(V); 3402 // During PHI resolution, it is possible to create two SCEVs for the same 3403 // V, so it is needed to double check whether V->S is inserted into 3404 // ValueExprMap before insert S->V into ExprValueMap. 3405 std::pair<ValueExprMapType::iterator, bool> Pair = 3406 ValueExprMap.insert({SCEVCallbackVH(V, this), S}); 3407 if (Pair.second) 3408 ExprValueMap[S].insert(V); 3409 } 3410 return S; 3411 } 3412 3413 const SCEV *ScalarEvolution::getExistingSCEV(Value *V) { 3414 assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); 3415 3416 ValueExprMapType::iterator I = ValueExprMap.find_as(V); 3417 if (I != ValueExprMap.end()) { 3418 const SCEV *S = I->second; 3419 if (checkValidity(S)) 3420 return S; 3421 forgetMemoizedResults(S); 3422 ValueExprMap.erase(I); 3423 } 3424 return nullptr; 3425 } 3426 3427 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V 3428 /// 3429 const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V, 3430 SCEV::NoWrapFlags Flags) { 3431 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) 3432 return getConstant( 3433 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); 3434 3435 Type *Ty = V->getType(); 3436 Ty = getEffectiveSCEVType(Ty); 3437 return getMulExpr( 3438 V, getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))), Flags); 3439 } 3440 3441 /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V 3442 const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) { 3443 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) 3444 return getConstant( 3445 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); 3446 3447 Type *Ty = V->getType(); 3448 Ty = getEffectiveSCEVType(Ty); 3449 const SCEV *AllOnes = 3450 getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))); 3451 return getMinusSCEV(AllOnes, V); 3452 } 3453 3454 /// getMinusSCEV - Return LHS-RHS. Minus is represented in SCEV as A+B*-1. 3455 const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS, 3456 SCEV::NoWrapFlags Flags) { 3457 // Fast path: X - X --> 0. 3458 if (LHS == RHS) 3459 return getZero(LHS->getType()); 3460 3461 // We represent LHS - RHS as LHS + (-1)*RHS. This transformation 3462 // makes it so that we cannot make much use of NUW. 3463 auto AddFlags = SCEV::FlagAnyWrap; 3464 const bool RHSIsNotMinSigned = 3465 !getSignedRange(RHS).getSignedMin().isMinSignedValue(); 3466 if (maskFlags(Flags, SCEV::FlagNSW) == SCEV::FlagNSW) { 3467 // Let M be the minimum representable signed value. Then (-1)*RHS 3468 // signed-wraps if and only if RHS is M. That can happen even for 3469 // a NSW subtraction because e.g. (-1)*M signed-wraps even though 3470 // -1 - M does not. So to transfer NSW from LHS - RHS to LHS + 3471 // (-1)*RHS, we need to prove that RHS != M. 3472 // 3473 // If LHS is non-negative and we know that LHS - RHS does not 3474 // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap 3475 // either by proving that RHS > M or that LHS >= 0. 3476 if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) { 3477 AddFlags = SCEV::FlagNSW; 3478 } 3479 } 3480 3481 // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS - 3482 // RHS is NSW and LHS >= 0. 3483 // 3484 // The difficulty here is that the NSW flag may have been proven 3485 // relative to a loop that is to be found in a recurrence in LHS and 3486 // not in RHS. Applying NSW to (-1)*M may then let the NSW have a 3487 // larger scope than intended. 3488 auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap; 3489 3490 return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags); 3491 } 3492 3493 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the 3494 /// input value to the specified type. If the type must be extended, it is zero 3495 /// extended. 3496 const SCEV * 3497 ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty) { 3498 Type *SrcTy = V->getType(); 3499 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && 3500 (Ty->isIntegerTy() || Ty->isPointerTy()) && 3501 "Cannot truncate or zero extend with non-integer arguments!"); 3502 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 3503 return V; // No conversion 3504 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) 3505 return getTruncateExpr(V, Ty); 3506 return getZeroExtendExpr(V, Ty); 3507 } 3508 3509 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the 3510 /// input value to the specified type. If the type must be extended, it is sign 3511 /// extended. 3512 const SCEV * 3513 ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, 3514 Type *Ty) { 3515 Type *SrcTy = V->getType(); 3516 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && 3517 (Ty->isIntegerTy() || Ty->isPointerTy()) && 3518 "Cannot truncate or zero extend with non-integer arguments!"); 3519 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 3520 return V; // No conversion 3521 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) 3522 return getTruncateExpr(V, Ty); 3523 return getSignExtendExpr(V, Ty); 3524 } 3525 3526 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the 3527 /// input value to the specified type. If the type must be extended, it is zero 3528 /// extended. The conversion must not be narrowing. 3529 const SCEV * 3530 ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) { 3531 Type *SrcTy = V->getType(); 3532 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && 3533 (Ty->isIntegerTy() || Ty->isPointerTy()) && 3534 "Cannot noop or zero extend with non-integer arguments!"); 3535 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 3536 "getNoopOrZeroExtend cannot truncate!"); 3537 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 3538 return V; // No conversion 3539 return getZeroExtendExpr(V, Ty); 3540 } 3541 3542 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the 3543 /// input value to the specified type. If the type must be extended, it is sign 3544 /// extended. The conversion must not be narrowing. 3545 const SCEV * 3546 ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) { 3547 Type *SrcTy = V->getType(); 3548 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && 3549 (Ty->isIntegerTy() || Ty->isPointerTy()) && 3550 "Cannot noop or sign extend with non-integer arguments!"); 3551 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 3552 "getNoopOrSignExtend cannot truncate!"); 3553 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 3554 return V; // No conversion 3555 return getSignExtendExpr(V, Ty); 3556 } 3557 3558 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of 3559 /// the input value to the specified type. If the type must be extended, 3560 /// it is extended with unspecified bits. The conversion must not be 3561 /// narrowing. 3562 const SCEV * 3563 ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) { 3564 Type *SrcTy = V->getType(); 3565 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && 3566 (Ty->isIntegerTy() || Ty->isPointerTy()) && 3567 "Cannot noop or any extend with non-integer arguments!"); 3568 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 3569 "getNoopOrAnyExtend cannot truncate!"); 3570 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 3571 return V; // No conversion 3572 return getAnyExtendExpr(V, Ty); 3573 } 3574 3575 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the 3576 /// input value to the specified type. The conversion must not be widening. 3577 const SCEV * 3578 ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) { 3579 Type *SrcTy = V->getType(); 3580 assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) && 3581 (Ty->isIntegerTy() || Ty->isPointerTy()) && 3582 "Cannot truncate or noop with non-integer arguments!"); 3583 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && 3584 "getTruncateOrNoop cannot extend!"); 3585 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 3586 return V; // No conversion 3587 return getTruncateExpr(V, Ty); 3588 } 3589 3590 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of 3591 /// the types using zero-extension, and then perform a umax operation 3592 /// with them. 3593 const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, 3594 const SCEV *RHS) { 3595 const SCEV *PromotedLHS = LHS; 3596 const SCEV *PromotedRHS = RHS; 3597 3598 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) 3599 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); 3600 else 3601 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); 3602 3603 return getUMaxExpr(PromotedLHS, PromotedRHS); 3604 } 3605 3606 /// getUMinFromMismatchedTypes - Promote the operands to the wider of 3607 /// the types using zero-extension, and then perform a umin operation 3608 /// with them. 3609 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, 3610 const SCEV *RHS) { 3611 const SCEV *PromotedLHS = LHS; 3612 const SCEV *PromotedRHS = RHS; 3613 3614 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) 3615 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); 3616 else 3617 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); 3618 3619 return getUMinExpr(PromotedLHS, PromotedRHS); 3620 } 3621 3622 /// getPointerBase - Transitively follow the chain of pointer-type operands 3623 /// until reaching a SCEV that does not have a single pointer operand. This 3624 /// returns a SCEVUnknown pointer for well-formed pointer-type expressions, 3625 /// but corner cases do exist. 3626 const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) { 3627 // A pointer operand may evaluate to a nonpointer expression, such as null. 3628 if (!V->getType()->isPointerTy()) 3629 return V; 3630 3631 if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) { 3632 return getPointerBase(Cast->getOperand()); 3633 } else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) { 3634 const SCEV *PtrOp = nullptr; 3635 for (const SCEV *NAryOp : NAry->operands()) { 3636 if (NAryOp->getType()->isPointerTy()) { 3637 // Cannot find the base of an expression with multiple pointer operands. 3638 if (PtrOp) 3639 return V; 3640 PtrOp = NAryOp; 3641 } 3642 } 3643 if (!PtrOp) 3644 return V; 3645 return getPointerBase(PtrOp); 3646 } 3647 return V; 3648 } 3649 3650 /// PushDefUseChildren - Push users of the given Instruction 3651 /// onto the given Worklist. 3652 static void 3653 PushDefUseChildren(Instruction *I, 3654 SmallVectorImpl<Instruction *> &Worklist) { 3655 // Push the def-use children onto the Worklist stack. 3656 for (User *U : I->users()) 3657 Worklist.push_back(cast<Instruction>(U)); 3658 } 3659 3660 /// ForgetSymbolicValue - This looks up computed SCEV values for all 3661 /// instructions that depend on the given instruction and removes them from 3662 /// the ValueExprMapType map if they reference SymName. This is used during PHI 3663 /// resolution. 3664 void ScalarEvolution::forgetSymbolicName(Instruction *PN, const SCEV *SymName) { 3665 SmallVector<Instruction *, 16> Worklist; 3666 PushDefUseChildren(PN, Worklist); 3667 3668 SmallPtrSet<Instruction *, 8> Visited; 3669 Visited.insert(PN); 3670 while (!Worklist.empty()) { 3671 Instruction *I = Worklist.pop_back_val(); 3672 if (!Visited.insert(I).second) 3673 continue; 3674 3675 auto It = ValueExprMap.find_as(static_cast<Value *>(I)); 3676 if (It != ValueExprMap.end()) { 3677 const SCEV *Old = It->second; 3678 3679 // Short-circuit the def-use traversal if the symbolic name 3680 // ceases to appear in expressions. 3681 if (Old != SymName && !hasOperand(Old, SymName)) 3682 continue; 3683 3684 // SCEVUnknown for a PHI either means that it has an unrecognized 3685 // structure, it's a PHI that's in the progress of being computed 3686 // by createNodeForPHI, or it's a single-value PHI. In the first case, 3687 // additional loop trip count information isn't going to change anything. 3688 // In the second case, createNodeForPHI will perform the necessary 3689 // updates on its own when it gets to that point. In the third, we do 3690 // want to forget the SCEVUnknown. 3691 if (!isa<PHINode>(I) || 3692 !isa<SCEVUnknown>(Old) || 3693 (I != PN && Old == SymName)) { 3694 forgetMemoizedResults(Old); 3695 ValueExprMap.erase(It); 3696 } 3697 } 3698 3699 PushDefUseChildren(I, Worklist); 3700 } 3701 } 3702 3703 namespace { 3704 class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> { 3705 public: 3706 static const SCEV *rewrite(const SCEV *S, const Loop *L, 3707 ScalarEvolution &SE) { 3708 SCEVInitRewriter Rewriter(L, SE); 3709 const SCEV *Result = Rewriter.visit(S); 3710 return Rewriter.isValid() ? Result : SE.getCouldNotCompute(); 3711 } 3712 3713 SCEVInitRewriter(const Loop *L, ScalarEvolution &SE) 3714 : SCEVRewriteVisitor(SE), L(L), Valid(true) {} 3715 3716 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 3717 if (!(SE.getLoopDisposition(Expr, L) == ScalarEvolution::LoopInvariant)) 3718 Valid = false; 3719 return Expr; 3720 } 3721 3722 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { 3723 // Only allow AddRecExprs for this loop. 3724 if (Expr->getLoop() == L) 3725 return Expr->getStart(); 3726 Valid = false; 3727 return Expr; 3728 } 3729 3730 bool isValid() { return Valid; } 3731 3732 private: 3733 const Loop *L; 3734 bool Valid; 3735 }; 3736 3737 class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> { 3738 public: 3739 static const SCEV *rewrite(const SCEV *S, const Loop *L, 3740 ScalarEvolution &SE) { 3741 SCEVShiftRewriter Rewriter(L, SE); 3742 const SCEV *Result = Rewriter.visit(S); 3743 return Rewriter.isValid() ? Result : SE.getCouldNotCompute(); 3744 } 3745 3746 SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE) 3747 : SCEVRewriteVisitor(SE), L(L), Valid(true) {} 3748 3749 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 3750 // Only allow AddRecExprs for this loop. 3751 if (!(SE.getLoopDisposition(Expr, L) == ScalarEvolution::LoopInvariant)) 3752 Valid = false; 3753 return Expr; 3754 } 3755 3756 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { 3757 if (Expr->getLoop() == L && Expr->isAffine()) 3758 return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE)); 3759 Valid = false; 3760 return Expr; 3761 } 3762 bool isValid() { return Valid; } 3763 3764 private: 3765 const Loop *L; 3766 bool Valid; 3767 }; 3768 } // end anonymous namespace 3769 3770 SCEV::NoWrapFlags 3771 ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) { 3772 if (!AR->isAffine()) 3773 return SCEV::FlagAnyWrap; 3774 3775 typedef OverflowingBinaryOperator OBO; 3776 SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap; 3777 3778 if (!AR->hasNoSignedWrap()) { 3779 ConstantRange AddRecRange = getSignedRange(AR); 3780 ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this)); 3781 3782 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 3783 Instruction::Add, IncRange, OBO::NoSignedWrap); 3784 if (NSWRegion.contains(AddRecRange)) 3785 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW); 3786 } 3787 3788 if (!AR->hasNoUnsignedWrap()) { 3789 ConstantRange AddRecRange = getUnsignedRange(AR); 3790 ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this)); 3791 3792 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 3793 Instruction::Add, IncRange, OBO::NoUnsignedWrap); 3794 if (NUWRegion.contains(AddRecRange)) 3795 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW); 3796 } 3797 3798 return Result; 3799 } 3800 3801 namespace { 3802 /// Represents an abstract binary operation. This may exist as a 3803 /// normal instruction or constant expression, or may have been 3804 /// derived from an expression tree. 3805 struct BinaryOp { 3806 unsigned Opcode; 3807 Value *LHS; 3808 Value *RHS; 3809 bool IsNSW; 3810 bool IsNUW; 3811 3812 /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or 3813 /// constant expression. 3814 Operator *Op; 3815 3816 explicit BinaryOp(Operator *Op) 3817 : Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)), 3818 IsNSW(false), IsNUW(false), Op(Op) { 3819 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) { 3820 IsNSW = OBO->hasNoSignedWrap(); 3821 IsNUW = OBO->hasNoUnsignedWrap(); 3822 } 3823 } 3824 3825 explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false, 3826 bool IsNUW = false) 3827 : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW), 3828 Op(nullptr) {} 3829 }; 3830 } 3831 3832 3833 /// Try to map \p V into a BinaryOp, and return \c None on failure. 3834 static Optional<BinaryOp> MatchBinaryOp(Value *V) { 3835 auto *Op = dyn_cast<Operator>(V); 3836 if (!Op) 3837 return None; 3838 3839 // Implementation detail: all the cleverness here should happen without 3840 // creating new SCEV expressions -- our caller knowns tricks to avoid creating 3841 // SCEV expressions when possible, and we should not break that. 3842 3843 switch (Op->getOpcode()) { 3844 case Instruction::Add: 3845 case Instruction::Sub: 3846 case Instruction::Mul: 3847 case Instruction::UDiv: 3848 case Instruction::And: 3849 case Instruction::Or: 3850 case Instruction::AShr: 3851 case Instruction::Shl: 3852 return BinaryOp(Op); 3853 3854 case Instruction::Xor: 3855 if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1))) 3856 // If the RHS of the xor is a signbit, then this is just an add. 3857 // Instcombine turns add of signbit into xor as a strength reduction step. 3858 if (RHSC->getValue().isSignBit()) 3859 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1)); 3860 return BinaryOp(Op); 3861 3862 case Instruction::LShr: 3863 // Turn logical shift right of a constant into a unsigned divide. 3864 if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) { 3865 uint32_t BitWidth = cast<IntegerType>(Op->getType())->getBitWidth(); 3866 3867 // If the shift count is not less than the bitwidth, the result of 3868 // the shift is undefined. Don't try to analyze it, because the 3869 // resolution chosen here may differ from the resolution chosen in 3870 // other parts of the compiler. 3871 if (SA->getValue().ult(BitWidth)) { 3872 Constant *X = 3873 ConstantInt::get(SA->getContext(), 3874 APInt::getOneBitSet(BitWidth, SA->getZExtValue())); 3875 return BinaryOp(Instruction::UDiv, Op->getOperand(0), X); 3876 } 3877 } 3878 return BinaryOp(Op); 3879 3880 default: 3881 break; 3882 } 3883 3884 return None; 3885 } 3886 3887 const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) { 3888 const Loop *L = LI.getLoopFor(PN->getParent()); 3889 if (!L || L->getHeader() != PN->getParent()) 3890 return nullptr; 3891 3892 // The loop may have multiple entrances or multiple exits; we can analyze 3893 // this phi as an addrec if it has a unique entry value and a unique 3894 // backedge value. 3895 Value *BEValueV = nullptr, *StartValueV = nullptr; 3896 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 3897 Value *V = PN->getIncomingValue(i); 3898 if (L->contains(PN->getIncomingBlock(i))) { 3899 if (!BEValueV) { 3900 BEValueV = V; 3901 } else if (BEValueV != V) { 3902 BEValueV = nullptr; 3903 break; 3904 } 3905 } else if (!StartValueV) { 3906 StartValueV = V; 3907 } else if (StartValueV != V) { 3908 StartValueV = nullptr; 3909 break; 3910 } 3911 } 3912 if (BEValueV && StartValueV) { 3913 // While we are analyzing this PHI node, handle its value symbolically. 3914 const SCEV *SymbolicName = getUnknown(PN); 3915 assert(ValueExprMap.find_as(PN) == ValueExprMap.end() && 3916 "PHI node already processed?"); 3917 ValueExprMap.insert({SCEVCallbackVH(PN, this), SymbolicName}); 3918 3919 // Using this symbolic name for the PHI, analyze the value coming around 3920 // the back-edge. 3921 const SCEV *BEValue = getSCEV(BEValueV); 3922 3923 // NOTE: If BEValue is loop invariant, we know that the PHI node just 3924 // has a special value for the first iteration of the loop. 3925 3926 // If the value coming around the backedge is an add with the symbolic 3927 // value we just inserted, then we found a simple induction variable! 3928 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { 3929 // If there is a single occurrence of the symbolic value, replace it 3930 // with a recurrence. 3931 unsigned FoundIndex = Add->getNumOperands(); 3932 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) 3933 if (Add->getOperand(i) == SymbolicName) 3934 if (FoundIndex == e) { 3935 FoundIndex = i; 3936 break; 3937 } 3938 3939 if (FoundIndex != Add->getNumOperands()) { 3940 // Create an add with everything but the specified operand. 3941 SmallVector<const SCEV *, 8> Ops; 3942 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) 3943 if (i != FoundIndex) 3944 Ops.push_back(Add->getOperand(i)); 3945 const SCEV *Accum = getAddExpr(Ops); 3946 3947 // This is not a valid addrec if the step amount is varying each 3948 // loop iteration, but is not itself an addrec in this loop. 3949 if (isLoopInvariant(Accum, L) || 3950 (isa<SCEVAddRecExpr>(Accum) && 3951 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { 3952 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; 3953 3954 // If the increment doesn't overflow, then neither the addrec nor 3955 // the post-increment will overflow. 3956 if (auto BO = MatchBinaryOp(BEValueV)) { 3957 if (BO->Opcode == Instruction::Add && BO->LHS == PN) { 3958 if (BO->IsNUW) 3959 Flags = setFlags(Flags, SCEV::FlagNUW); 3960 if (BO->IsNSW) 3961 Flags = setFlags(Flags, SCEV::FlagNSW); 3962 } 3963 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) { 3964 // If the increment is an inbounds GEP, then we know the address 3965 // space cannot be wrapped around. We cannot make any guarantee 3966 // about signed or unsigned overflow because pointers are 3967 // unsigned but we may have a negative index from the base 3968 // pointer. We can guarantee that no unsigned wrap occurs if the 3969 // indices form a positive value. 3970 if (GEP->isInBounds() && GEP->getOperand(0) == PN) { 3971 Flags = setFlags(Flags, SCEV::FlagNW); 3972 3973 const SCEV *Ptr = getSCEV(GEP->getPointerOperand()); 3974 if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr))) 3975 Flags = setFlags(Flags, SCEV::FlagNUW); 3976 } 3977 3978 // We cannot transfer nuw and nsw flags from subtraction 3979 // operations -- sub nuw X, Y is not the same as add nuw X, -Y 3980 // for instance. 3981 } 3982 3983 const SCEV *StartVal = getSCEV(StartValueV); 3984 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags); 3985 3986 // Since the no-wrap flags are on the increment, they apply to the 3987 // post-incremented value as well. 3988 if (isLoopInvariant(Accum, L)) 3989 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags); 3990 3991 // Okay, for the entire analysis of this edge we assumed the PHI 3992 // to be symbolic. We now need to go back and purge all of the 3993 // entries for the scalars that use the symbolic expression. 3994 forgetSymbolicName(PN, SymbolicName); 3995 ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV; 3996 return PHISCEV; 3997 } 3998 } 3999 } else { 4000 // Otherwise, this could be a loop like this: 4001 // i = 0; for (j = 1; ..; ++j) { .... i = j; } 4002 // In this case, j = {1,+,1} and BEValue is j. 4003 // Because the other in-value of i (0) fits the evolution of BEValue 4004 // i really is an addrec evolution. 4005 // 4006 // We can generalize this saying that i is the shifted value of BEValue 4007 // by one iteration: 4008 // PHI(f(0), f({1,+,1})) --> f({0,+,1}) 4009 const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this); 4010 const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this); 4011 if (Shifted != getCouldNotCompute() && 4012 Start != getCouldNotCompute()) { 4013 const SCEV *StartVal = getSCEV(StartValueV); 4014 if (Start == StartVal) { 4015 // Okay, for the entire analysis of this edge we assumed the PHI 4016 // to be symbolic. We now need to go back and purge all of the 4017 // entries for the scalars that use the symbolic expression. 4018 forgetSymbolicName(PN, SymbolicName); 4019 ValueExprMap[SCEVCallbackVH(PN, this)] = Shifted; 4020 return Shifted; 4021 } 4022 } 4023 } 4024 4025 // Remove the temporary PHI node SCEV that has been inserted while intending 4026 // to create an AddRecExpr for this PHI node. We can not keep this temporary 4027 // as it will prevent later (possibly simpler) SCEV expressions to be added 4028 // to the ValueExprMap. 4029 ValueExprMap.erase(PN); 4030 } 4031 4032 return nullptr; 4033 } 4034 4035 // Checks if the SCEV S is available at BB. S is considered available at BB 4036 // if S can be materialized at BB without introducing a fault. 4037 static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S, 4038 BasicBlock *BB) { 4039 struct CheckAvailable { 4040 bool TraversalDone = false; 4041 bool Available = true; 4042 4043 const Loop *L = nullptr; // The loop BB is in (can be nullptr) 4044 BasicBlock *BB = nullptr; 4045 DominatorTree &DT; 4046 4047 CheckAvailable(const Loop *L, BasicBlock *BB, DominatorTree &DT) 4048 : L(L), BB(BB), DT(DT) {} 4049 4050 bool setUnavailable() { 4051 TraversalDone = true; 4052 Available = false; 4053 return false; 4054 } 4055 4056 bool follow(const SCEV *S) { 4057 switch (S->getSCEVType()) { 4058 case scConstant: case scTruncate: case scZeroExtend: case scSignExtend: 4059 case scAddExpr: case scMulExpr: case scUMaxExpr: case scSMaxExpr: 4060 // These expressions are available if their operand(s) is/are. 4061 return true; 4062 4063 case scAddRecExpr: { 4064 // We allow add recurrences that are on the loop BB is in, or some 4065 // outer loop. This guarantees availability because the value of the 4066 // add recurrence at BB is simply the "current" value of the induction 4067 // variable. We can relax this in the future; for instance an add 4068 // recurrence on a sibling dominating loop is also available at BB. 4069 const auto *ARLoop = cast<SCEVAddRecExpr>(S)->getLoop(); 4070 if (L && (ARLoop == L || ARLoop->contains(L))) 4071 return true; 4072 4073 return setUnavailable(); 4074 } 4075 4076 case scUnknown: { 4077 // For SCEVUnknown, we check for simple dominance. 4078 const auto *SU = cast<SCEVUnknown>(S); 4079 Value *V = SU->getValue(); 4080 4081 if (isa<Argument>(V)) 4082 return false; 4083 4084 if (isa<Instruction>(V) && DT.dominates(cast<Instruction>(V), BB)) 4085 return false; 4086 4087 return setUnavailable(); 4088 } 4089 4090 case scUDivExpr: 4091 case scCouldNotCompute: 4092 // We do not try to smart about these at all. 4093 return setUnavailable(); 4094 } 4095 llvm_unreachable("switch should be fully covered!"); 4096 } 4097 4098 bool isDone() { return TraversalDone; } 4099 }; 4100 4101 CheckAvailable CA(L, BB, DT); 4102 SCEVTraversal<CheckAvailable> ST(CA); 4103 4104 ST.visitAll(S); 4105 return CA.Available; 4106 } 4107 4108 // Try to match a control flow sequence that branches out at BI and merges back 4109 // at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful 4110 // match. 4111 static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge, 4112 Value *&C, Value *&LHS, Value *&RHS) { 4113 C = BI->getCondition(); 4114 4115 BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0)); 4116 BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1)); 4117 4118 if (!LeftEdge.isSingleEdge()) 4119 return false; 4120 4121 assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()"); 4122 4123 Use &LeftUse = Merge->getOperandUse(0); 4124 Use &RightUse = Merge->getOperandUse(1); 4125 4126 if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) { 4127 LHS = LeftUse; 4128 RHS = RightUse; 4129 return true; 4130 } 4131 4132 if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) { 4133 LHS = RightUse; 4134 RHS = LeftUse; 4135 return true; 4136 } 4137 4138 return false; 4139 } 4140 4141 const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) { 4142 if (PN->getNumIncomingValues() == 2) { 4143 const Loop *L = LI.getLoopFor(PN->getParent()); 4144 4145 // We don't want to break LCSSA, even in a SCEV expression tree. 4146 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 4147 if (LI.getLoopFor(PN->getIncomingBlock(i)) != L) 4148 return nullptr; 4149 4150 // Try to match 4151 // 4152 // br %cond, label %left, label %right 4153 // left: 4154 // br label %merge 4155 // right: 4156 // br label %merge 4157 // merge: 4158 // V = phi [ %x, %left ], [ %y, %right ] 4159 // 4160 // as "select %cond, %x, %y" 4161 4162 BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock(); 4163 assert(IDom && "At least the entry block should dominate PN"); 4164 4165 auto *BI = dyn_cast<BranchInst>(IDom->getTerminator()); 4166 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr; 4167 4168 if (BI && BI->isConditional() && 4169 BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) && 4170 IsAvailableOnEntry(L, DT, getSCEV(LHS), PN->getParent()) && 4171 IsAvailableOnEntry(L, DT, getSCEV(RHS), PN->getParent())) 4172 return createNodeForSelectOrPHI(PN, Cond, LHS, RHS); 4173 } 4174 4175 return nullptr; 4176 } 4177 4178 const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) { 4179 if (const SCEV *S = createAddRecFromPHI(PN)) 4180 return S; 4181 4182 if (const SCEV *S = createNodeFromSelectLikePHI(PN)) 4183 return S; 4184 4185 // If the PHI has a single incoming value, follow that value, unless the 4186 // PHI's incoming blocks are in a different loop, in which case doing so 4187 // risks breaking LCSSA form. Instcombine would normally zap these, but 4188 // it doesn't have DominatorTree information, so it may miss cases. 4189 if (Value *V = SimplifyInstruction(PN, getDataLayout(), &TLI, &DT, &AC)) 4190 if (LI.replacementPreservesLCSSAForm(PN, V)) 4191 return getSCEV(V); 4192 4193 // If it's not a loop phi, we can't handle it yet. 4194 return getUnknown(PN); 4195 } 4196 4197 const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Instruction *I, 4198 Value *Cond, 4199 Value *TrueVal, 4200 Value *FalseVal) { 4201 // Handle "constant" branch or select. This can occur for instance when a 4202 // loop pass transforms an inner loop and moves on to process the outer loop. 4203 if (auto *CI = dyn_cast<ConstantInt>(Cond)) 4204 return getSCEV(CI->isOne() ? TrueVal : FalseVal); 4205 4206 // Try to match some simple smax or umax patterns. 4207 auto *ICI = dyn_cast<ICmpInst>(Cond); 4208 if (!ICI) 4209 return getUnknown(I); 4210 4211 Value *LHS = ICI->getOperand(0); 4212 Value *RHS = ICI->getOperand(1); 4213 4214 switch (ICI->getPredicate()) { 4215 case ICmpInst::ICMP_SLT: 4216 case ICmpInst::ICMP_SLE: 4217 std::swap(LHS, RHS); 4218 // fall through 4219 case ICmpInst::ICMP_SGT: 4220 case ICmpInst::ICMP_SGE: 4221 // a >s b ? a+x : b+x -> smax(a, b)+x 4222 // a >s b ? b+x : a+x -> smin(a, b)+x 4223 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) { 4224 const SCEV *LS = getNoopOrSignExtend(getSCEV(LHS), I->getType()); 4225 const SCEV *RS = getNoopOrSignExtend(getSCEV(RHS), I->getType()); 4226 const SCEV *LA = getSCEV(TrueVal); 4227 const SCEV *RA = getSCEV(FalseVal); 4228 const SCEV *LDiff = getMinusSCEV(LA, LS); 4229 const SCEV *RDiff = getMinusSCEV(RA, RS); 4230 if (LDiff == RDiff) 4231 return getAddExpr(getSMaxExpr(LS, RS), LDiff); 4232 LDiff = getMinusSCEV(LA, RS); 4233 RDiff = getMinusSCEV(RA, LS); 4234 if (LDiff == RDiff) 4235 return getAddExpr(getSMinExpr(LS, RS), LDiff); 4236 } 4237 break; 4238 case ICmpInst::ICMP_ULT: 4239 case ICmpInst::ICMP_ULE: 4240 std::swap(LHS, RHS); 4241 // fall through 4242 case ICmpInst::ICMP_UGT: 4243 case ICmpInst::ICMP_UGE: 4244 // a >u b ? a+x : b+x -> umax(a, b)+x 4245 // a >u b ? b+x : a+x -> umin(a, b)+x 4246 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) { 4247 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType()); 4248 const SCEV *RS = getNoopOrZeroExtend(getSCEV(RHS), I->getType()); 4249 const SCEV *LA = getSCEV(TrueVal); 4250 const SCEV *RA = getSCEV(FalseVal); 4251 const SCEV *LDiff = getMinusSCEV(LA, LS); 4252 const SCEV *RDiff = getMinusSCEV(RA, RS); 4253 if (LDiff == RDiff) 4254 return getAddExpr(getUMaxExpr(LS, RS), LDiff); 4255 LDiff = getMinusSCEV(LA, RS); 4256 RDiff = getMinusSCEV(RA, LS); 4257 if (LDiff == RDiff) 4258 return getAddExpr(getUMinExpr(LS, RS), LDiff); 4259 } 4260 break; 4261 case ICmpInst::ICMP_NE: 4262 // n != 0 ? n+x : 1+x -> umax(n, 1)+x 4263 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) && 4264 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) { 4265 const SCEV *One = getOne(I->getType()); 4266 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType()); 4267 const SCEV *LA = getSCEV(TrueVal); 4268 const SCEV *RA = getSCEV(FalseVal); 4269 const SCEV *LDiff = getMinusSCEV(LA, LS); 4270 const SCEV *RDiff = getMinusSCEV(RA, One); 4271 if (LDiff == RDiff) 4272 return getAddExpr(getUMaxExpr(One, LS), LDiff); 4273 } 4274 break; 4275 case ICmpInst::ICMP_EQ: 4276 // n == 0 ? 1+x : n+x -> umax(n, 1)+x 4277 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) && 4278 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) { 4279 const SCEV *One = getOne(I->getType()); 4280 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType()); 4281 const SCEV *LA = getSCEV(TrueVal); 4282 const SCEV *RA = getSCEV(FalseVal); 4283 const SCEV *LDiff = getMinusSCEV(LA, One); 4284 const SCEV *RDiff = getMinusSCEV(RA, LS); 4285 if (LDiff == RDiff) 4286 return getAddExpr(getUMaxExpr(One, LS), LDiff); 4287 } 4288 break; 4289 default: 4290 break; 4291 } 4292 4293 return getUnknown(I); 4294 } 4295 4296 /// createNodeForGEP - Expand GEP instructions into add and multiply 4297 /// operations. This allows them to be analyzed by regular SCEV code. 4298 /// 4299 const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) { 4300 // Don't attempt to analyze GEPs over unsized objects. 4301 if (!GEP->getSourceElementType()->isSized()) 4302 return getUnknown(GEP); 4303 4304 SmallVector<const SCEV *, 4> IndexExprs; 4305 for (auto Index = GEP->idx_begin(); Index != GEP->idx_end(); ++Index) 4306 IndexExprs.push_back(getSCEV(*Index)); 4307 return getGEPExpr(GEP->getSourceElementType(), 4308 getSCEV(GEP->getPointerOperand()), 4309 IndexExprs, GEP->isInBounds()); 4310 } 4311 4312 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is 4313 /// guaranteed to end in (at every loop iteration). It is, at the same time, 4314 /// the minimum number of times S is divisible by 2. For example, given {4,+,8} 4315 /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S. 4316 uint32_t 4317 ScalarEvolution::GetMinTrailingZeros(const SCEV *S) { 4318 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) 4319 return C->getAPInt().countTrailingZeros(); 4320 4321 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) 4322 return std::min(GetMinTrailingZeros(T->getOperand()), 4323 (uint32_t)getTypeSizeInBits(T->getType())); 4324 4325 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { 4326 uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); 4327 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? 4328 getTypeSizeInBits(E->getType()) : OpRes; 4329 } 4330 4331 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { 4332 uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); 4333 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? 4334 getTypeSizeInBits(E->getType()) : OpRes; 4335 } 4336 4337 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { 4338 // The result is the min of all operands results. 4339 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); 4340 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) 4341 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); 4342 return MinOpRes; 4343 } 4344 4345 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { 4346 // The result is the sum of all operands results. 4347 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); 4348 uint32_t BitWidth = getTypeSizeInBits(M->getType()); 4349 for (unsigned i = 1, e = M->getNumOperands(); 4350 SumOpRes != BitWidth && i != e; ++i) 4351 SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), 4352 BitWidth); 4353 return SumOpRes; 4354 } 4355 4356 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { 4357 // The result is the min of all operands results. 4358 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); 4359 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) 4360 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); 4361 return MinOpRes; 4362 } 4363 4364 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { 4365 // The result is the min of all operands results. 4366 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); 4367 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) 4368 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); 4369 return MinOpRes; 4370 } 4371 4372 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { 4373 // The result is the min of all operands results. 4374 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); 4375 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) 4376 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); 4377 return MinOpRes; 4378 } 4379 4380 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 4381 // For a SCEVUnknown, ask ValueTracking. 4382 unsigned BitWidth = getTypeSizeInBits(U->getType()); 4383 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); 4384 computeKnownBits(U->getValue(), Zeros, Ones, getDataLayout(), 0, &AC, 4385 nullptr, &DT); 4386 return Zeros.countTrailingOnes(); 4387 } 4388 4389 // SCEVUDivExpr 4390 return 0; 4391 } 4392 4393 /// GetRangeFromMetadata - Helper method to assign a range to V from 4394 /// metadata present in the IR. 4395 static Optional<ConstantRange> GetRangeFromMetadata(Value *V) { 4396 if (Instruction *I = dyn_cast<Instruction>(V)) 4397 if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) 4398 return getConstantRangeFromMetadata(*MD); 4399 4400 return None; 4401 } 4402 4403 /// getRange - Determine the range for a particular SCEV. If SignHint is 4404 /// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges 4405 /// with a "cleaner" unsigned (resp. signed) representation. 4406 /// 4407 ConstantRange 4408 ScalarEvolution::getRange(const SCEV *S, 4409 ScalarEvolution::RangeSignHint SignHint) { 4410 DenseMap<const SCEV *, ConstantRange> &Cache = 4411 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges 4412 : SignedRanges; 4413 4414 // See if we've computed this range already. 4415 DenseMap<const SCEV *, ConstantRange>::iterator I = Cache.find(S); 4416 if (I != Cache.end()) 4417 return I->second; 4418 4419 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) 4420 return setRange(C, SignHint, ConstantRange(C->getAPInt())); 4421 4422 unsigned BitWidth = getTypeSizeInBits(S->getType()); 4423 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true); 4424 4425 // If the value has known zeros, the maximum value will have those known zeros 4426 // as well. 4427 uint32_t TZ = GetMinTrailingZeros(S); 4428 if (TZ != 0) { 4429 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) 4430 ConservativeResult = 4431 ConstantRange(APInt::getMinValue(BitWidth), 4432 APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1); 4433 else 4434 ConservativeResult = ConstantRange( 4435 APInt::getSignedMinValue(BitWidth), 4436 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1); 4437 } 4438 4439 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) { 4440 ConstantRange X = getRange(Add->getOperand(0), SignHint); 4441 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i) 4442 X = X.add(getRange(Add->getOperand(i), SignHint)); 4443 return setRange(Add, SignHint, ConservativeResult.intersectWith(X)); 4444 } 4445 4446 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) { 4447 ConstantRange X = getRange(Mul->getOperand(0), SignHint); 4448 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i) 4449 X = X.multiply(getRange(Mul->getOperand(i), SignHint)); 4450 return setRange(Mul, SignHint, ConservativeResult.intersectWith(X)); 4451 } 4452 4453 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) { 4454 ConstantRange X = getRange(SMax->getOperand(0), SignHint); 4455 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i) 4456 X = X.smax(getRange(SMax->getOperand(i), SignHint)); 4457 return setRange(SMax, SignHint, ConservativeResult.intersectWith(X)); 4458 } 4459 4460 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) { 4461 ConstantRange X = getRange(UMax->getOperand(0), SignHint); 4462 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i) 4463 X = X.umax(getRange(UMax->getOperand(i), SignHint)); 4464 return setRange(UMax, SignHint, ConservativeResult.intersectWith(X)); 4465 } 4466 4467 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) { 4468 ConstantRange X = getRange(UDiv->getLHS(), SignHint); 4469 ConstantRange Y = getRange(UDiv->getRHS(), SignHint); 4470 return setRange(UDiv, SignHint, 4471 ConservativeResult.intersectWith(X.udiv(Y))); 4472 } 4473 4474 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) { 4475 ConstantRange X = getRange(ZExt->getOperand(), SignHint); 4476 return setRange(ZExt, SignHint, 4477 ConservativeResult.intersectWith(X.zeroExtend(BitWidth))); 4478 } 4479 4480 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) { 4481 ConstantRange X = getRange(SExt->getOperand(), SignHint); 4482 return setRange(SExt, SignHint, 4483 ConservativeResult.intersectWith(X.signExtend(BitWidth))); 4484 } 4485 4486 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) { 4487 ConstantRange X = getRange(Trunc->getOperand(), SignHint); 4488 return setRange(Trunc, SignHint, 4489 ConservativeResult.intersectWith(X.truncate(BitWidth))); 4490 } 4491 4492 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) { 4493 // If there's no unsigned wrap, the value will never be less than its 4494 // initial value. 4495 if (AddRec->hasNoUnsignedWrap()) 4496 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart())) 4497 if (!C->getValue()->isZero()) 4498 ConservativeResult = ConservativeResult.intersectWith( 4499 ConstantRange(C->getAPInt(), APInt(BitWidth, 0))); 4500 4501 // If there's no signed wrap, and all the operands have the same sign or 4502 // zero, the value won't ever change sign. 4503 if (AddRec->hasNoSignedWrap()) { 4504 bool AllNonNeg = true; 4505 bool AllNonPos = true; 4506 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { 4507 if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false; 4508 if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false; 4509 } 4510 if (AllNonNeg) 4511 ConservativeResult = ConservativeResult.intersectWith( 4512 ConstantRange(APInt(BitWidth, 0), 4513 APInt::getSignedMinValue(BitWidth))); 4514 else if (AllNonPos) 4515 ConservativeResult = ConservativeResult.intersectWith( 4516 ConstantRange(APInt::getSignedMinValue(BitWidth), 4517 APInt(BitWidth, 1))); 4518 } 4519 4520 // TODO: non-affine addrec 4521 if (AddRec->isAffine()) { 4522 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop()); 4523 if (!isa<SCEVCouldNotCompute>(MaxBECount) && 4524 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) { 4525 auto RangeFromAffine = getRangeForAffineAR( 4526 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount, 4527 BitWidth); 4528 if (!RangeFromAffine.isFullSet()) 4529 ConservativeResult = 4530 ConservativeResult.intersectWith(RangeFromAffine); 4531 4532 auto RangeFromFactoring = getRangeViaFactoring( 4533 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount, 4534 BitWidth); 4535 if (!RangeFromFactoring.isFullSet()) 4536 ConservativeResult = 4537 ConservativeResult.intersectWith(RangeFromFactoring); 4538 } 4539 } 4540 4541 return setRange(AddRec, SignHint, ConservativeResult); 4542 } 4543 4544 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 4545 // Check if the IR explicitly contains !range metadata. 4546 Optional<ConstantRange> MDRange = GetRangeFromMetadata(U->getValue()); 4547 if (MDRange.hasValue()) 4548 ConservativeResult = ConservativeResult.intersectWith(MDRange.getValue()); 4549 4550 // Split here to avoid paying the compile-time cost of calling both 4551 // computeKnownBits and ComputeNumSignBits. This restriction can be lifted 4552 // if needed. 4553 const DataLayout &DL = getDataLayout(); 4554 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) { 4555 // For a SCEVUnknown, ask ValueTracking. 4556 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); 4557 computeKnownBits(U->getValue(), Zeros, Ones, DL, 0, &AC, nullptr, &DT); 4558 if (Ones != ~Zeros + 1) 4559 ConservativeResult = 4560 ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1)); 4561 } else { 4562 assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED && 4563 "generalize as needed!"); 4564 unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT); 4565 if (NS > 1) 4566 ConservativeResult = ConservativeResult.intersectWith( 4567 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1), 4568 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1)); 4569 } 4570 4571 return setRange(U, SignHint, ConservativeResult); 4572 } 4573 4574 return setRange(S, SignHint, ConservativeResult); 4575 } 4576 4577 ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start, 4578 const SCEV *Step, 4579 const SCEV *MaxBECount, 4580 unsigned BitWidth) { 4581 assert(!isa<SCEVCouldNotCompute>(MaxBECount) && 4582 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth && 4583 "Precondition!"); 4584 4585 ConstantRange Result(BitWidth, /* isFullSet = */ true); 4586 4587 // Check for overflow. This must be done with ConstantRange arithmetic 4588 // because we could be called from within the ScalarEvolution overflow 4589 // checking code. 4590 4591 MaxBECount = getNoopOrZeroExtend(MaxBECount, Start->getType()); 4592 ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount); 4593 ConstantRange ZExtMaxBECountRange = 4594 MaxBECountRange.zextOrTrunc(BitWidth * 2 + 1); 4595 4596 ConstantRange StepSRange = getSignedRange(Step); 4597 ConstantRange SExtStepSRange = StepSRange.sextOrTrunc(BitWidth * 2 + 1); 4598 4599 ConstantRange StartURange = getUnsignedRange(Start); 4600 ConstantRange EndURange = 4601 StartURange.add(MaxBECountRange.multiply(StepSRange)); 4602 4603 // Check for unsigned overflow. 4604 ConstantRange ZExtStartURange = StartURange.zextOrTrunc(BitWidth * 2 + 1); 4605 ConstantRange ZExtEndURange = EndURange.zextOrTrunc(BitWidth * 2 + 1); 4606 if (ZExtStartURange.add(ZExtMaxBECountRange.multiply(SExtStepSRange)) == 4607 ZExtEndURange) { 4608 APInt Min = APIntOps::umin(StartURange.getUnsignedMin(), 4609 EndURange.getUnsignedMin()); 4610 APInt Max = APIntOps::umax(StartURange.getUnsignedMax(), 4611 EndURange.getUnsignedMax()); 4612 bool IsFullRange = Min.isMinValue() && Max.isMaxValue(); 4613 if (!IsFullRange) 4614 Result = 4615 Result.intersectWith(ConstantRange(Min, Max + 1)); 4616 } 4617 4618 ConstantRange StartSRange = getSignedRange(Start); 4619 ConstantRange EndSRange = 4620 StartSRange.add(MaxBECountRange.multiply(StepSRange)); 4621 4622 // Check for signed overflow. This must be done with ConstantRange 4623 // arithmetic because we could be called from within the ScalarEvolution 4624 // overflow checking code. 4625 ConstantRange SExtStartSRange = StartSRange.sextOrTrunc(BitWidth * 2 + 1); 4626 ConstantRange SExtEndSRange = EndSRange.sextOrTrunc(BitWidth * 2 + 1); 4627 if (SExtStartSRange.add(ZExtMaxBECountRange.multiply(SExtStepSRange)) == 4628 SExtEndSRange) { 4629 APInt Min = 4630 APIntOps::smin(StartSRange.getSignedMin(), EndSRange.getSignedMin()); 4631 APInt Max = 4632 APIntOps::smax(StartSRange.getSignedMax(), EndSRange.getSignedMax()); 4633 bool IsFullRange = Min.isMinSignedValue() && Max.isMaxSignedValue(); 4634 if (!IsFullRange) 4635 Result = 4636 Result.intersectWith(ConstantRange(Min, Max + 1)); 4637 } 4638 4639 return Result; 4640 } 4641 4642 ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start, 4643 const SCEV *Step, 4644 const SCEV *MaxBECount, 4645 unsigned BitWidth) { 4646 // RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q}) 4647 // == RangeOf({A,+,P}) union RangeOf({B,+,Q}) 4648 4649 struct SelectPattern { 4650 Value *Condition = nullptr; 4651 APInt TrueValue; 4652 APInt FalseValue; 4653 4654 explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth, 4655 const SCEV *S) { 4656 Optional<unsigned> CastOp; 4657 APInt Offset(BitWidth, 0); 4658 4659 assert(SE.getTypeSizeInBits(S->getType()) == BitWidth && 4660 "Should be!"); 4661 4662 // Peel off a constant offset: 4663 if (auto *SA = dyn_cast<SCEVAddExpr>(S)) { 4664 // In the future we could consider being smarter here and handle 4665 // {Start+Step,+,Step} too. 4666 if (SA->getNumOperands() != 2 || !isa<SCEVConstant>(SA->getOperand(0))) 4667 return; 4668 4669 Offset = cast<SCEVConstant>(SA->getOperand(0))->getAPInt(); 4670 S = SA->getOperand(1); 4671 } 4672 4673 // Peel off a cast operation 4674 if (auto *SCast = dyn_cast<SCEVCastExpr>(S)) { 4675 CastOp = SCast->getSCEVType(); 4676 S = SCast->getOperand(); 4677 } 4678 4679 using namespace llvm::PatternMatch; 4680 4681 auto *SU = dyn_cast<SCEVUnknown>(S); 4682 const APInt *TrueVal, *FalseVal; 4683 if (!SU || 4684 !match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal), 4685 m_APInt(FalseVal)))) { 4686 Condition = nullptr; 4687 return; 4688 } 4689 4690 TrueValue = *TrueVal; 4691 FalseValue = *FalseVal; 4692 4693 // Re-apply the cast we peeled off earlier 4694 if (CastOp.hasValue()) 4695 switch (*CastOp) { 4696 default: 4697 llvm_unreachable("Unknown SCEV cast type!"); 4698 4699 case scTruncate: 4700 TrueValue = TrueValue.trunc(BitWidth); 4701 FalseValue = FalseValue.trunc(BitWidth); 4702 break; 4703 case scZeroExtend: 4704 TrueValue = TrueValue.zext(BitWidth); 4705 FalseValue = FalseValue.zext(BitWidth); 4706 break; 4707 case scSignExtend: 4708 TrueValue = TrueValue.sext(BitWidth); 4709 FalseValue = FalseValue.sext(BitWidth); 4710 break; 4711 } 4712 4713 // Re-apply the constant offset we peeled off earlier 4714 TrueValue += Offset; 4715 FalseValue += Offset; 4716 } 4717 4718 bool isRecognized() { return Condition != nullptr; } 4719 }; 4720 4721 SelectPattern StartPattern(*this, BitWidth, Start); 4722 if (!StartPattern.isRecognized()) 4723 return ConstantRange(BitWidth, /* isFullSet = */ true); 4724 4725 SelectPattern StepPattern(*this, BitWidth, Step); 4726 if (!StepPattern.isRecognized()) 4727 return ConstantRange(BitWidth, /* isFullSet = */ true); 4728 4729 if (StartPattern.Condition != StepPattern.Condition) { 4730 // We don't handle this case today; but we could, by considering four 4731 // possibilities below instead of two. I'm not sure if there are cases where 4732 // that will help over what getRange already does, though. 4733 return ConstantRange(BitWidth, /* isFullSet = */ true); 4734 } 4735 4736 // NB! Calling ScalarEvolution::getConstant is fine, but we should not try to 4737 // construct arbitrary general SCEV expressions here. This function is called 4738 // from deep in the call stack, and calling getSCEV (on a sext instruction, 4739 // say) can end up caching a suboptimal value. 4740 4741 // FIXME: without the explicit `this` receiver below, MSVC errors out with 4742 // C2352 and C2512 (otherwise it isn't needed). 4743 4744 const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue); 4745 const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue); 4746 const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue); 4747 const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue); 4748 4749 ConstantRange TrueRange = 4750 this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount, BitWidth); 4751 ConstantRange FalseRange = 4752 this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount, BitWidth); 4753 4754 return TrueRange.unionWith(FalseRange); 4755 } 4756 4757 SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) { 4758 if (isa<ConstantExpr>(V)) return SCEV::FlagAnyWrap; 4759 const BinaryOperator *BinOp = cast<BinaryOperator>(V); 4760 4761 // Return early if there are no flags to propagate to the SCEV. 4762 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; 4763 if (BinOp->hasNoUnsignedWrap()) 4764 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW); 4765 if (BinOp->hasNoSignedWrap()) 4766 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW); 4767 if (Flags == SCEV::FlagAnyWrap) 4768 return SCEV::FlagAnyWrap; 4769 4770 // Here we check that BinOp is in the header of the innermost loop 4771 // containing BinOp, since we only deal with instructions in the loop 4772 // header. The actual loop we need to check later will come from an add 4773 // recurrence, but getting that requires computing the SCEV of the operands, 4774 // which can be expensive. This check we can do cheaply to rule out some 4775 // cases early. 4776 Loop *InnermostContainingLoop = LI.getLoopFor(BinOp->getParent()); 4777 if (InnermostContainingLoop == nullptr || 4778 InnermostContainingLoop->getHeader() != BinOp->getParent()) 4779 return SCEV::FlagAnyWrap; 4780 4781 // Only proceed if we can prove that BinOp does not yield poison. 4782 if (!isKnownNotFullPoison(BinOp)) return SCEV::FlagAnyWrap; 4783 4784 // At this point we know that if V is executed, then it does not wrap 4785 // according to at least one of NSW or NUW. If V is not executed, then we do 4786 // not know if the calculation that V represents would wrap. Multiple 4787 // instructions can map to the same SCEV. If we apply NSW or NUW from V to 4788 // the SCEV, we must guarantee no wrapping for that SCEV also when it is 4789 // derived from other instructions that map to the same SCEV. We cannot make 4790 // that guarantee for cases where V is not executed. So we need to find the 4791 // loop that V is considered in relation to and prove that V is executed for 4792 // every iteration of that loop. That implies that the value that V 4793 // calculates does not wrap anywhere in the loop, so then we can apply the 4794 // flags to the SCEV. 4795 // 4796 // We check isLoopInvariant to disambiguate in case we are adding two 4797 // recurrences from different loops, so that we know which loop to prove 4798 // that V is executed in. 4799 for (int OpIndex = 0; OpIndex < 2; ++OpIndex) { 4800 const SCEV *Op = getSCEV(BinOp->getOperand(OpIndex)); 4801 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { 4802 const int OtherOpIndex = 1 - OpIndex; 4803 const SCEV *OtherOp = getSCEV(BinOp->getOperand(OtherOpIndex)); 4804 if (isLoopInvariant(OtherOp, AddRec->getLoop()) && 4805 isGuaranteedToExecuteForEveryIteration(BinOp, AddRec->getLoop())) 4806 return Flags; 4807 } 4808 } 4809 return SCEV::FlagAnyWrap; 4810 } 4811 4812 /// createSCEV - We know that there is no SCEV for the specified value. Analyze 4813 /// the expression. 4814 /// 4815 const SCEV *ScalarEvolution::createSCEV(Value *V) { 4816 if (!isSCEVable(V->getType())) 4817 return getUnknown(V); 4818 4819 if (Instruction *I = dyn_cast<Instruction>(V)) { 4820 // Don't attempt to analyze instructions in blocks that aren't 4821 // reachable. Such instructions don't matter, and they aren't required 4822 // to obey basic rules for definitions dominating uses which this 4823 // analysis depends on. 4824 if (!DT.isReachableFromEntry(I->getParent())) 4825 return getUnknown(V); 4826 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) 4827 return getConstant(CI); 4828 else if (isa<ConstantPointerNull>(V)) 4829 return getZero(V->getType()); 4830 else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) 4831 return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee()); 4832 else if (!isa<ConstantExpr>(V)) 4833 return getUnknown(V); 4834 4835 Operator *U = cast<Operator>(V); 4836 if (auto BO = MatchBinaryOp(U)) { 4837 switch (BO->Opcode) { 4838 case Instruction::Add: { 4839 // The simple thing to do would be to just call getSCEV on both operands 4840 // and call getAddExpr with the result. However if we're looking at a 4841 // bunch of things all added together, this can be quite inefficient, 4842 // because it leads to N-1 getAddExpr calls for N ultimate operands. 4843 // Instead, gather up all the operands and make a single getAddExpr call. 4844 // LLVM IR canonical form means we need only traverse the left operands. 4845 SmallVector<const SCEV *, 4> AddOps; 4846 do { 4847 if (BO->Op) { 4848 if (auto *OpSCEV = getExistingSCEV(BO->Op)) { 4849 AddOps.push_back(OpSCEV); 4850 break; 4851 } 4852 4853 // If a NUW or NSW flag can be applied to the SCEV for this 4854 // addition, then compute the SCEV for this addition by itself 4855 // with a separate call to getAddExpr. We need to do that 4856 // instead of pushing the operands of the addition onto AddOps, 4857 // since the flags are only known to apply to this particular 4858 // addition - they may not apply to other additions that can be 4859 // formed with operands from AddOps. 4860 const SCEV *RHS = getSCEV(BO->RHS); 4861 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op); 4862 if (Flags != SCEV::FlagAnyWrap) { 4863 const SCEV *LHS = getSCEV(BO->LHS); 4864 if (BO->Opcode == Instruction::Sub) 4865 AddOps.push_back(getMinusSCEV(LHS, RHS, Flags)); 4866 else 4867 AddOps.push_back(getAddExpr(LHS, RHS, Flags)); 4868 break; 4869 } 4870 } 4871 4872 if (BO->Opcode == Instruction::Sub) 4873 AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS))); 4874 else 4875 AddOps.push_back(getSCEV(BO->RHS)); 4876 4877 auto NewBO = MatchBinaryOp(BO->LHS); 4878 if (!NewBO || (NewBO->Opcode != Instruction::Add && 4879 NewBO->Opcode != Instruction::Sub)) { 4880 AddOps.push_back(getSCEV(BO->LHS)); 4881 break; 4882 } 4883 BO = NewBO; 4884 } while (true); 4885 4886 return getAddExpr(AddOps); 4887 } 4888 4889 case Instruction::Mul: { 4890 SmallVector<const SCEV *, 4> MulOps; 4891 do { 4892 if (BO->Op) { 4893 if (auto *OpSCEV = getExistingSCEV(BO->Op)) { 4894 MulOps.push_back(OpSCEV); 4895 break; 4896 } 4897 4898 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op); 4899 if (Flags != SCEV::FlagAnyWrap) { 4900 MulOps.push_back( 4901 getMulExpr(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags)); 4902 break; 4903 } 4904 } 4905 4906 MulOps.push_back(getSCEV(BO->RHS)); 4907 auto NewBO = MatchBinaryOp(BO->LHS); 4908 if (!NewBO || NewBO->Opcode != Instruction::Mul) { 4909 MulOps.push_back(getSCEV(BO->LHS)); 4910 break; 4911 } 4912 BO = NewBO; 4913 } while (true); 4914 4915 return getMulExpr(MulOps); 4916 } 4917 case Instruction::UDiv: 4918 return getUDivExpr(getSCEV(BO->LHS), getSCEV(BO->RHS)); 4919 case Instruction::Sub: { 4920 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap; 4921 if (BO->Op) 4922 Flags = getNoWrapFlagsFromUB(BO->Op); 4923 return getMinusSCEV(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags); 4924 } 4925 case Instruction::And: 4926 // For an expression like x&255 that merely masks off the high bits, 4927 // use zext(trunc(x)) as the SCEV expression. 4928 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) { 4929 if (CI->isNullValue()) 4930 return getSCEV(BO->RHS); 4931 if (CI->isAllOnesValue()) 4932 return getSCEV(BO->LHS); 4933 const APInt &A = CI->getValue(); 4934 4935 // Instcombine's ShrinkDemandedConstant may strip bits out of 4936 // constants, obscuring what would otherwise be a low-bits mask. 4937 // Use computeKnownBits to compute what ShrinkDemandedConstant 4938 // knew about to reconstruct a low-bits mask value. 4939 unsigned LZ = A.countLeadingZeros(); 4940 unsigned TZ = A.countTrailingZeros(); 4941 unsigned BitWidth = A.getBitWidth(); 4942 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 4943 computeKnownBits(BO->LHS, KnownZero, KnownOne, getDataLayout(), 4944 0, &AC, nullptr, &DT); 4945 4946 APInt EffectiveMask = 4947 APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ); 4948 if ((LZ != 0 || TZ != 0) && !((~A & ~KnownZero) & EffectiveMask)) { 4949 const SCEV *MulCount = getConstant(ConstantInt::get( 4950 getContext(), APInt::getOneBitSet(BitWidth, TZ))); 4951 return getMulExpr( 4952 getZeroExtendExpr( 4953 getTruncateExpr( 4954 getUDivExactExpr(getSCEV(BO->LHS), MulCount), 4955 IntegerType::get(getContext(), BitWidth - LZ - TZ)), 4956 BO->LHS->getType()), 4957 MulCount); 4958 } 4959 } 4960 break; 4961 4962 case Instruction::Or: 4963 // If the RHS of the Or is a constant, we may have something like: 4964 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop 4965 // optimizations will transparently handle this case. 4966 // 4967 // In order for this transformation to be safe, the LHS must be of the 4968 // form X*(2^n) and the Or constant must be less than 2^n. 4969 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) { 4970 const SCEV *LHS = getSCEV(BO->LHS); 4971 const APInt &CIVal = CI->getValue(); 4972 if (GetMinTrailingZeros(LHS) >= 4973 (CIVal.getBitWidth() - CIVal.countLeadingZeros())) { 4974 // Build a plain add SCEV. 4975 const SCEV *S = getAddExpr(LHS, getSCEV(CI)); 4976 // If the LHS of the add was an addrec and it has no-wrap flags, 4977 // transfer the no-wrap flags, since an or won't introduce a wrap. 4978 if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) { 4979 const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS); 4980 const_cast<SCEVAddRecExpr *>(NewAR)->setNoWrapFlags( 4981 OldAR->getNoWrapFlags()); 4982 } 4983 return S; 4984 } 4985 } 4986 break; 4987 4988 case Instruction::Xor: 4989 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) { 4990 // If the RHS of xor is -1, then this is a not operation. 4991 if (CI->isAllOnesValue()) 4992 return getNotSCEV(getSCEV(BO->LHS)); 4993 4994 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. 4995 // This is a variant of the check for xor with -1, and it handles 4996 // the case where instcombine has trimmed non-demanded bits out 4997 // of an xor with -1. 4998 if (auto *LBO = dyn_cast<BinaryOperator>(BO->LHS)) 4999 if (ConstantInt *LCI = dyn_cast<ConstantInt>(LBO->getOperand(1))) 5000 if (LBO->getOpcode() == Instruction::And && 5001 LCI->getValue() == CI->getValue()) 5002 if (const SCEVZeroExtendExpr *Z = 5003 dyn_cast<SCEVZeroExtendExpr>(getSCEV(BO->LHS))) { 5004 Type *UTy = BO->LHS->getType(); 5005 const SCEV *Z0 = Z->getOperand(); 5006 Type *Z0Ty = Z0->getType(); 5007 unsigned Z0TySize = getTypeSizeInBits(Z0Ty); 5008 5009 // If C is a low-bits mask, the zero extend is serving to 5010 // mask off the high bits. Complement the operand and 5011 // re-apply the zext. 5012 if (APIntOps::isMask(Z0TySize, CI->getValue())) 5013 return getZeroExtendExpr(getNotSCEV(Z0), UTy); 5014 5015 // If C is a single bit, it may be in the sign-bit position 5016 // before the zero-extend. In this case, represent the xor 5017 // using an add, which is equivalent, and re-apply the zext. 5018 APInt Trunc = CI->getValue().trunc(Z0TySize); 5019 if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() && 5020 Trunc.isSignBit()) 5021 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), 5022 UTy); 5023 } 5024 } 5025 break; 5026 5027 case Instruction::Shl: 5028 // Turn shift left of a constant amount into a multiply. 5029 if (ConstantInt *SA = dyn_cast<ConstantInt>(BO->RHS)) { 5030 uint32_t BitWidth = cast<IntegerType>(SA->getType())->getBitWidth(); 5031 5032 // If the shift count is not less than the bitwidth, the result of 5033 // the shift is undefined. Don't try to analyze it, because the 5034 // resolution chosen here may differ from the resolution chosen in 5035 // other parts of the compiler. 5036 if (SA->getValue().uge(BitWidth)) 5037 break; 5038 5039 // It is currently not resolved how to interpret NSW for left 5040 // shift by BitWidth - 1, so we avoid applying flags in that 5041 // case. Remove this check (or this comment) once the situation 5042 // is resolved. See 5043 // http://lists.llvm.org/pipermail/llvm-dev/2015-April/084195.html 5044 // and http://reviews.llvm.org/D8890 . 5045 auto Flags = SCEV::FlagAnyWrap; 5046 if (BO->Op && SA->getValue().ult(BitWidth - 1)) 5047 Flags = getNoWrapFlagsFromUB(BO->Op); 5048 5049 Constant *X = ConstantInt::get(getContext(), 5050 APInt::getOneBitSet(BitWidth, SA->getZExtValue())); 5051 return getMulExpr(getSCEV(BO->LHS), getSCEV(X), Flags); 5052 } 5053 break; 5054 5055 case Instruction::AShr: 5056 // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. 5057 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) 5058 if (Operator *L = dyn_cast<Operator>(BO->LHS)) 5059 if (L->getOpcode() == Instruction::Shl && 5060 L->getOperand(1) == BO->RHS) { 5061 uint64_t BitWidth = getTypeSizeInBits(BO->LHS->getType()); 5062 5063 // If the shift count is not less than the bitwidth, the result of 5064 // the shift is undefined. Don't try to analyze it, because the 5065 // resolution chosen here may differ from the resolution chosen in 5066 // other parts of the compiler. 5067 if (CI->getValue().uge(BitWidth)) 5068 break; 5069 5070 uint64_t Amt = BitWidth - CI->getZExtValue(); 5071 if (Amt == BitWidth) 5072 return getSCEV(L->getOperand(0)); // shift by zero --> noop 5073 return getSignExtendExpr( 5074 getTruncateExpr(getSCEV(L->getOperand(0)), 5075 IntegerType::get(getContext(), Amt)), 5076 BO->LHS->getType()); 5077 } 5078 break; 5079 } 5080 } 5081 5082 switch (U->getOpcode()) { 5083 case Instruction::Trunc: 5084 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); 5085 5086 case Instruction::ZExt: 5087 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); 5088 5089 case Instruction::SExt: 5090 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); 5091 5092 case Instruction::BitCast: 5093 // BitCasts are no-op casts so we just eliminate the cast. 5094 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) 5095 return getSCEV(U->getOperand(0)); 5096 break; 5097 5098 // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can 5099 // lead to pointer expressions which cannot safely be expanded to GEPs, 5100 // because ScalarEvolution doesn't respect the GEP aliasing rules when 5101 // simplifying integer expressions. 5102 5103 case Instruction::GetElementPtr: 5104 return createNodeForGEP(cast<GEPOperator>(U)); 5105 5106 case Instruction::PHI: 5107 return createNodeForPHI(cast<PHINode>(U)); 5108 5109 case Instruction::Select: 5110 // U can also be a select constant expr, which let fall through. Since 5111 // createNodeForSelect only works for a condition that is an `ICmpInst`, and 5112 // constant expressions cannot have instructions as operands, we'd have 5113 // returned getUnknown for a select constant expressions anyway. 5114 if (isa<Instruction>(U)) 5115 return createNodeForSelectOrPHI(cast<Instruction>(U), U->getOperand(0), 5116 U->getOperand(1), U->getOperand(2)); 5117 } 5118 5119 return getUnknown(V); 5120 } 5121 5122 5123 5124 //===----------------------------------------------------------------------===// 5125 // Iteration Count Computation Code 5126 // 5127 5128 unsigned ScalarEvolution::getSmallConstantTripCount(Loop *L) { 5129 if (BasicBlock *ExitingBB = L->getExitingBlock()) 5130 return getSmallConstantTripCount(L, ExitingBB); 5131 5132 // No trip count information for multiple exits. 5133 return 0; 5134 } 5135 5136 /// getSmallConstantTripCount - Returns the maximum trip count of this loop as a 5137 /// normal unsigned value. Returns 0 if the trip count is unknown or not 5138 /// constant. Will also return 0 if the maximum trip count is very large (>= 5139 /// 2^32). 5140 /// 5141 /// This "trip count" assumes that control exits via ExitingBlock. More 5142 /// precisely, it is the number of times that control may reach ExitingBlock 5143 /// before taking the branch. For loops with multiple exits, it may not be the 5144 /// number times that the loop header executes because the loop may exit 5145 /// prematurely via another branch. 5146 unsigned ScalarEvolution::getSmallConstantTripCount(Loop *L, 5147 BasicBlock *ExitingBlock) { 5148 assert(ExitingBlock && "Must pass a non-null exiting block!"); 5149 assert(L->isLoopExiting(ExitingBlock) && 5150 "Exiting block must actually branch out of the loop!"); 5151 const SCEVConstant *ExitCount = 5152 dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock)); 5153 if (!ExitCount) 5154 return 0; 5155 5156 ConstantInt *ExitConst = ExitCount->getValue(); 5157 5158 // Guard against huge trip counts. 5159 if (ExitConst->getValue().getActiveBits() > 32) 5160 return 0; 5161 5162 // In case of integer overflow, this returns 0, which is correct. 5163 return ((unsigned)ExitConst->getZExtValue()) + 1; 5164 } 5165 5166 unsigned ScalarEvolution::getSmallConstantTripMultiple(Loop *L) { 5167 if (BasicBlock *ExitingBB = L->getExitingBlock()) 5168 return getSmallConstantTripMultiple(L, ExitingBB); 5169 5170 // No trip multiple information for multiple exits. 5171 return 0; 5172 } 5173 5174 /// getSmallConstantTripMultiple - Returns the largest constant divisor of the 5175 /// trip count of this loop as a normal unsigned value, if possible. This 5176 /// means that the actual trip count is always a multiple of the returned 5177 /// value (don't forget the trip count could very well be zero as well!). 5178 /// 5179 /// Returns 1 if the trip count is unknown or not guaranteed to be the 5180 /// multiple of a constant (which is also the case if the trip count is simply 5181 /// constant, use getSmallConstantTripCount for that case), Will also return 1 5182 /// if the trip count is very large (>= 2^32). 5183 /// 5184 /// As explained in the comments for getSmallConstantTripCount, this assumes 5185 /// that control exits the loop via ExitingBlock. 5186 unsigned 5187 ScalarEvolution::getSmallConstantTripMultiple(Loop *L, 5188 BasicBlock *ExitingBlock) { 5189 assert(ExitingBlock && "Must pass a non-null exiting block!"); 5190 assert(L->isLoopExiting(ExitingBlock) && 5191 "Exiting block must actually branch out of the loop!"); 5192 const SCEV *ExitCount = getExitCount(L, ExitingBlock); 5193 if (ExitCount == getCouldNotCompute()) 5194 return 1; 5195 5196 // Get the trip count from the BE count by adding 1. 5197 const SCEV *TCMul = getAddExpr(ExitCount, getOne(ExitCount->getType())); 5198 // FIXME: SCEV distributes multiplication as V1*C1 + V2*C1. We could attempt 5199 // to factor simple cases. 5200 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(TCMul)) 5201 TCMul = Mul->getOperand(0); 5202 5203 const SCEVConstant *MulC = dyn_cast<SCEVConstant>(TCMul); 5204 if (!MulC) 5205 return 1; 5206 5207 ConstantInt *Result = MulC->getValue(); 5208 5209 // Guard against huge trip counts (this requires checking 5210 // for zero to handle the case where the trip count == -1 and the 5211 // addition wraps). 5212 if (!Result || Result->getValue().getActiveBits() > 32 || 5213 Result->getValue().getActiveBits() == 0) 5214 return 1; 5215 5216 return (unsigned)Result->getZExtValue(); 5217 } 5218 5219 // getExitCount - Get the expression for the number of loop iterations for which 5220 // this loop is guaranteed not to exit via ExitingBlock. Otherwise return 5221 // SCEVCouldNotCompute. 5222 const SCEV *ScalarEvolution::getExitCount(Loop *L, BasicBlock *ExitingBlock) { 5223 return getBackedgeTakenInfo(L).getExact(ExitingBlock, this); 5224 } 5225 5226 const SCEV * 5227 ScalarEvolution::getPredicatedBackedgeTakenCount(const Loop *L, 5228 SCEVUnionPredicate &Preds) { 5229 return getPredicatedBackedgeTakenInfo(L).getExact(this, &Preds); 5230 } 5231 5232 /// getBackedgeTakenCount - If the specified loop has a predictable 5233 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute 5234 /// object. The backedge-taken count is the number of times the loop header 5235 /// will be branched to from within the loop. This is one less than the 5236 /// trip count of the loop, since it doesn't count the first iteration, 5237 /// when the header is branched to from outside the loop. 5238 /// 5239 /// Note that it is not valid to call this method on a loop without a 5240 /// loop-invariant backedge-taken count (see 5241 /// hasLoopInvariantBackedgeTakenCount). 5242 /// 5243 const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) { 5244 return getBackedgeTakenInfo(L).getExact(this); 5245 } 5246 5247 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except 5248 /// return the least SCEV value that is known never to be less than the 5249 /// actual backedge taken count. 5250 const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { 5251 return getBackedgeTakenInfo(L).getMax(this); 5252 } 5253 5254 /// PushLoopPHIs - Push PHI nodes in the header of the given loop 5255 /// onto the given Worklist. 5256 static void 5257 PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) { 5258 BasicBlock *Header = L->getHeader(); 5259 5260 // Push all Loop-header PHIs onto the Worklist stack. 5261 for (BasicBlock::iterator I = Header->begin(); 5262 PHINode *PN = dyn_cast<PHINode>(I); ++I) 5263 Worklist.push_back(PN); 5264 } 5265 5266 const ScalarEvolution::BackedgeTakenInfo & 5267 ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) { 5268 auto &BTI = getBackedgeTakenInfo(L); 5269 if (BTI.hasFullInfo()) 5270 return BTI; 5271 5272 auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()}); 5273 5274 if (!Pair.second) 5275 return Pair.first->second; 5276 5277 BackedgeTakenInfo Result = 5278 computeBackedgeTakenCount(L, /*AllowPredicates=*/true); 5279 5280 return PredicatedBackedgeTakenCounts.find(L)->second = Result; 5281 } 5282 5283 const ScalarEvolution::BackedgeTakenInfo & 5284 ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { 5285 // Initially insert an invalid entry for this loop. If the insertion 5286 // succeeds, proceed to actually compute a backedge-taken count and 5287 // update the value. The temporary CouldNotCompute value tells SCEV 5288 // code elsewhere that it shouldn't attempt to request a new 5289 // backedge-taken count, which could result in infinite recursion. 5290 std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair = 5291 BackedgeTakenCounts.insert({L, BackedgeTakenInfo()}); 5292 if (!Pair.second) 5293 return Pair.first->second; 5294 5295 // computeBackedgeTakenCount may allocate memory for its result. Inserting it 5296 // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result 5297 // must be cleared in this scope. 5298 BackedgeTakenInfo Result = computeBackedgeTakenCount(L); 5299 5300 if (Result.getExact(this) != getCouldNotCompute()) { 5301 assert(isLoopInvariant(Result.getExact(this), L) && 5302 isLoopInvariant(Result.getMax(this), L) && 5303 "Computed backedge-taken count isn't loop invariant for loop!"); 5304 ++NumTripCountsComputed; 5305 } 5306 else if (Result.getMax(this) == getCouldNotCompute() && 5307 isa<PHINode>(L->getHeader()->begin())) { 5308 // Only count loops that have phi nodes as not being computable. 5309 ++NumTripCountsNotComputed; 5310 } 5311 5312 // Now that we know more about the trip count for this loop, forget any 5313 // existing SCEV values for PHI nodes in this loop since they are only 5314 // conservative estimates made without the benefit of trip count 5315 // information. This is similar to the code in forgetLoop, except that 5316 // it handles SCEVUnknown PHI nodes specially. 5317 if (Result.hasAnyInfo()) { 5318 SmallVector<Instruction *, 16> Worklist; 5319 PushLoopPHIs(L, Worklist); 5320 5321 SmallPtrSet<Instruction *, 8> Visited; 5322 while (!Worklist.empty()) { 5323 Instruction *I = Worklist.pop_back_val(); 5324 if (!Visited.insert(I).second) 5325 continue; 5326 5327 ValueExprMapType::iterator It = 5328 ValueExprMap.find_as(static_cast<Value *>(I)); 5329 if (It != ValueExprMap.end()) { 5330 const SCEV *Old = It->second; 5331 5332 // SCEVUnknown for a PHI either means that it has an unrecognized 5333 // structure, or it's a PHI that's in the progress of being computed 5334 // by createNodeForPHI. In the former case, additional loop trip 5335 // count information isn't going to change anything. In the later 5336 // case, createNodeForPHI will perform the necessary updates on its 5337 // own when it gets to that point. 5338 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) { 5339 forgetMemoizedResults(Old); 5340 ValueExprMap.erase(It); 5341 } 5342 if (PHINode *PN = dyn_cast<PHINode>(I)) 5343 ConstantEvolutionLoopExitValue.erase(PN); 5344 } 5345 5346 PushDefUseChildren(I, Worklist); 5347 } 5348 } 5349 5350 // Re-lookup the insert position, since the call to 5351 // computeBackedgeTakenCount above could result in a 5352 // recusive call to getBackedgeTakenInfo (on a different 5353 // loop), which would invalidate the iterator computed 5354 // earlier. 5355 return BackedgeTakenCounts.find(L)->second = Result; 5356 } 5357 5358 /// forgetLoop - This method should be called by the client when it has 5359 /// changed a loop in a way that may effect ScalarEvolution's ability to 5360 /// compute a trip count, or if the loop is deleted. 5361 void ScalarEvolution::forgetLoop(const Loop *L) { 5362 // Drop any stored trip count value. 5363 auto RemoveLoopFromBackedgeMap = 5364 [L](DenseMap<const Loop *, BackedgeTakenInfo> &Map) { 5365 auto BTCPos = Map.find(L); 5366 if (BTCPos != Map.end()) { 5367 BTCPos->second.clear(); 5368 Map.erase(BTCPos); 5369 } 5370 }; 5371 5372 RemoveLoopFromBackedgeMap(BackedgeTakenCounts); 5373 RemoveLoopFromBackedgeMap(PredicatedBackedgeTakenCounts); 5374 5375 // Drop information about expressions based on loop-header PHIs. 5376 SmallVector<Instruction *, 16> Worklist; 5377 PushLoopPHIs(L, Worklist); 5378 5379 SmallPtrSet<Instruction *, 8> Visited; 5380 while (!Worklist.empty()) { 5381 Instruction *I = Worklist.pop_back_val(); 5382 if (!Visited.insert(I).second) 5383 continue; 5384 5385 ValueExprMapType::iterator It = 5386 ValueExprMap.find_as(static_cast<Value *>(I)); 5387 if (It != ValueExprMap.end()) { 5388 forgetMemoizedResults(It->second); 5389 ValueExprMap.erase(It); 5390 if (PHINode *PN = dyn_cast<PHINode>(I)) 5391 ConstantEvolutionLoopExitValue.erase(PN); 5392 } 5393 5394 PushDefUseChildren(I, Worklist); 5395 } 5396 5397 // Forget all contained loops too, to avoid dangling entries in the 5398 // ValuesAtScopes map. 5399 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) 5400 forgetLoop(*I); 5401 } 5402 5403 /// forgetValue - This method should be called by the client when it has 5404 /// changed a value in a way that may effect its value, or which may 5405 /// disconnect it from a def-use chain linking it to a loop. 5406 void ScalarEvolution::forgetValue(Value *V) { 5407 Instruction *I = dyn_cast<Instruction>(V); 5408 if (!I) return; 5409 5410 // Drop information about expressions based on loop-header PHIs. 5411 SmallVector<Instruction *, 16> Worklist; 5412 Worklist.push_back(I); 5413 5414 SmallPtrSet<Instruction *, 8> Visited; 5415 while (!Worklist.empty()) { 5416 I = Worklist.pop_back_val(); 5417 if (!Visited.insert(I).second) 5418 continue; 5419 5420 ValueExprMapType::iterator It = 5421 ValueExprMap.find_as(static_cast<Value *>(I)); 5422 if (It != ValueExprMap.end()) { 5423 forgetMemoizedResults(It->second); 5424 ValueExprMap.erase(It); 5425 if (PHINode *PN = dyn_cast<PHINode>(I)) 5426 ConstantEvolutionLoopExitValue.erase(PN); 5427 } 5428 5429 PushDefUseChildren(I, Worklist); 5430 } 5431 } 5432 5433 /// getExact - Get the exact loop backedge taken count considering all loop 5434 /// exits. A computable result can only be returned for loops with a single 5435 /// exit. Returning the minimum taken count among all exits is incorrect 5436 /// because one of the loop's exit limit's may have been skipped. HowFarToZero 5437 /// assumes that the limit of each loop test is never skipped. This is a valid 5438 /// assumption as long as the loop exits via that test. For precise results, it 5439 /// is the caller's responsibility to specify the relevant loop exit using 5440 /// getExact(ExitingBlock, SE). 5441 const SCEV * 5442 ScalarEvolution::BackedgeTakenInfo::getExact( 5443 ScalarEvolution *SE, SCEVUnionPredicate *Preds) const { 5444 // If any exits were not computable, the loop is not computable. 5445 if (!ExitNotTaken.isCompleteList()) return SE->getCouldNotCompute(); 5446 5447 // We need exactly one computable exit. 5448 if (!ExitNotTaken.ExitingBlock) return SE->getCouldNotCompute(); 5449 assert(ExitNotTaken.ExactNotTaken && "uninitialized not-taken info"); 5450 5451 const SCEV *BECount = nullptr; 5452 for (auto &ENT : ExitNotTaken) { 5453 assert(ENT.ExactNotTaken != SE->getCouldNotCompute() && "bad exit SCEV"); 5454 5455 if (!BECount) 5456 BECount = ENT.ExactNotTaken; 5457 else if (BECount != ENT.ExactNotTaken) 5458 return SE->getCouldNotCompute(); 5459 if (Preds && ENT.getPred()) 5460 Preds->add(ENT.getPred()); 5461 5462 assert((Preds || ENT.hasAlwaysTruePred()) && 5463 "Predicate should be always true!"); 5464 } 5465 5466 assert(BECount && "Invalid not taken count for loop exit"); 5467 return BECount; 5468 } 5469 5470 /// getExact - Get the exact not taken count for this loop exit. 5471 const SCEV * 5472 ScalarEvolution::BackedgeTakenInfo::getExact(BasicBlock *ExitingBlock, 5473 ScalarEvolution *SE) const { 5474 for (auto &ENT : ExitNotTaken) 5475 if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePred()) 5476 return ENT.ExactNotTaken; 5477 5478 return SE->getCouldNotCompute(); 5479 } 5480 5481 /// getMax - Get the max backedge taken count for the loop. 5482 const SCEV * 5483 ScalarEvolution::BackedgeTakenInfo::getMax(ScalarEvolution *SE) const { 5484 for (auto &ENT : ExitNotTaken) 5485 if (!ENT.hasAlwaysTruePred()) 5486 return SE->getCouldNotCompute(); 5487 5488 return Max ? Max : SE->getCouldNotCompute(); 5489 } 5490 5491 bool ScalarEvolution::BackedgeTakenInfo::hasOperand(const SCEV *S, 5492 ScalarEvolution *SE) const { 5493 if (Max && Max != SE->getCouldNotCompute() && SE->hasOperand(Max, S)) 5494 return true; 5495 5496 if (!ExitNotTaken.ExitingBlock) 5497 return false; 5498 5499 for (auto &ENT : ExitNotTaken) 5500 if (ENT.ExactNotTaken != SE->getCouldNotCompute() && 5501 SE->hasOperand(ENT.ExactNotTaken, S)) 5502 return true; 5503 5504 return false; 5505 } 5506 5507 /// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each 5508 /// computable exit into a persistent ExitNotTakenInfo array. 5509 ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo( 5510 SmallVectorImpl<EdgeInfo> &ExitCounts, bool Complete, const SCEV *MaxCount) 5511 : Max(MaxCount) { 5512 5513 if (!Complete) 5514 ExitNotTaken.setIncomplete(); 5515 5516 unsigned NumExits = ExitCounts.size(); 5517 if (NumExits == 0) return; 5518 5519 ExitNotTaken.ExitingBlock = ExitCounts[0].ExitBlock; 5520 ExitNotTaken.ExactNotTaken = ExitCounts[0].Taken; 5521 5522 // Determine the number of ExitNotTakenExtras structures that we need. 5523 unsigned ExtraInfoSize = 0; 5524 if (NumExits > 1) 5525 ExtraInfoSize = 1 + std::count_if(std::next(ExitCounts.begin()), 5526 ExitCounts.end(), [](EdgeInfo &Entry) { 5527 return !Entry.Pred.isAlwaysTrue(); 5528 }); 5529 else if (!ExitCounts[0].Pred.isAlwaysTrue()) 5530 ExtraInfoSize = 1; 5531 5532 ExitNotTakenExtras *ENT = nullptr; 5533 5534 // Allocate the ExitNotTakenExtras structures and initialize the first 5535 // element (ExitNotTaken). 5536 if (ExtraInfoSize > 0) { 5537 ENT = new ExitNotTakenExtras[ExtraInfoSize]; 5538 ExitNotTaken.ExtraInfo.setPointer(&ENT[0]); 5539 *ExitNotTaken.getPred() = std::move(ExitCounts[0].Pred); 5540 } 5541 5542 if (NumExits == 1) 5543 return; 5544 5545 auto &Exits = ExitNotTaken.ExtraInfo.getPointer()->Exits; 5546 5547 // Handle the rare case of multiple computable exits. 5548 for (unsigned i = 1, PredPos = 1; i < NumExits; ++i) { 5549 ExitNotTakenExtras *Ptr = nullptr; 5550 if (!ExitCounts[i].Pred.isAlwaysTrue()) { 5551 Ptr = &ENT[PredPos++]; 5552 Ptr->Pred = std::move(ExitCounts[i].Pred); 5553 } 5554 5555 Exits.emplace_back(ExitCounts[i].ExitBlock, ExitCounts[i].Taken, Ptr); 5556 } 5557 } 5558 5559 /// clear - Invalidate this result and free the ExitNotTakenInfo array. 5560 void ScalarEvolution::BackedgeTakenInfo::clear() { 5561 ExitNotTaken.ExitingBlock = nullptr; 5562 ExitNotTaken.ExactNotTaken = nullptr; 5563 delete[] ExitNotTaken.ExtraInfo.getPointer(); 5564 } 5565 5566 /// computeBackedgeTakenCount - Compute the number of times the backedge 5567 /// of the specified loop will execute. 5568 ScalarEvolution::BackedgeTakenInfo 5569 ScalarEvolution::computeBackedgeTakenCount(const Loop *L, 5570 bool AllowPredicates) { 5571 SmallVector<BasicBlock *, 8> ExitingBlocks; 5572 L->getExitingBlocks(ExitingBlocks); 5573 5574 SmallVector<EdgeInfo, 4> ExitCounts; 5575 bool CouldComputeBECount = true; 5576 BasicBlock *Latch = L->getLoopLatch(); // may be NULL. 5577 const SCEV *MustExitMaxBECount = nullptr; 5578 const SCEV *MayExitMaxBECount = nullptr; 5579 5580 // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts 5581 // and compute maxBECount. 5582 // Do a union of all the predicates here. 5583 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { 5584 BasicBlock *ExitBB = ExitingBlocks[i]; 5585 ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates); 5586 5587 assert((AllowPredicates || EL.Pred.isAlwaysTrue()) && 5588 "Predicated exit limit when predicates are not allowed!"); 5589 5590 // 1. For each exit that can be computed, add an entry to ExitCounts. 5591 // CouldComputeBECount is true only if all exits can be computed. 5592 if (EL.Exact == getCouldNotCompute()) 5593 // We couldn't compute an exact value for this exit, so 5594 // we won't be able to compute an exact value for the loop. 5595 CouldComputeBECount = false; 5596 else 5597 ExitCounts.emplace_back(EdgeInfo(ExitBB, EL.Exact, EL.Pred)); 5598 5599 // 2. Derive the loop's MaxBECount from each exit's max number of 5600 // non-exiting iterations. Partition the loop exits into two kinds: 5601 // LoopMustExits and LoopMayExits. 5602 // 5603 // If the exit dominates the loop latch, it is a LoopMustExit otherwise it 5604 // is a LoopMayExit. If any computable LoopMustExit is found, then 5605 // MaxBECount is the minimum EL.Max of computable LoopMustExits. Otherwise, 5606 // MaxBECount is conservatively the maximum EL.Max, where CouldNotCompute is 5607 // considered greater than any computable EL.Max. 5608 if (EL.Max != getCouldNotCompute() && Latch && 5609 DT.dominates(ExitBB, Latch)) { 5610 if (!MustExitMaxBECount) 5611 MustExitMaxBECount = EL.Max; 5612 else { 5613 MustExitMaxBECount = 5614 getUMinFromMismatchedTypes(MustExitMaxBECount, EL.Max); 5615 } 5616 } else if (MayExitMaxBECount != getCouldNotCompute()) { 5617 if (!MayExitMaxBECount || EL.Max == getCouldNotCompute()) 5618 MayExitMaxBECount = EL.Max; 5619 else { 5620 MayExitMaxBECount = 5621 getUMaxFromMismatchedTypes(MayExitMaxBECount, EL.Max); 5622 } 5623 } 5624 } 5625 const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount : 5626 (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute()); 5627 return BackedgeTakenInfo(ExitCounts, CouldComputeBECount, MaxBECount); 5628 } 5629 5630 ScalarEvolution::ExitLimit 5631 ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock, 5632 bool AllowPredicates) { 5633 5634 // Okay, we've chosen an exiting block. See what condition causes us to exit 5635 // at this block and remember the exit block and whether all other targets 5636 // lead to the loop header. 5637 bool MustExecuteLoopHeader = true; 5638 BasicBlock *Exit = nullptr; 5639 for (auto *SBB : successors(ExitingBlock)) 5640 if (!L->contains(SBB)) { 5641 if (Exit) // Multiple exit successors. 5642 return getCouldNotCompute(); 5643 Exit = SBB; 5644 } else if (SBB != L->getHeader()) { 5645 MustExecuteLoopHeader = false; 5646 } 5647 5648 // At this point, we know we have a conditional branch that determines whether 5649 // the loop is exited. However, we don't know if the branch is executed each 5650 // time through the loop. If not, then the execution count of the branch will 5651 // not be equal to the trip count of the loop. 5652 // 5653 // Currently we check for this by checking to see if the Exit branch goes to 5654 // the loop header. If so, we know it will always execute the same number of 5655 // times as the loop. We also handle the case where the exit block *is* the 5656 // loop header. This is common for un-rotated loops. 5657 // 5658 // If both of those tests fail, walk up the unique predecessor chain to the 5659 // header, stopping if there is an edge that doesn't exit the loop. If the 5660 // header is reached, the execution count of the branch will be equal to the 5661 // trip count of the loop. 5662 // 5663 // More extensive analysis could be done to handle more cases here. 5664 // 5665 if (!MustExecuteLoopHeader && ExitingBlock != L->getHeader()) { 5666 // The simple checks failed, try climbing the unique predecessor chain 5667 // up to the header. 5668 bool Ok = false; 5669 for (BasicBlock *BB = ExitingBlock; BB; ) { 5670 BasicBlock *Pred = BB->getUniquePredecessor(); 5671 if (!Pred) 5672 return getCouldNotCompute(); 5673 TerminatorInst *PredTerm = Pred->getTerminator(); 5674 for (const BasicBlock *PredSucc : PredTerm->successors()) { 5675 if (PredSucc == BB) 5676 continue; 5677 // If the predecessor has a successor that isn't BB and isn't 5678 // outside the loop, assume the worst. 5679 if (L->contains(PredSucc)) 5680 return getCouldNotCompute(); 5681 } 5682 if (Pred == L->getHeader()) { 5683 Ok = true; 5684 break; 5685 } 5686 BB = Pred; 5687 } 5688 if (!Ok) 5689 return getCouldNotCompute(); 5690 } 5691 5692 bool IsOnlyExit = (L->getExitingBlock() != nullptr); 5693 TerminatorInst *Term = ExitingBlock->getTerminator(); 5694 if (BranchInst *BI = dyn_cast<BranchInst>(Term)) { 5695 assert(BI->isConditional() && "If unconditional, it can't be in loop!"); 5696 // Proceed to the next level to examine the exit condition expression. 5697 return computeExitLimitFromCond( 5698 L, BI->getCondition(), BI->getSuccessor(0), BI->getSuccessor(1), 5699 /*ControlsExit=*/IsOnlyExit, AllowPredicates); 5700 } 5701 5702 if (SwitchInst *SI = dyn_cast<SwitchInst>(Term)) 5703 return computeExitLimitFromSingleExitSwitch(L, SI, Exit, 5704 /*ControlsExit=*/IsOnlyExit); 5705 5706 return getCouldNotCompute(); 5707 } 5708 5709 /// computeExitLimitFromCond - Compute the number of times the 5710 /// backedge of the specified loop will execute if its exit condition 5711 /// were a conditional branch of ExitCond, TBB, and FBB. 5712 /// 5713 /// @param ControlsExit is true if ExitCond directly controls the exit 5714 /// branch. In this case, we can assume that the loop exits only if the 5715 /// condition is true and can infer that failing to meet the condition prior to 5716 /// integer wraparound results in undefined behavior. 5717 ScalarEvolution::ExitLimit 5718 ScalarEvolution::computeExitLimitFromCond(const Loop *L, 5719 Value *ExitCond, 5720 BasicBlock *TBB, 5721 BasicBlock *FBB, 5722 bool ControlsExit, 5723 bool AllowPredicates) { 5724 // Check if the controlling expression for this loop is an And or Or. 5725 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { 5726 if (BO->getOpcode() == Instruction::And) { 5727 // Recurse on the operands of the and. 5728 bool EitherMayExit = L->contains(TBB); 5729 ExitLimit EL0 = computeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB, 5730 ControlsExit && !EitherMayExit, 5731 AllowPredicates); 5732 ExitLimit EL1 = computeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB, 5733 ControlsExit && !EitherMayExit, 5734 AllowPredicates); 5735 const SCEV *BECount = getCouldNotCompute(); 5736 const SCEV *MaxBECount = getCouldNotCompute(); 5737 if (EitherMayExit) { 5738 // Both conditions must be true for the loop to continue executing. 5739 // Choose the less conservative count. 5740 if (EL0.Exact == getCouldNotCompute() || 5741 EL1.Exact == getCouldNotCompute()) 5742 BECount = getCouldNotCompute(); 5743 else 5744 BECount = getUMinFromMismatchedTypes(EL0.Exact, EL1.Exact); 5745 if (EL0.Max == getCouldNotCompute()) 5746 MaxBECount = EL1.Max; 5747 else if (EL1.Max == getCouldNotCompute()) 5748 MaxBECount = EL0.Max; 5749 else 5750 MaxBECount = getUMinFromMismatchedTypes(EL0.Max, EL1.Max); 5751 } else { 5752 // Both conditions must be true at the same time for the loop to exit. 5753 // For now, be conservative. 5754 assert(L->contains(FBB) && "Loop block has no successor in loop!"); 5755 if (EL0.Max == EL1.Max) 5756 MaxBECount = EL0.Max; 5757 if (EL0.Exact == EL1.Exact) 5758 BECount = EL0.Exact; 5759 } 5760 5761 SCEVUnionPredicate NP; 5762 NP.add(&EL0.Pred); 5763 NP.add(&EL1.Pred); 5764 // There are cases (e.g. PR26207) where computeExitLimitFromCond is able 5765 // to be more aggressive when computing BECount than when computing 5766 // MaxBECount. In these cases it is possible for EL0.Exact and EL1.Exact 5767 // to match, but for EL0.Max and EL1.Max to not. 5768 if (isa<SCEVCouldNotCompute>(MaxBECount) && 5769 !isa<SCEVCouldNotCompute>(BECount)) 5770 MaxBECount = BECount; 5771 5772 return ExitLimit(BECount, MaxBECount, NP); 5773 } 5774 if (BO->getOpcode() == Instruction::Or) { 5775 // Recurse on the operands of the or. 5776 bool EitherMayExit = L->contains(FBB); 5777 ExitLimit EL0 = computeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB, 5778 ControlsExit && !EitherMayExit, 5779 AllowPredicates); 5780 ExitLimit EL1 = computeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB, 5781 ControlsExit && !EitherMayExit, 5782 AllowPredicates); 5783 const SCEV *BECount = getCouldNotCompute(); 5784 const SCEV *MaxBECount = getCouldNotCompute(); 5785 if (EitherMayExit) { 5786 // Both conditions must be false for the loop to continue executing. 5787 // Choose the less conservative count. 5788 if (EL0.Exact == getCouldNotCompute() || 5789 EL1.Exact == getCouldNotCompute()) 5790 BECount = getCouldNotCompute(); 5791 else 5792 BECount = getUMinFromMismatchedTypes(EL0.Exact, EL1.Exact); 5793 if (EL0.Max == getCouldNotCompute()) 5794 MaxBECount = EL1.Max; 5795 else if (EL1.Max == getCouldNotCompute()) 5796 MaxBECount = EL0.Max; 5797 else 5798 MaxBECount = getUMinFromMismatchedTypes(EL0.Max, EL1.Max); 5799 } else { 5800 // Both conditions must be false at the same time for the loop to exit. 5801 // For now, be conservative. 5802 assert(L->contains(TBB) && "Loop block has no successor in loop!"); 5803 if (EL0.Max == EL1.Max) 5804 MaxBECount = EL0.Max; 5805 if (EL0.Exact == EL1.Exact) 5806 BECount = EL0.Exact; 5807 } 5808 5809 SCEVUnionPredicate NP; 5810 NP.add(&EL0.Pred); 5811 NP.add(&EL1.Pred); 5812 return ExitLimit(BECount, MaxBECount, NP); 5813 } 5814 } 5815 5816 // With an icmp, it may be feasible to compute an exact backedge-taken count. 5817 // Proceed to the next level to examine the icmp. 5818 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) { 5819 ExitLimit EL = 5820 computeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, ControlsExit); 5821 if (EL.hasFullInfo() || !AllowPredicates) 5822 return EL; 5823 5824 // Try again, but use SCEV predicates this time. 5825 return computeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, ControlsExit, 5826 /*AllowPredicates=*/true); 5827 } 5828 5829 // Check for a constant condition. These are normally stripped out by 5830 // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to 5831 // preserve the CFG and is temporarily leaving constant conditions 5832 // in place. 5833 if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) { 5834 if (L->contains(FBB) == !CI->getZExtValue()) 5835 // The backedge is always taken. 5836 return getCouldNotCompute(); 5837 else 5838 // The backedge is never taken. 5839 return getZero(CI->getType()); 5840 } 5841 5842 // If it's not an integer or pointer comparison then compute it the hard way. 5843 return computeExitCountExhaustively(L, ExitCond, !L->contains(TBB)); 5844 } 5845 5846 ScalarEvolution::ExitLimit 5847 ScalarEvolution::computeExitLimitFromICmp(const Loop *L, 5848 ICmpInst *ExitCond, 5849 BasicBlock *TBB, 5850 BasicBlock *FBB, 5851 bool ControlsExit, 5852 bool AllowPredicates) { 5853 5854 // If the condition was exit on true, convert the condition to exit on false 5855 ICmpInst::Predicate Cond; 5856 if (!L->contains(FBB)) 5857 Cond = ExitCond->getPredicate(); 5858 else 5859 Cond = ExitCond->getInversePredicate(); 5860 5861 // Handle common loops like: for (X = "string"; *X; ++X) 5862 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) 5863 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { 5864 ExitLimit ItCnt = 5865 computeLoadConstantCompareExitLimit(LI, RHS, L, Cond); 5866 if (ItCnt.hasAnyInfo()) 5867 return ItCnt; 5868 } 5869 5870 ExitLimit ShiftEL = computeShiftCompareExitLimit( 5871 ExitCond->getOperand(0), ExitCond->getOperand(1), L, Cond); 5872 if (ShiftEL.hasAnyInfo()) 5873 return ShiftEL; 5874 5875 const SCEV *LHS = getSCEV(ExitCond->getOperand(0)); 5876 const SCEV *RHS = getSCEV(ExitCond->getOperand(1)); 5877 5878 // Try to evaluate any dependencies out of the loop. 5879 LHS = getSCEVAtScope(LHS, L); 5880 RHS = getSCEVAtScope(RHS, L); 5881 5882 // At this point, we would like to compute how many iterations of the 5883 // loop the predicate will return true for these inputs. 5884 if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) { 5885 // If there is a loop-invariant, force it into the RHS. 5886 std::swap(LHS, RHS); 5887 Cond = ICmpInst::getSwappedPredicate(Cond); 5888 } 5889 5890 // Simplify the operands before analyzing them. 5891 (void)SimplifyICmpOperands(Cond, LHS, RHS); 5892 5893 // If we have a comparison of a chrec against a constant, try to use value 5894 // ranges to answer this query. 5895 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) 5896 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) 5897 if (AddRec->getLoop() == L) { 5898 // Form the constant range. 5899 ConstantRange CompRange( 5900 ICmpInst::makeConstantRange(Cond, RHSC->getAPInt())); 5901 5902 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this); 5903 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; 5904 } 5905 5906 switch (Cond) { 5907 case ICmpInst::ICMP_NE: { // while (X != Y) 5908 // Convert to: while (X-Y != 0) 5909 ExitLimit EL = HowFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit, 5910 AllowPredicates); 5911 if (EL.hasAnyInfo()) return EL; 5912 break; 5913 } 5914 case ICmpInst::ICMP_EQ: { // while (X == Y) 5915 // Convert to: while (X-Y == 0) 5916 ExitLimit EL = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); 5917 if (EL.hasAnyInfo()) return EL; 5918 break; 5919 } 5920 case ICmpInst::ICMP_SLT: 5921 case ICmpInst::ICMP_ULT: { // while (X < Y) 5922 bool IsSigned = Cond == ICmpInst::ICMP_SLT; 5923 ExitLimit EL = HowManyLessThans(LHS, RHS, L, IsSigned, ControlsExit, 5924 AllowPredicates); 5925 if (EL.hasAnyInfo()) return EL; 5926 break; 5927 } 5928 case ICmpInst::ICMP_SGT: 5929 case ICmpInst::ICMP_UGT: { // while (X > Y) 5930 bool IsSigned = Cond == ICmpInst::ICMP_SGT; 5931 ExitLimit EL = 5932 HowManyGreaterThans(LHS, RHS, L, IsSigned, ControlsExit, 5933 AllowPredicates); 5934 if (EL.hasAnyInfo()) return EL; 5935 break; 5936 } 5937 default: 5938 break; 5939 } 5940 return computeExitCountExhaustively(L, ExitCond, !L->contains(TBB)); 5941 } 5942 5943 ScalarEvolution::ExitLimit 5944 ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L, 5945 SwitchInst *Switch, 5946 BasicBlock *ExitingBlock, 5947 bool ControlsExit) { 5948 assert(!L->contains(ExitingBlock) && "Not an exiting block!"); 5949 5950 // Give up if the exit is the default dest of a switch. 5951 if (Switch->getDefaultDest() == ExitingBlock) 5952 return getCouldNotCompute(); 5953 5954 assert(L->contains(Switch->getDefaultDest()) && 5955 "Default case must not exit the loop!"); 5956 const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L); 5957 const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock)); 5958 5959 // while (X != Y) --> while (X-Y != 0) 5960 ExitLimit EL = HowFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit); 5961 if (EL.hasAnyInfo()) 5962 return EL; 5963 5964 return getCouldNotCompute(); 5965 } 5966 5967 static ConstantInt * 5968 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, 5969 ScalarEvolution &SE) { 5970 const SCEV *InVal = SE.getConstant(C); 5971 const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE); 5972 assert(isa<SCEVConstant>(Val) && 5973 "Evaluation of SCEV at constant didn't fold correctly?"); 5974 return cast<SCEVConstant>(Val)->getValue(); 5975 } 5976 5977 /// computeLoadConstantCompareExitLimit - Given an exit condition of 5978 /// 'icmp op load X, cst', try to see if we can compute the backedge 5979 /// execution count. 5980 ScalarEvolution::ExitLimit 5981 ScalarEvolution::computeLoadConstantCompareExitLimit( 5982 LoadInst *LI, 5983 Constant *RHS, 5984 const Loop *L, 5985 ICmpInst::Predicate predicate) { 5986 5987 if (LI->isVolatile()) return getCouldNotCompute(); 5988 5989 // Check to see if the loaded pointer is a getelementptr of a global. 5990 // TODO: Use SCEV instead of manually grubbing with GEPs. 5991 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); 5992 if (!GEP) return getCouldNotCompute(); 5993 5994 // Make sure that it is really a constant global we are gepping, with an 5995 // initializer, and make sure the first IDX is really 0. 5996 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); 5997 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() || 5998 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || 5999 !cast<Constant>(GEP->getOperand(1))->isNullValue()) 6000 return getCouldNotCompute(); 6001 6002 // Okay, we allow one non-constant index into the GEP instruction. 6003 Value *VarIdx = nullptr; 6004 std::vector<Constant*> Indexes; 6005 unsigned VarIdxNum = 0; 6006 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) 6007 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { 6008 Indexes.push_back(CI); 6009 } else if (!isa<ConstantInt>(GEP->getOperand(i))) { 6010 if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. 6011 VarIdx = GEP->getOperand(i); 6012 VarIdxNum = i-2; 6013 Indexes.push_back(nullptr); 6014 } 6015 6016 // Loop-invariant loads may be a byproduct of loop optimization. Skip them. 6017 if (!VarIdx) 6018 return getCouldNotCompute(); 6019 6020 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. 6021 // Check to see if X is a loop variant variable value now. 6022 const SCEV *Idx = getSCEV(VarIdx); 6023 Idx = getSCEVAtScope(Idx, L); 6024 6025 // We can only recognize very limited forms of loop index expressions, in 6026 // particular, only affine AddRec's like {C1,+,C2}. 6027 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); 6028 if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) || 6029 !isa<SCEVConstant>(IdxExpr->getOperand(0)) || 6030 !isa<SCEVConstant>(IdxExpr->getOperand(1))) 6031 return getCouldNotCompute(); 6032 6033 unsigned MaxSteps = MaxBruteForceIterations; 6034 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { 6035 ConstantInt *ItCst = ConstantInt::get( 6036 cast<IntegerType>(IdxExpr->getType()), IterationNum); 6037 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); 6038 6039 // Form the GEP offset. 6040 Indexes[VarIdxNum] = Val; 6041 6042 Constant *Result = ConstantFoldLoadThroughGEPIndices(GV->getInitializer(), 6043 Indexes); 6044 if (!Result) break; // Cannot compute! 6045 6046 // Evaluate the condition for this iteration. 6047 Result = ConstantExpr::getICmp(predicate, Result, RHS); 6048 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure 6049 if (cast<ConstantInt>(Result)->getValue().isMinValue()) { 6050 ++NumArrayLenItCounts; 6051 return getConstant(ItCst); // Found terminating iteration! 6052 } 6053 } 6054 return getCouldNotCompute(); 6055 } 6056 6057 ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit( 6058 Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) { 6059 ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV); 6060 if (!RHS) 6061 return getCouldNotCompute(); 6062 6063 const BasicBlock *Latch = L->getLoopLatch(); 6064 if (!Latch) 6065 return getCouldNotCompute(); 6066 6067 const BasicBlock *Predecessor = L->getLoopPredecessor(); 6068 if (!Predecessor) 6069 return getCouldNotCompute(); 6070 6071 // Return true if V is of the form "LHS `shift_op` <positive constant>". 6072 // Return LHS in OutLHS and shift_opt in OutOpCode. 6073 auto MatchPositiveShift = 6074 [](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) { 6075 6076 using namespace PatternMatch; 6077 6078 ConstantInt *ShiftAmt; 6079 if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) 6080 OutOpCode = Instruction::LShr; 6081 else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) 6082 OutOpCode = Instruction::AShr; 6083 else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt)))) 6084 OutOpCode = Instruction::Shl; 6085 else 6086 return false; 6087 6088 return ShiftAmt->getValue().isStrictlyPositive(); 6089 }; 6090 6091 // Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in 6092 // 6093 // loop: 6094 // %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ] 6095 // %iv.shifted = lshr i32 %iv, <positive constant> 6096 // 6097 // Return true on a succesful match. Return the corresponding PHI node (%iv 6098 // above) in PNOut and the opcode of the shift operation in OpCodeOut. 6099 auto MatchShiftRecurrence = 6100 [&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) { 6101 Optional<Instruction::BinaryOps> PostShiftOpCode; 6102 6103 { 6104 Instruction::BinaryOps OpC; 6105 Value *V; 6106 6107 // If we encounter a shift instruction, "peel off" the shift operation, 6108 // and remember that we did so. Later when we inspect %iv's backedge 6109 // value, we will make sure that the backedge value uses the same 6110 // operation. 6111 // 6112 // Note: the peeled shift operation does not have to be the same 6113 // instruction as the one feeding into the PHI's backedge value. We only 6114 // really care about it being the same *kind* of shift instruction -- 6115 // that's all that is required for our later inferences to hold. 6116 if (MatchPositiveShift(LHS, V, OpC)) { 6117 PostShiftOpCode = OpC; 6118 LHS = V; 6119 } 6120 } 6121 6122 PNOut = dyn_cast<PHINode>(LHS); 6123 if (!PNOut || PNOut->getParent() != L->getHeader()) 6124 return false; 6125 6126 Value *BEValue = PNOut->getIncomingValueForBlock(Latch); 6127 Value *OpLHS; 6128 6129 return 6130 // The backedge value for the PHI node must be a shift by a positive 6131 // amount 6132 MatchPositiveShift(BEValue, OpLHS, OpCodeOut) && 6133 6134 // of the PHI node itself 6135 OpLHS == PNOut && 6136 6137 // and the kind of shift should be match the kind of shift we peeled 6138 // off, if any. 6139 (!PostShiftOpCode.hasValue() || *PostShiftOpCode == OpCodeOut); 6140 }; 6141 6142 PHINode *PN; 6143 Instruction::BinaryOps OpCode; 6144 if (!MatchShiftRecurrence(LHS, PN, OpCode)) 6145 return getCouldNotCompute(); 6146 6147 const DataLayout &DL = getDataLayout(); 6148 6149 // The key rationale for this optimization is that for some kinds of shift 6150 // recurrences, the value of the recurrence "stabilizes" to either 0 or -1 6151 // within a finite number of iterations. If the condition guarding the 6152 // backedge (in the sense that the backedge is taken if the condition is true) 6153 // is false for the value the shift recurrence stabilizes to, then we know 6154 // that the backedge is taken only a finite number of times. 6155 6156 ConstantInt *StableValue = nullptr; 6157 switch (OpCode) { 6158 default: 6159 llvm_unreachable("Impossible case!"); 6160 6161 case Instruction::AShr: { 6162 // {K,ashr,<positive-constant>} stabilizes to signum(K) in at most 6163 // bitwidth(K) iterations. 6164 Value *FirstValue = PN->getIncomingValueForBlock(Predecessor); 6165 bool KnownZero, KnownOne; 6166 ComputeSignBit(FirstValue, KnownZero, KnownOne, DL, 0, nullptr, 6167 Predecessor->getTerminator(), &DT); 6168 auto *Ty = cast<IntegerType>(RHS->getType()); 6169 if (KnownZero) 6170 StableValue = ConstantInt::get(Ty, 0); 6171 else if (KnownOne) 6172 StableValue = ConstantInt::get(Ty, -1, true); 6173 else 6174 return getCouldNotCompute(); 6175 6176 break; 6177 } 6178 case Instruction::LShr: 6179 case Instruction::Shl: 6180 // Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>} 6181 // stabilize to 0 in at most bitwidth(K) iterations. 6182 StableValue = ConstantInt::get(cast<IntegerType>(RHS->getType()), 0); 6183 break; 6184 } 6185 6186 auto *Result = 6187 ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI); 6188 assert(Result->getType()->isIntegerTy(1) && 6189 "Otherwise cannot be an operand to a branch instruction"); 6190 6191 if (Result->isZeroValue()) { 6192 unsigned BitWidth = getTypeSizeInBits(RHS->getType()); 6193 const SCEV *UpperBound = 6194 getConstant(getEffectiveSCEVType(RHS->getType()), BitWidth); 6195 SCEVUnionPredicate P; 6196 return ExitLimit(getCouldNotCompute(), UpperBound, P); 6197 } 6198 6199 return getCouldNotCompute(); 6200 } 6201 6202 /// CanConstantFold - Return true if we can constant fold an instruction of the 6203 /// specified type, assuming that all operands were constants. 6204 static bool CanConstantFold(const Instruction *I) { 6205 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || 6206 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) || 6207 isa<LoadInst>(I)) 6208 return true; 6209 6210 if (const CallInst *CI = dyn_cast<CallInst>(I)) 6211 if (const Function *F = CI->getCalledFunction()) 6212 return canConstantFoldCallTo(F); 6213 return false; 6214 } 6215 6216 /// Determine whether this instruction can constant evolve within this loop 6217 /// assuming its operands can all constant evolve. 6218 static bool canConstantEvolve(Instruction *I, const Loop *L) { 6219 // An instruction outside of the loop can't be derived from a loop PHI. 6220 if (!L->contains(I)) return false; 6221 6222 if (isa<PHINode>(I)) { 6223 // We don't currently keep track of the control flow needed to evaluate 6224 // PHIs, so we cannot handle PHIs inside of loops. 6225 return L->getHeader() == I->getParent(); 6226 } 6227 6228 // If we won't be able to constant fold this expression even if the operands 6229 // are constants, bail early. 6230 return CanConstantFold(I); 6231 } 6232 6233 /// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by 6234 /// recursing through each instruction operand until reaching a loop header phi. 6235 static PHINode * 6236 getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L, 6237 DenseMap<Instruction *, PHINode *> &PHIMap) { 6238 6239 // Otherwise, we can evaluate this instruction if all of its operands are 6240 // constant or derived from a PHI node themselves. 6241 PHINode *PHI = nullptr; 6242 for (Value *Op : UseInst->operands()) { 6243 if (isa<Constant>(Op)) continue; 6244 6245 Instruction *OpInst = dyn_cast<Instruction>(Op); 6246 if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr; 6247 6248 PHINode *P = dyn_cast<PHINode>(OpInst); 6249 if (!P) 6250 // If this operand is already visited, reuse the prior result. 6251 // We may have P != PHI if this is the deepest point at which the 6252 // inconsistent paths meet. 6253 P = PHIMap.lookup(OpInst); 6254 if (!P) { 6255 // Recurse and memoize the results, whether a phi is found or not. 6256 // This recursive call invalidates pointers into PHIMap. 6257 P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap); 6258 PHIMap[OpInst] = P; 6259 } 6260 if (!P) 6261 return nullptr; // Not evolving from PHI 6262 if (PHI && PHI != P) 6263 return nullptr; // Evolving from multiple different PHIs. 6264 PHI = P; 6265 } 6266 // This is a expression evolving from a constant PHI! 6267 return PHI; 6268 } 6269 6270 /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node 6271 /// in the loop that V is derived from. We allow arbitrary operations along the 6272 /// way, but the operands of an operation must either be constants or a value 6273 /// derived from a constant PHI. If this expression does not fit with these 6274 /// constraints, return null. 6275 static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { 6276 Instruction *I = dyn_cast<Instruction>(V); 6277 if (!I || !canConstantEvolve(I, L)) return nullptr; 6278 6279 if (PHINode *PN = dyn_cast<PHINode>(I)) 6280 return PN; 6281 6282 // Record non-constant instructions contained by the loop. 6283 DenseMap<Instruction *, PHINode *> PHIMap; 6284 return getConstantEvolvingPHIOperands(I, L, PHIMap); 6285 } 6286 6287 /// EvaluateExpression - Given an expression that passes the 6288 /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node 6289 /// in the loop has the value PHIVal. If we can't fold this expression for some 6290 /// reason, return null. 6291 static Constant *EvaluateExpression(Value *V, const Loop *L, 6292 DenseMap<Instruction *, Constant *> &Vals, 6293 const DataLayout &DL, 6294 const TargetLibraryInfo *TLI) { 6295 // Convenient constant check, but redundant for recursive calls. 6296 if (Constant *C = dyn_cast<Constant>(V)) return C; 6297 Instruction *I = dyn_cast<Instruction>(V); 6298 if (!I) return nullptr; 6299 6300 if (Constant *C = Vals.lookup(I)) return C; 6301 6302 // An instruction inside the loop depends on a value outside the loop that we 6303 // weren't given a mapping for, or a value such as a call inside the loop. 6304 if (!canConstantEvolve(I, L)) return nullptr; 6305 6306 // An unmapped PHI can be due to a branch or another loop inside this loop, 6307 // or due to this not being the initial iteration through a loop where we 6308 // couldn't compute the evolution of this particular PHI last time. 6309 if (isa<PHINode>(I)) return nullptr; 6310 6311 std::vector<Constant*> Operands(I->getNumOperands()); 6312 6313 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 6314 Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i)); 6315 if (!Operand) { 6316 Operands[i] = dyn_cast<Constant>(I->getOperand(i)); 6317 if (!Operands[i]) return nullptr; 6318 continue; 6319 } 6320 Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI); 6321 Vals[Operand] = C; 6322 if (!C) return nullptr; 6323 Operands[i] = C; 6324 } 6325 6326 if (CmpInst *CI = dyn_cast<CmpInst>(I)) 6327 return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], 6328 Operands[1], DL, TLI); 6329 if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 6330 if (!LI->isVolatile()) 6331 return ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL); 6332 } 6333 return ConstantFoldInstOperands(I, Operands, DL, TLI); 6334 } 6335 6336 6337 // If every incoming value to PN except the one for BB is a specific Constant, 6338 // return that, else return nullptr. 6339 static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) { 6340 Constant *IncomingVal = nullptr; 6341 6342 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 6343 if (PN->getIncomingBlock(i) == BB) 6344 continue; 6345 6346 auto *CurrentVal = dyn_cast<Constant>(PN->getIncomingValue(i)); 6347 if (!CurrentVal) 6348 return nullptr; 6349 6350 if (IncomingVal != CurrentVal) { 6351 if (IncomingVal) 6352 return nullptr; 6353 IncomingVal = CurrentVal; 6354 } 6355 } 6356 6357 return IncomingVal; 6358 } 6359 6360 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is 6361 /// in the header of its containing loop, we know the loop executes a 6362 /// constant number of times, and the PHI node is just a recurrence 6363 /// involving constants, fold it. 6364 Constant * 6365 ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, 6366 const APInt &BEs, 6367 const Loop *L) { 6368 auto I = ConstantEvolutionLoopExitValue.find(PN); 6369 if (I != ConstantEvolutionLoopExitValue.end()) 6370 return I->second; 6371 6372 if (BEs.ugt(MaxBruteForceIterations)) 6373 return ConstantEvolutionLoopExitValue[PN] = nullptr; // Not going to evaluate it. 6374 6375 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; 6376 6377 DenseMap<Instruction *, Constant *> CurrentIterVals; 6378 BasicBlock *Header = L->getHeader(); 6379 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!"); 6380 6381 BasicBlock *Latch = L->getLoopLatch(); 6382 if (!Latch) 6383 return nullptr; 6384 6385 for (auto &I : *Header) { 6386 PHINode *PHI = dyn_cast<PHINode>(&I); 6387 if (!PHI) break; 6388 auto *StartCST = getOtherIncomingValue(PHI, Latch); 6389 if (!StartCST) continue; 6390 CurrentIterVals[PHI] = StartCST; 6391 } 6392 if (!CurrentIterVals.count(PN)) 6393 return RetVal = nullptr; 6394 6395 Value *BEValue = PN->getIncomingValueForBlock(Latch); 6396 6397 // Execute the loop symbolically to determine the exit value. 6398 if (BEs.getActiveBits() >= 32) 6399 return RetVal = nullptr; // More than 2^32-1 iterations?? Not doing it! 6400 6401 unsigned NumIterations = BEs.getZExtValue(); // must be in range 6402 unsigned IterationNum = 0; 6403 const DataLayout &DL = getDataLayout(); 6404 for (; ; ++IterationNum) { 6405 if (IterationNum == NumIterations) 6406 return RetVal = CurrentIterVals[PN]; // Got exit value! 6407 6408 // Compute the value of the PHIs for the next iteration. 6409 // EvaluateExpression adds non-phi values to the CurrentIterVals map. 6410 DenseMap<Instruction *, Constant *> NextIterVals; 6411 Constant *NextPHI = 6412 EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); 6413 if (!NextPHI) 6414 return nullptr; // Couldn't evaluate! 6415 NextIterVals[PN] = NextPHI; 6416 6417 bool StoppedEvolving = NextPHI == CurrentIterVals[PN]; 6418 6419 // Also evaluate the other PHI nodes. However, we don't get to stop if we 6420 // cease to be able to evaluate one of them or if they stop evolving, 6421 // because that doesn't necessarily prevent us from computing PN. 6422 SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute; 6423 for (const auto &I : CurrentIterVals) { 6424 PHINode *PHI = dyn_cast<PHINode>(I.first); 6425 if (!PHI || PHI == PN || PHI->getParent() != Header) continue; 6426 PHIsToCompute.emplace_back(PHI, I.second); 6427 } 6428 // We use two distinct loops because EvaluateExpression may invalidate any 6429 // iterators into CurrentIterVals. 6430 for (const auto &I : PHIsToCompute) { 6431 PHINode *PHI = I.first; 6432 Constant *&NextPHI = NextIterVals[PHI]; 6433 if (!NextPHI) { // Not already computed. 6434 Value *BEValue = PHI->getIncomingValueForBlock(Latch); 6435 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); 6436 } 6437 if (NextPHI != I.second) 6438 StoppedEvolving = false; 6439 } 6440 6441 // If all entries in CurrentIterVals == NextIterVals then we can stop 6442 // iterating, the loop can't continue to change. 6443 if (StoppedEvolving) 6444 return RetVal = CurrentIterVals[PN]; 6445 6446 CurrentIterVals.swap(NextIterVals); 6447 } 6448 } 6449 6450 const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L, 6451 Value *Cond, 6452 bool ExitWhen) { 6453 PHINode *PN = getConstantEvolvingPHI(Cond, L); 6454 if (!PN) return getCouldNotCompute(); 6455 6456 // If the loop is canonicalized, the PHI will have exactly two entries. 6457 // That's the only form we support here. 6458 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute(); 6459 6460 DenseMap<Instruction *, Constant *> CurrentIterVals; 6461 BasicBlock *Header = L->getHeader(); 6462 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!"); 6463 6464 BasicBlock *Latch = L->getLoopLatch(); 6465 assert(Latch && "Should follow from NumIncomingValues == 2!"); 6466 6467 for (auto &I : *Header) { 6468 PHINode *PHI = dyn_cast<PHINode>(&I); 6469 if (!PHI) 6470 break; 6471 auto *StartCST = getOtherIncomingValue(PHI, Latch); 6472 if (!StartCST) continue; 6473 CurrentIterVals[PHI] = StartCST; 6474 } 6475 if (!CurrentIterVals.count(PN)) 6476 return getCouldNotCompute(); 6477 6478 // Okay, we find a PHI node that defines the trip count of this loop. Execute 6479 // the loop symbolically to determine when the condition gets a value of 6480 // "ExitWhen". 6481 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. 6482 const DataLayout &DL = getDataLayout(); 6483 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){ 6484 auto *CondVal = dyn_cast_or_null<ConstantInt>( 6485 EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI)); 6486 6487 // Couldn't symbolically evaluate. 6488 if (!CondVal) return getCouldNotCompute(); 6489 6490 if (CondVal->getValue() == uint64_t(ExitWhen)) { 6491 ++NumBruteForceTripCountsComputed; 6492 return getConstant(Type::getInt32Ty(getContext()), IterationNum); 6493 } 6494 6495 // Update all the PHI nodes for the next iteration. 6496 DenseMap<Instruction *, Constant *> NextIterVals; 6497 6498 // Create a list of which PHIs we need to compute. We want to do this before 6499 // calling EvaluateExpression on them because that may invalidate iterators 6500 // into CurrentIterVals. 6501 SmallVector<PHINode *, 8> PHIsToCompute; 6502 for (const auto &I : CurrentIterVals) { 6503 PHINode *PHI = dyn_cast<PHINode>(I.first); 6504 if (!PHI || PHI->getParent() != Header) continue; 6505 PHIsToCompute.push_back(PHI); 6506 } 6507 for (PHINode *PHI : PHIsToCompute) { 6508 Constant *&NextPHI = NextIterVals[PHI]; 6509 if (NextPHI) continue; // Already computed! 6510 6511 Value *BEValue = PHI->getIncomingValueForBlock(Latch); 6512 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI); 6513 } 6514 CurrentIterVals.swap(NextIterVals); 6515 } 6516 6517 // Too many iterations were needed to evaluate. 6518 return getCouldNotCompute(); 6519 } 6520 6521 /// getSCEVAtScope - Return a SCEV expression for the specified value 6522 /// at the specified scope in the program. The L value specifies a loop 6523 /// nest to evaluate the expression at, where null is the top-level or a 6524 /// specified loop is immediately inside of the loop. 6525 /// 6526 /// This method can be used to compute the exit value for a variable defined 6527 /// in a loop by querying what the value will hold in the parent loop. 6528 /// 6529 /// In the case that a relevant loop exit value cannot be computed, the 6530 /// original value V is returned. 6531 const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { 6532 SmallVector<std::pair<const Loop *, const SCEV *>, 2> &Values = 6533 ValuesAtScopes[V]; 6534 // Check to see if we've folded this expression at this loop before. 6535 for (auto &LS : Values) 6536 if (LS.first == L) 6537 return LS.second ? LS.second : V; 6538 6539 Values.emplace_back(L, nullptr); 6540 6541 // Otherwise compute it. 6542 const SCEV *C = computeSCEVAtScope(V, L); 6543 for (auto &LS : reverse(ValuesAtScopes[V])) 6544 if (LS.first == L) { 6545 LS.second = C; 6546 break; 6547 } 6548 return C; 6549 } 6550 6551 /// This builds up a Constant using the ConstantExpr interface. That way, we 6552 /// will return Constants for objects which aren't represented by a 6553 /// SCEVConstant, because SCEVConstant is restricted to ConstantInt. 6554 /// Returns NULL if the SCEV isn't representable as a Constant. 6555 static Constant *BuildConstantFromSCEV(const SCEV *V) { 6556 switch (static_cast<SCEVTypes>(V->getSCEVType())) { 6557 case scCouldNotCompute: 6558 case scAddRecExpr: 6559 break; 6560 case scConstant: 6561 return cast<SCEVConstant>(V)->getValue(); 6562 case scUnknown: 6563 return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue()); 6564 case scSignExtend: { 6565 const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V); 6566 if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand())) 6567 return ConstantExpr::getSExt(CastOp, SS->getType()); 6568 break; 6569 } 6570 case scZeroExtend: { 6571 const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V); 6572 if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand())) 6573 return ConstantExpr::getZExt(CastOp, SZ->getType()); 6574 break; 6575 } 6576 case scTruncate: { 6577 const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V); 6578 if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand())) 6579 return ConstantExpr::getTrunc(CastOp, ST->getType()); 6580 break; 6581 } 6582 case scAddExpr: { 6583 const SCEVAddExpr *SA = cast<SCEVAddExpr>(V); 6584 if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) { 6585 if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) { 6586 unsigned AS = PTy->getAddressSpace(); 6587 Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS); 6588 C = ConstantExpr::getBitCast(C, DestPtrTy); 6589 } 6590 for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) { 6591 Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i)); 6592 if (!C2) return nullptr; 6593 6594 // First pointer! 6595 if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) { 6596 unsigned AS = C2->getType()->getPointerAddressSpace(); 6597 std::swap(C, C2); 6598 Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS); 6599 // The offsets have been converted to bytes. We can add bytes to an 6600 // i8* by GEP with the byte count in the first index. 6601 C = ConstantExpr::getBitCast(C, DestPtrTy); 6602 } 6603 6604 // Don't bother trying to sum two pointers. We probably can't 6605 // statically compute a load that results from it anyway. 6606 if (C2->getType()->isPointerTy()) 6607 return nullptr; 6608 6609 if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) { 6610 if (PTy->getElementType()->isStructTy()) 6611 C2 = ConstantExpr::getIntegerCast( 6612 C2, Type::getInt32Ty(C->getContext()), true); 6613 C = ConstantExpr::getGetElementPtr(PTy->getElementType(), C, C2); 6614 } else 6615 C = ConstantExpr::getAdd(C, C2); 6616 } 6617 return C; 6618 } 6619 break; 6620 } 6621 case scMulExpr: { 6622 const SCEVMulExpr *SM = cast<SCEVMulExpr>(V); 6623 if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) { 6624 // Don't bother with pointers at all. 6625 if (C->getType()->isPointerTy()) return nullptr; 6626 for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) { 6627 Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i)); 6628 if (!C2 || C2->getType()->isPointerTy()) return nullptr; 6629 C = ConstantExpr::getMul(C, C2); 6630 } 6631 return C; 6632 } 6633 break; 6634 } 6635 case scUDivExpr: { 6636 const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V); 6637 if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS())) 6638 if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS())) 6639 if (LHS->getType() == RHS->getType()) 6640 return ConstantExpr::getUDiv(LHS, RHS); 6641 break; 6642 } 6643 case scSMaxExpr: 6644 case scUMaxExpr: 6645 break; // TODO: smax, umax. 6646 } 6647 return nullptr; 6648 } 6649 6650 const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) { 6651 if (isa<SCEVConstant>(V)) return V; 6652 6653 // If this instruction is evolved from a constant-evolving PHI, compute the 6654 // exit value from the loop without using SCEVs. 6655 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { 6656 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { 6657 const Loop *LI = this->LI[I->getParent()]; 6658 if (LI && LI->getParentLoop() == L) // Looking for loop exit value. 6659 if (PHINode *PN = dyn_cast<PHINode>(I)) 6660 if (PN->getParent() == LI->getHeader()) { 6661 // Okay, there is no closed form solution for the PHI node. Check 6662 // to see if the loop that contains it has a known backedge-taken 6663 // count. If so, we may be able to force computation of the exit 6664 // value. 6665 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI); 6666 if (const SCEVConstant *BTCC = 6667 dyn_cast<SCEVConstant>(BackedgeTakenCount)) { 6668 // Okay, we know how many times the containing loop executes. If 6669 // this is a constant evolving PHI node, get the final value at 6670 // the specified iteration number. 6671 Constant *RV = 6672 getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), LI); 6673 if (RV) return getSCEV(RV); 6674 } 6675 } 6676 6677 // Okay, this is an expression that we cannot symbolically evaluate 6678 // into a SCEV. Check to see if it's possible to symbolically evaluate 6679 // the arguments into constants, and if so, try to constant propagate the 6680 // result. This is particularly useful for computing loop exit values. 6681 if (CanConstantFold(I)) { 6682 SmallVector<Constant *, 4> Operands; 6683 bool MadeImprovement = false; 6684 for (Value *Op : I->operands()) { 6685 if (Constant *C = dyn_cast<Constant>(Op)) { 6686 Operands.push_back(C); 6687 continue; 6688 } 6689 6690 // If any of the operands is non-constant and if they are 6691 // non-integer and non-pointer, don't even try to analyze them 6692 // with scev techniques. 6693 if (!isSCEVable(Op->getType())) 6694 return V; 6695 6696 const SCEV *OrigV = getSCEV(Op); 6697 const SCEV *OpV = getSCEVAtScope(OrigV, L); 6698 MadeImprovement |= OrigV != OpV; 6699 6700 Constant *C = BuildConstantFromSCEV(OpV); 6701 if (!C) return V; 6702 if (C->getType() != Op->getType()) 6703 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 6704 Op->getType(), 6705 false), 6706 C, Op->getType()); 6707 Operands.push_back(C); 6708 } 6709 6710 // Check to see if getSCEVAtScope actually made an improvement. 6711 if (MadeImprovement) { 6712 Constant *C = nullptr; 6713 const DataLayout &DL = getDataLayout(); 6714 if (const CmpInst *CI = dyn_cast<CmpInst>(I)) 6715 C = ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], 6716 Operands[1], DL, &TLI); 6717 else if (const LoadInst *LI = dyn_cast<LoadInst>(I)) { 6718 if (!LI->isVolatile()) 6719 C = ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL); 6720 } else 6721 C = ConstantFoldInstOperands(I, Operands, DL, &TLI); 6722 if (!C) return V; 6723 return getSCEV(C); 6724 } 6725 } 6726 } 6727 6728 // This is some other type of SCEVUnknown, just return it. 6729 return V; 6730 } 6731 6732 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { 6733 // Avoid performing the look-up in the common case where the specified 6734 // expression has no loop-variant portions. 6735 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { 6736 const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); 6737 if (OpAtScope != Comm->getOperand(i)) { 6738 // Okay, at least one of these operands is loop variant but might be 6739 // foldable. Build a new instance of the folded commutative expression. 6740 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), 6741 Comm->op_begin()+i); 6742 NewOps.push_back(OpAtScope); 6743 6744 for (++i; i != e; ++i) { 6745 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); 6746 NewOps.push_back(OpAtScope); 6747 } 6748 if (isa<SCEVAddExpr>(Comm)) 6749 return getAddExpr(NewOps); 6750 if (isa<SCEVMulExpr>(Comm)) 6751 return getMulExpr(NewOps); 6752 if (isa<SCEVSMaxExpr>(Comm)) 6753 return getSMaxExpr(NewOps); 6754 if (isa<SCEVUMaxExpr>(Comm)) 6755 return getUMaxExpr(NewOps); 6756 llvm_unreachable("Unknown commutative SCEV type!"); 6757 } 6758 } 6759 // If we got here, all operands are loop invariant. 6760 return Comm; 6761 } 6762 6763 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { 6764 const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L); 6765 const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L); 6766 if (LHS == Div->getLHS() && RHS == Div->getRHS()) 6767 return Div; // must be loop invariant 6768 return getUDivExpr(LHS, RHS); 6769 } 6770 6771 // If this is a loop recurrence for a loop that does not contain L, then we 6772 // are dealing with the final value computed by the loop. 6773 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { 6774 // First, attempt to evaluate each operand. 6775 // Avoid performing the look-up in the common case where the specified 6776 // expression has no loop-variant portions. 6777 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { 6778 const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L); 6779 if (OpAtScope == AddRec->getOperand(i)) 6780 continue; 6781 6782 // Okay, at least one of these operands is loop variant but might be 6783 // foldable. Build a new instance of the folded commutative expression. 6784 SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(), 6785 AddRec->op_begin()+i); 6786 NewOps.push_back(OpAtScope); 6787 for (++i; i != e; ++i) 6788 NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L)); 6789 6790 const SCEV *FoldedRec = 6791 getAddRecExpr(NewOps, AddRec->getLoop(), 6792 AddRec->getNoWrapFlags(SCEV::FlagNW)); 6793 AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec); 6794 // The addrec may be folded to a nonrecurrence, for example, if the 6795 // induction variable is multiplied by zero after constant folding. Go 6796 // ahead and return the folded value. 6797 if (!AddRec) 6798 return FoldedRec; 6799 break; 6800 } 6801 6802 // If the scope is outside the addrec's loop, evaluate it by using the 6803 // loop exit value of the addrec. 6804 if (!AddRec->getLoop()->contains(L)) { 6805 // To evaluate this recurrence, we need to know how many times the AddRec 6806 // loop iterates. Compute this now. 6807 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); 6808 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; 6809 6810 // Then, evaluate the AddRec. 6811 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); 6812 } 6813 6814 return AddRec; 6815 } 6816 6817 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { 6818 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); 6819 if (Op == Cast->getOperand()) 6820 return Cast; // must be loop invariant 6821 return getZeroExtendExpr(Op, Cast->getType()); 6822 } 6823 6824 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { 6825 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); 6826 if (Op == Cast->getOperand()) 6827 return Cast; // must be loop invariant 6828 return getSignExtendExpr(Op, Cast->getType()); 6829 } 6830 6831 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { 6832 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L); 6833 if (Op == Cast->getOperand()) 6834 return Cast; // must be loop invariant 6835 return getTruncateExpr(Op, Cast->getType()); 6836 } 6837 6838 llvm_unreachable("Unknown SCEV type!"); 6839 } 6840 6841 /// getSCEVAtScope - This is a convenience function which does 6842 /// getSCEVAtScope(getSCEV(V), L). 6843 const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { 6844 return getSCEVAtScope(getSCEV(V), L); 6845 } 6846 6847 /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the 6848 /// following equation: 6849 /// 6850 /// A * X = B (mod N) 6851 /// 6852 /// where N = 2^BW and BW is the common bit width of A and B. The signedness of 6853 /// A and B isn't important. 6854 /// 6855 /// If the equation does not have a solution, SCEVCouldNotCompute is returned. 6856 static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B, 6857 ScalarEvolution &SE) { 6858 uint32_t BW = A.getBitWidth(); 6859 assert(BW == B.getBitWidth() && "Bit widths must be the same."); 6860 assert(A != 0 && "A must be non-zero."); 6861 6862 // 1. D = gcd(A, N) 6863 // 6864 // The gcd of A and N may have only one prime factor: 2. The number of 6865 // trailing zeros in A is its multiplicity 6866 uint32_t Mult2 = A.countTrailingZeros(); 6867 // D = 2^Mult2 6868 6869 // 2. Check if B is divisible by D. 6870 // 6871 // B is divisible by D if and only if the multiplicity of prime factor 2 for B 6872 // is not less than multiplicity of this prime factor for D. 6873 if (B.countTrailingZeros() < Mult2) 6874 return SE.getCouldNotCompute(); 6875 6876 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic 6877 // modulo (N / D). 6878 // 6879 // (N / D) may need BW+1 bits in its representation. Hence, we'll use this 6880 // bit width during computations. 6881 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D 6882 APInt Mod(BW + 1, 0); 6883 Mod.setBit(BW - Mult2); // Mod = N / D 6884 APInt I = AD.multiplicativeInverse(Mod); 6885 6886 // 4. Compute the minimum unsigned root of the equation: 6887 // I * (B / D) mod (N / D) 6888 APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); 6889 6890 // The result is guaranteed to be less than 2^BW so we may truncate it to BW 6891 // bits. 6892 return SE.getConstant(Result.trunc(BW)); 6893 } 6894 6895 /// SolveQuadraticEquation - Find the roots of the quadratic equation for the 6896 /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which 6897 /// might be the same) or two SCEVCouldNotCompute objects. 6898 /// 6899 static std::pair<const SCEV *,const SCEV *> 6900 SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { 6901 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); 6902 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); 6903 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); 6904 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); 6905 6906 // We currently can only solve this if the coefficients are constants. 6907 if (!LC || !MC || !NC) { 6908 const SCEV *CNC = SE.getCouldNotCompute(); 6909 return {CNC, CNC}; 6910 } 6911 6912 uint32_t BitWidth = LC->getAPInt().getBitWidth(); 6913 const APInt &L = LC->getAPInt(); 6914 const APInt &M = MC->getAPInt(); 6915 const APInt &N = NC->getAPInt(); 6916 APInt Two(BitWidth, 2); 6917 APInt Four(BitWidth, 4); 6918 6919 { 6920 using namespace APIntOps; 6921 const APInt& C = L; 6922 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C 6923 // The B coefficient is M-N/2 6924 APInt B(M); 6925 B -= sdiv(N,Two); 6926 6927 // The A coefficient is N/2 6928 APInt A(N.sdiv(Two)); 6929 6930 // Compute the B^2-4ac term. 6931 APInt SqrtTerm(B); 6932 SqrtTerm *= B; 6933 SqrtTerm -= Four * (A * C); 6934 6935 if (SqrtTerm.isNegative()) { 6936 // The loop is provably infinite. 6937 const SCEV *CNC = SE.getCouldNotCompute(); 6938 return {CNC, CNC}; 6939 } 6940 6941 // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest 6942 // integer value or else APInt::sqrt() will assert. 6943 APInt SqrtVal(SqrtTerm.sqrt()); 6944 6945 // Compute the two solutions for the quadratic formula. 6946 // The divisions must be performed as signed divisions. 6947 APInt NegB(-B); 6948 APInt TwoA(A << 1); 6949 if (TwoA.isMinValue()) { 6950 const SCEV *CNC = SE.getCouldNotCompute(); 6951 return {CNC, CNC}; 6952 } 6953 6954 LLVMContext &Context = SE.getContext(); 6955 6956 ConstantInt *Solution1 = 6957 ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA)); 6958 ConstantInt *Solution2 = 6959 ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA)); 6960 6961 return {SE.getConstant(Solution1), SE.getConstant(Solution2)}; 6962 } // end APIntOps namespace 6963 } 6964 6965 /// HowFarToZero - Return the number of times a backedge comparing the specified 6966 /// value to zero will execute. If not computable, return CouldNotCompute. 6967 /// 6968 /// This is only used for loops with a "x != y" exit test. The exit condition is 6969 /// now expressed as a single expression, V = x-y. So the exit test is 6970 /// effectively V != 0. We know and take advantage of the fact that this 6971 /// expression only being used in a comparison by zero context. 6972 ScalarEvolution::ExitLimit 6973 ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L, bool ControlsExit, 6974 bool AllowPredicates) { 6975 SCEVUnionPredicate P; 6976 // If the value is a constant 6977 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { 6978 // If the value is already zero, the branch will execute zero times. 6979 if (C->getValue()->isZero()) return C; 6980 return getCouldNotCompute(); // Otherwise it will loop infinitely. 6981 } 6982 6983 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); 6984 if (!AddRec && AllowPredicates) 6985 // Try to make this an AddRec using runtime tests, in the first X 6986 // iterations of this loop, where X is the SCEV expression found by the 6987 // algorithm below. 6988 AddRec = convertSCEVToAddRecWithPredicates(V, L, P); 6989 6990 if (!AddRec || AddRec->getLoop() != L) 6991 return getCouldNotCompute(); 6992 6993 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of 6994 // the quadratic equation to solve it. 6995 if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) { 6996 std::pair<const SCEV *,const SCEV *> Roots = 6997 SolveQuadraticEquation(AddRec, *this); 6998 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); 6999 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); 7000 if (R1 && R2) { 7001 // Pick the smallest positive root value. 7002 if (ConstantInt *CB = 7003 dyn_cast<ConstantInt>(ConstantExpr::getICmp(CmpInst::ICMP_ULT, 7004 R1->getValue(), 7005 R2->getValue()))) { 7006 if (!CB->getZExtValue()) 7007 std::swap(R1, R2); // R1 is the minimum root now. 7008 7009 // We can only use this value if the chrec ends up with an exact zero 7010 // value at this index. When solving for "X*X != 5", for example, we 7011 // should not accept a root of 2. 7012 const SCEV *Val = AddRec->evaluateAtIteration(R1, *this); 7013 if (Val->isZero()) 7014 return ExitLimit(R1, R1, P); // We found a quadratic root! 7015 } 7016 } 7017 return getCouldNotCompute(); 7018 } 7019 7020 // Otherwise we can only handle this if it is affine. 7021 if (!AddRec->isAffine()) 7022 return getCouldNotCompute(); 7023 7024 // If this is an affine expression, the execution count of this branch is 7025 // the minimum unsigned root of the following equation: 7026 // 7027 // Start + Step*N = 0 (mod 2^BW) 7028 // 7029 // equivalent to: 7030 // 7031 // Step*N = -Start (mod 2^BW) 7032 // 7033 // where BW is the common bit width of Start and Step. 7034 7035 // Get the initial value for the loop. 7036 const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop()); 7037 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop()); 7038 7039 // For now we handle only constant steps. 7040 // 7041 // TODO: Handle a nonconstant Step given AddRec<NUW>. If the 7042 // AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap 7043 // to 0, it must be counting down to equal 0. Consequently, N = Start / -Step. 7044 // We have not yet seen any such cases. 7045 const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step); 7046 if (!StepC || StepC->getValue()->equalsInt(0)) 7047 return getCouldNotCompute(); 7048 7049 // For positive steps (counting up until unsigned overflow): 7050 // N = -Start/Step (as unsigned) 7051 // For negative steps (counting down to zero): 7052 // N = Start/-Step 7053 // First compute the unsigned distance from zero in the direction of Step. 7054 bool CountDown = StepC->getAPInt().isNegative(); 7055 const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start); 7056 7057 // Handle unitary steps, which cannot wraparound. 7058 // 1*N = -Start; -1*N = Start (mod 2^BW), so: 7059 // N = Distance (as unsigned) 7060 if (StepC->getValue()->equalsInt(1) || StepC->getValue()->isAllOnesValue()) { 7061 ConstantRange CR = getUnsignedRange(Start); 7062 const SCEV *MaxBECount; 7063 if (!CountDown && CR.getUnsignedMin().isMinValue()) 7064 // When counting up, the worst starting value is 1, not 0. 7065 MaxBECount = CR.getUnsignedMax().isMinValue() 7066 ? getConstant(APInt::getMinValue(CR.getBitWidth())) 7067 : getConstant(APInt::getMaxValue(CR.getBitWidth())); 7068 else 7069 MaxBECount = getConstant(CountDown ? CR.getUnsignedMax() 7070 : -CR.getUnsignedMin()); 7071 return ExitLimit(Distance, MaxBECount, P); 7072 } 7073 7074 // As a special case, handle the instance where Step is a positive power of 7075 // two. In this case, determining whether Step divides Distance evenly can be 7076 // done by counting and comparing the number of trailing zeros of Step and 7077 // Distance. 7078 if (!CountDown) { 7079 const APInt &StepV = StepC->getAPInt(); 7080 // StepV.isPowerOf2() returns true if StepV is an positive power of two. It 7081 // also returns true if StepV is maximally negative (eg, INT_MIN), but that 7082 // case is not handled as this code is guarded by !CountDown. 7083 if (StepV.isPowerOf2() && 7084 GetMinTrailingZeros(Distance) >= StepV.countTrailingZeros()) { 7085 // Here we've constrained the equation to be of the form 7086 // 7087 // 2^(N + k) * Distance' = (StepV == 2^N) * X (mod 2^W) ... (0) 7088 // 7089 // where we're operating on a W bit wide integer domain and k is 7090 // non-negative. The smallest unsigned solution for X is the trip count. 7091 // 7092 // (0) is equivalent to: 7093 // 7094 // 2^(N + k) * Distance' - 2^N * X = L * 2^W 7095 // <=> 2^N(2^k * Distance' - X) = L * 2^(W - N) * 2^N 7096 // <=> 2^k * Distance' - X = L * 2^(W - N) 7097 // <=> 2^k * Distance' = L * 2^(W - N) + X ... (1) 7098 // 7099 // The smallest X satisfying (1) is unsigned remainder of dividing the LHS 7100 // by 2^(W - N). 7101 // 7102 // <=> X = 2^k * Distance' URem 2^(W - N) ... (2) 7103 // 7104 // E.g. say we're solving 7105 // 7106 // 2 * Val = 2 * X (in i8) ... (3) 7107 // 7108 // then from (2), we get X = Val URem i8 128 (k = 0 in this case). 7109 // 7110 // Note: It is tempting to solve (3) by setting X = Val, but Val is not 7111 // necessarily the smallest unsigned value of X that satisfies (3). 7112 // E.g. if Val is i8 -127 then the smallest value of X that satisfies (3) 7113 // is i8 1, not i8 -127 7114 7115 const auto *ModuloResult = getUDivExactExpr(Distance, Step); 7116 7117 // Since SCEV does not have a URem node, we construct one using a truncate 7118 // and a zero extend. 7119 7120 unsigned NarrowWidth = StepV.getBitWidth() - StepV.countTrailingZeros(); 7121 auto *NarrowTy = IntegerType::get(getContext(), NarrowWidth); 7122 auto *WideTy = Distance->getType(); 7123 7124 const SCEV *Limit = 7125 getZeroExtendExpr(getTruncateExpr(ModuloResult, NarrowTy), WideTy); 7126 return ExitLimit(Limit, Limit, P); 7127 } 7128 } 7129 7130 // If the condition controls loop exit (the loop exits only if the expression 7131 // is true) and the addition is no-wrap we can use unsigned divide to 7132 // compute the backedge count. In this case, the step may not divide the 7133 // distance, but we don't care because if the condition is "missed" the loop 7134 // will have undefined behavior due to wrapping. 7135 if (ControlsExit && AddRec->hasNoSelfWrap()) { 7136 const SCEV *Exact = 7137 getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step); 7138 return ExitLimit(Exact, Exact, P); 7139 } 7140 7141 // Then, try to solve the above equation provided that Start is constant. 7142 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) { 7143 const SCEV *E = SolveLinEquationWithOverflow( 7144 StepC->getValue()->getValue(), -StartC->getValue()->getValue(), *this); 7145 return ExitLimit(E, E, P); 7146 } 7147 return getCouldNotCompute(); 7148 } 7149 7150 /// HowFarToNonZero - Return the number of times a backedge checking the 7151 /// specified value for nonzero will execute. If not computable, return 7152 /// CouldNotCompute 7153 ScalarEvolution::ExitLimit 7154 ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { 7155 // Loops that look like: while (X == 0) are very strange indeed. We don't 7156 // handle them yet except for the trivial case. This could be expanded in the 7157 // future as needed. 7158 7159 // If the value is a constant, check to see if it is known to be non-zero 7160 // already. If so, the backedge will execute zero times. 7161 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { 7162 if (!C->getValue()->isNullValue()) 7163 return getZero(C->getType()); 7164 return getCouldNotCompute(); // Otherwise it will loop infinitely. 7165 } 7166 7167 // We could implement others, but I really doubt anyone writes loops like 7168 // this, and if they did, they would already be constant folded. 7169 return getCouldNotCompute(); 7170 } 7171 7172 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB 7173 /// (which may not be an immediate predecessor) which has exactly one 7174 /// successor from which BB is reachable, or null if no such block is 7175 /// found. 7176 /// 7177 std::pair<BasicBlock *, BasicBlock *> 7178 ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { 7179 // If the block has a unique predecessor, then there is no path from the 7180 // predecessor to the block that does not go through the direct edge 7181 // from the predecessor to the block. 7182 if (BasicBlock *Pred = BB->getSinglePredecessor()) 7183 return {Pred, BB}; 7184 7185 // A loop's header is defined to be a block that dominates the loop. 7186 // If the header has a unique predecessor outside the loop, it must be 7187 // a block that has exactly one successor that can reach the loop. 7188 if (Loop *L = LI.getLoopFor(BB)) 7189 return {L->getLoopPredecessor(), L->getHeader()}; 7190 7191 return {nullptr, nullptr}; 7192 } 7193 7194 /// HasSameValue - SCEV structural equivalence is usually sufficient for 7195 /// testing whether two expressions are equal, however for the purposes of 7196 /// looking for a condition guarding a loop, it can be useful to be a little 7197 /// more general, since a front-end may have replicated the controlling 7198 /// expression. 7199 /// 7200 static bool HasSameValue(const SCEV *A, const SCEV *B) { 7201 // Quick check to see if they are the same SCEV. 7202 if (A == B) return true; 7203 7204 auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) { 7205 // Not all instructions that are "identical" compute the same value. For 7206 // instance, two distinct alloca instructions allocating the same type are 7207 // identical and do not read memory; but compute distinct values. 7208 return A->isIdenticalTo(B) && (isa<BinaryOperator>(A) || isa<GetElementPtrInst>(A)); 7209 }; 7210 7211 // Otherwise, if they're both SCEVUnknown, it's possible that they hold 7212 // two different instructions with the same value. Check for this case. 7213 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) 7214 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) 7215 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) 7216 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) 7217 if (ComputesEqualValues(AI, BI)) 7218 return true; 7219 7220 // Otherwise assume they may have a different value. 7221 return false; 7222 } 7223 7224 /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with 7225 /// predicate Pred. Return true iff any changes were made. 7226 /// 7227 bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred, 7228 const SCEV *&LHS, const SCEV *&RHS, 7229 unsigned Depth) { 7230 bool Changed = false; 7231 7232 // If we hit the max recursion limit bail out. 7233 if (Depth >= 3) 7234 return false; 7235 7236 // Canonicalize a constant to the right side. 7237 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { 7238 // Check for both operands constant. 7239 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { 7240 if (ConstantExpr::getICmp(Pred, 7241 LHSC->getValue(), 7242 RHSC->getValue())->isNullValue()) 7243 goto trivially_false; 7244 else 7245 goto trivially_true; 7246 } 7247 // Otherwise swap the operands to put the constant on the right. 7248 std::swap(LHS, RHS); 7249 Pred = ICmpInst::getSwappedPredicate(Pred); 7250 Changed = true; 7251 } 7252 7253 // If we're comparing an addrec with a value which is loop-invariant in the 7254 // addrec's loop, put the addrec on the left. Also make a dominance check, 7255 // as both operands could be addrecs loop-invariant in each other's loop. 7256 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) { 7257 const Loop *L = AR->getLoop(); 7258 if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) { 7259 std::swap(LHS, RHS); 7260 Pred = ICmpInst::getSwappedPredicate(Pred); 7261 Changed = true; 7262 } 7263 } 7264 7265 // If there's a constant operand, canonicalize comparisons with boundary 7266 // cases, and canonicalize *-or-equal comparisons to regular comparisons. 7267 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) { 7268 const APInt &RA = RC->getAPInt(); 7269 switch (Pred) { 7270 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); 7271 case ICmpInst::ICMP_EQ: 7272 case ICmpInst::ICMP_NE: 7273 // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b. 7274 if (!RA) 7275 if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS)) 7276 if (const SCEVMulExpr *ME = dyn_cast<SCEVMulExpr>(AE->getOperand(0))) 7277 if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 && 7278 ME->getOperand(0)->isAllOnesValue()) { 7279 RHS = AE->getOperand(1); 7280 LHS = ME->getOperand(1); 7281 Changed = true; 7282 } 7283 break; 7284 case ICmpInst::ICMP_UGE: 7285 if ((RA - 1).isMinValue()) { 7286 Pred = ICmpInst::ICMP_NE; 7287 RHS = getConstant(RA - 1); 7288 Changed = true; 7289 break; 7290 } 7291 if (RA.isMaxValue()) { 7292 Pred = ICmpInst::ICMP_EQ; 7293 Changed = true; 7294 break; 7295 } 7296 if (RA.isMinValue()) goto trivially_true; 7297 7298 Pred = ICmpInst::ICMP_UGT; 7299 RHS = getConstant(RA - 1); 7300 Changed = true; 7301 break; 7302 case ICmpInst::ICMP_ULE: 7303 if ((RA + 1).isMaxValue()) { 7304 Pred = ICmpInst::ICMP_NE; 7305 RHS = getConstant(RA + 1); 7306 Changed = true; 7307 break; 7308 } 7309 if (RA.isMinValue()) { 7310 Pred = ICmpInst::ICMP_EQ; 7311 Changed = true; 7312 break; 7313 } 7314 if (RA.isMaxValue()) goto trivially_true; 7315 7316 Pred = ICmpInst::ICMP_ULT; 7317 RHS = getConstant(RA + 1); 7318 Changed = true; 7319 break; 7320 case ICmpInst::ICMP_SGE: 7321 if ((RA - 1).isMinSignedValue()) { 7322 Pred = ICmpInst::ICMP_NE; 7323 RHS = getConstant(RA - 1); 7324 Changed = true; 7325 break; 7326 } 7327 if (RA.isMaxSignedValue()) { 7328 Pred = ICmpInst::ICMP_EQ; 7329 Changed = true; 7330 break; 7331 } 7332 if (RA.isMinSignedValue()) goto trivially_true; 7333 7334 Pred = ICmpInst::ICMP_SGT; 7335 RHS = getConstant(RA - 1); 7336 Changed = true; 7337 break; 7338 case ICmpInst::ICMP_SLE: 7339 if ((RA + 1).isMaxSignedValue()) { 7340 Pred = ICmpInst::ICMP_NE; 7341 RHS = getConstant(RA + 1); 7342 Changed = true; 7343 break; 7344 } 7345 if (RA.isMinSignedValue()) { 7346 Pred = ICmpInst::ICMP_EQ; 7347 Changed = true; 7348 break; 7349 } 7350 if (RA.isMaxSignedValue()) goto trivially_true; 7351 7352 Pred = ICmpInst::ICMP_SLT; 7353 RHS = getConstant(RA + 1); 7354 Changed = true; 7355 break; 7356 case ICmpInst::ICMP_UGT: 7357 if (RA.isMinValue()) { 7358 Pred = ICmpInst::ICMP_NE; 7359 Changed = true; 7360 break; 7361 } 7362 if ((RA + 1).isMaxValue()) { 7363 Pred = ICmpInst::ICMP_EQ; 7364 RHS = getConstant(RA + 1); 7365 Changed = true; 7366 break; 7367 } 7368 if (RA.isMaxValue()) goto trivially_false; 7369 break; 7370 case ICmpInst::ICMP_ULT: 7371 if (RA.isMaxValue()) { 7372 Pred = ICmpInst::ICMP_NE; 7373 Changed = true; 7374 break; 7375 } 7376 if ((RA - 1).isMinValue()) { 7377 Pred = ICmpInst::ICMP_EQ; 7378 RHS = getConstant(RA - 1); 7379 Changed = true; 7380 break; 7381 } 7382 if (RA.isMinValue()) goto trivially_false; 7383 break; 7384 case ICmpInst::ICMP_SGT: 7385 if (RA.isMinSignedValue()) { 7386 Pred = ICmpInst::ICMP_NE; 7387 Changed = true; 7388 break; 7389 } 7390 if ((RA + 1).isMaxSignedValue()) { 7391 Pred = ICmpInst::ICMP_EQ; 7392 RHS = getConstant(RA + 1); 7393 Changed = true; 7394 break; 7395 } 7396 if (RA.isMaxSignedValue()) goto trivially_false; 7397 break; 7398 case ICmpInst::ICMP_SLT: 7399 if (RA.isMaxSignedValue()) { 7400 Pred = ICmpInst::ICMP_NE; 7401 Changed = true; 7402 break; 7403 } 7404 if ((RA - 1).isMinSignedValue()) { 7405 Pred = ICmpInst::ICMP_EQ; 7406 RHS = getConstant(RA - 1); 7407 Changed = true; 7408 break; 7409 } 7410 if (RA.isMinSignedValue()) goto trivially_false; 7411 break; 7412 } 7413 } 7414 7415 // Check for obvious equality. 7416 if (HasSameValue(LHS, RHS)) { 7417 if (ICmpInst::isTrueWhenEqual(Pred)) 7418 goto trivially_true; 7419 if (ICmpInst::isFalseWhenEqual(Pred)) 7420 goto trivially_false; 7421 } 7422 7423 // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by 7424 // adding or subtracting 1 from one of the operands. 7425 switch (Pred) { 7426 case ICmpInst::ICMP_SLE: 7427 if (!getSignedRange(RHS).getSignedMax().isMaxSignedValue()) { 7428 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, 7429 SCEV::FlagNSW); 7430 Pred = ICmpInst::ICMP_SLT; 7431 Changed = true; 7432 } else if (!getSignedRange(LHS).getSignedMin().isMinSignedValue()) { 7433 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS, 7434 SCEV::FlagNSW); 7435 Pred = ICmpInst::ICMP_SLT; 7436 Changed = true; 7437 } 7438 break; 7439 case ICmpInst::ICMP_SGE: 7440 if (!getSignedRange(RHS).getSignedMin().isMinSignedValue()) { 7441 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS, 7442 SCEV::FlagNSW); 7443 Pred = ICmpInst::ICMP_SGT; 7444 Changed = true; 7445 } else if (!getSignedRange(LHS).getSignedMax().isMaxSignedValue()) { 7446 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, 7447 SCEV::FlagNSW); 7448 Pred = ICmpInst::ICMP_SGT; 7449 Changed = true; 7450 } 7451 break; 7452 case ICmpInst::ICMP_ULE: 7453 if (!getUnsignedRange(RHS).getUnsignedMax().isMaxValue()) { 7454 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS, 7455 SCEV::FlagNUW); 7456 Pred = ICmpInst::ICMP_ULT; 7457 Changed = true; 7458 } else if (!getUnsignedRange(LHS).getUnsignedMin().isMinValue()) { 7459 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS); 7460 Pred = ICmpInst::ICMP_ULT; 7461 Changed = true; 7462 } 7463 break; 7464 case ICmpInst::ICMP_UGE: 7465 if (!getUnsignedRange(RHS).getUnsignedMin().isMinValue()) { 7466 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS); 7467 Pred = ICmpInst::ICMP_UGT; 7468 Changed = true; 7469 } else if (!getUnsignedRange(LHS).getUnsignedMax().isMaxValue()) { 7470 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS, 7471 SCEV::FlagNUW); 7472 Pred = ICmpInst::ICMP_UGT; 7473 Changed = true; 7474 } 7475 break; 7476 default: 7477 break; 7478 } 7479 7480 // TODO: More simplifications are possible here. 7481 7482 // Recursively simplify until we either hit a recursion limit or nothing 7483 // changes. 7484 if (Changed) 7485 return SimplifyICmpOperands(Pred, LHS, RHS, Depth+1); 7486 7487 return Changed; 7488 7489 trivially_true: 7490 // Return 0 == 0. 7491 LHS = RHS = getConstant(ConstantInt::getFalse(getContext())); 7492 Pred = ICmpInst::ICMP_EQ; 7493 return true; 7494 7495 trivially_false: 7496 // Return 0 != 0. 7497 LHS = RHS = getConstant(ConstantInt::getFalse(getContext())); 7498 Pred = ICmpInst::ICMP_NE; 7499 return true; 7500 } 7501 7502 bool ScalarEvolution::isKnownNegative(const SCEV *S) { 7503 return getSignedRange(S).getSignedMax().isNegative(); 7504 } 7505 7506 bool ScalarEvolution::isKnownPositive(const SCEV *S) { 7507 return getSignedRange(S).getSignedMin().isStrictlyPositive(); 7508 } 7509 7510 bool ScalarEvolution::isKnownNonNegative(const SCEV *S) { 7511 return !getSignedRange(S).getSignedMin().isNegative(); 7512 } 7513 7514 bool ScalarEvolution::isKnownNonPositive(const SCEV *S) { 7515 return !getSignedRange(S).getSignedMax().isStrictlyPositive(); 7516 } 7517 7518 bool ScalarEvolution::isKnownNonZero(const SCEV *S) { 7519 return isKnownNegative(S) || isKnownPositive(S); 7520 } 7521 7522 bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred, 7523 const SCEV *LHS, const SCEV *RHS) { 7524 // Canonicalize the inputs first. 7525 (void)SimplifyICmpOperands(Pred, LHS, RHS); 7526 7527 // If LHS or RHS is an addrec, check to see if the condition is true in 7528 // every iteration of the loop. 7529 // If LHS and RHS are both addrec, both conditions must be true in 7530 // every iteration of the loop. 7531 const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS); 7532 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS); 7533 bool LeftGuarded = false; 7534 bool RightGuarded = false; 7535 if (LAR) { 7536 const Loop *L = LAR->getLoop(); 7537 if (isLoopEntryGuardedByCond(L, Pred, LAR->getStart(), RHS) && 7538 isLoopBackedgeGuardedByCond(L, Pred, LAR->getPostIncExpr(*this), RHS)) { 7539 if (!RAR) return true; 7540 LeftGuarded = true; 7541 } 7542 } 7543 if (RAR) { 7544 const Loop *L = RAR->getLoop(); 7545 if (isLoopEntryGuardedByCond(L, Pred, LHS, RAR->getStart()) && 7546 isLoopBackedgeGuardedByCond(L, Pred, LHS, RAR->getPostIncExpr(*this))) { 7547 if (!LAR) return true; 7548 RightGuarded = true; 7549 } 7550 } 7551 if (LeftGuarded && RightGuarded) 7552 return true; 7553 7554 if (isKnownPredicateViaSplitting(Pred, LHS, RHS)) 7555 return true; 7556 7557 // Otherwise see what can be done with known constant ranges. 7558 return isKnownPredicateViaConstantRanges(Pred, LHS, RHS); 7559 } 7560 7561 bool ScalarEvolution::isMonotonicPredicate(const SCEVAddRecExpr *LHS, 7562 ICmpInst::Predicate Pred, 7563 bool &Increasing) { 7564 bool Result = isMonotonicPredicateImpl(LHS, Pred, Increasing); 7565 7566 #ifndef NDEBUG 7567 // Verify an invariant: inverting the predicate should turn a monotonically 7568 // increasing change to a monotonically decreasing one, and vice versa. 7569 bool IncreasingSwapped; 7570 bool ResultSwapped = isMonotonicPredicateImpl( 7571 LHS, ICmpInst::getSwappedPredicate(Pred), IncreasingSwapped); 7572 7573 assert(Result == ResultSwapped && "should be able to analyze both!"); 7574 if (ResultSwapped) 7575 assert(Increasing == !IncreasingSwapped && 7576 "monotonicity should flip as we flip the predicate"); 7577 #endif 7578 7579 return Result; 7580 } 7581 7582 bool ScalarEvolution::isMonotonicPredicateImpl(const SCEVAddRecExpr *LHS, 7583 ICmpInst::Predicate Pred, 7584 bool &Increasing) { 7585 7586 // A zero step value for LHS means the induction variable is essentially a 7587 // loop invariant value. We don't really depend on the predicate actually 7588 // flipping from false to true (for increasing predicates, and the other way 7589 // around for decreasing predicates), all we care about is that *if* the 7590 // predicate changes then it only changes from false to true. 7591 // 7592 // A zero step value in itself is not very useful, but there may be places 7593 // where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be 7594 // as general as possible. 7595 7596 switch (Pred) { 7597 default: 7598 return false; // Conservative answer 7599 7600 case ICmpInst::ICMP_UGT: 7601 case ICmpInst::ICMP_UGE: 7602 case ICmpInst::ICMP_ULT: 7603 case ICmpInst::ICMP_ULE: 7604 if (!LHS->hasNoUnsignedWrap()) 7605 return false; 7606 7607 Increasing = Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE; 7608 return true; 7609 7610 case ICmpInst::ICMP_SGT: 7611 case ICmpInst::ICMP_SGE: 7612 case ICmpInst::ICMP_SLT: 7613 case ICmpInst::ICMP_SLE: { 7614 if (!LHS->hasNoSignedWrap()) 7615 return false; 7616 7617 const SCEV *Step = LHS->getStepRecurrence(*this); 7618 7619 if (isKnownNonNegative(Step)) { 7620 Increasing = Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE; 7621 return true; 7622 } 7623 7624 if (isKnownNonPositive(Step)) { 7625 Increasing = Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE; 7626 return true; 7627 } 7628 7629 return false; 7630 } 7631 7632 } 7633 7634 llvm_unreachable("switch has default clause!"); 7635 } 7636 7637 bool ScalarEvolution::isLoopInvariantPredicate( 7638 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, 7639 ICmpInst::Predicate &InvariantPred, const SCEV *&InvariantLHS, 7640 const SCEV *&InvariantRHS) { 7641 7642 // If there is a loop-invariant, force it into the RHS, otherwise bail out. 7643 if (!isLoopInvariant(RHS, L)) { 7644 if (!isLoopInvariant(LHS, L)) 7645 return false; 7646 7647 std::swap(LHS, RHS); 7648 Pred = ICmpInst::getSwappedPredicate(Pred); 7649 } 7650 7651 const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS); 7652 if (!ArLHS || ArLHS->getLoop() != L) 7653 return false; 7654 7655 bool Increasing; 7656 if (!isMonotonicPredicate(ArLHS, Pred, Increasing)) 7657 return false; 7658 7659 // If the predicate "ArLHS `Pred` RHS" monotonically increases from false to 7660 // true as the loop iterates, and the backedge is control dependent on 7661 // "ArLHS `Pred` RHS" == true then we can reason as follows: 7662 // 7663 // * if the predicate was false in the first iteration then the predicate 7664 // is never evaluated again, since the loop exits without taking the 7665 // backedge. 7666 // * if the predicate was true in the first iteration then it will 7667 // continue to be true for all future iterations since it is 7668 // monotonically increasing. 7669 // 7670 // For both the above possibilities, we can replace the loop varying 7671 // predicate with its value on the first iteration of the loop (which is 7672 // loop invariant). 7673 // 7674 // A similar reasoning applies for a monotonically decreasing predicate, by 7675 // replacing true with false and false with true in the above two bullets. 7676 7677 auto P = Increasing ? Pred : ICmpInst::getInversePredicate(Pred); 7678 7679 if (!isLoopBackedgeGuardedByCond(L, P, LHS, RHS)) 7680 return false; 7681 7682 InvariantPred = Pred; 7683 InvariantLHS = ArLHS->getStart(); 7684 InvariantRHS = RHS; 7685 return true; 7686 } 7687 7688 bool ScalarEvolution::isKnownPredicateViaConstantRanges( 7689 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) { 7690 if (HasSameValue(LHS, RHS)) 7691 return ICmpInst::isTrueWhenEqual(Pred); 7692 7693 // This code is split out from isKnownPredicate because it is called from 7694 // within isLoopEntryGuardedByCond. 7695 7696 auto CheckRanges = 7697 [&](const ConstantRange &RangeLHS, const ConstantRange &RangeRHS) { 7698 return ConstantRange::makeSatisfyingICmpRegion(Pred, RangeRHS) 7699 .contains(RangeLHS); 7700 }; 7701 7702 // The check at the top of the function catches the case where the values are 7703 // known to be equal. 7704 if (Pred == CmpInst::ICMP_EQ) 7705 return false; 7706 7707 if (Pred == CmpInst::ICMP_NE) 7708 return CheckRanges(getSignedRange(LHS), getSignedRange(RHS)) || 7709 CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS)) || 7710 isKnownNonZero(getMinusSCEV(LHS, RHS)); 7711 7712 if (CmpInst::isSigned(Pred)) 7713 return CheckRanges(getSignedRange(LHS), getSignedRange(RHS)); 7714 7715 return CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS)); 7716 } 7717 7718 bool ScalarEvolution::isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred, 7719 const SCEV *LHS, 7720 const SCEV *RHS) { 7721 7722 // Match Result to (X + Y)<ExpectedFlags> where Y is a constant integer. 7723 // Return Y via OutY. 7724 auto MatchBinaryAddToConst = 7725 [this](const SCEV *Result, const SCEV *X, APInt &OutY, 7726 SCEV::NoWrapFlags ExpectedFlags) { 7727 const SCEV *NonConstOp, *ConstOp; 7728 SCEV::NoWrapFlags FlagsPresent; 7729 7730 if (!splitBinaryAdd(Result, ConstOp, NonConstOp, FlagsPresent) || 7731 !isa<SCEVConstant>(ConstOp) || NonConstOp != X) 7732 return false; 7733 7734 OutY = cast<SCEVConstant>(ConstOp)->getAPInt(); 7735 return (FlagsPresent & ExpectedFlags) == ExpectedFlags; 7736 }; 7737 7738 APInt C; 7739 7740 switch (Pred) { 7741 default: 7742 break; 7743 7744 case ICmpInst::ICMP_SGE: 7745 std::swap(LHS, RHS); 7746 case ICmpInst::ICMP_SLE: 7747 // X s<= (X + C)<nsw> if C >= 0 7748 if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) && C.isNonNegative()) 7749 return true; 7750 7751 // (X + C)<nsw> s<= X if C <= 0 7752 if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) && 7753 !C.isStrictlyPositive()) 7754 return true; 7755 break; 7756 7757 case ICmpInst::ICMP_SGT: 7758 std::swap(LHS, RHS); 7759 case ICmpInst::ICMP_SLT: 7760 // X s< (X + C)<nsw> if C > 0 7761 if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) && 7762 C.isStrictlyPositive()) 7763 return true; 7764 7765 // (X + C)<nsw> s< X if C < 0 7766 if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) && C.isNegative()) 7767 return true; 7768 break; 7769 } 7770 7771 return false; 7772 } 7773 7774 bool ScalarEvolution::isKnownPredicateViaSplitting(ICmpInst::Predicate Pred, 7775 const SCEV *LHS, 7776 const SCEV *RHS) { 7777 if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate) 7778 return false; 7779 7780 // Allowing arbitrary number of activations of isKnownPredicateViaSplitting on 7781 // the stack can result in exponential time complexity. 7782 SaveAndRestore<bool> Restore(ProvingSplitPredicate, true); 7783 7784 // If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L 7785 // 7786 // To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use 7787 // isKnownPredicate. isKnownPredicate is more powerful, but also more 7788 // expensive; and using isKnownNonNegative(RHS) is sufficient for most of the 7789 // interesting cases seen in practice. We can consider "upgrading" L >= 0 to 7790 // use isKnownPredicate later if needed. 7791 return isKnownNonNegative(RHS) && 7792 isKnownPredicate(CmpInst::ICMP_SGE, LHS, getZero(LHS->getType())) && 7793 isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS); 7794 } 7795 7796 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is 7797 /// protected by a conditional between LHS and RHS. This is used to 7798 /// to eliminate casts. 7799 bool 7800 ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L, 7801 ICmpInst::Predicate Pred, 7802 const SCEV *LHS, const SCEV *RHS) { 7803 // Interpret a null as meaning no loop, where there is obviously no guard 7804 // (interprocedural conditions notwithstanding). 7805 if (!L) return true; 7806 7807 if (isKnownPredicateViaConstantRanges(Pred, LHS, RHS)) 7808 return true; 7809 7810 BasicBlock *Latch = L->getLoopLatch(); 7811 if (!Latch) 7812 return false; 7813 7814 BranchInst *LoopContinuePredicate = 7815 dyn_cast<BranchInst>(Latch->getTerminator()); 7816 if (LoopContinuePredicate && LoopContinuePredicate->isConditional() && 7817 isImpliedCond(Pred, LHS, RHS, 7818 LoopContinuePredicate->getCondition(), 7819 LoopContinuePredicate->getSuccessor(0) != L->getHeader())) 7820 return true; 7821 7822 // We don't want more than one activation of the following loops on the stack 7823 // -- that can lead to O(n!) time complexity. 7824 if (WalkingBEDominatingConds) 7825 return false; 7826 7827 SaveAndRestore<bool> ClearOnExit(WalkingBEDominatingConds, true); 7828 7829 // See if we can exploit a trip count to prove the predicate. 7830 const auto &BETakenInfo = getBackedgeTakenInfo(L); 7831 const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this); 7832 if (LatchBECount != getCouldNotCompute()) { 7833 // We know that Latch branches back to the loop header exactly 7834 // LatchBECount times. This means the backdege condition at Latch is 7835 // equivalent to "{0,+,1} u< LatchBECount". 7836 Type *Ty = LatchBECount->getType(); 7837 auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW); 7838 const SCEV *LoopCounter = 7839 getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags); 7840 if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter, 7841 LatchBECount)) 7842 return true; 7843 } 7844 7845 // Check conditions due to any @llvm.assume intrinsics. 7846 for (auto &AssumeVH : AC.assumptions()) { 7847 if (!AssumeVH) 7848 continue; 7849 auto *CI = cast<CallInst>(AssumeVH); 7850 if (!DT.dominates(CI, Latch->getTerminator())) 7851 continue; 7852 7853 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false)) 7854 return true; 7855 } 7856 7857 // If the loop is not reachable from the entry block, we risk running into an 7858 // infinite loop as we walk up into the dom tree. These loops do not matter 7859 // anyway, so we just return a conservative answer when we see them. 7860 if (!DT.isReachableFromEntry(L->getHeader())) 7861 return false; 7862 7863 for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()]; 7864 DTN != HeaderDTN; DTN = DTN->getIDom()) { 7865 7866 assert(DTN && "should reach the loop header before reaching the root!"); 7867 7868 BasicBlock *BB = DTN->getBlock(); 7869 BasicBlock *PBB = BB->getSinglePredecessor(); 7870 if (!PBB) 7871 continue; 7872 7873 BranchInst *ContinuePredicate = dyn_cast<BranchInst>(PBB->getTerminator()); 7874 if (!ContinuePredicate || !ContinuePredicate->isConditional()) 7875 continue; 7876 7877 Value *Condition = ContinuePredicate->getCondition(); 7878 7879 // If we have an edge `E` within the loop body that dominates the only 7880 // latch, the condition guarding `E` also guards the backedge. This 7881 // reasoning works only for loops with a single latch. 7882 7883 BasicBlockEdge DominatingEdge(PBB, BB); 7884 if (DominatingEdge.isSingleEdge()) { 7885 // We're constructively (and conservatively) enumerating edges within the 7886 // loop body that dominate the latch. The dominator tree better agree 7887 // with us on this: 7888 assert(DT.dominates(DominatingEdge, Latch) && "should be!"); 7889 7890 if (isImpliedCond(Pred, LHS, RHS, Condition, 7891 BB != ContinuePredicate->getSuccessor(0))) 7892 return true; 7893 } 7894 } 7895 7896 return false; 7897 } 7898 7899 /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected 7900 /// by a conditional between LHS and RHS. This is used to help avoid max 7901 /// expressions in loop trip counts, and to eliminate casts. 7902 bool 7903 ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L, 7904 ICmpInst::Predicate Pred, 7905 const SCEV *LHS, const SCEV *RHS) { 7906 // Interpret a null as meaning no loop, where there is obviously no guard 7907 // (interprocedural conditions notwithstanding). 7908 if (!L) return false; 7909 7910 if (isKnownPredicateViaConstantRanges(Pred, LHS, RHS)) 7911 return true; 7912 7913 // Starting at the loop predecessor, climb up the predecessor chain, as long 7914 // as there are predecessors that can be found that have unique successors 7915 // leading to the original header. 7916 for (std::pair<BasicBlock *, BasicBlock *> 7917 Pair(L->getLoopPredecessor(), L->getHeader()); 7918 Pair.first; 7919 Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) { 7920 7921 BranchInst *LoopEntryPredicate = 7922 dyn_cast<BranchInst>(Pair.first->getTerminator()); 7923 if (!LoopEntryPredicate || 7924 LoopEntryPredicate->isUnconditional()) 7925 continue; 7926 7927 if (isImpliedCond(Pred, LHS, RHS, 7928 LoopEntryPredicate->getCondition(), 7929 LoopEntryPredicate->getSuccessor(0) != Pair.second)) 7930 return true; 7931 } 7932 7933 // Check conditions due to any @llvm.assume intrinsics. 7934 for (auto &AssumeVH : AC.assumptions()) { 7935 if (!AssumeVH) 7936 continue; 7937 auto *CI = cast<CallInst>(AssumeVH); 7938 if (!DT.dominates(CI, L->getHeader())) 7939 continue; 7940 7941 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false)) 7942 return true; 7943 } 7944 7945 return false; 7946 } 7947 7948 namespace { 7949 /// RAII wrapper to prevent recursive application of isImpliedCond. 7950 /// ScalarEvolution's PendingLoopPredicates set must be empty unless we are 7951 /// currently evaluating isImpliedCond. 7952 struct MarkPendingLoopPredicate { 7953 Value *Cond; 7954 DenseSet<Value*> &LoopPreds; 7955 bool Pending; 7956 7957 MarkPendingLoopPredicate(Value *C, DenseSet<Value*> &LP) 7958 : Cond(C), LoopPreds(LP) { 7959 Pending = !LoopPreds.insert(Cond).second; 7960 } 7961 ~MarkPendingLoopPredicate() { 7962 if (!Pending) 7963 LoopPreds.erase(Cond); 7964 } 7965 }; 7966 } // end anonymous namespace 7967 7968 /// isImpliedCond - Test whether the condition described by Pred, LHS, 7969 /// and RHS is true whenever the given Cond value evaluates to true. 7970 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, 7971 const SCEV *LHS, const SCEV *RHS, 7972 Value *FoundCondValue, 7973 bool Inverse) { 7974 MarkPendingLoopPredicate Mark(FoundCondValue, PendingLoopPredicates); 7975 if (Mark.Pending) 7976 return false; 7977 7978 // Recursively handle And and Or conditions. 7979 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) { 7980 if (BO->getOpcode() == Instruction::And) { 7981 if (!Inverse) 7982 return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) || 7983 isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse); 7984 } else if (BO->getOpcode() == Instruction::Or) { 7985 if (Inverse) 7986 return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) || 7987 isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse); 7988 } 7989 } 7990 7991 ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue); 7992 if (!ICI) return false; 7993 7994 // Now that we found a conditional branch that dominates the loop or controls 7995 // the loop latch. Check to see if it is the comparison we are looking for. 7996 ICmpInst::Predicate FoundPred; 7997 if (Inverse) 7998 FoundPred = ICI->getInversePredicate(); 7999 else 8000 FoundPred = ICI->getPredicate(); 8001 8002 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0)); 8003 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1)); 8004 8005 return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS); 8006 } 8007 8008 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS, 8009 const SCEV *RHS, 8010 ICmpInst::Predicate FoundPred, 8011 const SCEV *FoundLHS, 8012 const SCEV *FoundRHS) { 8013 // Balance the types. 8014 if (getTypeSizeInBits(LHS->getType()) < 8015 getTypeSizeInBits(FoundLHS->getType())) { 8016 if (CmpInst::isSigned(Pred)) { 8017 LHS = getSignExtendExpr(LHS, FoundLHS->getType()); 8018 RHS = getSignExtendExpr(RHS, FoundLHS->getType()); 8019 } else { 8020 LHS = getZeroExtendExpr(LHS, FoundLHS->getType()); 8021 RHS = getZeroExtendExpr(RHS, FoundLHS->getType()); 8022 } 8023 } else if (getTypeSizeInBits(LHS->getType()) > 8024 getTypeSizeInBits(FoundLHS->getType())) { 8025 if (CmpInst::isSigned(FoundPred)) { 8026 FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType()); 8027 FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType()); 8028 } else { 8029 FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType()); 8030 FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType()); 8031 } 8032 } 8033 8034 // Canonicalize the query to match the way instcombine will have 8035 // canonicalized the comparison. 8036 if (SimplifyICmpOperands(Pred, LHS, RHS)) 8037 if (LHS == RHS) 8038 return CmpInst::isTrueWhenEqual(Pred); 8039 if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS)) 8040 if (FoundLHS == FoundRHS) 8041 return CmpInst::isFalseWhenEqual(FoundPred); 8042 8043 // Check to see if we can make the LHS or RHS match. 8044 if (LHS == FoundRHS || RHS == FoundLHS) { 8045 if (isa<SCEVConstant>(RHS)) { 8046 std::swap(FoundLHS, FoundRHS); 8047 FoundPred = ICmpInst::getSwappedPredicate(FoundPred); 8048 } else { 8049 std::swap(LHS, RHS); 8050 Pred = ICmpInst::getSwappedPredicate(Pred); 8051 } 8052 } 8053 8054 // Check whether the found predicate is the same as the desired predicate. 8055 if (FoundPred == Pred) 8056 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); 8057 8058 // Check whether swapping the found predicate makes it the same as the 8059 // desired predicate. 8060 if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) { 8061 if (isa<SCEVConstant>(RHS)) 8062 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS); 8063 else 8064 return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred), 8065 RHS, LHS, FoundLHS, FoundRHS); 8066 } 8067 8068 // Unsigned comparison is the same as signed comparison when both the operands 8069 // are non-negative. 8070 if (CmpInst::isUnsigned(FoundPred) && 8071 CmpInst::getSignedPredicate(FoundPred) == Pred && 8072 isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) 8073 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS); 8074 8075 // Check if we can make progress by sharpening ranges. 8076 if (FoundPred == ICmpInst::ICMP_NE && 8077 (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) { 8078 8079 const SCEVConstant *C = nullptr; 8080 const SCEV *V = nullptr; 8081 8082 if (isa<SCEVConstant>(FoundLHS)) { 8083 C = cast<SCEVConstant>(FoundLHS); 8084 V = FoundRHS; 8085 } else { 8086 C = cast<SCEVConstant>(FoundRHS); 8087 V = FoundLHS; 8088 } 8089 8090 // The guarding predicate tells us that C != V. If the known range 8091 // of V is [C, t), we can sharpen the range to [C + 1, t). The 8092 // range we consider has to correspond to same signedness as the 8093 // predicate we're interested in folding. 8094 8095 APInt Min = ICmpInst::isSigned(Pred) ? 8096 getSignedRange(V).getSignedMin() : getUnsignedRange(V).getUnsignedMin(); 8097 8098 if (Min == C->getAPInt()) { 8099 // Given (V >= Min && V != Min) we conclude V >= (Min + 1). 8100 // This is true even if (Min + 1) wraps around -- in case of 8101 // wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)). 8102 8103 APInt SharperMin = Min + 1; 8104 8105 switch (Pred) { 8106 case ICmpInst::ICMP_SGE: 8107 case ICmpInst::ICMP_UGE: 8108 // We know V `Pred` SharperMin. If this implies LHS `Pred` 8109 // RHS, we're done. 8110 if (isImpliedCondOperands(Pred, LHS, RHS, V, 8111 getConstant(SharperMin))) 8112 return true; 8113 8114 case ICmpInst::ICMP_SGT: 8115 case ICmpInst::ICMP_UGT: 8116 // We know from the range information that (V `Pred` Min || 8117 // V == Min). We know from the guarding condition that !(V 8118 // == Min). This gives us 8119 // 8120 // V `Pred` Min || V == Min && !(V == Min) 8121 // => V `Pred` Min 8122 // 8123 // If V `Pred` Min implies LHS `Pred` RHS, we're done. 8124 8125 if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min))) 8126 return true; 8127 8128 default: 8129 // No change 8130 break; 8131 } 8132 } 8133 } 8134 8135 // Check whether the actual condition is beyond sufficient. 8136 if (FoundPred == ICmpInst::ICMP_EQ) 8137 if (ICmpInst::isTrueWhenEqual(Pred)) 8138 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS)) 8139 return true; 8140 if (Pred == ICmpInst::ICMP_NE) 8141 if (!ICmpInst::isTrueWhenEqual(FoundPred)) 8142 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS)) 8143 return true; 8144 8145 // Otherwise assume the worst. 8146 return false; 8147 } 8148 8149 bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr, 8150 const SCEV *&L, const SCEV *&R, 8151 SCEV::NoWrapFlags &Flags) { 8152 const auto *AE = dyn_cast<SCEVAddExpr>(Expr); 8153 if (!AE || AE->getNumOperands() != 2) 8154 return false; 8155 8156 L = AE->getOperand(0); 8157 R = AE->getOperand(1); 8158 Flags = AE->getNoWrapFlags(); 8159 return true; 8160 } 8161 8162 bool ScalarEvolution::computeConstantDifference(const SCEV *Less, 8163 const SCEV *More, 8164 APInt &C) { 8165 // We avoid subtracting expressions here because this function is usually 8166 // fairly deep in the call stack (i.e. is called many times). 8167 8168 if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) { 8169 const auto *LAR = cast<SCEVAddRecExpr>(Less); 8170 const auto *MAR = cast<SCEVAddRecExpr>(More); 8171 8172 if (LAR->getLoop() != MAR->getLoop()) 8173 return false; 8174 8175 // We look at affine expressions only; not for correctness but to keep 8176 // getStepRecurrence cheap. 8177 if (!LAR->isAffine() || !MAR->isAffine()) 8178 return false; 8179 8180 if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this)) 8181 return false; 8182 8183 Less = LAR->getStart(); 8184 More = MAR->getStart(); 8185 8186 // fall through 8187 } 8188 8189 if (isa<SCEVConstant>(Less) && isa<SCEVConstant>(More)) { 8190 const auto &M = cast<SCEVConstant>(More)->getAPInt(); 8191 const auto &L = cast<SCEVConstant>(Less)->getAPInt(); 8192 C = M - L; 8193 return true; 8194 } 8195 8196 const SCEV *L, *R; 8197 SCEV::NoWrapFlags Flags; 8198 if (splitBinaryAdd(Less, L, R, Flags)) 8199 if (const auto *LC = dyn_cast<SCEVConstant>(L)) 8200 if (R == More) { 8201 C = -(LC->getAPInt()); 8202 return true; 8203 } 8204 8205 if (splitBinaryAdd(More, L, R, Flags)) 8206 if (const auto *LC = dyn_cast<SCEVConstant>(L)) 8207 if (R == Less) { 8208 C = LC->getAPInt(); 8209 return true; 8210 } 8211 8212 return false; 8213 } 8214 8215 bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow( 8216 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, 8217 const SCEV *FoundLHS, const SCEV *FoundRHS) { 8218 if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT) 8219 return false; 8220 8221 const auto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(LHS); 8222 if (!AddRecLHS) 8223 return false; 8224 8225 const auto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(FoundLHS); 8226 if (!AddRecFoundLHS) 8227 return false; 8228 8229 // We'd like to let SCEV reason about control dependencies, so we constrain 8230 // both the inequalities to be about add recurrences on the same loop. This 8231 // way we can use isLoopEntryGuardedByCond later. 8232 8233 const Loop *L = AddRecFoundLHS->getLoop(); 8234 if (L != AddRecLHS->getLoop()) 8235 return false; 8236 8237 // FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1) 8238 // 8239 // FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C) 8240 // ... (2) 8241 // 8242 // Informal proof for (2), assuming (1) [*]: 8243 // 8244 // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**] 8245 // 8246 // Then 8247 // 8248 // FoundLHS s< FoundRHS s< INT_MIN - C 8249 // <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ] 8250 // <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ] 8251 // <=> (FoundLHS + INT_MIN + C + INT_MIN) s< 8252 // (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ] 8253 // <=> FoundLHS + C s< FoundRHS + C 8254 // 8255 // [*]: (1) can be proved by ruling out overflow. 8256 // 8257 // [**]: This can be proved by analyzing all the four possibilities: 8258 // (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and 8259 // (A s>= 0, B s>= 0). 8260 // 8261 // Note: 8262 // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C" 8263 // will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS 8264 // = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS 8265 // s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is 8266 // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS + 8267 // C)". 8268 8269 APInt LDiff, RDiff; 8270 if (!computeConstantDifference(FoundLHS, LHS, LDiff) || 8271 !computeConstantDifference(FoundRHS, RHS, RDiff) || 8272 LDiff != RDiff) 8273 return false; 8274 8275 if (LDiff == 0) 8276 return true; 8277 8278 APInt FoundRHSLimit; 8279 8280 if (Pred == CmpInst::ICMP_ULT) { 8281 FoundRHSLimit = -RDiff; 8282 } else { 8283 assert(Pred == CmpInst::ICMP_SLT && "Checked above!"); 8284 FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - RDiff; 8285 } 8286 8287 // Try to prove (1) or (2), as needed. 8288 return isLoopEntryGuardedByCond(L, Pred, FoundRHS, 8289 getConstant(FoundRHSLimit)); 8290 } 8291 8292 /// isImpliedCondOperands - Test whether the condition described by Pred, 8293 /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS, 8294 /// and FoundRHS is true. 8295 bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred, 8296 const SCEV *LHS, const SCEV *RHS, 8297 const SCEV *FoundLHS, 8298 const SCEV *FoundRHS) { 8299 if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS)) 8300 return true; 8301 8302 if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS)) 8303 return true; 8304 8305 return isImpliedCondOperandsHelper(Pred, LHS, RHS, 8306 FoundLHS, FoundRHS) || 8307 // ~x < ~y --> x > y 8308 isImpliedCondOperandsHelper(Pred, LHS, RHS, 8309 getNotSCEV(FoundRHS), 8310 getNotSCEV(FoundLHS)); 8311 } 8312 8313 8314 /// If Expr computes ~A, return A else return nullptr 8315 static const SCEV *MatchNotExpr(const SCEV *Expr) { 8316 const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr); 8317 if (!Add || Add->getNumOperands() != 2 || 8318 !Add->getOperand(0)->isAllOnesValue()) 8319 return nullptr; 8320 8321 const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1)); 8322 if (!AddRHS || AddRHS->getNumOperands() != 2 || 8323 !AddRHS->getOperand(0)->isAllOnesValue()) 8324 return nullptr; 8325 8326 return AddRHS->getOperand(1); 8327 } 8328 8329 8330 /// Is MaybeMaxExpr an SMax or UMax of Candidate and some other values? 8331 template<typename MaxExprType> 8332 static bool IsMaxConsistingOf(const SCEV *MaybeMaxExpr, 8333 const SCEV *Candidate) { 8334 const MaxExprType *MaxExpr = dyn_cast<MaxExprType>(MaybeMaxExpr); 8335 if (!MaxExpr) return false; 8336 8337 return find(MaxExpr->operands(), Candidate) != MaxExpr->op_end(); 8338 } 8339 8340 8341 /// Is MaybeMinExpr an SMin or UMin of Candidate and some other values? 8342 template<typename MaxExprType> 8343 static bool IsMinConsistingOf(ScalarEvolution &SE, 8344 const SCEV *MaybeMinExpr, 8345 const SCEV *Candidate) { 8346 const SCEV *MaybeMaxExpr = MatchNotExpr(MaybeMinExpr); 8347 if (!MaybeMaxExpr) 8348 return false; 8349 8350 return IsMaxConsistingOf<MaxExprType>(MaybeMaxExpr, SE.getNotSCEV(Candidate)); 8351 } 8352 8353 static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE, 8354 ICmpInst::Predicate Pred, 8355 const SCEV *LHS, const SCEV *RHS) { 8356 8357 // If both sides are affine addrecs for the same loop, with equal 8358 // steps, and we know the recurrences don't wrap, then we only 8359 // need to check the predicate on the starting values. 8360 8361 if (!ICmpInst::isRelational(Pred)) 8362 return false; 8363 8364 const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS); 8365 if (!LAR) 8366 return false; 8367 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS); 8368 if (!RAR) 8369 return false; 8370 if (LAR->getLoop() != RAR->getLoop()) 8371 return false; 8372 if (!LAR->isAffine() || !RAR->isAffine()) 8373 return false; 8374 8375 if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE)) 8376 return false; 8377 8378 SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ? 8379 SCEV::FlagNSW : SCEV::FlagNUW; 8380 if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW)) 8381 return false; 8382 8383 return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart()); 8384 } 8385 8386 /// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max 8387 /// expression? 8388 static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE, 8389 ICmpInst::Predicate Pred, 8390 const SCEV *LHS, const SCEV *RHS) { 8391 switch (Pred) { 8392 default: 8393 return false; 8394 8395 case ICmpInst::ICMP_SGE: 8396 std::swap(LHS, RHS); 8397 // fall through 8398 case ICmpInst::ICMP_SLE: 8399 return 8400 // min(A, ...) <= A 8401 IsMinConsistingOf<SCEVSMaxExpr>(SE, LHS, RHS) || 8402 // A <= max(A, ...) 8403 IsMaxConsistingOf<SCEVSMaxExpr>(RHS, LHS); 8404 8405 case ICmpInst::ICMP_UGE: 8406 std::swap(LHS, RHS); 8407 // fall through 8408 case ICmpInst::ICMP_ULE: 8409 return 8410 // min(A, ...) <= A 8411 IsMinConsistingOf<SCEVUMaxExpr>(SE, LHS, RHS) || 8412 // A <= max(A, ...) 8413 IsMaxConsistingOf<SCEVUMaxExpr>(RHS, LHS); 8414 } 8415 8416 llvm_unreachable("covered switch fell through?!"); 8417 } 8418 8419 /// isImpliedCondOperandsHelper - Test whether the condition described by 8420 /// Pred, LHS, and RHS is true whenever the condition described by Pred, 8421 /// FoundLHS, and FoundRHS is true. 8422 bool 8423 ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred, 8424 const SCEV *LHS, const SCEV *RHS, 8425 const SCEV *FoundLHS, 8426 const SCEV *FoundRHS) { 8427 auto IsKnownPredicateFull = 8428 [this](ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) { 8429 return isKnownPredicateViaConstantRanges(Pred, LHS, RHS) || 8430 IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) || 8431 IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) || 8432 isKnownPredicateViaNoOverflow(Pred, LHS, RHS); 8433 }; 8434 8435 switch (Pred) { 8436 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!"); 8437 case ICmpInst::ICMP_EQ: 8438 case ICmpInst::ICMP_NE: 8439 if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS)) 8440 return true; 8441 break; 8442 case ICmpInst::ICMP_SLT: 8443 case ICmpInst::ICMP_SLE: 8444 if (IsKnownPredicateFull(ICmpInst::ICMP_SLE, LHS, FoundLHS) && 8445 IsKnownPredicateFull(ICmpInst::ICMP_SGE, RHS, FoundRHS)) 8446 return true; 8447 break; 8448 case ICmpInst::ICMP_SGT: 8449 case ICmpInst::ICMP_SGE: 8450 if (IsKnownPredicateFull(ICmpInst::ICMP_SGE, LHS, FoundLHS) && 8451 IsKnownPredicateFull(ICmpInst::ICMP_SLE, RHS, FoundRHS)) 8452 return true; 8453 break; 8454 case ICmpInst::ICMP_ULT: 8455 case ICmpInst::ICMP_ULE: 8456 if (IsKnownPredicateFull(ICmpInst::ICMP_ULE, LHS, FoundLHS) && 8457 IsKnownPredicateFull(ICmpInst::ICMP_UGE, RHS, FoundRHS)) 8458 return true; 8459 break; 8460 case ICmpInst::ICMP_UGT: 8461 case ICmpInst::ICMP_UGE: 8462 if (IsKnownPredicateFull(ICmpInst::ICMP_UGE, LHS, FoundLHS) && 8463 IsKnownPredicateFull(ICmpInst::ICMP_ULE, RHS, FoundRHS)) 8464 return true; 8465 break; 8466 } 8467 8468 return false; 8469 } 8470 8471 /// isImpliedCondOperandsViaRanges - helper function for isImpliedCondOperands. 8472 /// Tries to get cases like "X `sgt` 0 => X - 1 `sgt` -1". 8473 bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred, 8474 const SCEV *LHS, 8475 const SCEV *RHS, 8476 const SCEV *FoundLHS, 8477 const SCEV *FoundRHS) { 8478 if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS)) 8479 // The restriction on `FoundRHS` be lifted easily -- it exists only to 8480 // reduce the compile time impact of this optimization. 8481 return false; 8482 8483 const SCEVAddExpr *AddLHS = dyn_cast<SCEVAddExpr>(LHS); 8484 if (!AddLHS || AddLHS->getOperand(1) != FoundLHS || 8485 !isa<SCEVConstant>(AddLHS->getOperand(0))) 8486 return false; 8487 8488 APInt ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getAPInt(); 8489 8490 // `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the 8491 // antecedent "`FoundLHS` `Pred` `FoundRHS`". 8492 ConstantRange FoundLHSRange = 8493 ConstantRange::makeAllowedICmpRegion(Pred, ConstFoundRHS); 8494 8495 // Since `LHS` is `FoundLHS` + `AddLHS->getOperand(0)`, we can compute a range 8496 // for `LHS`: 8497 APInt Addend = cast<SCEVConstant>(AddLHS->getOperand(0))->getAPInt(); 8498 ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(Addend)); 8499 8500 // We can also compute the range of values for `LHS` that satisfy the 8501 // consequent, "`LHS` `Pred` `RHS`": 8502 APInt ConstRHS = cast<SCEVConstant>(RHS)->getAPInt(); 8503 ConstantRange SatisfyingLHSRange = 8504 ConstantRange::makeSatisfyingICmpRegion(Pred, ConstRHS); 8505 8506 // The antecedent implies the consequent if every value of `LHS` that 8507 // satisfies the antecedent also satisfies the consequent. 8508 return SatisfyingLHSRange.contains(LHSRange); 8509 } 8510 8511 // Verify if an linear IV with positive stride can overflow when in a 8512 // less-than comparison, knowing the invariant term of the comparison, the 8513 // stride and the knowledge of NSW/NUW flags on the recurrence. 8514 bool ScalarEvolution::doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride, 8515 bool IsSigned, bool NoWrap) { 8516 if (NoWrap) return false; 8517 8518 unsigned BitWidth = getTypeSizeInBits(RHS->getType()); 8519 const SCEV *One = getOne(Stride->getType()); 8520 8521 if (IsSigned) { 8522 APInt MaxRHS = getSignedRange(RHS).getSignedMax(); 8523 APInt MaxValue = APInt::getSignedMaxValue(BitWidth); 8524 APInt MaxStrideMinusOne = getSignedRange(getMinusSCEV(Stride, One)) 8525 .getSignedMax(); 8526 8527 // SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow! 8528 return (MaxValue - MaxStrideMinusOne).slt(MaxRHS); 8529 } 8530 8531 APInt MaxRHS = getUnsignedRange(RHS).getUnsignedMax(); 8532 APInt MaxValue = APInt::getMaxValue(BitWidth); 8533 APInt MaxStrideMinusOne = getUnsignedRange(getMinusSCEV(Stride, One)) 8534 .getUnsignedMax(); 8535 8536 // UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow! 8537 return (MaxValue - MaxStrideMinusOne).ult(MaxRHS); 8538 } 8539 8540 // Verify if an linear IV with negative stride can overflow when in a 8541 // greater-than comparison, knowing the invariant term of the comparison, 8542 // the stride and the knowledge of NSW/NUW flags on the recurrence. 8543 bool ScalarEvolution::doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride, 8544 bool IsSigned, bool NoWrap) { 8545 if (NoWrap) return false; 8546 8547 unsigned BitWidth = getTypeSizeInBits(RHS->getType()); 8548 const SCEV *One = getOne(Stride->getType()); 8549 8550 if (IsSigned) { 8551 APInt MinRHS = getSignedRange(RHS).getSignedMin(); 8552 APInt MinValue = APInt::getSignedMinValue(BitWidth); 8553 APInt MaxStrideMinusOne = getSignedRange(getMinusSCEV(Stride, One)) 8554 .getSignedMax(); 8555 8556 // SMinRHS - SMaxStrideMinusOne < SMinValue => overflow! 8557 return (MinValue + MaxStrideMinusOne).sgt(MinRHS); 8558 } 8559 8560 APInt MinRHS = getUnsignedRange(RHS).getUnsignedMin(); 8561 APInt MinValue = APInt::getMinValue(BitWidth); 8562 APInt MaxStrideMinusOne = getUnsignedRange(getMinusSCEV(Stride, One)) 8563 .getUnsignedMax(); 8564 8565 // UMinRHS - UMaxStrideMinusOne < UMinValue => overflow! 8566 return (MinValue + MaxStrideMinusOne).ugt(MinRHS); 8567 } 8568 8569 // Compute the backedge taken count knowing the interval difference, the 8570 // stride and presence of the equality in the comparison. 8571 const SCEV *ScalarEvolution::computeBECount(const SCEV *Delta, const SCEV *Step, 8572 bool Equality) { 8573 const SCEV *One = getOne(Step->getType()); 8574 Delta = Equality ? getAddExpr(Delta, Step) 8575 : getAddExpr(Delta, getMinusSCEV(Step, One)); 8576 return getUDivExpr(Delta, Step); 8577 } 8578 8579 /// HowManyLessThans - Return the number of times a backedge containing the 8580 /// specified less-than comparison will execute. If not computable, return 8581 /// CouldNotCompute. 8582 /// 8583 /// @param ControlsExit is true when the LHS < RHS condition directly controls 8584 /// the branch (loops exits only if condition is true). In this case, we can use 8585 /// NoWrapFlags to skip overflow checks. 8586 ScalarEvolution::ExitLimit 8587 ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, 8588 const Loop *L, bool IsSigned, 8589 bool ControlsExit, bool AllowPredicates) { 8590 SCEVUnionPredicate P; 8591 // We handle only IV < Invariant 8592 if (!isLoopInvariant(RHS, L)) 8593 return getCouldNotCompute(); 8594 8595 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS); 8596 if (!IV && AllowPredicates) 8597 // Try to make this an AddRec using runtime tests, in the first X 8598 // iterations of this loop, where X is the SCEV expression found by the 8599 // algorithm below. 8600 IV = convertSCEVToAddRecWithPredicates(LHS, L, P); 8601 8602 // Avoid weird loops 8603 if (!IV || IV->getLoop() != L || !IV->isAffine()) 8604 return getCouldNotCompute(); 8605 8606 bool NoWrap = ControlsExit && 8607 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW); 8608 8609 const SCEV *Stride = IV->getStepRecurrence(*this); 8610 8611 // Avoid negative or zero stride values 8612 if (!isKnownPositive(Stride)) 8613 return getCouldNotCompute(); 8614 8615 // Avoid proven overflow cases: this will ensure that the backedge taken count 8616 // will not generate any unsigned overflow. Relaxed no-overflow conditions 8617 // exploit NoWrapFlags, allowing to optimize in presence of undefined 8618 // behaviors like the case of C language. 8619 if (!Stride->isOne() && doesIVOverflowOnLT(RHS, Stride, IsSigned, NoWrap)) 8620 return getCouldNotCompute(); 8621 8622 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT 8623 : ICmpInst::ICMP_ULT; 8624 const SCEV *Start = IV->getStart(); 8625 const SCEV *End = RHS; 8626 if (!isLoopEntryGuardedByCond(L, Cond, getMinusSCEV(Start, Stride), RHS)) { 8627 const SCEV *Diff = getMinusSCEV(RHS, Start); 8628 // If we have NoWrap set, then we can assume that the increment won't 8629 // overflow, in which case if RHS - Start is a constant, we don't need to 8630 // do a max operation since we can just figure it out statically 8631 if (NoWrap && isa<SCEVConstant>(Diff)) { 8632 APInt D = dyn_cast<const SCEVConstant>(Diff)->getAPInt(); 8633 if (D.isNegative()) 8634 End = Start; 8635 } else 8636 End = IsSigned ? getSMaxExpr(RHS, Start) 8637 : getUMaxExpr(RHS, Start); 8638 } 8639 8640 const SCEV *BECount = computeBECount(getMinusSCEV(End, Start), Stride, false); 8641 8642 APInt MinStart = IsSigned ? getSignedRange(Start).getSignedMin() 8643 : getUnsignedRange(Start).getUnsignedMin(); 8644 8645 APInt MinStride = IsSigned ? getSignedRange(Stride).getSignedMin() 8646 : getUnsignedRange(Stride).getUnsignedMin(); 8647 8648 unsigned BitWidth = getTypeSizeInBits(LHS->getType()); 8649 APInt Limit = IsSigned ? APInt::getSignedMaxValue(BitWidth) - (MinStride - 1) 8650 : APInt::getMaxValue(BitWidth) - (MinStride - 1); 8651 8652 // Although End can be a MAX expression we estimate MaxEnd considering only 8653 // the case End = RHS. This is safe because in the other case (End - Start) 8654 // is zero, leading to a zero maximum backedge taken count. 8655 APInt MaxEnd = 8656 IsSigned ? APIntOps::smin(getSignedRange(RHS).getSignedMax(), Limit) 8657 : APIntOps::umin(getUnsignedRange(RHS).getUnsignedMax(), Limit); 8658 8659 const SCEV *MaxBECount; 8660 if (isa<SCEVConstant>(BECount)) 8661 MaxBECount = BECount; 8662 else 8663 MaxBECount = computeBECount(getConstant(MaxEnd - MinStart), 8664 getConstant(MinStride), false); 8665 8666 if (isa<SCEVCouldNotCompute>(MaxBECount)) 8667 MaxBECount = BECount; 8668 8669 return ExitLimit(BECount, MaxBECount, P); 8670 } 8671 8672 ScalarEvolution::ExitLimit 8673 ScalarEvolution::HowManyGreaterThans(const SCEV *LHS, const SCEV *RHS, 8674 const Loop *L, bool IsSigned, 8675 bool ControlsExit, bool AllowPredicates) { 8676 SCEVUnionPredicate P; 8677 // We handle only IV > Invariant 8678 if (!isLoopInvariant(RHS, L)) 8679 return getCouldNotCompute(); 8680 8681 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS); 8682 if (!IV && AllowPredicates) 8683 // Try to make this an AddRec using runtime tests, in the first X 8684 // iterations of this loop, where X is the SCEV expression found by the 8685 // algorithm below. 8686 IV = convertSCEVToAddRecWithPredicates(LHS, L, P); 8687 8688 // Avoid weird loops 8689 if (!IV || IV->getLoop() != L || !IV->isAffine()) 8690 return getCouldNotCompute(); 8691 8692 bool NoWrap = ControlsExit && 8693 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW); 8694 8695 const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this)); 8696 8697 // Avoid negative or zero stride values 8698 if (!isKnownPositive(Stride)) 8699 return getCouldNotCompute(); 8700 8701 // Avoid proven overflow cases: this will ensure that the backedge taken count 8702 // will not generate any unsigned overflow. Relaxed no-overflow conditions 8703 // exploit NoWrapFlags, allowing to optimize in presence of undefined 8704 // behaviors like the case of C language. 8705 if (!Stride->isOne() && doesIVOverflowOnGT(RHS, Stride, IsSigned, NoWrap)) 8706 return getCouldNotCompute(); 8707 8708 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT 8709 : ICmpInst::ICMP_UGT; 8710 8711 const SCEV *Start = IV->getStart(); 8712 const SCEV *End = RHS; 8713 if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS)) { 8714 const SCEV *Diff = getMinusSCEV(RHS, Start); 8715 // If we have NoWrap set, then we can assume that the increment won't 8716 // overflow, in which case if RHS - Start is a constant, we don't need to 8717 // do a max operation since we can just figure it out statically 8718 if (NoWrap && isa<SCEVConstant>(Diff)) { 8719 APInt D = dyn_cast<const SCEVConstant>(Diff)->getAPInt(); 8720 if (!D.isNegative()) 8721 End = Start; 8722 } else 8723 End = IsSigned ? getSMinExpr(RHS, Start) 8724 : getUMinExpr(RHS, Start); 8725 } 8726 8727 const SCEV *BECount = computeBECount(getMinusSCEV(Start, End), Stride, false); 8728 8729 APInt MaxStart = IsSigned ? getSignedRange(Start).getSignedMax() 8730 : getUnsignedRange(Start).getUnsignedMax(); 8731 8732 APInt MinStride = IsSigned ? getSignedRange(Stride).getSignedMin() 8733 : getUnsignedRange(Stride).getUnsignedMin(); 8734 8735 unsigned BitWidth = getTypeSizeInBits(LHS->getType()); 8736 APInt Limit = IsSigned ? APInt::getSignedMinValue(BitWidth) + (MinStride - 1) 8737 : APInt::getMinValue(BitWidth) + (MinStride - 1); 8738 8739 // Although End can be a MIN expression we estimate MinEnd considering only 8740 // the case End = RHS. This is safe because in the other case (Start - End) 8741 // is zero, leading to a zero maximum backedge taken count. 8742 APInt MinEnd = 8743 IsSigned ? APIntOps::smax(getSignedRange(RHS).getSignedMin(), Limit) 8744 : APIntOps::umax(getUnsignedRange(RHS).getUnsignedMin(), Limit); 8745 8746 8747 const SCEV *MaxBECount = getCouldNotCompute(); 8748 if (isa<SCEVConstant>(BECount)) 8749 MaxBECount = BECount; 8750 else 8751 MaxBECount = computeBECount(getConstant(MaxStart - MinEnd), 8752 getConstant(MinStride), false); 8753 8754 if (isa<SCEVCouldNotCompute>(MaxBECount)) 8755 MaxBECount = BECount; 8756 8757 return ExitLimit(BECount, MaxBECount, P); 8758 } 8759 8760 /// getNumIterationsInRange - Return the number of iterations of this loop that 8761 /// produce values in the specified constant range. Another way of looking at 8762 /// this is that it returns the first iteration number where the value is not in 8763 /// the condition, thus computing the exit count. If the iteration count can't 8764 /// be computed, an instance of SCEVCouldNotCompute is returned. 8765 const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, 8766 ScalarEvolution &SE) const { 8767 if (Range.isFullSet()) // Infinite loop. 8768 return SE.getCouldNotCompute(); 8769 8770 // If the start is a non-zero constant, shift the range to simplify things. 8771 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) 8772 if (!SC->getValue()->isZero()) { 8773 SmallVector<const SCEV *, 4> Operands(op_begin(), op_end()); 8774 Operands[0] = SE.getZero(SC->getType()); 8775 const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(), 8776 getNoWrapFlags(FlagNW)); 8777 if (const auto *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted)) 8778 return ShiftedAddRec->getNumIterationsInRange( 8779 Range.subtract(SC->getAPInt()), SE); 8780 // This is strange and shouldn't happen. 8781 return SE.getCouldNotCompute(); 8782 } 8783 8784 // The only time we can solve this is when we have all constant indices. 8785 // Otherwise, we cannot determine the overflow conditions. 8786 if (any_of(operands(), [](const SCEV *Op) { return !isa<SCEVConstant>(Op); })) 8787 return SE.getCouldNotCompute(); 8788 8789 // Okay at this point we know that all elements of the chrec are constants and 8790 // that the start element is zero. 8791 8792 // First check to see if the range contains zero. If not, the first 8793 // iteration exits. 8794 unsigned BitWidth = SE.getTypeSizeInBits(getType()); 8795 if (!Range.contains(APInt(BitWidth, 0))) 8796 return SE.getZero(getType()); 8797 8798 if (isAffine()) { 8799 // If this is an affine expression then we have this situation: 8800 // Solve {0,+,A} in Range === Ax in Range 8801 8802 // We know that zero is in the range. If A is positive then we know that 8803 // the upper value of the range must be the first possible exit value. 8804 // If A is negative then the lower of the range is the last possible loop 8805 // value. Also note that we already checked for a full range. 8806 APInt One(BitWidth,1); 8807 APInt A = cast<SCEVConstant>(getOperand(1))->getAPInt(); 8808 APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); 8809 8810 // The exit value should be (End+A)/A. 8811 APInt ExitVal = (End + A).udiv(A); 8812 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal); 8813 8814 // Evaluate at the exit value. If we really did fall out of the valid 8815 // range, then we computed our trip count, otherwise wrap around or other 8816 // things must have happened. 8817 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); 8818 if (Range.contains(Val->getValue())) 8819 return SE.getCouldNotCompute(); // Something strange happened 8820 8821 // Ensure that the previous value is in the range. This is a sanity check. 8822 assert(Range.contains( 8823 EvaluateConstantChrecAtConstant(this, 8824 ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) && 8825 "Linear scev computation is off in a bad way!"); 8826 return SE.getConstant(ExitValue); 8827 } else if (isQuadratic()) { 8828 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the 8829 // quadratic equation to solve it. To do this, we must frame our problem in 8830 // terms of figuring out when zero is crossed, instead of when 8831 // Range.getUpper() is crossed. 8832 SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end()); 8833 NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); 8834 const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop(), 8835 // getNoWrapFlags(FlagNW) 8836 FlagAnyWrap); 8837 8838 // Next, solve the constructed addrec 8839 auto Roots = SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); 8840 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); 8841 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); 8842 if (R1) { 8843 // Pick the smallest positive root value. 8844 if (ConstantInt *CB = dyn_cast<ConstantInt>(ConstantExpr::getICmp( 8845 ICmpInst::ICMP_ULT, R1->getValue(), R2->getValue()))) { 8846 if (!CB->getZExtValue()) 8847 std::swap(R1, R2); // R1 is the minimum root now. 8848 8849 // Make sure the root is not off by one. The returned iteration should 8850 // not be in the range, but the previous one should be. When solving 8851 // for "X*X < 5", for example, we should not return a root of 2. 8852 ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, 8853 R1->getValue(), 8854 SE); 8855 if (Range.contains(R1Val->getValue())) { 8856 // The next iteration must be out of the range... 8857 ConstantInt *NextVal = 8858 ConstantInt::get(SE.getContext(), R1->getAPInt() + 1); 8859 8860 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); 8861 if (!Range.contains(R1Val->getValue())) 8862 return SE.getConstant(NextVal); 8863 return SE.getCouldNotCompute(); // Something strange happened 8864 } 8865 8866 // If R1 was not in the range, then it is a good return value. Make 8867 // sure that R1-1 WAS in the range though, just in case. 8868 ConstantInt *NextVal = 8869 ConstantInt::get(SE.getContext(), R1->getAPInt() - 1); 8870 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); 8871 if (Range.contains(R1Val->getValue())) 8872 return R1; 8873 return SE.getCouldNotCompute(); // Something strange happened 8874 } 8875 } 8876 } 8877 8878 return SE.getCouldNotCompute(); 8879 } 8880 8881 namespace { 8882 struct FindUndefs { 8883 bool Found; 8884 FindUndefs() : Found(false) {} 8885 8886 bool follow(const SCEV *S) { 8887 if (const SCEVUnknown *C = dyn_cast<SCEVUnknown>(S)) { 8888 if (isa<UndefValue>(C->getValue())) 8889 Found = true; 8890 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { 8891 if (isa<UndefValue>(C->getValue())) 8892 Found = true; 8893 } 8894 8895 // Keep looking if we haven't found it yet. 8896 return !Found; 8897 } 8898 bool isDone() const { 8899 // Stop recursion if we have found an undef. 8900 return Found; 8901 } 8902 }; 8903 } 8904 8905 // Return true when S contains at least an undef value. 8906 static inline bool 8907 containsUndefs(const SCEV *S) { 8908 FindUndefs F; 8909 SCEVTraversal<FindUndefs> ST(F); 8910 ST.visitAll(S); 8911 8912 return F.Found; 8913 } 8914 8915 namespace { 8916 // Collect all steps of SCEV expressions. 8917 struct SCEVCollectStrides { 8918 ScalarEvolution &SE; 8919 SmallVectorImpl<const SCEV *> &Strides; 8920 8921 SCEVCollectStrides(ScalarEvolution &SE, SmallVectorImpl<const SCEV *> &S) 8922 : SE(SE), Strides(S) {} 8923 8924 bool follow(const SCEV *S) { 8925 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) 8926 Strides.push_back(AR->getStepRecurrence(SE)); 8927 return true; 8928 } 8929 bool isDone() const { return false; } 8930 }; 8931 8932 // Collect all SCEVUnknown and SCEVMulExpr expressions. 8933 struct SCEVCollectTerms { 8934 SmallVectorImpl<const SCEV *> &Terms; 8935 8936 SCEVCollectTerms(SmallVectorImpl<const SCEV *> &T) 8937 : Terms(T) {} 8938 8939 bool follow(const SCEV *S) { 8940 if (isa<SCEVUnknown>(S) || isa<SCEVMulExpr>(S)) { 8941 if (!containsUndefs(S)) 8942 Terms.push_back(S); 8943 8944 // Stop recursion: once we collected a term, do not walk its operands. 8945 return false; 8946 } 8947 8948 // Keep looking. 8949 return true; 8950 } 8951 bool isDone() const { return false; } 8952 }; 8953 8954 // Check if a SCEV contains an AddRecExpr. 8955 struct SCEVHasAddRec { 8956 bool &ContainsAddRec; 8957 8958 SCEVHasAddRec(bool &ContainsAddRec) : ContainsAddRec(ContainsAddRec) { 8959 ContainsAddRec = false; 8960 } 8961 8962 bool follow(const SCEV *S) { 8963 if (isa<SCEVAddRecExpr>(S)) { 8964 ContainsAddRec = true; 8965 8966 // Stop recursion: once we collected a term, do not walk its operands. 8967 return false; 8968 } 8969 8970 // Keep looking. 8971 return true; 8972 } 8973 bool isDone() const { return false; } 8974 }; 8975 8976 // Find factors that are multiplied with an expression that (possibly as a 8977 // subexpression) contains an AddRecExpr. In the expression: 8978 // 8979 // 8 * (100 + %p * %q * (%a + {0, +, 1}_loop)) 8980 // 8981 // "%p * %q" are factors multiplied by the expression "(%a + {0, +, 1}_loop)" 8982 // that contains the AddRec {0, +, 1}_loop. %p * %q are likely to be array size 8983 // parameters as they form a product with an induction variable. 8984 // 8985 // This collector expects all array size parameters to be in the same MulExpr. 8986 // It might be necessary to later add support for collecting parameters that are 8987 // spread over different nested MulExpr. 8988 struct SCEVCollectAddRecMultiplies { 8989 SmallVectorImpl<const SCEV *> &Terms; 8990 ScalarEvolution &SE; 8991 8992 SCEVCollectAddRecMultiplies(SmallVectorImpl<const SCEV *> &T, ScalarEvolution &SE) 8993 : Terms(T), SE(SE) {} 8994 8995 bool follow(const SCEV *S) { 8996 if (auto *Mul = dyn_cast<SCEVMulExpr>(S)) { 8997 bool HasAddRec = false; 8998 SmallVector<const SCEV *, 0> Operands; 8999 for (auto Op : Mul->operands()) { 9000 if (isa<SCEVUnknown>(Op)) { 9001 Operands.push_back(Op); 9002 } else { 9003 bool ContainsAddRec; 9004 SCEVHasAddRec ContiansAddRec(ContainsAddRec); 9005 visitAll(Op, ContiansAddRec); 9006 HasAddRec |= ContainsAddRec; 9007 } 9008 } 9009 if (Operands.size() == 0) 9010 return true; 9011 9012 if (!HasAddRec) 9013 return false; 9014 9015 Terms.push_back(SE.getMulExpr(Operands)); 9016 // Stop recursion: once we collected a term, do not walk its operands. 9017 return false; 9018 } 9019 9020 // Keep looking. 9021 return true; 9022 } 9023 bool isDone() const { return false; } 9024 }; 9025 } 9026 9027 /// Find parametric terms in this SCEVAddRecExpr. We first for parameters in 9028 /// two places: 9029 /// 1) The strides of AddRec expressions. 9030 /// 2) Unknowns that are multiplied with AddRec expressions. 9031 void ScalarEvolution::collectParametricTerms(const SCEV *Expr, 9032 SmallVectorImpl<const SCEV *> &Terms) { 9033 SmallVector<const SCEV *, 4> Strides; 9034 SCEVCollectStrides StrideCollector(*this, Strides); 9035 visitAll(Expr, StrideCollector); 9036 9037 DEBUG({ 9038 dbgs() << "Strides:\n"; 9039 for (const SCEV *S : Strides) 9040 dbgs() << *S << "\n"; 9041 }); 9042 9043 for (const SCEV *S : Strides) { 9044 SCEVCollectTerms TermCollector(Terms); 9045 visitAll(S, TermCollector); 9046 } 9047 9048 DEBUG({ 9049 dbgs() << "Terms:\n"; 9050 for (const SCEV *T : Terms) 9051 dbgs() << *T << "\n"; 9052 }); 9053 9054 SCEVCollectAddRecMultiplies MulCollector(Terms, *this); 9055 visitAll(Expr, MulCollector); 9056 } 9057 9058 static bool findArrayDimensionsRec(ScalarEvolution &SE, 9059 SmallVectorImpl<const SCEV *> &Terms, 9060 SmallVectorImpl<const SCEV *> &Sizes) { 9061 int Last = Terms.size() - 1; 9062 const SCEV *Step = Terms[Last]; 9063 9064 // End of recursion. 9065 if (Last == 0) { 9066 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Step)) { 9067 SmallVector<const SCEV *, 2> Qs; 9068 for (const SCEV *Op : M->operands()) 9069 if (!isa<SCEVConstant>(Op)) 9070 Qs.push_back(Op); 9071 9072 Step = SE.getMulExpr(Qs); 9073 } 9074 9075 Sizes.push_back(Step); 9076 return true; 9077 } 9078 9079 for (const SCEV *&Term : Terms) { 9080 // Normalize the terms before the next call to findArrayDimensionsRec. 9081 const SCEV *Q, *R; 9082 SCEVDivision::divide(SE, Term, Step, &Q, &R); 9083 9084 // Bail out when GCD does not evenly divide one of the terms. 9085 if (!R->isZero()) 9086 return false; 9087 9088 Term = Q; 9089 } 9090 9091 // Remove all SCEVConstants. 9092 Terms.erase(std::remove_if(Terms.begin(), Terms.end(), [](const SCEV *E) { 9093 return isa<SCEVConstant>(E); 9094 }), 9095 Terms.end()); 9096 9097 if (Terms.size() > 0) 9098 if (!findArrayDimensionsRec(SE, Terms, Sizes)) 9099 return false; 9100 9101 Sizes.push_back(Step); 9102 return true; 9103 } 9104 9105 // Returns true when S contains at least a SCEVUnknown parameter. 9106 static inline bool 9107 containsParameters(const SCEV *S) { 9108 struct FindParameter { 9109 bool FoundParameter; 9110 FindParameter() : FoundParameter(false) {} 9111 9112 bool follow(const SCEV *S) { 9113 if (isa<SCEVUnknown>(S)) { 9114 FoundParameter = true; 9115 // Stop recursion: we found a parameter. 9116 return false; 9117 } 9118 // Keep looking. 9119 return true; 9120 } 9121 bool isDone() const { 9122 // Stop recursion if we have found a parameter. 9123 return FoundParameter; 9124 } 9125 }; 9126 9127 FindParameter F; 9128 SCEVTraversal<FindParameter> ST(F); 9129 ST.visitAll(S); 9130 9131 return F.FoundParameter; 9132 } 9133 9134 // Returns true when one of the SCEVs of Terms contains a SCEVUnknown parameter. 9135 static inline bool 9136 containsParameters(SmallVectorImpl<const SCEV *> &Terms) { 9137 for (const SCEV *T : Terms) 9138 if (containsParameters(T)) 9139 return true; 9140 return false; 9141 } 9142 9143 // Return the number of product terms in S. 9144 static inline int numberOfTerms(const SCEV *S) { 9145 if (const SCEVMulExpr *Expr = dyn_cast<SCEVMulExpr>(S)) 9146 return Expr->getNumOperands(); 9147 return 1; 9148 } 9149 9150 static const SCEV *removeConstantFactors(ScalarEvolution &SE, const SCEV *T) { 9151 if (isa<SCEVConstant>(T)) 9152 return nullptr; 9153 9154 if (isa<SCEVUnknown>(T)) 9155 return T; 9156 9157 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(T)) { 9158 SmallVector<const SCEV *, 2> Factors; 9159 for (const SCEV *Op : M->operands()) 9160 if (!isa<SCEVConstant>(Op)) 9161 Factors.push_back(Op); 9162 9163 return SE.getMulExpr(Factors); 9164 } 9165 9166 return T; 9167 } 9168 9169 /// Return the size of an element read or written by Inst. 9170 const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) { 9171 Type *Ty; 9172 if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) 9173 Ty = Store->getValueOperand()->getType(); 9174 else if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) 9175 Ty = Load->getType(); 9176 else 9177 return nullptr; 9178 9179 Type *ETy = getEffectiveSCEVType(PointerType::getUnqual(Ty)); 9180 return getSizeOfExpr(ETy, Ty); 9181 } 9182 9183 /// Second step of delinearization: compute the array dimensions Sizes from the 9184 /// set of Terms extracted from the memory access function of this SCEVAddRec. 9185 void ScalarEvolution::findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms, 9186 SmallVectorImpl<const SCEV *> &Sizes, 9187 const SCEV *ElementSize) const { 9188 9189 if (Terms.size() < 1 || !ElementSize) 9190 return; 9191 9192 // Early return when Terms do not contain parameters: we do not delinearize 9193 // non parametric SCEVs. 9194 if (!containsParameters(Terms)) 9195 return; 9196 9197 DEBUG({ 9198 dbgs() << "Terms:\n"; 9199 for (const SCEV *T : Terms) 9200 dbgs() << *T << "\n"; 9201 }); 9202 9203 // Remove duplicates. 9204 std::sort(Terms.begin(), Terms.end()); 9205 Terms.erase(std::unique(Terms.begin(), Terms.end()), Terms.end()); 9206 9207 // Put larger terms first. 9208 std::sort(Terms.begin(), Terms.end(), [](const SCEV *LHS, const SCEV *RHS) { 9209 return numberOfTerms(LHS) > numberOfTerms(RHS); 9210 }); 9211 9212 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); 9213 9214 // Try to divide all terms by the element size. If term is not divisible by 9215 // element size, proceed with the original term. 9216 for (const SCEV *&Term : Terms) { 9217 const SCEV *Q, *R; 9218 SCEVDivision::divide(SE, Term, ElementSize, &Q, &R); 9219 if (!Q->isZero()) 9220 Term = Q; 9221 } 9222 9223 SmallVector<const SCEV *, 4> NewTerms; 9224 9225 // Remove constant factors. 9226 for (const SCEV *T : Terms) 9227 if (const SCEV *NewT = removeConstantFactors(SE, T)) 9228 NewTerms.push_back(NewT); 9229 9230 DEBUG({ 9231 dbgs() << "Terms after sorting:\n"; 9232 for (const SCEV *T : NewTerms) 9233 dbgs() << *T << "\n"; 9234 }); 9235 9236 if (NewTerms.empty() || 9237 !findArrayDimensionsRec(SE, NewTerms, Sizes)) { 9238 Sizes.clear(); 9239 return; 9240 } 9241 9242 // The last element to be pushed into Sizes is the size of an element. 9243 Sizes.push_back(ElementSize); 9244 9245 DEBUG({ 9246 dbgs() << "Sizes:\n"; 9247 for (const SCEV *S : Sizes) 9248 dbgs() << *S << "\n"; 9249 }); 9250 } 9251 9252 /// Third step of delinearization: compute the access functions for the 9253 /// Subscripts based on the dimensions in Sizes. 9254 void ScalarEvolution::computeAccessFunctions( 9255 const SCEV *Expr, SmallVectorImpl<const SCEV *> &Subscripts, 9256 SmallVectorImpl<const SCEV *> &Sizes) { 9257 9258 // Early exit in case this SCEV is not an affine multivariate function. 9259 if (Sizes.empty()) 9260 return; 9261 9262 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Expr)) 9263 if (!AR->isAffine()) 9264 return; 9265 9266 const SCEV *Res = Expr; 9267 int Last = Sizes.size() - 1; 9268 for (int i = Last; i >= 0; i--) { 9269 const SCEV *Q, *R; 9270 SCEVDivision::divide(*this, Res, Sizes[i], &Q, &R); 9271 9272 DEBUG({ 9273 dbgs() << "Res: " << *Res << "\n"; 9274 dbgs() << "Sizes[i]: " << *Sizes[i] << "\n"; 9275 dbgs() << "Res divided by Sizes[i]:\n"; 9276 dbgs() << "Quotient: " << *Q << "\n"; 9277 dbgs() << "Remainder: " << *R << "\n"; 9278 }); 9279 9280 Res = Q; 9281 9282 // Do not record the last subscript corresponding to the size of elements in 9283 // the array. 9284 if (i == Last) { 9285 9286 // Bail out if the remainder is too complex. 9287 if (isa<SCEVAddRecExpr>(R)) { 9288 Subscripts.clear(); 9289 Sizes.clear(); 9290 return; 9291 } 9292 9293 continue; 9294 } 9295 9296 // Record the access function for the current subscript. 9297 Subscripts.push_back(R); 9298 } 9299 9300 // Also push in last position the remainder of the last division: it will be 9301 // the access function of the innermost dimension. 9302 Subscripts.push_back(Res); 9303 9304 std::reverse(Subscripts.begin(), Subscripts.end()); 9305 9306 DEBUG({ 9307 dbgs() << "Subscripts:\n"; 9308 for (const SCEV *S : Subscripts) 9309 dbgs() << *S << "\n"; 9310 }); 9311 } 9312 9313 /// Splits the SCEV into two vectors of SCEVs representing the subscripts and 9314 /// sizes of an array access. Returns the remainder of the delinearization that 9315 /// is the offset start of the array. The SCEV->delinearize algorithm computes 9316 /// the multiples of SCEV coefficients: that is a pattern matching of sub 9317 /// expressions in the stride and base of a SCEV corresponding to the 9318 /// computation of a GCD (greatest common divisor) of base and stride. When 9319 /// SCEV->delinearize fails, it returns the SCEV unchanged. 9320 /// 9321 /// For example: when analyzing the memory access A[i][j][k] in this loop nest 9322 /// 9323 /// void foo(long n, long m, long o, double A[n][m][o]) { 9324 /// 9325 /// for (long i = 0; i < n; i++) 9326 /// for (long j = 0; j < m; j++) 9327 /// for (long k = 0; k < o; k++) 9328 /// A[i][j][k] = 1.0; 9329 /// } 9330 /// 9331 /// the delinearization input is the following AddRec SCEV: 9332 /// 9333 /// AddRec: {{{%A,+,(8 * %m * %o)}<%for.i>,+,(8 * %o)}<%for.j>,+,8}<%for.k> 9334 /// 9335 /// From this SCEV, we are able to say that the base offset of the access is %A 9336 /// because it appears as an offset that does not divide any of the strides in 9337 /// the loops: 9338 /// 9339 /// CHECK: Base offset: %A 9340 /// 9341 /// and then SCEV->delinearize determines the size of some of the dimensions of 9342 /// the array as these are the multiples by which the strides are happening: 9343 /// 9344 /// CHECK: ArrayDecl[UnknownSize][%m][%o] with elements of sizeof(double) bytes. 9345 /// 9346 /// Note that the outermost dimension remains of UnknownSize because there are 9347 /// no strides that would help identifying the size of the last dimension: when 9348 /// the array has been statically allocated, one could compute the size of that 9349 /// dimension by dividing the overall size of the array by the size of the known 9350 /// dimensions: %m * %o * 8. 9351 /// 9352 /// Finally delinearize provides the access functions for the array reference 9353 /// that does correspond to A[i][j][k] of the above C testcase: 9354 /// 9355 /// CHECK: ArrayRef[{0,+,1}<%for.i>][{0,+,1}<%for.j>][{0,+,1}<%for.k>] 9356 /// 9357 /// The testcases are checking the output of a function pass: 9358 /// DelinearizationPass that walks through all loads and stores of a function 9359 /// asking for the SCEV of the memory access with respect to all enclosing 9360 /// loops, calling SCEV->delinearize on that and printing the results. 9361 9362 void ScalarEvolution::delinearize(const SCEV *Expr, 9363 SmallVectorImpl<const SCEV *> &Subscripts, 9364 SmallVectorImpl<const SCEV *> &Sizes, 9365 const SCEV *ElementSize) { 9366 // First step: collect parametric terms. 9367 SmallVector<const SCEV *, 4> Terms; 9368 collectParametricTerms(Expr, Terms); 9369 9370 if (Terms.empty()) 9371 return; 9372 9373 // Second step: find subscript sizes. 9374 findArrayDimensions(Terms, Sizes, ElementSize); 9375 9376 if (Sizes.empty()) 9377 return; 9378 9379 // Third step: compute the access functions for each subscript. 9380 computeAccessFunctions(Expr, Subscripts, Sizes); 9381 9382 if (Subscripts.empty()) 9383 return; 9384 9385 DEBUG({ 9386 dbgs() << "succeeded to delinearize " << *Expr << "\n"; 9387 dbgs() << "ArrayDecl[UnknownSize]"; 9388 for (const SCEV *S : Sizes) 9389 dbgs() << "[" << *S << "]"; 9390 9391 dbgs() << "\nArrayRef"; 9392 for (const SCEV *S : Subscripts) 9393 dbgs() << "[" << *S << "]"; 9394 dbgs() << "\n"; 9395 }); 9396 } 9397 9398 //===----------------------------------------------------------------------===// 9399 // SCEVCallbackVH Class Implementation 9400 //===----------------------------------------------------------------------===// 9401 9402 void ScalarEvolution::SCEVCallbackVH::deleted() { 9403 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); 9404 if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) 9405 SE->ConstantEvolutionLoopExitValue.erase(PN); 9406 SE->eraseValueFromMap(getValPtr()); 9407 // this now dangles! 9408 } 9409 9410 void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) { 9411 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!"); 9412 9413 // Forget all the expressions associated with users of the old value, 9414 // so that future queries will recompute the expressions using the new 9415 // value. 9416 Value *Old = getValPtr(); 9417 SmallVector<User *, 16> Worklist(Old->user_begin(), Old->user_end()); 9418 SmallPtrSet<User *, 8> Visited; 9419 while (!Worklist.empty()) { 9420 User *U = Worklist.pop_back_val(); 9421 // Deleting the Old value will cause this to dangle. Postpone 9422 // that until everything else is done. 9423 if (U == Old) 9424 continue; 9425 if (!Visited.insert(U).second) 9426 continue; 9427 if (PHINode *PN = dyn_cast<PHINode>(U)) 9428 SE->ConstantEvolutionLoopExitValue.erase(PN); 9429 SE->eraseValueFromMap(U); 9430 Worklist.insert(Worklist.end(), U->user_begin(), U->user_end()); 9431 } 9432 // Delete the Old value. 9433 if (PHINode *PN = dyn_cast<PHINode>(Old)) 9434 SE->ConstantEvolutionLoopExitValue.erase(PN); 9435 SE->eraseValueFromMap(Old); 9436 // this now dangles! 9437 } 9438 9439 ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) 9440 : CallbackVH(V), SE(se) {} 9441 9442 //===----------------------------------------------------------------------===// 9443 // ScalarEvolution Class Implementation 9444 //===----------------------------------------------------------------------===// 9445 9446 ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI, 9447 AssumptionCache &AC, DominatorTree &DT, 9448 LoopInfo &LI) 9449 : F(F), TLI(TLI), AC(AC), DT(DT), LI(LI), 9450 CouldNotCompute(new SCEVCouldNotCompute()), 9451 WalkingBEDominatingConds(false), ProvingSplitPredicate(false), 9452 ValuesAtScopes(64), LoopDispositions(64), BlockDispositions(64), 9453 FirstUnknown(nullptr) {} 9454 9455 ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg) 9456 : F(Arg.F), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT), LI(Arg.LI), 9457 CouldNotCompute(std::move(Arg.CouldNotCompute)), 9458 ValueExprMap(std::move(Arg.ValueExprMap)), 9459 WalkingBEDominatingConds(false), ProvingSplitPredicate(false), 9460 BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)), 9461 PredicatedBackedgeTakenCounts( 9462 std::move(Arg.PredicatedBackedgeTakenCounts)), 9463 ConstantEvolutionLoopExitValue( 9464 std::move(Arg.ConstantEvolutionLoopExitValue)), 9465 ValuesAtScopes(std::move(Arg.ValuesAtScopes)), 9466 LoopDispositions(std::move(Arg.LoopDispositions)), 9467 BlockDispositions(std::move(Arg.BlockDispositions)), 9468 UnsignedRanges(std::move(Arg.UnsignedRanges)), 9469 SignedRanges(std::move(Arg.SignedRanges)), 9470 UniqueSCEVs(std::move(Arg.UniqueSCEVs)), 9471 UniquePreds(std::move(Arg.UniquePreds)), 9472 SCEVAllocator(std::move(Arg.SCEVAllocator)), 9473 FirstUnknown(Arg.FirstUnknown) { 9474 Arg.FirstUnknown = nullptr; 9475 } 9476 9477 ScalarEvolution::~ScalarEvolution() { 9478 // Iterate through all the SCEVUnknown instances and call their 9479 // destructors, so that they release their references to their values. 9480 for (SCEVUnknown *U = FirstUnknown; U;) { 9481 SCEVUnknown *Tmp = U; 9482 U = U->Next; 9483 Tmp->~SCEVUnknown(); 9484 } 9485 FirstUnknown = nullptr; 9486 9487 ExprValueMap.clear(); 9488 ValueExprMap.clear(); 9489 HasRecMap.clear(); 9490 9491 // Free any extra memory created for ExitNotTakenInfo in the unlikely event 9492 // that a loop had multiple computable exits. 9493 for (auto &BTCI : BackedgeTakenCounts) 9494 BTCI.second.clear(); 9495 for (auto &BTCI : PredicatedBackedgeTakenCounts) 9496 BTCI.second.clear(); 9497 9498 assert(PendingLoopPredicates.empty() && "isImpliedCond garbage"); 9499 assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!"); 9500 assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!"); 9501 } 9502 9503 bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { 9504 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); 9505 } 9506 9507 static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, 9508 const Loop *L) { 9509 // Print all inner loops first 9510 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) 9511 PrintLoopInfo(OS, SE, *I); 9512 9513 OS << "Loop "; 9514 L->getHeader()->printAsOperand(OS, /*PrintType=*/false); 9515 OS << ": "; 9516 9517 SmallVector<BasicBlock *, 8> ExitBlocks; 9518 L->getExitBlocks(ExitBlocks); 9519 if (ExitBlocks.size() != 1) 9520 OS << "<multiple exits> "; 9521 9522 if (SE->hasLoopInvariantBackedgeTakenCount(L)) { 9523 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); 9524 } else { 9525 OS << "Unpredictable backedge-taken count. "; 9526 } 9527 9528 OS << "\n" 9529 "Loop "; 9530 L->getHeader()->printAsOperand(OS, /*PrintType=*/false); 9531 OS << ": "; 9532 9533 if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { 9534 OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); 9535 } else { 9536 OS << "Unpredictable max backedge-taken count. "; 9537 } 9538 9539 OS << "\n" 9540 "Loop "; 9541 L->getHeader()->printAsOperand(OS, /*PrintType=*/false); 9542 OS << ": "; 9543 9544 SCEVUnionPredicate Pred; 9545 auto PBT = SE->getPredicatedBackedgeTakenCount(L, Pred); 9546 if (!isa<SCEVCouldNotCompute>(PBT)) { 9547 OS << "Predicated backedge-taken count is " << *PBT << "\n"; 9548 OS << " Predicates:\n"; 9549 Pred.print(OS, 4); 9550 } else { 9551 OS << "Unpredictable predicated backedge-taken count. "; 9552 } 9553 OS << "\n"; 9554 } 9555 9556 void ScalarEvolution::print(raw_ostream &OS) const { 9557 // ScalarEvolution's implementation of the print method is to print 9558 // out SCEV values of all instructions that are interesting. Doing 9559 // this potentially causes it to create new SCEV objects though, 9560 // which technically conflicts with the const qualifier. This isn't 9561 // observable from outside the class though, so casting away the 9562 // const isn't dangerous. 9563 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); 9564 9565 OS << "Classifying expressions for: "; 9566 F.printAsOperand(OS, /*PrintType=*/false); 9567 OS << "\n"; 9568 for (Instruction &I : instructions(F)) 9569 if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) { 9570 OS << I << '\n'; 9571 OS << " --> "; 9572 const SCEV *SV = SE.getSCEV(&I); 9573 SV->print(OS); 9574 if (!isa<SCEVCouldNotCompute>(SV)) { 9575 OS << " U: "; 9576 SE.getUnsignedRange(SV).print(OS); 9577 OS << " S: "; 9578 SE.getSignedRange(SV).print(OS); 9579 } 9580 9581 const Loop *L = LI.getLoopFor(I.getParent()); 9582 9583 const SCEV *AtUse = SE.getSCEVAtScope(SV, L); 9584 if (AtUse != SV) { 9585 OS << " --> "; 9586 AtUse->print(OS); 9587 if (!isa<SCEVCouldNotCompute>(AtUse)) { 9588 OS << " U: "; 9589 SE.getUnsignedRange(AtUse).print(OS); 9590 OS << " S: "; 9591 SE.getSignedRange(AtUse).print(OS); 9592 } 9593 } 9594 9595 if (L) { 9596 OS << "\t\t" "Exits: "; 9597 const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); 9598 if (!SE.isLoopInvariant(ExitValue, L)) { 9599 OS << "<<Unknown>>"; 9600 } else { 9601 OS << *ExitValue; 9602 } 9603 } 9604 9605 OS << "\n"; 9606 } 9607 9608 OS << "Determining loop execution counts for: "; 9609 F.printAsOperand(OS, /*PrintType=*/false); 9610 OS << "\n"; 9611 for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I) 9612 PrintLoopInfo(OS, &SE, *I); 9613 } 9614 9615 ScalarEvolution::LoopDisposition 9616 ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) { 9617 auto &Values = LoopDispositions[S]; 9618 for (auto &V : Values) { 9619 if (V.getPointer() == L) 9620 return V.getInt(); 9621 } 9622 Values.emplace_back(L, LoopVariant); 9623 LoopDisposition D = computeLoopDisposition(S, L); 9624 auto &Values2 = LoopDispositions[S]; 9625 for (auto &V : make_range(Values2.rbegin(), Values2.rend())) { 9626 if (V.getPointer() == L) { 9627 V.setInt(D); 9628 break; 9629 } 9630 } 9631 return D; 9632 } 9633 9634 ScalarEvolution::LoopDisposition 9635 ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) { 9636 switch (static_cast<SCEVTypes>(S->getSCEVType())) { 9637 case scConstant: 9638 return LoopInvariant; 9639 case scTruncate: 9640 case scZeroExtend: 9641 case scSignExtend: 9642 return getLoopDisposition(cast<SCEVCastExpr>(S)->getOperand(), L); 9643 case scAddRecExpr: { 9644 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); 9645 9646 // If L is the addrec's loop, it's computable. 9647 if (AR->getLoop() == L) 9648 return LoopComputable; 9649 9650 // Add recurrences are never invariant in the function-body (null loop). 9651 if (!L) 9652 return LoopVariant; 9653 9654 // This recurrence is variant w.r.t. L if L contains AR's loop. 9655 if (L->contains(AR->getLoop())) 9656 return LoopVariant; 9657 9658 // This recurrence is invariant w.r.t. L if AR's loop contains L. 9659 if (AR->getLoop()->contains(L)) 9660 return LoopInvariant; 9661 9662 // This recurrence is variant w.r.t. L if any of its operands 9663 // are variant. 9664 for (auto *Op : AR->operands()) 9665 if (!isLoopInvariant(Op, L)) 9666 return LoopVariant; 9667 9668 // Otherwise it's loop-invariant. 9669 return LoopInvariant; 9670 } 9671 case scAddExpr: 9672 case scMulExpr: 9673 case scUMaxExpr: 9674 case scSMaxExpr: { 9675 bool HasVarying = false; 9676 for (auto *Op : cast<SCEVNAryExpr>(S)->operands()) { 9677 LoopDisposition D = getLoopDisposition(Op, L); 9678 if (D == LoopVariant) 9679 return LoopVariant; 9680 if (D == LoopComputable) 9681 HasVarying = true; 9682 } 9683 return HasVarying ? LoopComputable : LoopInvariant; 9684 } 9685 case scUDivExpr: { 9686 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); 9687 LoopDisposition LD = getLoopDisposition(UDiv->getLHS(), L); 9688 if (LD == LoopVariant) 9689 return LoopVariant; 9690 LoopDisposition RD = getLoopDisposition(UDiv->getRHS(), L); 9691 if (RD == LoopVariant) 9692 return LoopVariant; 9693 return (LD == LoopInvariant && RD == LoopInvariant) ? 9694 LoopInvariant : LoopComputable; 9695 } 9696 case scUnknown: 9697 // All non-instruction values are loop invariant. All instructions are loop 9698 // invariant if they are not contained in the specified loop. 9699 // Instructions are never considered invariant in the function body 9700 // (null loop) because they are defined within the "loop". 9701 if (auto *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) 9702 return (L && !L->contains(I)) ? LoopInvariant : LoopVariant; 9703 return LoopInvariant; 9704 case scCouldNotCompute: 9705 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); 9706 } 9707 llvm_unreachable("Unknown SCEV kind!"); 9708 } 9709 9710 bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) { 9711 return getLoopDisposition(S, L) == LoopInvariant; 9712 } 9713 9714 bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) { 9715 return getLoopDisposition(S, L) == LoopComputable; 9716 } 9717 9718 ScalarEvolution::BlockDisposition 9719 ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) { 9720 auto &Values = BlockDispositions[S]; 9721 for (auto &V : Values) { 9722 if (V.getPointer() == BB) 9723 return V.getInt(); 9724 } 9725 Values.emplace_back(BB, DoesNotDominateBlock); 9726 BlockDisposition D = computeBlockDisposition(S, BB); 9727 auto &Values2 = BlockDispositions[S]; 9728 for (auto &V : make_range(Values2.rbegin(), Values2.rend())) { 9729 if (V.getPointer() == BB) { 9730 V.setInt(D); 9731 break; 9732 } 9733 } 9734 return D; 9735 } 9736 9737 ScalarEvolution::BlockDisposition 9738 ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) { 9739 switch (static_cast<SCEVTypes>(S->getSCEVType())) { 9740 case scConstant: 9741 return ProperlyDominatesBlock; 9742 case scTruncate: 9743 case scZeroExtend: 9744 case scSignExtend: 9745 return getBlockDisposition(cast<SCEVCastExpr>(S)->getOperand(), BB); 9746 case scAddRecExpr: { 9747 // This uses a "dominates" query instead of "properly dominates" query 9748 // to test for proper dominance too, because the instruction which 9749 // produces the addrec's value is a PHI, and a PHI effectively properly 9750 // dominates its entire containing block. 9751 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S); 9752 if (!DT.dominates(AR->getLoop()->getHeader(), BB)) 9753 return DoesNotDominateBlock; 9754 } 9755 // FALL THROUGH into SCEVNAryExpr handling. 9756 case scAddExpr: 9757 case scMulExpr: 9758 case scUMaxExpr: 9759 case scSMaxExpr: { 9760 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S); 9761 bool Proper = true; 9762 for (const SCEV *NAryOp : NAry->operands()) { 9763 BlockDisposition D = getBlockDisposition(NAryOp, BB); 9764 if (D == DoesNotDominateBlock) 9765 return DoesNotDominateBlock; 9766 if (D == DominatesBlock) 9767 Proper = false; 9768 } 9769 return Proper ? ProperlyDominatesBlock : DominatesBlock; 9770 } 9771 case scUDivExpr: { 9772 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S); 9773 const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS(); 9774 BlockDisposition LD = getBlockDisposition(LHS, BB); 9775 if (LD == DoesNotDominateBlock) 9776 return DoesNotDominateBlock; 9777 BlockDisposition RD = getBlockDisposition(RHS, BB); 9778 if (RD == DoesNotDominateBlock) 9779 return DoesNotDominateBlock; 9780 return (LD == ProperlyDominatesBlock && RD == ProperlyDominatesBlock) ? 9781 ProperlyDominatesBlock : DominatesBlock; 9782 } 9783 case scUnknown: 9784 if (Instruction *I = 9785 dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) { 9786 if (I->getParent() == BB) 9787 return DominatesBlock; 9788 if (DT.properlyDominates(I->getParent(), BB)) 9789 return ProperlyDominatesBlock; 9790 return DoesNotDominateBlock; 9791 } 9792 return ProperlyDominatesBlock; 9793 case scCouldNotCompute: 9794 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!"); 9795 } 9796 llvm_unreachable("Unknown SCEV kind!"); 9797 } 9798 9799 bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) { 9800 return getBlockDisposition(S, BB) >= DominatesBlock; 9801 } 9802 9803 bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) { 9804 return getBlockDisposition(S, BB) == ProperlyDominatesBlock; 9805 } 9806 9807 bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const { 9808 // Search for a SCEV expression node within an expression tree. 9809 // Implements SCEVTraversal::Visitor. 9810 struct SCEVSearch { 9811 const SCEV *Node; 9812 bool IsFound; 9813 9814 SCEVSearch(const SCEV *N): Node(N), IsFound(false) {} 9815 9816 bool follow(const SCEV *S) { 9817 IsFound |= (S == Node); 9818 return !IsFound; 9819 } 9820 bool isDone() const { return IsFound; } 9821 }; 9822 9823 SCEVSearch Search(Op); 9824 visitAll(S, Search); 9825 return Search.IsFound; 9826 } 9827 9828 void ScalarEvolution::forgetMemoizedResults(const SCEV *S) { 9829 ValuesAtScopes.erase(S); 9830 LoopDispositions.erase(S); 9831 BlockDispositions.erase(S); 9832 UnsignedRanges.erase(S); 9833 SignedRanges.erase(S); 9834 ExprValueMap.erase(S); 9835 HasRecMap.erase(S); 9836 9837 auto RemoveSCEVFromBackedgeMap = 9838 [S, this](DenseMap<const Loop *, BackedgeTakenInfo> &Map) { 9839 for (auto I = Map.begin(), E = Map.end(); I != E;) { 9840 BackedgeTakenInfo &BEInfo = I->second; 9841 if (BEInfo.hasOperand(S, this)) { 9842 BEInfo.clear(); 9843 Map.erase(I++); 9844 } else 9845 ++I; 9846 } 9847 }; 9848 9849 RemoveSCEVFromBackedgeMap(BackedgeTakenCounts); 9850 RemoveSCEVFromBackedgeMap(PredicatedBackedgeTakenCounts); 9851 } 9852 9853 typedef DenseMap<const Loop *, std::string> VerifyMap; 9854 9855 /// replaceSubString - Replaces all occurrences of From in Str with To. 9856 static void replaceSubString(std::string &Str, StringRef From, StringRef To) { 9857 size_t Pos = 0; 9858 while ((Pos = Str.find(From, Pos)) != std::string::npos) { 9859 Str.replace(Pos, From.size(), To.data(), To.size()); 9860 Pos += To.size(); 9861 } 9862 } 9863 9864 /// getLoopBackedgeTakenCounts - Helper method for verifyAnalysis. 9865 static void 9866 getLoopBackedgeTakenCounts(Loop *L, VerifyMap &Map, ScalarEvolution &SE) { 9867 std::string &S = Map[L]; 9868 if (S.empty()) { 9869 raw_string_ostream OS(S); 9870 SE.getBackedgeTakenCount(L)->print(OS); 9871 9872 // false and 0 are semantically equivalent. This can happen in dead loops. 9873 replaceSubString(OS.str(), "false", "0"); 9874 // Remove wrap flags, their use in SCEV is highly fragile. 9875 // FIXME: Remove this when SCEV gets smarter about them. 9876 replaceSubString(OS.str(), "<nw>", ""); 9877 replaceSubString(OS.str(), "<nsw>", ""); 9878 replaceSubString(OS.str(), "<nuw>", ""); 9879 } 9880 9881 for (auto *R : reverse(*L)) 9882 getLoopBackedgeTakenCounts(R, Map, SE); // recurse. 9883 } 9884 9885 void ScalarEvolution::verify() const { 9886 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this); 9887 9888 // Gather stringified backedge taken counts for all loops using SCEV's caches. 9889 // FIXME: It would be much better to store actual values instead of strings, 9890 // but SCEV pointers will change if we drop the caches. 9891 VerifyMap BackedgeDumpsOld, BackedgeDumpsNew; 9892 for (LoopInfo::reverse_iterator I = LI.rbegin(), E = LI.rend(); I != E; ++I) 9893 getLoopBackedgeTakenCounts(*I, BackedgeDumpsOld, SE); 9894 9895 // Gather stringified backedge taken counts for all loops using a fresh 9896 // ScalarEvolution object. 9897 ScalarEvolution SE2(F, TLI, AC, DT, LI); 9898 for (LoopInfo::reverse_iterator I = LI.rbegin(), E = LI.rend(); I != E; ++I) 9899 getLoopBackedgeTakenCounts(*I, BackedgeDumpsNew, SE2); 9900 9901 // Now compare whether they're the same with and without caches. This allows 9902 // verifying that no pass changed the cache. 9903 assert(BackedgeDumpsOld.size() == BackedgeDumpsNew.size() && 9904 "New loops suddenly appeared!"); 9905 9906 for (VerifyMap::iterator OldI = BackedgeDumpsOld.begin(), 9907 OldE = BackedgeDumpsOld.end(), 9908 NewI = BackedgeDumpsNew.begin(); 9909 OldI != OldE; ++OldI, ++NewI) { 9910 assert(OldI->first == NewI->first && "Loop order changed!"); 9911 9912 // Compare the stringified SCEVs. We don't care if undef backedgetaken count 9913 // changes. 9914 // FIXME: We currently ignore SCEV changes from/to CouldNotCompute. This 9915 // means that a pass is buggy or SCEV has to learn a new pattern but is 9916 // usually not harmful. 9917 if (OldI->second != NewI->second && 9918 OldI->second.find("undef") == std::string::npos && 9919 NewI->second.find("undef") == std::string::npos && 9920 OldI->second != "***COULDNOTCOMPUTE***" && 9921 NewI->second != "***COULDNOTCOMPUTE***") { 9922 dbgs() << "SCEVValidator: SCEV for loop '" 9923 << OldI->first->getHeader()->getName() 9924 << "' changed from '" << OldI->second 9925 << "' to '" << NewI->second << "'!\n"; 9926 std::abort(); 9927 } 9928 } 9929 9930 // TODO: Verify more things. 9931 } 9932 9933 char ScalarEvolutionAnalysis::PassID; 9934 9935 ScalarEvolution ScalarEvolutionAnalysis::run(Function &F, 9936 AnalysisManager<Function> &AM) { 9937 return ScalarEvolution(F, AM.getResult<TargetLibraryAnalysis>(F), 9938 AM.getResult<AssumptionAnalysis>(F), 9939 AM.getResult<DominatorTreeAnalysis>(F), 9940 AM.getResult<LoopAnalysis>(F)); 9941 } 9942 9943 PreservedAnalyses 9944 ScalarEvolutionPrinterPass::run(Function &F, AnalysisManager<Function> &AM) { 9945 AM.getResult<ScalarEvolutionAnalysis>(F).print(OS); 9946 return PreservedAnalyses::all(); 9947 } 9948 9949 INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution", 9950 "Scalar Evolution Analysis", false, true) 9951 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 9952 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 9953 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 9954 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 9955 INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution", 9956 "Scalar Evolution Analysis", false, true) 9957 char ScalarEvolutionWrapperPass::ID = 0; 9958 9959 ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() : FunctionPass(ID) { 9960 initializeScalarEvolutionWrapperPassPass(*PassRegistry::getPassRegistry()); 9961 } 9962 9963 bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) { 9964 SE.reset(new ScalarEvolution( 9965 F, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(), 9966 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F), 9967 getAnalysis<DominatorTreeWrapperPass>().getDomTree(), 9968 getAnalysis<LoopInfoWrapperPass>().getLoopInfo())); 9969 return false; 9970 } 9971 9972 void ScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); } 9973 9974 void ScalarEvolutionWrapperPass::print(raw_ostream &OS, const Module *) const { 9975 SE->print(OS); 9976 } 9977 9978 void ScalarEvolutionWrapperPass::verifyAnalysis() const { 9979 if (!VerifySCEV) 9980 return; 9981 9982 SE->verify(); 9983 } 9984 9985 void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 9986 AU.setPreservesAll(); 9987 AU.addRequiredTransitive<AssumptionCacheTracker>(); 9988 AU.addRequiredTransitive<LoopInfoWrapperPass>(); 9989 AU.addRequiredTransitive<DominatorTreeWrapperPass>(); 9990 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>(); 9991 } 9992 9993 const SCEVPredicate * 9994 ScalarEvolution::getEqualPredicate(const SCEVUnknown *LHS, 9995 const SCEVConstant *RHS) { 9996 FoldingSetNodeID ID; 9997 // Unique this node based on the arguments 9998 ID.AddInteger(SCEVPredicate::P_Equal); 9999 ID.AddPointer(LHS); 10000 ID.AddPointer(RHS); 10001 void *IP = nullptr; 10002 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP)) 10003 return S; 10004 SCEVEqualPredicate *Eq = new (SCEVAllocator) 10005 SCEVEqualPredicate(ID.Intern(SCEVAllocator), LHS, RHS); 10006 UniquePreds.InsertNode(Eq, IP); 10007 return Eq; 10008 } 10009 10010 const SCEVPredicate *ScalarEvolution::getWrapPredicate( 10011 const SCEVAddRecExpr *AR, 10012 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) { 10013 FoldingSetNodeID ID; 10014 // Unique this node based on the arguments 10015 ID.AddInteger(SCEVPredicate::P_Wrap); 10016 ID.AddPointer(AR); 10017 ID.AddInteger(AddedFlags); 10018 void *IP = nullptr; 10019 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP)) 10020 return S; 10021 auto *OF = new (SCEVAllocator) 10022 SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags); 10023 UniquePreds.InsertNode(OF, IP); 10024 return OF; 10025 } 10026 10027 namespace { 10028 10029 class SCEVPredicateRewriter : public SCEVRewriteVisitor<SCEVPredicateRewriter> { 10030 public: 10031 // Rewrites \p S in the context of a loop L and the predicate A. 10032 // If Assume is true, rewrite is free to add further predicates to A 10033 // such that the result will be an AddRecExpr. 10034 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE, 10035 SCEVUnionPredicate &A, bool Assume) { 10036 SCEVPredicateRewriter Rewriter(L, SE, A, Assume); 10037 return Rewriter.visit(S); 10038 } 10039 10040 SCEVPredicateRewriter(const Loop *L, ScalarEvolution &SE, 10041 SCEVUnionPredicate &P, bool Assume) 10042 : SCEVRewriteVisitor(SE), P(P), L(L), Assume(Assume) {} 10043 10044 const SCEV *visitUnknown(const SCEVUnknown *Expr) { 10045 auto ExprPreds = P.getPredicatesForExpr(Expr); 10046 for (auto *Pred : ExprPreds) 10047 if (const auto *IPred = dyn_cast<const SCEVEqualPredicate>(Pred)) 10048 if (IPred->getLHS() == Expr) 10049 return IPred->getRHS(); 10050 10051 return Expr; 10052 } 10053 10054 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { 10055 const SCEV *Operand = visit(Expr->getOperand()); 10056 const SCEVAddRecExpr *AR = dyn_cast<const SCEVAddRecExpr>(Operand); 10057 if (AR && AR->getLoop() == L && AR->isAffine()) { 10058 // This couldn't be folded because the operand didn't have the nuw 10059 // flag. Add the nusw flag as an assumption that we could make. 10060 const SCEV *Step = AR->getStepRecurrence(SE); 10061 Type *Ty = Expr->getType(); 10062 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNUSW)) 10063 return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty), 10064 SE.getSignExtendExpr(Step, Ty), L, 10065 AR->getNoWrapFlags()); 10066 } 10067 return SE.getZeroExtendExpr(Operand, Expr->getType()); 10068 } 10069 10070 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { 10071 const SCEV *Operand = visit(Expr->getOperand()); 10072 const SCEVAddRecExpr *AR = dyn_cast<const SCEVAddRecExpr>(Operand); 10073 if (AR && AR->getLoop() == L && AR->isAffine()) { 10074 // This couldn't be folded because the operand didn't have the nsw 10075 // flag. Add the nssw flag as an assumption that we could make. 10076 const SCEV *Step = AR->getStepRecurrence(SE); 10077 Type *Ty = Expr->getType(); 10078 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNSSW)) 10079 return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty), 10080 SE.getSignExtendExpr(Step, Ty), L, 10081 AR->getNoWrapFlags()); 10082 } 10083 return SE.getSignExtendExpr(Operand, Expr->getType()); 10084 } 10085 10086 private: 10087 bool addOverflowAssumption(const SCEVAddRecExpr *AR, 10088 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) { 10089 auto *A = SE.getWrapPredicate(AR, AddedFlags); 10090 if (!Assume) { 10091 // Check if we've already made this assumption. 10092 if (P.implies(A)) 10093 return true; 10094 return false; 10095 } 10096 P.add(A); 10097 return true; 10098 } 10099 10100 SCEVUnionPredicate &P; 10101 const Loop *L; 10102 bool Assume; 10103 }; 10104 } // end anonymous namespace 10105 10106 const SCEV *ScalarEvolution::rewriteUsingPredicate(const SCEV *S, const Loop *L, 10107 SCEVUnionPredicate &Preds) { 10108 return SCEVPredicateRewriter::rewrite(S, L, *this, Preds, false); 10109 } 10110 10111 const SCEVAddRecExpr * 10112 ScalarEvolution::convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L, 10113 SCEVUnionPredicate &Preds) { 10114 SCEVUnionPredicate TransformPreds; 10115 S = SCEVPredicateRewriter::rewrite(S, L, *this, TransformPreds, true); 10116 auto *AddRec = dyn_cast<SCEVAddRecExpr>(S); 10117 10118 if (!AddRec) 10119 return nullptr; 10120 10121 // Since the transformation was successful, we can now transfer the SCEV 10122 // predicates. 10123 Preds.add(&TransformPreds); 10124 return AddRec; 10125 } 10126 10127 /// SCEV predicates 10128 SCEVPredicate::SCEVPredicate(const FoldingSetNodeIDRef ID, 10129 SCEVPredicateKind Kind) 10130 : FastID(ID), Kind(Kind) {} 10131 10132 SCEVEqualPredicate::SCEVEqualPredicate(const FoldingSetNodeIDRef ID, 10133 const SCEVUnknown *LHS, 10134 const SCEVConstant *RHS) 10135 : SCEVPredicate(ID, P_Equal), LHS(LHS), RHS(RHS) {} 10136 10137 bool SCEVEqualPredicate::implies(const SCEVPredicate *N) const { 10138 const auto *Op = dyn_cast<const SCEVEqualPredicate>(N); 10139 10140 if (!Op) 10141 return false; 10142 10143 return Op->LHS == LHS && Op->RHS == RHS; 10144 } 10145 10146 bool SCEVEqualPredicate::isAlwaysTrue() const { return false; } 10147 10148 const SCEV *SCEVEqualPredicate::getExpr() const { return LHS; } 10149 10150 void SCEVEqualPredicate::print(raw_ostream &OS, unsigned Depth) const { 10151 OS.indent(Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n"; 10152 } 10153 10154 SCEVWrapPredicate::SCEVWrapPredicate(const FoldingSetNodeIDRef ID, 10155 const SCEVAddRecExpr *AR, 10156 IncrementWrapFlags Flags) 10157 : SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {} 10158 10159 const SCEV *SCEVWrapPredicate::getExpr() const { return AR; } 10160 10161 bool SCEVWrapPredicate::implies(const SCEVPredicate *N) const { 10162 const auto *Op = dyn_cast<SCEVWrapPredicate>(N); 10163 10164 return Op && Op->AR == AR && setFlags(Flags, Op->Flags) == Flags; 10165 } 10166 10167 bool SCEVWrapPredicate::isAlwaysTrue() const { 10168 SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags(); 10169 IncrementWrapFlags IFlags = Flags; 10170 10171 if (ScalarEvolution::setFlags(ScevFlags, SCEV::FlagNSW) == ScevFlags) 10172 IFlags = clearFlags(IFlags, IncrementNSSW); 10173 10174 return IFlags == IncrementAnyWrap; 10175 } 10176 10177 void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const { 10178 OS.indent(Depth) << *getExpr() << " Added Flags: "; 10179 if (SCEVWrapPredicate::IncrementNUSW & getFlags()) 10180 OS << "<nusw>"; 10181 if (SCEVWrapPredicate::IncrementNSSW & getFlags()) 10182 OS << "<nssw>"; 10183 OS << "\n"; 10184 } 10185 10186 SCEVWrapPredicate::IncrementWrapFlags 10187 SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR, 10188 ScalarEvolution &SE) { 10189 IncrementWrapFlags ImpliedFlags = IncrementAnyWrap; 10190 SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags(); 10191 10192 // We can safely transfer the NSW flag as NSSW. 10193 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNSW) == StaticFlags) 10194 ImpliedFlags = IncrementNSSW; 10195 10196 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNUW) == StaticFlags) { 10197 // If the increment is positive, the SCEV NUW flag will also imply the 10198 // WrapPredicate NUSW flag. 10199 if (const auto *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE))) 10200 if (Step->getValue()->getValue().isNonNegative()) 10201 ImpliedFlags = setFlags(ImpliedFlags, IncrementNUSW); 10202 } 10203 10204 return ImpliedFlags; 10205 } 10206 10207 /// Union predicates don't get cached so create a dummy set ID for it. 10208 SCEVUnionPredicate::SCEVUnionPredicate() 10209 : SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) {} 10210 10211 bool SCEVUnionPredicate::isAlwaysTrue() const { 10212 return all_of(Preds, 10213 [](const SCEVPredicate *I) { return I->isAlwaysTrue(); }); 10214 } 10215 10216 ArrayRef<const SCEVPredicate *> 10217 SCEVUnionPredicate::getPredicatesForExpr(const SCEV *Expr) { 10218 auto I = SCEVToPreds.find(Expr); 10219 if (I == SCEVToPreds.end()) 10220 return ArrayRef<const SCEVPredicate *>(); 10221 return I->second; 10222 } 10223 10224 bool SCEVUnionPredicate::implies(const SCEVPredicate *N) const { 10225 if (const auto *Set = dyn_cast<const SCEVUnionPredicate>(N)) 10226 return all_of(Set->Preds, 10227 [this](const SCEVPredicate *I) { return this->implies(I); }); 10228 10229 auto ScevPredsIt = SCEVToPreds.find(N->getExpr()); 10230 if (ScevPredsIt == SCEVToPreds.end()) 10231 return false; 10232 auto &SCEVPreds = ScevPredsIt->second; 10233 10234 return any_of(SCEVPreds, 10235 [N](const SCEVPredicate *I) { return I->implies(N); }); 10236 } 10237 10238 const SCEV *SCEVUnionPredicate::getExpr() const { return nullptr; } 10239 10240 void SCEVUnionPredicate::print(raw_ostream &OS, unsigned Depth) const { 10241 for (auto Pred : Preds) 10242 Pred->print(OS, Depth); 10243 } 10244 10245 void SCEVUnionPredicate::add(const SCEVPredicate *N) { 10246 if (const auto *Set = dyn_cast<const SCEVUnionPredicate>(N)) { 10247 for (auto Pred : Set->Preds) 10248 add(Pred); 10249 return; 10250 } 10251 10252 if (implies(N)) 10253 return; 10254 10255 const SCEV *Key = N->getExpr(); 10256 assert(Key && "Only SCEVUnionPredicate doesn't have an " 10257 " associated expression!"); 10258 10259 SCEVToPreds[Key].push_back(N); 10260 Preds.push_back(N); 10261 } 10262 10263 PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE, 10264 Loop &L) 10265 : SE(SE), L(L), Generation(0), BackedgeCount(nullptr) {} 10266 10267 const SCEV *PredicatedScalarEvolution::getSCEV(Value *V) { 10268 const SCEV *Expr = SE.getSCEV(V); 10269 RewriteEntry &Entry = RewriteMap[Expr]; 10270 10271 // If we already have an entry and the version matches, return it. 10272 if (Entry.second && Generation == Entry.first) 10273 return Entry.second; 10274 10275 // We found an entry but it's stale. Rewrite the stale entry 10276 // acording to the current predicate. 10277 if (Entry.second) 10278 Expr = Entry.second; 10279 10280 const SCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, Preds); 10281 Entry = {Generation, NewSCEV}; 10282 10283 return NewSCEV; 10284 } 10285 10286 const SCEV *PredicatedScalarEvolution::getBackedgeTakenCount() { 10287 if (!BackedgeCount) { 10288 SCEVUnionPredicate BackedgePred; 10289 BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, BackedgePred); 10290 addPredicate(BackedgePred); 10291 } 10292 return BackedgeCount; 10293 } 10294 10295 void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) { 10296 if (Preds.implies(&Pred)) 10297 return; 10298 Preds.add(&Pred); 10299 updateGeneration(); 10300 } 10301 10302 const SCEVUnionPredicate &PredicatedScalarEvolution::getUnionPredicate() const { 10303 return Preds; 10304 } 10305 10306 void PredicatedScalarEvolution::updateGeneration() { 10307 // If the generation number wrapped recompute everything. 10308 if (++Generation == 0) { 10309 for (auto &II : RewriteMap) { 10310 const SCEV *Rewritten = II.second.second; 10311 II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, Preds)}; 10312 } 10313 } 10314 } 10315 10316 void PredicatedScalarEvolution::setNoOverflow( 10317 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) { 10318 const SCEV *Expr = getSCEV(V); 10319 const auto *AR = cast<SCEVAddRecExpr>(Expr); 10320 10321 auto ImpliedFlags = SCEVWrapPredicate::getImpliedFlags(AR, SE); 10322 10323 // Clear the statically implied flags. 10324 Flags = SCEVWrapPredicate::clearFlags(Flags, ImpliedFlags); 10325 addPredicate(*SE.getWrapPredicate(AR, Flags)); 10326 10327 auto II = FlagsMap.insert({V, Flags}); 10328 if (!II.second) 10329 II.first->second = SCEVWrapPredicate::setFlags(Flags, II.first->second); 10330 } 10331 10332 bool PredicatedScalarEvolution::hasNoOverflow( 10333 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) { 10334 const SCEV *Expr = getSCEV(V); 10335 const auto *AR = cast<SCEVAddRecExpr>(Expr); 10336 10337 Flags = SCEVWrapPredicate::clearFlags( 10338 Flags, SCEVWrapPredicate::getImpliedFlags(AR, SE)); 10339 10340 auto II = FlagsMap.find(V); 10341 10342 if (II != FlagsMap.end()) 10343 Flags = SCEVWrapPredicate::clearFlags(Flags, II->second); 10344 10345 return Flags == SCEVWrapPredicate::IncrementAnyWrap; 10346 } 10347 10348 const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) { 10349 const SCEV *Expr = this->getSCEV(V); 10350 auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, Preds); 10351 10352 if (!New) 10353 return nullptr; 10354 10355 updateGeneration(); 10356 RewriteMap[SE.getSCEV(V)] = {Generation, New}; 10357 return New; 10358 } 10359 10360 PredicatedScalarEvolution::PredicatedScalarEvolution( 10361 const PredicatedScalarEvolution &Init) 10362 : RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L), Preds(Init.Preds), 10363 Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) { 10364 for (auto I = Init.FlagsMap.begin(), E = Init.FlagsMap.end(); I != E; ++I) 10365 FlagsMap.insert(*I); 10366 } 10367