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