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