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