blob: 94c0ac6274e25f106be74922c6c5b91b6ec0d1c9 [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"
Hongbin Zheng68794212012-05-06 10:22:19 +000017#include "polly/CodeGen/CodeGeneration.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000018#include "polly/CodeGen/BlockGenerators.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000019#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000020#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000021#include "polly/Support/ScopHelper.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000022
Tobias Grossere71c6ab2012-04-27 16:36:14 +000023#include "llvm/Analysis/LoopInfo.h"
24#include "llvm/Analysis/ScalarEvolution.h"
25#include "llvm/Analysis/ScalarEvolutionExpander.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000026#include "llvm/Transforms/Utils/BasicBlockUtils.h"
27#include "llvm/Support/CommandLine.h"
28
29#include "isl/aff.h"
30#include "isl/set.h"
31
32using namespace llvm;
33using namespace polly;
34
35static cl::opt<bool>
Tobias Grosserc14582f2013-02-05 18:01:29 +000036Aligned("enable-polly-aligned", cl::desc("Assumed aligned memory accesses."),
37 cl::Hidden, cl::value_desc("OpenMP code generation enabled if true"),
38 cl::init(false), cl::ZeroOrMore);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000039
Tobias Grosserecfe21b2013-03-20 18:03:18 +000040static cl::opt<bool, true>
41SCEVCodegenF("polly-codegen-scev", cl::desc("Use SCEV based code generation."),
42 cl::Hidden, cl::location(SCEVCodegen), cl::init(false),
43 cl::ZeroOrMore);
44
45bool polly::SCEVCodegen;
46
47bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
48 ScalarEvolution *SE, const Region *R) {
49 if (SCEVCodegen) {
50 if (!I || !SE->isSCEVable(I->getType()))
51 return false;
52
53 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
54 if (!isa<SCEVCouldNotCompute>(Scev))
55 if (!hasScalarDepsInsideRegion(Scev, R))
56 return true;
57
58 return false;
59 }
60
61 Loop *L = LI->getLoopFor(I->getParent());
62 return L && I == L->getCanonicalInductionVariable();
63}
64
Tobias Grossere71c6ab2012-04-27 16:36:14 +000065
Hongbin Zheng3b11a162012-04-25 13:16:49 +000066// Helper class to generate memory location.
67namespace {
68class IslGenerator {
69public:
Tobias Grosser7242ad92013-02-22 08:07:06 +000070 IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS)
71 : Builder(Builder), IVS(IVS) {}
Hongbin Zheng3b11a162012-04-25 13:16:49 +000072 Value *generateIslInt(__isl_take isl_int Int);
73 Value *generateIslAff(__isl_take isl_aff *Aff);
74 Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff);
75
76private:
77 typedef struct {
78 Value *Result;
79 class IslGenerator *Generator;
80 } IslGenInfo;
81
82 IRBuilder<> &Builder;
83 std::vector<Value *> &IVS;
Tobias Grosser1bb59b02012-12-29 23:47:38 +000084 static int mergeIslAffValues(__isl_take isl_set *Set, __isl_take isl_aff *Aff,
85 void *User);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000086};
87}
88
Hongbin Zheng3b11a162012-04-25 13:16:49 +000089Value *IslGenerator::generateIslInt(isl_int Int) {
90 mpz_t IntMPZ;
91 mpz_init(IntMPZ);
92 isl_int_get_gmp(Int, IntMPZ);
93 Value *IntValue = Builder.getInt(APInt_from_MPZ(IntMPZ));
94 mpz_clear(IntMPZ);
95 return IntValue;
96}
97
98Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) {
99 Value *Result;
100 Value *ConstValue;
101 isl_int ConstIsl;
102
103 isl_int_init(ConstIsl);
104 isl_aff_get_constant(Aff, &ConstIsl);
105 ConstValue = generateIslInt(ConstIsl);
106 Type *Ty = Builder.getInt64Ty();
107
108 // FIXME: We should give the constant and coefficients the right type. Here
109 // we force it into i64.
110 Result = Builder.CreateSExtOrBitCast(ConstValue, Ty);
111
112 unsigned int NbInputDims = isl_aff_dim(Aff, isl_dim_in);
113
Tobias Grosser7242ad92013-02-22 08:07:06 +0000114 assert((IVS.size() == NbInputDims) &&
115 "The Dimension of Induction Variables must match the dimension of the "
116 "affine space.");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000117
118 isl_int CoefficientIsl;
119 isl_int_init(CoefficientIsl);
120
121 for (unsigned int i = 0; i < NbInputDims; ++i) {
122 Value *CoefficientValue;
123 isl_aff_get_coefficient(Aff, isl_dim_in, i, &CoefficientIsl);
124
125 if (isl_int_is_zero(CoefficientIsl))
126 continue;
127
128 CoefficientValue = generateIslInt(CoefficientIsl);
129 CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true);
130 Value *IV = Builder.CreateIntCast(IVS[i], Ty, true);
131 Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff");
132 Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff");
133 }
134
135 isl_int_clear(CoefficientIsl);
136 isl_int_clear(ConstIsl);
137 isl_aff_free(Aff);
138
139 return Result;
140}
141
142int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set,
143 __isl_take isl_aff *Aff, void *User) {
144 IslGenInfo *GenInfo = (IslGenInfo *)User;
145
Tobias Grosser7242ad92013-02-22 08:07:06 +0000146 assert((GenInfo->Result == NULL) &&
147 "Result is already set. Currently only single isl_aff is supported");
148 assert(isl_set_plain_is_universe(Set) &&
149 "Code generation failed because the set is not universe");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000150
151 GenInfo->Result = GenInfo->Generator->generateIslAff(Aff);
152
153 isl_set_free(Set);
154 return 0;
155}
156
157Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) {
158 IslGenInfo User;
159 User.Result = NULL;
160 User.Generator = this;
161 isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User);
162 assert(User.Result && "Code generation for isl_pw_aff failed");
163
164 isl_pw_aff_free(PwAff);
165 return User.Result;
166}
167
Tobias Grosser7242ad92013-02-22 08:07:06 +0000168BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P)
169 : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {
170}
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000171
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000172Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000173 ValueMapT &GlobalMap, LoopToScevMapT &LTS,
174 Loop *L) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000175 // We assume constants never change.
176 // This avoids map lookups for many calls to this function.
177 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +0000178 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000179
180 if (GlobalMap.count(Old)) {
181 Value *New = GlobalMap[Old];
182
Tobias Grosserc14582f2013-02-05 18:01:29 +0000183 if (Old->getType()->getScalarSizeInBits() <
184 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000185 New = Builder.CreateTruncOrBitCast(New, Old->getType());
186
187 return New;
188 }
189
190 if (BBMap.count(Old)) {
191 return BBMap[Old];
192 }
193
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000194 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000195 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000196 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000197 const SCEV *NewScev = apply(Scev, LTS, SE);
198 ValueToValueMap VTV;
199 VTV.insert(BBMap.begin(), BBMap.end());
200 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000201 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000202 SCEVExpander Expander(SE, "polly");
203 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
204 Builder.GetInsertPoint());
205
206 BBMap[Old] = Expanded;
207 return Expanded;
208 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000209 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000210
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000211 if (const Instruction *Inst = dyn_cast<Instruction>(Old)) {
212 (void) Inst;
213 assert(!Statement.getParent()->getRegion().contains(Inst->getParent()) &&
214 "unexpected scalar dependence in region");
215 }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000216
217 // Everything else is probably a scop-constant value defined as global,
218 // function parameter or an instruction not within the scop.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000219 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000220}
221
222void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000223 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000224 Instruction *NewInst = Inst->clone();
225
226 // Replace old operands with the new ones.
227 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000228 OE = Inst->op_end();
229 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000230 Value *OldOperand = *OI;
Tobias Grosser369430f2013-03-22 23:42:53 +0000231 Value *NewOperand =
232 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000233
234 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000235 assert(!isa<StoreInst>(NewInst) &&
236 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000237 delete NewInst;
238 return;
239 }
240
241 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
242 }
243
244 Builder.Insert(NewInst);
245 BBMap[Inst] = NewInst;
246
247 if (!NewInst->getType()->isVoidTy())
248 NewInst->setName("p_" + Inst->getName());
249}
250
Tobias Grosserc14582f2013-02-05 18:01:29 +0000251std::vector<Value *> BlockGenerator::getMemoryAccessIndex(
252 __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000253 ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000254
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000255 assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) &&
256 "Only single dimensional access functions supported");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000257
258 std::vector<Value *> IVS;
259 for (unsigned i = 0; i < Statement.getNumIterators(); ++i) {
260 const Value *OriginalIV = Statement.getInductionVariableForDimension(i);
Tobias Grosser369430f2013-03-22 23:42:53 +0000261 Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000262 IVS.push_back(NewIV);
263 }
264
265 isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0);
266 IslGenerator IslGen(Builder, IVS);
267 Value *OffsetValue = IslGen.generateIslPwAff(PwAff);
268
269 Type *Ty = Builder.getInt64Ty();
270 OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true);
271
Tobias Grosserc14582f2013-02-05 18:01:29 +0000272 std::vector<Value *> IndexArray;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000273 Value *NullValue = Constant::getNullValue(Ty);
274 IndexArray.push_back(NullValue);
275 IndexArray.push_back(OffsetValue);
276 return IndexArray;
277}
278
279Value *BlockGenerator::getNewAccessOperand(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000280 __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000281 ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
Tobias Grosser7242ad92013-02-22 08:07:06 +0000282 std::vector<Value *> IndexArray = getMemoryAccessIndex(
Tobias Grosser369430f2013-03-22 23:42:53 +0000283 NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS, L);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000284 Value *NewOperand =
285 Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000286 return NewOperand;
287}
288
Tobias Grosserc14582f2013-02-05 18:01:29 +0000289Value *BlockGenerator::generateLocationAccessed(
290 const Instruction *Inst, const Value *Pointer, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000291 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000292 MemoryAccess &Access = Statement.getAccessFor(Inst);
293 isl_map *CurrentAccessRelation = Access.getAccessRelation();
294 isl_map *NewAccessRelation = Access.getNewAccessRelation();
295
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000296 assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) &&
297 "Current and new access function use different spaces");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000298
299 Value *NewPointer;
300
301 if (!NewAccessRelation) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000302 NewPointer =
303 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000304 } else {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000305 Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr());
Tobias Grosser7242ad92013-02-22 08:07:06 +0000306 NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000307 GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000308 }
309
310 isl_map_free(CurrentAccessRelation);
311 isl_map_free(NewAccessRelation);
312 return NewPointer;
313}
314
Tobias Grosser369430f2013-03-22 23:42:53 +0000315Loop *
316BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
317 return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent());
318}
319
Tobias Grosser7242ad92013-02-22 08:07:06 +0000320Value *
321BlockGenerator::generateScalarLoad(const LoadInst *Load, ValueMapT &BBMap,
322 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000323 const Value *Pointer = Load->getPointerOperand();
324 const Instruction *Inst = dyn_cast<Instruction>(Load);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000325 Value *NewPointer =
326 generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000327 Value *ScalarLoad =
328 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000329 return ScalarLoad;
330}
331
Tobias Grosser7242ad92013-02-22 08:07:06 +0000332Value *
333BlockGenerator::generateScalarStore(const StoreInst *Store, ValueMapT &BBMap,
334 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000335 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000336 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000337 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000338 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
339 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000340
341 return Builder.CreateStore(ValueOperand, NewPointer);
342}
343
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000344void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000345 ValueMapT &GlobalMap,
346 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000347 // Terminator instructions control the control flow. They are explicitly
348 // expressed in the clast and do not need to be copied.
349 if (Inst->isTerminator())
350 return;
351
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000352 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
353 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000354 return;
355
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000356 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000357 BBMap[Load] = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358 return;
359 }
360
361 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000362 BBMap[Store] = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000363 return;
364 }
365
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000366 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000367}
368
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000369void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000370 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000371 BasicBlock *CopyBB =
372 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000373 CopyBB->setName("polly.stmt." + BB->getName());
374 Builder.SetInsertPoint(CopyBB->begin());
375
376 ValueMapT BBMap;
377
378 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
379 ++II)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000380 copyInstruction(II, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000381}
382
Tobias Grosserc14582f2013-02-05 18:01:29 +0000383VectorBlockGenerator::VectorBlockGenerator(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000384 IRBuilder<> &B, VectorValueMapT &GlobalMaps,
385 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
386 __isl_keep isl_map *Schedule, Pass *P)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000387 : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS),
388 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000389 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
390 assert(Schedule && "No statement domain provided");
391}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000392
Tobias Grosserc14582f2013-02-05 18:01:29 +0000393Value *VectorBlockGenerator::getVectorValue(
Tobias Grosser369430f2013-03-22 23:42:53 +0000394 const Value *Old, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps,
395 Loop *L) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000396 if (VectorMap.count(Old))
397 return VectorMap[Old];
398
399 int Width = getVectorWidth();
400
401 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
402
403 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000404 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000405 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000406 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000407 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000408
409 VectorMap[Old] = Vector;
410
411 return Vector;
412}
413
414Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
415 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
416 assert(PointerTy && "PointerType expected");
417
418 Type *ScalarType = PointerTy->getElementType();
419 VectorType *VectorType = VectorType::get(ScalarType, Width);
420
421 return PointerType::getUnqual(VectorType);
422}
423
424Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
425 ValueMapT &BBMap) {
426 const Value *Pointer = Load->getPointerOperand();
427 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Tobias Grosser369430f2013-03-22 23:42:53 +0000428 Value *NewPointer =
429 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000430 Value *VectorPtr =
431 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
432 LoadInst *VecLoad =
433 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000434 if (!Aligned)
435 VecLoad->setAlignment(8);
436
437 return VecLoad;
438}
439
440Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
441 ValueMapT &BBMap) {
442 const Value *Pointer = Load->getPointerOperand();
443 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000444 Value *NewPointer =
445 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000446 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
447 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000448 LoadInst *ScalarLoad =
449 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000450
451 if (!Aligned)
452 ScalarLoad->setAlignment(8);
453
Tobias Grosserc14582f2013-02-05 18:01:29 +0000454 Constant *SplatVector = Constant::getNullValue(
455 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000456
Tobias Grosserc14582f2013-02-05 18:01:29 +0000457 Value *VectorLoad = Builder.CreateShuffleVector(
458 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000459 return VectorLoad;
460}
461
Tobias Grosserc14582f2013-02-05 18:01:29 +0000462Value *VectorBlockGenerator::generateUnknownStrideLoad(
463 const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000464 int VectorWidth = getVectorWidth();
465 const Value *Pointer = Load->getPointerOperand();
466 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000467 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000468
469 Value *Vector = UndefValue::get(VectorType);
470
471 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000472 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
473 VLTS[i], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000474 Value *ScalarLoad =
475 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
476 Vector = Builder.CreateInsertElement(
477 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000478 }
479
480 return Vector;
481}
482
Tobias Grosserc14582f2013-02-05 18:01:29 +0000483void VectorBlockGenerator::generateLoad(
484 const LoadInst *Load, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000485 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
486 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000487 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000488 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000489 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000490 return;
491 }
492
493 MemoryAccess &Access = Statement.getAccessFor(Load);
494
495 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000496 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000497 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000498 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000499 NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
500 else
501 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
502
503 VectorMap[Load] = NewLoad;
504}
505
506void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
507 ValueMapT &VectorMap,
508 VectorValueMapT &ScalarMaps) {
509 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000510 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
511 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000512
513 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
514
515 const CastInst *Cast = dyn_cast<CastInst>(Inst);
516 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
517 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
518}
519
520void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
521 ValueMapT &VectorMap,
522 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000523 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000524 Value *OpZero = Inst->getOperand(0);
525 Value *OpOne = Inst->getOperand(1);
526
527 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000528 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
529 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000530
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000531 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000532 Inst->getName() + "p_vec");
533 VectorMap[Inst] = NewInst;
534}
535
Tobias Grosserc14582f2013-02-05 18:01:29 +0000536void VectorBlockGenerator::copyStore(
537 const StoreInst *Store, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000538 int VectorWidth = getVectorWidth();
539
540 MemoryAccess &Access = Statement.getAccessFor(Store);
541
542 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000543 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
544 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000545
Sebastian Popa00a0292012-12-18 07:46:06 +0000546 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000547 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
Tobias Grosser369430f2013-03-22 23:42:53 +0000548 Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0],
549 VLTS[0], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000550
Tobias Grosserc14582f2013-02-05 18:01:29 +0000551 Value *VectorPtr =
552 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000553 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
554
555 if (!Aligned)
556 Store->setAlignment(8);
557 } else {
558 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000559 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser369430f2013-03-22 23:42:53 +0000560 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
561 VLTS[i], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000562 Builder.CreateStore(Scalar, NewPointer);
563 }
564 }
565}
566
567bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
568 ValueMapT &VectorMap) {
569 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000570 OE = Inst->op_end();
571 OI != OE; ++OI)
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000572 if (VectorMap.count(*OI))
573 return true;
574 return false;
575}
576
577bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
578 ValueMapT &VectorMap,
579 VectorValueMapT &ScalarMaps) {
580 bool HasVectorOperand = false;
581 int VectorWidth = getVectorWidth();
582
583 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000584 OE = Inst->op_end();
585 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000586 ValueMapT::iterator VecOp = VectorMap.find(*OI);
587
588 if (VecOp == VectorMap.end())
589 continue;
590
591 HasVectorOperand = true;
592 Value *NewVector = VecOp->second;
593
594 for (int i = 0; i < VectorWidth; ++i) {
595 ValueMapT &SM = ScalarMaps[i];
596
597 // If there is one scalar extracted, all scalar elements should have
598 // already been extracted by the code here. So no need to check for the
599 // existance of all of them.
600 if (SM.count(*OI))
601 break;
602
603 SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
604 }
605 }
606
607 return HasVectorOperand;
608}
609
610void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
611 ValueMapT &VectorMap,
612 VectorValueMapT &ScalarMaps) {
613 bool HasVectorOperand;
614 int VectorWidth = getVectorWidth();
615
616 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
617
618 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000619 copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
620 VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000621
622 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
623 return;
624
625 // Make the result available as vector value.
626 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
627 Value *Vector = UndefValue::get(VectorType);
628
629 for (int i = 0; i < VectorWidth; i++)
630 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
631 Builder.getInt32(i));
632
633 VectorMap[Inst] = Vector;
634}
635
Tobias Grosserc14582f2013-02-05 18:01:29 +0000636int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000637
638void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
639 ValueMapT &VectorMap,
640 VectorValueMapT &ScalarMaps) {
641 // Terminator instructions control the control flow. They are explicitly
642 // expressed in the clast and do not need to be copied.
643 if (Inst->isTerminator())
644 return;
645
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000646 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
647 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000648 return;
649
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000650 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
651 generateLoad(Load, VectorMap, ScalarMaps);
652 return;
653 }
654
655 if (hasVectorOperands(Inst, VectorMap)) {
656 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
657 copyStore(Store, VectorMap, ScalarMaps);
658 return;
659 }
660
661 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
662 copyUnaryInst(Unary, VectorMap, ScalarMaps);
663 return;
664 }
665
666 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
667 copyBinaryInst(Binary, VectorMap, ScalarMaps);
668 return;
669 }
670
671 // Falltrough: We generate scalar instructions, if we don't know how to
672 // generate vector code.
673 }
674
675 copyInstScalarized(Inst, VectorMap, ScalarMaps);
676}
677
678void VectorBlockGenerator::copyBB() {
679 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000680 BasicBlock *CopyBB =
681 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000682 CopyBB->setName("polly.stmt." + BB->getName());
683 Builder.SetInsertPoint(CopyBB->begin());
684
685 // Create two maps that store the mapping from the original instructions of
686 // the old basic block to their copies in the new basic block. Those maps
687 // are basic block local.
688 //
689 // As vector code generation is supported there is one map for scalar values
690 // and one for vector values.
691 //
692 // In case we just do scalar code generation, the vectorMap is not used and
693 // the scalarMap has just one dimension, which contains the mapping.
694 //
695 // In case vector code generation is done, an instruction may either appear
696 // in the vector map once (as it is calculating >vectorwidth< values at a
697 // time. Or (if the values are calculated using scalar operations), it
698 // appears once in every dimension of the scalarMap.
699 VectorValueMapT ScalarBlockMap(getVectorWidth());
700 ValueMapT VectorBlockMap;
701
Tobias Grosserc14582f2013-02-05 18:01:29 +0000702 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
703 ++II)
704 copyInstruction(II, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000705}