Sebastian Pop | 082cea8 | 2012-05-07 16:20:07 +0000 | [diff] [blame] | 1 | //===------ IslCodeGeneration.cpp - Code generate the Scops using ISL. ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The IslCodeGeneration pass takes a Scop created by ScopInfo and translates it |
| 11 | // back to LLVM-IR using the ISL code generator. |
| 12 | // |
| 13 | // The Scop describes the high level memory behaviour of a control flow region. |
| 14 | // Transformation passes can update the schedule (execution order) of statements |
| 15 | // in the Scop. ISL is used to generate an abstract syntax tree that reflects |
| 16 | // the updated execution order. This clast is used to create new LLVM-IR that is |
| 17 | // computationally equivalent to the original control flow region, but executes |
| 18 | // its code in the new execution order defined by the changed scattering. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 21 | #include "polly/Config/config.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 22 | #include "polly/CodeGen/BlockGenerators.h" |
| 23 | #include "polly/CodeGen/CodeGeneration.h" |
| 24 | #include "polly/CodeGen/IslAst.h" |
| 25 | #include "polly/CodeGen/LoopGenerators.h" |
| 26 | #include "polly/CodeGen/Utils.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 27 | #include "polly/Dependences.h" |
| 28 | #include "polly/LinkAllPasses.h" |
| 29 | #include "polly/ScopInfo.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 30 | #include "polly/Support/GICHelper.h" |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 31 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 32 | #include "polly/TempScopInfo.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/LoopInfo.h" |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame^] | 34 | #include "llvm/Analysis/PostDominators.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 36 | #include "llvm/IR/Module.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
| 38 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 535d52c | 2013-01-02 11:47:44 +0000 | [diff] [blame] | 39 | #include "llvm/IR/DataLayout.h" |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 40 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 41 | |
| 42 | #include "isl/union_map.h" |
| 43 | #include "isl/list.h" |
| 44 | #include "isl/ast.h" |
| 45 | #include "isl/ast_build.h" |
| 46 | #include "isl/set.h" |
| 47 | #include "isl/map.h" |
| 48 | #include "isl/aff.h" |
| 49 | |
| 50 | #include <map> |
| 51 | |
| 52 | using namespace polly; |
| 53 | using namespace llvm; |
| 54 | |
Chandler Carruth | 95fef94 | 2014-04-22 03:30:19 +0000 | [diff] [blame] | 55 | #define DEBUG_TYPE "polly-codegen-isl" |
| 56 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 57 | /// @brief Insert function calls that print certain LLVM values at run time. |
| 58 | /// |
| 59 | /// This class inserts libc function calls to print certain LLVM values at |
| 60 | /// run time. |
| 61 | class RuntimeDebugBuilder { |
| 62 | public: |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 63 | RuntimeDebugBuilder(PollyIRBuilder &Builder) : Builder(Builder) {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 64 | |
| 65 | /// @brief Print a string to stdout. |
| 66 | /// |
| 67 | /// @param String The string to print. |
| 68 | void createStrPrinter(std::string String); |
| 69 | |
| 70 | /// @brief Print an integer value to stdout. |
| 71 | /// |
| 72 | /// @param V The value to print. |
| 73 | void createIntPrinter(Value *V); |
| 74 | |
| 75 | private: |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 76 | PollyIRBuilder &Builder; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 77 | |
| 78 | /// @brief Add a call to the fflush function with no file pointer given. |
| 79 | /// |
| 80 | /// This call will flush all opened file pointers including stdout and stderr. |
| 81 | void createFlush(); |
| 82 | |
| 83 | /// @brief Get a reference to the 'printf' function. |
| 84 | /// |
| 85 | /// If the current module does not yet contain a reference to printf, we |
| 86 | /// insert a reference to it. Otherwise the existing reference is returned. |
| 87 | Function *getPrintF(); |
| 88 | }; |
| 89 | |
| 90 | Function *RuntimeDebugBuilder::getPrintF() { |
| 91 | Module *M = Builder.GetInsertBlock()->getParent()->getParent(); |
| 92 | const char *Name = "printf"; |
| 93 | Function *F = M->getFunction(Name); |
| 94 | |
| 95 | if (!F) { |
| 96 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 97 | FunctionType *Ty = |
| 98 | FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), true); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 99 | F = Function::Create(Ty, Linkage, Name, M); |
| 100 | } |
| 101 | |
| 102 | return F; |
| 103 | } |
| 104 | |
| 105 | void RuntimeDebugBuilder::createFlush() { |
| 106 | Module *M = Builder.GetInsertBlock()->getParent()->getParent(); |
| 107 | const char *Name = "fflush"; |
| 108 | Function *F = M->getFunction(Name); |
| 109 | |
| 110 | if (!F) { |
| 111 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 112 | FunctionType *Ty = |
| 113 | FunctionType::get(Builder.getInt32Ty(), Builder.getInt8PtrTy(), false); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 114 | F = Function::Create(Ty, Linkage, Name, M); |
| 115 | } |
| 116 | |
| 117 | Builder.CreateCall(F, Constant::getNullValue(Builder.getInt8PtrTy())); |
| 118 | } |
| 119 | |
| 120 | void RuntimeDebugBuilder::createStrPrinter(std::string String) { |
| 121 | Function *F = getPrintF(); |
| 122 | Value *StringValue = Builder.CreateGlobalStringPtr(String); |
| 123 | Builder.CreateCall(F, StringValue); |
| 124 | |
| 125 | createFlush(); |
| 126 | } |
| 127 | |
| 128 | void RuntimeDebugBuilder::createIntPrinter(Value *V) { |
| 129 | IntegerType *Ty = dyn_cast<IntegerType>(V->getType()); |
Tobias Grosser | 58032cb | 2013-06-23 01:29:29 +0000 | [diff] [blame] | 130 | (void)Ty; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 131 | assert(Ty && Ty->getBitWidth() == 64 && |
| 132 | "Cannot insert printer for this type."); |
| 133 | |
| 134 | Function *F = getPrintF(); |
| 135 | Value *String = Builder.CreateGlobalStringPtr("%ld"); |
| 136 | Builder.CreateCall2(F, String, V); |
| 137 | createFlush(); |
| 138 | } |
| 139 | |
Tobias Grosser | 9818bc8 | 2014-04-22 14:26:51 +0000 | [diff] [blame] | 140 | /// @brief LLVM-IR generator for isl_ast_expr[essions] |
| 141 | /// |
| 142 | /// This generator generates LLVM-IR that performs the computation described by |
| 143 | /// an isl_ast_expr[ession]. |
| 144 | /// |
| 145 | /// Example: |
| 146 | /// |
| 147 | /// An isl_ast_expr[ession] can look like this: |
| 148 | /// |
| 149 | /// (N + M) + 10 |
| 150 | /// |
| 151 | /// The IslExprBuilder could create the following LLVM-IR: |
| 152 | /// |
| 153 | /// %tmp1 = add nsw i64 %N |
| 154 | /// %tmp2 = add nsw i64 %tmp1, %M |
| 155 | /// %tmp3 = add nsw i64 %tmp2, 10 |
| 156 | /// |
| 157 | /// The implementation of this class is mostly a mapping from isl_ast_expr |
| 158 | /// constructs to the corresponding LLVM-IR constructs. |
| 159 | /// |
| 160 | /// The following decisions may need some explanation: |
| 161 | /// |
| 162 | /// 1) Which data-type to choose |
| 163 | /// |
| 164 | /// isl_ast_expr[essions] are untyped expressions that assume arbitrary |
| 165 | /// precision integer computations. LLVM-IR instead has fixed size integers. |
| 166 | /// When lowering to LLVM-IR we need to chose both the size of the data type and |
| 167 | /// the sign of the operations we use. |
| 168 | /// |
| 169 | /// At the moment, we hardcode i64 bit signed computations. Our experience has |
| 170 | /// shown that 64 bit are generally large enough for the loop bounds that appear |
| 171 | /// in the wild. Signed computations are needed, as loop bounds may become |
| 172 | /// negative. |
| 173 | /// |
| 174 | /// FIXME: Hardcoding sizes can cause issues: |
| 175 | /// |
| 176 | /// a) Certain run-time checks that we may want to generate can involve the |
| 177 | /// size of the data types the computation is performed on. When code |
| 178 | /// generating these run-time checks to isl_ast_expr[essions], the |
| 179 | /// resulting computation may require more than 64 bit. |
| 180 | /// |
| 181 | /// b) On embedded systems and especially for high-level-synthesis 64 bit |
| 182 | /// computations are very costly. |
| 183 | /// |
| 184 | /// The right approach is to compute the minimal necessary bitwidth and |
| 185 | /// signedness for each subexpression during in the isl AST generation and |
| 186 | /// to use this information in our IslAstGenerator. Preliminary patches are |
| 187 | /// available, but have not been committed yet. |
| 188 | /// |
| 189 | /// 2) We always flag computations with 'nsw' |
| 190 | /// |
| 191 | /// As isl_ast_expr[essions] assume arbitrary precision, no wrapping should |
| 192 | /// ever occur in the generated LLVM-IR (assuming the data type chosen is large |
| 193 | /// enough). |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 194 | class IslExprBuilder { |
| 195 | public: |
Tobias Grosser | 9818bc8 | 2014-04-22 14:26:51 +0000 | [diff] [blame] | 196 | /// @brief Construct an IslExprBuilder. |
| 197 | /// |
| 198 | /// @param Builder The IRBuilder used to construct the isl_ast_expr[ession]. |
| 199 | /// The insert location of this IRBuilder defines WHERE the |
| 200 | /// corresponding LLVM-IR is generated. |
| 201 | /// |
| 202 | /// @param IDToValue The isl_ast_expr[ession] may reference parameters or |
| 203 | /// variables (identified by an isl_id). The IDTOValue map |
| 204 | /// specifies the LLVM-IR Values that correspond to these |
| 205 | /// parameters and variables. |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 206 | IslExprBuilder(PollyIRBuilder &Builder, |
Tobias Grosser | 9818bc8 | 2014-04-22 14:26:51 +0000 | [diff] [blame] | 207 | std::map<isl_id *, Value *> &IDToValue) |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 208 | : Builder(Builder), IDToValue(IDToValue) {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 209 | |
Tobias Grosser | 9818bc8 | 2014-04-22 14:26:51 +0000 | [diff] [blame] | 210 | /// @brief Create LLVM-IR for an isl_ast_expr[ession]. |
| 211 | /// |
| 212 | /// @param Expr The ast expression for which we generate LLVM-IR. |
| 213 | /// |
| 214 | /// @return The llvm::Value* containing the result of the computation. |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 215 | Value *create(__isl_take isl_ast_expr *Expr); |
Tobias Grosser | 9818bc8 | 2014-04-22 14:26:51 +0000 | [diff] [blame] | 216 | |
| 217 | /// @brief Return the largest of two types. |
| 218 | /// |
| 219 | /// @param T1 The first type. |
| 220 | /// @param T2 The second type. |
| 221 | /// |
| 222 | /// @return The largest of the two types. |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 223 | Type *getWidestType(Type *T1, Type *T2); |
Tobias Grosser | 9818bc8 | 2014-04-22 14:26:51 +0000 | [diff] [blame] | 224 | |
| 225 | /// @brief Return the type with which this expression should be computed. |
| 226 | /// |
| 227 | /// The type needs to be large enough to hold all possible input and all |
| 228 | /// possible output values. |
| 229 | /// |
| 230 | /// @param Expr The expression for which to find the type. |
| 231 | /// @return The type with which the expression should be computed. |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 232 | IntegerType *getType(__isl_keep isl_ast_expr *Expr); |
| 233 | |
| 234 | private: |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 235 | PollyIRBuilder &Builder; |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 236 | std::map<isl_id *, Value *> &IDToValue; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 237 | |
| 238 | Value *createOp(__isl_take isl_ast_expr *Expr); |
| 239 | Value *createOpUnary(__isl_take isl_ast_expr *Expr); |
| 240 | Value *createOpBin(__isl_take isl_ast_expr *Expr); |
| 241 | Value *createOpNAry(__isl_take isl_ast_expr *Expr); |
| 242 | Value *createOpSelect(__isl_take isl_ast_expr *Expr); |
| 243 | Value *createOpICmp(__isl_take isl_ast_expr *Expr); |
| 244 | Value *createOpBoolean(__isl_take isl_ast_expr *Expr); |
| 245 | Value *createId(__isl_take isl_ast_expr *Expr); |
| 246 | Value *createInt(__isl_take isl_ast_expr *Expr); |
| 247 | }; |
| 248 | |
| 249 | Type *IslExprBuilder::getWidestType(Type *T1, Type *T2) { |
| 250 | assert(isa<IntegerType>(T1) && isa<IntegerType>(T2)); |
| 251 | |
| 252 | if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits()) |
| 253 | return T2; |
| 254 | else |
| 255 | return T1; |
| 256 | } |
| 257 | |
| 258 | Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) { |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 259 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus && |
| 260 | "Unsupported unary operation"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 261 | |
| 262 | Value *V; |
| 263 | Type *MaxType = getType(Expr); |
| 264 | |
| 265 | V = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 266 | MaxType = getWidestType(MaxType, V->getType()); |
| 267 | |
| 268 | if (MaxType != V->getType()) |
| 269 | V = Builder.CreateSExt(V, MaxType); |
| 270 | |
| 271 | isl_ast_expr_free(Expr); |
| 272 | return Builder.CreateNSWNeg(V); |
| 273 | } |
| 274 | |
| 275 | Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) { |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 276 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 277 | "isl ast expression not of type isl_ast_op"); |
| 278 | assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 && |
| 279 | "We need at least two operands in an n-ary operation"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 280 | |
| 281 | Value *V; |
| 282 | |
| 283 | V = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 284 | |
| 285 | for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr); ++i) { |
| 286 | Value *OpV; |
| 287 | OpV = create(isl_ast_expr_get_op_arg(Expr, i)); |
| 288 | |
| 289 | Type *Ty = getWidestType(V->getType(), OpV->getType()); |
| 290 | |
| 291 | if (Ty != OpV->getType()) |
| 292 | OpV = Builder.CreateSExt(OpV, Ty); |
| 293 | |
| 294 | if (Ty != V->getType()) |
| 295 | V = Builder.CreateSExt(V, Ty); |
| 296 | |
| 297 | switch (isl_ast_expr_get_op_type(Expr)) { |
| 298 | default: |
| 299 | llvm_unreachable("This is no n-ary isl ast expression"); |
| 300 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 301 | case isl_ast_op_max: { |
| 302 | Value *Cmp = Builder.CreateICmpSGT(V, OpV); |
| 303 | V = Builder.CreateSelect(Cmp, V, OpV); |
| 304 | continue; |
| 305 | } |
| 306 | case isl_ast_op_min: { |
| 307 | Value *Cmp = Builder.CreateICmpSLT(V, OpV); |
| 308 | V = Builder.CreateSelect(Cmp, V, OpV); |
| 309 | continue; |
| 310 | } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | // TODO: We can truncate the result, if it fits into a smaller type. This can |
| 315 | // help in cases where we have larger operands (e.g. i67) but the result is |
| 316 | // known to fit into i64. Without the truncation, the larger i67 type may |
| 317 | // force all subsequent operations to be performed on a non-native type. |
| 318 | isl_ast_expr_free(Expr); |
| 319 | return V; |
| 320 | } |
| 321 | |
| 322 | Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) { |
| 323 | Value *LHS, *RHS, *Res; |
| 324 | Type *MaxType; |
| 325 | isl_ast_op_type OpType; |
| 326 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 327 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 328 | "isl ast expression not of type isl_ast_op"); |
| 329 | assert(isl_ast_expr_get_op_n_arg(Expr) == 2 && |
| 330 | "not a binary isl ast expression"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 331 | |
| 332 | OpType = isl_ast_expr_get_op_type(Expr); |
| 333 | |
| 334 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 335 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 336 | |
| 337 | MaxType = LHS->getType(); |
| 338 | MaxType = getWidestType(MaxType, RHS->getType()); |
| 339 | |
| 340 | // Take the result into account when calculating the widest type. |
| 341 | // |
| 342 | // For operations such as '+' the result may require a type larger than |
| 343 | // the type of the individual operands. For other operations such as '/', the |
| 344 | // result type cannot be larger than the type of the individual operand. isl |
| 345 | // does not calculate correct types for these operations and we consequently |
| 346 | // exclude those operations here. |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 347 | switch (OpType) { |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 348 | case isl_ast_op_pdiv_q: |
| 349 | case isl_ast_op_pdiv_r: |
| 350 | case isl_ast_op_div: |
| 351 | case isl_ast_op_fdiv_q: |
| 352 | // Do nothing |
| 353 | break; |
| 354 | case isl_ast_op_add: |
| 355 | case isl_ast_op_sub: |
| 356 | case isl_ast_op_mul: |
| 357 | MaxType = getWidestType(MaxType, getType(Expr)); |
| 358 | break; |
| 359 | default: |
| 360 | llvm_unreachable("This is no binary isl ast expression"); |
| 361 | } |
| 362 | |
| 363 | if (MaxType != RHS->getType()) |
| 364 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 365 | |
| 366 | if (MaxType != LHS->getType()) |
| 367 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 368 | |
| 369 | switch (OpType) { |
| 370 | default: |
| 371 | llvm_unreachable("This is no binary isl ast expression"); |
| 372 | case isl_ast_op_add: |
| 373 | Res = Builder.CreateNSWAdd(LHS, RHS); |
| 374 | break; |
| 375 | case isl_ast_op_sub: |
| 376 | Res = Builder.CreateNSWSub(LHS, RHS); |
| 377 | break; |
| 378 | case isl_ast_op_mul: |
| 379 | Res = Builder.CreateNSWMul(LHS, RHS); |
| 380 | break; |
| 381 | case isl_ast_op_div: |
| 382 | case isl_ast_op_pdiv_q: // Dividend is non-negative |
| 383 | Res = Builder.CreateSDiv(LHS, RHS); |
| 384 | break; |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 385 | case isl_ast_op_fdiv_q: { // Round towards -infty |
| 386 | // TODO: Review code and check that this calculation does not yield |
| 387 | // incorrect overflow in some bordercases. |
| 388 | // |
| 389 | // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d |
| 390 | Value *One = ConstantInt::get(MaxType, 1); |
| 391 | Value *Zero = ConstantInt::get(MaxType, 0); |
| 392 | Value *Sum1 = Builder.CreateSub(LHS, RHS); |
| 393 | Value *Sum2 = Builder.CreateAdd(Sum1, One); |
| 394 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero); |
| 395 | Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS); |
| 396 | Res = Builder.CreateSDiv(Dividend, RHS); |
| 397 | break; |
| 398 | } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 399 | case isl_ast_op_pdiv_r: // Dividend is non-negative |
| 400 | Res = Builder.CreateSRem(LHS, RHS); |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | // TODO: We can truncate the result, if it fits into a smaller type. This can |
| 405 | // help in cases where we have larger operands (e.g. i67) but the result is |
| 406 | // known to fit into i64. Without the truncation, the larger i67 type may |
| 407 | // force all subsequent operations to be performed on a non-native type. |
| 408 | isl_ast_expr_free(Expr); |
| 409 | return Res; |
| 410 | } |
| 411 | |
| 412 | Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) { |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 413 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_select && |
| 414 | "Unsupported unary isl ast expression"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 415 | Value *LHS, *RHS, *Cond; |
| 416 | Type *MaxType = getType(Expr); |
| 417 | |
| 418 | Cond = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 419 | |
| 420 | LHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 421 | RHS = create(isl_ast_expr_get_op_arg(Expr, 2)); |
| 422 | |
| 423 | MaxType = getWidestType(MaxType, LHS->getType()); |
| 424 | MaxType = getWidestType(MaxType, RHS->getType()); |
| 425 | |
| 426 | if (MaxType != RHS->getType()) |
| 427 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 428 | |
| 429 | if (MaxType != LHS->getType()) |
| 430 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 431 | |
| 432 | // TODO: Do we want to truncate the result? |
| 433 | isl_ast_expr_free(Expr); |
| 434 | return Builder.CreateSelect(Cond, LHS, RHS); |
| 435 | } |
| 436 | |
| 437 | Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) { |
| 438 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 439 | "Expected an isl_ast_expr_op expression"); |
| 440 | |
| 441 | Value *LHS, *RHS, *Res; |
| 442 | |
| 443 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 444 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 445 | |
| 446 | Type *MaxType = LHS->getType(); |
| 447 | MaxType = getWidestType(MaxType, RHS->getType()); |
| 448 | |
| 449 | if (MaxType != RHS->getType()) |
| 450 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 451 | |
| 452 | if (MaxType != LHS->getType()) |
| 453 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 454 | |
| 455 | switch (isl_ast_expr_get_op_type(Expr)) { |
| 456 | default: |
| 457 | llvm_unreachable("Unsupported ICmp isl ast expression"); |
| 458 | case isl_ast_op_eq: |
| 459 | Res = Builder.CreateICmpEQ(LHS, RHS); |
| 460 | break; |
| 461 | case isl_ast_op_le: |
| 462 | Res = Builder.CreateICmpSLE(LHS, RHS); |
| 463 | break; |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 464 | case isl_ast_op_lt: |
| 465 | Res = Builder.CreateICmpSLT(LHS, RHS); |
| 466 | break; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 467 | case isl_ast_op_ge: |
| 468 | Res = Builder.CreateICmpSGE(LHS, RHS); |
| 469 | break; |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 470 | case isl_ast_op_gt: |
| 471 | Res = Builder.CreateICmpSGT(LHS, RHS); |
| 472 | break; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | isl_ast_expr_free(Expr); |
| 476 | return Res; |
| 477 | } |
| 478 | |
| 479 | Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) { |
| 480 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 481 | "Expected an isl_ast_expr_op expression"); |
| 482 | |
| 483 | Value *LHS, *RHS, *Res; |
| 484 | isl_ast_op_type OpType; |
| 485 | |
| 486 | OpType = isl_ast_expr_get_op_type(Expr); |
| 487 | |
| 488 | assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) && |
| 489 | "Unsupported isl_ast_op_type"); |
| 490 | |
| 491 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 492 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 493 | |
| 494 | // Even though the isl pretty printer prints the expressions as 'exp && exp' |
| 495 | // or 'exp || exp', we actually code generate the bitwise expressions |
| 496 | // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches, |
| 497 | // but it is, due to the use of i1 types, otherwise equivalent. The reason |
| 498 | // to go for bitwise operations is, that we assume the reduced control flow |
| 499 | // will outweight the overhead introduced by evaluating unneeded expressions. |
| 500 | // The isl code generation currently does not take advantage of the fact that |
| 501 | // the expression after an '||' or '&&' is in some cases not evaluated. |
| 502 | // Evaluating it anyways does not cause any undefined behaviour. |
| 503 | // |
| 504 | // TODO: Document in isl itself, that the unconditionally evaluating the |
| 505 | // second part of '||' or '&&' expressions is safe. |
| 506 | assert(LHS->getType() == Builder.getInt1Ty() && "Expected i1 type"); |
| 507 | assert(RHS->getType() == Builder.getInt1Ty() && "Expected i1 type"); |
| 508 | |
| 509 | switch (OpType) { |
| 510 | default: |
| 511 | llvm_unreachable("Unsupported boolean expression"); |
| 512 | case isl_ast_op_and: |
| 513 | Res = Builder.CreateAnd(LHS, RHS); |
| 514 | break; |
| 515 | case isl_ast_op_or: |
| 516 | Res = Builder.CreateOr(LHS, RHS); |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | isl_ast_expr_free(Expr); |
| 521 | return Res; |
| 522 | } |
| 523 | |
| 524 | Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) { |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 525 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 526 | "Expression not of type isl_ast_expr_op"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 527 | switch (isl_ast_expr_get_op_type(Expr)) { |
| 528 | case isl_ast_op_error: |
| 529 | case isl_ast_op_cond: |
| 530 | case isl_ast_op_and_then: |
| 531 | case isl_ast_op_or_else: |
| 532 | case isl_ast_op_call: |
Tobias Grosser | a38c924 | 2014-01-26 19:36:28 +0000 | [diff] [blame] | 533 | case isl_ast_op_member: |
| 534 | case isl_ast_op_access: |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 535 | llvm_unreachable("Unsupported isl ast expression"); |
| 536 | case isl_ast_op_max: |
| 537 | case isl_ast_op_min: |
| 538 | return createOpNAry(Expr); |
| 539 | case isl_ast_op_add: |
| 540 | case isl_ast_op_sub: |
| 541 | case isl_ast_op_mul: |
| 542 | case isl_ast_op_div: |
| 543 | case isl_ast_op_fdiv_q: // Round towards -infty |
| 544 | case isl_ast_op_pdiv_q: // Dividend is non-negative |
| 545 | case isl_ast_op_pdiv_r: // Dividend is non-negative |
| 546 | return createOpBin(Expr); |
| 547 | case isl_ast_op_minus: |
| 548 | return createOpUnary(Expr); |
| 549 | case isl_ast_op_select: |
| 550 | return createOpSelect(Expr); |
| 551 | case isl_ast_op_and: |
| 552 | case isl_ast_op_or: |
| 553 | return createOpBoolean(Expr); |
| 554 | case isl_ast_op_eq: |
| 555 | case isl_ast_op_le: |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 556 | case isl_ast_op_lt: |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 557 | case isl_ast_op_ge: |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 558 | case isl_ast_op_gt: |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 559 | return createOpICmp(Expr); |
| 560 | } |
| 561 | |
| 562 | llvm_unreachable("Unsupported isl_ast_expr_op kind."); |
| 563 | } |
| 564 | |
| 565 | Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) { |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 566 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id && |
| 567 | "Expression not of type isl_ast_expr_ident"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 568 | |
| 569 | isl_id *Id; |
| 570 | Value *V; |
| 571 | |
| 572 | Id = isl_ast_expr_get_id(Expr); |
| 573 | |
| 574 | assert(IDToValue.count(Id) && "Identifier not found"); |
| 575 | |
| 576 | V = IDToValue[Id]; |
| 577 | |
| 578 | isl_id_free(Id); |
| 579 | isl_ast_expr_free(Expr); |
| 580 | |
| 581 | return V; |
| 582 | } |
| 583 | |
| 584 | IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) { |
| 585 | // XXX: We assume i64 is large enough. This is often true, but in general |
| 586 | // incorrect. Also, on 32bit architectures, it would be beneficial to |
| 587 | // use a smaller type. We can and should directly derive this information |
| 588 | // during code generation. |
| 589 | return IntegerType::get(Builder.getContext(), 64); |
| 590 | } |
| 591 | |
| 592 | Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) { |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 593 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int && |
| 594 | "Expression not of type isl_ast_expr_int"); |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 595 | isl_val *Val; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 596 | Value *V; |
| 597 | APInt APValue; |
| 598 | IntegerType *T; |
| 599 | |
Tobias Grosser | edab135 | 2013-06-21 06:41:31 +0000 | [diff] [blame] | 600 | Val = isl_ast_expr_get_val(Expr); |
| 601 | APValue = APIntFromVal(Val); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 602 | T = getType(Expr); |
| 603 | APValue = APValue.sextOrSelf(T->getBitWidth()); |
| 604 | V = ConstantInt::get(T, APValue); |
| 605 | |
| 606 | isl_ast_expr_free(Expr); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 607 | return V; |
| 608 | } |
| 609 | |
| 610 | Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) { |
| 611 | switch (isl_ast_expr_get_type(Expr)) { |
| 612 | case isl_ast_expr_error: |
| 613 | llvm_unreachable("Code generation error"); |
| 614 | case isl_ast_expr_op: |
| 615 | return createOp(Expr); |
| 616 | case isl_ast_expr_id: |
| 617 | return createId(Expr); |
| 618 | case isl_ast_expr_int: |
| 619 | return createInt(Expr); |
| 620 | } |
| 621 | |
| 622 | llvm_unreachable("Unexpected enum value"); |
| 623 | } |
| 624 | |
| 625 | class IslNodeBuilder { |
| 626 | public: |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 627 | IslNodeBuilder(PollyIRBuilder &Builder, LoopAnnotator &Annotator, Pass *P) |
Tobias Grosser | 34c8787 | 2014-04-22 16:39:41 +0000 | [diff] [blame] | 628 | : Builder(Builder), Annotator(Annotator), ExprBuilder(Builder, IDToValue), |
| 629 | P(P) {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 630 | |
| 631 | void addParameters(__isl_take isl_set *Context); |
| 632 | void create(__isl_take isl_ast_node *Node); |
Tobias Grosser | 54ee0ba | 2013-11-17 03:18:25 +0000 | [diff] [blame] | 633 | IslExprBuilder &getExprBuilder() { return ExprBuilder; } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 634 | |
| 635 | private: |
Tobias Grosser | 5103ba7 | 2014-03-04 14:58:49 +0000 | [diff] [blame] | 636 | PollyIRBuilder &Builder; |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 637 | LoopAnnotator &Annotator; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 638 | IslExprBuilder ExprBuilder; |
| 639 | Pass *P; |
| 640 | |
| 641 | // This maps an isl_id* to the Value* it has in the generated program. For now |
| 642 | // on, the only isl_ids that are stored here are the newly calculated loop |
| 643 | // ivs. |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 644 | std::map<isl_id *, Value *> IDToValue; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 645 | |
| 646 | // Extract the upper bound of this loop |
| 647 | // |
| 648 | // The isl code generation can generate arbitrary expressions to check if the |
| 649 | // upper bound of a loop is reached, but it provides an option to enforce |
| 650 | // 'atomic' upper bounds. An 'atomic upper bound is always of the form |
| 651 | // iv <= expr, where expr is an (arbitrary) expression not containing iv. |
| 652 | // |
| 653 | // This function extracts 'atomic' upper bounds. Polly, in general, requires |
| 654 | // atomic upper bounds for the following reasons: |
| 655 | // |
| 656 | // 1. An atomic upper bound is loop invariant |
| 657 | // |
| 658 | // It must not be calculated at each loop iteration and can often even be |
| 659 | // hoisted out further by the loop invariant code motion. |
| 660 | // |
| 661 | // 2. OpenMP needs a loop invarient upper bound to calculate the number |
| 662 | // of loop iterations. |
| 663 | // |
| 664 | // 3. With the existing code, upper bounds have been easier to implement. |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 665 | __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For, |
| 666 | CmpInst::Predicate &Predicate); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 667 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 668 | unsigned getNumberOfIterations(__isl_keep isl_ast_node *For); |
| 669 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 670 | void createFor(__isl_take isl_ast_node *For); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 671 | void createForVector(__isl_take isl_ast_node *For, int VectorWidth); |
| 672 | void createForSequential(__isl_take isl_ast_node *For); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 673 | |
| 674 | /// Generate LLVM-IR that computes the values of the original induction |
| 675 | /// variables in function of the newly generated loop induction variables. |
| 676 | /// |
| 677 | /// Example: |
| 678 | /// |
| 679 | /// // Original |
| 680 | /// for i |
| 681 | /// for j |
| 682 | /// S(i) |
| 683 | /// |
| 684 | /// Schedule: [i,j] -> [i+j, j] |
| 685 | /// |
| 686 | /// // New |
| 687 | /// for c0 |
| 688 | /// for c1 |
| 689 | /// S(c0 - c1, c1) |
| 690 | /// |
| 691 | /// Assuming the original code consists of two loops which are |
| 692 | /// transformed according to a schedule [i,j] -> [c0=i+j,c1=j]. The resulting |
| 693 | /// ast models the original statement as a call expression where each argument |
| 694 | /// is an expression that computes the old induction variables from the new |
| 695 | /// ones, ordered such that the first argument computes the value of induction |
| 696 | /// variable that was outermost in the original code. |
| 697 | /// |
| 698 | /// @param Expr The call expression that represents the statement. |
| 699 | /// @param Stmt The statement that is called. |
| 700 | /// @param VMap The value map into which the mapping from the old induction |
| 701 | /// variable to the new one is inserted. This mapping is used |
| 702 | /// for the classical code generation (not scev-based) and |
| 703 | /// gives an explicit mapping from an original, materialized |
| 704 | /// induction variable. It consequently can only be expressed |
| 705 | /// if there was an explicit induction variable. |
| 706 | /// @param LTS The loop to SCEV map in which the mapping from the original |
| 707 | /// loop to a SCEV representing the new loop iv is added. This |
| 708 | /// mapping does not require an explicit induction variable. |
| 709 | /// Instead, we think in terms of an implicit induction variable |
| 710 | /// that counts the number of times a loop is executed. For each |
| 711 | /// original loop this count, expressed in function of the new |
| 712 | /// induction variables, is added to the LTS map. |
| 713 | void createSubstitutions(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 714 | ValueMapT &VMap, LoopToScevMapT <S); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 715 | void createSubstitutionsVector(__isl_take isl_ast_expr *Expr, ScopStmt *Stmt, |
| 716 | VectorValueMapT &VMap, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 717 | std::vector<LoopToScevMapT> &VLTS, |
| 718 | std::vector<Value *> &IVS, |
| 719 | __isl_take isl_id *IteratorID); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 720 | void createIf(__isl_take isl_ast_node *If); |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 721 | void createUserVector(__isl_take isl_ast_node *User, |
| 722 | std::vector<Value *> &IVS, |
| 723 | __isl_take isl_id *IteratorID, |
| 724 | __isl_take isl_union_map *Schedule); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 725 | void createUser(__isl_take isl_ast_node *User); |
| 726 | void createBlock(__isl_take isl_ast_node *Block); |
| 727 | }; |
| 728 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 729 | __isl_give isl_ast_expr * |
| 730 | IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For, |
| 731 | ICmpInst::Predicate &Predicate) { |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 732 | isl_id *UBID, *IteratorID; |
| 733 | isl_ast_expr *Cond, *Iterator, *UB, *Arg0; |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 734 | isl_ast_op_type Type; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 735 | |
| 736 | Cond = isl_ast_node_for_get_cond(For); |
| 737 | Iterator = isl_ast_node_for_get_iterator(For); |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 738 | Type = isl_ast_expr_get_op_type(Cond); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 739 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 740 | assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op && |
| 741 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 742 | |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 743 | switch (Type) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 744 | case isl_ast_op_le: |
| 745 | Predicate = ICmpInst::ICMP_SLE; |
| 746 | break; |
| 747 | case isl_ast_op_lt: |
| 748 | Predicate = ICmpInst::ICMP_SLT; |
| 749 | break; |
| 750 | default: |
| 751 | llvm_unreachable("Unexpected comparision type in loop conditon"); |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 752 | } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 753 | |
| 754 | Arg0 = isl_ast_expr_get_op_arg(Cond, 0); |
| 755 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 756 | assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id && |
| 757 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 758 | |
| 759 | UBID = isl_ast_expr_get_id(Arg0); |
| 760 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 761 | assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id && |
| 762 | "Could not get the iterator"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 763 | |
| 764 | IteratorID = isl_ast_expr_get_id(Iterator); |
| 765 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 766 | assert(UBID == IteratorID && |
| 767 | "conditional expression is not an atomic upper bound"); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 768 | |
| 769 | UB = isl_ast_expr_get_op_arg(Cond, 1); |
| 770 | |
| 771 | isl_ast_expr_free(Cond); |
| 772 | isl_ast_expr_free(Iterator); |
| 773 | isl_ast_expr_free(Arg0); |
| 774 | isl_id_free(IteratorID); |
| 775 | isl_id_free(UBID); |
| 776 | |
| 777 | return UB; |
| 778 | } |
| 779 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 780 | unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) { |
| 781 | isl_id *Annotation = isl_ast_node_get_annotation(For); |
| 782 | if (!Annotation) |
| 783 | return -1; |
| 784 | |
Johannes Doerfert | 6e81905 | 2014-07-17 16:11:28 +0000 | [diff] [blame] | 785 | struct IslAstUserPayload *Info = |
| 786 | (struct IslAstUserPayload *)isl_id_get_user(Annotation); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 787 | if (!Info) { |
| 788 | isl_id_free(Annotation); |
| 789 | return -1; |
| 790 | } |
| 791 | |
| 792 | isl_union_map *Schedule = isl_ast_build_get_schedule(Info->Context); |
| 793 | isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule)); |
| 794 | isl_id_free(Annotation); |
Sebastian Pop | 2aa5c24 | 2012-12-18 08:56:51 +0000 | [diff] [blame] | 795 | int NumberOfIterations = polly::getNumberOfIterations(LoopDomain); |
| 796 | if (NumberOfIterations == -1) |
| 797 | return -1; |
| 798 | return NumberOfIterations + 1; |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 801 | void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User, |
| 802 | std::vector<Value *> &IVS, |
| 803 | __isl_take isl_id *IteratorID, |
| 804 | __isl_take isl_union_map *Schedule) { |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 805 | isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); |
| 806 | isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); |
| 807 | isl_id *Id = isl_ast_expr_get_id(StmtExpr); |
| 808 | isl_ast_expr_free(StmtExpr); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 809 | ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 810 | VectorValueMapT VectorMap(IVS.size()); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 811 | std::vector<LoopToScevMapT> VLTS(IVS.size()); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 812 | |
| 813 | isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain()); |
| 814 | Schedule = isl_union_map_intersect_domain(Schedule, Domain); |
| 815 | isl_map *S = isl_map_from_union_map(Schedule); |
| 816 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 817 | createSubstitutionsVector(Expr, Stmt, VectorMap, VLTS, IVS, IteratorID); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 818 | VectorBlockGenerator::generate(Builder, *Stmt, VectorMap, VLTS, S, P); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 819 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 820 | isl_map_free(S); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 821 | isl_id_free(Id); |
| 822 | isl_ast_node_free(User); |
| 823 | } |
| 824 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 825 | void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For, |
| 826 | int VectorWidth) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 827 | isl_ast_node *Body = isl_ast_node_for_get_body(For); |
| 828 | isl_ast_expr *Init = isl_ast_node_for_get_init(For); |
| 829 | isl_ast_expr *Inc = isl_ast_node_for_get_inc(For); |
| 830 | isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For); |
| 831 | isl_id *IteratorID = isl_ast_expr_get_id(Iterator); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 832 | |
| 833 | Value *ValueLB = ExprBuilder.create(Init); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 834 | Value *ValueInc = ExprBuilder.create(Inc); |
| 835 | |
| 836 | Type *MaxType = ExprBuilder.getType(Iterator); |
| 837 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 838 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
| 839 | |
| 840 | if (MaxType != ValueLB->getType()) |
| 841 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 842 | if (MaxType != ValueInc->getType()) |
| 843 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
| 844 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 845 | std::vector<Value *> IVS(VectorWidth); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 846 | IVS[0] = ValueLB; |
| 847 | |
| 848 | for (int i = 1; i < VectorWidth; i++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 849 | IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv"); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 850 | |
| 851 | isl_id *Annotation = isl_ast_node_get_annotation(For); |
| 852 | assert(Annotation && "For statement is not annotated"); |
| 853 | |
Johannes Doerfert | 6e81905 | 2014-07-17 16:11:28 +0000 | [diff] [blame] | 854 | struct IslAstUserPayload *Info = |
| 855 | (struct IslAstUserPayload *)isl_id_get_user(Annotation); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 856 | assert(Info && "For statement annotation does not contain info"); |
| 857 | |
| 858 | isl_union_map *Schedule = isl_ast_build_get_schedule(Info->Context); |
| 859 | assert(Schedule && "For statement annotation does not contain its schedule"); |
| 860 | |
| 861 | IDToValue[IteratorID] = ValueLB; |
| 862 | |
| 863 | switch (isl_ast_node_get_type(Body)) { |
| 864 | case isl_ast_node_user: |
| 865 | createUserVector(Body, IVS, isl_id_copy(IteratorID), |
| 866 | isl_union_map_copy(Schedule)); |
| 867 | break; |
| 868 | case isl_ast_node_block: { |
| 869 | isl_ast_node_list *List = isl_ast_node_block_get_children(Body); |
| 870 | |
| 871 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
| 872 | createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS, |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 873 | isl_id_copy(IteratorID), isl_union_map_copy(Schedule)); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 874 | |
| 875 | isl_ast_node_free(Body); |
| 876 | isl_ast_node_list_free(List); |
| 877 | break; |
| 878 | } |
| 879 | default: |
| 880 | isl_ast_node_dump(Body); |
| 881 | llvm_unreachable("Unhandled isl_ast_node in vectorizer"); |
| 882 | } |
| 883 | |
| 884 | IDToValue.erase(IteratorID); |
| 885 | isl_id_free(IteratorID); |
| 886 | isl_id_free(Annotation); |
| 887 | isl_union_map_free(Schedule); |
| 888 | |
| 889 | isl_ast_node_free(For); |
| 890 | isl_ast_expr_free(Iterator); |
| 891 | } |
| 892 | |
| 893 | void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) { |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 894 | isl_ast_node *Body; |
| 895 | isl_ast_expr *Init, *Inc, *Iterator, *UB; |
| 896 | isl_id *IteratorID; |
| 897 | Value *ValueLB, *ValueUB, *ValueInc; |
| 898 | Type *MaxType; |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 899 | BasicBlock *ExitBlock; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 900 | Value *IV; |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 901 | CmpInst::Predicate Predicate; |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 902 | bool Parallel; |
| 903 | |
Johannes Doerfert | 6e81905 | 2014-07-17 16:11:28 +0000 | [diff] [blame] | 904 | Parallel = IslAstInfo::isInnermostParallel(For); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 905 | |
| 906 | Body = isl_ast_node_for_get_body(For); |
| 907 | |
| 908 | // isl_ast_node_for_is_degenerate(For) |
| 909 | // |
| 910 | // TODO: For degenerated loops we could generate a plain assignment. |
| 911 | // However, for now we just reuse the logic for normal loops, which will |
| 912 | // create a loop with a single iteration. |
| 913 | |
| 914 | Init = isl_ast_node_for_get_init(For); |
| 915 | Inc = isl_ast_node_for_get_inc(For); |
| 916 | Iterator = isl_ast_node_for_get_iterator(For); |
| 917 | IteratorID = isl_ast_expr_get_id(Iterator); |
Tobias Grosser | c967d8e | 2012-10-16 07:29:13 +0000 | [diff] [blame] | 918 | UB = getUpperBound(For, Predicate); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 919 | |
| 920 | ValueLB = ExprBuilder.create(Init); |
| 921 | ValueUB = ExprBuilder.create(UB); |
| 922 | ValueInc = ExprBuilder.create(Inc); |
| 923 | |
| 924 | MaxType = ExprBuilder.getType(Iterator); |
| 925 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
| 926 | MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); |
| 927 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
| 928 | |
| 929 | if (MaxType != ValueLB->getType()) |
| 930 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
| 931 | if (MaxType != ValueUB->getType()) |
| 932 | ValueUB = Builder.CreateSExt(ValueUB, MaxType); |
| 933 | if (MaxType != ValueInc->getType()) |
| 934 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
| 935 | |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 936 | IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, ExitBlock, Predicate, |
| 937 | &Annotator, Parallel); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 938 | IDToValue[IteratorID] = IV; |
| 939 | |
| 940 | create(Body); |
| 941 | |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 942 | Annotator.End(); |
| 943 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 944 | IDToValue.erase(IteratorID); |
| 945 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 946 | Builder.SetInsertPoint(ExitBlock->begin()); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 947 | |
| 948 | isl_ast_node_free(For); |
| 949 | isl_ast_expr_free(Iterator); |
| 950 | isl_id_free(IteratorID); |
| 951 | } |
| 952 | |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 953 | void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) { |
| 954 | bool Vector = PollyVectorizerChoice != VECTORIZER_NONE; |
| 955 | |
Johannes Doerfert | 6e81905 | 2014-07-17 16:11:28 +0000 | [diff] [blame] | 956 | if (Vector && IslAstInfo::isInnermostParallel(For)) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 957 | int VectorWidth = getNumberOfIterations(For); |
| 958 | if (1 < VectorWidth && VectorWidth <= 16) { |
| 959 | createForVector(For, VectorWidth); |
| 960 | return; |
| 961 | } |
| 962 | } |
| 963 | createForSequential(For); |
| 964 | } |
| 965 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 966 | void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) { |
| 967 | isl_ast_expr *Cond = isl_ast_node_if_get_cond(If); |
| 968 | |
| 969 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 970 | LLVMContext &Context = F->getContext(); |
| 971 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 972 | BasicBlock *CondBB = |
| 973 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 974 | CondBB->setName("polly.cond"); |
| 975 | BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P); |
| 976 | MergeBB->setName("polly.merge"); |
| 977 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
| 978 | BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F); |
| 979 | |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 980 | DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 981 | DT.addNewBlock(ThenBB, CondBB); |
| 982 | DT.addNewBlock(ElseBB, CondBB); |
| 983 | DT.changeImmediateDominator(MergeBB, CondBB); |
| 984 | |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 985 | LoopInfo &LI = P->getAnalysis<LoopInfo>(); |
| 986 | Loop *L = LI.getLoopFor(CondBB); |
| 987 | if (L) { |
| 988 | L->addBasicBlockToLoop(ThenBB, LI.getBase()); |
| 989 | L->addBasicBlockToLoop(ElseBB, LI.getBase()); |
| 990 | } |
| 991 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 992 | CondBB->getTerminator()->eraseFromParent(); |
| 993 | |
| 994 | Builder.SetInsertPoint(CondBB); |
| 995 | Value *Predicate = ExprBuilder.create(Cond); |
| 996 | Builder.CreateCondBr(Predicate, ThenBB, ElseBB); |
| 997 | Builder.SetInsertPoint(ThenBB); |
| 998 | Builder.CreateBr(MergeBB); |
| 999 | Builder.SetInsertPoint(ElseBB); |
| 1000 | Builder.CreateBr(MergeBB); |
| 1001 | Builder.SetInsertPoint(ThenBB->begin()); |
| 1002 | |
| 1003 | create(isl_ast_node_if_get_then(If)); |
| 1004 | |
| 1005 | Builder.SetInsertPoint(ElseBB->begin()); |
| 1006 | |
| 1007 | if (isl_ast_node_if_has_else(If)) |
| 1008 | create(isl_ast_node_if_get_else(If)); |
| 1009 | |
| 1010 | Builder.SetInsertPoint(MergeBB->begin()); |
| 1011 | |
| 1012 | isl_ast_node_free(If); |
| 1013 | } |
| 1014 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1015 | void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt, |
| 1016 | ValueMapT &VMap, LoopToScevMapT <S) { |
| 1017 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 1018 | "Expression of type 'op' expected"); |
| 1019 | assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call && |
| 1020 | "Opertation of type 'call' expected"); |
| 1021 | for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) { |
| 1022 | isl_ast_expr *SubExpr; |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1023 | Value *V; |
| 1024 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1025 | SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1); |
| 1026 | V = ExprBuilder.create(SubExpr); |
Sebastian Pop | e039bb1 | 2013-03-18 19:09:49 +0000 | [diff] [blame] | 1027 | ScalarEvolution *SE = Stmt->getParent()->getSE(); |
| 1028 | LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V); |
| 1029 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1030 | // CreateIntCast can introduce trunc expressions. This is correct, as the |
| 1031 | // result will always fit into the type of the original induction variable |
| 1032 | // (because we calculate a value of the original induction variable). |
Sebastian Pop | e039bb1 | 2013-03-18 19:09:49 +0000 | [diff] [blame] | 1033 | const Value *OldIV = Stmt->getInductionVariableForDimension(i); |
| 1034 | if (OldIV) { |
| 1035 | V = Builder.CreateIntCast(V, OldIV->getType(), true); |
| 1036 | VMap[OldIV] = V; |
| 1037 | } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1040 | isl_ast_expr_free(Expr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 1043 | void IslNodeBuilder::createSubstitutionsVector( |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1044 | __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, VectorValueMapT &VMap, |
| 1045 | std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS, |
| 1046 | __isl_take isl_id *IteratorID) { |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 1047 | int i = 0; |
| 1048 | |
| 1049 | Value *OldValue = IDToValue[IteratorID]; |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 1050 | for (Value *IV : IVS) { |
| 1051 | IDToValue[IteratorID] = IV; |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1052 | createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VMap[i], VLTS[i]); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 1053 | i++; |
| 1054 | } |
| 1055 | |
| 1056 | IDToValue[IteratorID] = OldValue; |
| 1057 | isl_id_free(IteratorID); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1058 | isl_ast_expr_free(Expr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) { |
| 1062 | ValueMapT VMap; |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 1063 | LoopToScevMapT LTS; |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1064 | isl_id *Id; |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 1065 | ScopStmt *Stmt; |
| 1066 | |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1067 | isl_ast_expr *Expr = isl_ast_node_user_get_expr(User); |
| 1068 | isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0); |
| 1069 | Id = isl_ast_expr_get_id(StmtExpr); |
| 1070 | isl_ast_expr_free(StmtExpr); |
Sebastian Pop | 04c4ce3 | 2012-12-18 07:46:13 +0000 | [diff] [blame] | 1071 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 1072 | Stmt = (ScopStmt *)isl_id_get_user(Id); |
Tobias Grosser | ce67a04 | 2014-07-02 16:26:47 +0000 | [diff] [blame] | 1073 | createSubstitutions(Expr, Stmt, VMap, LTS); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 1074 | BlockGenerator::generate(Builder, *Stmt, VMap, LTS, P); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1075 | |
| 1076 | isl_ast_node_free(User); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1077 | isl_id_free(Id); |
| 1078 | } |
| 1079 | |
| 1080 | void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) { |
| 1081 | isl_ast_node_list *List = isl_ast_node_block_get_children(Block); |
| 1082 | |
| 1083 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
| 1084 | create(isl_ast_node_list_get_ast_node(List, i)); |
| 1085 | |
| 1086 | isl_ast_node_free(Block); |
| 1087 | isl_ast_node_list_free(List); |
| 1088 | } |
| 1089 | |
| 1090 | void IslNodeBuilder::create(__isl_take isl_ast_node *Node) { |
| 1091 | switch (isl_ast_node_get_type(Node)) { |
| 1092 | case isl_ast_node_error: |
| 1093 | llvm_unreachable("code generation error"); |
| 1094 | case isl_ast_node_for: |
| 1095 | createFor(Node); |
| 1096 | return; |
| 1097 | case isl_ast_node_if: |
| 1098 | createIf(Node); |
| 1099 | return; |
| 1100 | case isl_ast_node_user: |
| 1101 | createUser(Node); |
| 1102 | return; |
| 1103 | case isl_ast_node_block: |
| 1104 | createBlock(Node); |
| 1105 | return; |
| 1106 | } |
| 1107 | |
| 1108 | llvm_unreachable("Unknown isl_ast_node type"); |
| 1109 | } |
| 1110 | |
| 1111 | void IslNodeBuilder::addParameters(__isl_take isl_set *Context) { |
| 1112 | SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly"); |
| 1113 | |
| 1114 | for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) { |
| 1115 | isl_id *Id; |
| 1116 | const SCEV *Scev; |
| 1117 | IntegerType *T; |
| 1118 | Instruction *InsertLocation; |
| 1119 | |
| 1120 | Id = isl_set_get_dim_id(Context, isl_dim_param, i); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 1121 | Scev = (const SCEV *)isl_id_get_user(Id); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1122 | T = dyn_cast<IntegerType>(Scev->getType()); |
| 1123 | InsertLocation = --(Builder.GetInsertBlock()->end()); |
| 1124 | Value *V = Rewriter.expandCodeFor(Scev, T, InsertLocation); |
| 1125 | IDToValue[Id] = V; |
| 1126 | |
| 1127 | isl_id_free(Id); |
| 1128 | } |
| 1129 | |
| 1130 | isl_set_free(Context); |
| 1131 | } |
| 1132 | |
| 1133 | namespace { |
| 1134 | class IslCodeGeneration : public ScopPass { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 1135 | public: |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1136 | static char ID; |
| 1137 | |
| 1138 | IslCodeGeneration() : ScopPass(ID) {} |
| 1139 | |
| 1140 | bool runOnScop(Scop &S) { |
| 1141 | IslAstInfo &AstInfo = getAnalysis<IslAstInfo>(); |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 1142 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 1143 | assert(!S.getRegion().isTopLevelRegion() && |
| 1144 | "Top level regions are not supported"); |
Tobias Grosser | 0ee50f6 | 2013-04-10 06:55:31 +0000 | [diff] [blame] | 1145 | |
Tobias Grosser | 8edce4e | 2013-04-16 08:04:42 +0000 | [diff] [blame] | 1146 | simplifyRegion(&S, this); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1147 | |
| 1148 | BasicBlock *StartBlock = executeScopConditionally(S, this); |
| 1149 | isl_ast_node *Ast = AstInfo.getAst(); |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 1150 | LoopAnnotator Annotator; |
| 1151 | PollyIRBuilder Builder(StartBlock->getContext(), llvm::ConstantFolder(), |
| 1152 | polly::IRInserter(Annotator)); |
| 1153 | Builder.SetInsertPoint(StartBlock->begin()); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1154 | |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 1155 | IslNodeBuilder NodeBuilder(Builder, Annotator, this); |
Tobias Grosser | 54ee0ba | 2013-11-17 03:18:25 +0000 | [diff] [blame] | 1156 | |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1157 | Builder.SetInsertPoint(StartBlock->getSinglePredecessor()->begin()); |
| 1158 | NodeBuilder.addParameters(S.getContext()); |
Tobias Grosser | 54ee0ba | 2013-11-17 03:18:25 +0000 | [diff] [blame] | 1159 | // Build condition that evaluates at run-time if all assumptions taken |
| 1160 | // for the scop hold. If we detect some assumptions do not hold, the |
| 1161 | // original code is executed. |
| 1162 | Value *V = NodeBuilder.getExprBuilder().create(AstInfo.getRunCondition()); |
| 1163 | Value *Zero = ConstantInt::get(V->getType(), 0); |
| 1164 | V = Builder.CreateICmp(CmpInst::ICMP_NE, Zero, V); |
| 1165 | BasicBlock *PrevBB = StartBlock->getUniquePredecessor(); |
| 1166 | BranchInst *Branch = dyn_cast<BranchInst>(PrevBB->getTerminator()); |
| 1167 | Branch->setCondition(V); |
Tobias Grosser | 5e6813d | 2014-07-02 17:47:48 +0000 | [diff] [blame] | 1168 | Builder.SetInsertPoint(StartBlock->begin()); |
Tobias Grosser | 54ee0ba | 2013-11-17 03:18:25 +0000 | [diff] [blame] | 1169 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1170 | NodeBuilder.create(Ast); |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 1174 | virtual void printScop(raw_ostream &OS) const {} |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1175 | |
| 1176 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 1177 | AU.addRequired<DominatorTreeWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1178 | AU.addRequired<IslAstInfo>(); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame^] | 1179 | AU.addRequired<RegionInfoPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1180 | AU.addRequired<ScalarEvolution>(); |
| 1181 | AU.addRequired<ScopDetection>(); |
| 1182 | AU.addRequired<ScopInfo>(); |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 1183 | AU.addRequired<LoopInfo>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1184 | |
| 1185 | AU.addPreserved<Dependences>(); |
| 1186 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1187 | AU.addPreserved<LoopInfo>(); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 1188 | AU.addPreserved<DominatorTreeWrapperPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1189 | AU.addPreserved<IslAstInfo>(); |
| 1190 | AU.addPreserved<ScopDetection>(); |
| 1191 | AU.addPreserved<ScalarEvolution>(); |
| 1192 | |
| 1193 | // FIXME: We do not yet add regions for the newly generated code to the |
| 1194 | // region tree. |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame^] | 1195 | AU.addPreserved<RegionInfoPass>(); |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1196 | AU.addPreserved<TempScopInfo>(); |
| 1197 | AU.addPreserved<ScopInfo>(); |
| 1198 | AU.addPreservedID(IndependentBlocksID); |
| 1199 | } |
| 1200 | }; |
| 1201 | } |
| 1202 | |
| 1203 | char IslCodeGeneration::ID = 1; |
| 1204 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 1205 | Pass *polly::createIslCodeGenerationPass() { return new IslCodeGeneration(); } |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 1206 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 1207 | INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl", |
| 1208 | "Polly - Create LLVM-IR from SCoPs", false, false); |
| 1209 | INITIALIZE_PASS_DEPENDENCY(Dependences); |
Tobias Grosser | 42aff30 | 2014-01-13 22:29:56 +0000 | [diff] [blame] | 1210 | INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 1211 | INITIALIZE_PASS_DEPENDENCY(LoopInfo); |
Matt Arsenault | 8ca3681 | 2014-07-19 18:40:17 +0000 | [diff] [blame^] | 1212 | INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 1213 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution); |
| 1214 | INITIALIZE_PASS_DEPENDENCY(ScopDetection); |
| 1215 | INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl", |
| 1216 | "Polly - Create LLVM-IR from SCoPs", false, false) |