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