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