blob: 0e04235aca1758487cdfb07019aeef43a7b7045c [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 Grosser28736452015-03-23 07:00:36 +0000454 if (!VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000455 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000456 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000457 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000458 return;
459 }
460
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000461 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000462
Tobias Grosser95493982014-04-18 09:46:35 +0000463 // Make sure we have scalar values available to access the pointer to
464 // the data location.
465 extractScalarValues(Load, VectorMap, ScalarMaps);
466
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000467 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000468 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000469 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000470 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000471 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000472 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000473 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000474 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000475 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000476
477 VectorMap[Load] = NewLoad;
478}
479
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000480void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
481 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000482 ValueMapT &VectorMap,
483 VectorValueMapT &ScalarMaps) {
484 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000485 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
486 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000487
488 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
489
490 const CastInst *Cast = dyn_cast<CastInst>(Inst);
491 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
492 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
493}
494
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000495void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
496 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000497 ValueMapT &VectorMap,
498 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000499 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000500 Value *OpZero = Inst->getOperand(0);
501 Value *OpOne = Inst->getOperand(1);
502
503 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000504 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
505 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000506
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000507 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000508 Inst->getName() + "p_vec");
509 VectorMap[Inst] = NewInst;
510}
511
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000512void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000513 ValueMapT &VectorMap,
514 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000515 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000516
517 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000518 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000519 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000520
Tobias Grosser50fd7012014-04-17 23:13:49 +0000521 // Make sure we have scalar values available to access the pointer to
522 // the data location.
523 extractScalarValues(Store, VectorMap, ScalarMaps);
524
Sebastian Popa00a0292012-12-18 07:46:06 +0000525 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000526 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000527 Value *NewPointer = generateLocationAccessed(
528 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000529
Tobias Grosserc14582f2013-02-05 18:01:29 +0000530 Value *VectorPtr =
531 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000532 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
533
534 if (!Aligned)
535 Store->setAlignment(8);
536 } else {
537 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000538 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000539 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000540 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000541 Builder.CreateStore(Scalar, NewPointer);
542 }
543 }
544}
545
546bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
547 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000548 for (Value *Operand : Inst->operands())
549 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000550 return true;
551 return false;
552}
553
554bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
555 ValueMapT &VectorMap,
556 VectorValueMapT &ScalarMaps) {
557 bool HasVectorOperand = false;
558 int VectorWidth = getVectorWidth();
559
Tobias Grosser91f5b262014-06-04 08:06:40 +0000560 for (Value *Operand : Inst->operands()) {
561 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000562
563 if (VecOp == VectorMap.end())
564 continue;
565
566 HasVectorOperand = true;
567 Value *NewVector = VecOp->second;
568
569 for (int i = 0; i < VectorWidth; ++i) {
570 ValueMapT &SM = ScalarMaps[i];
571
572 // If there is one scalar extracted, all scalar elements should have
573 // already been extracted by the code here. So no need to check for the
574 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000575 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000576 break;
577
Tobias Grosser91f5b262014-06-04 08:06:40 +0000578 SM[Operand] =
579 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000580 }
581 }
582
583 return HasVectorOperand;
584}
585
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000586void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
587 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000588 ValueMapT &VectorMap,
589 VectorValueMapT &ScalarMaps) {
590 bool HasVectorOperand;
591 int VectorWidth = getVectorWidth();
592
593 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
594
595 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000596 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000597 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000598
599 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
600 return;
601
602 // Make the result available as vector value.
603 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
604 Value *Vector = UndefValue::get(VectorType);
605
606 for (int i = 0; i < VectorWidth; i++)
607 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
608 Builder.getInt32(i));
609
610 VectorMap[Inst] = Vector;
611}
612
Tobias Grosserc14582f2013-02-05 18:01:29 +0000613int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000614
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000615void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
616 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000617 ValueMapT &VectorMap,
618 VectorValueMapT &ScalarMaps) {
619 // Terminator instructions control the control flow. They are explicitly
620 // expressed in the clast and do not need to be copied.
621 if (Inst->isTerminator())
622 return;
623
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000624 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000625 return;
626
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000627 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000628 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000629 return;
630 }
631
632 if (hasVectorOperands(Inst, VectorMap)) {
633 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000634 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000635 return;
636 }
637
638 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000639 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000640 return;
641 }
642
643 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000644 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000645 return;
646 }
647
648 // Falltrough: We generate scalar instructions, if we don't know how to
649 // generate vector code.
650 }
651
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000652 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000653}
654
Johannes Doerfert275a1752015-02-24 16:16:32 +0000655void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
656 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
657 "the vector block generator");
658
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000659 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000660 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000661 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000662 CopyBB->setName("polly.stmt." + BB->getName());
663 Builder.SetInsertPoint(CopyBB->begin());
664
665 // Create two maps that store the mapping from the original instructions of
666 // the old basic block to their copies in the new basic block. Those maps
667 // are basic block local.
668 //
669 // As vector code generation is supported there is one map for scalar values
670 // and one for vector values.
671 //
672 // In case we just do scalar code generation, the vectorMap is not used and
673 // the scalarMap has just one dimension, which contains the mapping.
674 //
675 // In case vector code generation is done, an instruction may either appear
676 // in the vector map once (as it is calculating >vectorwidth< values at a
677 // time. Or (if the values are calculated using scalar operations), it
678 // appears once in every dimension of the scalarMap.
679 VectorValueMapT ScalarBlockMap(getVectorWidth());
680 ValueMapT VectorBlockMap;
681
Tobias Grosser91f5b262014-06-04 08:06:40 +0000682 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000683 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000684}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000685
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000686BasicBlock *RegionGenerator::repairDominance(
687 BasicBlock *BB, BasicBlock *BBCopy,
688 DenseMap<BasicBlock *, BasicBlock *> &BlockMap) {
689
690 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
691 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
692
693 if (BBCopyIDom)
694 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
695
696 return BBCopyIDom;
697}
698
Johannes Doerfert275a1752015-02-24 16:16:32 +0000699void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
700 LoopToScevMapT &LTS) {
701 assert(Stmt.isRegionStmt() &&
702 "Only region statements can be copied by the block generator");
703
704 // The region represented by the statement.
705 Region *R = Stmt.getRegion();
706
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000707 // The "BBMaps" for the whole region.
708 DenseMap<BasicBlock *, ValueMapT> RegionMaps;
709
710 // A map from old to new blocks in the region
711 DenseMap<BasicBlock *, BasicBlock *> BlockMap;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000712
713 // Iterate over all blocks in the region in a breadth-first search.
714 std::deque<BasicBlock *> Blocks;
715 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
716 Blocks.push_back(R->getEntry());
717 SeenBlocks.insert(R->getEntry());
718
719 while (!Blocks.empty()) {
720 BasicBlock *BB = Blocks.front();
721 Blocks.pop_front();
722
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000723 // First split the block and update dominance information.
724 BasicBlock *BBCopy = splitBB(BB);
725 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy, BlockMap);
726
727 // Get the mapping for this block and initialize it with the mapping
728 // available at its immediate dominator (in the new region).
729 ValueMapT &RegionMap = RegionMaps[BBCopy];
730 RegionMap = RegionMaps[BBCopyIDom];
731
Johannes Doerfert275a1752015-02-24 16:16:32 +0000732 // Copy the block with the BlockGenerator.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000733 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000734
735 // And continue with new successors inside the region.
736 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
737 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
738 Blocks.push_back(*SI);
739
740 // In order to remap PHI nodes we store also basic block mappings.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000741 BlockMap[BB] = BBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000742 }
743
744 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000745 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +0000746 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000747 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".as.exit");
748 BlockMap[R->getExit()] = ExitBBCopy;
749
750 repairDominance(R->getExit(), ExitBBCopy, BlockMap);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000751
752 // As the block generator doesn't handle control flow we need to add the
753 // region control flow by hand after all blocks have been copied.
754 for (BasicBlock *BB : SeenBlocks) {
755
756 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
757
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000758 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +0000759 Instruction *BICopy = BBCopy->getTerminator();
760
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000761 ValueMapT &RegionMap = RegionMaps[BBCopy];
762 RegionMap.insert(BlockMap.begin(), BlockMap.end());
763
Johannes Doerfert275a1752015-02-24 16:16:32 +0000764 Builder.SetInsertPoint(BBCopy);
765 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
766 BICopy->eraseFromParent();
767 }
768
769 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000770 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +0000771}