blob: 398408f86e53da6575512fbf27640fb68457dba0 [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#include "polly/Dependences.h"
24#include "polly/LinkAllPasses.h"
25#include "polly/ScopInfo.h"
26#include "polly/TempScopInfo.h"
27#include "polly/CodeGen/IslAst.h"
28#include "polly/CodeGen/BlockGenerators.h"
Sebastian Pop04c4ce32012-12-18 07:46:13 +000029#include "polly/CodeGen/CodeGeneration.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000030#include "polly/CodeGen/LoopGenerators.h"
31#include "polly/CodeGen/Utils.h"
32#include "polly/Support/GICHelper.h"
33
Chandler Carruth535d52c2013-01-02 11:47:44 +000034#include "llvm/IR/Module.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000035#include "llvm/Analysis/LoopInfo.h"
36#include "llvm/Analysis/ScalarEvolutionExpander.h"
37#define DEBUG_TYPE "polly-codegen-isl"
38#include "llvm/Support/CommandLine.h"
39#include "llvm/Support/Debug.h"
Chandler Carruth535d52c2013-01-02 11:47:44 +000040#include "llvm/IR/DataLayout.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000041#include "llvm/Transforms/Utils/BasicBlockUtils.h"
42
43#include "isl/union_map.h"
44#include "isl/list.h"
45#include "isl/ast.h"
46#include "isl/ast_build.h"
47#include "isl/set.h"
48#include "isl/map.h"
49#include "isl/aff.h"
50
51#include <map>
52
53using namespace polly;
54using namespace llvm;
55
56/// @brief Insert function calls that print certain LLVM values at run time.
57///
58/// This class inserts libc function calls to print certain LLVM values at
59/// run time.
60class RuntimeDebugBuilder {
61public:
62 RuntimeDebugBuilder(IRBuilder<> &Builder) : Builder(Builder) {}
63
64 /// @brief Print a string to stdout.
65 ///
66 /// @param String The string to print.
67 void createStrPrinter(std::string String);
68
69 /// @brief Print an integer value to stdout.
70 ///
71 /// @param V The value to print.
72 void createIntPrinter(Value *V);
73
74private:
75 IRBuilder<> &Builder;
76
77 /// @brief Add a call to the fflush function with no file pointer given.
78 ///
79 /// This call will flush all opened file pointers including stdout and stderr.
80 void createFlush();
81
82 /// @brief Get a reference to the 'printf' function.
83 ///
84 /// If the current module does not yet contain a reference to printf, we
85 /// insert a reference to it. Otherwise the existing reference is returned.
86 Function *getPrintF();
87};
88
89Function *RuntimeDebugBuilder::getPrintF() {
90 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
91 const char *Name = "printf";
92 Function *F = M->getFunction(Name);
93
94 if (!F) {
95 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
Tobias Grosserc14582f2013-02-05 18:01:29 +000096 FunctionType *Ty =
97 FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), true);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000098 F = Function::Create(Ty, Linkage, Name, M);
99 }
100
101 return F;
102}
103
104void RuntimeDebugBuilder::createFlush() {
105 Module *M = Builder.GetInsertBlock()->getParent()->getParent();
106 const char *Name = "fflush";
107 Function *F = M->getFunction(Name);
108
109 if (!F) {
110 GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
Tobias Grosserc14582f2013-02-05 18:01:29 +0000111 FunctionType *Ty =
112 FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000113 F = Function::Create(Ty, Linkage, Name, M);
114 }
115
116 Builder.CreateCall(F, Constant::getNullValue(Builder.getInt8PtrTy()));
117}
118
119void RuntimeDebugBuilder::createStrPrinter(std::string String) {
120 Function *F = getPrintF();
121 Value *StringValue = Builder.CreateGlobalStringPtr(String);
122 Builder.CreateCall(F, StringValue);
123
124 createFlush();
125}
126
127void RuntimeDebugBuilder::createIntPrinter(Value *V) {
128 IntegerType *Ty = dyn_cast<IntegerType>(V->getType());
129 assert(Ty && Ty->getBitWidth() == 64 &&
130 "Cannot insert printer for this type.");
131
132 Function *F = getPrintF();
133 Value *String = Builder.CreateGlobalStringPtr("%ld");
134 Builder.CreateCall2(F, String, V);
135 createFlush();
136}
137
138/// @brief Calculate the Value of a certain isl_ast_expr
139class IslExprBuilder {
140public:
Tobias Grosser7242ad92013-02-22 08:07:06 +0000141 IslExprBuilder(IRBuilder<> &Builder, std::map<isl_id *, Value *> &IDToValue,
142 Pass *P)
143 : Builder(Builder), IDToValue(IDToValue) {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000144
145 Value *create(__isl_take isl_ast_expr *Expr);
146 Type *getWidestType(Type *T1, Type *T2);
147 IntegerType *getType(__isl_keep isl_ast_expr *Expr);
148
149private:
150 IRBuilder<> &Builder;
Tobias Grosserc14582f2013-02-05 18:01:29 +0000151 std::map<isl_id *, Value *> &IDToValue;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000152
153 Value *createOp(__isl_take isl_ast_expr *Expr);
154 Value *createOpUnary(__isl_take isl_ast_expr *Expr);
155 Value *createOpBin(__isl_take isl_ast_expr *Expr);
156 Value *createOpNAry(__isl_take isl_ast_expr *Expr);
157 Value *createOpSelect(__isl_take isl_ast_expr *Expr);
158 Value *createOpICmp(__isl_take isl_ast_expr *Expr);
159 Value *createOpBoolean(__isl_take isl_ast_expr *Expr);
160 Value *createId(__isl_take isl_ast_expr *Expr);
161 Value *createInt(__isl_take isl_ast_expr *Expr);
162};
163
164Type *IslExprBuilder::getWidestType(Type *T1, Type *T2) {
165 assert(isa<IntegerType>(T1) && isa<IntegerType>(T2));
166
167 if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits())
168 return T2;
169 else
170 return T1;
171}
172
173Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000174 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus &&
175 "Unsupported unary operation");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000176
177 Value *V;
178 Type *MaxType = getType(Expr);
179
180 V = create(isl_ast_expr_get_op_arg(Expr, 0));
181 MaxType = getWidestType(MaxType, V->getType());
182
183 if (MaxType != V->getType())
184 V = Builder.CreateSExt(V, MaxType);
185
186 isl_ast_expr_free(Expr);
187 return Builder.CreateNSWNeg(V);
188}
189
190Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000191 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
192 "isl ast expression not of type isl_ast_op");
193 assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 &&
194 "We need at least two operands in an n-ary operation");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000195
196 Value *V;
197
198 V = create(isl_ast_expr_get_op_arg(Expr, 0));
199
200 for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr); ++i) {
201 Value *OpV;
202 OpV = create(isl_ast_expr_get_op_arg(Expr, i));
203
204 Type *Ty = getWidestType(V->getType(), OpV->getType());
205
206 if (Ty != OpV->getType())
207 OpV = Builder.CreateSExt(OpV, Ty);
208
209 if (Ty != V->getType())
210 V = Builder.CreateSExt(V, Ty);
211
212 switch (isl_ast_expr_get_op_type(Expr)) {
213 default:
214 llvm_unreachable("This is no n-ary isl ast expression");
215
Tobias Grosserc14582f2013-02-05 18:01:29 +0000216 case isl_ast_op_max: {
217 Value *Cmp = Builder.CreateICmpSGT(V, OpV);
218 V = Builder.CreateSelect(Cmp, V, OpV);
219 continue;
220 }
221 case isl_ast_op_min: {
222 Value *Cmp = Builder.CreateICmpSLT(V, OpV);
223 V = Builder.CreateSelect(Cmp, V, OpV);
224 continue;
225 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000226 }
227 }
228
229 // TODO: We can truncate the result, if it fits into a smaller type. This can
230 // help in cases where we have larger operands (e.g. i67) but the result is
231 // known to fit into i64. Without the truncation, the larger i67 type may
232 // force all subsequent operations to be performed on a non-native type.
233 isl_ast_expr_free(Expr);
234 return V;
235}
236
237Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) {
238 Value *LHS, *RHS, *Res;
239 Type *MaxType;
240 isl_ast_op_type OpType;
241
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000242 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
243 "isl ast expression not of type isl_ast_op");
244 assert(isl_ast_expr_get_op_n_arg(Expr) == 2 &&
245 "not a binary isl ast expression");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000246
247 OpType = isl_ast_expr_get_op_type(Expr);
248
249 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
250 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
251
252 MaxType = LHS->getType();
253 MaxType = getWidestType(MaxType, RHS->getType());
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.
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000262 switch (OpType) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000263 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 // Do nothing
268 break;
269 case isl_ast_op_add:
270 case isl_ast_op_sub:
271 case isl_ast_op_mul:
272 MaxType = getWidestType(MaxType, getType(Expr));
273 break;
274 default:
275 llvm_unreachable("This is no binary isl ast expression");
276 }
277
278 if (MaxType != RHS->getType())
279 RHS = Builder.CreateSExt(RHS, MaxType);
280
281 if (MaxType != LHS->getType())
282 LHS = Builder.CreateSExt(LHS, MaxType);
283
284 switch (OpType) {
285 default:
286 llvm_unreachable("This is no binary isl ast expression");
287 case isl_ast_op_add:
288 Res = Builder.CreateNSWAdd(LHS, RHS);
289 break;
290 case isl_ast_op_sub:
291 Res = Builder.CreateNSWSub(LHS, RHS);
292 break;
293 case isl_ast_op_mul:
294 Res = Builder.CreateNSWMul(LHS, RHS);
295 break;
296 case isl_ast_op_div:
297 case isl_ast_op_pdiv_q: // Dividend is non-negative
298 Res = Builder.CreateSDiv(LHS, RHS);
299 break;
Tobias Grosserc14582f2013-02-05 18:01:29 +0000300 case isl_ast_op_fdiv_q: { // Round towards -infty
301 // TODO: Review code and check that this calculation does not yield
302 // incorrect overflow in some bordercases.
303 //
304 // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
305 Value *One = ConstantInt::get(MaxType, 1);
306 Value *Zero = ConstantInt::get(MaxType, 0);
307 Value *Sum1 = Builder.CreateSub(LHS, RHS);
308 Value *Sum2 = Builder.CreateAdd(Sum1, One);
309 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
310 Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS);
311 Res = Builder.CreateSDiv(Dividend, RHS);
312 break;
313 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000314 case isl_ast_op_pdiv_r: // Dividend is non-negative
315 Res = Builder.CreateSRem(LHS, RHS);
316 break;
317 }
318
319 // TODO: We can truncate the result, if it fits into a smaller type. This can
320 // help in cases where we have larger operands (e.g. i67) but the result is
321 // known to fit into i64. Without the truncation, the larger i67 type may
322 // force all subsequent operations to be performed on a non-native type.
323 isl_ast_expr_free(Expr);
324 return Res;
325}
326
327Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000328 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_select &&
329 "Unsupported unary isl ast expression");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000330 Value *LHS, *RHS, *Cond;
331 Type *MaxType = getType(Expr);
332
333 Cond = create(isl_ast_expr_get_op_arg(Expr, 0));
334
335 LHS = create(isl_ast_expr_get_op_arg(Expr, 1));
336 RHS = create(isl_ast_expr_get_op_arg(Expr, 2));
337
338 MaxType = getWidestType(MaxType, LHS->getType());
339 MaxType = getWidestType(MaxType, RHS->getType());
340
341 if (MaxType != RHS->getType())
342 RHS = Builder.CreateSExt(RHS, MaxType);
343
344 if (MaxType != LHS->getType())
345 LHS = Builder.CreateSExt(LHS, MaxType);
346
347 // TODO: Do we want to truncate the result?
348 isl_ast_expr_free(Expr);
349 return Builder.CreateSelect(Cond, LHS, RHS);
350}
351
352Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
353 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
354 "Expected an isl_ast_expr_op expression");
355
356 Value *LHS, *RHS, *Res;
357
358 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
359 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
360
361 Type *MaxType = LHS->getType();
362 MaxType = getWidestType(MaxType, RHS->getType());
363
364 if (MaxType != RHS->getType())
365 RHS = Builder.CreateSExt(RHS, MaxType);
366
367 if (MaxType != LHS->getType())
368 LHS = Builder.CreateSExt(LHS, MaxType);
369
370 switch (isl_ast_expr_get_op_type(Expr)) {
371 default:
372 llvm_unreachable("Unsupported ICmp isl ast expression");
373 case isl_ast_op_eq:
374 Res = Builder.CreateICmpEQ(LHS, RHS);
375 break;
376 case isl_ast_op_le:
377 Res = Builder.CreateICmpSLE(LHS, RHS);
378 break;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000379 case isl_ast_op_lt:
380 Res = Builder.CreateICmpSLT(LHS, RHS);
381 break;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000382 case isl_ast_op_ge:
383 Res = Builder.CreateICmpSGE(LHS, RHS);
384 break;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000385 case isl_ast_op_gt:
386 Res = Builder.CreateICmpSGT(LHS, RHS);
387 break;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000388 }
389
390 isl_ast_expr_free(Expr);
391 return Res;
392}
393
394Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) {
395 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
396 "Expected an isl_ast_expr_op expression");
397
398 Value *LHS, *RHS, *Res;
399 isl_ast_op_type OpType;
400
401 OpType = isl_ast_expr_get_op_type(Expr);
402
403 assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) &&
404 "Unsupported isl_ast_op_type");
405
406 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
407 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
408
409 // Even though the isl pretty printer prints the expressions as 'exp && exp'
410 // or 'exp || exp', we actually code generate the bitwise expressions
411 // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches,
412 // but it is, due to the use of i1 types, otherwise equivalent. The reason
413 // to go for bitwise operations is, that we assume the reduced control flow
414 // will outweight the overhead introduced by evaluating unneeded expressions.
415 // The isl code generation currently does not take advantage of the fact that
416 // the expression after an '||' or '&&' is in some cases not evaluated.
417 // Evaluating it anyways does not cause any undefined behaviour.
418 //
419 // TODO: Document in isl itself, that the unconditionally evaluating the
420 // second part of '||' or '&&' expressions is safe.
421 assert(LHS->getType() == Builder.getInt1Ty() && "Expected i1 type");
422 assert(RHS->getType() == Builder.getInt1Ty() && "Expected i1 type");
423
424 switch (OpType) {
425 default:
426 llvm_unreachable("Unsupported boolean expression");
427 case isl_ast_op_and:
428 Res = Builder.CreateAnd(LHS, RHS);
429 break;
430 case isl_ast_op_or:
431 Res = Builder.CreateOr(LHS, RHS);
432 break;
433 }
434
435 isl_ast_expr_free(Expr);
436 return Res;
437}
438
439Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000440 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
441 "Expression not of type isl_ast_expr_op");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000442 switch (isl_ast_expr_get_op_type(Expr)) {
443 case isl_ast_op_error:
444 case isl_ast_op_cond:
445 case isl_ast_op_and_then:
446 case isl_ast_op_or_else:
447 case isl_ast_op_call:
448 llvm_unreachable("Unsupported isl ast expression");
449 case isl_ast_op_max:
450 case isl_ast_op_min:
451 return createOpNAry(Expr);
452 case isl_ast_op_add:
453 case isl_ast_op_sub:
454 case isl_ast_op_mul:
455 case isl_ast_op_div:
456 case isl_ast_op_fdiv_q: // Round towards -infty
457 case isl_ast_op_pdiv_q: // Dividend is non-negative
458 case isl_ast_op_pdiv_r: // Dividend is non-negative
459 return createOpBin(Expr);
460 case isl_ast_op_minus:
461 return createOpUnary(Expr);
462 case isl_ast_op_select:
463 return createOpSelect(Expr);
464 case isl_ast_op_and:
465 case isl_ast_op_or:
466 return createOpBoolean(Expr);
467 case isl_ast_op_eq:
468 case isl_ast_op_le:
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000469 case isl_ast_op_lt:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000470 case isl_ast_op_ge:
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000471 case isl_ast_op_gt:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000472 return createOpICmp(Expr);
473 }
474
475 llvm_unreachable("Unsupported isl_ast_expr_op kind.");
476}
477
478Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000479 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id &&
480 "Expression not of type isl_ast_expr_ident");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000481
482 isl_id *Id;
483 Value *V;
484
485 Id = isl_ast_expr_get_id(Expr);
486
487 assert(IDToValue.count(Id) && "Identifier not found");
488
489 V = IDToValue[Id];
490
491 isl_id_free(Id);
492 isl_ast_expr_free(Expr);
493
494 return V;
495}
496
497IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) {
498 // XXX: We assume i64 is large enough. This is often true, but in general
499 // incorrect. Also, on 32bit architectures, it would be beneficial to
500 // use a smaller type. We can and should directly derive this information
501 // during code generation.
502 return IntegerType::get(Builder.getContext(), 64);
503}
504
505Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000506 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int &&
507 "Expression not of type isl_ast_expr_int");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000508 isl_int Int;
509 Value *V;
510 APInt APValue;
511 IntegerType *T;
512
513 isl_int_init(Int);
514 isl_ast_expr_get_int(Expr, &Int);
515 APValue = APInt_from_MPZ(Int);
516 T = getType(Expr);
517 APValue = APValue.sextOrSelf(T->getBitWidth());
518 V = ConstantInt::get(T, APValue);
519
520 isl_ast_expr_free(Expr);
521 isl_int_clear(Int);
522 return V;
523}
524
525Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) {
526 switch (isl_ast_expr_get_type(Expr)) {
527 case isl_ast_expr_error:
528 llvm_unreachable("Code generation error");
529 case isl_ast_expr_op:
530 return createOp(Expr);
531 case isl_ast_expr_id:
532 return createId(Expr);
533 case isl_ast_expr_int:
534 return createInt(Expr);
535 }
536
537 llvm_unreachable("Unexpected enum value");
538}
539
540class IslNodeBuilder {
541public:
Tobias Grosser7242ad92013-02-22 08:07:06 +0000542 IslNodeBuilder(IRBuilder<> &Builder, Pass *P)
543 : Builder(Builder), ExprBuilder(Builder, IDToValue, P), P(P) {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000544
545 void addParameters(__isl_take isl_set *Context);
546 void create(__isl_take isl_ast_node *Node);
547
548private:
549 IRBuilder<> &Builder;
550 IslExprBuilder ExprBuilder;
551 Pass *P;
552
553 // This maps an isl_id* to the Value* it has in the generated program. For now
554 // on, the only isl_ids that are stored here are the newly calculated loop
555 // ivs.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000556 std::map<isl_id *, Value *> IDToValue;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000557
558 // Extract the upper bound of this loop
559 //
560 // The isl code generation can generate arbitrary expressions to check if the
561 // upper bound of a loop is reached, but it provides an option to enforce
562 // 'atomic' upper bounds. An 'atomic upper bound is always of the form
563 // iv <= expr, where expr is an (arbitrary) expression not containing iv.
564 //
565 // This function extracts 'atomic' upper bounds. Polly, in general, requires
566 // atomic upper bounds for the following reasons:
567 //
568 // 1. An atomic upper bound is loop invariant
569 //
570 // It must not be calculated at each loop iteration and can often even be
571 // hoisted out further by the loop invariant code motion.
572 //
573 // 2. OpenMP needs a loop invarient upper bound to calculate the number
574 // of loop iterations.
575 //
576 // 3. With the existing code, upper bounds have been easier to implement.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000577 __isl_give isl_ast_expr *
578 getUpperBound(__isl_keep isl_ast_node *For, CmpInst::Predicate &Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000579
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000580 unsigned getNumberOfIterations(__isl_keep isl_ast_node *For);
581
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000582 void createFor(__isl_take isl_ast_node *For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000583 void createForVector(__isl_take isl_ast_node *For, int VectorWidth);
584 void createForSequential(__isl_take isl_ast_node *For);
585 void createSubstitutions(__isl_take isl_pw_multi_aff *PMA,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000586 __isl_take isl_ast_build *Context, ScopStmt *Stmt,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000587 ValueMapT &VMap, LoopToScevMapT &LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000588 void createSubstitutionsVector(
589 __isl_take isl_pw_multi_aff *PMA, __isl_take isl_ast_build *Context,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000590 ScopStmt *Stmt, VectorValueMapT &VMap, std::vector<LoopToScevMapT> &VLTS,
591 std::vector<Value *> &IVS, __isl_take isl_id *IteratorID);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000592 void createIf(__isl_take isl_ast_node *If);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000593 void createUserVector(
594 __isl_take isl_ast_node *User, std::vector<Value *> &IVS,
595 __isl_take isl_id *IteratorID, __isl_take isl_union_map *Schedule);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000596 void createUser(__isl_take isl_ast_node *User);
597 void createBlock(__isl_take isl_ast_node *Block);
598};
599
600__isl_give isl_ast_expr *IslNodeBuilder::getUpperBound(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000601 __isl_keep isl_ast_node *For, ICmpInst::Predicate &Predicate) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000602 isl_id *UBID, *IteratorID;
603 isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000604 isl_ast_op_type Type;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000605
606 Cond = isl_ast_node_for_get_cond(For);
607 Iterator = isl_ast_node_for_get_iterator(For);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000608 Type = isl_ast_expr_get_op_type(Cond);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000609
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000610 assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op &&
611 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000612
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000613 switch (Type) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000614 case isl_ast_op_le:
615 Predicate = ICmpInst::ICMP_SLE;
616 break;
617 case isl_ast_op_lt:
618 Predicate = ICmpInst::ICMP_SLT;
619 break;
620 default:
621 llvm_unreachable("Unexpected comparision type in loop conditon");
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000622 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000623
624 Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
625
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000626 assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id &&
627 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000628
629 UBID = isl_ast_expr_get_id(Arg0);
630
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000631 assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id &&
632 "Could not get the iterator");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000633
634 IteratorID = isl_ast_expr_get_id(Iterator);
635
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000636 assert(UBID == IteratorID &&
637 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000638
639 UB = isl_ast_expr_get_op_arg(Cond, 1);
640
641 isl_ast_expr_free(Cond);
642 isl_ast_expr_free(Iterator);
643 isl_ast_expr_free(Arg0);
644 isl_id_free(IteratorID);
645 isl_id_free(UBID);
646
647 return UB;
648}
649
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000650unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) {
651 isl_id *Annotation = isl_ast_node_get_annotation(For);
652 if (!Annotation)
653 return -1;
654
Tobias Grosserc14582f2013-02-05 18:01:29 +0000655 struct IslAstUser *Info = (struct IslAstUser *)isl_id_get_user(Annotation);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000656 if (!Info) {
657 isl_id_free(Annotation);
658 return -1;
659 }
660
661 isl_union_map *Schedule = isl_ast_build_get_schedule(Info->Context);
662 isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule));
663 isl_id_free(Annotation);
Sebastian Pop2aa5c242012-12-18 08:56:51 +0000664 int NumberOfIterations = polly::getNumberOfIterations(LoopDomain);
665 if (NumberOfIterations == -1)
666 return -1;
667 return NumberOfIterations + 1;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000668}
669
Tobias Grosserc14582f2013-02-05 18:01:29 +0000670void IslNodeBuilder::createUserVector(
671 __isl_take isl_ast_node *User, std::vector<Value *> &IVS,
672 __isl_take isl_id *IteratorID, __isl_take isl_union_map *Schedule) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000673 isl_id *Annotation = isl_ast_node_get_annotation(User);
674 assert(Annotation && "Vector user statement is not annotated");
675
Tobias Grosserc14582f2013-02-05 18:01:29 +0000676 struct IslAstUser *Info = (struct IslAstUser *)isl_id_get_user(Annotation);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000677 assert(Info && "Vector user statement annotation does not contain info");
678
679 isl_id *Id = isl_pw_multi_aff_get_tuple_id(Info->PMA, isl_dim_out);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000680 ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000681 VectorValueMapT VectorMap(IVS.size());
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000682 std::vector<LoopToScevMapT> VLTS(IVS.size());
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000683
684 isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain());
685 Schedule = isl_union_map_intersect_domain(Schedule, Domain);
686 isl_map *S = isl_map_from_union_map(Schedule);
687
688 createSubstitutionsVector(isl_pw_multi_aff_copy(Info->PMA),
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000689 isl_ast_build_copy(Info->Context), Stmt, VectorMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000690 VLTS, IVS, IteratorID);
691 VectorBlockGenerator::generate(Builder, *Stmt, VectorMap, VLTS, S, P);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000692
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000693 isl_map_free(S);
694 isl_id_free(Annotation);
695 isl_id_free(Id);
696 isl_ast_node_free(User);
697}
698
699void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
700 int VectorWidth) {
701 isl_ast_node *Body = isl_ast_node_for_get_body(For);
702 isl_ast_expr *Init = isl_ast_node_for_get_init(For);
703 isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
704 isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
705 isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
706 CmpInst::Predicate Predicate;
707 isl_ast_expr *UB = getUpperBound(For, Predicate);
708
709 Value *ValueLB = ExprBuilder.create(Init);
710 Value *ValueUB = ExprBuilder.create(UB);
711 Value *ValueInc = ExprBuilder.create(Inc);
712
713 Type *MaxType = ExprBuilder.getType(Iterator);
714 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
715 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
716 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
717
718 if (MaxType != ValueLB->getType())
719 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
720 if (MaxType != ValueUB->getType())
721 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
722 if (MaxType != ValueInc->getType())
723 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
724
Tobias Grosserc14582f2013-02-05 18:01:29 +0000725 std::vector<Value *> IVS(VectorWidth);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000726 IVS[0] = ValueLB;
727
728 for (int i = 1; i < VectorWidth; i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000729 IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv");
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000730
731 isl_id *Annotation = isl_ast_node_get_annotation(For);
732 assert(Annotation && "For statement is not annotated");
733
Tobias Grosserc14582f2013-02-05 18:01:29 +0000734 struct IslAstUser *Info = (struct IslAstUser *)isl_id_get_user(Annotation);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000735 assert(Info && "For statement annotation does not contain info");
736
737 isl_union_map *Schedule = isl_ast_build_get_schedule(Info->Context);
738 assert(Schedule && "For statement annotation does not contain its schedule");
739
740 IDToValue[IteratorID] = ValueLB;
741
742 switch (isl_ast_node_get_type(Body)) {
743 case isl_ast_node_user:
744 createUserVector(Body, IVS, isl_id_copy(IteratorID),
745 isl_union_map_copy(Schedule));
746 break;
747 case isl_ast_node_block: {
748 isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
749
750 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
751 createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000752 isl_id_copy(IteratorID), isl_union_map_copy(Schedule));
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000753
754 isl_ast_node_free(Body);
755 isl_ast_node_list_free(List);
756 break;
757 }
758 default:
759 isl_ast_node_dump(Body);
760 llvm_unreachable("Unhandled isl_ast_node in vectorizer");
761 }
762
763 IDToValue.erase(IteratorID);
764 isl_id_free(IteratorID);
765 isl_id_free(Annotation);
766 isl_union_map_free(Schedule);
767
768 isl_ast_node_free(For);
769 isl_ast_expr_free(Iterator);
770}
771
772void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000773 isl_ast_node *Body;
774 isl_ast_expr *Init, *Inc, *Iterator, *UB;
775 isl_id *IteratorID;
776 Value *ValueLB, *ValueUB, *ValueInc;
777 Type *MaxType;
778 BasicBlock *AfterBlock;
779 Value *IV;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000780 CmpInst::Predicate Predicate;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000781
782 Body = isl_ast_node_for_get_body(For);
783
784 // isl_ast_node_for_is_degenerate(For)
785 //
786 // TODO: For degenerated loops we could generate a plain assignment.
787 // However, for now we just reuse the logic for normal loops, which will
788 // create a loop with a single iteration.
789
790 Init = isl_ast_node_for_get_init(For);
791 Inc = isl_ast_node_for_get_inc(For);
792 Iterator = isl_ast_node_for_get_iterator(For);
793 IteratorID = isl_ast_expr_get_id(Iterator);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000794 UB = getUpperBound(For, Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000795
796 ValueLB = ExprBuilder.create(Init);
797 ValueUB = ExprBuilder.create(UB);
798 ValueInc = ExprBuilder.create(Inc);
799
800 MaxType = ExprBuilder.getType(Iterator);
801 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
802 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
803 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
804
805 if (MaxType != ValueLB->getType())
806 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
807 if (MaxType != ValueUB->getType())
808 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
809 if (MaxType != ValueInc->getType())
810 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
811
812 // TODO: In case we can proof a loop is executed at least once, we can
813 // generate the condition iv != UB + stride (consider possible
814 // overflow). This condition will allow LLVM to prove the loop is
815 // executed at least once, which will enable a lot of loop invariant
816 // code motion.
817
Tobias Grosserc14582f2013-02-05 18:01:29 +0000818 IV =
819 createLoop(ValueLB, ValueUB, ValueInc, Builder, P, AfterBlock, Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000820 IDToValue[IteratorID] = IV;
821
822 create(Body);
823
824 IDToValue.erase(IteratorID);
825
826 Builder.SetInsertPoint(AfterBlock->begin());
827
828 isl_ast_node_free(For);
829 isl_ast_expr_free(Iterator);
830 isl_id_free(IteratorID);
831}
832
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000833void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
834 bool Vector = PollyVectorizerChoice != VECTORIZER_NONE;
835
836 if (Vector && isInnermostParallel(For)) {
837 int VectorWidth = getNumberOfIterations(For);
838 if (1 < VectorWidth && VectorWidth <= 16) {
839 createForVector(For, VectorWidth);
840 return;
841 }
842 }
843 createForSequential(For);
844}
845
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000846void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
847 isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
848
849 Function *F = Builder.GetInsertBlock()->getParent();
850 LLVMContext &Context = F->getContext();
851
Tobias Grosserc14582f2013-02-05 18:01:29 +0000852 BasicBlock *CondBB =
853 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000854 CondBB->setName("polly.cond");
855 BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
856 MergeBB->setName("polly.merge");
857 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
858 BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
859
860 DominatorTree &DT = P->getAnalysis<DominatorTree>();
861 DT.addNewBlock(ThenBB, CondBB);
862 DT.addNewBlock(ElseBB, CondBB);
863 DT.changeImmediateDominator(MergeBB, CondBB);
864
865 CondBB->getTerminator()->eraseFromParent();
866
867 Builder.SetInsertPoint(CondBB);
868 Value *Predicate = ExprBuilder.create(Cond);
869 Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
870 Builder.SetInsertPoint(ThenBB);
871 Builder.CreateBr(MergeBB);
872 Builder.SetInsertPoint(ElseBB);
873 Builder.CreateBr(MergeBB);
874 Builder.SetInsertPoint(ThenBB->begin());
875
876 create(isl_ast_node_if_get_then(If));
877
878 Builder.SetInsertPoint(ElseBB->begin());
879
880 if (isl_ast_node_if_has_else(If))
881 create(isl_ast_node_if_get_else(If));
882
883 Builder.SetInsertPoint(MergeBB->begin());
884
885 isl_ast_node_free(If);
886}
887
Tobias Grosser7242ad92013-02-22 08:07:06 +0000888void IslNodeBuilder::createSubstitutions(
889 __isl_take isl_pw_multi_aff *PMA, __isl_take isl_ast_build *Context,
890 ScopStmt *Stmt, ValueMapT &VMap, LoopToScevMapT &LTS) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000891 for (unsigned i = 0; i < isl_pw_multi_aff_dim(PMA, isl_dim_out); ++i) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000892 isl_pw_aff *Aff;
893 isl_ast_expr *Expr;
894 const Value *OldIV;
895 Value *V;
896
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000897 Aff = isl_pw_multi_aff_get_pw_aff(PMA, i);
898 Expr = isl_ast_build_expr_from_pw_aff(Context, Aff);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000899 OldIV = Stmt->getInductionVariableForDimension(i);
900 V = ExprBuilder.create(Expr);
901
902 // CreateIntCast can introduce trunc expressions. This is correct, as the
903 // result will always fit into the type of the original induction variable
904 // (because we calculate a value of the original induction variable).
905 V = Builder.CreateIntCast(V, OldIV->getType(), true);
906 VMap[OldIV] = V;
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000907 ScalarEvolution *SE = Stmt->getParent()->getSE();
908 LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000909 }
910
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000911 isl_pw_multi_aff_free(PMA);
912 isl_ast_build_free(Context);
913}
914
Tobias Grosserc14582f2013-02-05 18:01:29 +0000915void IslNodeBuilder::createSubstitutionsVector(
916 __isl_take isl_pw_multi_aff *PMA, __isl_take isl_ast_build *Context,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000917 ScopStmt *Stmt, VectorValueMapT &VMap, std::vector<LoopToScevMapT> &VLTS,
918 std::vector<Value *> &IVS, __isl_take isl_id *IteratorID) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000919 int i = 0;
920
921 Value *OldValue = IDToValue[IteratorID];
Tobias Grosserc14582f2013-02-05 18:01:29 +0000922 for (std::vector<Value *>::iterator II = IVS.begin(), IE = IVS.end();
923 II != IE; ++II) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000924 IDToValue[IteratorID] = *II;
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000925 createSubstitutions(isl_pw_multi_aff_copy(PMA), isl_ast_build_copy(Context),
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000926 Stmt, VMap[i], VLTS[i]);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000927 i++;
928 }
929
930 IDToValue[IteratorID] = OldValue;
931 isl_id_free(IteratorID);
932 isl_pw_multi_aff_free(PMA);
933 isl_ast_build_free(Context);
934}
935
936void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
937 ValueMapT VMap;
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000938 LoopToScevMapT LTS;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000939 struct IslAstUser *Info;
940 isl_id *Annotation, *Id;
941 ScopStmt *Stmt;
942
943 Annotation = isl_ast_node_get_annotation(User);
944 assert(Annotation && "Scalar user statement is not annotated");
945
Tobias Grosserc14582f2013-02-05 18:01:29 +0000946 Info = (struct IslAstUser *)isl_id_get_user(Annotation);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000947 assert(Info && "Scalar user statement annotation does not contain info");
948
949 Id = isl_pw_multi_aff_get_tuple_id(Info->PMA, isl_dim_out);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000950 Stmt = (ScopStmt *)isl_id_get_user(Id);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000951
952 createSubstitutions(isl_pw_multi_aff_copy(Info->PMA),
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000953 isl_ast_build_copy(Info->Context), Stmt, VMap, LTS);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000954
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000955 BlockGenerator::generate(Builder, *Stmt, VMap, LTS, P);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000956
957 isl_ast_node_free(User);
958 isl_id_free(Annotation);
959 isl_id_free(Id);
960}
961
962void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
963 isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
964
965 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
966 create(isl_ast_node_list_get_ast_node(List, i));
967
968 isl_ast_node_free(Block);
969 isl_ast_node_list_free(List);
970}
971
972void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
973 switch (isl_ast_node_get_type(Node)) {
974 case isl_ast_node_error:
975 llvm_unreachable("code generation error");
976 case isl_ast_node_for:
977 createFor(Node);
978 return;
979 case isl_ast_node_if:
980 createIf(Node);
981 return;
982 case isl_ast_node_user:
983 createUser(Node);
984 return;
985 case isl_ast_node_block:
986 createBlock(Node);
987 return;
988 }
989
990 llvm_unreachable("Unknown isl_ast_node type");
991}
992
993void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
994 SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly");
995
996 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
997 isl_id *Id;
998 const SCEV *Scev;
999 IntegerType *T;
1000 Instruction *InsertLocation;
1001
1002 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserc14582f2013-02-05 18:01:29 +00001003 Scev = (const SCEV *)isl_id_get_user(Id);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001004 T = dyn_cast<IntegerType>(Scev->getType());
1005 InsertLocation = --(Builder.GetInsertBlock()->end());
1006 Value *V = Rewriter.expandCodeFor(Scev, T, InsertLocation);
1007 IDToValue[Id] = V;
1008
1009 isl_id_free(Id);
1010 }
1011
1012 isl_set_free(Context);
1013}
1014
1015namespace {
1016class IslCodeGeneration : public ScopPass {
Tobias Grosser1bb59b02012-12-29 23:47:38 +00001017public:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001018 static char ID;
1019
1020 IslCodeGeneration() : ScopPass(ID) {}
1021
1022 bool runOnScop(Scop &S) {
1023 IslAstInfo &AstInfo = getAnalysis<IslAstInfo>();
1024 assert(S.getRegion().isSimple() && "Only simple regions are supported");
1025
1026 BasicBlock *StartBlock = executeScopConditionally(S, this);
1027 isl_ast_node *Ast = AstInfo.getAst();
1028 IRBuilder<> Builder(StartBlock->begin());
1029
1030 IslNodeBuilder NodeBuilder(Builder, this);
1031 NodeBuilder.addParameters(S.getContext());
1032 NodeBuilder.create(Ast);
1033 return true;
1034 }
1035
Tobias Grosserc14582f2013-02-05 18:01:29 +00001036 virtual void printScop(raw_ostream &OS) const {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001037
1038 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1039 AU.addRequired<DominatorTree>();
1040 AU.addRequired<IslAstInfo>();
1041 AU.addRequired<RegionInfo>();
1042 AU.addRequired<ScalarEvolution>();
1043 AU.addRequired<ScopDetection>();
1044 AU.addRequired<ScopInfo>();
1045
1046 AU.addPreserved<Dependences>();
1047
1048 // FIXME: We do not create LoopInfo for the newly generated loops.
1049 AU.addPreserved<LoopInfo>();
1050 AU.addPreserved<DominatorTree>();
1051 AU.addPreserved<IslAstInfo>();
1052 AU.addPreserved<ScopDetection>();
1053 AU.addPreserved<ScalarEvolution>();
1054
1055 // FIXME: We do not yet add regions for the newly generated code to the
1056 // region tree.
1057 AU.addPreserved<RegionInfo>();
1058 AU.addPreserved<TempScopInfo>();
1059 AU.addPreserved<ScopInfo>();
1060 AU.addPreservedID(IndependentBlocksID);
1061 }
1062};
1063}
1064
1065char IslCodeGeneration::ID = 1;
1066
Tobias Grosser7242ad92013-02-22 08:07:06 +00001067Pass *polly::createIslCodeGenerationPass() { return new IslCodeGeneration(); }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001068
Tobias Grosser7242ad92013-02-22 08:07:06 +00001069INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl",
1070 "Polly - Create LLVM-IR from SCoPs", false, false);
1071INITIALIZE_PASS_DEPENDENCY(Dependences);
1072INITIALIZE_PASS_DEPENDENCY(DominatorTree);
1073INITIALIZE_PASS_DEPENDENCY(LoopInfo);
1074INITIALIZE_PASS_DEPENDENCY(RegionInfo);
1075INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1076INITIALIZE_PASS_DEPENDENCY(ScopDetection);
1077INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl",
1078 "Polly - Create LLVM-IR from SCoPs", false, false)
1079