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