blob: f2eae1095861f0387f8b0cee78726697637c93a2 [file] [log] [blame]
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001//===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BlockGenerator and VectorBlockGenerator classes,
11// which generate sequential code and vectorized code for a polyhedral
12// statement, respectively.
13//
14//===----------------------------------------------------------------------===//
15
16#include "polly/ScopInfo.h"
Tobias Grosser83628182013-05-07 08:11:54 +000017#include "isl/aff.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000018#include "isl/ast.h"
19#include "isl/ast_build.h"
Tobias Grosser83628182013-05-07 08:11:54 +000020#include "isl/set.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000021#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000022#include "polly/CodeGen/CodeGeneration.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000023#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000024#include "polly/Options.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000025#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000026#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000027#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000028#include "llvm/Analysis/LoopInfo.h"
29#include "llvm/Analysis/ScalarEvolution.h"
30#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser030237d2014-02-21 15:06:05 +000031#include "llvm/IR/IntrinsicInst.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000032#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000033
Hongbin Zheng3b11a162012-04-25 13:16:49 +000034using namespace llvm;
35using namespace polly;
36
Tobias Grosser878aba42014-10-22 23:22:41 +000037static cl::opt<bool> Aligned("enable-polly-aligned",
38 cl::desc("Assumed aligned memory accesses."),
39 cl::Hidden, cl::init(false), cl::ZeroOrMore,
40 cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000041
Tobias Grosserecfe21b2013-03-20 18:03:18 +000042bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
43 ScalarEvolution *SE, const Region *R) {
Tobias Grosser683b8e42014-11-30 14:33:31 +000044 if (!I || !SE->isSCEVable(I->getType()))
Tobias Grosserecfe21b2013-03-20 18:03:18 +000045 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000046
Tobias Grosser683b8e42014-11-30 14:33:31 +000047 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
48 if (!isa<SCEVCouldNotCompute>(Scev))
49 if (!hasScalarDepsInsideRegion(Scev, R))
50 return true;
51
52 return false;
Tobias Grosserecfe21b2013-03-20 18:03:18 +000053}
54
Johannes Doerferta63b2572014-08-03 01:51:59 +000055BlockGenerator::BlockGenerator(PollyIRBuilder &B, ScopStmt &Stmt, Pass *P,
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000056 LoopInfo &LI, ScalarEvolution &SE,
Johannes Doerferta63b2572014-08-03 01:51:59 +000057 isl_ast_build *Build,
58 IslExprBuilder *ExprBuilder)
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000059 : Builder(B), Statement(Stmt), P(P), LI(LI), SE(SE), Build(Build),
60 ExprBuilder(ExprBuilder) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000061
Tobias Grosserd213a8b2014-11-05 19:46:04 +000062Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
63 ValueMapT &GlobalMap, LoopToScevMapT &LTS,
64 Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000065 // We assume constants never change.
66 // This avoids map lookups for many calls to this function.
67 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000068 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000069
Hongbin Zhengfe11e282013-06-29 13:22:15 +000070 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +000071 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +000072 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +000073 New = Builder.CreateTruncOrBitCast(New, Old->getType());
74
75 return New;
76 }
77
Hongbin Zhengfe11e282013-06-29 13:22:15 +000078 if (Value *New = BBMap.lookup(Old))
79 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +000080
Tobias Grosser683b8e42014-11-30 14:33:31 +000081 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +000082 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +000083 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +000084 const SCEV *NewScev = apply(Scev, LTS, SE);
85 ValueToValueMap VTV;
86 VTV.insert(BBMap.begin(), BBMap.end());
87 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +000088 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +000089 SCEVExpander Expander(SE, "polly");
90 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
91 Builder.GetInsertPoint());
92
93 BBMap[Old] = Expanded;
94 return Expanded;
95 }
Tobias Grosser369430f2013-03-22 23:42:53 +000096 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +000097
Tobias Grosser16371ac2014-11-05 20:48:56 +000098 // A scop-constant value defined by a global or a function parameter.
99 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
100 return const_cast<Value *>(Old);
101
102 // A scop-constant value defined by an instruction executed outside the scop.
103 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
104 if (!Statement.getParent()->getRegion().contains(Inst->getParent()))
105 return const_cast<Value *>(Old);
106
107 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000108 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000109 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000110}
111
112void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000113 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000114 // We do not generate debug intrinsics as we did not investigate how to
115 // copy them correctly. At the current state, they just crash the code
116 // generation as the meta-data operands are not correctly copied.
117 if (isa<DbgInfoIntrinsic>(Inst))
118 return;
119
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000120 Instruction *NewInst = Inst->clone();
121
122 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000123 for (Value *OldOperand : Inst->operands()) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000124 Value *NewOperand =
125 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000126
127 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000128 assert(!isa<StoreInst>(NewInst) &&
129 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000130 delete NewInst;
131 return;
132 }
133
134 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
135 }
136
137 Builder.Insert(NewInst);
138 BBMap[Inst] = NewInst;
139
140 if (!NewInst->getType()->isVoidTy())
141 NewInst->setName("p_" + Inst->getName());
142}
143
Johannes Doerferta63b2572014-08-03 01:51:59 +0000144Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000145 isl_pw_multi_aff *PWAccRel;
146 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000147 isl_ast_expr *Expr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000148
Johannes Doerferta63b2572014-08-03 01:51:59 +0000149 assert(ExprBuilder && Build &&
150 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000151
Johannes Doerferta99130f2014-10-13 12:58:03 +0000152 Schedule = isl_ast_build_get_schedule(Build);
153 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000154
Johannes Doerferta63b2572014-08-03 01:51:59 +0000155 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000156 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000157
158 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000159}
160
Tobias Grossere602a072013-05-07 07:30:56 +0000161Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
162 const Value *Pointer,
163 ValueMapT &BBMap,
164 ValueMapT &GlobalMap,
165 LoopToScevMapT &LTS) {
Johannes Doerferta63b2572014-08-03 01:51:59 +0000166 const MemoryAccess &MA = Statement.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000167
168 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000169 if (MA.hasNewAccessRelation())
Johannes Doerferta63b2572014-08-03 01:51:59 +0000170 NewPointer = getNewAccessOperand(MA);
171 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000172 NewPointer =
173 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000174
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000175 return NewPointer;
176}
177
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000178Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000179 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000180}
181
Tobias Grossere602a072013-05-07 07:30:56 +0000182Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
183 ValueMapT &BBMap,
184 ValueMapT &GlobalMap,
185 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000186 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000187 Value *NewPointer =
Johannes Doerfert1947f862014-10-08 20:18:32 +0000188 generateLocationAccessed(Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000189 Value *ScalarLoad = Builder.CreateAlignedLoad(
190 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000191 return ScalarLoad;
192}
193
Tobias Grossere602a072013-05-07 07:30:56 +0000194Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
195 ValueMapT &BBMap,
196 ValueMapT &GlobalMap,
197 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000198 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000199 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000200 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000201 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
202 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000203
Johannes Doerfert87901452014-10-02 16:22:19 +0000204 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
205 Store->getAlignment());
206 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000207}
208
Tobias Grossere602a072013-05-07 07:30:56 +0000209void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
210 ValueMapT &GlobalMap,
211 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000212 // Terminator instructions control the control flow. They are explicitly
213 // expressed in the clast and do not need to be copied.
214 if (Inst->isTerminator())
215 return;
216
Chandler Carruthf5579872015-01-17 14:16:56 +0000217 if (canSynthesize(Inst, &P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
218 &SE, &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000219 return;
220
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000221 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000222 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000223 // Compute NewLoad before its insertion in BBMap to make the insertion
224 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000225 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000226 return;
227 }
228
229 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000230 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000231 // Compute NewStore before its insertion in BBMap to make the insertion
232 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000233 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000234 return;
235 }
236
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000237 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000238}
239
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000240void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Chandler Carruth5ec33332015-01-18 10:52:23 +0000241 auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
242 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
243 auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
Tobias Grossera8cd1522015-01-18 15:59:16 +0000244 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
Chandler Carruth5ec33332015-01-18 10:52:23 +0000245
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000246 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000247 BasicBlock *CopyBB =
Chandler Carruth5ec33332015-01-18 10:52:23 +0000248 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), DT, LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000249 CopyBB->setName("polly.stmt." + BB->getName());
250 Builder.SetInsertPoint(CopyBB->begin());
251
252 ValueMapT BBMap;
253
Tobias Grosser91f5b262014-06-04 08:06:40 +0000254 for (Instruction &Inst : *BB)
255 copyInstruction(&Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000256}
257
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000258VectorBlockGenerator::VectorBlockGenerator(
259 PollyIRBuilder &B, VectorValueMapT &GlobalMaps,
260 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
Johannes Doerfert731685e2014-10-08 17:25:30 +0000261 __isl_keep isl_map *Schedule, Pass *P, LoopInfo &LI, ScalarEvolution &SE,
262 __isl_keep isl_ast_build *Build, IslExprBuilder *ExprBuilder)
263 : BlockGenerator(B, Stmt, P, LI, SE, Build, ExprBuilder),
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000264 GlobalMaps(GlobalMaps), VLTS(VLTS), Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000265 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
266 assert(Schedule && "No statement domain provided");
267}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000268
Tobias Grossere602a072013-05-07 07:30:56 +0000269Value *VectorBlockGenerator::getVectorValue(const Value *Old,
270 ValueMapT &VectorMap,
271 VectorValueMapT &ScalarMaps,
272 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000273 if (Value *NewValue = VectorMap.lookup(Old))
274 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000275
276 int Width = getVectorWidth();
277
278 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
279
280 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000281 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000282 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000283 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000284 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000285
286 VectorMap[Old] = Vector;
287
288 return Vector;
289}
290
291Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
292 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
293 assert(PointerTy && "PointerType expected");
294
295 Type *ScalarType = PointerTy->getElementType();
296 VectorType *VectorType = VectorType::get(ScalarType, Width);
297
298 return PointerType::getUnqual(VectorType);
299}
300
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000301Value *
302VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
303 VectorValueMapT &ScalarMaps,
304 bool NegativeStride = false) {
305 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000306 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000307 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
308 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
309
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000310 Value *NewPointer = nullptr;
Johannes Doerfert731685e2014-10-08 17:25:30 +0000311 NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[Offset],
312 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000313 Value *VectorPtr =
314 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
315 LoadInst *VecLoad =
316 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000317 if (!Aligned)
318 VecLoad->setAlignment(8);
319
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000320 if (NegativeStride) {
321 SmallVector<Constant *, 16> Indices;
322 for (int i = VectorWidth - 1; i >= 0; i--)
323 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
324 Constant *SV = llvm::ConstantVector::get(Indices);
325 Value *RevVecLoad = Builder.CreateShuffleVector(
326 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
327 return RevVecLoad;
328 }
329
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000330 return VecLoad;
331}
332
333Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
334 ValueMapT &BBMap) {
335 const Value *Pointer = Load->getPointerOperand();
336 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000337 Value *NewPointer =
Johannes Doerfert731685e2014-10-08 17:25:30 +0000338 generateLocationAccessed(Load, Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000339 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
340 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000341 LoadInst *ScalarLoad =
342 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000343
344 if (!Aligned)
345 ScalarLoad->setAlignment(8);
346
Tobias Grosserc14582f2013-02-05 18:01:29 +0000347 Constant *SplatVector = Constant::getNullValue(
348 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000349
Tobias Grosserc14582f2013-02-05 18:01:29 +0000350 Value *VectorLoad = Builder.CreateShuffleVector(
351 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000352 return VectorLoad;
353}
354
Tobias Grossere602a072013-05-07 07:30:56 +0000355Value *
356VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
357 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358 int VectorWidth = getVectorWidth();
359 const Value *Pointer = Load->getPointerOperand();
360 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000361 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000362
363 Value *Vector = UndefValue::get(VectorType);
364
365 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfert731685e2014-10-08 17:25:30 +0000366 Value *NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[i],
367 GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000368 Value *ScalarLoad =
369 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
370 Vector = Builder.CreateInsertElement(
371 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000372 }
373
374 return Vector;
375}
376
Tobias Grossere602a072013-05-07 07:30:56 +0000377void VectorBlockGenerator::generateLoad(const LoadInst *Load,
378 ValueMapT &VectorMap,
379 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000380 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
381 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000382 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000383 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000384 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000385 return;
386 }
387
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000388 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000389
Tobias Grosser95493982014-04-18 09:46:35 +0000390 // Make sure we have scalar values available to access the pointer to
391 // the data location.
392 extractScalarValues(Load, VectorMap, ScalarMaps);
393
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000394 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000395 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000396 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000397 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000398 NewLoad = generateStrideOneLoad(Load, ScalarMaps);
399 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
400 NewLoad = generateStrideOneLoad(Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000401 else
402 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
403
404 VectorMap[Load] = NewLoad;
405}
406
407void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
408 ValueMapT &VectorMap,
409 VectorValueMapT &ScalarMaps) {
410 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000411 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
412 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000413
414 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
415
416 const CastInst *Cast = dyn_cast<CastInst>(Inst);
417 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
418 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
419}
420
421void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
422 ValueMapT &VectorMap,
423 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000424 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000425 Value *OpZero = Inst->getOperand(0);
426 Value *OpOne = Inst->getOperand(1);
427
428 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000429 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
430 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000431
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000432 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000433 Inst->getName() + "p_vec");
434 VectorMap[Inst] = NewInst;
435}
436
Tobias Grossere602a072013-05-07 07:30:56 +0000437void VectorBlockGenerator::copyStore(const StoreInst *Store,
438 ValueMapT &VectorMap,
439 VectorValueMapT &ScalarMaps) {
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000440 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000441
442 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000443 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
444 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000445
Tobias Grosser50fd7012014-04-17 23:13:49 +0000446 // Make sure we have scalar values available to access the pointer to
447 // the data location.
448 extractScalarValues(Store, VectorMap, ScalarMaps);
449
Sebastian Popa00a0292012-12-18 07:46:06 +0000450 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000451 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfert731685e2014-10-08 17:25:30 +0000452 Value *NewPointer = generateLocationAccessed(Store, Pointer, ScalarMaps[0],
453 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000454
Tobias Grosserc14582f2013-02-05 18:01:29 +0000455 Value *VectorPtr =
456 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000457 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
458
459 if (!Aligned)
460 Store->setAlignment(8);
461 } else {
462 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000463 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000464 Value *NewPointer = generateLocationAccessed(
465 Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000466 Builder.CreateStore(Scalar, NewPointer);
467 }
468 }
469}
470
471bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
472 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000473 for (Value *Operand : Inst->operands())
474 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000475 return true;
476 return false;
477}
478
479bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
480 ValueMapT &VectorMap,
481 VectorValueMapT &ScalarMaps) {
482 bool HasVectorOperand = false;
483 int VectorWidth = getVectorWidth();
484
Tobias Grosser91f5b262014-06-04 08:06:40 +0000485 for (Value *Operand : Inst->operands()) {
486 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000487
488 if (VecOp == VectorMap.end())
489 continue;
490
491 HasVectorOperand = true;
492 Value *NewVector = VecOp->second;
493
494 for (int i = 0; i < VectorWidth; ++i) {
495 ValueMapT &SM = ScalarMaps[i];
496
497 // If there is one scalar extracted, all scalar elements should have
498 // already been extracted by the code here. So no need to check for the
499 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000500 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000501 break;
502
Tobias Grosser91f5b262014-06-04 08:06:40 +0000503 SM[Operand] =
504 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000505 }
506 }
507
508 return HasVectorOperand;
509}
510
511void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
512 ValueMapT &VectorMap,
513 VectorValueMapT &ScalarMaps) {
514 bool HasVectorOperand;
515 int VectorWidth = getVectorWidth();
516
517 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
518
519 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfert731685e2014-10-08 17:25:30 +0000520 BlockGenerator::copyInstruction(Inst, ScalarMaps[VectorLane],
521 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000522
523 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
524 return;
525
526 // Make the result available as vector value.
527 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
528 Value *Vector = UndefValue::get(VectorType);
529
530 for (int i = 0; i < VectorWidth; i++)
531 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
532 Builder.getInt32(i));
533
534 VectorMap[Inst] = Vector;
535}
536
Tobias Grosserc14582f2013-02-05 18:01:29 +0000537int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000538
539void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
540 ValueMapT &VectorMap,
541 VectorValueMapT &ScalarMaps) {
542 // Terminator instructions control the control flow. They are explicitly
543 // expressed in the clast and do not need to be copied.
544 if (Inst->isTerminator())
545 return;
546
Chandler Carruthf5579872015-01-17 14:16:56 +0000547 if (canSynthesize(Inst, &P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
548 &SE, &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000549 return;
550
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000551 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
552 generateLoad(Load, VectorMap, ScalarMaps);
553 return;
554 }
555
556 if (hasVectorOperands(Inst, VectorMap)) {
557 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
558 copyStore(Store, VectorMap, ScalarMaps);
559 return;
560 }
561
562 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
563 copyUnaryInst(Unary, VectorMap, ScalarMaps);
564 return;
565 }
566
567 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
568 copyBinaryInst(Binary, VectorMap, ScalarMaps);
569 return;
570 }
571
572 // Falltrough: We generate scalar instructions, if we don't know how to
573 // generate vector code.
574 }
575
576 copyInstScalarized(Inst, VectorMap, ScalarMaps);
577}
578
579void VectorBlockGenerator::copyBB() {
Chandler Carruth5ec33332015-01-18 10:52:23 +0000580 auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
581 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
582 auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
Tobias Grossera8cd1522015-01-18 15:59:16 +0000583 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
Chandler Carruth5ec33332015-01-18 10:52:23 +0000584
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000585 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000586 BasicBlock *CopyBB =
Chandler Carruth5ec33332015-01-18 10:52:23 +0000587 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), DT, LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000588 CopyBB->setName("polly.stmt." + BB->getName());
589 Builder.SetInsertPoint(CopyBB->begin());
590
591 // Create two maps that store the mapping from the original instructions of
592 // the old basic block to their copies in the new basic block. Those maps
593 // are basic block local.
594 //
595 // As vector code generation is supported there is one map for scalar values
596 // and one for vector values.
597 //
598 // In case we just do scalar code generation, the vectorMap is not used and
599 // the scalarMap has just one dimension, which contains the mapping.
600 //
601 // In case vector code generation is done, an instruction may either appear
602 // in the vector map once (as it is calculating >vectorwidth< values at a
603 // time. Or (if the values are calculated using scalar operations), it
604 // appears once in every dimension of the scalarMap.
605 VectorValueMapT ScalarBlockMap(getVectorWidth());
606 ValueMapT VectorBlockMap;
607
Tobias Grosser91f5b262014-06-04 08:06:40 +0000608 for (Instruction &Inst : *BB)
609 copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000610}