Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 1 | //===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===// |
| 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 | // This file implements the BlockGenerator and VectorBlockGenerator classes, |
| 11 | // which generate sequential code and vectorized code for a polyhedral |
| 12 | // statement, respectively. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "polly/ScopInfo.h" |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 17 | #include "polly/CodeGen/CodeGeneration.h" |
Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 18 | #include "polly/CodeGen/BlockGenerators.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 19 | #include "polly/Support/GICHelper.h" |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 20 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 21 | #include "polly/Support/ScopHelper.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 22 | |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/LoopInfo.h" |
| 24 | #include "llvm/Analysis/ScalarEvolution.h" |
| 25 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 27 | #include "llvm/Support/CommandLine.h" |
| 28 | |
| 29 | #include "isl/aff.h" |
| 30 | #include "isl/set.h" |
| 31 | |
| 32 | using namespace llvm; |
| 33 | using namespace polly; |
| 34 | |
| 35 | static cl::opt<bool> |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 36 | Aligned("enable-polly-aligned", cl::desc("Assumed aligned memory accesses."), |
| 37 | cl::Hidden, cl::value_desc("OpenMP code generation enabled if true"), |
| 38 | cl::init(false), cl::ZeroOrMore); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 39 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 40 | static cl::opt<bool, true> |
| 41 | SCEVCodegenF("polly-codegen-scev", cl::desc("Use SCEV based code generation."), |
| 42 | cl::Hidden, cl::location(SCEVCodegen), cl::init(false), |
| 43 | cl::ZeroOrMore); |
| 44 | |
| 45 | bool polly::SCEVCodegen; |
| 46 | |
| 47 | bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI, |
| 48 | ScalarEvolution *SE, const Region *R) { |
| 49 | if (SCEVCodegen) { |
| 50 | if (!I || !SE->isSCEVable(I->getType())) |
| 51 | return false; |
| 52 | |
| 53 | if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I))) |
| 54 | if (!isa<SCEVCouldNotCompute>(Scev)) |
| 55 | if (!hasScalarDepsInsideRegion(Scev, R)) |
| 56 | return true; |
| 57 | |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | Loop *L = LI->getLoopFor(I->getParent()); |
| 62 | return L && I == L->getCanonicalInductionVariable(); |
| 63 | } |
| 64 | |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 65 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 66 | // Helper class to generate memory location. |
| 67 | namespace { |
| 68 | class IslGenerator { |
| 69 | public: |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 70 | IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS) |
| 71 | : Builder(Builder), IVS(IVS) {} |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 72 | Value *generateIslInt(__isl_take isl_int Int); |
| 73 | Value *generateIslAff(__isl_take isl_aff *Aff); |
| 74 | Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff); |
| 75 | |
| 76 | private: |
| 77 | typedef struct { |
| 78 | Value *Result; |
| 79 | class IslGenerator *Generator; |
| 80 | } IslGenInfo; |
| 81 | |
| 82 | IRBuilder<> &Builder; |
| 83 | std::vector<Value *> &IVS; |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 84 | static int mergeIslAffValues(__isl_take isl_set *Set, __isl_take isl_aff *Aff, |
| 85 | void *User); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 86 | }; |
| 87 | } |
| 88 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 89 | Value *IslGenerator::generateIslInt(isl_int Int) { |
| 90 | mpz_t IntMPZ; |
| 91 | mpz_init(IntMPZ); |
| 92 | isl_int_get_gmp(Int, IntMPZ); |
| 93 | Value *IntValue = Builder.getInt(APInt_from_MPZ(IntMPZ)); |
| 94 | mpz_clear(IntMPZ); |
| 95 | return IntValue; |
| 96 | } |
| 97 | |
| 98 | Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) { |
| 99 | Value *Result; |
| 100 | Value *ConstValue; |
| 101 | isl_int ConstIsl; |
| 102 | |
| 103 | isl_int_init(ConstIsl); |
| 104 | isl_aff_get_constant(Aff, &ConstIsl); |
| 105 | ConstValue = generateIslInt(ConstIsl); |
| 106 | Type *Ty = Builder.getInt64Ty(); |
| 107 | |
| 108 | // FIXME: We should give the constant and coefficients the right type. Here |
| 109 | // we force it into i64. |
| 110 | Result = Builder.CreateSExtOrBitCast(ConstValue, Ty); |
| 111 | |
| 112 | unsigned int NbInputDims = isl_aff_dim(Aff, isl_dim_in); |
| 113 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 114 | assert((IVS.size() == NbInputDims) && |
| 115 | "The Dimension of Induction Variables must match the dimension of the " |
| 116 | "affine space."); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 117 | |
| 118 | isl_int CoefficientIsl; |
| 119 | isl_int_init(CoefficientIsl); |
| 120 | |
| 121 | for (unsigned int i = 0; i < NbInputDims; ++i) { |
| 122 | Value *CoefficientValue; |
| 123 | isl_aff_get_coefficient(Aff, isl_dim_in, i, &CoefficientIsl); |
| 124 | |
| 125 | if (isl_int_is_zero(CoefficientIsl)) |
| 126 | continue; |
| 127 | |
| 128 | CoefficientValue = generateIslInt(CoefficientIsl); |
| 129 | CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true); |
| 130 | Value *IV = Builder.CreateIntCast(IVS[i], Ty, true); |
| 131 | Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff"); |
| 132 | Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff"); |
| 133 | } |
| 134 | |
| 135 | isl_int_clear(CoefficientIsl); |
| 136 | isl_int_clear(ConstIsl); |
| 137 | isl_aff_free(Aff); |
| 138 | |
| 139 | return Result; |
| 140 | } |
| 141 | |
| 142 | int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set, |
| 143 | __isl_take isl_aff *Aff, void *User) { |
| 144 | IslGenInfo *GenInfo = (IslGenInfo *)User; |
| 145 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 146 | assert((GenInfo->Result == NULL) && |
| 147 | "Result is already set. Currently only single isl_aff is supported"); |
| 148 | assert(isl_set_plain_is_universe(Set) && |
| 149 | "Code generation failed because the set is not universe"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 150 | |
| 151 | GenInfo->Result = GenInfo->Generator->generateIslAff(Aff); |
| 152 | |
| 153 | isl_set_free(Set); |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) { |
| 158 | IslGenInfo User; |
| 159 | User.Result = NULL; |
| 160 | User.Generator = this; |
| 161 | isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User); |
| 162 | assert(User.Result && "Code generation for isl_pw_aff failed"); |
| 163 | |
| 164 | isl_pw_aff_free(PwAff); |
| 165 | return User.Result; |
| 166 | } |
| 167 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 168 | BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P) |
| 169 | : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) { |
| 170 | } |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 171 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 172 | Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 173 | ValueMapT &GlobalMap, LoopToScevMapT <S, |
| 174 | Loop *L) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 175 | // We assume constants never change. |
| 176 | // This avoids map lookups for many calls to this function. |
| 177 | if (isa<Constant>(Old)) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 178 | return const_cast<Value *>(Old); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 179 | |
| 180 | if (GlobalMap.count(Old)) { |
| 181 | Value *New = GlobalMap[Old]; |
| 182 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 183 | if (Old->getType()->getScalarSizeInBits() < |
| 184 | New->getType()->getScalarSizeInBits()) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 185 | New = Builder.CreateTruncOrBitCast(New, Old->getType()); |
| 186 | |
| 187 | return New; |
| 188 | } |
| 189 | |
| 190 | if (BBMap.count(Old)) { |
| 191 | return BBMap[Old]; |
| 192 | } |
| 193 | |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 194 | if (SCEVCodegen && SE.isSCEVable(Old->getType())) |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 195 | if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) { |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 196 | if (!isa<SCEVCouldNotCompute>(Scev)) { |
Sebastian Pop | 637b23d | 2013-02-15 20:56:01 +0000 | [diff] [blame] | 197 | const SCEV *NewScev = apply(Scev, LTS, SE); |
| 198 | ValueToValueMap VTV; |
| 199 | VTV.insert(BBMap.begin(), BBMap.end()); |
| 200 | VTV.insert(GlobalMap.begin(), GlobalMap.end()); |
Sebastian Pop | 47d4ee3 | 2013-02-15 21:26:53 +0000 | [diff] [blame] | 201 | NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV); |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 202 | SCEVExpander Expander(SE, "polly"); |
| 203 | Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(), |
| 204 | Builder.GetInsertPoint()); |
| 205 | |
| 206 | BBMap[Old] = Expanded; |
| 207 | return Expanded; |
| 208 | } |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 209 | } |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 210 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 211 | if (const Instruction *Inst = dyn_cast<Instruction>(Old)) { |
| 212 | (void) Inst; |
| 213 | assert(!Statement.getParent()->getRegion().contains(Inst->getParent()) && |
| 214 | "unexpected scalar dependence in region"); |
| 215 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 216 | |
| 217 | // Everything else is probably a scop-constant value defined as global, |
| 218 | // function parameter or an instruction not within the scop. |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 219 | return const_cast<Value *>(Old); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 223 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 224 | Instruction *NewInst = Inst->clone(); |
| 225 | |
| 226 | // Replace old operands with the new ones. |
| 227 | for (Instruction::const_op_iterator OI = Inst->op_begin(), |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 228 | OE = Inst->op_end(); |
| 229 | OI != OE; ++OI) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 230 | Value *OldOperand = *OI; |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 231 | Value *NewOperand = |
| 232 | getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 233 | |
| 234 | if (!NewOperand) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 235 | assert(!isa<StoreInst>(NewInst) && |
| 236 | "Store instructions are always needed!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 237 | delete NewInst; |
| 238 | return; |
| 239 | } |
| 240 | |
| 241 | NewInst->replaceUsesOfWith(OldOperand, NewOperand); |
| 242 | } |
| 243 | |
| 244 | Builder.Insert(NewInst); |
| 245 | BBMap[Inst] = NewInst; |
| 246 | |
| 247 | if (!NewInst->getType()->isVoidTy()) |
| 248 | NewInst->setName("p_" + Inst->getName()); |
| 249 | } |
| 250 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 251 | std::vector<Value *> BlockGenerator::getMemoryAccessIndex( |
| 252 | __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 253 | ValueMapT &GlobalMap, LoopToScevMapT <S, Loop *L) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 254 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 255 | assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) && |
| 256 | "Only single dimensional access functions supported"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 257 | |
| 258 | std::vector<Value *> IVS; |
| 259 | for (unsigned i = 0; i < Statement.getNumIterators(); ++i) { |
| 260 | const Value *OriginalIV = Statement.getInductionVariableForDimension(i); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 261 | Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS, L); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 262 | IVS.push_back(NewIV); |
| 263 | } |
| 264 | |
| 265 | isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0); |
| 266 | IslGenerator IslGen(Builder, IVS); |
| 267 | Value *OffsetValue = IslGen.generateIslPwAff(PwAff); |
| 268 | |
| 269 | Type *Ty = Builder.getInt64Ty(); |
| 270 | OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true); |
| 271 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 272 | std::vector<Value *> IndexArray; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 273 | Value *NullValue = Constant::getNullValue(Ty); |
| 274 | IndexArray.push_back(NullValue); |
| 275 | IndexArray.push_back(OffsetValue); |
| 276 | return IndexArray; |
| 277 | } |
| 278 | |
| 279 | Value *BlockGenerator::getNewAccessOperand( |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 280 | __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 281 | ValueMapT &GlobalMap, LoopToScevMapT <S, Loop *L) { |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 282 | std::vector<Value *> IndexArray = getMemoryAccessIndex( |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 283 | NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS, L); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 284 | Value *NewOperand = |
| 285 | Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 286 | return NewOperand; |
| 287 | } |
| 288 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 289 | Value *BlockGenerator::generateLocationAccessed( |
| 290 | const Instruction *Inst, const Value *Pointer, ValueMapT &BBMap, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 291 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 292 | MemoryAccess &Access = Statement.getAccessFor(Inst); |
| 293 | isl_map *CurrentAccessRelation = Access.getAccessRelation(); |
| 294 | isl_map *NewAccessRelation = Access.getNewAccessRelation(); |
| 295 | |
Tobias Grosser | ae2d83e | 2012-12-29 23:57:18 +0000 | [diff] [blame] | 296 | assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) && |
| 297 | "Current and new access function use different spaces"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 298 | |
| 299 | Value *NewPointer; |
| 300 | |
| 301 | if (!NewAccessRelation) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 302 | NewPointer = |
| 303 | getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 304 | } else { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 305 | Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr()); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 306 | NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 307 | GlobalMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | isl_map_free(CurrentAccessRelation); |
| 311 | isl_map_free(NewAccessRelation); |
| 312 | return NewPointer; |
| 313 | } |
| 314 | |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 315 | Loop * |
| 316 | BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) { |
| 317 | return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent()); |
| 318 | } |
| 319 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 320 | Value * |
| 321 | BlockGenerator::generateScalarLoad(const LoadInst *Load, ValueMapT &BBMap, |
| 322 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 323 | const Value *Pointer = Load->getPointerOperand(); |
| 324 | const Instruction *Inst = dyn_cast<Instruction>(Load); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 325 | Value *NewPointer = |
| 326 | generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 327 | Value *ScalarLoad = |
| 328 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 329 | return ScalarLoad; |
| 330 | } |
| 331 | |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 332 | Value * |
| 333 | BlockGenerator::generateScalarStore(const StoreInst *Store, ValueMapT &BBMap, |
| 334 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 335 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 336 | Value *NewPointer = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 337 | generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 338 | Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap, |
| 339 | LTS, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 340 | |
| 341 | return Builder.CreateStore(ValueOperand, NewPointer); |
| 342 | } |
| 343 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 344 | void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 345 | ValueMapT &GlobalMap, |
| 346 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 347 | // Terminator instructions control the control flow. They are explicitly |
| 348 | // expressed in the clast and do not need to be copied. |
| 349 | if (Inst->isTerminator()) |
| 350 | return; |
| 351 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 352 | if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE, |
| 353 | &Statement.getParent()->getRegion())) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 354 | return; |
| 355 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 356 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 357 | BBMap[Load] = generateScalarLoad(Load, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 358 | return; |
| 359 | } |
| 360 | |
| 361 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 362 | BBMap[Store] = generateScalarStore(Store, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 366 | copyInstScalar(Inst, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 369 | void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 370 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 371 | BasicBlock *CopyBB = |
| 372 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 373 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 374 | Builder.SetInsertPoint(CopyBB->begin()); |
| 375 | |
| 376 | ValueMapT BBMap; |
| 377 | |
| 378 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE; |
| 379 | ++II) |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 380 | copyInstruction(II, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 383 | VectorBlockGenerator::VectorBlockGenerator( |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 384 | IRBuilder<> &B, VectorValueMapT &GlobalMaps, |
| 385 | std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt, |
| 386 | __isl_keep isl_map *Schedule, Pass *P) |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 387 | : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS), |
| 388 | Schedule(Schedule) { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 389 | assert(GlobalMaps.size() > 1 && "Only one vector lane found"); |
| 390 | assert(Schedule && "No statement domain provided"); |
| 391 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 392 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 393 | Value *VectorBlockGenerator::getVectorValue( |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 394 | const Value *Old, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps, |
| 395 | Loop *L) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 396 | if (VectorMap.count(Old)) |
| 397 | return VectorMap[Old]; |
| 398 | |
| 399 | int Width = getVectorWidth(); |
| 400 | |
| 401 | Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); |
| 402 | |
| 403 | for (int Lane = 0; Lane < Width; Lane++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 404 | Vector = Builder.CreateInsertElement( |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 405 | Vector, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 406 | getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L), |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 407 | Builder.getInt32(Lane)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 408 | |
| 409 | VectorMap[Old] = Vector; |
| 410 | |
| 411 | return Vector; |
| 412 | } |
| 413 | |
| 414 | Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { |
| 415 | PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); |
| 416 | assert(PointerTy && "PointerType expected"); |
| 417 | |
| 418 | Type *ScalarType = PointerTy->getElementType(); |
| 419 | VectorType *VectorType = VectorType::get(ScalarType, Width); |
| 420 | |
| 421 | return PointerType::getUnqual(VectorType); |
| 422 | } |
| 423 | |
| 424 | Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load, |
| 425 | ValueMapT &BBMap) { |
| 426 | const Value *Pointer = Load->getPointerOperand(); |
| 427 | Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 428 | Value *NewPointer = |
| 429 | getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load)); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 430 | Value *VectorPtr = |
| 431 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
| 432 | LoadInst *VecLoad = |
| 433 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 434 | if (!Aligned) |
| 435 | VecLoad->setAlignment(8); |
| 436 | |
| 437 | return VecLoad; |
| 438 | } |
| 439 | |
| 440 | Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load, |
| 441 | ValueMapT &BBMap) { |
| 442 | const Value *Pointer = Load->getPointerOperand(); |
| 443 | Type *VectorPtrType = getVectorPtrTy(Pointer, 1); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 444 | Value *NewPointer = |
| 445 | getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 446 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 447 | Load->getName() + "_p_vec_p"); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 448 | LoadInst *ScalarLoad = |
| 449 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 450 | |
| 451 | if (!Aligned) |
| 452 | ScalarLoad->setAlignment(8); |
| 453 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 454 | Constant *SplatVector = Constant::getNullValue( |
| 455 | VectorType::get(Builder.getInt32Ty(), getVectorWidth())); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 456 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 457 | Value *VectorLoad = Builder.CreateShuffleVector( |
| 458 | ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 459 | return VectorLoad; |
| 460 | } |
| 461 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 462 | Value *VectorBlockGenerator::generateUnknownStrideLoad( |
| 463 | const LoadInst *Load, VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 464 | int VectorWidth = getVectorWidth(); |
| 465 | const Value *Pointer = Load->getPointerOperand(); |
| 466 | VectorType *VectorType = VectorType::get( |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 467 | dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 468 | |
| 469 | Value *Vector = UndefValue::get(VectorType); |
| 470 | |
| 471 | for (int i = 0; i < VectorWidth; i++) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 472 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], |
| 473 | VLTS[i], getLoopForInst(Load)); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 474 | Value *ScalarLoad = |
| 475 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
| 476 | Vector = Builder.CreateInsertElement( |
| 477 | Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | return Vector; |
| 481 | } |
| 482 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 483 | void VectorBlockGenerator::generateLoad( |
| 484 | const LoadInst *Load, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 485 | if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL || |
| 486 | !VectorType::isValidElementType(Load->getType())) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 487 | for (int i = 0; i < getVectorWidth(); i++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 488 | ScalarMaps[i][Load] = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 489 | generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
| 493 | MemoryAccess &Access = Statement.getAccessFor(Load); |
| 494 | |
| 495 | Value *NewLoad; |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 496 | if (Access.isStrideZero(isl_map_copy(Schedule))) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 497 | NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 498 | else if (Access.isStrideOne(isl_map_copy(Schedule))) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 499 | NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]); |
| 500 | else |
| 501 | NewLoad = generateUnknownStrideLoad(Load, ScalarMaps); |
| 502 | |
| 503 | VectorMap[Load] = NewLoad; |
| 504 | } |
| 505 | |
| 506 | void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst, |
| 507 | ValueMapT &VectorMap, |
| 508 | VectorValueMapT &ScalarMaps) { |
| 509 | int VectorWidth = getVectorWidth(); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 510 | Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps, |
| 511 | getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 512 | |
| 513 | assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); |
| 514 | |
| 515 | const CastInst *Cast = dyn_cast<CastInst>(Inst); |
| 516 | VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); |
| 517 | VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); |
| 518 | } |
| 519 | |
| 520 | void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst, |
| 521 | ValueMapT &VectorMap, |
| 522 | VectorValueMapT &ScalarMaps) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 523 | Loop *L = getLoopForInst(Inst); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 524 | Value *OpZero = Inst->getOperand(0); |
| 525 | Value *OpOne = Inst->getOperand(1); |
| 526 | |
| 527 | Value *NewOpZero, *NewOpOne; |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 528 | NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L); |
| 529 | NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 530 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 531 | Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 532 | Inst->getName() + "p_vec"); |
| 533 | VectorMap[Inst] = NewInst; |
| 534 | } |
| 535 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 536 | void VectorBlockGenerator::copyStore( |
| 537 | const StoreInst *Store, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 538 | int VectorWidth = getVectorWidth(); |
| 539 | |
| 540 | MemoryAccess &Access = Statement.getAccessFor(Store); |
| 541 | |
| 542 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 543 | Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap, |
| 544 | ScalarMaps, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 545 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 546 | if (Access.isStrideOne(isl_map_copy(Schedule))) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 547 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 548 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0], |
| 549 | VLTS[0], getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 550 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 551 | Value *VectorPtr = |
| 552 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 553 | StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); |
| 554 | |
| 555 | if (!Aligned) |
| 556 | Store->setAlignment(8); |
| 557 | } else { |
| 558 | for (unsigned i = 0; i < ScalarMaps.size(); i++) { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 559 | Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame^] | 560 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], |
| 561 | VLTS[i], getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 562 | Builder.CreateStore(Scalar, NewPointer); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, |
| 568 | ValueMapT &VectorMap) { |
| 569 | for (Instruction::const_op_iterator OI = Inst->op_begin(), |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 570 | OE = Inst->op_end(); |
| 571 | OI != OE; ++OI) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 572 | if (VectorMap.count(*OI)) |
| 573 | return true; |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, |
| 578 | ValueMapT &VectorMap, |
| 579 | VectorValueMapT &ScalarMaps) { |
| 580 | bool HasVectorOperand = false; |
| 581 | int VectorWidth = getVectorWidth(); |
| 582 | |
| 583 | for (Instruction::const_op_iterator OI = Inst->op_begin(), |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 584 | OE = Inst->op_end(); |
| 585 | OI != OE; ++OI) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 586 | ValueMapT::iterator VecOp = VectorMap.find(*OI); |
| 587 | |
| 588 | if (VecOp == VectorMap.end()) |
| 589 | continue; |
| 590 | |
| 591 | HasVectorOperand = true; |
| 592 | Value *NewVector = VecOp->second; |
| 593 | |
| 594 | for (int i = 0; i < VectorWidth; ++i) { |
| 595 | ValueMapT &SM = ScalarMaps[i]; |
| 596 | |
| 597 | // If there is one scalar extracted, all scalar elements should have |
| 598 | // already been extracted by the code here. So no need to check for the |
| 599 | // existance of all of them. |
| 600 | if (SM.count(*OI)) |
| 601 | break; |
| 602 | |
| 603 | SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | return HasVectorOperand; |
| 608 | } |
| 609 | |
| 610 | void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst, |
| 611 | ValueMapT &VectorMap, |
| 612 | VectorValueMapT &ScalarMaps) { |
| 613 | bool HasVectorOperand; |
| 614 | int VectorWidth = getVectorWidth(); |
| 615 | |
| 616 | HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); |
| 617 | |
| 618 | for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 619 | copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane], |
| 620 | VLTS[VectorLane]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 621 | |
| 622 | if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) |
| 623 | return; |
| 624 | |
| 625 | // Make the result available as vector value. |
| 626 | VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); |
| 627 | Value *Vector = UndefValue::get(VectorType); |
| 628 | |
| 629 | for (int i = 0; i < VectorWidth; i++) |
| 630 | Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], |
| 631 | Builder.getInt32(i)); |
| 632 | |
| 633 | VectorMap[Inst] = Vector; |
| 634 | } |
| 635 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 636 | int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 637 | |
| 638 | void VectorBlockGenerator::copyInstruction(const Instruction *Inst, |
| 639 | ValueMapT &VectorMap, |
| 640 | VectorValueMapT &ScalarMaps) { |
| 641 | // Terminator instructions control the control flow. They are explicitly |
| 642 | // expressed in the clast and do not need to be copied. |
| 643 | if (Inst->isTerminator()) |
| 644 | return; |
| 645 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 646 | if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE, |
| 647 | &Statement.getParent()->getRegion())) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 648 | return; |
| 649 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 650 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
| 651 | generateLoad(Load, VectorMap, ScalarMaps); |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | if (hasVectorOperands(Inst, VectorMap)) { |
| 656 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
| 657 | copyStore(Store, VectorMap, ScalarMaps); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { |
| 662 | copyUnaryInst(Unary, VectorMap, ScalarMaps); |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { |
| 667 | copyBinaryInst(Binary, VectorMap, ScalarMaps); |
| 668 | return; |
| 669 | } |
| 670 | |
| 671 | // Falltrough: We generate scalar instructions, if we don't know how to |
| 672 | // generate vector code. |
| 673 | } |
| 674 | |
| 675 | copyInstScalarized(Inst, VectorMap, ScalarMaps); |
| 676 | } |
| 677 | |
| 678 | void VectorBlockGenerator::copyBB() { |
| 679 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 680 | BasicBlock *CopyBB = |
| 681 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 682 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 683 | Builder.SetInsertPoint(CopyBB->begin()); |
| 684 | |
| 685 | // Create two maps that store the mapping from the original instructions of |
| 686 | // the old basic block to their copies in the new basic block. Those maps |
| 687 | // are basic block local. |
| 688 | // |
| 689 | // As vector code generation is supported there is one map for scalar values |
| 690 | // and one for vector values. |
| 691 | // |
| 692 | // In case we just do scalar code generation, the vectorMap is not used and |
| 693 | // the scalarMap has just one dimension, which contains the mapping. |
| 694 | // |
| 695 | // In case vector code generation is done, an instruction may either appear |
| 696 | // in the vector map once (as it is calculating >vectorwidth< values at a |
| 697 | // time. Or (if the values are calculated using scalar operations), it |
| 698 | // appears once in every dimension of the scalarMap. |
| 699 | VectorValueMapT ScalarBlockMap(getVectorWidth()); |
| 700 | ValueMapT VectorBlockMap; |
| 701 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 702 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE; |
| 703 | ++II) |
| 704 | copyInstruction(II, VectorBlockMap, ScalarBlockMap); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 705 | } |