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