blob: 6790b9dbd971c7683eb1d8e1aaf7d3fcf143711e [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 Doerfertbe9c9112015-02-06 21:39:31 +000079BlockGenerator::BlockGenerator(PollyIRBuilder &B, Pass *P, LoopInfo &LI,
80 ScalarEvolution &SE, IslExprBuilder *ExprBuilder)
81 : Builder(B), P(P), LI(LI), SE(SE), ExprBuilder(ExprBuilder) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000082
Johannes Doerfertbe9c9112015-02-06 21:39:31 +000083Value *BlockGenerator::getNewValue(ScopStmt &Stmt, const Value *Old,
84 ValueMapT &BBMap, ValueMapT &GlobalMap,
85 LoopToScevMapT &LTS, Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000086 // We assume constants never change.
87 // This avoids map lookups for many calls to this function.
88 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000089 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000090
Hongbin Zhengfe11e282013-06-29 13:22:15 +000091 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +000092 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +000093 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +000094 New = Builder.CreateTruncOrBitCast(New, Old->getType());
95
96 return New;
97 }
98
Hongbin Zhengfe11e282013-06-29 13:22:15 +000099 if (Value *New = BBMap.lookup(Old))
100 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000101
Tobias Grosser683b8e42014-11-30 14:33:31 +0000102 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000103 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000104 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000105 const SCEV *NewScev = apply(Scev, LTS, SE);
106 ValueToValueMap VTV;
107 VTV.insert(BBMap.begin(), BBMap.end());
108 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000109 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000110 SCEVExpander Expander(SE, "polly");
111 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
112 Builder.GetInsertPoint());
113
114 BBMap[Old] = Expanded;
115 return Expanded;
116 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000117 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000118
Tobias Grosser16371ac2014-11-05 20:48:56 +0000119 // A scop-constant value defined by a global or a function parameter.
120 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
121 return const_cast<Value *>(Old);
122
123 // A scop-constant value defined by an instruction executed outside the scop.
124 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000125 if (!Stmt.getParent()->getRegion().contains(Inst->getParent()))
Tobias Grosser16371ac2014-11-05 20:48:56 +0000126 return const_cast<Value *>(Old);
127
128 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000129 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000130 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000131}
132
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000133void BlockGenerator::copyInstScalar(ScopStmt &Stmt, const Instruction *Inst,
134 ValueMapT &BBMap, ValueMapT &GlobalMap,
135 LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000136 // We do not generate debug intrinsics as we did not investigate how to
137 // copy them correctly. At the current state, they just crash the code
138 // generation as the meta-data operands are not correctly copied.
139 if (isa<DbgInfoIntrinsic>(Inst))
140 return;
141
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000142 Instruction *NewInst = Inst->clone();
143
144 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000145 for (Value *OldOperand : Inst->operands()) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000146 Value *NewOperand = getNewValue(Stmt, OldOperand, BBMap, GlobalMap, LTS,
147 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000148
149 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000150 assert(!isa<StoreInst>(NewInst) &&
151 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000152 delete NewInst;
153 return;
154 }
155
156 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
157 }
158
159 Builder.Insert(NewInst);
160 BBMap[Inst] = NewInst;
161
162 if (!NewInst->getType()->isVoidTy())
163 NewInst->setName("p_" + Inst->getName());
164}
165
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000166Value *BlockGenerator::getNewAccessOperand(ScopStmt &Stmt,
167 const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000168 isl_pw_multi_aff *PWAccRel;
169 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000170 isl_ast_expr *Expr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000171 isl_ast_build *Build = Stmt.getAstBuild();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000172
Johannes Doerferta63b2572014-08-03 01:51:59 +0000173 assert(ExprBuilder && Build &&
174 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000175
Johannes Doerferta99130f2014-10-13 12:58:03 +0000176 Schedule = isl_ast_build_get_schedule(Build);
177 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000178
Johannes Doerferta63b2572014-08-03 01:51:59 +0000179 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000180 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000181
182 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000183}
184
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000185Value *BlockGenerator::generateLocationAccessed(
186 ScopStmt &Stmt, const Instruction *Inst, const Value *Pointer,
187 ValueMapT &BBMap, ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
188 const MemoryAccess &MA = Stmt.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000189
190 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000191 if (MA.hasNewAccessRelation())
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000192 NewPointer = getNewAccessOperand(Stmt, MA);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000193 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000194 NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000195 getNewValue(Stmt, Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000196
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000197 return NewPointer;
198}
199
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000200Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000201 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000202}
203
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000204Value *BlockGenerator::generateScalarLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000205 ValueMapT &BBMap,
206 ValueMapT &GlobalMap,
207 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000208 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000209 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000210 generateLocationAccessed(Stmt, Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000211 Value *ScalarLoad = Builder.CreateAlignedLoad(
212 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000213 return ScalarLoad;
214}
215
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000216Value *BlockGenerator::generateScalarStore(ScopStmt &Stmt,
217 const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000218 ValueMapT &BBMap,
219 ValueMapT &GlobalMap,
220 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000221 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000222 Value *NewPointer =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000223 generateLocationAccessed(Stmt, Store, Pointer, BBMap, GlobalMap, LTS);
224 Value *ValueOperand = getNewValue(Stmt, Store->getValueOperand(), BBMap,
225 GlobalMap, LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000226
Johannes Doerfert87901452014-10-02 16:22:19 +0000227 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
228 Store->getAlignment());
229 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000230}
231
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000232void BlockGenerator::copyInstruction(ScopStmt &Stmt, const Instruction *Inst,
233 ValueMapT &BBMap, ValueMapT &GlobalMap,
Tobias Grossere602a072013-05-07 07:30:56 +0000234 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000235 // Terminator instructions control the control flow. They are explicitly
236 // expressed in the clast and do not need to be copied.
237 if (Inst->isTerminator())
238 return;
239
Chandler Carruthf5579872015-01-17 14:16:56 +0000240 if (canSynthesize(Inst, &P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000241 &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 Doerfertbe9c9112015-02-06 21:39:31 +0000287void BlockGenerator::copyBB(ScopStmt &Stmt, ValueMapT &GlobalMap,
288 LoopToScevMapT &LTS) {
Chandler Carruth5ec33332015-01-18 10:52:23 +0000289 auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
290 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
291 auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
Tobias Grossera8cd1522015-01-18 15:59:16 +0000292 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
Chandler Carruth5ec33332015-01-18 10:52:23 +0000293
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000294 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000295 BasicBlock *CopyBB =
Chandler Carruth5ec33332015-01-18 10:52:23 +0000296 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), DT, LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000297 CopyBB->setName("polly.stmt." + BB->getName());
298 Builder.SetInsertPoint(CopyBB->begin());
299
300 ValueMapT BBMap;
301
Tobias Grosser91f5b262014-06-04 08:06:40 +0000302 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000303 copyInstruction(Stmt, &Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000304}
305
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000306VectorBlockGenerator::VectorBlockGenerator(BlockGenerator &BlockGen,
307 VectorValueMapT &GlobalMaps,
308 std::vector<LoopToScevMapT> &VLTS,
309 isl_map *Schedule)
310 : BlockGenerator(BlockGen), GlobalMaps(GlobalMaps), VLTS(VLTS),
311 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000312 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
313 assert(Schedule && "No statement domain provided");
314}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000315
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000316Value *VectorBlockGenerator::getVectorValue(ScopStmt &Stmt, const Value *Old,
Tobias Grossere602a072013-05-07 07:30:56 +0000317 ValueMapT &VectorMap,
318 VectorValueMapT &ScalarMaps,
319 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000320 if (Value *NewValue = VectorMap.lookup(Old))
321 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000322
323 int Width = getVectorWidth();
324
325 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
326
327 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000328 Vector = Builder.CreateInsertElement(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000329 Vector, getNewValue(Stmt, Old, ScalarMaps[Lane], GlobalMaps[Lane],
330 VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000331 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000332
333 VectorMap[Old] = Vector;
334
335 return Vector;
336}
337
338Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
339 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
340 assert(PointerTy && "PointerType expected");
341
342 Type *ScalarType = PointerTy->getElementType();
343 VectorType *VectorType = VectorType::get(ScalarType, Width);
344
345 return PointerType::getUnqual(VectorType);
346}
347
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000348Value *VectorBlockGenerator::generateStrideOneLoad(
349 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps,
350 bool NegativeStride = false) {
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000351 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000352 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000353 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
354 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
355
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000356 Value *NewPointer = nullptr;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000357 NewPointer = generateLocationAccessed(Stmt, Load, Pointer, ScalarMaps[Offset],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000358 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000359 Value *VectorPtr =
360 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
361 LoadInst *VecLoad =
362 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000363 if (!Aligned)
364 VecLoad->setAlignment(8);
365
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000366 if (NegativeStride) {
367 SmallVector<Constant *, 16> Indices;
368 for (int i = VectorWidth - 1; i >= 0; i--)
369 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
370 Constant *SV = llvm::ConstantVector::get(Indices);
371 Value *RevVecLoad = Builder.CreateShuffleVector(
372 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
373 return RevVecLoad;
374 }
375
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000376 return VecLoad;
377}
378
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000379Value *VectorBlockGenerator::generateStrideZeroLoad(ScopStmt &Stmt,
380 const LoadInst *Load,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000381 ValueMapT &BBMap) {
382 const Value *Pointer = Load->getPointerOperand();
383 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000384 Value *NewPointer = generateLocationAccessed(Stmt, Load, Pointer, BBMap,
385 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000386 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
387 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000388 LoadInst *ScalarLoad =
389 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000390
391 if (!Aligned)
392 ScalarLoad->setAlignment(8);
393
Tobias Grosserc14582f2013-02-05 18:01:29 +0000394 Constant *SplatVector = Constant::getNullValue(
395 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000396
Tobias Grosserc14582f2013-02-05 18:01:29 +0000397 Value *VectorLoad = Builder.CreateShuffleVector(
398 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000399 return VectorLoad;
400}
401
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000402Value *VectorBlockGenerator::generateUnknownStrideLoad(
403 ScopStmt &Stmt, const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000404 int VectorWidth = getVectorWidth();
405 const Value *Pointer = Load->getPointerOperand();
406 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000407 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000408
409 Value *Vector = UndefValue::get(VectorType);
410
411 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000412 Value *NewPointer = generateLocationAccessed(
413 Stmt, Load, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000414 Value *ScalarLoad =
415 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
416 Vector = Builder.CreateInsertElement(
417 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000418 }
419
420 return Vector;
421}
422
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000423void VectorBlockGenerator::generateLoad(ScopStmt &Stmt, const LoadInst *Load,
Tobias Grossere602a072013-05-07 07:30:56 +0000424 ValueMapT &VectorMap,
425 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000426 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
427 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000428 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000429 ScalarMaps[i][Load] =
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000430 generateScalarLoad(Stmt, Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000431 return;
432 }
433
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000434 const MemoryAccess &Access = Stmt.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000435
Tobias Grosser95493982014-04-18 09:46:35 +0000436 // Make sure we have scalar values available to access the pointer to
437 // the data location.
438 extractScalarValues(Load, VectorMap, ScalarMaps);
439
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000440 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000441 if (Access.isStrideZero(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000442 NewLoad = generateStrideZeroLoad(Stmt, Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000443 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000444 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps);
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000445 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000446 NewLoad = generateStrideOneLoad(Stmt, Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000447 else
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000448 NewLoad = generateUnknownStrideLoad(Stmt, Load, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000449
450 VectorMap[Load] = NewLoad;
451}
452
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000453void VectorBlockGenerator::copyUnaryInst(ScopStmt &Stmt,
454 const UnaryInstruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000455 ValueMapT &VectorMap,
456 VectorValueMapT &ScalarMaps) {
457 int VectorWidth = getVectorWidth();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000458 Value *NewOperand = getVectorValue(Stmt, Inst->getOperand(0), VectorMap,
459 ScalarMaps, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000460
461 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
462
463 const CastInst *Cast = dyn_cast<CastInst>(Inst);
464 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
465 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
466}
467
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000468void VectorBlockGenerator::copyBinaryInst(ScopStmt &Stmt,
469 const BinaryOperator *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000470 ValueMapT &VectorMap,
471 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000472 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000473 Value *OpZero = Inst->getOperand(0);
474 Value *OpOne = Inst->getOperand(1);
475
476 Value *NewOpZero, *NewOpOne;
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000477 NewOpZero = getVectorValue(Stmt, OpZero, VectorMap, ScalarMaps, L);
478 NewOpOne = getVectorValue(Stmt, OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000479
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000480 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000481 Inst->getName() + "p_vec");
482 VectorMap[Inst] = NewInst;
483}
484
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000485void VectorBlockGenerator::copyStore(ScopStmt &Stmt, const StoreInst *Store,
Tobias Grossere602a072013-05-07 07:30:56 +0000486 ValueMapT &VectorMap,
487 VectorValueMapT &ScalarMaps) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000488 const MemoryAccess &Access = Stmt.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000489
490 const Value *Pointer = Store->getPointerOperand();
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000491 Value *Vector = getVectorValue(Stmt, Store->getValueOperand(), VectorMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000492 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000493
Tobias Grosser50fd7012014-04-17 23:13:49 +0000494 // Make sure we have scalar values available to access the pointer to
495 // the data location.
496 extractScalarValues(Store, VectorMap, ScalarMaps);
497
Sebastian Popa00a0292012-12-18 07:46:06 +0000498 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000499 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000500 Value *NewPointer = generateLocationAccessed(
501 Stmt, Store, Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000502
Tobias Grosserc14582f2013-02-05 18:01:29 +0000503 Value *VectorPtr =
504 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000505 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
506
507 if (!Aligned)
508 Store->setAlignment(8);
509 } else {
510 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000511 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000512 Value *NewPointer = generateLocationAccessed(
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000513 Stmt, Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000514 Builder.CreateStore(Scalar, NewPointer);
515 }
516 }
517}
518
519bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
520 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000521 for (Value *Operand : Inst->operands())
522 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000523 return true;
524 return false;
525}
526
527bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
528 ValueMapT &VectorMap,
529 VectorValueMapT &ScalarMaps) {
530 bool HasVectorOperand = false;
531 int VectorWidth = getVectorWidth();
532
Tobias Grosser91f5b262014-06-04 08:06:40 +0000533 for (Value *Operand : Inst->operands()) {
534 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000535
536 if (VecOp == VectorMap.end())
537 continue;
538
539 HasVectorOperand = true;
540 Value *NewVector = VecOp->second;
541
542 for (int i = 0; i < VectorWidth; ++i) {
543 ValueMapT &SM = ScalarMaps[i];
544
545 // If there is one scalar extracted, all scalar elements should have
546 // already been extracted by the code here. So no need to check for the
547 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000548 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000549 break;
550
Tobias Grosser91f5b262014-06-04 08:06:40 +0000551 SM[Operand] =
552 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000553 }
554 }
555
556 return HasVectorOperand;
557}
558
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000559void VectorBlockGenerator::copyInstScalarized(ScopStmt &Stmt,
560 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000561 ValueMapT &VectorMap,
562 VectorValueMapT &ScalarMaps) {
563 bool HasVectorOperand;
564 int VectorWidth = getVectorWidth();
565
566 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
567
568 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000569 BlockGenerator::copyInstruction(Stmt, Inst, ScalarMaps[VectorLane],
Johannes Doerfert731685e2014-10-08 17:25:30 +0000570 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000571
572 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
573 return;
574
575 // Make the result available as vector value.
576 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
577 Value *Vector = UndefValue::get(VectorType);
578
579 for (int i = 0; i < VectorWidth; i++)
580 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
581 Builder.getInt32(i));
582
583 VectorMap[Inst] = Vector;
584}
585
Tobias Grosserc14582f2013-02-05 18:01:29 +0000586int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000587
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000588void VectorBlockGenerator::copyInstruction(ScopStmt &Stmt,
589 const Instruction *Inst,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000590 ValueMapT &VectorMap,
591 VectorValueMapT &ScalarMaps) {
592 // Terminator instructions control the control flow. They are explicitly
593 // expressed in the clast and do not need to be copied.
594 if (Inst->isTerminator())
595 return;
596
Chandler Carruthf5579872015-01-17 14:16:56 +0000597 if (canSynthesize(Inst, &P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000598 &SE, &Stmt.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000599 return;
600
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000601 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000602 generateLoad(Stmt, Load, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000603 return;
604 }
605
606 if (hasVectorOperands(Inst, VectorMap)) {
607 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000608 copyStore(Stmt, Store, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000609 return;
610 }
611
612 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000613 copyUnaryInst(Stmt, Unary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000614 return;
615 }
616
617 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000618 copyBinaryInst(Stmt, Binary, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000619 return;
620 }
621
622 // Falltrough: We generate scalar instructions, if we don't know how to
623 // generate vector code.
624 }
625
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000626 copyInstScalarized(Stmt, Inst, VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000627}
628
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000629void VectorBlockGenerator::copyBB(ScopStmt &Stmt) {
Chandler Carruth5ec33332015-01-18 10:52:23 +0000630 auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
631 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
632 auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
Tobias Grossera8cd1522015-01-18 15:59:16 +0000633 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
Chandler Carruth5ec33332015-01-18 10:52:23 +0000634
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000635 BasicBlock *BB = Stmt.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000636 BasicBlock *CopyBB =
Chandler Carruth5ec33332015-01-18 10:52:23 +0000637 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), DT, LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000638 CopyBB->setName("polly.stmt." + BB->getName());
639 Builder.SetInsertPoint(CopyBB->begin());
640
641 // Create two maps that store the mapping from the original instructions of
642 // the old basic block to their copies in the new basic block. Those maps
643 // are basic block local.
644 //
645 // As vector code generation is supported there is one map for scalar values
646 // and one for vector values.
647 //
648 // In case we just do scalar code generation, the vectorMap is not used and
649 // the scalarMap has just one dimension, which contains the mapping.
650 //
651 // In case vector code generation is done, an instruction may either appear
652 // in the vector map once (as it is calculating >vectorwidth< values at a
653 // time. Or (if the values are calculated using scalar operations), it
654 // appears once in every dimension of the scalarMap.
655 VectorValueMapT ScalarBlockMap(getVectorWidth());
656 ValueMapT VectorBlockMap;
657
Tobias Grosser91f5b262014-06-04 08:06:40 +0000658 for (Instruction &Inst : *BB)
Johannes Doerfertbe9c9112015-02-06 21:39:31 +0000659 copyInstruction(Stmt, &Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000660}