blob: 9d39ec2ff0ab319233c561540b39bae229567cc7 [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 Grosser75805372011-04-29 06:27:02 +000023#include "polly/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"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000034#include "polly/Support/GICHelper.h"
Tobias Grosserbda1f8f2012-02-01 14:23:29 +000035
36#include "llvm/Module.h"
37#include "llvm/ADT/SetVector.h"
Tobias Grosser900893d2012-03-29 13:10:26 +000038#include "llvm/ADT/PostOrderIterator.h"
Tobias Grosserbda1f8f2012-02-01 14:23:29 +000039#include "llvm/Analysis/LoopInfo.h"
40#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser75805372011-04-29 06:27:02 +000041#include "llvm/Support/CommandLine.h"
42#include "llvm/Support/Debug.h"
Tobias Grosser75805372011-04-29 06:27:02 +000043#include "llvm/Target/TargetData.h"
Tobias Grosserbda1f8f2012-02-01 14:23:29 +000044#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Tobias Grosser75805372011-04-29 06:27:02 +000045
46#define CLOOG_INT_GMP 1
47#include "cloog/cloog.h"
48#include "cloog/isl/cloog.h"
49
Raghesh Aloora71989c2011-12-28 02:48:26 +000050#include "isl/aff.h"
51
Tobias Grosser75805372011-04-29 06:27:02 +000052#include <vector>
53#include <utility>
54
55using namespace polly;
56using namespace llvm;
57
58struct isl_set;
59
60namespace polly {
Tobias Grosser75805372011-04-29 06:27:02 +000061static cl::opt<bool>
62OpenMP("enable-polly-openmp",
63 cl::desc("Generate OpenMP parallel code"), cl::Hidden,
64 cl::value_desc("OpenMP code generation enabled if true"),
Tobias Grosser0acfcdb2012-03-16 17:17:16 +000065 cl::init(false), cl::ZeroOrMore);
Tobias Grosser75805372011-04-29 06:27:02 +000066
67static cl::opt<bool>
68AtLeastOnce("enable-polly-atLeastOnce",
69 cl::desc("Give polly the hint, that every loop is executed at least"
70 "once"), cl::Hidden,
71 cl::value_desc("OpenMP code generation enabled if true"),
Tobias Grosser0acfcdb2012-03-16 17:17:16 +000072 cl::init(false), cl::ZeroOrMore);
Tobias Grosser75805372011-04-29 06:27:02 +000073
Tobias Grosser75805372011-04-29 06:27:02 +000074typedef DenseMap<const char*, Value*> CharMapT;
Tobias Grosser70e8cdb2012-01-24 16:42:21 +000075
Tobias Grosser75805372011-04-29 06:27:02 +000076/// Class to generate LLVM-IR that calculates the value of a clast_expr.
77class ClastExpCodeGen {
78 IRBuilder<> &Builder;
Tobias Grosser5e8ffa82012-03-23 12:20:36 +000079 const CharMapT &IVS;
Tobias Grosser75805372011-04-29 06:27:02 +000080
Tobias Grosserbb137e32012-01-24 16:42:28 +000081 Value *codegen(const clast_name *e, Type *Ty);
82 Value *codegen(const clast_term *e, Type *Ty);
83 Value *codegen(const clast_binary *e, Type *Ty);
84 Value *codegen(const clast_reduction *r, Type *Ty);
Tobias Grosser75805372011-04-29 06:27:02 +000085public:
86
87 // A generator for clast expressions.
88 //
89 // @param B The IRBuilder that defines where the code to calculate the
90 // clast expressions should be inserted.
91 // @param IVMAP A Map that translates strings describing the induction
92 // variables to the Values* that represent these variables
93 // on the LLVM side.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +000094 ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap);
Tobias Grosser75805372011-04-29 06:27:02 +000095
96 // Generates code to calculate a given clast expression.
97 //
98 // @param e The expression to calculate.
99 // @return The Value that holds the result.
Tobias Grosserbb137e32012-01-24 16:42:28 +0000100 Value *codegen(const clast_expr *e, Type *Ty);
Tobias Grosserbb137e32012-01-24 16:42:28 +0000101};
102
103Value *ClastExpCodeGen::codegen(const clast_name *e, Type *Ty) {
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000104 CharMapT::const_iterator I = IVS.find(e->name);
Tobias Grosserbb137e32012-01-24 16:42:28 +0000105
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000106 assert(I != IVS.end() && "Clast name not found");
Tobias Grosserbb137e32012-01-24 16:42:28 +0000107
108 return Builder.CreateSExtOrBitCast(I->second, Ty);
109}
110
111Value *ClastExpCodeGen::codegen(const clast_term *e, Type *Ty) {
112 APInt a = APInt_from_MPZ(e->val);
113
114 Value *ConstOne = ConstantInt::get(Builder.getContext(), a);
115 ConstOne = Builder.CreateSExtOrBitCast(ConstOne, Ty);
116
117 if (!e->var)
118 return ConstOne;
119
120 Value *var = codegen(e->var, Ty);
121 return Builder.CreateMul(ConstOne, var);
122}
123
124Value *ClastExpCodeGen::codegen(const clast_binary *e, Type *Ty) {
125 Value *LHS = codegen(e->LHS, Ty);
126
127 APInt RHS_AP = APInt_from_MPZ(e->RHS);
128
129 Value *RHS = ConstantInt::get(Builder.getContext(), RHS_AP);
130 RHS = Builder.CreateSExtOrBitCast(RHS, Ty);
131
132 switch (e->type) {
133 case clast_bin_mod:
134 return Builder.CreateSRem(LHS, RHS);
135 case clast_bin_fdiv:
136 {
Tobias Grosser9a44b972012-02-16 14:13:19 +0000137 // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d
Tobias Grosser906eafe2012-02-16 09:56:10 +0000138 Value *One = ConstantInt::get(Ty, 1);
139 Value *Zero = ConstantInt::get(Ty, 0);
Tobias Grosser9a44b972012-02-16 14:13:19 +0000140 Value *Sum1 = Builder.CreateSub(LHS, RHS);
141 Value *Sum2 = Builder.CreateAdd(Sum1, One);
142 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
143 Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS);
144 return Builder.CreateSDiv(Dividend, RHS);
Tobias Grosserbb137e32012-01-24 16:42:28 +0000145 }
146 case clast_bin_cdiv:
147 {
Tobias Grosser9a44b972012-02-16 14:13:19 +0000148 // ceild(n,d) ((n < 0) ? n : (n + d - 1)) / d
149 Value *One = ConstantInt::get(Ty, 1);
Tobias Grosser906eafe2012-02-16 09:56:10 +0000150 Value *Zero = ConstantInt::get(Ty, 0);
Tobias Grosser9a44b972012-02-16 14:13:19 +0000151 Value *Sum1 = Builder.CreateAdd(LHS, RHS);
152 Value *Sum2 = Builder.CreateSub(Sum1, One);
153 Value *isNegative = Builder.CreateICmpSLT(LHS, Zero);
154 Value *Dividend = Builder.CreateSelect(isNegative, LHS, Sum2);
155 return Builder.CreateSDiv(Dividend, RHS);
Tobias Grosserbb137e32012-01-24 16:42:28 +0000156 }
157 case clast_bin_div:
158 return Builder.CreateSDiv(LHS, RHS);
159 };
160
161 llvm_unreachable("Unknown clast binary expression type");
162}
163
164Value *ClastExpCodeGen::codegen(const clast_reduction *r, Type *Ty) {
165 assert(( r->type == clast_red_min
166 || r->type == clast_red_max
167 || r->type == clast_red_sum)
168 && "Clast reduction type not supported");
169 Value *old = codegen(r->elts[0], Ty);
170
171 for (int i=1; i < r->n; ++i) {
172 Value *exprValue = codegen(r->elts[i], Ty);
173
174 switch (r->type) {
175 case clast_red_min:
176 {
177 Value *cmp = Builder.CreateICmpSLT(old, exprValue);
178 old = Builder.CreateSelect(cmp, old, exprValue);
179 break;
180 }
181 case clast_red_max:
182 {
183 Value *cmp = Builder.CreateICmpSGT(old, exprValue);
184 old = Builder.CreateSelect(cmp, old, exprValue);
185 break;
186 }
187 case clast_red_sum:
188 old = Builder.CreateAdd(old, exprValue);
189 break;
Tobias Grosserbb137e32012-01-24 16:42:28 +0000190 }
Tobias Grosser75805372011-04-29 06:27:02 +0000191 }
192
Tobias Grosserbb137e32012-01-24 16:42:28 +0000193 return old;
194}
195
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000196ClastExpCodeGen::ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap)
Tobias Grosserbb137e32012-01-24 16:42:28 +0000197 : Builder(B), IVS(IVMap) {}
198
199Value *ClastExpCodeGen::codegen(const clast_expr *e, Type *Ty) {
200 switch(e->type) {
201 case clast_expr_name:
202 return codegen((const clast_name *)e, Ty);
203 case clast_expr_term:
204 return codegen((const clast_term *)e, Ty);
205 case clast_expr_bin:
206 return codegen((const clast_binary *)e, Ty);
207 case clast_expr_red:
208 return codegen((const clast_reduction *)e, Ty);
209 }
210
211 llvm_unreachable("Unknown clast expression!");
212}
213
Tobias Grosser75805372011-04-29 06:27:02 +0000214class ClastStmtCodeGen {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000215public:
216 const std::vector<std::string> &getParallelLoops();
217
218private:
Tobias Grosser75805372011-04-29 06:27:02 +0000219 // The Scop we code generate.
220 Scop *S;
Tobias Grosser0ac92142012-02-14 14:02:27 +0000221 Pass *P;
Tobias Grosser75805372011-04-29 06:27:02 +0000222
223 // The Builder specifies the current location to code generate at.
224 IRBuilder<> &Builder;
225
226 // Map the Values from the old code to their counterparts in the new code.
227 ValueMapT ValueMap;
228
229 // clastVars maps from the textual representation of a clast variable to its
230 // current *Value. clast variables are scheduling variables, original
231 // induction variables or parameters. They are used either in loop bounds or
232 // to define the statement instance that is executed.
233 //
234 // for (s = 0; s < n + 3; ++i)
235 // for (t = s; t < m; ++j)
236 // Stmt(i = s + 3 * m, j = t);
237 //
238 // {s,t,i,j,n,m} is the set of clast variables in this clast.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000239 CharMapT ClastVars;
Tobias Grosser75805372011-04-29 06:27:02 +0000240
241 // Codegenerator for clast expressions.
242 ClastExpCodeGen ExpGen;
243
244 // Do we currently generate parallel code?
245 bool parallelCodeGeneration;
246
247 std::vector<std::string> parallelLoops;
248
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000249 void codegen(const clast_assignment *a);
Tobias Grosser75805372011-04-29 06:27:02 +0000250
251 void codegen(const clast_assignment *a, ScopStmt *Statement,
252 unsigned Dimension, int vectorDim,
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000253 std::vector<ValueMapT> *VectorVMap = 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000254
255 void codegenSubstitutions(const clast_stmt *Assignment,
256 ScopStmt *Statement, int vectorDim = 0,
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000257 std::vector<ValueMapT> *VectorVMap = 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000258
259 void codegen(const clast_user_stmt *u, std::vector<Value*> *IVS = NULL,
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000260 const char *iterator = NULL, isl_set *scatteringDomain = 0);
Tobias Grosser75805372011-04-29 06:27:02 +0000261
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000262 void codegen(const clast_block *b);
Tobias Grosser75805372011-04-29 06:27:02 +0000263
264 /// @brief Create a classical sequential loop.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000265 void codegenForSequential(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000266
267 /// @brief Create OpenMP structure values.
268 ///
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000269 /// Create a list of values that has to be stored into the OpenMP subfuncition
Tobias Grosser75805372011-04-29 06:27:02 +0000270 /// structure.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000271 SetVector<Value*> getOMPValues();
Tobias Grosser75805372011-04-29 06:27:02 +0000272
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000273 /// @brief Update the internal structures according to a Value Map.
274 ///
275 /// @param VMap A map from old to new values.
276 /// @param Reverse If true, we assume the update should be reversed.
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000277 void updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap,
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000278 bool Reverse);
Tobias Grosser75805372011-04-29 06:27:02 +0000279
280 /// @brief Create an OpenMP parallel for loop.
281 ///
282 /// This loop reflects a loop as if it would have been created by an OpenMP
283 /// statement.
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000284 void codegenForOpenMP(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000285
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000286 bool isInnermostLoop(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000287
288 /// @brief Get the number of loop iterations for this loop.
289 /// @param f The clast for loop to check.
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000290 int getNumberOfIterations(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000291
292 /// @brief Create vector instructions for this loop.
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000293 void codegenForVector(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000294
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000295 void codegen(const clast_for *f);
Tobias Grosser75805372011-04-29 06:27:02 +0000296
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000297 Value *codegen(const clast_equation *eq);
Tobias Grosser75805372011-04-29 06:27:02 +0000298
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000299 void codegen(const clast_guard *g);
Tobias Grosser75805372011-04-29 06:27:02 +0000300
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000301 void codegen(const clast_stmt *stmt);
Tobias Grosser75805372011-04-29 06:27:02 +0000302
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000303 void addParameters(const CloogNames *names);
Tobias Grosser75805372011-04-29 06:27:02 +0000304
Tobias Grossere9ffea22012-03-15 09:34:48 +0000305 IntegerType *getIntPtrTy();
306
Tobias Grosser75805372011-04-29 06:27:02 +0000307 public:
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000308 void codegen(const clast_root *r);
Tobias Grosser75805372011-04-29 06:27:02 +0000309
Tobias Grosserd596b372012-03-15 09:34:52 +0000310 ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P);
Tobias Grosser75805372011-04-29 06:27:02 +0000311};
312}
313
Tobias Grossere9ffea22012-03-15 09:34:48 +0000314IntegerType *ClastStmtCodeGen::getIntPtrTy() {
315 return P->getAnalysis<TargetData>().getIntPtrType(Builder.getContext());
316}
317
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000318const std::vector<std::string> &ClastStmtCodeGen::getParallelLoops() {
319 return parallelLoops;
320}
321
322void ClastStmtCodeGen::codegen(const clast_assignment *a) {
Tobias Grossere9ffea22012-03-15 09:34:48 +0000323 Value *V= ExpGen.codegen(a->RHS, getIntPtrTy());
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000324 ClastVars[a->LHS] = V;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000325}
326
Tobias Grosserf00bd962012-04-02 12:26:13 +0000327void ClastStmtCodeGen::codegen(const clast_assignment *A, ScopStmt *Stmt,
328 unsigned Dim, int VectorDim,
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000329 std::vector<ValueMapT> *VectorVMap) {
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000330 const PHINode *PN;
Tobias Grosserf00bd962012-04-02 12:26:13 +0000331 Value *RHS;
332
333 assert(!A->LHS && "Statement assignments do not have left hand side");
334
Tobias Grosserf00bd962012-04-02 12:26:13 +0000335 PN = Stmt->getInductionVariableForDimension(Dim);
Tobias Grosser0905a232012-04-03 12:24:32 +0000336 RHS = ExpGen.codegen(A->RHS, Builder.getInt64Ty());
337 RHS = Builder.CreateTruncOrBitCast(RHS, PN->getType());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000338
339 if (VectorVMap)
Tobias Grosserf00bd962012-04-02 12:26:13 +0000340 (*VectorVMap)[VectorDim][PN] = RHS;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000341
Tobias Grosserf00bd962012-04-02 12:26:13 +0000342 ValueMap[PN] = RHS;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000343}
344
345void ClastStmtCodeGen::codegenSubstitutions(const clast_stmt *Assignment,
346 ScopStmt *Statement, int vectorDim,
347 std::vector<ValueMapT> *VectorVMap) {
348 int Dimension = 0;
349
350 while (Assignment) {
351 assert(CLAST_STMT_IS_A(Assignment, stmt_ass)
352 && "Substitions are expected to be assignments");
353 codegen((const clast_assignment *)Assignment, Statement, Dimension,
354 vectorDim, VectorVMap);
355 Assignment = Assignment->next;
356 Dimension++;
357 }
358}
359
360void ClastStmtCodeGen::codegen(const clast_user_stmt *u,
361 std::vector<Value*> *IVS , const char *iterator,
Tobias Grosser14bcbd52012-03-02 11:26:52 +0000362 isl_set *Domain) {
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000363 ScopStmt *Statement = (ScopStmt *)u->statement->usr;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000364
365 if (u->substitutions)
366 codegenSubstitutions(u->substitutions, Statement);
367
Tobias Grosser80998e72012-03-02 11:27:28 +0000368 int VectorDimensions = IVS ? IVS->size() : 1;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000369
Tobias Grosser80998e72012-03-02 11:27:28 +0000370 if (VectorDimensions == 1) {
Tobias Grosser55d52082012-03-02 15:20:39 +0000371 BlockGenerator::generate(Builder, *Statement, ValueMap, P);
Tobias Grosser80998e72012-03-02 11:27:28 +0000372 return;
373 }
374
375 VectorValueMapT VectorMap(VectorDimensions);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000376
377 if (IVS) {
378 assert (u->substitutions && "Substitutions expected!");
379 int i = 0;
380 for (std::vector<Value*>::iterator II = IVS->begin(), IE = IVS->end();
381 II != IE; ++II) {
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000382 ClastVars[iterator] = *II;
Tobias Grosser14bcbd52012-03-02 11:26:52 +0000383 codegenSubstitutions(u->substitutions, Statement, i, &VectorMap);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000384 i++;
385 }
386 }
387
Tobias Grosser55d52082012-03-02 15:20:39 +0000388 VectorBlockGenerator::generate(Builder, *Statement, VectorMap, Domain, P);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000389}
390
391void ClastStmtCodeGen::codegen(const clast_block *b) {
392 if (b->body)
393 codegen(b->body);
394}
395
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000396void ClastStmtCodeGen::codegenForSequential(const clast_for *f) {
397 Value *LowerBound, *UpperBound, *IV, *Stride;
Tobias Grosser0ac92142012-02-14 14:02:27 +0000398 BasicBlock *AfterBB;
Tobias Grossere9ffea22012-03-15 09:34:48 +0000399 Type *IntPtrTy = getIntPtrTy();
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000400
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000401 LowerBound = ExpGen.codegen(f->LB, IntPtrTy);
402 UpperBound = ExpGen.codegen(f->UB, IntPtrTy);
403 Stride = Builder.getInt(APInt_from_MPZ(f->stride));
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000404
Hongbin Zheng4ac4e152012-04-23 13:03:56 +0000405 IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000406
407 // Add loop iv to symbols.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000408 ClastVars[f->iterator] = IV;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000409
410 if (f->body)
411 codegen(f->body);
412
413 // Loop is finished, so remove its iv from the live symbols.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000414 ClastVars.erase(f->iterator);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000415 Builder.SetInsertPoint(AfterBB->begin());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000416}
417
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000418SetVector<Value*> ClastStmtCodeGen::getOMPValues() {
419 SetVector<Value*> Values;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000420
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000421 // The clast variables
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000422 for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000423 I != E; I++)
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000424 Values.insert(I->second);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000425
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000426 // The memory reference base addresses
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000427 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) {
428 ScopStmt *Stmt = *SI;
429 for (SmallVector<MemoryAccess*, 8>::iterator I = Stmt->memacc_begin(),
430 E = Stmt->memacc_end(); I != E; ++I) {
431 Value *BaseAddr = const_cast<Value*>((*I)->getBaseAddr());
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000432 Values.insert((BaseAddr));
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000433 }
434 }
435
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000436 return Values;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000437}
438
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000439void ClastStmtCodeGen::updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap,
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000440 bool Reverse) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000441 std::set<Value*> Inserted;
442
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000443 if (Reverse) {
444 OMPGenerator::ValueToValueMapTy ReverseMap;
445
446 for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end();
447 I != E; ++I)
448 ReverseMap.insert(std::make_pair(I->second, I->first));
449
450 for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
451 I != E; I++) {
452 ClastVars[I->first] = ReverseMap[I->second];
453 Inserted.insert(I->second);
454 }
455
456 /// FIXME: At the moment we do not reverse the update of the ValueMap.
457 /// This is incomplet, but the failure should be obvious, such that
458 /// we can fix this later.
459 return;
460 }
461
462 for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end();
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000463 I != E; I++) {
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000464 ClastVars[I->first] = VMap[I->second];
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000465 Inserted.insert(I->second);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000466 }
467
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000468 for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end();
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000469 I != E; ++I) {
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000470 if (Inserted.count(I->first))
471 continue;
472
473 ValueMap[I->first] = I->second;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000474 }
475}
476
Tobias Grosser900893d2012-03-29 13:10:26 +0000477static void clearDomtree(Function *F, DominatorTree &DT) {
478 DomTreeNode *N = DT.getNode(&F->getEntryBlock());
479 std::vector<BasicBlock*> Nodes;
480 for (po_iterator<DomTreeNode*> I = po_begin(N), E = po_end(N); I != E; ++I)
481 Nodes.push_back(I->getBlock());
482
483 for (std::vector<BasicBlock*>::iterator I = Nodes.begin(), E = Nodes.end();
484 I != E; ++I)
485 DT.eraseNode(*I);
486}
487
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000488void ClastStmtCodeGen::codegenForOpenMP(const clast_for *For) {
Tobias Grosserebf30082012-03-23 12:20:32 +0000489 Value *Stride, *LB, *UB, *IV;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000490 BasicBlock::iterator LoopBody;
491 IntegerType *IntPtrTy = getIntPtrTy();
492 SetVector<Value*> Values;
493 OMPGenerator::ValueToValueMapTy VMap;
494 OMPGenerator OMPGen(Builder, P);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000495
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000496 Stride = Builder.getInt(APInt_from_MPZ(For->stride));
497 Stride = Builder.CreateSExtOrBitCast(Stride, IntPtrTy);
Tobias Grosserebf30082012-03-23 12:20:32 +0000498 LB = ExpGen.codegen(For->LB, IntPtrTy);
499 UB = ExpGen.codegen(For->UB, IntPtrTy);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000500
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000501 Values = getOMPValues();
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000502
Tobias Grosserebf30082012-03-23 12:20:32 +0000503 IV = OMPGen.createParallelLoop(LB, UB, Stride, Values, VMap, &LoopBody);
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000504 BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
505 Builder.SetInsertPoint(LoopBody);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000506
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000507 updateWithValueMap(VMap, /* reverse */ false);
508 ClastVars[For->iterator] = IV;
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000509
510 if (For->body)
511 codegen(For->body);
512
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000513 ClastVars.erase(For->iterator);
514 updateWithValueMap(VMap, /* reverse */ true);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000515
Tobias Grosser900893d2012-03-29 13:10:26 +0000516 clearDomtree((*LoopBody).getParent()->getParent(),
517 P->getAnalysis<DominatorTree>());
518
Tobias Grosserf74a4cd2012-03-23 10:35:18 +0000519 Builder.SetInsertPoint(AfterLoop);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000520}
521
522bool ClastStmtCodeGen::isInnermostLoop(const clast_for *f) {
523 const clast_stmt *stmt = f->body;
524
525 while (stmt) {
526 if (!CLAST_STMT_IS_A(stmt, stmt_user))
527 return false;
528
529 stmt = stmt->next;
530 }
531
532 return true;
533}
534
535int ClastStmtCodeGen::getNumberOfIterations(const clast_for *f) {
536 isl_set *loopDomain = isl_set_copy(isl_set_from_cloog_domain(f->domain));
537 isl_set *tmp = isl_set_copy(loopDomain);
538
539 // Calculate a map similar to the identity map, but with the last input
540 // and output dimension not related.
541 // [i0, i1, i2, i3] -> [i0, i1, i2, o0]
542 isl_space *Space = isl_set_get_space(loopDomain);
543 Space = isl_space_drop_outputs(Space,
544 isl_set_dim(loopDomain, isl_dim_set) - 2, 1);
545 Space = isl_space_map_from_set(Space);
546 isl_map *identity = isl_map_identity(Space);
547 identity = isl_map_add_dims(identity, isl_dim_in, 1);
548 identity = isl_map_add_dims(identity, isl_dim_out, 1);
549
550 isl_map *map = isl_map_from_domain_and_range(tmp, loopDomain);
551 map = isl_map_intersect(map, identity);
552
553 isl_map *lexmax = isl_map_lexmax(isl_map_copy(map));
554 isl_map *lexmin = isl_map_lexmin(map);
555 isl_map *sub = isl_map_sum(lexmax, isl_map_neg(lexmin));
556
557 isl_set *elements = isl_map_range(sub);
558
559 if (!isl_set_is_singleton(elements)) {
560 isl_set_free(elements);
561 return -1;
562 }
563
564 isl_point *p = isl_set_sample_point(elements);
565
566 isl_int v;
567 isl_int_init(v);
568 isl_point_get_coordinate(p, isl_dim_set, isl_set_n_dim(loopDomain) - 1, &v);
569 int numberIterations = isl_int_get_si(v);
570 isl_int_clear(v);
571 isl_point_free(p);
572
573 return (numberIterations) / isl_int_get_si(f->stride) + 1;
574}
575
Tobias Grosser2da263e2012-03-15 09:34:55 +0000576void ClastStmtCodeGen::codegenForVector(const clast_for *F) {
577 DEBUG(dbgs() << "Vectorizing loop '" << F->iterator << "'\n";);
578 int VectorWidth = getNumberOfIterations(F);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000579
Tobias Grosser2da263e2012-03-15 09:34:55 +0000580 Value *LB = ExpGen.codegen(F->LB, getIntPtrTy());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000581
Tobias Grosser2da263e2012-03-15 09:34:55 +0000582 APInt Stride = APInt_from_MPZ(F->stride);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000583 IntegerType *LoopIVType = dyn_cast<IntegerType>(LB->getType());
584 Stride = Stride.zext(LoopIVType->getBitWidth());
585 Value *StrideValue = ConstantInt::get(LoopIVType, Stride);
586
Tobias Grosser2da263e2012-03-15 09:34:55 +0000587 std::vector<Value*> IVS(VectorWidth);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000588 IVS[0] = LB;
589
Tobias Grosser2da263e2012-03-15 09:34:55 +0000590 for (int i = 1; i < VectorWidth; i++)
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000591 IVS[i] = Builder.CreateAdd(IVS[i-1], StrideValue, "p_vector_iv");
592
Tobias Grosser00d898d2012-03-15 09:34:58 +0000593 isl_set *Domain = isl_set_from_cloog_domain(F->domain);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000594
595 // Add loop iv to symbols.
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000596 ClastVars[F->iterator] = LB;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000597
Tobias Grosser2da263e2012-03-15 09:34:55 +0000598 const clast_stmt *Stmt = F->body;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000599
Tobias Grosser2da263e2012-03-15 09:34:55 +0000600 while (Stmt) {
Tobias Grosser00d898d2012-03-15 09:34:58 +0000601 codegen((const clast_user_stmt *)Stmt, &IVS, F->iterator,
602 isl_set_copy(Domain));
Tobias Grosser2da263e2012-03-15 09:34:55 +0000603 Stmt = Stmt->next;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000604 }
605
606 // Loop is finished, so remove its iv from the live symbols.
Tobias Grosser00d898d2012-03-15 09:34:58 +0000607 isl_set_free(Domain);
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000608 ClastVars.erase(F->iterator);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000609}
610
611void ClastStmtCodeGen::codegen(const clast_for *f) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000612 bool Vector = PollyVectorizerChoice != VECTORIZER_NONE;
Tobias Grosserd596b372012-03-15 09:34:52 +0000613 if ((Vector || OpenMP) && P->getAnalysis<Dependences>().isParallelFor(f)) {
Tobias Grosserce3f5372012-03-02 11:26:42 +0000614 if (Vector && isInnermostLoop(f) && (-1 != getNumberOfIterations(f))
615 && (getNumberOfIterations(f) <= 16)) {
616 codegenForVector(f);
617 return;
618 }
619
620 if (OpenMP && !parallelCodeGeneration) {
621 parallelCodeGeneration = true;
622 parallelLoops.push_back(f->iterator);
623 codegenForOpenMP(f);
624 parallelCodeGeneration = false;
625 return;
626 }
627 }
628
629 codegenForSequential(f);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000630}
631
632Value *ClastStmtCodeGen::codegen(const clast_equation *eq) {
Tobias Grossere9ffea22012-03-15 09:34:48 +0000633 Value *LHS = ExpGen.codegen(eq->LHS, getIntPtrTy());
634 Value *RHS = ExpGen.codegen(eq->RHS, getIntPtrTy());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000635 CmpInst::Predicate P;
636
637 if (eq->sign == 0)
638 P = ICmpInst::ICMP_EQ;
639 else if (eq->sign > 0)
640 P = ICmpInst::ICMP_SGE;
641 else
642 P = ICmpInst::ICMP_SLE;
643
644 return Builder.CreateICmp(P, LHS, RHS);
645}
646
647void ClastStmtCodeGen::codegen(const clast_guard *g) {
648 Function *F = Builder.GetInsertBlock()->getParent();
649 LLVMContext &Context = F->getContext();
Tobias Grosser0ac92142012-02-14 14:02:27 +0000650
651 BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
652 Builder.GetInsertPoint(), P);
653 CondBB->setName("polly.cond");
654 BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P);
655 MergeBB->setName("polly.merge");
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000656 BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000657
Tobias Grosserd596b372012-03-15 09:34:52 +0000658 DominatorTree &DT = P->getAnalysis<DominatorTree>();
659 DT.addNewBlock(ThenBB, CondBB);
660 DT.changeImmediateDominator(MergeBB, CondBB);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000661
662 CondBB->getTerminator()->eraseFromParent();
663
664 Builder.SetInsertPoint(CondBB);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000665
666 Value *Predicate = codegen(&(g->eq[0]));
667
668 for (int i = 1; i < g->n; ++i) {
669 Value *TmpPredicate = codegen(&(g->eq[i]));
670 Predicate = Builder.CreateAnd(Predicate, TmpPredicate);
671 }
672
673 Builder.CreateCondBr(Predicate, ThenBB, MergeBB);
674 Builder.SetInsertPoint(ThenBB);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000675 Builder.CreateBr(MergeBB);
676 Builder.SetInsertPoint(ThenBB->begin());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000677
678 codegen(g->then);
Tobias Grosser62a3c962012-02-16 09:56:21 +0000679
680 Builder.SetInsertPoint(MergeBB->begin());
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000681}
682
683void ClastStmtCodeGen::codegen(const clast_stmt *stmt) {
684 if (CLAST_STMT_IS_A(stmt, stmt_root))
685 assert(false && "No second root statement expected");
686 else if (CLAST_STMT_IS_A(stmt, stmt_ass))
687 codegen((const clast_assignment *)stmt);
688 else if (CLAST_STMT_IS_A(stmt, stmt_user))
689 codegen((const clast_user_stmt *)stmt);
690 else if (CLAST_STMT_IS_A(stmt, stmt_block))
691 codegen((const clast_block *)stmt);
692 else if (CLAST_STMT_IS_A(stmt, stmt_for))
693 codegen((const clast_for *)stmt);
694 else if (CLAST_STMT_IS_A(stmt, stmt_guard))
695 codegen((const clast_guard *)stmt);
696
697 if (stmt->next)
698 codegen(stmt->next);
699}
700
701void ClastStmtCodeGen::addParameters(const CloogNames *names) {
Tobias Grosserd596b372012-03-15 09:34:52 +0000702 SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly");
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000703
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000704 int i = 0;
705 for (Scop::param_iterator PI = S->param_begin(), PE = S->param_end();
706 PI != PE; ++PI) {
707 assert(i < names->nb_parameters && "Not enough parameter names");
708
709 const SCEV *Param = *PI;
710 Type *Ty = Param->getType();
711
712 Instruction *insertLocation = --(Builder.GetInsertBlock()->end());
713 Value *V = Rewriter.expandCodeFor(Param, Ty, insertLocation);
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000714 ClastVars[names->parameters[i]] = V;
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000715
716 ++i;
717 }
718}
719
720void ClastStmtCodeGen::codegen(const clast_root *r) {
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000721 addParameters(r->names);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000722
723 parallelCodeGeneration = false;
724
725 const clast_stmt *stmt = (const clast_stmt*) r;
726 if (stmt->next)
727 codegen(stmt->next);
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000728}
729
Tobias Grosserd596b372012-03-15 09:34:52 +0000730ClastStmtCodeGen::ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P) :
Tobias Grosser5e8ffa82012-03-23 12:20:36 +0000731 S(scop), P(P), Builder(B), ExpGen(Builder, ClastVars) {}
Tobias Grosser9bc5eb082012-01-24 16:42:32 +0000732
Tobias Grosser75805372011-04-29 06:27:02 +0000733namespace {
734class CodeGeneration : public ScopPass {
735 Region *region;
736 Scop *S;
737 DominatorTree *DT;
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000738 RegionInfo *RI;
Tobias Grosser75805372011-04-29 06:27:02 +0000739
740 std::vector<std::string> parallelLoops;
741
742 public:
743 static char ID;
744
745 CodeGeneration() : ScopPass(ID) {}
746
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000747 // Split the entry edge of the region and generate a new basic block on this
748 // edge. This function also updates ScopInfo and RegionInfo.
749 //
750 // @param region The region where the entry edge will be splitted.
751 BasicBlock *splitEdgeAdvanced(Region *region) {
752 BasicBlock *newBlock;
753 BasicBlock *splitBlock;
754
755 newBlock = SplitEdge(region->getEnteringBlock(), region->getEntry(), this);
756
757 if (DT->dominates(region->getEntry(), newBlock)) {
Tobias Grossercb47dfe2012-02-15 09:58:50 +0000758 BasicBlock *OldBlock = region->getEntry();
759 std::string OldName = OldBlock->getName();
760
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000761 // Update ScopInfo.
762 for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI)
Tobias Grosserf12cea42012-02-15 09:58:53 +0000763 if ((*SI)->getBasicBlock() == OldBlock) {
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000764 (*SI)->setBasicBlock(newBlock);
765 break;
766 }
767
768 // Update RegionInfo.
Tobias Grossercb47dfe2012-02-15 09:58:50 +0000769 splitBlock = OldBlock;
770 OldBlock->setName("polly.split");
771 newBlock->setName(OldName);
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000772 region->replaceEntry(newBlock);
Tobias Grosser7a16c892011-05-14 19:01:55 +0000773 RI->setRegionFor(newBlock, region);
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000774 } else {
775 RI->setRegionFor(newBlock, region->getParent());
776 splitBlock = newBlock;
777 }
778
779 return splitBlock;
780 }
781
782 // Create a split block that branches either to the old code or to a new basic
783 // block where the new code can be inserted.
784 //
Tobias Grosserbd608a82012-02-12 12:09:41 +0000785 // @param Builder A builder that will be set to point to a basic block, where
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000786 // the new code can be generated.
787 // @return The split basic block.
Tobias Grosserbd608a82012-02-12 12:09:41 +0000788 BasicBlock *addSplitAndStartBlock(IRBuilder<> *Builder) {
789 BasicBlock *StartBlock, *SplitBlock;
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000790
Tobias Grosserbd608a82012-02-12 12:09:41 +0000791 SplitBlock = splitEdgeAdvanced(region);
792 SplitBlock->setName("polly.split_new_and_old");
793 Function *F = SplitBlock->getParent();
794 StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F);
795 SplitBlock->getTerminator()->eraseFromParent();
796 Builder->SetInsertPoint(SplitBlock);
797 Builder->CreateCondBr(Builder->getTrue(), StartBlock, region->getEntry());
798 DT->addNewBlock(StartBlock, SplitBlock);
799 Builder->SetInsertPoint(StartBlock);
800 return SplitBlock;
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000801 }
802
803 // Merge the control flow of the newly generated code with the existing code.
804 //
Tobias Grosserbd608a82012-02-12 12:09:41 +0000805 // @param SplitBlock The basic block where the control flow was split between
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000806 // old and new version of the Scop.
Tobias Grosserbd608a82012-02-12 12:09:41 +0000807 // @param Builder An IRBuilder that points to the last instruction of the
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000808 // newly generated code.
Tobias Grosserbd608a82012-02-12 12:09:41 +0000809 void mergeControlFlow(BasicBlock *SplitBlock, IRBuilder<> *Builder) {
810 BasicBlock *MergeBlock;
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000811 Region *R = region;
812
813 if (R->getExit()->getSinglePredecessor())
814 // No splitEdge required. A block with a single predecessor cannot have
815 // PHI nodes that would complicate life.
Tobias Grosserbd608a82012-02-12 12:09:41 +0000816 MergeBlock = R->getExit();
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000817 else {
Tobias Grosserbd608a82012-02-12 12:09:41 +0000818 MergeBlock = SplitEdge(R->getExitingBlock(), R->getExit(), this);
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000819 // SplitEdge will never split R->getExit(), as R->getExit() has more than
820 // one predecessor. Hence, mergeBlock is always a newly generated block.
Tobias Grosserbd608a82012-02-12 12:09:41 +0000821 R->replaceExit(MergeBlock);
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000822 }
823
Tobias Grosserbd608a82012-02-12 12:09:41 +0000824 Builder->CreateBr(MergeBlock);
Tobias Grosser8518bbe2012-02-12 12:09:46 +0000825 MergeBlock->setName("polly.merge_new_and_old");
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000826
Tobias Grosserbd608a82012-02-12 12:09:41 +0000827 if (DT->dominates(SplitBlock, MergeBlock))
828 DT->changeImmediateDominator(MergeBlock, SplitBlock);
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000829 }
830
Tobias Grosser75805372011-04-29 06:27:02 +0000831 bool runOnScop(Scop &scop) {
832 S = &scop;
833 region = &S->getRegion();
Tobias Grosser75805372011-04-29 06:27:02 +0000834 DT = &getAnalysis<DominatorTree>();
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000835 RI = &getAnalysis<RegionInfo>();
Tobias Grosser75805372011-04-29 06:27:02 +0000836
837 parallelLoops.clear();
838
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000839 assert(region->isSimple() && "Only simple regions are supported");
Tobias Grosser76d7c522011-05-14 19:01:37 +0000840
Tobias Grosser5772e652012-02-01 14:23:33 +0000841 // In the CFG the optimized code of the SCoP is generated next to the
842 // original code. Both the new and the original version of the code remain
843 // in the CFG. A branch statement decides which version is executed.
844 // For now, we always execute the new version (the old one is dead code
845 // eliminated by the cleanup passes). In the future we may decide to execute
846 // the new version only if certain run time checks succeed. This will be
847 // useful to support constructs for which we cannot prove all assumptions at
848 // compile time.
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000849 //
850 // Before transformation:
851 //
852 // bb0
853 // |
854 // orig_scop
855 // |
856 // bb1
857 //
858 // After transformation:
859 // bb0
860 // |
861 // polly.splitBlock
Tobias Grosser2bd3af12011-08-01 22:39:00 +0000862 // / \.
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000863 // | startBlock
864 // | |
865 // orig_scop new_scop
866 // \ /
867 // \ /
868 // bb1 (joinBlock)
869 IRBuilder<> builder(region->getEntry());
Tobias Grosser75805372011-04-29 06:27:02 +0000870
Tobias Grosser8c4cfc322011-05-14 19:01:49 +0000871 // The builder will be set to startBlock.
872 BasicBlock *splitBlock = addSplitAndStartBlock(&builder);
Tobias Grosser0ac92142012-02-14 14:02:27 +0000873 BasicBlock *StartBlock = builder.GetInsertBlock();
Tobias Grosser75805372011-04-29 06:27:02 +0000874
Tobias Grosser0ac92142012-02-14 14:02:27 +0000875 mergeControlFlow(splitBlock, &builder);
876 builder.SetInsertPoint(StartBlock->begin());
877
Tobias Grosserd596b372012-03-15 09:34:52 +0000878 ClastStmtCodeGen CodeGen(S, builder, this);
Tobias Grosser3fdecae2011-05-14 19:02:39 +0000879 CloogInfo &C = getAnalysis<CloogInfo>();
880 CodeGen.codegen(C.getClast());
Tobias Grosser75805372011-04-29 06:27:02 +0000881
Tobias Grosser75805372011-04-29 06:27:02 +0000882 parallelLoops.insert(parallelLoops.begin(),
883 CodeGen.getParallelLoops().begin(),
884 CodeGen.getParallelLoops().end());
885
Tobias Grosserabb6dcd2011-05-14 19:02:34 +0000886 return true;
Tobias Grosser75805372011-04-29 06:27:02 +0000887 }
888
889 virtual void printScop(raw_ostream &OS) const {
890 for (std::vector<std::string>::const_iterator PI = parallelLoops.begin(),
891 PE = parallelLoops.end(); PI != PE; ++PI)
892 OS << "Parallel loop with iterator '" << *PI << "' generated\n";
893 }
894
895 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
896 AU.addRequired<CloogInfo>();
897 AU.addRequired<Dependences>();
898 AU.addRequired<DominatorTree>();
Tobias Grosser75805372011-04-29 06:27:02 +0000899 AU.addRequired<RegionInfo>();
Tobias Grosser73600b82011-10-08 00:30:40 +0000900 AU.addRequired<ScalarEvolution>();
Tobias Grosser75805372011-04-29 06:27:02 +0000901 AU.addRequired<ScopDetection>();
902 AU.addRequired<ScopInfo>();
903 AU.addRequired<TargetData>();
904
905 AU.addPreserved<CloogInfo>();
906 AU.addPreserved<Dependences>();
Tobias Grosser5d6eb862011-05-14 19:02:45 +0000907
Tobias Grosser4e3f9a42011-05-23 15:23:36 +0000908 // FIXME: We do not create LoopInfo for the newly generated loops.
Tobias Grosser75805372011-04-29 06:27:02 +0000909 AU.addPreserved<LoopInfo>();
910 AU.addPreserved<DominatorTree>();
Tobias Grosser75805372011-04-29 06:27:02 +0000911 AU.addPreserved<ScopDetection>();
912 AU.addPreserved<ScalarEvolution>();
Tobias Grosser5d6eb862011-05-14 19:02:45 +0000913
Tobias Grosser4e3f9a42011-05-23 15:23:36 +0000914 // FIXME: We do not yet add regions for the newly generated code to the
915 // region tree.
Tobias Grosser75805372011-04-29 06:27:02 +0000916 AU.addPreserved<RegionInfo>();
917 AU.addPreserved<TempScopInfo>();
918 AU.addPreserved<ScopInfo>();
919 AU.addPreservedID(IndependentBlocksID);
920 }
921};
922}
923
924char CodeGeneration::ID = 1;
925
Tobias Grosser73600b82011-10-08 00:30:40 +0000926INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen",
Tobias Grosser3c2efba2012-03-06 07:38:57 +0000927 "Polly - Create LLVM-IR from SCoPs", false, false)
Tobias Grosser73600b82011-10-08 00:30:40 +0000928INITIALIZE_PASS_DEPENDENCY(CloogInfo)
929INITIALIZE_PASS_DEPENDENCY(Dependences)
930INITIALIZE_PASS_DEPENDENCY(DominatorTree)
931INITIALIZE_PASS_DEPENDENCY(RegionInfo)
932INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
933INITIALIZE_PASS_DEPENDENCY(ScopDetection)
934INITIALIZE_PASS_DEPENDENCY(TargetData)
935INITIALIZE_PASS_END(CodeGeneration, "polly-codegen",
Tobias Grosser3c2efba2012-03-06 07:38:57 +0000936 "Polly - Create LLVM-IR from SCoPs", false, false)
Tobias Grosser75805372011-04-29 06:27:02 +0000937
Tobias Grosser7ffe4e82011-11-17 12:56:10 +0000938Pass *polly::createCodeGenerationPass() {
Tobias Grosser75805372011-04-29 06:27:02 +0000939 return new CodeGeneration();
940}
Sebastian Pope1f65542012-05-07 16:35:11 +0000941
942#endif // CLOOG_FOUND