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