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