blob: e2bec0d1b987bb4d96daf08bb148e28dbab31c5d [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
Tobias Grosser54839312015-04-21 11:37:25 +000018// its code in the new execution order defined by the changed schedule.
Sebastian Pop082cea82012-05-07 16:20:07 +000019//
20//===----------------------------------------------------------------------===//
Tobias Grosserf3ba5b52015-04-27 12:17:22 +000021
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"
Johannes Doerfertf6557f92015-03-04 22:43:40 +000028#include "polly/DependenceInfo.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000029#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 Grossere3c05582014-11-15 21:32:53 +000033#include "polly/Support/SCEVValidator.h"
Tobias Grosser83628182013-05-07 08:11:54 +000034#include "polly/TempScopInfo.h"
Tobias Grossere3c05582014-11-15 21:32:53 +000035
36#include "llvm/ADT/PostOrderIterator.h"
Matt Arsenault8ca36812014-07-19 18:40:17 +000037#include "llvm/Analysis/PostDominators.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000038#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser83628182013-05-07 08:11:54 +000039#include "llvm/IR/Module.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000040#include "llvm/Support/CommandLine.h"
41#include "llvm/Support/Debug.h"
Johannes Doerfert0b169c02015-02-27 17:37:05 +000042#include "llvm/IR/Verifier.h"
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000043#include "llvm/Transforms/Utils/BasicBlockUtils.h"
44
45#include "isl/union_map.h"
46#include "isl/list.h"
47#include "isl/ast.h"
48#include "isl/ast_build.h"
49#include "isl/set.h"
50#include "isl/map.h"
51#include "isl/aff.h"
52
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000053using namespace polly;
54using namespace llvm;
55
Chandler Carruth95fef942014-04-22 03:30:19 +000056#define DEBUG_TYPE "polly-codegen-isl"
57
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000058class IslNodeBuilder {
59public:
Johannes Doerfert51d1c742014-10-02 15:32:17 +000060 IslNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P,
Tobias Grossere3c05582014-11-15 21:32:53 +000061 const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE,
62 DominatorTree &DT, Scop &S)
Tobias Grosserc9895062015-03-10 15:24:33 +000063 : S(S), Builder(Builder), Annotator(Annotator), Rewriter(SE, DL, "polly"),
Tobias Grosserbb8d1562015-03-04 19:33:31 +000064 ExprBuilder(Builder, IDToValue, Rewriter, DT, LI),
Johannes Doerfertf4af99b2015-03-08 21:38:35 +000065 BlockGen(Builder, LI, SE, DT, &ExprBuilder), RegionGen(BlockGen), P(P),
66 DL(DL), LI(LI), SE(SE), DT(DT) {}
Johannes Doerfert2ef33e92014-10-05 11:33:59 +000067
Tobias Grosserbb8d1562015-03-04 19:33:31 +000068 ~IslNodeBuilder() {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000069
70 void addParameters(__isl_take isl_set *Context);
71 void create(__isl_take isl_ast_node *Node);
Tobias Grosser54ee0ba2013-11-17 03:18:25 +000072 IslExprBuilder &getExprBuilder() { return ExprBuilder; }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000073
74private:
Tobias Grossere3c05582014-11-15 21:32:53 +000075 Scop &S;
Tobias Grosser5103ba72014-03-04 14:58:49 +000076 PollyIRBuilder &Builder;
Johannes Doerfert51d1c742014-10-02 15:32:17 +000077 ScopAnnotator &Annotator;
Johannes Doerfert2ef33e92014-10-05 11:33:59 +000078
79 /// @brief A SCEVExpander to create llvm values from SCEVs.
Tobias Grosserbb8d1562015-03-04 19:33:31 +000080 SCEVExpander Rewriter;
Johannes Doerfert2ef33e92014-10-05 11:33:59 +000081
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000082 IslExprBuilder ExprBuilder;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000083 BlockGenerator BlockGen;
Johannes Doerfert275a1752015-02-24 16:16:32 +000084
85 /// @brief Generator for region statements.
86 RegionGenerator RegionGen;
87
Johannes Doerfertf4af99b2015-03-08 21:38:35 +000088 Pass *const P;
Tobias Grossere3c05582014-11-15 21:32:53 +000089 const DataLayout &DL;
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000090 LoopInfo &LI;
91 ScalarEvolution &SE;
92 DominatorTree &DT;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +000093
Tobias Grossere3c05582014-11-15 21:32:53 +000094 /// @brief The current iteration of out-of-scop loops
95 ///
96 /// This map provides for a given loop a llvm::Value that contains the current
97 /// loop iteration.
98 LoopToScevMapT OutsideLoopIterations;
99
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000100 // This maps an isl_id* to the Value* it has in the generated program. For now
101 // on, the only isl_ids that are stored here are the newly calculated loop
102 // ivs.
Tobias Grosser566ad582014-08-31 16:21:12 +0000103 IslExprBuilder::IDToValueTy IDToValue;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000104
Tobias Grosserec7d67e2014-11-06 00:27:01 +0000105 /// Generate code for a given SCEV*
106 ///
107 /// This function generates code for a given SCEV expression. It generated
108 /// code is emmitted at the end of the basic block our Builder currently
109 /// points to and the resulting value is returned.
110 ///
111 /// @param Expr The expression to code generate.
112 Value *generateSCEV(const SCEV *Expr);
113
Tobias Grossere3c05582014-11-15 21:32:53 +0000114 /// A set of Value -> Value remappings to apply when generating new code.
115 ///
116 /// When generating new code for a ScopStmt this map is used to map certain
117 /// llvm::Values to new llvm::Values.
118 ValueMapT ValueMap;
119
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000120 // Extract the upper bound of this loop
121 //
122 // The isl code generation can generate arbitrary expressions to check if the
123 // upper bound of a loop is reached, but it provides an option to enforce
124 // 'atomic' upper bounds. An 'atomic upper bound is always of the form
125 // iv <= expr, where expr is an (arbitrary) expression not containing iv.
126 //
127 // This function extracts 'atomic' upper bounds. Polly, in general, requires
128 // atomic upper bounds for the following reasons:
129 //
130 // 1. An atomic upper bound is loop invariant
131 //
132 // It must not be calculated at each loop iteration and can often even be
133 // hoisted out further by the loop invariant code motion.
134 //
135 // 2. OpenMP needs a loop invarient upper bound to calculate the number
136 // of loop iterations.
137 //
138 // 3. With the existing code, upper bounds have been easier to implement.
Tobias Grossere602a072013-05-07 07:30:56 +0000139 __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For,
140 CmpInst::Predicate &Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000141
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000142 unsigned getNumberOfIterations(__isl_keep isl_ast_node *For);
143
Tobias Grossere3c05582014-11-15 21:32:53 +0000144 /// Compute the values and loops referenced in this subtree.
145 ///
146 /// This function looks at all ScopStmts scheduled below the provided For node
147 /// and finds the llvm::Value[s] and llvm::Loops[s] which are referenced but
148 /// not locally defined.
149 ///
150 /// Values that can be synthesized or that are available as globals are
151 /// considered locally defined.
152 ///
153 /// Loops that contain the scop or that are part of the scop are considered
154 /// locally defined. Loops that are before the scop, but do not contain the
155 /// scop itself are considered not locally defined.
156 ///
157 /// @param For The node defining the subtree.
158 /// @param Values A vector that will be filled with the Values referenced in
159 /// this subtree.
160 /// @param Loops A vector that will be filled with the Loops referenced in
161 /// this subtree.
162 void getReferencesInSubtree(__isl_keep isl_ast_node *For,
163 SetVector<Value *> &Values,
164 SetVector<const Loop *> &Loops);
165
166 /// Change the llvm::Value(s) used for code generation.
167 ///
168 /// When generating code certain values (e.g., references to induction
169 /// variables or array base pointers) in the original code may be replaced by
170 /// new values. This function allows to (partially) update the set of values
171 /// used. A typical use case for this function is the case when we continue
172 /// code generation in a subfunction/kernel function and need to explicitly
173 /// pass down certain values.
174 ///
175 /// @param NewValues A map that maps certain llvm::Values to new llvm::Values.
176 void updateValues(ParallelLoopGenerator::ValueToValueMapTy &NewValues);
177
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000178 void createFor(__isl_take isl_ast_node *For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000179 void createForVector(__isl_take isl_ast_node *For, int VectorWidth);
180 void createForSequential(__isl_take isl_ast_node *For);
Tobias Grosserce67a042014-07-02 16:26:47 +0000181
Tobias Grossere3c05582014-11-15 21:32:53 +0000182 /// Create LLVM-IR that executes a for node thread parallel.
183 ///
184 /// @param For The FOR isl_ast_node for which code is generated.
185 void createForParallel(__isl_take isl_ast_node *For);
186
Tobias Grosserce67a042014-07-02 16:26:47 +0000187 /// Generate LLVM-IR that computes the values of the original induction
188 /// variables in function of the newly generated loop induction variables.
189 ///
190 /// Example:
191 ///
192 /// // Original
193 /// for i
194 /// for j
195 /// S(i)
196 ///
197 /// Schedule: [i,j] -> [i+j, j]
198 ///
199 /// // New
200 /// for c0
201 /// for c1
202 /// S(c0 - c1, c1)
203 ///
204 /// Assuming the original code consists of two loops which are
205 /// transformed according to a schedule [i,j] -> [c0=i+j,c1=j]. The resulting
206 /// ast models the original statement as a call expression where each argument
207 /// is an expression that computes the old induction variables from the new
208 /// ones, ordered such that the first argument computes the value of induction
209 /// variable that was outermost in the original code.
210 ///
211 /// @param Expr The call expression that represents the statement.
212 /// @param Stmt The statement that is called.
213 /// @param VMap The value map into which the mapping from the old induction
214 /// variable to the new one is inserted. This mapping is used
215 /// for the classical code generation (not scev-based) and
216 /// gives an explicit mapping from an original, materialized
217 /// induction variable. It consequently can only be expressed
218 /// if there was an explicit induction variable.
219 /// @param LTS The loop to SCEV map in which the mapping from the original
220 /// loop to a SCEV representing the new loop iv is added. This
221 /// mapping does not require an explicit induction variable.
222 /// Instead, we think in terms of an implicit induction variable
223 /// that counts the number of times a loop is executed. For each
224 /// original loop this count, expressed in function of the new
225 /// induction variables, is added to the LTS map.
226 void createSubstitutions(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000227 ValueMapT &VMap, LoopToScevMapT &LTS);
Tobias Grosserce67a042014-07-02 16:26:47 +0000228 void createSubstitutionsVector(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt,
229 VectorValueMapT &VMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000230 std::vector<LoopToScevMapT> &VLTS,
231 std::vector<Value *> &IVS,
232 __isl_take isl_id *IteratorID);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000233 void createIf(__isl_take isl_ast_node *If);
Tobias Grossere602a072013-05-07 07:30:56 +0000234 void 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 Grosser8a5bc6e2012-10-02 19:50:43 +0000238 void createUser(__isl_take isl_ast_node *User);
239 void createBlock(__isl_take isl_ast_node *Block);
240};
241
Tobias Grossere602a072013-05-07 07:30:56 +0000242__isl_give isl_ast_expr *
243IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For,
244 ICmpInst::Predicate &Predicate) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000245 isl_id *UBID, *IteratorID;
246 isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000247 isl_ast_op_type Type;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000248
249 Cond = isl_ast_node_for_get_cond(For);
250 Iterator = isl_ast_node_for_get_iterator(For);
Tobias Grosser2784b082015-01-10 07:40:39 +0000251 isl_ast_expr_get_type(Cond);
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000252 assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op &&
253 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000254
Tobias Grosser2784b082015-01-10 07:40:39 +0000255 Type = isl_ast_expr_get_op_type(Cond);
256
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000257 switch (Type) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000258 case isl_ast_op_le:
259 Predicate = ICmpInst::ICMP_SLE;
260 break;
261 case isl_ast_op_lt:
262 Predicate = ICmpInst::ICMP_SLT;
263 break;
264 default:
265 llvm_unreachable("Unexpected comparision type in loop conditon");
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000266 }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000267
268 Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
269
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000270 assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id &&
271 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000272
273 UBID = isl_ast_expr_get_id(Arg0);
274
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000275 assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id &&
276 "Could not get the iterator");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000277
278 IteratorID = isl_ast_expr_get_id(Iterator);
279
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000280 assert(UBID == IteratorID &&
281 "conditional expression is not an atomic upper bound");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000282
283 UB = isl_ast_expr_get_op_arg(Cond, 1);
284
285 isl_ast_expr_free(Cond);
286 isl_ast_expr_free(Iterator);
287 isl_ast_expr_free(Arg0);
288 isl_id_free(IteratorID);
289 isl_id_free(UBID);
290
291 return UB;
292}
293
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000294unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) {
Johannes Doerfert94d90822014-07-23 20:26:25 +0000295 isl_union_map *Schedule = IslAstInfo::getSchedule(For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000296 isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule));
Tobias Grosserb68068b2015-04-27 10:38:45 +0000297 int Dim = isl_set_dim(LoopDomain, isl_dim_set);
298
299 // Calculate a map similar to the identity map, but with the last input
300 // and output dimension not related.
301 // [i0, i1, i2, i3] -> [i0, i1, i2, o0]
302 isl_space *Space = isl_set_get_space(LoopDomain);
303 Space = isl_space_drop_dims(Space, isl_dim_out, Dim - 1, 1);
304 Space = isl_space_map_from_set(Space);
305 isl_map *Identity = isl_map_identity(Space);
306 Identity = isl_map_add_dims(Identity, isl_dim_in, 1);
307 Identity = isl_map_add_dims(Identity, isl_dim_out, 1);
308
309 LoopDomain = isl_set_reset_tuple_id(LoopDomain);
310
311 isl_map *Map = isl_map_from_domain_and_range(isl_set_copy(LoopDomain),
312 isl_set_copy(LoopDomain));
313 isl_set_free(LoopDomain);
314 Map = isl_map_intersect(Map, Identity);
315
316 isl_map *LexMax = isl_map_lexmax(isl_map_copy(Map));
317 isl_map *LexMin = isl_map_lexmin(Map);
318 isl_map *Sub = isl_map_sum(LexMax, isl_map_neg(LexMin));
319
320 isl_set *Elements = isl_map_range(Sub);
321
322 if (!isl_set_is_singleton(Elements)) {
323 isl_set_free(Elements);
Sebastian Pop2aa5c242012-12-18 08:56:51 +0000324 return -1;
Tobias Grosserb68068b2015-04-27 10:38:45 +0000325 }
326
327 isl_point *P = isl_set_sample_point(Elements);
328
329 isl_val *V;
330 V = isl_point_get_coordinate_val(P, isl_dim_set, Dim - 1);
331 int NumberIterations = isl_val_get_num_si(V);
332 isl_val_free(V);
333 isl_point_free(P);
334 if (NumberIterations == -1)
335 return -1;
336 return NumberIterations + 1;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000337}
338
Tobias Grossere3c05582014-11-15 21:32:53 +0000339struct FindValuesUser {
340 LoopInfo &LI;
341 ScalarEvolution &SE;
342 Region &R;
343 SetVector<Value *> &Values;
344 SetVector<const SCEV *> &SCEVs;
345};
346
Johannes Doerfertbbf30842015-03-02 13:41:53 +0000347/// @brief Extract the values and SCEVs needed to generate code for a block.
348static int findValuesInBlock(struct FindValuesUser &User, const ScopStmt *Stmt,
349 const BasicBlock *BB) {
Tobias Grossere3c05582014-11-15 21:32:53 +0000350 // Check all the operands of instructions in the basic block.
351 for (const Instruction &Inst : *BB) {
352 for (Value *SrcVal : Inst.operands()) {
353 if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal))
354 if (canSynthesize(OpInst, &User.LI, &User.SE, &User.R)) {
355 User.SCEVs.insert(
356 User.SE.getSCEVAtScope(OpInst, User.LI.getLoopFor(BB)));
357 continue;
358 }
359 if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal))
360 if (Stmt->getParent()->getRegion().contains(OpInst))
361 continue;
362
363 if (isa<Instruction>(SrcVal) || isa<Argument>(SrcVal))
364 User.Values.insert(SrcVal);
365 }
366 }
Johannes Doerfertbbf30842015-03-02 13:41:53 +0000367 return 0;
368}
369
370/// Extract the values and SCEVs needed to generate code for a ScopStmt.
371///
372/// This function extracts a ScopStmt from a given isl_set and computes the
373/// Values this statement depends on as well as a set of SCEV expressions that
374/// need to be synthesized when generating code for this statment.
375static int findValuesInStmt(isl_set *Set, void *UserPtr) {
376 isl_id *Id = isl_set_get_tuple_id(Set);
377 struct FindValuesUser &User = *static_cast<struct FindValuesUser *>(UserPtr);
378 const ScopStmt *Stmt = static_cast<const ScopStmt *>(isl_id_get_user(Id));
379
380 if (Stmt->isBlockStmt())
381 findValuesInBlock(User, Stmt, Stmt->getBasicBlock());
382 else {
383 assert(Stmt->isRegionStmt() &&
384 "Stmt was neither block nor region statement");
385 for (const BasicBlock *BB : Stmt->getRegion()->blocks())
386 findValuesInBlock(User, Stmt, BB);
387 }
388
Tobias Grossere3c05582014-11-15 21:32:53 +0000389 isl_id_free(Id);
390 isl_set_free(Set);
391 return 0;
392}
393
394void IslNodeBuilder::getReferencesInSubtree(__isl_keep isl_ast_node *For,
395 SetVector<Value *> &Values,
396 SetVector<const Loop *> &Loops) {
397
398 SetVector<const SCEV *> SCEVs;
399 struct FindValuesUser FindValues = {LI, SE, S.getRegion(), Values, SCEVs};
400
401 for (const auto &I : IDToValue)
402 Values.insert(I.second);
403
404 for (const auto &I : OutsideLoopIterations)
405 Values.insert(cast<SCEVUnknown>(I.second)->getValue());
406
407 isl_union_set *Schedule = isl_union_map_domain(IslAstInfo::getSchedule(For));
408
409 isl_union_set_foreach_set(Schedule, findValuesInStmt, &FindValues);
410 isl_union_set_free(Schedule);
411
412 for (const SCEV *Expr : SCEVs) {
413 findValues(Expr, Values);
414 findLoops(Expr, Loops);
415 }
416
417 Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); });
418
419 /// Remove loops that contain the scop or that are part of the scop, as they
420 /// are considered local. This leaves only loops that are before the scop, but
421 /// do not contain the scop itself.
422 Loops.remove_if([this](const Loop *L) {
423 return this->S.getRegion().contains(L) ||
424 L->contains(S.getRegion().getEntry());
425 });
426}
427
428void IslNodeBuilder::updateValues(
429 ParallelLoopGenerator::ValueToValueMapTy &NewValues) {
430 SmallPtrSet<Value *, 5> Inserted;
431
432 for (const auto &I : IDToValue) {
433 IDToValue[I.first] = NewValues[I.second];
434 Inserted.insert(I.second);
435 }
436
437 for (const auto &I : NewValues) {
438 if (Inserted.count(I.first))
439 continue;
440
441 ValueMap[I.first] = I.second;
442 }
443}
444
Tobias Grossere602a072013-05-07 07:30:56 +0000445void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User,
446 std::vector<Value *> &IVS,
447 __isl_take isl_id *IteratorID,
448 __isl_take isl_union_map *Schedule) {
Tobias Grosserce67a042014-07-02 16:26:47 +0000449 isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
450 isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
451 isl_id *Id = isl_ast_expr_get_id(StmtExpr);
452 isl_ast_expr_free(StmtExpr);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000453 ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000454 Stmt->setAstBuild(IslAstInfo::getBuild(User));
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000455 VectorValueMapT VectorMap(IVS.size());
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000456 std::vector<LoopToScevMapT> VLTS(IVS.size());
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000457
458 isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain());
459 Schedule = isl_union_map_intersect_domain(Schedule, Domain);
460 isl_map *S = isl_map_from_union_map(Schedule);
461
Tobias Grosserce67a042014-07-02 16:26:47 +0000462 createSubstitutionsVector(Expr, Stmt, VectorMap, VLTS, IVS, IteratorID);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000463 VectorBlockGenerator::generate(BlockGen, *Stmt, VectorMap, VLTS, S);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000464
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000465 isl_map_free(S);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000466 isl_id_free(Id);
467 isl_ast_node_free(User);
468}
469
Tobias Grossere602a072013-05-07 07:30:56 +0000470void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
471 int VectorWidth) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000472 isl_ast_node *Body = isl_ast_node_for_get_body(For);
473 isl_ast_expr *Init = isl_ast_node_for_get_init(For);
474 isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
475 isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
476 isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000477
478 Value *ValueLB = ExprBuilder.create(Init);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000479 Value *ValueInc = ExprBuilder.create(Inc);
480
481 Type *MaxType = ExprBuilder.getType(Iterator);
482 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000483 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
484
485 if (MaxType != ValueLB->getType())
486 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000487 if (MaxType != ValueInc->getType())
488 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
489
Tobias Grosserc14582f2013-02-05 18:01:29 +0000490 std::vector<Value *> IVS(VectorWidth);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000491 IVS[0] = ValueLB;
492
493 for (int i = 1; i < VectorWidth; i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000494 IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv");
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000495
Johannes Doerfert94d90822014-07-23 20:26:25 +0000496 isl_union_map *Schedule = IslAstInfo::getSchedule(For);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000497 assert(Schedule && "For statement annotation does not contain its schedule");
498
499 IDToValue[IteratorID] = ValueLB;
500
501 switch (isl_ast_node_get_type(Body)) {
502 case isl_ast_node_user:
503 createUserVector(Body, IVS, isl_id_copy(IteratorID),
504 isl_union_map_copy(Schedule));
505 break;
506 case isl_ast_node_block: {
507 isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
508
509 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
510 createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000511 isl_id_copy(IteratorID), isl_union_map_copy(Schedule));
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000512
513 isl_ast_node_free(Body);
514 isl_ast_node_list_free(List);
515 break;
516 }
517 default:
518 isl_ast_node_dump(Body);
519 llvm_unreachable("Unhandled isl_ast_node in vectorizer");
520 }
521
Tobias Grossere3c05582014-11-15 21:32:53 +0000522 IDToValue.erase(IDToValue.find(IteratorID));
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000523 isl_id_free(IteratorID);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000524 isl_union_map_free(Schedule);
525
526 isl_ast_node_free(For);
527 isl_ast_expr_free(Iterator);
528}
529
530void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000531 isl_ast_node *Body;
532 isl_ast_expr *Init, *Inc, *Iterator, *UB;
533 isl_id *IteratorID;
534 Value *ValueLB, *ValueUB, *ValueInc;
535 Type *MaxType;
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000536 BasicBlock *ExitBlock;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000537 Value *IV;
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000538 CmpInst::Predicate Predicate;
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000539 bool Parallel;
540
Johannes Doerfertc7b719f2014-10-01 20:10:44 +0000541 Parallel =
542 IslAstInfo::isParallel(For) && !IslAstInfo::isReductionParallel(For);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000543
544 Body = isl_ast_node_for_get_body(For);
545
546 // isl_ast_node_for_is_degenerate(For)
547 //
548 // TODO: For degenerated loops we could generate a plain assignment.
549 // However, for now we just reuse the logic for normal loops, which will
550 // create a loop with a single iteration.
551
552 Init = isl_ast_node_for_get_init(For);
553 Inc = isl_ast_node_for_get_inc(For);
554 Iterator = isl_ast_node_for_get_iterator(For);
555 IteratorID = isl_ast_expr_get_id(Iterator);
Tobias Grosserc967d8e2012-10-16 07:29:13 +0000556 UB = getUpperBound(For, Predicate);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000557
558 ValueLB = ExprBuilder.create(Init);
559 ValueUB = ExprBuilder.create(UB);
560 ValueInc = ExprBuilder.create(Inc);
561
562 MaxType = ExprBuilder.getType(Iterator);
563 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
564 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
565 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
566
567 if (MaxType != ValueLB->getType())
568 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
569 if (MaxType != ValueUB->getType())
570 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
571 if (MaxType != ValueInc->getType())
572 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
573
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000574 // If we can show that LB <Predicate> UB holds at least once, we can
575 // omit the GuardBB in front of the loop.
576 bool UseGuardBB =
577 !SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB));
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000578 IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, LI, DT, ExitBlock,
Johannes Doerfertdd5c1442014-09-10 17:33:32 +0000579 Predicate, &Annotator, Parallel, UseGuardBB);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000580 IDToValue[IteratorID] = IV;
581
582 create(Body);
583
Johannes Doerfertc7b719f2014-10-01 20:10:44 +0000584 Annotator.popLoop(Parallel);
Tobias Grosser37c9b8e2014-03-04 14:59:00 +0000585
Tobias Grossere3c05582014-11-15 21:32:53 +0000586 IDToValue.erase(IDToValue.find(IteratorID));
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000587
Tobias Grosser5db6ffd2013-05-16 06:40:06 +0000588 Builder.SetInsertPoint(ExitBlock->begin());
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000589
590 isl_ast_node_free(For);
591 isl_ast_expr_free(Iterator);
592 isl_id_free(IteratorID);
593}
594
Tobias Grossere3c05582014-11-15 21:32:53 +0000595/// @brief Remove the BBs contained in a (sub)function from the dominator tree.
596///
597/// This function removes the basic blocks that are part of a subfunction from
598/// the dominator tree. Specifically, when generating code it may happen that at
599/// some point the code generation continues in a new sub-function (e.g., when
600/// generating OpenMP code). The basic blocks that are created in this
601/// sub-function are then still part of the dominator tree of the original
602/// function, such that the dominator tree reaches over function boundaries.
603/// This is not only incorrect, but also causes crashes. This function now
604/// removes from the dominator tree all basic blocks that are dominated (and
605/// consequently reachable) from the entry block of this (sub)function.
606///
607/// FIXME: A LLVM (function or region) pass should not touch anything outside of
608/// the function/region it runs on. Hence, the pure need for this function shows
609/// that we do not comply to this rule. At the moment, this does not cause any
610/// issues, but we should be aware that such issues may appear. Unfortunately
611/// the current LLVM pass infrastructure does not allow to make Polly a module
612/// or call-graph pass to solve this issue, as such a pass would not have access
613/// to the per-function analyses passes needed by Polly. A future pass manager
614/// infrastructure is supposed to enable such kind of access possibly allowing
615/// us to create a cleaner solution here.
616///
617/// FIXME: Instead of adding the dominance information and then dropping it
618/// later on, we should try to just not add it in the first place. This requires
619/// some careful testing to make sure this does not break in interaction with
620/// the SCEVBuilder and SplitBlock which may rely on the dominator tree or
621/// which may try to update it.
622///
623/// @param F The function which contains the BBs to removed.
624/// @param DT The dominator tree from which to remove the BBs.
625static void removeSubFuncFromDomTree(Function *F, DominatorTree &DT) {
626 DomTreeNode *N = DT.getNode(&F->getEntryBlock());
627 std::vector<BasicBlock *> Nodes;
628
629 // We can only remove an element from the dominator tree, if all its children
630 // have been removed. To ensure this we obtain the list of nodes to remove
631 // using a post-order tree traversal.
632 for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I)
633 Nodes.push_back(I->getBlock());
634
635 for (BasicBlock *BB : Nodes)
636 DT.eraseNode(BB);
637}
638
639void IslNodeBuilder::createForParallel(__isl_take isl_ast_node *For) {
640 isl_ast_node *Body;
641 isl_ast_expr *Init, *Inc, *Iterator, *UB;
642 isl_id *IteratorID;
643 Value *ValueLB, *ValueUB, *ValueInc;
644 Type *MaxType;
645 Value *IV;
646 CmpInst::Predicate Predicate;
647
648 Body = isl_ast_node_for_get_body(For);
649 Init = isl_ast_node_for_get_init(For);
650 Inc = isl_ast_node_for_get_inc(For);
651 Iterator = isl_ast_node_for_get_iterator(For);
652 IteratorID = isl_ast_expr_get_id(Iterator);
653 UB = getUpperBound(For, Predicate);
654
655 ValueLB = ExprBuilder.create(Init);
656 ValueUB = ExprBuilder.create(UB);
657 ValueInc = ExprBuilder.create(Inc);
658
659 // OpenMP always uses SLE. In case the isl generated AST uses a SLT
660 // expression, we need to adjust the loop blound by one.
661 if (Predicate == CmpInst::ICMP_SLT)
662 ValueUB = Builder.CreateAdd(
663 ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType()));
664
665 MaxType = ExprBuilder.getType(Iterator);
666 MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
667 MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
668 MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
669
670 if (MaxType != ValueLB->getType())
671 ValueLB = Builder.CreateSExt(ValueLB, MaxType);
672 if (MaxType != ValueUB->getType())
673 ValueUB = Builder.CreateSExt(ValueUB, MaxType);
674 if (MaxType != ValueInc->getType())
675 ValueInc = Builder.CreateSExt(ValueInc, MaxType);
676
677 BasicBlock::iterator LoopBody;
678
679 SetVector<Value *> SubtreeValues;
680 SetVector<const Loop *> Loops;
681
682 getReferencesInSubtree(For, SubtreeValues, Loops);
683
684 // Create for all loops we depend on values that contain the current loop
685 // iteration. These values are necessary to generate code for SCEVs that
686 // depend on such loops. As a result we need to pass them to the subfunction.
687 for (const Loop *L : Loops) {
688 const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
689 SE.getUnknown(Builder.getInt64(1)),
690 L, SCEV::FlagAnyWrap);
691 Value *V = generateSCEV(OuterLIV);
692 OutsideLoopIterations[L] = SE.getUnknown(V);
693 SubtreeValues.insert(V);
694 }
695
696 ParallelLoopGenerator::ValueToValueMapTy NewValues;
697 ParallelLoopGenerator ParallelLoopGen(Builder, P, LI, DT, DL);
698
699 IV = ParallelLoopGen.createParallelLoop(ValueLB, ValueUB, ValueInc,
700 SubtreeValues, NewValues, &LoopBody);
701 BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
702 Builder.SetInsertPoint(LoopBody);
703
704 // Save the current values.
705 ValueMapT ValueMapCopy = ValueMap;
706 IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue;
707
708 updateValues(NewValues);
709 IDToValue[IteratorID] = IV;
710
711 create(Body);
712
713 // Restore the original values.
714 ValueMap = ValueMapCopy;
715 IDToValue = IDToValueCopy;
716
717 Builder.SetInsertPoint(AfterLoop);
718 removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT);
719
720 for (const Loop *L : Loops)
721 OutsideLoopIterations.erase(L);
722
723 isl_ast_node_free(For);
724 isl_ast_expr_free(Iterator);
725 isl_id_free(IteratorID);
726}
727
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000728void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
Tobias Grosser7527e3f2015-04-05 06:53:21 +0000729 bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000730
Johannes Doerferted67f8b2014-08-01 08:14:28 +0000731 if (Vector && IslAstInfo::isInnermostParallel(For) &&
732 !IslAstInfo::isReductionParallel(For)) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000733 int VectorWidth = getNumberOfIterations(For);
734 if (1 < VectorWidth && VectorWidth <= 16) {
735 createForVector(For, VectorWidth);
736 return;
737 }
738 }
Tobias Grossere3c05582014-11-15 21:32:53 +0000739
740 if (IslAstInfo::isExecutedInParallel(For)) {
741 createForParallel(For);
742 return;
743 }
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000744 createForSequential(For);
745}
746
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000747void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
748 isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
749
750 Function *F = Builder.GetInsertBlock()->getParent();
751 LLVMContext &Context = F->getContext();
752
Tobias Grosserc14582f2013-02-05 18:01:29 +0000753 BasicBlock *CondBB =
Chandler Carruth5ec33332015-01-18 10:52:23 +0000754 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000755 CondBB->setName("polly.cond");
Chandler Carruth5ec33332015-01-18 10:52:23 +0000756 BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), &DT, &LI);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000757 MergeBB->setName("polly.merge");
758 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
759 BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
760
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000761 DT.addNewBlock(ThenBB, CondBB);
762 DT.addNewBlock(ElseBB, CondBB);
763 DT.changeImmediateDominator(MergeBB, CondBB);
764
Tobias Grosser3081b0f2013-05-16 06:40:24 +0000765 Loop *L = LI.getLoopFor(CondBB);
766 if (L) {
Chandler Carruth6adcf562015-01-18 01:47:30 +0000767 L->addBasicBlockToLoop(ThenBB, LI);
768 L->addBasicBlockToLoop(ElseBB, LI);
Tobias Grosser3081b0f2013-05-16 06:40:24 +0000769 }
770
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000771 CondBB->getTerminator()->eraseFromParent();
772
773 Builder.SetInsertPoint(CondBB);
774 Value *Predicate = ExprBuilder.create(Cond);
775 Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
776 Builder.SetInsertPoint(ThenBB);
777 Builder.CreateBr(MergeBB);
778 Builder.SetInsertPoint(ElseBB);
779 Builder.CreateBr(MergeBB);
780 Builder.SetInsertPoint(ThenBB->begin());
781
782 create(isl_ast_node_if_get_then(If));
783
784 Builder.SetInsertPoint(ElseBB->begin());
785
786 if (isl_ast_node_if_has_else(If))
787 create(isl_ast_node_if_get_else(If));
788
789 Builder.SetInsertPoint(MergeBB->begin());
790
791 isl_ast_node_free(If);
792}
793
Tobias Grosserce67a042014-07-02 16:26:47 +0000794void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt,
795 ValueMapT &VMap, LoopToScevMapT &LTS) {
796 assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
797 "Expression of type 'op' expected");
798 assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&
799 "Opertation of type 'call' expected");
800 for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) {
801 isl_ast_expr *SubExpr;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000802 Value *V;
803
Tobias Grosserce67a042014-07-02 16:26:47 +0000804 SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1);
805 V = ExprBuilder.create(SubExpr);
Sebastian Pope039bb12013-03-18 19:09:49 +0000806 ScalarEvolution *SE = Stmt->getParent()->getSE();
807 LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000808 }
809
Tobias Grossere3c05582014-11-15 21:32:53 +0000810 // Add the current ValueMap to our per-statement value map.
811 //
812 // This is needed e.g. to rewrite array base addresses when moving code
813 // into a parallely executed subfunction.
814 VMap.insert(ValueMap.begin(), ValueMap.end());
815
Tobias Grosserce67a042014-07-02 16:26:47 +0000816 isl_ast_expr_free(Expr);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000817}
818
Tobias Grosserc14582f2013-02-05 18:01:29 +0000819void IslNodeBuilder::createSubstitutionsVector(
Tobias Grosserce67a042014-07-02 16:26:47 +0000820 __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, VectorValueMapT &VMap,
821 std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS,
822 __isl_take isl_id *IteratorID) {
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000823 int i = 0;
824
825 Value *OldValue = IDToValue[IteratorID];
Tobias Grosser91f5b262014-06-04 08:06:40 +0000826 for (Value *IV : IVS) {
827 IDToValue[IteratorID] = IV;
Tobias Grosserce67a042014-07-02 16:26:47 +0000828 createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VMap[i], VLTS[i]);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000829 i++;
830 }
831
832 IDToValue[IteratorID] = OldValue;
833 isl_id_free(IteratorID);
Tobias Grosserce67a042014-07-02 16:26:47 +0000834 isl_ast_expr_free(Expr);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000835}
836
837void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
838 ValueMapT VMap;
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000839 LoopToScevMapT LTS;
Tobias Grosserce67a042014-07-02 16:26:47 +0000840 isl_id *Id;
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000841 ScopStmt *Stmt;
842
Tobias Grosserce67a042014-07-02 16:26:47 +0000843 isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
844 isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
845 Id = isl_ast_expr_get_id(StmtExpr);
846 isl_ast_expr_free(StmtExpr);
Sebastian Pop04c4ce32012-12-18 07:46:13 +0000847
Tobias Grossere3c05582014-11-15 21:32:53 +0000848 LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
849
Tobias Grosserc14582f2013-02-05 18:01:29 +0000850 Stmt = (ScopStmt *)isl_id_get_user(Id);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000851 Stmt->setAstBuild(IslAstInfo::getBuild(User));
Johannes Doerferta63b2572014-08-03 01:51:59 +0000852
Tobias Grosserce67a042014-07-02 16:26:47 +0000853 createSubstitutions(Expr, Stmt, VMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000854 if (Stmt->isBlockStmt())
855 BlockGen.copyStmt(*Stmt, VMap, LTS);
856 else
857 RegionGen.copyStmt(*Stmt, VMap, LTS);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000858
859 isl_ast_node_free(User);
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000860 isl_id_free(Id);
861}
862
863void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
864 isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
865
866 for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
867 create(isl_ast_node_list_get_ast_node(List, i));
868
869 isl_ast_node_free(Block);
870 isl_ast_node_list_free(List);
871}
872
873void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
874 switch (isl_ast_node_get_type(Node)) {
875 case isl_ast_node_error:
Tobias Grosser29e36dc2015-03-30 17:28:57 +0000876 llvm_unreachable("code generation error");
877 case isl_ast_node_mark:
878 llvm_unreachable("Mark node unexpected");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000879 case isl_ast_node_for:
880 createFor(Node);
881 return;
882 case isl_ast_node_if:
883 createIf(Node);
884 return;
885 case isl_ast_node_user:
886 createUser(Node);
887 return;
888 case isl_ast_node_block:
889 createBlock(Node);
890 return;
891 }
892
893 llvm_unreachable("Unknown isl_ast_node type");
894}
895
896void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000897
898 for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
899 isl_id *Id;
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000900
901 Id = isl_set_get_dim_id(Context, isl_dim_param, i);
Tobias Grosserec7d67e2014-11-06 00:27:01 +0000902 IDToValue[Id] = generateSCEV((const SCEV *)isl_id_get_user(Id));
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000903
904 isl_id_free(Id);
905 }
906
Tobias Grossere3c05582014-11-15 21:32:53 +0000907 // Generate values for the current loop iteration for all surrounding loops.
908 //
909 // We may also reference loops outside of the scop which do not contain the
910 // scop itself, but as the number of such scops may be arbitrarily large we do
911 // not generate code for them here, but only at the point of code generation
912 // where these values are needed.
913 Region &R = S.getRegion();
914 Loop *L = LI.getLoopFor(R.getEntry());
915
916 while (L != nullptr && R.contains(L))
917 L = L->getParentLoop();
918
919 while (L != nullptr) {
920 const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
921 SE.getUnknown(Builder.getInt64(1)),
922 L, SCEV::FlagAnyWrap);
923 Value *V = generateSCEV(OuterLIV);
924 OutsideLoopIterations[L] = SE.getUnknown(V);
925 L = L->getParentLoop();
926 }
927
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000928 isl_set_free(Context);
929}
930
Tobias Grosserec7d67e2014-11-06 00:27:01 +0000931Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) {
932 Instruction *InsertLocation = --(Builder.GetInsertBlock()->end());
Tobias Grosserbb8d1562015-03-04 19:33:31 +0000933 return Rewriter.expandCodeFor(Expr, Expr->getType(), InsertLocation);
Tobias Grosserec7d67e2014-11-06 00:27:01 +0000934}
935
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000936namespace {
937class IslCodeGeneration : public ScopPass {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000938public:
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +0000939 static char ID;
940
941 IslCodeGeneration() : ScopPass(ID) {}
942
Tobias Grossere3c05582014-11-15 21:32:53 +0000943 /// @brief The datalayout used
944 const DataLayout *DL;
945
Johannes Doerfert38262242014-09-10 14:50:23 +0000946 /// @name The analysis passes we need to generate code.
947 ///
948 ///{
949 LoopInfo *LI;
950 IslAstInfo *AI;
951 DominatorTree *DT;
952 ScalarEvolution *SE;
953 ///}
954
955 /// @brief The loop annotator to generate llvm.loop metadata.
Johannes Doerfert51d1c742014-10-02 15:32:17 +0000956 ScopAnnotator Annotator;
Johannes Doerfert38262242014-09-10 14:50:23 +0000957
958 /// @brief Build the runtime condition.
959 ///
960 /// Build the condition that evaluates at run-time to true iff all
961 /// assumptions taken for the SCoP hold, and to false otherwise.
962 ///
963 /// @return A value evaluating to true/false if execution is save/unsafe.
964 Value *buildRTC(PollyIRBuilder &Builder, IslExprBuilder &ExprBuilder) {
965 Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
966 Value *RTC = ExprBuilder.create(AI->getRunCondition());
Johannes Doerfertb164c792014-09-18 11:17:17 +0000967 if (!RTC->getType()->isIntegerTy(1))
968 RTC = Builder.CreateIsNotNull(RTC);
969 return RTC;
Johannes Doerfert38262242014-09-10 14:50:23 +0000970 }
971
Johannes Doerfert0b169c02015-02-27 17:37:05 +0000972 bool verifyGeneratedFunction(Scop &S, Function &F) {
973 if (!verifyFunction(F))
974 return false;
975
976 DEBUG({
977 errs() << "== ISL Codegen created an invalid function ==\n\n== The "
978 "SCoP ==\n";
979 S.print(errs());
980 errs() << "\n== The isl AST ==\n";
Johannes Doerfert3fe584d2015-03-01 18:40:25 +0000981 AI->printScop(errs(), S);
Johannes Doerfert0b169c02015-02-27 17:37:05 +0000982 errs() << "\n== The invalid function ==\n";
983 F.print(errs());
984 errs() << "\n== The errors ==\n";
985 verifyFunction(F, &errs());
986 });
987
988 return true;
989 }
990
Johannes Doerfert909a3bf2015-03-01 18:42:08 +0000991 bool runOnScop(Scop &S) override {
Johannes Doerfert38262242014-09-10 14:50:23 +0000992 AI = &getAnalysis<IslAstInfo>();
Johannes Doerfert7ceb0402015-02-11 17:25:09 +0000993
994 // Check if we created an isl_ast root node, otherwise exit.
995 isl_ast_node *AstRoot = AI->getAst();
996 if (!AstRoot)
997 return false;
998
999 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Johannes Doerfert38262242014-09-10 14:50:23 +00001000 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1001 SE = &getAnalysis<ScalarEvolution>();
Tobias Grosser140b3942015-03-05 09:48:20 +00001002 DL = &S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
Tobias Grosser0ee50f62013-04-10 06:55:31 +00001003
Tobias Grossere602a072013-05-07 07:30:56 +00001004 assert(!S.getRegion().isTopLevelRegion() &&
1005 "Top level regions are not supported");
Tobias Grosser0ee50f62013-04-10 06:55:31 +00001006
Tobias Grosser6325cd22015-04-27 10:43:10 +00001007 Annotator.buildAliasScopes(S);
Johannes Doerfertecdf2632014-10-02 15:31:24 +00001008
Johannes Doerfert38262242014-09-10 14:50:23 +00001009 BasicBlock *EnteringBB = simplifyRegion(&S, this);
1010 PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
Johannes Doerfert9744c4a2014-08-12 18:35:54 +00001011
Tobias Grossere3c05582014-11-15 21:32:53 +00001012 IslNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, S);
Johannes Doerfert38262242014-09-10 14:50:23 +00001013
Tobias Grosser67942382015-03-28 09:34:40 +00001014 // Only build the run-time condition and parameters _after_ having
1015 // introduced the conditional branch. This is important as the conditional
1016 // branch will guard the original scop from new induction variables that
1017 // the SCEVExpander may introduce while code generating the parameters and
1018 // which may introduce scalar dependences that prevent us from correctly
1019 // code generating this scop.
1020 BasicBlock *StartBlock =
1021 executeScopConditionally(S, this, Builder.getTrue());
1022 auto SplitBlock = StartBlock->getSinglePredecessor();
1023 Builder.SetInsertPoint(SplitBlock->getTerminator());
1024 NodeBuilder.addParameters(S.getContext());
Johannes Doerfert38262242014-09-10 14:50:23 +00001025 Value *RTC = buildRTC(Builder, NodeBuilder.getExprBuilder());
Tobias Grosser67942382015-03-28 09:34:40 +00001026 SplitBlock->getTerminator()->setOperand(0, RTC);
Tobias Grosser28735942014-08-16 09:09:15 +00001027 Builder.SetInsertPoint(StartBlock->begin());
1028
Johannes Doerfert7ceb0402015-02-11 17:25:09 +00001029 NodeBuilder.create(AstRoot);
Johannes Doerfert0b169c02015-02-27 17:37:05 +00001030
1031 assert(!verifyGeneratedFunction(S, *EnteringBB->getParent()) &&
1032 "Verification of generated function failed");
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001033 return true;
1034 }
1035
Johannes Doerfert909a3bf2015-03-01 18:42:08 +00001036 void printScop(raw_ostream &, Scop &) const override {}
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001037
Johannes Doerfert909a3bf2015-03-01 18:42:08 +00001038 void getAnalysisUsage(AnalysisUsage &AU) const override {
Tobias Grosser42aff302014-01-13 22:29:56 +00001039 AU.addRequired<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001040 AU.addRequired<IslAstInfo>();
Matt Arsenault8ca36812014-07-19 18:40:17 +00001041 AU.addRequired<RegionInfoPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001042 AU.addRequired<ScalarEvolution>();
1043 AU.addRequired<ScopDetection>();
1044 AU.addRequired<ScopInfo>();
Chandler Carruthf5579872015-01-17 14:16:56 +00001045 AU.addRequired<LoopInfoWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001046
Johannes Doerfertf6557f92015-03-04 22:43:40 +00001047 AU.addPreserved<DependenceInfo>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001048
Chandler Carruthf5579872015-01-17 14:16:56 +00001049 AU.addPreserved<LoopInfoWrapperPass>();
Tobias Grosser42aff302014-01-13 22:29:56 +00001050 AU.addPreserved<DominatorTreeWrapperPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001051 AU.addPreserved<IslAstInfo>();
1052 AU.addPreserved<ScopDetection>();
1053 AU.addPreserved<ScalarEvolution>();
1054
1055 // FIXME: We do not yet add regions for the newly generated code to the
1056 // region tree.
Matt Arsenault8ca36812014-07-19 18:40:17 +00001057 AU.addPreserved<RegionInfoPass>();
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001058 AU.addPreserved<TempScopInfo>();
1059 AU.addPreserved<ScopInfo>();
1060 AU.addPreservedID(IndependentBlocksID);
1061 }
1062};
1063}
1064
1065char IslCodeGeneration::ID = 1;
1066
Tobias Grosser7242ad92013-02-22 08:07:06 +00001067Pass *polly::createIslCodeGenerationPass() { return new IslCodeGeneration(); }
Tobias Grosser8a5bc6e2012-10-02 19:50:43 +00001068
Tobias Grosser7242ad92013-02-22 08:07:06 +00001069INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl",
1070 "Polly - Create LLVM-IR from SCoPs", false, false);
Johannes Doerfertf6557f92015-03-04 22:43:40 +00001071INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
Tobias Grosser42aff302014-01-13 22:29:56 +00001072INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
Chandler Carruthf5579872015-01-17 14:16:56 +00001073INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
Matt Arsenault8ca36812014-07-19 18:40:17 +00001074INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
Tobias Grosser7242ad92013-02-22 08:07:06 +00001075INITIALIZE_PASS_DEPENDENCY(ScalarEvolution);
1076INITIALIZE_PASS_DEPENDENCY(ScopDetection);
1077INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl",
1078 "Polly - Create LLVM-IR from SCoPs", false, false)