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