blob: 26ac56de57c78c523acbaec198b2f25016ca504c [file] [log] [blame]
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001//===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BlockGenerator and VectorBlockGenerator classes,
11// which generate sequential code and vectorized code for a polyhedral
12// statement, respectively.
13//
14//===----------------------------------------------------------------------===//
15
16#include "polly/ScopInfo.h"
Tobias Grosser83628182013-05-07 08:11:54 +000017#include "isl/aff.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000018#include "isl/ast.h"
19#include "isl/ast_build.h"
Tobias Grosser83628182013-05-07 08:11:54 +000020#include "isl/set.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000021#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000022#include "polly/CodeGen/CodeGeneration.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000023#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000024#include "polly/Options.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000025#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000026#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000027#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000028#include "llvm/Analysis/LoopInfo.h"
29#include "llvm/Analysis/ScalarEvolution.h"
30#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser030237d2014-02-21 15:06:05 +000031#include "llvm/IR/IntrinsicInst.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000032#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000033
Hongbin Zheng3b11a162012-04-25 13:16:49 +000034using namespace llvm;
35using namespace polly;
36
Tobias Grosser878aba42014-10-22 23:22:41 +000037static cl::opt<bool> Aligned("enable-polly-aligned",
38 cl::desc("Assumed aligned memory accesses."),
39 cl::Hidden, cl::init(false), cl::ZeroOrMore,
40 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000041
Tobias Grosserecfe21b2013-03-20 18:03:18 +000042bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
43 ScalarEvolution *SE, const Region *R) {
Tobias Grosser683b8e42014-11-30 14:33:31 +000044 if (!I || !SE->isSCEVable(I->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000045 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000046
Tobias Grosser683b8e42014-11-30 14:33:31 +000047 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
48 if (!isa<SCEVCouldNotCompute>(Scev))
49 if (!hasScalarDepsInsideRegion(Scev, R))
50 return true;
51
52 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000053}
54
Johannes Doerfert9e3a5db2015-01-26 15:55:54 +000055bool polly::isIgnoredIntrinsic(const Value *V) {
56 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
57 switch (IT->getIntrinsicID()) {
58 // Lifetime markers are supported/ignored.
59 case llvm::Intrinsic::lifetime_start:
60 case llvm::Intrinsic::lifetime_end:
61 // Invariant markers are supported/ignored.
62 case llvm::Intrinsic::invariant_start:
63 case llvm::Intrinsic::invariant_end:
64 // Some misc annotations are supported/ignored.
65 case llvm::Intrinsic::var_annotation:
66 case llvm::Intrinsic::ptr_annotation:
67 case llvm::Intrinsic::annotation:
68 case llvm::Intrinsic::donothing:
69 case llvm::Intrinsic::assume:
70 case llvm::Intrinsic::expect:
71 return true;
72 default:
73 break;
74 }
75 }
76 return false;
77}
78
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +000079BlockGenerator::BlockGenerator(PollyIRBuilder &B, LoopInfo &LI,
80 ScalarEvolution &SE, DominatorTree &DT,
81 IslExprBuilder *ExprBuilder)
82 : Builder(B), LI(LI), SE(SE), ExprBuilder(ExprBuilder), DT(DT) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000083
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000084Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
85 ValueMapT &BBMap, ValueMapT &GlobalMap,
86 LoopToScevMapT &LTS, Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000087 // We assume constants never change.
88 // This avoids map lookups for many calls to this function.
89 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000090 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000091
Hongbin Zhengfe11e282013-06-29 13:22:15 +000092 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +000093 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +000094 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +000095 New = Builder.CreateTruncOrBitCast(New, Old->getType());
96
97 return New;
98 }
99
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000100 if (Value *New = BBMap.lookup(Old))
101 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000102
Tobias Grosser683b8e42014-11-30 14:33:31 +0000103 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000104 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000105 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000106 const SCEV *NewScev = apply(Scev, LTS, SE);
107 ValueToValueMap VTV;
108 VTV.insert(BBMap.begin(), BBMap.end());
109 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000110 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000111 SCEVExpander Expander(SE, "polly");
112 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
113 Builder.GetInsertPoint());
114
115 BBMap[Old] = Expanded;
116 return Expanded;
117 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000118 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000119
Tobias Grosser16371ac2014-11-05 20:48:56 +0000120 // A scop-constant value defined by a global or a function parameter.
121 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
122 return const_cast<Value *>(Old);
123
124 // A scop-constant value defined by an instruction executed outside the scop.
125 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000126 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000127 return const_cast<Value *>(Old);
128
129 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000130 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000131 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000132}
133
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000134void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
135 ValueMapT &BBMap, ValueMapT &GlobalMap,
136 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000137 // We do not generate debug intrinsics as we did not investigate how to
138 // copy them correctly. At the current state, they just crash the code
139 // generation as the meta-data operands are not correctly copied.
140 if (isa<DbgInfoIntrinsic>(Inst))
141 return;
142
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000143 Instruction *NewInst = Inst->clone();
144
145 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000146 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000147 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
148 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000149
150 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000151 assert(!isa<StoreInst>(NewInst) &&
152 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000153 delete NewInst;
154 return;
155 }
156
157 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
158 }
159
160 Builder.Insert(NewInst);
161 BBMap[Inst] = NewInst;
162
163 if (!NewInst->getType()->isVoidTy())
164 NewInst->setName("p_" + Inst->getName());
165}
166
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000167Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt,
168 const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000169 isl_pw_multi_aff *PWAccRel;
170 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000171 isl_ast_expr *Expr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000172 isl_ast_build *Build = Stmt.getAstBuild();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000173
Johannes Doerferta63b2572014-08-03 01:51:59 +0000174 assert(ExprBuilder && Build &&
175 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000176
Johannes Doerferta99130f2014-10-13 12:58:03 +0000177 Schedule = isl_ast_build_get_schedule(Build);
178 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000179
Johannes Doerferta63b2572014-08-03 01:51:59 +0000180 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000181 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000182
183 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000184}
185
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000186Value *BlockGenerator::generateLocationAccessed(
187 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
188 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
189 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000190
191 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000192 if (MA.hasNewAccessRelation())
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000193 NewPointer = getNewAccessOperand(Stmt, MA);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000194 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000195 NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000196 getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000197
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000198 return NewPointer;
199}
200
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000201Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000202 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000203}
204
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000205Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000206 ValueMapT &BBMap,
207 ValueMapT &GlobalMap,
208 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000209 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000210 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000211 generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000212 Value *ScalarLoad = Builder.CreateAlignedLoad(
213 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000214 return ScalarLoad;
215}
216
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000217Value *BlockGenerator::generateScalarStore(ScopStmt &Stmt,
218 const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000219 ValueMapT &BBMap,
220 ValueMapT &GlobalMap,
221 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000222 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000223 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000224 generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS);
225 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
226 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000227
Johannes Doerfert87901452014-10-02 16:22:19 +0000228 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
229 Store->getAlignment());
230 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000231}
232
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000233void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
234 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000235 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000236 // Terminator instructions control the control flow. They are explicitly
237 // expressed in the clast and do not need to be copied.
238 if (Inst->isTerminator())
239 return;
240
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000241 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000242 return;
243
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000244 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000245 Value *NewLoad = generateScalarLoad(Stmt, Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000246 // Compute NewLoad before its insertion in BBMap to make the insertion
247 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000248 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000249 return;
250 }
251
252 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000253 Value *NewStore = generateScalarStore(Stmt, Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000254 // Compute NewStore before its insertion in BBMap to make the insertion
255 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000256 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000257 return;
258 }
259
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000260 // Skip some special intrinsics for which we do not adjust the semantics to
261 // the new schedule. All others are handled like every other instruction.
262 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
263 switch (IT->getIntrinsicID()) {
264 // Lifetime markers are ignored.
265 case llvm::Intrinsic::lifetime_start:
266 case llvm::Intrinsic::lifetime_end:
267 // Invariant markers are ignored.
268 case llvm::Intrinsic::invariant_start:
269 case llvm::Intrinsic::invariant_end:
270 // Some misc annotations are ignored.
271 case llvm::Intrinsic::var_annotation:
272 case llvm::Intrinsic::ptr_annotation:
273 case llvm::Intrinsic::annotation:
274 case llvm::Intrinsic::donothing:
275 case llvm::Intrinsic::assume:
276 case llvm::Intrinsic::expect:
277 return;
278 default:
279 // Other intrinsics are copied.
280 break;
281 }
282 }
283
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000284 copyInstScalar(Stmt, Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000285}
286
Johannes Doerfert275a1752015-02-24 16:16:32 +0000287void BlockGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
288 LoopToScevMapT &LTS) {
289 assert(Stmt.isBlockStmt() &&
290 "Only block statements can be copied by the block generator");
291
292 ValueMapT BBMap;
293
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000294 BasicBlock *BB = Stmt.getBasicBlock();
Johannes Doerfert275a1752015-02-24 16:16:32 +0000295 copyBB(Stmt, BB, BBMap, GlobalMap, LTS);
296}
297
298BasicBlock *BlockGenerator::copyBB(ScopStmt &Stmt, BasicBlock *BB,
299 ValueMapT &BBMap, ValueMapT &GlobalMap,
300 LoopToScevMapT &LTS) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000301 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000302 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000303 CopyBB->setName("polly.stmt." + BB->getName());
304 Builder.SetInsertPoint(CopyBB->begin());
305
Tobias Grosser91f5b262014-06-04 08:06:40 +0000306 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000307 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Johannes Doerfert275a1752015-02-24 16:16:32 +0000308
309 return CopyBB;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000310}
311
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000312VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
313 VectorValueMapT &GlobalMaps,
314 std::vector<LoopToScevMapT> &VLTS,
315 isl_map *Schedule)
316 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
317 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000318 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
319 assert(Schedule && "No statement domain provided");
320}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000321
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000322Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000323 ValueMapT &VectorMap,
324 VectorValueMapT &ScalarMaps,
325 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000326 if (Value *NewValue = VectorMap.lookup(Old))
327 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000328
329 int Width = getVectorWidth();
330
331 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
332
333 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000334 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000335 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
336 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000337 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000338
339 VectorMap[Old] = Vector;
340
341 return Vector;
342}
343
344Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
345 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
346 assert(PointerTy && "PointerType expected");
347
348 Type *ScalarType = PointerTy->getElementType();
349 VectorType *VectorType = VectorType::get(ScalarType, Width);
350
351 return PointerType::getUnqual(VectorType);
352}
353
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000354Value *VectorBlockGenerator::generateStrideOneLoad(
355 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
356 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000357 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000359 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
360 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
361
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000362 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000363 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000364 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000365 Value *VectorPtr =
366 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
367 LoadInst *VecLoad =
368 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000369 if (!Aligned)
370 VecLoad->setAlignment(8);
371
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000372 if (NegativeStride) {
373 SmallVector<Constant *, 16> Indices;
374 for (int i = VectorWidth - 1; i >= 0; i--)
375 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
376 Constant *SV = llvm::ConstantVector::get(Indices);
377 Value *RevVecLoad = Builder.CreateShuffleVector(
378 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
379 return RevVecLoad;
380 }
381
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000382 return VecLoad;
383}
384
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000385Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
386 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000387 ValueMapT &BBMap) {
388 const Value *Pointer = Load->getPointerOperand();
389 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000390 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
391 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000392 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
393 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000394 LoadInst *ScalarLoad =
395 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000396
397 if (!Aligned)
398 ScalarLoad->setAlignment(8);
399
Tobias Grosserc14582f2013-02-05 18:01:29 +0000400 Constant *SplatVector = Constant::getNullValue(
401 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000402
Tobias Grosserc14582f2013-02-05 18:01:29 +0000403 Value *VectorLoad = Builder.CreateShuffleVector(
404 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000405 return VectorLoad;
406}
407
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000408Value *VectorBlockGenerator::generateUnknownStrideLoad(
409 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000410 int VectorWidth = getVectorWidth();
411 const Value *Pointer = Load->getPointerOperand();
412 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000413 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000414
415 Value *Vector = UndefValue::get(VectorType);
416
417 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000418 Value *NewPointer = generateLocationAccessed(
419 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000420 Value *ScalarLoad =
421 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
422 Vector = Builder.CreateInsertElement(
423 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000424 }
425
426 return Vector;
427}
428
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000429void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000430 ValueMapT &VectorMap,
431 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000432 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
433 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000434 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000435 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000436 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000437 return;
438 }
439
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000440 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000441
Tobias Grosser95493982014-04-18 09:46:35 +0000442 // Make sure we have scalar values available to access the pointer to
443 // the data location.
444 extractScalarValues(Load, VectorMap, ScalarMaps);
445
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000446 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000447 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000448 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000449 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000450 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000451 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000452 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000453 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000454 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000455
456 VectorMap[Load] = NewLoad;
457}
458
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000459void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
460 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000461 ValueMapT &VectorMap,
462 VectorValueMapT &ScalarMaps) {
463 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000464 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
465 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000466
467 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
468
469 const CastInst *Cast = dyn_cast<CastInst>(Inst);
470 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
471 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
472}
473
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000474void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
475 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000476 ValueMapT &VectorMap,
477 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000478 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000479 Value *OpZero = Inst->getOperand(0);
480 Value *OpOne = Inst->getOperand(1);
481
482 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000483 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
484 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000485
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000486 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000487 Inst->getName() + "p_vec");
488 VectorMap[Inst] = NewInst;
489}
490
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000491void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000492 ValueMapT &VectorMap,
493 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000494 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000495
496 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000497 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000498 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000499
Tobias Grosser50fd7012014-04-17 23:13:49 +0000500 // Make sure we have scalar values available to access the pointer to
501 // the data location.
502 extractScalarValues(Store, VectorMap, ScalarMaps);
503
Sebastian Popa00a0292012-12-18 07:46:06 +0000504 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000505 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000506 Value *NewPointer = generateLocationAccessed(
507 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000508
Tobias Grosserc14582f2013-02-05 18:01:29 +0000509 Value *VectorPtr =
510 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000511 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
512
513 if (!Aligned)
514 Store->setAlignment(8);
515 } else {
516 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000517 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000518 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000519 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000520 Builder.CreateStore(Scalar, NewPointer);
521 }
522 }
523}
524
525bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
526 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000527 for (Value *Operand : Inst->operands())
528 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000529 return true;
530 return false;
531}
532
533bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
534 ValueMapT &VectorMap,
535 VectorValueMapT &ScalarMaps) {
536 bool HasVectorOperand = false;
537 int VectorWidth = getVectorWidth();
538
Tobias Grosser91f5b262014-06-04 08:06:40 +0000539 for (Value *Operand : Inst->operands()) {
540 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000541
542 if (VecOp == VectorMap.end())
543 continue;
544
545 HasVectorOperand = true;
546 Value *NewVector = VecOp->second;
547
548 for (int i = 0; i < VectorWidth; ++i) {
549 ValueMapT &SM = ScalarMaps[i];
550
551 // If there is one scalar extracted, all scalar elements should have
552 // already been extracted by the code here. So no need to check for the
553 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000554 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000555 break;
556
Tobias Grosser91f5b262014-06-04 08:06:40 +0000557 SM[Operand] =
558 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000559 }
560 }
561
562 return HasVectorOperand;
563}
564
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000565void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
566 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000567 ValueMapT &VectorMap,
568 VectorValueMapT &ScalarMaps) {
569 bool HasVectorOperand;
570 int VectorWidth = getVectorWidth();
571
572 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
573
574 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000575 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000576 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000577
578 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
579 return;
580
581 // Make the result available as vector value.
582 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
583 Value *Vector = UndefValue::get(VectorType);
584
585 for (int i = 0; i < VectorWidth; i++)
586 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
587 Builder.getInt32(i));
588
589 VectorMap[Inst] = Vector;
590}
591
Tobias Grosserc14582f2013-02-05 18:01:29 +0000592int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000593
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000594void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
595 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000596 ValueMapT &VectorMap,
597 VectorValueMapT &ScalarMaps) {
598 // Terminator instructions control the control flow. They are explicitly
599 // expressed in the clast and do not need to be copied.
600 if (Inst->isTerminator())
601 return;
602
Johannes Doerfert1ef52332015-02-08 20:50:42 +0000603 if (canSynthesize(Inst, &LI, &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000604 return;
605
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000606 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000607 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000608 return;
609 }
610
611 if (hasVectorOperands(Inst, VectorMap)) {
612 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000613 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000614 return;
615 }
616
617 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000618 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000619 return;
620 }
621
622 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000623 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000624 return;
625 }
626
627 // Falltrough: We generate scalar instructions, if we don't know how to
628 // generate vector code.
629 }
630
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000631 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000632}
633
Johannes Doerfert275a1752015-02-24 16:16:32 +0000634void VectorBlockGenerator::copyStmt(ScopStmt &Stmt) {
635 assert(Stmt.isBlockStmt() && "TODO: Only block statements can be copied by "
636 "the vector block generator");
637
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000638 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000639 BasicBlock *CopyBB =
Johannes Doerfertb4f08eb2015-02-23 13:51:35 +0000640 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000641 CopyBB->setName("polly.stmt." + BB->getName());
642 Builder.SetInsertPoint(CopyBB->begin());
643
644 // Create two maps that store the mapping from the original instructions of
645 // the old basic block to their copies in the new basic block. Those maps
646 // are basic block local.
647 //
648 // As vector code generation is supported there is one map for scalar values
649 // and one for vector values.
650 //
651 // In case we just do scalar code generation, the vectorMap is not used and
652 // the scalarMap has just one dimension, which contains the mapping.
653 //
654 // In case vector code generation is done, an instruction may either appear
655 // in the vector map once (as it is calculating >vectorwidth< values at a
656 // time. Or (if the values are calculated using scalar operations), it
657 // appears once in every dimension of the scalarMap.
658 VectorValueMapT ScalarBlockMap(getVectorWidth());
659 ValueMapT VectorBlockMap;
660
Tobias Grosser91f5b262014-06-04 08:06:40 +0000661 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000662 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000663}
Johannes Doerfert275a1752015-02-24 16:16:32 +0000664
665void RegionGenerator::copyStmt(ScopStmt &Stmt, ValueMapT &GlobalMap,
666 LoopToScevMapT &LTS) {
667 assert(Stmt.isRegionStmt() &&
668 "Only region statements can be copied by the block generator");
669
670 // The region represented by the statement.
671 Region *R = Stmt.getRegion();
672
673 // The "BBMap" for the whole region.
674 ValueMapT RegionMap;
675
676 // Iterate over all blocks in the region in a breadth-first search.
677 std::deque<BasicBlock *> Blocks;
678 SmallPtrSet<BasicBlock *, 8> SeenBlocks;
679 Blocks.push_back(R->getEntry());
680 SeenBlocks.insert(R->getEntry());
681
682 while (!Blocks.empty()) {
683 BasicBlock *BB = Blocks.front();
684 Blocks.pop_front();
685
686 // Copy the block with the BlockGenerator.
687 BasicBlock *BBCopy = copyBB(Stmt, BB, RegionMap, GlobalMap, LTS);
688
689 // And continue with new successors inside the region.
690 for (auto SI = succ_begin(BB), SE = succ_end(BB); SI != SE; SI++)
691 if (R->contains(*SI) && SeenBlocks.insert(*SI).second)
692 Blocks.push_back(*SI);
693
694 // In order to remap PHI nodes we store also basic block mappings.
695 RegionMap[BB] = BBCopy;
696 }
697
698 // Now create a new dedicated region exit block and add it to the region map.
699 BasicBlock *RegionExit =
700 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
701 RegionExit->setName("polly.stmt." + R->getExit()->getName() + ".pre");
702 RegionMap[R->getExit()] = RegionExit;
703
704 // As the block generator doesn't handle control flow we need to add the
705 // region control flow by hand after all blocks have been copied.
706 for (BasicBlock *BB : SeenBlocks) {
707
708 BranchInst *BI = cast<BranchInst>(BB->getTerminator());
709
710 BasicBlock *BBCopy = cast<BasicBlock>(RegionMap[BB]);
711 Instruction *BICopy = BBCopy->getTerminator();
712
713 Builder.SetInsertPoint(BBCopy);
714 copyInstScalar(Stmt, BI, RegionMap, GlobalMap, LTS);
715 BICopy->eraseFromParent();
716 }
717
718 // Reset the old insert point for the build.
719 Builder.SetInsertPoint(RegionExit->begin());
720}