1 2 #include "polly/Support/SCEVValidator.h" 3 #include "polly/ScopDetection.h" 4 #include "llvm/Analysis/RegionInfo.h" 5 #include "llvm/Analysis/ScalarEvolution.h" 6 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 7 #include "llvm/Support/Debug.h" 8 9 using namespace llvm; 10 using namespace polly; 11 12 #define DEBUG_TYPE "polly-scev-validator" 13 14 namespace SCEVType { 15 /// The type of a SCEV 16 /// 17 /// To check for the validity of a SCEV we assign to each SCEV a type. The 18 /// possible types are INT, PARAM, IV and INVALID. The order of the types is 19 /// important. The subexpressions of SCEV with a type X can only have a type 20 /// that is smaller or equal than X. 21 enum TYPE { 22 // An integer value. 23 INT, 24 25 // An expression that is constant during the execution of the Scop, 26 // but that may depend on parameters unknown at compile time. 27 PARAM, 28 29 // An expression that may change during the execution of the SCoP. 30 IV, 31 32 // An invalid expression. 33 INVALID 34 }; 35 } // namespace SCEVType 36 37 /// The result the validator returns for a SCEV expression. 38 class ValidatorResult { 39 /// The type of the expression 40 SCEVType::TYPE Type; 41 42 /// The set of Parameters in the expression. 43 ParameterSetTy Parameters; 44 45 public: 46 /// The copy constructor 47 ValidatorResult(const ValidatorResult &Source) { 48 Type = Source.Type; 49 Parameters = Source.Parameters; 50 } 51 52 /// Construct a result with a certain type and no parameters. 53 ValidatorResult(SCEVType::TYPE Type) : Type(Type) { 54 assert(Type != SCEVType::PARAM && "Did you forget to pass the parameter"); 55 } 56 57 /// Construct a result with a certain type and a single parameter. 58 ValidatorResult(SCEVType::TYPE Type, const SCEV *Expr) : Type(Type) { 59 Parameters.insert(Expr); 60 } 61 62 /// Get the type of the ValidatorResult. 63 SCEVType::TYPE getType() { return Type; } 64 65 /// Is the analyzed SCEV constant during the execution of the SCoP. 66 bool isConstant() { return Type == SCEVType::INT || Type == SCEVType::PARAM; } 67 68 /// Is the analyzed SCEV valid. 69 bool isValid() { return Type != SCEVType::INVALID; } 70 71 /// Is the analyzed SCEV of Type IV. 72 bool isIV() { return Type == SCEVType::IV; } 73 74 /// Is the analyzed SCEV of Type INT. 75 bool isINT() { return Type == SCEVType::INT; } 76 77 /// Is the analyzed SCEV of Type PARAM. 78 bool isPARAM() { return Type == SCEVType::PARAM; } 79 80 /// Get the parameters of this validator result. 81 const ParameterSetTy &getParameters() { return Parameters; } 82 83 /// Add the parameters of Source to this result. 84 void addParamsFrom(const ValidatorResult &Source) { 85 Parameters.insert(Source.Parameters.begin(), Source.Parameters.end()); 86 } 87 88 /// Merge a result. 89 /// 90 /// This means to merge the parameters and to set the Type to the most 91 /// specific Type that matches both. 92 void merge(const ValidatorResult &ToMerge) { 93 Type = std::max(Type, ToMerge.Type); 94 addParamsFrom(ToMerge); 95 } 96 97 void print(raw_ostream &OS) { 98 switch (Type) { 99 case SCEVType::INT: 100 OS << "SCEVType::INT"; 101 break; 102 case SCEVType::PARAM: 103 OS << "SCEVType::PARAM"; 104 break; 105 case SCEVType::IV: 106 OS << "SCEVType::IV"; 107 break; 108 case SCEVType::INVALID: 109 OS << "SCEVType::INVALID"; 110 break; 111 } 112 } 113 }; 114 115 raw_ostream &operator<<(raw_ostream &OS, class ValidatorResult &VR) { 116 VR.print(OS); 117 return OS; 118 } 119 120 bool polly::isConstCall(llvm::CallInst *Call) { 121 if (Call->mayReadOrWriteMemory()) 122 return false; 123 124 for (auto &Operand : Call->arg_operands()) 125 if (!isa<ConstantInt>(&Operand)) 126 return false; 127 128 return true; 129 } 130 131 /// Check if a SCEV is valid in a SCoP. 132 struct SCEVValidator 133 : public SCEVVisitor<SCEVValidator, class ValidatorResult> { 134 private: 135 const Region *R; 136 Loop *Scope; 137 ScalarEvolution &SE; 138 InvariantLoadsSetTy *ILS; 139 140 public: 141 SCEVValidator(const Region *R, Loop *Scope, ScalarEvolution &SE, 142 InvariantLoadsSetTy *ILS) 143 : R(R), Scope(Scope), SE(SE), ILS(ILS) {} 144 145 class ValidatorResult visitConstant(const SCEVConstant *Constant) { 146 return ValidatorResult(SCEVType::INT); 147 } 148 149 class ValidatorResult visitZeroExtendOrTruncateExpr(const SCEV *Expr, 150 const SCEV *Operand) { 151 ValidatorResult Op = visit(Operand); 152 auto Type = Op.getType(); 153 154 // If unsigned operations are allowed return the operand, otherwise 155 // check if we can model the expression without unsigned assumptions. 156 if (PollyAllowUnsignedOperations || Type == SCEVType::INVALID) 157 return Op; 158 159 if (Type == SCEVType::IV) 160 return ValidatorResult(SCEVType::INVALID); 161 return ValidatorResult(SCEVType::PARAM, Expr); 162 } 163 164 class ValidatorResult visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) { 165 return visit(Expr->getOperand()); 166 } 167 168 class ValidatorResult visitTruncateExpr(const SCEVTruncateExpr *Expr) { 169 return visitZeroExtendOrTruncateExpr(Expr, Expr->getOperand()); 170 } 171 172 class ValidatorResult visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { 173 return visitZeroExtendOrTruncateExpr(Expr, Expr->getOperand()); 174 } 175 176 class ValidatorResult visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { 177 return visit(Expr->getOperand()); 178 } 179 180 class ValidatorResult visitAddExpr(const SCEVAddExpr *Expr) { 181 ValidatorResult Return(SCEVType::INT); 182 183 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { 184 ValidatorResult Op = visit(Expr->getOperand(i)); 185 Return.merge(Op); 186 187 // Early exit. 188 if (!Return.isValid()) 189 break; 190 } 191 192 return Return; 193 } 194 195 class ValidatorResult visitMulExpr(const SCEVMulExpr *Expr) { 196 ValidatorResult Return(SCEVType::INT); 197 198 bool HasMultipleParams = false; 199 200 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { 201 ValidatorResult Op = visit(Expr->getOperand(i)); 202 203 if (Op.isINT()) 204 continue; 205 206 if (Op.isPARAM() && Return.isPARAM()) { 207 HasMultipleParams = true; 208 continue; 209 } 210 211 if ((Op.isIV() || Op.isPARAM()) && !Return.isINT()) { 212 LLVM_DEBUG( 213 dbgs() << "INVALID: More than one non-int operand in MulExpr\n" 214 << "\tExpr: " << *Expr << "\n" 215 << "\tPrevious expression type: " << Return << "\n" 216 << "\tNext operand (" << Op << "): " << *Expr->getOperand(i) 217 << "\n"); 218 219 return ValidatorResult(SCEVType::INVALID); 220 } 221 222 Return.merge(Op); 223 } 224 225 if (HasMultipleParams && Return.isValid()) 226 return ValidatorResult(SCEVType::PARAM, Expr); 227 228 return Return; 229 } 230 231 class ValidatorResult visitAddRecExpr(const SCEVAddRecExpr *Expr) { 232 if (!Expr->isAffine()) { 233 LLVM_DEBUG(dbgs() << "INVALID: AddRec is not affine"); 234 return ValidatorResult(SCEVType::INVALID); 235 } 236 237 ValidatorResult Start = visit(Expr->getStart()); 238 ValidatorResult Recurrence = visit(Expr->getStepRecurrence(SE)); 239 240 if (!Start.isValid()) 241 return Start; 242 243 if (!Recurrence.isValid()) 244 return Recurrence; 245 246 auto *L = Expr->getLoop(); 247 if (R->contains(L) && (!Scope || !L->contains(Scope))) { 248 LLVM_DEBUG( 249 dbgs() << "INVALID: Loop of AddRec expression boxed in an a " 250 "non-affine subregion or has a non-synthesizable exit " 251 "value."); 252 return ValidatorResult(SCEVType::INVALID); 253 } 254 255 if (R->contains(L)) { 256 if (Recurrence.isINT()) { 257 ValidatorResult Result(SCEVType::IV); 258 Result.addParamsFrom(Start); 259 return Result; 260 } 261 262 LLVM_DEBUG(dbgs() << "INVALID: AddRec within scop has non-int" 263 "recurrence part"); 264 return ValidatorResult(SCEVType::INVALID); 265 } 266 267 assert(Recurrence.isConstant() && "Expected 'Recurrence' to be constant"); 268 269 // Directly generate ValidatorResult for Expr if 'start' is zero. 270 if (Expr->getStart()->isZero()) 271 return ValidatorResult(SCEVType::PARAM, Expr); 272 273 // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}' 274 // if 'start' is not zero. 275 const SCEV *ZeroStartExpr = SE.getAddRecExpr( 276 SE.getConstant(Expr->getStart()->getType(), 0), 277 Expr->getStepRecurrence(SE), Expr->getLoop(), Expr->getNoWrapFlags()); 278 279 ValidatorResult ZeroStartResult = 280 ValidatorResult(SCEVType::PARAM, ZeroStartExpr); 281 ZeroStartResult.addParamsFrom(Start); 282 283 return ZeroStartResult; 284 } 285 286 class ValidatorResult visitSMaxExpr(const SCEVSMaxExpr *Expr) { 287 ValidatorResult Return(SCEVType::INT); 288 289 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { 290 ValidatorResult Op = visit(Expr->getOperand(i)); 291 292 if (!Op.isValid()) 293 return Op; 294 295 Return.merge(Op); 296 } 297 298 return Return; 299 } 300 301 class ValidatorResult visitSMinExpr(const SCEVSMinExpr *Expr) { 302 ValidatorResult Return(SCEVType::INT); 303 304 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { 305 ValidatorResult Op = visit(Expr->getOperand(i)); 306 307 if (!Op.isValid()) 308 return Op; 309 310 Return.merge(Op); 311 } 312 313 return Return; 314 } 315 316 class ValidatorResult visitUMaxExpr(const SCEVUMaxExpr *Expr) { 317 // We do not support unsigned max operations. If 'Expr' is constant during 318 // Scop execution we treat this as a parameter, otherwise we bail out. 319 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { 320 ValidatorResult Op = visit(Expr->getOperand(i)); 321 322 if (!Op.isConstant()) { 323 LLVM_DEBUG(dbgs() << "INVALID: UMaxExpr has a non-constant operand"); 324 return ValidatorResult(SCEVType::INVALID); 325 } 326 } 327 328 return ValidatorResult(SCEVType::PARAM, Expr); 329 } 330 331 class ValidatorResult visitUMinExpr(const SCEVUMinExpr *Expr) { 332 // We do not support unsigned min operations. If 'Expr' is constant during 333 // Scop execution we treat this as a parameter, otherwise we bail out. 334 for (int i = 0, e = Expr->getNumOperands(); i < e; ++i) { 335 ValidatorResult Op = visit(Expr->getOperand(i)); 336 337 if (!Op.isConstant()) { 338 LLVM_DEBUG(dbgs() << "INVALID: UMinExpr has a non-constant operand"); 339 return ValidatorResult(SCEVType::INVALID); 340 } 341 } 342 343 return ValidatorResult(SCEVType::PARAM, Expr); 344 } 345 346 ValidatorResult visitGenericInst(Instruction *I, const SCEV *S) { 347 if (R->contains(I)) { 348 LLVM_DEBUG(dbgs() << "INVALID: UnknownExpr references an instruction " 349 "within the region\n"); 350 return ValidatorResult(SCEVType::INVALID); 351 } 352 353 return ValidatorResult(SCEVType::PARAM, S); 354 } 355 356 ValidatorResult visitCallInstruction(Instruction *I, const SCEV *S) { 357 assert(I->getOpcode() == Instruction::Call && "Call instruction expected"); 358 359 if (R->contains(I)) { 360 auto Call = cast<CallInst>(I); 361 362 if (!isConstCall(Call)) 363 return ValidatorResult(SCEVType::INVALID, S); 364 } 365 return ValidatorResult(SCEVType::PARAM, S); 366 } 367 368 ValidatorResult visitLoadInstruction(Instruction *I, const SCEV *S) { 369 if (R->contains(I) && ILS) { 370 ILS->insert(cast<LoadInst>(I)); 371 return ValidatorResult(SCEVType::PARAM, S); 372 } 373 374 return visitGenericInst(I, S); 375 } 376 377 ValidatorResult visitDivision(const SCEV *Dividend, const SCEV *Divisor, 378 const SCEV *DivExpr, 379 Instruction *SDiv = nullptr) { 380 381 // First check if we might be able to model the division, thus if the 382 // divisor is constant. If so, check the dividend, otherwise check if 383 // the whole division can be seen as a parameter. 384 if (isa<SCEVConstant>(Divisor) && !Divisor->isZero()) 385 return visit(Dividend); 386 387 // For signed divisions use the SDiv instruction to check for a parameter 388 // division, for unsigned divisions check the operands. 389 if (SDiv) 390 return visitGenericInst(SDiv, DivExpr); 391 392 ValidatorResult LHS = visit(Dividend); 393 ValidatorResult RHS = visit(Divisor); 394 if (LHS.isConstant() && RHS.isConstant()) 395 return ValidatorResult(SCEVType::PARAM, DivExpr); 396 397 LLVM_DEBUG( 398 dbgs() << "INVALID: unsigned division of non-constant expressions"); 399 return ValidatorResult(SCEVType::INVALID); 400 } 401 402 ValidatorResult visitUDivExpr(const SCEVUDivExpr *Expr) { 403 if (!PollyAllowUnsignedOperations) 404 return ValidatorResult(SCEVType::INVALID); 405 406 auto *Dividend = Expr->getLHS(); 407 auto *Divisor = Expr->getRHS(); 408 return visitDivision(Dividend, Divisor, Expr); 409 } 410 411 ValidatorResult visitSDivInstruction(Instruction *SDiv, const SCEV *Expr) { 412 assert(SDiv->getOpcode() == Instruction::SDiv && 413 "Assumed SDiv instruction!"); 414 415 auto *Dividend = SE.getSCEV(SDiv->getOperand(0)); 416 auto *Divisor = SE.getSCEV(SDiv->getOperand(1)); 417 return visitDivision(Dividend, Divisor, Expr, SDiv); 418 } 419 420 ValidatorResult visitSRemInstruction(Instruction *SRem, const SCEV *S) { 421 assert(SRem->getOpcode() == Instruction::SRem && 422 "Assumed SRem instruction!"); 423 424 auto *Divisor = SRem->getOperand(1); 425 auto *CI = dyn_cast<ConstantInt>(Divisor); 426 if (!CI || CI->isZeroValue()) 427 return visitGenericInst(SRem, S); 428 429 auto *Dividend = SRem->getOperand(0); 430 auto *DividendSCEV = SE.getSCEV(Dividend); 431 return visit(DividendSCEV); 432 } 433 434 ValidatorResult visitUnknown(const SCEVUnknown *Expr) { 435 Value *V = Expr->getValue(); 436 437 if (!Expr->getType()->isIntegerTy() && !Expr->getType()->isPointerTy()) { 438 LLVM_DEBUG(dbgs() << "INVALID: UnknownExpr is not an integer or pointer"); 439 return ValidatorResult(SCEVType::INVALID); 440 } 441 442 if (isa<UndefValue>(V)) { 443 LLVM_DEBUG(dbgs() << "INVALID: UnknownExpr references an undef value"); 444 return ValidatorResult(SCEVType::INVALID); 445 } 446 447 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) { 448 switch (I->getOpcode()) { 449 case Instruction::IntToPtr: 450 return visit(SE.getSCEVAtScope(I->getOperand(0), Scope)); 451 case Instruction::Load: 452 return visitLoadInstruction(I, Expr); 453 case Instruction::SDiv: 454 return visitSDivInstruction(I, Expr); 455 case Instruction::SRem: 456 return visitSRemInstruction(I, Expr); 457 case Instruction::Call: 458 return visitCallInstruction(I, Expr); 459 default: 460 return visitGenericInst(I, Expr); 461 } 462 } 463 464 return ValidatorResult(SCEVType::PARAM, Expr); 465 } 466 }; 467 468 class SCEVHasIVParams { 469 bool HasIVParams = false; 470 471 public: 472 SCEVHasIVParams() {} 473 474 bool follow(const SCEV *S) { 475 const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(S); 476 if (!Unknown) 477 return true; 478 479 CallInst *Call = dyn_cast<CallInst>(Unknown->getValue()); 480 481 if (!Call) 482 return true; 483 484 if (isConstCall(Call)) { 485 HasIVParams = true; 486 return false; 487 } 488 489 return true; 490 } 491 492 bool isDone() { return HasIVParams; } 493 bool hasIVParams() { return HasIVParams; } 494 }; 495 496 /// Check whether a SCEV refers to an SSA name defined inside a region. 497 class SCEVInRegionDependences { 498 const Region *R; 499 Loop *Scope; 500 const InvariantLoadsSetTy &ILS; 501 bool AllowLoops; 502 bool HasInRegionDeps = false; 503 504 public: 505 SCEVInRegionDependences(const Region *R, Loop *Scope, bool AllowLoops, 506 const InvariantLoadsSetTy &ILS) 507 : R(R), Scope(Scope), ILS(ILS), AllowLoops(AllowLoops) {} 508 509 bool follow(const SCEV *S) { 510 if (auto Unknown = dyn_cast<SCEVUnknown>(S)) { 511 Instruction *Inst = dyn_cast<Instruction>(Unknown->getValue()); 512 513 CallInst *Call = dyn_cast<CallInst>(Unknown->getValue()); 514 515 if (Call && isConstCall(Call)) 516 return false; 517 518 if (Inst) { 519 // When we invariant load hoist a load, we first make sure that there 520 // can be no dependences created by it in the Scop region. So, we should 521 // not consider scalar dependences to `LoadInst`s that are invariant 522 // load hoisted. 523 // 524 // If this check is not present, then we create data dependences which 525 // are strictly not necessary by tracking the invariant load as a 526 // scalar. 527 LoadInst *LI = dyn_cast<LoadInst>(Inst); 528 if (LI && ILS.count(LI) > 0) 529 return false; 530 } 531 532 // Return true when Inst is defined inside the region R. 533 if (!Inst || !R->contains(Inst)) 534 return true; 535 536 HasInRegionDeps = true; 537 return false; 538 } 539 540 if (auto AddRec = dyn_cast<SCEVAddRecExpr>(S)) { 541 if (AllowLoops) 542 return true; 543 544 auto *L = AddRec->getLoop(); 545 if (R->contains(L) && !L->contains(Scope)) { 546 HasInRegionDeps = true; 547 return false; 548 } 549 } 550 551 return true; 552 } 553 bool isDone() { return false; } 554 bool hasDependences() { return HasInRegionDeps; } 555 }; 556 557 namespace polly { 558 /// Find all loops referenced in SCEVAddRecExprs. 559 class SCEVFindLoops { 560 SetVector<const Loop *> &Loops; 561 562 public: 563 SCEVFindLoops(SetVector<const Loop *> &Loops) : Loops(Loops) {} 564 565 bool follow(const SCEV *S) { 566 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) 567 Loops.insert(AddRec->getLoop()); 568 return true; 569 } 570 bool isDone() { return false; } 571 }; 572 573 void findLoops(const SCEV *Expr, SetVector<const Loop *> &Loops) { 574 SCEVFindLoops FindLoops(Loops); 575 SCEVTraversal<SCEVFindLoops> ST(FindLoops); 576 ST.visitAll(Expr); 577 } 578 579 /// Find all values referenced in SCEVUnknowns. 580 class SCEVFindValues { 581 ScalarEvolution &SE; 582 SetVector<Value *> &Values; 583 584 public: 585 SCEVFindValues(ScalarEvolution &SE, SetVector<Value *> &Values) 586 : SE(SE), Values(Values) {} 587 588 bool follow(const SCEV *S) { 589 const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(S); 590 if (!Unknown) 591 return true; 592 593 Values.insert(Unknown->getValue()); 594 Instruction *Inst = dyn_cast<Instruction>(Unknown->getValue()); 595 if (!Inst || (Inst->getOpcode() != Instruction::SRem && 596 Inst->getOpcode() != Instruction::SDiv)) 597 return false; 598 599 auto *Dividend = SE.getSCEV(Inst->getOperand(1)); 600 if (!isa<SCEVConstant>(Dividend)) 601 return false; 602 603 auto *Divisor = SE.getSCEV(Inst->getOperand(0)); 604 SCEVFindValues FindValues(SE, Values); 605 SCEVTraversal<SCEVFindValues> ST(FindValues); 606 ST.visitAll(Dividend); 607 ST.visitAll(Divisor); 608 609 return false; 610 } 611 bool isDone() { return false; } 612 }; 613 614 void findValues(const SCEV *Expr, ScalarEvolution &SE, 615 SetVector<Value *> &Values) { 616 SCEVFindValues FindValues(SE, Values); 617 SCEVTraversal<SCEVFindValues> ST(FindValues); 618 ST.visitAll(Expr); 619 } 620 621 bool hasIVParams(const SCEV *Expr) { 622 SCEVHasIVParams HasIVParams; 623 SCEVTraversal<SCEVHasIVParams> ST(HasIVParams); 624 ST.visitAll(Expr); 625 return HasIVParams.hasIVParams(); 626 } 627 628 bool hasScalarDepsInsideRegion(const SCEV *Expr, const Region *R, 629 llvm::Loop *Scope, bool AllowLoops, 630 const InvariantLoadsSetTy &ILS) { 631 SCEVInRegionDependences InRegionDeps(R, Scope, AllowLoops, ILS); 632 SCEVTraversal<SCEVInRegionDependences> ST(InRegionDeps); 633 ST.visitAll(Expr); 634 return InRegionDeps.hasDependences(); 635 } 636 637 bool isAffineExpr(const Region *R, llvm::Loop *Scope, const SCEV *Expr, 638 ScalarEvolution &SE, InvariantLoadsSetTy *ILS) { 639 if (isa<SCEVCouldNotCompute>(Expr)) 640 return false; 641 642 SCEVValidator Validator(R, Scope, SE, ILS); 643 LLVM_DEBUG({ 644 dbgs() << "\n"; 645 dbgs() << "Expr: " << *Expr << "\n"; 646 dbgs() << "Region: " << R->getNameStr() << "\n"; 647 dbgs() << " -> "; 648 }); 649 650 ValidatorResult Result = Validator.visit(Expr); 651 652 LLVM_DEBUG({ 653 if (Result.isValid()) 654 dbgs() << "VALID\n"; 655 dbgs() << "\n"; 656 }); 657 658 return Result.isValid(); 659 } 660 661 static bool isAffineExpr(Value *V, const Region *R, Loop *Scope, 662 ScalarEvolution &SE, ParameterSetTy &Params) { 663 auto *E = SE.getSCEV(V); 664 if (isa<SCEVCouldNotCompute>(E)) 665 return false; 666 667 SCEVValidator Validator(R, Scope, SE, nullptr); 668 ValidatorResult Result = Validator.visit(E); 669 if (!Result.isValid()) 670 return false; 671 672 auto ResultParams = Result.getParameters(); 673 Params.insert(ResultParams.begin(), ResultParams.end()); 674 675 return true; 676 } 677 678 bool isAffineConstraint(Value *V, const Region *R, llvm::Loop *Scope, 679 ScalarEvolution &SE, ParameterSetTy &Params, 680 bool OrExpr) { 681 if (auto *ICmp = dyn_cast<ICmpInst>(V)) { 682 return isAffineConstraint(ICmp->getOperand(0), R, Scope, SE, Params, 683 true) && 684 isAffineConstraint(ICmp->getOperand(1), R, Scope, SE, Params, true); 685 } else if (auto *BinOp = dyn_cast<BinaryOperator>(V)) { 686 auto Opcode = BinOp->getOpcode(); 687 if (Opcode == Instruction::And || Opcode == Instruction::Or) 688 return isAffineConstraint(BinOp->getOperand(0), R, Scope, SE, Params, 689 false) && 690 isAffineConstraint(BinOp->getOperand(1), R, Scope, SE, Params, 691 false); 692 /* Fall through */ 693 } 694 695 if (!OrExpr) 696 return false; 697 698 return isAffineExpr(V, R, Scope, SE, Params); 699 } 700 701 ParameterSetTy getParamsInAffineExpr(const Region *R, Loop *Scope, 702 const SCEV *Expr, ScalarEvolution &SE) { 703 if (isa<SCEVCouldNotCompute>(Expr)) 704 return ParameterSetTy(); 705 706 InvariantLoadsSetTy ILS; 707 SCEVValidator Validator(R, Scope, SE, &ILS); 708 ValidatorResult Result = Validator.visit(Expr); 709 assert(Result.isValid() && "Requested parameters for an invalid SCEV!"); 710 711 return Result.getParameters(); 712 } 713 714 std::pair<const SCEVConstant *, const SCEV *> 715 extractConstantFactor(const SCEV *S, ScalarEvolution &SE) { 716 auto *ConstPart = cast<SCEVConstant>(SE.getConstant(S->getType(), 1)); 717 718 if (auto *Constant = dyn_cast<SCEVConstant>(S)) 719 return std::make_pair(Constant, SE.getConstant(S->getType(), 1)); 720 721 auto *AddRec = dyn_cast<SCEVAddRecExpr>(S); 722 if (AddRec) { 723 auto *StartExpr = AddRec->getStart(); 724 if (StartExpr->isZero()) { 725 auto StepPair = extractConstantFactor(AddRec->getStepRecurrence(SE), SE); 726 auto *LeftOverAddRec = 727 SE.getAddRecExpr(StartExpr, StepPair.second, AddRec->getLoop(), 728 AddRec->getNoWrapFlags()); 729 return std::make_pair(StepPair.first, LeftOverAddRec); 730 } 731 return std::make_pair(ConstPart, S); 732 } 733 734 if (auto *Add = dyn_cast<SCEVAddExpr>(S)) { 735 SmallVector<const SCEV *, 4> LeftOvers; 736 auto Op0Pair = extractConstantFactor(Add->getOperand(0), SE); 737 auto *Factor = Op0Pair.first; 738 if (SE.isKnownNegative(Factor)) { 739 Factor = cast<SCEVConstant>(SE.getNegativeSCEV(Factor)); 740 LeftOvers.push_back(SE.getNegativeSCEV(Op0Pair.second)); 741 } else { 742 LeftOvers.push_back(Op0Pair.second); 743 } 744 745 for (unsigned u = 1, e = Add->getNumOperands(); u < e; u++) { 746 auto OpUPair = extractConstantFactor(Add->getOperand(u), SE); 747 // TODO: Use something smarter than equality here, e.g., gcd. 748 if (Factor == OpUPair.first) 749 LeftOvers.push_back(OpUPair.second); 750 else if (Factor == SE.getNegativeSCEV(OpUPair.first)) 751 LeftOvers.push_back(SE.getNegativeSCEV(OpUPair.second)); 752 else 753 return std::make_pair(ConstPart, S); 754 } 755 756 auto *NewAdd = SE.getAddExpr(LeftOvers, Add->getNoWrapFlags()); 757 return std::make_pair(Factor, NewAdd); 758 } 759 760 auto *Mul = dyn_cast<SCEVMulExpr>(S); 761 if (!Mul) 762 return std::make_pair(ConstPart, S); 763 764 SmallVector<const SCEV *, 4> LeftOvers; 765 for (auto *Op : Mul->operands()) 766 if (isa<SCEVConstant>(Op)) 767 ConstPart = cast<SCEVConstant>(SE.getMulExpr(ConstPart, Op)); 768 else 769 LeftOvers.push_back(Op); 770 771 return std::make_pair(ConstPart, SE.getMulExpr(LeftOvers)); 772 } 773 774 const SCEV *tryForwardThroughPHI(const SCEV *Expr, Region &R, 775 ScalarEvolution &SE, LoopInfo &LI, 776 const DominatorTree &DT) { 777 if (auto *Unknown = dyn_cast<SCEVUnknown>(Expr)) { 778 Value *V = Unknown->getValue(); 779 auto *PHI = dyn_cast<PHINode>(V); 780 if (!PHI) 781 return Expr; 782 783 Value *Final = nullptr; 784 785 for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) { 786 BasicBlock *Incoming = PHI->getIncomingBlock(i); 787 if (isErrorBlock(*Incoming, R, LI, DT) && R.contains(Incoming)) 788 continue; 789 if (Final) 790 return Expr; 791 Final = PHI->getIncomingValue(i); 792 } 793 794 if (Final) 795 return SE.getSCEV(Final); 796 } 797 return Expr; 798 } 799 800 Value *getUniqueNonErrorValue(PHINode *PHI, Region *R, LoopInfo &LI, 801 const DominatorTree &DT) { 802 Value *V = nullptr; 803 for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) { 804 BasicBlock *BB = PHI->getIncomingBlock(i); 805 if (!isErrorBlock(*BB, *R, LI, DT)) { 806 if (V) 807 return nullptr; 808 V = PHI->getIncomingValue(i); 809 } 810 } 811 812 return V; 813 } 814 } // namespace polly 815