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