blob: ab8fb8bef5afe681d4fea3ebb28dd6320222c9d1 [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"
Tobias Grosser83628182013-05-07 08:11:54 +000022#include "polly/CodeGen/BlockGenerators.h"
23#include "polly/CodeGen/CodeGeneration.h"
24#include "polly/CodeGen/IslAst.h"
25#include "polly/CodeGen/LoopGenerators.h"
26#include "polly/CodeGen/Utils.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000027#include "polly/Dependences.h"
28#include "polly/LinkAllPasses.h"
29#include "polly/ScopInfo.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000030#include "polly/Support/GICHelper.h"
Tobias Grosser0ee50f62013-04-10 06:55:31 +000031#include "polly/Support/ScopHelper.h"
Tobias Grosser83628182013-05-07 08:11:54 +000032#include "polly/TempScopInfo.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000033#include "llvm/Analysis/LoopInfo.h"
34#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser83628182013-05-07 08:11:54 +000035#include "llvm/IR/Module.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
Chandler Carruth535d52c2013-01-02 11:47:44 +000038#include "llvm/IR/DataLayout.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000039#include "llvm/Transforms/Utils/BasicBlockUtils.h"
40
41#include "isl/union_map.h"
42#include "isl/list.h"
43#include "isl/ast.h"
44#include "isl/ast_build.h"
45#include "isl/set.h"
46#include "isl/map.h"
47#include "isl/aff.h"
48
49#include <map>
50
51using namespace polly;
52using namespace llvm;
53
Chandler Carruth95fef942014-04-22 03:30:19 +000054#define DEBUG_TYPE "polly-codegen-isl"
55
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000056/// @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:
Tobias Grosser5103ba72014-03-04 14:58:49 +000062 RuntimeDebugBuilder(PollyIRBuilder &Builder) : Builder(Builder) {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000063
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:
Tobias Grosser5103ba72014-03-04 14:58:49 +000075 PollyIRBuilder &Builder;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000076
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());
Tobias Grosser58032cb2013-06-23 01:29:29 +0000129 (void)Ty;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000130 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
Tobias Grosser9818bc82014-04-22 14:26:51 +0000139/// @brief LLVM-IR generator for isl_ast_expr[essions]
140///
141/// This generator generates LLVM-IR that performs the computation described by
142/// an isl_ast_expr[ession].
143///
144/// Example:
145///
146/// An isl_ast_expr[ession] can look like this:
147///
148/// (N + M) + 10
149///
150/// The IslExprBuilder could create the following LLVM-IR:
151///
152/// %tmp1 = add nsw i64 %N
153/// %tmp2 = add nsw i64 %tmp1, %M
154/// %tmp3 = add nsw i64 %tmp2, 10
155///
156/// The implementation of this class is mostly a mapping from isl_ast_expr
157/// constructs to the corresponding LLVM-IR constructs.
158///
159/// The following decisions may need some explanation:
160///
161/// 1) Which data-type to choose
162///
163/// isl_ast_expr[essions] are untyped expressions that assume arbitrary
164/// precision integer computations. LLVM-IR instead has fixed size integers.
165/// When lowering to LLVM-IR we need to chose both the size of the data type and
166/// the sign of the operations we use.
167///
168/// At the moment, we hardcode i64 bit signed computations. Our experience has
169/// shown that 64 bit are generally large enough for the loop bounds that appear
170/// in the wild. Signed computations are needed, as loop bounds may become
171/// negative.
172///
173/// FIXME: Hardcoding sizes can cause issues:
174///
175/// a) Certain run-time checks that we may want to generate can involve the
176/// size of the data types the computation is performed on. When code
177/// generating these run-time checks to isl_ast_expr[essions], the
178/// resulting computation may require more than 64 bit.
179///
180/// b) On embedded systems and especially for high-level-synthesis 64 bit
181/// computations are very costly.
182///
183/// The right approach is to compute the minimal necessary bitwidth and
184/// signedness for each subexpression during in the isl AST generation and
185/// to use this information in our IslAstGenerator. Preliminary patches are
186/// available, but have not been committed yet.
187///
188/// 2) We always flag computations with 'nsw'
189///
190/// As isl_ast_expr[essions] assume arbitrary precision, no wrapping should
191/// ever occur in the generated LLVM-IR (assuming the data type chosen is large
192/// enough).
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000193class IslExprBuilder {
194public:
Tobias Grosser9818bc82014-04-22 14:26:51 +0000195 /// @brief Construct an IslExprBuilder.
196 ///
197 /// @param Builder The IRBuilder used to construct the isl_ast_expr[ession].
198 /// The insert location of this IRBuilder defines WHERE the
199 /// corresponding LLVM-IR is generated.
200 ///
201 /// @param IDToValue The isl_ast_expr[ession] may reference parameters or
202 /// variables (identified by an isl_id). The IDTOValue map
203 /// specifies the LLVM-IR Values that correspond to these
204 /// parameters and variables.
Tobias Grosser5103ba72014-03-04 14:58:49 +0000205 IslExprBuilder(PollyIRBuilder &Builder,
Tobias Grosser9818bc82014-04-22 14:26:51 +0000206 std::map<isl_id *, Value *> &IDToValue)
Tobias Grosser7242ad92013-02-22 08:07:06 +0000207 : Builder(Builder), IDToValue(IDToValue) {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000208
Tobias Grosser9818bc82014-04-22 14:26:51 +0000209 /// @brief Create LLVM-IR for an isl_ast_expr[ession].
210 ///
211 /// @param Expr The ast expression for which we generate LLVM-IR.
212 ///
213 /// @return The llvm::Value* containing the result of the computation.
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000214 Value *create(__isl_take isl_ast_expr *Expr);
Tobias Grosser9818bc82014-04-22 14:26:51 +0000215
216 /// @brief Return the largest of two types.
217 ///
218 /// @param T1 The first type.
219 /// @param T2 The second type.
220 ///
221 /// @return The largest of the two types.
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000222 Type *getWidestType(Type *T1, Type *T2);
Tobias Grosser9818bc82014-04-22 14:26:51 +0000223
224 /// @brief Return the type with which this expression should be computed.
225 ///
226 /// The type needs to be large enough to hold all possible input and all
227 /// possible output values.
228 ///
229 /// @param Expr The expression for which to find the type.
230 /// @return The type with which the expression should be computed.
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000231 IntegerType *getType(__isl_keep isl_ast_expr *Expr);
232
233private:
Tobias Grosser5103ba72014-03-04 14:58:49 +0000234 PollyIRBuilder &Builder;
Tobias Grosserc14582f2013-02-05 18:01:29 +0000235 std::map<isl_id *, Value *> &IDToValue;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000236
237 Value *createOp(__isl_take isl_ast_expr *Expr);
238 Value *createOpUnary(__isl_take isl_ast_expr *Expr);
239 Value *createOpBin(__isl_take isl_ast_expr *Expr);
240 Value *createOpNAry(__isl_take isl_ast_expr *Expr);
241 Value *createOpSelect(__isl_take isl_ast_expr *Expr);
242 Value *createOpICmp(__isl_take isl_ast_expr *Expr);
243 Value *createOpBoolean(__isl_take isl_ast_expr *Expr);
244 Value *createId(__isl_take isl_ast_expr *Expr);
245 Value *createInt(__isl_take isl_ast_expr *Expr);
246};
247
248Type *IslExprBuilder::getWidestType(Type *T1, Type *T2) {
249 assert(isa<IntegerType>(T1) && isa<IntegerType>(T2));
250
251 if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits())
252 return T2;
253 else
254 return T1;
255}
256
257Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000258 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus &&
259 "Unsupported unary operation");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000260
261 Value *V;
262 Type *MaxType = getType(Expr);
263
264 V = create(isl_ast_expr_get_op_arg(Expr, 0));
265 MaxType = getWidestType(MaxType, V->getType());
266
267 if (MaxType != V->getType())
268 V = Builder.CreateSExt(V, MaxType);
269
270 isl_ast_expr_free(Expr);
271 return Builder.CreateNSWNeg(V);
272}
273
274Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000275 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
276 "isl ast expression not of type isl_ast_op");
277 assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 &&
278 "We need at least two operands in an n-ary operation");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000279
280 Value *V;
281
282 V = create(isl_ast_expr_get_op_arg(Expr, 0));
283
284 for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr); ++i) {
285 Value *OpV;
286 OpV = create(isl_ast_expr_get_op_arg(Expr, i));
287
288 Type *Ty = getWidestType(V->getType(), OpV->getType());
289
290 if (Ty != OpV->getType())
291 OpV = Builder.CreateSExt(OpV, Ty);
292
293 if (Ty != V->getType())
294 V = Builder.CreateSExt(V, Ty);
295
296 switch (isl_ast_expr_get_op_type(Expr)) {
297 default:
298 llvm_unreachable("This is no n-ary isl ast expression");
299
Tobias Grosserc14582f2013-02-05 18:01:29 +0000300 case isl_ast_op_max: {
301 Value *Cmp = Builder.CreateICmpSGT(V, OpV);
302 V = Builder.CreateSelect(Cmp, V, OpV);
303 continue;
304 }
305 case isl_ast_op_min: {
306 Value *Cmp = Builder.CreateICmpSLT(V, OpV);
307 V = Builder.CreateSelect(Cmp, V, OpV);
308 continue;
309 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000310 }
311 }
312
313 // TODO: We can truncate the result, if it fits into a smaller type. This can
314 // help in cases where we have larger operands (e.g. i67) but the result is
315 // known to fit into i64. Without the truncation, the larger i67 type may
316 // force all subsequent operations to be performed on a non-native type.
317 isl_ast_expr_free(Expr);
318 return V;
319}
320
321Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) {
322 Value *LHS, *RHS, *Res;
323 Type *MaxType;
324 isl_ast_op_type OpType;
325
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000326 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
327 "isl ast expression not of type isl_ast_op");
328 assert(isl_ast_expr_get_op_n_arg(Expr) == 2 &&
329 "not a binary isl ast expression");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000330
331 OpType = isl_ast_expr_get_op_type(Expr);
332
333 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
334 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
335
336 MaxType = LHS->getType();
337 MaxType = getWidestType(MaxType, RHS->getType());
338
339 // Take the result into account when calculating the widest type.
340 //
341 // For operations such as '+' the result may require a type larger than
342 // the type of the individual operands. For other operations such as '/', the
343 // result type cannot be larger than the type of the individual operand. isl
344 // does not calculate correct types for these operations and we consequently
345 // exclude those operations here.
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000346 switch (OpType) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000347 case isl_ast_op_pdiv_q:
348 case isl_ast_op_pdiv_r:
349 case isl_ast_op_div:
350 case isl_ast_op_fdiv_q:
351 // Do nothing
352 break;
353 case isl_ast_op_add:
354 case isl_ast_op_sub:
355 case isl_ast_op_mul:
356 MaxType = getWidestType(MaxType, getType(Expr));
357 break;
358 default:
359 llvm_unreachable("This is no binary isl ast expression");
360 }
361
362 if (MaxType != RHS->getType())
363 RHS = Builder.CreateSExt(RHS, MaxType);
364
365 if (MaxType != LHS->getType())
366 LHS = Builder.CreateSExt(LHS, MaxType);
367
368 switch (OpType) {
369 default:
370 llvm_unreachable("This is no binary isl ast expression");
371 case isl_ast_op_add:
372 Res = Builder.CreateNSWAdd(LHS, RHS);
373 break;
374 case isl_ast_op_sub:
375 Res = Builder.CreateNSWSub(LHS, RHS);
376 break;
377 case isl_ast_op_mul:
378 Res = Builder.CreateNSWMul(LHS, RHS);
379 break;
380 case isl_ast_op_div:
381 case isl_ast_op_pdiv_q: // Dividend is non-negative
382 Res = Builder.CreateSDiv(LHS, RHS);
383 break;
Tobias Grosserc14582f2013-02-05 18:01:29 +0000384 case isl_ast_op_fdiv_q: { // Round towards -infty
385 // TODO: Review code and check that this calculation does not yield
386 // incorrect overflow in some bordercases.
387 //
388 // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
389 Value *One = ConstantInt::get(MaxType, 1);
390 Value *Zero = ConstantInt::get(MaxType, 0);
391 Value *Sum1 = Builder.CreateSub(LHS, RHS);
392 Value *Sum2 = Builder.CreateAdd(Sum1, One);
393 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
394 Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS);
395 Res = Builder.CreateSDiv(Dividend, RHS);
396 break;
397 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000398 case isl_ast_op_pdiv_r: // Dividend is non-negative
399 Res = Builder.CreateSRem(LHS, RHS);
400 break;
401 }
402
403 // TODO: We can truncate the result, if it fits into a smaller type. This can
404 // help in cases where we have larger operands (e.g. i67) but the result is
405 // known to fit into i64. Without the truncation, the larger i67 type may
406 // force all subsequent operations to be performed on a non-native type.
407 isl_ast_expr_free(Expr);
408 return Res;
409}
410
411Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000412 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_select &&
413 "Unsupported unary isl ast expression");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000414 Value *LHS, *RHS, *Cond;
415 Type *MaxType = getType(Expr);
416
417 Cond = create(isl_ast_expr_get_op_arg(Expr, 0));
418
419 LHS = create(isl_ast_expr_get_op_arg(Expr, 1));
420 RHS = create(isl_ast_expr_get_op_arg(Expr, 2));
421
422 MaxType = getWidestType(MaxType, LHS->getType());
423 MaxType = getWidestType(MaxType, RHS->getType());
424
425 if (MaxType != RHS->getType())
426 RHS = Builder.CreateSExt(RHS, MaxType);
427
428 if (MaxType != LHS->getType())
429 LHS = Builder.CreateSExt(LHS, MaxType);
430
431 // TODO: Do we want to truncate the result?
432 isl_ast_expr_free(Expr);
433 return Builder.CreateSelect(Cond, LHS, RHS);
434}
435
436Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) {
437 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
438 "Expected an isl_ast_expr_op expression");
439
440 Value *LHS, *RHS, *Res;
441
442 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
443 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
444
445 Type *MaxType = LHS->getType();
446 MaxType = getWidestType(MaxType, RHS->getType());
447
448 if (MaxType != RHS->getType())
449 RHS = Builder.CreateSExt(RHS, MaxType);
450
451 if (MaxType != LHS->getType())
452 LHS = Builder.CreateSExt(LHS, MaxType);
453
454 switch (isl_ast_expr_get_op_type(Expr)) {
455 default:
456 llvm_unreachable("Unsupported ICmp isl ast expression");
457 case isl_ast_op_eq:
458 Res = Builder.CreateICmpEQ(LHS, RHS);
459 break;
460 case isl_ast_op_le:
461 Res = Builder.CreateICmpSLE(LHS, RHS);
462 break;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000463 case isl_ast_op_lt:
464 Res = Builder.CreateICmpSLT(LHS, RHS);
465 break;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000466 case isl_ast_op_ge:
467 Res = Builder.CreateICmpSGE(LHS, RHS);
468 break;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000469 case isl_ast_op_gt:
470 Res = Builder.CreateICmpSGT(LHS, RHS);
471 break;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000472 }
473
474 isl_ast_expr_free(Expr);
475 return Res;
476}
477
478Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) {
479 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
480 "Expected an isl_ast_expr_op expression");
481
482 Value *LHS, *RHS, *Res;
483 isl_ast_op_type OpType;
484
485 OpType = isl_ast_expr_get_op_type(Expr);
486
487 assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) &&
488 "Unsupported isl_ast_op_type");
489
490 LHS = create(isl_ast_expr_get_op_arg(Expr, 0));
491 RHS = create(isl_ast_expr_get_op_arg(Expr, 1));
492
493 // Even though the isl pretty printer prints the expressions as 'exp && exp'
494 // or 'exp || exp', we actually code generate the bitwise expressions
495 // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches,
496 // but it is, due to the use of i1 types, otherwise equivalent. The reason
497 // to go for bitwise operations is, that we assume the reduced control flow
498 // will outweight the overhead introduced by evaluating unneeded expressions.
499 // The isl code generation currently does not take advantage of the fact that
500 // the expression after an '||' or '&&' is in some cases not evaluated.
501 // Evaluating it anyways does not cause any undefined behaviour.
502 //
503 // TODO: Document in isl itself, that the unconditionally evaluating the
504 // second part of '||' or '&&' expressions is safe.
505 assert(LHS->getType() == Builder.getInt1Ty() && "Expected i1 type");
506 assert(RHS->getType() == Builder.getInt1Ty() && "Expected i1 type");
507
508 switch (OpType) {
509 default:
510 llvm_unreachable("Unsupported boolean expression");
511 case isl_ast_op_and:
512 Res = Builder.CreateAnd(LHS, RHS);
513 break;
514 case isl_ast_op_or:
515 Res = Builder.CreateOr(LHS, RHS);
516 break;
517 }
518
519 isl_ast_expr_free(Expr);
520 return Res;
521}
522
523Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000524 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
525 "Expression not of type isl_ast_expr_op");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000526 switch (isl_ast_expr_get_op_type(Expr)) {
527 case isl_ast_op_error:
528 case isl_ast_op_cond:
529 case isl_ast_op_and_then:
530 case isl_ast_op_or_else:
531 case isl_ast_op_call:
Tobias Grossera38c9242014-01-26 19:36:28 +0000532 case isl_ast_op_member:
533 case isl_ast_op_access:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000534 llvm_unreachable("Unsupported isl ast expression");
535 case isl_ast_op_max:
536 case isl_ast_op_min:
537 return createOpNAry(Expr);
538 case isl_ast_op_add:
539 case isl_ast_op_sub:
540 case isl_ast_op_mul:
541 case isl_ast_op_div:
542 case isl_ast_op_fdiv_q: // Round towards -infty
543 case isl_ast_op_pdiv_q: // Dividend is non-negative
544 case isl_ast_op_pdiv_r: // Dividend is non-negative
545 return createOpBin(Expr);
546 case isl_ast_op_minus:
547 return createOpUnary(Expr);
548 case isl_ast_op_select:
549 return createOpSelect(Expr);
550 case isl_ast_op_and:
551 case isl_ast_op_or:
552 return createOpBoolean(Expr);
553 case isl_ast_op_eq:
554 case isl_ast_op_le:
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000555 case isl_ast_op_lt:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000556 case isl_ast_op_ge:
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000557 case isl_ast_op_gt:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000558 return createOpICmp(Expr);
559 }
560
561 llvm_unreachable("Unsupported isl_ast_expr_op kind.");
562}
563
564Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000565 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id &&
566 "Expression not of type isl_ast_expr_ident");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000567
568 isl_id *Id;
569 Value *V;
570
571 Id = isl_ast_expr_get_id(Expr);
572
573 assert(IDToValue.count(Id) && "Identifier not found");
574
575 V = IDToValue[Id];
576
577 isl_id_free(Id);
578 isl_ast_expr_free(Expr);
579
580 return V;
581}
582
583IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) {
584 // XXX: We assume i64 is large enough. This is often true, but in general
585 // incorrect. Also, on 32bit architectures, it would be beneficial to
586 // use a smaller type. We can and should directly derive this information
587 // during code generation.
588 return IntegerType::get(Builder.getContext(), 64);
589}
590
591Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) {
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000592 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int &&
593 "Expression not of type isl_ast_expr_int");
Tobias Grosseredab1352013-06-21 06:41:31 +0000594 isl_val *Val;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000595 Value *V;
596 APInt APValue;
597 IntegerType *T;
598
Tobias Grosseredab1352013-06-21 06:41:31 +0000599 Val = isl_ast_expr_get_val(Expr);
600 APValue = APIntFromVal(Val);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000601 T = getType(Expr);
602 APValue = APValue.sextOrSelf(T->getBitWidth());
603 V = ConstantInt::get(T, APValue);
604
605 isl_ast_expr_free(Expr);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000606 return V;
607}
608
609Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) {
610 switch (isl_ast_expr_get_type(Expr)) {
611 case isl_ast_expr_error:
612 llvm_unreachable("Code generation error");
613 case isl_ast_expr_op:
614 return createOp(Expr);
615 case isl_ast_expr_id:
616 return createId(Expr);
617 case isl_ast_expr_int:
618 return createInt(Expr);
619 }
620
621 llvm_unreachable("Unexpected enum value");
622}
623
624class IslNodeBuilder {
625public:
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000626 IslNodeBuilder(PollyIRBuilder &Builder, LoopAnnotator &Annotator, Pass *P)
Tobias Grosser34c87872014-04-22 16:39:41 +0000627 : Builder(Builder), Annotator(Annotator), ExprBuilder(Builder, IDToValue),
628 P(P) {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000629
630 void addParameters(__isl_take isl_set *Context);
631 void create(__isl_take isl_ast_node *Node);
Tobias Grosser54ee0ba2013-11-17 03:18:25 +0000632 IslExprBuilder &getExprBuilder() { return ExprBuilder; }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000633
634private:
Tobias Grosser5103ba72014-03-04 14:58:49 +0000635 PollyIRBuilder &Builder;
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000636 LoopAnnotator &Annotator;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000637 IslExprBuilder ExprBuilder;
638 Pass *P;
639
640 // This maps an isl_id* to the Value* it has in the generated program. For now
641 // on, the only isl_ids that are stored here are the newly calculated loop
642 // ivs.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000643 std::map<isl_id *, Value *> IDToValue;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000644
645 // Extract the upper bound of this loop
646 //
647 // The isl code generation can generate arbitrary expressions to check if the
648 // upper bound of a loop is reached, but it provides an option to enforce
649 // 'atomic' upper bounds. An 'atomic upper bound is always of the form
650 // iv <= expr, where expr is an (arbitrary) expression not containing iv.
651 //
652 // This function extracts 'atomic' upper bounds. Polly, in general, requires
653 // atomic upper bounds for the following reasons:
654 //
655 // 1. An atomic upper bound is loop invariant
656 //
657 // It must not be calculated at each loop iteration and can often even be
658 // hoisted out further by the loop invariant code motion.
659 //
660 // 2. OpenMP needs a loop invarient upper bound to calculate the number
661 // of loop iterations.
662 //
663 // 3. With the existing code, upper bounds have been easier to implement.
Tobias Grossere602a072013-05-07 07:30:56 +0000664 __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For,
665 CmpInst::Predicate &Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000666
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000667 unsigned getNumberOfIterations(__isl_keep isl_ast_node *For);
668
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000669 void createFor(__isl_take isl_ast_node *For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000670 void createForVector(__isl_take isl_ast_node *For, int VectorWidth);
671 void createForSequential(__isl_take isl_ast_node *For);
672 void createSubstitutions(__isl_take isl_pw_multi_aff *PMA,
Tobias Grosserc14582f2013-02-05 18:01:29 +0000673 __isl_take isl_ast_build *Context, ScopStmt *Stmt,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000674 ValueMapT &VMap, LoopToScevMapT &LTS);
Tobias Grossere602a072013-05-07 07:30:56 +0000675 void createSubstitutionsVector(__isl_take isl_pw_multi_aff *PMA,
676 __isl_take isl_ast_build *Context,
677 ScopStmt *Stmt, VectorValueMapT &VMap,
678 std::vector<LoopToScevMapT> &VLTS,
679 std::vector<Value *> &IVS,
680 __isl_take isl_id *IteratorID);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000681 void createIf(__isl_take isl_ast_node *If);
Tobias Grossere602a072013-05-07 07:30:56 +0000682 void createUserVector(__isl_take isl_ast_node *User,
683 std::vector<Value *> &IVS,
684 __isl_take isl_id *IteratorID,
685 __isl_take isl_union_map *Schedule);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000686 void createUser(__isl_take isl_ast_node *User);
687 void createBlock(__isl_take isl_ast_node *Block);
688};
689
Tobias Grossere602a072013-05-07 07:30:56 +0000690__isl_give isl_ast_expr *
691IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For,
692 ICmpInst::Predicate &Predicate) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000693 isl_id *UBID, *IteratorID;
694 isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000695 isl_ast_op_type Type;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000696
697 Cond = isl_ast_node_for_get_cond(For);
698 Iterator = isl_ast_node_for_get_iterator(For);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000699 Type = isl_ast_expr_get_op_type(Cond);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000700
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000701 assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op &&
702 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000703
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000704 switch (Type) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000705 case isl_ast_op_le:
706 Predicate = ICmpInst::ICMP_SLE;
707 break;
708 case isl_ast_op_lt:
709 Predicate = ICmpInst::ICMP_SLT;
710 break;
711 default:
712 llvm_unreachable("Unexpected comparision type in loop conditon");
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000713 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000714
715 Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
716
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000717 assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id &&
718 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000719
720 UBID = isl_ast_expr_get_id(Arg0);
721
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000722 assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id &&
723 "Could not get the iterator");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000724
725 IteratorID = isl_ast_expr_get_id(Iterator);
726
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000727 assert(UBID == IteratorID &&
728 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000729
730 UB = isl_ast_expr_get_op_arg(Cond, 1);
731
732 isl_ast_expr_free(Cond);
733 isl_ast_expr_free(Iterator);
734 isl_ast_expr_free(Arg0);
735 isl_id_free(IteratorID);
736 isl_id_free(UBID);
737
738 return UB;
739}
740
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000741unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) {
742 isl_id *Annotation = isl_ast_node_get_annotation(For);
743 if (!Annotation)
744 return -1;
745
Tobias Grosserc14582f2013-02-05 18:01:29 +0000746 struct IslAstUser *Info = (struct IslAstUser *)isl_id_get_user(Annotation);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000747 if (!Info) {
748 isl_id_free(Annotation);
749 return -1;
750 }
751
752 isl_union_map *Schedule = isl_ast_build_get_schedule(Info->Context);
753 isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule));
754 isl_id_free(Annotation);
Sebastian Pop2aa5c242012-12-18 08:56:51 +0000755 int NumberOfIterations = polly::getNumberOfIterations(LoopDomain);
756 if (NumberOfIterations == -1)
757 return -1;
758 return NumberOfIterations + 1;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000759}
760
Tobias Grossere602a072013-05-07 07:30:56 +0000761void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User,
762 std::vector<Value *> &IVS,
763 __isl_take isl_id *IteratorID,
764 __isl_take isl_union_map *Schedule) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000765 isl_id *Annotation = isl_ast_node_get_annotation(User);
766 assert(Annotation && "Vector user statement is not annotated");
767
Tobias Grosserc14582f2013-02-05 18:01:29 +0000768 struct IslAstUser *Info = (struct IslAstUser *)isl_id_get_user(Annotation);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000769 assert(Info && "Vector user statement annotation does not contain info");
770
771 isl_id *Id = isl_pw_multi_aff_get_tuple_id(Info->PMA, isl_dim_out);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000772 ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000773 VectorValueMapT VectorMap(IVS.size());
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000774 std::vector<LoopToScevMapT> VLTS(IVS.size());
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000775
776 isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain());
777 Schedule = isl_union_map_intersect_domain(Schedule, Domain);
778 isl_map *S = isl_map_from_union_map(Schedule);
779
780 createSubstitutionsVector(isl_pw_multi_aff_copy(Info->PMA),
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000781 isl_ast_build_copy(Info->Context), Stmt, VectorMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000782 VLTS, IVS, IteratorID);
783 VectorBlockGenerator::generate(Builder, *Stmt, VectorMap, VLTS, S, P);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000784
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000785 isl_map_free(S);
786 isl_id_free(Annotation);
787 isl_id_free(Id);
788 isl_ast_node_free(User);
789}
790
Tobias Grossere602a072013-05-07 07:30:56 +0000791void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
792 int VectorWidth) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000793 isl_ast_node *Body = isl_ast_node_for_get_body(For);
794 isl_ast_expr *Init = isl_ast_node_for_get_init(For);
795 isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
796 isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
797 isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000798
799 Value *ValueLB = ExprBuilder.create(Init);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000800 Value *ValueInc = ExprBuilder.create(Inc);
801
802 Type *MaxType = ExprBuilder.getType(Iterator);
803 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000804 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
805
806 if (MaxType != ValueLB->getType())
807 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000808 if (MaxType != ValueInc->getType())
809 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
810
Tobias Grosserc14582f2013-02-05 18:01:29 +0000811 std::vector<Value *> IVS(VectorWidth);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000812 IVS[0] = ValueLB;
813
814 for (int i = 1; i < VectorWidth; i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000815 IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv");
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000816
817 isl_id *Annotation = isl_ast_node_get_annotation(For);
818 assert(Annotation && "For statement is not annotated");
819
Tobias Grosserc14582f2013-02-05 18:01:29 +0000820 struct IslAstUser *Info = (struct IslAstUser *)isl_id_get_user(Annotation);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000821 assert(Info && "For statement annotation does not contain info");
822
823 isl_union_map *Schedule = isl_ast_build_get_schedule(Info->Context);
824 assert(Schedule && "For statement annotation does not contain its schedule");
825
826 IDToValue[IteratorID] = ValueLB;
827
828 switch (isl_ast_node_get_type(Body)) {
829 case isl_ast_node_user:
830 createUserVector(Body, IVS, isl_id_copy(IteratorID),
831 isl_union_map_copy(Schedule));
832 break;
833 case isl_ast_node_block: {
834 isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
835
836 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
837 createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000838 isl_id_copy(IteratorID), isl_union_map_copy(Schedule));
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000839
840 isl_ast_node_free(Body);
841 isl_ast_node_list_free(List);
842 break;
843 }
844 default:
845 isl_ast_node_dump(Body);
846 llvm_unreachable("Unhandled isl_ast_node in vectorizer");
847 }
848
849 IDToValue.erase(IteratorID);
850 isl_id_free(IteratorID);
851 isl_id_free(Annotation);
852 isl_union_map_free(Schedule);
853
854 isl_ast_node_free(For);
855 isl_ast_expr_free(Iterator);
856}
857
858void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000859 isl_ast_node *Body;
860 isl_ast_expr *Init, *Inc, *Iterator, *UB;
861 isl_id *IteratorID;
862 Value *ValueLB, *ValueUB, *ValueInc;
863 Type *MaxType;
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000864 BasicBlock *ExitBlock;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000865 Value *IV;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000866 CmpInst::Predicate Predicate;
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000867 bool Parallel;
868
869 Parallel = isInnermostParallel(For);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000870
871 Body = isl_ast_node_for_get_body(For);
872
873 // isl_ast_node_for_is_degenerate(For)
874 //
875 // TODO: For degenerated loops we could generate a plain assignment.
876 // However, for now we just reuse the logic for normal loops, which will
877 // create a loop with a single iteration.
878
879 Init = isl_ast_node_for_get_init(For);
880 Inc = isl_ast_node_for_get_inc(For);
881 Iterator = isl_ast_node_for_get_iterator(For);
882 IteratorID = isl_ast_expr_get_id(Iterator);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000883 UB = getUpperBound(For, Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000884
885 ValueLB = ExprBuilder.create(Init);
886 ValueUB = ExprBuilder.create(UB);
887 ValueInc = ExprBuilder.create(Inc);
888
889 MaxType = ExprBuilder.getType(Iterator);
890 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
891 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
892 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
893
894 if (MaxType != ValueLB->getType())
895 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
896 if (MaxType != ValueUB->getType())
897 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
898 if (MaxType != ValueInc->getType())
899 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
900
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000901 IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, ExitBlock, Predicate,
902 &Annotator, Parallel);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000903 IDToValue[IteratorID] = IV;
904
905 create(Body);
906
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000907 Annotator.End();
908
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000909 IDToValue.erase(IteratorID);
910
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000911 Builder.SetInsertPoint(ExitBlock->begin());
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000912
913 isl_ast_node_free(For);
914 isl_ast_expr_free(Iterator);
915 isl_id_free(IteratorID);
916}
917
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000918void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
919 bool Vector = PollyVectorizerChoice != VECTORIZER_NONE;
920
921 if (Vector && isInnermostParallel(For)) {
922 int VectorWidth = getNumberOfIterations(For);
923 if (1 < VectorWidth && VectorWidth <= 16) {
924 createForVector(For, VectorWidth);
925 return;
926 }
927 }
928 createForSequential(For);
929}
930
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000931void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
932 isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
933
934 Function *F = Builder.GetInsertBlock()->getParent();
935 LLVMContext &Context = F->getContext();
936
Tobias Grosserc14582f2013-02-05 18:01:29 +0000937 BasicBlock *CondBB =
938 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000939 CondBB->setName("polly.cond");
940 BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
941 MergeBB->setName("polly.merge");
942 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
943 BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
944
Tobias Grosser42aff302014-01-13 22:29:56 +0000945 DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000946 DT.addNewBlock(ThenBB, CondBB);
947 DT.addNewBlock(ElseBB, CondBB);
948 DT.changeImmediateDominator(MergeBB, CondBB);
949
Tobias Grosser3081b0f2013-05-16 06:40:24 +0000950 LoopInfo &LI = P->getAnalysis<LoopInfo>();
951 Loop *L = LI.getLoopFor(CondBB);
952 if (L) {
953 L->addBasicBlockToLoop(ThenBB, LI.getBase());
954 L->addBasicBlockToLoop(ElseBB, LI.getBase());
955 }
956
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000957 CondBB->getTerminator()->eraseFromParent();
958
959 Builder.SetInsertPoint(CondBB);
960 Value *Predicate = ExprBuilder.create(Cond);
961 Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
962 Builder.SetInsertPoint(ThenBB);
963 Builder.CreateBr(MergeBB);
964 Builder.SetInsertPoint(ElseBB);
965 Builder.CreateBr(MergeBB);
966 Builder.SetInsertPoint(ThenBB->begin());
967
968 create(isl_ast_node_if_get_then(If));
969
970 Builder.SetInsertPoint(ElseBB->begin());
971
972 if (isl_ast_node_if_has_else(If))
973 create(isl_ast_node_if_get_else(If));
974
975 Builder.SetInsertPoint(MergeBB->begin());
976
977 isl_ast_node_free(If);
978}
979
Tobias Grossere602a072013-05-07 07:30:56 +0000980void IslNodeBuilder::createSubstitutions(__isl_take isl_pw_multi_aff *PMA,
981 __isl_take isl_ast_build *Context,
982 ScopStmt *Stmt, ValueMapT &VMap,
983 LoopToScevMapT &LTS) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000984 for (unsigned i = 0; i < isl_pw_multi_aff_dim(PMA, isl_dim_out); ++i) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000985 isl_pw_aff *Aff;
986 isl_ast_expr *Expr;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000987 Value *V;
988
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000989 Aff = isl_pw_multi_aff_get_pw_aff(PMA, i);
990 Expr = isl_ast_build_expr_from_pw_aff(Context, Aff);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000991 V = ExprBuilder.create(Expr);
992
Sebastian Pope039bb12013-03-18 19:09:49 +0000993 ScalarEvolution *SE = Stmt->getParent()->getSE();
994 LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
995
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000996 // CreateIntCast can introduce trunc expressions. This is correct, as the
997 // result will always fit into the type of the original induction variable
998 // (because we calculate a value of the original induction variable).
Sebastian Pope039bb12013-03-18 19:09:49 +0000999 const Value *OldIV = Stmt->getInductionVariableForDimension(i);
1000 if (OldIV) {
1001 V = Builder.CreateIntCast(V, OldIV->getType(), true);
1002 VMap[OldIV] = V;
1003 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001004 }
1005
Sebastian Pop04c4ce32012-12-18 07:46:13 +00001006 isl_pw_multi_aff_free(PMA);
1007 isl_ast_build_free(Context);
1008}
1009
Tobias Grosserc14582f2013-02-05 18:01:29 +00001010void IslNodeBuilder::createSubstitutionsVector(
1011 __isl_take isl_pw_multi_aff *PMA, __isl_take isl_ast_build *Context,
Sebastian Pop9d10fff2013-02-15 20:55:59 +00001012 ScopStmt *Stmt, VectorValueMapT &VMap, std::vector<LoopToScevMapT> &VLTS,
1013 std::vector<Value *> &IVS, __isl_take isl_id *IteratorID) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +00001014 int i = 0;
1015
1016 Value *OldValue = IDToValue[IteratorID];
Tobias Grosser91f5b262014-06-04 08:06:40 +00001017 for (Value *IV : IVS) {
1018 IDToValue[IteratorID] = IV;
Tobias Grosser1bb59b02012-12-29 23:47:38 +00001019 createSubstitutions(isl_pw_multi_aff_copy(PMA), isl_ast_build_copy(Context),
Sebastian Pop9d10fff2013-02-15 20:55:59 +00001020 Stmt, VMap[i], VLTS[i]);
Sebastian Pop04c4ce32012-12-18 07:46:13 +00001021 i++;
1022 }
1023
1024 IDToValue[IteratorID] = OldValue;
1025 isl_id_free(IteratorID);
1026 isl_pw_multi_aff_free(PMA);
1027 isl_ast_build_free(Context);
1028}
1029
1030void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
1031 ValueMapT VMap;
Sebastian Pop9d10fff2013-02-15 20:55:59 +00001032 LoopToScevMapT LTS;
Sebastian Pop04c4ce32012-12-18 07:46:13 +00001033 struct IslAstUser *Info;
1034 isl_id *Annotation, *Id;
1035 ScopStmt *Stmt;
1036
1037 Annotation = isl_ast_node_get_annotation(User);
1038 assert(Annotation && "Scalar user statement is not annotated");
1039
Tobias Grosserc14582f2013-02-05 18:01:29 +00001040 Info = (struct IslAstUser *)isl_id_get_user(Annotation);
Sebastian Pop04c4ce32012-12-18 07:46:13 +00001041 assert(Info && "Scalar user statement annotation does not contain info");
1042
1043 Id = isl_pw_multi_aff_get_tuple_id(Info->PMA, isl_dim_out);
Tobias Grosserc14582f2013-02-05 18:01:29 +00001044 Stmt = (ScopStmt *)isl_id_get_user(Id);
Sebastian Pop04c4ce32012-12-18 07:46:13 +00001045
1046 createSubstitutions(isl_pw_multi_aff_copy(Info->PMA),
Sebastian Pop9d10fff2013-02-15 20:55:59 +00001047 isl_ast_build_copy(Info->Context), Stmt, VMap, LTS);
Sebastian Pop04c4ce32012-12-18 07:46:13 +00001048
Sebastian Pop9d10fff2013-02-15 20:55:59 +00001049 BlockGenerator::generate(Builder, *Stmt, VMap, LTS, P);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001050
1051 isl_ast_node_free(User);
1052 isl_id_free(Annotation);
1053 isl_id_free(Id);
1054}
1055
1056void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
1057 isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
1058
1059 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
1060 create(isl_ast_node_list_get_ast_node(List, i));
1061
1062 isl_ast_node_free(Block);
1063 isl_ast_node_list_free(List);
1064}
1065
1066void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
1067 switch (isl_ast_node_get_type(Node)) {
1068 case isl_ast_node_error:
1069 llvm_unreachable("code generation error");
1070 case isl_ast_node_for:
1071 createFor(Node);
1072 return;
1073 case isl_ast_node_if:
1074 createIf(Node);
1075 return;
1076 case isl_ast_node_user:
1077 createUser(Node);
1078 return;
1079 case isl_ast_node_block:
1080 createBlock(Node);
1081 return;
1082 }
1083
1084 llvm_unreachable("Unknown isl_ast_node type");
1085}
1086
1087void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
1088 SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly");
1089
1090 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
1091 isl_id *Id;
1092 const SCEV *Scev;
1093 IntegerType *T;
1094 Instruction *InsertLocation;
1095
1096 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserc14582f2013-02-05 18:01:29 +00001097 Scev = (const SCEV *)isl_id_get_user(Id);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001098 T = dyn_cast<IntegerType>(Scev->getType());
1099 InsertLocation = --(Builder.GetInsertBlock()->end());
1100 Value *V = Rewriter.expandCodeFor(Scev, T, InsertLocation);
1101 IDToValue[Id] = V;
1102
1103 isl_id_free(Id);
1104 }
1105
1106 isl_set_free(Context);
1107}
1108
1109namespace {
1110class IslCodeGeneration : public ScopPass {
Tobias Grosser1bb59b02012-12-29 23:47:38 +00001111public:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001112 static char ID;
1113
1114 IslCodeGeneration() : ScopPass(ID) {}
1115
1116 bool runOnScop(Scop &S) {
1117 IslAstInfo &AstInfo = getAnalysis<IslAstInfo>();
Tobias Grosser0ee50f62013-04-10 06:55:31 +00001118
Tobias Grossere602a072013-05-07 07:30:56 +00001119 assert(!S.getRegion().isTopLevelRegion() &&
1120 "Top level regions are not supported");
Tobias Grosser0ee50f62013-04-10 06:55:31 +00001121
Tobias Grosser8edce4e2013-04-16 08:04:42 +00001122 simplifyRegion(&S, this);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001123
1124 BasicBlock *StartBlock = executeScopConditionally(S, this);
1125 isl_ast_node *Ast = AstInfo.getAst();
Tobias Grosser37c9b8e2014-03-04 14:59:00 +00001126 LoopAnnotator Annotator;
1127 PollyIRBuilder Builder(StartBlock->getContext(), llvm::ConstantFolder(),
1128 polly::IRInserter(Annotator));
1129 Builder.SetInsertPoint(StartBlock->begin());
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001130
Tobias Grosser37c9b8e2014-03-04 14:59:00 +00001131 IslNodeBuilder NodeBuilder(Builder, Annotator, this);
Tobias Grosser54ee0ba2013-11-17 03:18:25 +00001132
1133 // Build condition that evaluates at run-time if all assumptions taken
1134 // for the scop hold. If we detect some assumptions do not hold, the
1135 // original code is executed.
1136 Value *V = NodeBuilder.getExprBuilder().create(AstInfo.getRunCondition());
1137 Value *Zero = ConstantInt::get(V->getType(), 0);
1138 V = Builder.CreateICmp(CmpInst::ICMP_NE, Zero, V);
1139 BasicBlock *PrevBB = StartBlock->getUniquePredecessor();
1140 BranchInst *Branch = dyn_cast<BranchInst>(PrevBB->getTerminator());
1141 Branch->setCondition(V);
1142
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001143 NodeBuilder.addParameters(S.getContext());
1144 NodeBuilder.create(Ast);
1145 return true;
1146 }
1147
Tobias Grosserc14582f2013-02-05 18:01:29 +00001148 virtual void printScop(raw_ostream &OS) const {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001149
1150 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser42aff302014-01-13 22:29:56 +00001151 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001152 AU.addRequired<IslAstInfo>();
1153 AU.addRequired<RegionInfo>();
1154 AU.addRequired<ScalarEvolution>();
1155 AU.addRequired<ScopDetection>();
1156 AU.addRequired<ScopInfo>();
Tobias Grosserecfe21b2013-03-20 18:03:18 +00001157 AU.addRequired<LoopInfo>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001158
1159 AU.addPreserved<Dependences>();
1160
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001161 AU.addPreserved<LoopInfo>();
Tobias Grosser42aff302014-01-13 22:29:56 +00001162 AU.addPreserved<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001163 AU.addPreserved<IslAstInfo>();
1164 AU.addPreserved<ScopDetection>();
1165 AU.addPreserved<ScalarEvolution>();
1166
1167 // FIXME: We do not yet add regions for the newly generated code to the
1168 // region tree.
1169 AU.addPreserved<RegionInfo>();
1170 AU.addPreserved<TempScopInfo>();
1171 AU.addPreserved<ScopInfo>();
1172 AU.addPreservedID(IndependentBlocksID);
1173 }
1174};
1175}
1176
1177char IslCodeGeneration::ID = 1;
1178
Tobias Grosser7242ad92013-02-22 08:07:06 +00001179Pass *polly::createIslCodeGenerationPass() { return new IslCodeGeneration(); }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001180
Tobias Grosser7242ad92013-02-22 08:07:06 +00001181INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl",
1182 "Polly - Create LLVM-IR from SCoPs", false, false);
1183INITIALIZE_PASS_DEPENDENCY(Dependences);
Tobias Grosser42aff302014-01-13 22:29:56 +00001184INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Tobias Grosser7242ad92013-02-22 08:07:06 +00001185INITIALIZE_PASS_DEPENDENCY(LoopInfo);
1186INITIALIZE_PASS_DEPENDENCY(RegionInfo);
1187INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1188INITIALIZE_PASS_DEPENDENCY(ScopDetection);
1189INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl",
1190 "Polly - Create LLVM-IR from SCoPs", false, false)