Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===------ 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 Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 23 | #include "polly/Cloog.h" |
Sebastian Pop | e1f6554 | 2012-05-07 16:35:11 +0000 | [diff] [blame^] | 24 | #ifdef CLOOG_FOUND |
| 25 | |
| 26 | #define DEBUG_TYPE "polly-codegen" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 27 | #include "polly/Dependences.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 28 | #include "polly/LinkAllPasses.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 29 | #include "polly/ScopInfo.h" |
| 30 | #include "polly/TempScopInfo.h" |
Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 31 | #include "polly/CodeGen/CodeGeneration.h" |
| 32 | #include "polly/CodeGen/BlockGenerators.h" |
| 33 | #include "polly/CodeGen/LoopGenerators.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 34 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 35 | |
| 36 | #include "llvm/Module.h" |
| 37 | #include "llvm/ADT/SetVector.h" |
Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 38 | #include "llvm/ADT/PostOrderIterator.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/LoopInfo.h" |
| 40 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 41 | #include "llvm/Support/CommandLine.h" |
| 42 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 43 | #include "llvm/Target/TargetData.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 44 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 45 | |
| 46 | #define CLOOG_INT_GMP 1 |
| 47 | #include "cloog/cloog.h" |
| 48 | #include "cloog/isl/cloog.h" |
| 49 | |
Raghesh Aloor | a71989c | 2011-12-28 02:48:26 +0000 | [diff] [blame] | 50 | #include "isl/aff.h" |
| 51 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 52 | #include <vector> |
| 53 | #include <utility> |
| 54 | |
| 55 | using namespace polly; |
| 56 | using namespace llvm; |
| 57 | |
| 58 | struct isl_set; |
| 59 | |
| 60 | namespace polly { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 61 | static cl::opt<bool> |
| 62 | OpenMP("enable-polly-openmp", |
| 63 | cl::desc("Generate OpenMP parallel code"), cl::Hidden, |
| 64 | cl::value_desc("OpenMP code generation enabled if true"), |
Tobias Grosser | 0acfcdb | 2012-03-16 17:17:16 +0000 | [diff] [blame] | 65 | cl::init(false), cl::ZeroOrMore); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 66 | |
| 67 | static cl::opt<bool> |
| 68 | AtLeastOnce("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 Grosser | 0acfcdb | 2012-03-16 17:17:16 +0000 | [diff] [blame] | 72 | cl::init(false), cl::ZeroOrMore); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 73 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 74 | typedef DenseMap<const char*, Value*> CharMapT; |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 75 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 76 | /// Class to generate LLVM-IR that calculates the value of a clast_expr. |
| 77 | class ClastExpCodeGen { |
| 78 | IRBuilder<> &Builder; |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 79 | const CharMapT &IVS; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 80 | |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 81 | 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 Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 85 | public: |
| 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 Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 94 | ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 95 | |
| 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 Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 100 | Value *codegen(const clast_expr *e, Type *Ty); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | Value *ClastExpCodeGen::codegen(const clast_name *e, Type *Ty) { |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 104 | CharMapT::const_iterator I = IVS.find(e->name); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 105 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 106 | assert(I != IVS.end() && "Clast name not found"); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 107 | |
| 108 | return Builder.CreateSExtOrBitCast(I->second, Ty); |
| 109 | } |
| 110 | |
| 111 | Value *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 | |
| 124 | Value *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 Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 137 | // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d |
Tobias Grosser | 906eafe | 2012-02-16 09:56:10 +0000 | [diff] [blame] | 138 | Value *One = ConstantInt::get(Ty, 1); |
| 139 | Value *Zero = ConstantInt::get(Ty, 0); |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 140 | 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 Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 145 | } |
| 146 | case clast_bin_cdiv: |
| 147 | { |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 148 | // ceild(n,d) ((n < 0) ? n : (n + d - 1)) / d |
| 149 | Value *One = ConstantInt::get(Ty, 1); |
Tobias Grosser | 906eafe | 2012-02-16 09:56:10 +0000 | [diff] [blame] | 150 | Value *Zero = ConstantInt::get(Ty, 0); |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 151 | 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 Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 156 | } |
| 157 | case clast_bin_div: |
| 158 | return Builder.CreateSDiv(LHS, RHS); |
| 159 | }; |
| 160 | |
| 161 | llvm_unreachable("Unknown clast binary expression type"); |
| 162 | } |
| 163 | |
| 164 | Value *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 Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 190 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 193 | return old; |
| 194 | } |
| 195 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 196 | ClastExpCodeGen::ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap) |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 197 | : Builder(B), IVS(IVMap) {} |
| 198 | |
| 199 | Value *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 Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 214 | class ClastStmtCodeGen { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 215 | public: |
| 216 | const std::vector<std::string> &getParallelLoops(); |
| 217 | |
| 218 | private: |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 219 | // The Scop we code generate. |
| 220 | Scop *S; |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 221 | Pass *P; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 222 | |
| 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 Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 239 | CharMapT ClastVars; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 240 | |
| 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 Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 249 | void codegen(const clast_assignment *a); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 250 | |
| 251 | void codegen(const clast_assignment *a, ScopStmt *Statement, |
| 252 | unsigned Dimension, int vectorDim, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 253 | std::vector<ValueMapT> *VectorVMap = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 254 | |
| 255 | void codegenSubstitutions(const clast_stmt *Assignment, |
| 256 | ScopStmt *Statement, int vectorDim = 0, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 257 | std::vector<ValueMapT> *VectorVMap = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 258 | |
| 259 | void codegen(const clast_user_stmt *u, std::vector<Value*> *IVS = NULL, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 260 | const char *iterator = NULL, isl_set *scatteringDomain = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 261 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 262 | void codegen(const clast_block *b); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 263 | |
| 264 | /// @brief Create a classical sequential loop. |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 265 | void codegenForSequential(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 266 | |
| 267 | /// @brief Create OpenMP structure values. |
| 268 | /// |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 269 | /// Create a list of values that has to be stored into the OpenMP subfuncition |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 270 | /// structure. |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 271 | SetVector<Value*> getOMPValues(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 272 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 273 | /// @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 Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 277 | void updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap, |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 278 | bool Reverse); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 279 | |
| 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 Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 284 | void codegenForOpenMP(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 285 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 286 | bool isInnermostLoop(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 287 | |
| 288 | /// @brief Get the number of loop iterations for this loop. |
| 289 | /// @param f The clast for loop to check. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 290 | int getNumberOfIterations(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 291 | |
| 292 | /// @brief Create vector instructions for this loop. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 293 | void codegenForVector(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 294 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 295 | void codegen(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 296 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 297 | Value *codegen(const clast_equation *eq); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 298 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 299 | void codegen(const clast_guard *g); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 300 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 301 | void codegen(const clast_stmt *stmt); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 302 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 303 | void addParameters(const CloogNames *names); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 304 | |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 305 | IntegerType *getIntPtrTy(); |
| 306 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 307 | public: |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 308 | void codegen(const clast_root *r); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 309 | |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 310 | ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 311 | }; |
| 312 | } |
| 313 | |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 314 | IntegerType *ClastStmtCodeGen::getIntPtrTy() { |
| 315 | return P->getAnalysis<TargetData>().getIntPtrType(Builder.getContext()); |
| 316 | } |
| 317 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 318 | const std::vector<std::string> &ClastStmtCodeGen::getParallelLoops() { |
| 319 | return parallelLoops; |
| 320 | } |
| 321 | |
| 322 | void ClastStmtCodeGen::codegen(const clast_assignment *a) { |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 323 | Value *V= ExpGen.codegen(a->RHS, getIntPtrTy()); |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 324 | ClastVars[a->LHS] = V; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 327 | void ClastStmtCodeGen::codegen(const clast_assignment *A, ScopStmt *Stmt, |
| 328 | unsigned Dim, int VectorDim, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 329 | std::vector<ValueMapT> *VectorVMap) { |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 330 | const PHINode *PN; |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 331 | Value *RHS; |
| 332 | |
| 333 | assert(!A->LHS && "Statement assignments do not have left hand side"); |
| 334 | |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 335 | PN = Stmt->getInductionVariableForDimension(Dim); |
Tobias Grosser | 0905a23 | 2012-04-03 12:24:32 +0000 | [diff] [blame] | 336 | RHS = ExpGen.codegen(A->RHS, Builder.getInt64Ty()); |
| 337 | RHS = Builder.CreateTruncOrBitCast(RHS, PN->getType()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 338 | |
| 339 | if (VectorVMap) |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 340 | (*VectorVMap)[VectorDim][PN] = RHS; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 341 | |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 342 | ValueMap[PN] = RHS; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void 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 | |
| 360 | void ClastStmtCodeGen::codegen(const clast_user_stmt *u, |
| 361 | std::vector<Value*> *IVS , const char *iterator, |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 362 | isl_set *Domain) { |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 363 | ScopStmt *Statement = (ScopStmt *)u->statement->usr; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 364 | |
| 365 | if (u->substitutions) |
| 366 | codegenSubstitutions(u->substitutions, Statement); |
| 367 | |
Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 368 | int VectorDimensions = IVS ? IVS->size() : 1; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 369 | |
Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 370 | if (VectorDimensions == 1) { |
Tobias Grosser | 55d5208 | 2012-03-02 15:20:39 +0000 | [diff] [blame] | 371 | BlockGenerator::generate(Builder, *Statement, ValueMap, P); |
Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 372 | return; |
| 373 | } |
| 374 | |
| 375 | VectorValueMapT VectorMap(VectorDimensions); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 376 | |
| 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 Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 382 | ClastVars[iterator] = *II; |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 383 | codegenSubstitutions(u->substitutions, Statement, i, &VectorMap); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 384 | i++; |
| 385 | } |
| 386 | } |
| 387 | |
Tobias Grosser | 55d5208 | 2012-03-02 15:20:39 +0000 | [diff] [blame] | 388 | VectorBlockGenerator::generate(Builder, *Statement, VectorMap, Domain, P); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | void ClastStmtCodeGen::codegen(const clast_block *b) { |
| 392 | if (b->body) |
| 393 | codegen(b->body); |
| 394 | } |
| 395 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 396 | void ClastStmtCodeGen::codegenForSequential(const clast_for *f) { |
| 397 | Value *LowerBound, *UpperBound, *IV, *Stride; |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 398 | BasicBlock *AfterBB; |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 399 | Type *IntPtrTy = getIntPtrTy(); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 400 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 401 | LowerBound = ExpGen.codegen(f->LB, IntPtrTy); |
| 402 | UpperBound = ExpGen.codegen(f->UB, IntPtrTy); |
| 403 | Stride = Builder.getInt(APInt_from_MPZ(f->stride)); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 404 | |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 405 | IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 406 | |
| 407 | // Add loop iv to symbols. |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 408 | ClastVars[f->iterator] = IV; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 409 | |
| 410 | if (f->body) |
| 411 | codegen(f->body); |
| 412 | |
| 413 | // Loop is finished, so remove its iv from the live symbols. |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 414 | ClastVars.erase(f->iterator); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 415 | Builder.SetInsertPoint(AfterBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 418 | SetVector<Value*> ClastStmtCodeGen::getOMPValues() { |
| 419 | SetVector<Value*> Values; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 420 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 421 | // The clast variables |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 422 | for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end(); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 423 | I != E; I++) |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 424 | Values.insert(I->second); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 425 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 426 | // The memory reference base addresses |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 427 | 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 Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 432 | Values.insert((BaseAddr)); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 436 | return Values; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 439 | void ClastStmtCodeGen::updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap, |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 440 | bool Reverse) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 441 | std::set<Value*> Inserted; |
| 442 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 443 | 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 Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 463 | I != E; I++) { |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 464 | ClastVars[I->first] = VMap[I->second]; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 465 | Inserted.insert(I->second); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 468 | for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end(); |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 469 | I != E; ++I) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 470 | if (Inserted.count(I->first)) |
| 471 | continue; |
| 472 | |
| 473 | ValueMap[I->first] = I->second; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 477 | static 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 Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 488 | void ClastStmtCodeGen::codegenForOpenMP(const clast_for *For) { |
Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 489 | Value *Stride, *LB, *UB, *IV; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 490 | BasicBlock::iterator LoopBody; |
| 491 | IntegerType *IntPtrTy = getIntPtrTy(); |
| 492 | SetVector<Value*> Values; |
| 493 | OMPGenerator::ValueToValueMapTy VMap; |
| 494 | OMPGenerator OMPGen(Builder, P); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 495 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 496 | Stride = Builder.getInt(APInt_from_MPZ(For->stride)); |
| 497 | Stride = Builder.CreateSExtOrBitCast(Stride, IntPtrTy); |
Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 498 | LB = ExpGen.codegen(For->LB, IntPtrTy); |
| 499 | UB = ExpGen.codegen(For->UB, IntPtrTy); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 500 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 501 | Values = getOMPValues(); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 502 | |
Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 503 | IV = OMPGen.createParallelLoop(LB, UB, Stride, Values, VMap, &LoopBody); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 504 | BasicBlock::iterator AfterLoop = Builder.GetInsertPoint(); |
| 505 | Builder.SetInsertPoint(LoopBody); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 506 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 507 | updateWithValueMap(VMap, /* reverse */ false); |
| 508 | ClastVars[For->iterator] = IV; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 509 | |
| 510 | if (For->body) |
| 511 | codegen(For->body); |
| 512 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 513 | ClastVars.erase(For->iterator); |
| 514 | updateWithValueMap(VMap, /* reverse */ true); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 515 | |
Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 516 | clearDomtree((*LoopBody).getParent()->getParent(), |
| 517 | P->getAnalysis<DominatorTree>()); |
| 518 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 519 | Builder.SetInsertPoint(AfterLoop); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | bool 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 | |
| 535 | int 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 Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 576 | void ClastStmtCodeGen::codegenForVector(const clast_for *F) { |
| 577 | DEBUG(dbgs() << "Vectorizing loop '" << F->iterator << "'\n";); |
| 578 | int VectorWidth = getNumberOfIterations(F); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 579 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 580 | Value *LB = ExpGen.codegen(F->LB, getIntPtrTy()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 581 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 582 | APInt Stride = APInt_from_MPZ(F->stride); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 583 | IntegerType *LoopIVType = dyn_cast<IntegerType>(LB->getType()); |
| 584 | Stride = Stride.zext(LoopIVType->getBitWidth()); |
| 585 | Value *StrideValue = ConstantInt::get(LoopIVType, Stride); |
| 586 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 587 | std::vector<Value*> IVS(VectorWidth); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 588 | IVS[0] = LB; |
| 589 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 590 | for (int i = 1; i < VectorWidth; i++) |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 591 | IVS[i] = Builder.CreateAdd(IVS[i-1], StrideValue, "p_vector_iv"); |
| 592 | |
Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 593 | isl_set *Domain = isl_set_from_cloog_domain(F->domain); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 594 | |
| 595 | // Add loop iv to symbols. |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 596 | ClastVars[F->iterator] = LB; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 597 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 598 | const clast_stmt *Stmt = F->body; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 599 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 600 | while (Stmt) { |
Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 601 | codegen((const clast_user_stmt *)Stmt, &IVS, F->iterator, |
| 602 | isl_set_copy(Domain)); |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 603 | Stmt = Stmt->next; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | // Loop is finished, so remove its iv from the live symbols. |
Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 607 | isl_set_free(Domain); |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 608 | ClastVars.erase(F->iterator); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | void ClastStmtCodeGen::codegen(const clast_for *f) { |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 612 | bool Vector = PollyVectorizerChoice != VECTORIZER_NONE; |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 613 | if ((Vector || OpenMP) && P->getAnalysis<Dependences>().isParallelFor(f)) { |
Tobias Grosser | ce3f537 | 2012-03-02 11:26:42 +0000 | [diff] [blame] | 614 | 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 Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | Value *ClastStmtCodeGen::codegen(const clast_equation *eq) { |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 633 | Value *LHS = ExpGen.codegen(eq->LHS, getIntPtrTy()); |
| 634 | Value *RHS = ExpGen.codegen(eq->RHS, getIntPtrTy()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 635 | 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 | |
| 647 | void ClastStmtCodeGen::codegen(const clast_guard *g) { |
| 648 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 649 | LLVMContext &Context = F->getContext(); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 650 | |
| 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 Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 656 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 657 | |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 658 | DominatorTree &DT = P->getAnalysis<DominatorTree>(); |
| 659 | DT.addNewBlock(ThenBB, CondBB); |
| 660 | DT.changeImmediateDominator(MergeBB, CondBB); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 661 | |
| 662 | CondBB->getTerminator()->eraseFromParent(); |
| 663 | |
| 664 | Builder.SetInsertPoint(CondBB); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 665 | |
| 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 Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 675 | Builder.CreateBr(MergeBB); |
| 676 | Builder.SetInsertPoint(ThenBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 677 | |
| 678 | codegen(g->then); |
Tobias Grosser | 62a3c96 | 2012-02-16 09:56:21 +0000 | [diff] [blame] | 679 | |
| 680 | Builder.SetInsertPoint(MergeBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void 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 | |
| 701 | void ClastStmtCodeGen::addParameters(const CloogNames *names) { |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 702 | SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly"); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 703 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 704 | 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 Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 714 | ClastVars[names->parameters[i]] = V; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 715 | |
| 716 | ++i; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | void ClastStmtCodeGen::codegen(const clast_root *r) { |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 721 | addParameters(r->names); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 722 | |
| 723 | parallelCodeGeneration = false; |
| 724 | |
| 725 | const clast_stmt *stmt = (const clast_stmt*) r; |
| 726 | if (stmt->next) |
| 727 | codegen(stmt->next); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 730 | ClastStmtCodeGen::ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P) : |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 731 | S(scop), P(P), Builder(B), ExpGen(Builder, ClastVars) {} |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 732 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 733 | namespace { |
| 734 | class CodeGeneration : public ScopPass { |
| 735 | Region *region; |
| 736 | Scop *S; |
| 737 | DominatorTree *DT; |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 738 | RegionInfo *RI; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 739 | |
| 740 | std::vector<std::string> parallelLoops; |
| 741 | |
| 742 | public: |
| 743 | static char ID; |
| 744 | |
| 745 | CodeGeneration() : ScopPass(ID) {} |
| 746 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 747 | // 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 Grosser | cb47dfe | 2012-02-15 09:58:50 +0000 | [diff] [blame] | 758 | BasicBlock *OldBlock = region->getEntry(); |
| 759 | std::string OldName = OldBlock->getName(); |
| 760 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 761 | // Update ScopInfo. |
| 762 | for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) |
Tobias Grosser | f12cea4 | 2012-02-15 09:58:53 +0000 | [diff] [blame] | 763 | if ((*SI)->getBasicBlock() == OldBlock) { |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 764 | (*SI)->setBasicBlock(newBlock); |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | // Update RegionInfo. |
Tobias Grosser | cb47dfe | 2012-02-15 09:58:50 +0000 | [diff] [blame] | 769 | splitBlock = OldBlock; |
| 770 | OldBlock->setName("polly.split"); |
| 771 | newBlock->setName(OldName); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 772 | region->replaceEntry(newBlock); |
Tobias Grosser | 7a16c89 | 2011-05-14 19:01:55 +0000 | [diff] [blame] | 773 | RI->setRegionFor(newBlock, region); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 774 | } 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 Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 785 | // @param Builder A builder that will be set to point to a basic block, where |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 786 | // the new code can be generated. |
| 787 | // @return The split basic block. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 788 | BasicBlock *addSplitAndStartBlock(IRBuilder<> *Builder) { |
| 789 | BasicBlock *StartBlock, *SplitBlock; |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 790 | |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 791 | 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 Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | // Merge the control flow of the newly generated code with the existing code. |
| 804 | // |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 805 | // @param SplitBlock The basic block where the control flow was split between |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 806 | // old and new version of the Scop. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 807 | // @param Builder An IRBuilder that points to the last instruction of the |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 808 | // newly generated code. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 809 | void mergeControlFlow(BasicBlock *SplitBlock, IRBuilder<> *Builder) { |
| 810 | BasicBlock *MergeBlock; |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 811 | 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 Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 816 | MergeBlock = R->getExit(); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 817 | else { |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 818 | MergeBlock = SplitEdge(R->getExitingBlock(), R->getExit(), this); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 819 | // SplitEdge will never split R->getExit(), as R->getExit() has more than |
| 820 | // one predecessor. Hence, mergeBlock is always a newly generated block. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 821 | R->replaceExit(MergeBlock); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 822 | } |
| 823 | |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 824 | Builder->CreateBr(MergeBlock); |
Tobias Grosser | 8518bbe | 2012-02-12 12:09:46 +0000 | [diff] [blame] | 825 | MergeBlock->setName("polly.merge_new_and_old"); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 826 | |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 827 | if (DT->dominates(SplitBlock, MergeBlock)) |
| 828 | DT->changeImmediateDominator(MergeBlock, SplitBlock); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 831 | bool runOnScop(Scop &scop) { |
| 832 | S = &scop; |
| 833 | region = &S->getRegion(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 834 | DT = &getAnalysis<DominatorTree>(); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 835 | RI = &getAnalysis<RegionInfo>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 836 | |
| 837 | parallelLoops.clear(); |
| 838 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 839 | assert(region->isSimple() && "Only simple regions are supported"); |
Tobias Grosser | 76d7c52 | 2011-05-14 19:01:37 +0000 | [diff] [blame] | 840 | |
Tobias Grosser | 5772e65 | 2012-02-01 14:23:33 +0000 | [diff] [blame] | 841 | // 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 Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 849 | // |
| 850 | // Before transformation: |
| 851 | // |
| 852 | // bb0 |
| 853 | // | |
| 854 | // orig_scop |
| 855 | // | |
| 856 | // bb1 |
| 857 | // |
| 858 | // After transformation: |
| 859 | // bb0 |
| 860 | // | |
| 861 | // polly.splitBlock |
Tobias Grosser | 2bd3af1 | 2011-08-01 22:39:00 +0000 | [diff] [blame] | 862 | // / \. |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 863 | // | startBlock |
| 864 | // | | |
| 865 | // orig_scop new_scop |
| 866 | // \ / |
| 867 | // \ / |
| 868 | // bb1 (joinBlock) |
| 869 | IRBuilder<> builder(region->getEntry()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 870 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 871 | // The builder will be set to startBlock. |
| 872 | BasicBlock *splitBlock = addSplitAndStartBlock(&builder); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 873 | BasicBlock *StartBlock = builder.GetInsertBlock(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 874 | |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 875 | mergeControlFlow(splitBlock, &builder); |
| 876 | builder.SetInsertPoint(StartBlock->begin()); |
| 877 | |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 878 | ClastStmtCodeGen CodeGen(S, builder, this); |
Tobias Grosser | 3fdecae | 2011-05-14 19:02:39 +0000 | [diff] [blame] | 879 | CloogInfo &C = getAnalysis<CloogInfo>(); |
| 880 | CodeGen.codegen(C.getClast()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 881 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 882 | parallelLoops.insert(parallelLoops.begin(), |
| 883 | CodeGen.getParallelLoops().begin(), |
| 884 | CodeGen.getParallelLoops().end()); |
| 885 | |
Tobias Grosser | abb6dcd | 2011-05-14 19:02:34 +0000 | [diff] [blame] | 886 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 887 | } |
| 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 Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 899 | AU.addRequired<RegionInfo>(); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 900 | AU.addRequired<ScalarEvolution>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 901 | AU.addRequired<ScopDetection>(); |
| 902 | AU.addRequired<ScopInfo>(); |
| 903 | AU.addRequired<TargetData>(); |
| 904 | |
| 905 | AU.addPreserved<CloogInfo>(); |
| 906 | AU.addPreserved<Dependences>(); |
Tobias Grosser | 5d6eb86 | 2011-05-14 19:02:45 +0000 | [diff] [blame] | 907 | |
Tobias Grosser | 4e3f9a4 | 2011-05-23 15:23:36 +0000 | [diff] [blame] | 908 | // FIXME: We do not create LoopInfo for the newly generated loops. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 909 | AU.addPreserved<LoopInfo>(); |
| 910 | AU.addPreserved<DominatorTree>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 911 | AU.addPreserved<ScopDetection>(); |
| 912 | AU.addPreserved<ScalarEvolution>(); |
Tobias Grosser | 5d6eb86 | 2011-05-14 19:02:45 +0000 | [diff] [blame] | 913 | |
Tobias Grosser | 4e3f9a4 | 2011-05-23 15:23:36 +0000 | [diff] [blame] | 914 | // FIXME: We do not yet add regions for the newly generated code to the |
| 915 | // region tree. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 916 | AU.addPreserved<RegionInfo>(); |
| 917 | AU.addPreserved<TempScopInfo>(); |
| 918 | AU.addPreserved<ScopInfo>(); |
| 919 | AU.addPreservedID(IndependentBlocksID); |
| 920 | } |
| 921 | }; |
| 922 | } |
| 923 | |
| 924 | char CodeGeneration::ID = 1; |
| 925 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 926 | INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", |
Tobias Grosser | 3c2efba | 2012-03-06 07:38:57 +0000 | [diff] [blame] | 927 | "Polly - Create LLVM-IR from SCoPs", false, false) |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 928 | INITIALIZE_PASS_DEPENDENCY(CloogInfo) |
| 929 | INITIALIZE_PASS_DEPENDENCY(Dependences) |
| 930 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 931 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 932 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 933 | INITIALIZE_PASS_DEPENDENCY(ScopDetection) |
| 934 | INITIALIZE_PASS_DEPENDENCY(TargetData) |
| 935 | INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", |
Tobias Grosser | 3c2efba | 2012-03-06 07:38:57 +0000 | [diff] [blame] | 936 | "Polly - Create LLVM-IR from SCoPs", false, false) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 937 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 938 | Pass *polly::createCodeGenerationPass() { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 939 | return new CodeGeneration(); |
| 940 | } |
Sebastian Pop | e1f6554 | 2012-05-07 16:35:11 +0000 | [diff] [blame^] | 941 | |
| 942 | #endif // CLOOG_FOUND |