blob: ed48e9ef6774cb6d3c80df40b2bb568a5251ffd3 [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"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000024
Tobias Grossere71c6ab2012-04-27 16:36:14 +000025#include "llvm/Analysis/LoopInfo.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000026#include "llvm/Analysis/RegionInfo.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000027#include "llvm/Analysis/ScalarEvolution.h"
28#include "llvm/Analysis/ScalarEvolutionExpander.h"
Johannes Doerfertf32d6512015-03-01 18:45:58 +000029
Tobias Grosser030237d2014-02-21 15:06:05 +000030#include "llvm/IR/IntrinsicInst.h"
Tobias Grosserc9895062015-03-10 15:24:33 +000031#include "llvm/IR/Module.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000032#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000033
Johannes Doerfertf32d6512015-03-01 18:45:58 +000034#include "isl/aff.h"
35#include "isl/ast.h"
36#include "isl/set.h"
37#include "isl/ast_build.h"
38
39#include <deque>
40
Hongbin Zheng3b11a162012-04-25 13:16:49 +000041using namespace llvm;
42using namespace polly;
43
Tobias Grosser878aba42014-10-22 23:22:41 +000044static cl::opt<bool> Aligned("enable-polly-aligned",
45 cl::desc("Assumed aligned memory accesses."),
46 cl::Hidden, cl::init(false), cl::ZeroOrMore,
47 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000048
Tobias Grosserecfe21b2013-03-20 18:03:18 +000049bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
50 ScalarEvolution *SE, const Region *R) {
Tobias Grosser683b8e42014-11-30 14:33:31 +000051 if (!I || !SE->isSCEVable(I->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000052 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000053
Tobias Grosser683b8e42014-11-30 14:33:31 +000054 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
55 if (!isa<SCEVCouldNotCompute>(Scev))
56 if (!hasScalarDepsInsideRegion(Scev, R))
57 return true;
58
59 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000060}
61
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000062bool polly::isIgnoredIntrinsic(const Value *V) {
63 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
64 switch (IT->getIntrinsicID()) {
65 // Lifetime markers are supported/ignored.
66 case llvm::Intrinsic::lifetime_start:
67 case llvm::Intrinsic::lifetime_end:
68 // Invariant markers are supported/ignored.
69 case llvm::Intrinsic::invariant_start:
70 case llvm::Intrinsic::invariant_end:
71 // Some misc annotations are supported/ignored.
72 case llvm::Intrinsic::var_annotation:
73 case llvm::Intrinsic::ptr_annotation:
74 case llvm::Intrinsic::annotation:
75 case llvm::Intrinsic::donothing:
76 case llvm::Intrinsic::assume:
77 case llvm::Intrinsic::expect:
78 return true;
79 default:
80 break;
81 }
82 }
83 return false;
84}
85
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000086BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
87 ScalarEvolution &SE, DominatorTree &DT,
88 IslExprBuilder *ExprBuilder)
89 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT) {}
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);
Tobias Grosserc9895062015-03-10 15:24:33 +0000118 SCEVExpander Expander(SE, Stmt.getParent()
119 ->getRegion()
120 .getEntry()
121 ->getParent()
122 ->getParent()
123 ->getDataLayout(),
124 "polly");
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000125 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
126 Builder.GetInsertPoint());
127
128 BBMap[Old] = Expanded;
129 return Expanded;
130 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000131 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000132
Tobias Grosser16371ac2014-11-05 20:48:56 +0000133 // A scop-constant value defined by a global or a function parameter.
134 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
135 return const_cast<Value *>(Old);
136
137 // A scop-constant value defined by an instruction executed outside the scop.
138 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000139 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000140 return const_cast<Value *>(Old);
141
142 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000143 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000144 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000145}
146
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000147void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
148 ValueMapT &BBMap, ValueMapT &GlobalMap,
149 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000150 // We do not generate debug intrinsics as we did not investigate how to
151 // copy them correctly. At the current state, they just crash the code
152 // generation as the meta-data operands are not correctly copied.
153 if (isa<DbgInfoIntrinsic>(Inst))
154 return;
155
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000156 Instruction *NewInst = Inst->clone();
157
158 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000159 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000160 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
161 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000162
163 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000164 assert(!isa<StoreInst>(NewInst) &&
165 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000166 delete NewInst;
167 return;
168 }
169
170 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
171 }
172
173 Builder.Insert(NewInst);
174 BBMap[Inst] = NewInst;
175
176 if (!NewInst->getType()->isVoidTy())
177 NewInst->setName("p_" + Inst->getName());
178}
179
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000180Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt,
181 const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000182 isl_pw_multi_aff *PWAccRel;
183 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000184 isl_ast_expr *Expr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000185 isl_ast_build *Build = Stmt.getAstBuild();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000186
Johannes Doerferta63b2572014-08-03 01:51:59 +0000187 assert(ExprBuilder && Build &&
188 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000189
Johannes Doerferta99130f2014-10-13 12:58:03 +0000190 Schedule = isl_ast_build_get_schedule(Build);
191 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000192
Johannes Doerferta63b2572014-08-03 01:51:59 +0000193 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000194 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000195
196 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000197}
198
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000199Value *BlockGenerator::generateLocationAccessed(
200 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
201 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
202 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000203
204 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000205 if (MA.hasNewAccessRelation())
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000206 NewPointer = getNewAccessOperand(Stmt, MA);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000207 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000208 NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000209 getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000210
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000211 return NewPointer;
212}
213
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000214Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000215 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000216}
217
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000218Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
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 = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000223 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000224 generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000225 Value *ScalarLoad = Builder.CreateAlignedLoad(
226 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000227 return ScalarLoad;
228}
229
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000230Value *BlockGenerator::generateScalarStore(ScopStmt &Stmt,
231 const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000232 ValueMapT &BBMap,
233 ValueMapT &GlobalMap,
234 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000235 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000236 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000237 generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS);
238 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
239 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000240
Johannes Doerfert87901452014-10-02 16:22:19 +0000241 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
242 Store->getAlignment());
243 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000244}
245
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000246void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
247 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000248 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000249 // Terminator instructions control the control flow. They are explicitly
250 // expressed in the clast and do not need to be copied.
251 if (Inst->isTerminator())
252 return;
253
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000254 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000255 return;
256
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000257 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000258 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000259 // Compute NewLoad before its insertion in BBMap to make the insertion
260 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000261 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000262 return;
263 }
264
265 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000266 Value *NewStore = generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000267 // Compute NewStore before its insertion in BBMap to make the insertion
268 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000269 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000270 return;
271 }
272
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000273 // Skip some special intrinsics for which we do not adjust the semantics to
274 // the new schedule. All others are handled like every other instruction.
275 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
276 switch (IT->getIntrinsicID()) {
277 // Lifetime markers are ignored.
278 case llvm::Intrinsic::lifetime_start:
279 case llvm::Intrinsic::lifetime_end:
280 // Invariant markers are ignored.
281 case llvm::Intrinsic::invariant_start:
282 case llvm::Intrinsic::invariant_end:
283 // Some misc annotations are ignored.
284 case llvm::Intrinsic::var_annotation:
285 case llvm::Intrinsic::ptr_annotation:
286 case llvm::Intrinsic::annotation:
287 case llvm::Intrinsic::donothing:
288 case llvm::Intrinsic::assume:
289 case llvm::Intrinsic::expect:
290 return;
291 default:
292 // Other intrinsics are copied.
293 break;
294 }
295 }
296
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000297 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000298}
299
Johannes Doerfert275a1752015-02-24 16:16:32 +0000300void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
301 LoopToScevMapT &LTS) {
302 assert(Stmt.isBlockStmt() &&
303 "Only block statements can be copied by the block generator");
304
305 ValueMapT BBMap;
306
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000307 BasicBlock *BB = Stmt.getBasicBlock();
Johannes Doerfert275a1752015-02-24 16:16:32 +0000308 copyBB(Stmt, BB, BBMap, GlobalMap, LTS);
309}
310
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000311BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000312 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000313 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000314 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000315 return CopyBB;
316}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000317
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000318BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
319 ValueMapT &BBMap, ValueMapT &GlobalMap,
320 LoopToScevMapT &LTS) {
321 BasicBlock *CopyBB = splitBB(BB);
322 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS);
323 return CopyBB;
324}
325
326void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
327 ValueMapT &BBMap, ValueMapT &GlobalMap,
328 LoopToScevMapT &LTS) {
329 Builder.SetInsertPoint(CopyBB->begin());
Tobias Grosser91f5b262014-06-04 08:06:40 +0000330 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000331 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000332}
333
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000334VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
335 VectorValueMapT &GlobalMaps,
336 std::vector<LoopToScevMapT> &VLTS,
337 isl_map *Schedule)
338 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
339 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000340 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
341 assert(Schedule && "No statement domain provided");
342}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000343
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000344Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000345 ValueMapT &VectorMap,
346 VectorValueMapT &ScalarMaps,
347 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000348 if (Value *NewValue = VectorMap.lookup(Old))
349 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000350
351 int Width = getVectorWidth();
352
353 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
354
355 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000356 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000357 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
358 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000359 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000360
361 VectorMap[Old] = Vector;
362
363 return Vector;
364}
365
366Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
367 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
368 assert(PointerTy && "PointerType expected");
369
370 Type *ScalarType = PointerTy->getElementType();
371 VectorType *VectorType = VectorType::get(ScalarType, Width);
372
373 return PointerType::getUnqual(VectorType);
374}
375
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000376Value *VectorBlockGenerator::generateStrideOneLoad(
377 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
378 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000379 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000380 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000381 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
382 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
383
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000384 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000385 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000386 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000387 Value *VectorPtr =
388 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
389 LoadInst *VecLoad =
390 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000391 if (!Aligned)
392 VecLoad->setAlignment(8);
393
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000394 if (NegativeStride) {
395 SmallVector<Constant *, 16> Indices;
396 for (int i = VectorWidth - 1; i >= 0; i--)
397 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
398 Constant *SV = llvm::ConstantVector::get(Indices);
399 Value *RevVecLoad = Builder.CreateShuffleVector(
400 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
401 return RevVecLoad;
402 }
403
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000404 return VecLoad;
405}
406
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000407Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
408 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000409 ValueMapT &BBMap) {
410 const Value *Pointer = Load->getPointerOperand();
411 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000412 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
413 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000414 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
415 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000416 LoadInst *ScalarLoad =
417 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000418
419 if (!Aligned)
420 ScalarLoad->setAlignment(8);
421
Tobias Grosserc14582f2013-02-05 18:01:29 +0000422 Constant *SplatVector = Constant::getNullValue(
423 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000424
Tobias Grosserc14582f2013-02-05 18:01:29 +0000425 Value *VectorLoad = Builder.CreateShuffleVector(
426 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000427 return VectorLoad;
428}
429
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000430Value *VectorBlockGenerator::generateUnknownStrideLoad(
431 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000432 int VectorWidth = getVectorWidth();
433 const Value *Pointer = Load->getPointerOperand();
434 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000435 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000436
437 Value *Vector = UndefValue::get(VectorType);
438
439 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000440 Value *NewPointer = generateLocationAccessed(
441 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000442 Value *ScalarLoad =
443 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
444 Vector = Builder.CreateInsertElement(
445 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000446 }
447
448 return Vector;
449}
450
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000451void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000452 ValueMapT &VectorMap,
453 VectorValueMapT &ScalarMaps) {
Tobias Grosserbb412642015-03-12 20:47:58 +0000454 if (PollyVectorizerChoice == VECTORIZER_UNROLL_ONLY ||
Hongbin Zheng68794212012-05-06 10:22:19 +0000455 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000456 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000457 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000458 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000459 return;
460 }
461
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000462 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000463
Tobias Grosser95493982014-04-18 09:46:35 +0000464 // Make sure we have scalar values available to access the pointer to
465 // the data location.
466 extractScalarValues(Load, VectorMap, ScalarMaps);
467
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000468 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000469 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000470 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000471 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000472 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000473 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000474 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000475 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000476 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000477
478 VectorMap[Load] = NewLoad;
479}
480
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000481void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
482 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000483 ValueMapT &VectorMap,
484 VectorValueMapT &ScalarMaps) {
485 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000486 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
487 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000488
489 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
490
491 const CastInst *Cast = dyn_cast<CastInst>(Inst);
492 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
493 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
494}
495
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000496void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
497 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000498 ValueMapT &VectorMap,
499 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000500 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000501 Value *OpZero = Inst->getOperand(0);
502 Value *OpOne = Inst->getOperand(1);
503
504 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000505 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
506 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000507
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000508 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000509 Inst->getName() + "p_vec");
510 VectorMap[Inst] = NewInst;
511}
512
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000513void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000514 ValueMapT &VectorMap,
515 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000516 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000517
518 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000519 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000520 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000521
Tobias Grosser50fd7012014-04-17 23:13:49 +0000522 // Make sure we have scalar values available to access the pointer to
523 // the data location.
524 extractScalarValues(Store, VectorMap, ScalarMaps);
525
Sebastian Popa00a0292012-12-18 07:46:06 +0000526 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000527 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000528 Value *NewPointer = generateLocationAccessed(
529 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000530
Tobias Grosserc14582f2013-02-05 18:01:29 +0000531 Value *VectorPtr =
532 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000533 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
534
535 if (!Aligned)
536 Store->setAlignment(8);
537 } else {
538 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000539 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000540 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000541 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000542 Builder.CreateStore(Scalar, NewPointer);
543 }
544 }
545}
546
547bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
548 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000549 for (Value *Operand : Inst->operands())
550 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000551 return true;
552 return false;
553}
554
555bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
556 ValueMapT &VectorMap,
557 VectorValueMapT &ScalarMaps) {
558 bool HasVectorOperand = false;
559 int VectorWidth = getVectorWidth();
560
Tobias Grosser91f5b262014-06-04 08:06:40 +0000561 for (Value *Operand : Inst->operands()) {
562 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000563
564 if (VecOp == VectorMap.end())
565 continue;
566
567 HasVectorOperand = true;
568 Value *NewVector = VecOp->second;
569
570 for (int i = 0; i < VectorWidth; ++i) {
571 ValueMapT &SM = ScalarMaps[i];
572
573 // If there is one scalar extracted, all scalar elements should have
574 // already been extracted by the code here. So no need to check for the
575 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000576 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000577 break;
578
Tobias Grosser91f5b262014-06-04 08:06:40 +0000579 SM[Operand] =
580 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000581 }
582 }
583
584 return HasVectorOperand;
585}
586
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000587void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
588 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000589 ValueMapT &VectorMap,
590 VectorValueMapT &ScalarMaps) {
591 bool HasVectorOperand;
592 int VectorWidth = getVectorWidth();
593
594 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
595
596 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000597 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000598 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000599
600 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
601 return;
602
603 // Make the result available as vector value.
604 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
605 Value *Vector = UndefValue::get(VectorType);
606
607 for (int i = 0; i < VectorWidth; i++)
608 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
609 Builder.getInt32(i));
610
611 VectorMap[Inst] = Vector;
612}
613
Tobias Grosserc14582f2013-02-05 18:01:29 +0000614int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000615
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000616void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
617 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000618 ValueMapT &VectorMap,
619 VectorValueMapT &ScalarMaps) {
620 // Terminator instructions control the control flow. They are explicitly
621 // expressed in the clast and do not need to be copied.
622 if (Inst->isTerminator())
623 return;
624
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000625 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000626 return;
627
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000628 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000629 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000630 return;
631 }
632
633 if (hasVectorOperands(Inst, VectorMap)) {
634 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000635 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000636 return;
637 }
638
639 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000640 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000641 return;
642 }
643
644 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000645 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000646 return;
647 }
648
649 // Falltrough: We generate scalar instructions, if we don't know how to
650 // generate vector code.
651 }
652
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000653 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000654}
655
Johannes Doerfert275a1752015-02-24 16:16:32 +0000656void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
657 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
658 "the vector block generator");
659
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000660 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000661 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000662 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000663 CopyBB->setName("polly.stmt." + BB->getName());
664 Builder.SetInsertPoint(CopyBB->begin());
665
666 // Create two maps that store the mapping from the original instructions of
667 // the old basic block to their copies in the new basic block. Those maps
668 // are basic block local.
669 //
670 // As vector code generation is supported there is one map for scalar values
671 // and one for vector values.
672 //
673 // In case we just do scalar code generation, the vectorMap is not used and
674 // the scalarMap has just one dimension, which contains the mapping.
675 //
676 // In case vector code generation is done, an instruction may either appear
677 // in the vector map once (as it is calculating >vectorwidth< values at a
678 // time. Or (if the values are calculated using scalar operations), it
679 // appears once in every dimension of the scalarMap.
680 VectorValueMapT ScalarBlockMap(getVectorWidth());
681 ValueMapT VectorBlockMap;
682
Tobias Grosser91f5b262014-06-04 08:06:40 +0000683 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000684 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000685}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000686
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000687BasicBlock *RegionGenerator::repairDominance(
688 BasicBlock *BB, BasicBlock *BBCopy,
689 DenseMap<BasicBlock *, BasicBlock *> &BlockMap) {
690
691 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
692 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
693
694 if (BBCopyIDom)
695 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
696
697 return BBCopyIDom;
698}
699
Johannes Doerfert275a1752015-02-24 16:16:32 +0000700void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
701 LoopToScevMapT &LTS) {
702 assert(Stmt.isRegionStmt() &&
703 "Only region statements can be copied by the block generator");
704
705 // The region represented by the statement.
706 Region *R = Stmt.getRegion();
707
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000708 // The "BBMaps" for the whole region.
709 DenseMap<BasicBlock *, ValueMapT> RegionMaps;
710
711 // A map from old to new blocks in the region
712 DenseMap<BasicBlock *, BasicBlock *> BlockMap;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000713
714 // Iterate over all blocks in the region in a breadth-first search.
715 std::deque<BasicBlock *> Blocks;
716 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
717 Blocks.push_back(R->getEntry());
718 SeenBlocks.insert(R->getEntry());
719
720 while (!Blocks.empty()) {
721 BasicBlock *BB = Blocks.front();
722 Blocks.pop_front();
723
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000724 // First split the block and update dominance information.
725 BasicBlock *BBCopy = splitBB(BB);
726 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy, BlockMap);
727
728 // Get the mapping for this block and initialize it with the mapping
729 // available at its immediate dominator (in the new region).
730 ValueMapT &RegionMap = RegionMaps[BBCopy];
731 RegionMap = RegionMaps[BBCopyIDom];
732
Johannes Doerfert275a1752015-02-24 16:16:32 +0000733 // Copy the block with the BlockGenerator.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000734 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000735
736 // And continue with new successors inside the region.
737 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
738 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
739 Blocks.push_back(*SI);
740
741 // In order to remap PHI nodes we store also basic block mappings.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000742 BlockMap[BB] = BBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000743 }
744
745 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000746 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +0000747 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000748 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".as.exit");
749 BlockMap[R->getExit()] = ExitBBCopy;
750
751 repairDominance(R->getExit(), ExitBBCopy, BlockMap);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000752
753 // As the block generator doesn't handle control flow we need to add the
754 // region control flow by hand after all blocks have been copied.
755 for (BasicBlock *BB : SeenBlocks) {
756
757 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
758
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000759 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +0000760 Instruction *BICopy = BBCopy->getTerminator();
761
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000762 ValueMapT &RegionMap = RegionMaps[BBCopy];
763 RegionMap.insert(BlockMap.begin(), BlockMap.end());
764
Johannes Doerfert275a1752015-02-24 16:16:32 +0000765 Builder.SetInsertPoint(BBCopy);
766 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
767 BICopy->eraseFromParent();
768 }
769
770 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000771 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +0000772}