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_expr *LOp, *ROp;
190   isl_ast_op_type OpType;
191 
192   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
193          "isl ast expression not of type isl_ast_op");
194   assert(isl_ast_expr_get_op_n_arg(Expr) == 2 &&
195          "not a binary isl ast expression");
196 
197   OpType = isl_ast_expr_get_op_type(Expr);
198 
199   LOp = isl_ast_expr_get_op_arg(Expr, 0);
200   ROp = isl_ast_expr_get_op_arg(Expr, 1);
201 
202   // Catch the special case ((-<pointer>) + <pointer>) which is for
203   // isl the same as (<pointer> - <pointer>). We have to treat it here because
204   // there is no valid semantics for the (-<pointer>) expression, hence in
205   // createOpUnary such an expression will trigger a crash.
206   // FIXME: The same problem can now be triggered by a subexpression of the LHS,
207   //        however it is much less likely.
208   if (OpType == isl_ast_op_add &&
209       isl_ast_expr_get_type(LOp) == isl_ast_expr_op &&
210       isl_ast_expr_get_op_type(LOp) == isl_ast_op_minus) {
211     // Change the binary addition to a substraction.
212     OpType = isl_ast_op_sub;
213 
214     // Extract the unary operand of the LHS.
215     auto *LOpOp = isl_ast_expr_get_op_arg(LOp, 0);
216     isl_ast_expr_free(LOp);
217 
218     // Swap the unary operand of the LHS and the RHS.
219     LOp = ROp;
220     ROp = LOpOp;
221   }
222 
223   LHS = create(LOp);
224   RHS = create(ROp);
225 
226   Type *LHSType = LHS->getType();
227   Type *RHSType = RHS->getType();
228 
229   // Handle <pointer> - <pointer>
230   if (LHSType->isPointerTy() && RHSType->isPointerTy()) {
231     isl_ast_expr_free(Expr);
232     assert(OpType == isl_ast_op_sub && "Substraction is the only valid binary "
233                                        "pointer <-> pointer operation.");
234 
235     return Builder.CreatePtrDiff(LHS, RHS);
236   }
237 
238   // Handle <pointer> +/- <integer> and <integer> +/- <pointer>
239   if (LHSType->isPointerTy() || RHSType->isPointerTy()) {
240     isl_ast_expr_free(Expr);
241 
242     assert((LHSType->isIntegerTy() || RHSType->isIntegerTy()) &&
243            "Arithmetic operations might only performed on one but not two "
244            "pointer types.");
245 
246     if (LHSType->isIntegerTy())
247       std::swap(LHS, RHS);
248 
249     switch (OpType) {
250     default:
251       llvm_unreachable(
252           "Only additive binary operations are allowed on pointer types.");
253     case isl_ast_op_sub:
254       RHS = Builder.CreateNeg(RHS);
255     // Fall through
256     case isl_ast_op_add:
257       return Builder.CreateGEP(LHS, RHS);
258     }
259   }
260 
261   MaxType = getWidestType(LHSType, RHSType);
262 
263   // Take the result into account when calculating the widest type.
264   //
265   // For operations such as '+' the result may require a type larger than
266   // the type of the individual operands. For other operations such as '/', the
267   // result type cannot be larger than the type of the individual operand. isl
268   // does not calculate correct types for these operations and we consequently
269   // exclude those operations here.
270   switch (OpType) {
271   case isl_ast_op_pdiv_q:
272   case isl_ast_op_pdiv_r:
273   case isl_ast_op_div:
274   case isl_ast_op_fdiv_q:
275   case isl_ast_op_zdiv_r:
276     // Do nothing
277     break;
278   case isl_ast_op_add:
279   case isl_ast_op_sub:
280   case isl_ast_op_mul:
281     MaxType = getWidestType(MaxType, getType(Expr));
282     break;
283   default:
284     llvm_unreachable("This is no binary isl ast expression");
285   }
286 
287   if (MaxType != RHS->getType())
288     RHS = Builder.CreateSExt(RHS, MaxType);
289 
290   if (MaxType != LHS->getType())
291     LHS = Builder.CreateSExt(LHS, MaxType);
292 
293   switch (OpType) {
294   default:
295     llvm_unreachable("This is no binary isl ast expression");
296   case isl_ast_op_add:
297     Res = Builder.CreateNSWAdd(LHS, RHS);
298     break;
299   case isl_ast_op_sub:
300     Res = Builder.CreateNSWSub(LHS, RHS);
301     break;
302   case isl_ast_op_mul:
303     Res = Builder.CreateNSWMul(LHS, RHS);
304     break;
305   case isl_ast_op_div:
306     Res = Builder.CreateSDiv(LHS, RHS, "pexp.div", true);
307     break;
308   case isl_ast_op_pdiv_q: // Dividend is non-negative
309     Res = Builder.CreateUDiv(LHS, RHS, "pexp.p_div_q");
310     break;
311   case isl_ast_op_fdiv_q: { // Round towards -infty
312     if (auto *Const = dyn_cast<ConstantInt>(RHS)) {
313       auto &Val = Const->getValue();
314       if (Val.isPowerOf2() && Val.isNonNegative()) {
315         Res = Builder.CreateAShr(LHS, Val.ceilLogBase2(), "polly.fdiv_q.shr");
316         break;
317       }
318     }
319     // TODO: Review code and check that this calculation does not yield
320     //       incorrect overflow in some bordercases.
321     //
322     // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
323     Value *One = ConstantInt::get(MaxType, 1);
324     Value *Zero = ConstantInt::get(MaxType, 0);
325     Value *Sum1 = Builder.CreateSub(LHS, RHS, "pexp.fdiv_q.0");
326     Value *Sum2 = Builder.CreateAdd(Sum1, One, "pexp.fdiv_q.1");
327     Value *isNegative = Builder.CreateICmpSLT(LHS, Zero, "pexp.fdiv_q.2");
328     Value *Dividend =
329         Builder.CreateSelect(isNegative, Sum2, LHS, "pexp.fdiv_q.3");
330     Res = Builder.CreateSDiv(Dividend, RHS, "pexp.fdiv_q.4");
331     break;
332   }
333   case isl_ast_op_pdiv_r: // Dividend is non-negative
334     Res = Builder.CreateURem(LHS, RHS, "pexp.pdiv_r");
335     break;
336 
337   case isl_ast_op_zdiv_r: // Result only compared against zero
338     Res = Builder.CreateURem(LHS, RHS, "pexp.zdiv_r");
339     break;
340   }
341 
342   // TODO: We can truncate the result, if it fits into a smaller type. This can
343   // help in cases where we have larger operands (e.g. i67) but the result is
344   // known to fit into i64. Without the truncation, the larger i67 type may
345   // force all subsequent operations to be performed on a non-native type.
346   isl_ast_expr_free(Expr);
347   return Res;
348 }
349 
350 Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) {
351   assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_select &&
352          "Unsupported unary isl ast expression");
353   Value *LHS, *RHS, *Cond;
354   Type *MaxType = getType(Expr);
355 
356   Cond = create(isl_ast_expr_get_op_arg(Expr, 0));
357   if (!Cond->getType()->isIntegerTy(1))
358     Cond = Builder.CreateIsNotNull(Cond);
359 
360   LHS = create(isl_ast_expr_get_op_arg(Expr, 1));
361   RHS = create(isl_ast_expr_get_op_arg(Expr, 2));
362 
363   MaxType = getWidestType(MaxType, LHS->getType());
364   MaxType = getWidestType(MaxType, RHS->getType());
365 
366   if (MaxType != RHS->getType())
367     RHS = Builder.CreateSExt(RHS, MaxType);
368 
369   if (MaxType != LHS->getType())
370     LHS = Builder.CreateSExt(LHS, MaxType);
371 
372   // TODO: Do we want to truncate the result?
373   isl_ast_expr_free(Expr);
374   return Builder.CreateSelect(Cond, LHS, RHS);
375 }
376 
377 Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
378   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
379          "Expected an isl_ast_expr_op expression");
380 
381   Value *LHS, *RHS, *Res;
382 
383   LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
384   RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
385 
386   bool IsPtrType =
387       LHS->getType()->isPointerTy() || RHS->getType()->isPointerTy();
388 
389   if (LHS->getType() != RHS->getType()) {
390     if (IsPtrType) {
391       Type *I8PtrTy = Builder.getInt8PtrTy();
392       if (!LHS->getType()->isPointerTy())
393         LHS = Builder.CreateIntToPtr(LHS, I8PtrTy);
394       if (!RHS->getType()->isPointerTy())
395         RHS = Builder.CreateIntToPtr(RHS, I8PtrTy);
396       if (LHS->getType() != I8PtrTy)
397         LHS = Builder.CreateBitCast(LHS, I8PtrTy);
398       if (RHS->getType() != I8PtrTy)
399         RHS = Builder.CreateBitCast(RHS, I8PtrTy);
400     } else {
401       Type *MaxType = LHS->getType();
402       MaxType = getWidestType(MaxType, RHS->getType());
403 
404       if (MaxType != RHS->getType())
405         RHS = Builder.CreateSExt(RHS, MaxType);
406 
407       if (MaxType != LHS->getType())
408         LHS = Builder.CreateSExt(LHS, MaxType);
409     }
410   }
411 
412   isl_ast_op_type OpType = isl_ast_expr_get_op_type(Expr);
413   assert(OpType >= isl_ast_op_eq && OpType <= isl_ast_op_gt &&
414          "Unsupported ICmp isl ast expression");
415   assert(isl_ast_op_eq + 4 == isl_ast_op_gt &&
416          "Isl ast op type interface changed");
417 
418   CmpInst::Predicate Predicates[5][2] = {
419       {CmpInst::ICMP_EQ, CmpInst::ICMP_EQ},
420       {CmpInst::ICMP_SLE, CmpInst::ICMP_ULE},
421       {CmpInst::ICMP_SLT, CmpInst::ICMP_ULT},
422       {CmpInst::ICMP_SGE, CmpInst::ICMP_UGE},
423       {CmpInst::ICMP_SGT, CmpInst::ICMP_UGT},
424   };
425 
426   Res = Builder.CreateICmp(Predicates[OpType - isl_ast_op_eq][IsPtrType], LHS,
427                            RHS);
428 
429   isl_ast_expr_free(Expr);
430   return Res;
431 }
432 
433 Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) {
434   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
435          "Expected an isl_ast_expr_op expression");
436 
437   Value *LHS, *RHS, *Res;
438   isl_ast_op_type OpType;
439 
440   OpType = isl_ast_expr_get_op_type(Expr);
441 
442   assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) &&
443          "Unsupported isl_ast_op_type");
444 
445   LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
446   RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
447 
448   // Even though the isl pretty printer prints the expressions as 'exp && exp'
449   // or 'exp || exp', we actually code generate the bitwise expressions
450   // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches,
451   // but it is, due to the use of i1 types, otherwise equivalent. The reason
452   // to go for bitwise operations is, that we assume the reduced control flow
453   // will outweight the overhead introduced by evaluating unneeded expressions.
454   // The isl code generation currently does not take advantage of the fact that
455   // the expression after an '||' or '&&' is in some cases not evaluated.
456   // Evaluating it anyways does not cause any undefined behaviour.
457   //
458   // TODO: Document in isl itself, that the unconditionally evaluating the
459   // second part of '||' or '&&' expressions is safe.
460   if (!LHS->getType()->isIntegerTy(1))
461     LHS = Builder.CreateIsNotNull(LHS);
462   if (!RHS->getType()->isIntegerTy(1))
463     RHS = Builder.CreateIsNotNull(RHS);
464 
465   switch (OpType) {
466   default:
467     llvm_unreachable("Unsupported boolean expression");
468   case isl_ast_op_and:
469     Res = Builder.CreateAnd(LHS, RHS);
470     break;
471   case isl_ast_op_or:
472     Res = Builder.CreateOr(LHS, RHS);
473     break;
474   }
475 
476   isl_ast_expr_free(Expr);
477   return Res;
478 }
479 
480 Value *
481 IslExprBuilder::createOpBooleanConditional(__isl_take isl_ast_expr *Expr) {
482   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
483          "Expected an isl_ast_expr_op expression");
484 
485   Value *LHS, *RHS;
486   isl_ast_op_type OpType;
487 
488   Function *F = Builder.GetInsertBlock()->getParent();
489   LLVMContext &Context = F->getContext();
490 
491   OpType = isl_ast_expr_get_op_type(Expr);
492 
493   assert((OpType == isl_ast_op_and_then || OpType == isl_ast_op_or_else) &&
494          "Unsupported isl_ast_op_type");
495 
496   auto InsertBB = Builder.GetInsertBlock();
497   auto InsertPoint = Builder.GetInsertPoint();
498   auto NextBB = SplitBlock(InsertBB, &*InsertPoint, &DT, &LI);
499   BasicBlock *CondBB = BasicBlock::Create(Context, "polly.cond", F);
500   LI.changeLoopFor(CondBB, LI.getLoopFor(InsertBB));
501   DT.addNewBlock(CondBB, InsertBB);
502 
503   InsertBB->getTerminator()->eraseFromParent();
504   Builder.SetInsertPoint(InsertBB);
505   auto BR = Builder.CreateCondBr(Builder.getTrue(), NextBB, CondBB);
506 
507   Builder.SetInsertPoint(CondBB);
508   Builder.CreateBr(NextBB);
509 
510   Builder.SetInsertPoint(InsertBB->getTerminator());
511 
512   LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
513   if (!LHS->getType()->isIntegerTy(1))
514     LHS = Builder.CreateIsNotNull(LHS);
515   auto LeftBB = Builder.GetInsertBlock();
516 
517   if (OpType == isl_ast_op_and || OpType == isl_ast_op_and_then)
518     BR->setCondition(Builder.CreateNeg(LHS));
519   else
520     BR->setCondition(LHS);
521 
522   Builder.SetInsertPoint(CondBB->getTerminator());
523   RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
524   if (!RHS->getType()->isIntegerTy(1))
525     RHS = Builder.CreateIsNotNull(RHS);
526   auto RightBB = Builder.GetInsertBlock();
527 
528   Builder.SetInsertPoint(NextBB->getTerminator());
529   auto PHI = Builder.CreatePHI(Builder.getInt1Ty(), 2);
530   PHI->addIncoming(OpType == isl_ast_op_and_then ? Builder.getFalse()
531                                                  : Builder.getTrue(),
532                    LeftBB);
533   PHI->addIncoming(RHS, RightBB);
534 
535   isl_ast_expr_free(Expr);
536   return PHI;
537 }
538 
539 Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) {
540   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
541          "Expression not of type isl_ast_expr_op");
542   switch (isl_ast_expr_get_op_type(Expr)) {
543   case isl_ast_op_error:
544   case isl_ast_op_cond:
545   case isl_ast_op_call:
546   case isl_ast_op_member:
547     llvm_unreachable("Unsupported isl ast expression");
548   case isl_ast_op_access:
549     return createOpAccess(Expr);
550   case isl_ast_op_max:
551   case isl_ast_op_min:
552     return createOpNAry(Expr);
553   case isl_ast_op_add:
554   case isl_ast_op_sub:
555   case isl_ast_op_mul:
556   case isl_ast_op_div:
557   case isl_ast_op_fdiv_q: // Round towards -infty
558   case isl_ast_op_pdiv_q: // Dividend is non-negative
559   case isl_ast_op_pdiv_r: // Dividend is non-negative
560   case isl_ast_op_zdiv_r: // Result only compared against zero
561     return createOpBin(Expr);
562   case isl_ast_op_minus:
563     return createOpUnary(Expr);
564   case isl_ast_op_select:
565     return createOpSelect(Expr);
566   case isl_ast_op_and:
567   case isl_ast_op_or:
568     return createOpBoolean(Expr);
569   case isl_ast_op_and_then:
570   case isl_ast_op_or_else:
571     return createOpBooleanConditional(Expr);
572   case isl_ast_op_eq:
573   case isl_ast_op_le:
574   case isl_ast_op_lt:
575   case isl_ast_op_ge:
576   case isl_ast_op_gt:
577     return createOpICmp(Expr);
578   case isl_ast_op_address_of:
579     return createOpAddressOf(Expr);
580   }
581 
582   llvm_unreachable("Unsupported isl_ast_expr_op kind.");
583 }
584 
585 Value *IslExprBuilder::createOpAddressOf(__isl_take isl_ast_expr *Expr) {
586   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
587          "Expected an isl_ast_expr_op expression.");
588   assert(isl_ast_expr_get_op_n_arg(Expr) == 1 && "Address of should be unary.");
589 
590   isl_ast_expr *Op = isl_ast_expr_get_op_arg(Expr, 0);
591   assert(isl_ast_expr_get_type(Op) == isl_ast_expr_op &&
592          "Expected address of operator to be an isl_ast_expr_op expression.");
593   assert(isl_ast_expr_get_op_type(Op) == isl_ast_op_access &&
594          "Expected address of operator to be an access expression.");
595 
596   Value *V = createAccessAddress(Op);
597 
598   isl_ast_expr_free(Expr);
599 
600   return V;
601 }
602 
603 Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) {
604   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id &&
605          "Expression not of type isl_ast_expr_ident");
606 
607   isl_id *Id;
608   Value *V;
609 
610   Id = isl_ast_expr_get_id(Expr);
611 
612   assert(IDToValue.count(Id) && "Identifier not found");
613 
614   V = IDToValue[Id];
615   if (!V)
616     V = UndefValue::get(getType(Expr));
617 
618   assert(V && "Unknown parameter id found");
619 
620   isl_id_free(Id);
621   isl_ast_expr_free(Expr);
622 
623   return V;
624 }
625 
626 IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) {
627   // XXX: We assume i64 is large enough. This is often true, but in general
628   //      incorrect. Also, on 32bit architectures, it would be beneficial to
629   //      use a smaller type. We can and should directly derive this information
630   //      during code generation.
631   return IntegerType::get(Builder.getContext(), 64);
632 }
633 
634 Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) {
635   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int &&
636          "Expression not of type isl_ast_expr_int");
637   isl_val *Val;
638   Value *V;
639   APInt APValue;
640   IntegerType *T;
641 
642   Val = isl_ast_expr_get_val(Expr);
643   APValue = APIntFromVal(Val);
644 
645   auto BitWidth = APValue.getBitWidth();
646   if (BitWidth <= 64)
647     T = getType(Expr);
648   else
649     T = Builder.getIntNTy(BitWidth);
650 
651   APValue = APValue.sextOrSelf(T->getBitWidth());
652   V = ConstantInt::get(T, APValue);
653 
654   isl_ast_expr_free(Expr);
655   return V;
656 }
657 
658 Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) {
659   switch (isl_ast_expr_get_type(Expr)) {
660   case isl_ast_expr_error:
661     llvm_unreachable("Code generation error");
662   case isl_ast_expr_op:
663     return createOp(Expr);
664   case isl_ast_expr_id:
665     return createId(Expr);
666   case isl_ast_expr_int:
667     return createInt(Expr);
668   }
669 
670   llvm_unreachable("Unexpected enum value");
671 }
672