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 | 86bc93a | 2015-09-06 08:47:57 +0000 | [diff] [blame] | 20 | #include "polly/CodeGen/RuntimeDebugBuilder.h" |
Tobias Grosser | 637bd63 | 2013-05-07 07:31:10 +0000 | [diff] [blame] | 21 | #include "polly/Options.h" |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 22 | #include "polly/Support/GICHelper.h" |
Sebastian Pop | 97cb813 | 2013-03-18 20:21:13 +0000 | [diff] [blame] | 23 | #include "polly/Support/SCEVValidator.h" |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 24 | #include "polly/Support/ScopHelper.h" |
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" |
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 | 86bc93a | 2015-09-06 08:47:57 +0000 | [diff] [blame] | 45 | static cl::opt<bool> DebugPrinting( |
| 46 | "polly-codegen-add-debug-printing", |
| 47 | cl::desc("Add printf calls that show the values loaded/stored."), |
| 48 | cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); |
| 49 | |
Tobias Grosser | dcc3b43 | 2015-08-04 13:54:20 +0000 | [diff] [blame] | 50 | bool polly::canSynthesize(const Value *V, const llvm::LoopInfo *LI, |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 51 | ScalarEvolution *SE, const Region *R) { |
Tobias Grosser | dcc3b43 | 2015-08-04 13:54:20 +0000 | [diff] [blame] | 52 | if (!V || !SE->isSCEVable(V->getType())) |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 53 | return false; |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 54 | |
Tobias Grosser | dcc3b43 | 2015-08-04 13:54:20 +0000 | [diff] [blame] | 55 | if (const SCEV *Scev = SE->getSCEV(const_cast<Value *>(V))) |
Tobias Grosser | 683b8e4 | 2014-11-30 14:33:31 +0000 | [diff] [blame] | 56 | if (!isa<SCEVCouldNotCompute>(Scev)) |
| 57 | if (!hasScalarDepsInsideRegion(Scev, R)) |
| 58 | return true; |
| 59 | |
| 60 | return false; |
Tobias Grosser | ecfe21b | 2013-03-20 18:03:18 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Johannes Doerfert | 9e3a5db | 2015-01-26 15:55:54 +0000 | [diff] [blame] | 63 | bool polly::isIgnoredIntrinsic(const Value *V) { |
| 64 | if (auto *IT = dyn_cast<IntrinsicInst>(V)) { |
| 65 | switch (IT->getIntrinsicID()) { |
| 66 | // Lifetime markers are supported/ignored. |
| 67 | case llvm::Intrinsic::lifetime_start: |
| 68 | case llvm::Intrinsic::lifetime_end: |
| 69 | // Invariant markers are supported/ignored. |
| 70 | case llvm::Intrinsic::invariant_start: |
| 71 | case llvm::Intrinsic::invariant_end: |
| 72 | // Some misc annotations are supported/ignored. |
| 73 | case llvm::Intrinsic::var_annotation: |
| 74 | case llvm::Intrinsic::ptr_annotation: |
| 75 | case llvm::Intrinsic::annotation: |
| 76 | case llvm::Intrinsic::donothing: |
| 77 | case llvm::Intrinsic::assume: |
| 78 | case llvm::Intrinsic::expect: |
Tobias Grosser | e83a396 | 2015-08-30 16:57:20 +0000 | [diff] [blame] | 79 | // Some debug info intrisics are supported/ignored. |
| 80 | case llvm::Intrinsic::dbg_value: |
| 81 | case llvm::Intrinsic::dbg_declare: |
Johannes Doerfert | 9e3a5db | 2015-01-26 15:55:54 +0000 | [diff] [blame] | 82 | return true; |
| 83 | default: |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | return false; |
| 88 | } |
| 89 | |
Johannes Doerfert | b4f08eb | 2015-02-23 13:51:35 +0000 | [diff] [blame] | 90 | BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI, |
| 91 | ScalarEvolution &SE, DominatorTree &DT, |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 92 | ScalarAllocaMapTy &ScalarMap, |
| 93 | ScalarAllocaMapTy &PHIOpMap, |
| 94 | EscapeUsersAllocaMapTy &EscapeMap, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 95 | ValueToValueMap &GlobalMap, |
Johannes Doerfert | b4f08eb | 2015-02-23 13:51:35 +0000 | [diff] [blame] | 96 | IslExprBuilder *ExprBuilder) |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 97 | : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT), |
| 98 | EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap), |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 99 | EscapeMap(EscapeMap), GlobalMap(GlobalMap) {} |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 100 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 101 | Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 102 | ValueMapT &BBMap, LoopToScevMapT <S, |
| 103 | Loop *L) const { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 104 | // We assume constants never change. |
| 105 | // This avoids map lookups for many calls to this function. |
| 106 | if (isa<Constant>(Old)) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 107 | return const_cast<Value *>(Old); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 108 | |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 109 | if (Value *New = GlobalMap.lookup(Old)) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 110 | if (Old->getType()->getScalarSizeInBits() < |
Tobias Grosser | d7e5864 | 2013-04-10 06:55:45 +0000 | [diff] [blame] | 111 | New->getType()->getScalarSizeInBits()) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 112 | New = Builder.CreateTruncOrBitCast(New, Old->getType()); |
| 113 | |
| 114 | return New; |
| 115 | } |
| 116 | |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 117 | if (Value *New = BBMap.lookup(Old)) |
| 118 | return New; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 119 | |
Tobias Grosser | 683b8e4 | 2014-11-30 14:33:31 +0000 | [diff] [blame] | 120 | if (SE.isSCEVable(Old->getType())) |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 121 | if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) { |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 122 | if (!isa<SCEVCouldNotCompute>(Scev)) { |
Sebastian Pop | 637b23d | 2013-02-15 20:56:01 +0000 | [diff] [blame] | 123 | const SCEV *NewScev = apply(Scev, LTS, SE); |
| 124 | ValueToValueMap VTV; |
| 125 | VTV.insert(BBMap.begin(), BBMap.end()); |
| 126 | VTV.insert(GlobalMap.begin(), GlobalMap.end()); |
Sebastian Pop | 47d4ee3 | 2013-02-15 21:26:53 +0000 | [diff] [blame] | 127 | NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV); |
Johannes Doerfert | e69e114 | 2015-08-18 11:56:00 +0000 | [diff] [blame] | 128 | |
| 129 | Scop &S = *Stmt.getParent(); |
| 130 | const DataLayout &DL = |
| 131 | S.getRegion().getEntry()->getParent()->getParent()->getDataLayout(); |
| 132 | auto IP = Builder.GetInsertPoint(); |
| 133 | |
| 134 | assert(IP != Builder.GetInsertBlock()->end() && |
Tobias Grosser | 45e7944 | 2015-08-01 09:07:57 +0000 | [diff] [blame] | 135 | "Only instructions can be insert points for SCEVExpander"); |
Johannes Doerfert | e69e114 | 2015-08-18 11:56:00 +0000 | [diff] [blame] | 136 | Value *Expanded = |
| 137 | expandCodeFor(S, SE, DL, "polly", NewScev, Old->getType(), IP); |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 138 | |
| 139 | BBMap[Old] = Expanded; |
| 140 | return Expanded; |
| 141 | } |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 142 | } |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 143 | |
Tobias Grosser | 16371ac | 2014-11-05 20:48:56 +0000 | [diff] [blame] | 144 | // A scop-constant value defined by a global or a function parameter. |
| 145 | if (isa<GlobalValue>(Old) || isa<Argument>(Old)) |
| 146 | return const_cast<Value *>(Old); |
| 147 | |
| 148 | // A scop-constant value defined by an instruction executed outside the scop. |
| 149 | if (const Instruction *Inst = dyn_cast<Instruction>(Old)) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 150 | if (!Stmt.getParent()->getRegion().contains(Inst->getParent())) |
Tobias Grosser | 16371ac | 2014-11-05 20:48:56 +0000 | [diff] [blame] | 151 | return const_cast<Value *>(Old); |
| 152 | |
| 153 | // The scalar dependence is neither available nor SCEVCodegenable. |
Hongbin Zheng | 5b463ce | 2013-07-25 09:12:07 +0000 | [diff] [blame] | 154 | llvm_unreachable("Unexpected scalar dependence in region!"); |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 155 | return nullptr; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 158 | void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 159 | ValueMapT &BBMap, LoopToScevMapT <S) { |
Tobias Grosser | 030237d | 2014-02-21 15:06:05 +0000 | [diff] [blame] | 160 | // We do not generate debug intrinsics as we did not investigate how to |
| 161 | // copy them correctly. At the current state, they just crash the code |
| 162 | // generation as the meta-data operands are not correctly copied. |
| 163 | if (isa<DbgInfoIntrinsic>(Inst)) |
| 164 | return; |
| 165 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 166 | Instruction *NewInst = Inst->clone(); |
| 167 | |
| 168 | // Replace old operands with the new ones. |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 169 | for (Value *OldOperand : Inst->operands()) { |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 170 | Value *NewOperand = |
| 171 | getNewValue(Stmt, OldOperand, BBMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 172 | |
| 173 | if (!NewOperand) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 174 | assert(!isa<StoreInst>(NewInst) && |
| 175 | "Store instructions are always needed!"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 176 | delete NewInst; |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | NewInst->replaceUsesOfWith(OldOperand, NewOperand); |
| 181 | } |
| 182 | |
| 183 | Builder.Insert(NewInst); |
| 184 | BBMap[Inst] = NewInst; |
| 185 | |
| 186 | if (!NewInst->getType()->isVoidTy()) |
| 187 | NewInst->setName("p_" + Inst->getName()); |
| 188 | } |
| 189 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 190 | Value *BlockGenerator::generateLocationAccessed( |
| 191 | ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 192 | ValueMapT &BBMap, LoopToScevMapT <S, isl_id_to_ast_expr *NewAccesses) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 193 | const MemoryAccess &MA = Stmt.getAccessFor(Inst); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 194 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 195 | isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, MA.getId()); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 196 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 197 | if (AccessExpr) { |
| 198 | AccessExpr = isl_ast_expr_address_of(AccessExpr); |
| 199 | return ExprBuilder->create(AccessExpr); |
| 200 | } |
| 201 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 202 | return getNewValue(Stmt, Pointer, BBMap, LTS, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Tobias Grosser | 4d96c8d | 2013-03-23 01:05:07 +0000 | [diff] [blame] | 205 | Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) { |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 206 | return LI.getLoopFor(Inst->getParent()); |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 209 | Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 210 | ValueMapT &BBMap, LoopToScevMapT <S, |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 211 | isl_id_to_ast_expr *NewAccesses) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 212 | const Value *Pointer = Load->getPointerOperand(); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 213 | Value *NewPointer = |
| 214 | generateLocationAccessed(Stmt, Load, Pointer, BBMap, LTS, NewAccesses); |
Johannes Doerfert | 8790145 | 2014-10-02 16:22:19 +0000 | [diff] [blame] | 215 | Value *ScalarLoad = Builder.CreateAlignedLoad( |
| 216 | NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_"); |
Tobias Grosser | 86bc93a | 2015-09-06 08:47:57 +0000 | [diff] [blame] | 217 | |
| 218 | if (DebugPrinting) |
| 219 | RuntimeDebugBuilder::createCPUPrinter(Builder, "Load from ", NewPointer, |
| 220 | ": ", ScalarLoad, "\n"); |
| 221 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 222 | return ScalarLoad; |
| 223 | } |
| 224 | |
Tobias Grosser | c186ac7 | 2015-08-11 08:13:15 +0000 | [diff] [blame] | 225 | void BlockGenerator::generateScalarStore(ScopStmt &Stmt, const StoreInst *Store, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 226 | ValueMapT &BBMap, LoopToScevMapT <S, |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 227 | isl_id_to_ast_expr *NewAccesses) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 228 | const Value *Pointer = Store->getPointerOperand(); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 229 | Value *NewPointer = |
| 230 | generateLocationAccessed(Stmt, Store, Pointer, BBMap, LTS, NewAccesses); |
| 231 | Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap, LTS, |
| 232 | getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 233 | |
Tobias Grosser | 86bc93a | 2015-09-06 08:47:57 +0000 | [diff] [blame] | 234 | if (DebugPrinting) |
| 235 | RuntimeDebugBuilder::createCPUPrinter(Builder, "Store to ", NewPointer, |
| 236 | ": ", ValueOperand, "\n"); |
| 237 | |
Tobias Grosser | c186ac7 | 2015-08-11 08:13:15 +0000 | [diff] [blame] | 238 | Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment()); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 241 | void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 242 | ValueMapT &BBMap, LoopToScevMapT <S, |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 243 | isl_id_to_ast_expr *NewAccesses) { |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 244 | |
| 245 | // First check for possible scalar dependences for this instruction. |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 246 | generateScalarLoads(Stmt, Inst, BBMap); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 247 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 248 | // Terminator instructions control the control flow. They are explicitly |
| 249 | // expressed in the clast and do not need to be copied. |
| 250 | if (Inst->isTerminator()) |
| 251 | return; |
| 252 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 253 | Loop *L = getLoopForInst(Inst); |
| 254 | if ((Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) && |
| 255 | canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) { |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 256 | Value *NewValue = getNewValue(Stmt, Inst, BBMap, LTS, L); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 257 | BBMap[Inst] = NewValue; |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 258 | return; |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 259 | } |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 260 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 261 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 262 | Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, LTS, NewAccesses); |
Sebastian Pop | 3d94fed | 2013-05-24 18:46:02 +0000 | [diff] [blame] | 263 | // Compute NewLoad 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[Load] = NewLoad; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 266 | return; |
| 267 | } |
| 268 | |
| 269 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 270 | generateScalarStore(Stmt, Store, BBMap, LTS, NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 274 | if (const PHINode *PHI = dyn_cast<PHINode>(Inst)) { |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 275 | copyPHIInstruction(Stmt, PHI, BBMap, LTS); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 279 | // Skip some special intrinsics for which we do not adjust the semantics to |
| 280 | // the new schedule. All others are handled like every other instruction. |
Tobias Grosser | 9c0ffe3 | 2015-08-30 16:57:15 +0000 | [diff] [blame] | 281 | if (isIgnoredIntrinsic(Inst)) |
| 282 | return; |
Johannes Doerfert | 3f500fa | 2015-01-25 18:07:30 +0000 | [diff] [blame] | 283 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 284 | copyInstScalar(Stmt, Inst, BBMap, LTS); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 287 | void BlockGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 288 | isl_id_to_ast_expr *NewAccesses) { |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 289 | assert(Stmt.isBlockStmt() && |
| 290 | "Only block statements can be copied by the block generator"); |
| 291 | |
| 292 | ValueMapT BBMap; |
| 293 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 294 | BasicBlock *BB = Stmt.getBasicBlock(); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 295 | copyBB(Stmt, BB, BBMap, LTS, NewAccesses); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 298 | BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) { |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 299 | BasicBlock *CopyBB = |
Johannes Doerfert | b4f08eb | 2015-02-23 13:51:35 +0000 | [diff] [blame] | 300 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 301 | CopyBB->setName("polly.stmt." + BB->getName()); |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 302 | return CopyBB; |
| 303 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 304 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 305 | BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 306 | ValueMapT &BBMap, LoopToScevMapT <S, |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 307 | isl_id_to_ast_expr *NewAccesses) { |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 308 | BasicBlock *CopyBB = splitBB(BB); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 309 | copyBB(Stmt, BB, CopyBB, BBMap, LTS, NewAccesses); |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 310 | return CopyBB; |
| 311 | } |
| 312 | |
| 313 | void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 314 | ValueMapT &BBMap, LoopToScevMapT <S, |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 315 | isl_id_to_ast_expr *NewAccesses) { |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 316 | Builder.SetInsertPoint(CopyBB->begin()); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 317 | EntryBB = &CopyBB->getParent()->getEntryBlock(); |
| 318 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 319 | for (Instruction &Inst : *BB) |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 320 | copyInstruction(Stmt, &Inst, BBMap, LTS, NewAccesses); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 321 | |
| 322 | // After a basic block was copied store all scalars that escape this block |
| 323 | // in their alloca. First the scalars that have dependences inside the SCoP, |
| 324 | // then the ones that might escape the SCoP. |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 325 | generateScalarStores(Stmt, BB, BBMap); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 326 | |
| 327 | const Region &R = Stmt.getParent()->getRegion(); |
| 328 | for (Instruction &Inst : *BB) |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 329 | handleOutsideUsers(R, &Inst, BBMap[&Inst]); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Tobias Grosser | 64c0ff4 | 2015-08-31 05:52:24 +0000 | [diff] [blame] | 332 | Value *BlockGenerator::getOrCreateAlloca(Value *ScalarBase, |
| 333 | ScalarAllocaMapTy &Map, |
Tobias Grosser | 64c0ff4 | 2015-08-31 05:52:24 +0000 | [diff] [blame] | 334 | const char *NameExt) { |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 335 | // Check if an alloca was cached for the base instruction. |
| 336 | AllocaInst *&Addr = Map[ScalarBase]; |
| 337 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 338 | // If no alloca was found create one and insert it in the entry block. |
| 339 | if (!Addr) { |
| 340 | auto *Ty = ScalarBase->getType(); |
| 341 | Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt); |
Tobias Grosser | 64c0ff4 | 2015-08-31 05:52:24 +0000 | [diff] [blame] | 342 | EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock(); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 343 | Addr->insertBefore(EntryBB->getFirstInsertionPt()); |
| 344 | } |
| 345 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 346 | if (GlobalMap.count(Addr)) |
| 347 | return GlobalMap[Addr]; |
Tobias Grosser | 64c0ff4 | 2015-08-31 05:52:24 +0000 | [diff] [blame] | 348 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 349 | return Addr; |
| 350 | } |
| 351 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 352 | Value *BlockGenerator::getOrCreateAlloca(MemoryAccess &Access) { |
Tobias Grosser | f8d55f7 | 2015-08-29 18:12:03 +0000 | [diff] [blame] | 353 | if (Access.getScopArrayInfo()->isPHI()) |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 354 | return getOrCreatePHIAlloca(Access.getBaseAddr()); |
Tobias Grosser | f8d55f7 | 2015-08-29 18:12:03 +0000 | [diff] [blame] | 355 | else |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 356 | return getOrCreateScalarAlloca(Access.getBaseAddr()); |
Tobias Grosser | f8d55f7 | 2015-08-29 18:12:03 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 359 | Value *BlockGenerator::getOrCreateScalarAlloca(Value *ScalarBase) { |
| 360 | return getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a"); |
Tobias Grosser | b79a67d | 2015-08-28 08:23:35 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 363 | Value *BlockGenerator::getOrCreatePHIAlloca(Value *ScalarBase) { |
| 364 | return getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops"); |
Tobias Grosser | b79a67d | 2015-08-28 08:23:35 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 367 | void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst, |
Johannes Doerfert | 717b866 | 2015-09-08 21:44:27 +0000 | [diff] [blame^] | 368 | Value *InstCopy, AllocaInst *Address) { |
Tobias Grosser | 2985400 | 2015-08-30 15:03:59 +0000 | [diff] [blame] | 369 | // If there are escape users we get the alloca for this instruction and put it |
| 370 | // in the EscapeMap for later finalization. Lastly, if the instruction was |
| 371 | // copied multiple times we already did this and can exit. |
Michael Kruse | acb6ade | 2015-08-18 17:25:48 +0000 | [diff] [blame] | 372 | if (EscapeMap.count(Inst)) |
| 373 | return; |
Johannes Doerfert | e69e114 | 2015-08-18 11:56:00 +0000 | [diff] [blame] | 374 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 375 | EscapeUserVectorTy EscapeUsers; |
| 376 | for (User *U : Inst->users()) { |
| 377 | |
| 378 | // Non-instruction user will never escape. |
| 379 | Instruction *UI = dyn_cast<Instruction>(U); |
| 380 | if (!UI) |
| 381 | continue; |
| 382 | |
Johannes Doerfert | ddb83d0 | 2015-08-16 08:35:40 +0000 | [diff] [blame] | 383 | if (R.contains(UI)) |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 384 | continue; |
| 385 | |
| 386 | EscapeUsers.push_back(UI); |
| 387 | } |
| 388 | |
| 389 | // Exit if no escape uses were found. |
| 390 | if (EscapeUsers.empty()) |
| 391 | return; |
| 392 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 393 | // Get or create an escape alloca for this instruction. |
Johannes Doerfert | 717b866 | 2015-09-08 21:44:27 +0000 | [diff] [blame^] | 394 | auto *ScalarAddr = |
| 395 | Address ? Address : cast<AllocaInst>(getOrCreateScalarAlloca(Inst)); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 396 | |
| 397 | // Remember that this instruction has escape uses and the escape alloca. |
| 398 | EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers)); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void BlockGenerator::generateScalarLoads(ScopStmt &Stmt, |
| 402 | const Instruction *Inst, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 403 | ValueMapT &BBMap) { |
Tobias Grosser | d4dd6ec | 2015-07-27 17:57:58 +0000 | [diff] [blame] | 404 | auto *MAL = Stmt.lookupAccessesFor(Inst); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 405 | |
Tobias Grosser | d4dd6ec | 2015-07-27 17:57:58 +0000 | [diff] [blame] | 406 | if (!MAL) |
| 407 | return; |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 408 | |
Tobias Grosser | d4dd6ec | 2015-07-27 17:57:58 +0000 | [diff] [blame] | 409 | for (MemoryAccess &MA : *MAL) { |
Tobias Grosser | d4dd6ec | 2015-07-27 17:57:58 +0000 | [diff] [blame] | 410 | if (!MA.isScalar() || !MA.isRead()) |
| 411 | continue; |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 412 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 413 | auto *Address = getOrCreateAlloca(MA); |
Tobias Grosser | 2fc50df | 2015-08-30 19:51:01 +0000 | [diff] [blame] | 414 | BBMap[MA.getBaseAddr()] = |
| 415 | Builder.CreateLoad(Address, Address->getName() + ".reload"); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
| 419 | Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 420 | ValueMapT &BBMap) { |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 421 | // If the value we want to store is an instruction we might have demoted it |
| 422 | // in order to make it accessible here. In such a case a reload is |
| 423 | // necessary. If it is no instruction it will always be a value that |
| 424 | // dominates the current point and we can just use it. In total there are 4 |
| 425 | // options: |
| 426 | // (1) The value is no instruction ==> use the value. |
| 427 | // (2) The value is an instruction that was split out of the region prior to |
| 428 | // code generation ==> use the instruction as it dominates the region. |
| 429 | // (3) The value is an instruction: |
| 430 | // (a) The value was defined in the current block, thus a copy is in |
| 431 | // the BBMap ==> use the mapped value. |
| 432 | // (b) The value was defined in a previous block, thus we demoted it |
| 433 | // earlier ==> use the reloaded value. |
| 434 | Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue); |
| 435 | if (!ScalarValueInst) |
| 436 | return ScalarValue; |
| 437 | |
| 438 | if (!R.contains(ScalarValueInst)) { |
| 439 | if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst)) |
| 440 | return /* Case (3a) */ ScalarValueCopy; |
| 441 | else |
| 442 | return /* Case 2 */ ScalarValue; |
| 443 | } |
| 444 | |
| 445 | if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst)) |
| 446 | return /* Case (3a) */ ScalarValueCopy; |
| 447 | |
| 448 | // Case (3b) |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 449 | Value *Address = getOrCreateScalarAlloca(ScalarValueInst); |
Tobias Grosser | b649e26 | 2015-08-30 17:37:55 +0000 | [diff] [blame] | 450 | ScalarValue = Builder.CreateLoad(Address, Address->getName() + ".reload"); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 451 | |
| 452 | return ScalarValue; |
| 453 | } |
| 454 | |
| 455 | void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 456 | ValueMapT &BBMap) { |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 457 | const Region &R = Stmt.getParent()->getRegion(); |
| 458 | |
| 459 | assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() && |
| 460 | "Region statements need to use the generateScalarStores() " |
| 461 | "function in the RegionGenerator"); |
| 462 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 463 | for (MemoryAccess *MA : Stmt) { |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 464 | if (!MA->isScalar() || MA->isRead()) |
| 465 | continue; |
| 466 | |
Johannes Doerfert | d86f215 | 2015-08-17 10:58:17 +0000 | [diff] [blame] | 467 | Value *Val = MA->getAccessValue(); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 468 | auto *Address = getOrCreateAlloca(*MA); |
Johannes Doerfert | d86f215 | 2015-08-17 10:58:17 +0000 | [diff] [blame] | 469 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 470 | Val = getNewScalarValue(Val, R, BBMap); |
Tobias Grosser | 9224522 | 2015-07-28 14:53:44 +0000 | [diff] [blame] | 471 | Builder.CreateStore(Val, Address); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 475 | void BlockGenerator::createScalarInitialization(Scop &S) { |
| 476 | Region &R = S.getRegion(); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 477 | // The split block __just before__ the region and optimized region. |
| 478 | BasicBlock *SplitBB = R.getEnteringBlock(); |
| 479 | BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator()); |
| 480 | assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!"); |
| 481 | |
| 482 | // Get the start block of the __optimized__ region. |
| 483 | BasicBlock *StartBB = SplitBBTerm->getSuccessor(0); |
| 484 | if (StartBB == R.getEntry()) |
| 485 | StartBB = SplitBBTerm->getSuccessor(1); |
| 486 | |
Tobias Grosser | 9f3d55c | 2015-08-31 11:06:19 +0000 | [diff] [blame] | 487 | Builder.SetInsertPoint(StartBB->begin()); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 488 | |
Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 489 | for (auto &Pair : S.arrays()) { |
| 490 | auto &Array = Pair.second; |
| 491 | if (Array->getNumberOfDimensions() != 0) |
| 492 | continue; |
| 493 | if (Array->isPHI()) { |
| 494 | // For PHI nodes, the only values we need to store are the ones that |
| 495 | // reach the PHI node from outside the region. In general there should |
| 496 | // only be one such incoming edge and this edge should enter through |
| 497 | // 'SplitBB'. |
| 498 | auto PHI = cast<PHINode>(Array->getBasePtr()); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 499 | |
Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 500 | for (auto BI = PHI->block_begin(), BE = PHI->block_end(); BI != BE; BI++) |
| 501 | if (!R.contains(*BI) && *BI != SplitBB) |
| 502 | llvm_unreachable("Incoming edges from outside the scop should always " |
| 503 | "come from SplitBB"); |
| 504 | |
| 505 | int Idx = PHI->getBasicBlockIndex(SplitBB); |
| 506 | if (Idx < 0) |
| 507 | continue; |
| 508 | |
| 509 | Value *ScalarValue = PHI->getIncomingValue(Idx); |
| 510 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 511 | Builder.CreateStore(ScalarValue, getOrCreatePHIAlloca(PHI)); |
Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 512 | continue; |
| 513 | } |
| 514 | |
| 515 | auto *Inst = dyn_cast<Instruction>(Array->getBasePtr()); |
| 516 | |
| 517 | if (Inst && R.contains(Inst)) |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 518 | continue; |
| 519 | |
Johannes Doerfert | 717b866 | 2015-09-08 21:44:27 +0000 | [diff] [blame^] | 520 | // PHI nodes that are not marked as such in their SAI object are exit PHI |
| 521 | // nodes we model as common scalars but do not need to initialize. |
| 522 | if (Inst && isa<PHINode>(Inst)) |
| 523 | continue; |
| 524 | |
Tobias Grosser | 64c0ff4 | 2015-08-31 05:52:24 +0000 | [diff] [blame] | 525 | ValueMapT EmptyMap; |
Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 526 | Builder.CreateStore(Array->getBasePtr(), |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 527 | getOrCreateScalarAlloca(Array->getBasePtr())); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
| 530 | |
| 531 | void BlockGenerator::createScalarFinalization(Region &R) { |
| 532 | // The exit block of the __unoptimized__ region. |
| 533 | BasicBlock *ExitBB = R.getExitingBlock(); |
| 534 | // The merge block __just after__ the region and the optimized region. |
| 535 | BasicBlock *MergeBB = R.getExit(); |
| 536 | |
| 537 | // The exit block of the __optimized__ region. |
| 538 | BasicBlock *OptExitBB = *(pred_begin(MergeBB)); |
| 539 | if (OptExitBB == ExitBB) |
| 540 | OptExitBB = *(++pred_begin(MergeBB)); |
| 541 | |
| 542 | Builder.SetInsertPoint(OptExitBB->getTerminator()); |
| 543 | for (const auto &EscapeMapping : EscapeMap) { |
| 544 | // Extract the escaping instruction and the escaping users as well as the |
| 545 | // alloca the instruction was demoted to. |
| 546 | Instruction *EscapeInst = EscapeMapping.getFirst(); |
| 547 | const auto &EscapeMappingValue = EscapeMapping.getSecond(); |
| 548 | const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second; |
Tobias Grosser | 64c0ff4 | 2015-08-31 05:52:24 +0000 | [diff] [blame] | 549 | Value *ScalarAddr = EscapeMappingValue.first; |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 550 | |
| 551 | // Reload the demoted instruction in the optimized version of the SCoP. |
| 552 | Instruction *EscapeInstReload = |
| 553 | Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload"); |
| 554 | |
| 555 | // Create the merge PHI that merges the optimized and unoptimized version. |
| 556 | PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2, |
| 557 | EscapeInst->getName() + ".merge"); |
| 558 | MergePHI->insertBefore(MergeBB->getFirstInsertionPt()); |
| 559 | |
| 560 | // Add the respective values to the merge PHI. |
| 561 | MergePHI->addIncoming(EscapeInstReload, OptExitBB); |
| 562 | MergePHI->addIncoming(EscapeInst, ExitBB); |
| 563 | |
| 564 | // The information of scalar evolution about the escaping instruction needs |
| 565 | // to be revoked so the new merged instruction will be used. |
| 566 | if (SE.isSCEVable(EscapeInst->getType())) |
| 567 | SE.forgetValue(EscapeInst); |
| 568 | |
| 569 | // Replace all uses of the demoted instruction with the merge PHI. |
| 570 | for (Instruction *EUser : EscapeUsers) |
| 571 | EUser->replaceUsesOfWith(EscapeInst, MergePHI); |
| 572 | } |
| 573 | } |
| 574 | |
Tobias Grosser | 655a457 | 2015-08-30 17:32:39 +0000 | [diff] [blame] | 575 | void BlockGenerator::finalizeSCoP(Scop &S) { |
Johannes Doerfert | 717b866 | 2015-09-08 21:44:27 +0000 | [diff] [blame^] | 576 | |
| 577 | // Handle PHI nodes that were in the original exit and are now |
| 578 | // moved into the region exiting block. |
| 579 | if (!S.hasSingleExitEdge()) { |
| 580 | for (Instruction &I : *S.getRegion().getExitingBlock()) { |
| 581 | PHINode *PHI = dyn_cast<PHINode>(&I); |
| 582 | if (!PHI) |
| 583 | break; |
| 584 | |
| 585 | assert(PHI->getNumUses() == 1); |
| 586 | assert(ScalarMap.count(PHI->user_back())); |
| 587 | |
| 588 | handleOutsideUsers(S.getRegion(), PHI, nullptr, |
| 589 | ScalarMap[PHI->user_back()]); |
| 590 | } |
| 591 | } |
| 592 | |
Tobias Grosser | c0091a7 | 2015-08-30 19:19:34 +0000 | [diff] [blame] | 593 | createScalarInitialization(S); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 594 | createScalarFinalization(S.getRegion()); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 597 | VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen, |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 598 | std::vector<LoopToScevMapT> &VLTS, |
| 599 | isl_map *Schedule) |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 600 | : BlockGenerator(BlockGen), VLTS(VLTS), Schedule(Schedule) { |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 601 | assert(Schedule && "No statement domain provided"); |
| 602 | } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 603 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 604 | Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old, |
Tobias Grosser | e602a07 | 2013-05-07 07:30:56 +0000 | [diff] [blame] | 605 | ValueMapT &VectorMap, |
| 606 | VectorValueMapT &ScalarMaps, |
| 607 | Loop *L) { |
Hongbin Zheng | fe11e28 | 2013-06-29 13:22:15 +0000 | [diff] [blame] | 608 | if (Value *NewValue = VectorMap.lookup(Old)) |
| 609 | return NewValue; |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 610 | |
| 611 | int Width = getVectorWidth(); |
| 612 | |
| 613 | Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); |
| 614 | |
| 615 | for (int Lane = 0; Lane < Width; Lane++) |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 616 | Vector = Builder.CreateInsertElement( |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 617 | Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], VLTS[Lane], L), |
Tobias Grosser | 7242ad9 | 2013-02-22 08:07:06 +0000 | [diff] [blame] | 618 | Builder.getInt32(Lane)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 619 | |
| 620 | VectorMap[Old] = Vector; |
| 621 | |
| 622 | return Vector; |
| 623 | } |
| 624 | |
| 625 | Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { |
| 626 | PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); |
| 627 | assert(PointerTy && "PointerType expected"); |
| 628 | |
| 629 | Type *ScalarType = PointerTy->getElementType(); |
| 630 | VectorType *VectorType = VectorType::get(ScalarType, Width); |
| 631 | |
| 632 | return PointerType::getUnqual(VectorType); |
| 633 | } |
| 634 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 635 | Value *VectorBlockGenerator::generateStrideOneLoad( |
| 636 | ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps, |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 637 | __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) { |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 638 | unsigned VectorWidth = getVectorWidth(); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 639 | const Value *Pointer = Load->getPointerOperand(); |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 640 | Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); |
| 641 | unsigned Offset = NegativeStride ? VectorWidth - 1 : 0; |
| 642 | |
Tobias Grosser | 5a56cbf | 2014-04-16 07:33:47 +0000 | [diff] [blame] | 643 | Value *NewPointer = nullptr; |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 644 | NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset], |
| 645 | VLTS[Offset], NewAccesses); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 646 | Value *VectorPtr = |
| 647 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
| 648 | LoadInst *VecLoad = |
| 649 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 650 | if (!Aligned) |
| 651 | VecLoad->setAlignment(8); |
| 652 | |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 653 | if (NegativeStride) { |
| 654 | SmallVector<Constant *, 16> Indices; |
| 655 | for (int i = VectorWidth - 1; i >= 0; i--) |
| 656 | Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i)); |
| 657 | Constant *SV = llvm::ConstantVector::get(Indices); |
| 658 | Value *RevVecLoad = Builder.CreateShuffleVector( |
| 659 | VecLoad, VecLoad, SV, Load->getName() + "_reverse"); |
| 660 | return RevVecLoad; |
| 661 | } |
| 662 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 663 | return VecLoad; |
| 664 | } |
| 665 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 666 | Value *VectorBlockGenerator::generateStrideZeroLoad( |
| 667 | ScopStmt &Stmt, const LoadInst *Load, ValueMapT &BBMap, |
| 668 | __isl_keep isl_id_to_ast_expr *NewAccesses) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 669 | const Value *Pointer = Load->getPointerOperand(); |
| 670 | Type *VectorPtrType = getVectorPtrTy(Pointer, 1); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 671 | Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap, |
| 672 | VLTS[0], NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 673 | Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, |
| 674 | Load->getName() + "_p_vec_p"); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 675 | LoadInst *ScalarLoad = |
| 676 | Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 677 | |
| 678 | if (!Aligned) |
| 679 | ScalarLoad->setAlignment(8); |
| 680 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 681 | Constant *SplatVector = Constant::getNullValue( |
| 682 | VectorType::get(Builder.getInt32Ty(), getVectorWidth())); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 683 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 684 | Value *VectorLoad = Builder.CreateShuffleVector( |
| 685 | ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 686 | return VectorLoad; |
| 687 | } |
| 688 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 689 | Value *VectorBlockGenerator::generateUnknownStrideLoad( |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 690 | ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps, |
| 691 | __isl_keep isl_id_to_ast_expr *NewAccesses |
| 692 | |
| 693 | ) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 694 | int VectorWidth = getVectorWidth(); |
| 695 | const Value *Pointer = Load->getPointerOperand(); |
| 696 | VectorType *VectorType = VectorType::get( |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 697 | dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 698 | |
| 699 | Value *Vector = UndefValue::get(VectorType); |
| 700 | |
| 701 | for (int i = 0; i < VectorWidth; i++) { |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 702 | Value *NewPointer = generateLocationAccessed( |
| 703 | Stmt, Load, Pointer, ScalarMaps[i], VLTS[i], NewAccesses); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 704 | Value *ScalarLoad = |
| 705 | Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); |
| 706 | Vector = Builder.CreateInsertElement( |
| 707 | Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | return Vector; |
| 711 | } |
| 712 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 713 | void VectorBlockGenerator::generateLoad( |
| 714 | ScopStmt &Stmt, const LoadInst *Load, ValueMapT &VectorMap, |
| 715 | VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { |
Tobias Grosser | 2873645 | 2015-03-23 07:00:36 +0000 | [diff] [blame] | 716 | if (!VectorType::isValidElementType(Load->getType())) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 717 | for (int i = 0; i < getVectorWidth(); i++) |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 718 | ScalarMaps[i][Load] = |
| 719 | generateScalarLoad(Stmt, Load, ScalarMaps[i], VLTS[i], NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 720 | return; |
| 721 | } |
| 722 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 723 | const MemoryAccess &Access = Stmt.getAccessFor(Load); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 724 | |
Tobias Grosser | 9549398 | 2014-04-18 09:46:35 +0000 | [diff] [blame] | 725 | // Make sure we have scalar values available to access the pointer to |
| 726 | // the data location. |
| 727 | extractScalarValues(Load, VectorMap, ScalarMaps); |
| 728 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 729 | Value *NewLoad; |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 730 | if (Access.isStrideZero(isl_map_copy(Schedule))) |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 731 | NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses); |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 732 | else if (Access.isStrideOne(isl_map_copy(Schedule))) |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 733 | NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses); |
Tobias Grosser | 0dd463f | 2014-03-19 19:27:24 +0000 | [diff] [blame] | 734 | else if (Access.isStrideX(isl_map_copy(Schedule), -1)) |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 735 | NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 736 | else |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 737 | NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 738 | |
| 739 | VectorMap[Load] = NewLoad; |
| 740 | } |
| 741 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 742 | void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt, |
| 743 | const UnaryInstruction *Inst, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 744 | ValueMapT &VectorMap, |
| 745 | VectorValueMapT &ScalarMaps) { |
| 746 | int VectorWidth = getVectorWidth(); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 747 | Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap, |
| 748 | ScalarMaps, getLoopForInst(Inst)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 749 | |
| 750 | assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); |
| 751 | |
| 752 | const CastInst *Cast = dyn_cast<CastInst>(Inst); |
| 753 | VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); |
| 754 | VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); |
| 755 | } |
| 756 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 757 | void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt, |
| 758 | const BinaryOperator *Inst, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 759 | ValueMapT &VectorMap, |
| 760 | VectorValueMapT &ScalarMaps) { |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 761 | Loop *L = getLoopForInst(Inst); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 762 | Value *OpZero = Inst->getOperand(0); |
| 763 | Value *OpOne = Inst->getOperand(1); |
| 764 | |
| 765 | Value *NewOpZero, *NewOpOne; |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 766 | NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L); |
| 767 | NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 768 | |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 769 | Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 770 | Inst->getName() + "p_vec"); |
| 771 | VectorMap[Inst] = NewInst; |
| 772 | } |
| 773 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 774 | void VectorBlockGenerator::copyStore( |
| 775 | ScopStmt &Stmt, const StoreInst *Store, ValueMapT &VectorMap, |
| 776 | VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 777 | const MemoryAccess &Access = Stmt.getAccessFor(Store); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 778 | |
| 779 | const Value *Pointer = Store->getPointerOperand(); |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 780 | Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap, |
Tobias Grosser | 369430f | 2013-03-22 23:42:53 +0000 | [diff] [blame] | 781 | ScalarMaps, getLoopForInst(Store)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 782 | |
Tobias Grosser | 50fd701 | 2014-04-17 23:13:49 +0000 | [diff] [blame] | 783 | // Make sure we have scalar values available to access the pointer to |
| 784 | // the data location. |
| 785 | extractScalarValues(Store, VectorMap, ScalarMaps); |
| 786 | |
Sebastian Pop | a00a029 | 2012-12-18 07:46:06 +0000 | [diff] [blame] | 787 | if (Access.isStrideOne(isl_map_copy(Schedule))) { |
Johannes Doerfert | 1947f86 | 2014-10-08 20:18:32 +0000 | [diff] [blame] | 788 | Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 789 | Value *NewPointer = generateLocationAccessed( |
| 790 | Stmt, Store, Pointer, ScalarMaps[0], VLTS[0], NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 791 | |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 792 | Value *VectorPtr = |
| 793 | Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 794 | StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); |
| 795 | |
| 796 | if (!Aligned) |
| 797 | Store->setAlignment(8); |
| 798 | } else { |
| 799 | for (unsigned i = 0; i < ScalarMaps.size(); i++) { |
Tobias Grosser | 1bb59b0 | 2012-12-29 23:47:38 +0000 | [diff] [blame] | 800 | Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 801 | Value *NewPointer = generateLocationAccessed( |
| 802 | Stmt, Store, Pointer, ScalarMaps[i], VLTS[i], NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 803 | Builder.CreateStore(Scalar, NewPointer); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, |
| 809 | ValueMapT &VectorMap) { |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 810 | for (Value *Operand : Inst->operands()) |
| 811 | if (VectorMap.count(Operand)) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 812 | return true; |
| 813 | return false; |
| 814 | } |
| 815 | |
| 816 | bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, |
| 817 | ValueMapT &VectorMap, |
| 818 | VectorValueMapT &ScalarMaps) { |
| 819 | bool HasVectorOperand = false; |
| 820 | int VectorWidth = getVectorWidth(); |
| 821 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 822 | for (Value *Operand : Inst->operands()) { |
| 823 | ValueMapT::iterator VecOp = VectorMap.find(Operand); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 824 | |
| 825 | if (VecOp == VectorMap.end()) |
| 826 | continue; |
| 827 | |
| 828 | HasVectorOperand = true; |
| 829 | Value *NewVector = VecOp->second; |
| 830 | |
| 831 | for (int i = 0; i < VectorWidth; ++i) { |
| 832 | ValueMapT &SM = ScalarMaps[i]; |
| 833 | |
| 834 | // If there is one scalar extracted, all scalar elements should have |
| 835 | // already been extracted by the code here. So no need to check for the |
| 836 | // existance of all of them. |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 837 | if (SM.count(Operand)) |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 838 | break; |
| 839 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 840 | SM[Operand] = |
| 841 | Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
| 845 | return HasVectorOperand; |
| 846 | } |
| 847 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 848 | void VectorBlockGenerator::copyInstScalarized( |
| 849 | ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap, |
| 850 | VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 851 | bool HasVectorOperand; |
| 852 | int VectorWidth = getVectorWidth(); |
| 853 | |
| 854 | HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); |
| 855 | |
| 856 | for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 857 | BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane], |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 858 | VLTS[VectorLane], NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 859 | |
| 860 | if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) |
| 861 | return; |
| 862 | |
| 863 | // Make the result available as vector value. |
| 864 | VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); |
| 865 | Value *Vector = UndefValue::get(VectorType); |
| 866 | |
| 867 | for (int i = 0; i < VectorWidth; i++) |
| 868 | Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], |
| 869 | Builder.getInt32(i)); |
| 870 | |
| 871 | VectorMap[Inst] = Vector; |
| 872 | } |
| 873 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 874 | int VectorBlockGenerator::getVectorWidth() { return VLTS.size(); } |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 875 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 876 | void VectorBlockGenerator::copyInstruction( |
| 877 | ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap, |
| 878 | VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) { |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 879 | // Terminator instructions control the control flow. They are explicitly |
| 880 | // expressed in the clast and do not need to be copied. |
| 881 | if (Inst->isTerminator()) |
| 882 | return; |
| 883 | |
Johannes Doerfert | 1ef5233 | 2015-02-08 20:50:42 +0000 | [diff] [blame] | 884 | if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) |
Tobias Grosser | e71c6ab | 2012-04-27 16:36:14 +0000 | [diff] [blame] | 885 | return; |
| 886 | |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 887 | if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 888 | generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
| 892 | if (hasVectorOperands(Inst, VectorMap)) { |
| 893 | if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 894 | copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 895 | return; |
| 896 | } |
| 897 | |
| 898 | if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 899 | copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | |
| 903 | if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 904 | copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 905 | return; |
| 906 | } |
| 907 | |
| 908 | // Falltrough: We generate scalar instructions, if we don't know how to |
| 909 | // generate vector code. |
| 910 | } |
| 911 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 912 | copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 915 | void VectorBlockGenerator::copyStmt( |
| 916 | ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) { |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 917 | assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by " |
| 918 | "the vector block generator"); |
| 919 | |
Johannes Doerfert | be9c911 | 2015-02-06 21:39:31 +0000 | [diff] [blame] | 920 | BasicBlock *BB = Stmt.getBasicBlock(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 921 | BasicBlock *CopyBB = |
Johannes Doerfert | b4f08eb | 2015-02-23 13:51:35 +0000 | [diff] [blame] | 922 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 923 | CopyBB->setName("polly.stmt." + BB->getName()); |
| 924 | Builder.SetInsertPoint(CopyBB->begin()); |
| 925 | |
| 926 | // Create two maps that store the mapping from the original instructions of |
| 927 | // the old basic block to their copies in the new basic block. Those maps |
| 928 | // are basic block local. |
| 929 | // |
| 930 | // As vector code generation is supported there is one map for scalar values |
| 931 | // and one for vector values. |
| 932 | // |
| 933 | // In case we just do scalar code generation, the vectorMap is not used and |
| 934 | // the scalarMap has just one dimension, which contains the mapping. |
| 935 | // |
| 936 | // In case vector code generation is done, an instruction may either appear |
| 937 | // in the vector map once (as it is calculating >vectorwidth< values at a |
| 938 | // time. Or (if the values are calculated using scalar operations), it |
| 939 | // appears once in every dimension of the scalarMap. |
| 940 | VectorValueMapT ScalarBlockMap(getVectorWidth()); |
| 941 | ValueMapT VectorBlockMap; |
| 942 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 943 | for (Instruction &Inst : *BB) |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 944 | copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses); |
Hongbin Zheng | 3b11a16 | 2012-04-25 13:16:49 +0000 | [diff] [blame] | 945 | } |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 946 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 947 | BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB, |
| 948 | BasicBlock *BBCopy) { |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 949 | |
| 950 | BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock(); |
| 951 | BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom); |
| 952 | |
| 953 | if (BBCopyIDom) |
| 954 | DT.changeImmediateDominator(BBCopy, BBCopyIDom); |
| 955 | |
| 956 | return BBCopyIDom; |
| 957 | } |
| 958 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 959 | void RegionGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, |
Tobias Grosser | 2d1ed0b | 2015-08-27 07:28:16 +0000 | [diff] [blame] | 960 | isl_id_to_ast_expr *IdToAstExp) { |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 961 | assert(Stmt.isRegionStmt() && |
Tobias Grosser | d3f2183 | 2015-08-01 06:26:51 +0000 | [diff] [blame] | 962 | "Only region statements can be copied by the region generator"); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 963 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 964 | // Forget all old mappings. |
| 965 | BlockMap.clear(); |
| 966 | RegionMaps.clear(); |
| 967 | IncompletePHINodeMap.clear(); |
| 968 | |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 969 | // The region represented by the statement. |
| 970 | Region *R = Stmt.getRegion(); |
| 971 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 972 | // Create a dedicated entry for the region where we can reload all demoted |
| 973 | // inputs. |
| 974 | BasicBlock *EntryBB = R->getEntry(); |
| 975 | BasicBlock *EntryBBCopy = |
| 976 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI); |
| 977 | EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry"); |
| 978 | Builder.SetInsertPoint(EntryBBCopy->begin()); |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 979 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 980 | for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI) |
| 981 | if (!R->contains(*PI)) |
| 982 | BlockMap[*PI] = EntryBBCopy; |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 983 | |
| 984 | // Iterate over all blocks in the region in a breadth-first search. |
| 985 | std::deque<BasicBlock *> Blocks; |
| 986 | SmallPtrSet<BasicBlock *, 8> SeenBlocks; |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 987 | Blocks.push_back(EntryBB); |
| 988 | SeenBlocks.insert(EntryBB); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 989 | |
| 990 | while (!Blocks.empty()) { |
| 991 | BasicBlock *BB = Blocks.front(); |
| 992 | Blocks.pop_front(); |
| 993 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 994 | // First split the block and update dominance information. |
| 995 | BasicBlock *BBCopy = splitBB(BB); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 996 | BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy); |
| 997 | |
| 998 | // In order to remap PHI nodes we store also basic block mappings. |
| 999 | BlockMap[BB] = BBCopy; |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1000 | |
| 1001 | // Get the mapping for this block and initialize it with the mapping |
| 1002 | // available at its immediate dominator (in the new region). |
| 1003 | ValueMapT &RegionMap = RegionMaps[BBCopy]; |
| 1004 | RegionMap = RegionMaps[BBCopyIDom]; |
| 1005 | |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1006 | // Copy the block with the BlockGenerator. |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1007 | copyBB(Stmt, BB, BBCopy, RegionMap, LTS, IdToAstExp); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1008 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1009 | // In order to remap PHI nodes we store also basic block mappings. |
| 1010 | BlockMap[BB] = BBCopy; |
| 1011 | |
| 1012 | // Add values to incomplete PHI nodes waiting for this block to be copied. |
| 1013 | for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB]) |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1014 | addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB, LTS); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1015 | IncompletePHINodeMap[BB].clear(); |
| 1016 | |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1017 | // And continue with new successors inside the region. |
| 1018 | for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++) |
| 1019 | if (R->contains(*SI) && SeenBlocks.insert(*SI).second) |
| 1020 | Blocks.push_back(*SI); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | // 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] | 1024 | BasicBlock *ExitBBCopy = |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1025 | SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1026 | ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit"); |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1027 | BlockMap[R->getExit()] = ExitBBCopy; |
| 1028 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1029 | repairDominance(R->getExit(), ExitBBCopy); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1030 | |
| 1031 | // As the block generator doesn't handle control flow we need to add the |
| 1032 | // region control flow by hand after all blocks have been copied. |
| 1033 | for (BasicBlock *BB : SeenBlocks) { |
| 1034 | |
| 1035 | BranchInst *BI = cast<BranchInst>(BB->getTerminator()); |
| 1036 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1037 | BasicBlock *BBCopy = BlockMap[BB]; |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1038 | Instruction *BICopy = BBCopy->getTerminator(); |
| 1039 | |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1040 | ValueMapT &RegionMap = RegionMaps[BBCopy]; |
| 1041 | RegionMap.insert(BlockMap.begin(), BlockMap.end()); |
| 1042 | |
Tobias Grosser | 45e7944 | 2015-08-01 09:07:57 +0000 | [diff] [blame] | 1043 | Builder.SetInsertPoint(BICopy); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1044 | copyInstScalar(Stmt, BI, RegionMap, LTS); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1045 | BICopy->eraseFromParent(); |
| 1046 | } |
| 1047 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1048 | // Add counting PHI nodes to all loops in the region that can be used as |
| 1049 | // replacement for SCEVs refering to the old loop. |
| 1050 | for (BasicBlock *BB : SeenBlocks) { |
| 1051 | Loop *L = LI.getLoopFor(BB); |
| 1052 | if (L == nullptr || L->getHeader() != BB) |
| 1053 | continue; |
| 1054 | |
| 1055 | BasicBlock *BBCopy = BlockMap[BB]; |
| 1056 | Value *NullVal = Builder.getInt32(0); |
| 1057 | PHINode *LoopPHI = |
| 1058 | PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv"); |
| 1059 | Instruction *LoopPHIInc = BinaryOperator::CreateAdd( |
| 1060 | LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc"); |
| 1061 | LoopPHI->insertBefore(BBCopy->begin()); |
| 1062 | LoopPHIInc->insertBefore(BBCopy->getTerminator()); |
| 1063 | |
| 1064 | for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) { |
| 1065 | if (!R->contains(PredBB)) |
| 1066 | continue; |
| 1067 | if (L->contains(PredBB)) |
| 1068 | LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]); |
| 1069 | else |
| 1070 | LoopPHI->addIncoming(NullVal, BlockMap[PredBB]); |
| 1071 | } |
| 1072 | |
| 1073 | for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy))) |
| 1074 | if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0) |
| 1075 | LoopPHI->addIncoming(NullVal, PredBBCopy); |
| 1076 | |
| 1077 | LTS[L] = SE.getUnknown(LoopPHI); |
| 1078 | } |
| 1079 | |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1080 | // Reset the old insert point for the build. |
Johannes Doerfert | 514f6ef | 2015-02-27 18:29:04 +0000 | [diff] [blame] | 1081 | Builder.SetInsertPoint(ExitBBCopy->begin()); |
Johannes Doerfert | 275a175 | 2015-02-24 16:16:32 +0000 | [diff] [blame] | 1082 | } |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1083 | |
| 1084 | void RegionGenerator::generateScalarLoads(ScopStmt &Stmt, |
| 1085 | const Instruction *Inst, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1086 | ValueMapT &BBMap) { |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1087 | |
| 1088 | // Inside a non-affine region PHI nodes are copied not demoted. Once the |
| 1089 | // phi is copied it will reload all inputs from outside the region, hence |
| 1090 | // we do not need to generate code for the read access of the operands of a |
| 1091 | // PHI. |
| 1092 | if (isa<PHINode>(Inst)) |
| 1093 | return; |
| 1094 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1095 | return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB, |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1099 | ValueMapT &BBMap) { |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1100 | const Region &R = Stmt.getParent()->getRegion(); |
| 1101 | |
Tobias Grosser | 7529690 | 2015-08-21 19:23:21 +0000 | [diff] [blame] | 1102 | assert(Stmt.getRegion() && |
| 1103 | "Block statements need to use the generateScalarStores() " |
| 1104 | "function in the BlockGenerator"); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1105 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1106 | for (MemoryAccess *MA : Stmt) { |
| 1107 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1108 | if (!MA->isScalar() || MA->isRead()) |
| 1109 | continue; |
| 1110 | |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1111 | Instruction *ScalarInst = MA->getAccessInstruction(); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1112 | |
Tobias Grosser | 6213913 | 2015-08-02 16:17:41 +0000 | [diff] [blame] | 1113 | // Only generate accesses that belong to this basic block. |
| 1114 | if (ScalarInst->getParent() != BB) |
| 1115 | continue; |
| 1116 | |
Johannes Doerfert | d86f215 | 2015-08-17 10:58:17 +0000 | [diff] [blame] | 1117 | Value *Val = MA->getAccessValue(); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1118 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1119 | auto Address = getOrCreateAlloca(*MA); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1120 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1121 | Val = getNewScalarValue(Val, R, BBMap); |
Tobias Grosser | f8d55f7 | 2015-08-29 18:12:03 +0000 | [diff] [blame] | 1122 | Builder.CreateStore(Val, Address); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI, |
| 1127 | PHINode *PHICopy, BasicBlock *IncomingBB, |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1128 | LoopToScevMapT <S) { |
| 1129 | Region *StmtR = Stmt.getRegion(); |
| 1130 | |
| 1131 | // If the incoming block was not yet copied mark this PHI as incomplete. |
| 1132 | // Once the block will be copied the incoming value will be added. |
| 1133 | BasicBlock *BBCopy = BlockMap[IncomingBB]; |
| 1134 | if (!BBCopy) { |
| 1135 | assert(StmtR->contains(IncomingBB) && |
| 1136 | "Bad incoming block for PHI in non-affine region"); |
| 1137 | IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy)); |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | Value *OpCopy = nullptr; |
| 1142 | if (StmtR->contains(IncomingBB)) { |
| 1143 | assert(RegionMaps.count(BBCopy) && |
| 1144 | "Incoming PHI block did not have a BBMap"); |
| 1145 | ValueMapT &BBCopyMap = RegionMaps[BBCopy]; |
| 1146 | |
| 1147 | Value *Op = PHI->getIncomingValueForBlock(IncomingBB); |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1148 | OpCopy = getNewValue(Stmt, Op, BBCopyMap, LTS, getLoopForInst(PHI)); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1149 | } else { |
| 1150 | |
| 1151 | if (PHICopy->getBasicBlockIndex(BBCopy) >= 0) |
| 1152 | return; |
| 1153 | |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1154 | Value *PHIOpAddr = getOrCreatePHIAlloca(const_cast<PHINode *>(PHI)); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1155 | OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload", |
| 1156 | BlockMap[IncomingBB]->getTerminator()); |
| 1157 | } |
| 1158 | |
| 1159 | assert(OpCopy && "Incoming PHI value was not copied properly"); |
| 1160 | assert(BBCopy && "Incoming PHI block was not copied properly"); |
| 1161 | PHICopy->addIncoming(OpCopy, BBCopy); |
| 1162 | } |
| 1163 | |
| 1164 | Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI, |
| 1165 | ValueMapT &BBMap, |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1166 | LoopToScevMapT <S) { |
| 1167 | unsigned NumIncoming = PHI->getNumIncomingValues(); |
| 1168 | PHINode *PHICopy = |
| 1169 | Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName()); |
| 1170 | PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI()); |
| 1171 | BBMap[PHI] = PHICopy; |
| 1172 | |
| 1173 | for (unsigned u = 0; u < NumIncoming; u++) |
Tobias Grosser | bc13260 | 2015-09-05 09:56:54 +0000 | [diff] [blame] | 1174 | addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), LTS); |
Johannes Doerfert | ecff11d | 2015-05-22 23:43:58 +0000 | [diff] [blame] | 1175 | return PHICopy; |
| 1176 | } |