blob: e0856eacafc78235b551b15d98eee26f2b7ed07f [file] [log] [blame]
Sebastian Pop082cea82012-05-07 16:20:07 +00001//===------ IslCodeGeneration.cpp - Code generate the Scops using ISL. ----===//
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// The IslCodeGeneration pass takes a Scop created by ScopInfo and translates it
11// back to LLVM-IR using the ISL code generator.
12//
13// The Scop describes the high level memory behaviour of a control flow region.
14// Transformation passes can update the schedule (execution order) of statements
15// in the Scop. ISL is used to generate an abstract syntax tree that reflects
16// the updated execution order. This clast is used to create new LLVM-IR that is
17// computationally equivalent to the original control flow region, but executes
18// its code in the new execution order defined by the changed scattering.
19//
20//===----------------------------------------------------------------------===//
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000021#include "polly/Config/config.h"
Sebastian Pop082cea82012-05-07 16:20:07 +000022
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000023#ifdef ISL_CODEGEN_FOUND
24
25#include "polly/Dependences.h"
26#include "polly/LinkAllPasses.h"
27#include "polly/ScopInfo.h"
28#include "polly/TempScopInfo.h"
29#include "polly/CodeGen/IslAst.h"
30#include "polly/CodeGen/BlockGenerators.h"
31#include "polly/CodeGen/LoopGenerators.h"
32#include "polly/CodeGen/Utils.h"
33#include "polly/Support/GICHelper.h"
34
35#include "llvm/Module.h"
36#include "llvm/Analysis/LoopInfo.h"
37#include "llvm/Analysis/ScalarEvolutionExpander.h"
38#define DEBUG_TYPE "polly-codegen-isl"
39#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/Debug.h"
Micah Villmow7a3d8202012-10-08 17:26:19 +000041#include "llvm/DataLayout.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000042#include "llvm/Transforms/Utils/BasicBlockUtils.h"
43
44#include "isl/union_map.h"
45#include "isl/list.h"
46#include "isl/ast.h"
47#include "isl/ast_build.h"
48#include "isl/set.h"
49#include "isl/map.h"
50#include "isl/aff.h"
51
52#include <map>
53
54using namespace polly;
55using namespace llvm;
56
57/// @brief Insert function calls that print certain LLVM values at run time.
58///
59/// This class inserts libc function calls to print certain LLVM values at
60/// run time.
61class RuntimeDebugBuilder {
62public:
63 RuntimeDebugBuilder(IRBuilder<> &Builder) : Builder(Builder) {}
64
65 /// @brief Print a string to stdout.
66 ///
67 /// @param String The string to print.
68 void createStrPrinter(std::string String);
69
70 /// @brief Print an integer value to stdout.
71 ///
72 /// @param V The value to print.
73 void createIntPrinter(Value *V);
74
75private:
76 IRBuilder<> &Builder;
77
78 /// @brief Add a call to the fflush function with no file pointer given.
79 ///
80 /// This call will flush all opened file pointers including stdout and stderr.
81 void createFlush();
82
83 /// @brief Get a reference to the 'printf' function.
84 ///
85 /// If the current module does not yet contain a reference to printf, we
86 /// insert a reference to it. Otherwise the existing reference is returned.
87 Function *getPrintF();
88};
89
90Function *RuntimeDebugBuilder::getPrintF() {
91 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
92 const char *Name = "printf";
93 Function *F = M->getFunction(Name);
94
95 if (!F) {
96 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
97 FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(),
98 Builder.getInt8PtrTy(), true);
99 F = Function::Create(Ty, Linkage, Name, M);
100 }
101
102 return F;
103}
104
105void RuntimeDebugBuilder::createFlush() {
106 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
107 const char *Name = "fflush";
108 Function *F = M->getFunction(Name);
109
110 if (!F) {
111 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
112 FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(),
113 Builder.getInt8PtrTy(), false);
114 F = Function::Create(Ty, Linkage, Name, M);
115 }
116
117 Builder.CreateCall(F, Constant::getNullValue(Builder.getInt8PtrTy()));
118}
119
120void RuntimeDebugBuilder::createStrPrinter(std::string String) {
121 Function *F = getPrintF();
122 Value *StringValue = Builder.CreateGlobalStringPtr(String);
123 Builder.CreateCall(F, StringValue);
124
125 createFlush();
126}
127
128void RuntimeDebugBuilder::createIntPrinter(Value *V) {
129 IntegerType *Ty = dyn_cast<IntegerType>(V->getType());
130 assert(Ty && Ty->getBitWidth() == 64 &&
131 "Cannot insert printer for this type.");
132
133 Function *F = getPrintF();
134 Value *String = Builder.CreateGlobalStringPtr("%ld");
135 Builder.CreateCall2(F, String, V);
136 createFlush();
137}
138
139/// @brief Calculate the Value of a certain isl_ast_expr
140class IslExprBuilder {
141public:
142 IslExprBuilder(IRBuilder<> &Builder,
143 std::map<isl_id *, Value*> &IDToValue, Pass *P)
144 : Builder(Builder), IDToValue(IDToValue) { }
145
146 Value *create(__isl_take isl_ast_expr *Expr);
147 Type *getWidestType(Type *T1, Type *T2);
148 IntegerType *getType(__isl_keep isl_ast_expr *Expr);
149
150private:
151 IRBuilder<> &Builder;
152 std::map<isl_id *, Value*> &IDToValue;
153
154 Value *createOp(__isl_take isl_ast_expr *Expr);
155 Value *createOpUnary(__isl_take isl_ast_expr *Expr);
156 Value *createOpBin(__isl_take isl_ast_expr *Expr);
157 Value *createOpNAry(__isl_take isl_ast_expr *Expr);
158 Value *createOpSelect(__isl_take isl_ast_expr *Expr);
159 Value *createOpICmp(__isl_take isl_ast_expr *Expr);
160 Value *createOpBoolean(__isl_take isl_ast_expr *Expr);
161 Value *createId(__isl_take isl_ast_expr *Expr);
162 Value *createInt(__isl_take isl_ast_expr *Expr);
163};
164
165Type *IslExprBuilder::getWidestType(Type *T1, Type *T2) {
166 assert(isa<IntegerType>(T1) && isa<IntegerType>(T2));
167
168 if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits())
169 return T2;
170 else
171 return T1;
172}
173
174Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) {
175 assert (isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus
176 && "Unsupported unary operation");
177
178 Value *V;
179 Type *MaxType = getType(Expr);
180
181 V = create(isl_ast_expr_get_op_arg(Expr, 0));
182 MaxType = getWidestType(MaxType, V->getType());
183
184 if (MaxType != V->getType())
185 V = Builder.CreateSExt(V, MaxType);
186
187 isl_ast_expr_free(Expr);
188 return Builder.CreateNSWNeg(V);
189}
190
191Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) {
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 && "We need at least two operands in an n-ary operation");
196
197 Value *V;
198
199 V = create(isl_ast_expr_get_op_arg(Expr, 0));
200
201 for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr); ++i) {
202 Value *OpV;
203 OpV = create(isl_ast_expr_get_op_arg(Expr, i));
204
205 Type *Ty = getWidestType(V->getType(), OpV->getType());
206
207 if (Ty != OpV->getType())
208 OpV = Builder.CreateSExt(OpV, Ty);
209
210 if (Ty != V->getType())
211 V = Builder.CreateSExt(V, Ty);
212
213 switch (isl_ast_expr_get_op_type(Expr)) {
214 default:
215 llvm_unreachable("This is no n-ary isl ast expression");
216
217 case isl_ast_op_max:
218 {
219 Value *Cmp = Builder.CreateICmpSGT(V, OpV);
220 V = Builder.CreateSelect(Cmp, V, OpV);
221 continue;
222 }
223 case isl_ast_op_min:
224 {
225 Value *Cmp = Builder.CreateICmpSLT(V, OpV);
226 V = Builder.CreateSelect(Cmp, V, OpV);
227 continue;
228 }
229 }
230 }
231
232 // TODO: We can truncate the result, if it fits into a smaller type. This can
233 // help in cases where we have larger operands (e.g. i67) but the result is
234 // known to fit into i64. Without the truncation, the larger i67 type may
235 // force all subsequent operations to be performed on a non-native type.
236 isl_ast_expr_free(Expr);
237 return V;
238}
239
240Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) {
241 Value *LHS, *RHS, *Res;
242 Type *MaxType;
243 isl_ast_op_type OpType;
244
245 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op
246 && "isl ast expression not of type isl_ast_op");
247 assert(isl_ast_expr_get_op_n_arg(Expr) == 2
248 && "not a binary isl ast expression");
249
250 OpType = isl_ast_expr_get_op_type(Expr);
251
252 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
253 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
254
255 MaxType = LHS->getType();
256 MaxType = getWidestType(MaxType, RHS->getType());
257
258 // Take the result into account when calculating the widest type.
259 //
260 // For operations such as '+' the result may require a type larger than
261 // the type of the individual operands. For other operations such as '/', the
262 // result type cannot be larger than the type of the individual operand. isl
263 // does not calculate correct types for these operations and we consequently
264 // exclude those operations here.
265 switch(OpType) {
266 case isl_ast_op_pdiv_q:
267 case isl_ast_op_pdiv_r:
268 case isl_ast_op_div:
269 case isl_ast_op_fdiv_q:
270 // Do nothing
271 break;
272 case isl_ast_op_add:
273 case isl_ast_op_sub:
274 case isl_ast_op_mul:
275 MaxType = getWidestType(MaxType, getType(Expr));
276 break;
277 default:
278 llvm_unreachable("This is no binary isl ast expression");
279 }
280
281 if (MaxType != RHS->getType())
282 RHS = Builder.CreateSExt(RHS, MaxType);
283
284 if (MaxType != LHS->getType())
285 LHS = Builder.CreateSExt(LHS, MaxType);
286
287 switch (OpType) {
288 default:
289 llvm_unreachable("This is no binary isl ast expression");
290 case isl_ast_op_add:
291 Res = Builder.CreateNSWAdd(LHS, RHS);
292 break;
293 case isl_ast_op_sub:
294 Res = Builder.CreateNSWSub(LHS, RHS);
295 break;
296 case isl_ast_op_mul:
297 Res = Builder.CreateNSWMul(LHS, RHS);
298 break;
299 case isl_ast_op_div:
300 case isl_ast_op_pdiv_q: // Dividend is non-negative
301 Res = Builder.CreateSDiv(LHS, RHS);
302 break;
303 case isl_ast_op_fdiv_q: // Round towards -infty
304 {
305 // TODO: Review code and check that this calculation does not yield
306 // incorrect overflow in some bordercases.
307 //
308 // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
309 Value *One = ConstantInt::get(MaxType, 1);
310 Value *Zero = ConstantInt::get(MaxType, 0);
311 Value *Sum1 = Builder.CreateSub(LHS, RHS);
312 Value *Sum2 = Builder.CreateAdd(Sum1, One);
313 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
314 Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS);
315 Res = Builder.CreateSDiv(Dividend, RHS);
316 break;
317 }
318 case isl_ast_op_pdiv_r: // Dividend is non-negative
319 Res = Builder.CreateSRem(LHS, RHS);
320 break;
321 }
322
323 // TODO: We can truncate the result, if it fits into a smaller type. This can
324 // help in cases where we have larger operands (e.g. i67) but the result is
325 // known to fit into i64. Without the truncation, the larger i67 type may
326 // force all subsequent operations to be performed on a non-native type.
327 isl_ast_expr_free(Expr);
328 return Res;
329}
330
331Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) {
332 assert (isl_ast_expr_get_op_type(Expr) == isl_ast_op_select
333 && "Unsupported unary isl ast expression");
334 Value *LHS, *RHS, *Cond;
335 Type *MaxType = getType(Expr);
336
337 Cond = create(isl_ast_expr_get_op_arg(Expr, 0));
338
339 LHS = create(isl_ast_expr_get_op_arg(Expr, 1));
340 RHS = create(isl_ast_expr_get_op_arg(Expr, 2));
341
342 MaxType = getWidestType(MaxType, LHS->getType());
343 MaxType = getWidestType(MaxType, RHS->getType());
344
345 if (MaxType != RHS->getType())
346 RHS = Builder.CreateSExt(RHS, MaxType);
347
348 if (MaxType != LHS->getType())
349 LHS = Builder.CreateSExt(LHS, MaxType);
350
351 // TODO: Do we want to truncate the result?
352 isl_ast_expr_free(Expr);
353 return Builder.CreateSelect(Cond, LHS, RHS);
354}
355
356Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
357 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
358 "Expected an isl_ast_expr_op expression");
359
360 Value *LHS, *RHS, *Res;
361
362 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
363 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
364
365 Type *MaxType = LHS->getType();
366 MaxType = getWidestType(MaxType, RHS->getType());
367
368 if (MaxType != RHS->getType())
369 RHS = Builder.CreateSExt(RHS, MaxType);
370
371 if (MaxType != LHS->getType())
372 LHS = Builder.CreateSExt(LHS, MaxType);
373
374 switch (isl_ast_expr_get_op_type(Expr)) {
375 default:
376 llvm_unreachable("Unsupported ICmp isl ast expression");
377 case isl_ast_op_eq:
378 Res = Builder.CreateICmpEQ(LHS, RHS);
379 break;
380 case isl_ast_op_le:
381 Res = Builder.CreateICmpSLE(LHS, RHS);
382 break;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000383 case isl_ast_op_lt:
384 Res = Builder.CreateICmpSLT(LHS, RHS);
385 break;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000386 case isl_ast_op_ge:
387 Res = Builder.CreateICmpSGE(LHS, RHS);
388 break;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000389 case isl_ast_op_gt:
390 Res = Builder.CreateICmpSGT(LHS, RHS);
391 break;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000392 }
393
394 isl_ast_expr_free(Expr);
395 return Res;
396}
397
398Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) {
399 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
400 "Expected an isl_ast_expr_op expression");
401
402 Value *LHS, *RHS, *Res;
403 isl_ast_op_type OpType;
404
405 OpType = isl_ast_expr_get_op_type(Expr);
406
407 assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) &&
408 "Unsupported isl_ast_op_type");
409
410 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
411 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
412
413 // Even though the isl pretty printer prints the expressions as 'exp && exp'
414 // or 'exp || exp', we actually code generate the bitwise expressions
415 // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches,
416 // but it is, due to the use of i1 types, otherwise equivalent. The reason
417 // to go for bitwise operations is, that we assume the reduced control flow
418 // will outweight the overhead introduced by evaluating unneeded expressions.
419 // The isl code generation currently does not take advantage of the fact that
420 // the expression after an '||' or '&&' is in some cases not evaluated.
421 // Evaluating it anyways does not cause any undefined behaviour.
422 //
423 // TODO: Document in isl itself, that the unconditionally evaluating the
424 // second part of '||' or '&&' expressions is safe.
425 assert(LHS->getType() == Builder.getInt1Ty() && "Expected i1 type");
426 assert(RHS->getType() == Builder.getInt1Ty() && "Expected i1 type");
427
428 switch (OpType) {
429 default:
430 llvm_unreachable("Unsupported boolean expression");
431 case isl_ast_op_and:
432 Res = Builder.CreateAnd(LHS, RHS);
433 break;
434 case isl_ast_op_or:
435 Res = Builder.CreateOr(LHS, RHS);
436 break;
437 }
438
439 isl_ast_expr_free(Expr);
440 return Res;
441}
442
443Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) {
444 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op
445 && "Expression not of type isl_ast_expr_op");
446 switch (isl_ast_expr_get_op_type(Expr)) {
447 case isl_ast_op_error:
448 case isl_ast_op_cond:
449 case isl_ast_op_and_then:
450 case isl_ast_op_or_else:
451 case isl_ast_op_call:
452 llvm_unreachable("Unsupported isl ast expression");
453 case isl_ast_op_max:
454 case isl_ast_op_min:
455 return createOpNAry(Expr);
456 case isl_ast_op_add:
457 case isl_ast_op_sub:
458 case isl_ast_op_mul:
459 case isl_ast_op_div:
460 case isl_ast_op_fdiv_q: // Round towards -infty
461 case isl_ast_op_pdiv_q: // Dividend is non-negative
462 case isl_ast_op_pdiv_r: // Dividend is non-negative
463 return createOpBin(Expr);
464 case isl_ast_op_minus:
465 return createOpUnary(Expr);
466 case isl_ast_op_select:
467 return createOpSelect(Expr);
468 case isl_ast_op_and:
469 case isl_ast_op_or:
470 return createOpBoolean(Expr);
471 case isl_ast_op_eq:
472 case isl_ast_op_le:
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000473 case isl_ast_op_lt:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000474 case isl_ast_op_ge:
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000475 case isl_ast_op_gt:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000476 return createOpICmp(Expr);
477 }
478
479 llvm_unreachable("Unsupported isl_ast_expr_op kind.");
480}
481
482Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) {
483 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id
484 && "Expression not of type isl_ast_expr_ident");
485
486 isl_id *Id;
487 Value *V;
488
489 Id = isl_ast_expr_get_id(Expr);
490
491 assert(IDToValue.count(Id) && "Identifier not found");
492
493 V = IDToValue[Id];
494
495 isl_id_free(Id);
496 isl_ast_expr_free(Expr);
497
498 return V;
499}
500
501IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) {
502 // XXX: We assume i64 is large enough. This is often true, but in general
503 // incorrect. Also, on 32bit architectures, it would be beneficial to
504 // use a smaller type. We can and should directly derive this information
505 // during code generation.
506 return IntegerType::get(Builder.getContext(), 64);
507}
508
509Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) {
510 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int
511 && "Expression not of type isl_ast_expr_int");
512 isl_int Int;
513 Value *V;
514 APInt APValue;
515 IntegerType *T;
516
517 isl_int_init(Int);
518 isl_ast_expr_get_int(Expr, &Int);
519 APValue = APInt_from_MPZ(Int);
520 T = getType(Expr);
521 APValue = APValue.sextOrSelf(T->getBitWidth());
522 V = ConstantInt::get(T, APValue);
523
524 isl_ast_expr_free(Expr);
525 isl_int_clear(Int);
526 return V;
527}
528
529Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) {
530 switch (isl_ast_expr_get_type(Expr)) {
531 case isl_ast_expr_error:
532 llvm_unreachable("Code generation error");
533 case isl_ast_expr_op:
534 return createOp(Expr);
535 case isl_ast_expr_id:
536 return createId(Expr);
537 case isl_ast_expr_int:
538 return createInt(Expr);
539 }
540
541 llvm_unreachable("Unexpected enum value");
542}
543
544class IslNodeBuilder {
545public:
546 IslNodeBuilder(IRBuilder<> &Builder, Pass *P):
547 Builder(Builder), ExprBuilder(Builder, IDToValue, P), P(P) {}
548
549 void addParameters(__isl_take isl_set *Context);
550 void create(__isl_take isl_ast_node *Node);
551
552private:
553 IRBuilder<> &Builder;
554 IslExprBuilder ExprBuilder;
555 Pass *P;
556
557 // This maps an isl_id* to the Value* it has in the generated program. For now
558 // on, the only isl_ids that are stored here are the newly calculated loop
559 // ivs.
560 std::map<isl_id *, Value*> IDToValue;
561
562 // Extract the upper bound of this loop
563 //
564 // The isl code generation can generate arbitrary expressions to check if the
565 // upper bound of a loop is reached, but it provides an option to enforce
566 // 'atomic' upper bounds. An 'atomic upper bound is always of the form
567 // iv <= expr, where expr is an (arbitrary) expression not containing iv.
568 //
569 // This function extracts 'atomic' upper bounds. Polly, in general, requires
570 // atomic upper bounds for the following reasons:
571 //
572 // 1. An atomic upper bound is loop invariant
573 //
574 // It must not be calculated at each loop iteration and can often even be
575 // hoisted out further by the loop invariant code motion.
576 //
577 // 2. OpenMP needs a loop invarient upper bound to calculate the number
578 // of loop iterations.
579 //
580 // 3. With the existing code, upper bounds have been easier to implement.
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000581 __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For,
582 CmpInst::Predicate &Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000583
584 void createFor(__isl_take isl_ast_node *For);
585 void createIf(__isl_take isl_ast_node *If);
586 void createUser(__isl_take isl_ast_node *User);
587 void createBlock(__isl_take isl_ast_node *Block);
588};
589
590__isl_give isl_ast_expr *IslNodeBuilder::getUpperBound(
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000591 __isl_keep isl_ast_node *For, ICmpInst::Predicate &Predicate) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000592 isl_id *UBID, *IteratorID;
593 isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000594 isl_ast_op_type Type;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000595
596 Cond = isl_ast_node_for_get_cond(For);
597 Iterator = isl_ast_node_for_get_iterator(For);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000598 Type = isl_ast_expr_get_op_type(Cond);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000599
600 assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op
601 && "conditional expression is not an atomic upper bound");
602
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000603 switch (Type) {
604 case isl_ast_op_le:
605 Predicate = ICmpInst::ICMP_SLE;
606 break;
607 case isl_ast_op_lt:
608 Predicate = ICmpInst::ICMP_SLT;
609 break;
610 default:
611 llvm_unreachable("Unexpected comparision type in loop conditon");
612 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000613
614 Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
615
616 assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id
617 && "conditional expression is not an atomic upper bound");
618
619 UBID = isl_ast_expr_get_id(Arg0);
620
621 assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id
622 && "Could not get the iterator");
623
624 IteratorID = isl_ast_expr_get_id(Iterator);
625
626 assert(UBID == IteratorID
627 && "conditional expression is not an atomic upper bound");
628
629 UB = isl_ast_expr_get_op_arg(Cond, 1);
630
631 isl_ast_expr_free(Cond);
632 isl_ast_expr_free(Iterator);
633 isl_ast_expr_free(Arg0);
634 isl_id_free(IteratorID);
635 isl_id_free(UBID);
636
637 return UB;
638}
639
640void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
641 isl_ast_node *Body;
642 isl_ast_expr *Init, *Inc, *Iterator, *UB;
643 isl_id *IteratorID;
644 Value *ValueLB, *ValueUB, *ValueInc;
645 Type *MaxType;
646 BasicBlock *AfterBlock;
647 Value *IV;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000648 CmpInst::Predicate Predicate;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000649
650 Body = isl_ast_node_for_get_body(For);
651
652 // isl_ast_node_for_is_degenerate(For)
653 //
654 // TODO: For degenerated loops we could generate a plain assignment.
655 // However, for now we just reuse the logic for normal loops, which will
656 // create a loop with a single iteration.
657
658 Init = isl_ast_node_for_get_init(For);
659 Inc = isl_ast_node_for_get_inc(For);
660 Iterator = isl_ast_node_for_get_iterator(For);
661 IteratorID = isl_ast_expr_get_id(Iterator);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000662 UB = getUpperBound(For, Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000663
664 ValueLB = ExprBuilder.create(Init);
665 ValueUB = ExprBuilder.create(UB);
666 ValueInc = ExprBuilder.create(Inc);
667
668 MaxType = ExprBuilder.getType(Iterator);
669 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
670 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
671 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
672
673 if (MaxType != ValueLB->getType())
674 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
675 if (MaxType != ValueUB->getType())
676 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
677 if (MaxType != ValueInc->getType())
678 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
679
680 // TODO: In case we can proof a loop is executed at least once, we can
681 // generate the condition iv != UB + stride (consider possible
682 // overflow). This condition will allow LLVM to prove the loop is
683 // executed at least once, which will enable a lot of loop invariant
684 // code motion.
685
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000686 IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, AfterBlock,
687 Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000688 IDToValue[IteratorID] = IV;
689
690 create(Body);
691
692 IDToValue.erase(IteratorID);
693
694 Builder.SetInsertPoint(AfterBlock->begin());
695
696 isl_ast_node_free(For);
697 isl_ast_expr_free(Iterator);
698 isl_id_free(IteratorID);
699}
700
701void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
702 isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
703
704 Function *F = Builder.GetInsertBlock()->getParent();
705 LLVMContext &Context = F->getContext();
706
707 BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
708 Builder.GetInsertPoint(), P);
709 CondBB->setName("polly.cond");
710 BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
711 MergeBB->setName("polly.merge");
712 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
713 BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
714
715 DominatorTree &DT = P->getAnalysis<DominatorTree>();
716 DT.addNewBlock(ThenBB, CondBB);
717 DT.addNewBlock(ElseBB, CondBB);
718 DT.changeImmediateDominator(MergeBB, CondBB);
719
720 CondBB->getTerminator()->eraseFromParent();
721
722 Builder.SetInsertPoint(CondBB);
723 Value *Predicate = ExprBuilder.create(Cond);
724 Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
725 Builder.SetInsertPoint(ThenBB);
726 Builder.CreateBr(MergeBB);
727 Builder.SetInsertPoint(ElseBB);
728 Builder.CreateBr(MergeBB);
729 Builder.SetInsertPoint(ThenBB->begin());
730
731 create(isl_ast_node_if_get_then(If));
732
733 Builder.SetInsertPoint(ElseBB->begin());
734
735 if (isl_ast_node_if_has_else(If))
736 create(isl_ast_node_if_get_else(If));
737
738 Builder.SetInsertPoint(MergeBB->begin());
739
740 isl_ast_node_free(If);
741}
742
743void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
744 ValueMapT VMap;
745 struct IslAstUser *UserInfo;
746 isl_id *Annotation, *Id;
747 ScopStmt *Stmt;
748
749 Annotation = isl_ast_node_get_annotation(User);
750 UserInfo = (struct IslAstUser *) isl_id_get_user(Annotation);
751 Id = isl_pw_multi_aff_get_tuple_id(UserInfo->PMA, isl_dim_out);
752 Stmt = (ScopStmt *) isl_id_get_user(Id);
753
754 for (unsigned i = 0; i < isl_pw_multi_aff_dim(UserInfo->PMA, isl_dim_out);
755 ++i) {
756 isl_pw_aff *Aff;
757 isl_ast_expr *Expr;
758 const Value *OldIV;
759 Value *V;
760
761 Aff = isl_pw_multi_aff_get_pw_aff(UserInfo->PMA, i);
762 Expr = isl_ast_build_expr_from_pw_aff(UserInfo->Context, Aff);
763 OldIV = Stmt->getInductionVariableForDimension(i);
764 V = ExprBuilder.create(Expr);
765
766 // CreateIntCast can introduce trunc expressions. This is correct, as the
767 // result will always fit into the type of the original induction variable
768 // (because we calculate a value of the original induction variable).
769 V = Builder.CreateIntCast(V, OldIV->getType(), true);
770 VMap[OldIV] = V;
771 }
772
773 BlockGenerator::generate(Builder, *Stmt, VMap, P);
774
775 isl_ast_node_free(User);
776 isl_id_free(Annotation);
777 isl_id_free(Id);
778}
779
780void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
781 isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
782
783 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
784 create(isl_ast_node_list_get_ast_node(List, i));
785
786 isl_ast_node_free(Block);
787 isl_ast_node_list_free(List);
788}
789
790void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
791 switch (isl_ast_node_get_type(Node)) {
792 case isl_ast_node_error:
793 llvm_unreachable("code generation error");
794 case isl_ast_node_for:
795 createFor(Node);
796 return;
797 case isl_ast_node_if:
798 createIf(Node);
799 return;
800 case isl_ast_node_user:
801 createUser(Node);
802 return;
803 case isl_ast_node_block:
804 createBlock(Node);
805 return;
806 }
807
808 llvm_unreachable("Unknown isl_ast_node type");
809}
810
811void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
812 SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly");
813
814 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
815 isl_id *Id;
816 const SCEV *Scev;
817 IntegerType *T;
818 Instruction *InsertLocation;
819
820 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
821 Scev = (const SCEV*) isl_id_get_user(Id);
822 T = dyn_cast<IntegerType>(Scev->getType());
823 InsertLocation = --(Builder.GetInsertBlock()->end());
824 Value *V = Rewriter.expandCodeFor(Scev, T, InsertLocation);
825 IDToValue[Id] = V;
826
827 isl_id_free(Id);
828 }
829
830 isl_set_free(Context);
831}
832
833namespace {
834class IslCodeGeneration : public ScopPass {
835 public:
836 static char ID;
837
838 IslCodeGeneration() : ScopPass(ID) {}
839
840 bool runOnScop(Scop &S) {
841 IslAstInfo &AstInfo = getAnalysis<IslAstInfo>();
842 assert(S.getRegion().isSimple() && "Only simple regions are supported");
843
844 BasicBlock *StartBlock = executeScopConditionally(S, this);
845 isl_ast_node *Ast = AstInfo.getAst();
846 IRBuilder<> Builder(StartBlock->begin());
847
848 IslNodeBuilder NodeBuilder(Builder, this);
849 NodeBuilder.addParameters(S.getContext());
850 NodeBuilder.create(Ast);
851 return true;
852 }
853
854 virtual void printScop(raw_ostream &OS) const {
855 }
856
857 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
858 AU.addRequired<DominatorTree>();
859 AU.addRequired<IslAstInfo>();
860 AU.addRequired<RegionInfo>();
861 AU.addRequired<ScalarEvolution>();
862 AU.addRequired<ScopDetection>();
863 AU.addRequired<ScopInfo>();
864
865 AU.addPreserved<Dependences>();
866
867 // FIXME: We do not create LoopInfo for the newly generated loops.
868 AU.addPreserved<LoopInfo>();
869 AU.addPreserved<DominatorTree>();
870 AU.addPreserved<IslAstInfo>();
871 AU.addPreserved<ScopDetection>();
872 AU.addPreserved<ScalarEvolution>();
873
874 // FIXME: We do not yet add regions for the newly generated code to the
875 // region tree.
876 AU.addPreserved<RegionInfo>();
877 AU.addPreserved<TempScopInfo>();
878 AU.addPreserved<ScopInfo>();
879 AU.addPreservedID(IndependentBlocksID);
880 }
881};
882}
883
884char IslCodeGeneration::ID = 1;
885
886INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl",
887 "Polly - Create LLVM-IR from SCoPs", false, false)
888INITIALIZE_PASS_DEPENDENCY(Dependences)
889INITIALIZE_PASS_DEPENDENCY(DominatorTree)
890INITIALIZE_PASS_DEPENDENCY(LoopInfo)
891INITIALIZE_PASS_DEPENDENCY(RegionInfo)
892INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
893INITIALIZE_PASS_DEPENDENCY(ScopDetection)
894INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl",
895 "Polly - Create LLVM-IR from SCoPs", false, false)
896
897Pass *polly::createIslCodeGenerationPass() {
898 return new IslCodeGeneration();
899}
900
901#endif /* ISL_CODEGEN_FOUND */