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