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 | 0a91f32 | 2012-05-29 09:11:46 +0000 | [diff] [blame] | 23 | #include "polly/CodeGen/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" |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 34 | #include "polly/CodeGen/Utils.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 35 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 36 | |
| 37 | #include "llvm/Module.h" |
| 38 | #include "llvm/ADT/SetVector.h" |
Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 39 | #include "llvm/ADT/PostOrderIterator.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/LoopInfo.h" |
| 41 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 42 | #include "llvm/Support/CommandLine.h" |
| 43 | #include "llvm/Support/Debug.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 44 | #include "llvm/Target/TargetData.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 45 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 46 | |
| 47 | #define CLOOG_INT_GMP 1 |
| 48 | #include "cloog/cloog.h" |
| 49 | #include "cloog/isl/cloog.h" |
| 50 | |
Raghesh Aloor | a71989c | 2011-12-28 02:48:26 +0000 | [diff] [blame] | 51 | #include "isl/aff.h" |
| 52 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 53 | #include <vector> |
| 54 | #include <utility> |
| 55 | |
| 56 | using namespace polly; |
| 57 | using namespace llvm; |
| 58 | |
| 59 | struct isl_set; |
| 60 | |
| 61 | namespace polly { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 62 | static cl::opt<bool> |
| 63 | OpenMP("enable-polly-openmp", |
| 64 | cl::desc("Generate OpenMP parallel code"), cl::Hidden, |
| 65 | cl::value_desc("OpenMP code generation enabled if true"), |
Tobias Grosser | 0acfcdb | 2012-03-16 17:17:16 +0000 | [diff] [blame] | 66 | cl::init(false), cl::ZeroOrMore); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 67 | |
| 68 | static cl::opt<bool> |
| 69 | AtLeastOnce("enable-polly-atLeastOnce", |
| 70 | cl::desc("Give polly the hint, that every loop is executed at least" |
| 71 | "once"), cl::Hidden, |
| 72 | cl::value_desc("OpenMP code generation enabled if true"), |
Tobias Grosser | 0acfcdb | 2012-03-16 17:17:16 +0000 | [diff] [blame] | 73 | cl::init(false), cl::ZeroOrMore); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 74 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 75 | typedef DenseMap<const char*, Value*> CharMapT; |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 76 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 77 | /// Class to generate LLVM-IR that calculates the value of a clast_expr. |
| 78 | class ClastExpCodeGen { |
| 79 | IRBuilder<> &Builder; |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 80 | const CharMapT &IVS; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 81 | |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 82 | Value *codegen(const clast_name *e, Type *Ty); |
| 83 | Value *codegen(const clast_term *e, Type *Ty); |
| 84 | Value *codegen(const clast_binary *e, Type *Ty); |
| 85 | Value *codegen(const clast_reduction *r, Type *Ty); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 86 | public: |
| 87 | |
| 88 | // A generator for clast expressions. |
| 89 | // |
| 90 | // @param B The IRBuilder that defines where the code to calculate the |
| 91 | // clast expressions should be inserted. |
| 92 | // @param IVMAP A Map that translates strings describing the induction |
| 93 | // variables to the Values* that represent these variables |
| 94 | // on the LLVM side. |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 95 | ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 96 | |
| 97 | // Generates code to calculate a given clast expression. |
| 98 | // |
| 99 | // @param e The expression to calculate. |
| 100 | // @return The Value that holds the result. |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 101 | Value *codegen(const clast_expr *e, Type *Ty); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | Value *ClastExpCodeGen::codegen(const clast_name *e, Type *Ty) { |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 105 | CharMapT::const_iterator I = IVS.find(e->name); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 106 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 107 | assert(I != IVS.end() && "Clast name not found"); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 108 | |
| 109 | return Builder.CreateSExtOrBitCast(I->second, Ty); |
| 110 | } |
| 111 | |
| 112 | Value *ClastExpCodeGen::codegen(const clast_term *e, Type *Ty) { |
| 113 | APInt a = APInt_from_MPZ(e->val); |
| 114 | |
| 115 | Value *ConstOne = ConstantInt::get(Builder.getContext(), a); |
| 116 | ConstOne = Builder.CreateSExtOrBitCast(ConstOne, Ty); |
| 117 | |
| 118 | if (!e->var) |
| 119 | return ConstOne; |
| 120 | |
| 121 | Value *var = codegen(e->var, Ty); |
| 122 | return Builder.CreateMul(ConstOne, var); |
| 123 | } |
| 124 | |
| 125 | Value *ClastExpCodeGen::codegen(const clast_binary *e, Type *Ty) { |
| 126 | Value *LHS = codegen(e->LHS, Ty); |
| 127 | |
| 128 | APInt RHS_AP = APInt_from_MPZ(e->RHS); |
| 129 | |
| 130 | Value *RHS = ConstantInt::get(Builder.getContext(), RHS_AP); |
| 131 | RHS = Builder.CreateSExtOrBitCast(RHS, Ty); |
| 132 | |
| 133 | switch (e->type) { |
| 134 | case clast_bin_mod: |
| 135 | return Builder.CreateSRem(LHS, RHS); |
| 136 | case clast_bin_fdiv: |
| 137 | { |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 138 | // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d |
Tobias Grosser | 906eafe | 2012-02-16 09:56:10 +0000 | [diff] [blame] | 139 | Value *One = ConstantInt::get(Ty, 1); |
| 140 | Value *Zero = ConstantInt::get(Ty, 0); |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 141 | Value *Sum1 = Builder.CreateSub(LHS, RHS); |
| 142 | Value *Sum2 = Builder.CreateAdd(Sum1, One); |
| 143 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero); |
| 144 | Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS); |
| 145 | return Builder.CreateSDiv(Dividend, RHS); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 146 | } |
| 147 | case clast_bin_cdiv: |
| 148 | { |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 149 | // ceild(n,d) ((n < 0) ? n : (n + d - 1)) / d |
| 150 | Value *One = ConstantInt::get(Ty, 1); |
Tobias Grosser | 906eafe | 2012-02-16 09:56:10 +0000 | [diff] [blame] | 151 | Value *Zero = ConstantInt::get(Ty, 0); |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 152 | Value *Sum1 = Builder.CreateAdd(LHS, RHS); |
| 153 | Value *Sum2 = Builder.CreateSub(Sum1, One); |
| 154 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero); |
| 155 | Value *Dividend = Builder.CreateSelect(isNegative, LHS, Sum2); |
| 156 | return Builder.CreateSDiv(Dividend, RHS); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 157 | } |
| 158 | case clast_bin_div: |
| 159 | return Builder.CreateSDiv(LHS, RHS); |
| 160 | }; |
| 161 | |
| 162 | llvm_unreachable("Unknown clast binary expression type"); |
| 163 | } |
| 164 | |
| 165 | Value *ClastExpCodeGen::codegen(const clast_reduction *r, Type *Ty) { |
| 166 | assert(( r->type == clast_red_min |
| 167 | || r->type == clast_red_max |
| 168 | || r->type == clast_red_sum) |
| 169 | && "Clast reduction type not supported"); |
| 170 | Value *old = codegen(r->elts[0], Ty); |
| 171 | |
| 172 | for (int i=1; i < r->n; ++i) { |
| 173 | Value *exprValue = codegen(r->elts[i], Ty); |
| 174 | |
| 175 | switch (r->type) { |
| 176 | case clast_red_min: |
| 177 | { |
| 178 | Value *cmp = Builder.CreateICmpSLT(old, exprValue); |
| 179 | old = Builder.CreateSelect(cmp, old, exprValue); |
| 180 | break; |
| 181 | } |
| 182 | case clast_red_max: |
| 183 | { |
| 184 | Value *cmp = Builder.CreateICmpSGT(old, exprValue); |
| 185 | old = Builder.CreateSelect(cmp, old, exprValue); |
| 186 | break; |
| 187 | } |
| 188 | case clast_red_sum: |
| 189 | old = Builder.CreateAdd(old, exprValue); |
| 190 | break; |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 191 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 194 | return old; |
| 195 | } |
| 196 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 197 | ClastExpCodeGen::ClastExpCodeGen(IRBuilder<> &B, CharMapT &IVMap) |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 198 | : Builder(B), IVS(IVMap) {} |
| 199 | |
| 200 | Value *ClastExpCodeGen::codegen(const clast_expr *e, Type *Ty) { |
| 201 | switch(e->type) { |
| 202 | case clast_expr_name: |
| 203 | return codegen((const clast_name *)e, Ty); |
| 204 | case clast_expr_term: |
| 205 | return codegen((const clast_term *)e, Ty); |
| 206 | case clast_expr_bin: |
| 207 | return codegen((const clast_binary *)e, Ty); |
| 208 | case clast_expr_red: |
| 209 | return codegen((const clast_reduction *)e, Ty); |
| 210 | } |
| 211 | |
| 212 | llvm_unreachable("Unknown clast expression!"); |
| 213 | } |
| 214 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 215 | class ClastStmtCodeGen { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 216 | public: |
| 217 | const std::vector<std::string> &getParallelLoops(); |
| 218 | |
| 219 | private: |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 220 | // The Scop we code generate. |
| 221 | Scop *S; |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 222 | Pass *P; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 223 | |
| 224 | // The Builder specifies the current location to code generate at. |
| 225 | IRBuilder<> &Builder; |
| 226 | |
| 227 | // Map the Values from the old code to their counterparts in the new code. |
| 228 | ValueMapT ValueMap; |
| 229 | |
| 230 | // clastVars maps from the textual representation of a clast variable to its |
| 231 | // current *Value. clast variables are scheduling variables, original |
| 232 | // induction variables or parameters. They are used either in loop bounds or |
| 233 | // to define the statement instance that is executed. |
| 234 | // |
| 235 | // for (s = 0; s < n + 3; ++i) |
| 236 | // for (t = s; t < m; ++j) |
| 237 | // Stmt(i = s + 3 * m, j = t); |
| 238 | // |
| 239 | // {s,t,i,j,n,m} is the set of clast variables in this clast. |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 240 | CharMapT ClastVars; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 241 | |
| 242 | // Codegenerator for clast expressions. |
| 243 | ClastExpCodeGen ExpGen; |
| 244 | |
| 245 | // Do we currently generate parallel code? |
| 246 | bool parallelCodeGeneration; |
| 247 | |
| 248 | std::vector<std::string> parallelLoops; |
| 249 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 250 | void codegen(const clast_assignment *a); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 251 | |
| 252 | void codegen(const clast_assignment *a, ScopStmt *Statement, |
| 253 | unsigned Dimension, int vectorDim, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 254 | std::vector<ValueMapT> *VectorVMap = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 255 | |
| 256 | void codegenSubstitutions(const clast_stmt *Assignment, |
| 257 | ScopStmt *Statement, int vectorDim = 0, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 258 | std::vector<ValueMapT> *VectorVMap = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 259 | |
| 260 | void codegen(const clast_user_stmt *u, std::vector<Value*> *IVS = NULL, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 261 | const char *iterator = NULL, isl_set *scatteringDomain = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 262 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 263 | void codegen(const clast_block *b); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 264 | |
| 265 | /// @brief Create a classical sequential loop. |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 266 | void codegenForSequential(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 267 | |
| 268 | /// @brief Create OpenMP structure values. |
| 269 | /// |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 270 | /// 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] | 271 | /// structure. |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 272 | SetVector<Value*> getOMPValues(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 273 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 274 | /// @brief Update the internal structures according to a Value Map. |
| 275 | /// |
| 276 | /// @param VMap A map from old to new values. |
| 277 | /// @param Reverse If true, we assume the update should be reversed. |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 278 | void updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap, |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 279 | bool Reverse); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 280 | |
| 281 | /// @brief Create an OpenMP parallel for loop. |
| 282 | /// |
| 283 | /// This loop reflects a loop as if it would have been created by an OpenMP |
| 284 | /// statement. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 285 | void codegenForOpenMP(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 286 | |
Tobias Grosser | e192b23 | 2012-05-22 08:46:07 +0000 | [diff] [blame] | 287 | /// @brief Check if a loop is parallel |
| 288 | /// |
| 289 | /// Detect if a clast_for loop can be executed in parallel. |
| 290 | /// |
| 291 | /// @param f The clast for loop to check. |
| 292 | /// |
| 293 | /// @return bool Returns true if the incoming clast_for statement can |
| 294 | /// execute in parallel. |
| 295 | bool isParallelFor(const clast_for *For); |
| 296 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 297 | bool isInnermostLoop(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 298 | |
| 299 | /// @brief Get the number of loop iterations for this loop. |
| 300 | /// @param f The clast for loop to check. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 301 | int getNumberOfIterations(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 302 | |
| 303 | /// @brief Create vector instructions for this loop. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 304 | void codegenForVector(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 305 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 306 | void codegen(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 307 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 308 | Value *codegen(const clast_equation *eq); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 309 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 310 | void codegen(const clast_guard *g); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 311 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 312 | void codegen(const clast_stmt *stmt); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 313 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 314 | void addParameters(const CloogNames *names); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 315 | |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 316 | IntegerType *getIntPtrTy(); |
| 317 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 318 | public: |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 319 | void codegen(const clast_root *r); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 320 | |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 321 | ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 322 | }; |
| 323 | } |
| 324 | |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 325 | IntegerType *ClastStmtCodeGen::getIntPtrTy() { |
| 326 | return P->getAnalysis<TargetData>().getIntPtrType(Builder.getContext()); |
| 327 | } |
| 328 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 329 | const std::vector<std::string> &ClastStmtCodeGen::getParallelLoops() { |
| 330 | return parallelLoops; |
| 331 | } |
| 332 | |
| 333 | void ClastStmtCodeGen::codegen(const clast_assignment *a) { |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 334 | Value *V= ExpGen.codegen(a->RHS, getIntPtrTy()); |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 335 | ClastVars[a->LHS] = V; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 338 | void ClastStmtCodeGen::codegen(const clast_assignment *A, ScopStmt *Stmt, |
| 339 | unsigned Dim, int VectorDim, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 340 | std::vector<ValueMapT> *VectorVMap) { |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 341 | const PHINode *PN; |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 342 | Value *RHS; |
| 343 | |
| 344 | assert(!A->LHS && "Statement assignments do not have left hand side"); |
| 345 | |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 346 | PN = Stmt->getInductionVariableForDimension(Dim); |
Tobias Grosser | 0905a23 | 2012-04-03 12:24:32 +0000 | [diff] [blame] | 347 | RHS = ExpGen.codegen(A->RHS, Builder.getInt64Ty()); |
| 348 | RHS = Builder.CreateTruncOrBitCast(RHS, PN->getType()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 349 | |
| 350 | if (VectorVMap) |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 351 | (*VectorVMap)[VectorDim][PN] = RHS; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 352 | |
Tobias Grosser | f00bd96 | 2012-04-02 12:26:13 +0000 | [diff] [blame] | 353 | ValueMap[PN] = RHS; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void ClastStmtCodeGen::codegenSubstitutions(const clast_stmt *Assignment, |
| 357 | ScopStmt *Statement, int vectorDim, |
| 358 | std::vector<ValueMapT> *VectorVMap) { |
| 359 | int Dimension = 0; |
| 360 | |
| 361 | while (Assignment) { |
| 362 | assert(CLAST_STMT_IS_A(Assignment, stmt_ass) |
| 363 | && "Substitions are expected to be assignments"); |
| 364 | codegen((const clast_assignment *)Assignment, Statement, Dimension, |
| 365 | vectorDim, VectorVMap); |
| 366 | Assignment = Assignment->next; |
| 367 | Dimension++; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | void ClastStmtCodeGen::codegen(const clast_user_stmt *u, |
| 372 | std::vector<Value*> *IVS , const char *iterator, |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 373 | isl_set *Domain) { |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 374 | ScopStmt *Statement = (ScopStmt *)u->statement->usr; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 375 | |
| 376 | if (u->substitutions) |
| 377 | codegenSubstitutions(u->substitutions, Statement); |
| 378 | |
Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 379 | int VectorDimensions = IVS ? IVS->size() : 1; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 380 | |
Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 381 | if (VectorDimensions == 1) { |
Tobias Grosser | 55d5208 | 2012-03-02 15:20:39 +0000 | [diff] [blame] | 382 | BlockGenerator::generate(Builder, *Statement, ValueMap, P); |
Tobias Grosser | 80998e7 | 2012-03-02 11:27:28 +0000 | [diff] [blame] | 383 | return; |
| 384 | } |
| 385 | |
| 386 | VectorValueMapT VectorMap(VectorDimensions); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 387 | |
| 388 | if (IVS) { |
| 389 | assert (u->substitutions && "Substitutions expected!"); |
| 390 | int i = 0; |
| 391 | for (std::vector<Value*>::iterator II = IVS->begin(), IE = IVS->end(); |
| 392 | II != IE; ++II) { |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 393 | ClastVars[iterator] = *II; |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 394 | codegenSubstitutions(u->substitutions, Statement, i, &VectorMap); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 395 | i++; |
| 396 | } |
| 397 | } |
| 398 | |
Tobias Grosser | 55d5208 | 2012-03-02 15:20:39 +0000 | [diff] [blame] | 399 | VectorBlockGenerator::generate(Builder, *Statement, VectorMap, Domain, P); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void ClastStmtCodeGen::codegen(const clast_block *b) { |
| 403 | if (b->body) |
| 404 | codegen(b->body); |
| 405 | } |
| 406 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 407 | void ClastStmtCodeGen::codegenForSequential(const clast_for *f) { |
| 408 | Value *LowerBound, *UpperBound, *IV, *Stride; |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 409 | BasicBlock *AfterBB; |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 410 | Type *IntPtrTy = getIntPtrTy(); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 411 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 412 | LowerBound = ExpGen.codegen(f->LB, IntPtrTy); |
| 413 | UpperBound = ExpGen.codegen(f->UB, IntPtrTy); |
| 414 | Stride = Builder.getInt(APInt_from_MPZ(f->stride)); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 415 | |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 416 | IV = createLoop(LowerBound, UpperBound, Stride, Builder, P, AfterBB); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 417 | |
| 418 | // Add loop iv to symbols. |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 419 | ClastVars[f->iterator] = IV; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 420 | |
| 421 | if (f->body) |
| 422 | codegen(f->body); |
| 423 | |
| 424 | // Loop is finished, so remove its iv from the live symbols. |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 425 | ClastVars.erase(f->iterator); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 426 | Builder.SetInsertPoint(AfterBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 429 | SetVector<Value*> ClastStmtCodeGen::getOMPValues() { |
| 430 | SetVector<Value*> Values; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 431 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 432 | // The clast variables |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 433 | for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end(); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 434 | I != E; I++) |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 435 | Values.insert(I->second); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 436 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 437 | // The memory reference base addresses |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 438 | for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { |
| 439 | ScopStmt *Stmt = *SI; |
| 440 | for (SmallVector<MemoryAccess*, 8>::iterator I = Stmt->memacc_begin(), |
| 441 | E = Stmt->memacc_end(); I != E; ++I) { |
| 442 | Value *BaseAddr = const_cast<Value*>((*I)->getBaseAddr()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 443 | Values.insert((BaseAddr)); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 447 | return Values; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 450 | void ClastStmtCodeGen::updateWithValueMap(OMPGenerator::ValueToValueMapTy &VMap, |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 451 | bool Reverse) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 452 | std::set<Value*> Inserted; |
| 453 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 454 | if (Reverse) { |
| 455 | OMPGenerator::ValueToValueMapTy ReverseMap; |
| 456 | |
| 457 | for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end(); |
| 458 | I != E; ++I) |
| 459 | ReverseMap.insert(std::make_pair(I->second, I->first)); |
| 460 | |
| 461 | for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end(); |
| 462 | I != E; I++) { |
| 463 | ClastVars[I->first] = ReverseMap[I->second]; |
| 464 | Inserted.insert(I->second); |
| 465 | } |
| 466 | |
| 467 | /// FIXME: At the moment we do not reverse the update of the ValueMap. |
| 468 | /// This is incomplet, but the failure should be obvious, such that |
| 469 | /// we can fix this later. |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | for (CharMapT::iterator I = ClastVars.begin(), E = ClastVars.end(); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 474 | I != E; I++) { |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 475 | ClastVars[I->first] = VMap[I->second]; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 476 | Inserted.insert(I->second); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 479 | for (std::map<Value*, Value*>::iterator I = VMap.begin(), E = VMap.end(); |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 480 | I != E; ++I) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 481 | if (Inserted.count(I->first)) |
| 482 | continue; |
| 483 | |
| 484 | ValueMap[I->first] = I->second; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 488 | static void clearDomtree(Function *F, DominatorTree &DT) { |
| 489 | DomTreeNode *N = DT.getNode(&F->getEntryBlock()); |
| 490 | std::vector<BasicBlock*> Nodes; |
| 491 | for (po_iterator<DomTreeNode*> I = po_begin(N), E = po_end(N); I != E; ++I) |
| 492 | Nodes.push_back(I->getBlock()); |
| 493 | |
| 494 | for (std::vector<BasicBlock*>::iterator I = Nodes.begin(), E = Nodes.end(); |
| 495 | I != E; ++I) |
| 496 | DT.eraseNode(*I); |
| 497 | } |
| 498 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 499 | void ClastStmtCodeGen::codegenForOpenMP(const clast_for *For) { |
Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 500 | Value *Stride, *LB, *UB, *IV; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 501 | BasicBlock::iterator LoopBody; |
| 502 | IntegerType *IntPtrTy = getIntPtrTy(); |
| 503 | SetVector<Value*> Values; |
| 504 | OMPGenerator::ValueToValueMapTy VMap; |
| 505 | OMPGenerator OMPGen(Builder, P); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 506 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 507 | Stride = Builder.getInt(APInt_from_MPZ(For->stride)); |
| 508 | Stride = Builder.CreateSExtOrBitCast(Stride, IntPtrTy); |
Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 509 | LB = ExpGen.codegen(For->LB, IntPtrTy); |
| 510 | UB = ExpGen.codegen(For->UB, IntPtrTy); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 511 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 512 | Values = getOMPValues(); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 513 | |
Tobias Grosser | ebf3008 | 2012-03-23 12:20:32 +0000 | [diff] [blame] | 514 | IV = OMPGen.createParallelLoop(LB, UB, Stride, Values, VMap, &LoopBody); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 515 | BasicBlock::iterator AfterLoop = Builder.GetInsertPoint(); |
| 516 | Builder.SetInsertPoint(LoopBody); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 517 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 518 | updateWithValueMap(VMap, /* reverse */ false); |
| 519 | ClastVars[For->iterator] = IV; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 520 | |
| 521 | if (For->body) |
| 522 | codegen(For->body); |
| 523 | |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 524 | ClastVars.erase(For->iterator); |
| 525 | updateWithValueMap(VMap, /* reverse */ true); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 526 | |
Tobias Grosser | 900893d | 2012-03-29 13:10:26 +0000 | [diff] [blame] | 527 | clearDomtree((*LoopBody).getParent()->getParent(), |
| 528 | P->getAnalysis<DominatorTree>()); |
| 529 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 530 | Builder.SetInsertPoint(AfterLoop); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | bool ClastStmtCodeGen::isInnermostLoop(const clast_for *f) { |
| 534 | const clast_stmt *stmt = f->body; |
| 535 | |
| 536 | while (stmt) { |
| 537 | if (!CLAST_STMT_IS_A(stmt, stmt_user)) |
| 538 | return false; |
| 539 | |
| 540 | stmt = stmt->next; |
| 541 | } |
| 542 | |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | int ClastStmtCodeGen::getNumberOfIterations(const clast_for *f) { |
| 547 | isl_set *loopDomain = isl_set_copy(isl_set_from_cloog_domain(f->domain)); |
| 548 | isl_set *tmp = isl_set_copy(loopDomain); |
| 549 | |
| 550 | // Calculate a map similar to the identity map, but with the last input |
| 551 | // and output dimension not related. |
| 552 | // [i0, i1, i2, i3] -> [i0, i1, i2, o0] |
| 553 | isl_space *Space = isl_set_get_space(loopDomain); |
| 554 | Space = isl_space_drop_outputs(Space, |
| 555 | isl_set_dim(loopDomain, isl_dim_set) - 2, 1); |
| 556 | Space = isl_space_map_from_set(Space); |
| 557 | isl_map *identity = isl_map_identity(Space); |
| 558 | identity = isl_map_add_dims(identity, isl_dim_in, 1); |
| 559 | identity = isl_map_add_dims(identity, isl_dim_out, 1); |
| 560 | |
| 561 | isl_map *map = isl_map_from_domain_and_range(tmp, loopDomain); |
| 562 | map = isl_map_intersect(map, identity); |
| 563 | |
| 564 | isl_map *lexmax = isl_map_lexmax(isl_map_copy(map)); |
| 565 | isl_map *lexmin = isl_map_lexmin(map); |
| 566 | isl_map *sub = isl_map_sum(lexmax, isl_map_neg(lexmin)); |
| 567 | |
| 568 | isl_set *elements = isl_map_range(sub); |
| 569 | |
| 570 | if (!isl_set_is_singleton(elements)) { |
| 571 | isl_set_free(elements); |
| 572 | return -1; |
| 573 | } |
| 574 | |
| 575 | isl_point *p = isl_set_sample_point(elements); |
| 576 | |
| 577 | isl_int v; |
| 578 | isl_int_init(v); |
| 579 | isl_point_get_coordinate(p, isl_dim_set, isl_set_n_dim(loopDomain) - 1, &v); |
| 580 | int numberIterations = isl_int_get_si(v); |
| 581 | isl_int_clear(v); |
| 582 | isl_point_free(p); |
| 583 | |
| 584 | return (numberIterations) / isl_int_get_si(f->stride) + 1; |
| 585 | } |
| 586 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 587 | void ClastStmtCodeGen::codegenForVector(const clast_for *F) { |
| 588 | DEBUG(dbgs() << "Vectorizing loop '" << F->iterator << "'\n";); |
| 589 | int VectorWidth = getNumberOfIterations(F); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 590 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 591 | Value *LB = ExpGen.codegen(F->LB, getIntPtrTy()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 592 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 593 | APInt Stride = APInt_from_MPZ(F->stride); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 594 | IntegerType *LoopIVType = dyn_cast<IntegerType>(LB->getType()); |
| 595 | Stride = Stride.zext(LoopIVType->getBitWidth()); |
| 596 | Value *StrideValue = ConstantInt::get(LoopIVType, Stride); |
| 597 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 598 | std::vector<Value*> IVS(VectorWidth); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 599 | IVS[0] = LB; |
| 600 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 601 | for (int i = 1; i < VectorWidth; i++) |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 602 | IVS[i] = Builder.CreateAdd(IVS[i-1], StrideValue, "p_vector_iv"); |
| 603 | |
Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 604 | isl_set *Domain = isl_set_from_cloog_domain(F->domain); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 605 | |
| 606 | // Add loop iv to symbols. |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 607 | ClastVars[F->iterator] = LB; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 608 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 609 | const clast_stmt *Stmt = F->body; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 610 | |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 611 | while (Stmt) { |
Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 612 | codegen((const clast_user_stmt *)Stmt, &IVS, F->iterator, |
| 613 | isl_set_copy(Domain)); |
Tobias Grosser | 2da263e | 2012-03-15 09:34:55 +0000 | [diff] [blame] | 614 | Stmt = Stmt->next; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | // Loop is finished, so remove its iv from the live symbols. |
Tobias Grosser | 00d898d | 2012-03-15 09:34:58 +0000 | [diff] [blame] | 618 | isl_set_free(Domain); |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 619 | ClastVars.erase(F->iterator); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Tobias Grosser | e192b23 | 2012-05-22 08:46:07 +0000 | [diff] [blame] | 622 | |
| 623 | bool ClastStmtCodeGen::isParallelFor(const clast_for *f) { |
| 624 | isl_set *Domain = isl_set_from_cloog_domain(f->domain); |
| 625 | assert(Domain && "Cannot access domain of loop"); |
| 626 | |
| 627 | Dependences &D = P->getAnalysis<Dependences>(); |
| 628 | |
| 629 | return D.isParallelDimension(isl_set_copy(Domain), isl_set_n_dim(Domain)); |
| 630 | } |
| 631 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 632 | void ClastStmtCodeGen::codegen(const clast_for *f) { |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 633 | bool Vector = PollyVectorizerChoice != VECTORIZER_NONE; |
Tobias Grosser | e192b23 | 2012-05-22 08:46:07 +0000 | [diff] [blame] | 634 | if ((Vector || OpenMP) && isParallelFor(f)) { |
Tobias Grosser | ce3f537 | 2012-03-02 11:26:42 +0000 | [diff] [blame] | 635 | if (Vector && isInnermostLoop(f) && (-1 != getNumberOfIterations(f)) |
| 636 | && (getNumberOfIterations(f) <= 16)) { |
| 637 | codegenForVector(f); |
| 638 | return; |
| 639 | } |
| 640 | |
| 641 | if (OpenMP && !parallelCodeGeneration) { |
| 642 | parallelCodeGeneration = true; |
| 643 | parallelLoops.push_back(f->iterator); |
| 644 | codegenForOpenMP(f); |
| 645 | parallelCodeGeneration = false; |
| 646 | return; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | codegenForSequential(f); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | Value *ClastStmtCodeGen::codegen(const clast_equation *eq) { |
Tobias Grosser | e9ffea2 | 2012-03-15 09:34:48 +0000 | [diff] [blame] | 654 | Value *LHS = ExpGen.codegen(eq->LHS, getIntPtrTy()); |
| 655 | Value *RHS = ExpGen.codegen(eq->RHS, getIntPtrTy()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 656 | CmpInst::Predicate P; |
| 657 | |
| 658 | if (eq->sign == 0) |
| 659 | P = ICmpInst::ICMP_EQ; |
| 660 | else if (eq->sign > 0) |
| 661 | P = ICmpInst::ICMP_SGE; |
| 662 | else |
| 663 | P = ICmpInst::ICMP_SLE; |
| 664 | |
| 665 | return Builder.CreateICmp(P, LHS, RHS); |
| 666 | } |
| 667 | |
| 668 | void ClastStmtCodeGen::codegen(const clast_guard *g) { |
| 669 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 670 | LLVMContext &Context = F->getContext(); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 671 | |
| 672 | BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(), |
| 673 | Builder.GetInsertPoint(), P); |
| 674 | CondBB->setName("polly.cond"); |
| 675 | BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P); |
| 676 | MergeBB->setName("polly.merge"); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 677 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 678 | |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 679 | DominatorTree &DT = P->getAnalysis<DominatorTree>(); |
| 680 | DT.addNewBlock(ThenBB, CondBB); |
| 681 | DT.changeImmediateDominator(MergeBB, CondBB); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 682 | |
| 683 | CondBB->getTerminator()->eraseFromParent(); |
| 684 | |
| 685 | Builder.SetInsertPoint(CondBB); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 686 | |
| 687 | Value *Predicate = codegen(&(g->eq[0])); |
| 688 | |
| 689 | for (int i = 1; i < g->n; ++i) { |
| 690 | Value *TmpPredicate = codegen(&(g->eq[i])); |
| 691 | Predicate = Builder.CreateAnd(Predicate, TmpPredicate); |
| 692 | } |
| 693 | |
| 694 | Builder.CreateCondBr(Predicate, ThenBB, MergeBB); |
| 695 | Builder.SetInsertPoint(ThenBB); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 696 | Builder.CreateBr(MergeBB); |
| 697 | Builder.SetInsertPoint(ThenBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 698 | |
| 699 | codegen(g->then); |
Tobias Grosser | 62a3c96 | 2012-02-16 09:56:21 +0000 | [diff] [blame] | 700 | |
| 701 | Builder.SetInsertPoint(MergeBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | void ClastStmtCodeGen::codegen(const clast_stmt *stmt) { |
| 705 | if (CLAST_STMT_IS_A(stmt, stmt_root)) |
| 706 | assert(false && "No second root statement expected"); |
| 707 | else if (CLAST_STMT_IS_A(stmt, stmt_ass)) |
| 708 | codegen((const clast_assignment *)stmt); |
| 709 | else if (CLAST_STMT_IS_A(stmt, stmt_user)) |
| 710 | codegen((const clast_user_stmt *)stmt); |
| 711 | else if (CLAST_STMT_IS_A(stmt, stmt_block)) |
| 712 | codegen((const clast_block *)stmt); |
| 713 | else if (CLAST_STMT_IS_A(stmt, stmt_for)) |
| 714 | codegen((const clast_for *)stmt); |
| 715 | else if (CLAST_STMT_IS_A(stmt, stmt_guard)) |
| 716 | codegen((const clast_guard *)stmt); |
| 717 | |
| 718 | if (stmt->next) |
| 719 | codegen(stmt->next); |
| 720 | } |
| 721 | |
| 722 | void ClastStmtCodeGen::addParameters(const CloogNames *names) { |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 723 | SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly"); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 724 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 725 | int i = 0; |
| 726 | for (Scop::param_iterator PI = S->param_begin(), PE = S->param_end(); |
| 727 | PI != PE; ++PI) { |
| 728 | assert(i < names->nb_parameters && "Not enough parameter names"); |
| 729 | |
| 730 | const SCEV *Param = *PI; |
| 731 | Type *Ty = Param->getType(); |
| 732 | |
| 733 | Instruction *insertLocation = --(Builder.GetInsertBlock()->end()); |
| 734 | Value *V = Rewriter.expandCodeFor(Param, Ty, insertLocation); |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 735 | ClastVars[names->parameters[i]] = V; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 736 | |
| 737 | ++i; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | void ClastStmtCodeGen::codegen(const clast_root *r) { |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 742 | addParameters(r->names); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 743 | |
| 744 | parallelCodeGeneration = false; |
| 745 | |
| 746 | const clast_stmt *stmt = (const clast_stmt*) r; |
| 747 | if (stmt->next) |
| 748 | codegen(stmt->next); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Tobias Grosser | d596b37 | 2012-03-15 09:34:52 +0000 | [diff] [blame] | 751 | ClastStmtCodeGen::ClastStmtCodeGen(Scop *scop, IRBuilder<> &B, Pass *P) : |
Tobias Grosser | 5e8ffa8 | 2012-03-23 12:20:36 +0000 | [diff] [blame] | 752 | S(scop), P(P), Builder(B), ExpGen(Builder, ClastVars) {} |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 753 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 754 | namespace { |
| 755 | class CodeGeneration : public ScopPass { |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 756 | std::vector<std::string> ParallelLoops; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 757 | |
| 758 | public: |
| 759 | static char ID; |
| 760 | |
| 761 | CodeGeneration() : ScopPass(ID) {} |
| 762 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 763 | |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 764 | bool runOnScop(Scop &S) { |
| 765 | ParallelLoops.clear(); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 766 | |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 767 | assert(S.getRegion().isSimple() && "Only simple regions are supported"); |
Tobias Grosser | cb47dfe | 2012-02-15 09:58:50 +0000 | [diff] [blame] | 768 | |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 769 | BasicBlock *StartBlock = executeScopConditionally(S, this); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 770 | |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 771 | IRBuilder<> Builder(StartBlock->begin()); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 772 | |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 773 | ClastStmtCodeGen CodeGen(&S, Builder, this); |
Tobias Grosser | 3fdecae | 2011-05-14 19:02:39 +0000 | [diff] [blame] | 774 | CloogInfo &C = getAnalysis<CloogInfo>(); |
| 775 | CodeGen.codegen(C.getClast()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 776 | |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 777 | ParallelLoops.insert(ParallelLoops.begin(), |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 778 | CodeGen.getParallelLoops().begin(), |
| 779 | CodeGen.getParallelLoops().end()); |
Tobias Grosser | abb6dcd | 2011-05-14 19:02:34 +0000 | [diff] [blame] | 780 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | virtual void printScop(raw_ostream &OS) const { |
Tobias Grosser | 3a275d2 | 2012-05-29 09:11:54 +0000 | [diff] [blame] | 784 | for (std::vector<std::string>::const_iterator PI = ParallelLoops.begin(), |
| 785 | PE = ParallelLoops.end(); PI != PE; ++PI) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 786 | OS << "Parallel loop with iterator '" << *PI << "' generated\n"; |
| 787 | } |
| 788 | |
| 789 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 790 | AU.addRequired<CloogInfo>(); |
| 791 | AU.addRequired<Dependences>(); |
| 792 | AU.addRequired<DominatorTree>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 793 | AU.addRequired<RegionInfo>(); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 794 | AU.addRequired<ScalarEvolution>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 795 | AU.addRequired<ScopDetection>(); |
| 796 | AU.addRequired<ScopInfo>(); |
| 797 | AU.addRequired<TargetData>(); |
| 798 | |
| 799 | AU.addPreserved<CloogInfo>(); |
| 800 | AU.addPreserved<Dependences>(); |
Tobias Grosser | 5d6eb86 | 2011-05-14 19:02:45 +0000 | [diff] [blame] | 801 | |
Tobias Grosser | 4e3f9a4 | 2011-05-23 15:23:36 +0000 | [diff] [blame] | 802 | // FIXME: We do not create LoopInfo for the newly generated loops. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 803 | AU.addPreserved<LoopInfo>(); |
| 804 | AU.addPreserved<DominatorTree>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 805 | AU.addPreserved<ScopDetection>(); |
| 806 | AU.addPreserved<ScalarEvolution>(); |
Tobias Grosser | 5d6eb86 | 2011-05-14 19:02:45 +0000 | [diff] [blame] | 807 | |
Tobias Grosser | 4e3f9a4 | 2011-05-23 15:23:36 +0000 | [diff] [blame] | 808 | // FIXME: We do not yet add regions for the newly generated code to the |
| 809 | // region tree. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 810 | AU.addPreserved<RegionInfo>(); |
| 811 | AU.addPreserved<TempScopInfo>(); |
| 812 | AU.addPreserved<ScopInfo>(); |
| 813 | AU.addPreservedID(IndependentBlocksID); |
| 814 | } |
| 815 | }; |
| 816 | } |
| 817 | |
| 818 | char CodeGeneration::ID = 1; |
| 819 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 820 | INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", |
Tobias Grosser | 3c2efba | 2012-03-06 07:38:57 +0000 | [diff] [blame] | 821 | "Polly - Create LLVM-IR from SCoPs", false, false) |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 822 | INITIALIZE_PASS_DEPENDENCY(CloogInfo) |
| 823 | INITIALIZE_PASS_DEPENDENCY(Dependences) |
| 824 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 825 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 826 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 827 | INITIALIZE_PASS_DEPENDENCY(ScopDetection) |
| 828 | INITIALIZE_PASS_DEPENDENCY(TargetData) |
| 829 | INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", |
Tobias Grosser | 3c2efba | 2012-03-06 07:38:57 +0000 | [diff] [blame] | 830 | "Polly - Create LLVM-IR from SCoPs", false, false) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 831 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 832 | Pass *polly::createCodeGenerationPass() { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 833 | return new CodeGeneration(); |
| 834 | } |
Sebastian Pop | e1f6554 | 2012-05-07 16:35:11 +0000 | [diff] [blame] | 835 | |
| 836 | #endif // CLOOG_FOUND |