blob: 0f488e783f2b9728baad66cdbd779aa8646ca06c [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"
Tobias Grosser030237d2014-02-21 15:06:05 +000027#include "llvm/IR/IntrinsicInst.h"
Tobias Grosserc9895062015-03-10 15:24:33 +000028#include "llvm/IR/Module.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000029#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000030#include "isl/aff.h"
31#include "isl/ast.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000032#include "isl/ast_build.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000033#include "isl/set.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000034#include <deque>
35
Hongbin Zheng3b11a162012-04-25 13:16:49 +000036using namespace llvm;
37using namespace polly;
38
Tobias Grosser878aba42014-10-22 23:22:41 +000039static cl::opt<bool> Aligned("enable-polly-aligned",
40 cl::desc("Assumed aligned memory accesses."),
41 cl::Hidden, cl::init(false), cl::ZeroOrMore,
42 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000043
Tobias Grosserdcc3b432015-08-04 13:54:20 +000044bool polly::canSynthesize(const Value *V, const llvm::LoopInfo *LI,
Tobias Grosserecfe21b2013-03-20 18:03:18 +000045 ScalarEvolution *SE, const Region *R) {
Tobias Grosserdcc3b432015-08-04 13:54:20 +000046 if (!V || !SE->isSCEVable(V->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000047 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000048
Tobias Grosserdcc3b432015-08-04 13:54:20 +000049 if (const SCEV *Scev = SE->getSCEV(const_cast<Value *>(V)))
Tobias Grosser683b8e42014-11-30 14:33:31 +000050 if (!isa<SCEVCouldNotCompute>(Scev))
51 if (!hasScalarDepsInsideRegion(Scev, R))
52 return true;
53
54 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000055}
56
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000057bool polly::isIgnoredIntrinsic(const Value *V) {
58 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
59 switch (IT->getIntrinsicID()) {
60 // Lifetime markers are supported/ignored.
61 case llvm::Intrinsic::lifetime_start:
62 case llvm::Intrinsic::lifetime_end:
63 // Invariant markers are supported/ignored.
64 case llvm::Intrinsic::invariant_start:
65 case llvm::Intrinsic::invariant_end:
66 // Some misc annotations are supported/ignored.
67 case llvm::Intrinsic::var_annotation:
68 case llvm::Intrinsic::ptr_annotation:
69 case llvm::Intrinsic::annotation:
70 case llvm::Intrinsic::donothing:
71 case llvm::Intrinsic::assume:
72 case llvm::Intrinsic::expect:
73 return true;
74 default:
75 break;
76 }
77 }
78 return false;
79}
80
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000081BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
82 ScalarEvolution &SE, DominatorTree &DT,
Johannes Doerfertecff11d2015-05-22 23:43:58 +000083 ScalarAllocaMapTy &ScalarMap,
84 ScalarAllocaMapTy &PHIOpMap,
85 EscapeUsersAllocaMapTy &EscapeMap,
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000086 IslExprBuilder *ExprBuilder)
Johannes Doerfertecff11d2015-05-22 23:43:58 +000087 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT),
88 EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap),
89 EscapeMap(EscapeMap) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000090
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000091Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
92 ValueMapT &BBMap, ValueMapT &GlobalMap,
93 LoopToScevMapT &LTS, Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000094 // We assume constants never change.
95 // This avoids map lookups for many calls to this function.
96 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000097 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000098
Hongbin Zhengfe11e282013-06-29 13:22:15 +000099 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000100 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +0000101 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000102 New = Builder.CreateTruncOrBitCast(New, Old->getType());
103
104 return New;
105 }
106
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000107 if (Value *New = BBMap.lookup(Old))
108 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000109
Tobias Grosser683b8e42014-11-30 14:33:31 +0000110 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000111 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000112 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000113 const SCEV *NewScev = apply(Scev, LTS, SE);
114 ValueToValueMap VTV;
115 VTV.insert(BBMap.begin(), BBMap.end());
116 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000117 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Johannes Doerferte69e1142015-08-18 11:56:00 +0000118
119 Scop &S = *Stmt.getParent();
120 const DataLayout &DL =
121 S.getRegion().getEntry()->getParent()->getParent()->getDataLayout();
122 auto IP = Builder.GetInsertPoint();
123
124 assert(IP != Builder.GetInsertBlock()->end() &&
Tobias Grosser45e79442015-08-01 09:07:57 +0000125 "Only instructions can be insert points for SCEVExpander");
Johannes Doerferte69e1142015-08-18 11:56:00 +0000126 Value *Expanded =
127 expandCodeFor(S, SE, DL, "polly", NewScev, Old->getType(), IP);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000128
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
Tobias Grosserc186ac72015-08-11 08:13:15 +0000231void BlockGenerator::generateScalarStore(ScopStmt &Stmt, const StoreInst *Store,
232 ValueMapT &BBMap, ValueMapT &GlobalMap,
233 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000234 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000235 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000236 generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS);
237 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
238 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000239
Tobias Grosserc186ac72015-08-11 08:13:15 +0000240 Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000241}
242
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000243void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
244 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000245 LoopToScevMapT &LTS) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000246
247 // First check for possible scalar dependences for this instruction.
248 generateScalarLoads(Stmt, Inst, BBMap);
249
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000250 // Terminator instructions control the control flow. They are explicitly
251 // expressed in the clast and do not need to be copied.
252 if (Inst->isTerminator())
253 return;
254
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000255 Loop *L = getLoopForInst(Inst);
256 if ((Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) &&
257 canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) {
258 Value *NewValue = getNewValue(Stmt, Inst, BBMap, GlobalMap, LTS, L);
259 BBMap[Inst] = NewValue;
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000260 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000261 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000262
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000263 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000264 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000265 // Compute NewLoad before its insertion in BBMap to make the insertion
266 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000267 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000268 return;
269 }
270
271 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosserc186ac72015-08-11 08:13:15 +0000272 generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000273 return;
274 }
275
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000276 if (const PHINode *PHI = dyn_cast<PHINode>(Inst)) {
277 copyPHIInstruction(Stmt, PHI, BBMap, GlobalMap, LTS);
278 return;
279 }
280
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000281 // Skip some special intrinsics for which we do not adjust the semantics to
282 // the new schedule. All others are handled like every other instruction.
283 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
284 switch (IT->getIntrinsicID()) {
285 // Lifetime markers are ignored.
286 case llvm::Intrinsic::lifetime_start:
287 case llvm::Intrinsic::lifetime_end:
288 // Invariant markers are ignored.
289 case llvm::Intrinsic::invariant_start:
290 case llvm::Intrinsic::invariant_end:
291 // Some misc annotations are ignored.
292 case llvm::Intrinsic::var_annotation:
293 case llvm::Intrinsic::ptr_annotation:
294 case llvm::Intrinsic::annotation:
295 case llvm::Intrinsic::donothing:
296 case llvm::Intrinsic::assume:
297 case llvm::Intrinsic::expect:
298 return;
299 default:
300 // Other intrinsics are copied.
301 break;
302 }
303 }
304
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000305 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000306}
307
Johannes Doerfert275a1752015-02-24 16:16:32 +0000308void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
309 LoopToScevMapT &LTS) {
310 assert(Stmt.isBlockStmt() &&
311 "Only block statements can be copied by the block generator");
312
313 ValueMapT BBMap;
314
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000315 BasicBlock *BB = Stmt.getBasicBlock();
Johannes Doerfert275a1752015-02-24 16:16:32 +0000316 copyBB(Stmt, BB, BBMap, GlobalMap, LTS);
317}
318
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000319BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000320 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000321 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000322 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000323 return CopyBB;
324}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000325
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000326BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
327 ValueMapT &BBMap, ValueMapT &GlobalMap,
328 LoopToScevMapT &LTS) {
329 BasicBlock *CopyBB = splitBB(BB);
330 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS);
331 return CopyBB;
332}
333
334void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
335 ValueMapT &BBMap, ValueMapT &GlobalMap,
336 LoopToScevMapT &LTS) {
337 Builder.SetInsertPoint(CopyBB->begin());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000338 EntryBB = &CopyBB->getParent()->getEntryBlock();
339
Tobias Grosser91f5b262014-06-04 08:06:40 +0000340 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000341 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000342
343 // After a basic block was copied store all scalars that escape this block
344 // in their alloca. First the scalars that have dependences inside the SCoP,
345 // then the ones that might escape the SCoP.
346 generateScalarStores(Stmt, BB, BBMap, GlobalMap);
347
348 const Region &R = Stmt.getParent()->getRegion();
349 for (Instruction &Inst : *BB)
350 handleOutsideUsers(R, &Inst, BBMap[&Inst]);
351}
352
Tobias Grosser0164b8f2015-08-13 08:07:39 +0000353AllocaInst *BlockGenerator::getOrCreateAlloca(Value *ScalarBase,
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000354 ScalarAllocaMapTy &Map,
355 const char *NameExt,
356 bool *IsNew) {
357
358 // Check if an alloca was cached for the base instruction.
359 AllocaInst *&Addr = Map[ScalarBase];
360
361 // If needed indicate if it was found already or will be created.
362 if (IsNew)
363 *IsNew = (Addr == nullptr);
364
365 // If no alloca was found create one and insert it in the entry block.
366 if (!Addr) {
367 auto *Ty = ScalarBase->getType();
368 Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
369 Addr->insertBefore(EntryBB->getFirstInsertionPt());
370 }
371
372 return Addr;
373}
374
375void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst,
376 Value *InstCopy) {
Johannes Doerferte69e1142015-08-18 11:56:00 +0000377
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000378 EscapeUserVectorTy EscapeUsers;
379 for (User *U : Inst->users()) {
380
381 // Non-instruction user will never escape.
382 Instruction *UI = dyn_cast<Instruction>(U);
383 if (!UI)
384 continue;
385
Johannes Doerfertddb83d02015-08-16 08:35:40 +0000386 if (R.contains(UI))
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000387 continue;
388
389 EscapeUsers.push_back(UI);
390 }
391
392 // Exit if no escape uses were found.
393 if (EscapeUsers.empty())
394 return;
395
396 // If there are escape users we get the alloca for this instruction and put
397 // it in the EscapeMap for later finalization. However, if the alloca was not
398 // created by an already handled scalar dependence we have to initialize it
399 // also. Lastly, if the instruction was copied multiple times we already did
400 // this and can exit.
401 if (EscapeMap.count(Inst))
402 return;
403
404 // Get or create an escape alloca for this instruction.
405 bool IsNew;
406 AllocaInst *ScalarAddr =
407 getOrCreateAlloca(Inst, ScalarMap, ".escape", &IsNew);
408
409 // Remember that this instruction has escape uses and the escape alloca.
410 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
411
412 // If the escape alloca was just created store the instruction in there,
413 // otherwise that happened already.
414 if (IsNew) {
415 assert(InstCopy && "Except PHIs every instruction should have a copy!");
416 Builder.CreateStore(InstCopy, ScalarAddr);
417 }
418}
419
420void BlockGenerator::generateScalarLoads(ScopStmt &Stmt,
421 const Instruction *Inst,
422 ValueMapT &BBMap) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000423 auto *MAL = Stmt.lookupAccessesFor(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000424
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000425 if (!MAL)
426 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000427
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000428 for (MemoryAccess &MA : *MAL) {
429 AllocaInst *Address;
430 if (!MA.isScalar() || !MA.isRead())
431 continue;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000432
Tobias Grosser0164b8f2015-08-13 08:07:39 +0000433 auto Base = MA.getBaseAddr();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000434
Tobias Grosser92245222015-07-28 14:53:44 +0000435 if (MA.getScopArrayInfo()->isPHI())
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000436 Address = getOrCreateAlloca(Base, PHIOpMap, ".phiops");
437 else
438 Address = getOrCreateAlloca(Base, ScalarMap, ".s2a");
439
440 BBMap[Base] = Builder.CreateLoad(Address, Address->getName() + ".reload");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000441 }
442}
443
444Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R,
445 ScalarAllocaMapTy &ReloadMap,
446 ValueMapT &BBMap,
447 ValueMapT &GlobalMap) {
448 // If the value we want to store is an instruction we might have demoted it
449 // in order to make it accessible here. In such a case a reload is
450 // necessary. If it is no instruction it will always be a value that
451 // dominates the current point and we can just use it. In total there are 4
452 // options:
453 // (1) The value is no instruction ==> use the value.
454 // (2) The value is an instruction that was split out of the region prior to
455 // code generation ==> use the instruction as it dominates the region.
456 // (3) The value is an instruction:
457 // (a) The value was defined in the current block, thus a copy is in
458 // the BBMap ==> use the mapped value.
459 // (b) The value was defined in a previous block, thus we demoted it
460 // earlier ==> use the reloaded value.
461 Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue);
462 if (!ScalarValueInst)
463 return ScalarValue;
464
465 if (!R.contains(ScalarValueInst)) {
466 if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst))
467 return /* Case (3a) */ ScalarValueCopy;
468 else
469 return /* Case 2 */ ScalarValue;
470 }
471
472 if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst))
473 return /* Case (3a) */ ScalarValueCopy;
474
475 // Case (3b)
Johannes Doerferte1fa6da2015-08-17 09:38:46 +0000476 Value *ReloadAddr = getOrCreateAlloca(ScalarValueInst, ReloadMap, ".s2a");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000477 ScalarValue =
478 Builder.CreateLoad(ReloadAddr, ReloadAddr->getName() + ".reload");
479
480 return ScalarValue;
481}
482
483void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
484 ValueMapT &BBMap,
485 ValueMapT &GlobalMap) {
486 const Region &R = Stmt.getParent()->getRegion();
487
488 assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() &&
489 "Region statements need to use the generateScalarStores() "
490 "function in the RegionGenerator");
491
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000492 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000493 if (!MA->isScalar() || MA->isRead())
494 continue;
495
Tobias Grosser92245222015-07-28 14:53:44 +0000496 Instruction *Base = cast<Instruction>(MA->getBaseAddr());
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000497 Value *Val = MA->getAccessValue();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000498
Tobias Grosser92245222015-07-28 14:53:44 +0000499 AllocaInst *Address = nullptr;
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000500 if (MA->getScopArrayInfo()->isPHI())
Tobias Grosser92245222015-07-28 14:53:44 +0000501 Address = getOrCreateAlloca(Base, PHIOpMap, ".phiops");
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000502 else
Tobias Grosser92245222015-07-28 14:53:44 +0000503 Address = getOrCreateAlloca(Base, ScalarMap, ".s2a");
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000504
Tobias Grosser92245222015-07-28 14:53:44 +0000505 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
506 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000507 }
508}
509
510void BlockGenerator::createScalarInitialization(Region &R,
511 ValueMapT &GlobalMap) {
512 // The split block __just before__ the region and optimized region.
513 BasicBlock *SplitBB = R.getEnteringBlock();
514 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
515 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
516
517 // Get the start block of the __optimized__ region.
518 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
519 if (StartBB == R.getEntry())
520 StartBB = SplitBBTerm->getSuccessor(1);
521
522 // For each PHI predecessor outside the region store the incoming operand
523 // value prior to entering the optimized region.
524 Builder.SetInsertPoint(StartBB->getTerminator());
525
526 ScalarAllocaMapTy EmptyMap;
527 for (const auto &PHIOpMapping : PHIOpMap) {
528 const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
529
530 // Check if this PHI has the split block as predecessor (that is the only
531 // possible predecessor outside the SCoP).
532 int idx = PHI->getBasicBlockIndex(SplitBB);
533 if (idx < 0)
534 continue;
535
536 Value *ScalarValue = PHI->getIncomingValue(idx);
537 ScalarValue =
538 getNewScalarValue(ScalarValue, R, EmptyMap, GlobalMap, GlobalMap);
539
540 // If the split block is the predecessor initialize the PHI operator alloca.
541 Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
542 }
543}
544
545void BlockGenerator::createScalarFinalization(Region &R) {
546 // The exit block of the __unoptimized__ region.
547 BasicBlock *ExitBB = R.getExitingBlock();
548 // The merge block __just after__ the region and the optimized region.
549 BasicBlock *MergeBB = R.getExit();
550
551 // The exit block of the __optimized__ region.
552 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
553 if (OptExitBB == ExitBB)
554 OptExitBB = *(++pred_begin(MergeBB));
555
556 Builder.SetInsertPoint(OptExitBB->getTerminator());
557 for (const auto &EscapeMapping : EscapeMap) {
558 // Extract the escaping instruction and the escaping users as well as the
559 // alloca the instruction was demoted to.
560 Instruction *EscapeInst = EscapeMapping.getFirst();
561 const auto &EscapeMappingValue = EscapeMapping.getSecond();
562 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
563 AllocaInst *ScalarAddr = EscapeMappingValue.first;
564
565 // Reload the demoted instruction in the optimized version of the SCoP.
566 Instruction *EscapeInstReload =
567 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
568
569 // Create the merge PHI that merges the optimized and unoptimized version.
570 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
571 EscapeInst->getName() + ".merge");
572 MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
573
574 // Add the respective values to the merge PHI.
575 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
576 MergePHI->addIncoming(EscapeInst, ExitBB);
577
578 // The information of scalar evolution about the escaping instruction needs
579 // to be revoked so the new merged instruction will be used.
580 if (SE.isSCEVable(EscapeInst->getType()))
581 SE.forgetValue(EscapeInst);
582
583 // Replace all uses of the demoted instruction with the merge PHI.
584 for (Instruction *EUser : EscapeUsers)
585 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
586 }
587}
588
589void BlockGenerator::finalizeSCoP(Scop &S, ValueMapT &GlobalMap) {
590 createScalarInitialization(S.getRegion(), GlobalMap);
591 createScalarFinalization(S.getRegion());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000592}
593
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000594VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
595 VectorValueMapT &GlobalMaps,
596 std::vector<LoopToScevMapT> &VLTS,
597 isl_map *Schedule)
598 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
599 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000600 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
601 assert(Schedule && "No statement domain provided");
602}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000603
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000604Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000605 ValueMapT &VectorMap,
606 VectorValueMapT &ScalarMaps,
607 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000608 if (Value *NewValue = VectorMap.lookup(Old))
609 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000610
611 int Width = getVectorWidth();
612
613 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
614
615 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000616 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000617 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
618 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000619 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000620
621 VectorMap[Old] = Vector;
622
623 return Vector;
624}
625
626Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
627 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
628 assert(PointerTy && "PointerType expected");
629
630 Type *ScalarType = PointerTy->getElementType();
631 VectorType *VectorType = VectorType::get(ScalarType, Width);
632
633 return PointerType::getUnqual(VectorType);
634}
635
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000636Value *VectorBlockGenerator::generateStrideOneLoad(
637 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
638 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000639 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000640 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000641 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
642 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
643
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000644 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000645 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000646 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000647 Value *VectorPtr =
648 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
649 LoadInst *VecLoad =
650 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000651 if (!Aligned)
652 VecLoad->setAlignment(8);
653
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000654 if (NegativeStride) {
655 SmallVector<Constant *, 16> Indices;
656 for (int i = VectorWidth - 1; i >= 0; i--)
657 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
658 Constant *SV = llvm::ConstantVector::get(Indices);
659 Value *RevVecLoad = Builder.CreateShuffleVector(
660 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
661 return RevVecLoad;
662 }
663
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000664 return VecLoad;
665}
666
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000667Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
668 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000669 ValueMapT &BBMap) {
670 const Value *Pointer = Load->getPointerOperand();
671 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000672 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
673 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000674 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
675 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000676 LoadInst *ScalarLoad =
677 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000678
679 if (!Aligned)
680 ScalarLoad->setAlignment(8);
681
Tobias Grosserc14582f2013-02-05 18:01:29 +0000682 Constant *SplatVector = Constant::getNullValue(
683 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000684
Tobias Grosserc14582f2013-02-05 18:01:29 +0000685 Value *VectorLoad = Builder.CreateShuffleVector(
686 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000687 return VectorLoad;
688}
689
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000690Value *VectorBlockGenerator::generateUnknownStrideLoad(
691 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000692 int VectorWidth = getVectorWidth();
693 const Value *Pointer = Load->getPointerOperand();
694 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000695 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000696
697 Value *Vector = UndefValue::get(VectorType);
698
699 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000700 Value *NewPointer = generateLocationAccessed(
701 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000702 Value *ScalarLoad =
703 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
704 Vector = Builder.CreateInsertElement(
705 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000706 }
707
708 return Vector;
709}
710
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000711void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000712 ValueMapT &VectorMap,
713 VectorValueMapT &ScalarMaps) {
Tobias Grosser28736452015-03-23 07:00:36 +0000714 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000715 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000716 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000717 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000718 return;
719 }
720
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000721 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000722
Tobias Grosser95493982014-04-18 09:46:35 +0000723 // Make sure we have scalar values available to access the pointer to
724 // the data location.
725 extractScalarValues(Load, VectorMap, ScalarMaps);
726
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000727 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000728 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000729 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000730 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000731 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000732 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000733 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000734 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000735 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000736
737 VectorMap[Load] = NewLoad;
738}
739
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000740void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
741 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000742 ValueMapT &VectorMap,
743 VectorValueMapT &ScalarMaps) {
744 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000745 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
746 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000747
748 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
749
750 const CastInst *Cast = dyn_cast<CastInst>(Inst);
751 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
752 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
753}
754
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000755void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
756 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000757 ValueMapT &VectorMap,
758 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000759 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000760 Value *OpZero = Inst->getOperand(0);
761 Value *OpOne = Inst->getOperand(1);
762
763 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000764 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
765 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000766
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000767 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000768 Inst->getName() + "p_vec");
769 VectorMap[Inst] = NewInst;
770}
771
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000772void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000773 ValueMapT &VectorMap,
774 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000775 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000776
777 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000778 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000779 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000780
Tobias Grosser50fd7012014-04-17 23:13:49 +0000781 // Make sure we have scalar values available to access the pointer to
782 // the data location.
783 extractScalarValues(Store, VectorMap, ScalarMaps);
784
Sebastian Popa00a0292012-12-18 07:46:06 +0000785 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000786 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000787 Value *NewPointer = generateLocationAccessed(
788 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000789
Tobias Grosserc14582f2013-02-05 18:01:29 +0000790 Value *VectorPtr =
791 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000792 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
793
794 if (!Aligned)
795 Store->setAlignment(8);
796 } else {
797 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000798 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000799 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000800 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000801 Builder.CreateStore(Scalar, NewPointer);
802 }
803 }
804}
805
806bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
807 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000808 for (Value *Operand : Inst->operands())
809 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000810 return true;
811 return false;
812}
813
814bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
815 ValueMapT &VectorMap,
816 VectorValueMapT &ScalarMaps) {
817 bool HasVectorOperand = false;
818 int VectorWidth = getVectorWidth();
819
Tobias Grosser91f5b262014-06-04 08:06:40 +0000820 for (Value *Operand : Inst->operands()) {
821 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000822
823 if (VecOp == VectorMap.end())
824 continue;
825
826 HasVectorOperand = true;
827 Value *NewVector = VecOp->second;
828
829 for (int i = 0; i < VectorWidth; ++i) {
830 ValueMapT &SM = ScalarMaps[i];
831
832 // If there is one scalar extracted, all scalar elements should have
833 // already been extracted by the code here. So no need to check for the
834 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000835 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000836 break;
837
Tobias Grosser91f5b262014-06-04 08:06:40 +0000838 SM[Operand] =
839 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000840 }
841 }
842
843 return HasVectorOperand;
844}
845
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000846void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
847 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000848 ValueMapT &VectorMap,
849 VectorValueMapT &ScalarMaps) {
850 bool HasVectorOperand;
851 int VectorWidth = getVectorWidth();
852
853 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
854
855 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000856 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000857 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000858
859 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
860 return;
861
862 // Make the result available as vector value.
863 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
864 Value *Vector = UndefValue::get(VectorType);
865
866 for (int i = 0; i < VectorWidth; i++)
867 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
868 Builder.getInt32(i));
869
870 VectorMap[Inst] = Vector;
871}
872
Tobias Grosserc14582f2013-02-05 18:01:29 +0000873int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000874
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000875void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
876 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000877 ValueMapT &VectorMap,
878 VectorValueMapT &ScalarMaps) {
879 // Terminator instructions control the control flow. They are explicitly
880 // expressed in the clast and do not need to be copied.
881 if (Inst->isTerminator())
882 return;
883
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000884 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000885 return;
886
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000887 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000888 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000889 return;
890 }
891
892 if (hasVectorOperands(Inst, VectorMap)) {
893 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000894 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000895 return;
896 }
897
898 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000899 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000900 return;
901 }
902
903 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000904 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000905 return;
906 }
907
908 // Falltrough: We generate scalar instructions, if we don't know how to
909 // generate vector code.
910 }
911
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000912 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000913}
914
Johannes Doerfert275a1752015-02-24 16:16:32 +0000915void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
916 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
917 "the vector block generator");
918
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000919 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000920 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000921 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000922 CopyBB->setName("polly.stmt." + BB->getName());
923 Builder.SetInsertPoint(CopyBB->begin());
924
925 // Create two maps that store the mapping from the original instructions of
926 // the old basic block to their copies in the new basic block. Those maps
927 // are basic block local.
928 //
929 // As vector code generation is supported there is one map for scalar values
930 // and one for vector values.
931 //
932 // In case we just do scalar code generation, the vectorMap is not used and
933 // the scalarMap has just one dimension, which contains the mapping.
934 //
935 // In case vector code generation is done, an instruction may either appear
936 // in the vector map once (as it is calculating >vectorwidth< values at a
937 // time. Or (if the values are calculated using scalar operations), it
938 // appears once in every dimension of the scalarMap.
939 VectorValueMapT ScalarBlockMap(getVectorWidth());
940 ValueMapT VectorBlockMap;
941
Tobias Grosser91f5b262014-06-04 08:06:40 +0000942 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000943 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000944}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000945
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000946BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
947 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000948
949 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
950 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
951
952 if (BBCopyIDom)
953 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
954
955 return BBCopyIDom;
956}
957
Johannes Doerfert275a1752015-02-24 16:16:32 +0000958void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
959 LoopToScevMapT &LTS) {
960 assert(Stmt.isRegionStmt() &&
Tobias Grosserd3f21832015-08-01 06:26:51 +0000961 "Only region statements can be copied by the region generator");
Johannes Doerfert275a1752015-02-24 16:16:32 +0000962
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000963 // Forget all old mappings.
964 BlockMap.clear();
965 RegionMaps.clear();
966 IncompletePHINodeMap.clear();
967
Johannes Doerfert275a1752015-02-24 16:16:32 +0000968 // The region represented by the statement.
969 Region *R = Stmt.getRegion();
970
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000971 // Create a dedicated entry for the region where we can reload all demoted
972 // inputs.
973 BasicBlock *EntryBB = R->getEntry();
974 BasicBlock *EntryBBCopy =
975 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
976 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
977 Builder.SetInsertPoint(EntryBBCopy->begin());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000978
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000979 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
980 if (!R->contains(*PI))
981 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000982
983 // Iterate over all blocks in the region in a breadth-first search.
984 std::deque<BasicBlock *> Blocks;
985 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000986 Blocks.push_back(EntryBB);
987 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000988
989 while (!Blocks.empty()) {
990 BasicBlock *BB = Blocks.front();
991 Blocks.pop_front();
992
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000993 // First split the block and update dominance information.
994 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000995 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
996
997 // In order to remap PHI nodes we store also basic block mappings.
998 BlockMap[BB] = BBCopy;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000999
1000 // Get the mapping for this block and initialize it with the mapping
1001 // available at its immediate dominator (in the new region).
1002 ValueMapT &RegionMap = RegionMaps[BBCopy];
1003 RegionMap = RegionMaps[BBCopyIDom];
1004
Johannes Doerfert275a1752015-02-24 16:16:32 +00001005 // Copy the block with the BlockGenerator.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001006 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001007
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001008 // In order to remap PHI nodes we store also basic block mappings.
1009 BlockMap[BB] = BBCopy;
1010
1011 // Add values to incomplete PHI nodes waiting for this block to be copied.
1012 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
1013 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB,
1014 GlobalMap, LTS);
1015 IncompletePHINodeMap[BB].clear();
1016
Johannes Doerfert275a1752015-02-24 16:16:32 +00001017 // And continue with new successors inside the region.
1018 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
1019 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
1020 Blocks.push_back(*SI);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001021 }
1022
1023 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001024 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +00001025 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001026 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001027 BlockMap[R->getExit()] = ExitBBCopy;
1028
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001029 repairDominance(R->getExit(), ExitBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001030
1031 // As the block generator doesn't handle control flow we need to add the
1032 // region control flow by hand after all blocks have been copied.
1033 for (BasicBlock *BB : SeenBlocks) {
1034
1035 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
1036
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001037 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +00001038 Instruction *BICopy = BBCopy->getTerminator();
1039
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001040 ValueMapT &RegionMap = RegionMaps[BBCopy];
1041 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1042
Tobias Grosser45e79442015-08-01 09:07:57 +00001043 Builder.SetInsertPoint(BICopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001044 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
1045 BICopy->eraseFromParent();
1046 }
1047
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001048 // Add counting PHI nodes to all loops in the region that can be used as
1049 // replacement for SCEVs refering to the old loop.
1050 for (BasicBlock *BB : SeenBlocks) {
1051 Loop *L = LI.getLoopFor(BB);
1052 if (L == nullptr || L->getHeader() != BB)
1053 continue;
1054
1055 BasicBlock *BBCopy = BlockMap[BB];
1056 Value *NullVal = Builder.getInt32(0);
1057 PHINode *LoopPHI =
1058 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1059 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1060 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
1061 LoopPHI->insertBefore(BBCopy->begin());
1062 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1063
1064 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1065 if (!R->contains(PredBB))
1066 continue;
1067 if (L->contains(PredBB))
1068 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1069 else
1070 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1071 }
1072
1073 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1074 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1075 LoopPHI->addIncoming(NullVal, PredBBCopy);
1076
1077 LTS[L] = SE.getUnknown(LoopPHI);
1078 }
1079
1080 // Add all mappings from the region to the global map so outside uses will use
1081 // the copied instructions.
1082 for (auto &BBMap : RegionMaps)
1083 GlobalMap.insert(BBMap.second.begin(), BBMap.second.end());
1084
Johannes Doerfert275a1752015-02-24 16:16:32 +00001085 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001086 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001087}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001088
1089void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
1090 const Instruction *Inst,
1091 ValueMapT &BBMap) {
1092
1093 // Inside a non-affine region PHI nodes are copied not demoted. Once the
1094 // phi is copied it will reload all inputs from outside the region, hence
1095 // we do not need to generate code for the read access of the operands of a
1096 // PHI.
1097 if (isa<PHINode>(Inst))
1098 return;
1099
1100 return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
1101}
1102
1103void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
1104 ValueMapT &BBMap,
1105 ValueMapT &GlobalMap) {
1106 const Region &R = Stmt.getParent()->getRegion();
1107
1108 Region *StmtR = Stmt.getRegion();
1109 assert(StmtR && "Block statements need to use the generateScalarStores() "
1110 "function in the BlockGenerator");
1111
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001112 for (MemoryAccess *MA : Stmt) {
1113
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001114 if (!MA->isScalar() || MA->isRead())
1115 continue;
1116
1117 Instruction *ScalarBase = cast<Instruction>(MA->getBaseAddr());
1118 Instruction *ScalarInst = MA->getAccessInstruction();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001119
Tobias Grosser62139132015-08-02 16:17:41 +00001120 // Only generate accesses that belong to this basic block.
1121 if (ScalarInst->getParent() != BB)
1122 continue;
1123
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001124 Value *Val = MA->getAccessValue();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001125 AllocaInst *ScalarAddr = nullptr;
1126
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001127 if (MA->getScopArrayInfo()->isPHI())
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001128 ScalarAddr = getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001129 else
Tobias Grosser92245222015-07-28 14:53:44 +00001130 ScalarAddr = getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001131
Tobias Grosser92245222015-07-28 14:53:44 +00001132 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
1133 Builder.CreateStore(Val, ScalarAddr);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001134 }
1135}
1136
1137void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1138 PHINode *PHICopy, BasicBlock *IncomingBB,
1139 ValueMapT &GlobalMap,
1140 LoopToScevMapT &LTS) {
1141 Region *StmtR = Stmt.getRegion();
1142
1143 // If the incoming block was not yet copied mark this PHI as incomplete.
1144 // Once the block will be copied the incoming value will be added.
1145 BasicBlock *BBCopy = BlockMap[IncomingBB];
1146 if (!BBCopy) {
1147 assert(StmtR->contains(IncomingBB) &&
1148 "Bad incoming block for PHI in non-affine region");
1149 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1150 return;
1151 }
1152
1153 Value *OpCopy = nullptr;
1154 if (StmtR->contains(IncomingBB)) {
1155 assert(RegionMaps.count(BBCopy) &&
1156 "Incoming PHI block did not have a BBMap");
1157 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1158
1159 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
1160 OpCopy =
1161 getNewValue(Stmt, Op, BBCopyMap, GlobalMap, LTS, getLoopForInst(PHI));
1162 } else {
1163
1164 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1165 return;
1166
1167 AllocaInst *PHIOpAddr =
1168 getOrCreateAlloca(const_cast<PHINode *>(PHI), PHIOpMap, ".phiops");
1169 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1170 BlockMap[IncomingBB]->getTerminator());
1171 }
1172
1173 assert(OpCopy && "Incoming PHI value was not copied properly");
1174 assert(BBCopy && "Incoming PHI block was not copied properly");
1175 PHICopy->addIncoming(OpCopy, BBCopy);
1176}
1177
1178Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
1179 ValueMapT &BBMap,
1180 ValueMapT &GlobalMap,
1181 LoopToScevMapT &LTS) {
1182 unsigned NumIncoming = PHI->getNumIncomingValues();
1183 PHINode *PHICopy =
1184 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1185 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1186 BBMap[PHI] = PHICopy;
1187
1188 for (unsigned u = 0; u < NumIncoming; u++)
1189 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), GlobalMap,
1190 LTS);
1191 return PHICopy;
1192}