blob: 063ec77a868f65b204ad29663ec681459e1a8fdd [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,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000173 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000174 // We assume constants never change.
175 // This avoids map lookups for many calls to this function.
176 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +0000177 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000178
179 if (GlobalMap.count(Old)) {
180 Value *New = GlobalMap[Old];
181
Tobias Grosserc14582f2013-02-05 18:01:29 +0000182 if (Old->getType()->getScalarSizeInBits() <
183 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000184 New = Builder.CreateTruncOrBitCast(New, Old->getType());
185
186 return New;
187 }
188
189 if (BBMap.count(Old)) {
190 return BBMap[Old];
191 }
192
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000193 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosserc14582f2013-02-05 18:01:29 +0000194 if (const SCEV *Scev = SE.getSCEV(const_cast<Value *>(Old)))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000195 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000196 const SCEV *NewScev = apply(Scev, LTS, SE);
197 ValueToValueMap VTV;
198 VTV.insert(BBMap.begin(), BBMap.end());
199 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000200 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000201 SCEVExpander Expander(SE, "polly");
202 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
203 Builder.GetInsertPoint());
204
205 BBMap[Old] = Expanded;
206 return Expanded;
207 }
208
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000209 if (const Instruction *Inst = dyn_cast<Instruction>(Old)) {
210 (void) Inst;
211 assert(!Statement.getParent()->getRegion().contains(Inst->getParent()) &&
212 "unexpected scalar dependence in region");
213 }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000214
215 // Everything else is probably a scop-constant value defined as global,
216 // function parameter or an instruction not within the scop.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000217 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000218}
219
220void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000221 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000222 Instruction *NewInst = Inst->clone();
223
224 // Replace old operands with the new ones.
225 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000226 OE = Inst->op_end();
227 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000228 Value *OldOperand = *OI;
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000229 Value *NewOperand = getNewValue(OldOperand, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000230
231 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000232 assert(!isa<StoreInst>(NewInst) &&
233 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000234 delete NewInst;
235 return;
236 }
237
238 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
239 }
240
241 Builder.Insert(NewInst);
242 BBMap[Inst] = NewInst;
243
244 if (!NewInst->getType()->isVoidTy())
245 NewInst->setName("p_" + Inst->getName());
246}
247
Tobias Grosserc14582f2013-02-05 18:01:29 +0000248std::vector<Value *> BlockGenerator::getMemoryAccessIndex(
249 __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000250 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000251
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000252 assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) &&
253 "Only single dimensional access functions supported");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000254
255 std::vector<Value *> IVS;
256 for (unsigned i = 0; i < Statement.getNumIterators(); ++i) {
257 const Value *OriginalIV = Statement.getInductionVariableForDimension(i);
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000258 Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000259 IVS.push_back(NewIV);
260 }
261
262 isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0);
263 IslGenerator IslGen(Builder, IVS);
264 Value *OffsetValue = IslGen.generateIslPwAff(PwAff);
265
266 Type *Ty = Builder.getInt64Ty();
267 OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true);
268
Tobias Grosserc14582f2013-02-05 18:01:29 +0000269 std::vector<Value *> IndexArray;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000270 Value *NullValue = Constant::getNullValue(Ty);
271 IndexArray.push_back(NullValue);
272 IndexArray.push_back(OffsetValue);
273 return IndexArray;
274}
275
276Value *BlockGenerator::getNewAccessOperand(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000277 __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000278 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosser7242ad92013-02-22 08:07:06 +0000279 std::vector<Value *> IndexArray = getMemoryAccessIndex(
280 NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000281 Value *NewOperand =
282 Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000283 return NewOperand;
284}
285
Tobias Grosserc14582f2013-02-05 18:01:29 +0000286Value *BlockGenerator::generateLocationAccessed(
287 const Instruction *Inst, const Value *Pointer, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000288 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000289 MemoryAccess &Access = Statement.getAccessFor(Inst);
290 isl_map *CurrentAccessRelation = Access.getAccessRelation();
291 isl_map *NewAccessRelation = Access.getNewAccessRelation();
292
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000293 assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) &&
294 "Current and new access function use different spaces");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000295
296 Value *NewPointer;
297
298 if (!NewAccessRelation) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000299 NewPointer = getNewValue(Pointer, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000300 } else {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000301 Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr());
Tobias Grosser7242ad92013-02-22 08:07:06 +0000302 NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap,
303 GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000304 }
305
306 isl_map_free(CurrentAccessRelation);
307 isl_map_free(NewAccessRelation);
308 return NewPointer;
309}
310
Tobias Grosser7242ad92013-02-22 08:07:06 +0000311Value *
312BlockGenerator::generateScalarLoad(const LoadInst *Load, ValueMapT &BBMap,
313 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000314 const Value *Pointer = Load->getPointerOperand();
315 const Instruction *Inst = dyn_cast<Instruction>(Load);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000316 Value *NewPointer =
317 generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000318 Value *ScalarLoad =
319 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000320 return ScalarLoad;
321}
322
Tobias Grosser7242ad92013-02-22 08:07:06 +0000323Value *
324BlockGenerator::generateScalarStore(const StoreInst *Store, ValueMapT &BBMap,
325 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000326 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000327 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000328 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000329 Value *ValueOperand =
330 getNewValue(Store->getValueOperand(), BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000331
332 return Builder.CreateStore(ValueOperand, NewPointer);
333}
334
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000335void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000336 ValueMapT &GlobalMap,
337 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000338 // Terminator instructions control the control flow. They are explicitly
339 // expressed in the clast and do not need to be copied.
340 if (Inst->isTerminator())
341 return;
342
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000343 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
344 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000345 return;
346
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000347 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000348 BBMap[Load] = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000349 return;
350 }
351
352 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000353 BBMap[Store] = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000354 return;
355 }
356
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000357 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358}
359
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000360void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000361 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000362 BasicBlock *CopyBB =
363 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000364 CopyBB->setName("polly.stmt." + BB->getName());
365 Builder.SetInsertPoint(CopyBB->begin());
366
367 ValueMapT BBMap;
368
369 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
370 ++II)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000371 copyInstruction(II, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000372}
373
Tobias Grosserc14582f2013-02-05 18:01:29 +0000374VectorBlockGenerator::VectorBlockGenerator(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000375 IRBuilder<> &B, VectorValueMapT &GlobalMaps,
376 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
377 __isl_keep isl_map *Schedule, Pass *P)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000378 : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS),
379 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000380 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
381 assert(Schedule && "No statement domain provided");
382}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000383
Tobias Grosserc14582f2013-02-05 18:01:29 +0000384Value *VectorBlockGenerator::getVectorValue(
385 const Value *Old, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000386 if (VectorMap.count(Old))
387 return VectorMap[Old];
388
389 int Width = getVectorWidth();
390
391 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
392
393 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000394 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000395 Vector,
396 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane]),
397 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000398
399 VectorMap[Old] = Vector;
400
401 return Vector;
402}
403
404Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
405 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
406 assert(PointerTy && "PointerType expected");
407
408 Type *ScalarType = PointerTy->getElementType();
409 VectorType *VectorType = VectorType::get(ScalarType, Width);
410
411 return PointerType::getUnqual(VectorType);
412}
413
414Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
415 ValueMapT &BBMap) {
416 const Value *Pointer = Load->getPointerOperand();
417 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000418 Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000419 Value *VectorPtr =
420 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
421 LoadInst *VecLoad =
422 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000423 if (!Aligned)
424 VecLoad->setAlignment(8);
425
426 return VecLoad;
427}
428
429Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
430 ValueMapT &BBMap) {
431 const Value *Pointer = Load->getPointerOperand();
432 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000433 Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000434 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
435 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000436 LoadInst *ScalarLoad =
437 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000438
439 if (!Aligned)
440 ScalarLoad->setAlignment(8);
441
Tobias Grosserc14582f2013-02-05 18:01:29 +0000442 Constant *SplatVector = Constant::getNullValue(
443 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000444
Tobias Grosserc14582f2013-02-05 18:01:29 +0000445 Value *VectorLoad = Builder.CreateShuffleVector(
446 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000447 return VectorLoad;
448}
449
Tobias Grosserc14582f2013-02-05 18:01:29 +0000450Value *VectorBlockGenerator::generateUnknownStrideLoad(
451 const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000452 int VectorWidth = getVectorWidth();
453 const Value *Pointer = Load->getPointerOperand();
454 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000455 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000456
457 Value *Vector = UndefValue::get(VectorType);
458
459 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser7242ad92013-02-22 08:07:06 +0000460 Value *NewPointer =
461 getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000462 Value *ScalarLoad =
463 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
464 Vector = Builder.CreateInsertElement(
465 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000466 }
467
468 return Vector;
469}
470
Tobias Grosserc14582f2013-02-05 18:01:29 +0000471void VectorBlockGenerator::generateLoad(
472 const LoadInst *Load, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000473 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
474 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000475 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000476 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000477 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000478 return;
479 }
480
481 MemoryAccess &Access = Statement.getAccessFor(Load);
482
483 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000484 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000485 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000486 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000487 NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
488 else
489 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
490
491 VectorMap[Load] = NewLoad;
492}
493
494void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
495 ValueMapT &VectorMap,
496 VectorValueMapT &ScalarMaps) {
497 int VectorWidth = getVectorWidth();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000498 Value *NewOperand =
499 getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000500
501 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
502
503 const CastInst *Cast = dyn_cast<CastInst>(Inst);
504 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
505 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
506}
507
508void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
509 ValueMapT &VectorMap,
510 VectorValueMapT &ScalarMaps) {
511 Value *OpZero = Inst->getOperand(0);
512 Value *OpOne = Inst->getOperand(1);
513
514 Value *NewOpZero, *NewOpOne;
515 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps);
516 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps);
517
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000518 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000519 Inst->getName() + "p_vec");
520 VectorMap[Inst] = NewInst;
521}
522
Tobias Grosserc14582f2013-02-05 18:01:29 +0000523void VectorBlockGenerator::copyStore(
524 const StoreInst *Store, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000525 int VectorWidth = getVectorWidth();
526
527 MemoryAccess &Access = Statement.getAccessFor(Store);
528
529 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000530 Value *Vector =
531 getVectorValue(Store->getValueOperand(), VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000532
Sebastian Popa00a0292012-12-18 07:46:06 +0000533 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000534 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000535 Value *NewPointer =
536 getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000537
Tobias Grosserc14582f2013-02-05 18:01:29 +0000538 Value *VectorPtr =
539 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000540 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
541
542 if (!Aligned)
543 Store->setAlignment(8);
544 } else {
545 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000546 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser7242ad92013-02-22 08:07:06 +0000547 Value *NewPointer =
548 getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000549 Builder.CreateStore(Scalar, NewPointer);
550 }
551 }
552}
553
554bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
555 ValueMapT &VectorMap) {
556 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000557 OE = Inst->op_end();
558 OI != OE; ++OI)
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000559 if (VectorMap.count(*OI))
560 return true;
561 return false;
562}
563
564bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
565 ValueMapT &VectorMap,
566 VectorValueMapT &ScalarMaps) {
567 bool HasVectorOperand = false;
568 int VectorWidth = getVectorWidth();
569
570 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000571 OE = Inst->op_end();
572 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000573 ValueMapT::iterator VecOp = VectorMap.find(*OI);
574
575 if (VecOp == VectorMap.end())
576 continue;
577
578 HasVectorOperand = true;
579 Value *NewVector = VecOp->second;
580
581 for (int i = 0; i < VectorWidth; ++i) {
582 ValueMapT &SM = ScalarMaps[i];
583
584 // If there is one scalar extracted, all scalar elements should have
585 // already been extracted by the code here. So no need to check for the
586 // existance of all of them.
587 if (SM.count(*OI))
588 break;
589
590 SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
591 }
592 }
593
594 return HasVectorOperand;
595}
596
597void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
598 ValueMapT &VectorMap,
599 VectorValueMapT &ScalarMaps) {
600 bool HasVectorOperand;
601 int VectorWidth = getVectorWidth();
602
603 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
604
605 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000606 copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
607 VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000608
609 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
610 return;
611
612 // Make the result available as vector value.
613 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
614 Value *Vector = UndefValue::get(VectorType);
615
616 for (int i = 0; i < VectorWidth; i++)
617 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
618 Builder.getInt32(i));
619
620 VectorMap[Inst] = Vector;
621}
622
Tobias Grosserc14582f2013-02-05 18:01:29 +0000623int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000624
625void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
626 ValueMapT &VectorMap,
627 VectorValueMapT &ScalarMaps) {
628 // Terminator instructions control the control flow. They are explicitly
629 // expressed in the clast and do not need to be copied.
630 if (Inst->isTerminator())
631 return;
632
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000633 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
634 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000635 return;
636
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000637 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
638 generateLoad(Load, VectorMap, ScalarMaps);
639 return;
640 }
641
642 if (hasVectorOperands(Inst, VectorMap)) {
643 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
644 copyStore(Store, VectorMap, ScalarMaps);
645 return;
646 }
647
648 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
649 copyUnaryInst(Unary, VectorMap, ScalarMaps);
650 return;
651 }
652
653 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
654 copyBinaryInst(Binary, VectorMap, ScalarMaps);
655 return;
656 }
657
658 // Falltrough: We generate scalar instructions, if we don't know how to
659 // generate vector code.
660 }
661
662 copyInstScalarized(Inst, VectorMap, ScalarMaps);
663}
664
665void VectorBlockGenerator::copyBB() {
666 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000667 BasicBlock *CopyBB =
668 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000669 CopyBB->setName("polly.stmt." + BB->getName());
670 Builder.SetInsertPoint(CopyBB->begin());
671
672 // Create two maps that store the mapping from the original instructions of
673 // the old basic block to their copies in the new basic block. Those maps
674 // are basic block local.
675 //
676 // As vector code generation is supported there is one map for scalar values
677 // and one for vector values.
678 //
679 // In case we just do scalar code generation, the vectorMap is not used and
680 // the scalarMap has just one dimension, which contains the mapping.
681 //
682 // In case vector code generation is done, an instruction may either appear
683 // in the vector map once (as it is calculating >vectorwidth< values at a
684 // time. Or (if the values are calculated using scalar operations), it
685 // appears once in every dimension of the scalarMap.
686 VectorValueMapT ScalarBlockMap(getVectorWidth());
687 ValueMapT VectorBlockMap;
688
Tobias Grosserc14582f2013-02-05 18:01:29 +0000689 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
690 ++II)
691 copyInstruction(II, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000692}