blob: 7764d82e0adb4d5e64b47b54de7a8d42450849ce [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 Grosserecfe21b2013-03-20 18:03:18 +000045bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
46 ScalarEvolution *SE, const Region *R) {
Tobias Grosser683b8e42014-11-30 14:33:31 +000047 if (!I || !SE->isSCEVable(I->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000048 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000049
Tobias Grosser683b8e42014-11-30 14:33:31 +000050 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
51 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 Grossere71c6ab2012-04-27 16:36:14 +0000126 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
127 Builder.GetInsertPoint());
128
129 BBMap[Old] = Expanded;
130 return Expanded;
131 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000132 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000133
Tobias Grosser16371ac2014-11-05 20:48:56 +0000134 // 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 Doerfertbe9c9112015-02-06 21:39:31 +0000140 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000141 return const_cast<Value *>(Old);
142
143 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000144 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000145 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000146}
147
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000148void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
149 ValueMapT &BBMap, ValueMapT &GlobalMap,
150 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000151 // 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 Zheng3b11a162012-04-25 13:16:49 +0000157 Instruction *NewInst = Inst->clone();
158
159 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000160 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000161 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
162 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000163
164 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000165 assert(!isa<StoreInst>(NewInst) &&
166 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000167 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 Doerfertbe9c9112015-02-06 21:39:31 +0000181Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt,
182 const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000183 isl_pw_multi_aff *PWAccRel;
184 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000185 isl_ast_expr *Expr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000186 isl_ast_build *Build = Stmt.getAstBuild();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000187
Johannes Doerferta63b2572014-08-03 01:51:59 +0000188 assert(ExprBuilder && Build &&
189 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000190
Johannes Doerferta99130f2014-10-13 12:58:03 +0000191 Schedule = isl_ast_build_get_schedule(Build);
192 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000193
Johannes Doerferta63b2572014-08-03 01:51:59 +0000194 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000195 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000196
197 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000198}
199
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000200Value *BlockGenerator::generateLocationAccessed(
201 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
202 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
203 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000204
205 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000206 if (MA.hasNewAccessRelation())
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000207 NewPointer = getNewAccessOperand(Stmt, MA);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000208 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000209 NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000210 getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000211
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000212 return NewPointer;
213}
214
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000215Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000216 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000217}
218
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000219Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000220 ValueMapT &BBMap,
221 ValueMapT &GlobalMap,
222 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000223 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000224 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000225 generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000226 Value *ScalarLoad = Builder.CreateAlignedLoad(
227 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000228 return ScalarLoad;
229}
230
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000231Value *BlockGenerator::generateScalarStore(ScopStmt &Stmt,
232 const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000233 ValueMapT &BBMap,
234 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
Johannes Doerfert87901452014-10-02 16:22:19 +0000242 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
243 Store->getAlignment());
244 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000245}
246
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000247void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
248 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000249 LoopToScevMapT &LTS) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000250
251 // First check for possible scalar dependences for this instruction.
252 generateScalarLoads(Stmt, Inst, BBMap);
253
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000254 // Terminator instructions control the control flow. They are explicitly
255 // expressed in the clast and do not need to be copied.
256 if (Inst->isTerminator())
257 return;
258
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000259 Loop *L = getLoopForInst(Inst);
260 if ((Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) &&
261 canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) {
262 Value *NewValue = getNewValue(Stmt, Inst, BBMap, GlobalMap, LTS, L);
263 BBMap[Inst] = NewValue;
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000264 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000265 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000266
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000267 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000268 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000269 // Compute NewLoad before its insertion in BBMap to make the insertion
270 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000271 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000272 return;
273 }
274
275 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000276 Value *NewStore = generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000277 // Compute NewStore before its insertion in BBMap to make the insertion
278 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000279 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000280 return;
281 }
282
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000283 if (const PHINode *PHI = dyn_cast<PHINode>(Inst)) {
284 copyPHIInstruction(Stmt, PHI, BBMap, GlobalMap, LTS);
285 return;
286 }
287
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000288 // Skip some special intrinsics for which we do not adjust the semantics to
289 // the new schedule. All others are handled like every other instruction.
290 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
291 switch (IT->getIntrinsicID()) {
292 // Lifetime markers are ignored.
293 case llvm::Intrinsic::lifetime_start:
294 case llvm::Intrinsic::lifetime_end:
295 // Invariant markers are ignored.
296 case llvm::Intrinsic::invariant_start:
297 case llvm::Intrinsic::invariant_end:
298 // Some misc annotations are ignored.
299 case llvm::Intrinsic::var_annotation:
300 case llvm::Intrinsic::ptr_annotation:
301 case llvm::Intrinsic::annotation:
302 case llvm::Intrinsic::donothing:
303 case llvm::Intrinsic::assume:
304 case llvm::Intrinsic::expect:
305 return;
306 default:
307 // Other intrinsics are copied.
308 break;
309 }
310 }
311
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000312 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000313}
314
Johannes Doerfert275a1752015-02-24 16:16:32 +0000315void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
316 LoopToScevMapT &LTS) {
317 assert(Stmt.isBlockStmt() &&
318 "Only block statements can be copied by the block generator");
319
320 ValueMapT BBMap;
321
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000322 BasicBlock *BB = Stmt.getBasicBlock();
Johannes Doerfert275a1752015-02-24 16:16:32 +0000323 copyBB(Stmt, BB, BBMap, GlobalMap, LTS);
324}
325
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000326BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000327 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000328 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000329 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000330 return CopyBB;
331}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000332
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000333BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
334 ValueMapT &BBMap, ValueMapT &GlobalMap,
335 LoopToScevMapT &LTS) {
336 BasicBlock *CopyBB = splitBB(BB);
337 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS);
338 return CopyBB;
339}
340
341void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
342 ValueMapT &BBMap, ValueMapT &GlobalMap,
343 LoopToScevMapT &LTS) {
344 Builder.SetInsertPoint(CopyBB->begin());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000345 EntryBB = &CopyBB->getParent()->getEntryBlock();
346
Tobias Grosser91f5b262014-06-04 08:06:40 +0000347 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000348 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000349
350 // After a basic block was copied store all scalars that escape this block
351 // in their alloca. First the scalars that have dependences inside the SCoP,
352 // then the ones that might escape the SCoP.
353 generateScalarStores(Stmt, BB, BBMap, GlobalMap);
354
355 const Region &R = Stmt.getParent()->getRegion();
356 for (Instruction &Inst : *BB)
357 handleOutsideUsers(R, &Inst, BBMap[&Inst]);
358}
359
360AllocaInst *BlockGenerator::getOrCreateAlloca(Instruction *ScalarBase,
361 ScalarAllocaMapTy &Map,
362 const char *NameExt,
363 bool *IsNew) {
364
365 // Check if an alloca was cached for the base instruction.
366 AllocaInst *&Addr = Map[ScalarBase];
367
368 // If needed indicate if it was found already or will be created.
369 if (IsNew)
370 *IsNew = (Addr == nullptr);
371
372 // If no alloca was found create one and insert it in the entry block.
373 if (!Addr) {
374 auto *Ty = ScalarBase->getType();
375 Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
376 Addr->insertBefore(EntryBB->getFirstInsertionPt());
377 }
378
379 return Addr;
380}
381
382void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst,
383 Value *InstCopy) {
384 BasicBlock *ExitBB = R.getExit();
385
386 EscapeUserVectorTy EscapeUsers;
387 for (User *U : Inst->users()) {
388
389 // Non-instruction user will never escape.
390 Instruction *UI = dyn_cast<Instruction>(U);
391 if (!UI)
392 continue;
393
394 if (R.contains(UI) && ExitBB != UI->getParent())
395 continue;
396
397 EscapeUsers.push_back(UI);
398 }
399
400 // Exit if no escape uses were found.
401 if (EscapeUsers.empty())
402 return;
403
404 // If there are escape users we get the alloca for this instruction and put
405 // it in the EscapeMap for later finalization. However, if the alloca was not
406 // created by an already handled scalar dependence we have to initialize it
407 // also. Lastly, if the instruction was copied multiple times we already did
408 // this and can exit.
409 if (EscapeMap.count(Inst))
410 return;
411
412 // Get or create an escape alloca for this instruction.
413 bool IsNew;
414 AllocaInst *ScalarAddr =
415 getOrCreateAlloca(Inst, ScalarMap, ".escape", &IsNew);
416
417 // Remember that this instruction has escape uses and the escape alloca.
418 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
419
420 // If the escape alloca was just created store the instruction in there,
421 // otherwise that happened already.
422 if (IsNew) {
423 assert(InstCopy && "Except PHIs every instruction should have a copy!");
424 Builder.CreateStore(InstCopy, ScalarAddr);
425 }
426}
427
428void BlockGenerator::generateScalarLoads(ScopStmt &Stmt,
429 const Instruction *Inst,
430 ValueMapT &BBMap) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000431 auto *MAL = Stmt.lookupAccessesFor(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000432
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000433 if (!MAL)
434 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000435
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000436 for (MemoryAccess &MA : *MAL) {
437 AllocaInst *Address;
438 if (!MA.isScalar() || !MA.isRead())
439 continue;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000440
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000441 auto Base = cast<Instruction>(MA.getBaseAddr());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000442
Tobias Grosser92245222015-07-28 14:53:44 +0000443 if (MA.getScopArrayInfo()->isPHI())
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000444 Address = getOrCreateAlloca(Base, PHIOpMap, ".phiops");
445 else
446 Address = getOrCreateAlloca(Base, ScalarMap, ".s2a");
447
448 BBMap[Base] = Builder.CreateLoad(Address, Address->getName() + ".reload");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000449 }
450}
451
452Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R,
453 ScalarAllocaMapTy &ReloadMap,
454 ValueMapT &BBMap,
455 ValueMapT &GlobalMap) {
456 // If the value we want to store is an instruction we might have demoted it
457 // in order to make it accessible here. In such a case a reload is
458 // necessary. If it is no instruction it will always be a value that
459 // dominates the current point and we can just use it. In total there are 4
460 // options:
461 // (1) The value is no instruction ==> use the value.
462 // (2) The value is an instruction that was split out of the region prior to
463 // code generation ==> use the instruction as it dominates the region.
464 // (3) The value is an instruction:
465 // (a) The value was defined in the current block, thus a copy is in
466 // the BBMap ==> use the mapped value.
467 // (b) The value was defined in a previous block, thus we demoted it
468 // earlier ==> use the reloaded value.
469 Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue);
470 if (!ScalarValueInst)
471 return ScalarValue;
472
473 if (!R.contains(ScalarValueInst)) {
474 if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst))
475 return /* Case (3a) */ ScalarValueCopy;
476 else
477 return /* Case 2 */ ScalarValue;
478 }
479
480 if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst))
481 return /* Case (3a) */ ScalarValueCopy;
482
483 // Case (3b)
484 assert(ReloadMap.count(ScalarValueInst) &&
485 "ScalarInst not mapped in the block and not in the given reload map!");
486 Value *ReloadAddr = ReloadMap[ScalarValueInst];
487 ScalarValue =
488 Builder.CreateLoad(ReloadAddr, ReloadAddr->getName() + ".reload");
489
490 return ScalarValue;
491}
492
493void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
494 ValueMapT &BBMap,
495 ValueMapT &GlobalMap) {
496 const Region &R = Stmt.getParent()->getRegion();
497
498 assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() &&
499 "Region statements need to use the generateScalarStores() "
500 "function in the RegionGenerator");
501
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000502 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000503 if (!MA->isScalar() || MA->isRead())
504 continue;
505
Tobias Grosser92245222015-07-28 14:53:44 +0000506 Instruction *Base = cast<Instruction>(MA->getBaseAddr());
507 Instruction *Inst = MA->getAccessInstruction();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000508
Tobias Grosser92245222015-07-28 14:53:44 +0000509 Value *Val = nullptr;
510 AllocaInst *Address = nullptr;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000511
Tobias Grosser92245222015-07-28 14:53:44 +0000512 if (MA->getScopArrayInfo()->isPHI()) {
513 PHINode *BasePHI = dyn_cast<PHINode>(Base);
514 int PHIIdx = BasePHI->getBasicBlockIndex(BB);
515 Address = getOrCreateAlloca(Base, PHIOpMap, ".phiops");
516 Val = BasePHI->getIncomingValue(PHIIdx);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000517 } else {
Tobias Grosser92245222015-07-28 14:53:44 +0000518 Address = getOrCreateAlloca(Base, ScalarMap, ".s2a");
519 Val = Inst;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000520 }
Tobias Grosser92245222015-07-28 14:53:44 +0000521 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
522 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000523 }
524}
525
526void BlockGenerator::createScalarInitialization(Region &R,
527 ValueMapT &GlobalMap) {
528 // The split block __just before__ the region and optimized region.
529 BasicBlock *SplitBB = R.getEnteringBlock();
530 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
531 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
532
533 // Get the start block of the __optimized__ region.
534 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
535 if (StartBB == R.getEntry())
536 StartBB = SplitBBTerm->getSuccessor(1);
537
538 // For each PHI predecessor outside the region store the incoming operand
539 // value prior to entering the optimized region.
540 Builder.SetInsertPoint(StartBB->getTerminator());
541
542 ScalarAllocaMapTy EmptyMap;
543 for (const auto &PHIOpMapping : PHIOpMap) {
544 const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
545
546 // Check if this PHI has the split block as predecessor (that is the only
547 // possible predecessor outside the SCoP).
548 int idx = PHI->getBasicBlockIndex(SplitBB);
549 if (idx < 0)
550 continue;
551
552 Value *ScalarValue = PHI->getIncomingValue(idx);
553 ScalarValue =
554 getNewScalarValue(ScalarValue, R, EmptyMap, GlobalMap, GlobalMap);
555
556 // If the split block is the predecessor initialize the PHI operator alloca.
557 Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
558 }
559}
560
561void BlockGenerator::createScalarFinalization(Region &R) {
562 // The exit block of the __unoptimized__ region.
563 BasicBlock *ExitBB = R.getExitingBlock();
564 // The merge block __just after__ the region and the optimized region.
565 BasicBlock *MergeBB = R.getExit();
566
567 // The exit block of the __optimized__ region.
568 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
569 if (OptExitBB == ExitBB)
570 OptExitBB = *(++pred_begin(MergeBB));
571
572 Builder.SetInsertPoint(OptExitBB->getTerminator());
573 for (const auto &EscapeMapping : EscapeMap) {
574 // Extract the escaping instruction and the escaping users as well as the
575 // alloca the instruction was demoted to.
576 Instruction *EscapeInst = EscapeMapping.getFirst();
577 const auto &EscapeMappingValue = EscapeMapping.getSecond();
578 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
579 AllocaInst *ScalarAddr = EscapeMappingValue.first;
580
581 // Reload the demoted instruction in the optimized version of the SCoP.
582 Instruction *EscapeInstReload =
583 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
584
585 // Create the merge PHI that merges the optimized and unoptimized version.
586 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
587 EscapeInst->getName() + ".merge");
588 MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
589
590 // Add the respective values to the merge PHI.
591 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
592 MergePHI->addIncoming(EscapeInst, ExitBB);
593
594 // The information of scalar evolution about the escaping instruction needs
595 // to be revoked so the new merged instruction will be used.
596 if (SE.isSCEVable(EscapeInst->getType()))
597 SE.forgetValue(EscapeInst);
598
599 // Replace all uses of the demoted instruction with the merge PHI.
600 for (Instruction *EUser : EscapeUsers)
601 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
602 }
603}
604
605void BlockGenerator::finalizeSCoP(Scop &S, ValueMapT &GlobalMap) {
606 createScalarInitialization(S.getRegion(), GlobalMap);
607 createScalarFinalization(S.getRegion());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000608}
609
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000610VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
611 VectorValueMapT &GlobalMaps,
612 std::vector<LoopToScevMapT> &VLTS,
613 isl_map *Schedule)
614 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
615 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000616 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
617 assert(Schedule && "No statement domain provided");
618}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000619
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000620Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000621 ValueMapT &VectorMap,
622 VectorValueMapT &ScalarMaps,
623 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000624 if (Value *NewValue = VectorMap.lookup(Old))
625 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000626
627 int Width = getVectorWidth();
628
629 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
630
631 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000632 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000633 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
634 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000635 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000636
637 VectorMap[Old] = Vector;
638
639 return Vector;
640}
641
642Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
643 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
644 assert(PointerTy && "PointerType expected");
645
646 Type *ScalarType = PointerTy->getElementType();
647 VectorType *VectorType = VectorType::get(ScalarType, Width);
648
649 return PointerType::getUnqual(VectorType);
650}
651
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000652Value *VectorBlockGenerator::generateStrideOneLoad(
653 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
654 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000655 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000656 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000657 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
658 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
659
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000660 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000661 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000662 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000663 Value *VectorPtr =
664 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
665 LoadInst *VecLoad =
666 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000667 if (!Aligned)
668 VecLoad->setAlignment(8);
669
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000670 if (NegativeStride) {
671 SmallVector<Constant *, 16> Indices;
672 for (int i = VectorWidth - 1; i >= 0; i--)
673 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
674 Constant *SV = llvm::ConstantVector::get(Indices);
675 Value *RevVecLoad = Builder.CreateShuffleVector(
676 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
677 return RevVecLoad;
678 }
679
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000680 return VecLoad;
681}
682
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000683Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
684 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000685 ValueMapT &BBMap) {
686 const Value *Pointer = Load->getPointerOperand();
687 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000688 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
689 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000690 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
691 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000692 LoadInst *ScalarLoad =
693 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000694
695 if (!Aligned)
696 ScalarLoad->setAlignment(8);
697
Tobias Grosserc14582f2013-02-05 18:01:29 +0000698 Constant *SplatVector = Constant::getNullValue(
699 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000700
Tobias Grosserc14582f2013-02-05 18:01:29 +0000701 Value *VectorLoad = Builder.CreateShuffleVector(
702 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000703 return VectorLoad;
704}
705
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000706Value *VectorBlockGenerator::generateUnknownStrideLoad(
707 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000708 int VectorWidth = getVectorWidth();
709 const Value *Pointer = Load->getPointerOperand();
710 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000711 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000712
713 Value *Vector = UndefValue::get(VectorType);
714
715 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000716 Value *NewPointer = generateLocationAccessed(
717 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000718 Value *ScalarLoad =
719 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
720 Vector = Builder.CreateInsertElement(
721 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000722 }
723
724 return Vector;
725}
726
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000727void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000728 ValueMapT &VectorMap,
729 VectorValueMapT &ScalarMaps) {
Tobias Grosser28736452015-03-23 07:00:36 +0000730 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000731 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000732 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000733 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000734 return;
735 }
736
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000737 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000738
Tobias Grosser95493982014-04-18 09:46:35 +0000739 // Make sure we have scalar values available to access the pointer to
740 // the data location.
741 extractScalarValues(Load, VectorMap, ScalarMaps);
742
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000743 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000744 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000745 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000746 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000747 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000748 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000749 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000750 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000751 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000752
753 VectorMap[Load] = NewLoad;
754}
755
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000756void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
757 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000758 ValueMapT &VectorMap,
759 VectorValueMapT &ScalarMaps) {
760 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000761 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
762 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000763
764 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
765
766 const CastInst *Cast = dyn_cast<CastInst>(Inst);
767 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
768 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
769}
770
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000771void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
772 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000773 ValueMapT &VectorMap,
774 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000775 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000776 Value *OpZero = Inst->getOperand(0);
777 Value *OpOne = Inst->getOperand(1);
778
779 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000780 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
781 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000782
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000783 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000784 Inst->getName() + "p_vec");
785 VectorMap[Inst] = NewInst;
786}
787
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000788void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000789 ValueMapT &VectorMap,
790 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000791 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000792
793 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000794 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000795 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000796
Tobias Grosser50fd7012014-04-17 23:13:49 +0000797 // Make sure we have scalar values available to access the pointer to
798 // the data location.
799 extractScalarValues(Store, VectorMap, ScalarMaps);
800
Sebastian Popa00a0292012-12-18 07:46:06 +0000801 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000802 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000803 Value *NewPointer = generateLocationAccessed(
804 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000805
Tobias Grosserc14582f2013-02-05 18:01:29 +0000806 Value *VectorPtr =
807 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000808 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
809
810 if (!Aligned)
811 Store->setAlignment(8);
812 } else {
813 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000814 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000815 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000816 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000817 Builder.CreateStore(Scalar, NewPointer);
818 }
819 }
820}
821
822bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
823 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000824 for (Value *Operand : Inst->operands())
825 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000826 return true;
827 return false;
828}
829
830bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
831 ValueMapT &VectorMap,
832 VectorValueMapT &ScalarMaps) {
833 bool HasVectorOperand = false;
834 int VectorWidth = getVectorWidth();
835
Tobias Grosser91f5b262014-06-04 08:06:40 +0000836 for (Value *Operand : Inst->operands()) {
837 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000838
839 if (VecOp == VectorMap.end())
840 continue;
841
842 HasVectorOperand = true;
843 Value *NewVector = VecOp->second;
844
845 for (int i = 0; i < VectorWidth; ++i) {
846 ValueMapT &SM = ScalarMaps[i];
847
848 // If there is one scalar extracted, all scalar elements should have
849 // already been extracted by the code here. So no need to check for the
850 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000851 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000852 break;
853
Tobias Grosser91f5b262014-06-04 08:06:40 +0000854 SM[Operand] =
855 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000856 }
857 }
858
859 return HasVectorOperand;
860}
861
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000862void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
863 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000864 ValueMapT &VectorMap,
865 VectorValueMapT &ScalarMaps) {
866 bool HasVectorOperand;
867 int VectorWidth = getVectorWidth();
868
869 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
870
871 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000872 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000873 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000874
875 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
876 return;
877
878 // Make the result available as vector value.
879 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
880 Value *Vector = UndefValue::get(VectorType);
881
882 for (int i = 0; i < VectorWidth; i++)
883 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
884 Builder.getInt32(i));
885
886 VectorMap[Inst] = Vector;
887}
888
Tobias Grosserc14582f2013-02-05 18:01:29 +0000889int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000890
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000891void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
892 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000893 ValueMapT &VectorMap,
894 VectorValueMapT &ScalarMaps) {
895 // Terminator instructions control the control flow. They are explicitly
896 // expressed in the clast and do not need to be copied.
897 if (Inst->isTerminator())
898 return;
899
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000900 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000901 return;
902
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000903 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000904 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000905 return;
906 }
907
908 if (hasVectorOperands(Inst, VectorMap)) {
909 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000910 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000911 return;
912 }
913
914 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000915 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000916 return;
917 }
918
919 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000920 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000921 return;
922 }
923
924 // Falltrough: We generate scalar instructions, if we don't know how to
925 // generate vector code.
926 }
927
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000928 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000929}
930
Johannes Doerfert275a1752015-02-24 16:16:32 +0000931void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
932 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
933 "the vector block generator");
934
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000935 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000936 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000937 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000938 CopyBB->setName("polly.stmt." + BB->getName());
939 Builder.SetInsertPoint(CopyBB->begin());
940
941 // Create two maps that store the mapping from the original instructions of
942 // the old basic block to their copies in the new basic block. Those maps
943 // are basic block local.
944 //
945 // As vector code generation is supported there is one map for scalar values
946 // and one for vector values.
947 //
948 // In case we just do scalar code generation, the vectorMap is not used and
949 // the scalarMap has just one dimension, which contains the mapping.
950 //
951 // In case vector code generation is done, an instruction may either appear
952 // in the vector map once (as it is calculating >vectorwidth< values at a
953 // time. Or (if the values are calculated using scalar operations), it
954 // appears once in every dimension of the scalarMap.
955 VectorValueMapT ScalarBlockMap(getVectorWidth());
956 ValueMapT VectorBlockMap;
957
Tobias Grosser91f5b262014-06-04 08:06:40 +0000958 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000959 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000960}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000961
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000962BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
963 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000964
965 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
966 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
967
968 if (BBCopyIDom)
969 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
970
971 return BBCopyIDom;
972}
973
Johannes Doerfert275a1752015-02-24 16:16:32 +0000974void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
975 LoopToScevMapT &LTS) {
976 assert(Stmt.isRegionStmt() &&
977 "Only region statements can be copied by the block generator");
978
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000979 // Forget all old mappings.
980 BlockMap.clear();
981 RegionMaps.clear();
982 IncompletePHINodeMap.clear();
983
Johannes Doerfert275a1752015-02-24 16:16:32 +0000984 // The region represented by the statement.
985 Region *R = Stmt.getRegion();
986
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000987 // Create a dedicated entry for the region where we can reload all demoted
988 // inputs.
989 BasicBlock *EntryBB = R->getEntry();
990 BasicBlock *EntryBBCopy =
991 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
992 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
993 Builder.SetInsertPoint(EntryBBCopy->begin());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000994
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000995 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
996 if (!R->contains(*PI))
997 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000998
999 // Iterate over all blocks in the region in a breadth-first search.
1000 std::deque<BasicBlock *> Blocks;
1001 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001002 Blocks.push_back(EntryBB);
1003 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001004
1005 while (!Blocks.empty()) {
1006 BasicBlock *BB = Blocks.front();
1007 Blocks.pop_front();
1008
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001009 // First split the block and update dominance information.
1010 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001011 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
1012
1013 // In order to remap PHI nodes we store also basic block mappings.
1014 BlockMap[BB] = BBCopy;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001015
1016 // Get the mapping for this block and initialize it with the mapping
1017 // available at its immediate dominator (in the new region).
1018 ValueMapT &RegionMap = RegionMaps[BBCopy];
1019 RegionMap = RegionMaps[BBCopyIDom];
1020
Johannes Doerfert275a1752015-02-24 16:16:32 +00001021 // Copy the block with the BlockGenerator.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001022 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001023
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001024 // In order to remap PHI nodes we store also basic block mappings.
1025 BlockMap[BB] = BBCopy;
1026
1027 // Add values to incomplete PHI nodes waiting for this block to be copied.
1028 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
1029 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB,
1030 GlobalMap, LTS);
1031 IncompletePHINodeMap[BB].clear();
1032
Johannes Doerfert275a1752015-02-24 16:16:32 +00001033 // And continue with new successors inside the region.
1034 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
1035 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
1036 Blocks.push_back(*SI);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001037 }
1038
1039 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001040 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +00001041 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001042 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001043 BlockMap[R->getExit()] = ExitBBCopy;
1044
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001045 repairDominance(R->getExit(), ExitBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001046
1047 // As the block generator doesn't handle control flow we need to add the
1048 // region control flow by hand after all blocks have been copied.
1049 for (BasicBlock *BB : SeenBlocks) {
1050
1051 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
1052
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001053 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +00001054 Instruction *BICopy = BBCopy->getTerminator();
1055
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001056 ValueMapT &RegionMap = RegionMaps[BBCopy];
1057 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1058
Johannes Doerfert275a1752015-02-24 16:16:32 +00001059 Builder.SetInsertPoint(BBCopy);
1060 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
1061 BICopy->eraseFromParent();
1062 }
1063
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001064 // Add counting PHI nodes to all loops in the region that can be used as
1065 // replacement for SCEVs refering to the old loop.
1066 for (BasicBlock *BB : SeenBlocks) {
1067 Loop *L = LI.getLoopFor(BB);
1068 if (L == nullptr || L->getHeader() != BB)
1069 continue;
1070
1071 BasicBlock *BBCopy = BlockMap[BB];
1072 Value *NullVal = Builder.getInt32(0);
1073 PHINode *LoopPHI =
1074 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1075 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1076 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
1077 LoopPHI->insertBefore(BBCopy->begin());
1078 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1079
1080 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1081 if (!R->contains(PredBB))
1082 continue;
1083 if (L->contains(PredBB))
1084 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1085 else
1086 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1087 }
1088
1089 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1090 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1091 LoopPHI->addIncoming(NullVal, PredBBCopy);
1092
1093 LTS[L] = SE.getUnknown(LoopPHI);
1094 }
1095
1096 // Add all mappings from the region to the global map so outside uses will use
1097 // the copied instructions.
1098 for (auto &BBMap : RegionMaps)
1099 GlobalMap.insert(BBMap.second.begin(), BBMap.second.end());
1100
Johannes Doerfert275a1752015-02-24 16:16:32 +00001101 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001102 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001103}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001104
1105void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
1106 const Instruction *Inst,
1107 ValueMapT &BBMap) {
1108
1109 // Inside a non-affine region PHI nodes are copied not demoted. Once the
1110 // phi is copied it will reload all inputs from outside the region, hence
1111 // we do not need to generate code for the read access of the operands of a
1112 // PHI.
1113 if (isa<PHINode>(Inst))
1114 return;
1115
1116 return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
1117}
1118
1119void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
1120 ValueMapT &BBMap,
1121 ValueMapT &GlobalMap) {
1122 const Region &R = Stmt.getParent()->getRegion();
1123
1124 Region *StmtR = Stmt.getRegion();
1125 assert(StmtR && "Block statements need to use the generateScalarStores() "
1126 "function in the BlockGenerator");
1127
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001128 for (MemoryAccess *MA : Stmt) {
1129
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001130 if (!MA->isScalar() || MA->isRead())
1131 continue;
1132
1133 Instruction *ScalarBase = cast<Instruction>(MA->getBaseAddr());
1134 Instruction *ScalarInst = MA->getAccessInstruction();
1135 PHINode *ScalarBasePHI = dyn_cast<PHINode>(ScalarBase);
1136
Tobias Grosser92245222015-07-28 14:53:44 +00001137 Value *Val = nullptr;
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001138 AllocaInst *ScalarAddr = nullptr;
1139
Tobias Grosser92245222015-07-28 14:53:44 +00001140 if (MA->getScopArrayInfo()->isPHI()) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001141 int PHIIdx = ScalarBasePHI->getBasicBlockIndex(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001142 ScalarAddr = getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
Tobias Grosser92245222015-07-28 14:53:44 +00001143 Val = ScalarBasePHI->getIncomingValue(PHIIdx);
1144 } else {
1145 ScalarAddr = getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
1146 Val = ScalarInst;
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001147 }
1148
Tobias Grosser92245222015-07-28 14:53:44 +00001149 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
1150 Builder.CreateStore(Val, ScalarAddr);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001151 }
1152}
1153
1154void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1155 PHINode *PHICopy, BasicBlock *IncomingBB,
1156 ValueMapT &GlobalMap,
1157 LoopToScevMapT &LTS) {
1158 Region *StmtR = Stmt.getRegion();
1159
1160 // If the incoming block was not yet copied mark this PHI as incomplete.
1161 // Once the block will be copied the incoming value will be added.
1162 BasicBlock *BBCopy = BlockMap[IncomingBB];
1163 if (!BBCopy) {
1164 assert(StmtR->contains(IncomingBB) &&
1165 "Bad incoming block for PHI in non-affine region");
1166 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1167 return;
1168 }
1169
1170 Value *OpCopy = nullptr;
1171 if (StmtR->contains(IncomingBB)) {
1172 assert(RegionMaps.count(BBCopy) &&
1173 "Incoming PHI block did not have a BBMap");
1174 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1175
1176 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
1177 OpCopy =
1178 getNewValue(Stmt, Op, BBCopyMap, GlobalMap, LTS, getLoopForInst(PHI));
1179 } else {
1180
1181 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1182 return;
1183
1184 AllocaInst *PHIOpAddr =
1185 getOrCreateAlloca(const_cast<PHINode *>(PHI), PHIOpMap, ".phiops");
1186 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1187 BlockMap[IncomingBB]->getTerminator());
1188 }
1189
1190 assert(OpCopy && "Incoming PHI value was not copied properly");
1191 assert(BBCopy && "Incoming PHI block was not copied properly");
1192 PHICopy->addIncoming(OpCopy, BBCopy);
1193}
1194
1195Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
1196 ValueMapT &BBMap,
1197 ValueMapT &GlobalMap,
1198 LoopToScevMapT &LTS) {
1199 unsigned NumIncoming = PHI->getNumIncomingValues();
1200 PHINode *PHICopy =
1201 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1202 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1203 BBMap[PHI] = PHICopy;
1204
1205 for (unsigned u = 0; u < NumIncoming; u++)
1206 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), GlobalMap,
1207 LTS);
1208 return PHICopy;
1209}