IslExprBuilder.cpp 26 KB

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