blob: c4bbbf4923e87ef2d0907b459399cd7837878667 [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,
345 const char *NameExt,
346 bool *IsNew) {
347
348 // Check if an alloca was cached for the base instruction.
349 AllocaInst *&Addr = Map[ScalarBase];
350
351 // If needed indicate if it was found already or will be created.
352 if (IsNew)
353 *IsNew = (Addr == nullptr);
354
355 // If no alloca was found create one and insert it in the entry block.
356 if (!Addr) {
357 auto *Ty = ScalarBase->getType();
358 Addr = new AllocaInst(Ty, ScalarBase->getName() + NameExt);
359 Addr->insertBefore(EntryBB->getFirstInsertionPt());
360 }
361
362 return Addr;
363}
364
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000365AllocaInst *BlockGenerator::getOrCreateAlloca(MemoryAccess &Access) {
366 if (Access.getScopArrayInfo()->isPHI())
367 return getOrCreatePHIAlloca(Access.getBaseAddr());
368 else
369 return getOrCreateScalarAlloca(Access.getBaseAddr());
370}
371
Tobias Grosserb79a67d2015-08-28 08:23:35 +0000372AllocaInst *BlockGenerator::getOrCreateScalarAlloca(Value *ScalarBase) {
373 return getOrCreateAlloca(ScalarBase, ScalarMap, ".s2a");
374}
375
376AllocaInst *BlockGenerator::getOrCreatePHIAlloca(Value *ScalarBase) {
377 return getOrCreateAlloca(ScalarBase, PHIOpMap, ".phiops");
378}
379
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000380void BlockGenerator::handleOutsideUsers(const Region &R, Instruction *Inst,
381 Value *InstCopy) {
Michael Kruseacb6ade2015-08-18 17:25:48 +0000382 // If there are escape users we get the alloca for this instruction and put
383 // it in the EscapeMap for later finalization. However, if the alloca was not
384 // created by an already handled scalar dependence we have to initialize it
385 // also. Lastly, if the instruction was copied multiple times we already did
386 // this and can exit.
387 if (EscapeMap.count(Inst))
388 return;
Johannes Doerferte69e1142015-08-18 11:56:00 +0000389
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000390 EscapeUserVectorTy EscapeUsers;
391 for (User *U : Inst->users()) {
392
393 // Non-instruction user will never escape.
394 Instruction *UI = dyn_cast<Instruction>(U);
395 if (!UI)
396 continue;
397
Johannes Doerfertddb83d02015-08-16 08:35:40 +0000398 if (R.contains(UI))
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000399 continue;
400
401 EscapeUsers.push_back(UI);
402 }
403
404 // Exit if no escape uses were found.
405 if (EscapeUsers.empty())
406 return;
407
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000408 // Get or create an escape alloca for this instruction.
409 bool IsNew;
410 AllocaInst *ScalarAddr =
411 getOrCreateAlloca(Inst, ScalarMap, ".escape", &IsNew);
412
413 // Remember that this instruction has escape uses and the escape alloca.
414 EscapeMap[Inst] = std::make_pair(ScalarAddr, std::move(EscapeUsers));
415
416 // If the escape alloca was just created store the instruction in there,
417 // otherwise that happened already.
418 if (IsNew) {
419 assert(InstCopy && "Except PHIs every instruction should have a copy!");
420 Builder.CreateStore(InstCopy, ScalarAddr);
421 }
422}
423
424void BlockGenerator::generateScalarLoads(ScopStmt &Stmt,
425 const Instruction *Inst,
426 ValueMapT &BBMap) {
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000427 auto *MAL = Stmt.lookupAccessesFor(Inst);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000428
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000429 if (!MAL)
430 return;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000431
Tobias Grosserd4dd6ec2015-07-27 17:57:58 +0000432 for (MemoryAccess &MA : *MAL) {
433 AllocaInst *Address;
434 if (!MA.isScalar() || !MA.isRead())
435 continue;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000436
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000437 Address = getOrCreateAlloca(MA);
438 BBMap[MA.getBaseAddr()] =
439 Builder.CreateLoad(Address, Address->getName() + ".reload");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000440 }
441}
442
443Value *BlockGenerator::getNewScalarValue(Value *ScalarValue, const Region &R,
444 ScalarAllocaMapTy &ReloadMap,
445 ValueMapT &BBMap,
446 ValueMapT &GlobalMap) {
447 // If the value we want to store is an instruction we might have demoted it
448 // in order to make it accessible here. In such a case a reload is
449 // necessary. If it is no instruction it will always be a value that
450 // dominates the current point and we can just use it. In total there are 4
451 // options:
452 // (1) The value is no instruction ==> use the value.
453 // (2) The value is an instruction that was split out of the region prior to
454 // code generation ==> use the instruction as it dominates the region.
455 // (3) The value is an instruction:
456 // (a) The value was defined in the current block, thus a copy is in
457 // the BBMap ==> use the mapped value.
458 // (b) The value was defined in a previous block, thus we demoted it
459 // earlier ==> use the reloaded value.
460 Instruction *ScalarValueInst = dyn_cast<Instruction>(ScalarValue);
461 if (!ScalarValueInst)
462 return ScalarValue;
463
464 if (!R.contains(ScalarValueInst)) {
465 if (Value *ScalarValueCopy = GlobalMap.lookup(ScalarValueInst))
466 return /* Case (3a) */ ScalarValueCopy;
467 else
468 return /* Case 2 */ ScalarValue;
469 }
470
471 if (Value *ScalarValueCopy = BBMap.lookup(ScalarValueInst))
472 return /* Case (3a) */ ScalarValueCopy;
473
474 // Case (3b)
Johannes Doerferte1fa6da2015-08-17 09:38:46 +0000475 Value *ReloadAddr = getOrCreateAlloca(ScalarValueInst, ReloadMap, ".s2a");
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000476 ScalarValue =
477 Builder.CreateLoad(ReloadAddr, ReloadAddr->getName() + ".reload");
478
479 return ScalarValue;
480}
481
482void BlockGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
483 ValueMapT &BBMap,
484 ValueMapT &GlobalMap) {
485 const Region &R = Stmt.getParent()->getRegion();
486
487 assert(Stmt.isBlockStmt() && BB == Stmt.getBasicBlock() &&
488 "Region statements need to use the generateScalarStores() "
489 "function in the RegionGenerator");
490
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000491 for (MemoryAccess *MA : Stmt) {
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000492 if (!MA->isScalar() || MA->isRead())
493 continue;
494
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000495 Value *Val = MA->getAccessValue();
Tobias Grosserf8d55f72015-08-29 18:12:03 +0000496 auto *Address = getOrCreateAlloca(*MA);
Johannes Doerfertd86f2152015-08-17 10:58:17 +0000497
Tobias Grosser92245222015-07-28 14:53:44 +0000498 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
499 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000500 }
501}
502
503void BlockGenerator::createScalarInitialization(Region &R,
504 ValueMapT &GlobalMap) {
505 // The split block __just before__ the region and optimized region.
506 BasicBlock *SplitBB = R.getEnteringBlock();
507 BranchInst *SplitBBTerm = cast<BranchInst>(SplitBB->getTerminator());
508 assert(SplitBBTerm->getNumSuccessors() == 2 && "Bad region entering block!");
509
510 // Get the start block of the __optimized__ region.
511 BasicBlock *StartBB = SplitBBTerm->getSuccessor(0);
512 if (StartBB == R.getEntry())
513 StartBB = SplitBBTerm->getSuccessor(1);
514
515 // For each PHI predecessor outside the region store the incoming operand
516 // value prior to entering the optimized region.
517 Builder.SetInsertPoint(StartBB->getTerminator());
518
519 ScalarAllocaMapTy EmptyMap;
520 for (const auto &PHIOpMapping : PHIOpMap) {
521 const PHINode *PHI = cast<PHINode>(PHIOpMapping.getFirst());
522
523 // Check if this PHI has the split block as predecessor (that is the only
524 // possible predecessor outside the SCoP).
525 int idx = PHI->getBasicBlockIndex(SplitBB);
526 if (idx < 0)
527 continue;
528
529 Value *ScalarValue = PHI->getIncomingValue(idx);
530 ScalarValue =
531 getNewScalarValue(ScalarValue, R, EmptyMap, GlobalMap, GlobalMap);
532
533 // If the split block is the predecessor initialize the PHI operator alloca.
534 Builder.CreateStore(ScalarValue, PHIOpMapping.getSecond());
535 }
536}
537
538void BlockGenerator::createScalarFinalization(Region &R) {
539 // The exit block of the __unoptimized__ region.
540 BasicBlock *ExitBB = R.getExitingBlock();
541 // The merge block __just after__ the region and the optimized region.
542 BasicBlock *MergeBB = R.getExit();
543
544 // The exit block of the __optimized__ region.
545 BasicBlock *OptExitBB = *(pred_begin(MergeBB));
546 if (OptExitBB == ExitBB)
547 OptExitBB = *(++pred_begin(MergeBB));
548
549 Builder.SetInsertPoint(OptExitBB->getTerminator());
550 for (const auto &EscapeMapping : EscapeMap) {
551 // Extract the escaping instruction and the escaping users as well as the
552 // alloca the instruction was demoted to.
553 Instruction *EscapeInst = EscapeMapping.getFirst();
554 const auto &EscapeMappingValue = EscapeMapping.getSecond();
555 const EscapeUserVectorTy &EscapeUsers = EscapeMappingValue.second;
556 AllocaInst *ScalarAddr = EscapeMappingValue.first;
557
558 // Reload the demoted instruction in the optimized version of the SCoP.
559 Instruction *EscapeInstReload =
560 Builder.CreateLoad(ScalarAddr, EscapeInst->getName() + ".final_reload");
561
562 // Create the merge PHI that merges the optimized and unoptimized version.
563 PHINode *MergePHI = PHINode::Create(EscapeInst->getType(), 2,
564 EscapeInst->getName() + ".merge");
565 MergePHI->insertBefore(MergeBB->getFirstInsertionPt());
566
567 // Add the respective values to the merge PHI.
568 MergePHI->addIncoming(EscapeInstReload, OptExitBB);
569 MergePHI->addIncoming(EscapeInst, ExitBB);
570
571 // The information of scalar evolution about the escaping instruction needs
572 // to be revoked so the new merged instruction will be used.
573 if (SE.isSCEVable(EscapeInst->getType()))
574 SE.forgetValue(EscapeInst);
575
576 // Replace all uses of the demoted instruction with the merge PHI.
577 for (Instruction *EUser : EscapeUsers)
578 EUser->replaceUsesOfWith(EscapeInst, MergePHI);
579 }
580}
581
582void BlockGenerator::finalizeSCoP(Scop &S, ValueMapT &GlobalMap) {
583 createScalarInitialization(S.getRegion(), GlobalMap);
584 createScalarFinalization(S.getRegion());
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000585}
586
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000587VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
588 VectorValueMapT &GlobalMaps,
589 std::vector<LoopToScevMapT> &VLTS,
590 isl_map *Schedule)
591 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
592 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000593 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
594 assert(Schedule && "No statement domain provided");
595}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000596
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000597Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000598 ValueMapT &VectorMap,
599 VectorValueMapT &ScalarMaps,
600 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000601 if (Value *NewValue = VectorMap.lookup(Old))
602 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000603
604 int Width = getVectorWidth();
605
606 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
607
608 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000609 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000610 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
611 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000612 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000613
614 VectorMap[Old] = Vector;
615
616 return Vector;
617}
618
619Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
620 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
621 assert(PointerTy && "PointerType expected");
622
623 Type *ScalarType = PointerTy->getElementType();
624 VectorType *VectorType = VectorType::get(ScalarType, Width);
625
626 return PointerType::getUnqual(VectorType);
627}
628
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000629Value *VectorBlockGenerator::generateStrideOneLoad(
630 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000631 __isl_keep isl_id_to_ast_expr *NewAccesses, bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000632 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000633 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000634 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
635 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
636
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000637 Value *NewPointer = nullptr;
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000638 NewPointer =
639 generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
640 GlobalMaps[Offset], VLTS[Offset], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000641 Value *VectorPtr =
642 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
643 LoadInst *VecLoad =
644 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000645 if (!Aligned)
646 VecLoad->setAlignment(8);
647
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000648 if (NegativeStride) {
649 SmallVector<Constant *, 16> Indices;
650 for (int i = VectorWidth - 1; i >= 0; i--)
651 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
652 Constant *SV = llvm::ConstantVector::get(Indices);
653 Value *RevVecLoad = Builder.CreateShuffleVector(
654 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
655 return RevVecLoad;
656 }
657
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000658 return VecLoad;
659}
660
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000661Value *VectorBlockGenerator::generateStrideZeroLoad(
662 ScopStmt &Stmt, const LoadInst *Load, ValueMapT &BBMap,
663 __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000664 const Value *Pointer = Load->getPointerOperand();
665 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000666 Value *NewPointer = generateLocationAccessed(
667 Stmt, Load, Pointer, BBMap, GlobalMaps[0], VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000668 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
669 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000670 LoadInst *ScalarLoad =
671 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000672
673 if (!Aligned)
674 ScalarLoad->setAlignment(8);
675
Tobias Grosserc14582f2013-02-05 18:01:29 +0000676 Constant *SplatVector = Constant::getNullValue(
677 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000678
Tobias Grosserc14582f2013-02-05 18:01:29 +0000679 Value *VectorLoad = Builder.CreateShuffleVector(
680 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000681 return VectorLoad;
682}
683
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000684Value *VectorBlockGenerator::generateUnknownStrideLoad(
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000685 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
686 __isl_keep isl_id_to_ast_expr *NewAccesses
687
688 ) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000689 int VectorWidth = getVectorWidth();
690 const Value *Pointer = Load->getPointerOperand();
691 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000692 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000693
694 Value *Vector = UndefValue::get(VectorType);
695
696 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000697 Value *NewPointer =
698 generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[i],
699 GlobalMaps[i], VLTS[i], NewAccesses);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000700 Value *ScalarLoad =
701 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
702 Vector = Builder.CreateInsertElement(
703 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000704 }
705
706 return Vector;
707}
708
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000709void VectorBlockGenerator::generateLoad(
710 ScopStmt &Stmt, const LoadInst *Load, ValueMapT &VectorMap,
711 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Tobias Grosser28736452015-03-23 07:00:36 +0000712 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000713 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000714 ScalarMaps[i][Load] = generateScalarLoad(
715 Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000716 return;
717 }
718
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000719 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000720
Tobias Grosser95493982014-04-18 09:46:35 +0000721 // Make sure we have scalar values available to access the pointer to
722 // the data location.
723 extractScalarValues(Load, VectorMap, ScalarMaps);
724
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000725 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000726 if (Access.isStrideZero(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000727 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0], NewAccesses);
Sebastian Popa00a0292012-12-18 07:46:06 +0000728 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000729 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000730 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000731 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, NewAccesses, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000732 else
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000733 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000734
735 VectorMap[Load] = NewLoad;
736}
737
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000738void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
739 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000740 ValueMapT &VectorMap,
741 VectorValueMapT &ScalarMaps) {
742 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000743 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
744 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000745
746 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
747
748 const CastInst *Cast = dyn_cast<CastInst>(Inst);
749 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
750 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
751}
752
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000753void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
754 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000755 ValueMapT &VectorMap,
756 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000757 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000758 Value *OpZero = Inst->getOperand(0);
759 Value *OpOne = Inst->getOperand(1);
760
761 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000762 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
763 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000764
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000765 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000766 Inst->getName() + "p_vec");
767 VectorMap[Inst] = NewInst;
768}
769
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000770void VectorBlockGenerator::copyStore(
771 ScopStmt &Stmt, const StoreInst *Store, ValueMapT &VectorMap,
772 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000773 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000774
775 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000776 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000777 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000778
Tobias Grosser50fd7012014-04-17 23:13:49 +0000779 // Make sure we have scalar values available to access the pointer to
780 // the data location.
781 extractScalarValues(Store, VectorMap, ScalarMaps);
782
Sebastian Popa00a0292012-12-18 07:46:06 +0000783 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000784 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000785 Value *NewPointer =
786 generateLocationAccessed(Stmt, Store, Pointer, ScalarMaps[0],
787 GlobalMaps[0], VLTS[0], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000788
Tobias Grosserc14582f2013-02-05 18:01:29 +0000789 Value *VectorPtr =
790 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000791 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
792
793 if (!Aligned)
794 Store->setAlignment(8);
795 } else {
796 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000797 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000798 Value *NewPointer =
799 generateLocationAccessed(Stmt, Store, Pointer, ScalarMaps[i],
800 GlobalMaps[i], VLTS[i], NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000801 Builder.CreateStore(Scalar, NewPointer);
802 }
803 }
804}
805
806bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
807 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000808 for (Value *Operand : Inst->operands())
809 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000810 return true;
811 return false;
812}
813
814bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
815 ValueMapT &VectorMap,
816 VectorValueMapT &ScalarMaps) {
817 bool HasVectorOperand = false;
818 int VectorWidth = getVectorWidth();
819
Tobias Grosser91f5b262014-06-04 08:06:40 +0000820 for (Value *Operand : Inst->operands()) {
821 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000822
823 if (VecOp == VectorMap.end())
824 continue;
825
826 HasVectorOperand = true;
827 Value *NewVector = VecOp->second;
828
829 for (int i = 0; i < VectorWidth; ++i) {
830 ValueMapT &SM = ScalarMaps[i];
831
832 // If there is one scalar extracted, all scalar elements should have
833 // already been extracted by the code here. So no need to check for the
834 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000835 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000836 break;
837
Tobias Grosser91f5b262014-06-04 08:06:40 +0000838 SM[Operand] =
839 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000840 }
841 }
842
843 return HasVectorOperand;
844}
845
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000846void VectorBlockGenerator::copyInstScalarized(
847 ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
848 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000849 bool HasVectorOperand;
850 int VectorWidth = getVectorWidth();
851
852 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
853
854 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000855 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000856 GlobalMaps[VectorLane], VLTS[VectorLane],
857 NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000858
859 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
860 return;
861
862 // Make the result available as vector value.
863 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
864 Value *Vector = UndefValue::get(VectorType);
865
866 for (int i = 0; i < VectorWidth; i++)
867 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
868 Builder.getInt32(i));
869
870 VectorMap[Inst] = Vector;
871}
872
Tobias Grosserc14582f2013-02-05 18:01:29 +0000873int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000874
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000875void VectorBlockGenerator::copyInstruction(
876 ScopStmt &Stmt, const Instruction *Inst, ValueMapT &VectorMap,
877 VectorValueMapT &ScalarMaps, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000878 // Terminator instructions control the control flow. They are explicitly
879 // expressed in the clast and do not need to be copied.
880 if (Inst->isTerminator())
881 return;
882
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000883 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000884 return;
885
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000886 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000887 generateLoad(Stmt, Load, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000888 return;
889 }
890
891 if (hasVectorOperands(Inst, VectorMap)) {
892 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000893 copyStore(Stmt, Store, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000894 return;
895 }
896
897 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000898 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000899 return;
900 }
901
902 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000903 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000904 return;
905 }
906
907 // Falltrough: We generate scalar instructions, if we don't know how to
908 // generate vector code.
909 }
910
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000911 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000912}
913
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000914void VectorBlockGenerator::copyStmt(
915 ScopStmt &Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000916 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
917 "the vector block generator");
918
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000919 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000920 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000921 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000922 CopyBB->setName("polly.stmt." + BB->getName());
923 Builder.SetInsertPoint(CopyBB->begin());
924
925 // Create two maps that store the mapping from the original instructions of
926 // the old basic block to their copies in the new basic block. Those maps
927 // are basic block local.
928 //
929 // As vector code generation is supported there is one map for scalar values
930 // and one for vector values.
931 //
932 // In case we just do scalar code generation, the vectorMap is not used and
933 // the scalarMap has just one dimension, which contains the mapping.
934 //
935 // In case vector code generation is done, an instruction may either appear
936 // in the vector map once (as it is calculating >vectorwidth< values at a
937 // time. Or (if the values are calculated using scalar operations), it
938 // appears once in every dimension of the scalarMap.
939 VectorValueMapT ScalarBlockMap(getVectorWidth());
940 ValueMapT VectorBlockMap;
941
Tobias Grosser91f5b262014-06-04 08:06:40 +0000942 for (Instruction &Inst : *BB)
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000943 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap, NewAccesses);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000944}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000945
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000946BasicBlock *RegionGenerator::repairDominance(BasicBlock *BB,
947 BasicBlock *BBCopy) {
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000948
949 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
950 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
951
952 if (BBCopyIDom)
953 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
954
955 return BBCopyIDom;
956}
957
Johannes Doerfert275a1752015-02-24 16:16:32 +0000958void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +0000959 LoopToScevMapT &LTS,
960 isl_id_to_ast_expr *IdToAstExp) {
Johannes Doerfert275a1752015-02-24 16:16:32 +0000961 assert(Stmt.isRegionStmt() &&
Tobias Grosserd3f21832015-08-01 06:26:51 +0000962 "Only region statements can be copied by the region generator");
Johannes Doerfert275a1752015-02-24 16:16:32 +0000963
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000964 // Forget all old mappings.
965 BlockMap.clear();
966 RegionMaps.clear();
967 IncompletePHINodeMap.clear();
968
Johannes Doerfert275a1752015-02-24 16:16:32 +0000969 // The region represented by the statement.
970 Region *R = Stmt.getRegion();
971
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000972 // Create a dedicated entry for the region where we can reload all demoted
973 // inputs.
974 BasicBlock *EntryBB = R->getEntry();
975 BasicBlock *EntryBBCopy =
976 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
977 EntryBBCopy->setName("polly.stmt." + EntryBB->getName() + ".entry");
978 Builder.SetInsertPoint(EntryBBCopy->begin());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000979
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000980 for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI)
981 if (!R->contains(*PI))
982 BlockMap[*PI] = EntryBBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000983
984 // Iterate over all blocks in the region in a breadth-first search.
985 std::deque<BasicBlock *> Blocks;
986 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000987 Blocks.push_back(EntryBB);
988 SeenBlocks.insert(EntryBB);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000989
990 while (!Blocks.empty()) {
991 BasicBlock *BB = Blocks.front();
992 Blocks.pop_front();
993
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000994 // First split the block and update dominance information.
995 BasicBlock *BBCopy = splitBB(BB);
Johannes Doerfertecff11d2015-05-22 23:43:58 +0000996 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy);
997
998 // In order to remap PHI nodes we store also basic block mappings.
999 BlockMap[BB] = BBCopy;
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001000
1001 // Get the mapping for this block and initialize it with the mapping
1002 // available at its immediate dominator (in the new region).
1003 ValueMapT &RegionMap = RegionMaps[BBCopy];
1004 RegionMap = RegionMaps[BBCopyIDom];
1005
Johannes Doerfert275a1752015-02-24 16:16:32 +00001006 // Copy the block with the BlockGenerator.
Tobias Grosser2d1ed0b2015-08-27 07:28:16 +00001007 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS, IdToAstExp);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001008
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001009 // In order to remap PHI nodes we store also basic block mappings.
1010 BlockMap[BB] = BBCopy;
1011
1012 // Add values to incomplete PHI nodes waiting for this block to be copied.
1013 for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB])
1014 addOperandToPHI(Stmt, PHINodePair.first, PHINodePair.second, BB,
1015 GlobalMap, LTS);
1016 IncompletePHINodeMap[BB].clear();
1017
Johannes Doerfert275a1752015-02-24 16:16:32 +00001018 // And continue with new successors inside the region.
1019 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
1020 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
1021 Blocks.push_back(*SI);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001022 }
1023
1024 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001025 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +00001026 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001027 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit");
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001028 BlockMap[R->getExit()] = ExitBBCopy;
1029
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001030 repairDominance(R->getExit(), ExitBBCopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001031
1032 // As the block generator doesn't handle control flow we need to add the
1033 // region control flow by hand after all blocks have been copied.
1034 for (BasicBlock *BB : SeenBlocks) {
1035
1036 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
1037
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001038 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +00001039 Instruction *BICopy = BBCopy->getTerminator();
1040
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001041 ValueMapT &RegionMap = RegionMaps[BBCopy];
1042 RegionMap.insert(BlockMap.begin(), BlockMap.end());
1043
Tobias Grosser45e79442015-08-01 09:07:57 +00001044 Builder.SetInsertPoint(BICopy);
Johannes Doerfert275a1752015-02-24 16:16:32 +00001045 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
1046 BICopy->eraseFromParent();
1047 }
1048
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001049 // Add counting PHI nodes to all loops in the region that can be used as
1050 // replacement for SCEVs refering to the old loop.
1051 for (BasicBlock *BB : SeenBlocks) {
1052 Loop *L = LI.getLoopFor(BB);
1053 if (L == nullptr || L->getHeader() != BB)
1054 continue;
1055
1056 BasicBlock *BBCopy = BlockMap[BB];
1057 Value *NullVal = Builder.getInt32(0);
1058 PHINode *LoopPHI =
1059 PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv");
1060 Instruction *LoopPHIInc = BinaryOperator::CreateAdd(
1061 LoopPHI, Builder.getInt32(1), "polly.subregion.iv.inc");
1062 LoopPHI->insertBefore(BBCopy->begin());
1063 LoopPHIInc->insertBefore(BBCopy->getTerminator());
1064
1065 for (auto *PredBB : make_range(pred_begin(BB), pred_end(BB))) {
1066 if (!R->contains(PredBB))
1067 continue;
1068 if (L->contains(PredBB))
1069 LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]);
1070 else
1071 LoopPHI->addIncoming(NullVal, BlockMap[PredBB]);
1072 }
1073
1074 for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy)))
1075 if (LoopPHI->getBasicBlockIndex(PredBBCopy) < 0)
1076 LoopPHI->addIncoming(NullVal, PredBBCopy);
1077
1078 LTS[L] = SE.getUnknown(LoopPHI);
1079 }
1080
1081 // Add all mappings from the region to the global map so outside uses will use
1082 // the copied instructions.
1083 for (auto &BBMap : RegionMaps)
1084 GlobalMap.insert(BBMap.second.begin(), BBMap.second.end());
1085
Johannes Doerfert275a1752015-02-24 16:16:32 +00001086 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +00001087 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +00001088}
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001089
1090void RegionGenerator::generateScalarLoads(ScopStmt &Stmt,
1091 const Instruction *Inst,
1092 ValueMapT &BBMap) {
1093
1094 // Inside a non-affine region PHI nodes are copied not demoted. Once the
1095 // phi is copied it will reload all inputs from outside the region, hence
1096 // we do not need to generate code for the read access of the operands of a
1097 // PHI.
1098 if (isa<PHINode>(Inst))
1099 return;
1100
1101 return BlockGenerator::generateScalarLoads(Stmt, Inst, BBMap);
1102}
1103
1104void RegionGenerator::generateScalarStores(ScopStmt &Stmt, BasicBlock *BB,
1105 ValueMapT &BBMap,
1106 ValueMapT &GlobalMap) {
1107 const Region &R = Stmt.getParent()->getRegion();
1108
Tobias Grosser75296902015-08-21 19:23:21 +00001109 assert(Stmt.getRegion() &&
1110 "Block statements need to use the generateScalarStores() "
1111 "function in the BlockGenerator");
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001112
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001113 for (MemoryAccess *MA : Stmt) {
1114
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001115 if (!MA->isScalar() || MA->isRead())
1116 continue;
1117
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001118 Instruction *ScalarInst = MA->getAccessInstruction();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001119
Tobias Grosser62139132015-08-02 16:17:41 +00001120 // Only generate accesses that belong to this basic block.
1121 if (ScalarInst->getParent() != BB)
1122 continue;
1123
Johannes Doerfertd86f2152015-08-17 10:58:17 +00001124 Value *Val = MA->getAccessValue();
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001125
Tobias Grosserf8d55f72015-08-29 18:12:03 +00001126 auto Address = getOrCreateAlloca(*MA);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001127
Tobias Grosser92245222015-07-28 14:53:44 +00001128 Val = getNewScalarValue(Val, R, ScalarMap, BBMap, GlobalMap);
Tobias Grosserf8d55f72015-08-29 18:12:03 +00001129 Builder.CreateStore(Val, Address);
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001130 }
1131}
1132
1133void RegionGenerator::addOperandToPHI(ScopStmt &Stmt, const PHINode *PHI,
1134 PHINode *PHICopy, BasicBlock *IncomingBB,
1135 ValueMapT &GlobalMap,
1136 LoopToScevMapT &LTS) {
1137 Region *StmtR = Stmt.getRegion();
1138
1139 // If the incoming block was not yet copied mark this PHI as incomplete.
1140 // Once the block will be copied the incoming value will be added.
1141 BasicBlock *BBCopy = BlockMap[IncomingBB];
1142 if (!BBCopy) {
1143 assert(StmtR->contains(IncomingBB) &&
1144 "Bad incoming block for PHI in non-affine region");
1145 IncompletePHINodeMap[IncomingBB].push_back(std::make_pair(PHI, PHICopy));
1146 return;
1147 }
1148
1149 Value *OpCopy = nullptr;
1150 if (StmtR->contains(IncomingBB)) {
1151 assert(RegionMaps.count(BBCopy) &&
1152 "Incoming PHI block did not have a BBMap");
1153 ValueMapT &BBCopyMap = RegionMaps[BBCopy];
1154
1155 Value *Op = PHI->getIncomingValueForBlock(IncomingBB);
1156 OpCopy =
1157 getNewValue(Stmt, Op, BBCopyMap, GlobalMap, LTS, getLoopForInst(PHI));
1158 } else {
1159
1160 if (PHICopy->getBasicBlockIndex(BBCopy) >= 0)
1161 return;
1162
Tobias Grosserb79a67d2015-08-28 08:23:35 +00001163 AllocaInst *PHIOpAddr = getOrCreatePHIAlloca(const_cast<PHINode *>(PHI));
Johannes Doerfertecff11d2015-05-22 23:43:58 +00001164 OpCopy = new LoadInst(PHIOpAddr, PHIOpAddr->getName() + ".reload",
1165 BlockMap[IncomingBB]->getTerminator());
1166 }
1167
1168 assert(OpCopy && "Incoming PHI value was not copied properly");
1169 assert(BBCopy && "Incoming PHI block was not copied properly");
1170 PHICopy->addIncoming(OpCopy, BBCopy);
1171}
1172
1173Value *RegionGenerator::copyPHIInstruction(ScopStmt &Stmt, const PHINode *PHI,
1174 ValueMapT &BBMap,
1175 ValueMapT &GlobalMap,
1176 LoopToScevMapT &LTS) {
1177 unsigned NumIncoming = PHI->getNumIncomingValues();
1178 PHINode *PHICopy =
1179 Builder.CreatePHI(PHI->getType(), NumIncoming, "polly." + PHI->getName());
1180 PHICopy->moveBefore(PHICopy->getParent()->getFirstNonPHI());
1181 BBMap[PHI] = PHICopy;
1182
1183 for (unsigned u = 0; u < NumIncoming; u++)
1184 addOperandToPHI(Stmt, PHI, PHICopy, PHI->getIncomingBlock(u), GlobalMap,
1185 LTS);
1186 return PHICopy;
1187}