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" |
Sebastian Pop | 082cea8 | 2012-05-07 16:20:07 +0000 | [diff] [blame] | 22 | |
Tobias Grosser | 8a5bc6e | 2012-10-02 19:50:43 +0000 | [diff] [blame] | 23 | #ifdef ISL_CODEGEN_FOUND |
| 24 | |
| 25 | #include "polly/Dependences.h" |
| 26 | #include "polly/LinkAllPasses.h" |
| 27 | #include "polly/ScopInfo.h" |
| 28 | #include "polly/TempScopInfo.h" |
| 29 | #include "polly/CodeGen/IslAst.h" |
| 30 | #include "polly/CodeGen/BlockGenerators.h" |
| 31 | #include "polly/CodeGen/LoopGenerators.h" |
| 32 | #include "polly/CodeGen/Utils.h" |
| 33 | #include "polly/Support/GICHelper.h" |
| 34 | |
| 35 | #include "llvm/Module.h" |
| 36 | #include "llvm/Analysis/LoopInfo.h" |
| 37 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
| 38 | #define DEBUG_TYPE "polly-codegen-isl" |
| 39 | #include "llvm/Support/CommandLine.h" |
| 40 | #include "llvm/Support/Debug.h" |
| 41 | #include "llvm/Target/TargetData.h" |
| 42 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 43 | |
| 44 | #include "isl/union_map.h" |
| 45 | #include "isl/list.h" |
| 46 | #include "isl/ast.h" |
| 47 | #include "isl/ast_build.h" |
| 48 | #include "isl/set.h" |
| 49 | #include "isl/map.h" |
| 50 | #include "isl/aff.h" |
| 51 | |
| 52 | #include <map> |
| 53 | |
| 54 | using namespace polly; |
| 55 | using namespace llvm; |
| 56 | |
| 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: |
| 63 | RuntimeDebugBuilder(IRBuilder<> &Builder) : Builder(Builder) {} |
| 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: |
| 76 | IRBuilder<> &Builder; |
| 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; |
| 97 | FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(), |
| 98 | Builder.getInt8PtrTy(), true); |
| 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; |
| 112 | FunctionType *Ty = FunctionType::get(Builder.getInt32Ty(), |
| 113 | Builder.getInt8PtrTy(), false); |
| 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()); |
| 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 | |
| 139 | /// @brief Calculate the Value of a certain isl_ast_expr |
| 140 | class IslExprBuilder { |
| 141 | public: |
| 142 | IslExprBuilder(IRBuilder<> &Builder, |
| 143 | std::map<isl_id *, Value*> &IDToValue, Pass *P) |
| 144 | : Builder(Builder), IDToValue(IDToValue) { } |
| 145 | |
| 146 | Value *create(__isl_take isl_ast_expr *Expr); |
| 147 | Type *getWidestType(Type *T1, Type *T2); |
| 148 | IntegerType *getType(__isl_keep isl_ast_expr *Expr); |
| 149 | |
| 150 | private: |
| 151 | IRBuilder<> &Builder; |
| 152 | std::map<isl_id *, Value*> &IDToValue; |
| 153 | |
| 154 | Value *createOp(__isl_take isl_ast_expr *Expr); |
| 155 | Value *createOpUnary(__isl_take isl_ast_expr *Expr); |
| 156 | Value *createOpBin(__isl_take isl_ast_expr *Expr); |
| 157 | Value *createOpNAry(__isl_take isl_ast_expr *Expr); |
| 158 | Value *createOpSelect(__isl_take isl_ast_expr *Expr); |
| 159 | Value *createOpICmp(__isl_take isl_ast_expr *Expr); |
| 160 | Value *createOpBoolean(__isl_take isl_ast_expr *Expr); |
| 161 | Value *createId(__isl_take isl_ast_expr *Expr); |
| 162 | Value *createInt(__isl_take isl_ast_expr *Expr); |
| 163 | }; |
| 164 | |
| 165 | Type *IslExprBuilder::getWidestType(Type *T1, Type *T2) { |
| 166 | assert(isa<IntegerType>(T1) && isa<IntegerType>(T2)); |
| 167 | |
| 168 | if (T1->getPrimitiveSizeInBits() < T2->getPrimitiveSizeInBits()) |
| 169 | return T2; |
| 170 | else |
| 171 | return T1; |
| 172 | } |
| 173 | |
| 174 | Value *IslExprBuilder::createOpUnary(__isl_take isl_ast_expr *Expr) { |
| 175 | assert (isl_ast_expr_get_op_type(Expr) == isl_ast_op_minus |
| 176 | && "Unsupported unary operation"); |
| 177 | |
| 178 | Value *V; |
| 179 | Type *MaxType = getType(Expr); |
| 180 | |
| 181 | V = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 182 | MaxType = getWidestType(MaxType, V->getType()); |
| 183 | |
| 184 | if (MaxType != V->getType()) |
| 185 | V = Builder.CreateSExt(V, MaxType); |
| 186 | |
| 187 | isl_ast_expr_free(Expr); |
| 188 | return Builder.CreateNSWNeg(V); |
| 189 | } |
| 190 | |
| 191 | Value *IslExprBuilder::createOpNAry(__isl_take isl_ast_expr *Expr) { |
| 192 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op |
| 193 | && "isl ast expression not of type isl_ast_op"); |
| 194 | assert(isl_ast_expr_get_op_n_arg(Expr) >= 2 |
| 195 | && "We need at least two operands in an n-ary operation"); |
| 196 | |
| 197 | Value *V; |
| 198 | |
| 199 | V = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 200 | |
| 201 | for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr); ++i) { |
| 202 | Value *OpV; |
| 203 | OpV = create(isl_ast_expr_get_op_arg(Expr, i)); |
| 204 | |
| 205 | Type *Ty = getWidestType(V->getType(), OpV->getType()); |
| 206 | |
| 207 | if (Ty != OpV->getType()) |
| 208 | OpV = Builder.CreateSExt(OpV, Ty); |
| 209 | |
| 210 | if (Ty != V->getType()) |
| 211 | V = Builder.CreateSExt(V, Ty); |
| 212 | |
| 213 | switch (isl_ast_expr_get_op_type(Expr)) { |
| 214 | default: |
| 215 | llvm_unreachable("This is no n-ary isl ast expression"); |
| 216 | |
| 217 | case isl_ast_op_max: |
| 218 | { |
| 219 | Value *Cmp = Builder.CreateICmpSGT(V, OpV); |
| 220 | V = Builder.CreateSelect(Cmp, V, OpV); |
| 221 | continue; |
| 222 | } |
| 223 | case isl_ast_op_min: |
| 224 | { |
| 225 | Value *Cmp = Builder.CreateICmpSLT(V, OpV); |
| 226 | V = Builder.CreateSelect(Cmp, V, OpV); |
| 227 | continue; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | // TODO: We can truncate the result, if it fits into a smaller type. This can |
| 233 | // help in cases where we have larger operands (e.g. i67) but the result is |
| 234 | // known to fit into i64. Without the truncation, the larger i67 type may |
| 235 | // force all subsequent operations to be performed on a non-native type. |
| 236 | isl_ast_expr_free(Expr); |
| 237 | return V; |
| 238 | } |
| 239 | |
| 240 | Value *IslExprBuilder::createOpBin(__isl_take isl_ast_expr *Expr) { |
| 241 | Value *LHS, *RHS, *Res; |
| 242 | Type *MaxType; |
| 243 | isl_ast_op_type OpType; |
| 244 | |
| 245 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op |
| 246 | && "isl ast expression not of type isl_ast_op"); |
| 247 | assert(isl_ast_expr_get_op_n_arg(Expr) == 2 |
| 248 | && "not a binary isl ast expression"); |
| 249 | |
| 250 | OpType = isl_ast_expr_get_op_type(Expr); |
| 251 | |
| 252 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 253 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 254 | |
| 255 | MaxType = LHS->getType(); |
| 256 | MaxType = getWidestType(MaxType, RHS->getType()); |
| 257 | |
| 258 | // Take the result into account when calculating the widest type. |
| 259 | // |
| 260 | // For operations such as '+' the result may require a type larger than |
| 261 | // the type of the individual operands. For other operations such as '/', the |
| 262 | // result type cannot be larger than the type of the individual operand. isl |
| 263 | // does not calculate correct types for these operations and we consequently |
| 264 | // exclude those operations here. |
| 265 | switch(OpType) { |
| 266 | case isl_ast_op_pdiv_q: |
| 267 | case isl_ast_op_pdiv_r: |
| 268 | case isl_ast_op_div: |
| 269 | case isl_ast_op_fdiv_q: |
| 270 | // Do nothing |
| 271 | break; |
| 272 | case isl_ast_op_add: |
| 273 | case isl_ast_op_sub: |
| 274 | case isl_ast_op_mul: |
| 275 | MaxType = getWidestType(MaxType, getType(Expr)); |
| 276 | break; |
| 277 | default: |
| 278 | llvm_unreachable("This is no binary isl ast expression"); |
| 279 | } |
| 280 | |
| 281 | if (MaxType != RHS->getType()) |
| 282 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 283 | |
| 284 | if (MaxType != LHS->getType()) |
| 285 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 286 | |
| 287 | switch (OpType) { |
| 288 | default: |
| 289 | llvm_unreachable("This is no binary isl ast expression"); |
| 290 | case isl_ast_op_add: |
| 291 | Res = Builder.CreateNSWAdd(LHS, RHS); |
| 292 | break; |
| 293 | case isl_ast_op_sub: |
| 294 | Res = Builder.CreateNSWSub(LHS, RHS); |
| 295 | break; |
| 296 | case isl_ast_op_mul: |
| 297 | Res = Builder.CreateNSWMul(LHS, RHS); |
| 298 | break; |
| 299 | case isl_ast_op_div: |
| 300 | case isl_ast_op_pdiv_q: // Dividend is non-negative |
| 301 | Res = Builder.CreateSDiv(LHS, RHS); |
| 302 | break; |
| 303 | case isl_ast_op_fdiv_q: // Round towards -infty |
| 304 | { |
| 305 | // TODO: Review code and check that this calculation does not yield |
| 306 | // incorrect overflow in some bordercases. |
| 307 | // |
| 308 | // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d |
| 309 | Value *One = ConstantInt::get(MaxType, 1); |
| 310 | Value *Zero = ConstantInt::get(MaxType, 0); |
| 311 | Value *Sum1 = Builder.CreateSub(LHS, RHS); |
| 312 | Value *Sum2 = Builder.CreateAdd(Sum1, One); |
| 313 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero); |
| 314 | Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS); |
| 315 | Res = Builder.CreateSDiv(Dividend, RHS); |
| 316 | break; |
| 317 | } |
| 318 | case isl_ast_op_pdiv_r: // Dividend is non-negative |
| 319 | Res = Builder.CreateSRem(LHS, RHS); |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | // TODO: We can truncate the result, if it fits into a smaller type. This can |
| 324 | // help in cases where we have larger operands (e.g. i67) but the result is |
| 325 | // known to fit into i64. Without the truncation, the larger i67 type may |
| 326 | // force all subsequent operations to be performed on a non-native type. |
| 327 | isl_ast_expr_free(Expr); |
| 328 | return Res; |
| 329 | } |
| 330 | |
| 331 | Value *IslExprBuilder::createOpSelect(__isl_take isl_ast_expr *Expr) { |
| 332 | assert (isl_ast_expr_get_op_type(Expr) == isl_ast_op_select |
| 333 | && "Unsupported unary isl ast expression"); |
| 334 | Value *LHS, *RHS, *Cond; |
| 335 | Type *MaxType = getType(Expr); |
| 336 | |
| 337 | Cond = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 338 | |
| 339 | LHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 340 | RHS = create(isl_ast_expr_get_op_arg(Expr, 2)); |
| 341 | |
| 342 | MaxType = getWidestType(MaxType, LHS->getType()); |
| 343 | MaxType = getWidestType(MaxType, RHS->getType()); |
| 344 | |
| 345 | if (MaxType != RHS->getType()) |
| 346 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 347 | |
| 348 | if (MaxType != LHS->getType()) |
| 349 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 350 | |
| 351 | // TODO: Do we want to truncate the result? |
| 352 | isl_ast_expr_free(Expr); |
| 353 | return Builder.CreateSelect(Cond, LHS, RHS); |
| 354 | } |
| 355 | |
| 356 | Value *IslExprBuilder::createOpICmp(__isl_take isl_ast_expr *Expr) { |
| 357 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 358 | "Expected an isl_ast_expr_op expression"); |
| 359 | |
| 360 | Value *LHS, *RHS, *Res; |
| 361 | |
| 362 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 363 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 364 | |
| 365 | Type *MaxType = LHS->getType(); |
| 366 | MaxType = getWidestType(MaxType, RHS->getType()); |
| 367 | |
| 368 | if (MaxType != RHS->getType()) |
| 369 | RHS = Builder.CreateSExt(RHS, MaxType); |
| 370 | |
| 371 | if (MaxType != LHS->getType()) |
| 372 | LHS = Builder.CreateSExt(LHS, MaxType); |
| 373 | |
| 374 | switch (isl_ast_expr_get_op_type(Expr)) { |
| 375 | default: |
| 376 | llvm_unreachable("Unsupported ICmp isl ast expression"); |
| 377 | case isl_ast_op_eq: |
| 378 | Res = Builder.CreateICmpEQ(LHS, RHS); |
| 379 | break; |
| 380 | case isl_ast_op_le: |
| 381 | Res = Builder.CreateICmpSLE(LHS, RHS); |
| 382 | break; |
| 383 | case isl_ast_op_ge: |
| 384 | Res = Builder.CreateICmpSGE(LHS, RHS); |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | isl_ast_expr_free(Expr); |
| 389 | return Res; |
| 390 | } |
| 391 | |
| 392 | Value *IslExprBuilder::createOpBoolean(__isl_take isl_ast_expr *Expr) { |
| 393 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op && |
| 394 | "Expected an isl_ast_expr_op expression"); |
| 395 | |
| 396 | Value *LHS, *RHS, *Res; |
| 397 | isl_ast_op_type OpType; |
| 398 | |
| 399 | OpType = isl_ast_expr_get_op_type(Expr); |
| 400 | |
| 401 | assert((OpType == isl_ast_op_and || OpType == isl_ast_op_or) && |
| 402 | "Unsupported isl_ast_op_type"); |
| 403 | |
| 404 | LHS = create(isl_ast_expr_get_op_arg(Expr, 0)); |
| 405 | RHS = create(isl_ast_expr_get_op_arg(Expr, 1)); |
| 406 | |
| 407 | // Even though the isl pretty printer prints the expressions as 'exp && exp' |
| 408 | // or 'exp || exp', we actually code generate the bitwise expressions |
| 409 | // 'exp & exp' or 'exp | exp'. This forces the evaluation of both branches, |
| 410 | // but it is, due to the use of i1 types, otherwise equivalent. The reason |
| 411 | // to go for bitwise operations is, that we assume the reduced control flow |
| 412 | // will outweight the overhead introduced by evaluating unneeded expressions. |
| 413 | // The isl code generation currently does not take advantage of the fact that |
| 414 | // the expression after an '||' or '&&' is in some cases not evaluated. |
| 415 | // Evaluating it anyways does not cause any undefined behaviour. |
| 416 | // |
| 417 | // TODO: Document in isl itself, that the unconditionally evaluating the |
| 418 | // second part of '||' or '&&' expressions is safe. |
| 419 | assert(LHS->getType() == Builder.getInt1Ty() && "Expected i1 type"); |
| 420 | assert(RHS->getType() == Builder.getInt1Ty() && "Expected i1 type"); |
| 421 | |
| 422 | switch (OpType) { |
| 423 | default: |
| 424 | llvm_unreachable("Unsupported boolean expression"); |
| 425 | case isl_ast_op_and: |
| 426 | Res = Builder.CreateAnd(LHS, RHS); |
| 427 | break; |
| 428 | case isl_ast_op_or: |
| 429 | Res = Builder.CreateOr(LHS, RHS); |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | isl_ast_expr_free(Expr); |
| 434 | return Res; |
| 435 | } |
| 436 | |
| 437 | Value *IslExprBuilder::createOp(__isl_take isl_ast_expr *Expr) { |
| 438 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op |
| 439 | && "Expression not of type isl_ast_expr_op"); |
| 440 | switch (isl_ast_expr_get_op_type(Expr)) { |
| 441 | case isl_ast_op_error: |
| 442 | case isl_ast_op_cond: |
| 443 | case isl_ast_op_and_then: |
| 444 | case isl_ast_op_or_else: |
| 445 | case isl_ast_op_call: |
| 446 | llvm_unreachable("Unsupported isl ast expression"); |
| 447 | case isl_ast_op_max: |
| 448 | case isl_ast_op_min: |
| 449 | return createOpNAry(Expr); |
| 450 | case isl_ast_op_add: |
| 451 | case isl_ast_op_sub: |
| 452 | case isl_ast_op_mul: |
| 453 | case isl_ast_op_div: |
| 454 | case isl_ast_op_fdiv_q: // Round towards -infty |
| 455 | case isl_ast_op_pdiv_q: // Dividend is non-negative |
| 456 | case isl_ast_op_pdiv_r: // Dividend is non-negative |
| 457 | return createOpBin(Expr); |
| 458 | case isl_ast_op_minus: |
| 459 | return createOpUnary(Expr); |
| 460 | case isl_ast_op_select: |
| 461 | return createOpSelect(Expr); |
| 462 | case isl_ast_op_and: |
| 463 | case isl_ast_op_or: |
| 464 | return createOpBoolean(Expr); |
| 465 | case isl_ast_op_eq: |
| 466 | case isl_ast_op_le: |
| 467 | case isl_ast_op_ge: |
| 468 | return createOpICmp(Expr); |
| 469 | } |
| 470 | |
| 471 | llvm_unreachable("Unsupported isl_ast_expr_op kind."); |
| 472 | } |
| 473 | |
| 474 | Value *IslExprBuilder::createId(__isl_take isl_ast_expr *Expr) { |
| 475 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_id |
| 476 | && "Expression not of type isl_ast_expr_ident"); |
| 477 | |
| 478 | isl_id *Id; |
| 479 | Value *V; |
| 480 | |
| 481 | Id = isl_ast_expr_get_id(Expr); |
| 482 | |
| 483 | assert(IDToValue.count(Id) && "Identifier not found"); |
| 484 | |
| 485 | V = IDToValue[Id]; |
| 486 | |
| 487 | isl_id_free(Id); |
| 488 | isl_ast_expr_free(Expr); |
| 489 | |
| 490 | return V; |
| 491 | } |
| 492 | |
| 493 | IntegerType *IslExprBuilder::getType(__isl_keep isl_ast_expr *Expr) { |
| 494 | // XXX: We assume i64 is large enough. This is often true, but in general |
| 495 | // incorrect. Also, on 32bit architectures, it would be beneficial to |
| 496 | // use a smaller type. We can and should directly derive this information |
| 497 | // during code generation. |
| 498 | return IntegerType::get(Builder.getContext(), 64); |
| 499 | } |
| 500 | |
| 501 | Value *IslExprBuilder::createInt(__isl_take isl_ast_expr *Expr) { |
| 502 | assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_int |
| 503 | && "Expression not of type isl_ast_expr_int"); |
| 504 | isl_int Int; |
| 505 | Value *V; |
| 506 | APInt APValue; |
| 507 | IntegerType *T; |
| 508 | |
| 509 | isl_int_init(Int); |
| 510 | isl_ast_expr_get_int(Expr, &Int); |
| 511 | APValue = APInt_from_MPZ(Int); |
| 512 | T = getType(Expr); |
| 513 | APValue = APValue.sextOrSelf(T->getBitWidth()); |
| 514 | V = ConstantInt::get(T, APValue); |
| 515 | |
| 516 | isl_ast_expr_free(Expr); |
| 517 | isl_int_clear(Int); |
| 518 | return V; |
| 519 | } |
| 520 | |
| 521 | Value *IslExprBuilder::create(__isl_take isl_ast_expr *Expr) { |
| 522 | switch (isl_ast_expr_get_type(Expr)) { |
| 523 | case isl_ast_expr_error: |
| 524 | llvm_unreachable("Code generation error"); |
| 525 | case isl_ast_expr_op: |
| 526 | return createOp(Expr); |
| 527 | case isl_ast_expr_id: |
| 528 | return createId(Expr); |
| 529 | case isl_ast_expr_int: |
| 530 | return createInt(Expr); |
| 531 | } |
| 532 | |
| 533 | llvm_unreachable("Unexpected enum value"); |
| 534 | } |
| 535 | |
| 536 | class IslNodeBuilder { |
| 537 | public: |
| 538 | IslNodeBuilder(IRBuilder<> &Builder, Pass *P): |
| 539 | Builder(Builder), ExprBuilder(Builder, IDToValue, P), P(P) {} |
| 540 | |
| 541 | void addParameters(__isl_take isl_set *Context); |
| 542 | void create(__isl_take isl_ast_node *Node); |
| 543 | |
| 544 | private: |
| 545 | IRBuilder<> &Builder; |
| 546 | IslExprBuilder ExprBuilder; |
| 547 | Pass *P; |
| 548 | |
| 549 | // This maps an isl_id* to the Value* it has in the generated program. For now |
| 550 | // on, the only isl_ids that are stored here are the newly calculated loop |
| 551 | // ivs. |
| 552 | std::map<isl_id *, Value*> IDToValue; |
| 553 | |
| 554 | // Extract the upper bound of this loop |
| 555 | // |
| 556 | // The isl code generation can generate arbitrary expressions to check if the |
| 557 | // upper bound of a loop is reached, but it provides an option to enforce |
| 558 | // 'atomic' upper bounds. An 'atomic upper bound is always of the form |
| 559 | // iv <= expr, where expr is an (arbitrary) expression not containing iv. |
| 560 | // |
| 561 | // This function extracts 'atomic' upper bounds. Polly, in general, requires |
| 562 | // atomic upper bounds for the following reasons: |
| 563 | // |
| 564 | // 1. An atomic upper bound is loop invariant |
| 565 | // |
| 566 | // It must not be calculated at each loop iteration and can often even be |
| 567 | // hoisted out further by the loop invariant code motion. |
| 568 | // |
| 569 | // 2. OpenMP needs a loop invarient upper bound to calculate the number |
| 570 | // of loop iterations. |
| 571 | // |
| 572 | // 3. With the existing code, upper bounds have been easier to implement. |
| 573 | __isl_give isl_ast_expr *getUpperBound(__isl_keep isl_ast_node *For); |
| 574 | |
| 575 | void createFor(__isl_take isl_ast_node *For); |
| 576 | void createIf(__isl_take isl_ast_node *If); |
| 577 | void createUser(__isl_take isl_ast_node *User); |
| 578 | void createBlock(__isl_take isl_ast_node *Block); |
| 579 | }; |
| 580 | |
| 581 | __isl_give isl_ast_expr *IslNodeBuilder::getUpperBound( |
| 582 | __isl_keep isl_ast_node *For) { |
| 583 | isl_id *UBID, *IteratorID; |
| 584 | isl_ast_expr *Cond, *Iterator, *UB, *Arg0; |
| 585 | |
| 586 | Cond = isl_ast_node_for_get_cond(For); |
| 587 | Iterator = isl_ast_node_for_get_iterator(For); |
| 588 | |
| 589 | assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op |
| 590 | && "conditional expression is not an atomic upper bound"); |
| 591 | |
| 592 | assert(isl_ast_expr_get_op_type(Cond) == isl_ast_op_le |
| 593 | && "conditional expression is not an atomic upper bound"); |
| 594 | |
| 595 | Arg0 = isl_ast_expr_get_op_arg(Cond, 0); |
| 596 | |
| 597 | assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id |
| 598 | && "conditional expression is not an atomic upper bound"); |
| 599 | |
| 600 | UBID = isl_ast_expr_get_id(Arg0); |
| 601 | |
| 602 | assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id |
| 603 | && "Could not get the iterator"); |
| 604 | |
| 605 | IteratorID = isl_ast_expr_get_id(Iterator); |
| 606 | |
| 607 | assert(UBID == IteratorID |
| 608 | && "conditional expression is not an atomic upper bound"); |
| 609 | |
| 610 | UB = isl_ast_expr_get_op_arg(Cond, 1); |
| 611 | |
| 612 | isl_ast_expr_free(Cond); |
| 613 | isl_ast_expr_free(Iterator); |
| 614 | isl_ast_expr_free(Arg0); |
| 615 | isl_id_free(IteratorID); |
| 616 | isl_id_free(UBID); |
| 617 | |
| 618 | return UB; |
| 619 | } |
| 620 | |
| 621 | void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) { |
| 622 | isl_ast_node *Body; |
| 623 | isl_ast_expr *Init, *Inc, *Iterator, *UB; |
| 624 | isl_id *IteratorID; |
| 625 | Value *ValueLB, *ValueUB, *ValueInc; |
| 626 | Type *MaxType; |
| 627 | BasicBlock *AfterBlock; |
| 628 | Value *IV; |
| 629 | |
| 630 | Body = isl_ast_node_for_get_body(For); |
| 631 | |
| 632 | // isl_ast_node_for_is_degenerate(For) |
| 633 | // |
| 634 | // TODO: For degenerated loops we could generate a plain assignment. |
| 635 | // However, for now we just reuse the logic for normal loops, which will |
| 636 | // create a loop with a single iteration. |
| 637 | |
| 638 | Init = isl_ast_node_for_get_init(For); |
| 639 | Inc = isl_ast_node_for_get_inc(For); |
| 640 | Iterator = isl_ast_node_for_get_iterator(For); |
| 641 | IteratorID = isl_ast_expr_get_id(Iterator); |
| 642 | UB = getUpperBound(For); |
| 643 | |
| 644 | ValueLB = ExprBuilder.create(Init); |
| 645 | ValueUB = ExprBuilder.create(UB); |
| 646 | ValueInc = ExprBuilder.create(Inc); |
| 647 | |
| 648 | MaxType = ExprBuilder.getType(Iterator); |
| 649 | MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType()); |
| 650 | MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType()); |
| 651 | MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType()); |
| 652 | |
| 653 | if (MaxType != ValueLB->getType()) |
| 654 | ValueLB = Builder.CreateSExt(ValueLB, MaxType); |
| 655 | if (MaxType != ValueUB->getType()) |
| 656 | ValueUB = Builder.CreateSExt(ValueUB, MaxType); |
| 657 | if (MaxType != ValueInc->getType()) |
| 658 | ValueInc = Builder.CreateSExt(ValueInc, MaxType); |
| 659 | |
| 660 | // TODO: In case we can proof a loop is executed at least once, we can |
| 661 | // generate the condition iv != UB + stride (consider possible |
| 662 | // overflow). This condition will allow LLVM to prove the loop is |
| 663 | // executed at least once, which will enable a lot of loop invariant |
| 664 | // code motion. |
| 665 | |
| 666 | IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, AfterBlock); |
| 667 | IDToValue[IteratorID] = IV; |
| 668 | |
| 669 | create(Body); |
| 670 | |
| 671 | IDToValue.erase(IteratorID); |
| 672 | |
| 673 | Builder.SetInsertPoint(AfterBlock->begin()); |
| 674 | |
| 675 | isl_ast_node_free(For); |
| 676 | isl_ast_expr_free(Iterator); |
| 677 | isl_id_free(IteratorID); |
| 678 | } |
| 679 | |
| 680 | void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) { |
| 681 | isl_ast_expr *Cond = isl_ast_node_if_get_cond(If); |
| 682 | |
| 683 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 684 | LLVMContext &Context = F->getContext(); |
| 685 | |
| 686 | BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(), |
| 687 | Builder.GetInsertPoint(), P); |
| 688 | CondBB->setName("polly.cond"); |
| 689 | BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P); |
| 690 | MergeBB->setName("polly.merge"); |
| 691 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
| 692 | BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F); |
| 693 | |
| 694 | DominatorTree &DT = P->getAnalysis<DominatorTree>(); |
| 695 | DT.addNewBlock(ThenBB, CondBB); |
| 696 | DT.addNewBlock(ElseBB, CondBB); |
| 697 | DT.changeImmediateDominator(MergeBB, CondBB); |
| 698 | |
| 699 | CondBB->getTerminator()->eraseFromParent(); |
| 700 | |
| 701 | Builder.SetInsertPoint(CondBB); |
| 702 | Value *Predicate = ExprBuilder.create(Cond); |
| 703 | Builder.CreateCondBr(Predicate, ThenBB, ElseBB); |
| 704 | Builder.SetInsertPoint(ThenBB); |
| 705 | Builder.CreateBr(MergeBB); |
| 706 | Builder.SetInsertPoint(ElseBB); |
| 707 | Builder.CreateBr(MergeBB); |
| 708 | Builder.SetInsertPoint(ThenBB->begin()); |
| 709 | |
| 710 | create(isl_ast_node_if_get_then(If)); |
| 711 | |
| 712 | Builder.SetInsertPoint(ElseBB->begin()); |
| 713 | |
| 714 | if (isl_ast_node_if_has_else(If)) |
| 715 | create(isl_ast_node_if_get_else(If)); |
| 716 | |
| 717 | Builder.SetInsertPoint(MergeBB->begin()); |
| 718 | |
| 719 | isl_ast_node_free(If); |
| 720 | } |
| 721 | |
| 722 | void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) { |
| 723 | ValueMapT VMap; |
| 724 | struct IslAstUser *UserInfo; |
| 725 | isl_id *Annotation, *Id; |
| 726 | ScopStmt *Stmt; |
| 727 | |
| 728 | Annotation = isl_ast_node_get_annotation(User); |
| 729 | UserInfo = (struct IslAstUser *) isl_id_get_user(Annotation); |
| 730 | Id = isl_pw_multi_aff_get_tuple_id(UserInfo->PMA, isl_dim_out); |
| 731 | Stmt = (ScopStmt *) isl_id_get_user(Id); |
| 732 | |
| 733 | for (unsigned i = 0; i < isl_pw_multi_aff_dim(UserInfo->PMA, isl_dim_out); |
| 734 | ++i) { |
| 735 | isl_pw_aff *Aff; |
| 736 | isl_ast_expr *Expr; |
| 737 | const Value *OldIV; |
| 738 | Value *V; |
| 739 | |
| 740 | Aff = isl_pw_multi_aff_get_pw_aff(UserInfo->PMA, i); |
| 741 | Expr = isl_ast_build_expr_from_pw_aff(UserInfo->Context, Aff); |
| 742 | OldIV = Stmt->getInductionVariableForDimension(i); |
| 743 | V = ExprBuilder.create(Expr); |
| 744 | |
| 745 | // CreateIntCast can introduce trunc expressions. This is correct, as the |
| 746 | // result will always fit into the type of the original induction variable |
| 747 | // (because we calculate a value of the original induction variable). |
| 748 | V = Builder.CreateIntCast(V, OldIV->getType(), true); |
| 749 | VMap[OldIV] = V; |
| 750 | } |
| 751 | |
| 752 | BlockGenerator::generate(Builder, *Stmt, VMap, P); |
| 753 | |
| 754 | isl_ast_node_free(User); |
| 755 | isl_id_free(Annotation); |
| 756 | isl_id_free(Id); |
| 757 | } |
| 758 | |
| 759 | void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) { |
| 760 | isl_ast_node_list *List = isl_ast_node_block_get_children(Block); |
| 761 | |
| 762 | for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i) |
| 763 | create(isl_ast_node_list_get_ast_node(List, i)); |
| 764 | |
| 765 | isl_ast_node_free(Block); |
| 766 | isl_ast_node_list_free(List); |
| 767 | } |
| 768 | |
| 769 | void IslNodeBuilder::create(__isl_take isl_ast_node *Node) { |
| 770 | switch (isl_ast_node_get_type(Node)) { |
| 771 | case isl_ast_node_error: |
| 772 | llvm_unreachable("code generation error"); |
| 773 | case isl_ast_node_for: |
| 774 | createFor(Node); |
| 775 | return; |
| 776 | case isl_ast_node_if: |
| 777 | createIf(Node); |
| 778 | return; |
| 779 | case isl_ast_node_user: |
| 780 | createUser(Node); |
| 781 | return; |
| 782 | case isl_ast_node_block: |
| 783 | createBlock(Node); |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | llvm_unreachable("Unknown isl_ast_node type"); |
| 788 | } |
| 789 | |
| 790 | void IslNodeBuilder::addParameters(__isl_take isl_set *Context) { |
| 791 | SCEVExpander Rewriter(P->getAnalysis<ScalarEvolution>(), "polly"); |
| 792 | |
| 793 | for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) { |
| 794 | isl_id *Id; |
| 795 | const SCEV *Scev; |
| 796 | IntegerType *T; |
| 797 | Instruction *InsertLocation; |
| 798 | |
| 799 | Id = isl_set_get_dim_id(Context, isl_dim_param, i); |
| 800 | Scev = (const SCEV*) isl_id_get_user(Id); |
| 801 | T = dyn_cast<IntegerType>(Scev->getType()); |
| 802 | InsertLocation = --(Builder.GetInsertBlock()->end()); |
| 803 | Value *V = Rewriter.expandCodeFor(Scev, T, InsertLocation); |
| 804 | IDToValue[Id] = V; |
| 805 | |
| 806 | isl_id_free(Id); |
| 807 | } |
| 808 | |
| 809 | isl_set_free(Context); |
| 810 | } |
| 811 | |
| 812 | namespace { |
| 813 | class IslCodeGeneration : public ScopPass { |
| 814 | public: |
| 815 | static char ID; |
| 816 | |
| 817 | IslCodeGeneration() : ScopPass(ID) {} |
| 818 | |
| 819 | bool runOnScop(Scop &S) { |
| 820 | IslAstInfo &AstInfo = getAnalysis<IslAstInfo>(); |
| 821 | assert(S.getRegion().isSimple() && "Only simple regions are supported"); |
| 822 | |
| 823 | BasicBlock *StartBlock = executeScopConditionally(S, this); |
| 824 | isl_ast_node *Ast = AstInfo.getAst(); |
| 825 | IRBuilder<> Builder(StartBlock->begin()); |
| 826 | |
| 827 | IslNodeBuilder NodeBuilder(Builder, this); |
| 828 | NodeBuilder.addParameters(S.getContext()); |
| 829 | NodeBuilder.create(Ast); |
| 830 | return true; |
| 831 | } |
| 832 | |
| 833 | virtual void printScop(raw_ostream &OS) const { |
| 834 | } |
| 835 | |
| 836 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 837 | AU.addRequired<DominatorTree>(); |
| 838 | AU.addRequired<IslAstInfo>(); |
| 839 | AU.addRequired<RegionInfo>(); |
| 840 | AU.addRequired<ScalarEvolution>(); |
| 841 | AU.addRequired<ScopDetection>(); |
| 842 | AU.addRequired<ScopInfo>(); |
| 843 | |
| 844 | AU.addPreserved<Dependences>(); |
| 845 | |
| 846 | // FIXME: We do not create LoopInfo for the newly generated loops. |
| 847 | AU.addPreserved<LoopInfo>(); |
| 848 | AU.addPreserved<DominatorTree>(); |
| 849 | AU.addPreserved<IslAstInfo>(); |
| 850 | AU.addPreserved<ScopDetection>(); |
| 851 | AU.addPreserved<ScalarEvolution>(); |
| 852 | |
| 853 | // FIXME: We do not yet add regions for the newly generated code to the |
| 854 | // region tree. |
| 855 | AU.addPreserved<RegionInfo>(); |
| 856 | AU.addPreserved<TempScopInfo>(); |
| 857 | AU.addPreserved<ScopInfo>(); |
| 858 | AU.addPreservedID(IndependentBlocksID); |
| 859 | } |
| 860 | }; |
| 861 | } |
| 862 | |
| 863 | char IslCodeGeneration::ID = 1; |
| 864 | |
| 865 | INITIALIZE_PASS_BEGIN(IslCodeGeneration, "polly-codegen-isl", |
| 866 | "Polly - Create LLVM-IR from SCoPs", false, false) |
| 867 | INITIALIZE_PASS_DEPENDENCY(Dependences) |
| 868 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 869 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 870 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 871 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 872 | INITIALIZE_PASS_DEPENDENCY(ScopDetection) |
| 873 | INITIALIZE_PASS_END(IslCodeGeneration, "polly-codegen-isl", |
| 874 | "Polly - Create LLVM-IR from SCoPs", false, false) |
| 875 | |
| 876 | Pass *polly::createIslCodeGenerationPass() { |
| 877 | return new IslCodeGeneration(); |
| 878 | } |
| 879 | |
| 880 | #endif /* ISL_CODEGEN_FOUND */ |