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