blob: 8d1734c39485e2fb28f68467ec8500b261b6b4bd [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 Grossere602a072013-05-07 07:30:56 +000042static cl::opt<bool, true>
Tobias Grosser483a90d2014-07-09 10:50:10 +000043 SCEVCodegenF("polly-codegen-scev",
44 cl::desc("Use SCEV based code generation."), cl::Hidden,
45 cl::location(SCEVCodegen), cl::init(false), cl::ZeroOrMore,
46 cl::cat(PollyCategory));
Tobias Grosserecfe21b2013-03-20 18:03:18 +000047
48bool polly::SCEVCodegen;
49
50bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
51 ScalarEvolution *SE, const Region *R) {
52 if (SCEVCodegen) {
53 if (!I || !SE->isSCEVable(I->getType()))
54 return false;
55
56 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
57 if (!isa<SCEVCouldNotCompute>(Scev))
58 if (!hasScalarDepsInsideRegion(Scev, R))
59 return true;
60
61 return false;
62 }
63
64 Loop *L = LI->getLoopFor(I->getParent());
Tobias Grossere8df5bd2013-04-17 07:20:36 +000065 return L && I == L->getCanonicalInductionVariable() && R->contains(L);
Tobias Grosserecfe21b2013-03-20 18:03:18 +000066}
67
Johannes Doerferta63b2572014-08-03 01:51:59 +000068BlockGenerator::BlockGenerator(PollyIRBuilder &B, ScopStmt &Stmt, Pass *P,
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000069 LoopInfo &LI, ScalarEvolution &SE,
Johannes Doerferta63b2572014-08-03 01:51:59 +000070 isl_ast_build *Build,
71 IslExprBuilder *ExprBuilder)
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000072 : Builder(B), Statement(Stmt), P(P), LI(LI), SE(SE), Build(Build),
73 ExprBuilder(ExprBuilder) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000074
Tobias Grosserd213a8b2014-11-05 19:46:04 +000075Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
76 ValueMapT &GlobalMap, LoopToScevMapT &LTS,
77 Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000078 // We assume constants never change.
79 // This avoids map lookups for many calls to this function.
80 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000081 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000082
Hongbin Zhengfe11e282013-06-29 13:22:15 +000083 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +000084 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +000085 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +000086 New = Builder.CreateTruncOrBitCast(New, Old->getType());
87
88 return New;
89 }
90
Hongbin Zheng5b463ce2013-07-25 09:12:07 +000091 // Or it is probably a scop-constant value defined as global, function
92 // parameter or an instruction not within the scop.
93 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
94 return const_cast<Value *>(Old);
95
96 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
97 if (!Statement.getParent()->getRegion().contains(Inst->getParent()))
98 return const_cast<Value *>(Old);
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 Grossere71c6ab2012-04-27 16:36:14 +0000103 if (SCEVCodegen && 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
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000120 // Now the scalar dependence is neither available nor SCEVCodegenable, this
121 // should never happen in the current code generator.
122 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000123 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000124}
125
126void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000127 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000128 // We do not generate debug intrinsics as we did not investigate how to
129 // copy them correctly. At the current state, they just crash the code
130 // generation as the meta-data operands are not correctly copied.
131 if (isa<DbgInfoIntrinsic>(Inst))
132 return;
133
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000134 Instruction *NewInst = Inst->clone();
135
136 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000137 for (Value *OldOperand : Inst->operands()) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000138 Value *NewOperand =
139 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000140
141 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000142 assert(!isa<StoreInst>(NewInst) &&
143 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000144 delete NewInst;
145 return;
146 }
147
148 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
149 }
150
151 Builder.Insert(NewInst);
152 BBMap[Inst] = NewInst;
153
154 if (!NewInst->getType()->isVoidTy())
155 NewInst->setName("p_" + Inst->getName());
156}
157
Johannes Doerferta63b2572014-08-03 01:51:59 +0000158Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000159 isl_pw_multi_aff *PWAccRel;
160 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000161 isl_ast_expr *Expr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000162
Johannes Doerferta63b2572014-08-03 01:51:59 +0000163 assert(ExprBuilder && Build &&
164 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000165
Johannes Doerferta99130f2014-10-13 12:58:03 +0000166 Schedule = isl_ast_build_get_schedule(Build);
167 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000168
Johannes Doerferta63b2572014-08-03 01:51:59 +0000169 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000170 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000171
172 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000173}
174
Tobias Grossere602a072013-05-07 07:30:56 +0000175Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
176 const Value *Pointer,
177 ValueMapT &BBMap,
178 ValueMapT &GlobalMap,
179 LoopToScevMapT &LTS) {
Johannes Doerferta63b2572014-08-03 01:51:59 +0000180 const MemoryAccess &MA = Statement.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000181
182 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000183 if (MA.hasNewAccessRelation())
Johannes Doerferta63b2572014-08-03 01:51:59 +0000184 NewPointer = getNewAccessOperand(MA);
185 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000186 NewPointer =
187 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000188
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000189 return NewPointer;
190}
191
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000192Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000193 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000194}
195
Tobias Grossere602a072013-05-07 07:30:56 +0000196Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
197 ValueMapT &BBMap,
198 ValueMapT &GlobalMap,
199 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000200 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000201 Value *NewPointer =
Johannes Doerfert1947f862014-10-08 20:18:32 +0000202 generateLocationAccessed(Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000203 Value *ScalarLoad = Builder.CreateAlignedLoad(
204 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000205 return ScalarLoad;
206}
207
Tobias Grossere602a072013-05-07 07:30:56 +0000208Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
209 ValueMapT &BBMap,
210 ValueMapT &GlobalMap,
211 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000212 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000213 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000214 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000215 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
216 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000217
Johannes Doerfert87901452014-10-02 16:22:19 +0000218 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
219 Store->getAlignment());
220 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000221}
222
Tobias Grossere602a072013-05-07 07:30:56 +0000223void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
224 ValueMapT &GlobalMap,
225 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000226 // Terminator instructions control the control flow. They are explicitly
227 // expressed in the clast and do not need to be copied.
228 if (Inst->isTerminator())
229 return;
230
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000231 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
232 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000233 return;
234
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000235 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000236 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000237 // Compute NewLoad before its insertion in BBMap to make the insertion
238 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000239 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000240 return;
241 }
242
243 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000244 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000245 // Compute NewStore before its insertion in BBMap to make the insertion
246 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000247 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000248 return;
249 }
250
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000251 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000252}
253
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000254void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000255 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000256 BasicBlock *CopyBB =
257 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000258 CopyBB->setName("polly.stmt." + BB->getName());
259 Builder.SetInsertPoint(CopyBB->begin());
260
261 ValueMapT BBMap;
262
Tobias Grosser91f5b262014-06-04 08:06:40 +0000263 for (Instruction &Inst : *BB)
264 copyInstruction(&Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000265}
266
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000267VectorBlockGenerator::VectorBlockGenerator(
268 PollyIRBuilder &B, VectorValueMapT &GlobalMaps,
269 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
Johannes Doerfert731685e2014-10-08 17:25:30 +0000270 __isl_keep isl_map *Schedule, Pass *P, LoopInfo &LI, ScalarEvolution &SE,
271 __isl_keep isl_ast_build *Build, IslExprBuilder *ExprBuilder)
272 : BlockGenerator(B, Stmt, P, LI, SE, Build, ExprBuilder),
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000273 GlobalMaps(GlobalMaps), VLTS(VLTS), Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000274 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
275 assert(Schedule && "No statement domain provided");
276}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000277
Tobias Grossere602a072013-05-07 07:30:56 +0000278Value *VectorBlockGenerator::getVectorValue(const Value *Old,
279 ValueMapT &VectorMap,
280 VectorValueMapT &ScalarMaps,
281 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000282 if (Value *NewValue = VectorMap.lookup(Old))
283 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000284
285 int Width = getVectorWidth();
286
287 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
288
289 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000290 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000291 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000292 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000293 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000294
295 VectorMap[Old] = Vector;
296
297 return Vector;
298}
299
300Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
301 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
302 assert(PointerTy && "PointerType expected");
303
304 Type *ScalarType = PointerTy->getElementType();
305 VectorType *VectorType = VectorType::get(ScalarType, Width);
306
307 return PointerType::getUnqual(VectorType);
308}
309
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000310Value *
311VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
312 VectorValueMapT &ScalarMaps,
313 bool NegativeStride = false) {
314 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000315 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000316 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
317 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
318
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000319 Value *NewPointer = nullptr;
Johannes Doerfert731685e2014-10-08 17:25:30 +0000320 NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[Offset],
321 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000322 Value *VectorPtr =
323 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
324 LoadInst *VecLoad =
325 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000326 if (!Aligned)
327 VecLoad->setAlignment(8);
328
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000329 if (NegativeStride) {
330 SmallVector<Constant *, 16> Indices;
331 for (int i = VectorWidth - 1; i >= 0; i--)
332 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
333 Constant *SV = llvm::ConstantVector::get(Indices);
334 Value *RevVecLoad = Builder.CreateShuffleVector(
335 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
336 return RevVecLoad;
337 }
338
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000339 return VecLoad;
340}
341
342Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
343 ValueMapT &BBMap) {
344 const Value *Pointer = Load->getPointerOperand();
345 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000346 Value *NewPointer =
Johannes Doerfert731685e2014-10-08 17:25:30 +0000347 generateLocationAccessed(Load, Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000348 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
349 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000350 LoadInst *ScalarLoad =
351 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000352
353 if (!Aligned)
354 ScalarLoad->setAlignment(8);
355
Tobias Grosserc14582f2013-02-05 18:01:29 +0000356 Constant *SplatVector = Constant::getNullValue(
357 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358
Tobias Grosserc14582f2013-02-05 18:01:29 +0000359 Value *VectorLoad = Builder.CreateShuffleVector(
360 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000361 return VectorLoad;
362}
363
Tobias Grossere602a072013-05-07 07:30:56 +0000364Value *
365VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
366 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000367 int VectorWidth = getVectorWidth();
368 const Value *Pointer = Load->getPointerOperand();
369 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000370 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000371
372 Value *Vector = UndefValue::get(VectorType);
373
374 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfert731685e2014-10-08 17:25:30 +0000375 Value *NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[i],
376 GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000377 Value *ScalarLoad =
378 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
379 Vector = Builder.CreateInsertElement(
380 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000381 }
382
383 return Vector;
384}
385
Tobias Grossere602a072013-05-07 07:30:56 +0000386void VectorBlockGenerator::generateLoad(const LoadInst *Load,
387 ValueMapT &VectorMap,
388 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000389 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
390 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000391 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000392 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000393 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000394 return;
395 }
396
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000397 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000398
Tobias Grosser95493982014-04-18 09:46:35 +0000399 // Make sure we have scalar values available to access the pointer to
400 // the data location.
401 extractScalarValues(Load, VectorMap, ScalarMaps);
402
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000403 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000404 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000405 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000406 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000407 NewLoad = generateStrideOneLoad(Load, ScalarMaps);
408 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
409 NewLoad = generateStrideOneLoad(Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000410 else
411 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
412
413 VectorMap[Load] = NewLoad;
414}
415
416void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
417 ValueMapT &VectorMap,
418 VectorValueMapT &ScalarMaps) {
419 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000420 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
421 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000422
423 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
424
425 const CastInst *Cast = dyn_cast<CastInst>(Inst);
426 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
427 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
428}
429
430void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
431 ValueMapT &VectorMap,
432 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000433 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000434 Value *OpZero = Inst->getOperand(0);
435 Value *OpOne = Inst->getOperand(1);
436
437 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000438 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
439 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000440
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000441 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000442 Inst->getName() + "p_vec");
443 VectorMap[Inst] = NewInst;
444}
445
Tobias Grossere602a072013-05-07 07:30:56 +0000446void VectorBlockGenerator::copyStore(const StoreInst *Store,
447 ValueMapT &VectorMap,
448 VectorValueMapT &ScalarMaps) {
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000449 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000450
451 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000452 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
453 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000454
Tobias Grosser50fd7012014-04-17 23:13:49 +0000455 // Make sure we have scalar values available to access the pointer to
456 // the data location.
457 extractScalarValues(Store, VectorMap, ScalarMaps);
458
Sebastian Popa00a0292012-12-18 07:46:06 +0000459 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000460 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfert731685e2014-10-08 17:25:30 +0000461 Value *NewPointer = generateLocationAccessed(Store, Pointer, ScalarMaps[0],
462 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000463
Tobias Grosserc14582f2013-02-05 18:01:29 +0000464 Value *VectorPtr =
465 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000466 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
467
468 if (!Aligned)
469 Store->setAlignment(8);
470 } else {
471 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000472 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000473 Value *NewPointer = generateLocationAccessed(
474 Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000475 Builder.CreateStore(Scalar, NewPointer);
476 }
477 }
478}
479
480bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
481 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000482 for (Value *Operand : Inst->operands())
483 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000484 return true;
485 return false;
486}
487
488bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
489 ValueMapT &VectorMap,
490 VectorValueMapT &ScalarMaps) {
491 bool HasVectorOperand = false;
492 int VectorWidth = getVectorWidth();
493
Tobias Grosser91f5b262014-06-04 08:06:40 +0000494 for (Value *Operand : Inst->operands()) {
495 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000496
497 if (VecOp == VectorMap.end())
498 continue;
499
500 HasVectorOperand = true;
501 Value *NewVector = VecOp->second;
502
503 for (int i = 0; i < VectorWidth; ++i) {
504 ValueMapT &SM = ScalarMaps[i];
505
506 // If there is one scalar extracted, all scalar elements should have
507 // already been extracted by the code here. So no need to check for the
508 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000509 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000510 break;
511
Tobias Grosser91f5b262014-06-04 08:06:40 +0000512 SM[Operand] =
513 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000514 }
515 }
516
517 return HasVectorOperand;
518}
519
520void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
521 ValueMapT &VectorMap,
522 VectorValueMapT &ScalarMaps) {
523 bool HasVectorOperand;
524 int VectorWidth = getVectorWidth();
525
526 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
527
528 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfert731685e2014-10-08 17:25:30 +0000529 BlockGenerator::copyInstruction(Inst, ScalarMaps[VectorLane],
530 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000531
532 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
533 return;
534
535 // Make the result available as vector value.
536 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
537 Value *Vector = UndefValue::get(VectorType);
538
539 for (int i = 0; i < VectorWidth; i++)
540 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
541 Builder.getInt32(i));
542
543 VectorMap[Inst] = Vector;
544}
545
Tobias Grosserc14582f2013-02-05 18:01:29 +0000546int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000547
548void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
549 ValueMapT &VectorMap,
550 VectorValueMapT &ScalarMaps) {
551 // Terminator instructions control the control flow. They are explicitly
552 // expressed in the clast and do not need to be copied.
553 if (Inst->isTerminator())
554 return;
555
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000556 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
557 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000558 return;
559
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000560 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
561 generateLoad(Load, VectorMap, ScalarMaps);
562 return;
563 }
564
565 if (hasVectorOperands(Inst, VectorMap)) {
566 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
567 copyStore(Store, VectorMap, ScalarMaps);
568 return;
569 }
570
571 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
572 copyUnaryInst(Unary, VectorMap, ScalarMaps);
573 return;
574 }
575
576 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
577 copyBinaryInst(Binary, VectorMap, ScalarMaps);
578 return;
579 }
580
581 // Falltrough: We generate scalar instructions, if we don't know how to
582 // generate vector code.
583 }
584
585 copyInstScalarized(Inst, VectorMap, ScalarMaps);
586}
587
588void VectorBlockGenerator::copyBB() {
589 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000590 BasicBlock *CopyBB =
591 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000592 CopyBB->setName("polly.stmt." + BB->getName());
593 Builder.SetInsertPoint(CopyBB->begin());
594
595 // Create two maps that store the mapping from the original instructions of
596 // the old basic block to their copies in the new basic block. Those maps
597 // are basic block local.
598 //
599 // As vector code generation is supported there is one map for scalar values
600 // and one for vector values.
601 //
602 // In case we just do scalar code generation, the vectorMap is not used and
603 // the scalarMap has just one dimension, which contains the mapping.
604 //
605 // In case vector code generation is done, an instruction may either appear
606 // in the vector map once (as it is calculating >vectorwidth< values at a
607 // time. Or (if the values are calculated using scalar operations), it
608 // appears once in every dimension of the scalarMap.
609 VectorValueMapT ScalarBlockMap(getVectorWidth());
610 ValueMapT VectorBlockMap;
611
Tobias Grosser91f5b262014-06-04 08:06:40 +0000612 for (Instruction &Inst : *BB)
613 copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000614}