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