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