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