blob: ec595d5533979383ed6abc69c7d3901b9b654a4e [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"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000031#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000032
Johannes Doerfertf32d6512015-03-01 18:45:58 +000033#include "isl/aff.h"
34#include "isl/ast.h"
35#include "isl/set.h"
36#include "isl/ast_build.h"
37
38#include <deque>
39
Hongbin Zheng3b11a162012-04-25 13:16:49 +000040using namespace llvm;
41using namespace polly;
42
Tobias Grosser878aba42014-10-22 23:22:41 +000043static cl::opt<bool> Aligned("enable-polly-aligned",
44 cl::desc("Assumed aligned memory accesses."),
45 cl::Hidden, cl::init(false), cl::ZeroOrMore,
46 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000047
Tobias Grosserecfe21b2013-03-20 18:03:18 +000048bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
49 ScalarEvolution *SE, const Region *R) {
Tobias Grosser683b8e42014-11-30 14:33:31 +000050 if (!I || !SE->isSCEVable(I->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000051 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000052
Tobias Grosser683b8e42014-11-30 14:33:31 +000053 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
54 if (!isa<SCEVCouldNotCompute>(Scev))
55 if (!hasScalarDepsInsideRegion(Scev, R))
56 return true;
57
58 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000059}
60
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000061bool polly::isIgnoredIntrinsic(const Value *V) {
62 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
63 switch (IT->getIntrinsicID()) {
64 // Lifetime markers are supported/ignored.
65 case llvm::Intrinsic::lifetime_start:
66 case llvm::Intrinsic::lifetime_end:
67 // Invariant markers are supported/ignored.
68 case llvm::Intrinsic::invariant_start:
69 case llvm::Intrinsic::invariant_end:
70 // Some misc annotations are supported/ignored.
71 case llvm::Intrinsic::var_annotation:
72 case llvm::Intrinsic::ptr_annotation:
73 case llvm::Intrinsic::annotation:
74 case llvm::Intrinsic::donothing:
75 case llvm::Intrinsic::assume:
76 case llvm::Intrinsic::expect:
77 return true;
78 default:
79 break;
80 }
81 }
82 return false;
83}
84
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000085BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
86 ScalarEvolution &SE, DominatorTree &DT,
87 IslExprBuilder *ExprBuilder)
88 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000089
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000090Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
91 ValueMapT &BBMap, ValueMapT &GlobalMap,
92 LoopToScevMapT &LTS, Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000093 // We assume constants never change.
94 // This avoids map lookups for many calls to this function.
95 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000096 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000097
Hongbin Zhengfe11e282013-06-29 13:22:15 +000098 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +000099 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +0000100 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000101 New = Builder.CreateTruncOrBitCast(New, Old->getType());
102
103 return New;
104 }
105
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000106 if (Value *New = BBMap.lookup(Old))
107 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000108
Tobias Grosser683b8e42014-11-30 14:33:31 +0000109 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000110 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000111 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000112 const SCEV *NewScev = apply(Scev, LTS, SE);
113 ValueToValueMap VTV;
114 VTV.insert(BBMap.begin(), BBMap.end());
115 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000116 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000117 SCEVExpander Expander(SE, "polly");
118 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
119 Builder.GetInsertPoint());
120
121 BBMap[Old] = Expanded;
122 return Expanded;
123 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000124 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000125
Tobias Grosser16371ac2014-11-05 20:48:56 +0000126 // A scop-constant value defined by a global or a function parameter.
127 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
128 return const_cast<Value *>(Old);
129
130 // A scop-constant value defined by an instruction executed outside the scop.
131 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000132 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000133 return const_cast<Value *>(Old);
134
135 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000136 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000137 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000138}
139
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000140void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
141 ValueMapT &BBMap, ValueMapT &GlobalMap,
142 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000143 // We do not generate debug intrinsics as we did not investigate how to
144 // copy them correctly. At the current state, they just crash the code
145 // generation as the meta-data operands are not correctly copied.
146 if (isa<DbgInfoIntrinsic>(Inst))
147 return;
148
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000149 Instruction *NewInst = Inst->clone();
150
151 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000152 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000153 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
154 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000155
156 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000157 assert(!isa<StoreInst>(NewInst) &&
158 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000159 delete NewInst;
160 return;
161 }
162
163 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
164 }
165
166 Builder.Insert(NewInst);
167 BBMap[Inst] = NewInst;
168
169 if (!NewInst->getType()->isVoidTy())
170 NewInst->setName("p_" + Inst->getName());
171}
172
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000173Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt,
174 const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000175 isl_pw_multi_aff *PWAccRel;
176 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000177 isl_ast_expr *Expr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000178 isl_ast_build *Build = Stmt.getAstBuild();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000179
Johannes Doerferta63b2572014-08-03 01:51:59 +0000180 assert(ExprBuilder && Build &&
181 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000182
Johannes Doerferta99130f2014-10-13 12:58:03 +0000183 Schedule = isl_ast_build_get_schedule(Build);
184 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000185
Johannes Doerferta63b2572014-08-03 01:51:59 +0000186 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000187 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000188
189 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000190}
191
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000192Value *BlockGenerator::generateLocationAccessed(
193 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
194 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
195 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000196
197 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000198 if (MA.hasNewAccessRelation())
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000199 NewPointer = getNewAccessOperand(Stmt, MA);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000200 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000201 NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000202 getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000203
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000204 return NewPointer;
205}
206
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000207Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000208 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000209}
210
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000211Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000212 ValueMapT &BBMap,
213 ValueMapT &GlobalMap,
214 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000215 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000216 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000217 generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000218 Value *ScalarLoad = Builder.CreateAlignedLoad(
219 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000220 return ScalarLoad;
221}
222
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000223Value *BlockGenerator::generateScalarStore(ScopStmt &Stmt,
224 const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000225 ValueMapT &BBMap,
226 ValueMapT &GlobalMap,
227 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000228 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000229 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000230 generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS);
231 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
232 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000233
Johannes Doerfert87901452014-10-02 16:22:19 +0000234 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
235 Store->getAlignment());
236 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000237}
238
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000239void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
240 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000241 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000242 // Terminator instructions control the control flow. They are explicitly
243 // expressed in the clast and do not need to be copied.
244 if (Inst->isTerminator())
245 return;
246
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000247 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000248 return;
249
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000250 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000251 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000252 // Compute NewLoad before its insertion in BBMap to make the insertion
253 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000254 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000255 return;
256 }
257
258 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000259 Value *NewStore = generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000260 // Compute NewStore before its insertion in BBMap to make the insertion
261 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000262 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000263 return;
264 }
265
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000266 // Skip some special intrinsics for which we do not adjust the semantics to
267 // the new schedule. All others are handled like every other instruction.
268 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
269 switch (IT->getIntrinsicID()) {
270 // Lifetime markers are ignored.
271 case llvm::Intrinsic::lifetime_start:
272 case llvm::Intrinsic::lifetime_end:
273 // Invariant markers are ignored.
274 case llvm::Intrinsic::invariant_start:
275 case llvm::Intrinsic::invariant_end:
276 // Some misc annotations are ignored.
277 case llvm::Intrinsic::var_annotation:
278 case llvm::Intrinsic::ptr_annotation:
279 case llvm::Intrinsic::annotation:
280 case llvm::Intrinsic::donothing:
281 case llvm::Intrinsic::assume:
282 case llvm::Intrinsic::expect:
283 return;
284 default:
285 // Other intrinsics are copied.
286 break;
287 }
288 }
289
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000290 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000291}
292
Johannes Doerfert275a1752015-02-24 16:16:32 +0000293void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
294 LoopToScevMapT &LTS) {
295 assert(Stmt.isBlockStmt() &&
296 "Only block statements can be copied by the block generator");
297
298 ValueMapT BBMap;
299
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000300 BasicBlock *BB = Stmt.getBasicBlock();
Johannes Doerfert275a1752015-02-24 16:16:32 +0000301 copyBB(Stmt, BB, BBMap, GlobalMap, LTS);
302}
303
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000304BasicBlock *BlockGenerator::splitBB(BasicBlock *BB) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000305 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000306 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000307 CopyBB->setName("polly.stmt." + BB->getName());
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000308 return CopyBB;
309}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000310
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000311BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
312 ValueMapT &BBMap, ValueMapT &GlobalMap,
313 LoopToScevMapT &LTS) {
314 BasicBlock *CopyBB = splitBB(BB);
315 copyBB(Stmt, BB, CopyBB, BBMap, GlobalMap, LTS);
316 return CopyBB;
317}
318
319void BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB, BasicBlock *CopyBB,
320 ValueMapT &BBMap, ValueMapT &GlobalMap,
321 LoopToScevMapT &LTS) {
322 Builder.SetInsertPoint(CopyBB->begin());
Tobias Grosser91f5b262014-06-04 08:06:40 +0000323 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000324 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000325}
326
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000327VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
328 VectorValueMapT &GlobalMaps,
329 std::vector<LoopToScevMapT> &VLTS,
330 isl_map *Schedule)
331 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
332 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000333 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
334 assert(Schedule && "No statement domain provided");
335}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000336
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000337Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000338 ValueMapT &VectorMap,
339 VectorValueMapT &ScalarMaps,
340 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000341 if (Value *NewValue = VectorMap.lookup(Old))
342 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000343
344 int Width = getVectorWidth();
345
346 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
347
348 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000349 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000350 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
351 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000352 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000353
354 VectorMap[Old] = Vector;
355
356 return Vector;
357}
358
359Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
360 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
361 assert(PointerTy && "PointerType expected");
362
363 Type *ScalarType = PointerTy->getElementType();
364 VectorType *VectorType = VectorType::get(ScalarType, Width);
365
366 return PointerType::getUnqual(VectorType);
367}
368
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000369Value *VectorBlockGenerator::generateStrideOneLoad(
370 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
371 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000372 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000373 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000374 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
375 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
376
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000377 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000378 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000379 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000380 Value *VectorPtr =
381 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
382 LoadInst *VecLoad =
383 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000384 if (!Aligned)
385 VecLoad->setAlignment(8);
386
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000387 if (NegativeStride) {
388 SmallVector<Constant *, 16> Indices;
389 for (int i = VectorWidth - 1; i >= 0; i--)
390 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
391 Constant *SV = llvm::ConstantVector::get(Indices);
392 Value *RevVecLoad = Builder.CreateShuffleVector(
393 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
394 return RevVecLoad;
395 }
396
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000397 return VecLoad;
398}
399
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000400Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
401 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000402 ValueMapT &BBMap) {
403 const Value *Pointer = Load->getPointerOperand();
404 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000405 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
406 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000407 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
408 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000409 LoadInst *ScalarLoad =
410 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000411
412 if (!Aligned)
413 ScalarLoad->setAlignment(8);
414
Tobias Grosserc14582f2013-02-05 18:01:29 +0000415 Constant *SplatVector = Constant::getNullValue(
416 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000417
Tobias Grosserc14582f2013-02-05 18:01:29 +0000418 Value *VectorLoad = Builder.CreateShuffleVector(
419 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000420 return VectorLoad;
421}
422
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000423Value *VectorBlockGenerator::generateUnknownStrideLoad(
424 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000425 int VectorWidth = getVectorWidth();
426 const Value *Pointer = Load->getPointerOperand();
427 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000428 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000429
430 Value *Vector = UndefValue::get(VectorType);
431
432 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000433 Value *NewPointer = generateLocationAccessed(
434 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000435 Value *ScalarLoad =
436 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
437 Vector = Builder.CreateInsertElement(
438 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000439 }
440
441 return Vector;
442}
443
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000444void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000445 ValueMapT &VectorMap,
446 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000447 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
448 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000449 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000450 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000451 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000452 return;
453 }
454
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000455 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000456
Tobias Grosser95493982014-04-18 09:46:35 +0000457 // Make sure we have scalar values available to access the pointer to
458 // the data location.
459 extractScalarValues(Load, VectorMap, ScalarMaps);
460
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000461 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000462 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000463 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000464 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000465 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000466 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000467 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000468 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000469 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000470
471 VectorMap[Load] = NewLoad;
472}
473
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000474void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
475 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000476 ValueMapT &VectorMap,
477 VectorValueMapT &ScalarMaps) {
478 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000479 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
480 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000481
482 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
483
484 const CastInst *Cast = dyn_cast<CastInst>(Inst);
485 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
486 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
487}
488
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000489void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
490 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000491 ValueMapT &VectorMap,
492 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000493 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000494 Value *OpZero = Inst->getOperand(0);
495 Value *OpOne = Inst->getOperand(1);
496
497 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000498 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
499 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000500
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000501 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000502 Inst->getName() + "p_vec");
503 VectorMap[Inst] = NewInst;
504}
505
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000506void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000507 ValueMapT &VectorMap,
508 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000509 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000510
511 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000512 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000513 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000514
Tobias Grosser50fd7012014-04-17 23:13:49 +0000515 // Make sure we have scalar values available to access the pointer to
516 // the data location.
517 extractScalarValues(Store, VectorMap, ScalarMaps);
518
Sebastian Popa00a0292012-12-18 07:46:06 +0000519 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000520 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000521 Value *NewPointer = generateLocationAccessed(
522 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000523
Tobias Grosserc14582f2013-02-05 18:01:29 +0000524 Value *VectorPtr =
525 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000526 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
527
528 if (!Aligned)
529 Store->setAlignment(8);
530 } else {
531 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000532 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000533 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000534 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000535 Builder.CreateStore(Scalar, NewPointer);
536 }
537 }
538}
539
540bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
541 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000542 for (Value *Operand : Inst->operands())
543 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000544 return true;
545 return false;
546}
547
548bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
549 ValueMapT &VectorMap,
550 VectorValueMapT &ScalarMaps) {
551 bool HasVectorOperand = false;
552 int VectorWidth = getVectorWidth();
553
Tobias Grosser91f5b262014-06-04 08:06:40 +0000554 for (Value *Operand : Inst->operands()) {
555 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000556
557 if (VecOp == VectorMap.end())
558 continue;
559
560 HasVectorOperand = true;
561 Value *NewVector = VecOp->second;
562
563 for (int i = 0; i < VectorWidth; ++i) {
564 ValueMapT &SM = ScalarMaps[i];
565
566 // If there is one scalar extracted, all scalar elements should have
567 // already been extracted by the code here. So no need to check for the
568 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000569 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000570 break;
571
Tobias Grosser91f5b262014-06-04 08:06:40 +0000572 SM[Operand] =
573 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000574 }
575 }
576
577 return HasVectorOperand;
578}
579
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000580void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
581 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000582 ValueMapT &VectorMap,
583 VectorValueMapT &ScalarMaps) {
584 bool HasVectorOperand;
585 int VectorWidth = getVectorWidth();
586
587 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
588
589 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000590 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000591 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000592
593 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
594 return;
595
596 // Make the result available as vector value.
597 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
598 Value *Vector = UndefValue::get(VectorType);
599
600 for (int i = 0; i < VectorWidth; i++)
601 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
602 Builder.getInt32(i));
603
604 VectorMap[Inst] = Vector;
605}
606
Tobias Grosserc14582f2013-02-05 18:01:29 +0000607int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000608
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000609void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
610 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000611 ValueMapT &VectorMap,
612 VectorValueMapT &ScalarMaps) {
613 // Terminator instructions control the control flow. They are explicitly
614 // expressed in the clast and do not need to be copied.
615 if (Inst->isTerminator())
616 return;
617
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000618 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000619 return;
620
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000621 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000622 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000623 return;
624 }
625
626 if (hasVectorOperands(Inst, VectorMap)) {
627 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000628 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000629 return;
630 }
631
632 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000633 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000634 return;
635 }
636
637 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000638 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000639 return;
640 }
641
642 // Falltrough: We generate scalar instructions, if we don't know how to
643 // generate vector code.
644 }
645
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000646 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000647}
648
Johannes Doerfert275a1752015-02-24 16:16:32 +0000649void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
650 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
651 "the vector block generator");
652
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000653 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000654 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000655 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000656 CopyBB->setName("polly.stmt." + BB->getName());
657 Builder.SetInsertPoint(CopyBB->begin());
658
659 // Create two maps that store the mapping from the original instructions of
660 // the old basic block to their copies in the new basic block. Those maps
661 // are basic block local.
662 //
663 // As vector code generation is supported there is one map for scalar values
664 // and one for vector values.
665 //
666 // In case we just do scalar code generation, the vectorMap is not used and
667 // the scalarMap has just one dimension, which contains the mapping.
668 //
669 // In case vector code generation is done, an instruction may either appear
670 // in the vector map once (as it is calculating >vectorwidth< values at a
671 // time. Or (if the values are calculated using scalar operations), it
672 // appears once in every dimension of the scalarMap.
673 VectorValueMapT ScalarBlockMap(getVectorWidth());
674 ValueMapT VectorBlockMap;
675
Tobias Grosser91f5b262014-06-04 08:06:40 +0000676 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000677 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000678}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000679
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000680BasicBlock *RegionGenerator::repairDominance(
681 BasicBlock *BB, BasicBlock *BBCopy,
682 DenseMap<BasicBlock *, BasicBlock *> &BlockMap) {
683
684 BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock();
685 BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom);
686
687 if (BBCopyIDom)
688 DT.changeImmediateDominator(BBCopy, BBCopyIDom);
689
690 return BBCopyIDom;
691}
692
Johannes Doerfert275a1752015-02-24 16:16:32 +0000693void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
694 LoopToScevMapT &LTS) {
695 assert(Stmt.isRegionStmt() &&
696 "Only region statements can be copied by the block generator");
697
698 // The region represented by the statement.
699 Region *R = Stmt.getRegion();
700
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000701 // The "BBMaps" for the whole region.
702 DenseMap<BasicBlock *, ValueMapT> RegionMaps;
703
704 // A map from old to new blocks in the region
705 DenseMap<BasicBlock *, BasicBlock *> BlockMap;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000706
707 // Iterate over all blocks in the region in a breadth-first search.
708 std::deque<BasicBlock *> Blocks;
709 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
710 Blocks.push_back(R->getEntry());
711 SeenBlocks.insert(R->getEntry());
712
713 while (!Blocks.empty()) {
714 BasicBlock *BB = Blocks.front();
715 Blocks.pop_front();
716
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000717 // First split the block and update dominance information.
718 BasicBlock *BBCopy = splitBB(BB);
719 BasicBlock *BBCopyIDom = repairDominance(BB, BBCopy, BlockMap);
720
721 // Get the mapping for this block and initialize it with the mapping
722 // available at its immediate dominator (in the new region).
723 ValueMapT &RegionMap = RegionMaps[BBCopy];
724 RegionMap = RegionMaps[BBCopyIDom];
725
Johannes Doerfert275a1752015-02-24 16:16:32 +0000726 // Copy the block with the BlockGenerator.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000727 copyBB(Stmt, BB, BBCopy, RegionMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000728
729 // And continue with new successors inside the region.
730 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
731 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
732 Blocks.push_back(*SI);
733
734 // In order to remap PHI nodes we store also basic block mappings.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000735 BlockMap[BB] = BBCopy;
Johannes Doerfert275a1752015-02-24 16:16:32 +0000736 }
737
738 // Now create a new dedicated region exit block and add it to the region map.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000739 BasicBlock *ExitBBCopy =
Johannes Doerfert275a1752015-02-24 16:16:32 +0000740 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000741 ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".as.exit");
742 BlockMap[R->getExit()] = ExitBBCopy;
743
744 repairDominance(R->getExit(), ExitBBCopy, BlockMap);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000745
746 // As the block generator doesn't handle control flow we need to add the
747 // region control flow by hand after all blocks have been copied.
748 for (BasicBlock *BB : SeenBlocks) {
749
750 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
751
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000752 BasicBlock *BBCopy = BlockMap[BB];
Johannes Doerfert275a1752015-02-24 16:16:32 +0000753 Instruction *BICopy = BBCopy->getTerminator();
754
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000755 ValueMapT &RegionMap = RegionMaps[BBCopy];
756 RegionMap.insert(BlockMap.begin(), BlockMap.end());
757
Johannes Doerfert275a1752015-02-24 16:16:32 +0000758 Builder.SetInsertPoint(BBCopy);
759 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
760 BICopy->eraseFromParent();
761 }
762
763 // Reset the old insert point for the build.
Johannes Doerfert514f6ef2015-02-27 18:29:04 +0000764 Builder.SetInsertPoint(ExitBBCopy->begin());
Johannes Doerfert275a1752015-02-24 16:16:32 +0000765}