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