blob: 05e04c9aed3460658112fd5e6966f347ace5ad6b [file] [log] [blame]
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001//===--- 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 Zheng8a846612012-04-25 13:18:28 +000017#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000018#include "polly/CodeGen/CodeGeneration.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000019#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000020#include "polly/Options.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000021#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000022#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000023#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000024#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000025#include "llvm/Analysis/RegionInfo.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000026#include "llvm/Analysis/ScalarEvolution.h"
27#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser030237d2014-02-21 15:06:05 +000028#include "llvm/IR/IntrinsicInst.h"
Tobias Grosserc9895062015-03-10 15:24:33 +000029#include "llvm/IR/Module.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000030#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000031#include "isl/aff.h"
32#include "isl/ast.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000033#include "isl/ast_build.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000034#include "isl/set.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000035#include <deque>
36
Hongbin Zheng3b11a162012-04-25 13:16:49 +000037using namespace llvm;
38using namespace polly;
39
Tobias Grosser878aba42014-10-22 23:22:41 +000040static cl::opt<bool> Aligned("enable-polly-aligned",
41 cl::desc("Assumed aligned memory accesses."),
42 cl::Hidden, cl::init(false), cl::ZeroOrMore,
43 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000044
Tobias Grosserdcc3b432015-08-04 13:54:20 +000045bool polly::canSynthesize(const Value *V, const llvm::LoopInfo *LI,
Tobias Grosserecfe21b2013-03-20 18:03:18 +000046 ScalarEvolution *SE, const Region *R) {
Tobias Grosserdcc3b432015-08-04 13:54:20 +000047 if (!V || !SE->isSCEVable(V->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000048 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000049
Tobias Grosserdcc3b432015-08-04 13:54:20 +000050 if (const SCEV *Scev = SE->getSCEV(const_cast<Value *>(V)))
Tobias Grosser683b8e42014-11-30 14:33:31 +000051 if (!isa<SCEVCouldNotCompute>(Scev))
52 if (!hasScalarDepsInsideRegion(Scev, R))
53 return true;
54
55 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000056}
57
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000058bool polly::isIgnoredIntrinsic(const Value *V) {
59 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
60 switch (IT->getIntrinsicID()) {
61 // Lifetime markers are supported/ignored.
62 case llvm::Intrinsic::lifetime_start:
63 case llvm::Intrinsic::lifetime_end:
64 // Invariant markers are supported/ignored.
65 case llvm::Intrinsic::invariant_start:
66 case llvm::Intrinsic::invariant_end:
67 // Some misc annotations are supported/ignored.
68 case llvm::Intrinsic::var_annotation:
69 case llvm::Intrinsic::ptr_annotation:
70 case llvm::Intrinsic::annotation:
71 case llvm::Intrinsic::donothing:
72 case llvm::Intrinsic::assume:
73 case llvm::Intrinsic::expect:
74 return true;
75 default:
76 break;
77 }
78 }
79 return false;
80}
81
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000082BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
83 ScalarEvolution &SE, DominatorTree &DT,
Johannes Doerfertecff11d2015-05-22 23:43:58 +000084 ScalarAllocaMapTy &ScalarMap,
85 ScalarAllocaMapTy &PHIOpMap,
86 EscapeUsersAllocaMapTy &EscapeMap,
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000087 IslExprBuilder *ExprBuilder)
Johannes Doerfertecff11d2015-05-22 23:43:58 +000088 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT),
89 EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap),
90 EscapeMap(EscapeMap) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000091
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000092Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
93 ValueMapT &BBMap, ValueMapT &GlobalMap,
94 LoopToScevMapT &LTS, Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000095 // We assume constants never change.
96 // This avoids map lookups for many calls to this function.
97 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000098 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000099
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000100 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000101 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +0000102 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000103 New = Builder.CreateTruncOrBitCast(New, Old->getType());
104
105 return New;
106 }
107
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000108 if (Value *New = BBMap.lookup(Old))
109 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000110
Tobias Grosser683b8e42014-11-30 14:33:31 +0000111 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000112 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000113 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000114 const SCEV *NewScev = apply(Scev, LTS, SE);
115 ValueToValueMap VTV;
116 VTV.insert(BBMap.begin(), BBMap.end());
117 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000118 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grosserc9895062015-03-10 15:24:33 +0000119 SCEVExpander Expander(SE, Stmt.getParent()
120 ->getRegion()
121 .getEntry()
122 ->getParent()
123 ->getParent()
124 ->getDataLayout(),
125 "polly");
Tobias Grosser45e79442015-08-01 09:07:57 +0000126 assert(Builder.GetInsertPoint() != Builder.GetInsertBlock()->end() &&
127 "Only instructions can be insert points for SCEVExpander");
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000128 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
129 Builder.GetInsertPoint());
130
131 BBMap[Old] = Expanded;
132 return Expanded;
133 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000134 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000135
Tobias Grosser16371ac2014-11-05 20:48:56 +0000136 // A scop-constant value defined by a global or a function parameter.
137 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
138 return const_cast<Value *>(Old);
139
140 // A scop-constant value defined by an instruction executed outside the scop.
141 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000142 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000143 return const_cast<Value *>(Old);
144
145 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000146 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000147 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000148}
149
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000150void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
151 ValueMapT &BBMap, ValueMapT &GlobalMap,
152 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000153 // We do not generate debug intrinsics as we did not investigate how to
154 // copy them correctly. At the current state, they just crash the code
155 // generation as the meta-data operands are not correctly copied.
156 if (isa<DbgInfoIntrinsic>(Inst))
157 return;
158
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000159 Instruction *NewInst = Inst->clone();
160
161 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000162 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000163 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
164 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000165
166 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000167 assert(!isa<StoreInst>(NewInst) &&
168 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000169 delete NewInst;
170 return;
171 }
172
173 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
174 }
175
176 Builder.Insert(NewInst);
177 BBMap[Inst] = NewInst;
178
179 if (!NewInst->getType()->isVoidTy())
180 NewInst->setName("p_" + Inst->getName());
181}
182
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000183Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt,
184 const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000185 isl_pw_multi_aff *PWAccRel;
186 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000187 isl_ast_expr *Expr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000188 isl_ast_build *Build = Stmt.getAstBuild();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000189
Johannes Doerferta63b2572014-08-03 01:51:59 +0000190 assert(ExprBuilder && Build &&
191 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000192
Johannes Doerferta99130f2014-10-13 12:58:03 +0000193 Schedule = isl_ast_build_get_schedule(Build);
194 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000195
Johannes Doerferta63b2572014-08-03 01:51:59 +0000196 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000197 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000198
199 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000200}
201
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000202Value *BlockGenerator::generateLocationAccessed(
203 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
204 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
205 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000206
207 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000208 if (MA.hasNewAccessRelation())
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000209 NewPointer = getNewAccessOperand(Stmt, MA);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000210 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000211 NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000212 getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000213
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000214 return NewPointer;
215}
216
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000217Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000218 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000219}
220
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000221Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000222 ValueMapT &BBMap,
223 ValueMapT &GlobalMap,
224 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000225 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000226 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000227 generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000228 Value *ScalarLoad = Builder.CreateAlignedLoad(
229 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000230 return ScalarLoad;
231}
232
Tobias Grosserc186ac72015-08-11 08:13:15 +0000233void BlockGenerator::generateScalarStore(ScopStmt &Stmt, const StoreInst *Store,
234 ValueMapT &BBMap, ValueMapT &GlobalMap,
235 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000236 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000237 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000238 generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS);
239 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
240 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000241
Tobias Grosserc186ac72015-08-11 08:13:15 +0000242 Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000243}
244
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000245void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
246 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000247 LoopToScevMapT &LTS) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000248
249 // First check for possible scalar dependences for this instruction.
250 generateScalarLoads(Stmt, Inst, BBMap);
251
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000252 // Terminator instructions control the control flow. They are explicitly
253 // expressed in the clast and do not need to be copied.
254 if (Inst->isTerminator())
255 return;
256
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000257 Loop *L = getLoopForInst(Inst);
258 if ((Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) &&
259 canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) {
260 Value *NewValue = getNewValue(Stmt, Inst, BBMap, GlobalMap, LTS, L);
261 BBMap[Inst] = NewValue;
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000262 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000263 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000264
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000265 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000266 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000267 // Compute NewLoad before its insertion in BBMap to make the insertion
268 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000269 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000270 return;
271 }
272
273 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosserc186ac72015-08-11 08:13:15 +0000274 generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000275 return;
276 }
277
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000278 if (const PHINode *PHI = dyn_cast<PHINode>(Inst)) {
279 copyPHIInstruction(Stmt, PHI, BBMap, GlobalMap, LTS);
280 return;
281 }
282
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000283 // Skip some special intrinsics for which we do not adjust the semantics to
284 // the new schedule. All others are handled like every other instruction.
285 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
286 switch (IT->getIntrinsicID()) {
287 // Lifetime markers are ignored.
288 case llvm::Intrinsic::lifetime_start:
289 case llvm::Intrinsic::lifetime_end:
290 // Invariant markers are ignored.
291 case llvm::Intrinsic::invariant_start:
292 case llvm::Intrinsic::invariant_end:
293 // Some misc annotations are ignored.
294 case llvm::Intrinsic::var_annotation:
295 case llvm::Intrinsic::ptr_annotation:
296 case llvm::Intrinsic::annotation:
297 case llvm::Intrinsic::donothing:
298 case llvm::Intrinsic::assume:
299 case llvm::Intrinsic::expect:
300 return;
301 default:
302 // Other intrinsics are copied.
303 break;
304 }
305 }
306
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000307 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000308}
309
Johannes Doerfert275a1752015-02-24 16:16:32 +0000310void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
311 LoopToScevMapT &LTS) {
312 assert(Stmt.isBlockStmt() &&
313 "Only block statements can be copied by the block generator");
314
315 ValueMapT BBMap;
316
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000317 BasicBlock *BB = Stmt.getBasicBlock();
Johannes Doerfert275a1752015-02-24 16:16:32 +0000318 copyBB(Stmt, BB, BBMap, GlobalMap, LTS);
319}
320
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000321BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000322 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000323 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000324 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000325 return CopyBB;
326}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000327
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000328BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
329 ValueMapT &BBMap, ValueMapT &GlobalMap,
330 LoopToScevMapT &LTS) {
331 BasicBlock *CopyBB = splitBB(BB);
332 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS);
333 return CopyBB;
334}
335
336void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
337 ValueMapT &BBMap, ValueMapT &GlobalMap,
338 LoopToScevMapT &LTS) {
339 Builder.SetInsertPoint(CopyBB->begin());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000340 EntryBB = &CopyBB->getParent()->getEntryBlock();
341
Tobias Grosser91f5b262014-06-04 08:06:40 +0000342 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000343 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000344
345 // After a basic block was copied store all scalars that escape this block
346 // in their alloca. First the scalars that have dependences inside the SCoP,
347 // then the ones that might escape the SCoP.
348 generateScalarStores(Stmt, BB, BBMap, GlobalMap);
349
350 const Region &R = Stmt.getParent()->getRegion();
351 for (Instruction &Inst : *BB)
352 handleOutsideUsers(R, &Inst, BBMap[&Inst]);
353}
354
Tobias Grosser0164b8f2015-08-13 08:07:39 +0000355AllocaInst *BlockGenerator::getOrCreateAlloca(Value *ScalarBase,
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000356 ScalarAllocaMapTy &Map,
357 const char *NameExt,
358 bool *IsNew) {
359
360 // Check if an alloca was cached for the base instruction.
361 AllocaInst *&Addr = Map[ScalarBase];
362
363 // If needed indicate if it was found already or will be created.
364 if (IsNew)
365 *IsNew = (Addr == nullptr);
366
367 // If no alloca was found create one and insert it in the entry block.
368 if (!Addr) {
369 auto *Ty = ScalarBase->getType();
370 Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
371 Addr->insertBefore(EntryBB->getFirstInsertionPt());
372 }
373
374 return Addr;
375}
376
377void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst,
378 Value *InstCopy) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000379 EscapeUserVectorTy EscapeUsers;
380 for (User *U : Inst->users()) {
381
382 // Non-instruction user will never escape.
383 Instruction *UI = dyn_cast<Instruction>(U);
384 if (!UI)
385 continue;
386
Johannes Doerfertddb83d02015-08-16 08:35:40 +0000387 if (R.contains(UI))
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000388 continue;
389
390 EscapeUsers.push_back(UI);
391 }
392
393 // Exit if no escape uses were found.
394 if (EscapeUsers.empty())
395 return;
396
397 // If there are escape users we get the alloca for this instruction and put
398 // it in the EscapeMap for later finalization. However, if the alloca was not
399 // created by an already handled scalar dependence we have to initialize it
400 // also. Lastly, if the instruction was copied multiple times we already did
401 // this and can exit.
402 if (EscapeMap.count(Inst))
403 return;
404
405 // Get or create an escape alloca for this instruction.
406 bool IsNew;
407 AllocaInst *ScalarAddr =
408 getOrCreateAlloca(Inst, ScalarMap, ".escape", &IsNew);
409
410 // Remember that this instruction has escape uses and the escape alloca.
411 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
412
413 // If the escape alloca was just created store the instruction in there,
414 // otherwise that happened already.
415 if (IsNew) {
416 assert(InstCopy && "Except PHIs every instruction should have a copy!");
417 Builder.CreateStore(InstCopy, ScalarAddr);
418 }
419}
420
421void BlockGenerator::generateScalarLoads(ScopStmt &Stmt,
422 const Instruction *Inst,
423 ValueMapT &BBMap) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000424 auto *MAL = Stmt.lookupAccessesFor(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000425
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000426 if (!MAL)
427 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000428
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000429 for (MemoryAccess &MA : *MAL) {
430 AllocaInst *Address;
431 if (!MA.isScalar() || !MA.isRead())
432 continue;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000433
Tobias Grosser0164b8f2015-08-13 08:07:39 +0000434 auto Base = MA.getBaseAddr();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000435
Tobias Grosser92245222015-07-28 14:53:44 +0000436 if (MA.getScopArrayInfo()->isPHI())
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000437 Address = getOrCreateAlloca(Base, PHIOpMap, ".phiops");
438 else
439 Address = getOrCreateAlloca(Base, ScalarMap, ".s2a");
440
441 BBMap[Base] = Builder.CreateLoad(Address, Address->getName() + ".reload");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000442 }
443}
444
445Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R,
446 ScalarAllocaMapTy &ReloadMap,
447 ValueMapT &BBMap,
448 ValueMapT &GlobalMap) {
449 // If the value we want to store is an instruction we might have demoted it
450 // in order to make it accessible here. In such a case a reload is
451 // necessary. If it is no instruction it will always be a value that
452 // dominates the current point and we can just use it. In total there are 4
453 // options:
454 // (1) The value is no instruction ==> use the value.
455 // (2) The value is an instruction that was split out of the region prior to
456 // code generation ==> use the instruction as it dominates the region.
457 // (3) The value is an instruction:
458 // (a) The value was defined in the current block, thus a copy is in
459 // the BBMap ==> use the mapped value.
460 // (b) The value was defined in a previous block, thus we demoted it
461 // earlier ==> use the reloaded value.
462 Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue);
463 if (!ScalarValueInst)
464 return ScalarValue;
465
466 if (!R.contains(ScalarValueInst)) {
467 if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst))
468 return /* Case (3a) */ ScalarValueCopy;
469 else
470 return /* Case 2 */ ScalarValue;
471 }
472
473 if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst))
474 return /* Case (3a) */ ScalarValueCopy;
475
476 // Case (3b)
Johannes Doerferte1fa6da2015-08-17 09:38:46 +0000477 Value *ReloadAddr = getOrCreateAlloca(ScalarValueInst, ReloadMap, ".s2a");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000478 ScalarValue =
479 Builder.CreateLoad(ReloadAddr, ReloadAddr->getName() + ".reload");
480
481 return ScalarValue;
482}
483
484void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
485 ValueMapT &BBMap,
486 ValueMapT &GlobalMap) {
487 const Region &R = Stmt.getParent()->getRegion();
488
489 assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() &&
490 "Region statements need to use the generateScalarStores() "
491 "function in the RegionGenerator");
492
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000493 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000494 if (!MA->isScalar() || MA->isRead())
495 continue;
496
Tobias Grosser92245222015-07-28 14:53:44 +0000497 Instruction *Base = cast<Instruction>(MA->getBaseAddr());
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000498 Value *Val = MA->getAccessValue();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000499
Tobias Grosser92245222015-07-28 14:53:44 +0000500 AllocaInst *Address = nullptr;
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000501 if (MA->getScopArrayInfo()->isPHI())
Tobias Grosser92245222015-07-28 14:53:44 +0000502 Address = getOrCreateAlloca(Base, PHIOpMap, ".phiops");
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000503 else
Tobias Grosser92245222015-07-28 14:53:44 +0000504 Address = getOrCreateAlloca(Base, ScalarMap, ".s2a");
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000505
Tobias Grosser92245222015-07-28 14:53:44 +0000506 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
507 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000508 }
509}
510
511void BlockGenerator::createScalarInitialization(Region &R,
512 ValueMapT &GlobalMap) {
513 // The split block __just before__ the region and optimized region.
514 BasicBlock *SplitBB = R.getEnteringBlock();
515 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
516 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
517
518 // Get the start block of the __optimized__ region.
519 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
520 if (StartBB == R.getEntry())
521 StartBB = SplitBBTerm->getSuccessor(1);
522
523 // For each PHI predecessor outside the region store the incoming operand
524 // value prior to entering the optimized region.
525 Builder.SetInsertPoint(StartBB->getTerminator());
526
527 ScalarAllocaMapTy EmptyMap;
528 for (const auto &PHIOpMapping : PHIOpMap) {
529 const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
530
531 // Check if this PHI has the split block as predecessor (that is the only
532 // possible predecessor outside the SCoP).
533 int idx = PHI->getBasicBlockIndex(SplitBB);
534 if (idx < 0)
535 continue;
536
537 Value *ScalarValue = PHI->getIncomingValue(idx);
538 ScalarValue =
539 getNewScalarValue(ScalarValue, R, EmptyMap, GlobalMap, GlobalMap);
540
541 // If the split block is the predecessor initialize the PHI operator alloca.
542 Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
543 }
544}
545
546void BlockGenerator::createScalarFinalization(Region &R) {
547 // The exit block of the __unoptimized__ region.
548 BasicBlock *ExitBB = R.getExitingBlock();
549 // The merge block __just after__ the region and the optimized region.
550 BasicBlock *MergeBB = R.getExit();
551
552 // The exit block of the __optimized__ region.
553 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
554 if (OptExitBB == ExitBB)
555 OptExitBB = *(++pred_begin(MergeBB));
556
557 Builder.SetInsertPoint(OptExitBB->getTerminator());
558 for (const auto &EscapeMapping : EscapeMap) {
559 // Extract the escaping instruction and the escaping users as well as the
560 // alloca the instruction was demoted to.
561 Instruction *EscapeInst = EscapeMapping.getFirst();
562 const auto &EscapeMappingValue = EscapeMapping.getSecond();
563 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
564 AllocaInst *ScalarAddr = EscapeMappingValue.first;
565
566 // Reload the demoted instruction in the optimized version of the SCoP.
567 Instruction *EscapeInstReload =
568 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
569
570 // Create the merge PHI that merges the optimized and unoptimized version.
571 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
572 EscapeInst->getName() + ".merge");
573 MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
574
575 // Add the respective values to the merge PHI.
576 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
577 MergePHI->addIncoming(EscapeInst, ExitBB);
578
579 // The information of scalar evolution about the escaping instruction needs
580 // to be revoked so the new merged instruction will be used.
581 if (SE.isSCEVable(EscapeInst->getType()))
582 SE.forgetValue(EscapeInst);
583
584 // Replace all uses of the demoted instruction with the merge PHI.
585 for (Instruction *EUser : EscapeUsers)
586 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
587 }
588}
589
590void BlockGenerator::finalizeSCoP(Scop &S, ValueMapT &GlobalMap) {
591 createScalarInitialization(S.getRegion(), GlobalMap);
592 createScalarFinalization(S.getRegion());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000593}
594
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000595VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
596 VectorValueMapT &GlobalMaps,
597 std::vector<LoopToScevMapT> &VLTS,
598 isl_map *Schedule)
599 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
600 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000601 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
602 assert(Schedule && "No statement domain provided");
603}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000604
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000605Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000606 ValueMapT &VectorMap,
607 VectorValueMapT &ScalarMaps,
608 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000609 if (Value *NewValue = VectorMap.lookup(Old))
610 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000611
612 int Width = getVectorWidth();
613
614 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
615
616 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000617 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000618 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
619 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000620 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000621
622 VectorMap[Old] = Vector;
623
624 return Vector;
625}
626
627Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
628 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
629 assert(PointerTy && "PointerType expected");
630
631 Type *ScalarType = PointerTy->getElementType();
632 VectorType *VectorType = VectorType::get(ScalarType, Width);
633
634 return PointerType::getUnqual(VectorType);
635}
636
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000637Value *VectorBlockGenerator::generateStrideOneLoad(
638 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
639 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000640 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000641 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000642 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
643 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
644
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000645 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000646 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000647 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000648 Value *VectorPtr =
649 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
650 LoadInst *VecLoad =
651 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000652 if (!Aligned)
653 VecLoad->setAlignment(8);
654
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000655 if (NegativeStride) {
656 SmallVector<Constant *, 16> Indices;
657 for (int i = VectorWidth - 1; i >= 0; i--)
658 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
659 Constant *SV = llvm::ConstantVector::get(Indices);
660 Value *RevVecLoad = Builder.CreateShuffleVector(
661 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
662 return RevVecLoad;
663 }
664
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000665 return VecLoad;
666}
667
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000668Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
669 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000670 ValueMapT &BBMap) {
671 const Value *Pointer = Load->getPointerOperand();
672 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000673 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
674 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000675 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
676 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000677 LoadInst *ScalarLoad =
678 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000679
680 if (!Aligned)
681 ScalarLoad->setAlignment(8);
682
Tobias Grosserc14582f2013-02-05 18:01:29 +0000683 Constant *SplatVector = Constant::getNullValue(
684 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000685
Tobias Grosserc14582f2013-02-05 18:01:29 +0000686 Value *VectorLoad = Builder.CreateShuffleVector(
687 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000688 return VectorLoad;
689}
690
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000691Value *VectorBlockGenerator::generateUnknownStrideLoad(
692 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000693 int VectorWidth = getVectorWidth();
694 const Value *Pointer = Load->getPointerOperand();
695 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000696 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000697
698 Value *Vector = UndefValue::get(VectorType);
699
700 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000701 Value *NewPointer = generateLocationAccessed(
702 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000703 Value *ScalarLoad =
704 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
705 Vector = Builder.CreateInsertElement(
706 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000707 }
708
709 return Vector;
710}
711
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000712void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000713 ValueMapT &VectorMap,
714 VectorValueMapT &ScalarMaps) {
Tobias Grosser28736452015-03-23 07:00:36 +0000715 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000716 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000717 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000718 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000719 return;
720 }
721
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000722 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000723
Tobias Grosser95493982014-04-18 09:46:35 +0000724 // Make sure we have scalar values available to access the pointer to
725 // the data location.
726 extractScalarValues(Load, VectorMap, ScalarMaps);
727
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000728 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000729 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000730 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000731 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000732 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000733 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000734 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000735 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000736 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000737
738 VectorMap[Load] = NewLoad;
739}
740
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000741void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
742 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000743 ValueMapT &VectorMap,
744 VectorValueMapT &ScalarMaps) {
745 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000746 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
747 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000748
749 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
750
751 const CastInst *Cast = dyn_cast<CastInst>(Inst);
752 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
753 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
754}
755
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000756void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
757 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000758 ValueMapT &VectorMap,
759 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000760 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000761 Value *OpZero = Inst->getOperand(0);
762 Value *OpOne = Inst->getOperand(1);
763
764 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000765 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
766 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000767
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000768 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000769 Inst->getName() + "p_vec");
770 VectorMap[Inst] = NewInst;
771}
772
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000773void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000774 ValueMapT &VectorMap,
775 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000776 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000777
778 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000779 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000780 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000781
Tobias Grosser50fd7012014-04-17 23:13:49 +0000782 // Make sure we have scalar values available to access the pointer to
783 // the data location.
784 extractScalarValues(Store, VectorMap, ScalarMaps);
785
Sebastian Popa00a0292012-12-18 07:46:06 +0000786 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000787 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000788 Value *NewPointer = generateLocationAccessed(
789 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000790
Tobias Grosserc14582f2013-02-05 18:01:29 +0000791 Value *VectorPtr =
792 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000793 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
794
795 if (!Aligned)
796 Store->setAlignment(8);
797 } else {
798 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000799 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000800 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000801 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000802 Builder.CreateStore(Scalar, NewPointer);
803 }
804 }
805}
806
807bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
808 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000809 for (Value *Operand : Inst->operands())
810 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000811 return true;
812 return false;
813}
814
815bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
816 ValueMapT &VectorMap,
817 VectorValueMapT &ScalarMaps) {
818 bool HasVectorOperand = false;
819 int VectorWidth = getVectorWidth();
820
Tobias Grosser91f5b262014-06-04 08:06:40 +0000821 for (Value *Operand : Inst->operands()) {
822 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000823
824 if (VecOp == VectorMap.end())
825 continue;
826
827 HasVectorOperand = true;
828 Value *NewVector = VecOp->second;
829
830 for (int i = 0; i < VectorWidth; ++i) {
831 ValueMapT &SM = ScalarMaps[i];
832
833 // If there is one scalar extracted, all scalar elements should have
834 // already been extracted by the code here. So no need to check for the
835 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000836 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000837 break;
838
Tobias Grosser91f5b262014-06-04 08:06:40 +0000839 SM[Operand] =
840 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000841 }
842 }
843
844 return HasVectorOperand;
845}
846
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000847void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
848 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000849 ValueMapT &VectorMap,
850 VectorValueMapT &ScalarMaps) {
851 bool HasVectorOperand;
852 int VectorWidth = getVectorWidth();
853
854 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
855
856 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000857 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000858 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000859
860 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
861 return;
862
863 // Make the result available as vector value.
864 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
865 Value *Vector = UndefValue::get(VectorType);
866
867 for (int i = 0; i < VectorWidth; i++)
868 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
869 Builder.getInt32(i));
870
871 VectorMap[Inst] = Vector;
872}
873
Tobias Grosserc14582f2013-02-05 18:01:29 +0000874int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000875
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000876void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
877 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000878 ValueMapT &VectorMap,
879 VectorValueMapT &ScalarMaps) {
880 // Terminator instructions control the control flow. They are explicitly
881 // expressed in the clast and do not need to be copied.
882 if (Inst->isTerminator())
883 return;
884
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000885 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000886 return;
887
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000888 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000889 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000890 return;
891 }
892
893 if (hasVectorOperands(Inst, VectorMap)) {
894 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000895 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000896 return;
897 }
898
899 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000900 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000901 return;
902 }
903
904 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000905 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000906 return;
907 }
908
909 // Falltrough: We generate scalar instructions, if we don't know how to
910 // generate vector code.
911 }
912
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000913 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000914}
915
Johannes Doerfert275a1752015-02-24 16:16:32 +0000916void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
917 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
918 "the vector block generator");
919
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000920 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000921 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000922 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000923 CopyBB->setName("polly.stmt." + BB->getName());
924 Builder.SetInsertPoint(CopyBB->begin());
925
926 // Create two maps that store the mapping from the original instructions of
927 // the old basic block to their copies in the new basic block. Those maps
928 // are basic block local.
929 //
930 // As vector code generation is supported there is one map for scalar values
931 // and one for vector values.
932 //
933 // In case we just do scalar code generation, the vectorMap is not used and
934 // the scalarMap has just one dimension, which contains the mapping.
935 //
936 // In case vector code generation is done, an instruction may either appear
937 // in the vector map once (as it is calculating >vectorwidth< values at a
938 // time. Or (if the values are calculated using scalar operations), it
939 // appears once in every dimension of the scalarMap.
940 VectorValueMapT ScalarBlockMap(getVectorWidth());
941 ValueMapT VectorBlockMap;
942
Tobias Grosser91f5b262014-06-04 08:06:40 +0000943 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000944 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000945}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000946
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000947BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
948 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000949
950 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
951 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
952
953 if (BBCopyIDom)
954 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
955
956 return BBCopyIDom;
957}
958
Johannes Doerfert275a1752015-02-24 16:16:32 +0000959void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
960 LoopToScevMapT &LTS) {
961 assert(Stmt.isRegionStmt() &&
Tobias Grosserd3f21832015-08-01 06:26:51 +0000962 "Only region statements can be copied by the region generator");
Johannes Doerfert275a1752015-02-24 16:16:32 +0000963
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000964 // Forget all old mappings.
965 BlockMap.clear();
966 RegionMaps.clear();
967 IncompletePHINodeMap.clear();
968
Johannes Doerfert275a1752015-02-24 16:16:32 +0000969 // The region represented by the statement.
970 Region *R = Stmt.getRegion();
971
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000972 // Create a dedicated entry for the region where we can reload all demoted
973 // inputs.
974 BasicBlock *EntryBB = R->getEntry();
975 BasicBlock *EntryBBCopy =
976 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
977 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
978 Builder.SetInsertPoint(EntryBBCopy->begin());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000979
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000980 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
981 if (!R->contains(*PI))
982 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000983
984 // Iterate over all blocks in the region in a breadth-first search.
985 std::deque<BasicBlock *> Blocks;
986 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000987 Blocks.push_back(EntryBB);
988 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000989
990 while (!Blocks.empty()) {
991 BasicBlock *BB = Blocks.front();
992 Blocks.pop_front();
993
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000994 // First split the block and update dominance information.
995 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000996 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
997
998 // In order to remap PHI nodes we store also basic block mappings.
999 BlockMap[BB] = BBCopy;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001000
1001 // Get the mapping for this block and initialize it with the mapping
1002 // available at its immediate dominator (in the new region).
1003 ValueMapT &RegionMap = RegionMaps[BBCopy];
1004 RegionMap = RegionMaps[BBCopyIDom];
1005
Johannes Doerfert275a1752015-02-24 16:16:32 +00001006 // Copy the block with the BlockGenerator.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001007 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001008
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001009 // In order to remap PHI nodes we store also basic block mappings.
1010 BlockMap[BB] = BBCopy;
1011
1012 // Add values to incomplete PHI nodes waiting for this block to be copied.
1013 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
1014 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB,
1015 GlobalMap, LTS);
1016 IncompletePHINodeMap[BB].clear();
1017
Johannes Doerfert275a1752015-02-24 16:16:32 +00001018 // And continue with new successors inside the region.
1019 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
1020 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
1021 Blocks.push_back(*SI);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001022 }
1023
1024 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001025 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +00001026 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001027 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001028 BlockMap[R->getExit()] = ExitBBCopy;
1029
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001030 repairDominance(R->getExit(), ExitBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001031
1032 // As the block generator doesn't handle control flow we need to add the
1033 // region control flow by hand after all blocks have been copied.
1034 for (BasicBlock *BB : SeenBlocks) {
1035
1036 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
1037
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001038 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +00001039 Instruction *BICopy = BBCopy->getTerminator();
1040
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001041 ValueMapT &RegionMap = RegionMaps[BBCopy];
1042 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1043
Tobias Grosser45e79442015-08-01 09:07:57 +00001044 Builder.SetInsertPoint(BICopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001045 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
1046 BICopy->eraseFromParent();
1047 }
1048
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001049 // Add counting PHI nodes to all loops in the region that can be used as
1050 // replacement for SCEVs refering to the old loop.
1051 for (BasicBlock *BB : SeenBlocks) {
1052 Loop *L = LI.getLoopFor(BB);
1053 if (L == nullptr || L->getHeader() != BB)
1054 continue;
1055
1056 BasicBlock *BBCopy = BlockMap[BB];
1057 Value *NullVal = Builder.getInt32(0);
1058 PHINode *LoopPHI =
1059 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1060 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1061 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
1062 LoopPHI->insertBefore(BBCopy->begin());
1063 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1064
1065 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1066 if (!R->contains(PredBB))
1067 continue;
1068 if (L->contains(PredBB))
1069 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1070 else
1071 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1072 }
1073
1074 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1075 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1076 LoopPHI->addIncoming(NullVal, PredBBCopy);
1077
1078 LTS[L] = SE.getUnknown(LoopPHI);
1079 }
1080
1081 // Add all mappings from the region to the global map so outside uses will use
1082 // the copied instructions.
1083 for (auto &BBMap : RegionMaps)
1084 GlobalMap.insert(BBMap.second.begin(), BBMap.second.end());
1085
Johannes Doerfert275a1752015-02-24 16:16:32 +00001086 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001087 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001088}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001089
1090void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
1091 const Instruction *Inst,
1092 ValueMapT &BBMap) {
1093
1094 // Inside a non-affine region PHI nodes are copied not demoted. Once the
1095 // phi is copied it will reload all inputs from outside the region, hence
1096 // we do not need to generate code for the read access of the operands of a
1097 // PHI.
1098 if (isa<PHINode>(Inst))
1099 return;
1100
1101 return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
1102}
1103
1104void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
1105 ValueMapT &BBMap,
1106 ValueMapT &GlobalMap) {
1107 const Region &R = Stmt.getParent()->getRegion();
1108
1109 Region *StmtR = Stmt.getRegion();
1110 assert(StmtR && "Block statements need to use the generateScalarStores() "
1111 "function in the BlockGenerator");
1112
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001113 for (MemoryAccess *MA : Stmt) {
1114
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001115 if (!MA->isScalar() || MA->isRead())
1116 continue;
1117
1118 Instruction *ScalarBase = cast<Instruction>(MA->getBaseAddr());
1119 Instruction *ScalarInst = MA->getAccessInstruction();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001120
Tobias Grosser62139132015-08-02 16:17:41 +00001121 // Only generate accesses that belong to this basic block.
1122 if (ScalarInst->getParent() != BB)
1123 continue;
1124
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001125 Value *Val = MA->getAccessValue();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001126 AllocaInst *ScalarAddr = nullptr;
1127
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001128 if (MA->getScopArrayInfo()->isPHI())
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001129 ScalarAddr = getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001130 else
Tobias Grosser92245222015-07-28 14:53:44 +00001131 ScalarAddr = getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001132
Tobias Grosser92245222015-07-28 14:53:44 +00001133 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
1134 Builder.CreateStore(Val, ScalarAddr);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001135 }
1136}
1137
1138void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1139 PHINode *PHICopy, BasicBlock *IncomingBB,
1140 ValueMapT &GlobalMap,
1141 LoopToScevMapT &LTS) {
1142 Region *StmtR = Stmt.getRegion();
1143
1144 // If the incoming block was not yet copied mark this PHI as incomplete.
1145 // Once the block will be copied the incoming value will be added.
1146 BasicBlock *BBCopy = BlockMap[IncomingBB];
1147 if (!BBCopy) {
1148 assert(StmtR->contains(IncomingBB) &&
1149 "Bad incoming block for PHI in non-affine region");
1150 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1151 return;
1152 }
1153
1154 Value *OpCopy = nullptr;
1155 if (StmtR->contains(IncomingBB)) {
1156 assert(RegionMaps.count(BBCopy) &&
1157 "Incoming PHI block did not have a BBMap");
1158 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1159
1160 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
1161 OpCopy =
1162 getNewValue(Stmt, Op, BBCopyMap, GlobalMap, LTS, getLoopForInst(PHI));
1163 } else {
1164
1165 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1166 return;
1167
1168 AllocaInst *PHIOpAddr =
1169 getOrCreateAlloca(const_cast<PHINode *>(PHI), PHIOpMap, ".phiops");
1170 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1171 BlockMap[IncomingBB]->getTerminator());
1172 }
1173
1174 assert(OpCopy && "Incoming PHI value was not copied properly");
1175 assert(BBCopy && "Incoming PHI block was not copied properly");
1176 PHICopy->addIncoming(OpCopy, BBCopy);
1177}
1178
1179Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
1180 ValueMapT &BBMap,
1181 ValueMapT &GlobalMap,
1182 LoopToScevMapT &LTS) {
1183 unsigned NumIncoming = PHI->getNumIncomingValues();
1184 PHINode *PHICopy =
1185 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1186 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1187 BBMap[PHI] = PHICopy;
1188
1189 for (unsigned u = 0; u < NumIncoming; u++)
1190 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), GlobalMap,
1191 LTS);
1192 return PHICopy;
1193}