blob: 16621e4841ad9d9ec6a976d9a953c5b416ae458c [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
37static cl::opt<bool>
Tobias Grosser483a90d2014-07-09 10:50:10 +000038 Aligned("enable-polly-aligned",
39 cl::desc("Assumed aligned memory accesses."), cl::Hidden,
40 cl::value_desc("OpenMP code generation enabled if true"),
41 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000042
Tobias Grossere602a072013-05-07 07:30:56 +000043static cl::opt<bool, true>
Tobias Grosser483a90d2014-07-09 10:50:10 +000044 SCEVCodegenF("polly-codegen-scev",
45 cl::desc("Use SCEV based code generation."), cl::Hidden,
46 cl::location(SCEVCodegen), cl::init(false), cl::ZeroOrMore,
47 cl::cat(PollyCategory));
Tobias Grosserecfe21b2013-03-20 18:03:18 +000048
49bool polly::SCEVCodegen;
50
51bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
52 ScalarEvolution *SE, const Region *R) {
53 if (SCEVCodegen) {
54 if (!I || !SE->isSCEVable(I->getType()))
55 return false;
56
57 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
58 if (!isa<SCEVCouldNotCompute>(Scev))
59 if (!hasScalarDepsInsideRegion(Scev, R))
60 return true;
61
62 return false;
63 }
64
65 Loop *L = LI->getLoopFor(I->getParent());
Tobias Grossere8df5bd2013-04-17 07:20:36 +000066 return L && I == L->getCanonicalInductionVariable() && R->contains(L);
Tobias Grosserecfe21b2013-03-20 18:03:18 +000067}
68
Johannes Doerferta63b2572014-08-03 01:51:59 +000069BlockGenerator::BlockGenerator(PollyIRBuilder &B, ScopStmt &Stmt, Pass *P,
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000070 LoopInfo &LI, ScalarEvolution &SE,
Johannes Doerferta63b2572014-08-03 01:51:59 +000071 isl_ast_build *Build,
72 IslExprBuilder *ExprBuilder)
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000073 : Builder(B), Statement(Stmt), P(P), LI(LI), SE(SE), Build(Build),
74 ExprBuilder(ExprBuilder) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000075
Hongbin Zheng5b463ce2013-07-25 09:12:07 +000076Value *BlockGenerator::lookupAvailableValue(const Value *Old, ValueMapT &BBMap,
77 ValueMapT &GlobalMap) 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 Grosser5a56cbf2014-04-16 07:33:47 +0000103 return nullptr;
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000104}
105
106Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
107 ValueMapT &GlobalMap, LoopToScevMapT &LTS,
108 Loop *L) {
109 if (Value *New = lookupAvailableValue(Old, BBMap, GlobalMap))
110 return New;
111
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000112 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000113 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000114 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000115 const SCEV *NewScev = apply(Scev, LTS, SE);
116 ValueToValueMap VTV;
117 VTV.insert(BBMap.begin(), BBMap.end());
118 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000119 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000120 SCEVExpander Expander(SE, "polly");
121 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
122 Builder.GetInsertPoint());
123
124 BBMap[Old] = Expanded;
125 return Expanded;
126 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000127 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000128
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000129 // Now the scalar dependence is neither available nor SCEVCodegenable, this
130 // should never happen in the current code generator.
131 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000132 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000133}
134
135void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000136 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000137 // We do not generate debug intrinsics as we did not investigate how to
138 // copy them correctly. At the current state, they just crash the code
139 // generation as the meta-data operands are not correctly copied.
140 if (isa<DbgInfoIntrinsic>(Inst))
141 return;
142
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000143 Instruction *NewInst = Inst->clone();
144
145 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000146 for (Value *OldOperand : Inst->operands()) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000147 Value *NewOperand =
148 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000149
150 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000151 assert(!isa<StoreInst>(NewInst) &&
152 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000153 delete NewInst;
154 return;
155 }
156
157 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
158 }
159
160 Builder.Insert(NewInst);
161 BBMap[Inst] = NewInst;
162
163 if (!NewInst->getType()->isVoidTy())
164 NewInst->setName("p_" + Inst->getName());
165}
166
Johannes Doerferta63b2572014-08-03 01:51:59 +0000167Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) {
168 isl_pw_multi_aff *PWSchedule, *PWAccRel;
169 isl_union_map *ScheduleU;
170 isl_map *Schedule, *AccRel;
171 isl_ast_expr *Expr;
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 Doerferta63b2572014-08-03 01:51:59 +0000176 AccRel = MA.getNewAccessRelation();
177 assert(AccRel && "We generate new code only for new access relations!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000178
Johannes Doerferta63b2572014-08-03 01:51:59 +0000179 ScheduleU = isl_ast_build_get_schedule(Build);
180 ScheduleU = isl_union_map_intersect_domain(
181 ScheduleU, isl_union_set_from_set(MA.getStatement()->getDomain()));
182 Schedule = isl_map_from_union_map(ScheduleU);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000183
Johannes Doerferta63b2572014-08-03 01:51:59 +0000184 PWSchedule = isl_pw_multi_aff_from_map(isl_map_reverse(Schedule));
185 PWAccRel = isl_pw_multi_aff_from_map(AccRel);
186 PWAccRel = isl_pw_multi_aff_pullback_pw_multi_aff(PWAccRel, PWSchedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000187
Johannes Doerferta63b2572014-08-03 01:51:59 +0000188 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000189 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000190
191 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000192}
193
Tobias Grossere602a072013-05-07 07:30:56 +0000194Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
195 const Value *Pointer,
196 ValueMapT &BBMap,
197 ValueMapT &GlobalMap,
198 LoopToScevMapT &LTS) {
Johannes Doerferta63b2572014-08-03 01:51:59 +0000199 const MemoryAccess &MA = Statement.getAccessFor(Inst);
200 isl_map *NewAccRel = MA.getNewAccessRelation();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000201
202 Value *NewPointer;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000203 if (NewAccRel)
204 NewPointer = getNewAccessOperand(MA);
205 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000206 NewPointer =
207 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000208
Johannes Doerferta63b2572014-08-03 01:51:59 +0000209 isl_map_free(NewAccRel);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000210 return NewPointer;
211}
212
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000213Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000214 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000215}
216
Tobias Grossere602a072013-05-07 07:30:56 +0000217Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
218 ValueMapT &BBMap,
219 ValueMapT &GlobalMap,
220 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000221 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000222 Value *NewPointer =
Johannes Doerfert1947f862014-10-08 20:18:32 +0000223 generateLocationAccessed(Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000224 Value *ScalarLoad = Builder.CreateAlignedLoad(
225 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000226 return ScalarLoad;
227}
228
Tobias Grossere602a072013-05-07 07:30:56 +0000229Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
230 ValueMapT &BBMap,
231 ValueMapT &GlobalMap,
232 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000233 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000234 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000235 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000236 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
237 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000238
Johannes Doerfert87901452014-10-02 16:22:19 +0000239 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
240 Store->getAlignment());
241 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000242}
243
Tobias Grossere602a072013-05-07 07:30:56 +0000244void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
245 ValueMapT &GlobalMap,
246 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000247 // Terminator instructions control the control flow. They are explicitly
248 // expressed in the clast and do not need to be copied.
249 if (Inst->isTerminator())
250 return;
251
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000252 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
253 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000254 return;
255
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000256 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000257 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000258 // Compute NewLoad before its insertion in BBMap to make the insertion
259 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000260 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000261 return;
262 }
263
264 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000265 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000266 // Compute NewStore before its insertion in BBMap to make the insertion
267 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000268 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000269 return;
270 }
271
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000272 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000273}
274
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000275void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000276 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000277 BasicBlock *CopyBB =
278 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000279 CopyBB->setName("polly.stmt." + BB->getName());
280 Builder.SetInsertPoint(CopyBB->begin());
281
282 ValueMapT BBMap;
283
Tobias Grosser91f5b262014-06-04 08:06:40 +0000284 for (Instruction &Inst : *BB)
285 copyInstruction(&Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000286}
287
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000288VectorBlockGenerator::VectorBlockGenerator(
289 PollyIRBuilder &B, VectorValueMapT &GlobalMaps,
290 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
Johannes Doerfert731685e2014-10-08 17:25:30 +0000291 __isl_keep isl_map *Schedule, Pass *P, LoopInfo &LI, ScalarEvolution &SE,
292 __isl_keep isl_ast_build *Build, IslExprBuilder *ExprBuilder)
293 : BlockGenerator(B, Stmt, P, LI, SE, Build, ExprBuilder),
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000294 GlobalMaps(GlobalMaps), VLTS(VLTS), Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000295 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
296 assert(Schedule && "No statement domain provided");
297}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000298
Tobias Grossere602a072013-05-07 07:30:56 +0000299Value *VectorBlockGenerator::getVectorValue(const Value *Old,
300 ValueMapT &VectorMap,
301 VectorValueMapT &ScalarMaps,
302 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000303 if (Value *NewValue = VectorMap.lookup(Old))
304 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000305
306 int Width = getVectorWidth();
307
308 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
309
310 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000311 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000312 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000313 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000314 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000315
316 VectorMap[Old] = Vector;
317
318 return Vector;
319}
320
321Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
322 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
323 assert(PointerTy && "PointerType expected");
324
325 Type *ScalarType = PointerTy->getElementType();
326 VectorType *VectorType = VectorType::get(ScalarType, Width);
327
328 return PointerType::getUnqual(VectorType);
329}
330
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000331Value *
332VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
333 VectorValueMapT &ScalarMaps,
334 bool NegativeStride = false) {
335 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000336 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000337 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
338 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
339
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000340 Value *NewPointer = nullptr;
Johannes Doerfert731685e2014-10-08 17:25:30 +0000341 NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[Offset],
342 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000343 Value *VectorPtr =
344 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
345 LoadInst *VecLoad =
346 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000347 if (!Aligned)
348 VecLoad->setAlignment(8);
349
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000350 if (NegativeStride) {
351 SmallVector<Constant *, 16> Indices;
352 for (int i = VectorWidth - 1; i >= 0; i--)
353 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
354 Constant *SV = llvm::ConstantVector::get(Indices);
355 Value *RevVecLoad = Builder.CreateShuffleVector(
356 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
357 return RevVecLoad;
358 }
359
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000360 return VecLoad;
361}
362
363Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
364 ValueMapT &BBMap) {
365 const Value *Pointer = Load->getPointerOperand();
366 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000367 Value *NewPointer =
Johannes Doerfert731685e2014-10-08 17:25:30 +0000368 generateLocationAccessed(Load, Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000369 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
370 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000371 LoadInst *ScalarLoad =
372 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000373
374 if (!Aligned)
375 ScalarLoad->setAlignment(8);
376
Tobias Grosserc14582f2013-02-05 18:01:29 +0000377 Constant *SplatVector = Constant::getNullValue(
378 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000379
Tobias Grosserc14582f2013-02-05 18:01:29 +0000380 Value *VectorLoad = Builder.CreateShuffleVector(
381 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000382 return VectorLoad;
383}
384
Tobias Grossere602a072013-05-07 07:30:56 +0000385Value *
386VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
387 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000388 int VectorWidth = getVectorWidth();
389 const Value *Pointer = Load->getPointerOperand();
390 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000391 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000392
393 Value *Vector = UndefValue::get(VectorType);
394
395 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfert731685e2014-10-08 17:25:30 +0000396 Value *NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[i],
397 GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000398 Value *ScalarLoad =
399 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
400 Vector = Builder.CreateInsertElement(
401 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000402 }
403
404 return Vector;
405}
406
Tobias Grossere602a072013-05-07 07:30:56 +0000407void VectorBlockGenerator::generateLoad(const LoadInst *Load,
408 ValueMapT &VectorMap,
409 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000410 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
411 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000412 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000413 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000414 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000415 return;
416 }
417
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000418 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000419
Tobias Grosser95493982014-04-18 09:46:35 +0000420 // Make sure we have scalar values available to access the pointer to
421 // the data location.
422 extractScalarValues(Load, VectorMap, ScalarMaps);
423
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000424 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000425 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000426 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000427 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000428 NewLoad = generateStrideOneLoad(Load, ScalarMaps);
429 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
430 NewLoad = generateStrideOneLoad(Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000431 else
432 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
433
434 VectorMap[Load] = NewLoad;
435}
436
437void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
438 ValueMapT &VectorMap,
439 VectorValueMapT &ScalarMaps) {
440 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000441 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
442 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000443
444 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
445
446 const CastInst *Cast = dyn_cast<CastInst>(Inst);
447 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
448 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
449}
450
451void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
452 ValueMapT &VectorMap,
453 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000454 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000455 Value *OpZero = Inst->getOperand(0);
456 Value *OpOne = Inst->getOperand(1);
457
458 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000459 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
460 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000461
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000462 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000463 Inst->getName() + "p_vec");
464 VectorMap[Inst] = NewInst;
465}
466
Tobias Grossere602a072013-05-07 07:30:56 +0000467void VectorBlockGenerator::copyStore(const StoreInst *Store,
468 ValueMapT &VectorMap,
469 VectorValueMapT &ScalarMaps) {
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000470 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000471
472 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000473 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
474 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000475
Tobias Grosser50fd7012014-04-17 23:13:49 +0000476 // Make sure we have scalar values available to access the pointer to
477 // the data location.
478 extractScalarValues(Store, VectorMap, ScalarMaps);
479
Sebastian Popa00a0292012-12-18 07:46:06 +0000480 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000481 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfert731685e2014-10-08 17:25:30 +0000482 Value *NewPointer = generateLocationAccessed(Store, Pointer, ScalarMaps[0],
483 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000484
Tobias Grosserc14582f2013-02-05 18:01:29 +0000485 Value *VectorPtr =
486 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000487 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
488
489 if (!Aligned)
490 Store->setAlignment(8);
491 } else {
492 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000493 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000494 Value *NewPointer = generateLocationAccessed(
495 Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000496 Builder.CreateStore(Scalar, NewPointer);
497 }
498 }
499}
500
501bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
502 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000503 for (Value *Operand : Inst->operands())
504 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000505 return true;
506 return false;
507}
508
509bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
510 ValueMapT &VectorMap,
511 VectorValueMapT &ScalarMaps) {
512 bool HasVectorOperand = false;
513 int VectorWidth = getVectorWidth();
514
Tobias Grosser91f5b262014-06-04 08:06:40 +0000515 for (Value *Operand : Inst->operands()) {
516 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000517
518 if (VecOp == VectorMap.end())
519 continue;
520
521 HasVectorOperand = true;
522 Value *NewVector = VecOp->second;
523
524 for (int i = 0; i < VectorWidth; ++i) {
525 ValueMapT &SM = ScalarMaps[i];
526
527 // If there is one scalar extracted, all scalar elements should have
528 // already been extracted by the code here. So no need to check for the
529 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000530 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000531 break;
532
Tobias Grosser91f5b262014-06-04 08:06:40 +0000533 SM[Operand] =
534 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000535 }
536 }
537
538 return HasVectorOperand;
539}
540
541void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
542 ValueMapT &VectorMap,
543 VectorValueMapT &ScalarMaps) {
544 bool HasVectorOperand;
545 int VectorWidth = getVectorWidth();
546
547 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
548
549 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfert731685e2014-10-08 17:25:30 +0000550 BlockGenerator::copyInstruction(Inst, ScalarMaps[VectorLane],
551 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000552
553 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
554 return;
555
556 // Make the result available as vector value.
557 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
558 Value *Vector = UndefValue::get(VectorType);
559
560 for (int i = 0; i < VectorWidth; i++)
561 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
562 Builder.getInt32(i));
563
564 VectorMap[Inst] = Vector;
565}
566
Tobias Grosserc14582f2013-02-05 18:01:29 +0000567int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000568
569void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
570 ValueMapT &VectorMap,
571 VectorValueMapT &ScalarMaps) {
572 // Terminator instructions control the control flow. They are explicitly
573 // expressed in the clast and do not need to be copied.
574 if (Inst->isTerminator())
575 return;
576
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000577 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
578 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000579 return;
580
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000581 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
582 generateLoad(Load, VectorMap, ScalarMaps);
583 return;
584 }
585
586 if (hasVectorOperands(Inst, VectorMap)) {
587 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
588 copyStore(Store, VectorMap, ScalarMaps);
589 return;
590 }
591
592 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
593 copyUnaryInst(Unary, VectorMap, ScalarMaps);
594 return;
595 }
596
597 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
598 copyBinaryInst(Binary, VectorMap, ScalarMaps);
599 return;
600 }
601
602 // Falltrough: We generate scalar instructions, if we don't know how to
603 // generate vector code.
604 }
605
606 copyInstScalarized(Inst, VectorMap, ScalarMaps);
607}
608
609void VectorBlockGenerator::copyBB() {
610 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000611 BasicBlock *CopyBB =
612 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000613 CopyBB->setName("polly.stmt." + BB->getName());
614 Builder.SetInsertPoint(CopyBB->begin());
615
616 // Create two maps that store the mapping from the original instructions of
617 // the old basic block to their copies in the new basic block. Those maps
618 // are basic block local.
619 //
620 // As vector code generation is supported there is one map for scalar values
621 // and one for vector values.
622 //
623 // In case we just do scalar code generation, the vectorMap is not used and
624 // the scalarMap has just one dimension, which contains the mapping.
625 //
626 // In case vector code generation is done, an instruction may either appear
627 // in the vector map once (as it is calculating >vectorwidth< values at a
628 // time. Or (if the values are calculated using scalar operations), it
629 // appears once in every dimension of the scalarMap.
630 VectorValueMapT ScalarBlockMap(getVectorWidth());
631 ValueMapT VectorBlockMap;
632
Tobias Grosser91f5b262014-06-04 08:06:40 +0000633 for (Instruction &Inst : *BB)
634 copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000635}