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