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