blob: 9cb708f09ca393afb44e73b4d2f680335642ef10 [file] [log] [blame]
Tobias Grosser75805372011-04-29 06:27:02 +00001//===------ CodeGeneration.cpp - Code generate the Scops. -----------------===//
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 CodeGeneration pass takes a Scop created by ScopInfo and translates it
11// back to LLVM-IR using Cloog.
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. Cloog is used to generate an abstract syntax tree (clast) that
16// reflects the updated execution order. This clast is used to create new
17// LLVM-IR that is computational equivalent to the original control flow region,
18// but executes its code in the new execution order defined by the changed
19// scattering.
20//
21//===----------------------------------------------------------------------===//
22
Tobias Grosser0a91f322012-05-29 09:11:46 +000023#include "polly/CodeGen/Cloog.h"
Sebastian Pope1f65542012-05-07 16:35:11 +000024#ifdef CLOOG_FOUND
25
26#define DEBUG_TYPE "polly-codegen"
Tobias Grosser75805372011-04-29 06:27:02 +000027#include "polly/Dependences.h"
Tobias Grosserbda1f8f2012-02-01 14:23:29 +000028#include "polly/LinkAllPasses.h"
Tobias Grosser75805372011-04-29 06:27:02 +000029#include "polly/ScopInfo.h"
30#include "polly/TempScopInfo.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000031#include "polly/CodeGen/CodeGeneration.h"
32#include "polly/CodeGen/BlockGenerators.h"
33#include "polly/CodeGen/LoopGenerators.h"
Tobias Grosser3a275d22012-05-29 09:11:54 +000034#include "polly/CodeGen/Utils.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000035#include "polly/Support/GICHelper.h"
Tobias Grosserbda1f8f2012-02-01 14:23:29 +000036
37#include "llvm/Module.h"
38#include "llvm/ADT/SetVector.h"
Tobias Grosser900893d2012-03-29 13:10:26 +000039#include "llvm/ADT/PostOrderIterator.h"
Tobias Grosserbda1f8f2012-02-01 14:23:29 +000040#include "llvm/Analysis/LoopInfo.h"
41#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser75805372011-04-29 06:27:02 +000042#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/Debug.h"
Tobias Grosser75805372011-04-29 06:27:02 +000044#include "llvm/Target/TargetData.h"
Tobias Grosserbda1f8f2012-02-01 14:23:29 +000045#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Tobias Grosser75805372011-04-29 06:27:02 +000046
47#define CLOOG_INT_GMP 1
48#include "cloog/cloog.h"
49#include "cloog/isl/cloog.h"
50
Raghesh Aloora71989c2011-12-28 02:48:26 +000051#include "isl/aff.h"
52
Tobias Grosser75805372011-04-29 06:27:02 +000053#include <vector>
54#include <utility>
55
56using namespace polly;
57using namespace llvm;
58
59struct isl_set;
60
61namespace polly {
Tobias Grosser75805372011-04-29 06:27:02 +000062static cl::opt<bool>
63OpenMP("enable-polly-openmp",
64 cl::desc("Generate OpenMP parallel code"), cl::Hidden,
65 cl::value_desc("OpenMP code generation enabled if true"),
Tobias Grosser0acfcdb2012-03-16 17:17:16 +000066 cl::init(false), cl::ZeroOrMore);
Tobias Grosser75805372011-04-29 06:27:02 +000067
68static cl::opt<bool>
69AtLeastOnce("enable-polly-atLeastOnce",
70 cl::desc("Give polly the hint, that every loop is executed at least"
71 "once"), cl::Hidden,
72 cl::value_desc("OpenMP code generation enabled if true"),
Tobias Grosser0acfcdb2012-03-16 17:17:16 +000073 cl::init(false), cl::ZeroOrMore);
Tobias Grosser75805372011-04-29 06:27:02 +000074
Tobias Grosser75805372011-04-29 06:27:02 +000075typedef DenseMap<const char*, Value*> CharMapT;
Tobias Grosser70e8cdb2012-01-24 16:42:21 +000076
Tobias Grosser75805372011-04-29 06:27:02 +000077/// Class to generate LLVM-IR that calculates the value of a clast_expr.
78class ClastExpCodeGen {
79 IRBuilder<> &Builder;
Tobias Grosser5e8ffa82012-03-23 12:20:36 +000080 const CharMapT &IVS;
Tobias Grosser75805372011-04-29 06:27:02 +000081
Tobias Grosserbb137e32012-01-24 16:42:28 +000082 Value *codegen(const clast_name *e, Type *Ty);
83 Value *codegen(const clast_term *e, Type *Ty);
84 Value *codegen(const clast_binary *e, Type *Ty);
85 Value *codegen(const clast_reduction *r, Type *Ty);
Tobias Grosser75805372011-04-29 06:27:02 +000086public:
87
88 // A generator for clast expressions.
89 //
90 // @param B The IRBuilder that defines where the code to calculate the
91 // clast expressions should be inserted.
92 // @param IVMAP A Map that translates strings describing the induction
93 // variables to the Values* that represent these variables
94 // on the LLVM side.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +000095 ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap);
Tobias Grosser75805372011-04-29 06:27:02 +000096
97 // Generates code to calculate a given clast expression.
98 //
99 // @param e The expression to calculate.
100 // @return The Value that holds the result.
Tobias Grosserbb137e32012-01-24 16:42:28 +0000101 Value *codegen(const clast_expr *e, Type *Ty);
Tobias Grosserbb137e32012-01-24 16:42:28 +0000102};
103
104Value *ClastExpCodeGen::codegen(const clast_name *e, Type *Ty) {
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000105 CharMapT::const_iterator I = IVS.find(e->name);
Tobias Grosserbb137e32012-01-24 16:42:28 +0000106
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000107 assert(I != IVS.end() && "Clast name not found");
Tobias Grosserbb137e32012-01-24 16:42:28 +0000108
109 return Builder.CreateSExtOrBitCast(I->second, Ty);
110}
111
112Value *ClastExpCodeGen::codegen(const clast_term *e, Type *Ty) {
113 APInt a = APInt_from_MPZ(e->val);
114
115 Value *ConstOne = ConstantInt::get(Builder.getContext(), a);
116 ConstOne = Builder.CreateSExtOrBitCast(ConstOne, Ty);
117
118 if (!e->var)
119 return ConstOne;
120
121 Value *var = codegen(e->var, Ty);
122 return Builder.CreateMul(ConstOne, var);
123}
124
125Value *ClastExpCodeGen::codegen(const clast_binary *e, Type *Ty) {
126 Value *LHS = codegen(e->LHS, Ty);
127
128 APInt RHS_AP = APInt_from_MPZ(e->RHS);
129
130 Value *RHS = ConstantInt::get(Builder.getContext(), RHS_AP);
131 RHS = Builder.CreateSExtOrBitCast(RHS, Ty);
132
133 switch (e->type) {
134 case clast_bin_mod:
135 return Builder.CreateSRem(LHS, RHS);
136 case clast_bin_fdiv:
137 {
Tobias Grosser9a44b972012-02-16 14:13:19 +0000138 // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
Tobias Grosser906eafe2012-02-16 09:56:10 +0000139 Value *One = ConstantInt::get(Ty, 1);
140 Value *Zero = ConstantInt::get(Ty, 0);
Tobias Grosser9a44b972012-02-16 14:13:19 +0000141 Value *Sum1 = Builder.CreateSub(LHS, RHS);
142 Value *Sum2 = Builder.CreateAdd(Sum1, One);
143 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
144 Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS);
145 return Builder.CreateSDiv(Dividend, RHS);
Tobias Grosserbb137e32012-01-24 16:42:28 +0000146 }
147 case clast_bin_cdiv:
148 {
Tobias Grosser9a44b972012-02-16 14:13:19 +0000149 // ceild(n,d) ((n < 0) ? n : (n + d - 1)) / d
150 Value *One = ConstantInt::get(Ty, 1);
Tobias Grosser906eafe2012-02-16 09:56:10 +0000151 Value *Zero = ConstantInt::get(Ty, 0);
Tobias Grosser9a44b972012-02-16 14:13:19 +0000152 Value *Sum1 = Builder.CreateAdd(LHS, RHS);
153 Value *Sum2 = Builder.CreateSub(Sum1, One);
154 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
155 Value *Dividend = Builder.CreateSelect(isNegative, LHS, Sum2);
156 return Builder.CreateSDiv(Dividend, RHS);
Tobias Grosserbb137e32012-01-24 16:42:28 +0000157 }
158 case clast_bin_div:
159 return Builder.CreateSDiv(LHS, RHS);
160 };
161
162 llvm_unreachable("Unknown clast binary expression type");
163}
164
165Value *ClastExpCodeGen::codegen(const clast_reduction *r, Type *Ty) {
166 assert(( r->type == clast_red_min
167 || r->type == clast_red_max
168 || r->type == clast_red_sum)
169 && "Clast reduction type not supported");
170 Value *old = codegen(r->elts[0], Ty);
171
172 for (int i=1; i < r->n; ++i) {
173 Value *exprValue = codegen(r->elts[i], Ty);
174
175 switch (r->type) {
176 case clast_red_min:
177 {
178 Value *cmp = Builder.CreateICmpSLT(old, exprValue);
179 old = Builder.CreateSelect(cmp, old, exprValue);
180 break;
181 }
182 case clast_red_max:
183 {
184 Value *cmp = Builder.CreateICmpSGT(old, exprValue);
185 old = Builder.CreateSelect(cmp, old, exprValue);
186 break;
187 }
188 case clast_red_sum:
189 old = Builder.CreateAdd(old, exprValue);
190 break;
Tobias Grosserbb137e32012-01-24 16:42:28 +0000191 }
Tobias Grosser75805372011-04-29 06:27:02 +0000192 }
193
Tobias Grosserbb137e32012-01-24 16:42:28 +0000194 return old;
195}
196
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000197ClastExpCodeGen::ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap)
Tobias Grosserbb137e32012-01-24 16:42:28 +0000198 : Builder(B), IVS(IVMap) {}
199
200Value *ClastExpCodeGen::codegen(const clast_expr *e, Type *Ty) {
201 switch(e->type) {
202 case clast_expr_name:
203 return codegen((const clast_name *)e, Ty);
204 case clast_expr_term:
205 return codegen((const clast_term *)e, Ty);
206 case clast_expr_bin:
207 return codegen((const clast_binary *)e, Ty);
208 case clast_expr_red:
209 return codegen((const clast_reduction *)e, Ty);
210 }
211
212 llvm_unreachable("Unknown clast expression!");
213}
214
Tobias Grosser75805372011-04-29 06:27:02 +0000215class ClastStmtCodeGen {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000216public:
217 const std::vector<std::string> &getParallelLoops();
218
219private:
Tobias Grosser75805372011-04-29 06:27:02 +0000220 // The Scop we code generate.
221 Scop *S;
Tobias Grosser0ac92142012-02-14 14:02:27 +0000222 Pass *P;
Tobias Grosser75805372011-04-29 06:27:02 +0000223
224 // The Builder specifies the current location to code generate at.
225 IRBuilder<> &Builder;
226
227 // Map the Values from the old code to their counterparts in the new code.
228 ValueMapT ValueMap;
229
230 // clastVars maps from the textual representation of a clast variable to its
231 // current *Value. clast variables are scheduling variables, original
232 // induction variables or parameters. They are used either in loop bounds or
233 // to define the statement instance that is executed.
234 //
235 // for (s = 0; s < n + 3; ++i)
236 // for (t = s; t < m; ++j)
237 // Stmt(i = s + 3 * m, j = t);
238 //
239 // {s,t,i,j,n,m} is the set of clast variables in this clast.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000240 CharMapT ClastVars;
Tobias Grosser75805372011-04-29 06:27:02 +0000241
242 // Codegenerator for clast expressions.
243 ClastExpCodeGen ExpGen;
244
245 // Do we currently generate parallel code?
246 bool parallelCodeGeneration;
247
248 std::vector<std::string> parallelLoops;
249
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000250 void codegen(const clast_assignment *a);
Tobias Grosser75805372011-04-29 06:27:02 +0000251
252 void codegen(const clast_assignment *a, ScopStmt *Statement,
253 unsigned Dimension, int vectorDim,
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000254 std::vector<ValueMapT> *VectorVMap = 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000255
256 void codegenSubstitutions(const clast_stmt *Assignment,
257 ScopStmt *Statement, int vectorDim = 0,
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000258 std::vector<ValueMapT> *VectorVMap = 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000259
260 void codegen(const clast_user_stmt *u, std::vector<Value*> *IVS = NULL,
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000261 const char *iterator = NULL, isl_set *scatteringDomain = 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000262
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000263 void codegen(const clast_block *b);
Tobias Grosser75805372011-04-29 06:27:02 +0000264
265 /// @brief Create a classical sequential loop.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000266 void codegenForSequential(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000267
268 /// @brief Create OpenMP structure values.
269 ///
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000270 /// Create a list of values that has to be stored into the OpenMP subfuncition
Tobias Grosser75805372011-04-29 06:27:02 +0000271 /// structure.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000272 SetVector<Value*> getOMPValues();
Tobias Grosser75805372011-04-29 06:27:02 +0000273
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000274 /// @brief Update the internal structures according to a Value Map.
275 ///
276 /// @param VMap A map from old to new values.
277 /// @param Reverse If true, we assume the update should be reversed.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000278 void updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap,
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000279 bool Reverse);
Tobias Grosser75805372011-04-29 06:27:02 +0000280
281 /// @brief Create an OpenMP parallel for loop.
282 ///
283 /// This loop reflects a loop as if it would have been created by an OpenMP
284 /// statement.
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000285 void codegenForOpenMP(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000286
Tobias Grossere192b232012-05-22 08:46:07 +0000287 /// @brief Check if a loop is parallel
288 ///
289 /// Detect if a clast_for loop can be executed in parallel.
290 ///
291 /// @param f The clast for loop to check.
292 ///
293 /// @return bool Returns true if the incoming clast_for statement can
294 /// execute in parallel.
295 bool isParallelFor(const clast_for *For);
296
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000297 bool isInnermostLoop(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000298
299 /// @brief Get the number of loop iterations for this loop.
300 /// @param f The clast for loop to check.
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000301 int getNumberOfIterations(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000302
303 /// @brief Create vector instructions for this loop.
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000304 void codegenForVector(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000305
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000306 void codegen(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000307
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000308 Value *codegen(const clast_equation *eq);
Tobias Grosser75805372011-04-29 06:27:02 +0000309
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000310 void codegen(const clast_guard *g);
Tobias Grosser75805372011-04-29 06:27:02 +0000311
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000312 void codegen(const clast_stmt *stmt);
Tobias Grosser75805372011-04-29 06:27:02 +0000313
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000314 void addParameters(const CloogNames *names);
Tobias Grosser75805372011-04-29 06:27:02 +0000315
Tobias Grossere9ffea22012-03-15 09:34:48 +0000316 IntegerType *getIntPtrTy();
317
Tobias Grosser75805372011-04-29 06:27:02 +0000318 public:
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000319 void codegen(const clast_root *r);
Tobias Grosser75805372011-04-29 06:27:02 +0000320
Tobias Grosserd596b372012-03-15 09:34:52 +0000321 ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P);
Tobias Grosser75805372011-04-29 06:27:02 +0000322};
323}
324
Tobias Grossere9ffea22012-03-15 09:34:48 +0000325IntegerType *ClastStmtCodeGen::getIntPtrTy() {
326 return P->getAnalysis<TargetData>().getIntPtrType(Builder.getContext());
327}
328
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000329const std::vector<std::string> &ClastStmtCodeGen::getParallelLoops() {
330 return parallelLoops;
331}
332
333void ClastStmtCodeGen::codegen(const clast_assignment *a) {
Tobias Grossere9ffea22012-03-15 09:34:48 +0000334 Value *V= ExpGen.codegen(a->RHS, getIntPtrTy());
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000335 ClastVars[a->LHS] = V;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000336}
337
Tobias Grosserf00bd962012-04-02 12:26:13 +0000338void ClastStmtCodeGen::codegen(const clast_assignment *A, ScopStmt *Stmt,
339 unsigned Dim, int VectorDim,
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000340 std::vector<ValueMapT> *VectorVMap) {
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000341 const PHINode *PN;
Tobias Grosserf00bd962012-04-02 12:26:13 +0000342 Value *RHS;
343
344 assert(!A->LHS && "Statement assignments do not have left hand side");
345
Tobias Grosserf00bd962012-04-02 12:26:13 +0000346 PN = Stmt->getInductionVariableForDimension(Dim);
Tobias Grosser0905a232012-04-03 12:24:32 +0000347 RHS = ExpGen.codegen(A->RHS, Builder.getInt64Ty());
348 RHS = Builder.CreateTruncOrBitCast(RHS, PN->getType());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000349
350 if (VectorVMap)
Tobias Grosserf00bd962012-04-02 12:26:13 +0000351 (*VectorVMap)[VectorDim][PN] = RHS;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000352
Tobias Grosserf00bd962012-04-02 12:26:13 +0000353 ValueMap[PN] = RHS;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000354}
355
356void ClastStmtCodeGen::codegenSubstitutions(const clast_stmt *Assignment,
357 ScopStmt *Statement, int vectorDim,
358 std::vector<ValueMapT> *VectorVMap) {
359 int Dimension = 0;
360
361 while (Assignment) {
362 assert(CLAST_STMT_IS_A(Assignment, stmt_ass)
363 && "Substitions are expected to be assignments");
364 codegen((const clast_assignment *)Assignment, Statement, Dimension,
365 vectorDim, VectorVMap);
366 Assignment = Assignment->next;
367 Dimension++;
368 }
369}
370
371void ClastStmtCodeGen::codegen(const clast_user_stmt *u,
372 std::vector<Value*> *IVS , const char *iterator,
Tobias Grosser14bcbd52012-03-02 11:26:52 +0000373 isl_set *Domain) {
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000374 ScopStmt *Statement = (ScopStmt *)u->statement->usr;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000375
376 if (u->substitutions)
377 codegenSubstitutions(u->substitutions, Statement);
378
Tobias Grosser80998e72012-03-02 11:27:28 +0000379 int VectorDimensions = IVS ? IVS->size() : 1;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000380
Tobias Grosser80998e72012-03-02 11:27:28 +0000381 if (VectorDimensions == 1) {
Tobias Grosser55d52082012-03-02 15:20:39 +0000382 BlockGenerator::generate(Builder, *Statement, ValueMap, P);
Tobias Grosser80998e72012-03-02 11:27:28 +0000383 return;
384 }
385
386 VectorValueMapT VectorMap(VectorDimensions);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000387
388 if (IVS) {
389 assert (u->substitutions && "Substitutions expected!");
390 int i = 0;
391 for (std::vector<Value*>::iterator II = IVS->begin(), IE = IVS->end();
392 II != IE; ++II) {
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000393 ClastVars[iterator] = *II;
Tobias Grosser14bcbd52012-03-02 11:26:52 +0000394 codegenSubstitutions(u->substitutions, Statement, i, &VectorMap);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000395 i++;
396 }
397 }
398
Tobias Grosser55d52082012-03-02 15:20:39 +0000399 VectorBlockGenerator::generate(Builder, *Statement, VectorMap, Domain, P);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000400}
401
402void ClastStmtCodeGen::codegen(const clast_block *b) {
403 if (b->body)
404 codegen(b->body);
405}
406
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000407void ClastStmtCodeGen::codegenForSequential(const clast_for *f) {
408 Value *LowerBound, *UpperBound, *IV, *Stride;
Tobias Grosser0ac92142012-02-14 14:02:27 +0000409 BasicBlock *AfterBB;
Tobias Grossere9ffea22012-03-15 09:34:48 +0000410 Type *IntPtrTy = getIntPtrTy();
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000411
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000412 LowerBound = ExpGen.codegen(f->LB, IntPtrTy);
413 UpperBound = ExpGen.codegen(f->UB, IntPtrTy);
414 Stride = Builder.getInt(APInt_from_MPZ(f->stride));
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000415
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000416 IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000417
418 // Add loop iv to symbols.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000419 ClastVars[f->iterator] = IV;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000420
421 if (f->body)
422 codegen(f->body);
423
424 // Loop is finished, so remove its iv from the live symbols.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000425 ClastVars.erase(f->iterator);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000426 Builder.SetInsertPoint(AfterBB->begin());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000427}
428
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000429SetVector<Value*> ClastStmtCodeGen::getOMPValues() {
430 SetVector<Value*> Values;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000431
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000432 // The clast variables
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000433 for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000434 I != E; I++)
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000435 Values.insert(I->second);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000436
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000437 // The memory reference base addresses
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000438 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) {
439 ScopStmt *Stmt = *SI;
440 for (SmallVector<MemoryAccess*, 8>::iterator I = Stmt->memacc_begin(),
441 E = Stmt->memacc_end(); I != E; ++I) {
442 Value *BaseAddr = const_cast<Value*>((*I)->getBaseAddr());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000443 Values.insert((BaseAddr));
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000444 }
445 }
446
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000447 return Values;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000448}
449
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000450void ClastStmtCodeGen::updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap,
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000451 bool Reverse) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000452 std::set<Value*> Inserted;
453
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000454 if (Reverse) {
455 OMPGenerator::ValueToValueMapTy ReverseMap;
456
457 for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end();
458 I != E; ++I)
459 ReverseMap.insert(std::make_pair(I->second, I->first));
460
461 for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
462 I != E; I++) {
463 ClastVars[I->first] = ReverseMap[I->second];
464 Inserted.insert(I->second);
465 }
466
467 /// FIXME: At the moment we do not reverse the update of the ValueMap.
468 /// This is incomplet, but the failure should be obvious, such that
469 /// we can fix this later.
470 return;
471 }
472
473 for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000474 I != E; I++) {
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000475 ClastVars[I->first] = VMap[I->second];
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000476 Inserted.insert(I->second);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000477 }
478
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000479 for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end();
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000480 I != E; ++I) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000481 if (Inserted.count(I->first))
482 continue;
483
484 ValueMap[I->first] = I->second;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000485 }
486}
487
Tobias Grosser900893d2012-03-29 13:10:26 +0000488static void clearDomtree(Function *F, DominatorTree &DT) {
489 DomTreeNode *N = DT.getNode(&F->getEntryBlock());
490 std::vector<BasicBlock*> Nodes;
491 for (po_iterator<DomTreeNode*> I = po_begin(N), E = po_end(N); I != E; ++I)
492 Nodes.push_back(I->getBlock());
493
494 for (std::vector<BasicBlock*>::iterator I = Nodes.begin(), E = Nodes.end();
495 I != E; ++I)
496 DT.eraseNode(*I);
497}
498
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000499void ClastStmtCodeGen::codegenForOpenMP(const clast_for *For) {
Tobias Grosserebf30082012-03-23 12:20:32 +0000500 Value *Stride, *LB, *UB, *IV;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000501 BasicBlock::iterator LoopBody;
502 IntegerType *IntPtrTy = getIntPtrTy();
503 SetVector<Value*> Values;
504 OMPGenerator::ValueToValueMapTy VMap;
505 OMPGenerator OMPGen(Builder, P);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000506
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000507 Stride = Builder.getInt(APInt_from_MPZ(For->stride));
508 Stride = Builder.CreateSExtOrBitCast(Stride, IntPtrTy);
Tobias Grosserebf30082012-03-23 12:20:32 +0000509 LB = ExpGen.codegen(For->LB, IntPtrTy);
510 UB = ExpGen.codegen(For->UB, IntPtrTy);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000511
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000512 Values = getOMPValues();
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000513
Tobias Grosserebf30082012-03-23 12:20:32 +0000514 IV = OMPGen.createParallelLoop(LB, UB, Stride, Values, VMap, &LoopBody);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000515 BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
516 Builder.SetInsertPoint(LoopBody);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000517
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000518 updateWithValueMap(VMap, /* reverse */ false);
519 ClastVars[For->iterator] = IV;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000520
521 if (For->body)
522 codegen(For->body);
523
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000524 ClastVars.erase(For->iterator);
525 updateWithValueMap(VMap, /* reverse */ true);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000526
Tobias Grosser900893d2012-03-29 13:10:26 +0000527 clearDomtree((*LoopBody).getParent()->getParent(),
528 P->getAnalysis<DominatorTree>());
529
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000530 Builder.SetInsertPoint(AfterLoop);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000531}
532
533bool ClastStmtCodeGen::isInnermostLoop(const clast_for *f) {
534 const clast_stmt *stmt = f->body;
535
536 while (stmt) {
537 if (!CLAST_STMT_IS_A(stmt, stmt_user))
538 return false;
539
540 stmt = stmt->next;
541 }
542
543 return true;
544}
545
546int ClastStmtCodeGen::getNumberOfIterations(const clast_for *f) {
547 isl_set *loopDomain = isl_set_copy(isl_set_from_cloog_domain(f->domain));
548 isl_set *tmp = isl_set_copy(loopDomain);
549
550 // Calculate a map similar to the identity map, but with the last input
551 // and output dimension not related.
552 // [i0, i1, i2, i3] -> [i0, i1, i2, o0]
553 isl_space *Space = isl_set_get_space(loopDomain);
554 Space = isl_space_drop_outputs(Space,
555 isl_set_dim(loopDomain, isl_dim_set) - 2, 1);
556 Space = isl_space_map_from_set(Space);
557 isl_map *identity = isl_map_identity(Space);
558 identity = isl_map_add_dims(identity, isl_dim_in, 1);
559 identity = isl_map_add_dims(identity, isl_dim_out, 1);
560
561 isl_map *map = isl_map_from_domain_and_range(tmp, loopDomain);
562 map = isl_map_intersect(map, identity);
563
564 isl_map *lexmax = isl_map_lexmax(isl_map_copy(map));
565 isl_map *lexmin = isl_map_lexmin(map);
566 isl_map *sub = isl_map_sum(lexmax, isl_map_neg(lexmin));
567
568 isl_set *elements = isl_map_range(sub);
569
570 if (!isl_set_is_singleton(elements)) {
571 isl_set_free(elements);
572 return -1;
573 }
574
575 isl_point *p = isl_set_sample_point(elements);
576
577 isl_int v;
578 isl_int_init(v);
579 isl_point_get_coordinate(p, isl_dim_set, isl_set_n_dim(loopDomain) - 1, &v);
580 int numberIterations = isl_int_get_si(v);
581 isl_int_clear(v);
582 isl_point_free(p);
583
584 return (numberIterations) / isl_int_get_si(f->stride) + 1;
585}
586
Tobias Grosser2da263e2012-03-15 09:34:55 +0000587void ClastStmtCodeGen::codegenForVector(const clast_for *F) {
588 DEBUG(dbgs() << "Vectorizing loop '" << F->iterator << "'\n";);
589 int VectorWidth = getNumberOfIterations(F);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000590
Tobias Grosser2da263e2012-03-15 09:34:55 +0000591 Value *LB = ExpGen.codegen(F->LB, getIntPtrTy());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000592
Tobias Grosser2da263e2012-03-15 09:34:55 +0000593 APInt Stride = APInt_from_MPZ(F->stride);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000594 IntegerType *LoopIVType = dyn_cast<IntegerType>(LB->getType());
595 Stride = Stride.zext(LoopIVType->getBitWidth());
596 Value *StrideValue = ConstantInt::get(LoopIVType, Stride);
597
Tobias Grosser2da263e2012-03-15 09:34:55 +0000598 std::vector<Value*> IVS(VectorWidth);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000599 IVS[0] = LB;
600
Tobias Grosser2da263e2012-03-15 09:34:55 +0000601 for (int i = 1; i < VectorWidth; i++)
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000602 IVS[i] = Builder.CreateAdd(IVS[i-1], StrideValue, "p_vector_iv");
603
Tobias Grosser00d898d2012-03-15 09:34:58 +0000604 isl_set *Domain = isl_set_from_cloog_domain(F->domain);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000605
606 // Add loop iv to symbols.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000607 ClastVars[F->iterator] = LB;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000608
Tobias Grosser2da263e2012-03-15 09:34:55 +0000609 const clast_stmt *Stmt = F->body;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000610
Tobias Grosser2da263e2012-03-15 09:34:55 +0000611 while (Stmt) {
Tobias Grosser00d898d2012-03-15 09:34:58 +0000612 codegen((const clast_user_stmt *)Stmt, &IVS, F->iterator,
613 isl_set_copy(Domain));
Tobias Grosser2da263e2012-03-15 09:34:55 +0000614 Stmt = Stmt->next;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000615 }
616
617 // Loop is finished, so remove its iv from the live symbols.
Tobias Grosser00d898d2012-03-15 09:34:58 +0000618 isl_set_free(Domain);
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000619 ClastVars.erase(F->iterator);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000620}
621
Tobias Grossere192b232012-05-22 08:46:07 +0000622
623bool ClastStmtCodeGen::isParallelFor(const clast_for *f) {
624 isl_set *Domain = isl_set_from_cloog_domain(f->domain);
625 assert(Domain && "Cannot access domain of loop");
626
627 Dependences &D = P->getAnalysis<Dependences>();
628
629 return D.isParallelDimension(isl_set_copy(Domain), isl_set_n_dim(Domain));
630}
631
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000632void ClastStmtCodeGen::codegen(const clast_for *f) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000633 bool Vector = PollyVectorizerChoice != VECTORIZER_NONE;
Tobias Grossere192b232012-05-22 08:46:07 +0000634 if ((Vector || OpenMP) && isParallelFor(f)) {
Tobias Grosserce3f5372012-03-02 11:26:42 +0000635 if (Vector && isInnermostLoop(f) && (-1 != getNumberOfIterations(f))
636 && (getNumberOfIterations(f) <= 16)) {
637 codegenForVector(f);
638 return;
639 }
640
641 if (OpenMP && !parallelCodeGeneration) {
642 parallelCodeGeneration = true;
643 parallelLoops.push_back(f->iterator);
644 codegenForOpenMP(f);
645 parallelCodeGeneration = false;
646 return;
647 }
648 }
649
650 codegenForSequential(f);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000651}
652
653Value *ClastStmtCodeGen::codegen(const clast_equation *eq) {
Tobias Grossere9ffea22012-03-15 09:34:48 +0000654 Value *LHS = ExpGen.codegen(eq->LHS, getIntPtrTy());
655 Value *RHS = ExpGen.codegen(eq->RHS, getIntPtrTy());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000656 CmpInst::Predicate P;
657
658 if (eq->sign == 0)
659 P = ICmpInst::ICMP_EQ;
660 else if (eq->sign > 0)
661 P = ICmpInst::ICMP_SGE;
662 else
663 P = ICmpInst::ICMP_SLE;
664
665 return Builder.CreateICmp(P, LHS, RHS);
666}
667
668void ClastStmtCodeGen::codegen(const clast_guard *g) {
669 Function *F = Builder.GetInsertBlock()->getParent();
670 LLVMContext &Context = F->getContext();
Tobias Grosser0ac92142012-02-14 14:02:27 +0000671
672 BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
673 Builder.GetInsertPoint(), P);
674 CondBB->setName("polly.cond");
675 BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
676 MergeBB->setName("polly.merge");
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000677 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000678
Tobias Grosserd596b372012-03-15 09:34:52 +0000679 DominatorTree &DT = P->getAnalysis<DominatorTree>();
680 DT.addNewBlock(ThenBB, CondBB);
681 DT.changeImmediateDominator(MergeBB, CondBB);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000682
683 CondBB->getTerminator()->eraseFromParent();
684
685 Builder.SetInsertPoint(CondBB);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000686
687 Value *Predicate = codegen(&(g->eq[0]));
688
689 for (int i = 1; i < g->n; ++i) {
690 Value *TmpPredicate = codegen(&(g->eq[i]));
691 Predicate = Builder.CreateAnd(Predicate, TmpPredicate);
692 }
693
694 Builder.CreateCondBr(Predicate, ThenBB, MergeBB);
695 Builder.SetInsertPoint(ThenBB);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000696 Builder.CreateBr(MergeBB);
697 Builder.SetInsertPoint(ThenBB->begin());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000698
699 codegen(g->then);
Tobias Grosser62a3c962012-02-16 09:56:21 +0000700
701 Builder.SetInsertPoint(MergeBB->begin());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000702}
703
704void ClastStmtCodeGen::codegen(const clast_stmt *stmt) {
705 if (CLAST_STMT_IS_A(stmt, stmt_root))
706 assert(false && "No second root statement expected");
707 else if (CLAST_STMT_IS_A(stmt, stmt_ass))
708 codegen((const clast_assignment *)stmt);
709 else if (CLAST_STMT_IS_A(stmt, stmt_user))
710 codegen((const clast_user_stmt *)stmt);
711 else if (CLAST_STMT_IS_A(stmt, stmt_block))
712 codegen((const clast_block *)stmt);
713 else if (CLAST_STMT_IS_A(stmt, stmt_for))
714 codegen((const clast_for *)stmt);
715 else if (CLAST_STMT_IS_A(stmt, stmt_guard))
716 codegen((const clast_guard *)stmt);
717
718 if (stmt->next)
719 codegen(stmt->next);
720}
721
722void ClastStmtCodeGen::addParameters(const CloogNames *names) {
Tobias Grosserd596b372012-03-15 09:34:52 +0000723 SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly");
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000724
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000725 int i = 0;
726 for (Scop::param_iterator PI = S->param_begin(), PE = S->param_end();
727 PI != PE; ++PI) {
728 assert(i < names->nb_parameters && "Not enough parameter names");
729
730 const SCEV *Param = *PI;
731 Type *Ty = Param->getType();
732
733 Instruction *insertLocation = --(Builder.GetInsertBlock()->end());
734 Value *V = Rewriter.expandCodeFor(Param, Ty, insertLocation);
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000735 ClastVars[names->parameters[i]] = V;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000736
737 ++i;
738 }
739}
740
741void ClastStmtCodeGen::codegen(const clast_root *r) {
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000742 addParameters(r->names);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000743
744 parallelCodeGeneration = false;
745
746 const clast_stmt *stmt = (const clast_stmt*) r;
747 if (stmt->next)
748 codegen(stmt->next);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000749}
750
Tobias Grosserd596b372012-03-15 09:34:52 +0000751ClastStmtCodeGen::ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P) :
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000752 S(scop), P(P), Builder(B), ExpGen(Builder, ClastVars) {}
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000753
Tobias Grosser75805372011-04-29 06:27:02 +0000754namespace {
755class CodeGeneration : public ScopPass {
Tobias Grosser3a275d22012-05-29 09:11:54 +0000756 std::vector<std::string> ParallelLoops;
Tobias Grosser75805372011-04-29 06:27:02 +0000757
758 public:
759 static char ID;
760
761 CodeGeneration() : ScopPass(ID) {}
762
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000763
Tobias Grosser3a275d22012-05-29 09:11:54 +0000764 bool runOnScop(Scop &S) {
765 ParallelLoops.clear();
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000766
Tobias Grosser3a275d22012-05-29 09:11:54 +0000767 assert(S.getRegion().isSimple() && "Only simple regions are supported");
Tobias Grossercb47dfe2012-02-15 09:58:50 +0000768
Tobias Grosser3a275d22012-05-29 09:11:54 +0000769 BasicBlock *StartBlock = executeScopConditionally(S, this);
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000770
Tobias Grosser3a275d22012-05-29 09:11:54 +0000771 IRBuilder<> Builder(StartBlock->begin());
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000772
Tobias Grosser3a275d22012-05-29 09:11:54 +0000773 ClastStmtCodeGen CodeGen(&S, Builder, this);
Tobias Grosser3fdecae2011-05-14 19:02:39 +0000774 CloogInfo &C = getAnalysis<CloogInfo>();
775 CodeGen.codegen(C.getClast());
Tobias Grosser75805372011-04-29 06:27:02 +0000776
Tobias Grosser3a275d22012-05-29 09:11:54 +0000777 ParallelLoops.insert(ParallelLoops.begin(),
Tobias Grosser75805372011-04-29 06:27:02 +0000778 CodeGen.getParallelLoops().begin(),
779 CodeGen.getParallelLoops().end());
Tobias Grosserabb6dcd2011-05-14 19:02:34 +0000780 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000781 }
782
783 virtual void printScop(raw_ostream &OS) const {
Tobias Grosser3a275d22012-05-29 09:11:54 +0000784 for (std::vector<std::string>::const_iterator PI = ParallelLoops.begin(),
785 PE = ParallelLoops.end(); PI != PE; ++PI)
Tobias Grosser75805372011-04-29 06:27:02 +0000786 OS << "Parallel loop with iterator '" << *PI << "' generated\n";
787 }
788
789 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
790 AU.addRequired<CloogInfo>();
791 AU.addRequired<Dependences>();
792 AU.addRequired<DominatorTree>();
Tobias Grosser75805372011-04-29 06:27:02 +0000793 AU.addRequired<RegionInfo>();
Tobias Grosser73600b82011-10-08 00:30:40 +0000794 AU.addRequired<ScalarEvolution>();
Tobias Grosser75805372011-04-29 06:27:02 +0000795 AU.addRequired<ScopDetection>();
796 AU.addRequired<ScopInfo>();
797 AU.addRequired<TargetData>();
798
799 AU.addPreserved<CloogInfo>();
800 AU.addPreserved<Dependences>();
Tobias Grosser5d6eb862011-05-14 19:02:45 +0000801
Tobias Grosser4e3f9a42011-05-23 15:23:36 +0000802 // FIXME: We do not create LoopInfo for the newly generated loops.
Tobias Grosser75805372011-04-29 06:27:02 +0000803 AU.addPreserved<LoopInfo>();
804 AU.addPreserved<DominatorTree>();
Tobias Grosser75805372011-04-29 06:27:02 +0000805 AU.addPreserved<ScopDetection>();
806 AU.addPreserved<ScalarEvolution>();
Tobias Grosser5d6eb862011-05-14 19:02:45 +0000807
Tobias Grosser4e3f9a42011-05-23 15:23:36 +0000808 // FIXME: We do not yet add regions for the newly generated code to the
809 // region tree.
Tobias Grosser75805372011-04-29 06:27:02 +0000810 AU.addPreserved<RegionInfo>();
811 AU.addPreserved<TempScopInfo>();
812 AU.addPreserved<ScopInfo>();
813 AU.addPreservedID(IndependentBlocksID);
814 }
815};
816}
817
818char CodeGeneration::ID = 1;
819
Tobias Grosser73600b82011-10-08 00:30:40 +0000820INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
Tobias Grosser3c2efba2012-03-06 07:38:57 +0000821 "Polly - Create LLVM-IR from SCoPs", false, false)
Tobias Grosser73600b82011-10-08 00:30:40 +0000822INITIALIZE_PASS_DEPENDENCY(CloogInfo)
823INITIALIZE_PASS_DEPENDENCY(Dependences)
824INITIALIZE_PASS_DEPENDENCY(DominatorTree)
825INITIALIZE_PASS_DEPENDENCY(RegionInfo)
826INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
827INITIALIZE_PASS_DEPENDENCY(ScopDetection)
828INITIALIZE_PASS_DEPENDENCY(TargetData)
829INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
Tobias Grosser3c2efba2012-03-06 07:38:57 +0000830 "Polly - Create LLVM-IR from SCoPs", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000831
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000832Pass *polly::createCodeGenerationPass() {
Tobias Grosser75805372011-04-29 06:27:02 +0000833 return new CodeGeneration();
834}
Sebastian Pope1f65542012-05-07 16:35:11 +0000835
836#endif // CLOOG_FOUND