blob: 8788151dcceaaf9ac4d7637ab8117749161289ff [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 Zhengfe11e282013-06-29 13:22:15 +000091 if (Value *New = BBMap.lookup(Old))
92 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +000093
Tobias Grossere71c6ab2012-04-27 16:36:14 +000094 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +000095 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +000096 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +000097 const SCEV *NewScev = apply(Scev, LTS, SE);
98 ValueToValueMap VTV;
99 VTV.insert(BBMap.begin(), BBMap.end());
100 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000101 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000102 SCEVExpander Expander(SE, "polly");
103 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
104 Builder.GetInsertPoint());
105
106 BBMap[Old] = Expanded;
107 return Expanded;
108 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000109 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000110
Tobias Grosser16371ac2014-11-05 20:48:56 +0000111 // A scop-constant value defined by a global or a function parameter.
112 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
113 return const_cast<Value *>(Old);
114
115 // A scop-constant value defined by an instruction executed outside the scop.
116 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
117 if (!Statement.getParent()->getRegion().contains(Inst->getParent()))
118 return const_cast<Value *>(Old);
119
120 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000121 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000122 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000123}
124
125void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000126 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000127 // We do not generate debug intrinsics as we did not investigate how to
128 // copy them correctly. At the current state, they just crash the code
129 // generation as the meta-data operands are not correctly copied.
130 if (isa<DbgInfoIntrinsic>(Inst))
131 return;
132
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000133 Instruction *NewInst = Inst->clone();
134
135 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000136 for (Value *OldOperand : Inst->operands()) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000137 Value *NewOperand =
138 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000139
140 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000141 assert(!isa<StoreInst>(NewInst) &&
142 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000143 delete NewInst;
144 return;
145 }
146
147 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
148 }
149
150 Builder.Insert(NewInst);
151 BBMap[Inst] = NewInst;
152
153 if (!NewInst->getType()->isVoidTy())
154 NewInst->setName("p_" + Inst->getName());
155}
156
Johannes Doerferta63b2572014-08-03 01:51:59 +0000157Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000158 isl_pw_multi_aff *PWAccRel;
159 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000160 isl_ast_expr *Expr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000161
Johannes Doerferta63b2572014-08-03 01:51:59 +0000162 assert(ExprBuilder && Build &&
163 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000164
Johannes Doerferta99130f2014-10-13 12:58:03 +0000165 Schedule = isl_ast_build_get_schedule(Build);
166 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000167
Johannes Doerferta63b2572014-08-03 01:51:59 +0000168 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000169 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000170
171 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000172}
173
Tobias Grossere602a072013-05-07 07:30:56 +0000174Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
175 const Value *Pointer,
176 ValueMapT &BBMap,
177 ValueMapT &GlobalMap,
178 LoopToScevMapT &LTS) {
Johannes Doerferta63b2572014-08-03 01:51:59 +0000179 const MemoryAccess &MA = Statement.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000180
181 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000182 if (MA.hasNewAccessRelation())
Johannes Doerferta63b2572014-08-03 01:51:59 +0000183 NewPointer = getNewAccessOperand(MA);
184 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000185 NewPointer =
186 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000187
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000188 return NewPointer;
189}
190
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000191Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000192 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000193}
194
Tobias Grossere602a072013-05-07 07:30:56 +0000195Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
196 ValueMapT &BBMap,
197 ValueMapT &GlobalMap,
198 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000199 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000200 Value *NewPointer =
Johannes Doerfert1947f862014-10-08 20:18:32 +0000201 generateLocationAccessed(Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000202 Value *ScalarLoad = Builder.CreateAlignedLoad(
203 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000204 return ScalarLoad;
205}
206
Tobias Grossere602a072013-05-07 07:30:56 +0000207Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
208 ValueMapT &BBMap,
209 ValueMapT &GlobalMap,
210 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000211 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000212 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000213 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000214 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
215 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000216
Johannes Doerfert87901452014-10-02 16:22:19 +0000217 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
218 Store->getAlignment());
219 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000220}
221
Tobias Grossere602a072013-05-07 07:30:56 +0000222void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
223 ValueMapT &GlobalMap,
224 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000225 // Terminator instructions control the control flow. They are explicitly
226 // expressed in the clast and do not need to be copied.
227 if (Inst->isTerminator())
228 return;
229
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000230 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
231 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000232 return;
233
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000234 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000235 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000236 // Compute NewLoad before its insertion in BBMap to make the insertion
237 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000238 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000239 return;
240 }
241
242 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000243 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000244 // Compute NewStore before its insertion in BBMap to make the insertion
245 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000246 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000247 return;
248 }
249
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000250 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000251}
252
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000253void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000254 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000255 BasicBlock *CopyBB =
256 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000257 CopyBB->setName("polly.stmt." + BB->getName());
258 Builder.SetInsertPoint(CopyBB->begin());
259
260 ValueMapT BBMap;
261
Tobias Grosser91f5b262014-06-04 08:06:40 +0000262 for (Instruction &Inst : *BB)
263 copyInstruction(&Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000264}
265
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000266VectorBlockGenerator::VectorBlockGenerator(
267 PollyIRBuilder &B, VectorValueMapT &GlobalMaps,
268 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
Johannes Doerfert731685e2014-10-08 17:25:30 +0000269 __isl_keep isl_map *Schedule, Pass *P, LoopInfo &LI, ScalarEvolution &SE,
270 __isl_keep isl_ast_build *Build, IslExprBuilder *ExprBuilder)
271 : BlockGenerator(B, Stmt, P, LI, SE, Build, ExprBuilder),
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000272 GlobalMaps(GlobalMaps), VLTS(VLTS), Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000273 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
274 assert(Schedule && "No statement domain provided");
275}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000276
Tobias Grossere602a072013-05-07 07:30:56 +0000277Value *VectorBlockGenerator::getVectorValue(const Value *Old,
278 ValueMapT &VectorMap,
279 VectorValueMapT &ScalarMaps,
280 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000281 if (Value *NewValue = VectorMap.lookup(Old))
282 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000283
284 int Width = getVectorWidth();
285
286 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
287
288 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000289 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000290 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000291 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000292 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000293
294 VectorMap[Old] = Vector;
295
296 return Vector;
297}
298
299Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
300 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
301 assert(PointerTy && "PointerType expected");
302
303 Type *ScalarType = PointerTy->getElementType();
304 VectorType *VectorType = VectorType::get(ScalarType, Width);
305
306 return PointerType::getUnqual(VectorType);
307}
308
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000309Value *
310VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
311 VectorValueMapT &ScalarMaps,
312 bool NegativeStride = false) {
313 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000314 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000315 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
316 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
317
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000318 Value *NewPointer = nullptr;
Johannes Doerfert731685e2014-10-08 17:25:30 +0000319 NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[Offset],
320 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000321 Value *VectorPtr =
322 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
323 LoadInst *VecLoad =
324 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000325 if (!Aligned)
326 VecLoad->setAlignment(8);
327
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000328 if (NegativeStride) {
329 SmallVector<Constant *, 16> Indices;
330 for (int i = VectorWidth - 1; i >= 0; i--)
331 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
332 Constant *SV = llvm::ConstantVector::get(Indices);
333 Value *RevVecLoad = Builder.CreateShuffleVector(
334 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
335 return RevVecLoad;
336 }
337
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000338 return VecLoad;
339}
340
341Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
342 ValueMapT &BBMap) {
343 const Value *Pointer = Load->getPointerOperand();
344 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000345 Value *NewPointer =
Johannes Doerfert731685e2014-10-08 17:25:30 +0000346 generateLocationAccessed(Load, Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000347 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
348 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000349 LoadInst *ScalarLoad =
350 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000351
352 if (!Aligned)
353 ScalarLoad->setAlignment(8);
354
Tobias Grosserc14582f2013-02-05 18:01:29 +0000355 Constant *SplatVector = Constant::getNullValue(
356 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000357
Tobias Grosserc14582f2013-02-05 18:01:29 +0000358 Value *VectorLoad = Builder.CreateShuffleVector(
359 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000360 return VectorLoad;
361}
362
Tobias Grossere602a072013-05-07 07:30:56 +0000363Value *
364VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
365 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000366 int VectorWidth = getVectorWidth();
367 const Value *Pointer = Load->getPointerOperand();
368 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000369 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000370
371 Value *Vector = UndefValue::get(VectorType);
372
373 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfert731685e2014-10-08 17:25:30 +0000374 Value *NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[i],
375 GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000376 Value *ScalarLoad =
377 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
378 Vector = Builder.CreateInsertElement(
379 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000380 }
381
382 return Vector;
383}
384
Tobias Grossere602a072013-05-07 07:30:56 +0000385void VectorBlockGenerator::generateLoad(const LoadInst *Load,
386 ValueMapT &VectorMap,
387 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000388 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
389 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000390 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000391 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000392 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000393 return;
394 }
395
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000396 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000397
Tobias Grosser95493982014-04-18 09:46:35 +0000398 // Make sure we have scalar values available to access the pointer to
399 // the data location.
400 extractScalarValues(Load, VectorMap, ScalarMaps);
401
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000402 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000403 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000404 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000405 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000406 NewLoad = generateStrideOneLoad(Load, ScalarMaps);
407 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
408 NewLoad = generateStrideOneLoad(Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000409 else
410 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
411
412 VectorMap[Load] = NewLoad;
413}
414
415void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
416 ValueMapT &VectorMap,
417 VectorValueMapT &ScalarMaps) {
418 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000419 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
420 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000421
422 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
423
424 const CastInst *Cast = dyn_cast<CastInst>(Inst);
425 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
426 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
427}
428
429void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
430 ValueMapT &VectorMap,
431 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000432 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000433 Value *OpZero = Inst->getOperand(0);
434 Value *OpOne = Inst->getOperand(1);
435
436 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000437 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
438 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000439
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000440 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000441 Inst->getName() + "p_vec");
442 VectorMap[Inst] = NewInst;
443}
444
Tobias Grossere602a072013-05-07 07:30:56 +0000445void VectorBlockGenerator::copyStore(const StoreInst *Store,
446 ValueMapT &VectorMap,
447 VectorValueMapT &ScalarMaps) {
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000448 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000449
450 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000451 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
452 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000453
Tobias Grosser50fd7012014-04-17 23:13:49 +0000454 // Make sure we have scalar values available to access the pointer to
455 // the data location.
456 extractScalarValues(Store, VectorMap, ScalarMaps);
457
Sebastian Popa00a0292012-12-18 07:46:06 +0000458 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000459 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfert731685e2014-10-08 17:25:30 +0000460 Value *NewPointer = generateLocationAccessed(Store, Pointer, ScalarMaps[0],
461 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000462
Tobias Grosserc14582f2013-02-05 18:01:29 +0000463 Value *VectorPtr =
464 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000465 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
466
467 if (!Aligned)
468 Store->setAlignment(8);
469 } else {
470 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000471 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000472 Value *NewPointer = generateLocationAccessed(
473 Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000474 Builder.CreateStore(Scalar, NewPointer);
475 }
476 }
477}
478
479bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
480 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000481 for (Value *Operand : Inst->operands())
482 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000483 return true;
484 return false;
485}
486
487bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
488 ValueMapT &VectorMap,
489 VectorValueMapT &ScalarMaps) {
490 bool HasVectorOperand = false;
491 int VectorWidth = getVectorWidth();
492
Tobias Grosser91f5b262014-06-04 08:06:40 +0000493 for (Value *Operand : Inst->operands()) {
494 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000495
496 if (VecOp == VectorMap.end())
497 continue;
498
499 HasVectorOperand = true;
500 Value *NewVector = VecOp->second;
501
502 for (int i = 0; i < VectorWidth; ++i) {
503 ValueMapT &SM = ScalarMaps[i];
504
505 // If there is one scalar extracted, all scalar elements should have
506 // already been extracted by the code here. So no need to check for the
507 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000508 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000509 break;
510
Tobias Grosser91f5b262014-06-04 08:06:40 +0000511 SM[Operand] =
512 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000513 }
514 }
515
516 return HasVectorOperand;
517}
518
519void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
520 ValueMapT &VectorMap,
521 VectorValueMapT &ScalarMaps) {
522 bool HasVectorOperand;
523 int VectorWidth = getVectorWidth();
524
525 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
526
527 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfert731685e2014-10-08 17:25:30 +0000528 BlockGenerator::copyInstruction(Inst, ScalarMaps[VectorLane],
529 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000530
531 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
532 return;
533
534 // Make the result available as vector value.
535 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
536 Value *Vector = UndefValue::get(VectorType);
537
538 for (int i = 0; i < VectorWidth; i++)
539 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
540 Builder.getInt32(i));
541
542 VectorMap[Inst] = Vector;
543}
544
Tobias Grosserc14582f2013-02-05 18:01:29 +0000545int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000546
547void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
548 ValueMapT &VectorMap,
549 VectorValueMapT &ScalarMaps) {
550 // Terminator instructions control the control flow. They are explicitly
551 // expressed in the clast and do not need to be copied.
552 if (Inst->isTerminator())
553 return;
554
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000555 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
556 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000557 return;
558
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000559 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
560 generateLoad(Load, VectorMap, ScalarMaps);
561 return;
562 }
563
564 if (hasVectorOperands(Inst, VectorMap)) {
565 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
566 copyStore(Store, VectorMap, ScalarMaps);
567 return;
568 }
569
570 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
571 copyUnaryInst(Unary, VectorMap, ScalarMaps);
572 return;
573 }
574
575 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
576 copyBinaryInst(Binary, VectorMap, ScalarMaps);
577 return;
578 }
579
580 // Falltrough: We generate scalar instructions, if we don't know how to
581 // generate vector code.
582 }
583
584 copyInstScalarized(Inst, VectorMap, ScalarMaps);
585}
586
587void VectorBlockGenerator::copyBB() {
588 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000589 BasicBlock *CopyBB =
590 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000591 CopyBB->setName("polly.stmt." + BB->getName());
592 Builder.SetInsertPoint(CopyBB->begin());
593
594 // Create two maps that store the mapping from the original instructions of
595 // the old basic block to their copies in the new basic block. Those maps
596 // are basic block local.
597 //
598 // As vector code generation is supported there is one map for scalar values
599 // and one for vector values.
600 //
601 // In case we just do scalar code generation, the vectorMap is not used and
602 // the scalarMap has just one dimension, which contains the mapping.
603 //
604 // In case vector code generation is done, an instruction may either appear
605 // in the vector map once (as it is calculating >vectorwidth< values at a
606 // time. Or (if the values are calculated using scalar operations), it
607 // appears once in every dimension of the scalarMap.
608 VectorValueMapT ScalarBlockMap(getVectorWidth());
609 ValueMapT VectorBlockMap;
610
Tobias Grosser91f5b262014-06-04 08:06:40 +0000611 for (Instruction &Inst : *BB)
612 copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000613}