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