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