1 //===--------- SCEVAffinator.cpp - Create Scops from LLVM IR -------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Create a polyhedral description for a SCEV value. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "polly/Support/SCEVAffinator.h" 14 #include "polly/Options.h" 15 #include "polly/ScopInfo.h" 16 #include "polly/Support/GICHelper.h" 17 #include "polly/Support/SCEVValidator.h" 18 #include "isl/aff.h" 19 #include "isl/local_space.h" 20 #include "isl/set.h" 21 #include "isl/val.h" 22 23 using namespace llvm; 24 using namespace polly; 25 26 static cl::opt<bool> IgnoreIntegerWrapping( 27 "polly-ignore-integer-wrapping", 28 cl::desc("Do not build run-time checks to proof absence of integer " 29 "wrapping"), 30 cl::Hidden, cl::ZeroOrMore, cl::init(false), cl::cat(PollyCategory)); 31 32 // The maximal number of basic sets we allow during the construction of a 33 // piecewise affine function. More complex ones will result in very high 34 // compile time. 35 static int const MaxDisjunctionsInPwAff = 100; 36 37 // The maximal number of bits for which a general expression is modeled 38 // precisely. 39 static unsigned const MaxSmallBitWidth = 7; 40 41 /// Add the number of basic sets in @p Domain to @p User 42 static isl_stat addNumBasicSets(__isl_take isl_set *Domain, 43 __isl_take isl_aff *Aff, void *User) { 44 auto *NumBasicSets = static_cast<unsigned *>(User); 45 *NumBasicSets += isl_set_n_basic_set(Domain); 46 isl_set_free(Domain); 47 isl_aff_free(Aff); 48 return isl_stat_ok; 49 } 50 51 /// Determine if @p PWAC is too complex to continue. 52 static bool isTooComplex(PWACtx PWAC) { 53 unsigned NumBasicSets = 0; 54 isl_pw_aff_foreach_piece(PWAC.first.get(), addNumBasicSets, &NumBasicSets); 55 if (NumBasicSets <= MaxDisjunctionsInPwAff) 56 return false; 57 return true; 58 } 59 60 /// Return the flag describing the possible wrapping of @p Expr. 61 static SCEV::NoWrapFlags getNoWrapFlags(const SCEV *Expr) { 62 if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr)) 63 return NAry->getNoWrapFlags(); 64 return SCEV::NoWrapMask; 65 } 66 67 static PWACtx combine(PWACtx PWAC0, PWACtx PWAC1, 68 __isl_give isl_pw_aff *(Fn)(__isl_take isl_pw_aff *, 69 __isl_take isl_pw_aff *)) { 70 PWAC0.first = isl::manage(Fn(PWAC0.first.release(), PWAC1.first.release())); 71 PWAC0.second = PWAC0.second.unite(PWAC1.second); 72 return PWAC0; 73 } 74 75 static __isl_give isl_pw_aff *getWidthExpValOnDomain(unsigned Width, 76 __isl_take isl_set *Dom) { 77 auto *Ctx = isl_set_get_ctx(Dom); 78 auto *WidthVal = isl_val_int_from_ui(Ctx, Width); 79 auto *ExpVal = isl_val_2exp(WidthVal); 80 return isl_pw_aff_val_on_domain(Dom, ExpVal); 81 } 82 83 SCEVAffinator::SCEVAffinator(Scop *S, LoopInfo &LI) 84 : S(S), Ctx(S->getIslCtx().get()), SE(*S->getSE()), LI(LI), 85 TD(S->getFunction().getParent()->getDataLayout()) {} 86 87 Loop *SCEVAffinator::getScope() { return BB ? LI.getLoopFor(BB) : nullptr; } 88 89 void SCEVAffinator::interpretAsUnsigned(PWACtx &PWAC, unsigned Width) { 90 auto *NonNegDom = isl_pw_aff_nonneg_set(PWAC.first.copy()); 91 auto *NonNegPWA = 92 isl_pw_aff_intersect_domain(PWAC.first.copy(), isl_set_copy(NonNegDom)); 93 auto *ExpPWA = getWidthExpValOnDomain(Width, isl_set_complement(NonNegDom)); 94 PWAC.first = isl::manage(isl_pw_aff_union_add( 95 NonNegPWA, isl_pw_aff_add(PWAC.first.release(), ExpPWA))); 96 } 97 98 void SCEVAffinator::takeNonNegativeAssumption( 99 PWACtx &PWAC, RecordedAssumptionsTy *RecordedAssumptions) { 100 this->RecordedAssumptions = RecordedAssumptions; 101 102 auto *NegPWA = isl_pw_aff_neg(PWAC.first.copy()); 103 auto *NegDom = isl_pw_aff_pos_set(NegPWA); 104 PWAC.second = 105 isl::manage(isl_set_union(PWAC.second.release(), isl_set_copy(NegDom))); 106 auto *Restriction = BB ? NegDom : isl_set_params(NegDom); 107 auto DL = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); 108 recordAssumption(RecordedAssumptions, UNSIGNED, isl::manage(Restriction), DL, 109 AS_RESTRICTION, BB); 110 } 111 112 PWACtx SCEVAffinator::getPWACtxFromPWA(isl::pw_aff PWA) { 113 return std::make_pair(PWA, isl::set::empty(isl::space(Ctx, 0, NumIterators))); 114 } 115 116 PWACtx SCEVAffinator::getPwAff(const SCEV *Expr, BasicBlock *BB, 117 RecordedAssumptionsTy *RecordedAssumptions) { 118 this->BB = BB; 119 this->RecordedAssumptions = RecordedAssumptions; 120 121 if (BB) { 122 auto *DC = S->getDomainConditions(BB).release(); 123 NumIterators = isl_set_n_dim(DC); 124 isl_set_free(DC); 125 } else 126 NumIterators = 0; 127 128 return visit(Expr); 129 } 130 131 PWACtx SCEVAffinator::checkForWrapping(const SCEV *Expr, PWACtx PWAC) const { 132 // If the SCEV flags do contain NSW (no signed wrap) then PWA already 133 // represents Expr in modulo semantic (it is not allowed to overflow), thus we 134 // are done. Otherwise, we will compute: 135 // PWA = ((PWA + 2^(n-1)) mod (2 ^ n)) - 2^(n-1) 136 // whereas n is the number of bits of the Expr, hence: 137 // n = bitwidth(ExprType) 138 139 if (IgnoreIntegerWrapping || (getNoWrapFlags(Expr) & SCEV::FlagNSW)) 140 return PWAC; 141 142 isl::pw_aff PWAMod = addModuloSemantic(PWAC.first, Expr->getType()); 143 144 isl::set NotEqualSet = PWAC.first.ne_set(PWAMod); 145 PWAC.second = PWAC.second.unite(NotEqualSet).coalesce(); 146 147 const DebugLoc &Loc = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); 148 if (!BB) 149 NotEqualSet = NotEqualSet.params(); 150 NotEqualSet = NotEqualSet.coalesce(); 151 152 if (!NotEqualSet.is_empty()) 153 recordAssumption(RecordedAssumptions, WRAPPING, NotEqualSet, Loc, 154 AS_RESTRICTION, BB); 155 156 return PWAC; 157 } 158 159 isl::pw_aff SCEVAffinator::addModuloSemantic(isl::pw_aff PWA, 160 Type *ExprType) const { 161 unsigned Width = TD.getTypeSizeInBits(ExprType); 162 163 auto ModVal = isl::val::int_from_ui(Ctx, Width); 164 ModVal = ModVal.pow2(); 165 166 isl::set Domain = PWA.domain(); 167 isl::pw_aff AddPW = 168 isl::manage(getWidthExpValOnDomain(Width - 1, Domain.release())); 169 170 return PWA.add(AddPW).mod(ModVal).sub(AddPW); 171 } 172 173 bool SCEVAffinator::hasNSWAddRecForLoop(Loop *L) const { 174 for (const auto &CachedPair : CachedExpressions) { 175 auto *AddRec = dyn_cast<SCEVAddRecExpr>(CachedPair.first.first); 176 if (!AddRec) 177 continue; 178 if (AddRec->getLoop() != L) 179 continue; 180 if (AddRec->getNoWrapFlags() & SCEV::FlagNSW) 181 return true; 182 } 183 184 return false; 185 } 186 187 bool SCEVAffinator::computeModuloForExpr(const SCEV *Expr) { 188 unsigned Width = TD.getTypeSizeInBits(Expr->getType()); 189 // We assume nsw expressions never overflow. 190 if (auto *NAry = dyn_cast<SCEVNAryExpr>(Expr)) 191 if (NAry->getNoWrapFlags() & SCEV::FlagNSW) 192 return false; 193 return Width <= MaxSmallBitWidth; 194 } 195 196 PWACtx SCEVAffinator::visit(const SCEV *Expr) { 197 198 auto Key = std::make_pair(Expr, BB); 199 PWACtx PWAC = CachedExpressions[Key]; 200 if (PWAC.first) 201 return PWAC; 202 203 auto ConstantAndLeftOverPair = extractConstantFactor(Expr, SE); 204 auto *Factor = ConstantAndLeftOverPair.first; 205 Expr = ConstantAndLeftOverPair.second; 206 207 auto *Scope = getScope(); 208 S->addParams(getParamsInAffineExpr(&S->getRegion(), Scope, Expr, SE)); 209 210 // In case the scev is a valid parameter, we do not further analyze this 211 // expression, but create a new parameter in the isl_pw_aff. This allows us 212 // to treat subexpressions that we cannot translate into an piecewise affine 213 // expression, as constant parameters of the piecewise affine expression. 214 if (isl_id *Id = S->getIdForParam(Expr).release()) { 215 isl_space *Space = isl_space_set_alloc(Ctx.get(), 1, NumIterators); 216 Space = isl_space_set_dim_id(Space, isl_dim_param, 0, Id); 217 218 isl_set *Domain = isl_set_universe(isl_space_copy(Space)); 219 isl_aff *Affine = isl_aff_zero_on_domain(isl_local_space_from_space(Space)); 220 Affine = isl_aff_add_coefficient_si(Affine, isl_dim_param, 0, 1); 221 222 PWAC = getPWACtxFromPWA(isl::manage(isl_pw_aff_alloc(Domain, Affine))); 223 } else { 224 PWAC = SCEVVisitor<SCEVAffinator, PWACtx>::visit(Expr); 225 if (computeModuloForExpr(Expr)) 226 PWAC.first = addModuloSemantic(PWAC.first, Expr->getType()); 227 else 228 PWAC = checkForWrapping(Expr, PWAC); 229 } 230 231 if (!Factor->getType()->isIntegerTy(1)) { 232 PWAC = combine(PWAC, visitConstant(Factor), isl_pw_aff_mul); 233 if (computeModuloForExpr(Key.first)) 234 PWAC.first = addModuloSemantic(PWAC.first, Expr->getType()); 235 } 236 237 // For compile time reasons we need to simplify the PWAC before we cache and 238 // return it. 239 PWAC.first = PWAC.first.coalesce(); 240 if (!computeModuloForExpr(Key.first)) 241 PWAC = checkForWrapping(Key.first, PWAC); 242 243 CachedExpressions[Key] = PWAC; 244 return PWAC; 245 } 246 247 PWACtx SCEVAffinator::visitConstant(const SCEVConstant *Expr) { 248 ConstantInt *Value = Expr->getValue(); 249 isl_val *v; 250 251 // LLVM does not define if an integer value is interpreted as a signed or 252 // unsigned value. Hence, without further information, it is unknown how 253 // this value needs to be converted to GMP. At the moment, we only support 254 // signed operations. So we just interpret it as signed. Later, there are 255 // two options: 256 // 257 // 1. We always interpret any value as signed and convert the values on 258 // demand. 259 // 2. We pass down the signedness of the calculation and use it to interpret 260 // this constant correctly. 261 v = isl_valFromAPInt(Ctx.get(), Value->getValue(), /* isSigned */ true); 262 263 isl_space *Space = isl_space_set_alloc(Ctx.get(), 0, NumIterators); 264 isl_local_space *ls = isl_local_space_from_space(Space); 265 return getPWACtxFromPWA( 266 isl::manage(isl_pw_aff_from_aff(isl_aff_val_on_domain(ls, v)))); 267 } 268 269 PWACtx SCEVAffinator::visitTruncateExpr(const SCEVTruncateExpr *Expr) { 270 // Truncate operations are basically modulo operations, thus we can 271 // model them that way. However, for large types we assume the operand 272 // to fit in the new type size instead of introducing a modulo with a very 273 // large constant. 274 275 auto *Op = Expr->getOperand(); 276 auto OpPWAC = visit(Op); 277 278 unsigned Width = TD.getTypeSizeInBits(Expr->getType()); 279 280 if (computeModuloForExpr(Expr)) 281 return OpPWAC; 282 283 auto *Dom = OpPWAC.first.domain().release(); 284 auto *ExpPWA = getWidthExpValOnDomain(Width - 1, Dom); 285 auto *GreaterDom = 286 isl_pw_aff_ge_set(OpPWAC.first.copy(), isl_pw_aff_copy(ExpPWA)); 287 auto *SmallerDom = 288 isl_pw_aff_lt_set(OpPWAC.first.copy(), isl_pw_aff_neg(ExpPWA)); 289 auto *OutOfBoundsDom = isl_set_union(SmallerDom, GreaterDom); 290 OpPWAC.second = OpPWAC.second.unite(isl::manage_copy(OutOfBoundsDom)); 291 292 if (!BB) { 293 assert(isl_set_dim(OutOfBoundsDom, isl_dim_set) == 0 && 294 "Expected a zero dimensional set for non-basic-block domains"); 295 OutOfBoundsDom = isl_set_params(OutOfBoundsDom); 296 } 297 298 recordAssumption(RecordedAssumptions, UNSIGNED, isl::manage(OutOfBoundsDom), 299 DebugLoc(), AS_RESTRICTION, BB); 300 301 return OpPWAC; 302 } 303 304 PWACtx SCEVAffinator::visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { 305 // A zero-extended value can be interpreted as a piecewise defined signed 306 // value. If the value was non-negative it stays the same, otherwise it 307 // is the sum of the original value and 2^n where n is the bit-width of 308 // the original (or operand) type. Examples: 309 // zext i8 127 to i32 -> { [127] } 310 // zext i8 -1 to i32 -> { [256 + (-1)] } = { [255] } 311 // zext i8 %v to i32 -> [v] -> { [v] | v >= 0; [256 + v] | v < 0 } 312 // 313 // However, LLVM/Scalar Evolution uses zero-extend (potentially lead by a 314 // truncate) to represent some forms of modulo computation. The left-hand side 315 // of the condition in the code below would result in the SCEV 316 // "zext i1 <false, +, true>for.body" which is just another description 317 // of the C expression "i & 1 != 0" or, equivalently, "i % 2 != 0". 318 // 319 // for (i = 0; i < N; i++) 320 // if (i & 1 != 0 /* == i % 2 */) 321 // /* do something */ 322 // 323 // If we do not make the modulo explicit but only use the mechanism described 324 // above we will get the very restrictive assumption "N < 3", because for all 325 // values of N >= 3 the SCEVAddRecExpr operand of the zero-extend would wrap. 326 // Alternatively, we can make the modulo in the operand explicit in the 327 // resulting piecewise function and thereby avoid the assumption on N. For the 328 // example this would result in the following piecewise affine function: 329 // { [i0] -> [(1)] : 2*floor((-1 + i0)/2) = -1 + i0; 330 // [i0] -> [(0)] : 2*floor((i0)/2) = i0 } 331 // To this end we can first determine if the (immediate) operand of the 332 // zero-extend can wrap and, in case it might, we will use explicit modulo 333 // semantic to compute the result instead of emitting non-wrapping 334 // assumptions. 335 // 336 // Note that operands with large bit-widths are less likely to be negative 337 // because it would result in a very large access offset or loop bound after 338 // the zero-extend. To this end one can optimistically assume the operand to 339 // be positive and avoid the piecewise definition if the bit-width is bigger 340 // than some threshold (here MaxZextSmallBitWidth). 341 // 342 // We choose to go with a hybrid solution of all modeling techniques described 343 // above. For small bit-widths (up to MaxZextSmallBitWidth) we will model the 344 // wrapping explicitly and use a piecewise defined function. However, if the 345 // bit-width is bigger than MaxZextSmallBitWidth we will employ overflow 346 // assumptions and assume the "former negative" piece will not exist. 347 348 auto *Op = Expr->getOperand(); 349 auto OpPWAC = visit(Op); 350 351 // If the width is to big we assume the negative part does not occur. 352 if (!computeModuloForExpr(Op)) { 353 takeNonNegativeAssumption(OpPWAC, RecordedAssumptions); 354 return OpPWAC; 355 } 356 357 // If the width is small build the piece for the non-negative part and 358 // the one for the negative part and unify them. 359 unsigned Width = TD.getTypeSizeInBits(Op->getType()); 360 interpretAsUnsigned(OpPWAC, Width); 361 return OpPWAC; 362 } 363 364 PWACtx SCEVAffinator::visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { 365 // As all values are represented as signed, a sign extension is a noop. 366 return visit(Expr->getOperand()); 367 } 368 369 PWACtx SCEVAffinator::visitAddExpr(const SCEVAddExpr *Expr) { 370 PWACtx Sum = visit(Expr->getOperand(0)); 371 372 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { 373 Sum = combine(Sum, visit(Expr->getOperand(i)), isl_pw_aff_add); 374 if (isTooComplex(Sum)) 375 return complexityBailout(); 376 } 377 378 return Sum; 379 } 380 381 PWACtx SCEVAffinator::visitMulExpr(const SCEVMulExpr *Expr) { 382 PWACtx Prod = visit(Expr->getOperand(0)); 383 384 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { 385 Prod = combine(Prod, visit(Expr->getOperand(i)), isl_pw_aff_mul); 386 if (isTooComplex(Prod)) 387 return complexityBailout(); 388 } 389 390 return Prod; 391 } 392 393 PWACtx SCEVAffinator::visitAddRecExpr(const SCEVAddRecExpr *Expr) { 394 assert(Expr->isAffine() && "Only affine AddRecurrences allowed"); 395 396 auto Flags = Expr->getNoWrapFlags(); 397 398 // Directly generate isl_pw_aff for Expr if 'start' is zero. 399 if (Expr->getStart()->isZero()) { 400 assert(S->contains(Expr->getLoop()) && 401 "Scop does not contain the loop referenced in this AddRec"); 402 403 PWACtx Step = visit(Expr->getOperand(1)); 404 isl_space *Space = isl_space_set_alloc(Ctx.get(), 0, NumIterators); 405 isl_local_space *LocalSpace = isl_local_space_from_space(Space); 406 407 unsigned loopDimension = S->getRelativeLoopDepth(Expr->getLoop()); 408 409 isl_aff *LAff = isl_aff_set_coefficient_si( 410 isl_aff_zero_on_domain(LocalSpace), isl_dim_in, loopDimension, 1); 411 isl_pw_aff *LPwAff = isl_pw_aff_from_aff(LAff); 412 413 Step.first = Step.first.mul(isl::manage(LPwAff)); 414 return Step; 415 } 416 417 // Translate AddRecExpr from '{start, +, inc}' into 'start + {0, +, inc}' 418 // if 'start' is not zero. 419 // TODO: Using the original SCEV no-wrap flags is not always safe, however 420 // as our code generation is reordering the expression anyway it doesn't 421 // really matter. 422 const SCEV *ZeroStartExpr = 423 SE.getAddRecExpr(SE.getConstant(Expr->getStart()->getType(), 0), 424 Expr->getStepRecurrence(SE), Expr->getLoop(), Flags); 425 426 PWACtx Result = visit(ZeroStartExpr); 427 PWACtx Start = visit(Expr->getStart()); 428 Result = combine(Result, Start, isl_pw_aff_add); 429 return Result; 430 } 431 432 PWACtx SCEVAffinator::visitSMaxExpr(const SCEVSMaxExpr *Expr) { 433 PWACtx Max = visit(Expr->getOperand(0)); 434 435 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { 436 Max = combine(Max, visit(Expr->getOperand(i)), isl_pw_aff_max); 437 if (isTooComplex(Max)) 438 return complexityBailout(); 439 } 440 441 return Max; 442 } 443 444 PWACtx SCEVAffinator::visitSMinExpr(const SCEVSMinExpr *Expr) { 445 PWACtx Min = visit(Expr->getOperand(0)); 446 447 for (int i = 1, e = Expr->getNumOperands(); i < e; ++i) { 448 Min = combine(Min, visit(Expr->getOperand(i)), isl_pw_aff_min); 449 if (isTooComplex(Min)) 450 return complexityBailout(); 451 } 452 453 return Min; 454 } 455 456 PWACtx SCEVAffinator::visitUMaxExpr(const SCEVUMaxExpr *Expr) { 457 llvm_unreachable("SCEVUMaxExpr not yet supported"); 458 } 459 460 PWACtx SCEVAffinator::visitUMinExpr(const SCEVUMinExpr *Expr) { 461 llvm_unreachable("SCEVUMinExpr not yet supported"); 462 } 463 464 PWACtx SCEVAffinator::visitUDivExpr(const SCEVUDivExpr *Expr) { 465 // The handling of unsigned division is basically the same as for signed 466 // division, except the interpretation of the operands. As the divisor 467 // has to be constant in both cases we can simply interpret it as an 468 // unsigned value without additional complexity in the representation. 469 // For the dividend we could choose from the different representation 470 // schemes introduced for zero-extend operations but for now we will 471 // simply use an assumption. 472 auto *Dividend = Expr->getLHS(); 473 auto *Divisor = Expr->getRHS(); 474 assert(isa<SCEVConstant>(Divisor) && 475 "UDiv is no parameter but has a non-constant RHS."); 476 477 auto DividendPWAC = visit(Dividend); 478 auto DivisorPWAC = visit(Divisor); 479 480 if (SE.isKnownNegative(Divisor)) { 481 // Interpret negative divisors unsigned. This is a special case of the 482 // piece-wise defined value described for zero-extends as we already know 483 // the actual value of the constant divisor. 484 unsigned Width = TD.getTypeSizeInBits(Expr->getType()); 485 auto *DivisorDom = DivisorPWAC.first.domain().release(); 486 auto *WidthExpPWA = getWidthExpValOnDomain(Width, DivisorDom); 487 DivisorPWAC.first = DivisorPWAC.first.add(isl::manage(WidthExpPWA)); 488 } 489 490 // TODO: One can represent the dividend as piece-wise function to be more 491 // precise but therefor a heuristic is needed. 492 493 // Assume a non-negative dividend. 494 takeNonNegativeAssumption(DividendPWAC, RecordedAssumptions); 495 496 DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_div); 497 DividendPWAC.first = DividendPWAC.first.floor(); 498 499 return DividendPWAC; 500 } 501 502 PWACtx SCEVAffinator::visitSDivInstruction(Instruction *SDiv) { 503 assert(SDiv->getOpcode() == Instruction::SDiv && "Assumed SDiv instruction!"); 504 505 auto *Scope = getScope(); 506 auto *Divisor = SDiv->getOperand(1); 507 auto *DivisorSCEV = SE.getSCEVAtScope(Divisor, Scope); 508 auto DivisorPWAC = visit(DivisorSCEV); 509 assert(isa<SCEVConstant>(DivisorSCEV) && 510 "SDiv is no parameter but has a non-constant RHS."); 511 512 auto *Dividend = SDiv->getOperand(0); 513 auto *DividendSCEV = SE.getSCEVAtScope(Dividend, Scope); 514 auto DividendPWAC = visit(DividendSCEV); 515 DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_tdiv_q); 516 return DividendPWAC; 517 } 518 519 PWACtx SCEVAffinator::visitSRemInstruction(Instruction *SRem) { 520 assert(SRem->getOpcode() == Instruction::SRem && "Assumed SRem instruction!"); 521 522 auto *Scope = getScope(); 523 auto *Divisor = SRem->getOperand(1); 524 auto *DivisorSCEV = SE.getSCEVAtScope(Divisor, Scope); 525 auto DivisorPWAC = visit(DivisorSCEV); 526 assert(isa<ConstantInt>(Divisor) && 527 "SRem is no parameter but has a non-constant RHS."); 528 529 auto *Dividend = SRem->getOperand(0); 530 auto *DividendSCEV = SE.getSCEVAtScope(Dividend, Scope); 531 auto DividendPWAC = visit(DividendSCEV); 532 DividendPWAC = combine(DividendPWAC, DivisorPWAC, isl_pw_aff_tdiv_r); 533 return DividendPWAC; 534 } 535 536 PWACtx SCEVAffinator::visitUnknown(const SCEVUnknown *Expr) { 537 if (Instruction *I = dyn_cast<Instruction>(Expr->getValue())) { 538 switch (I->getOpcode()) { 539 case Instruction::IntToPtr: 540 return visit(SE.getSCEVAtScope(I->getOperand(0), getScope())); 541 case Instruction::PtrToInt: 542 return visit(SE.getSCEVAtScope(I->getOperand(0), getScope())); 543 case Instruction::SDiv: 544 return visitSDivInstruction(I); 545 case Instruction::SRem: 546 return visitSRemInstruction(I); 547 default: 548 break; // Fall through. 549 } 550 } 551 552 llvm_unreachable( 553 "Unknowns SCEV was neither parameter nor a valid instruction."); 554 } 555 556 PWACtx SCEVAffinator::complexityBailout() { 557 // We hit the complexity limit for affine expressions; invalidate the scop 558 // and return a constant zero. 559 const DebugLoc &Loc = BB ? BB->getTerminator()->getDebugLoc() : DebugLoc(); 560 S->invalidate(COMPLEXITY, Loc); 561 return visit(SE.getZero(Type::getInt32Ty(S->getFunction().getContext()))); 562 } 563