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