blob: 004544ca1038d6f2ca8e1d74c5821d7ab6f00716 [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"
Johannes Doerferta63b2572014-08-03 01:51:59 +000022#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser83628182013-05-07 08:11:54 +000023#include "polly/CodeGen/BlockGenerators.h"
24#include "polly/CodeGen/CodeGeneration.h"
25#include "polly/CodeGen/IslAst.h"
26#include "polly/CodeGen/LoopGenerators.h"
27#include "polly/CodeGen/Utils.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000028#include "polly/Dependences.h"
29#include "polly/LinkAllPasses.h"
30#include "polly/ScopInfo.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000031#include "polly/Support/GICHelper.h"
Tobias Grosser0ee50f62013-04-10 06:55:31 +000032#include "polly/Support/ScopHelper.h"
Tobias Grosser83628182013-05-07 08:11:54 +000033#include "polly/TempScopInfo.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000034#include "llvm/Analysis/LoopInfo.h"
Matt Arsenault8ca36812014-07-19 18:40:17 +000035#include "llvm/Analysis/PostDominators.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000036#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser83628182013-05-07 08:11:54 +000037#include "llvm/IR/Module.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000038#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
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000051using 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 +000056class IslNodeBuilder {
57public:
Johannes Doerfert51d1c742014-10-02 15:32:17 +000058 IslNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P,
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000059 LoopInfo &LI, ScalarEvolution &SE, DominatorTree &DT)
Johannes Doerfert2ef33e92014-10-05 11:33:59 +000060 : Builder(Builder), Annotator(Annotator),
61 Rewriter(new SCEVExpander(SE, "polly")),
62 ExprBuilder(Builder, IDToValue, *Rewriter), P(P), LI(LI), SE(SE),
63 DT(DT) {}
64
65 ~IslNodeBuilder() { delete Rewriter; }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000066
Johannes Doerferta63b2572014-08-03 01:51:59 +000067 /// @brief Add the mappings from array id's to array llvm::Value's.
68 void addMemoryAccesses(Scop &S);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000069 void addParameters(__isl_take isl_set *Context);
70 void create(__isl_take isl_ast_node *Node);
Tobias Grosser54ee0ba2013-11-17 03:18:25 +000071 IslExprBuilder &getExprBuilder() { return ExprBuilder; }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000072
73private:
Tobias Grosser5103ba72014-03-04 14:58:49 +000074 PollyIRBuilder &Builder;
Johannes Doerfert51d1c742014-10-02 15:32:17 +000075 ScopAnnotator &Annotator;
Johannes Doerfert2ef33e92014-10-05 11:33:59 +000076
77 /// @brief A SCEVExpander to create llvm values from SCEVs.
78 SCEVExpander *Rewriter;
79
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000080 IslExprBuilder ExprBuilder;
81 Pass *P;
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000082 LoopInfo &LI;
83 ScalarEvolution &SE;
84 DominatorTree &DT;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000085
86 // This maps an isl_id* to the Value* it has in the generated program. For now
87 // on, the only isl_ids that are stored here are the newly calculated loop
88 // ivs.
Tobias Grosser566ad582014-08-31 16:21:12 +000089 IslExprBuilder::IDToValueTy IDToValue;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000090
91 // Extract the upper bound of this loop
92 //
93 // The isl code generation can generate arbitrary expressions to check if the
94 // upper bound of a loop is reached, but it provides an option to enforce
95 // 'atomic' upper bounds. An 'atomic upper bound is always of the form
96 // iv <= expr, where expr is an (arbitrary) expression not containing iv.
97 //
98 // This function extracts 'atomic' upper bounds. Polly, in general, requires
99 // atomic upper bounds for the following reasons:
100 //
101 // 1. An atomic upper bound is loop invariant
102 //
103 // It must not be calculated at each loop iteration and can often even be
104 // hoisted out further by the loop invariant code motion.
105 //
106 // 2. OpenMP needs a loop invarient upper bound to calculate the number
107 // of loop iterations.
108 //
109 // 3. With the existing code, upper bounds have been easier to implement.
Tobias Grossere602a072013-05-07 07:30:56 +0000110 __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For,
111 CmpInst::Predicate &Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000112
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000113 unsigned getNumberOfIterations(__isl_keep isl_ast_node *For);
114
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000115 void createFor(__isl_take isl_ast_node *For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000116 void createForVector(__isl_take isl_ast_node *For, int VectorWidth);
117 void createForSequential(__isl_take isl_ast_node *For);
Tobias Grosserce67a042014-07-02 16:26:47 +0000118
119 /// Generate LLVM-IR that computes the values of the original induction
120 /// variables in function of the newly generated loop induction variables.
121 ///
122 /// Example:
123 ///
124 /// // Original
125 /// for i
126 /// for j
127 /// S(i)
128 ///
129 /// Schedule: [i,j] -> [i+j, j]
130 ///
131 /// // New
132 /// for c0
133 /// for c1
134 /// S(c0 - c1, c1)
135 ///
136 /// Assuming the original code consists of two loops which are
137 /// transformed according to a schedule [i,j] -> [c0=i+j,c1=j]. The resulting
138 /// ast models the original statement as a call expression where each argument
139 /// is an expression that computes the old induction variables from the new
140 /// ones, ordered such that the first argument computes the value of induction
141 /// variable that was outermost in the original code.
142 ///
143 /// @param Expr The call expression that represents the statement.
144 /// @param Stmt The statement that is called.
145 /// @param VMap The value map into which the mapping from the old induction
146 /// variable to the new one is inserted. This mapping is used
147 /// for the classical code generation (not scev-based) and
148 /// gives an explicit mapping from an original, materialized
149 /// induction variable. It consequently can only be expressed
150 /// if there was an explicit induction variable.
151 /// @param LTS The loop to SCEV map in which the mapping from the original
152 /// loop to a SCEV representing the new loop iv is added. This
153 /// mapping does not require an explicit induction variable.
154 /// Instead, we think in terms of an implicit induction variable
155 /// that counts the number of times a loop is executed. For each
156 /// original loop this count, expressed in function of the new
157 /// induction variables, is added to the LTS map.
158 void createSubstitutions(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000159 ValueMapT &VMap, LoopToScevMapT &LTS);
Tobias Grosserce67a042014-07-02 16:26:47 +0000160 void createSubstitutionsVector(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt,
161 VectorValueMapT &VMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000162 std::vector<LoopToScevMapT> &VLTS,
163 std::vector<Value *> &IVS,
164 __isl_take isl_id *IteratorID);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000165 void createIf(__isl_take isl_ast_node *If);
Tobias Grossere602a072013-05-07 07:30:56 +0000166 void createUserVector(__isl_take isl_ast_node *User,
167 std::vector<Value *> &IVS,
168 __isl_take isl_id *IteratorID,
169 __isl_take isl_union_map *Schedule);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000170 void createUser(__isl_take isl_ast_node *User);
171 void createBlock(__isl_take isl_ast_node *Block);
172};
173
Tobias Grossere602a072013-05-07 07:30:56 +0000174__isl_give isl_ast_expr *
175IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For,
176 ICmpInst::Predicate &Predicate) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000177 isl_id *UBID, *IteratorID;
178 isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000179 isl_ast_op_type Type;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000180
181 Cond = isl_ast_node_for_get_cond(For);
182 Iterator = isl_ast_node_for_get_iterator(For);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000183 Type = isl_ast_expr_get_op_type(Cond);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000184
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000185 assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op &&
186 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000187
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000188 switch (Type) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000189 case isl_ast_op_le:
190 Predicate = ICmpInst::ICMP_SLE;
191 break;
192 case isl_ast_op_lt:
193 Predicate = ICmpInst::ICMP_SLT;
194 break;
195 default:
196 llvm_unreachable("Unexpected comparision type in loop conditon");
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000197 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000198
199 Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
200
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000201 assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id &&
202 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000203
204 UBID = isl_ast_expr_get_id(Arg0);
205
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000206 assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id &&
207 "Could not get the iterator");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000208
209 IteratorID = isl_ast_expr_get_id(Iterator);
210
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000211 assert(UBID == IteratorID &&
212 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000213
214 UB = isl_ast_expr_get_op_arg(Cond, 1);
215
216 isl_ast_expr_free(Cond);
217 isl_ast_expr_free(Iterator);
218 isl_ast_expr_free(Arg0);
219 isl_id_free(IteratorID);
220 isl_id_free(UBID);
221
222 return UB;
223}
224
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000225unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) {
Johannes Doerfert94d90822014-07-23 20:26:25 +0000226 isl_union_map *Schedule = IslAstInfo::getSchedule(For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000227 isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule));
Sebastian Pop2aa5c242012-12-18 08:56:51 +0000228 int NumberOfIterations = polly::getNumberOfIterations(LoopDomain);
229 if (NumberOfIterations == -1)
230 return -1;
231 return NumberOfIterations + 1;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000232}
233
Tobias Grossere602a072013-05-07 07:30:56 +0000234void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User,
235 std::vector<Value *> &IVS,
236 __isl_take isl_id *IteratorID,
237 __isl_take isl_union_map *Schedule) {
Tobias Grosserce67a042014-07-02 16:26:47 +0000238 isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
239 isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
240 isl_id *Id = isl_ast_expr_get_id(StmtExpr);
241 isl_ast_expr_free(StmtExpr);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000242 ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000243 VectorValueMapT VectorMap(IVS.size());
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000244 std::vector<LoopToScevMapT> VLTS(IVS.size());
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000245
246 isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain());
247 Schedule = isl_union_map_intersect_domain(Schedule, Domain);
248 isl_map *S = isl_map_from_union_map(Schedule);
249
Tobias Grosserce67a042014-07-02 16:26:47 +0000250 createSubstitutionsVector(Expr, Stmt, VectorMap, VLTS, IVS, IteratorID);
Johannes Doerfert731685e2014-10-08 17:25:30 +0000251 VectorBlockGenerator::generate(Builder, *Stmt, VectorMap, VLTS, S, P, LI, SE,
252 IslAstInfo::getBuild(User), &ExprBuilder);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000253
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000254 isl_map_free(S);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000255 isl_id_free(Id);
256 isl_ast_node_free(User);
257}
258
Tobias Grossere602a072013-05-07 07:30:56 +0000259void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
260 int VectorWidth) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000261 isl_ast_node *Body = isl_ast_node_for_get_body(For);
262 isl_ast_expr *Init = isl_ast_node_for_get_init(For);
263 isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
264 isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
265 isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000266
267 Value *ValueLB = ExprBuilder.create(Init);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000268 Value *ValueInc = ExprBuilder.create(Inc);
269
270 Type *MaxType = ExprBuilder.getType(Iterator);
271 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000272 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
273
274 if (MaxType != ValueLB->getType())
275 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000276 if (MaxType != ValueInc->getType())
277 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
278
Tobias Grosserc14582f2013-02-05 18:01:29 +0000279 std::vector<Value *> IVS(VectorWidth);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000280 IVS[0] = ValueLB;
281
282 for (int i = 1; i < VectorWidth; i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000283 IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv");
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000284
Johannes Doerfert94d90822014-07-23 20:26:25 +0000285 isl_union_map *Schedule = IslAstInfo::getSchedule(For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000286 assert(Schedule && "For statement annotation does not contain its schedule");
287
288 IDToValue[IteratorID] = ValueLB;
289
290 switch (isl_ast_node_get_type(Body)) {
291 case isl_ast_node_user:
292 createUserVector(Body, IVS, isl_id_copy(IteratorID),
293 isl_union_map_copy(Schedule));
294 break;
295 case isl_ast_node_block: {
296 isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
297
298 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
299 createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000300 isl_id_copy(IteratorID), isl_union_map_copy(Schedule));
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000301
302 isl_ast_node_free(Body);
303 isl_ast_node_list_free(List);
304 break;
305 }
306 default:
307 isl_ast_node_dump(Body);
308 llvm_unreachable("Unhandled isl_ast_node in vectorizer");
309 }
310
311 IDToValue.erase(IteratorID);
312 isl_id_free(IteratorID);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000313 isl_union_map_free(Schedule);
314
315 isl_ast_node_free(For);
316 isl_ast_expr_free(Iterator);
317}
318
319void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000320 isl_ast_node *Body;
321 isl_ast_expr *Init, *Inc, *Iterator, *UB;
322 isl_id *IteratorID;
323 Value *ValueLB, *ValueUB, *ValueInc;
324 Type *MaxType;
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000325 BasicBlock *ExitBlock;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000326 Value *IV;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000327 CmpInst::Predicate Predicate;
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000328 bool Parallel;
329
Johannes Doerfertc7b719f2014-10-01 20:10:44 +0000330 Parallel =
331 IslAstInfo::isParallel(For) && !IslAstInfo::isReductionParallel(For);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000332
333 Body = isl_ast_node_for_get_body(For);
334
335 // isl_ast_node_for_is_degenerate(For)
336 //
337 // TODO: For degenerated loops we could generate a plain assignment.
338 // However, for now we just reuse the logic for normal loops, which will
339 // create a loop with a single iteration.
340
341 Init = isl_ast_node_for_get_init(For);
342 Inc = isl_ast_node_for_get_inc(For);
343 Iterator = isl_ast_node_for_get_iterator(For);
344 IteratorID = isl_ast_expr_get_id(Iterator);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000345 UB = getUpperBound(For, Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000346
347 ValueLB = ExprBuilder.create(Init);
348 ValueUB = ExprBuilder.create(UB);
349 ValueInc = ExprBuilder.create(Inc);
350
351 MaxType = ExprBuilder.getType(Iterator);
352 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
353 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
354 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
355
356 if (MaxType != ValueLB->getType())
357 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
358 if (MaxType != ValueUB->getType())
359 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
360 if (MaxType != ValueInc->getType())
361 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
362
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000363 // If we can show that LB <Predicate> UB holds at least once, we can
364 // omit the GuardBB in front of the loop.
365 bool UseGuardBB =
366 !SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB));
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000367 IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, LI, DT, ExitBlock,
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000368 Predicate, &Annotator, Parallel, UseGuardBB);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000369 IDToValue[IteratorID] = IV;
370
371 create(Body);
372
Johannes Doerfertc7b719f2014-10-01 20:10:44 +0000373 Annotator.popLoop(Parallel);
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000374
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000375 IDToValue.erase(IteratorID);
376
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000377 Builder.SetInsertPoint(ExitBlock->begin());
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000378
379 isl_ast_node_free(For);
380 isl_ast_expr_free(Iterator);
381 isl_id_free(IteratorID);
382}
383
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000384void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
385 bool Vector = PollyVectorizerChoice != VECTORIZER_NONE;
386
Johannes Doerferted67f8b2014-08-01 08:14:28 +0000387 if (Vector && IslAstInfo::isInnermostParallel(For) &&
388 !IslAstInfo::isReductionParallel(For)) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000389 int VectorWidth = getNumberOfIterations(For);
390 if (1 < VectorWidth && VectorWidth <= 16) {
391 createForVector(For, VectorWidth);
392 return;
393 }
394 }
395 createForSequential(For);
396}
397
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000398void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
399 isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
400
401 Function *F = Builder.GetInsertBlock()->getParent();
402 LLVMContext &Context = F->getContext();
403
Tobias Grosserc14582f2013-02-05 18:01:29 +0000404 BasicBlock *CondBB =
405 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000406 CondBB->setName("polly.cond");
407 BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
408 MergeBB->setName("polly.merge");
409 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
410 BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
411
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000412 DT.addNewBlock(ThenBB, CondBB);
413 DT.addNewBlock(ElseBB, CondBB);
414 DT.changeImmediateDominator(MergeBB, CondBB);
415
Tobias Grosser3081b0f2013-05-16 06:40:24 +0000416 Loop *L = LI.getLoopFor(CondBB);
417 if (L) {
418 L->addBasicBlockToLoop(ThenBB, LI.getBase());
419 L->addBasicBlockToLoop(ElseBB, LI.getBase());
420 }
421
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000422 CondBB->getTerminator()->eraseFromParent();
423
424 Builder.SetInsertPoint(CondBB);
425 Value *Predicate = ExprBuilder.create(Cond);
426 Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
427 Builder.SetInsertPoint(ThenBB);
428 Builder.CreateBr(MergeBB);
429 Builder.SetInsertPoint(ElseBB);
430 Builder.CreateBr(MergeBB);
431 Builder.SetInsertPoint(ThenBB->begin());
432
433 create(isl_ast_node_if_get_then(If));
434
435 Builder.SetInsertPoint(ElseBB->begin());
436
437 if (isl_ast_node_if_has_else(If))
438 create(isl_ast_node_if_get_else(If));
439
440 Builder.SetInsertPoint(MergeBB->begin());
441
442 isl_ast_node_free(If);
443}
444
Tobias Grosserce67a042014-07-02 16:26:47 +0000445void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt,
446 ValueMapT &VMap, LoopToScevMapT &LTS) {
447 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
448 "Expression of type 'op' expected");
449 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&
450 "Opertation of type 'call' expected");
451 for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) {
452 isl_ast_expr *SubExpr;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000453 Value *V;
454
Tobias Grosserce67a042014-07-02 16:26:47 +0000455 SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1);
456 V = ExprBuilder.create(SubExpr);
Sebastian Pope039bb12013-03-18 19:09:49 +0000457 ScalarEvolution *SE = Stmt->getParent()->getSE();
458 LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
459
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000460 // CreateIntCast can introduce trunc expressions. This is correct, as the
461 // result will always fit into the type of the original induction variable
462 // (because we calculate a value of the original induction variable).
Sebastian Pope039bb12013-03-18 19:09:49 +0000463 const Value *OldIV = Stmt->getInductionVariableForDimension(i);
464 if (OldIV) {
465 V = Builder.CreateIntCast(V, OldIV->getType(), true);
466 VMap[OldIV] = V;
467 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000468 }
469
Tobias Grosserce67a042014-07-02 16:26:47 +0000470 isl_ast_expr_free(Expr);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000471}
472
Tobias Grosserc14582f2013-02-05 18:01:29 +0000473void IslNodeBuilder::createSubstitutionsVector(
Tobias Grosserce67a042014-07-02 16:26:47 +0000474 __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, VectorValueMapT &VMap,
475 std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS,
476 __isl_take isl_id *IteratorID) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000477 int i = 0;
478
479 Value *OldValue = IDToValue[IteratorID];
Tobias Grosser91f5b262014-06-04 08:06:40 +0000480 for (Value *IV : IVS) {
481 IDToValue[IteratorID] = IV;
Tobias Grosserce67a042014-07-02 16:26:47 +0000482 createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VMap[i], VLTS[i]);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000483 i++;
484 }
485
486 IDToValue[IteratorID] = OldValue;
487 isl_id_free(IteratorID);
Tobias Grosserce67a042014-07-02 16:26:47 +0000488 isl_ast_expr_free(Expr);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000489}
490
491void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
492 ValueMapT VMap;
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000493 LoopToScevMapT LTS;
Tobias Grosserce67a042014-07-02 16:26:47 +0000494 isl_id *Id;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000495 ScopStmt *Stmt;
496
Tobias Grosserce67a042014-07-02 16:26:47 +0000497 isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
498 isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
499 Id = isl_ast_expr_get_id(StmtExpr);
500 isl_ast_expr_free(StmtExpr);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000501
Tobias Grosserc14582f2013-02-05 18:01:29 +0000502 Stmt = (ScopStmt *)isl_id_get_user(Id);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000503
Tobias Grosserce67a042014-07-02 16:26:47 +0000504 createSubstitutions(Expr, Stmt, VMap, LTS);
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000505 BlockGenerator::generate(Builder, *Stmt, VMap, LTS, P, LI, SE,
Johannes Doerferta63b2572014-08-03 01:51:59 +0000506 IslAstInfo::getBuild(User), &ExprBuilder);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000507
508 isl_ast_node_free(User);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000509 isl_id_free(Id);
510}
511
512void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
513 isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
514
515 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
516 create(isl_ast_node_list_get_ast_node(List, i));
517
518 isl_ast_node_free(Block);
519 isl_ast_node_list_free(List);
520}
521
522void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
523 switch (isl_ast_node_get_type(Node)) {
524 case isl_ast_node_error:
525 llvm_unreachable("code generation error");
526 case isl_ast_node_for:
527 createFor(Node);
528 return;
529 case isl_ast_node_if:
530 createIf(Node);
531 return;
532 case isl_ast_node_user:
533 createUser(Node);
534 return;
535 case isl_ast_node_block:
536 createBlock(Node);
537 return;
538 }
539
540 llvm_unreachable("Unknown isl_ast_node type");
541}
542
543void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000544
545 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
546 isl_id *Id;
547 const SCEV *Scev;
548 IntegerType *T;
549 Instruction *InsertLocation;
550
551 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000552 Scev = (const SCEV *)isl_id_get_user(Id);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000553 T = dyn_cast<IntegerType>(Scev->getType());
554 InsertLocation = --(Builder.GetInsertBlock()->end());
Johannes Doerfert2ef33e92014-10-05 11:33:59 +0000555 Value *V = Rewriter->expandCodeFor(Scev, T, InsertLocation);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000556 IDToValue[Id] = V;
557
558 isl_id_free(Id);
559 }
560
561 isl_set_free(Context);
562}
563
Johannes Doerferta63b2572014-08-03 01:51:59 +0000564void IslNodeBuilder::addMemoryAccesses(Scop &S) {
565 for (ScopStmt *Stmt : S)
566 for (MemoryAccess *MA : *Stmt) {
567 isl_id *Id = MA->getArrayId();
568 IDToValue[Id] = MA->getBaseAddr();
569 isl_id_free(Id);
570 }
571}
572
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000573namespace {
574class IslCodeGeneration : public ScopPass {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000575public:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000576 static char ID;
577
578 IslCodeGeneration() : ScopPass(ID) {}
579
Johannes Doerfert38262242014-09-10 14:50:23 +0000580 /// @name The analysis passes we need to generate code.
581 ///
582 ///{
583 LoopInfo *LI;
584 IslAstInfo *AI;
585 DominatorTree *DT;
586 ScalarEvolution *SE;
587 ///}
588
589 /// @brief The loop annotator to generate llvm.loop metadata.
Johannes Doerfert51d1c742014-10-02 15:32:17 +0000590 ScopAnnotator Annotator;
Johannes Doerfert38262242014-09-10 14:50:23 +0000591
592 /// @brief Build the runtime condition.
593 ///
594 /// Build the condition that evaluates at run-time to true iff all
595 /// assumptions taken for the SCoP hold, and to false otherwise.
596 ///
597 /// @return A value evaluating to true/false if execution is save/unsafe.
598 Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) {
599 Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
600 Value *RTC = ExprBuilder.create(AI->getRunCondition());
Johannes Doerfertb164c792014-09-18 11:17:17 +0000601 if (!RTC->getType()->isIntegerTy(1))
602 RTC = Builder.CreateIsNotNull(RTC);
603 return RTC;
Johannes Doerfert38262242014-09-10 14:50:23 +0000604 }
605
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000606 bool runOnScop(Scop &S) {
Johannes Doerfert38262242014-09-10 14:50:23 +0000607 LI = &getAnalysis<LoopInfo>();
608 AI = &getAnalysis<IslAstInfo>();
609 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
610 SE = &getAnalysis<ScalarEvolution>();
Tobias Grosser0ee50f62013-04-10 06:55:31 +0000611
Tobias Grossere602a072013-05-07 07:30:56 +0000612 assert(!S.getRegion().isTopLevelRegion() &&
613 "Top level regions are not supported");
Tobias Grosser0ee50f62013-04-10 06:55:31 +0000614
Johannes Doerfertecdf2632014-10-02 15:31:24 +0000615 // Build the alias scopes for annotations first.
616 if (PollyAnnotateAliasScopes)
617 Annotator.buildAliasScopes(S);
618
Johannes Doerfert38262242014-09-10 14:50:23 +0000619 BasicBlock *EnteringBB = simplifyRegion(&S, this);
620 PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
Johannes Doerfert9744c4a2014-08-12 18:35:54 +0000621
Johannes Doerfert38262242014-09-10 14:50:23 +0000622 IslNodeBuilder NodeBuilder(Builder, Annotator, this, *LI, *SE, *DT);
Tobias Grosser28735942014-08-16 09:09:15 +0000623 NodeBuilder.addMemoryAccesses(S);
624 NodeBuilder.addParameters(S.getContext());
Johannes Doerfert38262242014-09-10 14:50:23 +0000625
626 Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder());
627 BasicBlock *StartBlock = executeScopConditionally(S, this, RTC);
Tobias Grosser28735942014-08-16 09:09:15 +0000628 Builder.SetInsertPoint(StartBlock->begin());
629
Johannes Doerfert38262242014-09-10 14:50:23 +0000630 NodeBuilder.create(AI->getAst());
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000631 return true;
632 }
633
Tobias Grosserc14582f2013-02-05 18:01:29 +0000634 virtual void printScop(raw_ostream &OS) const {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000635
636 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Tobias Grosser42aff302014-01-13 22:29:56 +0000637 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000638 AU.addRequired<IslAstInfo>();
Matt Arsenault8ca36812014-07-19 18:40:17 +0000639 AU.addRequired<RegionInfoPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000640 AU.addRequired<ScalarEvolution>();
641 AU.addRequired<ScopDetection>();
642 AU.addRequired<ScopInfo>();
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000643 AU.addRequired<LoopInfo>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000644
645 AU.addPreserved<Dependences>();
646
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000647 AU.addPreserved<LoopInfo>();
Tobias Grosser42aff302014-01-13 22:29:56 +0000648 AU.addPreserved<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000649 AU.addPreserved<IslAstInfo>();
650 AU.addPreserved<ScopDetection>();
651 AU.addPreserved<ScalarEvolution>();
652
653 // FIXME: We do not yet add regions for the newly generated code to the
654 // region tree.
Matt Arsenault8ca36812014-07-19 18:40:17 +0000655 AU.addPreserved<RegionInfoPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000656 AU.addPreserved<TempScopInfo>();
657 AU.addPreserved<ScopInfo>();
658 AU.addPreservedID(IndependentBlocksID);
659 }
660};
661}
662
663char IslCodeGeneration::ID = 1;
664
Tobias Grosser7242ad92013-02-22 08:07:06 +0000665Pass *polly::createIslCodeGenerationPass() { return new IslCodeGeneration(); }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000666
Tobias Grosser7242ad92013-02-22 08:07:06 +0000667INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl",
668 "Polly - Create LLVM-IR from SCoPs", false, false);
669INITIALIZE_PASS_DEPENDENCY(Dependences);
Tobias Grosser42aff302014-01-13 22:29:56 +0000670INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000671INITIALIZE_PASS_DEPENDENCY(LoopInfo);
Matt Arsenault8ca36812014-07-19 18:40:17 +0000672INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000673INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
674INITIALIZE_PASS_DEPENDENCY(ScopDetection);
675INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl",
676 "Polly - Create LLVM-IR from SCoPs", false, false)