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" |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 18 | #include "isl/ast.h" |
| 19 | #include "isl/ast_build.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 20 | #include "isl/set.h" |
Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 21 | #include "polly/CodeGen/BlockGenerators.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 22 | #include "polly/CodeGen/CodeGeneration.h" |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 23 | #include "polly/CodeGen/IslExprBuilder.h" |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 24 | #include "polly/Options.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 25 | #include "polly/Support/GICHelper.h" |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 26 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 27 | #include "polly/Support/ScopHelper.h" |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/LoopInfo.h" |
| 29 | #include "llvm/Analysis/ScalarEvolution.h" |
| 30 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
Tobias Grosser | 030237d | 2014-02-21 15:06:05 +0000 | [diff] [blame] | 31 | #include "llvm/IR/IntrinsicInst.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 33 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 34 | using namespace llvm; |
| 35 | using namespace polly; |
| 36 | |
Tobias Grosser | 878aba4 | 2014-10-22 23:22:41 +0000 | [diff] [blame] | 37 | static cl::opt<bool> Aligned("enable-polly-aligned", |
| 38 | cl::desc("Assumed aligned memory accesses."), |
| 39 | cl::Hidden, cl::init(false), cl::ZeroOrMore, |
| 40 | cl::cat(PollyCategory)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 41 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 42 | static cl::opt<bool, true> |
Tobias Grosser | 483a90d | 2014-07-09 10:50:10 +0000 | [diff] [blame] | 43 | SCEVCodegenF("polly-codegen-scev", |
| 44 | cl::desc("Use SCEV based code generation."), cl::Hidden, |
| 45 | cl::location(SCEVCodegen), cl::init(false), cl::ZeroOrMore, |
| 46 | cl::cat(PollyCategory)); |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 47 | |
| 48 | bool polly::SCEVCodegen; |
| 49 | |
| 50 | bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI, |
| 51 | ScalarEvolution *SE, const Region *R) { |
| 52 | if (SCEVCodegen) { |
| 53 | if (!I || !SE->isSCEVable(I->getType())) |
| 54 | return false; |
| 55 | |
| 56 | if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I))) |
| 57 | if (!isa<SCEVCouldNotCompute>(Scev)) |
| 58 | if (!hasScalarDepsInsideRegion(Scev, R)) |
| 59 | return true; |
| 60 | |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | Loop *L = LI->getLoopFor(I->getParent()); |
Tobias Grosser | e8df5bd | 2013-04-17 07:20:36 +0000 | [diff] [blame] | 65 | return L && I == L->getCanonicalInductionVariable() && R->contains(L); |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 68 | BlockGenerator::BlockGenerator(PollyIRBuilder &B, ScopStmt &Stmt, Pass *P, |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 69 | LoopInfo &LI, ScalarEvolution &SE, |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 70 | isl_ast_build *Build, |
| 71 | IslExprBuilder *ExprBuilder) |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 72 | : Builder(B), Statement(Stmt), P(P), LI(LI), SE(SE), Build(Build), |
| 73 | ExprBuilder(ExprBuilder) {} |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 74 | |
Tobias Grosser | d213a8b | 2014-11-05 19:46:04 +0000 | [diff] [blame] | 75 | Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap, |
| 76 | ValueMapT &GlobalMap, LoopToScevMapT <S, |
| 77 | Loop *L) const { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 78 | // We assume constants never change. |
| 79 | // This avoids map lookups for many calls to this function. |
| 80 | if (isa<Constant>(Old)) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 81 | return const_cast<Value *>(Old); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 82 | |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 83 | if (Value *New = GlobalMap.lookup(Old)) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 84 | if (Old->getType()->getScalarSizeInBits() < |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 85 | New->getType()->getScalarSizeInBits()) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 86 | New = Builder.CreateTruncOrBitCast(New, Old->getType()); |
| 87 | |
| 88 | return New; |
| 89 | } |
| 90 | |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 91 | if (Value *New = BBMap.lookup(Old)) |
| 92 | return New; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 93 | |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 94 | if (SCEVCodegen && SE.isSCEVable(Old->getType())) |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 95 | if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) { |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 96 | if (!isa<SCEVCouldNotCompute>(Scev)) { |
Sebastian Pop | 637b23d | 2013-02-15 20:56:01 +0000 | [diff] [blame] | 97 | const SCEV *NewScev = apply(Scev, LTS, SE); |
| 98 | ValueToValueMap VTV; |
| 99 | VTV.insert(BBMap.begin(), BBMap.end()); |
| 100 | VTV.insert(GlobalMap.begin(), GlobalMap.end()); |
Sebastian Pop | 47d4ee3 | 2013-02-15 21:26:53 +0000 | [diff] [blame] | 101 | NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV); |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 102 | SCEVExpander Expander(SE, "polly"); |
| 103 | Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(), |
| 104 | Builder.GetInsertPoint()); |
| 105 | |
| 106 | BBMap[Old] = Expanded; |
| 107 | return Expanded; |
| 108 | } |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 109 | } |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 110 | |
Tobias Grosser | 16371ac | 2014-11-05 20:48:56 +0000 | [diff] [blame^] | 111 | // A scop-constant value defined by a global or a function parameter. |
| 112 | if (isa<GlobalValue>(Old) || isa<Argument>(Old)) |
| 113 | return const_cast<Value *>(Old); |
| 114 | |
| 115 | // A scop-constant value defined by an instruction executed outside the scop. |
| 116 | if (const Instruction *Inst = dyn_cast<Instruction>(Old)) |
| 117 | if (!Statement.getParent()->getRegion().contains(Inst->getParent())) |
| 118 | return const_cast<Value *>(Old); |
| 119 | |
| 120 | // The scalar dependence is neither available nor SCEVCodegenable. |
Hongbin Zheng | 5b463ce | 2013-07-25 09:12:07 +0000 | [diff] [blame] | 121 | llvm_unreachable("Unexpected scalar dependence in region!"); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 122 | return nullptr; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap, |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 126 | ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Tobias Grosser | 030237d | 2014-02-21 15:06:05 +0000 | [diff] [blame] | 127 | // We do not generate debug intrinsics as we did not investigate how to |
| 128 | // copy them correctly. At the current state, they just crash the code |
| 129 | // generation as the meta-data operands are not correctly copied. |
| 130 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 131 | return; |
| 132 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 133 | Instruction *NewInst = Inst->clone(); |
| 134 | |
| 135 | // Replace old operands with the new ones. |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 136 | for (Value *OldOperand : Inst->operands()) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 137 | Value *NewOperand = |
| 138 | getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 139 | |
| 140 | if (!NewOperand) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 141 | assert(!isa<StoreInst>(NewInst) && |
| 142 | "Store instructions are always needed!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 143 | delete NewInst; |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | NewInst->replaceUsesOfWith(OldOperand, NewOperand); |
| 148 | } |
| 149 | |
| 150 | Builder.Insert(NewInst); |
| 151 | BBMap[Inst] = NewInst; |
| 152 | |
| 153 | if (!NewInst->getType()->isVoidTy()) |
| 154 | NewInst->setName("p_" + Inst->getName()); |
| 155 | } |
| 156 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 157 | Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) { |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 158 | isl_pw_multi_aff *PWAccRel; |
| 159 | isl_union_map *Schedule; |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 160 | isl_ast_expr *Expr; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 161 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 162 | assert(ExprBuilder && Build && |
| 163 | "Cannot generate new value without IslExprBuilder!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 164 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 165 | Schedule = isl_ast_build_get_schedule(Build); |
| 166 | PWAccRel = MA.applyScheduleToAccessRelation(Schedule); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 167 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 168 | Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel); |
Johannes Doerfert | dcb5f1d | 2014-09-18 11:14:30 +0000 | [diff] [blame] | 169 | Expr = isl_ast_expr_address_of(Expr); |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 170 | |
| 171 | return ExprBuilder->create(Expr); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 174 | Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst, |
| 175 | const Value *Pointer, |
| 176 | ValueMapT &BBMap, |
| 177 | ValueMapT &GlobalMap, |
| 178 | LoopToScevMapT <S) { |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 179 | const MemoryAccess &MA = Statement.getAccessFor(Inst); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 180 | |
| 181 | Value *NewPointer; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 182 | if (MA.hasNewAccessRelation()) |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 183 | NewPointer = getNewAccessOperand(MA); |
| 184 | else |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 185 | NewPointer = |
| 186 | getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 187 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 188 | return NewPointer; |
| 189 | } |
| 190 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 191 | Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) { |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 192 | return LI.getLoopFor(Inst->getParent()); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 195 | Value *BlockGenerator::generateScalarLoad(const LoadInst *Load, |
| 196 | ValueMapT &BBMap, |
| 197 | ValueMapT &GlobalMap, |
| 198 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 199 | const Value *Pointer = Load->getPointerOperand(); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 200 | Value *NewPointer = |
Johannes Doerfert | 1947f86 | 2014-10-08 20:18:32 +0000 | [diff] [blame] | 201 | generateLocationAccessed(Load, Pointer, BBMap, GlobalMap, LTS); |
Johannes Doerfert | 8790145 | 2014-10-02 16:22:19 +0000 | [diff] [blame] | 202 | Value *ScalarLoad = Builder.CreateAlignedLoad( |
| 203 | NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 204 | return ScalarLoad; |
| 205 | } |
| 206 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 207 | Value *BlockGenerator::generateScalarStore(const StoreInst *Store, |
| 208 | ValueMapT &BBMap, |
| 209 | ValueMapT &GlobalMap, |
| 210 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 211 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 212 | Value *NewPointer = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 213 | generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 214 | Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap, |
| 215 | LTS, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 216 | |
Johannes Doerfert | 8790145 | 2014-10-02 16:22:19 +0000 | [diff] [blame] | 217 | Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer, |
| 218 | Store->getAlignment()); |
| 219 | return NewStore; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 222 | void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap, |
| 223 | ValueMapT &GlobalMap, |
| 224 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 225 | // Terminator instructions control the control flow. They are explicitly |
| 226 | // expressed in the clast and do not need to be copied. |
| 227 | if (Inst->isTerminator()) |
| 228 | return; |
| 229 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 230 | if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE, |
| 231 | &Statement.getParent()->getRegion())) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 232 | return; |
| 233 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 234 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 235 | Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS); |
Sebastian Pop | 3d94fed | 2013-05-24 18:46:02 +0000 | [diff] [blame] | 236 | // Compute NewLoad before its insertion in BBMap to make the insertion |
| 237 | // deterministic. |
Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 238 | BBMap[Load] = NewLoad; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 243 | Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS); |
Sebastian Pop | 3d94fed | 2013-05-24 18:46:02 +0000 | [diff] [blame] | 244 | // Compute NewStore before its insertion in BBMap to make the insertion |
| 245 | // deterministic. |
Sebastian Pop | 753d43f | 2013-05-24 17:16:02 +0000 | [diff] [blame] | 246 | BBMap[Store] = NewStore; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 250 | copyInstScalar(Inst, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 253 | void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 254 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 255 | BasicBlock *CopyBB = |
| 256 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 257 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 258 | Builder.SetInsertPoint(CopyBB->begin()); |
| 259 | |
| 260 | ValueMapT BBMap; |
| 261 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 262 | for (Instruction &Inst : *BB) |
| 263 | copyInstruction(&Inst, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 266 | VectorBlockGenerator::VectorBlockGenerator( |
| 267 | PollyIRBuilder &B, VectorValueMapT &GlobalMaps, |
| 268 | std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt, |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 269 | __isl_keep isl_map *Schedule, Pass *P, LoopInfo &LI, ScalarEvolution &SE, |
| 270 | __isl_keep isl_ast_build *Build, IslExprBuilder *ExprBuilder) |
| 271 | : BlockGenerator(B, Stmt, P, LI, SE, Build, ExprBuilder), |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 272 | GlobalMaps(GlobalMaps), VLTS(VLTS), Schedule(Schedule) { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 273 | assert(GlobalMaps.size() > 1 && "Only one vector lane found"); |
| 274 | assert(Schedule && "No statement domain provided"); |
| 275 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 276 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 277 | Value *VectorBlockGenerator::getVectorValue(const Value *Old, |
| 278 | ValueMapT &VectorMap, |
| 279 | VectorValueMapT &ScalarMaps, |
| 280 | Loop *L) { |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 281 | if (Value *NewValue = VectorMap.lookup(Old)) |
| 282 | return NewValue; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 283 | |
| 284 | int Width = getVectorWidth(); |
| 285 | |
| 286 | Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); |
| 287 | |
| 288 | for (int Lane = 0; Lane < Width; Lane++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 289 | Vector = Builder.CreateInsertElement( |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 290 | Vector, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 291 | getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L), |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 292 | Builder.getInt32(Lane)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 293 | |
| 294 | VectorMap[Old] = Vector; |
| 295 | |
| 296 | return Vector; |
| 297 | } |
| 298 | |
| 299 | Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { |
| 300 | PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); |
| 301 | assert(PointerTy && "PointerType expected"); |
| 302 | |
| 303 | Type *ScalarType = PointerTy->getElementType(); |
| 304 | VectorType *VectorType = VectorType::get(ScalarType, Width); |
| 305 | |
| 306 | return PointerType::getUnqual(VectorType); |
| 307 | } |
| 308 | |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 309 | Value * |
| 310 | VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load, |
| 311 | VectorValueMapT &ScalarMaps, |
| 312 | bool NegativeStride = false) { |
| 313 | unsigned VectorWidth = getVectorWidth(); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 314 | const Value *Pointer = Load->getPointerOperand(); |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 315 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); |
| 316 | unsigned Offset = NegativeStride ? VectorWidth - 1 : 0; |
| 317 | |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 318 | Value *NewPointer = nullptr; |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 319 | NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[Offset], |
| 320 | GlobalMaps[Offset], VLTS[Offset]); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 321 | Value *VectorPtr = |
| 322 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
| 323 | LoadInst *VecLoad = |
| 324 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 325 | if (!Aligned) |
| 326 | VecLoad->setAlignment(8); |
| 327 | |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 328 | if (NegativeStride) { |
| 329 | SmallVector<Constant *, 16> Indices; |
| 330 | for (int i = VectorWidth - 1; i >= 0; i--) |
| 331 | Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i)); |
| 332 | Constant *SV = llvm::ConstantVector::get(Indices); |
| 333 | Value *RevVecLoad = Builder.CreateShuffleVector( |
| 334 | VecLoad, VecLoad, SV, Load->getName() + "_reverse"); |
| 335 | return RevVecLoad; |
| 336 | } |
| 337 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 338 | return VecLoad; |
| 339 | } |
| 340 | |
| 341 | Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load, |
| 342 | ValueMapT &BBMap) { |
| 343 | const Value *Pointer = Load->getPointerOperand(); |
| 344 | Type *VectorPtrType = getVectorPtrTy(Pointer, 1); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 345 | Value *NewPointer = |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 346 | generateLocationAccessed(Load, Pointer, BBMap, GlobalMaps[0], VLTS[0]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 347 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 348 | Load->getName() + "_p_vec_p"); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 349 | LoadInst *ScalarLoad = |
| 350 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 351 | |
| 352 | if (!Aligned) |
| 353 | ScalarLoad->setAlignment(8); |
| 354 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 355 | Constant *SplatVector = Constant::getNullValue( |
| 356 | VectorType::get(Builder.getInt32Ty(), getVectorWidth())); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 357 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 358 | Value *VectorLoad = Builder.CreateShuffleVector( |
| 359 | ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 360 | return VectorLoad; |
| 361 | } |
| 362 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 363 | Value * |
| 364 | VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load, |
| 365 | VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 366 | int VectorWidth = getVectorWidth(); |
| 367 | const Value *Pointer = Load->getPointerOperand(); |
| 368 | VectorType *VectorType = VectorType::get( |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 369 | dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 370 | |
| 371 | Value *Vector = UndefValue::get(VectorType); |
| 372 | |
| 373 | for (int i = 0; i < VectorWidth; i++) { |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 374 | Value *NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[i], |
| 375 | GlobalMaps[i], VLTS[i]); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 376 | Value *ScalarLoad = |
| 377 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
| 378 | Vector = Builder.CreateInsertElement( |
| 379 | Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return Vector; |
| 383 | } |
| 384 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 385 | void VectorBlockGenerator::generateLoad(const LoadInst *Load, |
| 386 | ValueMapT &VectorMap, |
| 387 | VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 388 | if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL || |
| 389 | !VectorType::isValidElementType(Load->getType())) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 390 | for (int i = 0; i < getVectorWidth(); i++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 391 | ScalarMaps[i][Load] = |
Sebastian Pop | 9d10fff | 2013-02-15 20:55:59 +0000 | [diff] [blame] | 392 | generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 393 | return; |
| 394 | } |
| 395 | |
Hongbin Zheng | b5f24a6 | 2013-06-29 06:31:39 +0000 | [diff] [blame] | 396 | const MemoryAccess &Access = Statement.getAccessFor(Load); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 397 | |
Tobias Grosser | 9549398 | 2014-04-18 09:46:35 +0000 | [diff] [blame] | 398 | // Make sure we have scalar values available to access the pointer to |
| 399 | // the data location. |
| 400 | extractScalarValues(Load, VectorMap, ScalarMaps); |
| 401 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 402 | Value *NewLoad; |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 403 | if (Access.isStrideZero(isl_map_copy(Schedule))) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 404 | NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 405 | else if (Access.isStrideOne(isl_map_copy(Schedule))) |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 406 | NewLoad = generateStrideOneLoad(Load, ScalarMaps); |
| 407 | else if (Access.isStrideX(isl_map_copy(Schedule), -1)) |
| 408 | NewLoad = generateStrideOneLoad(Load, ScalarMaps, true); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 409 | else |
| 410 | NewLoad = generateUnknownStrideLoad(Load, ScalarMaps); |
| 411 | |
| 412 | VectorMap[Load] = NewLoad; |
| 413 | } |
| 414 | |
| 415 | void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst, |
| 416 | ValueMapT &VectorMap, |
| 417 | VectorValueMapT &ScalarMaps) { |
| 418 | int VectorWidth = getVectorWidth(); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 419 | Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps, |
| 420 | getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 421 | |
| 422 | assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); |
| 423 | |
| 424 | const CastInst *Cast = dyn_cast<CastInst>(Inst); |
| 425 | VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); |
| 426 | VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); |
| 427 | } |
| 428 | |
| 429 | void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst, |
| 430 | ValueMapT &VectorMap, |
| 431 | VectorValueMapT &ScalarMaps) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 432 | Loop *L = getLoopForInst(Inst); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 433 | Value *OpZero = Inst->getOperand(0); |
| 434 | Value *OpOne = Inst->getOperand(1); |
| 435 | |
| 436 | Value *NewOpZero, *NewOpOne; |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 437 | NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L); |
| 438 | NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 439 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 440 | Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 441 | Inst->getName() + "p_vec"); |
| 442 | VectorMap[Inst] = NewInst; |
| 443 | } |
| 444 | |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 445 | void VectorBlockGenerator::copyStore(const StoreInst *Store, |
| 446 | ValueMapT &VectorMap, |
| 447 | VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | b5f24a6 | 2013-06-29 06:31:39 +0000 | [diff] [blame] | 448 | const MemoryAccess &Access = Statement.getAccessFor(Store); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 449 | |
| 450 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 451 | Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap, |
| 452 | ScalarMaps, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 453 | |
Tobias Grosser | 50fd701 | 2014-04-17 23:13:49 +0000 | [diff] [blame] | 454 | // Make sure we have scalar values available to access the pointer to |
| 455 | // the data location. |
| 456 | extractScalarValues(Store, VectorMap, ScalarMaps); |
| 457 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 458 | if (Access.isStrideOne(isl_map_copy(Schedule))) { |
Johannes Doerfert | 1947f86 | 2014-10-08 20:18:32 +0000 | [diff] [blame] | 459 | Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 460 | Value *NewPointer = generateLocationAccessed(Store, Pointer, ScalarMaps[0], |
| 461 | GlobalMaps[0], VLTS[0]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 462 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 463 | Value *VectorPtr = |
| 464 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 465 | StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); |
| 466 | |
| 467 | if (!Aligned) |
| 468 | Store->setAlignment(8); |
| 469 | } else { |
| 470 | for (unsigned i = 0; i < ScalarMaps.size(); i++) { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 471 | Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 472 | Value *NewPointer = generateLocationAccessed( |
| 473 | Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 474 | Builder.CreateStore(Scalar, NewPointer); |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, |
| 480 | ValueMapT &VectorMap) { |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 481 | for (Value *Operand : Inst->operands()) |
| 482 | if (VectorMap.count(Operand)) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 483 | return true; |
| 484 | return false; |
| 485 | } |
| 486 | |
| 487 | bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, |
| 488 | ValueMapT &VectorMap, |
| 489 | VectorValueMapT &ScalarMaps) { |
| 490 | bool HasVectorOperand = false; |
| 491 | int VectorWidth = getVectorWidth(); |
| 492 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 493 | for (Value *Operand : Inst->operands()) { |
| 494 | ValueMapT::iterator VecOp = VectorMap.find(Operand); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 495 | |
| 496 | if (VecOp == VectorMap.end()) |
| 497 | continue; |
| 498 | |
| 499 | HasVectorOperand = true; |
| 500 | Value *NewVector = VecOp->second; |
| 501 | |
| 502 | for (int i = 0; i < VectorWidth; ++i) { |
| 503 | ValueMapT &SM = ScalarMaps[i]; |
| 504 | |
| 505 | // If there is one scalar extracted, all scalar elements should have |
| 506 | // already been extracted by the code here. So no need to check for the |
| 507 | // existance of all of them. |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 508 | if (SM.count(Operand)) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 509 | break; |
| 510 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 511 | SM[Operand] = |
| 512 | Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | |
| 516 | return HasVectorOperand; |
| 517 | } |
| 518 | |
| 519 | void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst, |
| 520 | ValueMapT &VectorMap, |
| 521 | VectorValueMapT &ScalarMaps) { |
| 522 | bool HasVectorOperand; |
| 523 | int VectorWidth = getVectorWidth(); |
| 524 | |
| 525 | HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); |
| 526 | |
| 527 | for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 528 | BlockGenerator::copyInstruction(Inst, ScalarMaps[VectorLane], |
| 529 | GlobalMaps[VectorLane], VLTS[VectorLane]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 530 | |
| 531 | if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) |
| 532 | return; |
| 533 | |
| 534 | // Make the result available as vector value. |
| 535 | VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); |
| 536 | Value *Vector = UndefValue::get(VectorType); |
| 537 | |
| 538 | for (int i = 0; i < VectorWidth; i++) |
| 539 | Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], |
| 540 | Builder.getInt32(i)); |
| 541 | |
| 542 | VectorMap[Inst] = Vector; |
| 543 | } |
| 544 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 545 | int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 546 | |
| 547 | void VectorBlockGenerator::copyInstruction(const Instruction *Inst, |
| 548 | ValueMapT &VectorMap, |
| 549 | VectorValueMapT &ScalarMaps) { |
| 550 | // Terminator instructions control the control flow. They are explicitly |
| 551 | // expressed in the clast and do not need to be copied. |
| 552 | if (Inst->isTerminator()) |
| 553 | return; |
| 554 | |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 555 | if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE, |
| 556 | &Statement.getParent()->getRegion())) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 557 | return; |
| 558 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 559 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
| 560 | generateLoad(Load, VectorMap, ScalarMaps); |
| 561 | return; |
| 562 | } |
| 563 | |
| 564 | if (hasVectorOperands(Inst, VectorMap)) { |
| 565 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
| 566 | copyStore(Store, VectorMap, ScalarMaps); |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { |
| 571 | copyUnaryInst(Unary, VectorMap, ScalarMaps); |
| 572 | return; |
| 573 | } |
| 574 | |
| 575 | if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { |
| 576 | copyBinaryInst(Binary, VectorMap, ScalarMaps); |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | // Falltrough: We generate scalar instructions, if we don't know how to |
| 581 | // generate vector code. |
| 582 | } |
| 583 | |
| 584 | copyInstScalarized(Inst, VectorMap, ScalarMaps); |
| 585 | } |
| 586 | |
| 587 | void VectorBlockGenerator::copyBB() { |
| 588 | BasicBlock *BB = Statement.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 589 | BasicBlock *CopyBB = |
| 590 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 591 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 592 | Builder.SetInsertPoint(CopyBB->begin()); |
| 593 | |
| 594 | // Create two maps that store the mapping from the original instructions of |
| 595 | // the old basic block to their copies in the new basic block. Those maps |
| 596 | // are basic block local. |
| 597 | // |
| 598 | // As vector code generation is supported there is one map for scalar values |
| 599 | // and one for vector values. |
| 600 | // |
| 601 | // In case we just do scalar code generation, the vectorMap is not used and |
| 602 | // the scalarMap has just one dimension, which contains the mapping. |
| 603 | // |
| 604 | // In case vector code generation is done, an instruction may either appear |
| 605 | // in the vector map once (as it is calculating >vectorwidth< values at a |
| 606 | // time. Or (if the values are calculated using scalar operations), it |
| 607 | // appears once in every dimension of the scalarMap. |
| 608 | VectorValueMapT ScalarBlockMap(getVectorWidth()); |
| 609 | ValueMapT VectorBlockMap; |
| 610 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 611 | for (Instruction &Inst : *BB) |
| 612 | copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 613 | } |