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