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