1 //===------ IslExprBuilder.cpp ----- Code generate isl AST expressions ----===// 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 //===----------------------------------------------------------------------===// 11 12 #include "polly/CodeGen/IslExprBuilder.h" 13 #include "polly/Options.h" 14 #include "polly/ScopInfo.h" 15 #include "polly/Support/GICHelper.h" 16 #include "polly/Support/ScopHelper.h" 17 #include "llvm/Support/Debug.h" 18 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 19 20 using namespace llvm; 21 using namespace polly; 22 23 /// @brief Different overflow tracking modes. 24 enum OverflowTrackingChoice { 25 OT_NEVER, ///< Never tack potential overflows. 26 OT_REQUEST, ///< Track potential overflows if requested. 27 OT_ALWAYS ///< Always track potential overflows. 28 }; 29 30 static cl::opt<OverflowTrackingChoice> OTMode( 31 "polly-overflow-tracking", 32 cl::desc("Define where potential integer overflows in generated " 33 "expressions should be tracked."), 34 cl::values(clEnumValN(OT_NEVER, "never", "Never track the overflow bit."), 35 clEnumValN(OT_REQUEST, "request", 36 "Track the overflow bit if requested."), 37 clEnumValN(OT_ALWAYS, "always", 38 "Always track the overflow bit."), 39 clEnumValEnd), 40 cl::Hidden, cl::init(OT_REQUEST), cl::ZeroOrMore, cl::cat(PollyCategory)); 41 42 // @TODO This should actually be derived from the DataLayout. 43 static cl::opt<unsigned> PollyMaxAllowedBitWidth( 44 "polly-max-expr-bit-width", 45 cl::desc("The maximal bit with for generated expressions."), cl::Hidden, 46 cl::ZeroOrMore, cl::init(64), cl::cat(PollyCategory)); 47 48 IslExprBuilder::IslExprBuilder(Scop &S, PollyIRBuilder &Builder, 49 IDToValueTy &IDToValue, ValueMapT &GlobalMap, 50 const DataLayout &DL, ScalarEvolution &SE, 51 DominatorTree &DT, LoopInfo &LI) 52 : S(S), Builder(Builder), IDToValue(IDToValue), GlobalMap(GlobalMap), 53 DL(DL), SE(SE), DT(DT), LI(LI) { 54 OverflowState = (OTMode == OT_ALWAYS) ? Builder.getFalse() : nullptr; 55 } 56 57 void IslExprBuilder::setTrackOverflow(bool Enable) { 58 // If potential overflows are tracked always or never we ignore requests 59 // to change the behaviour. 60 if (OTMode != OT_REQUEST) 61 return; 62 63 if (Enable) { 64 // If tracking should be enabled initialize the OverflowState. 65 OverflowState = Builder.getFalse(); 66 } else { 67 // If tracking should be disabled just unset the OverflowState. 68 OverflowState = nullptr; 69 } 70 } 71 72 Value *IslExprBuilder::getOverflowState() const { 73 // If the overflow tracking was requested but it is disabled we avoid the 74 // additional nullptr checks at the call sides but instead provide a 75 // meaningful result. 76 if (OTMode == OT_NEVER) 77 return Builder.getFalse(); 78 return OverflowState; 79 } 80 81 Value *IslExprBuilder::createBinOp(BinaryOperator::BinaryOps Opc, Value *LHS, 82 Value *RHS, const Twine &Name) { 83 // Flag that is true if the computation cannot overflow. 84 bool IsSafeToCompute = false; 85 switch (Opc) { 86 case Instruction::Add: 87 case Instruction::Sub: 88 IsSafeToCompute = adjustTypesForSafeAddition(LHS, RHS); 89 break; 90 case Instruction::Mul: 91 IsSafeToCompute = adjustTypesForSafeMultiplication(LHS, RHS); 92 break; 93 default: 94 llvm_unreachable("Unknown binary operator!"); 95 } 96 97 // Handle the plain operation (without overflow tracking or a safe 98 // computation) first. 99 if (!OverflowState || (IsSafeToCompute && (OTMode != OT_ALWAYS))) { 100 switch (Opc) { 101 case Instruction::Add: 102 return Builder.CreateNSWAdd(LHS, RHS, Name); 103 case Instruction::Sub: 104 return Builder.CreateNSWSub(LHS, RHS, Name); 105 case Instruction::Mul: 106 return Builder.CreateNSWMul(LHS, RHS, Name); 107 default: 108 llvm_unreachable("Unknown binary operator!"); 109 } 110 } 111 112 Function *F = nullptr; 113 Module *M = Builder.GetInsertBlock()->getModule(); 114 switch (Opc) { 115 case Instruction::Add: 116 F = Intrinsic::getDeclaration(M, Intrinsic::sadd_with_overflow, 117 {LHS->getType()}); 118 break; 119 case Instruction::Sub: 120 F = Intrinsic::getDeclaration(M, Intrinsic::ssub_with_overflow, 121 {LHS->getType()}); 122 break; 123 case Instruction::Mul: 124 F = Intrinsic::getDeclaration(M, Intrinsic::smul_with_overflow, 125 {LHS->getType()}); 126 break; 127 default: 128 llvm_unreachable("No overflow intrinsic for binary operator found!"); 129 } 130 131 auto *ResultStruct = Builder.CreateCall(F, {LHS, RHS}, Name); 132 assert(ResultStruct->getType()->isStructTy()); 133 134 auto *OverflowFlag = 135 Builder.CreateExtractValue(ResultStruct, 1, Name + ".obit"); 136 137 // If all overflows are tracked we do not combine the results as this could 138 // cause dominance problems. Instead we will always keep the last overflow 139 // flag as current state. 140 if (OTMode == OT_ALWAYS) 141 OverflowState = OverflowFlag; 142 else 143 OverflowState = 144 Builder.CreateOr(OverflowState, OverflowFlag, "polly.overflow.state"); 145 146 return Builder.CreateExtractValue(ResultStruct, 0, Name + ".res"); 147 } 148 149 Value *IslExprBuilder::createAdd(Value *LHS, Value *RHS, const Twine &Name) { 150 return createBinOp(Instruction::Add, LHS, RHS, Name); 151 } 152 153 Value *IslExprBuilder::createSub(Value *LHS, Value *RHS, const Twine &Name) { 154 return createBinOp(Instruction::Sub, LHS, RHS, Name); 155 } 156 157 Value *IslExprBuilder::createMul(Value *LHS, Value *RHS, const Twine &Name) { 158 return createBinOp(Instruction::Mul, LHS, RHS, Name); 159 } 160 161 static Type *getWidestType(Type *T1, Type *T2) { 162 assert(isa<IntegerType>(T1) && isa<IntegerType>(T2)); 163 164 if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits()) 165 return T2; 166 else 167 return T1; 168 } 169 170 void IslExprBuilder::unifyTypes(Value *&V0, Value *&V1, Value *&V2) { 171 auto *T0 = V0->getType(); 172 auto *T1 = V1->getType(); 173 auto *T2 = V2->getType(); 174 if (T0 == T1 && T1 == T2) 175 return; 176 auto *MaxT = getWidestType(T0, T1); 177 MaxT = getWidestType(MaxT, T2); 178 V0 = Builder.CreateSExt(V0, MaxT); 179 V1 = Builder.CreateSExt(V1, MaxT); 180 V2 = Builder.CreateSExt(V2, MaxT); 181 } 182 183 bool IslExprBuilder::adjustTypesForSafeComputation(Value *&LHS, Value *&RHS, 184 unsigned RequiredBitWidth) { 185 unsigned LBitWidth = LHS->getType()->getPrimitiveSizeInBits(); 186 unsigned RBitWidth = RHS->getType()->getPrimitiveSizeInBits(); 187 unsigned MaxUsedBitWidth = std::max(LBitWidth, RBitWidth); 188 189 // @TODO For now use the maximal bit width if the required one is to large but 190 // note that this is not sound. 191 unsigned MaxAllowedBitWidth = PollyMaxAllowedBitWidth; 192 unsigned NewBitWidth = 193 std::max(MaxUsedBitWidth, std::min(MaxAllowedBitWidth, RequiredBitWidth)); 194 195 Type *Ty = Builder.getIntNTy(NewBitWidth); 196 LHS = Builder.CreateSExt(LHS, Ty); 197 RHS = Builder.CreateSExt(RHS, Ty); 198 199 // If the new bit width is not large enough the computation is not sound. 200 return NewBitWidth == RequiredBitWidth; 201 } 202 203 bool IslExprBuilder::adjustTypesForSafeAddition(Value *&LHS, Value *&RHS) { 204 unsigned LBitWidth = LHS->getType()->getPrimitiveSizeInBits(); 205 unsigned RBitWidth = RHS->getType()->getPrimitiveSizeInBits(); 206 return adjustTypesForSafeComputation(LHS, RHS, 207 std::max(LBitWidth, RBitWidth) + 1); 208 } 209 210 bool IslExprBuilder::adjustTypesForSafeMultiplication(Value *&LHS, 211 Value *&RHS) { 212 unsigned LBitWidth = LHS->getType()->getPrimitiveSizeInBits(); 213 unsigned RBitWidth = RHS->getType()->getPrimitiveSizeInBits(); 214 return adjustTypesForSafeComputation(LHS, RHS, LBitWidth + RBitWidth); 215 } 216 217 Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) { 218 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus && 219 "Unsupported unary operation"); 220 221 auto *V = create(isl_ast_expr_get_op_arg(Expr, 0)); 222 assert(V->getType()->isIntegerTy() && 223 "Unary expressions can only be created for integer types"); 224 225 isl_ast_expr_free(Expr); 226 return createSub(ConstantInt::getNullValue(V->getType()), V); 227 } 228 229 Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) { 230 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 231 "isl ast expression not of type isl_ast_op"); 232 assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 && 233 "We need at least two operands in an n-ary operation"); 234 assert((isl_ast_expr_get_op_type(Expr) == isl_ast_op_max || 235 isl_ast_expr_get_op_type(Expr) == isl_ast_op_min) && 236 "This is no n-ary isl ast expression"); 237 238 bool IsMax = isl_ast_expr_get_op_type(Expr) == isl_ast_op_max; 239 auto Pred = IsMax ? CmpInst::ICMP_SGT : CmpInst::ICMP_SLT; 240 auto *V = create(isl_ast_expr_get_op_arg(Expr, 0)); 241 242 for (int i = 1; i < isl_ast_expr_get_op_n_arg(Expr); ++i) { 243 auto *OpV = create(isl_ast_expr_get_op_arg(Expr, i)); 244 unifyTypes(V, OpV); 245 V = Builder.CreateSelect(Builder.CreateICmp(Pred, V, OpV), V, OpV); 246 } 247 248 isl_ast_expr_free(Expr); 249 return V; 250 } 251 252 Value *IslExprBuilder::createAccessAddress(isl_ast_expr *Expr) { 253 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 254 "isl ast expression not of type isl_ast_op"); 255 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_access && 256 "not an access isl ast expression"); 257 assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 && 258 "We need at least two operands to create a member access."); 259 260 Value *Base, *IndexOp, *Access; 261 isl_ast_expr *BaseExpr; 262 isl_id *BaseId; 263 264 BaseExpr = isl_ast_expr_get_op_arg(Expr, 0); 265 BaseId = isl_ast_expr_get_id(BaseExpr); 266 isl_ast_expr_free(BaseExpr); 267 268 const ScopArrayInfo *SAI = ScopArrayInfo::getFromId(BaseId); 269 Base = SAI->getBasePtr(); 270 271 if (auto NewBase = GlobalMap.lookup(Base)) 272 Base = NewBase; 273 274 assert(Base->getType()->isPointerTy() && "Access base should be a pointer"); 275 StringRef BaseName = Base->getName(); 276 277 auto PointerTy = PointerType::get(SAI->getElementType(), 278 Base->getType()->getPointerAddressSpace()); 279 if (Base->getType() != PointerTy) { 280 Base = 281 Builder.CreateBitCast(Base, PointerTy, "polly.access.cast." + BaseName); 282 } 283 284 IndexOp = nullptr; 285 for (unsigned u = 1, e = isl_ast_expr_get_op_n_arg(Expr); u < e; u++) { 286 Value *NextIndex = create(isl_ast_expr_get_op_arg(Expr, u)); 287 assert(NextIndex->getType()->isIntegerTy() && 288 "Access index should be an integer"); 289 290 IndexOp = !IndexOp ? NextIndex : createAdd(IndexOp, NextIndex, 291 "polly.access.add." + BaseName); 292 293 // For every but the last dimension multiply the size, for the last 294 // dimension we can exit the loop. 295 if (u + 1 >= e) 296 break; 297 298 const SCEV *DimSCEV = SAI->getDimensionSize(u); 299 300 llvm::ValueToValueMap Map(GlobalMap.begin(), GlobalMap.end()); 301 DimSCEV = SCEVParameterRewriter::rewrite(DimSCEV, SE, Map); 302 Value *DimSize = 303 expandCodeFor(S, SE, DL, "polly", DimSCEV, DimSCEV->getType(), 304 &*Builder.GetInsertPoint()); 305 306 IndexOp = createMul(IndexOp, DimSize, "polly.access.mul." + BaseName); 307 } 308 309 Access = Builder.CreateGEP(Base, IndexOp, "polly.access." + BaseName); 310 311 isl_ast_expr_free(Expr); 312 return Access; 313 } 314 315 Value *IslExprBuilder::createOpAccess(isl_ast_expr *Expr) { 316 Value *Addr = createAccessAddress(Expr); 317 assert(Addr && "Could not create op access address"); 318 return Builder.CreateLoad(Addr, Addr->getName() + ".load"); 319 } 320 321 Value *IslExprBuilder::createDiv(Value *LHS, Value *RHS, DivisionMode DM) { 322 auto *ConstRHS = dyn_cast<ConstantInt>(RHS); 323 unsigned UnusedBits = 0; 324 Value *Res = nullptr; 325 326 if (ConstRHS) 327 UnusedBits = ConstRHS->getValue().logBase2(); 328 329 if (ConstRHS && ConstRHS->getValue().isPowerOf2() && 330 ConstRHS->getValue().isNonNegative()) 331 Res = Builder.CreateAShr(LHS, UnusedBits, "polly.div.shr"); 332 else if (DM == DM_SIGNED) 333 Res = Builder.CreateSDiv(LHS, RHS, "pexp.div", true); 334 else if (DM == DM_UNSIGNED) 335 Res = Builder.CreateUDiv(LHS, RHS, "pexp.p_div_q"); 336 else { 337 assert(DM == DM_FLOORED); 338 // TODO: Review code and check that this calculation does not yield 339 // incorrect overflow in some bordercases. 340 // 341 // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d 342 Value *Sum1 = createSub(LHS, RHS, "pexp.fdiv_q.0"); 343 Value *One = ConstantInt::get(Sum1->getType(), 1); 344 Value *Sum2 = createAdd(Sum1, One, "pexp.fdiv_q.1"); 345 Value *Zero = ConstantInt::get(LHS->getType(), 0); 346 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero, "pexp.fdiv_q.2"); 347 unifyTypes(LHS, Sum2); 348 Value *Dividend = 349 Builder.CreateSelect(isNegative, Sum2, LHS, "pexp.fdiv_q.3"); 350 unifyTypes(Dividend, RHS); 351 Res = Builder.CreateSDiv(Dividend, RHS, "pexp.fdiv_q.4"); 352 } 353 354 if (UnusedBits) { 355 auto RequiredBits = Res->getType()->getPrimitiveSizeInBits() - UnusedBits; 356 Res = Builder.CreateTrunc(Res, Builder.getIntNTy(RequiredBits), 357 "polly.div.trunc"); 358 } 359 360 return Res; 361 } 362 363 Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) { 364 Value *LHS, *RHS, *Res; 365 isl_ast_op_type OpType; 366 367 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 368 "isl ast expression not of type isl_ast_op"); 369 assert(isl_ast_expr_get_op_n_arg(Expr) == 2 && 370 "not a binary isl ast expression"); 371 372 OpType = isl_ast_expr_get_op_type(Expr); 373 374 LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); 375 RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); 376 377 // For possibly overflowing operations we will later adjust types but 378 // for others we do it now as we will directly create the operations. 379 switch (OpType) { 380 case isl_ast_op_pdiv_q: 381 case isl_ast_op_pdiv_r: 382 case isl_ast_op_div: 383 case isl_ast_op_fdiv_q: 384 case isl_ast_op_zdiv_r: 385 unifyTypes(LHS, RHS); 386 break; 387 case isl_ast_op_add: 388 case isl_ast_op_sub: 389 case isl_ast_op_mul: 390 // Do nothing 391 break; 392 default: 393 llvm_unreachable("This is no binary isl ast expression"); 394 } 395 396 switch (OpType) { 397 default: 398 llvm_unreachable("This is no binary isl ast expression"); 399 case isl_ast_op_add: 400 Res = createAdd(LHS, RHS); 401 break; 402 case isl_ast_op_sub: 403 Res = createSub(LHS, RHS); 404 break; 405 case isl_ast_op_mul: 406 Res = createMul(LHS, RHS); 407 break; 408 case isl_ast_op_div: 409 Res = createDiv(LHS, RHS, DM_SIGNED); 410 break; 411 case isl_ast_op_pdiv_q: // Dividend is non-negative 412 Res = createDiv(LHS, RHS, DM_UNSIGNED); 413 break; 414 case isl_ast_op_fdiv_q: // Round towards -infty 415 Res = createDiv(LHS, RHS, DM_FLOORED); 416 break; 417 case isl_ast_op_pdiv_r: // Dividend is non-negative 418 Res = Builder.CreateURem(LHS, RHS, "pexp.pdiv_r"); 419 break; 420 case isl_ast_op_zdiv_r: // Result only compared against zero 421 Res = Builder.CreateSRem(LHS, RHS, "pexp.zdiv_r"); 422 break; 423 } 424 425 isl_ast_expr_free(Expr); 426 return Res; 427 } 428 429 Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) { 430 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_select && 431 "Unsupported unary isl ast expression"); 432 Value *LHS, *RHS, *Cond; 433 434 Cond = create(isl_ast_expr_get_op_arg(Expr, 0)); 435 if (!Cond->getType()->isIntegerTy(1)) 436 Cond = Builder.CreateIsNotNull(Cond); 437 438 LHS = create(isl_ast_expr_get_op_arg(Expr, 1)); 439 RHS = create(isl_ast_expr_get_op_arg(Expr, 2)); 440 unifyTypes(LHS, RHS); 441 442 isl_ast_expr_free(Expr); 443 return Builder.CreateSelect(Cond, LHS, RHS); 444 } 445 446 Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) { 447 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 448 "Expected an isl_ast_expr_op expression"); 449 450 Value *LHS, *RHS, *Res; 451 452 auto *Op0 = isl_ast_expr_get_op_arg(Expr, 0); 453 auto *Op1 = isl_ast_expr_get_op_arg(Expr, 1); 454 bool HasNonAddressOfOperand = 455 isl_ast_expr_get_type(Op0) != isl_ast_expr_op || 456 isl_ast_expr_get_type(Op1) != isl_ast_expr_op || 457 isl_ast_expr_get_op_type(Op0) != isl_ast_op_address_of || 458 isl_ast_expr_get_op_type(Op1) != isl_ast_op_address_of; 459 460 LHS = create(Op0); 461 RHS = create(Op1); 462 463 auto *LHSTy = LHS->getType(); 464 auto *RHSTy = RHS->getType(); 465 bool IsPtrType = LHSTy->isPointerTy() || RHSTy->isPointerTy(); 466 bool UseUnsignedCmp = IsPtrType && !HasNonAddressOfOperand; 467 468 auto *PtrAsIntTy = Builder.getIntNTy(DL.getPointerSizeInBits()); 469 if (LHSTy->isPointerTy()) 470 LHS = Builder.CreatePtrToInt(LHS, PtrAsIntTy); 471 if (RHSTy->isPointerTy()) 472 RHS = Builder.CreatePtrToInt(RHS, PtrAsIntTy); 473 474 unifyTypes(LHS, RHS); 475 476 isl_ast_op_type OpType = isl_ast_expr_get_op_type(Expr); 477 assert(OpType >= isl_ast_op_eq && OpType <= isl_ast_op_gt && 478 "Unsupported ICmp isl ast expression"); 479 assert(isl_ast_op_eq + 4 == isl_ast_op_gt && 480 "Isl ast op type interface changed"); 481 482 CmpInst::Predicate Predicates[5][2] = { 483 {CmpInst::ICMP_EQ, CmpInst::ICMP_EQ}, 484 {CmpInst::ICMP_SLE, CmpInst::ICMP_ULE}, 485 {CmpInst::ICMP_SLT, CmpInst::ICMP_ULT}, 486 {CmpInst::ICMP_SGE, CmpInst::ICMP_UGE}, 487 {CmpInst::ICMP_SGT, CmpInst::ICMP_UGT}, 488 }; 489 490 Res = Builder.CreateICmp(Predicates[OpType - isl_ast_op_eq][UseUnsignedCmp], 491 LHS, RHS); 492 493 isl_ast_expr_free(Expr); 494 return Res; 495 } 496 497 Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) { 498 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 499 "Expected an isl_ast_expr_op expression"); 500 501 Value *LHS, *RHS, *Res; 502 isl_ast_op_type OpType; 503 504 OpType = isl_ast_expr_get_op_type(Expr); 505 506 assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) && 507 "Unsupported isl_ast_op_type"); 508 509 LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); 510 RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); 511 512 // Even though the isl pretty printer prints the expressions as 'exp && exp' 513 // or 'exp || exp', we actually code generate the bitwise expressions 514 // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches, 515 // but it is, due to the use of i1 types, otherwise equivalent. The reason 516 // to go for bitwise operations is, that we assume the reduced control flow 517 // will outweight the overhead introduced by evaluating unneeded expressions. 518 // The isl code generation currently does not take advantage of the fact that 519 // the expression after an '||' or '&&' is in some cases not evaluated. 520 // Evaluating it anyways does not cause any undefined behaviour. 521 // 522 // TODO: Document in isl itself, that the unconditionally evaluating the 523 // second part of '||' or '&&' expressions is safe. 524 if (!LHS->getType()->isIntegerTy(1)) 525 LHS = Builder.CreateIsNotNull(LHS); 526 if (!RHS->getType()->isIntegerTy(1)) 527 RHS = Builder.CreateIsNotNull(RHS); 528 529 switch (OpType) { 530 default: 531 llvm_unreachable("Unsupported boolean expression"); 532 case isl_ast_op_and: 533 Res = Builder.CreateAnd(LHS, RHS); 534 break; 535 case isl_ast_op_or: 536 Res = Builder.CreateOr(LHS, RHS); 537 break; 538 } 539 540 isl_ast_expr_free(Expr); 541 return Res; 542 } 543 544 Value * 545 IslExprBuilder::createOpBooleanConditional(__isl_take isl_ast_expr *Expr) { 546 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 547 "Expected an isl_ast_expr_op expression"); 548 549 Value *LHS, *RHS; 550 isl_ast_op_type OpType; 551 552 Function *F = Builder.GetInsertBlock()->getParent(); 553 LLVMContext &Context = F->getContext(); 554 555 OpType = isl_ast_expr_get_op_type(Expr); 556 557 assert((OpType == isl_ast_op_and_then || OpType == isl_ast_op_or_else) && 558 "Unsupported isl_ast_op_type"); 559 560 auto InsertBB = Builder.GetInsertBlock(); 561 auto InsertPoint = Builder.GetInsertPoint(); 562 auto NextBB = SplitBlock(InsertBB, &*InsertPoint, &DT, &LI); 563 BasicBlock *CondBB = BasicBlock::Create(Context, "polly.cond", F); 564 LI.changeLoopFor(CondBB, LI.getLoopFor(InsertBB)); 565 DT.addNewBlock(CondBB, InsertBB); 566 567 InsertBB->getTerminator()->eraseFromParent(); 568 Builder.SetInsertPoint(InsertBB); 569 auto BR = Builder.CreateCondBr(Builder.getTrue(), NextBB, CondBB); 570 571 Builder.SetInsertPoint(CondBB); 572 Builder.CreateBr(NextBB); 573 574 Builder.SetInsertPoint(InsertBB->getTerminator()); 575 576 LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); 577 if (!LHS->getType()->isIntegerTy(1)) 578 LHS = Builder.CreateIsNotNull(LHS); 579 auto LeftBB = Builder.GetInsertBlock(); 580 581 if (OpType == isl_ast_op_and || OpType == isl_ast_op_and_then) 582 BR->setCondition(Builder.CreateNeg(LHS)); 583 else 584 BR->setCondition(LHS); 585 586 Builder.SetInsertPoint(CondBB->getTerminator()); 587 RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); 588 if (!RHS->getType()->isIntegerTy(1)) 589 RHS = Builder.CreateIsNotNull(RHS); 590 auto RightBB = Builder.GetInsertBlock(); 591 592 Builder.SetInsertPoint(NextBB->getTerminator()); 593 auto PHI = Builder.CreatePHI(Builder.getInt1Ty(), 2); 594 PHI->addIncoming(OpType == isl_ast_op_and_then ? Builder.getFalse() 595 : Builder.getTrue(), 596 LeftBB); 597 PHI->addIncoming(RHS, RightBB); 598 599 isl_ast_expr_free(Expr); 600 return PHI; 601 } 602 603 Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) { 604 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 605 "Expression not of type isl_ast_expr_op"); 606 switch (isl_ast_expr_get_op_type(Expr)) { 607 case isl_ast_op_error: 608 case isl_ast_op_cond: 609 case isl_ast_op_call: 610 case isl_ast_op_member: 611 llvm_unreachable("Unsupported isl ast expression"); 612 case isl_ast_op_access: 613 return createOpAccess(Expr); 614 case isl_ast_op_max: 615 case isl_ast_op_min: 616 return createOpNAry(Expr); 617 case isl_ast_op_add: 618 case isl_ast_op_sub: 619 case isl_ast_op_mul: 620 case isl_ast_op_div: 621 case isl_ast_op_fdiv_q: // Round towards -infty 622 case isl_ast_op_pdiv_q: // Dividend is non-negative 623 case isl_ast_op_pdiv_r: // Dividend is non-negative 624 case isl_ast_op_zdiv_r: // Result only compared against zero 625 return createOpBin(Expr); 626 case isl_ast_op_minus: 627 return createOpUnary(Expr); 628 case isl_ast_op_select: 629 return createOpSelect(Expr); 630 case isl_ast_op_and: 631 case isl_ast_op_or: 632 return createOpBoolean(Expr); 633 case isl_ast_op_and_then: 634 case isl_ast_op_or_else: 635 return createOpBooleanConditional(Expr); 636 case isl_ast_op_eq: 637 case isl_ast_op_le: 638 case isl_ast_op_lt: 639 case isl_ast_op_ge: 640 case isl_ast_op_gt: 641 return createOpICmp(Expr); 642 case isl_ast_op_address_of: 643 return createOpAddressOf(Expr); 644 } 645 646 llvm_unreachable("Unsupported isl_ast_expr_op kind."); 647 } 648 649 Value *IslExprBuilder::createOpAddressOf(__isl_take isl_ast_expr *Expr) { 650 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && 651 "Expected an isl_ast_expr_op expression."); 652 assert(isl_ast_expr_get_op_n_arg(Expr) == 1 && "Address of should be unary."); 653 654 isl_ast_expr *Op = isl_ast_expr_get_op_arg(Expr, 0); 655 assert(isl_ast_expr_get_type(Op) == isl_ast_expr_op && 656 "Expected address of operator to be an isl_ast_expr_op expression."); 657 assert(isl_ast_expr_get_op_type(Op) == isl_ast_op_access && 658 "Expected address of operator to be an access expression."); 659 660 Value *V = createAccessAddress(Op); 661 662 isl_ast_expr_free(Expr); 663 664 return V; 665 } 666 667 Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) { 668 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id && 669 "Expression not of type isl_ast_expr_ident"); 670 671 isl_id *Id; 672 Value *V; 673 674 Id = isl_ast_expr_get_id(Expr); 675 676 assert(IDToValue.count(Id) && "Identifier not found"); 677 678 V = IDToValue[Id]; 679 if (!V) 680 V = UndefValue::get(Builder.getInt1Ty()); 681 682 if (V->getType()->isPointerTy()) 683 V = Builder.CreatePtrToInt(V, Builder.getIntNTy(DL.getPointerSizeInBits())); 684 685 assert(V && "Unknown parameter id found"); 686 687 isl_id_free(Id); 688 isl_ast_expr_free(Expr); 689 690 return V; 691 } 692 693 Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) { 694 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int && 695 "Expression not of type isl_ast_expr_int"); 696 697 auto *Val = isl_ast_expr_get_val(Expr); 698 auto *V = ConstantInt::get(Builder.getContext(), APIntFromVal(Val)); 699 700 isl_ast_expr_free(Expr); 701 return V; 702 } 703 704 Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) { 705 switch (isl_ast_expr_get_type(Expr)) { 706 case isl_ast_expr_error: 707 llvm_unreachable("Code generation error"); 708 case isl_ast_expr_op: 709 return createOp(Expr); 710 case isl_ast_expr_id: 711 return createId(Expr); 712 case isl_ast_expr_int: 713 return createInt(Expr); 714 } 715 716 llvm_unreachable("Unexpected enum value"); 717 } 718