blob: 2f22d18367e9c59a0b0a9e987d777007e524547d [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
34#include "llvm/Module.h"
35#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"
Micah Villmow7a3d8202012-10-08 17:26:19 +000040#include "llvm/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;
96 FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(),
97 Builder.getInt8PtrTy(), true);
98 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;
111 FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(),
112 Builder.getInt8PtrTy(), false);
113 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:
141 IslExprBuilder(IRBuilder<> &Builder,
142 std::map<isl_id *, Value*> &IDToValue, Pass *P)
143 : Builder(Builder), IDToValue(IDToValue) { }
144
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;
151 std::map<isl_id *, Value*> &IDToValue;
152
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) {
174 assert (isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus
175 && "Unsupported unary operation");
176
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) {
191 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");
195
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
216 case isl_ast_op_max:
217 {
218 Value *Cmp = Builder.CreateICmpSGT(V, OpV);
219 V = Builder.CreateSelect(Cmp, V, OpV);
220 continue;
221 }
222 case isl_ast_op_min:
223 {
224 Value *Cmp = Builder.CreateICmpSLT(V, OpV);
225 V = Builder.CreateSelect(Cmp, V, OpV);
226 continue;
227 }
228 }
229 }
230
231 // TODO: We can truncate the result, if it fits into a smaller type. This can
232 // help in cases where we have larger operands (e.g. i67) but the result is
233 // known to fit into i64. Without the truncation, the larger i67 type may
234 // force all subsequent operations to be performed on a non-native type.
235 isl_ast_expr_free(Expr);
236 return V;
237}
238
239Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) {
240 Value *LHS, *RHS, *Res;
241 Type *MaxType;
242 isl_ast_op_type OpType;
243
244 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op
245 && "isl ast expression not of type isl_ast_op");
246 assert(isl_ast_expr_get_op_n_arg(Expr) == 2
247 && "not a binary isl ast expression");
248
249 OpType = isl_ast_expr_get_op_type(Expr);
250
251 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
252 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
253
254 MaxType = LHS->getType();
255 MaxType = getWidestType(MaxType, RHS->getType());
256
257 // Take the result into account when calculating the widest type.
258 //
259 // For operations such as '+' the result may require a type larger than
260 // the type of the individual operands. For other operations such as '/', the
261 // result type cannot be larger than the type of the individual operand. isl
262 // does not calculate correct types for these operations and we consequently
263 // exclude those operations here.
264 switch(OpType) {
265 case isl_ast_op_pdiv_q:
266 case isl_ast_op_pdiv_r:
267 case isl_ast_op_div:
268 case isl_ast_op_fdiv_q:
269 // Do nothing
270 break;
271 case isl_ast_op_add:
272 case isl_ast_op_sub:
273 case isl_ast_op_mul:
274 MaxType = getWidestType(MaxType, getType(Expr));
275 break;
276 default:
277 llvm_unreachable("This is no binary isl ast expression");
278 }
279
280 if (MaxType != RHS->getType())
281 RHS = Builder.CreateSExt(RHS, MaxType);
282
283 if (MaxType != LHS->getType())
284 LHS = Builder.CreateSExt(LHS, MaxType);
285
286 switch (OpType) {
287 default:
288 llvm_unreachable("This is no binary isl ast expression");
289 case isl_ast_op_add:
290 Res = Builder.CreateNSWAdd(LHS, RHS);
291 break;
292 case isl_ast_op_sub:
293 Res = Builder.CreateNSWSub(LHS, RHS);
294 break;
295 case isl_ast_op_mul:
296 Res = Builder.CreateNSWMul(LHS, RHS);
297 break;
298 case isl_ast_op_div:
299 case isl_ast_op_pdiv_q: // Dividend is non-negative
300 Res = Builder.CreateSDiv(LHS, RHS);
301 break;
302 case isl_ast_op_fdiv_q: // Round towards -infty
303 {
304 // TODO: Review code and check that this calculation does not yield
305 // incorrect overflow in some bordercases.
306 //
307 // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
308 Value *One = ConstantInt::get(MaxType, 1);
309 Value *Zero = ConstantInt::get(MaxType, 0);
310 Value *Sum1 = Builder.CreateSub(LHS, RHS);
311 Value *Sum2 = Builder.CreateAdd(Sum1, One);
312 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
313 Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS);
314 Res = Builder.CreateSDiv(Dividend, RHS);
315 break;
316 }
317 case isl_ast_op_pdiv_r: // Dividend is non-negative
318 Res = Builder.CreateSRem(LHS, RHS);
319 break;
320 }
321
322 // TODO: We can truncate the result, if it fits into a smaller type. This can
323 // help in cases where we have larger operands (e.g. i67) but the result is
324 // known to fit into i64. Without the truncation, the larger i67 type may
325 // force all subsequent operations to be performed on a non-native type.
326 isl_ast_expr_free(Expr);
327 return Res;
328}
329
330Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) {
331 assert (isl_ast_expr_get_op_type(Expr) == isl_ast_op_select
332 && "Unsupported unary isl ast expression");
333 Value *LHS, *RHS, *Cond;
334 Type *MaxType = getType(Expr);
335
336 Cond = create(isl_ast_expr_get_op_arg(Expr, 0));
337
338 LHS = create(isl_ast_expr_get_op_arg(Expr, 1));
339 RHS = create(isl_ast_expr_get_op_arg(Expr, 2));
340
341 MaxType = getWidestType(MaxType, LHS->getType());
342 MaxType = getWidestType(MaxType, RHS->getType());
343
344 if (MaxType != RHS->getType())
345 RHS = Builder.CreateSExt(RHS, MaxType);
346
347 if (MaxType != LHS->getType())
348 LHS = Builder.CreateSExt(LHS, MaxType);
349
350 // TODO: Do we want to truncate the result?
351 isl_ast_expr_free(Expr);
352 return Builder.CreateSelect(Cond, LHS, RHS);
353}
354
355Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
356 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
357 "Expected an isl_ast_expr_op expression");
358
359 Value *LHS, *RHS, *Res;
360
361 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
362 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
363
364 Type *MaxType = LHS->getType();
365 MaxType = getWidestType(MaxType, RHS->getType());
366
367 if (MaxType != RHS->getType())
368 RHS = Builder.CreateSExt(RHS, MaxType);
369
370 if (MaxType != LHS->getType())
371 LHS = Builder.CreateSExt(LHS, MaxType);
372
373 switch (isl_ast_expr_get_op_type(Expr)) {
374 default:
375 llvm_unreachable("Unsupported ICmp isl ast expression");
376 case isl_ast_op_eq:
377 Res = Builder.CreateICmpEQ(LHS, RHS);
378 break;
379 case isl_ast_op_le:
380 Res = Builder.CreateICmpSLE(LHS, RHS);
381 break;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000382 case isl_ast_op_lt:
383 Res = Builder.CreateICmpSLT(LHS, RHS);
384 break;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000385 case isl_ast_op_ge:
386 Res = Builder.CreateICmpSGE(LHS, RHS);
387 break;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000388 case isl_ast_op_gt:
389 Res = Builder.CreateICmpSGT(LHS, RHS);
390 break;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000391 }
392
393 isl_ast_expr_free(Expr);
394 return Res;
395}
396
397Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) {
398 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
399 "Expected an isl_ast_expr_op expression");
400
401 Value *LHS, *RHS, *Res;
402 isl_ast_op_type OpType;
403
404 OpType = isl_ast_expr_get_op_type(Expr);
405
406 assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) &&
407 "Unsupported isl_ast_op_type");
408
409 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
410 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
411
412 // Even though the isl pretty printer prints the expressions as 'exp && exp'
413 // or 'exp || exp', we actually code generate the bitwise expressions
414 // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches,
415 // but it is, due to the use of i1 types, otherwise equivalent. The reason
416 // to go for bitwise operations is, that we assume the reduced control flow
417 // will outweight the overhead introduced by evaluating unneeded expressions.
418 // The isl code generation currently does not take advantage of the fact that
419 // the expression after an '||' or '&&' is in some cases not evaluated.
420 // Evaluating it anyways does not cause any undefined behaviour.
421 //
422 // TODO: Document in isl itself, that the unconditionally evaluating the
423 // second part of '||' or '&&' expressions is safe.
424 assert(LHS->getType() == Builder.getInt1Ty() && "Expected i1 type");
425 assert(RHS->getType() == Builder.getInt1Ty() && "Expected i1 type");
426
427 switch (OpType) {
428 default:
429 llvm_unreachable("Unsupported boolean expression");
430 case isl_ast_op_and:
431 Res = Builder.CreateAnd(LHS, RHS);
432 break;
433 case isl_ast_op_or:
434 Res = Builder.CreateOr(LHS, RHS);
435 break;
436 }
437
438 isl_ast_expr_free(Expr);
439 return Res;
440}
441
442Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) {
443 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op
444 && "Expression not of type isl_ast_expr_op");
445 switch (isl_ast_expr_get_op_type(Expr)) {
446 case isl_ast_op_error:
447 case isl_ast_op_cond:
448 case isl_ast_op_and_then:
449 case isl_ast_op_or_else:
450 case isl_ast_op_call:
451 llvm_unreachable("Unsupported isl ast expression");
452 case isl_ast_op_max:
453 case isl_ast_op_min:
454 return createOpNAry(Expr);
455 case isl_ast_op_add:
456 case isl_ast_op_sub:
457 case isl_ast_op_mul:
458 case isl_ast_op_div:
459 case isl_ast_op_fdiv_q: // Round towards -infty
460 case isl_ast_op_pdiv_q: // Dividend is non-negative
461 case isl_ast_op_pdiv_r: // Dividend is non-negative
462 return createOpBin(Expr);
463 case isl_ast_op_minus:
464 return createOpUnary(Expr);
465 case isl_ast_op_select:
466 return createOpSelect(Expr);
467 case isl_ast_op_and:
468 case isl_ast_op_or:
469 return createOpBoolean(Expr);
470 case isl_ast_op_eq:
471 case isl_ast_op_le:
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000472 case isl_ast_op_lt:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000473 case isl_ast_op_ge:
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000474 case isl_ast_op_gt:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000475 return createOpICmp(Expr);
476 }
477
478 llvm_unreachable("Unsupported isl_ast_expr_op kind.");
479}
480
481Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) {
482 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id
483 && "Expression not of type isl_ast_expr_ident");
484
485 isl_id *Id;
486 Value *V;
487
488 Id = isl_ast_expr_get_id(Expr);
489
490 assert(IDToValue.count(Id) && "Identifier not found");
491
492 V = IDToValue[Id];
493
494 isl_id_free(Id);
495 isl_ast_expr_free(Expr);
496
497 return V;
498}
499
500IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) {
501 // XXX: We assume i64 is large enough. This is often true, but in general
502 // incorrect. Also, on 32bit architectures, it would be beneficial to
503 // use a smaller type. We can and should directly derive this information
504 // during code generation.
505 return IntegerType::get(Builder.getContext(), 64);
506}
507
508Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) {
509 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int
510 && "Expression not of type isl_ast_expr_int");
511 isl_int Int;
512 Value *V;
513 APInt APValue;
514 IntegerType *T;
515
516 isl_int_init(Int);
517 isl_ast_expr_get_int(Expr, &Int);
518 APValue = APInt_from_MPZ(Int);
519 T = getType(Expr);
520 APValue = APValue.sextOrSelf(T->getBitWidth());
521 V = ConstantInt::get(T, APValue);
522
523 isl_ast_expr_free(Expr);
524 isl_int_clear(Int);
525 return V;
526}
527
528Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) {
529 switch (isl_ast_expr_get_type(Expr)) {
530 case isl_ast_expr_error:
531 llvm_unreachable("Code generation error");
532 case isl_ast_expr_op:
533 return createOp(Expr);
534 case isl_ast_expr_id:
535 return createId(Expr);
536 case isl_ast_expr_int:
537 return createInt(Expr);
538 }
539
540 llvm_unreachable("Unexpected enum value");
541}
542
543class IslNodeBuilder {
544public:
545 IslNodeBuilder(IRBuilder<> &Builder, Pass *P):
546 Builder(Builder), ExprBuilder(Builder, IDToValue, P), P(P) {}
547
548 void addParameters(__isl_take isl_set *Context);
549 void create(__isl_take isl_ast_node *Node);
550
551private:
552 IRBuilder<> &Builder;
553 IslExprBuilder ExprBuilder;
554 Pass *P;
555
556 // This maps an isl_id* to the Value* it has in the generated program. For now
557 // on, the only isl_ids that are stored here are the newly calculated loop
558 // ivs.
559 std::map<isl_id *, Value*> IDToValue;
560
561 // Extract the upper bound of this loop
562 //
563 // The isl code generation can generate arbitrary expressions to check if the
564 // upper bound of a loop is reached, but it provides an option to enforce
565 // 'atomic' upper bounds. An 'atomic upper bound is always of the form
566 // iv <= expr, where expr is an (arbitrary) expression not containing iv.
567 //
568 // This function extracts 'atomic' upper bounds. Polly, in general, requires
569 // atomic upper bounds for the following reasons:
570 //
571 // 1. An atomic upper bound is loop invariant
572 //
573 // It must not be calculated at each loop iteration and can often even be
574 // hoisted out further by the loop invariant code motion.
575 //
576 // 2. OpenMP needs a loop invarient upper bound to calculate the number
577 // of loop iterations.
578 //
579 // 3. With the existing code, upper bounds have been easier to implement.
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000580 __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For,
581 CmpInst::Predicate &Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000582
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000583 unsigned getNumberOfIterations(__isl_keep isl_ast_node *For);
584
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000585 void createFor(__isl_take isl_ast_node *For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000586 void createForVector(__isl_take isl_ast_node *For, int VectorWidth);
587 void createForSequential(__isl_take isl_ast_node *For);
588 void createSubstitutions(__isl_take isl_pw_multi_aff *PMA,
589 __isl_take isl_ast_build *Context,
590 ScopStmt *Stmt, ValueMapT &VMap);
591 void createSubstitutionsVector(__isl_take isl_pw_multi_aff *PMA,
592 __isl_take isl_ast_build *Context,
593 ScopStmt *Stmt, VectorValueMapT &VMap,
594 std::vector<Value*> &IVS,
595 __isl_take isl_id *IteratorID);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000596 void createIf(__isl_take isl_ast_node *If);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000597 void createUserVector(__isl_take isl_ast_node *User,
598 std::vector<Value*> &IVS, __isl_take isl_id *IteratorID,
599 __isl_take isl_union_map *Schedule);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000600 void createUser(__isl_take isl_ast_node *User);
601 void createBlock(__isl_take isl_ast_node *Block);
602};
603
604__isl_give isl_ast_expr *IslNodeBuilder::getUpperBound(
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000605 __isl_keep isl_ast_node *For, ICmpInst::Predicate &Predicate) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000606 isl_id *UBID, *IteratorID;
607 isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000608 isl_ast_op_type Type;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000609
610 Cond = isl_ast_node_for_get_cond(For);
611 Iterator = isl_ast_node_for_get_iterator(For);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000612 Type = isl_ast_expr_get_op_type(Cond);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000613
614 assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op
615 && "conditional expression is not an atomic upper bound");
616
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000617 switch (Type) {
618 case isl_ast_op_le:
619 Predicate = ICmpInst::ICMP_SLE;
620 break;
621 case isl_ast_op_lt:
622 Predicate = ICmpInst::ICMP_SLT;
623 break;
624 default:
625 llvm_unreachable("Unexpected comparision type in loop conditon");
626 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000627
628 Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
629
630 assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id
631 && "conditional expression is not an atomic upper bound");
632
633 UBID = isl_ast_expr_get_id(Arg0);
634
635 assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id
636 && "Could not get the iterator");
637
638 IteratorID = isl_ast_expr_get_id(Iterator);
639
640 assert(UBID == IteratorID
641 && "conditional expression is not an atomic upper bound");
642
643 UB = isl_ast_expr_get_op_arg(Cond, 1);
644
645 isl_ast_expr_free(Cond);
646 isl_ast_expr_free(Iterator);
647 isl_ast_expr_free(Arg0);
648 isl_id_free(IteratorID);
649 isl_id_free(UBID);
650
651 return UB;
652}
653
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000654unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) {
655 isl_id *Annotation = isl_ast_node_get_annotation(For);
656 if (!Annotation)
657 return -1;
658
659 struct IslAstUser *Info = (struct IslAstUser *) isl_id_get_user(Annotation);
660 if (!Info) {
661 isl_id_free(Annotation);
662 return -1;
663 }
664
665 isl_union_map *Schedule = isl_ast_build_get_schedule(Info->Context);
666 isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule));
667 isl_id_free(Annotation);
Sebastian Pop2aa5c242012-12-18 08:56:51 +0000668 int NumberOfIterations = polly::getNumberOfIterations(LoopDomain);
669 if (NumberOfIterations == -1)
670 return -1;
671 return NumberOfIterations + 1;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000672}
673
674void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User,
675 std::vector<Value*> &IVS,
676 __isl_take isl_id *IteratorID,
677 __isl_take isl_union_map *Schedule) {
678 isl_id *Annotation = isl_ast_node_get_annotation(User);
679 assert(Annotation && "Vector user statement is not annotated");
680
681 struct IslAstUser *Info = (struct IslAstUser *) isl_id_get_user(Annotation);
682 assert(Info && "Vector user statement annotation does not contain info");
683
684 isl_id *Id = isl_pw_multi_aff_get_tuple_id(Info->PMA, isl_dim_out);
685 ScopStmt *Stmt = (ScopStmt *) isl_id_get_user(Id);
686 VectorValueMapT VectorMap(IVS.size());
687
688 isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain());
689 Schedule = isl_union_map_intersect_domain(Schedule, Domain);
690 isl_map *S = isl_map_from_union_map(Schedule);
691
692 createSubstitutionsVector(isl_pw_multi_aff_copy(Info->PMA),
693 isl_ast_build_copy(Info->Context),
694 Stmt, VectorMap, IVS, IteratorID);
695 VectorBlockGenerator::generate(Builder, *Stmt, VectorMap, S, P);
696
697
698 isl_map_free(S);
699 isl_id_free(Annotation);
700 isl_id_free(Id);
701 isl_ast_node_free(User);
702}
703
704void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
705 int VectorWidth) {
706 isl_ast_node *Body = isl_ast_node_for_get_body(For);
707 isl_ast_expr *Init = isl_ast_node_for_get_init(For);
708 isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
709 isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
710 isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
711 CmpInst::Predicate Predicate;
712 isl_ast_expr *UB = getUpperBound(For, Predicate);
713
714 Value *ValueLB = ExprBuilder.create(Init);
715 Value *ValueUB = ExprBuilder.create(UB);
716 Value *ValueInc = ExprBuilder.create(Inc);
717
718 Type *MaxType = ExprBuilder.getType(Iterator);
719 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
720 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
721 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
722
723 if (MaxType != ValueLB->getType())
724 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
725 if (MaxType != ValueUB->getType())
726 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
727 if (MaxType != ValueInc->getType())
728 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
729
730 std::vector<Value*> IVS(VectorWidth);
731 IVS[0] = ValueLB;
732
733 for (int i = 1; i < VectorWidth; i++)
734 IVS[i] = Builder.CreateAdd(IVS[i-1], ValueInc, "p_vector_iv");
735
736 isl_id *Annotation = isl_ast_node_get_annotation(For);
737 assert(Annotation && "For statement is not annotated");
738
739 struct IslAstUser *Info = (struct IslAstUser *) isl_id_get_user(Annotation);
740 assert(Info && "For statement annotation does not contain info");
741
742 isl_union_map *Schedule = isl_ast_build_get_schedule(Info->Context);
743 assert(Schedule && "For statement annotation does not contain its schedule");
744
745 IDToValue[IteratorID] = ValueLB;
746
747 switch (isl_ast_node_get_type(Body)) {
748 case isl_ast_node_user:
749 createUserVector(Body, IVS, isl_id_copy(IteratorID),
750 isl_union_map_copy(Schedule));
751 break;
752 case isl_ast_node_block: {
753 isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
754
755 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
756 createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
757 isl_id_copy(IteratorID),
758 isl_union_map_copy(Schedule));
759
760 isl_ast_node_free(Body);
761 isl_ast_node_list_free(List);
762 break;
763 }
764 default:
765 isl_ast_node_dump(Body);
766 llvm_unreachable("Unhandled isl_ast_node in vectorizer");
767 }
768
769 IDToValue.erase(IteratorID);
770 isl_id_free(IteratorID);
771 isl_id_free(Annotation);
772 isl_union_map_free(Schedule);
773
774 isl_ast_node_free(For);
775 isl_ast_expr_free(Iterator);
776}
777
778void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000779 isl_ast_node *Body;
780 isl_ast_expr *Init, *Inc, *Iterator, *UB;
781 isl_id *IteratorID;
782 Value *ValueLB, *ValueUB, *ValueInc;
783 Type *MaxType;
784 BasicBlock *AfterBlock;
785 Value *IV;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000786 CmpInst::Predicate Predicate;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000787
788 Body = isl_ast_node_for_get_body(For);
789
790 // isl_ast_node_for_is_degenerate(For)
791 //
792 // TODO: For degenerated loops we could generate a plain assignment.
793 // However, for now we just reuse the logic for normal loops, which will
794 // create a loop with a single iteration.
795
796 Init = isl_ast_node_for_get_init(For);
797 Inc = isl_ast_node_for_get_inc(For);
798 Iterator = isl_ast_node_for_get_iterator(For);
799 IteratorID = isl_ast_expr_get_id(Iterator);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000800 UB = getUpperBound(For, Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000801
802 ValueLB = ExprBuilder.create(Init);
803 ValueUB = ExprBuilder.create(UB);
804 ValueInc = ExprBuilder.create(Inc);
805
806 MaxType = ExprBuilder.getType(Iterator);
807 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
808 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
809 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
810
811 if (MaxType != ValueLB->getType())
812 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
813 if (MaxType != ValueUB->getType())
814 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
815 if (MaxType != ValueInc->getType())
816 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
817
818 // TODO: In case we can proof a loop is executed at least once, we can
819 // generate the condition iv != UB + stride (consider possible
820 // overflow). This condition will allow LLVM to prove the loop is
821 // executed at least once, which will enable a lot of loop invariant
822 // code motion.
823
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000824 IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, AfterBlock,
825 Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000826 IDToValue[IteratorID] = IV;
827
828 create(Body);
829
830 IDToValue.erase(IteratorID);
831
832 Builder.SetInsertPoint(AfterBlock->begin());
833
834 isl_ast_node_free(For);
835 isl_ast_expr_free(Iterator);
836 isl_id_free(IteratorID);
837}
838
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000839void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
840 bool Vector = PollyVectorizerChoice != VECTORIZER_NONE;
841
842 if (Vector && isInnermostParallel(For)) {
843 int VectorWidth = getNumberOfIterations(For);
844 if (1 < VectorWidth && VectorWidth <= 16) {
845 createForVector(For, VectorWidth);
846 return;
847 }
848 }
849 createForSequential(For);
850}
851
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000852void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
853 isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
854
855 Function *F = Builder.GetInsertBlock()->getParent();
856 LLVMContext &Context = F->getContext();
857
858 BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
859 Builder.GetInsertPoint(), P);
860 CondBB->setName("polly.cond");
861 BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
862 MergeBB->setName("polly.merge");
863 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
864 BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
865
866 DominatorTree &DT = P->getAnalysis<DominatorTree>();
867 DT.addNewBlock(ThenBB, CondBB);
868 DT.addNewBlock(ElseBB, CondBB);
869 DT.changeImmediateDominator(MergeBB, CondBB);
870
871 CondBB->getTerminator()->eraseFromParent();
872
873 Builder.SetInsertPoint(CondBB);
874 Value *Predicate = ExprBuilder.create(Cond);
875 Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
876 Builder.SetInsertPoint(ThenBB);
877 Builder.CreateBr(MergeBB);
878 Builder.SetInsertPoint(ElseBB);
879 Builder.CreateBr(MergeBB);
880 Builder.SetInsertPoint(ThenBB->begin());
881
882 create(isl_ast_node_if_get_then(If));
883
884 Builder.SetInsertPoint(ElseBB->begin());
885
886 if (isl_ast_node_if_has_else(If))
887 create(isl_ast_node_if_get_else(If));
888
889 Builder.SetInsertPoint(MergeBB->begin());
890
891 isl_ast_node_free(If);
892}
893
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000894void IslNodeBuilder::createSubstitutions(__isl_take isl_pw_multi_aff *PMA,
895 __isl_take isl_ast_build *Context,
896 ScopStmt *Stmt, ValueMapT &VMap) {
897 for (unsigned i = 0; i < isl_pw_multi_aff_dim(PMA, isl_dim_out);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000898 ++i) {
899 isl_pw_aff *Aff;
900 isl_ast_expr *Expr;
901 const Value *OldIV;
902 Value *V;
903
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000904 Aff = isl_pw_multi_aff_get_pw_aff(PMA, i);
905 Expr = isl_ast_build_expr_from_pw_aff(Context, Aff);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000906 OldIV = Stmt->getInductionVariableForDimension(i);
907 V = ExprBuilder.create(Expr);
908
909 // CreateIntCast can introduce trunc expressions. This is correct, as the
910 // result will always fit into the type of the original induction variable
911 // (because we calculate a value of the original induction variable).
912 V = Builder.CreateIntCast(V, OldIV->getType(), true);
913 VMap[OldIV] = V;
914 }
915
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000916 isl_pw_multi_aff_free(PMA);
917 isl_ast_build_free(Context);
918}
919
920void IslNodeBuilder::createSubstitutionsVector(__isl_take isl_pw_multi_aff *PMA,
921 __isl_take isl_ast_build *Context, ScopStmt *Stmt, VectorValueMapT &VMap,
922 std::vector<Value*> &IVS, __isl_take isl_id *IteratorID) {
923 int i = 0;
924
925 Value *OldValue = IDToValue[IteratorID];
926 for (std::vector<Value*>::iterator II = IVS.begin(), IE = IVS.end();
927 II != IE; ++II) {
928 IDToValue[IteratorID] = *II;
929 createSubstitutions(isl_pw_multi_aff_copy(PMA),
930 isl_ast_build_copy(Context), Stmt, VMap[i]);
931 i++;
932 }
933
934 IDToValue[IteratorID] = OldValue;
935 isl_id_free(IteratorID);
936 isl_pw_multi_aff_free(PMA);
937 isl_ast_build_free(Context);
938}
939
940void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
941 ValueMapT VMap;
942 struct IslAstUser *Info;
943 isl_id *Annotation, *Id;
944 ScopStmt *Stmt;
945
946 Annotation = isl_ast_node_get_annotation(User);
947 assert(Annotation && "Scalar user statement is not annotated");
948
949 Info = (struct IslAstUser *) isl_id_get_user(Annotation);
950 assert(Info && "Scalar user statement annotation does not contain info");
951
952 Id = isl_pw_multi_aff_get_tuple_id(Info->PMA, isl_dim_out);
953 Stmt = (ScopStmt *) isl_id_get_user(Id);
954
955 createSubstitutions(isl_pw_multi_aff_copy(Info->PMA),
956 isl_ast_build_copy(Info->Context), Stmt, VMap);
957
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000958 BlockGenerator::generate(Builder, *Stmt, VMap, P);
959
960 isl_ast_node_free(User);
961 isl_id_free(Annotation);
962 isl_id_free(Id);
963}
964
965void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
966 isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
967
968 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
969 create(isl_ast_node_list_get_ast_node(List, i));
970
971 isl_ast_node_free(Block);
972 isl_ast_node_list_free(List);
973}
974
975void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
976 switch (isl_ast_node_get_type(Node)) {
977 case isl_ast_node_error:
978 llvm_unreachable("code generation error");
979 case isl_ast_node_for:
980 createFor(Node);
981 return;
982 case isl_ast_node_if:
983 createIf(Node);
984 return;
985 case isl_ast_node_user:
986 createUser(Node);
987 return;
988 case isl_ast_node_block:
989 createBlock(Node);
990 return;
991 }
992
993 llvm_unreachable("Unknown isl_ast_node type");
994}
995
996void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
997 SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly");
998
999 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
1000 isl_id *Id;
1001 const SCEV *Scev;
1002 IntegerType *T;
1003 Instruction *InsertLocation;
1004
1005 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
1006 Scev = (const SCEV*) isl_id_get_user(Id);
1007 T = dyn_cast<IntegerType>(Scev->getType());
1008 InsertLocation = --(Builder.GetInsertBlock()->end());
1009 Value *V = Rewriter.expandCodeFor(Scev, T, InsertLocation);
1010 IDToValue[Id] = V;
1011
1012 isl_id_free(Id);
1013 }
1014
1015 isl_set_free(Context);
1016}
1017
1018namespace {
1019class IslCodeGeneration : public ScopPass {
1020 public:
1021 static char ID;
1022
1023 IslCodeGeneration() : ScopPass(ID) {}
1024
1025 bool runOnScop(Scop &S) {
1026 IslAstInfo &AstInfo = getAnalysis<IslAstInfo>();
1027 assert(S.getRegion().isSimple() && "Only simple regions are supported");
1028
1029 BasicBlock *StartBlock = executeScopConditionally(S, this);
1030 isl_ast_node *Ast = AstInfo.getAst();
1031 IRBuilder<> Builder(StartBlock->begin());
1032
1033 IslNodeBuilder NodeBuilder(Builder, this);
1034 NodeBuilder.addParameters(S.getContext());
1035 NodeBuilder.create(Ast);
1036 return true;
1037 }
1038
1039 virtual void printScop(raw_ostream &OS) const {
1040 }
1041
1042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1043 AU.addRequired<DominatorTree>();
1044 AU.addRequired<IslAstInfo>();
1045 AU.addRequired<RegionInfo>();
1046 AU.addRequired<ScalarEvolution>();
1047 AU.addRequired<ScopDetection>();
1048 AU.addRequired<ScopInfo>();
1049
1050 AU.addPreserved<Dependences>();
1051
1052 // FIXME: We do not create LoopInfo for the newly generated loops.
1053 AU.addPreserved<LoopInfo>();
1054 AU.addPreserved<DominatorTree>();
1055 AU.addPreserved<IslAstInfo>();
1056 AU.addPreserved<ScopDetection>();
1057 AU.addPreserved<ScalarEvolution>();
1058
1059 // FIXME: We do not yet add regions for the newly generated code to the
1060 // region tree.
1061 AU.addPreserved<RegionInfo>();
1062 AU.addPreserved<TempScopInfo>();
1063 AU.addPreserved<ScopInfo>();
1064 AU.addPreservedID(IndependentBlocksID);
1065 }
1066};
1067}
1068
1069char IslCodeGeneration::ID = 1;
1070
1071INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl",
1072 "Polly - Create LLVM-IR from SCoPs", false, false)
1073INITIALIZE_PASS_DEPENDENCY(Dependences)
1074INITIALIZE_PASS_DEPENDENCY(DominatorTree)
1075INITIALIZE_PASS_DEPENDENCY(LoopInfo)
1076INITIALIZE_PASS_DEPENDENCY(RegionInfo)
1077INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
1078INITIALIZE_PASS_DEPENDENCY(ScopDetection)
1079INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl",
1080 "Polly - Create LLVM-IR from SCoPs", false, false)
1081
1082Pass *polly::createIslCodeGenerationPass() {
1083 return new IslCodeGeneration();
1084}