blob: c86ff617490f997fa9dcc33963fb4d72b664dfc6 [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::generateLocationAccessed(
182 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000183 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS,
184 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000185 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000186
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000187 isl_ast_expr *AccessExpr = isl_id_to_ast_expr_get(NewAccesses, MA.getId());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000188
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000189 if (AccessExpr) {
190 AccessExpr = isl_ast_expr_address_of(AccessExpr);
191 return ExprBuilder->create(AccessExpr);
192 }
193
194 return getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS,
195 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000196}
197
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000198Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000199 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000200}
201
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000202Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000203 ValueMapT &BBMap,
204 ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000205 LoopToScevMapT &LTS,
206 isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000207 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000208 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
209 GlobalMap, LTS, NewAccesses);
Johannes Doerfert87901452014-10-02 16:22:19 +0000210 Value *ScalarLoad = Builder.CreateAlignedLoad(
211 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000212 return ScalarLoad;
213}
214
Tobias Grosserc186ac72015-08-11 08:13:15 +0000215void BlockGenerator::generateScalarStore(ScopStmt &Stmt, const StoreInst *Store,
216 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000217 LoopToScevMapT &LTS,
218 isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000219 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000220 Value *NewPointer = generateLocationAccessed(Stmt, Store, Pointer, BBMap,
221 GlobalMap, LTS, NewAccesses);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000222 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
223 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000224
Tobias Grosserc186ac72015-08-11 08:13:15 +0000225 Builder.CreateAlignedStore(ValueOperand, NewPointer, Store->getAlignment());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000226}
227
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000228void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
229 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000230 LoopToScevMapT &LTS,
231 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000232
233 // First check for possible scalar dependences for this instruction.
234 generateScalarLoads(Stmt, Inst, BBMap);
235
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000236 // Terminator instructions control the control flow. They are explicitly
237 // expressed in the clast and do not need to be copied.
238 if (Inst->isTerminator())
239 return;
240
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000241 Loop *L = getLoopForInst(Inst);
242 if ((Stmt.isBlockStmt() || !Stmt.getRegion()->contains(L)) &&
243 canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion())) {
244 Value *NewValue = getNewValue(Stmt, Inst, BBMap, GlobalMap, LTS, L);
245 BBMap[Inst] = NewValue;
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000246 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000247 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000248
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000249 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000250 Value *NewLoad =
251 generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS, NewAccesses);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000252 // Compute NewLoad before its insertion in BBMap to make the insertion
253 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000254 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000255 return;
256 }
257
258 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000259 generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000260 return;
261 }
262
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000263 if (const PHINode *PHI = dyn_cast<PHINode>(Inst)) {
264 copyPHIInstruction(Stmt, PHI, BBMap, GlobalMap, LTS);
265 return;
266 }
267
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000268 // Skip some special intrinsics for which we do not adjust the semantics to
269 // the new schedule. All others are handled like every other instruction.
270 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
271 switch (IT->getIntrinsicID()) {
272 // Lifetime markers are ignored.
273 case llvm::Intrinsic::lifetime_start:
274 case llvm::Intrinsic::lifetime_end:
275 // Invariant markers are ignored.
276 case llvm::Intrinsic::invariant_start:
277 case llvm::Intrinsic::invariant_end:
278 // Some misc annotations are ignored.
279 case llvm::Intrinsic::var_annotation:
280 case llvm::Intrinsic::ptr_annotation:
281 case llvm::Intrinsic::annotation:
282 case llvm::Intrinsic::donothing:
283 case llvm::Intrinsic::assume:
284 case llvm::Intrinsic::expect:
285 return;
286 default:
287 // Other intrinsics are copied.
288 break;
289 }
290 }
291
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000292 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000293}
294
Johannes Doerfert275a1752015-02-24 16:16:32 +0000295void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000296 LoopToScevMapT &LTS,
297 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000298 assert(Stmt.isBlockStmt() &&
299 "Only block statements can be copied by the block generator");
300
301 ValueMapT BBMap;
302
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000303 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000304 copyBB(Stmt, BB, BBMap, GlobalMap, LTS, NewAccesses);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000305}
306
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000307BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000308 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000309 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000310 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000311 return CopyBB;
312}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000313
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000314BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
315 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000316 LoopToScevMapT &LTS,
317 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000318 BasicBlock *CopyBB = splitBB(BB);
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000319 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS, NewAccesses);
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000320 return CopyBB;
321}
322
323void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
324 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000325 LoopToScevMapT &LTS,
326 isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000327 Builder.SetInsertPoint(CopyBB->begin());
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000328 EntryBB = &CopyBB->getParent()->getEntryBlock();
329
Tobias Grosser91f5b262014-06-04 08:06:40 +0000330 for (Instruction &Inst : *BB)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000331 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS, NewAccesses);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000332
333 // After a basic block was copied store all scalars that escape this block
334 // in their alloca. First the scalars that have dependences inside the SCoP,
335 // then the ones that might escape the SCoP.
336 generateScalarStores(Stmt, BB, BBMap, GlobalMap);
337
338 const Region &R = Stmt.getParent()->getRegion();
339 for (Instruction &Inst : *BB)
340 handleOutsideUsers(R, &Inst, BBMap[&Inst]);
341}
342
Tobias Grosser0164b8f2015-08-13 08:07:39 +0000343AllocaInst *BlockGenerator::getOrCreateAlloca(Value *ScalarBase,
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000344 ScalarAllocaMapTy &Map,
Tobias Grosser29854002015-08-30 15:03:59 +0000345 const char *NameExt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000346 // Check if an alloca was cached for the base instruction.
347 AllocaInst *&Addr = Map[ScalarBase];
348
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000349 // If no alloca was found create one and insert it in the entry block.
350 if (!Addr) {
351 auto *Ty = ScalarBase->getType();
352 Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
353 Addr->insertBefore(EntryBB->getFirstInsertionPt());
354 }
355
356 return Addr;
357}
358
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000359AllocaInst *BlockGenerator::getOrCreateAlloca(MemoryAccess &Access) {
360 if (Access.getScopArrayInfo()->isPHI())
361 return getOrCreatePHIAlloca(Access.getBaseAddr());
362 else
363 return getOrCreateScalarAlloca(Access.getBaseAddr());
364}
365
Tobias Grosserb79a67d2015-08-28 08:23:35 +0000366AllocaInst *BlockGenerator::getOrCreateScalarAlloca(Value *ScalarBase) {
367 return getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
368}
369
370AllocaInst *BlockGenerator::getOrCreatePHIAlloca(Value *ScalarBase) {
371 return getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
372}
373
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000374void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst,
375 Value *InstCopy) {
Tobias Grosser29854002015-08-30 15:03:59 +0000376 // If there are escape users we get the alloca for this instruction and put it
377 // in the EscapeMap for later finalization. Lastly, if the instruction was
378 // copied multiple times we already did this and can exit.
Michael Kruseacb6ade2015-08-18 17:25:48 +0000379 if (EscapeMap.count(Inst))
380 return;
Johannes Doerferte69e1142015-08-18 11:56:00 +0000381
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000382 EscapeUserVectorTy EscapeUsers;
383 for (User *U : Inst->users()) {
384
385 // Non-instruction user will never escape.
386 Instruction *UI = dyn_cast<Instruction>(U);
387 if (!UI)
388 continue;
389
Johannes Doerfertddb83d02015-08-16 08:35:40 +0000390 if (R.contains(UI))
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000391 continue;
392
393 EscapeUsers.push_back(UI);
394 }
395
396 // Exit if no escape uses were found.
397 if (EscapeUsers.empty())
398 return;
399
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000400 // Get or create an escape alloca for this instruction.
Tobias Grosser29854002015-08-30 15:03:59 +0000401 AllocaInst *ScalarAddr = getOrCreateScalarAlloca(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000402
403 // Remember that this instruction has escape uses and the escape alloca.
404 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000405}
406
407void BlockGenerator::generateScalarLoads(ScopStmt &Stmt,
408 const Instruction *Inst,
409 ValueMapT &BBMap) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000410 auto *MAL = Stmt.lookupAccessesFor(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000411
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000412 if (!MAL)
413 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000414
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000415 for (MemoryAccess &MA : *MAL) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000416 if (!MA.isScalar() || !MA.isRead())
417 continue;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000418
Tobias Grosserfcfac082015-08-30 16:01:58 +0000419 auto *Address = getOrCreateAlloca(MA);
420 auto Name = Address->getName() + ".reload";
421 BBMap[MA.getBaseAddr()] = Builder.CreateLoad(Address, Name);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000422 }
423}
424
425Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R,
426 ScalarAllocaMapTy &ReloadMap,
427 ValueMapT &BBMap,
428 ValueMapT &GlobalMap) {
429 // If the value we want to store is an instruction we might have demoted it
430 // in order to make it accessible here. In such a case a reload is
431 // necessary. If it is no instruction it will always be a value that
432 // dominates the current point and we can just use it. In total there are 4
433 // options:
434 // (1) The value is no instruction ==> use the value.
435 // (2) The value is an instruction that was split out of the region prior to
436 // code generation ==> use the instruction as it dominates the region.
437 // (3) The value is an instruction:
438 // (a) The value was defined in the current block, thus a copy is in
439 // the BBMap ==> use the mapped value.
440 // (b) The value was defined in a previous block, thus we demoted it
441 // earlier ==> use the reloaded value.
442 Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue);
443 if (!ScalarValueInst)
444 return ScalarValue;
445
446 if (!R.contains(ScalarValueInst)) {
447 if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst))
448 return /* Case (3a) */ ScalarValueCopy;
449 else
450 return /* Case 2 */ ScalarValue;
451 }
452
453 if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst))
454 return /* Case (3a) */ ScalarValueCopy;
455
456 // Case (3b)
Johannes Doerferte1fa6da2015-08-17 09:38:46 +0000457 Value *ReloadAddr = getOrCreateAlloca(ScalarValueInst, ReloadMap, ".s2a");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000458 ScalarValue =
459 Builder.CreateLoad(ReloadAddr, ReloadAddr->getName() + ".reload");
460
461 return ScalarValue;
462}
463
464void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
465 ValueMapT &BBMap,
466 ValueMapT &GlobalMap) {
467 const Region &R = Stmt.getParent()->getRegion();
468
469 assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() &&
470 "Region statements need to use the generateScalarStores() "
471 "function in the RegionGenerator");
472
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000473 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000474 if (!MA->isScalar() || MA->isRead())
475 continue;
476
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000477 Value *Val = MA->getAccessValue();
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000478 auto *Address = getOrCreateAlloca(*MA);
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000479
Tobias Grosser92245222015-07-28 14:53:44 +0000480 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
481 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000482 }
483}
484
485void BlockGenerator::createScalarInitialization(Region &R,
486 ValueMapT &GlobalMap) {
487 // The split block __just before__ the region and optimized region.
488 BasicBlock *SplitBB = R.getEnteringBlock();
489 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
490 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
491
492 // Get the start block of the __optimized__ region.
493 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
494 if (StartBB == R.getEntry())
495 StartBB = SplitBBTerm->getSuccessor(1);
496
497 // For each PHI predecessor outside the region store the incoming operand
498 // value prior to entering the optimized region.
499 Builder.SetInsertPoint(StartBB->getTerminator());
500
501 ScalarAllocaMapTy EmptyMap;
502 for (const auto &PHIOpMapping : PHIOpMap) {
503 const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
504
505 // Check if this PHI has the split block as predecessor (that is the only
506 // possible predecessor outside the SCoP).
507 int idx = PHI->getBasicBlockIndex(SplitBB);
508 if (idx < 0)
509 continue;
510
511 Value *ScalarValue = PHI->getIncomingValue(idx);
512 ScalarValue =
513 getNewScalarValue(ScalarValue, R, EmptyMap, GlobalMap, GlobalMap);
514
515 // If the split block is the predecessor initialize the PHI operator alloca.
516 Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
517 }
518}
519
520void BlockGenerator::createScalarFinalization(Region &R) {
521 // The exit block of the __unoptimized__ region.
522 BasicBlock *ExitBB = R.getExitingBlock();
523 // The merge block __just after__ the region and the optimized region.
524 BasicBlock *MergeBB = R.getExit();
525
526 // The exit block of the __optimized__ region.
527 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
528 if (OptExitBB == ExitBB)
529 OptExitBB = *(++pred_begin(MergeBB));
530
531 Builder.SetInsertPoint(OptExitBB->getTerminator());
532 for (const auto &EscapeMapping : EscapeMap) {
533 // Extract the escaping instruction and the escaping users as well as the
534 // alloca the instruction was demoted to.
535 Instruction *EscapeInst = EscapeMapping.getFirst();
536 const auto &EscapeMappingValue = EscapeMapping.getSecond();
537 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
538 AllocaInst *ScalarAddr = EscapeMappingValue.first;
539
540 // Reload the demoted instruction in the optimized version of the SCoP.
541 Instruction *EscapeInstReload =
542 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
543
544 // Create the merge PHI that merges the optimized and unoptimized version.
545 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
546 EscapeInst->getName() + ".merge");
547 MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
548
549 // Add the respective values to the merge PHI.
550 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
551 MergePHI->addIncoming(EscapeInst, ExitBB);
552
553 // The information of scalar evolution about the escaping instruction needs
554 // to be revoked so the new merged instruction will be used.
555 if (SE.isSCEVable(EscapeInst->getType()))
556 SE.forgetValue(EscapeInst);
557
558 // Replace all uses of the demoted instruction with the merge PHI.
559 for (Instruction *EUser : EscapeUsers)
560 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
561 }
562}
563
564void BlockGenerator::finalizeSCoP(Scop &S, ValueMapT &GlobalMap) {
565 createScalarInitialization(S.getRegion(), GlobalMap);
566 createScalarFinalization(S.getRegion());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000567}
568
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000569VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
570 VectorValueMapT &GlobalMaps,
571 std::vector<LoopToScevMapT> &VLTS,
572 isl_map *Schedule)
573 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
574 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000575 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
576 assert(Schedule && "No statement domain provided");
577}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000578
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000579Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000580 ValueMapT &VectorMap,
581 VectorValueMapT &ScalarMaps,
582 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000583 if (Value *NewValue = VectorMap.lookup(Old))
584 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000585
586 int Width = getVectorWidth();
587
588 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
589
590 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000591 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000592 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
593 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000594 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000595
596 VectorMap[Old] = Vector;
597
598 return Vector;
599}
600
601Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
602 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
603 assert(PointerTy && "PointerType expected");
604
605 Type *ScalarType = PointerTy->getElementType();
606 VectorType *VectorType = VectorType::get(ScalarType, Width);
607
608 return PointerType::getUnqual(VectorType);
609}
610
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000611Value *VectorBlockGenerator::generateStrideOneLoad(
612 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000613 __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000614 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000615 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000616 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
617 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
618
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000619 Value *NewPointer = nullptr;
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000620 NewPointer =
621 generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
622 GlobalMaps[Offset], VLTS[Offset], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000623 Value *VectorPtr =
624 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
625 LoadInst *VecLoad =
626 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000627 if (!Aligned)
628 VecLoad->setAlignment(8);
629
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000630 if (NegativeStride) {
631 SmallVector<Constant *, 16> Indices;
632 for (int i = VectorWidth - 1; i >= 0; i--)
633 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
634 Constant *SV = llvm::ConstantVector::get(Indices);
635 Value *RevVecLoad = Builder.CreateShuffleVector(
636 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
637 return RevVecLoad;
638 }
639
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000640 return VecLoad;
641}
642
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000643Value *VectorBlockGenerator::generateStrideZeroLoad(
644 ScopStmt &Stmt, const LoadInst *Load, ValueMapT &BBMap,
645 __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000646 const Value *Pointer = Load->getPointerOperand();
647 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000648 Value *NewPointer = generateLocationAccessed(
649 Stmt, Load, Pointer, BBMap, GlobalMaps[0], VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000650 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
651 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000652 LoadInst *ScalarLoad =
653 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000654
655 if (!Aligned)
656 ScalarLoad->setAlignment(8);
657
Tobias Grosserc14582f2013-02-05 18:01:29 +0000658 Constant *SplatVector = Constant::getNullValue(
659 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000660
Tobias Grosserc14582f2013-02-05 18:01:29 +0000661 Value *VectorLoad = Builder.CreateShuffleVector(
662 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000663 return VectorLoad;
664}
665
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000666Value *VectorBlockGenerator::generateUnknownStrideLoad(
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000667 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
668 __isl_keep isl_id_to_ast_expr *NewAccesses
669
670 ) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000671 int VectorWidth = getVectorWidth();
672 const Value *Pointer = Load->getPointerOperand();
673 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000674 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000675
676 Value *Vector = UndefValue::get(VectorType);
677
678 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000679 Value *NewPointer =
680 generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[i],
681 GlobalMaps[i], VLTS[i], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000682 Value *ScalarLoad =
683 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
684 Vector = Builder.CreateInsertElement(
685 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000686 }
687
688 return Vector;
689}
690
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000691void VectorBlockGenerator::generateLoad(
692 ScopStmt &Stmt, const LoadInst *Load, ValueMapT &VectorMap,
693 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Tobias Grosser28736452015-03-23 07:00:36 +0000694 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000695 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000696 ScalarMaps[i][Load] = generateScalarLoad(
697 Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000698 return;
699 }
700
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000701 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000702
Tobias Grosser95493982014-04-18 09:46:35 +0000703 // Make sure we have scalar values available to access the pointer to
704 // the data location.
705 extractScalarValues(Load, VectorMap, ScalarMaps);
706
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000707 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000708 if (Access.isStrideZero(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000709 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses);
Sebastian Popa00a0292012-12-18 07:46:06 +0000710 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000711 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000712 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000713 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000714 else
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000715 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000716
717 VectorMap[Load] = NewLoad;
718}
719
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000720void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
721 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000722 ValueMapT &VectorMap,
723 VectorValueMapT &ScalarMaps) {
724 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000725 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
726 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000727
728 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
729
730 const CastInst *Cast = dyn_cast<CastInst>(Inst);
731 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
732 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
733}
734
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000735void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
736 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000737 ValueMapT &VectorMap,
738 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000739 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000740 Value *OpZero = Inst->getOperand(0);
741 Value *OpOne = Inst->getOperand(1);
742
743 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000744 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
745 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000746
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000747 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000748 Inst->getName() + "p_vec");
749 VectorMap[Inst] = NewInst;
750}
751
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000752void VectorBlockGenerator::copyStore(
753 ScopStmt &Stmt, const StoreInst *Store, ValueMapT &VectorMap,
754 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000755 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000756
757 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000758 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000759 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000760
Tobias Grosser50fd7012014-04-17 23:13:49 +0000761 // Make sure we have scalar values available to access the pointer to
762 // the data location.
763 extractScalarValues(Store, VectorMap, ScalarMaps);
764
Sebastian Popa00a0292012-12-18 07:46:06 +0000765 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000766 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000767 Value *NewPointer =
768 generateLocationAccessed(Stmt, Store, Pointer, ScalarMaps[0],
769 GlobalMaps[0], VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000770
Tobias Grosserc14582f2013-02-05 18:01:29 +0000771 Value *VectorPtr =
772 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000773 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
774
775 if (!Aligned)
776 Store->setAlignment(8);
777 } else {
778 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000779 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000780 Value *NewPointer =
781 generateLocationAccessed(Stmt, Store, Pointer, ScalarMaps[i],
782 GlobalMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000783 Builder.CreateStore(Scalar, NewPointer);
784 }
785 }
786}
787
788bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
789 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000790 for (Value *Operand : Inst->operands())
791 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000792 return true;
793 return false;
794}
795
796bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
797 ValueMapT &VectorMap,
798 VectorValueMapT &ScalarMaps) {
799 bool HasVectorOperand = false;
800 int VectorWidth = getVectorWidth();
801
Tobias Grosser91f5b262014-06-04 08:06:40 +0000802 for (Value *Operand : Inst->operands()) {
803 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000804
805 if (VecOp == VectorMap.end())
806 continue;
807
808 HasVectorOperand = true;
809 Value *NewVector = VecOp->second;
810
811 for (int i = 0; i < VectorWidth; ++i) {
812 ValueMapT &SM = ScalarMaps[i];
813
814 // If there is one scalar extracted, all scalar elements should have
815 // already been extracted by the code here. So no need to check for the
816 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000817 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000818 break;
819
Tobias Grosser91f5b262014-06-04 08:06:40 +0000820 SM[Operand] =
821 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000822 }
823 }
824
825 return HasVectorOperand;
826}
827
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000828void VectorBlockGenerator::copyInstScalarized(
829 ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
830 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000831 bool HasVectorOperand;
832 int VectorWidth = getVectorWidth();
833
834 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
835
836 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000837 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000838 GlobalMaps[VectorLane], VLTS[VectorLane],
839 NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000840
841 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
842 return;
843
844 // Make the result available as vector value.
845 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
846 Value *Vector = UndefValue::get(VectorType);
847
848 for (int i = 0; i < VectorWidth; i++)
849 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
850 Builder.getInt32(i));
851
852 VectorMap[Inst] = Vector;
853}
854
Tobias Grosserc14582f2013-02-05 18:01:29 +0000855int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000856
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000857void VectorBlockGenerator::copyInstruction(
858 ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
859 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000860 // Terminator instructions control the control flow. They are explicitly
861 // expressed in the clast and do not need to be copied.
862 if (Inst->isTerminator())
863 return;
864
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000865 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000866 return;
867
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000868 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000869 generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000870 return;
871 }
872
873 if (hasVectorOperands(Inst, VectorMap)) {
874 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000875 copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000876 return;
877 }
878
879 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000880 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000881 return;
882 }
883
884 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000885 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000886 return;
887 }
888
889 // Falltrough: We generate scalar instructions, if we don't know how to
890 // generate vector code.
891 }
892
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000893 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000894}
895
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000896void VectorBlockGenerator::copyStmt(
897 ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000898 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
899 "the vector block generator");
900
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000901 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000902 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000903 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000904 CopyBB->setName("polly.stmt." + BB->getName());
905 Builder.SetInsertPoint(CopyBB->begin());
906
907 // Create two maps that store the mapping from the original instructions of
908 // the old basic block to their copies in the new basic block. Those maps
909 // are basic block local.
910 //
911 // As vector code generation is supported there is one map for scalar values
912 // and one for vector values.
913 //
914 // In case we just do scalar code generation, the vectorMap is not used and
915 // the scalarMap has just one dimension, which contains the mapping.
916 //
917 // In case vector code generation is done, an instruction may either appear
918 // in the vector map once (as it is calculating >vectorwidth< values at a
919 // time. Or (if the values are calculated using scalar operations), it
920 // appears once in every dimension of the scalarMap.
921 VectorValueMapT ScalarBlockMap(getVectorWidth());
922 ValueMapT VectorBlockMap;
923
Tobias Grosser91f5b262014-06-04 08:06:40 +0000924 for (Instruction &Inst : *BB)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000925 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000926}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000927
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000928BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
929 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000930
931 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
932 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
933
934 if (BBCopyIDom)
935 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
936
937 return BBCopyIDom;
938}
939
Johannes Doerfert275a1752015-02-24 16:16:32 +0000940void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000941 LoopToScevMapT &LTS,
942 isl_id_to_ast_expr *IdToAstExp) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000943 assert(Stmt.isRegionStmt() &&
Tobias Grosserd3f21832015-08-01 06:26:51 +0000944 "Only region statements can be copied by the region generator");
Johannes Doerfert275a1752015-02-24 16:16:32 +0000945
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000946 // Forget all old mappings.
947 BlockMap.clear();
948 RegionMaps.clear();
949 IncompletePHINodeMap.clear();
950
Johannes Doerfert275a1752015-02-24 16:16:32 +0000951 // The region represented by the statement.
952 Region *R = Stmt.getRegion();
953
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000954 // Create a dedicated entry for the region where we can reload all demoted
955 // inputs.
956 BasicBlock *EntryBB = R->getEntry();
957 BasicBlock *EntryBBCopy =
958 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
959 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
960 Builder.SetInsertPoint(EntryBBCopy->begin());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000961
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000962 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
963 if (!R->contains(*PI))
964 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000965
966 // Iterate over all blocks in the region in a breadth-first search.
967 std::deque<BasicBlock *> Blocks;
968 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000969 Blocks.push_back(EntryBB);
970 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000971
972 while (!Blocks.empty()) {
973 BasicBlock *BB = Blocks.front();
974 Blocks.pop_front();
975
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000976 // First split the block and update dominance information.
977 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000978 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
979
980 // In order to remap PHI nodes we store also basic block mappings.
981 BlockMap[BB] = BBCopy;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000982
983 // Get the mapping for this block and initialize it with the mapping
984 // available at its immediate dominator (in the new region).
985 ValueMapT &RegionMap = RegionMaps[BBCopy];
986 RegionMap = RegionMaps[BBCopyIDom];
987
Johannes Doerfert275a1752015-02-24 16:16:32 +0000988 // Copy the block with the BlockGenerator.
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000989 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS, IdToAstExp);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000990
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000991 // In order to remap PHI nodes we store also basic block mappings.
992 BlockMap[BB] = BBCopy;
993
994 // Add values to incomplete PHI nodes waiting for this block to be copied.
995 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
996 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB,
997 GlobalMap, LTS);
998 IncompletePHINodeMap[BB].clear();
999
Johannes Doerfert275a1752015-02-24 16:16:32 +00001000 // And continue with new successors inside the region.
1001 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
1002 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
1003 Blocks.push_back(*SI);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001004 }
1005
1006 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001007 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +00001008 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001009 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001010 BlockMap[R->getExit()] = ExitBBCopy;
1011
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001012 repairDominance(R->getExit(), ExitBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001013
1014 // As the block generator doesn't handle control flow we need to add the
1015 // region control flow by hand after all blocks have been copied.
1016 for (BasicBlock *BB : SeenBlocks) {
1017
1018 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
1019
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001020 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +00001021 Instruction *BICopy = BBCopy->getTerminator();
1022
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001023 ValueMapT &RegionMap = RegionMaps[BBCopy];
1024 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1025
Tobias Grosser45e79442015-08-01 09:07:57 +00001026 Builder.SetInsertPoint(BICopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001027 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
1028 BICopy->eraseFromParent();
1029 }
1030
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001031 // Add counting PHI nodes to all loops in the region that can be used as
1032 // replacement for SCEVs refering to the old loop.
1033 for (BasicBlock *BB : SeenBlocks) {
1034 Loop *L = LI.getLoopFor(BB);
1035 if (L == nullptr || L->getHeader() != BB)
1036 continue;
1037
1038 BasicBlock *BBCopy = BlockMap[BB];
1039 Value *NullVal = Builder.getInt32(0);
1040 PHINode *LoopPHI =
1041 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1042 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1043 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
1044 LoopPHI->insertBefore(BBCopy->begin());
1045 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1046
1047 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1048 if (!R->contains(PredBB))
1049 continue;
1050 if (L->contains(PredBB))
1051 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1052 else
1053 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1054 }
1055
1056 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1057 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1058 LoopPHI->addIncoming(NullVal, PredBBCopy);
1059
1060 LTS[L] = SE.getUnknown(LoopPHI);
1061 }
1062
1063 // Add all mappings from the region to the global map so outside uses will use
1064 // the copied instructions.
1065 for (auto &BBMap : RegionMaps)
1066 GlobalMap.insert(BBMap.second.begin(), BBMap.second.end());
1067
Johannes Doerfert275a1752015-02-24 16:16:32 +00001068 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001069 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001070}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001071
1072void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
1073 const Instruction *Inst,
1074 ValueMapT &BBMap) {
1075
1076 // Inside a non-affine region PHI nodes are copied not demoted. Once the
1077 // phi is copied it will reload all inputs from outside the region, hence
1078 // we do not need to generate code for the read access of the operands of a
1079 // PHI.
1080 if (isa<PHINode>(Inst))
1081 return;
1082
1083 return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
1084}
1085
1086void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
1087 ValueMapT &BBMap,
1088 ValueMapT &GlobalMap) {
1089 const Region &R = Stmt.getParent()->getRegion();
1090
Tobias Grosser75296902015-08-21 19:23:21 +00001091 assert(Stmt.getRegion() &&
1092 "Block statements need to use the generateScalarStores() "
1093 "function in the BlockGenerator");
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001094
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001095 for (MemoryAccess *MA : Stmt) {
1096
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001097 if (!MA->isScalar() || MA->isRead())
1098 continue;
1099
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001100 Instruction *ScalarInst = MA->getAccessInstruction();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001101
Tobias Grosser62139132015-08-02 16:17:41 +00001102 // Only generate accesses that belong to this basic block.
1103 if (ScalarInst->getParent() != BB)
1104 continue;
1105
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001106 Value *Val = MA->getAccessValue();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001107
Tobias Grosserf8d55f72015-08-29 18:12:03 +00001108 auto Address = getOrCreateAlloca(*MA);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001109
Tobias Grosser92245222015-07-28 14:53:44 +00001110 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
Tobias Grosserf8d55f72015-08-29 18:12:03 +00001111 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001112 }
1113}
1114
1115void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1116 PHINode *PHICopy, BasicBlock *IncomingBB,
1117 ValueMapT &GlobalMap,
1118 LoopToScevMapT &LTS) {
1119 Region *StmtR = Stmt.getRegion();
1120
1121 // If the incoming block was not yet copied mark this PHI as incomplete.
1122 // Once the block will be copied the incoming value will be added.
1123 BasicBlock *BBCopy = BlockMap[IncomingBB];
1124 if (!BBCopy) {
1125 assert(StmtR->contains(IncomingBB) &&
1126 "Bad incoming block for PHI in non-affine region");
1127 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1128 return;
1129 }
1130
1131 Value *OpCopy = nullptr;
1132 if (StmtR->contains(IncomingBB)) {
1133 assert(RegionMaps.count(BBCopy) &&
1134 "Incoming PHI block did not have a BBMap");
1135 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1136
1137 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
1138 OpCopy =
1139 getNewValue(Stmt, Op, BBCopyMap, GlobalMap, LTS, getLoopForInst(PHI));
1140 } else {
1141
1142 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1143 return;
1144
Tobias Grosserb79a67d2015-08-28 08:23:35 +00001145 AllocaInst *PHIOpAddr = getOrCreatePHIAlloca(const_cast<PHINode *>(PHI));
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001146 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1147 BlockMap[IncomingBB]->getTerminator());
1148 }
1149
1150 assert(OpCopy && "Incoming PHI value was not copied properly");
1151 assert(BBCopy && "Incoming PHI block was not copied properly");
1152 PHICopy->addIncoming(OpCopy, BBCopy);
1153}
1154
1155Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
1156 ValueMapT &BBMap,
1157 ValueMapT &GlobalMap,
1158 LoopToScevMapT &LTS) {
1159 unsigned NumIncoming = PHI->getNumIncomingValues();
1160 PHINode *PHICopy =
1161 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1162 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1163 BBMap[PHI] = PHICopy;
1164
1165 for (unsigned u = 0; u < NumIncoming; u++)
1166 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), GlobalMap,
1167 LTS);
1168 return PHICopy;
1169}