Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1 | //===------ CodeGeneration.cpp - Code generate the Scops. -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The CodeGeneration pass takes a Scop created by ScopInfo and translates it |
| 11 | // back to LLVM-IR using Cloog. |
| 12 | // |
| 13 | // The Scop describes the high level memory behaviour of a control flow region. |
| 14 | // Transformation passes can update the schedule (execution order) of statements |
| 15 | // in the Scop. Cloog is used to generate an abstract syntax tree (clast) that |
| 16 | // reflects the updated execution order. This clast is used to create new |
| 17 | // LLVM-IR that is computational equivalent to the original control flow region, |
| 18 | // but executes its code in the new execution order defined by the changed |
| 19 | // scattering. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
| 23 | #define DEBUG_TYPE "polly-codegen" |
| 24 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 25 | #include "polly/Cloog.h" |
Tobias Grosser | 67707b7 | 2011-10-23 20:59:40 +0000 | [diff] [blame] | 26 | #include "polly/CodeGeneration.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 27 | #include "polly/Dependences.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 28 | #include "polly/LinkAllPasses.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 29 | #include "polly/ScopInfo.h" |
| 30 | #include "polly/TempScopInfo.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 31 | #include "polly/Support/GICHelper.h" |
| 32 | |
| 33 | #include "llvm/Module.h" |
| 34 | #include "llvm/ADT/SetVector.h" |
| 35 | #include "llvm/Analysis/LoopInfo.h" |
| 36 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
| 38 | #include "llvm/Support/Debug.h" |
| 39 | #include "llvm/Support/IRBuilder.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 40 | #include "llvm/Target/TargetData.h" |
Tobias Grosser | bda1f8f | 2012-02-01 14:23:29 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 42 | |
| 43 | #define CLOOG_INT_GMP 1 |
| 44 | #include "cloog/cloog.h" |
| 45 | #include "cloog/isl/cloog.h" |
| 46 | |
Raghesh Aloor | a71989c | 2011-12-28 02:48:26 +0000 | [diff] [blame] | 47 | #include "isl/aff.h" |
| 48 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 49 | #include <vector> |
| 50 | #include <utility> |
| 51 | |
| 52 | using namespace polly; |
| 53 | using namespace llvm; |
| 54 | |
| 55 | struct isl_set; |
| 56 | |
| 57 | namespace polly { |
| 58 | |
Tobias Grosser | 67707b7 | 2011-10-23 20:59:40 +0000 | [diff] [blame] | 59 | bool EnablePollyVector; |
| 60 | |
| 61 | static cl::opt<bool, true> |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 62 | Vector("enable-polly-vector", |
| 63 | cl::desc("Enable polly vector code generation"), cl::Hidden, |
Tobias Grosser | 67707b7 | 2011-10-23 20:59:40 +0000 | [diff] [blame] | 64 | cl::location(EnablePollyVector), cl::init(false)); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 65 | |
| 66 | static cl::opt<bool> |
| 67 | OpenMP("enable-polly-openmp", |
| 68 | cl::desc("Generate OpenMP parallel code"), cl::Hidden, |
| 69 | cl::value_desc("OpenMP code generation enabled if true"), |
| 70 | cl::init(false)); |
| 71 | |
| 72 | static cl::opt<bool> |
| 73 | AtLeastOnce("enable-polly-atLeastOnce", |
| 74 | cl::desc("Give polly the hint, that every loop is executed at least" |
| 75 | "once"), cl::Hidden, |
| 76 | cl::value_desc("OpenMP code generation enabled if true"), |
| 77 | cl::init(false)); |
| 78 | |
| 79 | static cl::opt<bool> |
| 80 | Aligned("enable-polly-aligned", |
| 81 | cl::desc("Assumed aligned memory accesses."), cl::Hidden, |
| 82 | cl::value_desc("OpenMP code generation enabled if true"), |
| 83 | cl::init(false)); |
| 84 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 85 | typedef DenseMap<const Value*, Value*> ValueMapT; |
| 86 | typedef DenseMap<const char*, Value*> CharMapT; |
| 87 | typedef std::vector<ValueMapT> VectorValueMapT; |
Raghesh Aloor | a71989c | 2011-12-28 02:48:26 +0000 | [diff] [blame] | 88 | typedef struct { |
Raghesh Aloor | a71989c | 2011-12-28 02:48:26 +0000 | [diff] [blame] | 89 | Value *Result; |
| 90 | IRBuilder<> *Builder; |
| 91 | }IslPwAffUserInfo; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 92 | |
| 93 | // Create a new loop. |
| 94 | // |
| 95 | // @param Builder The builder used to create the loop. It also defines the |
| 96 | // place where to create the loop. |
| 97 | // @param UB The upper bound of the loop iv. |
| 98 | // @param Stride The number by which the loop iv is incremented after every |
| 99 | // iteration. |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 100 | static Value *createLoop(IRBuilder<> *Builder, Value *LB, Value *UB, |
| 101 | APInt Stride, DominatorTree *DT, Pass *P, |
| 102 | BasicBlock **AfterBlock) { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 103 | Function *F = Builder->GetInsertBlock()->getParent(); |
| 104 | LLVMContext &Context = F->getContext(); |
| 105 | |
| 106 | BasicBlock *PreheaderBB = Builder->GetInsertBlock(); |
| 107 | BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F); |
| 108 | BasicBlock *BodyBB = BasicBlock::Create(Context, "polly.loop_body", F); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 109 | BasicBlock *AfterBB = SplitBlock(PreheaderBB, Builder->GetInsertPoint()++, P); |
| 110 | AfterBB->setName("polly.loop_after"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 111 | |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 112 | PreheaderBB->getTerminator()->setSuccessor(0, HeaderBB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 113 | DT->addNewBlock(HeaderBB, PreheaderBB); |
| 114 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 115 | Builder->SetInsertPoint(HeaderBB); |
| 116 | |
| 117 | // Use the type of upper and lower bound. |
| 118 | assert(LB->getType() == UB->getType() |
| 119 | && "Different types for upper and lower bound."); |
| 120 | |
Tobias Grosser | 55927aa | 2011-07-18 09:53:32 +0000 | [diff] [blame] | 121 | IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 122 | assert(LoopIVType && "UB is not integer?"); |
| 123 | |
| 124 | // IV |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 125 | PHINode *IV = Builder->CreatePHI(LoopIVType, 2, "polly.loopiv"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 126 | IV->addIncoming(LB, PreheaderBB); |
| 127 | |
| 128 | // IV increment. |
| 129 | Value *StrideValue = ConstantInt::get(LoopIVType, |
| 130 | Stride.zext(LoopIVType->getBitWidth())); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 131 | Value *IncrementedIV = Builder->CreateAdd(IV, StrideValue, |
| 132 | "polly.next_loopiv"); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 133 | |
| 134 | // Exit condition. |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 135 | Value *CMP; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 136 | if (AtLeastOnce) { // At least on iteration. |
| 137 | UB = Builder->CreateAdd(UB, Builder->getInt64(1)); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 138 | CMP = Builder->CreateICmpNE(IV, UB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 139 | } else { // Maybe not executed at all. |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 140 | CMP = Builder->CreateICmpSLE(IV, UB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 141 | } |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 142 | |
| 143 | Builder->CreateCondBr(CMP, BodyBB, AfterBB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 144 | DT->addNewBlock(BodyBB, HeaderBB); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 145 | |
| 146 | Builder->SetInsertPoint(BodyBB); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 147 | Builder->CreateBr(HeaderBB); |
| 148 | IV->addIncoming(IncrementedIV, BodyBB); |
| 149 | DT->changeImmediateDominator(AfterBB, HeaderBB); |
| 150 | |
| 151 | Builder->SetInsertPoint(BodyBB->begin()); |
| 152 | *AfterBlock = AfterBB; |
| 153 | |
| 154 | return IV; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Tobias Grosser | 642c411 | 2012-03-02 11:27:25 +0000 | [diff] [blame^] | 157 | class IslGenerator; |
| 158 | |
| 159 | class IslGenerator { |
| 160 | public: |
| 161 | IslGenerator(IRBuilder<> &Builder) : Builder(Builder) {} |
| 162 | Value *generateIslInt(__isl_take isl_int Int); |
| 163 | Value *generateIslAff(__isl_take isl_aff *Aff); |
| 164 | Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff); |
| 165 | |
| 166 | private: |
| 167 | typedef struct { |
| 168 | Value *Result; |
| 169 | class IslGenerator *Generator; |
| 170 | } IslGenInfo; |
| 171 | |
| 172 | IRBuilder<> &Builder; |
| 173 | static int mergeIslAffValues(__isl_take isl_set *Set, |
| 174 | __isl_take isl_aff *Aff, void *User); |
| 175 | }; |
| 176 | |
| 177 | Value *IslGenerator::generateIslInt(isl_int Int) { |
| 178 | mpz_t IntMPZ; |
| 179 | mpz_init(IntMPZ); |
| 180 | isl_int_get_gmp(Int, IntMPZ); |
| 181 | Value *IntValue = Builder.getInt(APInt_from_MPZ(IntMPZ)); |
| 182 | mpz_clear(IntMPZ); |
| 183 | return IntValue; |
| 184 | } |
| 185 | |
| 186 | Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) { |
| 187 | assert(isl_aff_is_cst(Aff) && "Only constant access functions supported"); |
| 188 | Value *ConstValue; |
| 189 | isl_int ConstIsl; |
| 190 | |
| 191 | isl_int_init(ConstIsl); |
| 192 | isl_aff_get_constant(Aff, &ConstIsl); |
| 193 | ConstValue = generateIslInt(ConstIsl); |
| 194 | |
| 195 | isl_int_clear(ConstIsl); |
| 196 | isl_aff_free(Aff); |
| 197 | |
| 198 | return ConstValue; |
| 199 | } |
| 200 | |
| 201 | int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set, |
| 202 | __isl_take isl_aff *Aff, void *User) { |
| 203 | IslGenInfo *GenInfo = (IslGenInfo *)User; |
| 204 | |
| 205 | assert((GenInfo->Result == NULL) && "Result is already set." |
| 206 | "Currently only single isl_aff is supported"); |
| 207 | assert(isl_set_plain_is_universe(Set) |
| 208 | && "Code generation failed because the set is not universe"); |
| 209 | |
| 210 | GenInfo->Result = GenInfo->Generator->generateIslAff(Aff); |
| 211 | |
| 212 | isl_set_free(Set); |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) { |
| 217 | IslGenInfo User; |
| 218 | User.Result = NULL; |
| 219 | User.Generator = this; |
| 220 | isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User); |
| 221 | assert(User.Result && "Code generation for isl_pw_aff failed"); |
| 222 | |
| 223 | isl_pw_aff_free(PwAff); |
| 224 | return User.Result; |
| 225 | } |
| 226 | |
| 227 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 228 | class BlockGenerator { |
Tobias Grosser | c941ede | 2012-03-02 11:26:49 +0000 | [diff] [blame] | 229 | public: |
| 230 | /// @brief Generate code for single basic block. |
| 231 | static void generate(IRBuilder<> &B, ValueMapT &ValueMap, |
| 232 | VectorValueMapT &VectorMaps, ScopStmt &Stmt, |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 233 | __isl_keep isl_set *Domain, Pass *P) { |
Tobias Grosser | 8412cda | 2012-03-02 11:26:55 +0000 | [diff] [blame] | 234 | BlockGenerator Generator(B, ValueMap, VectorMaps, Stmt, Domain, P); |
| 235 | Generator.copyBB(); |
Tobias Grosser | c941ede | 2012-03-02 11:26:49 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | private: |
| 239 | BlockGenerator(IRBuilder<> &B, ValueMapT &vmap, VectorValueMapT &vmaps, |
Tobias Grosser | 8412cda | 2012-03-02 11:26:55 +0000 | [diff] [blame] | 240 | ScopStmt &Stmt, __isl_keep isl_set *domain, Pass *p); |
Tobias Grosser | c941ede | 2012-03-02 11:26:49 +0000 | [diff] [blame] | 241 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 242 | IRBuilder<> &Builder; |
| 243 | ValueMapT &VMap; |
| 244 | VectorValueMapT &ValueMaps; |
| 245 | Scop &S; |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 246 | ScopStmt &Statement; |
| 247 | isl_set *ScatteringDomain; |
Tobias Grosser | 8412cda | 2012-03-02 11:26:55 +0000 | [diff] [blame] | 248 | Pass *P; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 249 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 250 | Value *makeVectorOperand(Value *Operand); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 251 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 252 | Value *getOperand(const Value *oldOperand, ValueMapT &BBMap, |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 253 | ValueMapT *VectorMap = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 254 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 255 | Type *getVectorPtrTy(const Value *V, int Width); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 256 | |
| 257 | /// @brief Load a vector from a set of adjacent scalars |
| 258 | /// |
| 259 | /// In case a set of scalars is known to be next to each other in memory, |
| 260 | /// create a vector load that loads those scalars |
| 261 | /// |
| 262 | /// %vector_ptr= bitcast double* %p to <4 x double>* |
| 263 | /// %vec_full = load <4 x double>* %vector_ptr |
| 264 | /// |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 265 | Value *generateStrideOneLoad(const LoadInst *Load, ValueMapT &BBMap); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 266 | |
| 267 | /// @brief Load a vector initialized from a single scalar in memory |
| 268 | /// |
| 269 | /// In case all elements of a vector are initialized to the same |
| 270 | /// scalar value, this value is loaded and shuffeled into all elements |
| 271 | /// of the vector. |
| 272 | /// |
| 273 | /// %splat_one = load <1 x double>* %p |
| 274 | /// %splat = shufflevector <1 x double> %splat_one, <1 x |
| 275 | /// double> %splat_one, <4 x i32> zeroinitializer |
| 276 | /// |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 277 | Value *generateStrideZeroLoad(const LoadInst *Load, ValueMapT &BBMap); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 278 | |
| 279 | /// @Load a vector from scalars distributed in memory |
| 280 | /// |
| 281 | /// In case some scalars a distributed randomly in memory. Create a vector |
| 282 | /// by loading each scalar and by inserting one after the other into the |
| 283 | /// vector. |
| 284 | /// |
| 285 | /// %scalar_1= load double* %p_1 |
| 286 | /// %vec_1 = insertelement <2 x double> undef, double %scalar_1, i32 0 |
| 287 | /// %scalar 2 = load double* %p_2 |
| 288 | /// %vec_2 = insertelement <2 x double> %vec_1, double %scalar_1, i32 1 |
| 289 | /// |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 290 | Value *generateUnknownStrideLoad(const LoadInst *Load, |
| 291 | VectorValueMapT &ScalarMaps); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 292 | |
Raghesh Aloor | 129e867 | 2011-08-15 02:33:39 +0000 | [diff] [blame] | 293 | /// @brief Get the memory access offset to be added to the base address |
Raghesh Aloor | 46eceba | 2011-12-09 14:27:17 +0000 | [diff] [blame] | 294 | std::vector <Value*> getMemoryAccessIndex(__isl_keep isl_map *AccessRelation, |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 295 | Value *BaseAddress); |
Raghesh Aloor | 129e867 | 2011-08-15 02:33:39 +0000 | [diff] [blame] | 296 | |
Raghesh Aloor | 62b1312 | 2011-08-03 17:02:50 +0000 | [diff] [blame] | 297 | /// @brief Get the new operand address according to the changed access in |
| 298 | /// JSCOP file. |
Raghesh Aloor | 46eceba | 2011-12-09 14:27:17 +0000 | [diff] [blame] | 299 | Value *getNewAccessOperand(__isl_keep isl_map *NewAccessRelation, |
| 300 | Value *BaseAddress, const Value *OldOperand, |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 301 | ValueMapT &BBMap); |
Raghesh Aloor | 62b1312 | 2011-08-03 17:02:50 +0000 | [diff] [blame] | 302 | |
| 303 | /// @brief Generate the operand address |
| 304 | Value *generateLocationAccessed(const Instruction *Inst, |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 305 | const Value *Pointer, ValueMapT &BBMap ); |
Raghesh Aloor | 129e867 | 2011-08-15 02:33:39 +0000 | [diff] [blame] | 306 | |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 307 | Value *generateScalarLoad(const LoadInst *load, ValueMapT &BBMap); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 308 | |
Tobias Grosser | fc1153f | 2012-03-02 11:27:15 +0000 | [diff] [blame] | 309 | void generateVectorLoad(const LoadInst *Load, ValueMapT &VectorMap, |
| 310 | VectorValueMapT &ScalarMaps); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 311 | |
Tobias Grosser | 8b4bf8b | 2012-03-02 11:27:11 +0000 | [diff] [blame] | 312 | void copyVectorUnaryInst(const UnaryInstruction *Inst, ValueMapT &BBMap, |
| 313 | ValueMapT &VectorMap); |
Tobias Grosser | c921515 | 2011-09-04 11:45:52 +0000 | [diff] [blame] | 314 | |
Tobias Grosser | 8b4bf8b | 2012-03-02 11:27:11 +0000 | [diff] [blame] | 315 | void copyVectorBinInst(const BinaryOperator *Inst, ValueMapT &BBMap, |
| 316 | ValueMapT &VectorMap); |
Tobias Grosser | 09c5710 | 2011-09-04 11:45:29 +0000 | [diff] [blame] | 317 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 318 | void copyVectorStore(const StoreInst *Store, ValueMapT &BBMap, |
Tobias Grosser | 8927a44 | 2012-03-02 11:27:05 +0000 | [diff] [blame] | 319 | ValueMapT &VectorMap, VectorValueMapT &ScalarMaps); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 320 | |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 321 | void copyInstScalar(const Instruction *Inst, ValueMapT &BBMap); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 322 | |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 323 | bool hasVectorOperands(const Instruction *Inst, ValueMapT &VectorMap); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 324 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 325 | int getVectorWidth(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 326 | |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 327 | bool isVectorBlock(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 328 | |
Tobias Grosser | 32152cb | 2012-03-02 11:27:18 +0000 | [diff] [blame] | 329 | void copyVectorInstruction(const Instruction *Inst, ValueMapT &VectorMap, |
| 330 | VectorValueMapT &ScalarMaps); |
Tobias Grosser | fc1153f | 2012-03-02 11:27:15 +0000 | [diff] [blame] | 331 | void copyInstruction(const Instruction *Inst, ValueMapT &VectorMap, |
| 332 | VectorValueMapT &ScalarMaps); |
Tobias Grosser | 7551c30 | 2011-09-04 11:45:41 +0000 | [diff] [blame] | 333 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 334 | // Insert a copy of a basic block in the newly generated code. |
| 335 | // |
| 336 | // @param Builder The builder used to insert the code. It also specifies |
| 337 | // where to insert the code. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 338 | // @param VMap A map returning for any old value its new equivalent. This |
| 339 | // is used to update the operands of the statements. |
| 340 | // For new statements a relation old->new is inserted in this |
| 341 | // map. |
Tobias Grosser | 8412cda | 2012-03-02 11:26:55 +0000 | [diff] [blame] | 342 | void copyBB(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 343 | }; |
| 344 | |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 345 | BlockGenerator::BlockGenerator(IRBuilder<> &B, ValueMapT &vmap, |
| 346 | VectorValueMapT &vmaps, ScopStmt &Stmt, |
Tobias Grosser | 8412cda | 2012-03-02 11:26:55 +0000 | [diff] [blame] | 347 | __isl_keep isl_set *domain, Pass *P) |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 348 | : Builder(B), VMap(vmap), ValueMaps(vmaps), S(*Stmt.getParent()), |
Tobias Grosser | 8412cda | 2012-03-02 11:26:55 +0000 | [diff] [blame] | 349 | Statement(Stmt), ScatteringDomain(domain), P(P) {} |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 350 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 351 | Value *BlockGenerator::makeVectorOperand(Value *Operand) { |
| 352 | int VectorWidth = getVectorWidth(); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 353 | if (Operand->getType()->isVectorTy()) |
| 354 | return Operand; |
| 355 | |
| 356 | VectorType *VectorType = VectorType::get(Operand->getType(), VectorWidth); |
| 357 | Value *Vector = UndefValue::get(VectorType); |
| 358 | Vector = Builder.CreateInsertElement(Vector, Operand, Builder.getInt32(0)); |
| 359 | |
| 360 | std::vector<Constant*> Splat; |
| 361 | |
| 362 | for (int i = 0; i < VectorWidth; i++) |
| 363 | Splat.push_back (Builder.getInt32(0)); |
| 364 | |
| 365 | Constant *SplatVector = ConstantVector::get(Splat); |
| 366 | |
| 367 | return Builder.CreateShuffleVector(Vector, Vector, SplatVector); |
| 368 | } |
| 369 | |
| 370 | Value *BlockGenerator::getOperand(const Value *OldOperand, ValueMapT &BBMap, |
| 371 | ValueMapT *VectorMap) { |
| 372 | const Instruction *OpInst = dyn_cast<Instruction>(OldOperand); |
| 373 | |
| 374 | if (!OpInst) |
| 375 | return const_cast<Value*>(OldOperand); |
| 376 | |
| 377 | if (VectorMap && VectorMap->count(OldOperand)) |
| 378 | return (*VectorMap)[OldOperand]; |
| 379 | |
| 380 | // IVS and Parameters. |
| 381 | if (VMap.count(OldOperand)) { |
| 382 | Value *NewOperand = VMap[OldOperand]; |
| 383 | |
| 384 | // Insert a cast if types are different |
| 385 | if (OldOperand->getType()->getScalarSizeInBits() |
| 386 | < NewOperand->getType()->getScalarSizeInBits()) |
| 387 | NewOperand = Builder.CreateTruncOrBitCast(NewOperand, |
| 388 | OldOperand->getType()); |
| 389 | |
| 390 | return NewOperand; |
| 391 | } |
| 392 | |
| 393 | // Instructions calculated in the current BB. |
| 394 | if (BBMap.count(OldOperand)) { |
| 395 | return BBMap[OldOperand]; |
| 396 | } |
| 397 | |
| 398 | // Ignore instructions that are referencing ops in the old BB. These |
| 399 | // instructions are unused. They where replace by new ones during |
| 400 | // createIndependentBlocks(). |
Tobias Grosser | 44d1695 | 2012-03-02 11:27:21 +0000 | [diff] [blame] | 401 | if (S.getRegion().contains(OpInst->getParent())) |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 402 | return NULL; |
| 403 | |
| 404 | return const_cast<Value*>(OldOperand); |
| 405 | } |
| 406 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 407 | Type *BlockGenerator::getVectorPtrTy(const Value *Val, int Width) { |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 408 | PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); |
| 409 | assert(PointerTy && "PointerType expected"); |
| 410 | |
| 411 | Type *ScalarType = PointerTy->getElementType(); |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 412 | VectorType *VectorType = VectorType::get(ScalarType, Width); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 413 | |
| 414 | return PointerType::getUnqual(VectorType); |
| 415 | } |
| 416 | |
| 417 | Value *BlockGenerator::generateStrideOneLoad(const LoadInst *Load, |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 418 | ValueMapT &BBMap) { |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 419 | const Value *Pointer = Load->getPointerOperand(); |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 420 | Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 421 | Value *NewPointer = getOperand(Pointer, BBMap); |
| 422 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 423 | "vector_ptr"); |
| 424 | LoadInst *VecLoad = Builder.CreateLoad(VectorPtr, |
| 425 | Load->getName() + "_p_vec_full"); |
| 426 | if (!Aligned) |
| 427 | VecLoad->setAlignment(8); |
| 428 | |
| 429 | return VecLoad; |
| 430 | } |
| 431 | |
| 432 | Value *BlockGenerator::generateStrideZeroLoad(const LoadInst *Load, |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 433 | ValueMapT &BBMap) { |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 434 | const Value *Pointer = Load->getPointerOperand(); |
| 435 | Type *VectorPtrType = getVectorPtrTy(Pointer, 1); |
| 436 | Value *NewPointer = getOperand(Pointer, BBMap); |
| 437 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 438 | Load->getName() + "_p_vec_p"); |
| 439 | LoadInst *ScalarLoad= Builder.CreateLoad(VectorPtr, |
| 440 | Load->getName() + "_p_splat_one"); |
| 441 | |
| 442 | if (!Aligned) |
| 443 | ScalarLoad->setAlignment(8); |
| 444 | |
Tobias Grosser | e5b42325 | 2012-01-24 16:42:25 +0000 | [diff] [blame] | 445 | Constant *SplatVector = |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 446 | Constant::getNullValue(VectorType::get(Builder.getInt32Ty(), |
| 447 | getVectorWidth())); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 448 | |
| 449 | Value *VectorLoad = Builder.CreateShuffleVector(ScalarLoad, ScalarLoad, |
| 450 | SplatVector, |
| 451 | Load->getName() |
| 452 | + "_p_splat"); |
| 453 | return VectorLoad; |
| 454 | } |
| 455 | |
| 456 | Value *BlockGenerator::generateUnknownStrideLoad(const LoadInst *Load, |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 457 | VectorValueMapT &ScalarMaps) { |
| 458 | int VectorWidth = getVectorWidth(); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 459 | const Value *Pointer = Load->getPointerOperand(); |
| 460 | VectorType *VectorType = VectorType::get( |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 461 | dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 462 | |
| 463 | Value *Vector = UndefValue::get(VectorType); |
| 464 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 465 | for (int i = 0; i < VectorWidth; i++) { |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 466 | Value *NewPointer = getOperand(Pointer, ScalarMaps[i]); |
| 467 | Value *ScalarLoad = Builder.CreateLoad(NewPointer, |
| 468 | Load->getName() + "_p_scalar_"); |
| 469 | Vector = Builder.CreateInsertElement(Vector, ScalarLoad, |
| 470 | Builder.getInt32(i), |
| 471 | Load->getName() + "_p_vec_"); |
| 472 | } |
| 473 | |
| 474 | return Vector; |
| 475 | } |
| 476 | |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 477 | std::vector <Value*> BlockGenerator::getMemoryAccessIndex( |
| 478 | __isl_keep isl_map *AccessRelation, Value *BaseAddress) { |
| 479 | assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) |
| 480 | && "Only single dimensional access functions supported"); |
| 481 | |
| 482 | isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0); |
Tobias Grosser | 642c411 | 2012-03-02 11:27:25 +0000 | [diff] [blame^] | 483 | IslGenerator IslGen(Builder); |
| 484 | Value *OffsetValue = IslGen.generateIslPwAff(PwAff); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 485 | |
| 486 | PointerType *BaseAddressType = dyn_cast<PointerType>( |
| 487 | BaseAddress->getType()); |
| 488 | Type *ArrayTy = BaseAddressType->getElementType(); |
| 489 | Type *ArrayElementType = dyn_cast<ArrayType>(ArrayTy)->getElementType(); |
| 490 | OffsetValue = Builder.CreateSExtOrBitCast(OffsetValue, ArrayElementType); |
| 491 | |
| 492 | std::vector<Value*> IndexArray; |
| 493 | Value *NullValue = Constant::getNullValue(ArrayElementType); |
| 494 | IndexArray.push_back(NullValue); |
| 495 | IndexArray.push_back(OffsetValue); |
| 496 | return IndexArray; |
| 497 | } |
| 498 | |
| 499 | Value *BlockGenerator::getNewAccessOperand( |
| 500 | __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, const Value |
| 501 | *OldOperand, ValueMapT &BBMap) { |
| 502 | std::vector<Value*> IndexArray = getMemoryAccessIndex(NewAccessRelation, |
| 503 | BaseAddress); |
| 504 | Value *NewOperand = Builder.CreateGEP(BaseAddress, IndexArray, |
| 505 | "p_newarrayidx_"); |
| 506 | return NewOperand; |
| 507 | } |
| 508 | |
| 509 | Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst, |
| 510 | const Value *Pointer, |
| 511 | ValueMapT &BBMap ) { |
| 512 | MemoryAccess &Access = Statement.getAccessFor(Inst); |
| 513 | isl_map *CurrentAccessRelation = Access.getAccessRelation(); |
| 514 | isl_map *NewAccessRelation = Access.getNewAccessRelation(); |
| 515 | |
| 516 | assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) |
| 517 | && "Current and new access function use different spaces"); |
| 518 | |
| 519 | Value *NewPointer; |
| 520 | |
| 521 | if (!NewAccessRelation) { |
| 522 | NewPointer = getOperand(Pointer, BBMap); |
| 523 | } else { |
| 524 | Value *BaseAddress = const_cast<Value*>(Access.getBaseAddr()); |
| 525 | NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, Pointer, |
| 526 | BBMap); |
| 527 | } |
| 528 | |
| 529 | isl_map_free(CurrentAccessRelation); |
| 530 | isl_map_free(NewAccessRelation); |
| 531 | return NewPointer; |
| 532 | } |
| 533 | |
| 534 | Value *BlockGenerator::generateScalarLoad(const LoadInst *Load, |
| 535 | ValueMapT &BBMap) { |
| 536 | const Value *Pointer = Load->getPointerOperand(); |
| 537 | const Instruction *Inst = dyn_cast<Instruction>(Load); |
| 538 | Value *NewPointer = generateLocationAccessed(Inst, Pointer, BBMap); |
| 539 | Value *ScalarLoad = Builder.CreateLoad(NewPointer, |
| 540 | Load->getName() + "_p_scalar_"); |
| 541 | return ScalarLoad; |
| 542 | } |
| 543 | |
Tobias Grosser | fc1153f | 2012-03-02 11:27:15 +0000 | [diff] [blame] | 544 | void BlockGenerator::generateVectorLoad(const LoadInst *Load, |
| 545 | ValueMapT &VectorMap, |
| 546 | VectorValueMapT &ScalarMaps) { |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 547 | Value *NewLoad; |
| 548 | |
| 549 | MemoryAccess &Access = Statement.getAccessFor(Load); |
| 550 | |
| 551 | assert(ScatteringDomain && "No scattering domain available"); |
| 552 | |
| 553 | if (Access.isStrideZero(isl_set_copy(ScatteringDomain))) |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 554 | NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 555 | else if (Access.isStrideOne(isl_set_copy(ScatteringDomain))) |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 556 | NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 557 | else |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 558 | NewLoad = generateUnknownStrideLoad(Load, ScalarMaps); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 559 | |
| 560 | VectorMap[Load] = NewLoad; |
| 561 | } |
| 562 | |
Tobias Grosser | 8b4bf8b | 2012-03-02 11:27:11 +0000 | [diff] [blame] | 563 | void BlockGenerator::copyVectorUnaryInst(const UnaryInstruction *Inst, |
| 564 | ValueMapT &BBMap, |
| 565 | ValueMapT &VectorMap) { |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 566 | int VectorWidth = getVectorWidth(); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 567 | Value *NewOperand = getOperand(Inst->getOperand(0), BBMap, &VectorMap); |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 568 | NewOperand = makeVectorOperand(NewOperand); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 569 | |
| 570 | assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); |
| 571 | |
| 572 | const CastInst *Cast = dyn_cast<CastInst>(Inst); |
| 573 | VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); |
| 574 | VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); |
| 575 | } |
| 576 | |
Tobias Grosser | 8b4bf8b | 2012-03-02 11:27:11 +0000 | [diff] [blame] | 577 | void BlockGenerator::copyVectorBinInst(const BinaryOperator *Inst, |
| 578 | ValueMapT &BBMap, ValueMapT &VectorMap) { |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 579 | Value *OpZero = Inst->getOperand(0); |
| 580 | Value *OpOne = Inst->getOperand(1); |
| 581 | |
| 582 | Value *NewOpZero, *NewOpOne; |
| 583 | NewOpZero = getOperand(OpZero, BBMap, &VectorMap); |
| 584 | NewOpOne = getOperand(OpOne, BBMap, &VectorMap); |
| 585 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 586 | NewOpZero = makeVectorOperand(NewOpZero); |
| 587 | NewOpOne = makeVectorOperand(NewOpOne); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 588 | |
| 589 | Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, |
| 590 | NewOpOne, |
| 591 | Inst->getName() + "p_vec"); |
| 592 | VectorMap[Inst] = NewInst; |
| 593 | } |
| 594 | |
| 595 | void BlockGenerator::copyVectorStore(const StoreInst *Store, ValueMapT &BBMap, |
| 596 | ValueMapT &VectorMap, |
Tobias Grosser | 8927a44 | 2012-03-02 11:27:05 +0000 | [diff] [blame] | 597 | VectorValueMapT &ScalarMaps) { |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 598 | int VectorWidth = getVectorWidth(); |
| 599 | |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 600 | MemoryAccess &Access = Statement.getAccessFor(Store); |
| 601 | |
| 602 | assert(ScatteringDomain && "No scattering domain available"); |
| 603 | |
| 604 | const Value *Pointer = Store->getPointerOperand(); |
| 605 | Value *Vector = getOperand(Store->getValueOperand(), BBMap, &VectorMap); |
| 606 | |
| 607 | if (Access.isStrideOne(isl_set_copy(ScatteringDomain))) { |
| 608 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); |
| 609 | Value *NewPointer = getOperand(Pointer, BBMap, &VectorMap); |
| 610 | |
| 611 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 612 | "vector_ptr"); |
| 613 | StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); |
| 614 | |
| 615 | if (!Aligned) |
| 616 | Store->setAlignment(8); |
| 617 | } else { |
| 618 | for (unsigned i = 0; i < ScalarMaps.size(); i++) { |
| 619 | Value *Scalar = Builder.CreateExtractElement(Vector, |
| 620 | Builder.getInt32(i)); |
| 621 | Value *NewPointer = getOperand(Pointer, ScalarMaps[i]); |
| 622 | Builder.CreateStore(Scalar, NewPointer); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap) { |
| 628 | Instruction *NewInst = Inst->clone(); |
| 629 | |
| 630 | // Replace old operands with the new ones. |
| 631 | for (Instruction::const_op_iterator OI = Inst->op_begin(), |
| 632 | OE = Inst->op_end(); OI != OE; ++OI) { |
| 633 | Value *OldOperand = *OI; |
| 634 | Value *NewOperand = getOperand(OldOperand, BBMap); |
| 635 | |
| 636 | if (!NewOperand) { |
| 637 | assert(!isa<StoreInst>(NewInst) |
| 638 | && "Store instructions are always needed!"); |
| 639 | delete NewInst; |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | NewInst->replaceUsesOfWith(OldOperand, NewOperand); |
| 644 | } |
| 645 | |
| 646 | Builder.Insert(NewInst); |
| 647 | BBMap[Inst] = NewInst; |
| 648 | |
| 649 | if (!NewInst->getType()->isVoidTy()) |
| 650 | NewInst->setName("p_" + Inst->getName()); |
| 651 | } |
| 652 | |
| 653 | bool BlockGenerator::hasVectorOperands(const Instruction *Inst, |
| 654 | ValueMapT &VectorMap) { |
| 655 | for (Instruction::const_op_iterator OI = Inst->op_begin(), |
| 656 | OE = Inst->op_end(); OI != OE; ++OI) |
| 657 | if (VectorMap.count(*OI)) |
| 658 | return true; |
| 659 | return false; |
| 660 | } |
| 661 | |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 662 | int BlockGenerator::getVectorWidth() { |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 663 | return ValueMaps.size(); |
| 664 | } |
| 665 | |
| 666 | bool BlockGenerator::isVectorBlock() { |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 667 | return getVectorWidth() > 1; |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Tobias Grosser | 32152cb | 2012-03-02 11:27:18 +0000 | [diff] [blame] | 670 | void BlockGenerator::copyVectorInstruction(const Instruction *Inst, |
| 671 | ValueMapT &VectorMap, |
| 672 | VectorValueMapT &ScalarMaps) { |
| 673 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
| 674 | generateVectorLoad(Load, VectorMap, ScalarMaps); |
| 675 | return; |
| 676 | } |
| 677 | |
| 678 | if (hasVectorOperands(Inst, VectorMap)) { |
| 679 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
| 680 | copyVectorStore(Store, ScalarMaps[0], VectorMap, ScalarMaps); |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { |
| 685 | copyVectorUnaryInst(Unary, ScalarMaps[0], VectorMap); |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { |
| 690 | copyVectorBinInst(Binary, ScalarMaps[0], VectorMap); |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | llvm_unreachable("Cannot issue vector code for this instruction"); |
| 695 | } |
| 696 | |
| 697 | for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) |
| 698 | copyInstScalar(Inst, ScalarMaps[VectorLane]); |
| 699 | } |
| 700 | |
Tobias Grosser | fc1153f | 2012-03-02 11:27:15 +0000 | [diff] [blame] | 701 | void BlockGenerator::copyInstruction(const Instruction *Inst, |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 702 | ValueMapT &VectorMap, |
Tobias Grosser | fc1153f | 2012-03-02 11:27:15 +0000 | [diff] [blame] | 703 | VectorValueMapT &ScalarMaps) { |
Tobias Grosser | b35d9c1 | 2012-03-02 11:27:08 +0000 | [diff] [blame] | 704 | // Terminator instructions control the control flow. They are explicitly |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 705 | // expressed in the clast and do not need to be copied. |
| 706 | if (Inst->isTerminator()) |
| 707 | return; |
| 708 | |
| 709 | if (isVectorBlock()) { |
Tobias Grosser | 32152cb | 2012-03-02 11:27:18 +0000 | [diff] [blame] | 710 | copyVectorInstruction(Inst, VectorMap, ScalarMaps); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 711 | return; |
| 712 | } |
| 713 | |
Tobias Grosser | fc1153f | 2012-03-02 11:27:15 +0000 | [diff] [blame] | 714 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
| 715 | ScalarMaps[0][Load] = generateScalarLoad(Load, ScalarMaps[0]); |
| 716 | return; |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Tobias Grosser | fc1153f | 2012-03-02 11:27:15 +0000 | [diff] [blame] | 719 | copyInstScalar(Inst, ScalarMaps[0]); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 720 | } |
| 721 | |
Tobias Grosser | 8412cda | 2012-03-02 11:26:55 +0000 | [diff] [blame] | 722 | void BlockGenerator::copyBB() { |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 723 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 724 | BasicBlock *CopyBB = SplitBlock(Builder.GetInsertBlock(), |
| 725 | Builder.GetInsertPoint(), P); |
Tobias Grosser | b61e631 | 2012-02-15 09:58:46 +0000 | [diff] [blame] | 726 | CopyBB->setName("polly.stmt." + BB->getName()); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 727 | Builder.SetInsertPoint(CopyBB->begin()); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 728 | |
| 729 | // Create two maps that store the mapping from the original instructions of |
| 730 | // the old basic block to their copies in the new basic block. Those maps |
| 731 | // are basic block local. |
| 732 | // |
| 733 | // As vector code generation is supported there is one map for scalar values |
| 734 | // and one for vector values. |
| 735 | // |
| 736 | // In case we just do scalar code generation, the vectorMap is not used and |
| 737 | // the scalarMap has just one dimension, which contains the mapping. |
| 738 | // |
| 739 | // In case vector code generation is done, an instruction may either appear |
| 740 | // in the vector map once (as it is calculating >vectorwidth< values at a |
| 741 | // time. Or (if the values are calculated using scalar operations), it |
| 742 | // appears once in every dimension of the scalarMap. |
Tobias Grosser | f81a691e | 2012-03-02 11:27:02 +0000 | [diff] [blame] | 743 | VectorValueMapT ScalarBlockMap(getVectorWidth()); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 744 | ValueMapT VectorBlockMap; |
| 745 | |
| 746 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); |
| 747 | II != IE; ++II) |
Tobias Grosser | fc1153f | 2012-03-02 11:27:15 +0000 | [diff] [blame] | 748 | copyInstruction(II, VectorBlockMap, ScalarBlockMap); |
Tobias Grosser | 70e8cdb | 2012-01-24 16:42:21 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 751 | /// Class to generate LLVM-IR that calculates the value of a clast_expr. |
| 752 | class ClastExpCodeGen { |
| 753 | IRBuilder<> &Builder; |
| 754 | const CharMapT *IVS; |
| 755 | |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 756 | Value *codegen(const clast_name *e, Type *Ty); |
| 757 | Value *codegen(const clast_term *e, Type *Ty); |
| 758 | Value *codegen(const clast_binary *e, Type *Ty); |
| 759 | Value *codegen(const clast_reduction *r, Type *Ty); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 760 | public: |
| 761 | |
| 762 | // A generator for clast expressions. |
| 763 | // |
| 764 | // @param B The IRBuilder that defines where the code to calculate the |
| 765 | // clast expressions should be inserted. |
| 766 | // @param IVMAP A Map that translates strings describing the induction |
| 767 | // variables to the Values* that represent these variables |
| 768 | // on the LLVM side. |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 769 | ClastExpCodeGen(IRBuilder<> &B, CharMapT *IVMap); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 770 | |
| 771 | // Generates code to calculate a given clast expression. |
| 772 | // |
| 773 | // @param e The expression to calculate. |
| 774 | // @return The Value that holds the result. |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 775 | Value *codegen(const clast_expr *e, Type *Ty); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 776 | |
| 777 | // @brief Reset the CharMap. |
| 778 | // |
| 779 | // This function is called to reset the CharMap to new one, while generating |
| 780 | // OpenMP code. |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 781 | void setIVS(CharMapT *IVSNew); |
| 782 | }; |
| 783 | |
| 784 | Value *ClastExpCodeGen::codegen(const clast_name *e, Type *Ty) { |
| 785 | CharMapT::const_iterator I = IVS->find(e->name); |
| 786 | |
| 787 | assert(I != IVS->end() && "Clast name not found"); |
| 788 | |
| 789 | return Builder.CreateSExtOrBitCast(I->second, Ty); |
| 790 | } |
| 791 | |
| 792 | Value *ClastExpCodeGen::codegen(const clast_term *e, Type *Ty) { |
| 793 | APInt a = APInt_from_MPZ(e->val); |
| 794 | |
| 795 | Value *ConstOne = ConstantInt::get(Builder.getContext(), a); |
| 796 | ConstOne = Builder.CreateSExtOrBitCast(ConstOne, Ty); |
| 797 | |
| 798 | if (!e->var) |
| 799 | return ConstOne; |
| 800 | |
| 801 | Value *var = codegen(e->var, Ty); |
| 802 | return Builder.CreateMul(ConstOne, var); |
| 803 | } |
| 804 | |
| 805 | Value *ClastExpCodeGen::codegen(const clast_binary *e, Type *Ty) { |
| 806 | Value *LHS = codegen(e->LHS, Ty); |
| 807 | |
| 808 | APInt RHS_AP = APInt_from_MPZ(e->RHS); |
| 809 | |
| 810 | Value *RHS = ConstantInt::get(Builder.getContext(), RHS_AP); |
| 811 | RHS = Builder.CreateSExtOrBitCast(RHS, Ty); |
| 812 | |
| 813 | switch (e->type) { |
| 814 | case clast_bin_mod: |
| 815 | return Builder.CreateSRem(LHS, RHS); |
| 816 | case clast_bin_fdiv: |
| 817 | { |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 818 | // floord(n,d) ((n < 0) ? (n - d + 1) : n) / d |
Tobias Grosser | 906eafe | 2012-02-16 09:56:10 +0000 | [diff] [blame] | 819 | Value *One = ConstantInt::get(Ty, 1); |
| 820 | Value *Zero = ConstantInt::get(Ty, 0); |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 821 | Value *Sum1 = Builder.CreateSub(LHS, RHS); |
| 822 | Value *Sum2 = Builder.CreateAdd(Sum1, One); |
| 823 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero); |
| 824 | Value *Dividend = Builder.CreateSelect(isNegative, Sum2, LHS); |
| 825 | return Builder.CreateSDiv(Dividend, RHS); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 826 | } |
| 827 | case clast_bin_cdiv: |
| 828 | { |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 829 | // ceild(n,d) ((n < 0) ? n : (n + d - 1)) / d |
| 830 | Value *One = ConstantInt::get(Ty, 1); |
Tobias Grosser | 906eafe | 2012-02-16 09:56:10 +0000 | [diff] [blame] | 831 | Value *Zero = ConstantInt::get(Ty, 0); |
Tobias Grosser | 9a44b97 | 2012-02-16 14:13:19 +0000 | [diff] [blame] | 832 | Value *Sum1 = Builder.CreateAdd(LHS, RHS); |
| 833 | Value *Sum2 = Builder.CreateSub(Sum1, One); |
| 834 | Value *isNegative = Builder.CreateICmpSLT(LHS, Zero); |
| 835 | Value *Dividend = Builder.CreateSelect(isNegative, LHS, Sum2); |
| 836 | return Builder.CreateSDiv(Dividend, RHS); |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 837 | } |
| 838 | case clast_bin_div: |
| 839 | return Builder.CreateSDiv(LHS, RHS); |
| 840 | }; |
| 841 | |
| 842 | llvm_unreachable("Unknown clast binary expression type"); |
| 843 | } |
| 844 | |
| 845 | Value *ClastExpCodeGen::codegen(const clast_reduction *r, Type *Ty) { |
| 846 | assert(( r->type == clast_red_min |
| 847 | || r->type == clast_red_max |
| 848 | || r->type == clast_red_sum) |
| 849 | && "Clast reduction type not supported"); |
| 850 | Value *old = codegen(r->elts[0], Ty); |
| 851 | |
| 852 | for (int i=1; i < r->n; ++i) { |
| 853 | Value *exprValue = codegen(r->elts[i], Ty); |
| 854 | |
| 855 | switch (r->type) { |
| 856 | case clast_red_min: |
| 857 | { |
| 858 | Value *cmp = Builder.CreateICmpSLT(old, exprValue); |
| 859 | old = Builder.CreateSelect(cmp, old, exprValue); |
| 860 | break; |
| 861 | } |
| 862 | case clast_red_max: |
| 863 | { |
| 864 | Value *cmp = Builder.CreateICmpSGT(old, exprValue); |
| 865 | old = Builder.CreateSelect(cmp, old, exprValue); |
| 866 | break; |
| 867 | } |
| 868 | case clast_red_sum: |
| 869 | old = Builder.CreateAdd(old, exprValue); |
| 870 | break; |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 871 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Tobias Grosser | bb137e3 | 2012-01-24 16:42:28 +0000 | [diff] [blame] | 874 | return old; |
| 875 | } |
| 876 | |
| 877 | ClastExpCodeGen::ClastExpCodeGen(IRBuilder<> &B, CharMapT *IVMap) |
| 878 | : Builder(B), IVS(IVMap) {} |
| 879 | |
| 880 | Value *ClastExpCodeGen::codegen(const clast_expr *e, Type *Ty) { |
| 881 | switch(e->type) { |
| 882 | case clast_expr_name: |
| 883 | return codegen((const clast_name *)e, Ty); |
| 884 | case clast_expr_term: |
| 885 | return codegen((const clast_term *)e, Ty); |
| 886 | case clast_expr_bin: |
| 887 | return codegen((const clast_binary *)e, Ty); |
| 888 | case clast_expr_red: |
| 889 | return codegen((const clast_reduction *)e, Ty); |
| 890 | } |
| 891 | |
| 892 | llvm_unreachable("Unknown clast expression!"); |
| 893 | } |
| 894 | |
| 895 | void ClastExpCodeGen::setIVS(CharMapT *IVSNew) { |
| 896 | IVS = IVSNew; |
| 897 | } |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 898 | |
| 899 | class ClastStmtCodeGen { |
| 900 | // The Scop we code generate. |
| 901 | Scop *S; |
| 902 | ScalarEvolution &SE; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 903 | DominatorTree *DT; |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 904 | ScopDetection *SD; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 905 | Dependences *DP; |
| 906 | TargetData *TD; |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 907 | Pass *P; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 908 | |
| 909 | // The Builder specifies the current location to code generate at. |
| 910 | IRBuilder<> &Builder; |
| 911 | |
| 912 | // Map the Values from the old code to their counterparts in the new code. |
| 913 | ValueMapT ValueMap; |
| 914 | |
| 915 | // clastVars maps from the textual representation of a clast variable to its |
| 916 | // current *Value. clast variables are scheduling variables, original |
| 917 | // induction variables or parameters. They are used either in loop bounds or |
| 918 | // to define the statement instance that is executed. |
| 919 | // |
| 920 | // for (s = 0; s < n + 3; ++i) |
| 921 | // for (t = s; t < m; ++j) |
| 922 | // Stmt(i = s + 3 * m, j = t); |
| 923 | // |
| 924 | // {s,t,i,j,n,m} is the set of clast variables in this clast. |
| 925 | CharMapT *clastVars; |
| 926 | |
| 927 | // Codegenerator for clast expressions. |
| 928 | ClastExpCodeGen ExpGen; |
| 929 | |
| 930 | // Do we currently generate parallel code? |
| 931 | bool parallelCodeGeneration; |
| 932 | |
| 933 | std::vector<std::string> parallelLoops; |
| 934 | |
| 935 | public: |
| 936 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 937 | const std::vector<std::string> &getParallelLoops(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 938 | |
| 939 | protected: |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 940 | void codegen(const clast_assignment *a); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 941 | |
| 942 | void codegen(const clast_assignment *a, ScopStmt *Statement, |
| 943 | unsigned Dimension, int vectorDim, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 944 | std::vector<ValueMapT> *VectorVMap = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 945 | |
| 946 | void codegenSubstitutions(const clast_stmt *Assignment, |
| 947 | ScopStmt *Statement, int vectorDim = 0, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 948 | std::vector<ValueMapT> *VectorVMap = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 949 | |
| 950 | void codegen(const clast_user_stmt *u, std::vector<Value*> *IVS = NULL, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 951 | const char *iterator = NULL, isl_set *scatteringDomain = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 952 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 953 | void codegen(const clast_block *b); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 954 | |
| 955 | /// @brief Create a classical sequential loop. |
Tobias Grosser | 545bc31 | 2011-12-06 10:48:27 +0000 | [diff] [blame] | 956 | void codegenForSequential(const clast_for *f, Value *LowerBound = 0, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 957 | Value *UpperBound = 0); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 958 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 959 | /// @brief Add a new definition of an openmp subfunction. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 960 | Function *addOpenMPSubfunction(Module *M); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 961 | |
| 962 | /// @brief Add values to the OpenMP structure. |
| 963 | /// |
| 964 | /// Create the subfunction structure and add the values from the list. |
| 965 | Value *addValuesToOpenMPStruct(SetVector<Value*> OMPDataVals, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 966 | Function *SubFunction); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 967 | |
| 968 | /// @brief Create OpenMP structure values. |
| 969 | /// |
| 970 | /// Create a list of values that has to be stored into the subfuncition |
| 971 | /// structure. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 972 | SetVector<Value*> createOpenMPStructValues(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 973 | |
| 974 | /// @brief Extract the values from the subfunction parameter. |
| 975 | /// |
| 976 | /// Extract the values from the subfunction parameter and update the clast |
| 977 | /// variables to point to the new values. |
| 978 | void extractValuesFromOpenMPStruct(CharMapT *clastVarsOMP, |
| 979 | SetVector<Value*> OMPDataVals, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 980 | Value *userContext); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 981 | |
| 982 | /// @brief Add body to the subfunction. |
| 983 | void addOpenMPSubfunctionBody(Function *FN, const clast_for *f, |
| 984 | Value *structData, |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 985 | SetVector<Value*> OMPDataVals); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 986 | |
| 987 | /// @brief Create an OpenMP parallel for loop. |
| 988 | /// |
| 989 | /// This loop reflects a loop as if it would have been created by an OpenMP |
| 990 | /// statement. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 991 | void codegenForOpenMP(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 992 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 993 | bool isInnermostLoop(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 994 | |
| 995 | /// @brief Get the number of loop iterations for this loop. |
| 996 | /// @param f The clast for loop to check. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 997 | int getNumberOfIterations(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 998 | |
| 999 | /// @brief Create vector instructions for this loop. |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1000 | void codegenForVector(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1001 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1002 | void codegen(const clast_for *f); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1003 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1004 | Value *codegen(const clast_equation *eq); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1005 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1006 | void codegen(const clast_guard *g); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1007 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1008 | void codegen(const clast_stmt *stmt); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1009 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1010 | void addParameters(const CloogNames *names); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1011 | |
| 1012 | public: |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1013 | void codegen(const clast_root *r); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1014 | |
| 1015 | ClastStmtCodeGen(Scop *scop, ScalarEvolution &se, DominatorTree *dt, |
Hongbin Zheng | 94c5df1 | 2011-05-06 02:38:20 +0000 | [diff] [blame] | 1016 | ScopDetection *sd, Dependences *dp, TargetData *td, |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1017 | IRBuilder<> &B, Pass *P); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1018 | }; |
| 1019 | } |
| 1020 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1021 | const std::vector<std::string> &ClastStmtCodeGen::getParallelLoops() { |
| 1022 | return parallelLoops; |
| 1023 | } |
| 1024 | |
| 1025 | void ClastStmtCodeGen::codegen(const clast_assignment *a) { |
| 1026 | Value *V= ExpGen.codegen(a->RHS, TD->getIntPtrType(Builder.getContext())); |
| 1027 | (*clastVars)[a->LHS] = V; |
| 1028 | } |
| 1029 | |
| 1030 | void ClastStmtCodeGen::codegen(const clast_assignment *a, ScopStmt *Statement, |
| 1031 | unsigned Dimension, int vectorDim, |
| 1032 | std::vector<ValueMapT> *VectorVMap) { |
| 1033 | Value *RHS = ExpGen.codegen(a->RHS, |
| 1034 | TD->getIntPtrType(Builder.getContext())); |
| 1035 | |
| 1036 | assert(!a->LHS && "Statement assignments do not have left hand side"); |
| 1037 | const PHINode *PN; |
| 1038 | PN = Statement->getInductionVariableForDimension(Dimension); |
| 1039 | const Value *V = PN; |
| 1040 | |
| 1041 | if (VectorVMap) |
| 1042 | (*VectorVMap)[vectorDim][V] = RHS; |
| 1043 | |
| 1044 | ValueMap[V] = RHS; |
| 1045 | } |
| 1046 | |
| 1047 | void ClastStmtCodeGen::codegenSubstitutions(const clast_stmt *Assignment, |
| 1048 | ScopStmt *Statement, int vectorDim, |
| 1049 | std::vector<ValueMapT> *VectorVMap) { |
| 1050 | int Dimension = 0; |
| 1051 | |
| 1052 | while (Assignment) { |
| 1053 | assert(CLAST_STMT_IS_A(Assignment, stmt_ass) |
| 1054 | && "Substitions are expected to be assignments"); |
| 1055 | codegen((const clast_assignment *)Assignment, Statement, Dimension, |
| 1056 | vectorDim, VectorVMap); |
| 1057 | Assignment = Assignment->next; |
| 1058 | Dimension++; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | void ClastStmtCodeGen::codegen(const clast_user_stmt *u, |
| 1063 | std::vector<Value*> *IVS , const char *iterator, |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 1064 | isl_set *Domain) { |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1065 | ScopStmt *Statement = (ScopStmt *)u->statement->usr; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (u->substitutions) |
| 1068 | codegenSubstitutions(u->substitutions, Statement); |
| 1069 | |
| 1070 | int vectorDimensions = IVS ? IVS->size() : 1; |
| 1071 | |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 1072 | VectorValueMapT VectorMap(vectorDimensions); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1073 | |
| 1074 | if (IVS) { |
| 1075 | assert (u->substitutions && "Substitutions expected!"); |
| 1076 | int i = 0; |
| 1077 | for (std::vector<Value*>::iterator II = IVS->begin(), IE = IVS->end(); |
| 1078 | II != IE; ++II) { |
| 1079 | (*clastVars)[iterator] = *II; |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 1080 | codegenSubstitutions(u->substitutions, Statement, i, &VectorMap); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1081 | i++; |
| 1082 | } |
| 1083 | } |
| 1084 | |
Tobias Grosser | 14bcbd5 | 2012-03-02 11:26:52 +0000 | [diff] [blame] | 1085 | BlockGenerator::generate(Builder, ValueMap, VectorMap, *Statement, Domain, P); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | void ClastStmtCodeGen::codegen(const clast_block *b) { |
| 1089 | if (b->body) |
| 1090 | codegen(b->body); |
| 1091 | } |
| 1092 | |
| 1093 | void ClastStmtCodeGen::codegenForSequential(const clast_for *f, |
| 1094 | Value *LowerBound, |
| 1095 | Value *UpperBound) { |
| 1096 | APInt Stride; |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1097 | BasicBlock *AfterBB; |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1098 | Type *IntPtrTy; |
| 1099 | |
| 1100 | Stride = APInt_from_MPZ(f->stride); |
| 1101 | IntPtrTy = TD->getIntPtrType(Builder.getContext()); |
| 1102 | |
| 1103 | // The value of lowerbound and upperbound will be supplied, if this |
| 1104 | // function is called while generating OpenMP code. Otherwise get |
| 1105 | // the values. |
| 1106 | assert(!!LowerBound == !!UpperBound && "Either give both bounds or none"); |
| 1107 | |
| 1108 | if (LowerBound == 0) { |
| 1109 | LowerBound = ExpGen.codegen(f->LB, IntPtrTy); |
| 1110 | UpperBound = ExpGen.codegen(f->UB, IntPtrTy); |
| 1111 | } |
| 1112 | |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1113 | Value *IV = createLoop(&Builder, LowerBound, UpperBound, Stride, DT, P, |
| 1114 | &AfterBB); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1115 | |
| 1116 | // Add loop iv to symbols. |
| 1117 | (*clastVars)[f->iterator] = IV; |
| 1118 | |
| 1119 | if (f->body) |
| 1120 | codegen(f->body); |
| 1121 | |
| 1122 | // Loop is finished, so remove its iv from the live symbols. |
| 1123 | clastVars->erase(f->iterator); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1124 | Builder.SetInsertPoint(AfterBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | Function *ClastStmtCodeGen::addOpenMPSubfunction(Module *M) { |
| 1128 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 1129 | std::vector<Type*> Arguments(1, Builder.getInt8PtrTy()); |
| 1130 | FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false); |
| 1131 | Function *FN = Function::Create(FT, Function::InternalLinkage, |
| 1132 | F->getName() + ".omp_subfn", M); |
| 1133 | // Do not run any polly pass on the new function. |
| 1134 | SD->markFunctionAsInvalid(FN); |
| 1135 | |
| 1136 | Function::arg_iterator AI = FN->arg_begin(); |
| 1137 | AI->setName("omp.userContext"); |
| 1138 | |
| 1139 | return FN; |
| 1140 | } |
| 1141 | |
| 1142 | Value *ClastStmtCodeGen::addValuesToOpenMPStruct(SetVector<Value*> OMPDataVals, |
| 1143 | Function *SubFunction) { |
| 1144 | std::vector<Type*> structMembers; |
| 1145 | |
| 1146 | // Create the structure. |
| 1147 | for (unsigned i = 0; i < OMPDataVals.size(); i++) |
| 1148 | structMembers.push_back(OMPDataVals[i]->getType()); |
| 1149 | |
| 1150 | StructType *structTy = StructType::get(Builder.getContext(), |
| 1151 | structMembers); |
| 1152 | // Store the values into the structure. |
| 1153 | Value *structData = Builder.CreateAlloca(structTy, 0, "omp.userContext"); |
| 1154 | for (unsigned i = 0; i < OMPDataVals.size(); i++) { |
| 1155 | Value *storeAddr = Builder.CreateStructGEP(structData, i); |
| 1156 | Builder.CreateStore(OMPDataVals[i], storeAddr); |
| 1157 | } |
| 1158 | |
| 1159 | return structData; |
| 1160 | } |
| 1161 | |
| 1162 | SetVector<Value*> ClastStmtCodeGen::createOpenMPStructValues() { |
| 1163 | SetVector<Value*> OMPDataVals; |
| 1164 | |
| 1165 | // Push the clast variables available in the clastVars. |
| 1166 | for (CharMapT::iterator I = clastVars->begin(), E = clastVars->end(); |
| 1167 | I != E; I++) |
| 1168 | OMPDataVals.insert(I->second); |
| 1169 | |
| 1170 | // Push the base addresses of memory references. |
| 1171 | for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) { |
| 1172 | ScopStmt *Stmt = *SI; |
| 1173 | for (SmallVector<MemoryAccess*, 8>::iterator I = Stmt->memacc_begin(), |
| 1174 | E = Stmt->memacc_end(); I != E; ++I) { |
| 1175 | Value *BaseAddr = const_cast<Value*>((*I)->getBaseAddr()); |
| 1176 | OMPDataVals.insert((BaseAddr)); |
| 1177 | } |
| 1178 | } |
| 1179 | |
| 1180 | return OMPDataVals; |
| 1181 | } |
| 1182 | |
| 1183 | void ClastStmtCodeGen::extractValuesFromOpenMPStruct(CharMapT *clastVarsOMP, |
| 1184 | SetVector<Value*> OMPDataVals, Value *userContext) { |
| 1185 | // Extract the clast variables. |
| 1186 | unsigned i = 0; |
| 1187 | for (CharMapT::iterator I = clastVars->begin(), E = clastVars->end(); |
| 1188 | I != E; I++) { |
| 1189 | Value *loadAddr = Builder.CreateStructGEP(userContext, i); |
| 1190 | (*clastVarsOMP)[I->first] = Builder.CreateLoad(loadAddr); |
| 1191 | i++; |
| 1192 | } |
| 1193 | |
| 1194 | // Extract the base addresses of memory references. |
| 1195 | for (unsigned j = i; j < OMPDataVals.size(); j++) { |
| 1196 | Value *loadAddr = Builder.CreateStructGEP(userContext, j); |
| 1197 | Value *baseAddr = OMPDataVals[j]; |
| 1198 | ValueMap[baseAddr] = Builder.CreateLoad(loadAddr); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | void ClastStmtCodeGen::addOpenMPSubfunctionBody(Function *FN, |
| 1203 | const clast_for *f, |
| 1204 | Value *structData, |
| 1205 | SetVector<Value*> OMPDataVals) { |
| 1206 | Module *M = Builder.GetInsertBlock()->getParent()->getParent(); |
| 1207 | LLVMContext &Context = FN->getContext(); |
| 1208 | IntegerType *intPtrTy = TD->getIntPtrType(Context); |
| 1209 | |
| 1210 | // Store the previous basic block. |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1211 | BasicBlock::iterator PrevInsertPoint = Builder.GetInsertPoint(); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1212 | BasicBlock *PrevBB = Builder.GetInsertBlock(); |
| 1213 | |
| 1214 | // Create basic blocks. |
| 1215 | BasicBlock *HeaderBB = BasicBlock::Create(Context, "omp.setup", FN); |
| 1216 | BasicBlock *ExitBB = BasicBlock::Create(Context, "omp.exit", FN); |
| 1217 | BasicBlock *checkNextBB = BasicBlock::Create(Context, "omp.checkNext", FN); |
| 1218 | BasicBlock *loadIVBoundsBB = BasicBlock::Create(Context, "omp.loadIVBounds", |
| 1219 | FN); |
| 1220 | |
| 1221 | DT->addNewBlock(HeaderBB, PrevBB); |
| 1222 | DT->addNewBlock(ExitBB, HeaderBB); |
| 1223 | DT->addNewBlock(checkNextBB, HeaderBB); |
| 1224 | DT->addNewBlock(loadIVBoundsBB, HeaderBB); |
| 1225 | |
| 1226 | // Fill up basic block HeaderBB. |
| 1227 | Builder.SetInsertPoint(HeaderBB); |
| 1228 | Value *lowerBoundPtr = Builder.CreateAlloca(intPtrTy, 0, |
| 1229 | "omp.lowerBoundPtr"); |
| 1230 | Value *upperBoundPtr = Builder.CreateAlloca(intPtrTy, 0, |
| 1231 | "omp.upperBoundPtr"); |
| 1232 | Value *userContext = Builder.CreateBitCast(FN->arg_begin(), |
| 1233 | structData->getType(), |
| 1234 | "omp.userContext"); |
| 1235 | |
| 1236 | CharMapT clastVarsOMP; |
| 1237 | extractValuesFromOpenMPStruct(&clastVarsOMP, OMPDataVals, userContext); |
| 1238 | |
| 1239 | Builder.CreateBr(checkNextBB); |
| 1240 | |
| 1241 | // Add code to check if another set of iterations will be executed. |
| 1242 | Builder.SetInsertPoint(checkNextBB); |
| 1243 | Function *runtimeNextFunction = M->getFunction("GOMP_loop_runtime_next"); |
| 1244 | Value *ret1 = Builder.CreateCall2(runtimeNextFunction, |
| 1245 | lowerBoundPtr, upperBoundPtr); |
| 1246 | Value *hasNextSchedule = Builder.CreateTrunc(ret1, Builder.getInt1Ty(), |
| 1247 | "omp.hasNextScheduleBlock"); |
| 1248 | Builder.CreateCondBr(hasNextSchedule, loadIVBoundsBB, ExitBB); |
| 1249 | |
| 1250 | // Add code to to load the iv bounds for this set of iterations. |
| 1251 | Builder.SetInsertPoint(loadIVBoundsBB); |
| 1252 | Value *lowerBound = Builder.CreateLoad(lowerBoundPtr, "omp.lowerBound"); |
| 1253 | Value *upperBound = Builder.CreateLoad(upperBoundPtr, "omp.upperBound"); |
| 1254 | |
| 1255 | // Subtract one as the upper bound provided by openmp is a < comparison |
| 1256 | // whereas the codegenForSequential function creates a <= comparison. |
| 1257 | upperBound = Builder.CreateSub(upperBound, ConstantInt::get(intPtrTy, 1), |
| 1258 | "omp.upperBoundAdjusted"); |
| 1259 | |
| 1260 | // Use clastVarsOMP during code generation of the OpenMP subfunction. |
| 1261 | CharMapT *oldClastVars = clastVars; |
| 1262 | clastVars = &clastVarsOMP; |
| 1263 | ExpGen.setIVS(&clastVarsOMP); |
| 1264 | |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1265 | Builder.CreateBr(checkNextBB); |
| 1266 | Builder.SetInsertPoint(--Builder.GetInsertPoint()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1267 | codegenForSequential(f, lowerBound, upperBound); |
| 1268 | |
| 1269 | // Restore the old clastVars. |
| 1270 | clastVars = oldClastVars; |
| 1271 | ExpGen.setIVS(oldClastVars); |
| 1272 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1273 | // Add code to terminate this openmp subfunction. |
| 1274 | Builder.SetInsertPoint(ExitBB); |
| 1275 | Function *endnowaitFunction = M->getFunction("GOMP_loop_end_nowait"); |
| 1276 | Builder.CreateCall(endnowaitFunction); |
| 1277 | Builder.CreateRetVoid(); |
| 1278 | |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1279 | // Restore the previous insert point. |
| 1280 | Builder.SetInsertPoint(PrevInsertPoint); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | void ClastStmtCodeGen::codegenForOpenMP(const clast_for *f) { |
| 1284 | Module *M = Builder.GetInsertBlock()->getParent()->getParent(); |
| 1285 | IntegerType *intPtrTy = TD->getIntPtrType(Builder.getContext()); |
| 1286 | |
| 1287 | Function *SubFunction = addOpenMPSubfunction(M); |
| 1288 | SetVector<Value*> OMPDataVals = createOpenMPStructValues(); |
| 1289 | Value *structData = addValuesToOpenMPStruct(OMPDataVals, SubFunction); |
| 1290 | |
| 1291 | addOpenMPSubfunctionBody(SubFunction, f, structData, OMPDataVals); |
| 1292 | |
| 1293 | // Create call for GOMP_parallel_loop_runtime_start. |
| 1294 | Value *subfunctionParam = Builder.CreateBitCast(structData, |
| 1295 | Builder.getInt8PtrTy(), |
| 1296 | "omp_data"); |
| 1297 | |
| 1298 | Value *numberOfThreads = Builder.getInt32(0); |
| 1299 | Value *lowerBound = ExpGen.codegen(f->LB, intPtrTy); |
| 1300 | Value *upperBound = ExpGen.codegen(f->UB, intPtrTy); |
| 1301 | |
| 1302 | // Add one as the upper bound provided by openmp is a < comparison |
| 1303 | // whereas the codegenForSequential function creates a <= comparison. |
| 1304 | upperBound = Builder.CreateAdd(upperBound, ConstantInt::get(intPtrTy, 1)); |
| 1305 | APInt APStride = APInt_from_MPZ(f->stride); |
| 1306 | Value *stride = ConstantInt::get(intPtrTy, |
| 1307 | APStride.zext(intPtrTy->getBitWidth())); |
| 1308 | |
| 1309 | SmallVector<Value *, 6> Arguments; |
| 1310 | Arguments.push_back(SubFunction); |
| 1311 | Arguments.push_back(subfunctionParam); |
| 1312 | Arguments.push_back(numberOfThreads); |
| 1313 | Arguments.push_back(lowerBound); |
| 1314 | Arguments.push_back(upperBound); |
| 1315 | Arguments.push_back(stride); |
| 1316 | |
| 1317 | Function *parallelStartFunction = |
| 1318 | M->getFunction("GOMP_parallel_loop_runtime_start"); |
| 1319 | Builder.CreateCall(parallelStartFunction, Arguments); |
| 1320 | |
| 1321 | // Create call to the subfunction. |
| 1322 | Builder.CreateCall(SubFunction, subfunctionParam); |
| 1323 | |
| 1324 | // Create call for GOMP_parallel_end. |
| 1325 | Function *FN = M->getFunction("GOMP_parallel_end"); |
| 1326 | Builder.CreateCall(FN); |
| 1327 | } |
| 1328 | |
| 1329 | bool ClastStmtCodeGen::isInnermostLoop(const clast_for *f) { |
| 1330 | const clast_stmt *stmt = f->body; |
| 1331 | |
| 1332 | while (stmt) { |
| 1333 | if (!CLAST_STMT_IS_A(stmt, stmt_user)) |
| 1334 | return false; |
| 1335 | |
| 1336 | stmt = stmt->next; |
| 1337 | } |
| 1338 | |
| 1339 | return true; |
| 1340 | } |
| 1341 | |
| 1342 | int ClastStmtCodeGen::getNumberOfIterations(const clast_for *f) { |
| 1343 | isl_set *loopDomain = isl_set_copy(isl_set_from_cloog_domain(f->domain)); |
| 1344 | isl_set *tmp = isl_set_copy(loopDomain); |
| 1345 | |
| 1346 | // Calculate a map similar to the identity map, but with the last input |
| 1347 | // and output dimension not related. |
| 1348 | // [i0, i1, i2, i3] -> [i0, i1, i2, o0] |
| 1349 | isl_space *Space = isl_set_get_space(loopDomain); |
| 1350 | Space = isl_space_drop_outputs(Space, |
| 1351 | isl_set_dim(loopDomain, isl_dim_set) - 2, 1); |
| 1352 | Space = isl_space_map_from_set(Space); |
| 1353 | isl_map *identity = isl_map_identity(Space); |
| 1354 | identity = isl_map_add_dims(identity, isl_dim_in, 1); |
| 1355 | identity = isl_map_add_dims(identity, isl_dim_out, 1); |
| 1356 | |
| 1357 | isl_map *map = isl_map_from_domain_and_range(tmp, loopDomain); |
| 1358 | map = isl_map_intersect(map, identity); |
| 1359 | |
| 1360 | isl_map *lexmax = isl_map_lexmax(isl_map_copy(map)); |
| 1361 | isl_map *lexmin = isl_map_lexmin(map); |
| 1362 | isl_map *sub = isl_map_sum(lexmax, isl_map_neg(lexmin)); |
| 1363 | |
| 1364 | isl_set *elements = isl_map_range(sub); |
| 1365 | |
| 1366 | if (!isl_set_is_singleton(elements)) { |
| 1367 | isl_set_free(elements); |
| 1368 | return -1; |
| 1369 | } |
| 1370 | |
| 1371 | isl_point *p = isl_set_sample_point(elements); |
| 1372 | |
| 1373 | isl_int v; |
| 1374 | isl_int_init(v); |
| 1375 | isl_point_get_coordinate(p, isl_dim_set, isl_set_n_dim(loopDomain) - 1, &v); |
| 1376 | int numberIterations = isl_int_get_si(v); |
| 1377 | isl_int_clear(v); |
| 1378 | isl_point_free(p); |
| 1379 | |
| 1380 | return (numberIterations) / isl_int_get_si(f->stride) + 1; |
| 1381 | } |
| 1382 | |
| 1383 | void ClastStmtCodeGen::codegenForVector(const clast_for *f) { |
| 1384 | DEBUG(dbgs() << "Vectorizing loop '" << f->iterator << "'\n";); |
| 1385 | int vectorWidth = getNumberOfIterations(f); |
| 1386 | |
| 1387 | Value *LB = ExpGen.codegen(f->LB, |
| 1388 | TD->getIntPtrType(Builder.getContext())); |
| 1389 | |
| 1390 | APInt Stride = APInt_from_MPZ(f->stride); |
| 1391 | IntegerType *LoopIVType = dyn_cast<IntegerType>(LB->getType()); |
| 1392 | Stride = Stride.zext(LoopIVType->getBitWidth()); |
| 1393 | Value *StrideValue = ConstantInt::get(LoopIVType, Stride); |
| 1394 | |
| 1395 | std::vector<Value*> IVS(vectorWidth); |
| 1396 | IVS[0] = LB; |
| 1397 | |
| 1398 | for (int i = 1; i < vectorWidth; i++) |
| 1399 | IVS[i] = Builder.CreateAdd(IVS[i-1], StrideValue, "p_vector_iv"); |
| 1400 | |
| 1401 | isl_set *scatteringDomain = |
| 1402 | isl_set_copy(isl_set_from_cloog_domain(f->domain)); |
| 1403 | |
| 1404 | // Add loop iv to symbols. |
| 1405 | (*clastVars)[f->iterator] = LB; |
| 1406 | |
| 1407 | const clast_stmt *stmt = f->body; |
| 1408 | |
| 1409 | while (stmt) { |
| 1410 | codegen((const clast_user_stmt *)stmt, &IVS, f->iterator, |
| 1411 | scatteringDomain); |
| 1412 | stmt = stmt->next; |
| 1413 | } |
| 1414 | |
| 1415 | // Loop is finished, so remove its iv from the live symbols. |
| 1416 | isl_set_free(scatteringDomain); |
| 1417 | clastVars->erase(f->iterator); |
| 1418 | } |
| 1419 | |
| 1420 | void ClastStmtCodeGen::codegen(const clast_for *f) { |
Tobias Grosser | ce3f537 | 2012-03-02 11:26:42 +0000 | [diff] [blame] | 1421 | if ((Vector || OpenMP) && DP->isParallelFor(f)) { |
| 1422 | if (Vector && isInnermostLoop(f) && (-1 != getNumberOfIterations(f)) |
| 1423 | && (getNumberOfIterations(f) <= 16)) { |
| 1424 | codegenForVector(f); |
| 1425 | return; |
| 1426 | } |
| 1427 | |
| 1428 | if (OpenMP && !parallelCodeGeneration) { |
| 1429 | parallelCodeGeneration = true; |
| 1430 | parallelLoops.push_back(f->iterator); |
| 1431 | codegenForOpenMP(f); |
| 1432 | parallelCodeGeneration = false; |
| 1433 | return; |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | codegenForSequential(f); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | Value *ClastStmtCodeGen::codegen(const clast_equation *eq) { |
| 1441 | Value *LHS = ExpGen.codegen(eq->LHS, |
| 1442 | TD->getIntPtrType(Builder.getContext())); |
| 1443 | Value *RHS = ExpGen.codegen(eq->RHS, |
| 1444 | TD->getIntPtrType(Builder.getContext())); |
| 1445 | CmpInst::Predicate P; |
| 1446 | |
| 1447 | if (eq->sign == 0) |
| 1448 | P = ICmpInst::ICMP_EQ; |
| 1449 | else if (eq->sign > 0) |
| 1450 | P = ICmpInst::ICMP_SGE; |
| 1451 | else |
| 1452 | P = ICmpInst::ICMP_SLE; |
| 1453 | |
| 1454 | return Builder.CreateICmp(P, LHS, RHS); |
| 1455 | } |
| 1456 | |
| 1457 | void ClastStmtCodeGen::codegen(const clast_guard *g) { |
| 1458 | Function *F = Builder.GetInsertBlock()->getParent(); |
| 1459 | LLVMContext &Context = F->getContext(); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1460 | |
| 1461 | BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(), |
| 1462 | Builder.GetInsertPoint(), P); |
| 1463 | CondBB->setName("polly.cond"); |
| 1464 | BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), P); |
| 1465 | MergeBB->setName("polly.merge"); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1466 | BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1467 | |
| 1468 | DT->addNewBlock(ThenBB, CondBB); |
| 1469 | DT->changeImmediateDominator(MergeBB, CondBB); |
| 1470 | |
| 1471 | CondBB->getTerminator()->eraseFromParent(); |
| 1472 | |
| 1473 | Builder.SetInsertPoint(CondBB); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1474 | |
| 1475 | Value *Predicate = codegen(&(g->eq[0])); |
| 1476 | |
| 1477 | for (int i = 1; i < g->n; ++i) { |
| 1478 | Value *TmpPredicate = codegen(&(g->eq[i])); |
| 1479 | Predicate = Builder.CreateAnd(Predicate, TmpPredicate); |
| 1480 | } |
| 1481 | |
| 1482 | Builder.CreateCondBr(Predicate, ThenBB, MergeBB); |
| 1483 | Builder.SetInsertPoint(ThenBB); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1484 | Builder.CreateBr(MergeBB); |
| 1485 | Builder.SetInsertPoint(ThenBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1486 | |
| 1487 | codegen(g->then); |
Tobias Grosser | 62a3c96 | 2012-02-16 09:56:21 +0000 | [diff] [blame] | 1488 | |
| 1489 | Builder.SetInsertPoint(MergeBB->begin()); |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | void ClastStmtCodeGen::codegen(const clast_stmt *stmt) { |
| 1493 | if (CLAST_STMT_IS_A(stmt, stmt_root)) |
| 1494 | assert(false && "No second root statement expected"); |
| 1495 | else if (CLAST_STMT_IS_A(stmt, stmt_ass)) |
| 1496 | codegen((const clast_assignment *)stmt); |
| 1497 | else if (CLAST_STMT_IS_A(stmt, stmt_user)) |
| 1498 | codegen((const clast_user_stmt *)stmt); |
| 1499 | else if (CLAST_STMT_IS_A(stmt, stmt_block)) |
| 1500 | codegen((const clast_block *)stmt); |
| 1501 | else if (CLAST_STMT_IS_A(stmt, stmt_for)) |
| 1502 | codegen((const clast_for *)stmt); |
| 1503 | else if (CLAST_STMT_IS_A(stmt, stmt_guard)) |
| 1504 | codegen((const clast_guard *)stmt); |
| 1505 | |
| 1506 | if (stmt->next) |
| 1507 | codegen(stmt->next); |
| 1508 | } |
| 1509 | |
| 1510 | void ClastStmtCodeGen::addParameters(const CloogNames *names) { |
| 1511 | SCEVExpander Rewriter(SE, "polly"); |
| 1512 | |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1513 | int i = 0; |
| 1514 | for (Scop::param_iterator PI = S->param_begin(), PE = S->param_end(); |
| 1515 | PI != PE; ++PI) { |
| 1516 | assert(i < names->nb_parameters && "Not enough parameter names"); |
| 1517 | |
| 1518 | const SCEV *Param = *PI; |
| 1519 | Type *Ty = Param->getType(); |
| 1520 | |
| 1521 | Instruction *insertLocation = --(Builder.GetInsertBlock()->end()); |
| 1522 | Value *V = Rewriter.expandCodeFor(Param, Ty, insertLocation); |
| 1523 | (*clastVars)[names->parameters[i]] = V; |
| 1524 | |
| 1525 | ++i; |
| 1526 | } |
| 1527 | } |
| 1528 | |
| 1529 | void ClastStmtCodeGen::codegen(const clast_root *r) { |
| 1530 | clastVars = new CharMapT(); |
| 1531 | addParameters(r->names); |
| 1532 | ExpGen.setIVS(clastVars); |
| 1533 | |
| 1534 | parallelCodeGeneration = false; |
| 1535 | |
| 1536 | const clast_stmt *stmt = (const clast_stmt*) r; |
| 1537 | if (stmt->next) |
| 1538 | codegen(stmt->next); |
| 1539 | |
| 1540 | delete clastVars; |
| 1541 | } |
| 1542 | |
| 1543 | ClastStmtCodeGen::ClastStmtCodeGen(Scop *scop, ScalarEvolution &se, |
| 1544 | DominatorTree *dt, ScopDetection *sd, |
| 1545 | Dependences *dp, TargetData *td, |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1546 | IRBuilder<> &B, Pass *P) : |
| 1547 | S(scop), SE(se), DT(dt), SD(sd), DP(dp), TD(td), P(P), Builder(B), |
Tobias Grosser | 9bc5eb08 | 2012-01-24 16:42:32 +0000 | [diff] [blame] | 1548 | ExpGen(Builder, NULL) {} |
| 1549 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1550 | namespace { |
| 1551 | class CodeGeneration : public ScopPass { |
| 1552 | Region *region; |
| 1553 | Scop *S; |
| 1554 | DominatorTree *DT; |
| 1555 | ScalarEvolution *SE; |
| 1556 | ScopDetection *SD; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1557 | TargetData *TD; |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1558 | RegionInfo *RI; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1559 | |
| 1560 | std::vector<std::string> parallelLoops; |
| 1561 | |
| 1562 | public: |
| 1563 | static char ID; |
| 1564 | |
| 1565 | CodeGeneration() : ScopPass(ID) {} |
| 1566 | |
Tobias Grosser | b1c9599 | 2012-02-12 12:09:27 +0000 | [diff] [blame] | 1567 | // Add the declarations needed by the OpenMP function calls that we insert in |
| 1568 | // OpenMP mode. |
| 1569 | void addOpenMPDeclarations(Module *M) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1570 | { |
Tobias Grosser | d855cc5 | 2012-02-12 12:09:32 +0000 | [diff] [blame] | 1571 | IRBuilder<> Builder(M->getContext()); |
| 1572 | IntegerType *LongTy = TD->getIntPtrType(M->getContext()); |
| 1573 | |
| 1574 | llvm::GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1575 | |
| 1576 | if (!M->getFunction("GOMP_parallel_end")) { |
Tobias Grosser | d855cc5 | 2012-02-12 12:09:32 +0000 | [diff] [blame] | 1577 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); |
| 1578 | Function::Create(Ty, Linkage, "GOMP_parallel_end", M); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
| 1581 | if (!M->getFunction("GOMP_parallel_loop_runtime_start")) { |
Tobias Grosser | d855cc5 | 2012-02-12 12:09:32 +0000 | [diff] [blame] | 1582 | Type *Params[] = { |
| 1583 | PointerType::getUnqual(FunctionType::get(Builder.getVoidTy(), |
| 1584 | Builder.getInt8PtrTy(), |
| 1585 | false)), |
| 1586 | Builder.getInt8PtrTy(), |
| 1587 | Builder.getInt32Ty(), |
| 1588 | LongTy, |
| 1589 | LongTy, |
| 1590 | LongTy, |
| 1591 | }; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1592 | |
Tobias Grosser | d855cc5 | 2012-02-12 12:09:32 +0000 | [diff] [blame] | 1593 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false); |
| 1594 | Function::Create(Ty, Linkage, "GOMP_parallel_loop_runtime_start", M); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1595 | } |
| 1596 | |
| 1597 | if (!M->getFunction("GOMP_loop_runtime_next")) { |
Tobias Grosser | d855cc5 | 2012-02-12 12:09:32 +0000 | [diff] [blame] | 1598 | PointerType *LongPtrTy = PointerType::getUnqual(LongTy); |
| 1599 | Type *Params[] = { |
| 1600 | LongPtrTy, |
| 1601 | LongPtrTy, |
| 1602 | }; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1603 | |
Tobias Grosser | d855cc5 | 2012-02-12 12:09:32 +0000 | [diff] [blame] | 1604 | FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false); |
| 1605 | Function::Create(Ty, Linkage, "GOMP_loop_runtime_next", M); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | if (!M->getFunction("GOMP_loop_end_nowait")) { |
Tobias Grosser | d855cc5 | 2012-02-12 12:09:32 +0000 | [diff] [blame] | 1609 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); |
| 1610 | Function::Create(Ty, Linkage, "GOMP_loop_end_nowait", M); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1614 | // Split the entry edge of the region and generate a new basic block on this |
| 1615 | // edge. This function also updates ScopInfo and RegionInfo. |
| 1616 | // |
| 1617 | // @param region The region where the entry edge will be splitted. |
| 1618 | BasicBlock *splitEdgeAdvanced(Region *region) { |
| 1619 | BasicBlock *newBlock; |
| 1620 | BasicBlock *splitBlock; |
| 1621 | |
| 1622 | newBlock = SplitEdge(region->getEnteringBlock(), region->getEntry(), this); |
| 1623 | |
| 1624 | if (DT->dominates(region->getEntry(), newBlock)) { |
Tobias Grosser | cb47dfe | 2012-02-15 09:58:50 +0000 | [diff] [blame] | 1625 | BasicBlock *OldBlock = region->getEntry(); |
| 1626 | std::string OldName = OldBlock->getName(); |
| 1627 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1628 | // Update ScopInfo. |
| 1629 | for (Scop::iterator SI = S->begin(), SE = S->end(); SI != SE; ++SI) |
Tobias Grosser | f12cea4 | 2012-02-15 09:58:53 +0000 | [diff] [blame] | 1630 | if ((*SI)->getBasicBlock() == OldBlock) { |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1631 | (*SI)->setBasicBlock(newBlock); |
| 1632 | break; |
| 1633 | } |
| 1634 | |
| 1635 | // Update RegionInfo. |
Tobias Grosser | cb47dfe | 2012-02-15 09:58:50 +0000 | [diff] [blame] | 1636 | splitBlock = OldBlock; |
| 1637 | OldBlock->setName("polly.split"); |
| 1638 | newBlock->setName(OldName); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1639 | region->replaceEntry(newBlock); |
Tobias Grosser | 7a16c89 | 2011-05-14 19:01:55 +0000 | [diff] [blame] | 1640 | RI->setRegionFor(newBlock, region); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1641 | } else { |
| 1642 | RI->setRegionFor(newBlock, region->getParent()); |
| 1643 | splitBlock = newBlock; |
| 1644 | } |
| 1645 | |
| 1646 | return splitBlock; |
| 1647 | } |
| 1648 | |
| 1649 | // Create a split block that branches either to the old code or to a new basic |
| 1650 | // block where the new code can be inserted. |
| 1651 | // |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1652 | // @param Builder A builder that will be set to point to a basic block, where |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1653 | // the new code can be generated. |
| 1654 | // @return The split basic block. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1655 | BasicBlock *addSplitAndStartBlock(IRBuilder<> *Builder) { |
| 1656 | BasicBlock *StartBlock, *SplitBlock; |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1657 | |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1658 | SplitBlock = splitEdgeAdvanced(region); |
| 1659 | SplitBlock->setName("polly.split_new_and_old"); |
| 1660 | Function *F = SplitBlock->getParent(); |
| 1661 | StartBlock = BasicBlock::Create(F->getContext(), "polly.start", F); |
| 1662 | SplitBlock->getTerminator()->eraseFromParent(); |
| 1663 | Builder->SetInsertPoint(SplitBlock); |
| 1664 | Builder->CreateCondBr(Builder->getTrue(), StartBlock, region->getEntry()); |
| 1665 | DT->addNewBlock(StartBlock, SplitBlock); |
| 1666 | Builder->SetInsertPoint(StartBlock); |
| 1667 | return SplitBlock; |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | // Merge the control flow of the newly generated code with the existing code. |
| 1671 | // |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1672 | // @param SplitBlock The basic block where the control flow was split between |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1673 | // old and new version of the Scop. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1674 | // @param Builder An IRBuilder that points to the last instruction of the |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1675 | // newly generated code. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1676 | void mergeControlFlow(BasicBlock *SplitBlock, IRBuilder<> *Builder) { |
| 1677 | BasicBlock *MergeBlock; |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1678 | Region *R = region; |
| 1679 | |
| 1680 | if (R->getExit()->getSinglePredecessor()) |
| 1681 | // No splitEdge required. A block with a single predecessor cannot have |
| 1682 | // PHI nodes that would complicate life. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1683 | MergeBlock = R->getExit(); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1684 | else { |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1685 | MergeBlock = SplitEdge(R->getExitingBlock(), R->getExit(), this); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1686 | // SplitEdge will never split R->getExit(), as R->getExit() has more than |
| 1687 | // one predecessor. Hence, mergeBlock is always a newly generated block. |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1688 | R->replaceExit(MergeBlock); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1691 | Builder->CreateBr(MergeBlock); |
Tobias Grosser | 8518bbe | 2012-02-12 12:09:46 +0000 | [diff] [blame] | 1692 | MergeBlock->setName("polly.merge_new_and_old"); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1693 | |
Tobias Grosser | bd608a8 | 2012-02-12 12:09:41 +0000 | [diff] [blame] | 1694 | if (DT->dominates(SplitBlock, MergeBlock)) |
| 1695 | DT->changeImmediateDominator(MergeBlock, SplitBlock); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1698 | bool runOnScop(Scop &scop) { |
| 1699 | S = &scop; |
| 1700 | region = &S->getRegion(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1701 | DT = &getAnalysis<DominatorTree>(); |
| 1702 | Dependences *DP = &getAnalysis<Dependences>(); |
| 1703 | SE = &getAnalysis<ScalarEvolution>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1704 | SD = &getAnalysis<ScopDetection>(); |
| 1705 | TD = &getAnalysis<TargetData>(); |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1706 | RI = &getAnalysis<RegionInfo>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1707 | |
| 1708 | parallelLoops.clear(); |
| 1709 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1710 | assert(region->isSimple() && "Only simple regions are supported"); |
Tobias Grosser | 76d7c52 | 2011-05-14 19:01:37 +0000 | [diff] [blame] | 1711 | |
Tobias Grosser | b1c9599 | 2012-02-12 12:09:27 +0000 | [diff] [blame] | 1712 | Module *M = region->getEntry()->getParent()->getParent(); |
| 1713 | |
Tobias Grosser | d855cc5 | 2012-02-12 12:09:32 +0000 | [diff] [blame] | 1714 | if (OpenMP) addOpenMPDeclarations(M); |
Tobias Grosser | b1c9599 | 2012-02-12 12:09:27 +0000 | [diff] [blame] | 1715 | |
Tobias Grosser | 5772e65 | 2012-02-01 14:23:33 +0000 | [diff] [blame] | 1716 | // In the CFG the optimized code of the SCoP is generated next to the |
| 1717 | // original code. Both the new and the original version of the code remain |
| 1718 | // in the CFG. A branch statement decides which version is executed. |
| 1719 | // For now, we always execute the new version (the old one is dead code |
| 1720 | // eliminated by the cleanup passes). In the future we may decide to execute |
| 1721 | // the new version only if certain run time checks succeed. This will be |
| 1722 | // useful to support constructs for which we cannot prove all assumptions at |
| 1723 | // compile time. |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1724 | // |
| 1725 | // Before transformation: |
| 1726 | // |
| 1727 | // bb0 |
| 1728 | // | |
| 1729 | // orig_scop |
| 1730 | // | |
| 1731 | // bb1 |
| 1732 | // |
| 1733 | // After transformation: |
| 1734 | // bb0 |
| 1735 | // | |
| 1736 | // polly.splitBlock |
Tobias Grosser | 2bd3af1 | 2011-08-01 22:39:00 +0000 | [diff] [blame] | 1737 | // / \. |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1738 | // | startBlock |
| 1739 | // | | |
| 1740 | // orig_scop new_scop |
| 1741 | // \ / |
| 1742 | // \ / |
| 1743 | // bb1 (joinBlock) |
| 1744 | IRBuilder<> builder(region->getEntry()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1745 | |
Tobias Grosser | 8c4cfc32 | 2011-05-14 19:01:49 +0000 | [diff] [blame] | 1746 | // The builder will be set to startBlock. |
| 1747 | BasicBlock *splitBlock = addSplitAndStartBlock(&builder); |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1748 | BasicBlock *StartBlock = builder.GetInsertBlock(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1749 | |
Tobias Grosser | 0ac9214 | 2012-02-14 14:02:27 +0000 | [diff] [blame] | 1750 | mergeControlFlow(splitBlock, &builder); |
| 1751 | builder.SetInsertPoint(StartBlock->begin()); |
| 1752 | |
| 1753 | ClastStmtCodeGen CodeGen(S, *SE, DT, SD, DP, TD, builder, this); |
Tobias Grosser | 3fdecae | 2011-05-14 19:02:39 +0000 | [diff] [blame] | 1754 | CloogInfo &C = getAnalysis<CloogInfo>(); |
| 1755 | CodeGen.codegen(C.getClast()); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1756 | |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1757 | parallelLoops.insert(parallelLoops.begin(), |
| 1758 | CodeGen.getParallelLoops().begin(), |
| 1759 | CodeGen.getParallelLoops().end()); |
| 1760 | |
Tobias Grosser | abb6dcd | 2011-05-14 19:02:34 +0000 | [diff] [blame] | 1761 | return true; |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | virtual void printScop(raw_ostream &OS) const { |
| 1765 | for (std::vector<std::string>::const_iterator PI = parallelLoops.begin(), |
| 1766 | PE = parallelLoops.end(); PI != PE; ++PI) |
| 1767 | OS << "Parallel loop with iterator '" << *PI << "' generated\n"; |
| 1768 | } |
| 1769 | |
| 1770 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 1771 | AU.addRequired<CloogInfo>(); |
| 1772 | AU.addRequired<Dependences>(); |
| 1773 | AU.addRequired<DominatorTree>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1774 | AU.addRequired<RegionInfo>(); |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1775 | AU.addRequired<ScalarEvolution>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1776 | AU.addRequired<ScopDetection>(); |
| 1777 | AU.addRequired<ScopInfo>(); |
| 1778 | AU.addRequired<TargetData>(); |
| 1779 | |
| 1780 | AU.addPreserved<CloogInfo>(); |
| 1781 | AU.addPreserved<Dependences>(); |
Tobias Grosser | 5d6eb86 | 2011-05-14 19:02:45 +0000 | [diff] [blame] | 1782 | |
Tobias Grosser | 4e3f9a4 | 2011-05-23 15:23:36 +0000 | [diff] [blame] | 1783 | // FIXME: We do not create LoopInfo for the newly generated loops. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1784 | AU.addPreserved<LoopInfo>(); |
| 1785 | AU.addPreserved<DominatorTree>(); |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1786 | AU.addPreserved<ScopDetection>(); |
| 1787 | AU.addPreserved<ScalarEvolution>(); |
Tobias Grosser | 5d6eb86 | 2011-05-14 19:02:45 +0000 | [diff] [blame] | 1788 | |
Tobias Grosser | 4e3f9a4 | 2011-05-23 15:23:36 +0000 | [diff] [blame] | 1789 | // FIXME: We do not yet add regions for the newly generated code to the |
| 1790 | // region tree. |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1791 | AU.addPreserved<RegionInfo>(); |
| 1792 | AU.addPreserved<TempScopInfo>(); |
| 1793 | AU.addPreserved<ScopInfo>(); |
| 1794 | AU.addPreservedID(IndependentBlocksID); |
| 1795 | } |
| 1796 | }; |
| 1797 | } |
| 1798 | |
| 1799 | char CodeGeneration::ID = 1; |
| 1800 | |
Tobias Grosser | 73600b8 | 2011-10-08 00:30:40 +0000 | [diff] [blame] | 1801 | INITIALIZE_PASS_BEGIN(CodeGeneration, "polly-codegen", |
| 1802 | "Polly - Create LLVM-IR form SCoPs", false, false) |
| 1803 | INITIALIZE_PASS_DEPENDENCY(CloogInfo) |
| 1804 | INITIALIZE_PASS_DEPENDENCY(Dependences) |
| 1805 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 1806 | INITIALIZE_PASS_DEPENDENCY(RegionInfo) |
| 1807 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 1808 | INITIALIZE_PASS_DEPENDENCY(ScopDetection) |
| 1809 | INITIALIZE_PASS_DEPENDENCY(TargetData) |
| 1810 | INITIALIZE_PASS_END(CodeGeneration, "polly-codegen", |
| 1811 | "Polly - Create LLVM-IR form SCoPs", false, false) |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1812 | |
Tobias Grosser | 7ffe4e8 | 2011-11-17 12:56:10 +0000 | [diff] [blame] | 1813 | Pass *polly::createCodeGenerationPass() { |
Tobias Grosser | 7580537 | 2011-04-29 06:27:02 +0000 | [diff] [blame] | 1814 | return new CodeGeneration(); |
| 1815 | } |