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