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