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