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