blob: 66d90d9cff5bb22cdbd7f3ab6a4bef237bcfe3e5 [file] [log] [blame]
Hongbin Zheng3b11a162012-04-25 13:16:49 +00001//===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the BlockGenerator and VectorBlockGenerator classes,
11// which generate sequential code and vectorized code for a polyhedral
12// statement, respectively.
13//
14//===----------------------------------------------------------------------===//
15
16#include "polly/ScopInfo.h"
Tobias Grosser83628182013-05-07 08:11:54 +000017#include "isl/aff.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000018#include "isl/ast.h"
19#include "isl/ast_build.h"
Tobias Grosser83628182013-05-07 08:11:54 +000020#include "isl/set.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000021#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000022#include "polly/CodeGen/CodeGeneration.h"
Johannes Doerferta63b2572014-08-03 01:51:59 +000023#include "polly/CodeGen/IslExprBuilder.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000024#include "polly/Options.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000025#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000026#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000027#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000028#include "llvm/Analysis/LoopInfo.h"
29#include "llvm/Analysis/ScalarEvolution.h"
30#include "llvm/Analysis/ScalarEvolutionExpander.h"
Tobias Grosser030237d2014-02-21 15:06:05 +000031#include "llvm/IR/IntrinsicInst.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000032#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000033
Hongbin Zheng3b11a162012-04-25 13:16:49 +000034using namespace llvm;
35using namespace polly;
36
37static cl::opt<bool>
Tobias Grosser483a90d2014-07-09 10:50:10 +000038 Aligned("enable-polly-aligned",
39 cl::desc("Assumed aligned memory accesses."), cl::Hidden,
40 cl::value_desc("OpenMP code generation enabled if true"),
41 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000042
Tobias Grossere602a072013-05-07 07:30:56 +000043static cl::opt<bool, true>
Tobias Grosser483a90d2014-07-09 10:50:10 +000044 SCEVCodegenF("polly-codegen-scev",
45 cl::desc("Use SCEV based code generation."), cl::Hidden,
46 cl::location(SCEVCodegen), cl::init(false), cl::ZeroOrMore,
47 cl::cat(PollyCategory));
Tobias Grosserecfe21b2013-03-20 18:03:18 +000048
49bool polly::SCEVCodegen;
50
51bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
52 ScalarEvolution *SE, const Region *R) {
53 if (SCEVCodegen) {
54 if (!I || !SE->isSCEVable(I->getType()))
55 return false;
56
57 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
58 if (!isa<SCEVCouldNotCompute>(Scev))
59 if (!hasScalarDepsInsideRegion(Scev, R))
60 return true;
61
62 return false;
63 }
64
65 Loop *L = LI->getLoopFor(I->getParent());
Tobias Grossere8df5bd2013-04-17 07:20:36 +000066 return L && I == L->getCanonicalInductionVariable() && R->contains(L);
Tobias Grosserecfe21b2013-03-20 18:03:18 +000067}
68
Johannes Doerferta63b2572014-08-03 01:51:59 +000069BlockGenerator::BlockGenerator(PollyIRBuilder &B, ScopStmt &Stmt, Pass *P,
70 isl_ast_build *Build,
71 IslExprBuilder *ExprBuilder)
72 : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()),
73 Build(Build), 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) {
167 isl_pw_multi_aff *PWSchedule, *PWAccRel;
168 isl_union_map *ScheduleU;
169 isl_map *Schedule, *AccRel;
170 isl_ast_expr *Expr;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000171
Johannes Doerferta63b2572014-08-03 01:51:59 +0000172 assert(ExprBuilder && Build &&
173 "Cannot generate new value without IslExprBuilder!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000174
Johannes Doerferta63b2572014-08-03 01:51:59 +0000175 AccRel = MA.getNewAccessRelation();
176 assert(AccRel && "We generate new code only for new access relations!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000177
Johannes Doerferta63b2572014-08-03 01:51:59 +0000178 ScheduleU = isl_ast_build_get_schedule(Build);
179 ScheduleU = isl_union_map_intersect_domain(
180 ScheduleU, isl_union_set_from_set(MA.getStatement()->getDomain()));
181 Schedule = isl_map_from_union_map(ScheduleU);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000182
Johannes Doerferta63b2572014-08-03 01:51:59 +0000183 PWSchedule = isl_pw_multi_aff_from_map(isl_map_reverse(Schedule));
184 PWAccRel = isl_pw_multi_aff_from_map(AccRel);
185 PWAccRel = isl_pw_multi_aff_pullback_pw_multi_aff(PWAccRel, PWSchedule);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000186
Johannes Doerferta63b2572014-08-03 01:51:59 +0000187 Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
188
189 return ExprBuilder->create(Expr);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000190}
191
Tobias Grossere602a072013-05-07 07:30:56 +0000192Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
193 const Value *Pointer,
194 ValueMapT &BBMap,
195 ValueMapT &GlobalMap,
196 LoopToScevMapT &LTS) {
Johannes Doerferta63b2572014-08-03 01:51:59 +0000197 const MemoryAccess &MA = Statement.getAccessFor(Inst);
198 isl_map *NewAccRel = MA.getNewAccessRelation();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000199
200 Value *NewPointer;
Johannes Doerferta63b2572014-08-03 01:51:59 +0000201 if (NewAccRel)
202 NewPointer = getNewAccessOperand(MA);
203 else
Tobias Grosser369430f2013-03-22 23:42:53 +0000204 NewPointer =
205 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000206
Johannes Doerferta63b2572014-08-03 01:51:59 +0000207 isl_map_free(NewAccRel);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000208 return NewPointer;
209}
210
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000211Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000212 return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent());
213}
214
Tobias Grossere602a072013-05-07 07:30:56 +0000215Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
216 ValueMapT &BBMap,
217 ValueMapT &GlobalMap,
218 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000219 const Value *Pointer = Load->getPointerOperand();
220 const Instruction *Inst = dyn_cast<Instruction>(Load);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000221 Value *NewPointer =
222 generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000223 Value *ScalarLoad =
224 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000225 return ScalarLoad;
226}
227
Tobias Grossere602a072013-05-07 07:30:56 +0000228Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
229 ValueMapT &BBMap,
230 ValueMapT &GlobalMap,
231 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000232 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000233 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000234 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000235 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
236 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000237
238 return Builder.CreateStore(ValueOperand, NewPointer);
239}
240
Tobias Grossere602a072013-05-07 07:30:56 +0000241void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
242 ValueMapT &GlobalMap,
243 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000244 // Terminator instructions control the control flow. They are explicitly
245 // expressed in the clast and do not need to be copied.
246 if (Inst->isTerminator())
247 return;
248
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000249 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
250 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000251 return;
252
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000253 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000254 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000255 // Compute NewLoad before its insertion in BBMap to make the insertion
256 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000257 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000258 return;
259 }
260
261 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000262 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000263 // Compute NewStore before its insertion in BBMap to make the insertion
264 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000265 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000266 return;
267 }
268
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000269 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000270}
271
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000272void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000273 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000274 BasicBlock *CopyBB =
275 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000276 CopyBB->setName("polly.stmt." + BB->getName());
277 Builder.SetInsertPoint(CopyBB->begin());
278
279 ValueMapT BBMap;
280
Tobias Grosser91f5b262014-06-04 08:06:40 +0000281 for (Instruction &Inst : *BB)
282 copyInstruction(&Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000283}
284
Tobias Grosser5103ba72014-03-04 14:58:49 +0000285VectorBlockGenerator::VectorBlockGenerator(PollyIRBuilder &B,
Tobias Grossere602a072013-05-07 07:30:56 +0000286 VectorValueMapT &GlobalMaps,
287 std::vector<LoopToScevMapT> &VLTS,
288 ScopStmt &Stmt,
289 __isl_keep isl_map *Schedule,
290 Pass *P)
Johannes Doerferta63b2572014-08-03 01:51:59 +0000291 : BlockGenerator(B, Stmt, P, nullptr, nullptr), GlobalMaps(GlobalMaps),
292 VLTS(VLTS), Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000293 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
294 assert(Schedule && "No statement domain provided");
295}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000296
Tobias Grossere602a072013-05-07 07:30:56 +0000297Value *VectorBlockGenerator::getVectorValue(const Value *Old,
298 ValueMapT &VectorMap,
299 VectorValueMapT &ScalarMaps,
300 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000301 if (Value *NewValue = VectorMap.lookup(Old))
302 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000303
304 int Width = getVectorWidth();
305
306 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
307
308 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000309 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000310 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000311 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000312 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000313
314 VectorMap[Old] = Vector;
315
316 return Vector;
317}
318
319Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
320 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
321 assert(PointerTy && "PointerType expected");
322
323 Type *ScalarType = PointerTy->getElementType();
324 VectorType *VectorType = VectorType::get(ScalarType, Width);
325
326 return PointerType::getUnqual(VectorType);
327}
328
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000329Value *
330VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
331 VectorValueMapT &ScalarMaps,
332 bool NegativeStride = false) {
333 unsigned VectorWidth = getVectorWidth();
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000334 const Value *Pointer = Load->getPointerOperand();
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000335 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
336 unsigned Offset = NegativeStride ? VectorWidth - 1 : 0;
337
Tobias Grosser5a56cbf2014-04-16 07:33:47 +0000338 Value *NewPointer = nullptr;
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000339 NewPointer = getNewValue(Pointer, ScalarMaps[Offset], GlobalMaps[Offset],
340 VLTS[Offset], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000341 Value *VectorPtr =
342 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
343 LoadInst *VecLoad =
344 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000345 if (!Aligned)
346 VecLoad->setAlignment(8);
347
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000348 if (NegativeStride) {
349 SmallVector<Constant *, 16> Indices;
350 for (int i = VectorWidth - 1; i >= 0; i--)
351 Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i));
352 Constant *SV = llvm::ConstantVector::get(Indices);
353 Value *RevVecLoad = Builder.CreateShuffleVector(
354 VecLoad, VecLoad, SV, Load->getName() + "_reverse");
355 return RevVecLoad;
356 }
357
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358 return VecLoad;
359}
360
361Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
362 ValueMapT &BBMap) {
363 const Value *Pointer = Load->getPointerOperand();
364 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000365 Value *NewPointer =
366 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000367 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
368 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000369 LoadInst *ScalarLoad =
370 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000371
372 if (!Aligned)
373 ScalarLoad->setAlignment(8);
374
Tobias Grosserc14582f2013-02-05 18:01:29 +0000375 Constant *SplatVector = Constant::getNullValue(
376 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000377
Tobias Grosserc14582f2013-02-05 18:01:29 +0000378 Value *VectorLoad = Builder.CreateShuffleVector(
379 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000380 return VectorLoad;
381}
382
Tobias Grossere602a072013-05-07 07:30:56 +0000383Value *
384VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
385 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000386 int VectorWidth = getVectorWidth();
387 const Value *Pointer = Load->getPointerOperand();
388 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000389 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000390
391 Value *Vector = UndefValue::get(VectorType);
392
393 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000394 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
395 VLTS[i], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000396 Value *ScalarLoad =
397 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
398 Vector = Builder.CreateInsertElement(
399 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000400 }
401
402 return Vector;
403}
404
Tobias Grossere602a072013-05-07 07:30:56 +0000405void VectorBlockGenerator::generateLoad(const LoadInst *Load,
406 ValueMapT &VectorMap,
407 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000408 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
409 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000410 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000411 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000412 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000413 return;
414 }
415
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000416 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000417
Tobias Grosser95493982014-04-18 09:46:35 +0000418 // Make sure we have scalar values available to access the pointer to
419 // the data location.
420 extractScalarValues(Load, VectorMap, ScalarMaps);
421
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000422 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000423 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000424 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000425 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Tobias Grosser0dd463f2014-03-19 19:27:24 +0000426 NewLoad = generateStrideOneLoad(Load, ScalarMaps);
427 else if (Access.isStrideX(isl_map_copy(Schedule), -1))
428 NewLoad = generateStrideOneLoad(Load, ScalarMaps, true);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000429 else
430 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
431
432 VectorMap[Load] = NewLoad;
433}
434
435void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
436 ValueMapT &VectorMap,
437 VectorValueMapT &ScalarMaps) {
438 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000439 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
440 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000441
442 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
443
444 const CastInst *Cast = dyn_cast<CastInst>(Inst);
445 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
446 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
447}
448
449void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
450 ValueMapT &VectorMap,
451 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000452 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000453 Value *OpZero = Inst->getOperand(0);
454 Value *OpOne = Inst->getOperand(1);
455
456 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000457 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
458 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000459
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000460 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000461 Inst->getName() + "p_vec");
462 VectorMap[Inst] = NewInst;
463}
464
Tobias Grossere602a072013-05-07 07:30:56 +0000465void VectorBlockGenerator::copyStore(const StoreInst *Store,
466 ValueMapT &VectorMap,
467 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000468 int VectorWidth = getVectorWidth();
469
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000470 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000471
472 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000473 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
474 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000475
Tobias Grosser50fd7012014-04-17 23:13:49 +0000476 // Make sure we have scalar values available to access the pointer to
477 // the data location.
478 extractScalarValues(Store, VectorMap, ScalarMaps);
479
Sebastian Popa00a0292012-12-18 07:46:06 +0000480 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000481 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
Tobias Grosser369430f2013-03-22 23:42:53 +0000482 Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0],
483 VLTS[0], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000484
Tobias Grosserc14582f2013-02-05 18:01:29 +0000485 Value *VectorPtr =
486 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000487 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
488
489 if (!Aligned)
490 Store->setAlignment(8);
491 } else {
492 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000493 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser369430f2013-03-22 23:42:53 +0000494 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
495 VLTS[i], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000496 Builder.CreateStore(Scalar, NewPointer);
497 }
498 }
499}
500
501bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
502 ValueMapT &VectorMap) {
Tobias Grosser91f5b262014-06-04 08:06:40 +0000503 for (Value *Operand : Inst->operands())
504 if (VectorMap.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000505 return true;
506 return false;
507}
508
509bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
510 ValueMapT &VectorMap,
511 VectorValueMapT &ScalarMaps) {
512 bool HasVectorOperand = false;
513 int VectorWidth = getVectorWidth();
514
Tobias Grosser91f5b262014-06-04 08:06:40 +0000515 for (Value *Operand : Inst->operands()) {
516 ValueMapT::iterator VecOp = VectorMap.find(Operand);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000517
518 if (VecOp == VectorMap.end())
519 continue;
520
521 HasVectorOperand = true;
522 Value *NewVector = VecOp->second;
523
524 for (int i = 0; i < VectorWidth; ++i) {
525 ValueMapT &SM = ScalarMaps[i];
526
527 // If there is one scalar extracted, all scalar elements should have
528 // already been extracted by the code here. So no need to check for the
529 // existance of all of them.
Tobias Grosser91f5b262014-06-04 08:06:40 +0000530 if (SM.count(Operand))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000531 break;
532
Tobias Grosser91f5b262014-06-04 08:06:40 +0000533 SM[Operand] =
534 Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000535 }
536 }
537
538 return HasVectorOperand;
539}
540
541void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
542 ValueMapT &VectorMap,
543 VectorValueMapT &ScalarMaps) {
544 bool HasVectorOperand;
545 int VectorWidth = getVectorWidth();
546
547 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
548
549 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000550 copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
551 VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000552
553 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
554 return;
555
556 // Make the result available as vector value.
557 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
558 Value *Vector = UndefValue::get(VectorType);
559
560 for (int i = 0; i < VectorWidth; i++)
561 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
562 Builder.getInt32(i));
563
564 VectorMap[Inst] = Vector;
565}
566
Tobias Grosserc14582f2013-02-05 18:01:29 +0000567int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000568
569void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
570 ValueMapT &VectorMap,
571 VectorValueMapT &ScalarMaps) {
572 // Terminator instructions control the control flow. They are explicitly
573 // expressed in the clast and do not need to be copied.
574 if (Inst->isTerminator())
575 return;
576
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000577 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
578 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000579 return;
580
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000581 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
582 generateLoad(Load, VectorMap, ScalarMaps);
583 return;
584 }
585
586 if (hasVectorOperands(Inst, VectorMap)) {
587 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
588 copyStore(Store, VectorMap, ScalarMaps);
589 return;
590 }
591
592 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
593 copyUnaryInst(Unary, VectorMap, ScalarMaps);
594 return;
595 }
596
597 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
598 copyBinaryInst(Binary, VectorMap, ScalarMaps);
599 return;
600 }
601
602 // Falltrough: We generate scalar instructions, if we don't know how to
603 // generate vector code.
604 }
605
606 copyInstScalarized(Inst, VectorMap, ScalarMaps);
607}
608
609void VectorBlockGenerator::copyBB() {
610 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000611 BasicBlock *CopyBB =
612 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000613 CopyBB->setName("polly.stmt." + BB->getName());
614 Builder.SetInsertPoint(CopyBB->begin());
615
616 // Create two maps that store the mapping from the original instructions of
617 // the old basic block to their copies in the new basic block. Those maps
618 // are basic block local.
619 //
620 // As vector code generation is supported there is one map for scalar values
621 // and one for vector values.
622 //
623 // In case we just do scalar code generation, the vectorMap is not used and
624 // the scalarMap has just one dimension, which contains the mapping.
625 //
626 // In case vector code generation is done, an instruction may either appear
627 // in the vector map once (as it is calculating >vectorwidth< values at a
628 // time. Or (if the values are calculated using scalar operations), it
629 // appears once in every dimension of the scalarMap.
630 VectorValueMapT ScalarBlockMap(getVectorWidth());
631 ValueMapT VectorBlockMap;
632
Tobias Grosser91f5b262014-06-04 08:06:40 +0000633 for (Instruction &Inst : *BB)
634 copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000635}