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: |
| 46 | IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS) : |
| 47 | Builder(Builder), IVS(IVS) {} |
| 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 | |
| 90 | assert((IVS.size() == NbInputDims) && "The Dimension of Induction Variables" |
| 91 | "must match the dimension of the affine space."); |
| 92 | |
| 93 | isl_int CoefficientIsl; |
| 94 | isl_int_init(CoefficientIsl); |
| 95 | |
| 96 | for (unsigned int i = 0; i < NbInputDims; ++i) { |
| 97 | Value *CoefficientValue; |
| 98 | isl_aff_get_coefficient(Aff, isl_dim_in, i, &CoefficientIsl); |
| 99 | |
| 100 | if (isl_int_is_zero(CoefficientIsl)) |
| 101 | continue; |
| 102 | |
| 103 | CoefficientValue = generateIslInt(CoefficientIsl); |
| 104 | CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true); |
| 105 | Value *IV = Builder.CreateIntCast(IVS[i], Ty, true); |
| 106 | Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff"); |
| 107 | Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff"); |
| 108 | } |
| 109 | |
| 110 | isl_int_clear(CoefficientIsl); |
| 111 | isl_int_clear(ConstIsl); |
| 112 | isl_aff_free(Aff); |
| 113 | |
| 114 | return Result; |
| 115 | } |
| 116 | |
| 117 | int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set, |
| 118 | __isl_take isl_aff *Aff, void *User) { |
| 119 | IslGenInfo *GenInfo = (IslGenInfo *)User; |
| 120 | |
| 121 | assert((GenInfo->Result == NULL) && "Result is already set." |
| 122 | "Currently only single isl_aff is supported"); |
| 123 | assert(isl_set_plain_is_universe(Set) |
| 124 | && "Code generation failed because the set is not universe"); |
| 125 | |
| 126 | GenInfo->Result = GenInfo->Generator->generateIslAff(Aff); |
| 127 | |
| 128 | isl_set_free(Set); |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) { |
| 133 | IslGenInfo User; |
| 134 | User.Result = NULL; |
| 135 | User.Generator = this; |
| 136 | isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User); |
| 137 | assert(User.Result && "Code generation for isl_pw_aff failed"); |
| 138 | |
| 139 | isl_pw_aff_free(PwAff); |
| 140 | return User.Result; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P): |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 145 | Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {} |
| 146 | |
| 147 | bool BlockGenerator::isSCEVIgnore(const Instruction *Inst) { |
| 148 | if (SCEVCodegen && SE.isSCEVable(Inst->getType())) |
| 149 | if (const SCEV *Scev = SE.getSCEV(const_cast<Instruction*>(Inst))) |
| 150 | if (!isa<SCEVCouldNotCompute>(Scev)) { |
| 151 | if (const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Scev)) { |
| 152 | if (Unknown->getValue() != Inst) |
| 153 | return true; |
| 154 | } else { |
| 155 | return true; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return false; |
| 160 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 161 | |
| 162 | Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 163 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
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() < |
| 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)) { |
| 180 | return BBMap[Old]; |
| 181 | } |
| 182 | |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 183 | if (SCEVCodegen && SE.isSCEVable(Old->getType())) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 184 | if (const SCEV *Scev = SE.getSCEV(const_cast<Value *>(Old))) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 185 | if (!isa<SCEVCouldNotCompute>(Scev)) { |
Sebastian Pop | 637b23d | 2013-02-15 20:56:01 +0000 | [diff] [blame] | 186 | const SCEV *NewScev = apply(Scev, LTS, SE); |
| 187 | ValueToValueMap VTV; |
| 188 | VTV.insert(BBMap.begin(), BBMap.end()); |
| 189 | VTV.insert(GlobalMap.begin(), GlobalMap.end()); |
Sebastian Pop | 47d4ee3 | 2013-02-15 21:26:53 +0000 | [diff] [blame] | 190 | NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV); |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 191 | SCEVExpander Expander(SE, "polly"); |
| 192 | Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(), |
| 193 | Builder.GetInsertPoint()); |
| 194 | |
| 195 | BBMap[Old] = Expanded; |
| 196 | return Expanded; |
| 197 | } |
| 198 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 199 | // 'Old' is within the original SCoP, but was not rewritten. |
| 200 | // |
| 201 | // Such values appear, if they only calculate information already available in |
| 202 | // the polyhedral description (e.g. an induction variable increment). They |
| 203 | // can be safely ignored. |
| 204 | if (const Instruction *Inst = dyn_cast<Instruction>(Old)) |
| 205 | if (Statement.getParent()->getRegion().contains(Inst->getParent())) |
| 206 | return NULL; |
| 207 | |
| 208 | // Everything else is probably a scop-constant value defined as global, |
| 209 | // function parameter or an instruction not within the scop. |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 210 | return const_cast<Value *>(Old); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 214 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 215 | Instruction *NewInst = Inst->clone(); |
| 216 | |
| 217 | // Replace old operands with the new ones. |
| 218 | for (Instruction::const_op_iterator OI = Inst->op_begin(), |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 219 | OE = Inst->op_end(); |
| 220 | OI != OE; ++OI) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 221 | Value *OldOperand = *OI; |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 222 | Value *NewOperand = getNewValue(OldOperand, BBMap, GlobalMap, LTS); |
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, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 243 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
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); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 251 | Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS); |
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, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 271 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 272 | std::vector<Value *> IndexArray = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 273 | getMemoryAccessIndex(NewAccessRelation, BaseAddress, BBMap, GlobalMap, |
| 274 | 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()); |
| 296 | NewPointer = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 297 | getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap, GlobalMap, |
| 298 | LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | isl_map_free(CurrentAccessRelation); |
| 302 | isl_map_free(NewAccessRelation); |
| 303 | return NewPointer; |
| 304 | } |
| 305 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 306 | Value *BlockGenerator::generateScalarLoad( |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 307 | const LoadInst *Load, ValueMapT &BBMap, ValueMapT &GlobalMap, |
| 308 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 309 | const Value *Pointer = Load->getPointerOperand(); |
| 310 | const Instruction *Inst = dyn_cast<Instruction>(Load); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 311 | Value *NewPointer = generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, |
| 312 | LTS); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 313 | Value *ScalarLoad = |
| 314 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 315 | return ScalarLoad; |
| 316 | } |
| 317 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 318 | Value *BlockGenerator::generateScalarStore( |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 319 | const StoreInst *Store, ValueMapT &BBMap, ValueMapT &GlobalMap, |
| 320 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 321 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 322 | Value *NewPointer = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 323 | generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS); |
| 324 | Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap, |
| 325 | LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 326 | |
| 327 | return Builder.CreateStore(ValueOperand, NewPointer); |
| 328 | } |
| 329 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 330 | void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 331 | ValueMapT &GlobalMap, |
| 332 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 333 | // Terminator instructions control the control flow. They are explicitly |
| 334 | // expressed in the clast and do not need to be copied. |
| 335 | if (Inst->isTerminator()) |
| 336 | return; |
| 337 | |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 338 | if (isSCEVIgnore(Inst)) |
| 339 | return; |
| 340 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 341 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 342 | BBMap[Load] = generateScalarLoad(Load, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 343 | return; |
| 344 | } |
| 345 | |
| 346 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 347 | BBMap[Store] = generateScalarStore(Store, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 348 | return; |
| 349 | } |
| 350 | |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 351 | copyInstScalar(Inst, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 354 | void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 355 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 356 | BasicBlock *CopyBB = |
| 357 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 358 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 359 | Builder.SetInsertPoint(CopyBB->begin()); |
| 360 | |
| 361 | ValueMapT BBMap; |
| 362 | |
| 363 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE; |
| 364 | ++II) |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 365 | copyInstruction(II, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 368 | VectorBlockGenerator::VectorBlockGenerator( |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 369 | IRBuilder<> &B, VectorValueMapT &GlobalMaps, std::vector<LoopToScevMapT> &VLTS, |
| 370 | ScopStmt &Stmt, __isl_keep isl_map *Schedule, Pass *P) |
| 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( |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 388 | Vector, getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], |
| 389 | VLTS[Lane]), Builder.getInt32(Lane)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 390 | |
| 391 | VectorMap[Old] = Vector; |
| 392 | |
| 393 | return Vector; |
| 394 | } |
| 395 | |
| 396 | Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { |
| 397 | PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); |
| 398 | assert(PointerTy && "PointerType expected"); |
| 399 | |
| 400 | Type *ScalarType = PointerTy->getElementType(); |
| 401 | VectorType *VectorType = VectorType::get(ScalarType, Width); |
| 402 | |
| 403 | return PointerType::getUnqual(VectorType); |
| 404 | } |
| 405 | |
| 406 | Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load, |
| 407 | ValueMapT &BBMap) { |
| 408 | const Value *Pointer = Load->getPointerOperand(); |
| 409 | Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 410 | Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 411 | Value *VectorPtr = |
| 412 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
| 413 | LoadInst *VecLoad = |
| 414 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 415 | if (!Aligned) |
| 416 | VecLoad->setAlignment(8); |
| 417 | |
| 418 | return VecLoad; |
| 419 | } |
| 420 | |
| 421 | Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load, |
| 422 | ValueMapT &BBMap) { |
| 423 | const Value *Pointer = Load->getPointerOperand(); |
| 424 | Type *VectorPtrType = getVectorPtrTy(Pointer, 1); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 425 | Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 426 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 427 | Load->getName() + "_p_vec_p"); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 428 | LoadInst *ScalarLoad = |
| 429 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 430 | |
| 431 | if (!Aligned) |
| 432 | ScalarLoad->setAlignment(8); |
| 433 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 434 | Constant *SplatVector = Constant::getNullValue( |
| 435 | VectorType::get(Builder.getInt32Ty(), getVectorWidth())); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 436 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 437 | Value *VectorLoad = Builder.CreateShuffleVector( |
| 438 | ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 439 | return VectorLoad; |
| 440 | } |
| 441 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 442 | Value *VectorBlockGenerator::generateUnknownStrideLoad( |
| 443 | const LoadInst *Load, VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 444 | int VectorWidth = getVectorWidth(); |
| 445 | const Value *Pointer = Load->getPointerOperand(); |
| 446 | VectorType *VectorType = VectorType::get( |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 447 | dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 448 | |
| 449 | Value *Vector = UndefValue::get(VectorType); |
| 450 | |
| 451 | for (int i = 0; i < VectorWidth; i++) { |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 452 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], |
| 453 | VLTS[i]); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 454 | Value *ScalarLoad = |
| 455 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
| 456 | Vector = Builder.CreateInsertElement( |
| 457 | Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | return Vector; |
| 461 | } |
| 462 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 463 | void VectorBlockGenerator::generateLoad( |
| 464 | const LoadInst *Load, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 465 | if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL || |
| 466 | !VectorType::isValidElementType(Load->getType())) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 467 | for (int i = 0; i < getVectorWidth(); i++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 468 | ScalarMaps[i][Load] = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 469 | generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 470 | return; |
| 471 | } |
| 472 | |
| 473 | MemoryAccess &Access = Statement.getAccessFor(Load); |
| 474 | |
| 475 | Value *NewLoad; |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 476 | if (Access.isStrideZero(isl_map_copy(Schedule))) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 477 | NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 478 | else if (Access.isStrideOne(isl_map_copy(Schedule))) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 479 | NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]); |
| 480 | else |
| 481 | NewLoad = generateUnknownStrideLoad(Load, ScalarMaps); |
| 482 | |
| 483 | VectorMap[Load] = NewLoad; |
| 484 | } |
| 485 | |
| 486 | void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst, |
| 487 | ValueMapT &VectorMap, |
| 488 | VectorValueMapT &ScalarMaps) { |
| 489 | int VectorWidth = getVectorWidth(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 490 | Value *NewOperand = |
| 491 | getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 492 | |
| 493 | assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); |
| 494 | |
| 495 | const CastInst *Cast = dyn_cast<CastInst>(Inst); |
| 496 | VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); |
| 497 | VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); |
| 498 | } |
| 499 | |
| 500 | void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst, |
| 501 | ValueMapT &VectorMap, |
| 502 | VectorValueMapT &ScalarMaps) { |
| 503 | Value *OpZero = Inst->getOperand(0); |
| 504 | Value *OpOne = Inst->getOperand(1); |
| 505 | |
| 506 | Value *NewOpZero, *NewOpOne; |
| 507 | NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps); |
| 508 | NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps); |
| 509 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 510 | Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 511 | Inst->getName() + "p_vec"); |
| 512 | VectorMap[Inst] = NewInst; |
| 513 | } |
| 514 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 515 | void VectorBlockGenerator::copyStore( |
| 516 | const StoreInst *Store, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 517 | int VectorWidth = getVectorWidth(); |
| 518 | |
| 519 | MemoryAccess &Access = Statement.getAccessFor(Store); |
| 520 | |
| 521 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 522 | Value *Vector = |
| 523 | getVectorValue(Store->getValueOperand(), VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 524 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 525 | if (Access.isStrideOne(isl_map_copy(Schedule))) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 526 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 527 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0], |
| 528 | VLTS[0]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 529 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 530 | Value *VectorPtr = |
| 531 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 532 | StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); |
| 533 | |
| 534 | if (!Aligned) |
| 535 | Store->setAlignment(8); |
| 536 | } else { |
| 537 | for (unsigned i = 0; i < ScalarMaps.size(); i++) { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 538 | Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 539 | Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], |
| 540 | VLTS[i]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 541 | Builder.CreateStore(Scalar, NewPointer); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, |
| 547 | ValueMapT &VectorMap) { |
| 548 | for (Instruction::const_op_iterator OI = Inst->op_begin(), |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 549 | OE = Inst->op_end(); |
| 550 | OI != OE; ++OI) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 551 | if (VectorMap.count(*OI)) |
| 552 | return true; |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, |
| 557 | ValueMapT &VectorMap, |
| 558 | VectorValueMapT &ScalarMaps) { |
| 559 | bool HasVectorOperand = false; |
| 560 | int VectorWidth = getVectorWidth(); |
| 561 | |
| 562 | for (Instruction::const_op_iterator OI = Inst->op_begin(), |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 563 | OE = Inst->op_end(); |
| 564 | OI != OE; ++OI) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 565 | ValueMapT::iterator VecOp = VectorMap.find(*OI); |
| 566 | |
| 567 | if (VecOp == VectorMap.end()) |
| 568 | continue; |
| 569 | |
| 570 | HasVectorOperand = true; |
| 571 | Value *NewVector = VecOp->second; |
| 572 | |
| 573 | for (int i = 0; i < VectorWidth; ++i) { |
| 574 | ValueMapT &SM = ScalarMaps[i]; |
| 575 | |
| 576 | // If there is one scalar extracted, all scalar elements should have |
| 577 | // already been extracted by the code here. So no need to check for the |
| 578 | // existance of all of them. |
| 579 | if (SM.count(*OI)) |
| 580 | break; |
| 581 | |
| 582 | SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return HasVectorOperand; |
| 587 | } |
| 588 | |
| 589 | void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst, |
| 590 | ValueMapT &VectorMap, |
| 591 | VectorValueMapT &ScalarMaps) { |
| 592 | bool HasVectorOperand; |
| 593 | int VectorWidth = getVectorWidth(); |
| 594 | |
| 595 | HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); |
| 596 | |
| 597 | for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 598 | copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane], |
| 599 | VLTS[VectorLane]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 600 | |
| 601 | if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) |
| 602 | return; |
| 603 | |
| 604 | // Make the result available as vector value. |
| 605 | VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); |
| 606 | Value *Vector = UndefValue::get(VectorType); |
| 607 | |
| 608 | for (int i = 0; i < VectorWidth; i++) |
| 609 | Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], |
| 610 | Builder.getInt32(i)); |
| 611 | |
| 612 | VectorMap[Inst] = Vector; |
| 613 | } |
| 614 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 615 | int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 616 | |
| 617 | void VectorBlockGenerator::copyInstruction(const Instruction *Inst, |
| 618 | ValueMapT &VectorMap, |
| 619 | VectorValueMapT &ScalarMaps) { |
| 620 | // Terminator instructions control the control flow. They are explicitly |
| 621 | // expressed in the clast and do not need to be copied. |
| 622 | if (Inst->isTerminator()) |
| 623 | return; |
| 624 | |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 625 | if (isSCEVIgnore(Inst)) |
| 626 | return; |
| 627 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 628 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
| 629 | generateLoad(Load, VectorMap, ScalarMaps); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | if (hasVectorOperands(Inst, VectorMap)) { |
| 634 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
| 635 | copyStore(Store, VectorMap, ScalarMaps); |
| 636 | return; |
| 637 | } |
| 638 | |
| 639 | if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { |
| 640 | copyUnaryInst(Unary, VectorMap, ScalarMaps); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { |
| 645 | copyBinaryInst(Binary, VectorMap, ScalarMaps); |
| 646 | return; |
| 647 | } |
| 648 | |
| 649 | // Falltrough: We generate scalar instructions, if we don't know how to |
| 650 | // generate vector code. |
| 651 | } |
| 652 | |
| 653 | copyInstScalarized(Inst, VectorMap, ScalarMaps); |
| 654 | } |
| 655 | |
| 656 | void VectorBlockGenerator::copyBB() { |
| 657 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 658 | BasicBlock *CopyBB = |
| 659 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 660 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 661 | Builder.SetInsertPoint(CopyBB->begin()); |
| 662 | |
| 663 | // Create two maps that store the mapping from the original instructions of |
| 664 | // the old basic block to their copies in the new basic block. Those maps |
| 665 | // are basic block local. |
| 666 | // |
| 667 | // As vector code generation is supported there is one map for scalar values |
| 668 | // and one for vector values. |
| 669 | // |
| 670 | // In case we just do scalar code generation, the vectorMap is not used and |
| 671 | // the scalarMap has just one dimension, which contains the mapping. |
| 672 | // |
| 673 | // In case vector code generation is done, an instruction may either appear |
| 674 | // in the vector map once (as it is calculating >vectorwidth< values at a |
| 675 | // time. Or (if the values are calculated using scalar operations), it |
| 676 | // appears once in every dimension of the scalarMap. |
| 677 | VectorValueMapT ScalarBlockMap(getVectorWidth()); |
| 678 | ValueMapT VectorBlockMap; |
| 679 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 680 | for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE; |
| 681 | ++II) |
| 682 | copyInstruction(II, VectorBlockMap, ScalarBlockMap); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 683 | } |