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