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 | b4f08eb | 2015-02-23 13:51:35 +0000 | [diff] [blame] | 79 | BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI, |
| 80 | ScalarEvolution &SE, DominatorTree &DT, |
| 81 | IslExprBuilder *ExprBuilder) |
| 82 | : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT) {} |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 83 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 84 | Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old, |
| 85 | ValueMapT &BBMap, ValueMapT &GlobalMap, |
| 86 | LoopToScevMapT <S, Loop *L) const { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 87 | // We assume constants never change. |
| 88 | // This avoids map lookups for many calls to this function. |
| 89 | if (isa<Constant>(Old)) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 90 | return const_cast<Value *>(Old); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 91 | |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 92 | if (Value *New = GlobalMap.lookup(Old)) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 93 | if (Old->getType()->getScalarSizeInBits() < |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 94 | New->getType()->getScalarSizeInBits()) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 95 | New = Builder.CreateTruncOrBitCast(New, Old->getType()); |
| 96 | |
| 97 | return New; |
| 98 | } |
| 99 | |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 100 | if (Value *New = BBMap.lookup(Old)) |
| 101 | return New; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 102 | |
Tobias Grosser | 683b8e4 | 2014-11-30 14:33:31 +0000 | [diff] [blame] | 103 | if (SE.isSCEVable(Old->getType())) |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 104 | if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) { |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 105 | if (!isa<SCEVCouldNotCompute>(Scev)) { |
Sebastian Pop | 637b23d | 2013-02-15 20:56:01 +0000 | [diff] [blame] | 106 | const SCEV *NewScev = apply(Scev, LTS, SE); |
| 107 | ValueToValueMap VTV; |
| 108 | VTV.insert(BBMap.begin(), BBMap.end()); |
| 109 | VTV.insert(GlobalMap.begin(), GlobalMap.end()); |
Sebastian Pop | 47d4ee3 | 2013-02-15 21:26:53 +0000 | [diff] [blame] | 110 | NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV); |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 111 | SCEVExpander Expander(SE, "polly"); |
| 112 | Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(), |
| 113 | Builder.GetInsertPoint()); |
| 114 | |
| 115 | BBMap[Old] = Expanded; |
| 116 | return Expanded; |
| 117 | } |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 118 | } |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 119 | |
Tobias Grosser | 16371ac | 2014-11-05 20:48:56 +0000 | [diff] [blame] | 120 | // A scop-constant value defined by a global or a function parameter. |
| 121 | if (isa<GlobalValue>(Old) || isa<Argument>(Old)) |
| 122 | return const_cast<Value *>(Old); |
| 123 | |
| 124 | // A scop-constant value defined by an instruction executed outside the scop. |
| 125 | if (const Instruction *Inst = dyn_cast<Instruction>(Old)) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 126 | if (!Stmt.getParent()->getRegion().contains(Inst->getParent())) |
Tobias Grosser | 16371ac | 2014-11-05 20:48:56 +0000 | [diff] [blame] | 127 | return const_cast<Value *>(Old); |
| 128 | |
| 129 | // The scalar dependence is neither available nor SCEVCodegenable. |
Hongbin Zheng | 5b463ce | 2013-07-25 09:12:07 +0000 | [diff] [blame] | 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 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 134 | void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst, |
| 135 | ValueMapT &BBMap, ValueMapT &GlobalMap, |
| 136 | LoopToScevMapT <S) { |
Tobias Grosser | 030237d | 2014-02-21 15:06:05 +0000 | [diff] [blame] | 137 | // We do not generate debug intrinsics as we did not investigate how to |
| 138 | // copy them correctly. At the current state, they just crash the code |
| 139 | // generation as the meta-data operands are not correctly copied. |
| 140 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 141 | return; |
| 142 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 143 | Instruction *NewInst = Inst->clone(); |
| 144 | |
| 145 | // Replace old operands with the new ones. |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 146 | for (Value *OldOperand : Inst->operands()) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 147 | Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS, |
| 148 | getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 149 | |
| 150 | if (!NewOperand) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 151 | assert(!isa<StoreInst>(NewInst) && |
| 152 | "Store instructions are always needed!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 153 | delete NewInst; |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | NewInst->replaceUsesOfWith(OldOperand, NewOperand); |
| 158 | } |
| 159 | |
| 160 | Builder.Insert(NewInst); |
| 161 | BBMap[Inst] = NewInst; |
| 162 | |
| 163 | if (!NewInst->getType()->isVoidTy()) |
| 164 | NewInst->setName("p_" + Inst->getName()); |
| 165 | } |
| 166 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 167 | Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt, |
| 168 | const MemoryAccess &MA) { |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 169 | isl_pw_multi_aff *PWAccRel; |
| 170 | isl_union_map *Schedule; |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 171 | isl_ast_expr *Expr; |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 172 | isl_ast_build *Build = Stmt.getAstBuild(); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 173 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 174 | assert(ExprBuilder && Build && |
| 175 | "Cannot generate new value without IslExprBuilder!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 176 | |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 177 | Schedule = isl_ast_build_get_schedule(Build); |
| 178 | PWAccRel = MA.applyScheduleToAccessRelation(Schedule); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 179 | |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 180 | Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel); |
Johannes Doerfert | dcb5f1d | 2014-09-18 11:14:30 +0000 | [diff] [blame] | 181 | Expr = isl_ast_expr_address_of(Expr); |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 182 | |
| 183 | return ExprBuilder->create(Expr); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 186 | Value *BlockGenerator::generateLocationAccessed( |
| 187 | ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer, |
| 188 | ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT <S) { |
| 189 | const MemoryAccess &MA = Stmt.getAccessFor(Inst); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 190 | |
| 191 | Value *NewPointer; |
Johannes Doerfert | a99130f | 2014-10-13 12:58:03 +0000 | [diff] [blame] | 192 | if (MA.hasNewAccessRelation()) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 193 | NewPointer = getNewAccessOperand(Stmt, MA); |
Johannes Doerfert | a63b257 | 2014-08-03 01:51:59 +0000 | [diff] [blame] | 194 | else |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 195 | NewPointer = |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 196 | getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 197 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 198 | return NewPointer; |
| 199 | } |
| 200 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 201 | Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) { |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 202 | return LI.getLoopFor(Inst->getParent()); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 205 | Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 206 | ValueMapT &BBMap, |
| 207 | ValueMapT &GlobalMap, |
| 208 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 209 | const Value *Pointer = Load->getPointerOperand(); |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 210 | Value *NewPointer = |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 211 | generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS); |
Johannes Doerfert | 8790145 | 2014-10-02 16:22:19 +0000 | [diff] [blame] | 212 | Value *ScalarLoad = Builder.CreateAlignedLoad( |
| 213 | NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 214 | return ScalarLoad; |
| 215 | } |
| 216 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 217 | Value *BlockGenerator::generateScalarStore(ScopStmt &Stmt, |
| 218 | const StoreInst *Store, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 219 | ValueMapT &BBMap, |
| 220 | ValueMapT &GlobalMap, |
| 221 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 222 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 223 | Value *NewPointer = |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 224 | generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS); |
| 225 | Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap, |
| 226 | GlobalMap, LTS, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 227 | |
Johannes Doerfert | 8790145 | 2014-10-02 16:22:19 +0000 | [diff] [blame] | 228 | Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer, |
| 229 | Store->getAlignment()); |
| 230 | return NewStore; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 233 | void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst, |
| 234 | ValueMapT &BBMap, ValueMapT &GlobalMap, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 235 | LoopToScevMapT <S) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 236 | // Terminator instructions control the control flow. They are explicitly |
| 237 | // expressed in the clast and do not need to be copied. |
| 238 | if (Inst->isTerminator()) |
| 239 | return; |
| 240 | |
Johannes Doerfert | 1ef5233 | 2015-02-08 20:50:42 +0000 | [diff] [blame] | 241 | if (canSynthesize(Inst, &LI, &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 | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 287 | void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap, |
| 288 | LoopToScevMapT <S) { |
| 289 | assert(Stmt.isBlockStmt() && |
| 290 | "Only block statements can be copied by the block generator"); |
| 291 | |
| 292 | ValueMapT BBMap; |
| 293 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 294 | BasicBlock *BB = Stmt.getBasicBlock(); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 295 | copyBB(Stmt, BB, BBMap, GlobalMap, LTS); |
| 296 | } |
| 297 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 298 | BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 299 | BasicBlock *CopyBB = |
Johannes Doerfert | b4f08eb | 2015-02-23 13:51:35 +0000 | [diff] [blame] | 300 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 301 | CopyBB->setName("polly.stmt." + BB->getName()); |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 302 | return CopyBB; |
| 303 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 304 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 305 | BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, |
| 306 | ValueMapT &BBMap, ValueMapT &GlobalMap, |
| 307 | LoopToScevMapT <S) { |
| 308 | BasicBlock *CopyBB = splitBB(BB); |
| 309 | copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS); |
| 310 | return CopyBB; |
| 311 | } |
| 312 | |
| 313 | void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB, |
| 314 | ValueMapT &BBMap, ValueMapT &GlobalMap, |
| 315 | LoopToScevMapT <S) { |
| 316 | Builder.SetInsertPoint(CopyBB->begin()); |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 317 | for (Instruction &Inst : *BB) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 318 | copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 321 | VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen, |
| 322 | VectorValueMapT &GlobalMaps, |
| 323 | std::vector<LoopToScevMapT> &VLTS, |
| 324 | isl_map *Schedule) |
| 325 | : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS), |
| 326 | Schedule(Schedule) { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 327 | assert(GlobalMaps.size() > 1 && "Only one vector lane found"); |
| 328 | assert(Schedule && "No statement domain provided"); |
| 329 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 330 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 331 | Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 332 | ValueMapT &VectorMap, |
| 333 | VectorValueMapT &ScalarMaps, |
| 334 | Loop *L) { |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 335 | if (Value *NewValue = VectorMap.lookup(Old)) |
| 336 | return NewValue; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 337 | |
| 338 | int Width = getVectorWidth(); |
| 339 | |
| 340 | Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); |
| 341 | |
| 342 | for (int Lane = 0; Lane < Width; Lane++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 343 | Vector = Builder.CreateInsertElement( |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 344 | Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane], |
| 345 | VLTS[Lane], L), |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 346 | Builder.getInt32(Lane)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 347 | |
| 348 | VectorMap[Old] = Vector; |
| 349 | |
| 350 | return Vector; |
| 351 | } |
| 352 | |
| 353 | Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { |
| 354 | PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); |
| 355 | assert(PointerTy && "PointerType expected"); |
| 356 | |
| 357 | Type *ScalarType = PointerTy->getElementType(); |
| 358 | VectorType *VectorType = VectorType::get(ScalarType, Width); |
| 359 | |
| 360 | return PointerType::getUnqual(VectorType); |
| 361 | } |
| 362 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 363 | Value *VectorBlockGenerator::generateStrideOneLoad( |
| 364 | ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps, |
| 365 | bool NegativeStride = false) { |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 366 | unsigned VectorWidth = getVectorWidth(); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 367 | const Value *Pointer = Load->getPointerOperand(); |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 368 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); |
| 369 | unsigned Offset = NegativeStride ? VectorWidth - 1 : 0; |
| 370 | |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 371 | Value *NewPointer = nullptr; |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 372 | NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset], |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 373 | GlobalMaps[Offset], VLTS[Offset]); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 374 | Value *VectorPtr = |
| 375 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
| 376 | LoadInst *VecLoad = |
| 377 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 378 | if (!Aligned) |
| 379 | VecLoad->setAlignment(8); |
| 380 | |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 381 | if (NegativeStride) { |
| 382 | SmallVector<Constant *, 16> Indices; |
| 383 | for (int i = VectorWidth - 1; i >= 0; i--) |
| 384 | Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i)); |
| 385 | Constant *SV = llvm::ConstantVector::get(Indices); |
| 386 | Value *RevVecLoad = Builder.CreateShuffleVector( |
| 387 | VecLoad, VecLoad, SV, Load->getName() + "_reverse"); |
| 388 | return RevVecLoad; |
| 389 | } |
| 390 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 391 | return VecLoad; |
| 392 | } |
| 393 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 394 | Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt, |
| 395 | const LoadInst *Load, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 396 | ValueMapT &BBMap) { |
| 397 | const Value *Pointer = Load->getPointerOperand(); |
| 398 | Type *VectorPtrType = getVectorPtrTy(Pointer, 1); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 399 | Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap, |
| 400 | GlobalMaps[0], VLTS[0]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 401 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 402 | Load->getName() + "_p_vec_p"); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 403 | LoadInst *ScalarLoad = |
| 404 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 405 | |
| 406 | if (!Aligned) |
| 407 | ScalarLoad->setAlignment(8); |
| 408 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 409 | Constant *SplatVector = Constant::getNullValue( |
| 410 | VectorType::get(Builder.getInt32Ty(), getVectorWidth())); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 411 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 412 | Value *VectorLoad = Builder.CreateShuffleVector( |
| 413 | ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 414 | return VectorLoad; |
| 415 | } |
| 416 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 417 | Value *VectorBlockGenerator::generateUnknownStrideLoad( |
| 418 | ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 419 | int VectorWidth = getVectorWidth(); |
| 420 | const Value *Pointer = Load->getPointerOperand(); |
| 421 | VectorType *VectorType = VectorType::get( |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 422 | dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 423 | |
| 424 | Value *Vector = UndefValue::get(VectorType); |
| 425 | |
| 426 | for (int i = 0; i < VectorWidth; i++) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 427 | Value *NewPointer = generateLocationAccessed( |
| 428 | Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 429 | Value *ScalarLoad = |
| 430 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
| 431 | Vector = Builder.CreateInsertElement( |
| 432 | Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return Vector; |
| 436 | } |
| 437 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 438 | void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 439 | ValueMapT &VectorMap, |
| 440 | VectorValueMapT &ScalarMaps) { |
Hongbin Zheng | 6879421 | 2012-05-06 10:22:19 +0000 | [diff] [blame] | 441 | if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL || |
| 442 | !VectorType::isValidElementType(Load->getType())) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 443 | for (int i = 0; i < getVectorWidth(); i++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 444 | ScalarMaps[i][Load] = |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 445 | generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 446 | return; |
| 447 | } |
| 448 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 449 | const MemoryAccess &Access = Stmt.getAccessFor(Load); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 450 | |
Tobias Grosser | 9549398 | 2014-04-18 09:46:35 +0000 | [diff] [blame] | 451 | // Make sure we have scalar values available to access the pointer to |
| 452 | // the data location. |
| 453 | extractScalarValues(Load, VectorMap, ScalarMaps); |
| 454 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 455 | Value *NewLoad; |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 456 | if (Access.isStrideZero(isl_map_copy(Schedule))) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 457 | NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 458 | else if (Access.isStrideOne(isl_map_copy(Schedule))) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 459 | NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps); |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 460 | else if (Access.isStrideX(isl_map_copy(Schedule), -1)) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 461 | NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 462 | else |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 463 | NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 464 | |
| 465 | VectorMap[Load] = NewLoad; |
| 466 | } |
| 467 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 468 | void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt, |
| 469 | const UnaryInstruction *Inst, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 470 | ValueMapT &VectorMap, |
| 471 | VectorValueMapT &ScalarMaps) { |
| 472 | int VectorWidth = getVectorWidth(); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 473 | Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap, |
| 474 | ScalarMaps, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 475 | |
| 476 | assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); |
| 477 | |
| 478 | const CastInst *Cast = dyn_cast<CastInst>(Inst); |
| 479 | VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); |
| 480 | VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); |
| 481 | } |
| 482 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 483 | void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt, |
| 484 | const BinaryOperator *Inst, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 485 | ValueMapT &VectorMap, |
| 486 | VectorValueMapT &ScalarMaps) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 487 | Loop *L = getLoopForInst(Inst); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 488 | Value *OpZero = Inst->getOperand(0); |
| 489 | Value *OpOne = Inst->getOperand(1); |
| 490 | |
| 491 | Value *NewOpZero, *NewOpOne; |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 492 | NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L); |
| 493 | NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 494 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 495 | Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 496 | Inst->getName() + "p_vec"); |
| 497 | VectorMap[Inst] = NewInst; |
| 498 | } |
| 499 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 500 | void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 501 | ValueMapT &VectorMap, |
| 502 | VectorValueMapT &ScalarMaps) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 503 | const MemoryAccess &Access = Stmt.getAccessFor(Store); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 504 | |
| 505 | const Value *Pointer = Store->getPointerOperand(); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 506 | Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 507 | ScalarMaps, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 508 | |
Tobias Grosser | 50fd701 | 2014-04-17 23:13:49 +0000 | [diff] [blame] | 509 | // Make sure we have scalar values available to access the pointer to |
| 510 | // the data location. |
| 511 | extractScalarValues(Store, VectorMap, ScalarMaps); |
| 512 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 513 | if (Access.isStrideOne(isl_map_copy(Schedule))) { |
Johannes Doerfert | 1947f86 | 2014-10-08 20:18:32 +0000 | [diff] [blame] | 514 | Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 515 | Value *NewPointer = generateLocationAccessed( |
| 516 | Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 517 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 518 | Value *VectorPtr = |
| 519 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 520 | StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); |
| 521 | |
| 522 | if (!Aligned) |
| 523 | Store->setAlignment(8); |
| 524 | } else { |
| 525 | for (unsigned i = 0; i < ScalarMaps.size(); i++) { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 526 | Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 527 | Value *NewPointer = generateLocationAccessed( |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 528 | Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 529 | Builder.CreateStore(Scalar, NewPointer); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, |
| 535 | ValueMapT &VectorMap) { |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 536 | for (Value *Operand : Inst->operands()) |
| 537 | if (VectorMap.count(Operand)) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 538 | return true; |
| 539 | return false; |
| 540 | } |
| 541 | |
| 542 | bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, |
| 543 | ValueMapT &VectorMap, |
| 544 | VectorValueMapT &ScalarMaps) { |
| 545 | bool HasVectorOperand = false; |
| 546 | int VectorWidth = getVectorWidth(); |
| 547 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 548 | for (Value *Operand : Inst->operands()) { |
| 549 | ValueMapT::iterator VecOp = VectorMap.find(Operand); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 550 | |
| 551 | if (VecOp == VectorMap.end()) |
| 552 | continue; |
| 553 | |
| 554 | HasVectorOperand = true; |
| 555 | Value *NewVector = VecOp->second; |
| 556 | |
| 557 | for (int i = 0; i < VectorWidth; ++i) { |
| 558 | ValueMapT &SM = ScalarMaps[i]; |
| 559 | |
| 560 | // If there is one scalar extracted, all scalar elements should have |
| 561 | // already been extracted by the code here. So no need to check for the |
| 562 | // existance of all of them. |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 563 | if (SM.count(Operand)) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 564 | break; |
| 565 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 566 | SM[Operand] = |
| 567 | Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | return HasVectorOperand; |
| 572 | } |
| 573 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 574 | void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt, |
| 575 | const Instruction *Inst, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 576 | ValueMapT &VectorMap, |
| 577 | VectorValueMapT &ScalarMaps) { |
| 578 | bool HasVectorOperand; |
| 579 | int VectorWidth = getVectorWidth(); |
| 580 | |
| 581 | HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); |
| 582 | |
| 583 | for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 584 | BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane], |
Johannes Doerfert | 731685e | 2014-10-08 17:25:30 +0000 | [diff] [blame] | 585 | GlobalMaps[VectorLane], VLTS[VectorLane]); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 586 | |
| 587 | if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) |
| 588 | return; |
| 589 | |
| 590 | // Make the result available as vector value. |
| 591 | VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); |
| 592 | Value *Vector = UndefValue::get(VectorType); |
| 593 | |
| 594 | for (int i = 0; i < VectorWidth; i++) |
| 595 | Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], |
| 596 | Builder.getInt32(i)); |
| 597 | |
| 598 | VectorMap[Inst] = Vector; |
| 599 | } |
| 600 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 601 | int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 602 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 603 | void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt, |
| 604 | const Instruction *Inst, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 605 | ValueMapT &VectorMap, |
| 606 | VectorValueMapT &ScalarMaps) { |
| 607 | // Terminator instructions control the control flow. They are explicitly |
| 608 | // expressed in the clast and do not need to be copied. |
| 609 | if (Inst->isTerminator()) |
| 610 | return; |
| 611 | |
Johannes Doerfert | 1ef5233 | 2015-02-08 20:50:42 +0000 | [diff] [blame] | 612 | if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 613 | return; |
| 614 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 615 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 616 | generateLoad(Stmt, Load, VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 617 | return; |
| 618 | } |
| 619 | |
| 620 | if (hasVectorOperands(Inst, VectorMap)) { |
| 621 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 622 | copyStore(Stmt, Store, VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 623 | return; |
| 624 | } |
| 625 | |
| 626 | if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 627 | copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | |
| 631 | if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 632 | copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 633 | return; |
| 634 | } |
| 635 | |
| 636 | // Falltrough: We generate scalar instructions, if we don't know how to |
| 637 | // generate vector code. |
| 638 | } |
| 639 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 640 | copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 643 | void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) { |
| 644 | assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by " |
| 645 | "the vector block generator"); |
| 646 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 647 | BasicBlock *BB = Stmt.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 648 | BasicBlock *CopyBB = |
Johannes Doerfert | b4f08eb | 2015-02-23 13:51:35 +0000 | [diff] [blame] | 649 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 650 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 651 | Builder.SetInsertPoint(CopyBB->begin()); |
| 652 | |
| 653 | // Create two maps that store the mapping from the original instructions of |
| 654 | // the old basic block to their copies in the new basic block. Those maps |
| 655 | // are basic block local. |
| 656 | // |
| 657 | // As vector code generation is supported there is one map for scalar values |
| 658 | // and one for vector values. |
| 659 | // |
| 660 | // In case we just do scalar code generation, the vectorMap is not used and |
| 661 | // the scalarMap has just one dimension, which contains the mapping. |
| 662 | // |
| 663 | // In case vector code generation is done, an instruction may either appear |
| 664 | // in the vector map once (as it is calculating >vectorwidth< values at a |
| 665 | // time. Or (if the values are calculated using scalar operations), it |
| 666 | // appears once in every dimension of the scalarMap. |
| 667 | VectorValueMapT ScalarBlockMap(getVectorWidth()); |
| 668 | ValueMapT VectorBlockMap; |
| 669 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 670 | for (Instruction &Inst : *BB) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 671 | copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 672 | } |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 673 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 674 | BasicBlock *RegionGenerator::repairDominance( |
| 675 | BasicBlock *BB, BasicBlock *BBCopy, |
| 676 | DenseMap<BasicBlock *, BasicBlock *> &BlockMap) { |
| 677 | |
| 678 | BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock(); |
| 679 | BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom); |
| 680 | |
| 681 | if (BBCopyIDom) |
| 682 | DT.changeImmediateDominator(BBCopy, BBCopyIDom); |
| 683 | |
| 684 | return BBCopyIDom; |
| 685 | } |
| 686 | |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 687 | void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap, |
| 688 | LoopToScevMapT <S) { |
| 689 | assert(Stmt.isRegionStmt() && |
| 690 | "Only region statements can be copied by the block generator"); |
| 691 | |
| 692 | // The region represented by the statement. |
| 693 | Region *R = Stmt.getRegion(); |
| 694 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 695 | // The "BBMaps" for the whole region. |
| 696 | DenseMap<BasicBlock *, ValueMapT> RegionMaps; |
| 697 | |
| 698 | // A map from old to new blocks in the region |
| 699 | DenseMap<BasicBlock *, BasicBlock *> BlockMap; |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 700 | |
| 701 | // Iterate over all blocks in the region in a breadth-first search. |
| 702 | std::deque<BasicBlock *> Blocks; |
| 703 | SmallPtrSet<BasicBlock *, 8> SeenBlocks; |
| 704 | Blocks.push_back(R->getEntry()); |
| 705 | SeenBlocks.insert(R->getEntry()); |
| 706 | |
| 707 | while (!Blocks.empty()) { |
| 708 | BasicBlock *BB = Blocks.front(); |
| 709 | Blocks.pop_front(); |
| 710 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 711 | // First split the block and update dominance information. |
| 712 | BasicBlock *BBCopy = splitBB(BB); |
| 713 | BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy, BlockMap); |
| 714 | |
| 715 | // Get the mapping for this block and initialize it with the mapping |
| 716 | // available at its immediate dominator (in the new region). |
| 717 | ValueMapT &RegionMap = RegionMaps[BBCopy]; |
| 718 | RegionMap = RegionMaps[BBCopyIDom]; |
| 719 | |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 720 | // Copy the block with the BlockGenerator. |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 721 | copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 722 | |
| 723 | // And continue with new successors inside the region. |
| 724 | for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++) |
| 725 | if (R->contains(*SI) && SeenBlocks.insert(*SI).second) |
| 726 | Blocks.push_back(*SI); |
| 727 | |
| 728 | // In order to remap PHI nodes we store also basic block mappings. |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 729 | BlockMap[BB] = BBCopy; |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | // Now create a new dedicated region exit block and add it to the region map. |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 733 | BasicBlock *ExitBBCopy = |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 734 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI); |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 735 | ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".as.exit"); |
| 736 | BlockMap[R->getExit()] = ExitBBCopy; |
| 737 | |
| 738 | repairDominance(R->getExit(), ExitBBCopy, BlockMap); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 739 | |
| 740 | // As the block generator doesn't handle control flow we need to add the |
| 741 | // region control flow by hand after all blocks have been copied. |
| 742 | for (BasicBlock *BB : SeenBlocks) { |
| 743 | |
| 744 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 745 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 746 | BasicBlock *BBCopy = BlockMap[BB]; |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 747 | Instruction *BICopy = BBCopy->getTerminator(); |
| 748 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 749 | ValueMapT &RegionMap = RegionMaps[BBCopy]; |
| 750 | RegionMap.insert(BlockMap.begin(), BlockMap.end()); |
| 751 | |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 752 | Builder.SetInsertPoint(BBCopy); |
| 753 | copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS); |
| 754 | BICopy->eraseFromParent(); |
| 755 | } |
| 756 | |
| 757 | // Reset the old insert point for the build. |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 758 | Builder.SetInsertPoint(ExitBBCopy->begin()); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 759 | } |