blob: d7ccea69c7c0012d760d815b3214f25041e43e64 [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 Doerfert9e3a5db2015-01-26 15:55:54 +000055bool polly::isIgnoredIntrinsic(const Value *V) {
56 if (auto *IT = dyn_cast<IntrinsicInst>(V)) {
57 switch (IT->getIntrinsicID()) {
58 // Lifetime markers are supported/ignored.
59 case llvm::Intrinsic::lifetime_start:
60 case llvm::Intrinsic::lifetime_end:
61 // Invariant markers are supported/ignored.
62 case llvm::Intrinsic::invariant_start:
63 case llvm::Intrinsic::invariant_end:
64 // Some misc annotations are supported/ignored.
65 case llvm::Intrinsic::var_annotation:
66 case llvm::Intrinsic::ptr_annotation:
67 case llvm::Intrinsic::annotation:
68 case llvm::Intrinsic::donothing:
69 case llvm::Intrinsic::assume:
70 case llvm::Intrinsic::expect:
71 return true;
72 default:
73 break;
74 }
75 }
76 return false;
77}
78
Johannes Doerferta63b2572014-08-03 01:51:59 +000079BlockGenerator::BlockGenerator(PollyIRBuilder &B, ScopStmt &Stmt, Pass *P,
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000080 LoopInfo &LI, ScalarEvolution &SE,
Johannes Doerferta63b2572014-08-03 01:51:59 +000081 isl_ast_build *Build,
82 IslExprBuilder *ExprBuilder)
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +000083 : Builder(B), Statement(Stmt), P(P), LI(LI), SE(SE), Build(Build),
84 ExprBuilder(ExprBuilder) {}
Tobias Grossere71c6ab2012-04-27 16:36:14 +000085
Tobias Grosserd213a8b2014-11-05 19:46:04 +000086Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
87 ValueMapT &GlobalMap, LoopToScevMapT &LTS,
88 Loop *L) const {
Hongbin Zheng3b11a162012-04-25 13:16:49 +000089 // We assume constants never change.
90 // This avoids map lookups for many calls to this function.
91 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +000092 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000093
Hongbin Zhengfe11e282013-06-29 13:22:15 +000094 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +000095 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +000096 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +000097 New = Builder.CreateTruncOrBitCast(New, Old->getType());
98
99 return New;
100 }
101
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000102 if (Value *New = BBMap.lookup(Old))
103 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000104
Tobias Grosser683b8e42014-11-30 14:33:31 +0000105 if (SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000106 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000107 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000108 const SCEV *NewScev = apply(Scev, LTS, SE);
109 ValueToValueMap VTV;
110 VTV.insert(BBMap.begin(), BBMap.end());
111 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000112 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000113 SCEVExpander Expander(SE, "polly");
114 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
115 Builder.GetInsertPoint());
116
117 BBMap[Old] = Expanded;
118 return Expanded;
119 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000120 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000121
Tobias Grosser16371ac2014-11-05 20:48:56 +0000122 // A scop-constant value defined by a global or a function parameter.
123 if (isa<GlobalValue>(Old) || isa<Argument>(Old))
124 return const_cast<Value *>(Old);
125
126 // A scop-constant value defined by an instruction executed outside the scop.
127 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
128 if (!Statement.getParent()->getRegion().contains(Inst->getParent()))
129 return const_cast<Value *>(Old);
130
131 // The scalar dependence is neither available nor SCEVCodegenable.
Hongbin Zheng5b463ce2013-07-25 09:12:07 +0000132 llvm_unreachable("Unexpected scalar dependence in region!");
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000133 return nullptr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000134}
135
136void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000137 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosser030237d2014-02-21 15:06:05 +0000138 // We do not generate debug intrinsics as we did not investigate how to
139 // copy them correctly. At the current state, they just crash the code
140 // generation as the meta-data operands are not correctly copied.
141 if (isa<DbgInfoIntrinsic>(Inst))
142 return;
143
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000144 Instruction *NewInst = Inst->clone();
145
146 // Replace old operands with the new ones.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000147 for (Value *OldOperand : Inst->operands()) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000148 Value *NewOperand =
149 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000150
151 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000152 assert(!isa<StoreInst>(NewInst) &&
153 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000154 delete NewInst;
155 return;
156 }
157
158 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
159 }
160
161 Builder.Insert(NewInst);
162 BBMap[Inst] = NewInst;
163
164 if (!NewInst->getType()->isVoidTy())
165 NewInst->setName("p_" + Inst->getName());
166}
167
Johannes Doerferta63b2572014-08-03 01:51:59 +0000168Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) {
Johannes Doerferta99130f2014-10-13 12:58:03 +0000169 isl_pw_multi_aff *PWAccRel;
170 isl_union_map *Schedule;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000171 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 Doerferta99130f2014-10-13 12:58:03 +0000176 Schedule = isl_ast_build_get_schedule(Build);
177 PWAccRel = MA.applyScheduleToAccessRelation(Schedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000178
Johannes Doerferta63b2572014-08-03 01:51:59 +0000179 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
Johannes Doerfertdcb5f1d2014-09-18 11:14:30 +0000180 Expr = isl_ast_expr_address_of(Expr);
Johannes Doerferta63b2572014-08-03 01:51:59 +0000181
182 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000183}
184
Tobias Grossere602a072013-05-07 07:30:56 +0000185Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
186 const Value *Pointer,
187 ValueMapT &BBMap,
188 ValueMapT &GlobalMap,
189 LoopToScevMapT &LTS) {
Johannes Doerferta63b2572014-08-03 01:51:59 +0000190 const MemoryAccess &MA = Statement.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000191
192 Value *NewPointer;
Johannes Doerferta99130f2014-10-13 12:58:03 +0000193 if (MA.hasNewAccessRelation())
Johannes Doerferta63b2572014-08-03 01:51:59 +0000194 NewPointer = getNewAccessOperand(MA);
195 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000196 NewPointer =
197 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000198
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000199 return NewPointer;
200}
201
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000202Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000203 return LI.getLoopFor(Inst->getParent());
Tobias Grosser369430f2013-03-22 23:42:53 +0000204}
205
Tobias Grossere602a072013-05-07 07:30:56 +0000206Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
207 ValueMapT &BBMap,
208 ValueMapT &GlobalMap,
209 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000210 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser7242ad92013-02-22 08:07:06 +0000211 Value *NewPointer =
Johannes Doerfert1947f862014-10-08 20:18:32 +0000212 generateLocationAccessed(Load, Pointer, BBMap, GlobalMap, LTS);
Johannes Doerfert87901452014-10-02 16:22:19 +0000213 Value *ScalarLoad = Builder.CreateAlignedLoad(
214 NewPointer, Load->getAlignment(), Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000215 return ScalarLoad;
216}
217
Tobias Grossere602a072013-05-07 07:30:56 +0000218Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
219 ValueMapT &BBMap,
220 ValueMapT &GlobalMap,
221 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000222 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000223 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000224 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000225 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
226 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000227
Johannes Doerfert87901452014-10-02 16:22:19 +0000228 Value *NewStore = Builder.CreateAlignedStore(ValueOperand, NewPointer,
229 Store->getAlignment());
230 return NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000231}
232
Tobias Grossere602a072013-05-07 07:30:56 +0000233void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
234 ValueMapT &GlobalMap,
235 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000236 // Terminator instructions control the control flow. They are explicitly
237 // expressed in the clast and do not need to be copied.
238 if (Inst->isTerminator())
239 return;
240
Chandler Carruthf5579872015-01-17 14:16:56 +0000241 if (canSynthesize(Inst, &P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
242 &SE, &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000243 return;
244
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000245 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000246 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000247 // Compute NewLoad before its insertion in BBMap to make the insertion
248 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000249 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000250 return;
251 }
252
253 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000254 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000255 // Compute NewStore before its insertion in BBMap to make the insertion
256 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000257 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000258 return;
259 }
260
Johannes Doerfert3f500fa2015-01-25 18:07:30 +0000261 // Skip some special intrinsics for which we do not adjust the semantics to
262 // the new schedule. All others are handled like every other instruction.
263 if (auto *IT = dyn_cast<IntrinsicInst>(Inst)) {
264 switch (IT->getIntrinsicID()) {
265 // Lifetime markers are ignored.
266 case llvm::Intrinsic::lifetime_start:
267 case llvm::Intrinsic::lifetime_end:
268 // Invariant markers are ignored.
269 case llvm::Intrinsic::invariant_start:
270 case llvm::Intrinsic::invariant_end:
271 // Some misc annotations are ignored.
272 case llvm::Intrinsic::var_annotation:
273 case llvm::Intrinsic::ptr_annotation:
274 case llvm::Intrinsic::annotation:
275 case llvm::Intrinsic::donothing:
276 case llvm::Intrinsic::assume:
277 case llvm::Intrinsic::expect:
278 return;
279 default:
280 // Other intrinsics are copied.
281 break;
282 }
283 }
284
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000285 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000286}
287
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000288void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Chandler Carruth5ec33332015-01-18 10:52:23 +0000289 auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
290 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
291 auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
Tobias Grossera8cd1522015-01-18 15:59:16 +0000292 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
Chandler Carruth5ec33332015-01-18 10:52:23 +0000293
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000294 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000295 BasicBlock *CopyBB =
Chandler Carruth5ec33332015-01-18 10:52:23 +0000296 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), DT, LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000297 CopyBB->setName("polly.stmt." + BB->getName());
298 Builder.SetInsertPoint(CopyBB->begin());
299
300 ValueMapT BBMap;
301
Tobias Grosser91f5b262014-06-04 08:06:40 +0000302 for (Instruction &Inst : *BB)
303 copyInstruction(&Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000304}
305
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000306VectorBlockGenerator::VectorBlockGenerator(
307 PollyIRBuilder &B, VectorValueMapT &GlobalMaps,
308 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
Johannes Doerfert731685e2014-10-08 17:25:30 +0000309 __isl_keep isl_map *Schedule, Pass *P, LoopInfo &LI, ScalarEvolution &SE,
310 __isl_keep isl_ast_build *Build, IslExprBuilder *ExprBuilder)
311 : BlockGenerator(B, Stmt, P, LI, SE, Build, ExprBuilder),
Johannes Doerfert2ef3f4f2014-08-07 17:14:54 +0000312 GlobalMaps(GlobalMaps), VLTS(VLTS), Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000313 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
314 assert(Schedule && "No statement domain provided");
315}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000316
Tobias Grossere602a072013-05-07 07:30:56 +0000317Value *VectorBlockGenerator::getVectorValue(const Value *Old,
318 ValueMapT &VectorMap,
319 VectorValueMapT &ScalarMaps,
320 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000321 if (Value *NewValue = VectorMap.lookup(Old))
322 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000323
324 int Width = getVectorWidth();
325
326 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
327
328 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000329 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000330 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000331 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000332 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000333
334 VectorMap[Old] = Vector;
335
336 return Vector;
337}
338
339Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
340 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
341 assert(PointerTy && "PointerType expected");
342
343 Type *ScalarType = PointerTy->getElementType();
344 VectorType *VectorType = VectorType::get(ScalarType, Width);
345
346 return PointerType::getUnqual(VectorType);
347}
348
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000349Value *
350VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
351 VectorValueMapT &ScalarMaps,
352 bool NegativeStride = false) {
353 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000354 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000355 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
356 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
357
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000358 Value *NewPointer = nullptr;
Johannes Doerfert731685e2014-10-08 17:25:30 +0000359 NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[Offset],
360 GlobalMaps[Offset], VLTS[Offset]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000361 Value *VectorPtr =
362 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
363 LoadInst *VecLoad =
364 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000365 if (!Aligned)
366 VecLoad->setAlignment(8);
367
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000368 if (NegativeStride) {
369 SmallVector<Constant *, 16> Indices;
370 for (int i = VectorWidth - 1; i >= 0; i--)
371 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
372 Constant *SV = llvm::ConstantVector::get(Indices);
373 Value *RevVecLoad = Builder.CreateShuffleVector(
374 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
375 return RevVecLoad;
376 }
377
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000378 return VecLoad;
379}
380
381Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
382 ValueMapT &BBMap) {
383 const Value *Pointer = Load->getPointerOperand();
384 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000385 Value *NewPointer =
Johannes Doerfert731685e2014-10-08 17:25:30 +0000386 generateLocationAccessed(Load, Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000387 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
388 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000389 LoadInst *ScalarLoad =
390 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000391
392 if (!Aligned)
393 ScalarLoad->setAlignment(8);
394
Tobias Grosserc14582f2013-02-05 18:01:29 +0000395 Constant *SplatVector = Constant::getNullValue(
396 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000397
Tobias Grosserc14582f2013-02-05 18:01:29 +0000398 Value *VectorLoad = Builder.CreateShuffleVector(
399 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000400 return VectorLoad;
401}
402
Tobias Grossere602a072013-05-07 07:30:56 +0000403Value *
404VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
405 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000406 int VectorWidth = getVectorWidth();
407 const Value *Pointer = Load->getPointerOperand();
408 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000409 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000410
411 Value *Vector = UndefValue::get(VectorType);
412
413 for (int i = 0; i < VectorWidth; i++) {
Johannes Doerfert731685e2014-10-08 17:25:30 +0000414 Value *NewPointer = generateLocationAccessed(Load, Pointer, ScalarMaps[i],
415 GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000416 Value *ScalarLoad =
417 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
418 Vector = Builder.CreateInsertElement(
419 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000420 }
421
422 return Vector;
423}
424
Tobias Grossere602a072013-05-07 07:30:56 +0000425void VectorBlockGenerator::generateLoad(const LoadInst *Load,
426 ValueMapT &VectorMap,
427 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000428 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
429 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000430 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000431 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000432 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000433 return;
434 }
435
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000436 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000437
Tobias Grosser95493982014-04-18 09:46:35 +0000438 // Make sure we have scalar values available to access the pointer to
439 // the data location.
440 extractScalarValues(Load, VectorMap, ScalarMaps);
441
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000442 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000443 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000444 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000445 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000446 NewLoad = generateStrideOneLoad(Load, ScalarMaps);
447 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
448 NewLoad = generateStrideOneLoad(Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000449 else
450 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
451
452 VectorMap[Load] = NewLoad;
453}
454
455void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
456 ValueMapT &VectorMap,
457 VectorValueMapT &ScalarMaps) {
458 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000459 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
460 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000461
462 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
463
464 const CastInst *Cast = dyn_cast<CastInst>(Inst);
465 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
466 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
467}
468
469void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
470 ValueMapT &VectorMap,
471 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000472 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000473 Value *OpZero = Inst->getOperand(0);
474 Value *OpOne = Inst->getOperand(1);
475
476 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000477 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
478 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000479
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000480 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000481 Inst->getName() + "p_vec");
482 VectorMap[Inst] = NewInst;
483}
484
Tobias Grossere602a072013-05-07 07:30:56 +0000485void VectorBlockGenerator::copyStore(const StoreInst *Store,
486 ValueMapT &VectorMap,
487 VectorValueMapT &ScalarMaps) {
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000488 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000489
490 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000491 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
492 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000493
Tobias Grosser50fd7012014-04-17 23:13:49 +0000494 // Make sure we have scalar values available to access the pointer to
495 // the data location.
496 extractScalarValues(Store, VectorMap, ScalarMaps);
497
Sebastian Popa00a0292012-12-18 07:46:06 +0000498 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Johannes Doerfert1947f862014-10-08 20:18:32 +0000499 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Johannes Doerfert731685e2014-10-08 17:25:30 +0000500 Value *NewPointer = generateLocationAccessed(Store, Pointer, ScalarMaps[0],
501 GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000502
Tobias Grosserc14582f2013-02-05 18:01:29 +0000503 Value *VectorPtr =
504 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000505 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
506
507 if (!Aligned)
508 Store->setAlignment(8);
509 } else {
510 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000511 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Johannes Doerfert731685e2014-10-08 17:25:30 +0000512 Value *NewPointer = generateLocationAccessed(
513 Store, Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000514 Builder.CreateStore(Scalar, NewPointer);
515 }
516 }
517}
518
519bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
520 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000521 for (Value *Operand : Inst->operands())
522 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000523 return true;
524 return false;
525}
526
527bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
528 ValueMapT &VectorMap,
529 VectorValueMapT &ScalarMaps) {
530 bool HasVectorOperand = false;
531 int VectorWidth = getVectorWidth();
532
Tobias Grosser91f5b262014-06-04 08:06:40 +0000533 for (Value *Operand : Inst->operands()) {
534 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000535
536 if (VecOp == VectorMap.end())
537 continue;
538
539 HasVectorOperand = true;
540 Value *NewVector = VecOp->second;
541
542 for (int i = 0; i < VectorWidth; ++i) {
543 ValueMapT &SM = ScalarMaps[i];
544
545 // If there is one scalar extracted, all scalar elements should have
546 // already been extracted by the code here. So no need to check for the
547 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000548 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000549 break;
550
Tobias Grosser91f5b262014-06-04 08:06:40 +0000551 SM[Operand] =
552 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000553 }
554 }
555
556 return HasVectorOperand;
557}
558
559void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
560 ValueMapT &VectorMap,
561 VectorValueMapT &ScalarMaps) {
562 bool HasVectorOperand;
563 int VectorWidth = getVectorWidth();
564
565 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
566
567 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Johannes Doerfert731685e2014-10-08 17:25:30 +0000568 BlockGenerator::copyInstruction(Inst, ScalarMaps[VectorLane],
569 GlobalMaps[VectorLane], VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000570
571 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
572 return;
573
574 // Make the result available as vector value.
575 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
576 Value *Vector = UndefValue::get(VectorType);
577
578 for (int i = 0; i < VectorWidth; i++)
579 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
580 Builder.getInt32(i));
581
582 VectorMap[Inst] = Vector;
583}
584
Tobias Grosserc14582f2013-02-05 18:01:29 +0000585int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000586
587void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
588 ValueMapT &VectorMap,
589 VectorValueMapT &ScalarMaps) {
590 // Terminator instructions control the control flow. They are explicitly
591 // expressed in the clast and do not need to be copied.
592 if (Inst->isTerminator())
593 return;
594
Chandler Carruthf5579872015-01-17 14:16:56 +0000595 if (canSynthesize(Inst, &P->getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
596 &SE, &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000597 return;
598
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000599 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
600 generateLoad(Load, VectorMap, ScalarMaps);
601 return;
602 }
603
604 if (hasVectorOperands(Inst, VectorMap)) {
605 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
606 copyStore(Store, VectorMap, ScalarMaps);
607 return;
608 }
609
610 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
611 copyUnaryInst(Unary, VectorMap, ScalarMaps);
612 return;
613 }
614
615 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
616 copyBinaryInst(Binary, VectorMap, ScalarMaps);
617 return;
618 }
619
620 // Falltrough: We generate scalar instructions, if we don't know how to
621 // generate vector code.
622 }
623
624 copyInstScalarized(Inst, VectorMap, ScalarMaps);
625}
626
627void VectorBlockGenerator::copyBB() {
Chandler Carruth5ec33332015-01-18 10:52:23 +0000628 auto *DTWP = P->getAnalysisIfAvailable<DominatorTreeWrapperPass>();
629 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
630 auto *LIWP = P->getAnalysisIfAvailable<LoopInfoWrapperPass>();
Tobias Grossera8cd1522015-01-18 15:59:16 +0000631 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
Chandler Carruth5ec33332015-01-18 10:52:23 +0000632
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000633 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000634 BasicBlock *CopyBB =
Chandler Carruth5ec33332015-01-18 10:52:23 +0000635 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), DT, LI);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000636 CopyBB->setName("polly.stmt." + BB->getName());
637 Builder.SetInsertPoint(CopyBB->begin());
638
639 // Create two maps that store the mapping from the original instructions of
640 // the old basic block to their copies in the new basic block. Those maps
641 // are basic block local.
642 //
643 // As vector code generation is supported there is one map for scalar values
644 // and one for vector values.
645 //
646 // In case we just do scalar code generation, the vectorMap is not used and
647 // the scalarMap has just one dimension, which contains the mapping.
648 //
649 // In case vector code generation is done, an instruction may either appear
650 // in the vector map once (as it is calculating >vectorwidth< values at a
651 // time. Or (if the values are calculated using scalar operations), it
652 // appears once in every dimension of the scalarMap.
653 VectorValueMapT ScalarBlockMap(getVectorWidth());
654 ValueMapT VectorBlockMap;
655
Tobias Grosser91f5b262014-06-04 08:06:40 +0000656 for (Instruction &Inst : *BB)
657 copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000658}