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