1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===// 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. These classes are reference counted, managed by the const SCEV* 18 // class. We only create one SCEV of a particular shape, so pointer-comparisons 19 // for equality are legal. 20 // 21 // One important aspect of the SCEV objects is that they are never cyclic, even 22 // if there is a cycle in the dataflow for an expression (ie, a PHI node). If 23 // the PHI node is one of the idioms that we can represent (e.g., a polynomial 24 // recurrence) then we represent it directly as a recurrence node, otherwise we 25 // represent it as a SCEVUnknown node. 26 // 27 // In addition to being able to represent expressions of various types, we also 28 // have folders that are used to build the *canonical* representation for a 29 // particular expression. These folders are capable of using a variety of 30 // rewrite rules to simplify the expressions. 31 // 32 // Once the folders are defined, we can implement the more interesting 33 // higher-level code, such as the code that recognizes PHI nodes of various 34 // types, computes the execution count of a loop, etc. 35 // 36 // TODO: We should use these routines and value representations to implement 37 // dependence analysis! 38 // 39 //===----------------------------------------------------------------------===// 40 // 41 // There are several good references for the techniques used in this analysis. 42 // 43 // Chains of recurrences -- a method to expedite the evaluation 44 // of closed-form functions 45 // Olaf Bachmann, Paul S. Wang, Eugene V. Zima 46 // 47 // On computational properties of chains of recurrences 48 // Eugene V. Zima 49 // 50 // Symbolic Evaluation of Chains of Recurrences for Loop Optimization 51 // Robert A. van Engelen 52 // 53 // Efficient Symbolic Analysis for Optimizing Compilers 54 // Robert A. van Engelen 55 // 56 // Using the chains of recurrences algebra for data dependence testing and 57 // induction variable substitution 58 // MS Thesis, Johnie Birch 59 // 60 //===----------------------------------------------------------------------===// 61 62 #define DEBUG_TYPE "scalar-evolution" 63 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 64 #include "llvm/Constants.h" 65 #include "llvm/DerivedTypes.h" 66 #include "llvm/GlobalVariable.h" 67 #include "llvm/Instructions.h" 68 #include "llvm/Analysis/ConstantFolding.h" 69 #include "llvm/Analysis/Dominators.h" 70 #include "llvm/Analysis/LoopInfo.h" 71 #include "llvm/Analysis/ValueTracking.h" 72 #include "llvm/Assembly/Writer.h" 73 #include "llvm/Target/TargetData.h" 74 #include "llvm/Support/CommandLine.h" 75 #include "llvm/Support/Compiler.h" 76 #include "llvm/Support/ConstantRange.h" 77 #include "llvm/Support/GetElementPtrTypeIterator.h" 78 #include "llvm/Support/InstIterator.h" 79 #include "llvm/Support/MathExtras.h" 80 #include "llvm/Support/raw_ostream.h" 81 #include "llvm/ADT/Statistic.h" 82 #include "llvm/ADT/STLExtras.h" 83 #include <algorithm> 84 using namespace llvm; 85 86 STATISTIC(NumArrayLenItCounts, 87 "Number of trip counts computed with array length"); 88 STATISTIC(NumTripCountsComputed, 89 "Number of loops with predictable loop counts"); 90 STATISTIC(NumTripCountsNotComputed, 91 "Number of loops without predictable loop counts"); 92 STATISTIC(NumBruteForceTripCountsComputed, 93 "Number of loops with trip counts computed by force"); 94 95 static cl::opt<unsigned> 96 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, 97 cl::desc("Maximum number of iterations SCEV will " 98 "symbolically execute a constant " 99 "derived loop"), 100 cl::init(100)); 101 102 static RegisterPass<ScalarEvolution> 103 R("scalar-evolution", "Scalar Evolution Analysis", false, true); 104 char ScalarEvolution::ID = 0; 105 106 //===----------------------------------------------------------------------===// 107 // SCEV class definitions 108 //===----------------------------------------------------------------------===// 109 110 //===----------------------------------------------------------------------===// 111 // Implementation of the SCEV class. 112 // 113 114 SCEV::~SCEV() {} 115 116 void SCEV::dump() const { 117 print(errs()); 118 errs() << '\n'; 119 } 120 121 void SCEV::print(std::ostream &o) const { 122 raw_os_ostream OS(o); 123 print(OS); 124 } 125 126 bool SCEV::isZero() const { 127 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 128 return SC->getValue()->isZero(); 129 return false; 130 } 131 132 bool SCEV::isOne() const { 133 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 134 return SC->getValue()->isOne(); 135 return false; 136 } 137 138 bool SCEV::isAllOnesValue() const { 139 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this)) 140 return SC->getValue()->isAllOnesValue(); 141 return false; 142 } 143 144 SCEVCouldNotCompute::SCEVCouldNotCompute() : 145 SCEV(scCouldNotCompute) {} 146 147 void SCEVCouldNotCompute::Profile(FoldingSetNodeID &ID) const { 148 assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); 149 } 150 151 bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const { 152 assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); 153 return false; 154 } 155 156 const Type *SCEVCouldNotCompute::getType() const { 157 assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); 158 return 0; 159 } 160 161 bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const { 162 assert(0 && "Attempt to use a SCEVCouldNotCompute object!"); 163 return false; 164 } 165 166 const SCEV * 167 SCEVCouldNotCompute::replaceSymbolicValuesWithConcrete( 168 const SCEV *Sym, 169 const SCEV *Conc, 170 ScalarEvolution &SE) const { 171 return this; 172 } 173 174 void SCEVCouldNotCompute::print(raw_ostream &OS) const { 175 OS << "***COULDNOTCOMPUTE***"; 176 } 177 178 bool SCEVCouldNotCompute::classof(const SCEV *S) { 179 return S->getSCEVType() == scCouldNotCompute; 180 } 181 182 const SCEV* ScalarEvolution::getConstant(ConstantInt *V) { 183 FoldingSetNodeID ID; 184 ID.AddInteger(scConstant); 185 ID.AddPointer(V); 186 void *IP = 0; 187 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 188 SCEV *S = SCEVAllocator.Allocate<SCEVConstant>(); 189 new (S) SCEVConstant(V); 190 UniqueSCEVs.InsertNode(S, IP); 191 return S; 192 } 193 194 const SCEV* ScalarEvolution::getConstant(const APInt& Val) { 195 return getConstant(ConstantInt::get(Val)); 196 } 197 198 const SCEV* 199 ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) { 200 return getConstant(ConstantInt::get(cast<IntegerType>(Ty), V, isSigned)); 201 } 202 203 void SCEVConstant::Profile(FoldingSetNodeID &ID) const { 204 ID.AddInteger(scConstant); 205 ID.AddPointer(V); 206 } 207 208 const Type *SCEVConstant::getType() const { return V->getType(); } 209 210 void SCEVConstant::print(raw_ostream &OS) const { 211 WriteAsOperand(OS, V, false); 212 } 213 214 SCEVCastExpr::SCEVCastExpr(unsigned SCEVTy, 215 const SCEV* op, const Type *ty) 216 : SCEV(SCEVTy), Op(op), Ty(ty) {} 217 218 void SCEVCastExpr::Profile(FoldingSetNodeID &ID) const { 219 ID.AddInteger(getSCEVType()); 220 ID.AddPointer(Op); 221 ID.AddPointer(Ty); 222 } 223 224 bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { 225 return Op->dominates(BB, DT); 226 } 227 228 SCEVTruncateExpr::SCEVTruncateExpr(const SCEV* op, const Type *ty) 229 : SCEVCastExpr(scTruncate, op, ty) { 230 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && 231 (Ty->isInteger() || isa<PointerType>(Ty)) && 232 "Cannot truncate non-integer value!"); 233 } 234 235 void SCEVTruncateExpr::print(raw_ostream &OS) const { 236 OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; 237 } 238 239 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEV* op, const Type *ty) 240 : SCEVCastExpr(scZeroExtend, op, ty) { 241 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && 242 (Ty->isInteger() || isa<PointerType>(Ty)) && 243 "Cannot zero extend non-integer value!"); 244 } 245 246 void SCEVZeroExtendExpr::print(raw_ostream &OS) const { 247 OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; 248 } 249 250 SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEV* op, const Type *ty) 251 : SCEVCastExpr(scSignExtend, op, ty) { 252 assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) && 253 (Ty->isInteger() || isa<PointerType>(Ty)) && 254 "Cannot sign extend non-integer value!"); 255 } 256 257 void SCEVSignExtendExpr::print(raw_ostream &OS) const { 258 OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")"; 259 } 260 261 void SCEVCommutativeExpr::print(raw_ostream &OS) const { 262 assert(Operands.size() > 1 && "This plus expr shouldn't exist!"); 263 const char *OpStr = getOperationStr(); 264 OS << "(" << *Operands[0]; 265 for (unsigned i = 1, e = Operands.size(); i != e; ++i) 266 OS << OpStr << *Operands[i]; 267 OS << ")"; 268 } 269 270 const SCEV * 271 SCEVCommutativeExpr::replaceSymbolicValuesWithConcrete( 272 const SCEV *Sym, 273 const SCEV *Conc, 274 ScalarEvolution &SE) const { 275 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 276 const SCEV* H = 277 getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); 278 if (H != getOperand(i)) { 279 SmallVector<const SCEV*, 8> NewOps; 280 NewOps.reserve(getNumOperands()); 281 for (unsigned j = 0; j != i; ++j) 282 NewOps.push_back(getOperand(j)); 283 NewOps.push_back(H); 284 for (++i; i != e; ++i) 285 NewOps.push_back(getOperand(i)-> 286 replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); 287 288 if (isa<SCEVAddExpr>(this)) 289 return SE.getAddExpr(NewOps); 290 else if (isa<SCEVMulExpr>(this)) 291 return SE.getMulExpr(NewOps); 292 else if (isa<SCEVSMaxExpr>(this)) 293 return SE.getSMaxExpr(NewOps); 294 else if (isa<SCEVUMaxExpr>(this)) 295 return SE.getUMaxExpr(NewOps); 296 else 297 assert(0 && "Unknown commutative expr!"); 298 } 299 } 300 return this; 301 } 302 303 void SCEVNAryExpr::Profile(FoldingSetNodeID &ID) const { 304 ID.AddInteger(getSCEVType()); 305 ID.AddInteger(Operands.size()); 306 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 307 ID.AddPointer(Operands[i]); 308 } 309 310 bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { 311 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 312 if (!getOperand(i)->dominates(BB, DT)) 313 return false; 314 } 315 return true; 316 } 317 318 void SCEVUDivExpr::Profile(FoldingSetNodeID &ID) const { 319 ID.AddInteger(scUDivExpr); 320 ID.AddPointer(LHS); 321 ID.AddPointer(RHS); 322 } 323 324 bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const { 325 return LHS->dominates(BB, DT) && RHS->dominates(BB, DT); 326 } 327 328 void SCEVUDivExpr::print(raw_ostream &OS) const { 329 OS << "(" << *LHS << " /u " << *RHS << ")"; 330 } 331 332 const Type *SCEVUDivExpr::getType() const { 333 // In most cases the types of LHS and RHS will be the same, but in some 334 // crazy cases one or the other may be a pointer. ScalarEvolution doesn't 335 // depend on the type for correctness, but handling types carefully can 336 // avoid extra casts in the SCEVExpander. The LHS is more likely to be 337 // a pointer type than the RHS, so use the RHS' type here. 338 return RHS->getType(); 339 } 340 341 void SCEVAddRecExpr::Profile(FoldingSetNodeID &ID) const { 342 ID.AddInteger(scAddRecExpr); 343 ID.AddInteger(Operands.size()); 344 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 345 ID.AddPointer(Operands[i]); 346 ID.AddPointer(L); 347 } 348 349 const SCEV * 350 SCEVAddRecExpr::replaceSymbolicValuesWithConcrete(const SCEV *Sym, 351 const SCEV *Conc, 352 ScalarEvolution &SE) const { 353 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { 354 const SCEV* H = 355 getOperand(i)->replaceSymbolicValuesWithConcrete(Sym, Conc, SE); 356 if (H != getOperand(i)) { 357 SmallVector<const SCEV*, 8> NewOps; 358 NewOps.reserve(getNumOperands()); 359 for (unsigned j = 0; j != i; ++j) 360 NewOps.push_back(getOperand(j)); 361 NewOps.push_back(H); 362 for (++i; i != e; ++i) 363 NewOps.push_back(getOperand(i)-> 364 replaceSymbolicValuesWithConcrete(Sym, Conc, SE)); 365 366 return SE.getAddRecExpr(NewOps, L); 367 } 368 } 369 return this; 370 } 371 372 373 bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const { 374 // Add recurrences are never invariant in the function-body (null loop). 375 if (!QueryLoop) 376 return false; 377 378 // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L. 379 if (QueryLoop->contains(L->getHeader())) 380 return false; 381 382 // This recurrence is variant w.r.t. QueryLoop if any of its operands 383 // are variant. 384 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 385 if (!getOperand(i)->isLoopInvariant(QueryLoop)) 386 return false; 387 388 // Otherwise it's loop-invariant. 389 return true; 390 } 391 392 void SCEVAddRecExpr::print(raw_ostream &OS) const { 393 OS << "{" << *Operands[0]; 394 for (unsigned i = 1, e = Operands.size(); i != e; ++i) 395 OS << ",+," << *Operands[i]; 396 OS << "}<" << L->getHeader()->getName() + ">"; 397 } 398 399 void SCEVUnknown::Profile(FoldingSetNodeID &ID) const { 400 ID.AddInteger(scUnknown); 401 ID.AddPointer(V); 402 } 403 404 bool SCEVUnknown::isLoopInvariant(const Loop *L) const { 405 // All non-instruction values are loop invariant. All instructions are loop 406 // invariant if they are not contained in the specified loop. 407 // Instructions are never considered invariant in the function body 408 // (null loop) because they are defined within the "loop". 409 if (Instruction *I = dyn_cast<Instruction>(V)) 410 return L && !L->contains(I->getParent()); 411 return true; 412 } 413 414 bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const { 415 if (Instruction *I = dyn_cast<Instruction>(getValue())) 416 return DT->dominates(I->getParent(), BB); 417 return true; 418 } 419 420 const Type *SCEVUnknown::getType() const { 421 return V->getType(); 422 } 423 424 void SCEVUnknown::print(raw_ostream &OS) const { 425 WriteAsOperand(OS, V, false); 426 } 427 428 //===----------------------------------------------------------------------===// 429 // SCEV Utilities 430 //===----------------------------------------------------------------------===// 431 432 namespace { 433 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less 434 /// than the complexity of the RHS. This comparator is used to canonicalize 435 /// expressions. 436 class VISIBILITY_HIDDEN SCEVComplexityCompare { 437 LoopInfo *LI; 438 public: 439 explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {} 440 441 bool operator()(const SCEV *LHS, const SCEV *RHS) const { 442 // Primarily, sort the SCEVs by their getSCEVType(). 443 if (LHS->getSCEVType() != RHS->getSCEVType()) 444 return LHS->getSCEVType() < RHS->getSCEVType(); 445 446 // Aside from the getSCEVType() ordering, the particular ordering 447 // isn't very important except that it's beneficial to be consistent, 448 // so that (a + b) and (b + a) don't end up as different expressions. 449 450 // Sort SCEVUnknown values with some loose heuristics. TODO: This is 451 // not as complete as it could be. 452 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) { 453 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS); 454 455 // Order pointer values after integer values. This helps SCEVExpander 456 // form GEPs. 457 if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType())) 458 return false; 459 if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType())) 460 return true; 461 462 // Compare getValueID values. 463 if (LU->getValue()->getValueID() != RU->getValue()->getValueID()) 464 return LU->getValue()->getValueID() < RU->getValue()->getValueID(); 465 466 // Sort arguments by their position. 467 if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) { 468 const Argument *RA = cast<Argument>(RU->getValue()); 469 return LA->getArgNo() < RA->getArgNo(); 470 } 471 472 // For instructions, compare their loop depth, and their opcode. 473 // This is pretty loose. 474 if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) { 475 Instruction *RV = cast<Instruction>(RU->getValue()); 476 477 // Compare loop depths. 478 if (LI->getLoopDepth(LV->getParent()) != 479 LI->getLoopDepth(RV->getParent())) 480 return LI->getLoopDepth(LV->getParent()) < 481 LI->getLoopDepth(RV->getParent()); 482 483 // Compare opcodes. 484 if (LV->getOpcode() != RV->getOpcode()) 485 return LV->getOpcode() < RV->getOpcode(); 486 487 // Compare the number of operands. 488 if (LV->getNumOperands() != RV->getNumOperands()) 489 return LV->getNumOperands() < RV->getNumOperands(); 490 } 491 492 return false; 493 } 494 495 // Compare constant values. 496 if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) { 497 const SCEVConstant *RC = cast<SCEVConstant>(RHS); 498 if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth()) 499 return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth(); 500 return LC->getValue()->getValue().ult(RC->getValue()->getValue()); 501 } 502 503 // Compare addrec loop depths. 504 if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) { 505 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS); 506 if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth()) 507 return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth(); 508 } 509 510 // Lexicographically compare n-ary expressions. 511 if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) { 512 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS); 513 for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) { 514 if (i >= RC->getNumOperands()) 515 return false; 516 if (operator()(LC->getOperand(i), RC->getOperand(i))) 517 return true; 518 if (operator()(RC->getOperand(i), LC->getOperand(i))) 519 return false; 520 } 521 return LC->getNumOperands() < RC->getNumOperands(); 522 } 523 524 // Lexicographically compare udiv expressions. 525 if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) { 526 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS); 527 if (operator()(LC->getLHS(), RC->getLHS())) 528 return true; 529 if (operator()(RC->getLHS(), LC->getLHS())) 530 return false; 531 if (operator()(LC->getRHS(), RC->getRHS())) 532 return true; 533 if (operator()(RC->getRHS(), LC->getRHS())) 534 return false; 535 return false; 536 } 537 538 // Compare cast expressions by operand. 539 if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) { 540 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS); 541 return operator()(LC->getOperand(), RC->getOperand()); 542 } 543 544 assert(0 && "Unknown SCEV kind!"); 545 return false; 546 } 547 }; 548 } 549 550 /// GroupByComplexity - Given a list of SCEV objects, order them by their 551 /// complexity, and group objects of the same complexity together by value. 552 /// When this routine is finished, we know that any duplicates in the vector are 553 /// consecutive and that complexity is monotonically increasing. 554 /// 555 /// Note that we go take special precautions to ensure that we get determinstic 556 /// results from this routine. In other words, we don't want the results of 557 /// this to depend on where the addresses of various SCEV objects happened to 558 /// land in memory. 559 /// 560 static void GroupByComplexity(SmallVectorImpl<const SCEV*> &Ops, 561 LoopInfo *LI) { 562 if (Ops.size() < 2) return; // Noop 563 if (Ops.size() == 2) { 564 // This is the common case, which also happens to be trivially simple. 565 // Special case it. 566 if (SCEVComplexityCompare(LI)(Ops[1], Ops[0])) 567 std::swap(Ops[0], Ops[1]); 568 return; 569 } 570 571 // Do the rough sort by complexity. 572 std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI)); 573 574 // Now that we are sorted by complexity, group elements of the same 575 // complexity. Note that this is, at worst, N^2, but the vector is likely to 576 // be extremely short in practice. Note that we take this approach because we 577 // do not want to depend on the addresses of the objects we are grouping. 578 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) { 579 const SCEV *S = Ops[i]; 580 unsigned Complexity = S->getSCEVType(); 581 582 // If there are any objects of the same complexity and same value as this 583 // one, group them. 584 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) { 585 if (Ops[j] == S) { // Found a duplicate. 586 // Move it to immediately after i'th element. 587 std::swap(Ops[i+1], Ops[j]); 588 ++i; // no need to rescan it. 589 if (i == e-2) return; // Done! 590 } 591 } 592 } 593 } 594 595 596 597 //===----------------------------------------------------------------------===// 598 // Simple SCEV method implementations 599 //===----------------------------------------------------------------------===// 600 601 /// BinomialCoefficient - Compute BC(It, K). The result has width W. 602 /// Assume, K > 0. 603 static const SCEV* BinomialCoefficient(const SCEV* It, unsigned K, 604 ScalarEvolution &SE, 605 const Type* ResultTy) { 606 // Handle the simplest case efficiently. 607 if (K == 1) 608 return SE.getTruncateOrZeroExtend(It, ResultTy); 609 610 // We are using the following formula for BC(It, K): 611 // 612 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K! 613 // 614 // Suppose, W is the bitwidth of the return value. We must be prepared for 615 // overflow. Hence, we must assure that the result of our computation is 616 // equal to the accurate one modulo 2^W. Unfortunately, division isn't 617 // safe in modular arithmetic. 618 // 619 // However, this code doesn't use exactly that formula; the formula it uses 620 // is something like the following, where T is the number of factors of 2 in 621 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is 622 // exponentiation: 623 // 624 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T) 625 // 626 // This formula is trivially equivalent to the previous formula. However, 627 // this formula can be implemented much more efficiently. The trick is that 628 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular 629 // arithmetic. To do exact division in modular arithmetic, all we have 630 // to do is multiply by the inverse. Therefore, this step can be done at 631 // width W. 632 // 633 // The next issue is how to safely do the division by 2^T. The way this 634 // is done is by doing the multiplication step at a width of at least W + T 635 // bits. This way, the bottom W+T bits of the product are accurate. Then, 636 // when we perform the division by 2^T (which is equivalent to a right shift 637 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get 638 // truncated out after the division by 2^T. 639 // 640 // In comparison to just directly using the first formula, this technique 641 // is much more efficient; using the first formula requires W * K bits, 642 // but this formula less than W + K bits. Also, the first formula requires 643 // a division step, whereas this formula only requires multiplies and shifts. 644 // 645 // It doesn't matter whether the subtraction step is done in the calculation 646 // width or the input iteration count's width; if the subtraction overflows, 647 // the result must be zero anyway. We prefer here to do it in the width of 648 // the induction variable because it helps a lot for certain cases; CodeGen 649 // isn't smart enough to ignore the overflow, which leads to much less 650 // efficient code if the width of the subtraction is wider than the native 651 // register width. 652 // 653 // (It's possible to not widen at all by pulling out factors of 2 before 654 // the multiplication; for example, K=2 can be calculated as 655 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires 656 // extra arithmetic, so it's not an obvious win, and it gets 657 // much more complicated for K > 3.) 658 659 // Protection from insane SCEVs; this bound is conservative, 660 // but it probably doesn't matter. 661 if (K > 1000) 662 return SE.getCouldNotCompute(); 663 664 unsigned W = SE.getTypeSizeInBits(ResultTy); 665 666 // Calculate K! / 2^T and T; we divide out the factors of two before 667 // multiplying for calculating K! / 2^T to avoid overflow. 668 // Other overflow doesn't matter because we only care about the bottom 669 // W bits of the result. 670 APInt OddFactorial(W, 1); 671 unsigned T = 1; 672 for (unsigned i = 3; i <= K; ++i) { 673 APInt Mult(W, i); 674 unsigned TwoFactors = Mult.countTrailingZeros(); 675 T += TwoFactors; 676 Mult = Mult.lshr(TwoFactors); 677 OddFactorial *= Mult; 678 } 679 680 // We need at least W + T bits for the multiplication step 681 unsigned CalculationBits = W + T; 682 683 // Calcuate 2^T, at width T+W. 684 APInt DivFactor = APInt(CalculationBits, 1).shl(T); 685 686 // Calculate the multiplicative inverse of K! / 2^T; 687 // this multiplication factor will perform the exact division by 688 // K! / 2^T. 689 APInt Mod = APInt::getSignedMinValue(W+1); 690 APInt MultiplyFactor = OddFactorial.zext(W+1); 691 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod); 692 MultiplyFactor = MultiplyFactor.trunc(W); 693 694 // Calculate the product, at width T+W 695 const IntegerType *CalculationTy = IntegerType::get(CalculationBits); 696 const SCEV* Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy); 697 for (unsigned i = 1; i != K; ++i) { 698 const SCEV* S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType())); 699 Dividend = SE.getMulExpr(Dividend, 700 SE.getTruncateOrZeroExtend(S, CalculationTy)); 701 } 702 703 // Divide by 2^T 704 const SCEV* DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor)); 705 706 // Truncate the result, and divide by K! / 2^T. 707 708 return SE.getMulExpr(SE.getConstant(MultiplyFactor), 709 SE.getTruncateOrZeroExtend(DivResult, ResultTy)); 710 } 711 712 /// evaluateAtIteration - Return the value of this chain of recurrences at 713 /// the specified iteration number. We can evaluate this recurrence by 714 /// multiplying each element in the chain by the binomial coefficient 715 /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as: 716 /// 717 /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3) 718 /// 719 /// where BC(It, k) stands for binomial coefficient. 720 /// 721 const SCEV* SCEVAddRecExpr::evaluateAtIteration(const SCEV* It, 722 ScalarEvolution &SE) const { 723 const SCEV* Result = getStart(); 724 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { 725 // The computation is correct in the face of overflow provided that the 726 // multiplication is performed _after_ the evaluation of the binomial 727 // coefficient. 728 const SCEV* Coeff = BinomialCoefficient(It, i, SE, getType()); 729 if (isa<SCEVCouldNotCompute>(Coeff)) 730 return Coeff; 731 732 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff)); 733 } 734 return Result; 735 } 736 737 //===----------------------------------------------------------------------===// 738 // SCEV Expression folder implementations 739 //===----------------------------------------------------------------------===// 740 741 const SCEV* ScalarEvolution::getTruncateExpr(const SCEV* Op, 742 const Type *Ty) { 743 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) && 744 "This is not a truncating conversion!"); 745 assert(isSCEVable(Ty) && 746 "This is not a conversion to a SCEVable type!"); 747 Ty = getEffectiveSCEVType(Ty); 748 749 // Fold if the operand is constant. 750 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 751 return getConstant( 752 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty))); 753 754 // trunc(trunc(x)) --> trunc(x) 755 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) 756 return getTruncateExpr(ST->getOperand(), Ty); 757 758 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing 759 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) 760 return getTruncateOrSignExtend(SS->getOperand(), Ty); 761 762 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing 763 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) 764 return getTruncateOrZeroExtend(SZ->getOperand(), Ty); 765 766 // If the input value is a chrec scev, truncate the chrec's operands. 767 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) { 768 SmallVector<const SCEV*, 4> Operands; 769 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) 770 Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty)); 771 return getAddRecExpr(Operands, AddRec->getLoop()); 772 } 773 774 FoldingSetNodeID ID; 775 ID.AddInteger(scTruncate); 776 ID.AddPointer(Op); 777 ID.AddPointer(Ty); 778 void *IP = 0; 779 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 780 SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>(); 781 new (S) SCEVTruncateExpr(Op, Ty); 782 UniqueSCEVs.InsertNode(S, IP); 783 return S; 784 } 785 786 const SCEV* ScalarEvolution::getZeroExtendExpr(const SCEV* Op, 787 const Type *Ty) { 788 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 789 "This is not an extending conversion!"); 790 assert(isSCEVable(Ty) && 791 "This is not a conversion to a SCEVable type!"); 792 Ty = getEffectiveSCEVType(Ty); 793 794 // Fold if the operand is constant. 795 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { 796 const Type *IntTy = getEffectiveSCEVType(Ty); 797 Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy); 798 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); 799 return getConstant(cast<ConstantInt>(C)); 800 } 801 802 // zext(zext(x)) --> zext(x) 803 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op)) 804 return getZeroExtendExpr(SZ->getOperand(), Ty); 805 806 // If the input value is a chrec scev, and we can prove that the value 807 // did not overflow the old, smaller, value, we can zero extend all of the 808 // operands (often constants). This allows analysis of something like 809 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; } 810 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) 811 if (AR->isAffine()) { 812 // Check whether the backedge-taken count is SCEVCouldNotCompute. 813 // Note that this serves two purposes: It filters out loops that are 814 // simply not analyzable, and it covers the case where this code is 815 // being called from within backedge-taken count analysis, such that 816 // attempting to ask for the backedge-taken count would likely result 817 // in infinite recursion. In the later case, the analysis code will 818 // cope with a conservative value, and it will take care to purge 819 // that value once it has finished. 820 const SCEV* MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); 821 if (!isa<SCEVCouldNotCompute>(MaxBECount)) { 822 // Manually compute the final value for AR, checking for 823 // overflow. 824 const SCEV* Start = AR->getStart(); 825 const SCEV* Step = AR->getStepRecurrence(*this); 826 827 // Check whether the backedge-taken count can be losslessly casted to 828 // the addrec's type. The count is always unsigned. 829 const SCEV* CastedMaxBECount = 830 getTruncateOrZeroExtend(MaxBECount, Start->getType()); 831 const SCEV* RecastedMaxBECount = 832 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); 833 if (MaxBECount == RecastedMaxBECount) { 834 const Type *WideTy = 835 IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); 836 // Check whether Start+Step*MaxBECount has no unsigned overflow. 837 const SCEV* ZMul = 838 getMulExpr(CastedMaxBECount, 839 getTruncateOrZeroExtend(Step, Start->getType())); 840 const SCEV* Add = getAddExpr(Start, ZMul); 841 const SCEV* OperandExtendedAdd = 842 getAddExpr(getZeroExtendExpr(Start, WideTy), 843 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), 844 getZeroExtendExpr(Step, WideTy))); 845 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) 846 // Return the expression with the addrec on the outside. 847 return getAddRecExpr(getZeroExtendExpr(Start, Ty), 848 getZeroExtendExpr(Step, Ty), 849 AR->getLoop()); 850 851 // Similar to above, only this time treat the step value as signed. 852 // This covers loops that count down. 853 const SCEV* SMul = 854 getMulExpr(CastedMaxBECount, 855 getTruncateOrSignExtend(Step, Start->getType())); 856 Add = getAddExpr(Start, SMul); 857 OperandExtendedAdd = 858 getAddExpr(getZeroExtendExpr(Start, WideTy), 859 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), 860 getSignExtendExpr(Step, WideTy))); 861 if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd) 862 // Return the expression with the addrec on the outside. 863 return getAddRecExpr(getZeroExtendExpr(Start, Ty), 864 getSignExtendExpr(Step, Ty), 865 AR->getLoop()); 866 } 867 } 868 } 869 870 FoldingSetNodeID ID; 871 ID.AddInteger(scZeroExtend); 872 ID.AddPointer(Op); 873 ID.AddPointer(Ty); 874 void *IP = 0; 875 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 876 SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>(); 877 new (S) SCEVZeroExtendExpr(Op, Ty); 878 UniqueSCEVs.InsertNode(S, IP); 879 return S; 880 } 881 882 const SCEV* ScalarEvolution::getSignExtendExpr(const SCEV* Op, 883 const Type *Ty) { 884 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 885 "This is not an extending conversion!"); 886 assert(isSCEVable(Ty) && 887 "This is not a conversion to a SCEVable type!"); 888 Ty = getEffectiveSCEVType(Ty); 889 890 // Fold if the operand is constant. 891 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) { 892 const Type *IntTy = getEffectiveSCEVType(Ty); 893 Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy); 894 if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty); 895 return getConstant(cast<ConstantInt>(C)); 896 } 897 898 // sext(sext(x)) --> sext(x) 899 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op)) 900 return getSignExtendExpr(SS->getOperand(), Ty); 901 902 // If the input value is a chrec scev, and we can prove that the value 903 // did not overflow the old, smaller, value, we can sign extend all of the 904 // operands (often constants). This allows analysis of something like 905 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; } 906 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) 907 if (AR->isAffine()) { 908 // Check whether the backedge-taken count is SCEVCouldNotCompute. 909 // Note that this serves two purposes: It filters out loops that are 910 // simply not analyzable, and it covers the case where this code is 911 // being called from within backedge-taken count analysis, such that 912 // attempting to ask for the backedge-taken count would likely result 913 // in infinite recursion. In the later case, the analysis code will 914 // cope with a conservative value, and it will take care to purge 915 // that value once it has finished. 916 const SCEV* MaxBECount = getMaxBackedgeTakenCount(AR->getLoop()); 917 if (!isa<SCEVCouldNotCompute>(MaxBECount)) { 918 // Manually compute the final value for AR, checking for 919 // overflow. 920 const SCEV* Start = AR->getStart(); 921 const SCEV* Step = AR->getStepRecurrence(*this); 922 923 // Check whether the backedge-taken count can be losslessly casted to 924 // the addrec's type. The count is always unsigned. 925 const SCEV* CastedMaxBECount = 926 getTruncateOrZeroExtend(MaxBECount, Start->getType()); 927 const SCEV* RecastedMaxBECount = 928 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType()); 929 if (MaxBECount == RecastedMaxBECount) { 930 const Type *WideTy = 931 IntegerType::get(getTypeSizeInBits(Start->getType()) * 2); 932 // Check whether Start+Step*MaxBECount has no signed overflow. 933 const SCEV* SMul = 934 getMulExpr(CastedMaxBECount, 935 getTruncateOrSignExtend(Step, Start->getType())); 936 const SCEV* Add = getAddExpr(Start, SMul); 937 const SCEV* OperandExtendedAdd = 938 getAddExpr(getSignExtendExpr(Start, WideTy), 939 getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy), 940 getSignExtendExpr(Step, WideTy))); 941 if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd) 942 // Return the expression with the addrec on the outside. 943 return getAddRecExpr(getSignExtendExpr(Start, Ty), 944 getSignExtendExpr(Step, Ty), 945 AR->getLoop()); 946 } 947 } 948 } 949 950 FoldingSetNodeID ID; 951 ID.AddInteger(scSignExtend); 952 ID.AddPointer(Op); 953 ID.AddPointer(Ty); 954 void *IP = 0; 955 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 956 SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>(); 957 new (S) SCEVSignExtendExpr(Op, Ty); 958 UniqueSCEVs.InsertNode(S, IP); 959 return S; 960 } 961 962 /// getAnyExtendExpr - Return a SCEV for the given operand extended with 963 /// unspecified bits out to the given type. 964 /// 965 const SCEV* ScalarEvolution::getAnyExtendExpr(const SCEV* Op, 966 const Type *Ty) { 967 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) && 968 "This is not an extending conversion!"); 969 assert(isSCEVable(Ty) && 970 "This is not a conversion to a SCEVable type!"); 971 Ty = getEffectiveSCEVType(Ty); 972 973 // Sign-extend negative constants. 974 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) 975 if (SC->getValue()->getValue().isNegative()) 976 return getSignExtendExpr(Op, Ty); 977 978 // Peel off a truncate cast. 979 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) { 980 const SCEV* NewOp = T->getOperand(); 981 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty)) 982 return getAnyExtendExpr(NewOp, Ty); 983 return getTruncateOrNoop(NewOp, Ty); 984 } 985 986 // Next try a zext cast. If the cast is folded, use it. 987 const SCEV* ZExt = getZeroExtendExpr(Op, Ty); 988 if (!isa<SCEVZeroExtendExpr>(ZExt)) 989 return ZExt; 990 991 // Next try a sext cast. If the cast is folded, use it. 992 const SCEV* SExt = getSignExtendExpr(Op, Ty); 993 if (!isa<SCEVSignExtendExpr>(SExt)) 994 return SExt; 995 996 // If the expression is obviously signed, use the sext cast value. 997 if (isa<SCEVSMaxExpr>(Op)) 998 return SExt; 999 1000 // Absent any other information, use the zext cast value. 1001 return ZExt; 1002 } 1003 1004 /// CollectAddOperandsWithScales - Process the given Ops list, which is 1005 /// a list of operands to be added under the given scale, update the given 1006 /// map. This is a helper function for getAddRecExpr. As an example of 1007 /// what it does, given a sequence of operands that would form an add 1008 /// expression like this: 1009 /// 1010 /// m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r) 1011 /// 1012 /// where A and B are constants, update the map with these values: 1013 /// 1014 /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0) 1015 /// 1016 /// and add 13 + A*B*29 to AccumulatedConstant. 1017 /// This will allow getAddRecExpr to produce this: 1018 /// 1019 /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B) 1020 /// 1021 /// This form often exposes folding opportunities that are hidden in 1022 /// the original operand list. 1023 /// 1024 /// Return true iff it appears that any interesting folding opportunities 1025 /// may be exposed. This helps getAddRecExpr short-circuit extra work in 1026 /// the common case where no interesting opportunities are present, and 1027 /// is also used as a check to avoid infinite recursion. 1028 /// 1029 static bool 1030 CollectAddOperandsWithScales(DenseMap<const SCEV*, APInt> &M, 1031 SmallVector<const SCEV*, 8> &NewOps, 1032 APInt &AccumulatedConstant, 1033 const SmallVectorImpl<const SCEV*> &Ops, 1034 const APInt &Scale, 1035 ScalarEvolution &SE) { 1036 bool Interesting = false; 1037 1038 // Iterate over the add operands. 1039 for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 1040 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]); 1041 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) { 1042 APInt NewScale = 1043 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue(); 1044 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) { 1045 // A multiplication of a constant with another add; recurse. 1046 Interesting |= 1047 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, 1048 cast<SCEVAddExpr>(Mul->getOperand(1)) 1049 ->getOperands(), 1050 NewScale, SE); 1051 } else { 1052 // A multiplication of a constant with some other value. Update 1053 // the map. 1054 SmallVector<const SCEV*, 4> MulOps(Mul->op_begin()+1, Mul->op_end()); 1055 const SCEV* Key = SE.getMulExpr(MulOps); 1056 std::pair<DenseMap<const SCEV*, APInt>::iterator, bool> Pair = 1057 M.insert(std::make_pair(Key, NewScale)); 1058 if (Pair.second) { 1059 NewOps.push_back(Pair.first->first); 1060 } else { 1061 Pair.first->second += NewScale; 1062 // The map already had an entry for this value, which may indicate 1063 // a folding opportunity. 1064 Interesting = true; 1065 } 1066 } 1067 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { 1068 // Pull a buried constant out to the outside. 1069 if (Scale != 1 || AccumulatedConstant != 0 || C->isZero()) 1070 Interesting = true; 1071 AccumulatedConstant += Scale * C->getValue()->getValue(); 1072 } else { 1073 // An ordinary operand. Update the map. 1074 std::pair<DenseMap<const SCEV*, APInt>::iterator, bool> Pair = 1075 M.insert(std::make_pair(Ops[i], Scale)); 1076 if (Pair.second) { 1077 NewOps.push_back(Pair.first->first); 1078 } else { 1079 Pair.first->second += Scale; 1080 // The map already had an entry for this value, which may indicate 1081 // a folding opportunity. 1082 Interesting = true; 1083 } 1084 } 1085 } 1086 1087 return Interesting; 1088 } 1089 1090 namespace { 1091 struct APIntCompare { 1092 bool operator()(const APInt &LHS, const APInt &RHS) const { 1093 return LHS.ult(RHS); 1094 } 1095 }; 1096 } 1097 1098 /// getAddExpr - Get a canonical add expression, or something simpler if 1099 /// possible. 1100 const SCEV* ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV*> &Ops) { 1101 assert(!Ops.empty() && "Cannot get empty add!"); 1102 if (Ops.size() == 1) return Ops[0]; 1103 #ifndef NDEBUG 1104 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 1105 assert(getEffectiveSCEVType(Ops[i]->getType()) == 1106 getEffectiveSCEVType(Ops[0]->getType()) && 1107 "SCEVAddExpr operand types don't match!"); 1108 #endif 1109 1110 // Sort by complexity, this groups all similar expression types together. 1111 GroupByComplexity(Ops, LI); 1112 1113 // If there are any constants, fold them together. 1114 unsigned Idx = 0; 1115 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 1116 ++Idx; 1117 assert(Idx < Ops.size()); 1118 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 1119 // We found two constants, fold them together! 1120 Ops[0] = getConstant(LHSC->getValue()->getValue() + 1121 RHSC->getValue()->getValue()); 1122 if (Ops.size() == 2) return Ops[0]; 1123 Ops.erase(Ops.begin()+1); // Erase the folded element 1124 LHSC = cast<SCEVConstant>(Ops[0]); 1125 } 1126 1127 // If we are left with a constant zero being added, strip it off. 1128 if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { 1129 Ops.erase(Ops.begin()); 1130 --Idx; 1131 } 1132 } 1133 1134 if (Ops.size() == 1) return Ops[0]; 1135 1136 // Okay, check to see if the same value occurs in the operand list twice. If 1137 // so, merge them together into an multiply expression. Since we sorted the 1138 // list, these values are required to be adjacent. 1139 const Type *Ty = Ops[0]->getType(); 1140 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) 1141 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2 1142 // Found a match, merge the two values into a multiply, and add any 1143 // remaining values to the result. 1144 const SCEV* Two = getIntegerSCEV(2, Ty); 1145 const SCEV* Mul = getMulExpr(Ops[i], Two); 1146 if (Ops.size() == 2) 1147 return Mul; 1148 Ops.erase(Ops.begin()+i, Ops.begin()+i+2); 1149 Ops.push_back(Mul); 1150 return getAddExpr(Ops); 1151 } 1152 1153 // Check for truncates. If all the operands are truncated from the same 1154 // type, see if factoring out the truncate would permit the result to be 1155 // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n) 1156 // if the contents of the resulting outer trunc fold to something simple. 1157 for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) { 1158 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]); 1159 const Type *DstType = Trunc->getType(); 1160 const Type *SrcType = Trunc->getOperand()->getType(); 1161 SmallVector<const SCEV*, 8> LargeOps; 1162 bool Ok = true; 1163 // Check all the operands to see if they can be represented in the 1164 // source type of the truncate. 1165 for (unsigned i = 0, e = Ops.size(); i != e; ++i) { 1166 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) { 1167 if (T->getOperand()->getType() != SrcType) { 1168 Ok = false; 1169 break; 1170 } 1171 LargeOps.push_back(T->getOperand()); 1172 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) { 1173 // This could be either sign or zero extension, but sign extension 1174 // is much more likely to be foldable here. 1175 LargeOps.push_back(getSignExtendExpr(C, SrcType)); 1176 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) { 1177 SmallVector<const SCEV*, 8> LargeMulOps; 1178 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) { 1179 if (const SCEVTruncateExpr *T = 1180 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) { 1181 if (T->getOperand()->getType() != SrcType) { 1182 Ok = false; 1183 break; 1184 } 1185 LargeMulOps.push_back(T->getOperand()); 1186 } else if (const SCEVConstant *C = 1187 dyn_cast<SCEVConstant>(M->getOperand(j))) { 1188 // This could be either sign or zero extension, but sign extension 1189 // is much more likely to be foldable here. 1190 LargeMulOps.push_back(getSignExtendExpr(C, SrcType)); 1191 } else { 1192 Ok = false; 1193 break; 1194 } 1195 } 1196 if (Ok) 1197 LargeOps.push_back(getMulExpr(LargeMulOps)); 1198 } else { 1199 Ok = false; 1200 break; 1201 } 1202 } 1203 if (Ok) { 1204 // Evaluate the expression in the larger type. 1205 const SCEV* Fold = getAddExpr(LargeOps); 1206 // If it folds to something simple, use it. Otherwise, don't. 1207 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold)) 1208 return getTruncateExpr(Fold, DstType); 1209 } 1210 } 1211 1212 // Skip past any other cast SCEVs. 1213 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr) 1214 ++Idx; 1215 1216 // If there are add operands they would be next. 1217 if (Idx < Ops.size()) { 1218 bool DeletedAdd = false; 1219 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) { 1220 // If we have an add, expand the add operands onto the end of the operands 1221 // list. 1222 Ops.insert(Ops.end(), Add->op_begin(), Add->op_end()); 1223 Ops.erase(Ops.begin()+Idx); 1224 DeletedAdd = true; 1225 } 1226 1227 // If we deleted at least one add, we added operands to the end of the list, 1228 // and they are not necessarily sorted. Recurse to resort and resimplify 1229 // any operands we just aquired. 1230 if (DeletedAdd) 1231 return getAddExpr(Ops); 1232 } 1233 1234 // Skip over the add expression until we get to a multiply. 1235 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) 1236 ++Idx; 1237 1238 // Check to see if there are any folding opportunities present with 1239 // operands multiplied by constant values. 1240 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) { 1241 uint64_t BitWidth = getTypeSizeInBits(Ty); 1242 DenseMap<const SCEV*, APInt> M; 1243 SmallVector<const SCEV*, 8> NewOps; 1244 APInt AccumulatedConstant(BitWidth, 0); 1245 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant, 1246 Ops, APInt(BitWidth, 1), *this)) { 1247 // Some interesting folding opportunity is present, so its worthwhile to 1248 // re-generate the operands list. Group the operands by constant scale, 1249 // to avoid multiplying by the same constant scale multiple times. 1250 std::map<APInt, SmallVector<const SCEV*, 4>, APIntCompare> MulOpLists; 1251 for (SmallVector<const SCEV*, 8>::iterator I = NewOps.begin(), 1252 E = NewOps.end(); I != E; ++I) 1253 MulOpLists[M.find(*I)->second].push_back(*I); 1254 // Re-generate the operands list. 1255 Ops.clear(); 1256 if (AccumulatedConstant != 0) 1257 Ops.push_back(getConstant(AccumulatedConstant)); 1258 for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator 1259 I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I) 1260 if (I->first != 0) 1261 Ops.push_back(getMulExpr(getConstant(I->first), 1262 getAddExpr(I->second))); 1263 if (Ops.empty()) 1264 return getIntegerSCEV(0, Ty); 1265 if (Ops.size() == 1) 1266 return Ops[0]; 1267 return getAddExpr(Ops); 1268 } 1269 } 1270 1271 // If we are adding something to a multiply expression, make sure the 1272 // something is not already an operand of the multiply. If so, merge it into 1273 // the multiply. 1274 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) { 1275 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]); 1276 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) { 1277 const SCEV *MulOpSCEV = Mul->getOperand(MulOp); 1278 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) 1279 if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) { 1280 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1)) 1281 const SCEV* InnerMul = Mul->getOperand(MulOp == 0); 1282 if (Mul->getNumOperands() != 2) { 1283 // If the multiply has more than two operands, we must get the 1284 // Y*Z term. 1285 SmallVector<const SCEV*, 4> MulOps(Mul->op_begin(), Mul->op_end()); 1286 MulOps.erase(MulOps.begin()+MulOp); 1287 InnerMul = getMulExpr(MulOps); 1288 } 1289 const SCEV* One = getIntegerSCEV(1, Ty); 1290 const SCEV* AddOne = getAddExpr(InnerMul, One); 1291 const SCEV* OuterMul = getMulExpr(AddOne, Ops[AddOp]); 1292 if (Ops.size() == 2) return OuterMul; 1293 if (AddOp < Idx) { 1294 Ops.erase(Ops.begin()+AddOp); 1295 Ops.erase(Ops.begin()+Idx-1); 1296 } else { 1297 Ops.erase(Ops.begin()+Idx); 1298 Ops.erase(Ops.begin()+AddOp-1); 1299 } 1300 Ops.push_back(OuterMul); 1301 return getAddExpr(Ops); 1302 } 1303 1304 // Check this multiply against other multiplies being added together. 1305 for (unsigned OtherMulIdx = Idx+1; 1306 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]); 1307 ++OtherMulIdx) { 1308 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]); 1309 // If MulOp occurs in OtherMul, we can fold the two multiplies 1310 // together. 1311 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands(); 1312 OMulOp != e; ++OMulOp) 1313 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) { 1314 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E)) 1315 const SCEV* InnerMul1 = Mul->getOperand(MulOp == 0); 1316 if (Mul->getNumOperands() != 2) { 1317 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), 1318 Mul->op_end()); 1319 MulOps.erase(MulOps.begin()+MulOp); 1320 InnerMul1 = getMulExpr(MulOps); 1321 } 1322 const SCEV* InnerMul2 = OtherMul->getOperand(OMulOp == 0); 1323 if (OtherMul->getNumOperands() != 2) { 1324 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(), 1325 OtherMul->op_end()); 1326 MulOps.erase(MulOps.begin()+OMulOp); 1327 InnerMul2 = getMulExpr(MulOps); 1328 } 1329 const SCEV* InnerMulSum = getAddExpr(InnerMul1,InnerMul2); 1330 const SCEV* OuterMul = getMulExpr(MulOpSCEV, InnerMulSum); 1331 if (Ops.size() == 2) return OuterMul; 1332 Ops.erase(Ops.begin()+Idx); 1333 Ops.erase(Ops.begin()+OtherMulIdx-1); 1334 Ops.push_back(OuterMul); 1335 return getAddExpr(Ops); 1336 } 1337 } 1338 } 1339 } 1340 1341 // If there are any add recurrences in the operands list, see if any other 1342 // added values are loop invariant. If so, we can fold them into the 1343 // recurrence. 1344 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) 1345 ++Idx; 1346 1347 // Scan over all recurrences, trying to fold loop invariants into them. 1348 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { 1349 // Scan all of the other operands to this add and add them to the vector if 1350 // they are loop invariant w.r.t. the recurrence. 1351 SmallVector<const SCEV*, 8> LIOps; 1352 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); 1353 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 1354 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { 1355 LIOps.push_back(Ops[i]); 1356 Ops.erase(Ops.begin()+i); 1357 --i; --e; 1358 } 1359 1360 // If we found some loop invariants, fold them into the recurrence. 1361 if (!LIOps.empty()) { 1362 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step} 1363 LIOps.push_back(AddRec->getStart()); 1364 1365 SmallVector<const SCEV*, 4> AddRecOps(AddRec->op_begin(), 1366 AddRec->op_end()); 1367 AddRecOps[0] = getAddExpr(LIOps); 1368 1369 const SCEV* NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop()); 1370 // If all of the other operands were loop invariant, we are done. 1371 if (Ops.size() == 1) return NewRec; 1372 1373 // Otherwise, add the folded AddRec by the non-liv parts. 1374 for (unsigned i = 0;; ++i) 1375 if (Ops[i] == AddRec) { 1376 Ops[i] = NewRec; 1377 break; 1378 } 1379 return getAddExpr(Ops); 1380 } 1381 1382 // Okay, if there weren't any loop invariants to be folded, check to see if 1383 // there are multiple AddRec's with the same loop induction variable being 1384 // added together. If so, we can fold them. 1385 for (unsigned OtherIdx = Idx+1; 1386 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) 1387 if (OtherIdx != Idx) { 1388 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); 1389 if (AddRec->getLoop() == OtherAddRec->getLoop()) { 1390 // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D} 1391 SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(), 1392 AddRec->op_end()); 1393 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) { 1394 if (i >= NewOps.size()) { 1395 NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i, 1396 OtherAddRec->op_end()); 1397 break; 1398 } 1399 NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i)); 1400 } 1401 const SCEV* NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop()); 1402 1403 if (Ops.size() == 2) return NewAddRec; 1404 1405 Ops.erase(Ops.begin()+Idx); 1406 Ops.erase(Ops.begin()+OtherIdx-1); 1407 Ops.push_back(NewAddRec); 1408 return getAddExpr(Ops); 1409 } 1410 } 1411 1412 // Otherwise couldn't fold anything into this recurrence. Move onto the 1413 // next one. 1414 } 1415 1416 // Okay, it looks like we really DO need an add expr. Check to see if we 1417 // already have one, otherwise create a new one. 1418 FoldingSetNodeID ID; 1419 ID.AddInteger(scAddExpr); 1420 ID.AddInteger(Ops.size()); 1421 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 1422 ID.AddPointer(Ops[i]); 1423 void *IP = 0; 1424 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1425 SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>(); 1426 new (S) SCEVAddExpr(Ops); 1427 UniqueSCEVs.InsertNode(S, IP); 1428 return S; 1429 } 1430 1431 1432 /// getMulExpr - Get a canonical multiply expression, or something simpler if 1433 /// possible. 1434 const SCEV* ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV*> &Ops) { 1435 assert(!Ops.empty() && "Cannot get empty mul!"); 1436 #ifndef NDEBUG 1437 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 1438 assert(getEffectiveSCEVType(Ops[i]->getType()) == 1439 getEffectiveSCEVType(Ops[0]->getType()) && 1440 "SCEVMulExpr operand types don't match!"); 1441 #endif 1442 1443 // Sort by complexity, this groups all similar expression types together. 1444 GroupByComplexity(Ops, LI); 1445 1446 // If there are any constants, fold them together. 1447 unsigned Idx = 0; 1448 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 1449 1450 // C1*(C2+V) -> C1*C2 + C1*V 1451 if (Ops.size() == 2) 1452 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) 1453 if (Add->getNumOperands() == 2 && 1454 isa<SCEVConstant>(Add->getOperand(0))) 1455 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)), 1456 getMulExpr(LHSC, Add->getOperand(1))); 1457 1458 1459 ++Idx; 1460 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 1461 // We found two constants, fold them together! 1462 ConstantInt *Fold = ConstantInt::get(LHSC->getValue()->getValue() * 1463 RHSC->getValue()->getValue()); 1464 Ops[0] = getConstant(Fold); 1465 Ops.erase(Ops.begin()+1); // Erase the folded element 1466 if (Ops.size() == 1) return Ops[0]; 1467 LHSC = cast<SCEVConstant>(Ops[0]); 1468 } 1469 1470 // If we are left with a constant one being multiplied, strip it off. 1471 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) { 1472 Ops.erase(Ops.begin()); 1473 --Idx; 1474 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) { 1475 // If we have a multiply of zero, it will always be zero. 1476 return Ops[0]; 1477 } 1478 } 1479 1480 // Skip over the add expression until we get to a multiply. 1481 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr) 1482 ++Idx; 1483 1484 if (Ops.size() == 1) 1485 return Ops[0]; 1486 1487 // If there are mul operands inline them all into this expression. 1488 if (Idx < Ops.size()) { 1489 bool DeletedMul = false; 1490 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) { 1491 // If we have an mul, expand the mul operands onto the end of the operands 1492 // list. 1493 Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end()); 1494 Ops.erase(Ops.begin()+Idx); 1495 DeletedMul = true; 1496 } 1497 1498 // If we deleted at least one mul, we added operands to the end of the list, 1499 // and they are not necessarily sorted. Recurse to resort and resimplify 1500 // any operands we just aquired. 1501 if (DeletedMul) 1502 return getMulExpr(Ops); 1503 } 1504 1505 // If there are any add recurrences in the operands list, see if any other 1506 // added values are loop invariant. If so, we can fold them into the 1507 // recurrence. 1508 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr) 1509 ++Idx; 1510 1511 // Scan over all recurrences, trying to fold loop invariants into them. 1512 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) { 1513 // Scan all of the other operands to this mul and add them to the vector if 1514 // they are loop invariant w.r.t. the recurrence. 1515 SmallVector<const SCEV*, 8> LIOps; 1516 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]); 1517 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 1518 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) { 1519 LIOps.push_back(Ops[i]); 1520 Ops.erase(Ops.begin()+i); 1521 --i; --e; 1522 } 1523 1524 // If we found some loop invariants, fold them into the recurrence. 1525 if (!LIOps.empty()) { 1526 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step} 1527 SmallVector<const SCEV*, 4> NewOps; 1528 NewOps.reserve(AddRec->getNumOperands()); 1529 if (LIOps.size() == 1) { 1530 const SCEV *Scale = LIOps[0]; 1531 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) 1532 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i))); 1533 } else { 1534 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) { 1535 SmallVector<const SCEV*, 4> MulOps(LIOps.begin(), LIOps.end()); 1536 MulOps.push_back(AddRec->getOperand(i)); 1537 NewOps.push_back(getMulExpr(MulOps)); 1538 } 1539 } 1540 1541 const SCEV* NewRec = getAddRecExpr(NewOps, AddRec->getLoop()); 1542 1543 // If all of the other operands were loop invariant, we are done. 1544 if (Ops.size() == 1) return NewRec; 1545 1546 // Otherwise, multiply the folded AddRec by the non-liv parts. 1547 for (unsigned i = 0;; ++i) 1548 if (Ops[i] == AddRec) { 1549 Ops[i] = NewRec; 1550 break; 1551 } 1552 return getMulExpr(Ops); 1553 } 1554 1555 // Okay, if there weren't any loop invariants to be folded, check to see if 1556 // there are multiple AddRec's with the same loop induction variable being 1557 // multiplied together. If so, we can fold them. 1558 for (unsigned OtherIdx = Idx+1; 1559 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx) 1560 if (OtherIdx != Idx) { 1561 const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]); 1562 if (AddRec->getLoop() == OtherAddRec->getLoop()) { 1563 // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D} 1564 const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec; 1565 const SCEV* NewStart = getMulExpr(F->getStart(), 1566 G->getStart()); 1567 const SCEV* B = F->getStepRecurrence(*this); 1568 const SCEV* D = G->getStepRecurrence(*this); 1569 const SCEV* NewStep = getAddExpr(getMulExpr(F, D), 1570 getMulExpr(G, B), 1571 getMulExpr(B, D)); 1572 const SCEV* NewAddRec = getAddRecExpr(NewStart, NewStep, 1573 F->getLoop()); 1574 if (Ops.size() == 2) return NewAddRec; 1575 1576 Ops.erase(Ops.begin()+Idx); 1577 Ops.erase(Ops.begin()+OtherIdx-1); 1578 Ops.push_back(NewAddRec); 1579 return getMulExpr(Ops); 1580 } 1581 } 1582 1583 // Otherwise couldn't fold anything into this recurrence. Move onto the 1584 // next one. 1585 } 1586 1587 // Okay, it looks like we really DO need an mul expr. Check to see if we 1588 // already have one, otherwise create a new one. 1589 FoldingSetNodeID ID; 1590 ID.AddInteger(scMulExpr); 1591 ID.AddInteger(Ops.size()); 1592 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 1593 ID.AddPointer(Ops[i]); 1594 void *IP = 0; 1595 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1596 SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>(); 1597 new (S) SCEVMulExpr(Ops); 1598 UniqueSCEVs.InsertNode(S, IP); 1599 return S; 1600 } 1601 1602 /// getUDivExpr - Get a canonical multiply expression, or something simpler if 1603 /// possible. 1604 const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS, 1605 const SCEV *RHS) { 1606 assert(getEffectiveSCEVType(LHS->getType()) == 1607 getEffectiveSCEVType(RHS->getType()) && 1608 "SCEVUDivExpr operand types don't match!"); 1609 1610 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { 1611 if (RHSC->getValue()->equalsInt(1)) 1612 return LHS; // X udiv 1 --> x 1613 if (RHSC->isZero()) 1614 return getIntegerSCEV(0, LHS->getType()); // value is undefined 1615 1616 // Determine if the division can be folded into the operands of 1617 // its operands. 1618 // TODO: Generalize this to non-constants by using known-bits information. 1619 const Type *Ty = LHS->getType(); 1620 unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros(); 1621 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ; 1622 // For non-power-of-two values, effectively round the value up to the 1623 // nearest power of two. 1624 if (!RHSC->getValue()->getValue().isPowerOf2()) 1625 ++MaxShiftAmt; 1626 const IntegerType *ExtTy = 1627 IntegerType::get(getTypeSizeInBits(Ty) + MaxShiftAmt); 1628 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded. 1629 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS)) 1630 if (const SCEVConstant *Step = 1631 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) 1632 if (!Step->getValue()->getValue() 1633 .urem(RHSC->getValue()->getValue()) && 1634 getZeroExtendExpr(AR, ExtTy) == 1635 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy), 1636 getZeroExtendExpr(Step, ExtTy), 1637 AR->getLoop())) { 1638 SmallVector<const SCEV*, 4> Operands; 1639 for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i) 1640 Operands.push_back(getUDivExpr(AR->getOperand(i), RHS)); 1641 return getAddRecExpr(Operands, AR->getLoop()); 1642 } 1643 // (A*B)/C --> A*(B/C) if safe and B/C can be folded. 1644 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) { 1645 SmallVector<const SCEV*, 4> Operands; 1646 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) 1647 Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy)); 1648 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands)) 1649 // Find an operand that's safely divisible. 1650 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) { 1651 const SCEV* Op = M->getOperand(i); 1652 const SCEV* Div = getUDivExpr(Op, RHSC); 1653 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) { 1654 const SmallVectorImpl<const SCEV*> &MOperands = M->getOperands(); 1655 Operands = SmallVector<const SCEV*, 4>(MOperands.begin(), 1656 MOperands.end()); 1657 Operands[i] = Div; 1658 return getMulExpr(Operands); 1659 } 1660 } 1661 } 1662 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded. 1663 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) { 1664 SmallVector<const SCEV*, 4> Operands; 1665 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) 1666 Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy)); 1667 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) { 1668 Operands.clear(); 1669 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { 1670 const SCEV* Op = getUDivExpr(A->getOperand(i), RHS); 1671 if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i)) 1672 break; 1673 Operands.push_back(Op); 1674 } 1675 if (Operands.size() == A->getNumOperands()) 1676 return getAddExpr(Operands); 1677 } 1678 } 1679 1680 // Fold if both operands are constant. 1681 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { 1682 Constant *LHSCV = LHSC->getValue(); 1683 Constant *RHSCV = RHSC->getValue(); 1684 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV, 1685 RHSCV))); 1686 } 1687 } 1688 1689 FoldingSetNodeID ID; 1690 ID.AddInteger(scUDivExpr); 1691 ID.AddPointer(LHS); 1692 ID.AddPointer(RHS); 1693 void *IP = 0; 1694 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1695 SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>(); 1696 new (S) SCEVUDivExpr(LHS, RHS); 1697 UniqueSCEVs.InsertNode(S, IP); 1698 return S; 1699 } 1700 1701 1702 /// getAddRecExpr - Get an add recurrence expression for the specified loop. 1703 /// Simplify the expression as much as possible. 1704 const SCEV* ScalarEvolution::getAddRecExpr(const SCEV* Start, 1705 const SCEV* Step, const Loop *L) { 1706 SmallVector<const SCEV*, 4> Operands; 1707 Operands.push_back(Start); 1708 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step)) 1709 if (StepChrec->getLoop() == L) { 1710 Operands.insert(Operands.end(), StepChrec->op_begin(), 1711 StepChrec->op_end()); 1712 return getAddRecExpr(Operands, L); 1713 } 1714 1715 Operands.push_back(Step); 1716 return getAddRecExpr(Operands, L); 1717 } 1718 1719 /// getAddRecExpr - Get an add recurrence expression for the specified loop. 1720 /// Simplify the expression as much as possible. 1721 const SCEV * 1722 ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV*> &Operands, 1723 const Loop *L) { 1724 if (Operands.size() == 1) return Operands[0]; 1725 #ifndef NDEBUG 1726 for (unsigned i = 1, e = Operands.size(); i != e; ++i) 1727 assert(getEffectiveSCEVType(Operands[i]->getType()) == 1728 getEffectiveSCEVType(Operands[0]->getType()) && 1729 "SCEVAddRecExpr operand types don't match!"); 1730 #endif 1731 1732 if (Operands.back()->isZero()) { 1733 Operands.pop_back(); 1734 return getAddRecExpr(Operands, L); // {X,+,0} --> X 1735 } 1736 1737 // Canonicalize nested AddRecs in by nesting them in order of loop depth. 1738 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) { 1739 const Loop* NestedLoop = NestedAR->getLoop(); 1740 if (L->getLoopDepth() < NestedLoop->getLoopDepth()) { 1741 SmallVector<const SCEV*, 4> NestedOperands(NestedAR->op_begin(), 1742 NestedAR->op_end()); 1743 Operands[0] = NestedAR->getStart(); 1744 // AddRecs require their operands be loop-invariant with respect to their 1745 // loops. Don't perform this transformation if it would break this 1746 // requirement. 1747 bool AllInvariant = true; 1748 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 1749 if (!Operands[i]->isLoopInvariant(L)) { 1750 AllInvariant = false; 1751 break; 1752 } 1753 if (AllInvariant) { 1754 NestedOperands[0] = getAddRecExpr(Operands, L); 1755 AllInvariant = true; 1756 for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i) 1757 if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) { 1758 AllInvariant = false; 1759 break; 1760 } 1761 if (AllInvariant) 1762 // Ok, both add recurrences are valid after the transformation. 1763 return getAddRecExpr(NestedOperands, NestedLoop); 1764 } 1765 // Reset Operands to its original state. 1766 Operands[0] = NestedAR; 1767 } 1768 } 1769 1770 FoldingSetNodeID ID; 1771 ID.AddInteger(scAddRecExpr); 1772 ID.AddInteger(Operands.size()); 1773 for (unsigned i = 0, e = Operands.size(); i != e; ++i) 1774 ID.AddPointer(Operands[i]); 1775 ID.AddPointer(L); 1776 void *IP = 0; 1777 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1778 SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>(); 1779 new (S) SCEVAddRecExpr(Operands, L); 1780 UniqueSCEVs.InsertNode(S, IP); 1781 return S; 1782 } 1783 1784 const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, 1785 const SCEV *RHS) { 1786 SmallVector<const SCEV*, 2> Ops; 1787 Ops.push_back(LHS); 1788 Ops.push_back(RHS); 1789 return getSMaxExpr(Ops); 1790 } 1791 1792 const SCEV* 1793 ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV*> &Ops) { 1794 assert(!Ops.empty() && "Cannot get empty smax!"); 1795 if (Ops.size() == 1) return Ops[0]; 1796 #ifndef NDEBUG 1797 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 1798 assert(getEffectiveSCEVType(Ops[i]->getType()) == 1799 getEffectiveSCEVType(Ops[0]->getType()) && 1800 "SCEVSMaxExpr operand types don't match!"); 1801 #endif 1802 1803 // Sort by complexity, this groups all similar expression types together. 1804 GroupByComplexity(Ops, LI); 1805 1806 // If there are any constants, fold them together. 1807 unsigned Idx = 0; 1808 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 1809 ++Idx; 1810 assert(Idx < Ops.size()); 1811 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 1812 // We found two constants, fold them together! 1813 ConstantInt *Fold = ConstantInt::get( 1814 APIntOps::smax(LHSC->getValue()->getValue(), 1815 RHSC->getValue()->getValue())); 1816 Ops[0] = getConstant(Fold); 1817 Ops.erase(Ops.begin()+1); // Erase the folded element 1818 if (Ops.size() == 1) return Ops[0]; 1819 LHSC = cast<SCEVConstant>(Ops[0]); 1820 } 1821 1822 // If we are left with a constant minimum-int, strip it off. 1823 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) { 1824 Ops.erase(Ops.begin()); 1825 --Idx; 1826 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) { 1827 // If we have an smax with a constant maximum-int, it will always be 1828 // maximum-int. 1829 return Ops[0]; 1830 } 1831 } 1832 1833 if (Ops.size() == 1) return Ops[0]; 1834 1835 // Find the first SMax 1836 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr) 1837 ++Idx; 1838 1839 // Check to see if one of the operands is an SMax. If so, expand its operands 1840 // onto our operand list, and recurse to simplify. 1841 if (Idx < Ops.size()) { 1842 bool DeletedSMax = false; 1843 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) { 1844 Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end()); 1845 Ops.erase(Ops.begin()+Idx); 1846 DeletedSMax = true; 1847 } 1848 1849 if (DeletedSMax) 1850 return getSMaxExpr(Ops); 1851 } 1852 1853 // Okay, check to see if the same value occurs in the operand list twice. If 1854 // so, delete one. Since we sorted the list, these values are required to 1855 // be adjacent. 1856 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) 1857 if (Ops[i] == Ops[i+1]) { // X smax Y smax Y --> X smax Y 1858 Ops.erase(Ops.begin()+i, Ops.begin()+i+1); 1859 --i; --e; 1860 } 1861 1862 if (Ops.size() == 1) return Ops[0]; 1863 1864 assert(!Ops.empty() && "Reduced smax down to nothing!"); 1865 1866 // Okay, it looks like we really DO need an smax expr. Check to see if we 1867 // already have one, otherwise create a new one. 1868 FoldingSetNodeID ID; 1869 ID.AddInteger(scSMaxExpr); 1870 ID.AddInteger(Ops.size()); 1871 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 1872 ID.AddPointer(Ops[i]); 1873 void *IP = 0; 1874 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1875 SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>(); 1876 new (S) SCEVSMaxExpr(Ops); 1877 UniqueSCEVs.InsertNode(S, IP); 1878 return S; 1879 } 1880 1881 const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, 1882 const SCEV *RHS) { 1883 SmallVector<const SCEV*, 2> Ops; 1884 Ops.push_back(LHS); 1885 Ops.push_back(RHS); 1886 return getUMaxExpr(Ops); 1887 } 1888 1889 const SCEV* 1890 ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV*> &Ops) { 1891 assert(!Ops.empty() && "Cannot get empty umax!"); 1892 if (Ops.size() == 1) return Ops[0]; 1893 #ifndef NDEBUG 1894 for (unsigned i = 1, e = Ops.size(); i != e; ++i) 1895 assert(getEffectiveSCEVType(Ops[i]->getType()) == 1896 getEffectiveSCEVType(Ops[0]->getType()) && 1897 "SCEVUMaxExpr operand types don't match!"); 1898 #endif 1899 1900 // Sort by complexity, this groups all similar expression types together. 1901 GroupByComplexity(Ops, LI); 1902 1903 // If there are any constants, fold them together. 1904 unsigned Idx = 0; 1905 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) { 1906 ++Idx; 1907 assert(Idx < Ops.size()); 1908 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) { 1909 // We found two constants, fold them together! 1910 ConstantInt *Fold = ConstantInt::get( 1911 APIntOps::umax(LHSC->getValue()->getValue(), 1912 RHSC->getValue()->getValue())); 1913 Ops[0] = getConstant(Fold); 1914 Ops.erase(Ops.begin()+1); // Erase the folded element 1915 if (Ops.size() == 1) return Ops[0]; 1916 LHSC = cast<SCEVConstant>(Ops[0]); 1917 } 1918 1919 // If we are left with a constant minimum-int, strip it off. 1920 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) { 1921 Ops.erase(Ops.begin()); 1922 --Idx; 1923 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) { 1924 // If we have an umax with a constant maximum-int, it will always be 1925 // maximum-int. 1926 return Ops[0]; 1927 } 1928 } 1929 1930 if (Ops.size() == 1) return Ops[0]; 1931 1932 // Find the first UMax 1933 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr) 1934 ++Idx; 1935 1936 // Check to see if one of the operands is a UMax. If so, expand its operands 1937 // onto our operand list, and recurse to simplify. 1938 if (Idx < Ops.size()) { 1939 bool DeletedUMax = false; 1940 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) { 1941 Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end()); 1942 Ops.erase(Ops.begin()+Idx); 1943 DeletedUMax = true; 1944 } 1945 1946 if (DeletedUMax) 1947 return getUMaxExpr(Ops); 1948 } 1949 1950 // Okay, check to see if the same value occurs in the operand list twice. If 1951 // so, delete one. Since we sorted the list, these values are required to 1952 // be adjacent. 1953 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i) 1954 if (Ops[i] == Ops[i+1]) { // X umax Y umax Y --> X umax Y 1955 Ops.erase(Ops.begin()+i, Ops.begin()+i+1); 1956 --i; --e; 1957 } 1958 1959 if (Ops.size() == 1) return Ops[0]; 1960 1961 assert(!Ops.empty() && "Reduced umax down to nothing!"); 1962 1963 // Okay, it looks like we really DO need a umax expr. Check to see if we 1964 // already have one, otherwise create a new one. 1965 FoldingSetNodeID ID; 1966 ID.AddInteger(scUMaxExpr); 1967 ID.AddInteger(Ops.size()); 1968 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 1969 ID.AddPointer(Ops[i]); 1970 void *IP = 0; 1971 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 1972 SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>(); 1973 new (S) SCEVUMaxExpr(Ops); 1974 UniqueSCEVs.InsertNode(S, IP); 1975 return S; 1976 } 1977 1978 const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS, 1979 const SCEV *RHS) { 1980 // ~smax(~x, ~y) == smin(x, y). 1981 return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); 1982 } 1983 1984 const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS, 1985 const SCEV *RHS) { 1986 // ~umax(~x, ~y) == umin(x, y) 1987 return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS))); 1988 } 1989 1990 const SCEV* ScalarEvolution::getUnknown(Value *V) { 1991 // Don't attempt to do anything other than create a SCEVUnknown object 1992 // here. createSCEV only calls getUnknown after checking for all other 1993 // interesting possibilities, and any other code that calls getUnknown 1994 // is doing so in order to hide a value from SCEV canonicalization. 1995 1996 FoldingSetNodeID ID; 1997 ID.AddInteger(scUnknown); 1998 ID.AddPointer(V); 1999 void *IP = 0; 2000 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S; 2001 SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>(); 2002 new (S) SCEVUnknown(V); 2003 UniqueSCEVs.InsertNode(S, IP); 2004 return S; 2005 } 2006 2007 //===----------------------------------------------------------------------===// 2008 // Basic SCEV Analysis and PHI Idiom Recognition Code 2009 // 2010 2011 /// isSCEVable - Test if values of the given type are analyzable within 2012 /// the SCEV framework. This primarily includes integer types, and it 2013 /// can optionally include pointer types if the ScalarEvolution class 2014 /// has access to target-specific information. 2015 bool ScalarEvolution::isSCEVable(const Type *Ty) const { 2016 // Integers are always SCEVable. 2017 if (Ty->isInteger()) 2018 return true; 2019 2020 // Pointers are SCEVable if TargetData information is available 2021 // to provide pointer size information. 2022 if (isa<PointerType>(Ty)) 2023 return TD != NULL; 2024 2025 // Otherwise it's not SCEVable. 2026 return false; 2027 } 2028 2029 /// getTypeSizeInBits - Return the size in bits of the specified type, 2030 /// for which isSCEVable must return true. 2031 uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const { 2032 assert(isSCEVable(Ty) && "Type is not SCEVable!"); 2033 2034 // If we have a TargetData, use it! 2035 if (TD) 2036 return TD->getTypeSizeInBits(Ty); 2037 2038 // Otherwise, we support only integer types. 2039 assert(Ty->isInteger() && "isSCEVable permitted a non-SCEVable type!"); 2040 return Ty->getPrimitiveSizeInBits(); 2041 } 2042 2043 /// getEffectiveSCEVType - Return a type with the same bitwidth as 2044 /// the given type and which represents how SCEV will treat the given 2045 /// type, for which isSCEVable must return true. For pointer types, 2046 /// this is the pointer-sized integer type. 2047 const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const { 2048 assert(isSCEVable(Ty) && "Type is not SCEVable!"); 2049 2050 if (Ty->isInteger()) 2051 return Ty; 2052 2053 assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!"); 2054 return TD->getIntPtrType(); 2055 } 2056 2057 const SCEV* ScalarEvolution::getCouldNotCompute() { 2058 return &CouldNotCompute; 2059 } 2060 2061 /// hasSCEV - Return true if the SCEV for this value has already been 2062 /// computed. 2063 bool ScalarEvolution::hasSCEV(Value *V) const { 2064 return Scalars.count(V); 2065 } 2066 2067 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the 2068 /// expression and create a new one. 2069 const SCEV* ScalarEvolution::getSCEV(Value *V) { 2070 assert(isSCEVable(V->getType()) && "Value is not SCEVable!"); 2071 2072 std::map<SCEVCallbackVH, const SCEV*>::iterator I = Scalars.find(V); 2073 if (I != Scalars.end()) return I->second; 2074 const SCEV* S = createSCEV(V); 2075 Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S)); 2076 return S; 2077 } 2078 2079 /// getIntegerSCEV - Given a SCEVable type, create a constant for the 2080 /// specified signed integer value and return a SCEV for the constant. 2081 const SCEV* ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) { 2082 const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty)); 2083 return getConstant(ConstantInt::get(ITy, Val)); 2084 } 2085 2086 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V 2087 /// 2088 const SCEV* ScalarEvolution::getNegativeSCEV(const SCEV* V) { 2089 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) 2090 return getConstant(cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue()))); 2091 2092 const Type *Ty = V->getType(); 2093 Ty = getEffectiveSCEVType(Ty); 2094 return getMulExpr(V, getConstant(ConstantInt::getAllOnesValue(Ty))); 2095 } 2096 2097 /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V 2098 const SCEV* ScalarEvolution::getNotSCEV(const SCEV* V) { 2099 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V)) 2100 return getConstant(cast<ConstantInt>(ConstantExpr::getNot(VC->getValue()))); 2101 2102 const Type *Ty = V->getType(); 2103 Ty = getEffectiveSCEVType(Ty); 2104 const SCEV* AllOnes = getConstant(ConstantInt::getAllOnesValue(Ty)); 2105 return getMinusSCEV(AllOnes, V); 2106 } 2107 2108 /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS. 2109 /// 2110 const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, 2111 const SCEV *RHS) { 2112 // X - Y --> X + -Y 2113 return getAddExpr(LHS, getNegativeSCEV(RHS)); 2114 } 2115 2116 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the 2117 /// input value to the specified type. If the type must be extended, it is zero 2118 /// extended. 2119 const SCEV* 2120 ScalarEvolution::getTruncateOrZeroExtend(const SCEV* V, 2121 const Type *Ty) { 2122 const Type *SrcTy = V->getType(); 2123 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && 2124 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && 2125 "Cannot truncate or zero extend with non-integer arguments!"); 2126 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 2127 return V; // No conversion 2128 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) 2129 return getTruncateExpr(V, Ty); 2130 return getZeroExtendExpr(V, Ty); 2131 } 2132 2133 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the 2134 /// input value to the specified type. If the type must be extended, it is sign 2135 /// extended. 2136 const SCEV* 2137 ScalarEvolution::getTruncateOrSignExtend(const SCEV* V, 2138 const Type *Ty) { 2139 const Type *SrcTy = V->getType(); 2140 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && 2141 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && 2142 "Cannot truncate or zero extend with non-integer arguments!"); 2143 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 2144 return V; // No conversion 2145 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty)) 2146 return getTruncateExpr(V, Ty); 2147 return getSignExtendExpr(V, Ty); 2148 } 2149 2150 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the 2151 /// input value to the specified type. If the type must be extended, it is zero 2152 /// extended. The conversion must not be narrowing. 2153 const SCEV* 2154 ScalarEvolution::getNoopOrZeroExtend(const SCEV* V, const Type *Ty) { 2155 const Type *SrcTy = V->getType(); 2156 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && 2157 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && 2158 "Cannot noop or zero extend with non-integer arguments!"); 2159 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 2160 "getNoopOrZeroExtend cannot truncate!"); 2161 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 2162 return V; // No conversion 2163 return getZeroExtendExpr(V, Ty); 2164 } 2165 2166 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the 2167 /// input value to the specified type. If the type must be extended, it is sign 2168 /// extended. The conversion must not be narrowing. 2169 const SCEV* 2170 ScalarEvolution::getNoopOrSignExtend(const SCEV* V, const Type *Ty) { 2171 const Type *SrcTy = V->getType(); 2172 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && 2173 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && 2174 "Cannot noop or sign extend with non-integer arguments!"); 2175 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 2176 "getNoopOrSignExtend cannot truncate!"); 2177 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 2178 return V; // No conversion 2179 return getSignExtendExpr(V, Ty); 2180 } 2181 2182 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of 2183 /// the input value to the specified type. If the type must be extended, 2184 /// it is extended with unspecified bits. The conversion must not be 2185 /// narrowing. 2186 const SCEV* 2187 ScalarEvolution::getNoopOrAnyExtend(const SCEV* V, const Type *Ty) { 2188 const Type *SrcTy = V->getType(); 2189 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && 2190 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && 2191 "Cannot noop or any extend with non-integer arguments!"); 2192 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) && 2193 "getNoopOrAnyExtend cannot truncate!"); 2194 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 2195 return V; // No conversion 2196 return getAnyExtendExpr(V, Ty); 2197 } 2198 2199 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the 2200 /// input value to the specified type. The conversion must not be widening. 2201 const SCEV* 2202 ScalarEvolution::getTruncateOrNoop(const SCEV* V, const Type *Ty) { 2203 const Type *SrcTy = V->getType(); 2204 assert((SrcTy->isInteger() || (TD && isa<PointerType>(SrcTy))) && 2205 (Ty->isInteger() || (TD && isa<PointerType>(Ty))) && 2206 "Cannot truncate or noop with non-integer arguments!"); 2207 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) && 2208 "getTruncateOrNoop cannot extend!"); 2209 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty)) 2210 return V; // No conversion 2211 return getTruncateExpr(V, Ty); 2212 } 2213 2214 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of 2215 /// the types using zero-extension, and then perform a umax operation 2216 /// with them. 2217 const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS, 2218 const SCEV *RHS) { 2219 const SCEV* PromotedLHS = LHS; 2220 const SCEV* PromotedRHS = RHS; 2221 2222 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) 2223 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); 2224 else 2225 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); 2226 2227 return getUMaxExpr(PromotedLHS, PromotedRHS); 2228 } 2229 2230 /// getUMinFromMismatchedTypes - Promote the operands to the wider of 2231 /// the types using zero-extension, and then perform a umin operation 2232 /// with them. 2233 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS, 2234 const SCEV *RHS) { 2235 const SCEV* PromotedLHS = LHS; 2236 const SCEV* PromotedRHS = RHS; 2237 2238 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType())) 2239 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType()); 2240 else 2241 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType()); 2242 2243 return getUMinExpr(PromotedLHS, PromotedRHS); 2244 } 2245 2246 /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for 2247 /// the specified instruction and replaces any references to the symbolic value 2248 /// SymName with the specified value. This is used during PHI resolution. 2249 void 2250 ScalarEvolution::ReplaceSymbolicValueWithConcrete(Instruction *I, 2251 const SCEV *SymName, 2252 const SCEV *NewVal) { 2253 std::map<SCEVCallbackVH, const SCEV*>::iterator SI = 2254 Scalars.find(SCEVCallbackVH(I, this)); 2255 if (SI == Scalars.end()) return; 2256 2257 const SCEV* NV = 2258 SI->second->replaceSymbolicValuesWithConcrete(SymName, NewVal, *this); 2259 if (NV == SI->second) return; // No change. 2260 2261 SI->second = NV; // Update the scalars map! 2262 2263 // Any instruction values that use this instruction might also need to be 2264 // updated! 2265 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); 2266 UI != E; ++UI) 2267 ReplaceSymbolicValueWithConcrete(cast<Instruction>(*UI), SymName, NewVal); 2268 } 2269 2270 /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in 2271 /// a loop header, making it a potential recurrence, or it doesn't. 2272 /// 2273 const SCEV* ScalarEvolution::createNodeForPHI(PHINode *PN) { 2274 if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized. 2275 if (const Loop *L = LI->getLoopFor(PN->getParent())) 2276 if (L->getHeader() == PN->getParent()) { 2277 // If it lives in the loop header, it has two incoming values, one 2278 // from outside the loop, and one from inside. 2279 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); 2280 unsigned BackEdge = IncomingEdge^1; 2281 2282 // While we are analyzing this PHI node, handle its value symbolically. 2283 const SCEV* SymbolicName = getUnknown(PN); 2284 assert(Scalars.find(PN) == Scalars.end() && 2285 "PHI node already processed?"); 2286 Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName)); 2287 2288 // Using this symbolic name for the PHI, analyze the value coming around 2289 // the back-edge. 2290 const SCEV* BEValue = getSCEV(PN->getIncomingValue(BackEdge)); 2291 2292 // NOTE: If BEValue is loop invariant, we know that the PHI node just 2293 // has a special value for the first iteration of the loop. 2294 2295 // If the value coming around the backedge is an add with the symbolic 2296 // value we just inserted, then we found a simple induction variable! 2297 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) { 2298 // If there is a single occurrence of the symbolic value, replace it 2299 // with a recurrence. 2300 unsigned FoundIndex = Add->getNumOperands(); 2301 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) 2302 if (Add->getOperand(i) == SymbolicName) 2303 if (FoundIndex == e) { 2304 FoundIndex = i; 2305 break; 2306 } 2307 2308 if (FoundIndex != Add->getNumOperands()) { 2309 // Create an add with everything but the specified operand. 2310 SmallVector<const SCEV*, 8> Ops; 2311 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i) 2312 if (i != FoundIndex) 2313 Ops.push_back(Add->getOperand(i)); 2314 const SCEV* Accum = getAddExpr(Ops); 2315 2316 // This is not a valid addrec if the step amount is varying each 2317 // loop iteration, but is not itself an addrec in this loop. 2318 if (Accum->isLoopInvariant(L) || 2319 (isa<SCEVAddRecExpr>(Accum) && 2320 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) { 2321 const SCEV *StartVal = 2322 getSCEV(PN->getIncomingValue(IncomingEdge)); 2323 const SCEV *PHISCEV = 2324 getAddRecExpr(StartVal, Accum, L); 2325 2326 // Okay, for the entire analysis of this edge we assumed the PHI 2327 // to be symbolic. We now need to go back and update all of the 2328 // entries for the scalars that use the PHI (except for the PHI 2329 // itself) to use the new analyzed value instead of the "symbolic" 2330 // value. 2331 ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); 2332 return PHISCEV; 2333 } 2334 } 2335 } else if (const SCEVAddRecExpr *AddRec = 2336 dyn_cast<SCEVAddRecExpr>(BEValue)) { 2337 // Otherwise, this could be a loop like this: 2338 // i = 0; for (j = 1; ..; ++j) { .... i = j; } 2339 // In this case, j = {1,+,1} and BEValue is j. 2340 // Because the other in-value of i (0) fits the evolution of BEValue 2341 // i really is an addrec evolution. 2342 if (AddRec->getLoop() == L && AddRec->isAffine()) { 2343 const SCEV* StartVal = getSCEV(PN->getIncomingValue(IncomingEdge)); 2344 2345 // If StartVal = j.start - j.stride, we can use StartVal as the 2346 // initial step of the addrec evolution. 2347 if (StartVal == getMinusSCEV(AddRec->getOperand(0), 2348 AddRec->getOperand(1))) { 2349 const SCEV* PHISCEV = 2350 getAddRecExpr(StartVal, AddRec->getOperand(1), L); 2351 2352 // Okay, for the entire analysis of this edge we assumed the PHI 2353 // to be symbolic. We now need to go back and update all of the 2354 // entries for the scalars that use the PHI (except for the PHI 2355 // itself) to use the new analyzed value instead of the "symbolic" 2356 // value. 2357 ReplaceSymbolicValueWithConcrete(PN, SymbolicName, PHISCEV); 2358 return PHISCEV; 2359 } 2360 } 2361 } 2362 2363 return SymbolicName; 2364 } 2365 2366 // If it's not a loop phi, we can't handle it yet. 2367 return getUnknown(PN); 2368 } 2369 2370 /// createNodeForGEP - Expand GEP instructions into add and multiply 2371 /// operations. This allows them to be analyzed by regular SCEV code. 2372 /// 2373 const SCEV* ScalarEvolution::createNodeForGEP(User *GEP) { 2374 2375 const Type *IntPtrTy = TD->getIntPtrType(); 2376 Value *Base = GEP->getOperand(0); 2377 // Don't attempt to analyze GEPs over unsized objects. 2378 if (!cast<PointerType>(Base->getType())->getElementType()->isSized()) 2379 return getUnknown(GEP); 2380 const SCEV* TotalOffset = getIntegerSCEV(0, IntPtrTy); 2381 gep_type_iterator GTI = gep_type_begin(GEP); 2382 for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()), 2383 E = GEP->op_end(); 2384 I != E; ++I) { 2385 Value *Index = *I; 2386 // Compute the (potentially symbolic) offset in bytes for this index. 2387 if (const StructType *STy = dyn_cast<StructType>(*GTI++)) { 2388 // For a struct, add the member offset. 2389 const StructLayout &SL = *TD->getStructLayout(STy); 2390 unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue(); 2391 uint64_t Offset = SL.getElementOffset(FieldNo); 2392 TotalOffset = getAddExpr(TotalOffset, 2393 getIntegerSCEV(Offset, IntPtrTy)); 2394 } else { 2395 // For an array, add the element offset, explicitly scaled. 2396 const SCEV* LocalOffset = getSCEV(Index); 2397 if (!isa<PointerType>(LocalOffset->getType())) 2398 // Getelementptr indicies are signed. 2399 LocalOffset = getTruncateOrSignExtend(LocalOffset, 2400 IntPtrTy); 2401 LocalOffset = 2402 getMulExpr(LocalOffset, 2403 getIntegerSCEV(TD->getTypeAllocSize(*GTI), 2404 IntPtrTy)); 2405 TotalOffset = getAddExpr(TotalOffset, LocalOffset); 2406 } 2407 } 2408 return getAddExpr(getSCEV(Base), TotalOffset); 2409 } 2410 2411 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is 2412 /// guaranteed to end in (at every loop iteration). It is, at the same time, 2413 /// the minimum number of times S is divisible by 2. For example, given {4,+,8} 2414 /// it returns 2. If S is guaranteed to be 0, it returns the bitwidth of S. 2415 uint32_t 2416 ScalarEvolution::GetMinTrailingZeros(const SCEV* S) { 2417 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) 2418 return C->getValue()->getValue().countTrailingZeros(); 2419 2420 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S)) 2421 return std::min(GetMinTrailingZeros(T->getOperand()), 2422 (uint32_t)getTypeSizeInBits(T->getType())); 2423 2424 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) { 2425 uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); 2426 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? 2427 getTypeSizeInBits(E->getType()) : OpRes; 2428 } 2429 2430 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) { 2431 uint32_t OpRes = GetMinTrailingZeros(E->getOperand()); 2432 return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ? 2433 getTypeSizeInBits(E->getType()) : OpRes; 2434 } 2435 2436 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { 2437 // The result is the min of all operands results. 2438 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); 2439 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) 2440 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); 2441 return MinOpRes; 2442 } 2443 2444 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) { 2445 // The result is the sum of all operands results. 2446 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0)); 2447 uint32_t BitWidth = getTypeSizeInBits(M->getType()); 2448 for (unsigned i = 1, e = M->getNumOperands(); 2449 SumOpRes != BitWidth && i != e; ++i) 2450 SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), 2451 BitWidth); 2452 return SumOpRes; 2453 } 2454 2455 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) { 2456 // The result is the min of all operands results. 2457 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0)); 2458 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i) 2459 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i))); 2460 return MinOpRes; 2461 } 2462 2463 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) { 2464 // The result is the min of all operands results. 2465 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); 2466 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) 2467 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); 2468 return MinOpRes; 2469 } 2470 2471 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) { 2472 // The result is the min of all operands results. 2473 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0)); 2474 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i) 2475 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i))); 2476 return MinOpRes; 2477 } 2478 2479 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 2480 // For a SCEVUnknown, ask ValueTracking. 2481 unsigned BitWidth = getTypeSizeInBits(U->getType()); 2482 APInt Mask = APInt::getAllOnesValue(BitWidth); 2483 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); 2484 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones); 2485 return Zeros.countTrailingOnes(); 2486 } 2487 2488 // SCEVUDivExpr 2489 return 0; 2490 } 2491 2492 uint32_t 2493 ScalarEvolution::GetMinLeadingZeros(const SCEV* S) { 2494 // TODO: Handle other SCEV expression types here. 2495 2496 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) 2497 return C->getValue()->getValue().countLeadingZeros(); 2498 2499 if (const SCEVZeroExtendExpr *C = dyn_cast<SCEVZeroExtendExpr>(S)) { 2500 // A zero-extension cast adds zero bits. 2501 return GetMinLeadingZeros(C->getOperand()) + 2502 (getTypeSizeInBits(C->getType()) - 2503 getTypeSizeInBits(C->getOperand()->getType())); 2504 } 2505 2506 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 2507 // For a SCEVUnknown, ask ValueTracking. 2508 unsigned BitWidth = getTypeSizeInBits(U->getType()); 2509 APInt Mask = APInt::getAllOnesValue(BitWidth); 2510 APInt Zeros(BitWidth, 0), Ones(BitWidth, 0); 2511 ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD); 2512 return Zeros.countLeadingOnes(); 2513 } 2514 2515 return 1; 2516 } 2517 2518 uint32_t 2519 ScalarEvolution::GetMinSignBits(const SCEV* S) { 2520 // TODO: Handle other SCEV expression types here. 2521 2522 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) { 2523 const APInt &A = C->getValue()->getValue(); 2524 return A.isNegative() ? A.countLeadingOnes() : 2525 A.countLeadingZeros(); 2526 } 2527 2528 if (const SCEVSignExtendExpr *C = dyn_cast<SCEVSignExtendExpr>(S)) { 2529 // A sign-extension cast adds sign bits. 2530 return GetMinSignBits(C->getOperand()) + 2531 (getTypeSizeInBits(C->getType()) - 2532 getTypeSizeInBits(C->getOperand()->getType())); 2533 } 2534 2535 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { 2536 unsigned BitWidth = getTypeSizeInBits(A->getType()); 2537 2538 // Special case decrementing a value (ADD X, -1): 2539 if (const SCEVConstant *CRHS = dyn_cast<SCEVConstant>(A->getOperand(0))) 2540 if (CRHS->isAllOnesValue()) { 2541 SmallVector<const SCEV *, 4> OtherOps(A->op_begin() + 1, A->op_end()); 2542 const SCEV *OtherOpsAdd = getAddExpr(OtherOps); 2543 unsigned LZ = GetMinLeadingZeros(OtherOpsAdd); 2544 2545 // If the input is known to be 0 or 1, the output is 0/-1, which is all 2546 // sign bits set. 2547 if (LZ == BitWidth - 1) 2548 return BitWidth; 2549 2550 // If we are subtracting one from a positive number, there is no carry 2551 // out of the result. 2552 if (LZ > 0) 2553 return GetMinSignBits(OtherOpsAdd); 2554 } 2555 2556 // Add can have at most one carry bit. Thus we know that the output 2557 // is, at worst, one more bit than the inputs. 2558 unsigned Min = BitWidth; 2559 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) { 2560 unsigned N = GetMinSignBits(A->getOperand(i)); 2561 Min = std::min(Min, N) - 1; 2562 if (Min == 0) return 1; 2563 } 2564 return 1; 2565 } 2566 2567 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { 2568 // For a SCEVUnknown, ask ValueTracking. 2569 return ComputeNumSignBits(U->getValue(), TD); 2570 } 2571 2572 return 1; 2573 } 2574 2575 /// createSCEV - We know that there is no SCEV for the specified value. 2576 /// Analyze the expression. 2577 /// 2578 const SCEV* ScalarEvolution::createSCEV(Value *V) { 2579 if (!isSCEVable(V->getType())) 2580 return getUnknown(V); 2581 2582 unsigned Opcode = Instruction::UserOp1; 2583 if (Instruction *I = dyn_cast<Instruction>(V)) 2584 Opcode = I->getOpcode(); 2585 else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) 2586 Opcode = CE->getOpcode(); 2587 else if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) 2588 return getConstant(CI); 2589 else if (isa<ConstantPointerNull>(V)) 2590 return getIntegerSCEV(0, V->getType()); 2591 else if (isa<UndefValue>(V)) 2592 return getIntegerSCEV(0, V->getType()); 2593 else 2594 return getUnknown(V); 2595 2596 User *U = cast<User>(V); 2597 switch (Opcode) { 2598 case Instruction::Add: 2599 return getAddExpr(getSCEV(U->getOperand(0)), 2600 getSCEV(U->getOperand(1))); 2601 case Instruction::Mul: 2602 return getMulExpr(getSCEV(U->getOperand(0)), 2603 getSCEV(U->getOperand(1))); 2604 case Instruction::UDiv: 2605 return getUDivExpr(getSCEV(U->getOperand(0)), 2606 getSCEV(U->getOperand(1))); 2607 case Instruction::Sub: 2608 return getMinusSCEV(getSCEV(U->getOperand(0)), 2609 getSCEV(U->getOperand(1))); 2610 case Instruction::And: 2611 // For an expression like x&255 that merely masks off the high bits, 2612 // use zext(trunc(x)) as the SCEV expression. 2613 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { 2614 if (CI->isNullValue()) 2615 return getSCEV(U->getOperand(1)); 2616 if (CI->isAllOnesValue()) 2617 return getSCEV(U->getOperand(0)); 2618 const APInt &A = CI->getValue(); 2619 2620 // Instcombine's ShrinkDemandedConstant may strip bits out of 2621 // constants, obscuring what would otherwise be a low-bits mask. 2622 // Use ComputeMaskedBits to compute what ShrinkDemandedConstant 2623 // knew about to reconstruct a low-bits mask value. 2624 unsigned LZ = A.countLeadingZeros(); 2625 unsigned BitWidth = A.getBitWidth(); 2626 APInt AllOnes = APInt::getAllOnesValue(BitWidth); 2627 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0); 2628 ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD); 2629 2630 APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ); 2631 2632 if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask)) 2633 return 2634 getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)), 2635 IntegerType::get(BitWidth - LZ)), 2636 U->getType()); 2637 } 2638 break; 2639 2640 case Instruction::Or: 2641 // If the RHS of the Or is a constant, we may have something like: 2642 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop 2643 // optimizations will transparently handle this case. 2644 // 2645 // In order for this transformation to be safe, the LHS must be of the 2646 // form X*(2^n) and the Or constant must be less than 2^n. 2647 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { 2648 const SCEV* LHS = getSCEV(U->getOperand(0)); 2649 const APInt &CIVal = CI->getValue(); 2650 if (GetMinTrailingZeros(LHS) >= 2651 (CIVal.getBitWidth() - CIVal.countLeadingZeros())) 2652 return getAddExpr(LHS, getSCEV(U->getOperand(1))); 2653 } 2654 break; 2655 case Instruction::Xor: 2656 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) { 2657 // If the RHS of the xor is a signbit, then this is just an add. 2658 // Instcombine turns add of signbit into xor as a strength reduction step. 2659 if (CI->getValue().isSignBit()) 2660 return getAddExpr(getSCEV(U->getOperand(0)), 2661 getSCEV(U->getOperand(1))); 2662 2663 // If the RHS of xor is -1, then this is a not operation. 2664 if (CI->isAllOnesValue()) 2665 return getNotSCEV(getSCEV(U->getOperand(0))); 2666 2667 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask. 2668 // This is a variant of the check for xor with -1, and it handles 2669 // the case where instcombine has trimmed non-demanded bits out 2670 // of an xor with -1. 2671 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0))) 2672 if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1))) 2673 if (BO->getOpcode() == Instruction::And && 2674 LCI->getValue() == CI->getValue()) 2675 if (const SCEVZeroExtendExpr *Z = 2676 dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) { 2677 const Type *UTy = U->getType(); 2678 const SCEV* Z0 = Z->getOperand(); 2679 const Type *Z0Ty = Z0->getType(); 2680 unsigned Z0TySize = getTypeSizeInBits(Z0Ty); 2681 2682 // If C is a low-bits mask, the zero extend is zerving to 2683 // mask off the high bits. Complement the operand and 2684 // re-apply the zext. 2685 if (APIntOps::isMask(Z0TySize, CI->getValue())) 2686 return getZeroExtendExpr(getNotSCEV(Z0), UTy); 2687 2688 // If C is a single bit, it may be in the sign-bit position 2689 // before the zero-extend. In this case, represent the xor 2690 // using an add, which is equivalent, and re-apply the zext. 2691 APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize); 2692 if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() && 2693 Trunc.isSignBit()) 2694 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)), 2695 UTy); 2696 } 2697 } 2698 break; 2699 2700 case Instruction::Shl: 2701 // Turn shift left of a constant amount into a multiply. 2702 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { 2703 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); 2704 Constant *X = ConstantInt::get( 2705 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); 2706 return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X)); 2707 } 2708 break; 2709 2710 case Instruction::LShr: 2711 // Turn logical shift right of a constant into a unsigned divide. 2712 if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { 2713 uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); 2714 Constant *X = ConstantInt::get( 2715 APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth))); 2716 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X)); 2717 } 2718 break; 2719 2720 case Instruction::AShr: 2721 // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression. 2722 if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) 2723 if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0))) 2724 if (L->getOpcode() == Instruction::Shl && 2725 L->getOperand(1) == U->getOperand(1)) { 2726 unsigned BitWidth = getTypeSizeInBits(U->getType()); 2727 uint64_t Amt = BitWidth - CI->getZExtValue(); 2728 if (Amt == BitWidth) 2729 return getSCEV(L->getOperand(0)); // shift by zero --> noop 2730 if (Amt > BitWidth) 2731 return getIntegerSCEV(0, U->getType()); // value is undefined 2732 return 2733 getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)), 2734 IntegerType::get(Amt)), 2735 U->getType()); 2736 } 2737 break; 2738 2739 case Instruction::Trunc: 2740 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType()); 2741 2742 case Instruction::ZExt: 2743 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType()); 2744 2745 case Instruction::SExt: 2746 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType()); 2747 2748 case Instruction::BitCast: 2749 // BitCasts are no-op casts so we just eliminate the cast. 2750 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) 2751 return getSCEV(U->getOperand(0)); 2752 break; 2753 2754 case Instruction::IntToPtr: 2755 if (!TD) break; // Without TD we can't analyze pointers. 2756 return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), 2757 TD->getIntPtrType()); 2758 2759 case Instruction::PtrToInt: 2760 if (!TD) break; // Without TD we can't analyze pointers. 2761 return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)), 2762 U->getType()); 2763 2764 case Instruction::GetElementPtr: 2765 if (!TD) break; // Without TD we can't analyze pointers. 2766 return createNodeForGEP(U); 2767 2768 case Instruction::PHI: 2769 return createNodeForPHI(cast<PHINode>(U)); 2770 2771 case Instruction::Select: 2772 // This could be a smax or umax that was lowered earlier. 2773 // Try to recover it. 2774 if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) { 2775 Value *LHS = ICI->getOperand(0); 2776 Value *RHS = ICI->getOperand(1); 2777 switch (ICI->getPredicate()) { 2778 case ICmpInst::ICMP_SLT: 2779 case ICmpInst::ICMP_SLE: 2780 std::swap(LHS, RHS); 2781 // fall through 2782 case ICmpInst::ICMP_SGT: 2783 case ICmpInst::ICMP_SGE: 2784 if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) 2785 return getSMaxExpr(getSCEV(LHS), getSCEV(RHS)); 2786 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) 2787 return getSMinExpr(getSCEV(LHS), getSCEV(RHS)); 2788 break; 2789 case ICmpInst::ICMP_ULT: 2790 case ICmpInst::ICMP_ULE: 2791 std::swap(LHS, RHS); 2792 // fall through 2793 case ICmpInst::ICMP_UGT: 2794 case ICmpInst::ICMP_UGE: 2795 if (LHS == U->getOperand(1) && RHS == U->getOperand(2)) 2796 return getUMaxExpr(getSCEV(LHS), getSCEV(RHS)); 2797 else if (LHS == U->getOperand(2) && RHS == U->getOperand(1)) 2798 return getUMinExpr(getSCEV(LHS), getSCEV(RHS)); 2799 break; 2800 case ICmpInst::ICMP_NE: 2801 // n != 0 ? n : 1 -> umax(n, 1) 2802 if (LHS == U->getOperand(1) && 2803 isa<ConstantInt>(U->getOperand(2)) && 2804 cast<ConstantInt>(U->getOperand(2))->isOne() && 2805 isa<ConstantInt>(RHS) && 2806 cast<ConstantInt>(RHS)->isZero()) 2807 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2))); 2808 break; 2809 case ICmpInst::ICMP_EQ: 2810 // n == 0 ? 1 : n -> umax(n, 1) 2811 if (LHS == U->getOperand(2) && 2812 isa<ConstantInt>(U->getOperand(1)) && 2813 cast<ConstantInt>(U->getOperand(1))->isOne() && 2814 isa<ConstantInt>(RHS) && 2815 cast<ConstantInt>(RHS)->isZero()) 2816 return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1))); 2817 break; 2818 default: 2819 break; 2820 } 2821 } 2822 2823 default: // We cannot analyze this expression. 2824 break; 2825 } 2826 2827 return getUnknown(V); 2828 } 2829 2830 2831 2832 //===----------------------------------------------------------------------===// 2833 // Iteration Count Computation Code 2834 // 2835 2836 /// getBackedgeTakenCount - If the specified loop has a predictable 2837 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute 2838 /// object. The backedge-taken count is the number of times the loop header 2839 /// will be branched to from within the loop. This is one less than the 2840 /// trip count of the loop, since it doesn't count the first iteration, 2841 /// when the header is branched to from outside the loop. 2842 /// 2843 /// Note that it is not valid to call this method on a loop without a 2844 /// loop-invariant backedge-taken count (see 2845 /// hasLoopInvariantBackedgeTakenCount). 2846 /// 2847 const SCEV* ScalarEvolution::getBackedgeTakenCount(const Loop *L) { 2848 return getBackedgeTakenInfo(L).Exact; 2849 } 2850 2851 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except 2852 /// return the least SCEV value that is known never to be less than the 2853 /// actual backedge taken count. 2854 const SCEV* ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) { 2855 return getBackedgeTakenInfo(L).Max; 2856 } 2857 2858 const ScalarEvolution::BackedgeTakenInfo & 2859 ScalarEvolution::getBackedgeTakenInfo(const Loop *L) { 2860 // Initially insert a CouldNotCompute for this loop. If the insertion 2861 // succeeds, procede to actually compute a backedge-taken count and 2862 // update the value. The temporary CouldNotCompute value tells SCEV 2863 // code elsewhere that it shouldn't attempt to request a new 2864 // backedge-taken count, which could result in infinite recursion. 2865 std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair = 2866 BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute())); 2867 if (Pair.second) { 2868 BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L); 2869 if (ItCount.Exact != getCouldNotCompute()) { 2870 assert(ItCount.Exact->isLoopInvariant(L) && 2871 ItCount.Max->isLoopInvariant(L) && 2872 "Computed trip count isn't loop invariant for loop!"); 2873 ++NumTripCountsComputed; 2874 2875 // Update the value in the map. 2876 Pair.first->second = ItCount; 2877 } else { 2878 if (ItCount.Max != getCouldNotCompute()) 2879 // Update the value in the map. 2880 Pair.first->second = ItCount; 2881 if (isa<PHINode>(L->getHeader()->begin())) 2882 // Only count loops that have phi nodes as not being computable. 2883 ++NumTripCountsNotComputed; 2884 } 2885 2886 // Now that we know more about the trip count for this loop, forget any 2887 // existing SCEV values for PHI nodes in this loop since they are only 2888 // conservative estimates made without the benefit 2889 // of trip count information. 2890 if (ItCount.hasAnyInfo()) 2891 forgetLoopPHIs(L); 2892 } 2893 return Pair.first->second; 2894 } 2895 2896 /// forgetLoopBackedgeTakenCount - This method should be called by the 2897 /// client when it has changed a loop in a way that may effect 2898 /// ScalarEvolution's ability to compute a trip count, or if the loop 2899 /// is deleted. 2900 void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) { 2901 BackedgeTakenCounts.erase(L); 2902 forgetLoopPHIs(L); 2903 } 2904 2905 /// forgetLoopPHIs - Delete the memoized SCEVs associated with the 2906 /// PHI nodes in the given loop. This is used when the trip count of 2907 /// the loop may have changed. 2908 void ScalarEvolution::forgetLoopPHIs(const Loop *L) { 2909 BasicBlock *Header = L->getHeader(); 2910 2911 // Push all Loop-header PHIs onto the Worklist stack, except those 2912 // that are presently represented via a SCEVUnknown. SCEVUnknown for 2913 // a PHI either means that it has an unrecognized structure, or it's 2914 // a PHI that's in the progress of being computed by createNodeForPHI. 2915 // In the former case, additional loop trip count information isn't 2916 // going to change anything. In the later case, createNodeForPHI will 2917 // perform the necessary updates on its own when it gets to that point. 2918 SmallVector<Instruction *, 16> Worklist; 2919 for (BasicBlock::iterator I = Header->begin(); 2920 PHINode *PN = dyn_cast<PHINode>(I); ++I) { 2921 std::map<SCEVCallbackVH, const SCEV*>::iterator It = 2922 Scalars.find((Value*)I); 2923 if (It != Scalars.end() && !isa<SCEVUnknown>(It->second)) 2924 Worklist.push_back(PN); 2925 } 2926 2927 while (!Worklist.empty()) { 2928 Instruction *I = Worklist.pop_back_val(); 2929 if (Scalars.erase(I)) 2930 for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); 2931 UI != UE; ++UI) 2932 Worklist.push_back(cast<Instruction>(UI)); 2933 } 2934 } 2935 2936 /// ComputeBackedgeTakenCount - Compute the number of times the backedge 2937 /// of the specified loop will execute. 2938 ScalarEvolution::BackedgeTakenInfo 2939 ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) { 2940 SmallVector<BasicBlock*, 8> ExitingBlocks; 2941 L->getExitingBlocks(ExitingBlocks); 2942 2943 // Examine all exits and pick the most conservative values. 2944 const SCEV* BECount = getCouldNotCompute(); 2945 const SCEV* MaxBECount = getCouldNotCompute(); 2946 bool CouldNotComputeBECount = false; 2947 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) { 2948 BackedgeTakenInfo NewBTI = 2949 ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]); 2950 2951 if (NewBTI.Exact == getCouldNotCompute()) { 2952 // We couldn't compute an exact value for this exit, so 2953 // we won't be able to compute an exact value for the loop. 2954 CouldNotComputeBECount = true; 2955 BECount = getCouldNotCompute(); 2956 } else if (!CouldNotComputeBECount) { 2957 if (BECount == getCouldNotCompute()) 2958 BECount = NewBTI.Exact; 2959 else 2960 BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact); 2961 } 2962 if (MaxBECount == getCouldNotCompute()) 2963 MaxBECount = NewBTI.Max; 2964 else if (NewBTI.Max != getCouldNotCompute()) 2965 MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max); 2966 } 2967 2968 return BackedgeTakenInfo(BECount, MaxBECount); 2969 } 2970 2971 /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge 2972 /// of the specified loop will execute if it exits via the specified block. 2973 ScalarEvolution::BackedgeTakenInfo 2974 ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L, 2975 BasicBlock *ExitingBlock) { 2976 2977 // Okay, we've chosen an exiting block. See what condition causes us to 2978 // exit at this block. 2979 // 2980 // FIXME: we should be able to handle switch instructions (with a single exit) 2981 BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator()); 2982 if (ExitBr == 0) return getCouldNotCompute(); 2983 assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!"); 2984 2985 // At this point, we know we have a conditional branch that determines whether 2986 // the loop is exited. However, we don't know if the branch is executed each 2987 // time through the loop. If not, then the execution count of the branch will 2988 // not be equal to the trip count of the loop. 2989 // 2990 // Currently we check for this by checking to see if the Exit branch goes to 2991 // the loop header. If so, we know it will always execute the same number of 2992 // times as the loop. We also handle the case where the exit block *is* the 2993 // loop header. This is common for un-rotated loops. 2994 // 2995 // If both of those tests fail, walk up the unique predecessor chain to the 2996 // header, stopping if there is an edge that doesn't exit the loop. If the 2997 // header is reached, the execution count of the branch will be equal to the 2998 // trip count of the loop. 2999 // 3000 // More extensive analysis could be done to handle more cases here. 3001 // 3002 if (ExitBr->getSuccessor(0) != L->getHeader() && 3003 ExitBr->getSuccessor(1) != L->getHeader() && 3004 ExitBr->getParent() != L->getHeader()) { 3005 // The simple checks failed, try climbing the unique predecessor chain 3006 // up to the header. 3007 bool Ok = false; 3008 for (BasicBlock *BB = ExitBr->getParent(); BB; ) { 3009 BasicBlock *Pred = BB->getUniquePredecessor(); 3010 if (!Pred) 3011 return getCouldNotCompute(); 3012 TerminatorInst *PredTerm = Pred->getTerminator(); 3013 for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) { 3014 BasicBlock *PredSucc = PredTerm->getSuccessor(i); 3015 if (PredSucc == BB) 3016 continue; 3017 // If the predecessor has a successor that isn't BB and isn't 3018 // outside the loop, assume the worst. 3019 if (L->contains(PredSucc)) 3020 return getCouldNotCompute(); 3021 } 3022 if (Pred == L->getHeader()) { 3023 Ok = true; 3024 break; 3025 } 3026 BB = Pred; 3027 } 3028 if (!Ok) 3029 return getCouldNotCompute(); 3030 } 3031 3032 // Procede to the next level to examine the exit condition expression. 3033 return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(), 3034 ExitBr->getSuccessor(0), 3035 ExitBr->getSuccessor(1)); 3036 } 3037 3038 /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the 3039 /// backedge of the specified loop will execute if its exit condition 3040 /// were a conditional branch of ExitCond, TBB, and FBB. 3041 ScalarEvolution::BackedgeTakenInfo 3042 ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L, 3043 Value *ExitCond, 3044 BasicBlock *TBB, 3045 BasicBlock *FBB) { 3046 // Check if the controlling expression for this loop is an And or Or. 3047 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) { 3048 if (BO->getOpcode() == Instruction::And) { 3049 // Recurse on the operands of the and. 3050 BackedgeTakenInfo BTI0 = 3051 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); 3052 BackedgeTakenInfo BTI1 = 3053 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); 3054 const SCEV* BECount = getCouldNotCompute(); 3055 const SCEV* MaxBECount = getCouldNotCompute(); 3056 if (L->contains(TBB)) { 3057 // Both conditions must be true for the loop to continue executing. 3058 // Choose the less conservative count. 3059 if (BTI0.Exact == getCouldNotCompute() || 3060 BTI1.Exact == getCouldNotCompute()) 3061 BECount = getCouldNotCompute(); 3062 else 3063 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); 3064 if (BTI0.Max == getCouldNotCompute()) 3065 MaxBECount = BTI1.Max; 3066 else if (BTI1.Max == getCouldNotCompute()) 3067 MaxBECount = BTI0.Max; 3068 else 3069 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); 3070 } else { 3071 // Both conditions must be true for the loop to exit. 3072 assert(L->contains(FBB) && "Loop block has no successor in loop!"); 3073 if (BTI0.Exact != getCouldNotCompute() && 3074 BTI1.Exact != getCouldNotCompute()) 3075 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); 3076 if (BTI0.Max != getCouldNotCompute() && 3077 BTI1.Max != getCouldNotCompute()) 3078 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); 3079 } 3080 3081 return BackedgeTakenInfo(BECount, MaxBECount); 3082 } 3083 if (BO->getOpcode() == Instruction::Or) { 3084 // Recurse on the operands of the or. 3085 BackedgeTakenInfo BTI0 = 3086 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB); 3087 BackedgeTakenInfo BTI1 = 3088 ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB); 3089 const SCEV* BECount = getCouldNotCompute(); 3090 const SCEV* MaxBECount = getCouldNotCompute(); 3091 if (L->contains(FBB)) { 3092 // Both conditions must be false for the loop to continue executing. 3093 // Choose the less conservative count. 3094 if (BTI0.Exact == getCouldNotCompute() || 3095 BTI1.Exact == getCouldNotCompute()) 3096 BECount = getCouldNotCompute(); 3097 else 3098 BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact); 3099 if (BTI0.Max == getCouldNotCompute()) 3100 MaxBECount = BTI1.Max; 3101 else if (BTI1.Max == getCouldNotCompute()) 3102 MaxBECount = BTI0.Max; 3103 else 3104 MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max); 3105 } else { 3106 // Both conditions must be false for the loop to exit. 3107 assert(L->contains(TBB) && "Loop block has no successor in loop!"); 3108 if (BTI0.Exact != getCouldNotCompute() && 3109 BTI1.Exact != getCouldNotCompute()) 3110 BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact); 3111 if (BTI0.Max != getCouldNotCompute() && 3112 BTI1.Max != getCouldNotCompute()) 3113 MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max); 3114 } 3115 3116 return BackedgeTakenInfo(BECount, MaxBECount); 3117 } 3118 } 3119 3120 // With an icmp, it may be feasible to compute an exact backedge-taken count. 3121 // Procede to the next level to examine the icmp. 3122 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) 3123 return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB); 3124 3125 // If it's not an integer or pointer comparison then compute it the hard way. 3126 return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); 3127 } 3128 3129 /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the 3130 /// backedge of the specified loop will execute if its exit condition 3131 /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB. 3132 ScalarEvolution::BackedgeTakenInfo 3133 ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L, 3134 ICmpInst *ExitCond, 3135 BasicBlock *TBB, 3136 BasicBlock *FBB) { 3137 3138 // If the condition was exit on true, convert the condition to exit on false 3139 ICmpInst::Predicate Cond; 3140 if (!L->contains(FBB)) 3141 Cond = ExitCond->getPredicate(); 3142 else 3143 Cond = ExitCond->getInversePredicate(); 3144 3145 // Handle common loops like: for (X = "string"; *X; ++X) 3146 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0))) 3147 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) { 3148 const SCEV* ItCnt = 3149 ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond); 3150 if (!isa<SCEVCouldNotCompute>(ItCnt)) { 3151 unsigned BitWidth = getTypeSizeInBits(ItCnt->getType()); 3152 return BackedgeTakenInfo(ItCnt, 3153 isa<SCEVConstant>(ItCnt) ? ItCnt : 3154 getConstant(APInt::getMaxValue(BitWidth)-1)); 3155 } 3156 } 3157 3158 const SCEV* LHS = getSCEV(ExitCond->getOperand(0)); 3159 const SCEV* RHS = getSCEV(ExitCond->getOperand(1)); 3160 3161 // Try to evaluate any dependencies out of the loop. 3162 LHS = getSCEVAtScope(LHS, L); 3163 RHS = getSCEVAtScope(RHS, L); 3164 3165 // At this point, we would like to compute how many iterations of the 3166 // loop the predicate will return true for these inputs. 3167 if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) { 3168 // If there is a loop-invariant, force it into the RHS. 3169 std::swap(LHS, RHS); 3170 Cond = ICmpInst::getSwappedPredicate(Cond); 3171 } 3172 3173 // If we have a comparison of a chrec against a constant, try to use value 3174 // ranges to answer this query. 3175 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) 3176 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS)) 3177 if (AddRec->getLoop() == L) { 3178 // Form the constant range. 3179 ConstantRange CompRange( 3180 ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue())); 3181 3182 const SCEV* Ret = AddRec->getNumIterationsInRange(CompRange, *this); 3183 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret; 3184 } 3185 3186 switch (Cond) { 3187 case ICmpInst::ICMP_NE: { // while (X != Y) 3188 // Convert to: while (X-Y != 0) 3189 const SCEV* TC = HowFarToZero(getMinusSCEV(LHS, RHS), L); 3190 if (!isa<SCEVCouldNotCompute>(TC)) return TC; 3191 break; 3192 } 3193 case ICmpInst::ICMP_EQ: { 3194 // Convert to: while (X-Y == 0) // while (X == Y) 3195 const SCEV* TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L); 3196 if (!isa<SCEVCouldNotCompute>(TC)) return TC; 3197 break; 3198 } 3199 case ICmpInst::ICMP_SLT: { 3200 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true); 3201 if (BTI.hasAnyInfo()) return BTI; 3202 break; 3203 } 3204 case ICmpInst::ICMP_SGT: { 3205 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), 3206 getNotSCEV(RHS), L, true); 3207 if (BTI.hasAnyInfo()) return BTI; 3208 break; 3209 } 3210 case ICmpInst::ICMP_ULT: { 3211 BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false); 3212 if (BTI.hasAnyInfo()) return BTI; 3213 break; 3214 } 3215 case ICmpInst::ICMP_UGT: { 3216 BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS), 3217 getNotSCEV(RHS), L, false); 3218 if (BTI.hasAnyInfo()) return BTI; 3219 break; 3220 } 3221 default: 3222 #if 0 3223 errs() << "ComputeBackedgeTakenCount "; 3224 if (ExitCond->getOperand(0)->getType()->isUnsigned()) 3225 errs() << "[unsigned] "; 3226 errs() << *LHS << " " 3227 << Instruction::getOpcodeName(Instruction::ICmp) 3228 << " " << *RHS << "\n"; 3229 #endif 3230 break; 3231 } 3232 return 3233 ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB)); 3234 } 3235 3236 static ConstantInt * 3237 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, 3238 ScalarEvolution &SE) { 3239 const SCEV* InVal = SE.getConstant(C); 3240 const SCEV* Val = AddRec->evaluateAtIteration(InVal, SE); 3241 assert(isa<SCEVConstant>(Val) && 3242 "Evaluation of SCEV at constant didn't fold correctly?"); 3243 return cast<SCEVConstant>(Val)->getValue(); 3244 } 3245 3246 /// GetAddressedElementFromGlobal - Given a global variable with an initializer 3247 /// and a GEP expression (missing the pointer index) indexing into it, return 3248 /// the addressed element of the initializer or null if the index expression is 3249 /// invalid. 3250 static Constant * 3251 GetAddressedElementFromGlobal(GlobalVariable *GV, 3252 const std::vector<ConstantInt*> &Indices) { 3253 Constant *Init = GV->getInitializer(); 3254 for (unsigned i = 0, e = Indices.size(); i != e; ++i) { 3255 uint64_t Idx = Indices[i]->getZExtValue(); 3256 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) { 3257 assert(Idx < CS->getNumOperands() && "Bad struct index!"); 3258 Init = cast<Constant>(CS->getOperand(Idx)); 3259 } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) { 3260 if (Idx >= CA->getNumOperands()) return 0; // Bogus program 3261 Init = cast<Constant>(CA->getOperand(Idx)); 3262 } else if (isa<ConstantAggregateZero>(Init)) { 3263 if (const StructType *STy = dyn_cast<StructType>(Init->getType())) { 3264 assert(Idx < STy->getNumElements() && "Bad struct index!"); 3265 Init = Constant::getNullValue(STy->getElementType(Idx)); 3266 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) { 3267 if (Idx >= ATy->getNumElements()) return 0; // Bogus program 3268 Init = Constant::getNullValue(ATy->getElementType()); 3269 } else { 3270 assert(0 && "Unknown constant aggregate type!"); 3271 } 3272 return 0; 3273 } else { 3274 return 0; // Unknown initializer type 3275 } 3276 } 3277 return Init; 3278 } 3279 3280 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of 3281 /// 'icmp op load X, cst', try to see if we can compute the backedge 3282 /// execution count. 3283 const SCEV * 3284 ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount( 3285 LoadInst *LI, 3286 Constant *RHS, 3287 const Loop *L, 3288 ICmpInst::Predicate predicate) { 3289 if (LI->isVolatile()) return getCouldNotCompute(); 3290 3291 // Check to see if the loaded pointer is a getelementptr of a global. 3292 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0)); 3293 if (!GEP) return getCouldNotCompute(); 3294 3295 // Make sure that it is really a constant global we are gepping, with an 3296 // initializer, and make sure the first IDX is really 0. 3297 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0)); 3298 if (!GV || !GV->isConstant() || !GV->hasInitializer() || 3299 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) || 3300 !cast<Constant>(GEP->getOperand(1))->isNullValue()) 3301 return getCouldNotCompute(); 3302 3303 // Okay, we allow one non-constant index into the GEP instruction. 3304 Value *VarIdx = 0; 3305 std::vector<ConstantInt*> Indexes; 3306 unsigned VarIdxNum = 0; 3307 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i) 3308 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) { 3309 Indexes.push_back(CI); 3310 } else if (!isa<ConstantInt>(GEP->getOperand(i))) { 3311 if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's. 3312 VarIdx = GEP->getOperand(i); 3313 VarIdxNum = i-2; 3314 Indexes.push_back(0); 3315 } 3316 3317 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant. 3318 // Check to see if X is a loop variant variable value now. 3319 const SCEV* Idx = getSCEV(VarIdx); 3320 Idx = getSCEVAtScope(Idx, L); 3321 3322 // We can only recognize very limited forms of loop index expressions, in 3323 // particular, only affine AddRec's like {C1,+,C2}. 3324 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx); 3325 if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) || 3326 !isa<SCEVConstant>(IdxExpr->getOperand(0)) || 3327 !isa<SCEVConstant>(IdxExpr->getOperand(1))) 3328 return getCouldNotCompute(); 3329 3330 unsigned MaxSteps = MaxBruteForceIterations; 3331 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) { 3332 ConstantInt *ItCst = 3333 ConstantInt::get(cast<IntegerType>(IdxExpr->getType()), IterationNum); 3334 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this); 3335 3336 // Form the GEP offset. 3337 Indexes[VarIdxNum] = Val; 3338 3339 Constant *Result = GetAddressedElementFromGlobal(GV, Indexes); 3340 if (Result == 0) break; // Cannot compute! 3341 3342 // Evaluate the condition for this iteration. 3343 Result = ConstantExpr::getICmp(predicate, Result, RHS); 3344 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure 3345 if (cast<ConstantInt>(Result)->getValue().isMinValue()) { 3346 #if 0 3347 errs() << "\n***\n*** Computed loop count " << *ItCst 3348 << "\n*** From global " << *GV << "*** BB: " << *L->getHeader() 3349 << "***\n"; 3350 #endif 3351 ++NumArrayLenItCounts; 3352 return getConstant(ItCst); // Found terminating iteration! 3353 } 3354 } 3355 return getCouldNotCompute(); 3356 } 3357 3358 3359 /// CanConstantFold - Return true if we can constant fold an instruction of the 3360 /// specified type, assuming that all operands were constants. 3361 static bool CanConstantFold(const Instruction *I) { 3362 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) || 3363 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I)) 3364 return true; 3365 3366 if (const CallInst *CI = dyn_cast<CallInst>(I)) 3367 if (const Function *F = CI->getCalledFunction()) 3368 return canConstantFoldCallTo(F); 3369 return false; 3370 } 3371 3372 /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node 3373 /// in the loop that V is derived from. We allow arbitrary operations along the 3374 /// way, but the operands of an operation must either be constants or a value 3375 /// derived from a constant PHI. If this expression does not fit with these 3376 /// constraints, return null. 3377 static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) { 3378 // If this is not an instruction, or if this is an instruction outside of the 3379 // loop, it can't be derived from a loop PHI. 3380 Instruction *I = dyn_cast<Instruction>(V); 3381 if (I == 0 || !L->contains(I->getParent())) return 0; 3382 3383 if (PHINode *PN = dyn_cast<PHINode>(I)) { 3384 if (L->getHeader() == I->getParent()) 3385 return PN; 3386 else 3387 // We don't currently keep track of the control flow needed to evaluate 3388 // PHIs, so we cannot handle PHIs inside of loops. 3389 return 0; 3390 } 3391 3392 // If we won't be able to constant fold this expression even if the operands 3393 // are constants, return early. 3394 if (!CanConstantFold(I)) return 0; 3395 3396 // Otherwise, we can evaluate this instruction if all of its operands are 3397 // constant or derived from a PHI node themselves. 3398 PHINode *PHI = 0; 3399 for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op) 3400 if (!(isa<Constant>(I->getOperand(Op)) || 3401 isa<GlobalValue>(I->getOperand(Op)))) { 3402 PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L); 3403 if (P == 0) return 0; // Not evolving from PHI 3404 if (PHI == 0) 3405 PHI = P; 3406 else if (PHI != P) 3407 return 0; // Evolving from multiple different PHIs. 3408 } 3409 3410 // This is a expression evolving from a constant PHI! 3411 return PHI; 3412 } 3413 3414 /// EvaluateExpression - Given an expression that passes the 3415 /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node 3416 /// in the loop has the value PHIVal. If we can't fold this expression for some 3417 /// reason, return null. 3418 static Constant *EvaluateExpression(Value *V, Constant *PHIVal) { 3419 if (isa<PHINode>(V)) return PHIVal; 3420 if (Constant *C = dyn_cast<Constant>(V)) return C; 3421 if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV; 3422 Instruction *I = cast<Instruction>(V); 3423 LLVMContext* Context = I->getParent()->getContext(); 3424 3425 std::vector<Constant*> Operands; 3426 Operands.resize(I->getNumOperands()); 3427 3428 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 3429 Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal); 3430 if (Operands[i] == 0) return 0; 3431 } 3432 3433 if (const CmpInst *CI = dyn_cast<CmpInst>(I)) 3434 return ConstantFoldCompareInstOperands(CI->getPredicate(), 3435 &Operands[0], Operands.size(), 3436 Context); 3437 else 3438 return ConstantFoldInstOperands(I->getOpcode(), I->getType(), 3439 &Operands[0], Operands.size(), 3440 Context); 3441 } 3442 3443 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is 3444 /// in the header of its containing loop, we know the loop executes a 3445 /// constant number of times, and the PHI node is just a recurrence 3446 /// involving constants, fold it. 3447 Constant * 3448 ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN, 3449 const APInt& BEs, 3450 const Loop *L) { 3451 std::map<PHINode*, Constant*>::iterator I = 3452 ConstantEvolutionLoopExitValue.find(PN); 3453 if (I != ConstantEvolutionLoopExitValue.end()) 3454 return I->second; 3455 3456 if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations))) 3457 return ConstantEvolutionLoopExitValue[PN] = 0; // Not going to evaluate it. 3458 3459 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN]; 3460 3461 // Since the loop is canonicalized, the PHI node must have two entries. One 3462 // entry must be a constant (coming in from outside of the loop), and the 3463 // second must be derived from the same PHI. 3464 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); 3465 Constant *StartCST = 3466 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); 3467 if (StartCST == 0) 3468 return RetVal = 0; // Must be a constant. 3469 3470 Value *BEValue = PN->getIncomingValue(SecondIsBackedge); 3471 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); 3472 if (PN2 != PN) 3473 return RetVal = 0; // Not derived from same PHI. 3474 3475 // Execute the loop symbolically to determine the exit value. 3476 if (BEs.getActiveBits() >= 32) 3477 return RetVal = 0; // More than 2^32-1 iterations?? Not doing it! 3478 3479 unsigned NumIterations = BEs.getZExtValue(); // must be in range 3480 unsigned IterationNum = 0; 3481 for (Constant *PHIVal = StartCST; ; ++IterationNum) { 3482 if (IterationNum == NumIterations) 3483 return RetVal = PHIVal; // Got exit value! 3484 3485 // Compute the value of the PHI node for the next iteration. 3486 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); 3487 if (NextPHI == PHIVal) 3488 return RetVal = NextPHI; // Stopped evolving! 3489 if (NextPHI == 0) 3490 return 0; // Couldn't evaluate! 3491 PHIVal = NextPHI; 3492 } 3493 } 3494 3495 /// ComputeBackedgeTakenCountExhaustively - If the trip is known to execute a 3496 /// constant number of times (the condition evolves only from constants), 3497 /// try to evaluate a few iterations of the loop until we get the exit 3498 /// condition gets a value of ExitWhen (true or false). If we cannot 3499 /// evaluate the trip count of the loop, return getCouldNotCompute(). 3500 const SCEV * 3501 ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L, 3502 Value *Cond, 3503 bool ExitWhen) { 3504 PHINode *PN = getConstantEvolvingPHI(Cond, L); 3505 if (PN == 0) return getCouldNotCompute(); 3506 3507 // Since the loop is canonicalized, the PHI node must have two entries. One 3508 // entry must be a constant (coming in from outside of the loop), and the 3509 // second must be derived from the same PHI. 3510 bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1)); 3511 Constant *StartCST = 3512 dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge)); 3513 if (StartCST == 0) return getCouldNotCompute(); // Must be a constant. 3514 3515 Value *BEValue = PN->getIncomingValue(SecondIsBackedge); 3516 PHINode *PN2 = getConstantEvolvingPHI(BEValue, L); 3517 if (PN2 != PN) return getCouldNotCompute(); // Not derived from same PHI. 3518 3519 // Okay, we find a PHI node that defines the trip count of this loop. Execute 3520 // the loop symbolically to determine when the condition gets a value of 3521 // "ExitWhen". 3522 unsigned IterationNum = 0; 3523 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis. 3524 for (Constant *PHIVal = StartCST; 3525 IterationNum != MaxIterations; ++IterationNum) { 3526 ConstantInt *CondVal = 3527 dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal)); 3528 3529 // Couldn't symbolically evaluate. 3530 if (!CondVal) return getCouldNotCompute(); 3531 3532 if (CondVal->getValue() == uint64_t(ExitWhen)) { 3533 ++NumBruteForceTripCountsComputed; 3534 return getConstant(Type::Int32Ty, IterationNum); 3535 } 3536 3537 // Compute the value of the PHI node for the next iteration. 3538 Constant *NextPHI = EvaluateExpression(BEValue, PHIVal); 3539 if (NextPHI == 0 || NextPHI == PHIVal) 3540 return getCouldNotCompute();// Couldn't evaluate or not making progress... 3541 PHIVal = NextPHI; 3542 } 3543 3544 // Too many iterations were needed to evaluate. 3545 return getCouldNotCompute(); 3546 } 3547 3548 /// getSCEVAtScope - Return a SCEV expression handle for the specified value 3549 /// at the specified scope in the program. The L value specifies a loop 3550 /// nest to evaluate the expression at, where null is the top-level or a 3551 /// specified loop is immediately inside of the loop. 3552 /// 3553 /// This method can be used to compute the exit value for a variable defined 3554 /// in a loop by querying what the value will hold in the parent loop. 3555 /// 3556 /// In the case that a relevant loop exit value cannot be computed, the 3557 /// original value V is returned. 3558 const SCEV* ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) { 3559 // FIXME: this should be turned into a virtual method on SCEV! 3560 3561 if (isa<SCEVConstant>(V)) return V; 3562 3563 // If this instruction is evolved from a constant-evolving PHI, compute the 3564 // exit value from the loop without using SCEVs. 3565 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) { 3566 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) { 3567 const Loop *LI = (*this->LI)[I->getParent()]; 3568 if (LI && LI->getParentLoop() == L) // Looking for loop exit value. 3569 if (PHINode *PN = dyn_cast<PHINode>(I)) 3570 if (PN->getParent() == LI->getHeader()) { 3571 // Okay, there is no closed form solution for the PHI node. Check 3572 // to see if the loop that contains it has a known backedge-taken 3573 // count. If so, we may be able to force computation of the exit 3574 // value. 3575 const SCEV* BackedgeTakenCount = getBackedgeTakenCount(LI); 3576 if (const SCEVConstant *BTCC = 3577 dyn_cast<SCEVConstant>(BackedgeTakenCount)) { 3578 // Okay, we know how many times the containing loop executes. If 3579 // this is a constant evolving PHI node, get the final value at 3580 // the specified iteration number. 3581 Constant *RV = getConstantEvolutionLoopExitValue(PN, 3582 BTCC->getValue()->getValue(), 3583 LI); 3584 if (RV) return getSCEV(RV); 3585 } 3586 } 3587 3588 // Okay, this is an expression that we cannot symbolically evaluate 3589 // into a SCEV. Check to see if it's possible to symbolically evaluate 3590 // the arguments into constants, and if so, try to constant propagate the 3591 // result. This is particularly useful for computing loop exit values. 3592 if (CanConstantFold(I)) { 3593 // Check to see if we've folded this instruction at this loop before. 3594 std::map<const Loop *, Constant *> &Values = ValuesAtScopes[I]; 3595 std::pair<std::map<const Loop *, Constant *>::iterator, bool> Pair = 3596 Values.insert(std::make_pair(L, static_cast<Constant *>(0))); 3597 if (!Pair.second) 3598 return Pair.first->second ? &*getSCEV(Pair.first->second) : V; 3599 3600 std::vector<Constant*> Operands; 3601 Operands.reserve(I->getNumOperands()); 3602 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { 3603 Value *Op = I->getOperand(i); 3604 if (Constant *C = dyn_cast<Constant>(Op)) { 3605 Operands.push_back(C); 3606 } else { 3607 // If any of the operands is non-constant and if they are 3608 // non-integer and non-pointer, don't even try to analyze them 3609 // with scev techniques. 3610 if (!isSCEVable(Op->getType())) 3611 return V; 3612 3613 const SCEV* OpV = getSCEVAtScope(getSCEV(Op), L); 3614 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) { 3615 Constant *C = SC->getValue(); 3616 if (C->getType() != Op->getType()) 3617 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 3618 Op->getType(), 3619 false), 3620 C, Op->getType()); 3621 Operands.push_back(C); 3622 } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) { 3623 if (Constant *C = dyn_cast<Constant>(SU->getValue())) { 3624 if (C->getType() != Op->getType()) 3625 C = 3626 ConstantExpr::getCast(CastInst::getCastOpcode(C, false, 3627 Op->getType(), 3628 false), 3629 C, Op->getType()); 3630 Operands.push_back(C); 3631 } else 3632 return V; 3633 } else { 3634 return V; 3635 } 3636 } 3637 } 3638 3639 Constant *C; 3640 if (const CmpInst *CI = dyn_cast<CmpInst>(I)) 3641 C = ConstantFoldCompareInstOperands(CI->getPredicate(), 3642 &Operands[0], Operands.size(), 3643 Context); 3644 else 3645 C = ConstantFoldInstOperands(I->getOpcode(), I->getType(), 3646 &Operands[0], Operands.size(), Context); 3647 Pair.first->second = C; 3648 return getSCEV(C); 3649 } 3650 } 3651 3652 // This is some other type of SCEVUnknown, just return it. 3653 return V; 3654 } 3655 3656 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) { 3657 // Avoid performing the look-up in the common case where the specified 3658 // expression has no loop-variant portions. 3659 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) { 3660 const SCEV* OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); 3661 if (OpAtScope != Comm->getOperand(i)) { 3662 // Okay, at least one of these operands is loop variant but might be 3663 // foldable. Build a new instance of the folded commutative expression. 3664 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(), 3665 Comm->op_begin()+i); 3666 NewOps.push_back(OpAtScope); 3667 3668 for (++i; i != e; ++i) { 3669 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L); 3670 NewOps.push_back(OpAtScope); 3671 } 3672 if (isa<SCEVAddExpr>(Comm)) 3673 return getAddExpr(NewOps); 3674 if (isa<SCEVMulExpr>(Comm)) 3675 return getMulExpr(NewOps); 3676 if (isa<SCEVSMaxExpr>(Comm)) 3677 return getSMaxExpr(NewOps); 3678 if (isa<SCEVUMaxExpr>(Comm)) 3679 return getUMaxExpr(NewOps); 3680 assert(0 && "Unknown commutative SCEV type!"); 3681 } 3682 } 3683 // If we got here, all operands are loop invariant. 3684 return Comm; 3685 } 3686 3687 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) { 3688 const SCEV* LHS = getSCEVAtScope(Div->getLHS(), L); 3689 const SCEV* RHS = getSCEVAtScope(Div->getRHS(), L); 3690 if (LHS == Div->getLHS() && RHS == Div->getRHS()) 3691 return Div; // must be loop invariant 3692 return getUDivExpr(LHS, RHS); 3693 } 3694 3695 // If this is a loop recurrence for a loop that does not contain L, then we 3696 // are dealing with the final value computed by the loop. 3697 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { 3698 if (!L || !AddRec->getLoop()->contains(L->getHeader())) { 3699 // To evaluate this recurrence, we need to know how many times the AddRec 3700 // loop iterates. Compute this now. 3701 const SCEV* BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop()); 3702 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec; 3703 3704 // Then, evaluate the AddRec. 3705 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this); 3706 } 3707 return AddRec; 3708 } 3709 3710 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) { 3711 const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); 3712 if (Op == Cast->getOperand()) 3713 return Cast; // must be loop invariant 3714 return getZeroExtendExpr(Op, Cast->getType()); 3715 } 3716 3717 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) { 3718 const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); 3719 if (Op == Cast->getOperand()) 3720 return Cast; // must be loop invariant 3721 return getSignExtendExpr(Op, Cast->getType()); 3722 } 3723 3724 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) { 3725 const SCEV* Op = getSCEVAtScope(Cast->getOperand(), L); 3726 if (Op == Cast->getOperand()) 3727 return Cast; // must be loop invariant 3728 return getTruncateExpr(Op, Cast->getType()); 3729 } 3730 3731 assert(0 && "Unknown SCEV type!"); 3732 return 0; 3733 } 3734 3735 /// getSCEVAtScope - This is a convenience function which does 3736 /// getSCEVAtScope(getSCEV(V), L). 3737 const SCEV* ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) { 3738 return getSCEVAtScope(getSCEV(V), L); 3739 } 3740 3741 /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the 3742 /// following equation: 3743 /// 3744 /// A * X = B (mod N) 3745 /// 3746 /// where N = 2^BW and BW is the common bit width of A and B. The signedness of 3747 /// A and B isn't important. 3748 /// 3749 /// If the equation does not have a solution, SCEVCouldNotCompute is returned. 3750 static const SCEV* SolveLinEquationWithOverflow(const APInt &A, const APInt &B, 3751 ScalarEvolution &SE) { 3752 uint32_t BW = A.getBitWidth(); 3753 assert(BW == B.getBitWidth() && "Bit widths must be the same."); 3754 assert(A != 0 && "A must be non-zero."); 3755 3756 // 1. D = gcd(A, N) 3757 // 3758 // The gcd of A and N may have only one prime factor: 2. The number of 3759 // trailing zeros in A is its multiplicity 3760 uint32_t Mult2 = A.countTrailingZeros(); 3761 // D = 2^Mult2 3762 3763 // 2. Check if B is divisible by D. 3764 // 3765 // B is divisible by D if and only if the multiplicity of prime factor 2 for B 3766 // is not less than multiplicity of this prime factor for D. 3767 if (B.countTrailingZeros() < Mult2) 3768 return SE.getCouldNotCompute(); 3769 3770 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic 3771 // modulo (N / D). 3772 // 3773 // (N / D) may need BW+1 bits in its representation. Hence, we'll use this 3774 // bit width during computations. 3775 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D 3776 APInt Mod(BW + 1, 0); 3777 Mod.set(BW - Mult2); // Mod = N / D 3778 APInt I = AD.multiplicativeInverse(Mod); 3779 3780 // 4. Compute the minimum unsigned root of the equation: 3781 // I * (B / D) mod (N / D) 3782 APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod); 3783 3784 // The result is guaranteed to be less than 2^BW so we may truncate it to BW 3785 // bits. 3786 return SE.getConstant(Result.trunc(BW)); 3787 } 3788 3789 /// SolveQuadraticEquation - Find the roots of the quadratic equation for the 3790 /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which 3791 /// might be the same) or two SCEVCouldNotCompute objects. 3792 /// 3793 static std::pair<const SCEV*,const SCEV*> 3794 SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) { 3795 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!"); 3796 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0)); 3797 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1)); 3798 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2)); 3799 3800 // We currently can only solve this if the coefficients are constants. 3801 if (!LC || !MC || !NC) { 3802 const SCEV *CNC = SE.getCouldNotCompute(); 3803 return std::make_pair(CNC, CNC); 3804 } 3805 3806 uint32_t BitWidth = LC->getValue()->getValue().getBitWidth(); 3807 const APInt &L = LC->getValue()->getValue(); 3808 const APInt &M = MC->getValue()->getValue(); 3809 const APInt &N = NC->getValue()->getValue(); 3810 APInt Two(BitWidth, 2); 3811 APInt Four(BitWidth, 4); 3812 3813 { 3814 using namespace APIntOps; 3815 const APInt& C = L; 3816 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C 3817 // The B coefficient is M-N/2 3818 APInt B(M); 3819 B -= sdiv(N,Two); 3820 3821 // The A coefficient is N/2 3822 APInt A(N.sdiv(Two)); 3823 3824 // Compute the B^2-4ac term. 3825 APInt SqrtTerm(B); 3826 SqrtTerm *= B; 3827 SqrtTerm -= Four * (A * C); 3828 3829 // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest 3830 // integer value or else APInt::sqrt() will assert. 3831 APInt SqrtVal(SqrtTerm.sqrt()); 3832 3833 // Compute the two solutions for the quadratic formula. 3834 // The divisions must be performed as signed divisions. 3835 APInt NegB(-B); 3836 APInt TwoA( A << 1 ); 3837 if (TwoA.isMinValue()) { 3838 const SCEV *CNC = SE.getCouldNotCompute(); 3839 return std::make_pair(CNC, CNC); 3840 } 3841 3842 ConstantInt *Solution1 = ConstantInt::get((NegB + SqrtVal).sdiv(TwoA)); 3843 ConstantInt *Solution2 = ConstantInt::get((NegB - SqrtVal).sdiv(TwoA)); 3844 3845 return std::make_pair(SE.getConstant(Solution1), 3846 SE.getConstant(Solution2)); 3847 } // end APIntOps namespace 3848 } 3849 3850 /// HowFarToZero - Return the number of times a backedge comparing the specified 3851 /// value to zero will execute. If not computable, return CouldNotCompute. 3852 const SCEV* ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) { 3853 // If the value is a constant 3854 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { 3855 // If the value is already zero, the branch will execute zero times. 3856 if (C->getValue()->isZero()) return C; 3857 return getCouldNotCompute(); // Otherwise it will loop infinitely. 3858 } 3859 3860 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V); 3861 if (!AddRec || AddRec->getLoop() != L) 3862 return getCouldNotCompute(); 3863 3864 if (AddRec->isAffine()) { 3865 // If this is an affine expression, the execution count of this branch is 3866 // the minimum unsigned root of the following equation: 3867 // 3868 // Start + Step*N = 0 (mod 2^BW) 3869 // 3870 // equivalent to: 3871 // 3872 // Step*N = -Start (mod 2^BW) 3873 // 3874 // where BW is the common bit width of Start and Step. 3875 3876 // Get the initial value for the loop. 3877 const SCEV *Start = getSCEVAtScope(AddRec->getStart(), 3878 L->getParentLoop()); 3879 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), 3880 L->getParentLoop()); 3881 3882 if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) { 3883 // For now we handle only constant steps. 3884 3885 // First, handle unitary steps. 3886 if (StepC->getValue()->equalsInt(1)) // 1*N = -Start (mod 2^BW), so: 3887 return getNegativeSCEV(Start); // N = -Start (as unsigned) 3888 if (StepC->getValue()->isAllOnesValue()) // -1*N = -Start (mod 2^BW), so: 3889 return Start; // N = Start (as unsigned) 3890 3891 // Then, try to solve the above equation provided that Start is constant. 3892 if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) 3893 return SolveLinEquationWithOverflow(StepC->getValue()->getValue(), 3894 -StartC->getValue()->getValue(), 3895 *this); 3896 } 3897 } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) { 3898 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of 3899 // the quadratic equation to solve it. 3900 std::pair<const SCEV*,const SCEV*> Roots = SolveQuadraticEquation(AddRec, 3901 *this); 3902 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); 3903 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); 3904 if (R1) { 3905 #if 0 3906 errs() << "HFTZ: " << *V << " - sol#1: " << *R1 3907 << " sol#2: " << *R2 << "\n"; 3908 #endif 3909 // Pick the smallest positive root value. 3910 if (ConstantInt *CB = 3911 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, 3912 R1->getValue(), R2->getValue()))) { 3913 if (CB->getZExtValue() == false) 3914 std::swap(R1, R2); // R1 is the minimum root now. 3915 3916 // We can only use this value if the chrec ends up with an exact zero 3917 // value at this index. When solving for "X*X != 5", for example, we 3918 // should not accept a root of 2. 3919 const SCEV* Val = AddRec->evaluateAtIteration(R1, *this); 3920 if (Val->isZero()) 3921 return R1; // We found a quadratic root! 3922 } 3923 } 3924 } 3925 3926 return getCouldNotCompute(); 3927 } 3928 3929 /// HowFarToNonZero - Return the number of times a backedge checking the 3930 /// specified value for nonzero will execute. If not computable, return 3931 /// CouldNotCompute 3932 const SCEV* ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) { 3933 // Loops that look like: while (X == 0) are very strange indeed. We don't 3934 // handle them yet except for the trivial case. This could be expanded in the 3935 // future as needed. 3936 3937 // If the value is a constant, check to see if it is known to be non-zero 3938 // already. If so, the backedge will execute zero times. 3939 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) { 3940 if (!C->getValue()->isNullValue()) 3941 return getIntegerSCEV(0, C->getType()); 3942 return getCouldNotCompute(); // Otherwise it will loop infinitely. 3943 } 3944 3945 // We could implement others, but I really doubt anyone writes loops like 3946 // this, and if they did, they would already be constant folded. 3947 return getCouldNotCompute(); 3948 } 3949 3950 /// getLoopPredecessor - If the given loop's header has exactly one unique 3951 /// predecessor outside the loop, return it. Otherwise return null. 3952 /// 3953 BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) { 3954 BasicBlock *Header = L->getHeader(); 3955 BasicBlock *Pred = 0; 3956 for (pred_iterator PI = pred_begin(Header), E = pred_end(Header); 3957 PI != E; ++PI) 3958 if (!L->contains(*PI)) { 3959 if (Pred && Pred != *PI) return 0; // Multiple predecessors. 3960 Pred = *PI; 3961 } 3962 return Pred; 3963 } 3964 3965 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB 3966 /// (which may not be an immediate predecessor) which has exactly one 3967 /// successor from which BB is reachable, or null if no such block is 3968 /// found. 3969 /// 3970 BasicBlock * 3971 ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) { 3972 // If the block has a unique predecessor, then there is no path from the 3973 // predecessor to the block that does not go through the direct edge 3974 // from the predecessor to the block. 3975 if (BasicBlock *Pred = BB->getSinglePredecessor()) 3976 return Pred; 3977 3978 // A loop's header is defined to be a block that dominates the loop. 3979 // If the header has a unique predecessor outside the loop, it must be 3980 // a block that has exactly one successor that can reach the loop. 3981 if (Loop *L = LI->getLoopFor(BB)) 3982 return getLoopPredecessor(L); 3983 3984 return 0; 3985 } 3986 3987 /// HasSameValue - SCEV structural equivalence is usually sufficient for 3988 /// testing whether two expressions are equal, however for the purposes of 3989 /// looking for a condition guarding a loop, it can be useful to be a little 3990 /// more general, since a front-end may have replicated the controlling 3991 /// expression. 3992 /// 3993 static bool HasSameValue(const SCEV* A, const SCEV* B) { 3994 // Quick check to see if they are the same SCEV. 3995 if (A == B) return true; 3996 3997 // Otherwise, if they're both SCEVUnknown, it's possible that they hold 3998 // two different instructions with the same value. Check for this case. 3999 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A)) 4000 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B)) 4001 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue())) 4002 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue())) 4003 if (AI->isIdenticalTo(BI)) 4004 return true; 4005 4006 // Otherwise assume they may have a different value. 4007 return false; 4008 } 4009 4010 /// isLoopGuardedByCond - Test whether entry to the loop is protected by 4011 /// a conditional between LHS and RHS. This is used to help avoid max 4012 /// expressions in loop trip counts. 4013 bool ScalarEvolution::isLoopGuardedByCond(const Loop *L, 4014 ICmpInst::Predicate Pred, 4015 const SCEV *LHS, const SCEV *RHS) { 4016 // Interpret a null as meaning no loop, where there is obviously no guard 4017 // (interprocedural conditions notwithstanding). 4018 if (!L) return false; 4019 4020 BasicBlock *Predecessor = getLoopPredecessor(L); 4021 BasicBlock *PredecessorDest = L->getHeader(); 4022 4023 // Starting at the loop predecessor, climb up the predecessor chain, as long 4024 // as there are predecessors that can be found that have unique successors 4025 // leading to the original header. 4026 for (; Predecessor; 4027 PredecessorDest = Predecessor, 4028 Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) { 4029 4030 BranchInst *LoopEntryPredicate = 4031 dyn_cast<BranchInst>(Predecessor->getTerminator()); 4032 if (!LoopEntryPredicate || 4033 LoopEntryPredicate->isUnconditional()) 4034 continue; 4035 4036 if (isNecessaryCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS, 4037 LoopEntryPredicate->getSuccessor(0) != PredecessorDest)) 4038 return true; 4039 } 4040 4041 return false; 4042 } 4043 4044 /// isNecessaryCond - Test whether the given CondValue value is a condition 4045 /// which is at least as strict as the one described by Pred, LHS, and RHS. 4046 bool ScalarEvolution::isNecessaryCond(Value *CondValue, 4047 ICmpInst::Predicate Pred, 4048 const SCEV *LHS, const SCEV *RHS, 4049 bool Inverse) { 4050 // Recursivly handle And and Or conditions. 4051 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) { 4052 if (BO->getOpcode() == Instruction::And) { 4053 if (!Inverse) 4054 return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || 4055 isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); 4056 } else if (BO->getOpcode() == Instruction::Or) { 4057 if (Inverse) 4058 return isNecessaryCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) || 4059 isNecessaryCond(BO->getOperand(1), Pred, LHS, RHS, Inverse); 4060 } 4061 } 4062 4063 ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue); 4064 if (!ICI) return false; 4065 4066 // Now that we found a conditional branch that dominates the loop, check to 4067 // see if it is the comparison we are looking for. 4068 Value *PreCondLHS = ICI->getOperand(0); 4069 Value *PreCondRHS = ICI->getOperand(1); 4070 ICmpInst::Predicate Cond; 4071 if (Inverse) 4072 Cond = ICI->getInversePredicate(); 4073 else 4074 Cond = ICI->getPredicate(); 4075 4076 if (Cond == Pred) 4077 ; // An exact match. 4078 else if (!ICmpInst::isTrueWhenEqual(Cond) && Pred == ICmpInst::ICMP_NE) 4079 ; // The actual condition is beyond sufficient. 4080 else 4081 // Check a few special cases. 4082 switch (Cond) { 4083 case ICmpInst::ICMP_UGT: 4084 if (Pred == ICmpInst::ICMP_ULT) { 4085 std::swap(PreCondLHS, PreCondRHS); 4086 Cond = ICmpInst::ICMP_ULT; 4087 break; 4088 } 4089 return false; 4090 case ICmpInst::ICMP_SGT: 4091 if (Pred == ICmpInst::ICMP_SLT) { 4092 std::swap(PreCondLHS, PreCondRHS); 4093 Cond = ICmpInst::ICMP_SLT; 4094 break; 4095 } 4096 return false; 4097 case ICmpInst::ICMP_NE: 4098 // Expressions like (x >u 0) are often canonicalized to (x != 0), 4099 // so check for this case by checking if the NE is comparing against 4100 // a minimum or maximum constant. 4101 if (!ICmpInst::isTrueWhenEqual(Pred)) 4102 if (ConstantInt *CI = dyn_cast<ConstantInt>(PreCondRHS)) { 4103 const APInt &A = CI->getValue(); 4104 switch (Pred) { 4105 case ICmpInst::ICMP_SLT: 4106 if (A.isMaxSignedValue()) break; 4107 return false; 4108 case ICmpInst::ICMP_SGT: 4109 if (A.isMinSignedValue()) break; 4110 return false; 4111 case ICmpInst::ICMP_ULT: 4112 if (A.isMaxValue()) break; 4113 return false; 4114 case ICmpInst::ICMP_UGT: 4115 if (A.isMinValue()) break; 4116 return false; 4117 default: 4118 return false; 4119 } 4120 Cond = ICmpInst::ICMP_NE; 4121 // NE is symmetric but the original comparison may not be. Swap 4122 // the operands if necessary so that they match below. 4123 if (isa<SCEVConstant>(LHS)) 4124 std::swap(PreCondLHS, PreCondRHS); 4125 break; 4126 } 4127 return false; 4128 default: 4129 // We weren't able to reconcile the condition. 4130 return false; 4131 } 4132 4133 if (!PreCondLHS->getType()->isInteger()) return false; 4134 4135 const SCEV *PreCondLHSSCEV = getSCEV(PreCondLHS); 4136 const SCEV *PreCondRHSSCEV = getSCEV(PreCondRHS); 4137 return (HasSameValue(LHS, PreCondLHSSCEV) && 4138 HasSameValue(RHS, PreCondRHSSCEV)) || 4139 (HasSameValue(LHS, getNotSCEV(PreCondRHSSCEV)) && 4140 HasSameValue(RHS, getNotSCEV(PreCondLHSSCEV))); 4141 } 4142 4143 /// getBECount - Subtract the end and start values and divide by the step, 4144 /// rounding up, to get the number of times the backedge is executed. Return 4145 /// CouldNotCompute if an intermediate computation overflows. 4146 const SCEV* ScalarEvolution::getBECount(const SCEV* Start, 4147 const SCEV* End, 4148 const SCEV* Step) { 4149 const Type *Ty = Start->getType(); 4150 const SCEV* NegOne = getIntegerSCEV(-1, Ty); 4151 const SCEV* Diff = getMinusSCEV(End, Start); 4152 const SCEV* RoundUp = getAddExpr(Step, NegOne); 4153 4154 // Add an adjustment to the difference between End and Start so that 4155 // the division will effectively round up. 4156 const SCEV* Add = getAddExpr(Diff, RoundUp); 4157 4158 // Check Add for unsigned overflow. 4159 // TODO: More sophisticated things could be done here. 4160 const Type *WideTy = IntegerType::get(getTypeSizeInBits(Ty) + 1); 4161 const SCEV* OperandExtendedAdd = 4162 getAddExpr(getZeroExtendExpr(Diff, WideTy), 4163 getZeroExtendExpr(RoundUp, WideTy)); 4164 if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd) 4165 return getCouldNotCompute(); 4166 4167 return getUDivExpr(Add, Step); 4168 } 4169 4170 /// HowManyLessThans - Return the number of times a backedge containing the 4171 /// specified less-than comparison will execute. If not computable, return 4172 /// CouldNotCompute. 4173 ScalarEvolution::BackedgeTakenInfo 4174 ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS, 4175 const Loop *L, bool isSigned) { 4176 // Only handle: "ADDREC < LoopInvariant". 4177 if (!RHS->isLoopInvariant(L)) return getCouldNotCompute(); 4178 4179 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS); 4180 if (!AddRec || AddRec->getLoop() != L) 4181 return getCouldNotCompute(); 4182 4183 if (AddRec->isAffine()) { 4184 // FORNOW: We only support unit strides. 4185 unsigned BitWidth = getTypeSizeInBits(AddRec->getType()); 4186 const SCEV* Step = AddRec->getStepRecurrence(*this); 4187 4188 // TODO: handle non-constant strides. 4189 const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step); 4190 if (!CStep || CStep->isZero()) 4191 return getCouldNotCompute(); 4192 if (CStep->isOne()) { 4193 // With unit stride, the iteration never steps past the limit value. 4194 } else if (CStep->getValue()->getValue().isStrictlyPositive()) { 4195 if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) { 4196 // Test whether a positive iteration iteration can step past the limit 4197 // value and past the maximum value for its type in a single step. 4198 if (isSigned) { 4199 APInt Max = APInt::getSignedMaxValue(BitWidth); 4200 if ((Max - CStep->getValue()->getValue()) 4201 .slt(CLimit->getValue()->getValue())) 4202 return getCouldNotCompute(); 4203 } else { 4204 APInt Max = APInt::getMaxValue(BitWidth); 4205 if ((Max - CStep->getValue()->getValue()) 4206 .ult(CLimit->getValue()->getValue())) 4207 return getCouldNotCompute(); 4208 } 4209 } else 4210 // TODO: handle non-constant limit values below. 4211 return getCouldNotCompute(); 4212 } else 4213 // TODO: handle negative strides below. 4214 return getCouldNotCompute(); 4215 4216 // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant 4217 // m. So, we count the number of iterations in which {n,+,s} < m is true. 4218 // Note that we cannot simply return max(m-n,0)/s because it's not safe to 4219 // treat m-n as signed nor unsigned due to overflow possibility. 4220 4221 // First, we get the value of the LHS in the first iteration: n 4222 const SCEV* Start = AddRec->getOperand(0); 4223 4224 // Determine the minimum constant start value. 4225 const SCEV *MinStart = isa<SCEVConstant>(Start) ? Start : 4226 getConstant(isSigned ? APInt::getSignedMinValue(BitWidth) : 4227 APInt::getMinValue(BitWidth)); 4228 4229 // If we know that the condition is true in order to enter the loop, 4230 // then we know that it will run exactly (m-n)/s times. Otherwise, we 4231 // only know that it will execute (max(m,n)-n)/s times. In both cases, 4232 // the division must round up. 4233 const SCEV* End = RHS; 4234 if (!isLoopGuardedByCond(L, 4235 isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, 4236 getMinusSCEV(Start, Step), RHS)) 4237 End = isSigned ? getSMaxExpr(RHS, Start) 4238 : getUMaxExpr(RHS, Start); 4239 4240 // Determine the maximum constant end value. 4241 const SCEV* MaxEnd = 4242 isa<SCEVConstant>(End) ? End : 4243 getConstant(isSigned ? APInt::getSignedMaxValue(BitWidth) 4244 .ashr(GetMinSignBits(End) - 1) : 4245 APInt::getMaxValue(BitWidth) 4246 .lshr(GetMinLeadingZeros(End))); 4247 4248 // Finally, we subtract these two values and divide, rounding up, to get 4249 // the number of times the backedge is executed. 4250 const SCEV* BECount = getBECount(Start, End, Step); 4251 4252 // The maximum backedge count is similar, except using the minimum start 4253 // value and the maximum end value. 4254 const SCEV* MaxBECount = getBECount(MinStart, MaxEnd, Step); 4255 4256 return BackedgeTakenInfo(BECount, MaxBECount); 4257 } 4258 4259 return getCouldNotCompute(); 4260 } 4261 4262 /// getNumIterationsInRange - Return the number of iterations of this loop that 4263 /// produce values in the specified constant range. Another way of looking at 4264 /// this is that it returns the first iteration number where the value is not in 4265 /// the condition, thus computing the exit count. If the iteration count can't 4266 /// be computed, an instance of SCEVCouldNotCompute is returned. 4267 const SCEV* SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range, 4268 ScalarEvolution &SE) const { 4269 if (Range.isFullSet()) // Infinite loop. 4270 return SE.getCouldNotCompute(); 4271 4272 // If the start is a non-zero constant, shift the range to simplify things. 4273 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart())) 4274 if (!SC->getValue()->isZero()) { 4275 SmallVector<const SCEV*, 4> Operands(op_begin(), op_end()); 4276 Operands[0] = SE.getIntegerSCEV(0, SC->getType()); 4277 const SCEV* Shifted = SE.getAddRecExpr(Operands, getLoop()); 4278 if (const SCEVAddRecExpr *ShiftedAddRec = 4279 dyn_cast<SCEVAddRecExpr>(Shifted)) 4280 return ShiftedAddRec->getNumIterationsInRange( 4281 Range.subtract(SC->getValue()->getValue()), SE); 4282 // This is strange and shouldn't happen. 4283 return SE.getCouldNotCompute(); 4284 } 4285 4286 // The only time we can solve this is when we have all constant indices. 4287 // Otherwise, we cannot determine the overflow conditions. 4288 for (unsigned i = 0, e = getNumOperands(); i != e; ++i) 4289 if (!isa<SCEVConstant>(getOperand(i))) 4290 return SE.getCouldNotCompute(); 4291 4292 4293 // Okay at this point we know that all elements of the chrec are constants and 4294 // that the start element is zero. 4295 4296 // First check to see if the range contains zero. If not, the first 4297 // iteration exits. 4298 unsigned BitWidth = SE.getTypeSizeInBits(getType()); 4299 if (!Range.contains(APInt(BitWidth, 0))) 4300 return SE.getIntegerSCEV(0, getType()); 4301 4302 if (isAffine()) { 4303 // If this is an affine expression then we have this situation: 4304 // Solve {0,+,A} in Range === Ax in Range 4305 4306 // We know that zero is in the range. If A is positive then we know that 4307 // the upper value of the range must be the first possible exit value. 4308 // If A is negative then the lower of the range is the last possible loop 4309 // value. Also note that we already checked for a full range. 4310 APInt One(BitWidth,1); 4311 APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue(); 4312 APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower(); 4313 4314 // The exit value should be (End+A)/A. 4315 APInt ExitVal = (End + A).udiv(A); 4316 ConstantInt *ExitValue = ConstantInt::get(ExitVal); 4317 4318 // Evaluate at the exit value. If we really did fall out of the valid 4319 // range, then we computed our trip count, otherwise wrap around or other 4320 // things must have happened. 4321 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE); 4322 if (Range.contains(Val->getValue())) 4323 return SE.getCouldNotCompute(); // Something strange happened 4324 4325 // Ensure that the previous value is in the range. This is a sanity check. 4326 assert(Range.contains( 4327 EvaluateConstantChrecAtConstant(this, 4328 ConstantInt::get(ExitVal - One), SE)->getValue()) && 4329 "Linear scev computation is off in a bad way!"); 4330 return SE.getConstant(ExitValue); 4331 } else if (isQuadratic()) { 4332 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the 4333 // quadratic equation to solve it. To do this, we must frame our problem in 4334 // terms of figuring out when zero is crossed, instead of when 4335 // Range.getUpper() is crossed. 4336 SmallVector<const SCEV*, 4> NewOps(op_begin(), op_end()); 4337 NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper())); 4338 const SCEV* NewAddRec = SE.getAddRecExpr(NewOps, getLoop()); 4339 4340 // Next, solve the constructed addrec 4341 std::pair<const SCEV*,const SCEV*> Roots = 4342 SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE); 4343 const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first); 4344 const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second); 4345 if (R1) { 4346 // Pick the smallest positive root value. 4347 if (ConstantInt *CB = 4348 dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT, 4349 R1->getValue(), R2->getValue()))) { 4350 if (CB->getZExtValue() == false) 4351 std::swap(R1, R2); // R1 is the minimum root now. 4352 4353 // Make sure the root is not off by one. The returned iteration should 4354 // not be in the range, but the previous one should be. When solving 4355 // for "X*X < 5", for example, we should not return a root of 2. 4356 ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this, 4357 R1->getValue(), 4358 SE); 4359 if (Range.contains(R1Val->getValue())) { 4360 // The next iteration must be out of the range... 4361 ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()+1); 4362 4363 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); 4364 if (!Range.contains(R1Val->getValue())) 4365 return SE.getConstant(NextVal); 4366 return SE.getCouldNotCompute(); // Something strange happened 4367 } 4368 4369 // If R1 was not in the range, then it is a good return value. Make 4370 // sure that R1-1 WAS in the range though, just in case. 4371 ConstantInt *NextVal = ConstantInt::get(R1->getValue()->getValue()-1); 4372 R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE); 4373 if (Range.contains(R1Val->getValue())) 4374 return R1; 4375 return SE.getCouldNotCompute(); // Something strange happened 4376 } 4377 } 4378 } 4379 4380 return SE.getCouldNotCompute(); 4381 } 4382 4383 4384 4385 //===----------------------------------------------------------------------===// 4386 // SCEVCallbackVH Class Implementation 4387 //===----------------------------------------------------------------------===// 4388 4389 void ScalarEvolution::SCEVCallbackVH::deleted() { 4390 assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); 4391 if (PHINode *PN = dyn_cast<PHINode>(getValPtr())) 4392 SE->ConstantEvolutionLoopExitValue.erase(PN); 4393 if (Instruction *I = dyn_cast<Instruction>(getValPtr())) 4394 SE->ValuesAtScopes.erase(I); 4395 SE->Scalars.erase(getValPtr()); 4396 // this now dangles! 4397 } 4398 4399 void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) { 4400 assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!"); 4401 4402 // Forget all the expressions associated with users of the old value, 4403 // so that future queries will recompute the expressions using the new 4404 // value. 4405 SmallVector<User *, 16> Worklist; 4406 Value *Old = getValPtr(); 4407 bool DeleteOld = false; 4408 for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end(); 4409 UI != UE; ++UI) 4410 Worklist.push_back(*UI); 4411 while (!Worklist.empty()) { 4412 User *U = Worklist.pop_back_val(); 4413 // Deleting the Old value will cause this to dangle. Postpone 4414 // that until everything else is done. 4415 if (U == Old) { 4416 DeleteOld = true; 4417 continue; 4418 } 4419 if (PHINode *PN = dyn_cast<PHINode>(U)) 4420 SE->ConstantEvolutionLoopExitValue.erase(PN); 4421 if (Instruction *I = dyn_cast<Instruction>(U)) 4422 SE->ValuesAtScopes.erase(I); 4423 if (SE->Scalars.erase(U)) 4424 for (Value::use_iterator UI = U->use_begin(), UE = U->use_end(); 4425 UI != UE; ++UI) 4426 Worklist.push_back(*UI); 4427 } 4428 if (DeleteOld) { 4429 if (PHINode *PN = dyn_cast<PHINode>(Old)) 4430 SE->ConstantEvolutionLoopExitValue.erase(PN); 4431 if (Instruction *I = dyn_cast<Instruction>(Old)) 4432 SE->ValuesAtScopes.erase(I); 4433 SE->Scalars.erase(Old); 4434 // this now dangles! 4435 } 4436 // this may dangle! 4437 } 4438 4439 ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se) 4440 : CallbackVH(V), SE(se) {} 4441 4442 //===----------------------------------------------------------------------===// 4443 // ScalarEvolution Class Implementation 4444 //===----------------------------------------------------------------------===// 4445 4446 ScalarEvolution::ScalarEvolution() 4447 : FunctionPass(&ID) { 4448 } 4449 4450 bool ScalarEvolution::runOnFunction(Function &F) { 4451 this->F = &F; 4452 LI = &getAnalysis<LoopInfo>(); 4453 TD = getAnalysisIfAvailable<TargetData>(); 4454 return false; 4455 } 4456 4457 void ScalarEvolution::releaseMemory() { 4458 Scalars.clear(); 4459 BackedgeTakenCounts.clear(); 4460 ConstantEvolutionLoopExitValue.clear(); 4461 ValuesAtScopes.clear(); 4462 UniqueSCEVs.clear(); 4463 SCEVAllocator.Reset(); 4464 } 4465 4466 void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const { 4467 AU.setPreservesAll(); 4468 AU.addRequiredTransitive<LoopInfo>(); 4469 } 4470 4471 bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) { 4472 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L)); 4473 } 4474 4475 static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, 4476 const Loop *L) { 4477 // Print all inner loops first 4478 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) 4479 PrintLoopInfo(OS, SE, *I); 4480 4481 OS << "Loop " << L->getHeader()->getName() << ": "; 4482 4483 SmallVector<BasicBlock*, 8> ExitBlocks; 4484 L->getExitBlocks(ExitBlocks); 4485 if (ExitBlocks.size() != 1) 4486 OS << "<multiple exits> "; 4487 4488 if (SE->hasLoopInvariantBackedgeTakenCount(L)) { 4489 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L); 4490 } else { 4491 OS << "Unpredictable backedge-taken count. "; 4492 } 4493 4494 OS << "\n"; 4495 OS << "Loop " << L->getHeader()->getName() << ": "; 4496 4497 if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) { 4498 OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L); 4499 } else { 4500 OS << "Unpredictable max backedge-taken count. "; 4501 } 4502 4503 OS << "\n"; 4504 } 4505 4506 void ScalarEvolution::print(raw_ostream &OS, const Module* ) const { 4507 // ScalarEvolution's implementaiton of the print method is to print 4508 // out SCEV values of all instructions that are interesting. Doing 4509 // this potentially causes it to create new SCEV objects though, 4510 // which technically conflicts with the const qualifier. This isn't 4511 // observable from outside the class though (the hasSCEV function 4512 // notwithstanding), so casting away the const isn't dangerous. 4513 ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this); 4514 4515 OS << "Classifying expressions for: " << F->getName() << "\n"; 4516 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) 4517 if (isSCEVable(I->getType())) { 4518 OS << *I; 4519 OS << " --> "; 4520 const SCEV* SV = SE.getSCEV(&*I); 4521 SV->print(OS); 4522 4523 const Loop *L = LI->getLoopFor((*I).getParent()); 4524 4525 const SCEV* AtUse = SE.getSCEVAtScope(SV, L); 4526 if (AtUse != SV) { 4527 OS << " --> "; 4528 AtUse->print(OS); 4529 } 4530 4531 if (L) { 4532 OS << "\t\t" "Exits: "; 4533 const SCEV* ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop()); 4534 if (!ExitValue->isLoopInvariant(L)) { 4535 OS << "<<Unknown>>"; 4536 } else { 4537 OS << *ExitValue; 4538 } 4539 } 4540 4541 OS << "\n"; 4542 } 4543 4544 OS << "Determining loop execution counts for: " << F->getName() << "\n"; 4545 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) 4546 PrintLoopInfo(OS, &SE, *I); 4547 } 4548 4549 void ScalarEvolution::print(std::ostream &o, const Module *M) const { 4550 raw_os_ostream OS(o); 4551 print(OS, M); 4552 } 4553