blob: e1926ef6108af7660f962983d35992ea8f71cb6b [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"
Tobias Grosser83628182013-05-07 08:11:54 +000017#include "isl/aff.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000018#include "isl/ast.h"
19#include "isl/ast_build.h"
Tobias Grosser83628182013-05-07 08:11:54 +000020#include "isl/set.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000021#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000022#include "polly/CodeGen/CodeGeneration.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000023#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000024#include "polly/Options.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000025#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000026#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000027#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000028#include "llvm/Analysis/LoopInfo.h"
29#include "llvm/Analysis/ScalarEvolution.h"
30#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser030237d2014-02-21 15:06:05 +000031#include "llvm/IR/IntrinsicInst.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000032#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000033
Hongbin Zheng3b11a162012-04-25 13:16:49 +000034using namespace llvm;
35using namespace polly;
36
Tobias Grosser878aba42014-10-22 23:22:41 +000037static cl::opt<bool> Aligned("enable-polly-aligned",
38 cl::desc("Assumed aligned memory accesses."),
39 cl::Hidden, cl::init(false), cl::ZeroOrMore,
40 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000041
Tobias Grosserecfe21b2013-03-20 18:03:18 +000042bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
43 ScalarEvolution *SE, const Region *R) {
Tobias Grosser683b8e42014-11-30 14:33:31 +000044 if (!I || !SE->isSCEVable(I->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000045 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000046
Tobias Grosser683b8e42014-11-30 14:33:31 +000047 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
48 if (!isa<SCEVCouldNotCompute>(Scev))
49 if (!hasScalarDepsInsideRegion(Scev, R))
50 return true;
51
52 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000053}
54
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000055bool polly::isIgnoredIntrinsic(const Value *V) {
56 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
57 switch (IT->getIntrinsicID()) {
58 // Lifetime markers are supported/ignored.
59 case llvm::Intrinsic::lifetime_start:
60 case llvm::Intrinsic::lifetime_end:
61 // Invariant markers are supported/ignored.
62 case llvm::Intrinsic::invariant_start:
63 case llvm::Intrinsic::invariant_end:
64 // Some misc annotations are supported/ignored.
65 case llvm::Intrinsic::var_annotation:
66 case llvm::Intrinsic::ptr_annotation:
67 case llvm::Intrinsic::annotation:
68 case llvm::Intrinsic::donothing:
69 case llvm::Intrinsic::assume:
70 case llvm::Intrinsic::expect:
71 return true;
72 default:
73 break;
74 }
75 }
76 return false;
77}
78
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000079BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
80 ScalarEvolution &SE, DominatorTree &DT,
81 IslExprBuilder *ExprBuilder)
82 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000083
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000084Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
85 ValueMapT &BBMap, ValueMapT &GlobalMap,
86 LoopToScevMapT &LTS, Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000087 // We assume constants never change.
88 // This avoids map lookups for many calls to this function.
89 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000090 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000091
Hongbin Zhengfe11e282013-06-29 13:22:15 +000092 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +000093 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +000094 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +000095 New = Builder.CreateTruncOrBitCast(New, Old->getType());
96
97 return New;
98 }
99
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000100 if (Value *New = BBMap.lookup(Old))
101 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000102
Tobias Grosser683b8e42014-11-30 14:33:31 +0000103 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000104 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000105 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000106 const SCEV *NewScev = apply(Scev, LTS, SE);
107 ValueToValueMap VTV;
108 VTV.insert(BBMap.begin(), BBMap.end());
109 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000110 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000111 SCEVExpander Expander(SE, "polly");
112 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
113 Builder.GetInsertPoint());
114
115 BBMap[Old] = Expanded;
116 return Expanded;
117 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000118 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000119
Tobias Grosser16371ac2014-11-05 20:48:56 +0000120 // A scop-constant value defined by a global or a function parameter.
121 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
122 return const_cast<Value *>(Old);
123
124 // A scop-constant value defined by an instruction executed outside the scop.
125 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000126 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000127 return const_cast<Value *>(Old);
128
129 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000130 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000131 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000132}
133
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000134void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
135 ValueMapT &BBMap, ValueMapT &GlobalMap,
136 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000137 // We do not generate debug intrinsics as we did not investigate how to
138 // copy them correctly. At the current state, they just crash the code
139 // generation as the meta-data operands are not correctly copied.
140 if (isa<DbgInfoIntrinsic>(Inst))
141 return;
142
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000143 Instruction *NewInst = Inst->clone();
144
145 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000146 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000147 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
148 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000149
150 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000151 assert(!isa<StoreInst>(NewInst) &&
152 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000153 delete NewInst;
154 return;
155 }
156
157 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
158 }
159
160 Builder.Insert(NewInst);
161 BBMap[Inst] = NewInst;
162
163 if (!NewInst->getType()->isVoidTy())
164 NewInst->setName("p_" + Inst->getName());
165}
166
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000167Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt,
168 const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000169 isl_pw_multi_aff *PWAccRel;
170 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000171 isl_ast_expr *Expr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000172 isl_ast_build *Build = Stmt.getAstBuild();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000173
Johannes Doerferta63b2572014-08-03 01:51:59 +0000174 assert(ExprBuilder && Build &&
175 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000176
Johannes Doerferta99130f2014-10-13 12:58:03 +0000177 Schedule = isl_ast_build_get_schedule(Build);
178 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000179
Johannes Doerferta63b2572014-08-03 01:51:59 +0000180 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000181 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000182
183 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000184}
185
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000186Value *BlockGenerator::generateLocationAccessed(
187 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
188 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
189 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000190
191 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000192 if (MA.hasNewAccessRelation())
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000193 NewPointer = getNewAccessOperand(Stmt, MA);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000194 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000195 NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000196 getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000197
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000198 return NewPointer;
199}
200
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000201Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000202 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000203}
204
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000205Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000206 ValueMapT &BBMap,
207 ValueMapT &GlobalMap,
208 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000209 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000210 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000211 generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000212 Value *ScalarLoad = Builder.CreateAlignedLoad(
213 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000214 return ScalarLoad;
215}
216
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000217Value *BlockGenerator::generateScalarStore(ScopStmt &Stmt,
218 const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000219 ValueMapT &BBMap,
220 ValueMapT &GlobalMap,
221 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000222 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000223 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000224 generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS);
225 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
226 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000227
Johannes Doerfert87901452014-10-02 16:22:19 +0000228 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
229 Store->getAlignment());
230 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000231}
232
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000233void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
234 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000235 LoopToScevMapT &LTS) {
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 Doerfert1ef52332015-02-08 20:50:42 +0000241 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000242 return;
243
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000244 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000245 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000246 // Compute NewLoad before its insertion in BBMap to make the insertion
247 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000248 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000249 return;
250 }
251
252 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000253 Value *NewStore = generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000254 // Compute NewStore before its insertion in BBMap to make the insertion
255 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000256 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000257 return;
258 }
259
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000260 // Skip some special intrinsics for which we do not adjust the semantics to
261 // the new schedule. All others are handled like every other instruction.
262 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
263 switch (IT->getIntrinsicID()) {
264 // Lifetime markers are ignored.
265 case llvm::Intrinsic::lifetime_start:
266 case llvm::Intrinsic::lifetime_end:
267 // Invariant markers are ignored.
268 case llvm::Intrinsic::invariant_start:
269 case llvm::Intrinsic::invariant_end:
270 // Some misc annotations are ignored.
271 case llvm::Intrinsic::var_annotation:
272 case llvm::Intrinsic::ptr_annotation:
273 case llvm::Intrinsic::annotation:
274 case llvm::Intrinsic::donothing:
275 case llvm::Intrinsic::assume:
276 case llvm::Intrinsic::expect:
277 return;
278 default:
279 // Other intrinsics are copied.
280 break;
281 }
282 }
283
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000284 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000285}
286
Johannes Doerfert275a1752015-02-24 16:16:32 +0000287void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
288 LoopToScevMapT &LTS) {
289 assert(Stmt.isBlockStmt() &&
290 "Only block statements can be copied by the block generator");
291
292 ValueMapT BBMap;
293
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000294 BasicBlock *BB = Stmt.getBasicBlock();
Johannes Doerfert275a1752015-02-24 16:16:32 +0000295 copyBB(Stmt, BB, BBMap, GlobalMap, LTS);
296}
297
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000298BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000299 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000300 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000301 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000302 return CopyBB;
303}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000304
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000305BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
306 ValueMapT &BBMap, ValueMapT &GlobalMap,
307 LoopToScevMapT &LTS) {
308 BasicBlock *CopyBB = splitBB(BB);
309 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS);
310 return CopyBB;
311}
312
313void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
314 ValueMapT &BBMap, ValueMapT &GlobalMap,
315 LoopToScevMapT &LTS) {
316 Builder.SetInsertPoint(CopyBB->begin());
Tobias Grosser91f5b262014-06-04 08:06:40 +0000317 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000318 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000319}
320
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000321VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
322 VectorValueMapT &GlobalMaps,
323 std::vector<LoopToScevMapT> &VLTS,
324 isl_map *Schedule)
325 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
326 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000327 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
328 assert(Schedule && "No statement domain provided");
329}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000330
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000331Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000332 ValueMapT &VectorMap,
333 VectorValueMapT &ScalarMaps,
334 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000335 if (Value *NewValue = VectorMap.lookup(Old))
336 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000337
338 int Width = getVectorWidth();
339
340 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
341
342 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000343 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000344 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
345 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000346 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000347
348 VectorMap[Old] = Vector;
349
350 return Vector;
351}
352
353Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
354 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
355 assert(PointerTy && "PointerType expected");
356
357 Type *ScalarType = PointerTy->getElementType();
358 VectorType *VectorType = VectorType::get(ScalarType, Width);
359
360 return PointerType::getUnqual(VectorType);
361}
362
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000363Value *VectorBlockGenerator::generateStrideOneLoad(
364 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
365 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000366 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000367 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000368 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
369 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
370
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000371 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000372 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000373 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000374 Value *VectorPtr =
375 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
376 LoadInst *VecLoad =
377 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000378 if (!Aligned)
379 VecLoad->setAlignment(8);
380
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000381 if (NegativeStride) {
382 SmallVector<Constant *, 16> Indices;
383 for (int i = VectorWidth - 1; i >= 0; i--)
384 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
385 Constant *SV = llvm::ConstantVector::get(Indices);
386 Value *RevVecLoad = Builder.CreateShuffleVector(
387 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
388 return RevVecLoad;
389 }
390
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000391 return VecLoad;
392}
393
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000394Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
395 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000396 ValueMapT &BBMap) {
397 const Value *Pointer = Load->getPointerOperand();
398 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000399 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
400 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000401 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
402 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000403 LoadInst *ScalarLoad =
404 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000405
406 if (!Aligned)
407 ScalarLoad->setAlignment(8);
408
Tobias Grosserc14582f2013-02-05 18:01:29 +0000409 Constant *SplatVector = Constant::getNullValue(
410 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000411
Tobias Grosserc14582f2013-02-05 18:01:29 +0000412 Value *VectorLoad = Builder.CreateShuffleVector(
413 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000414 return VectorLoad;
415}
416
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000417Value *VectorBlockGenerator::generateUnknownStrideLoad(
418 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000419 int VectorWidth = getVectorWidth();
420 const Value *Pointer = Load->getPointerOperand();
421 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000422 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000423
424 Value *Vector = UndefValue::get(VectorType);
425
426 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000427 Value *NewPointer = generateLocationAccessed(
428 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000429 Value *ScalarLoad =
430 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
431 Vector = Builder.CreateInsertElement(
432 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000433 }
434
435 return Vector;
436}
437
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000438void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000439 ValueMapT &VectorMap,
440 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000441 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
442 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000443 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000444 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000445 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000446 return;
447 }
448
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000449 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000450
Tobias Grosser95493982014-04-18 09:46:35 +0000451 // Make sure we have scalar values available to access the pointer to
452 // the data location.
453 extractScalarValues(Load, VectorMap, ScalarMaps);
454
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000455 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000456 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000457 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000458 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000459 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000460 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000461 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000462 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000463 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000464
465 VectorMap[Load] = NewLoad;
466}
467
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000468void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
469 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000470 ValueMapT &VectorMap,
471 VectorValueMapT &ScalarMaps) {
472 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000473 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
474 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000475
476 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
477
478 const CastInst *Cast = dyn_cast<CastInst>(Inst);
479 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
480 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
481}
482
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000483void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
484 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000485 ValueMapT &VectorMap,
486 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000487 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000488 Value *OpZero = Inst->getOperand(0);
489 Value *OpOne = Inst->getOperand(1);
490
491 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000492 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
493 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000494
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000495 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000496 Inst->getName() + "p_vec");
497 VectorMap[Inst] = NewInst;
498}
499
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000500void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000501 ValueMapT &VectorMap,
502 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000503 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000504
505 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000506 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000507 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000508
Tobias Grosser50fd7012014-04-17 23:13:49 +0000509 // Make sure we have scalar values available to access the pointer to
510 // the data location.
511 extractScalarValues(Store, VectorMap, ScalarMaps);
512
Sebastian Popa00a0292012-12-18 07:46:06 +0000513 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000514 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000515 Value *NewPointer = generateLocationAccessed(
516 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000517
Tobias Grosserc14582f2013-02-05 18:01:29 +0000518 Value *VectorPtr =
519 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000520 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
521
522 if (!Aligned)
523 Store->setAlignment(8);
524 } else {
525 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000526 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000527 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000528 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000529 Builder.CreateStore(Scalar, NewPointer);
530 }
531 }
532}
533
534bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
535 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000536 for (Value *Operand : Inst->operands())
537 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000538 return true;
539 return false;
540}
541
542bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
543 ValueMapT &VectorMap,
544 VectorValueMapT &ScalarMaps) {
545 bool HasVectorOperand = false;
546 int VectorWidth = getVectorWidth();
547
Tobias Grosser91f5b262014-06-04 08:06:40 +0000548 for (Value *Operand : Inst->operands()) {
549 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000550
551 if (VecOp == VectorMap.end())
552 continue;
553
554 HasVectorOperand = true;
555 Value *NewVector = VecOp->second;
556
557 for (int i = 0; i < VectorWidth; ++i) {
558 ValueMapT &SM = ScalarMaps[i];
559
560 // If there is one scalar extracted, all scalar elements should have
561 // already been extracted by the code here. So no need to check for the
562 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000563 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000564 break;
565
Tobias Grosser91f5b262014-06-04 08:06:40 +0000566 SM[Operand] =
567 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000568 }
569 }
570
571 return HasVectorOperand;
572}
573
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000574void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
575 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000576 ValueMapT &VectorMap,
577 VectorValueMapT &ScalarMaps) {
578 bool HasVectorOperand;
579 int VectorWidth = getVectorWidth();
580
581 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
582
583 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000584 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000585 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000586
587 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
588 return;
589
590 // Make the result available as vector value.
591 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
592 Value *Vector = UndefValue::get(VectorType);
593
594 for (int i = 0; i < VectorWidth; i++)
595 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
596 Builder.getInt32(i));
597
598 VectorMap[Inst] = Vector;
599}
600
Tobias Grosserc14582f2013-02-05 18:01:29 +0000601int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000602
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000603void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
604 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000605 ValueMapT &VectorMap,
606 VectorValueMapT &ScalarMaps) {
607 // Terminator instructions control the control flow. They are explicitly
608 // expressed in the clast and do not need to be copied.
609 if (Inst->isTerminator())
610 return;
611
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000612 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000613 return;
614
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000615 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000616 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000617 return;
618 }
619
620 if (hasVectorOperands(Inst, VectorMap)) {
621 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000622 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000623 return;
624 }
625
626 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000627 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000628 return;
629 }
630
631 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000632 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000633 return;
634 }
635
636 // Falltrough: We generate scalar instructions, if we don't know how to
637 // generate vector code.
638 }
639
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000640 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000641}
642
Johannes Doerfert275a1752015-02-24 16:16:32 +0000643void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
644 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
645 "the vector block generator");
646
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000647 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000648 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000649 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000650 CopyBB->setName("polly.stmt." + BB->getName());
651 Builder.SetInsertPoint(CopyBB->begin());
652
653 // Create two maps that store the mapping from the original instructions of
654 // the old basic block to their copies in the new basic block. Those maps
655 // are basic block local.
656 //
657 // As vector code generation is supported there is one map for scalar values
658 // and one for vector values.
659 //
660 // In case we just do scalar code generation, the vectorMap is not used and
661 // the scalarMap has just one dimension, which contains the mapping.
662 //
663 // In case vector code generation is done, an instruction may either appear
664 // in the vector map once (as it is calculating >vectorwidth< values at a
665 // time. Or (if the values are calculated using scalar operations), it
666 // appears once in every dimension of the scalarMap.
667 VectorValueMapT ScalarBlockMap(getVectorWidth());
668 ValueMapT VectorBlockMap;
669
Tobias Grosser91f5b262014-06-04 08:06:40 +0000670 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000671 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000672}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000673
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000674BasicBlock *RegionGenerator::repairDominance(
675 BasicBlock *BB, BasicBlock *BBCopy,
676 DenseMap<BasicBlock *, BasicBlock *> &BlockMap) {
677
678 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
679 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
680
681 if (BBCopyIDom)
682 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
683
684 return BBCopyIDom;
685}
686
Johannes Doerfert275a1752015-02-24 16:16:32 +0000687void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
688 LoopToScevMapT &LTS) {
689 assert(Stmt.isRegionStmt() &&
690 "Only region statements can be copied by the block generator");
691
692 // The region represented by the statement.
693 Region *R = Stmt.getRegion();
694
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000695 // The "BBMaps" for the whole region.
696 DenseMap<BasicBlock *, ValueMapT> RegionMaps;
697
698 // A map from old to new blocks in the region
699 DenseMap<BasicBlock *, BasicBlock *> BlockMap;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000700
701 // Iterate over all blocks in the region in a breadth-first search.
702 std::deque<BasicBlock *> Blocks;
703 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
704 Blocks.push_back(R->getEntry());
705 SeenBlocks.insert(R->getEntry());
706
707 while (!Blocks.empty()) {
708 BasicBlock *BB = Blocks.front();
709 Blocks.pop_front();
710
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000711 // First split the block and update dominance information.
712 BasicBlock *BBCopy = splitBB(BB);
713 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy, BlockMap);
714
715 // Get the mapping for this block and initialize it with the mapping
716 // available at its immediate dominator (in the new region).
717 ValueMapT &RegionMap = RegionMaps[BBCopy];
718 RegionMap = RegionMaps[BBCopyIDom];
719
Johannes Doerfert275a1752015-02-24 16:16:32 +0000720 // Copy the block with the BlockGenerator.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000721 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000722
723 // And continue with new successors inside the region.
724 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
725 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
726 Blocks.push_back(*SI);
727
728 // In order to remap PHI nodes we store also basic block mappings.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000729 BlockMap[BB] = BBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000730 }
731
732 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000733 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +0000734 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000735 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".as.exit");
736 BlockMap[R->getExit()] = ExitBBCopy;
737
738 repairDominance(R->getExit(), ExitBBCopy, BlockMap);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000739
740 // As the block generator doesn't handle control flow we need to add the
741 // region control flow by hand after all blocks have been copied.
742 for (BasicBlock *BB : SeenBlocks) {
743
744 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
745
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000746 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +0000747 Instruction *BICopy = BBCopy->getTerminator();
748
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000749 ValueMapT &RegionMap = RegionMaps[BBCopy];
750 RegionMap.insert(BlockMap.begin(), BlockMap.end());
751
Johannes Doerfert275a1752015-02-24 16:16:32 +0000752 Builder.SetInsertPoint(BBCopy);
753 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
754 BICopy->eraseFromParent();
755 }
756
757 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000758 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +0000759}