blob: 73fb6e23697dc1b1a2ee6e338f20f5b8494b0bdb [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
Hongbin Zheng5b463ce2013-07-25 09:12:07 +000075Value *BlockGenerator::lookupAvailableValue(const Value *Old, ValueMapT &BBMap,
76 ValueMapT &GlobalMap) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000077 // We assume constants never change.
78 // This avoids map lookups for many calls to this function.
79 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000080 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000081
Hongbin Zhengfe11e282013-06-29 13:22:15 +000082 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +000083 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +000084 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +000085 New = Builder.CreateTruncOrBitCast(New, Old->getType());
86
87 return New;
88 }
89
Hongbin Zheng5b463ce2013-07-25 09:12:07 +000090 // Or it is probably a scop-constant value defined as global, function
91 // parameter or an instruction not within the scop.
92 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
93 return const_cast<Value *>(Old);
94
95 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
96 if (!Statement.getParent()->getRegion().contains(Inst->getParent()))
97 return const_cast<Value *>(Old);
98
Hongbin Zhengfe11e282013-06-29 13:22:15 +000099 if (Value *New = BBMap.lookup(Old))
100 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000101
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000102 return nullptr;
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000103}
104
105Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
106 ValueMapT &GlobalMap, LoopToScevMapT &LTS,
107 Loop *L) {
108 if (Value *New = lookupAvailableValue(Old, BBMap, GlobalMap))
109 return New;
110
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000111 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000112 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000113 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000114 const SCEV *NewScev = apply(Scev, LTS, SE);
115 ValueToValueMap VTV;
116 VTV.insert(BBMap.begin(), BBMap.end());
117 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000118 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000119 SCEVExpander Expander(SE, "polly");
120 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
121 Builder.GetInsertPoint());
122
123 BBMap[Old] = Expanded;
124 return Expanded;
125 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000126 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000127
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000128 // Now the scalar dependence is neither available nor SCEVCodegenable, this
129 // should never happen in the current code generator.
130 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000131 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000132}
133
134void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000135 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000136 // We do not generate debug intrinsics as we did not investigate how to
137 // copy them correctly. At the current state, they just crash the code
138 // generation as the meta-data operands are not correctly copied.
139 if (isa<DbgInfoIntrinsic>(Inst))
140 return;
141
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000142 Instruction *NewInst = Inst->clone();
143
144 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000145 for (Value *OldOperand : Inst->operands()) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000146 Value *NewOperand =
147 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000148
149 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000150 assert(!isa<StoreInst>(NewInst) &&
151 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000152 delete NewInst;
153 return;
154 }
155
156 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
157 }
158
159 Builder.Insert(NewInst);
160 BBMap[Inst] = NewInst;
161
162 if (!NewInst->getType()->isVoidTy())
163 NewInst->setName("p_" + Inst->getName());
164}
165
Johannes Doerferta63b2572014-08-03 01:51:59 +0000166Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000167 isl_pw_multi_aff *PWAccRel;
168 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000169 isl_ast_expr *Expr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000170
Johannes Doerferta63b2572014-08-03 01:51:59 +0000171 assert(ExprBuilder && Build &&
172 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000173
Johannes Doerferta99130f2014-10-13 12:58:03 +0000174 Schedule = isl_ast_build_get_schedule(Build);
175 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000176
Johannes Doerferta63b2572014-08-03 01:51:59 +0000177 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000178 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000179
180 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000181}
182
Tobias Grossere602a072013-05-07 07:30:56 +0000183Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
184 const Value *Pointer,
185 ValueMapT &BBMap,
186 ValueMapT &GlobalMap,
187 LoopToScevMapT &LTS) {
Johannes Doerferta63b2572014-08-03 01:51:59 +0000188 const MemoryAccess &MA = Statement.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000189
190 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000191 if (MA.hasNewAccessRelation())
Johannes Doerferta63b2572014-08-03 01:51:59 +0000192 NewPointer = getNewAccessOperand(MA);
193 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000194 NewPointer =
195 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000196
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000197 return NewPointer;
198}
199
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000200Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000201 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000202}
203
Tobias Grossere602a072013-05-07 07:30:56 +0000204Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
205 ValueMapT &BBMap,
206 ValueMapT &GlobalMap,
207 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000208 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000209 Value *NewPointer =
Johannes Doerfert1947f862014-10-08 20:18:32 +0000210 generateLocationAccessed(Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000211 Value *ScalarLoad = Builder.CreateAlignedLoad(
212 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000213 return ScalarLoad;
214}
215
Tobias Grossere602a072013-05-07 07:30:56 +0000216Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
217 ValueMapT &BBMap,
218 ValueMapT &GlobalMap,
219 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000220 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000221 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000222 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000223 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
224 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000225
Johannes Doerfert87901452014-10-02 16:22:19 +0000226 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
227 Store->getAlignment());
228 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000229}
230
Tobias Grossere602a072013-05-07 07:30:56 +0000231void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
232 ValueMapT &GlobalMap,
233 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000234 // Terminator instructions control the control flow. They are explicitly
235 // expressed in the clast and do not need to be copied.
236 if (Inst->isTerminator())
237 return;
238
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000239 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
240 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000241 return;
242
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000243 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000244 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000245 // Compute NewLoad before its insertion in BBMap to make the insertion
246 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000247 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000248 return;
249 }
250
251 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000252 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000253 // Compute NewStore before its insertion in BBMap to make the insertion
254 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000255 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000256 return;
257 }
258
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000259 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000260}
261
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000262void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000263 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000264 BasicBlock *CopyBB =
265 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000266 CopyBB->setName("polly.stmt." + BB->getName());
267 Builder.SetInsertPoint(CopyBB->begin());
268
269 ValueMapT BBMap;
270
Tobias Grosser91f5b262014-06-04 08:06:40 +0000271 for (Instruction &Inst : *BB)
272 copyInstruction(&Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000273}
274
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000275VectorBlockGenerator::VectorBlockGenerator(
276 PollyIRBuilder &B, VectorValueMapT &GlobalMaps,
277 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
Johannes Doerfert731685e2014-10-08 17:25:30 +0000278 __isl_keep isl_map *Schedule, Pass *P, LoopInfo &LI, ScalarEvolution &SE,
279 __isl_keep isl_ast_build *Build, IslExprBuilder *ExprBuilder)
280 : BlockGenerator(B, Stmt, P, LI, SE, Build, ExprBuilder),
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000281 GlobalMaps(GlobalMaps), VLTS(VLTS), Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000282 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
283 assert(Schedule && "No statement domain provided");
284}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000285
Tobias Grossere602a072013-05-07 07:30:56 +0000286Value *VectorBlockGenerator::getVectorValue(const Value *Old,
287 ValueMapT &VectorMap,
288 VectorValueMapT &ScalarMaps,
289 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000290 if (Value *NewValue = VectorMap.lookup(Old))
291 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000292
293 int Width = getVectorWidth();
294
295 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
296
297 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000298 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000299 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000300 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000301 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000302
303 VectorMap[Old] = Vector;
304
305 return Vector;
306}
307
308Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
309 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
310 assert(PointerTy && "PointerType expected");
311
312 Type *ScalarType = PointerTy->getElementType();
313 VectorType *VectorType = VectorType::get(ScalarType, Width);
314
315 return PointerType::getUnqual(VectorType);
316}
317
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000318Value *
319VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
320 VectorValueMapT &ScalarMaps,
321 bool NegativeStride = false) {
322 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000323 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000324 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
325 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
326
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000327 Value *NewPointer = nullptr;
Johannes Doerfert731685e2014-10-08 17:25:30 +0000328 NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[Offset],
329 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000330 Value *VectorPtr =
331 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
332 LoadInst *VecLoad =
333 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000334 if (!Aligned)
335 VecLoad->setAlignment(8);
336
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000337 if (NegativeStride) {
338 SmallVector<Constant *, 16> Indices;
339 for (int i = VectorWidth - 1; i >= 0; i--)
340 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
341 Constant *SV = llvm::ConstantVector::get(Indices);
342 Value *RevVecLoad = Builder.CreateShuffleVector(
343 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
344 return RevVecLoad;
345 }
346
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000347 return VecLoad;
348}
349
350Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
351 ValueMapT &BBMap) {
352 const Value *Pointer = Load->getPointerOperand();
353 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000354 Value *NewPointer =
Johannes Doerfert731685e2014-10-08 17:25:30 +0000355 generateLocationAccessed(Load, Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000356 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
357 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000358 LoadInst *ScalarLoad =
359 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000360
361 if (!Aligned)
362 ScalarLoad->setAlignment(8);
363
Tobias Grosserc14582f2013-02-05 18:01:29 +0000364 Constant *SplatVector = Constant::getNullValue(
365 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000366
Tobias Grosserc14582f2013-02-05 18:01:29 +0000367 Value *VectorLoad = Builder.CreateShuffleVector(
368 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000369 return VectorLoad;
370}
371
Tobias Grossere602a072013-05-07 07:30:56 +0000372Value *
373VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
374 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000375 int VectorWidth = getVectorWidth();
376 const Value *Pointer = Load->getPointerOperand();
377 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000378 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000379
380 Value *Vector = UndefValue::get(VectorType);
381
382 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfert731685e2014-10-08 17:25:30 +0000383 Value *NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[i],
384 GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000385 Value *ScalarLoad =
386 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
387 Vector = Builder.CreateInsertElement(
388 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000389 }
390
391 return Vector;
392}
393
Tobias Grossere602a072013-05-07 07:30:56 +0000394void VectorBlockGenerator::generateLoad(const LoadInst *Load,
395 ValueMapT &VectorMap,
396 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000397 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
398 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000399 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000400 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000401 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000402 return;
403 }
404
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000405 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000406
Tobias Grosser95493982014-04-18 09:46:35 +0000407 // Make sure we have scalar values available to access the pointer to
408 // the data location.
409 extractScalarValues(Load, VectorMap, ScalarMaps);
410
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000411 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000412 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000413 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000414 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000415 NewLoad = generateStrideOneLoad(Load, ScalarMaps);
416 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
417 NewLoad = generateStrideOneLoad(Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000418 else
419 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
420
421 VectorMap[Load] = NewLoad;
422}
423
424void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
425 ValueMapT &VectorMap,
426 VectorValueMapT &ScalarMaps) {
427 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000428 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
429 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000430
431 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
432
433 const CastInst *Cast = dyn_cast<CastInst>(Inst);
434 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
435 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
436}
437
438void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
439 ValueMapT &VectorMap,
440 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000441 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000442 Value *OpZero = Inst->getOperand(0);
443 Value *OpOne = Inst->getOperand(1);
444
445 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000446 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
447 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000448
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000449 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000450 Inst->getName() + "p_vec");
451 VectorMap[Inst] = NewInst;
452}
453
Tobias Grossere602a072013-05-07 07:30:56 +0000454void VectorBlockGenerator::copyStore(const StoreInst *Store,
455 ValueMapT &VectorMap,
456 VectorValueMapT &ScalarMaps) {
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000457 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000458
459 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000460 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
461 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000462
Tobias Grosser50fd7012014-04-17 23:13:49 +0000463 // Make sure we have scalar values available to access the pointer to
464 // the data location.
465 extractScalarValues(Store, VectorMap, ScalarMaps);
466
Sebastian Popa00a0292012-12-18 07:46:06 +0000467 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000468 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfert731685e2014-10-08 17:25:30 +0000469 Value *NewPointer = generateLocationAccessed(Store, Pointer, ScalarMaps[0],
470 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000471
Tobias Grosserc14582f2013-02-05 18:01:29 +0000472 Value *VectorPtr =
473 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000474 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
475
476 if (!Aligned)
477 Store->setAlignment(8);
478 } else {
479 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000480 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000481 Value *NewPointer = generateLocationAccessed(
482 Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000483 Builder.CreateStore(Scalar, NewPointer);
484 }
485 }
486}
487
488bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
489 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000490 for (Value *Operand : Inst->operands())
491 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000492 return true;
493 return false;
494}
495
496bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
497 ValueMapT &VectorMap,
498 VectorValueMapT &ScalarMaps) {
499 bool HasVectorOperand = false;
500 int VectorWidth = getVectorWidth();
501
Tobias Grosser91f5b262014-06-04 08:06:40 +0000502 for (Value *Operand : Inst->operands()) {
503 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000504
505 if (VecOp == VectorMap.end())
506 continue;
507
508 HasVectorOperand = true;
509 Value *NewVector = VecOp->second;
510
511 for (int i = 0; i < VectorWidth; ++i) {
512 ValueMapT &SM = ScalarMaps[i];
513
514 // If there is one scalar extracted, all scalar elements should have
515 // already been extracted by the code here. So no need to check for the
516 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000517 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000518 break;
519
Tobias Grosser91f5b262014-06-04 08:06:40 +0000520 SM[Operand] =
521 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000522 }
523 }
524
525 return HasVectorOperand;
526}
527
528void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
529 ValueMapT &VectorMap,
530 VectorValueMapT &ScalarMaps) {
531 bool HasVectorOperand;
532 int VectorWidth = getVectorWidth();
533
534 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
535
536 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfert731685e2014-10-08 17:25:30 +0000537 BlockGenerator::copyInstruction(Inst, ScalarMaps[VectorLane],
538 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000539
540 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
541 return;
542
543 // Make the result available as vector value.
544 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
545 Value *Vector = UndefValue::get(VectorType);
546
547 for (int i = 0; i < VectorWidth; i++)
548 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
549 Builder.getInt32(i));
550
551 VectorMap[Inst] = Vector;
552}
553
Tobias Grosserc14582f2013-02-05 18:01:29 +0000554int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000555
556void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
557 ValueMapT &VectorMap,
558 VectorValueMapT &ScalarMaps) {
559 // Terminator instructions control the control flow. They are explicitly
560 // expressed in the clast and do not need to be copied.
561 if (Inst->isTerminator())
562 return;
563
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000564 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
565 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000566 return;
567
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000568 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
569 generateLoad(Load, VectorMap, ScalarMaps);
570 return;
571 }
572
573 if (hasVectorOperands(Inst, VectorMap)) {
574 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
575 copyStore(Store, VectorMap, ScalarMaps);
576 return;
577 }
578
579 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
580 copyUnaryInst(Unary, VectorMap, ScalarMaps);
581 return;
582 }
583
584 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
585 copyBinaryInst(Binary, VectorMap, ScalarMaps);
586 return;
587 }
588
589 // Falltrough: We generate scalar instructions, if we don't know how to
590 // generate vector code.
591 }
592
593 copyInstScalarized(Inst, VectorMap, ScalarMaps);
594}
595
596void VectorBlockGenerator::copyBB() {
597 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000598 BasicBlock *CopyBB =
599 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000600 CopyBB->setName("polly.stmt." + BB->getName());
601 Builder.SetInsertPoint(CopyBB->begin());
602
603 // Create two maps that store the mapping from the original instructions of
604 // the old basic block to their copies in the new basic block. Those maps
605 // are basic block local.
606 //
607 // As vector code generation is supported there is one map for scalar values
608 // and one for vector values.
609 //
610 // In case we just do scalar code generation, the vectorMap is not used and
611 // the scalarMap has just one dimension, which contains the mapping.
612 //
613 // In case vector code generation is done, an instruction may either appear
614 // in the vector map once (as it is calculating >vectorwidth< values at a
615 // time. Or (if the values are calculated using scalar operations), it
616 // appears once in every dimension of the scalarMap.
617 VectorValueMapT ScalarBlockMap(getVectorWidth());
618 ValueMapT VectorBlockMap;
619
Tobias Grosser91f5b262014-06-04 08:06:40 +0000620 for (Instruction &Inst : *BB)
621 copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000622}