blob: 0c9348d2cadeead9a2b06a51b024e3e18651cfec [file] [log] [blame]
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001//===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BlockGenerator and VectorBlockGenerator classes,
11// which generate sequential code and vectorized code for a polyhedral
12// statement, respectively.
13//
14//===----------------------------------------------------------------------===//
15
16#include "polly/ScopInfo.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000017#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000018#include "polly/CodeGen/CodeGeneration.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000019#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000020#include "polly/Options.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000021#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000022#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000023#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000024#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000025#include "llvm/Analysis/RegionInfo.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000026#include "llvm/Analysis/ScalarEvolution.h"
27#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser030237d2014-02-21 15:06:05 +000028#include "llvm/IR/IntrinsicInst.h"
Tobias Grosserc9895062015-03-10 15:24:33 +000029#include "llvm/IR/Module.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000030#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000031#include "isl/aff.h"
32#include "isl/ast.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000033#include "isl/ast_build.h"
Tobias Grosserba0d0922015-05-09 09:13:42 +000034#include "isl/set.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000035#include <deque>
36
Hongbin Zheng3b11a162012-04-25 13:16:49 +000037using namespace llvm;
38using namespace polly;
39
Tobias Grosser878aba42014-10-22 23:22:41 +000040static cl::opt<bool> Aligned("enable-polly-aligned",
41 cl::desc("Assumed aligned memory accesses."),
42 cl::Hidden, cl::init(false), cl::ZeroOrMore,
43 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000044
Tobias Grosserdcc3b432015-08-04 13:54:20 +000045bool polly::canSynthesize(const Value *V, const llvm::LoopInfo *LI,
Tobias Grosserecfe21b2013-03-20 18:03:18 +000046 ScalarEvolution *SE, const Region *R) {
Tobias Grosserdcc3b432015-08-04 13:54:20 +000047 if (!V || !SE->isSCEVable(V->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000048 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000049
Tobias Grosserdcc3b432015-08-04 13:54:20 +000050 if (const SCEV *Scev = SE->getSCEV(const_cast<Value *>(V)))
Tobias Grosser683b8e42014-11-30 14:33:31 +000051 if (!isa<SCEVCouldNotCompute>(Scev))
52 if (!hasScalarDepsInsideRegion(Scev, R))
53 return true;
54
55 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000056}
57
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000058bool polly::isIgnoredIntrinsic(const Value *V) {
59 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
60 switch (IT->getIntrinsicID()) {
61 // Lifetime markers are supported/ignored.
62 case llvm::Intrinsic::lifetime_start:
63 case llvm::Intrinsic::lifetime_end:
64 // Invariant markers are supported/ignored.
65 case llvm::Intrinsic::invariant_start:
66 case llvm::Intrinsic::invariant_end:
67 // Some misc annotations are supported/ignored.
68 case llvm::Intrinsic::var_annotation:
69 case llvm::Intrinsic::ptr_annotation:
70 case llvm::Intrinsic::annotation:
71 case llvm::Intrinsic::donothing:
72 case llvm::Intrinsic::assume:
73 case llvm::Intrinsic::expect:
74 return true;
75 default:
76 break;
77 }
78 }
79 return false;
80}
81
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000082BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
83 ScalarEvolution &SE, DominatorTree &DT,
Johannes Doerfertecff11d2015-05-22 23:43:58 +000084 ScalarAllocaMapTy &ScalarMap,
85 ScalarAllocaMapTy &PHIOpMap,
86 EscapeUsersAllocaMapTy &EscapeMap,
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000087 IslExprBuilder *ExprBuilder)
Johannes Doerfertecff11d2015-05-22 23:43:58 +000088 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT),
89 EntryBB(nullptr), PHIOpMap(PHIOpMap), ScalarMap(ScalarMap),
90 EscapeMap(EscapeMap) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000091
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000092Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
93 ValueMapT &BBMap, ValueMapT &GlobalMap,
94 LoopToScevMapT &LTS, Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000095 // We assume constants never change.
96 // This avoids map lookups for many calls to this function.
97 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000098 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000099
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000100 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000101 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +0000102 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000103 New = Builder.CreateTruncOrBitCast(New, Old->getType());
104
105 return New;
106 }
107
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000108 if (Value *New = BBMap.lookup(Old))
109 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000110
Tobias Grosser683b8e42014-11-30 14:33:31 +0000111 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000112 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000113 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000114 const SCEV *NewScev = apply(Scev, LTS, SE);
115 ValueToValueMap VTV;
116 VTV.insert(BBMap.begin(), BBMap.end());
117 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000118 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grosserc9895062015-03-10 15:24:33 +0000119 SCEVExpander Expander(SE, Stmt.getParent()
120 ->getRegion()
121 .getEntry()
122 ->getParent()
123 ->getParent()
124 ->getDataLayout(),
125 "polly");
Tobias Grosser45e79442015-08-01 09:07:57 +0000126 assert(Builder.GetInsertPoint() != Builder.GetInsertBlock()->end() &&
127 "Only instructions can be insert points for SCEVExpander");
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000128 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
129 Builder.GetInsertPoint());
130
131 BBMap[Old] = Expanded;
132 return Expanded;
133 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000134 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000135
Tobias Grosser16371ac2014-11-05 20:48:56 +0000136 // A scop-constant value defined by a global or a function parameter.
137 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
138 return const_cast<Value *>(Old);
139
140 // A scop-constant value defined by an instruction executed outside the scop.
141 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000142 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000143 return const_cast<Value *>(Old);
144
145 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000146 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000147 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000148}
149
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000150void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
151 ValueMapT &BBMap, ValueMapT &GlobalMap,
152 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000153 // We do not generate debug intrinsics as we did not investigate how to
154 // copy them correctly. At the current state, they just crash the code
155 // generation as the meta-data operands are not correctly copied.
156 if (isa<DbgInfoIntrinsic>(Inst))
157 return;
158
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000159 Instruction *NewInst = Inst->clone();
160
161 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000162 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000163 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
164 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000165
166 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000167 assert(!isa<StoreInst>(NewInst) &&
168 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000169 delete NewInst;
170 return;
171 }
172
173 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
174 }
175
176 Builder.Insert(NewInst);
177 BBMap[Inst] = NewInst;
178
179 if (!NewInst->getType()->isVoidTy())
180 NewInst->setName("p_" + Inst->getName());
181}
182
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000183Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt,
184 const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000185 isl_pw_multi_aff *PWAccRel;
186 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000187 isl_ast_expr *Expr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000188 isl_ast_build *Build = Stmt.getAstBuild();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000189
Johannes Doerferta63b2572014-08-03 01:51:59 +0000190 assert(ExprBuilder && Build &&
191 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000192
Johannes Doerferta99130f2014-10-13 12:58:03 +0000193 Schedule = isl_ast_build_get_schedule(Build);
194 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000195
Johannes Doerferta63b2572014-08-03 01:51:59 +0000196 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000197 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000198
199 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000200}
201
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000202Value *BlockGenerator::generateLocationAccessed(
203 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
204 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
205 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000206
207 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000208 if (MA.hasNewAccessRelation())
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000209 NewPointer = getNewAccessOperand(Stmt, MA);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000210 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000211 NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000212 getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000213
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000214 return NewPointer;
215}
216
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000217Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000218 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000219}
220
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000221Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000222 ValueMapT &BBMap,
223 ValueMapT &GlobalMap,
224 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000225 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000226 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000227 generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000228 Value *ScalarLoad = Builder.CreateAlignedLoad(
229 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000230 return ScalarLoad;
231}
232
Tobias Grosserc186ac72015-08-11 08:13:15 +0000233void BlockGenerator::generateScalarStore(ScopStmt &Stmt, const StoreInst *Store,
234 ValueMapT &BBMap, ValueMapT &GlobalMap,
235 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000236 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000237 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000238 generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS);
239 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
240 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000241
Tobias Grosserc186ac72015-08-11 08:13:15 +0000242 Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000243}
244
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000245void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
246 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000247 LoopToScevMapT &LTS) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000248
249 // First check for possible scalar dependences for this instruction.
250 generateScalarLoads(Stmt, Inst, BBMap);
251
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000252 // Terminator instructions control the control flow. They are explicitly
253 // expressed in the clast and do not need to be copied.
254 if (Inst->isTerminator())
255 return;
256
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000257 Loop *L = getLoopForInst(Inst);
258 if ((Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) &&
259 canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) {
260 Value *NewValue = getNewValue(Stmt, Inst, BBMap, GlobalMap, LTS, L);
261 BBMap[Inst] = NewValue;
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000262 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000263 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000264
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000265 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000266 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000267 // Compute NewLoad before its insertion in BBMap to make the insertion
268 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000269 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000270 return;
271 }
272
273 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosserc186ac72015-08-11 08:13:15 +0000274 generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000275 return;
276 }
277
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000278 if (const PHINode *PHI = dyn_cast<PHINode>(Inst)) {
279 copyPHIInstruction(Stmt, PHI, BBMap, GlobalMap, LTS);
280 return;
281 }
282
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000283 // Skip some special intrinsics for which we do not adjust the semantics to
284 // the new schedule. All others are handled like every other instruction.
285 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
286 switch (IT->getIntrinsicID()) {
287 // Lifetime markers are ignored.
288 case llvm::Intrinsic::lifetime_start:
289 case llvm::Intrinsic::lifetime_end:
290 // Invariant markers are ignored.
291 case llvm::Intrinsic::invariant_start:
292 case llvm::Intrinsic::invariant_end:
293 // Some misc annotations are ignored.
294 case llvm::Intrinsic::var_annotation:
295 case llvm::Intrinsic::ptr_annotation:
296 case llvm::Intrinsic::annotation:
297 case llvm::Intrinsic::donothing:
298 case llvm::Intrinsic::assume:
299 case llvm::Intrinsic::expect:
300 return;
301 default:
302 // Other intrinsics are copied.
303 break;
304 }
305 }
306
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000307 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000308}
309
Johannes Doerfert275a1752015-02-24 16:16:32 +0000310void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
311 LoopToScevMapT &LTS) {
312 assert(Stmt.isBlockStmt() &&
313 "Only block statements can be copied by the block generator");
314
315 ValueMapT BBMap;
316
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000317 BasicBlock *BB = Stmt.getBasicBlock();
Johannes Doerfert275a1752015-02-24 16:16:32 +0000318 copyBB(Stmt, BB, BBMap, GlobalMap, LTS);
319}
320
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000321BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000322 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000323 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000324 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000325 return CopyBB;
326}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000327
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000328BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
329 ValueMapT &BBMap, ValueMapT &GlobalMap,
330 LoopToScevMapT &LTS) {
331 BasicBlock *CopyBB = splitBB(BB);
332 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS);
333 return CopyBB;
334}
335
336void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
337 ValueMapT &BBMap, ValueMapT &GlobalMap,
338 LoopToScevMapT &LTS) {
339 Builder.SetInsertPoint(CopyBB->begin());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000340 EntryBB = &CopyBB->getParent()->getEntryBlock();
341
Tobias Grosser91f5b262014-06-04 08:06:40 +0000342 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000343 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000344
345 // After a basic block was copied store all scalars that escape this block
346 // in their alloca. First the scalars that have dependences inside the SCoP,
347 // then the ones that might escape the SCoP.
348 generateScalarStores(Stmt, BB, BBMap, GlobalMap);
349
350 const Region &R = Stmt.getParent()->getRegion();
351 for (Instruction &Inst : *BB)
352 handleOutsideUsers(R, &Inst, BBMap[&Inst]);
353}
354
Tobias Grosser0164b8f2015-08-13 08:07:39 +0000355AllocaInst *BlockGenerator::getOrCreateAlloca(Value *ScalarBase,
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000356 ScalarAllocaMapTy &Map,
357 const char *NameExt,
358 bool *IsNew) {
359
360 // Check if an alloca was cached for the base instruction.
361 AllocaInst *&Addr = Map[ScalarBase];
362
363 // If needed indicate if it was found already or will be created.
364 if (IsNew)
365 *IsNew = (Addr == nullptr);
366
367 // If no alloca was found create one and insert it in the entry block.
368 if (!Addr) {
369 auto *Ty = ScalarBase->getType();
370 Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
371 Addr->insertBefore(EntryBB->getFirstInsertionPt());
372 }
373
374 return Addr;
375}
376
377void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst,
378 Value *InstCopy) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000379 EscapeUserVectorTy EscapeUsers;
380 for (User *U : Inst->users()) {
381
382 // Non-instruction user will never escape.
383 Instruction *UI = dyn_cast<Instruction>(U);
384 if (!UI)
385 continue;
386
Johannes Doerfertddb83d02015-08-16 08:35:40 +0000387 if (R.contains(UI))
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000388 continue;
389
390 EscapeUsers.push_back(UI);
391 }
392
393 // Exit if no escape uses were found.
394 if (EscapeUsers.empty())
395 return;
396
397 // If there are escape users we get the alloca for this instruction and put
398 // it in the EscapeMap for later finalization. However, if the alloca was not
399 // created by an already handled scalar dependence we have to initialize it
400 // also. Lastly, if the instruction was copied multiple times we already did
401 // this and can exit.
402 if (EscapeMap.count(Inst))
403 return;
404
405 // Get or create an escape alloca for this instruction.
406 bool IsNew;
407 AllocaInst *ScalarAddr =
408 getOrCreateAlloca(Inst, ScalarMap, ".escape", &IsNew);
409
410 // Remember that this instruction has escape uses and the escape alloca.
411 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
412
413 // If the escape alloca was just created store the instruction in there,
414 // otherwise that happened already.
415 if (IsNew) {
416 assert(InstCopy && "Except PHIs every instruction should have a copy!");
417 Builder.CreateStore(InstCopy, ScalarAddr);
418 }
419}
420
421void BlockGenerator::generateScalarLoads(ScopStmt &Stmt,
422 const Instruction *Inst,
423 ValueMapT &BBMap) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000424 auto *MAL = Stmt.lookupAccessesFor(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000425
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000426 if (!MAL)
427 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000428
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000429 for (MemoryAccess &MA : *MAL) {
430 AllocaInst *Address;
431 if (!MA.isScalar() || !MA.isRead())
432 continue;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000433
Tobias Grosser0164b8f2015-08-13 08:07:39 +0000434 auto Base = MA.getBaseAddr();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000435
Tobias Grosser92245222015-07-28 14:53:44 +0000436 if (MA.getScopArrayInfo()->isPHI())
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000437 Address = getOrCreateAlloca(Base, PHIOpMap, ".phiops");
438 else
439 Address = getOrCreateAlloca(Base, ScalarMap, ".s2a");
440
441 BBMap[Base] = Builder.CreateLoad(Address, Address->getName() + ".reload");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000442 }
443}
444
445Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R,
446 ScalarAllocaMapTy &ReloadMap,
447 ValueMapT &BBMap,
448 ValueMapT &GlobalMap) {
449 // If the value we want to store is an instruction we might have demoted it
450 // in order to make it accessible here. In such a case a reload is
451 // necessary. If it is no instruction it will always be a value that
452 // dominates the current point and we can just use it. In total there are 4
453 // options:
454 // (1) The value is no instruction ==> use the value.
455 // (2) The value is an instruction that was split out of the region prior to
456 // code generation ==> use the instruction as it dominates the region.
457 // (3) The value is an instruction:
458 // (a) The value was defined in the current block, thus a copy is in
459 // the BBMap ==> use the mapped value.
460 // (b) The value was defined in a previous block, thus we demoted it
461 // earlier ==> use the reloaded value.
462 Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue);
463 if (!ScalarValueInst)
464 return ScalarValue;
465
466 if (!R.contains(ScalarValueInst)) {
467 if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst))
468 return /* Case (3a) */ ScalarValueCopy;
469 else
470 return /* Case 2 */ ScalarValue;
471 }
472
473 if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst))
474 return /* Case (3a) */ ScalarValueCopy;
475
476 // Case (3b)
Johannes Doerferte1fa6da2015-08-17 09:38:46 +0000477 Value *ReloadAddr = getOrCreateAlloca(ScalarValueInst, ReloadMap, ".s2a");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000478 ScalarValue =
479 Builder.CreateLoad(ReloadAddr, ReloadAddr->getName() + ".reload");
480
481 return ScalarValue;
482}
483
484void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
485 ValueMapT &BBMap,
486 ValueMapT &GlobalMap) {
487 const Region &R = Stmt.getParent()->getRegion();
488
489 assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() &&
490 "Region statements need to use the generateScalarStores() "
491 "function in the RegionGenerator");
492
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000493 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000494 if (!MA->isScalar() || MA->isRead())
495 continue;
496
Tobias Grosser92245222015-07-28 14:53:44 +0000497 Instruction *Base = cast<Instruction>(MA->getBaseAddr());
498 Instruction *Inst = MA->getAccessInstruction();
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000499
Tobias Grosser92245222015-07-28 14:53:44 +0000500 Value *Val = nullptr;
501 AllocaInst *Address = nullptr;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000502
Tobias Grosser92245222015-07-28 14:53:44 +0000503 if (MA->getScopArrayInfo()->isPHI()) {
504 PHINode *BasePHI = dyn_cast<PHINode>(Base);
505 int PHIIdx = BasePHI->getBasicBlockIndex(BB);
Michael Kruse9bb8ef032015-08-08 18:10:54 +0000506 assert(PHIIdx >= 0);
Tobias Grosser92245222015-07-28 14:53:44 +0000507 Address = getOrCreateAlloca(Base, PHIOpMap, ".phiops");
508 Val = BasePHI->getIncomingValue(PHIIdx);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000509 } else {
Tobias Grosser92245222015-07-28 14:53:44 +0000510 Address = getOrCreateAlloca(Base, ScalarMap, ".s2a");
511 Val = Inst;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000512 }
Tobias Grosser92245222015-07-28 14:53:44 +0000513 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
514 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000515 }
516}
517
518void BlockGenerator::createScalarInitialization(Region &R,
519 ValueMapT &GlobalMap) {
520 // The split block __just before__ the region and optimized region.
521 BasicBlock *SplitBB = R.getEnteringBlock();
522 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
523 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
524
525 // Get the start block of the __optimized__ region.
526 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
527 if (StartBB == R.getEntry())
528 StartBB = SplitBBTerm->getSuccessor(1);
529
530 // For each PHI predecessor outside the region store the incoming operand
531 // value prior to entering the optimized region.
532 Builder.SetInsertPoint(StartBB->getTerminator());
533
534 ScalarAllocaMapTy EmptyMap;
535 for (const auto &PHIOpMapping : PHIOpMap) {
536 const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
537
538 // Check if this PHI has the split block as predecessor (that is the only
539 // possible predecessor outside the SCoP).
540 int idx = PHI->getBasicBlockIndex(SplitBB);
541 if (idx < 0)
542 continue;
543
544 Value *ScalarValue = PHI->getIncomingValue(idx);
545 ScalarValue =
546 getNewScalarValue(ScalarValue, R, EmptyMap, GlobalMap, GlobalMap);
547
548 // If the split block is the predecessor initialize the PHI operator alloca.
549 Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
550 }
551}
552
553void BlockGenerator::createScalarFinalization(Region &R) {
554 // The exit block of the __unoptimized__ region.
555 BasicBlock *ExitBB = R.getExitingBlock();
556 // The merge block __just after__ the region and the optimized region.
557 BasicBlock *MergeBB = R.getExit();
558
559 // The exit block of the __optimized__ region.
560 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
561 if (OptExitBB == ExitBB)
562 OptExitBB = *(++pred_begin(MergeBB));
563
564 Builder.SetInsertPoint(OptExitBB->getTerminator());
565 for (const auto &EscapeMapping : EscapeMap) {
566 // Extract the escaping instruction and the escaping users as well as the
567 // alloca the instruction was demoted to.
568 Instruction *EscapeInst = EscapeMapping.getFirst();
569 const auto &EscapeMappingValue = EscapeMapping.getSecond();
570 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
571 AllocaInst *ScalarAddr = EscapeMappingValue.first;
572
573 // Reload the demoted instruction in the optimized version of the SCoP.
574 Instruction *EscapeInstReload =
575 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
576
577 // Create the merge PHI that merges the optimized and unoptimized version.
578 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
579 EscapeInst->getName() + ".merge");
580 MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
581
582 // Add the respective values to the merge PHI.
583 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
584 MergePHI->addIncoming(EscapeInst, ExitBB);
585
586 // The information of scalar evolution about the escaping instruction needs
587 // to be revoked so the new merged instruction will be used.
588 if (SE.isSCEVable(EscapeInst->getType()))
589 SE.forgetValue(EscapeInst);
590
591 // Replace all uses of the demoted instruction with the merge PHI.
592 for (Instruction *EUser : EscapeUsers)
593 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
594 }
595}
596
597void BlockGenerator::finalizeSCoP(Scop &S, ValueMapT &GlobalMap) {
598 createScalarInitialization(S.getRegion(), GlobalMap);
599 createScalarFinalization(S.getRegion());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000600}
601
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000602VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
603 VectorValueMapT &GlobalMaps,
604 std::vector<LoopToScevMapT> &VLTS,
605 isl_map *Schedule)
606 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
607 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000608 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
609 assert(Schedule && "No statement domain provided");
610}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000611
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000612Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000613 ValueMapT &VectorMap,
614 VectorValueMapT &ScalarMaps,
615 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000616 if (Value *NewValue = VectorMap.lookup(Old))
617 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000618
619 int Width = getVectorWidth();
620
621 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
622
623 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000624 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000625 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
626 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000627 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000628
629 VectorMap[Old] = Vector;
630
631 return Vector;
632}
633
634Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
635 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
636 assert(PointerTy && "PointerType expected");
637
638 Type *ScalarType = PointerTy->getElementType();
639 VectorType *VectorType = VectorType::get(ScalarType, Width);
640
641 return PointerType::getUnqual(VectorType);
642}
643
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000644Value *VectorBlockGenerator::generateStrideOneLoad(
645 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
646 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000647 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000648 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000649 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
650 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
651
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000652 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000653 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000654 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000655 Value *VectorPtr =
656 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
657 LoadInst *VecLoad =
658 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000659 if (!Aligned)
660 VecLoad->setAlignment(8);
661
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000662 if (NegativeStride) {
663 SmallVector<Constant *, 16> Indices;
664 for (int i = VectorWidth - 1; i >= 0; i--)
665 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
666 Constant *SV = llvm::ConstantVector::get(Indices);
667 Value *RevVecLoad = Builder.CreateShuffleVector(
668 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
669 return RevVecLoad;
670 }
671
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000672 return VecLoad;
673}
674
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000675Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
676 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000677 ValueMapT &BBMap) {
678 const Value *Pointer = Load->getPointerOperand();
679 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000680 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
681 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000682 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
683 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000684 LoadInst *ScalarLoad =
685 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000686
687 if (!Aligned)
688 ScalarLoad->setAlignment(8);
689
Tobias Grosserc14582f2013-02-05 18:01:29 +0000690 Constant *SplatVector = Constant::getNullValue(
691 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000692
Tobias Grosserc14582f2013-02-05 18:01:29 +0000693 Value *VectorLoad = Builder.CreateShuffleVector(
694 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000695 return VectorLoad;
696}
697
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000698Value *VectorBlockGenerator::generateUnknownStrideLoad(
699 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000700 int VectorWidth = getVectorWidth();
701 const Value *Pointer = Load->getPointerOperand();
702 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000703 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000704
705 Value *Vector = UndefValue::get(VectorType);
706
707 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000708 Value *NewPointer = generateLocationAccessed(
709 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000710 Value *ScalarLoad =
711 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
712 Vector = Builder.CreateInsertElement(
713 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000714 }
715
716 return Vector;
717}
718
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000719void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000720 ValueMapT &VectorMap,
721 VectorValueMapT &ScalarMaps) {
Tobias Grosser28736452015-03-23 07:00:36 +0000722 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000723 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000724 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000725 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000726 return;
727 }
728
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000729 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000730
Tobias Grosser95493982014-04-18 09:46:35 +0000731 // Make sure we have scalar values available to access the pointer to
732 // the data location.
733 extractScalarValues(Load, VectorMap, ScalarMaps);
734
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000735 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000736 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000737 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000738 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000739 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000740 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000741 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000742 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000743 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000744
745 VectorMap[Load] = NewLoad;
746}
747
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000748void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
749 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000750 ValueMapT &VectorMap,
751 VectorValueMapT &ScalarMaps) {
752 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000753 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
754 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000755
756 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
757
758 const CastInst *Cast = dyn_cast<CastInst>(Inst);
759 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
760 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
761}
762
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000763void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
764 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000765 ValueMapT &VectorMap,
766 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000767 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000768 Value *OpZero = Inst->getOperand(0);
769 Value *OpOne = Inst->getOperand(1);
770
771 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000772 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
773 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000774
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000775 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000776 Inst->getName() + "p_vec");
777 VectorMap[Inst] = NewInst;
778}
779
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000780void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000781 ValueMapT &VectorMap,
782 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000783 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000784
785 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000786 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000787 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000788
Tobias Grosser50fd7012014-04-17 23:13:49 +0000789 // Make sure we have scalar values available to access the pointer to
790 // the data location.
791 extractScalarValues(Store, VectorMap, ScalarMaps);
792
Sebastian Popa00a0292012-12-18 07:46:06 +0000793 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000794 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000795 Value *NewPointer = generateLocationAccessed(
796 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000797
Tobias Grosserc14582f2013-02-05 18:01:29 +0000798 Value *VectorPtr =
799 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000800 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
801
802 if (!Aligned)
803 Store->setAlignment(8);
804 } else {
805 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000806 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000807 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000808 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000809 Builder.CreateStore(Scalar, NewPointer);
810 }
811 }
812}
813
814bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
815 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000816 for (Value *Operand : Inst->operands())
817 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000818 return true;
819 return false;
820}
821
822bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
823 ValueMapT &VectorMap,
824 VectorValueMapT &ScalarMaps) {
825 bool HasVectorOperand = false;
826 int VectorWidth = getVectorWidth();
827
Tobias Grosser91f5b262014-06-04 08:06:40 +0000828 for (Value *Operand : Inst->operands()) {
829 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000830
831 if (VecOp == VectorMap.end())
832 continue;
833
834 HasVectorOperand = true;
835 Value *NewVector = VecOp->second;
836
837 for (int i = 0; i < VectorWidth; ++i) {
838 ValueMapT &SM = ScalarMaps[i];
839
840 // If there is one scalar extracted, all scalar elements should have
841 // already been extracted by the code here. So no need to check for the
842 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000843 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000844 break;
845
Tobias Grosser91f5b262014-06-04 08:06:40 +0000846 SM[Operand] =
847 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000848 }
849 }
850
851 return HasVectorOperand;
852}
853
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000854void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
855 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000856 ValueMapT &VectorMap,
857 VectorValueMapT &ScalarMaps) {
858 bool HasVectorOperand;
859 int VectorWidth = getVectorWidth();
860
861 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
862
863 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000864 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000865 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000866
867 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
868 return;
869
870 // Make the result available as vector value.
871 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
872 Value *Vector = UndefValue::get(VectorType);
873
874 for (int i = 0; i < VectorWidth; i++)
875 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
876 Builder.getInt32(i));
877
878 VectorMap[Inst] = Vector;
879}
880
Tobias Grosserc14582f2013-02-05 18:01:29 +0000881int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000882
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000883void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
884 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000885 ValueMapT &VectorMap,
886 VectorValueMapT &ScalarMaps) {
887 // Terminator instructions control the control flow. They are explicitly
888 // expressed in the clast and do not need to be copied.
889 if (Inst->isTerminator())
890 return;
891
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000892 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000893 return;
894
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000895 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000896 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000897 return;
898 }
899
900 if (hasVectorOperands(Inst, VectorMap)) {
901 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000902 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000903 return;
904 }
905
906 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000907 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000908 return;
909 }
910
911 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000912 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000913 return;
914 }
915
916 // Falltrough: We generate scalar instructions, if we don't know how to
917 // generate vector code.
918 }
919
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000920 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000921}
922
Johannes Doerfert275a1752015-02-24 16:16:32 +0000923void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
924 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
925 "the vector block generator");
926
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000927 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000928 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000929 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000930 CopyBB->setName("polly.stmt." + BB->getName());
931 Builder.SetInsertPoint(CopyBB->begin());
932
933 // Create two maps that store the mapping from the original instructions of
934 // the old basic block to their copies in the new basic block. Those maps
935 // are basic block local.
936 //
937 // As vector code generation is supported there is one map for scalar values
938 // and one for vector values.
939 //
940 // In case we just do scalar code generation, the vectorMap is not used and
941 // the scalarMap has just one dimension, which contains the mapping.
942 //
943 // In case vector code generation is done, an instruction may either appear
944 // in the vector map once (as it is calculating >vectorwidth< values at a
945 // time. Or (if the values are calculated using scalar operations), it
946 // appears once in every dimension of the scalarMap.
947 VectorValueMapT ScalarBlockMap(getVectorWidth());
948 ValueMapT VectorBlockMap;
949
Tobias Grosser91f5b262014-06-04 08:06:40 +0000950 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000951 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000952}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000953
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000954BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
955 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000956
957 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
958 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
959
960 if (BBCopyIDom)
961 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
962
963 return BBCopyIDom;
964}
965
Johannes Doerfert275a1752015-02-24 16:16:32 +0000966void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
967 LoopToScevMapT &LTS) {
968 assert(Stmt.isRegionStmt() &&
Tobias Grosserd3f21832015-08-01 06:26:51 +0000969 "Only region statements can be copied by the region generator");
Johannes Doerfert275a1752015-02-24 16:16:32 +0000970
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000971 // Forget all old mappings.
972 BlockMap.clear();
973 RegionMaps.clear();
974 IncompletePHINodeMap.clear();
975
Johannes Doerfert275a1752015-02-24 16:16:32 +0000976 // The region represented by the statement.
977 Region *R = Stmt.getRegion();
978
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000979 // Create a dedicated entry for the region where we can reload all demoted
980 // inputs.
981 BasicBlock *EntryBB = R->getEntry();
982 BasicBlock *EntryBBCopy =
983 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
984 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
985 Builder.SetInsertPoint(EntryBBCopy->begin());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000986
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000987 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
988 if (!R->contains(*PI))
989 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000990
991 // Iterate over all blocks in the region in a breadth-first search.
992 std::deque<BasicBlock *> Blocks;
993 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000994 Blocks.push_back(EntryBB);
995 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000996
997 while (!Blocks.empty()) {
998 BasicBlock *BB = Blocks.front();
999 Blocks.pop_front();
1000
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001001 // First split the block and update dominance information.
1002 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001003 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
1004
1005 // In order to remap PHI nodes we store also basic block mappings.
1006 BlockMap[BB] = BBCopy;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001007
1008 // Get the mapping for this block and initialize it with the mapping
1009 // available at its immediate dominator (in the new region).
1010 ValueMapT &RegionMap = RegionMaps[BBCopy];
1011 RegionMap = RegionMaps[BBCopyIDom];
1012
Johannes Doerfert275a1752015-02-24 16:16:32 +00001013 // Copy the block with the BlockGenerator.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001014 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001015
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001016 // In order to remap PHI nodes we store also basic block mappings.
1017 BlockMap[BB] = BBCopy;
1018
1019 // Add values to incomplete PHI nodes waiting for this block to be copied.
1020 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
1021 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB,
1022 GlobalMap, LTS);
1023 IncompletePHINodeMap[BB].clear();
1024
Johannes Doerfert275a1752015-02-24 16:16:32 +00001025 // And continue with new successors inside the region.
1026 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
1027 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
1028 Blocks.push_back(*SI);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001029 }
1030
1031 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001032 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +00001033 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001034 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001035 BlockMap[R->getExit()] = ExitBBCopy;
1036
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001037 repairDominance(R->getExit(), ExitBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001038
1039 // As the block generator doesn't handle control flow we need to add the
1040 // region control flow by hand after all blocks have been copied.
1041 for (BasicBlock *BB : SeenBlocks) {
1042
1043 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
1044
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001045 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +00001046 Instruction *BICopy = BBCopy->getTerminator();
1047
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001048 ValueMapT &RegionMap = RegionMaps[BBCopy];
1049 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1050
Tobias Grosser45e79442015-08-01 09:07:57 +00001051 Builder.SetInsertPoint(BICopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001052 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
1053 BICopy->eraseFromParent();
1054 }
1055
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001056 // Add counting PHI nodes to all loops in the region that can be used as
1057 // replacement for SCEVs refering to the old loop.
1058 for (BasicBlock *BB : SeenBlocks) {
1059 Loop *L = LI.getLoopFor(BB);
1060 if (L == nullptr || L->getHeader() != BB)
1061 continue;
1062
1063 BasicBlock *BBCopy = BlockMap[BB];
1064 Value *NullVal = Builder.getInt32(0);
1065 PHINode *LoopPHI =
1066 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1067 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1068 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
1069 LoopPHI->insertBefore(BBCopy->begin());
1070 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1071
1072 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1073 if (!R->contains(PredBB))
1074 continue;
1075 if (L->contains(PredBB))
1076 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1077 else
1078 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1079 }
1080
1081 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1082 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1083 LoopPHI->addIncoming(NullVal, PredBBCopy);
1084
1085 LTS[L] = SE.getUnknown(LoopPHI);
1086 }
1087
1088 // Add all mappings from the region to the global map so outside uses will use
1089 // the copied instructions.
1090 for (auto &BBMap : RegionMaps)
1091 GlobalMap.insert(BBMap.second.begin(), BBMap.second.end());
1092
Johannes Doerfert275a1752015-02-24 16:16:32 +00001093 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001094 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001095}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001096
1097void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
1098 const Instruction *Inst,
1099 ValueMapT &BBMap) {
1100
1101 // Inside a non-affine region PHI nodes are copied not demoted. Once the
1102 // phi is copied it will reload all inputs from outside the region, hence
1103 // we do not need to generate code for the read access of the operands of a
1104 // PHI.
1105 if (isa<PHINode>(Inst))
1106 return;
1107
1108 return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
1109}
1110
1111void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
1112 ValueMapT &BBMap,
1113 ValueMapT &GlobalMap) {
1114 const Region &R = Stmt.getParent()->getRegion();
1115
1116 Region *StmtR = Stmt.getRegion();
1117 assert(StmtR && "Block statements need to use the generateScalarStores() "
1118 "function in the BlockGenerator");
1119
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001120 for (MemoryAccess *MA : Stmt) {
1121
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001122 if (!MA->isScalar() || MA->isRead())
1123 continue;
1124
1125 Instruction *ScalarBase = cast<Instruction>(MA->getBaseAddr());
1126 Instruction *ScalarInst = MA->getAccessInstruction();
1127 PHINode *ScalarBasePHI = dyn_cast<PHINode>(ScalarBase);
1128
Tobias Grosser62139132015-08-02 16:17:41 +00001129 // Only generate accesses that belong to this basic block.
1130 if (ScalarInst->getParent() != BB)
1131 continue;
1132
Tobias Grosser92245222015-07-28 14:53:44 +00001133 Value *Val = nullptr;
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001134 AllocaInst *ScalarAddr = nullptr;
1135
Tobias Grosser92245222015-07-28 14:53:44 +00001136 if (MA->getScopArrayInfo()->isPHI()) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001137 int PHIIdx = ScalarBasePHI->getBasicBlockIndex(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001138 ScalarAddr = getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
Tobias Grosser92245222015-07-28 14:53:44 +00001139 Val = ScalarBasePHI->getIncomingValue(PHIIdx);
1140 } else {
1141 ScalarAddr = getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
1142 Val = ScalarInst;
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001143 }
1144
Tobias Grosser92245222015-07-28 14:53:44 +00001145 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
1146 Builder.CreateStore(Val, ScalarAddr);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001147 }
1148}
1149
1150void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1151 PHINode *PHICopy, BasicBlock *IncomingBB,
1152 ValueMapT &GlobalMap,
1153 LoopToScevMapT &LTS) {
1154 Region *StmtR = Stmt.getRegion();
1155
1156 // If the incoming block was not yet copied mark this PHI as incomplete.
1157 // Once the block will be copied the incoming value will be added.
1158 BasicBlock *BBCopy = BlockMap[IncomingBB];
1159 if (!BBCopy) {
1160 assert(StmtR->contains(IncomingBB) &&
1161 "Bad incoming block for PHI in non-affine region");
1162 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1163 return;
1164 }
1165
1166 Value *OpCopy = nullptr;
1167 if (StmtR->contains(IncomingBB)) {
1168 assert(RegionMaps.count(BBCopy) &&
1169 "Incoming PHI block did not have a BBMap");
1170 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1171
1172 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
1173 OpCopy =
1174 getNewValue(Stmt, Op, BBCopyMap, GlobalMap, LTS, getLoopForInst(PHI));
1175 } else {
1176
1177 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1178 return;
1179
1180 AllocaInst *PHIOpAddr =
1181 getOrCreateAlloca(const_cast<PHINode *>(PHI), PHIOpMap, ".phiops");
1182 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1183 BlockMap[IncomingBB]->getTerminator());
1184 }
1185
1186 assert(OpCopy && "Incoming PHI value was not copied properly");
1187 assert(BBCopy && "Incoming PHI block was not copied properly");
1188 PHICopy->addIncoming(OpCopy, BBCopy);
1189}
1190
1191Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
1192 ValueMapT &BBMap,
1193 ValueMapT &GlobalMap,
1194 LoopToScevMapT &LTS) {
1195 unsigned NumIncoming = PHI->getNumIncomingValues();
1196 PHINode *PHICopy =
1197 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1198 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1199 BBMap[PHI] = PHICopy;
1200
1201 for (unsigned u = 0; u < NumIncoming; u++)
1202 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), GlobalMap,
1203 LTS);
1204 return PHICopy;
1205}