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