blob: a07188e86668515369b26fa2db9abe5198dc4951 [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 Grossere602a072013-05-07 07:30:56 +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);
Tobias Grosserecfe21b2013-03-20 18:03:18 +000044
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());
Tobias Grossere8df5bd2013-04-17 07:20:36 +000062 return L && I == L->getCanonicalInductionVariable() && R->contains(L);
Tobias Grosserecfe21b2013-03-20 18:03:18 +000063}
64
Hongbin Zheng3b11a162012-04-25 13:16:49 +000065// Helper class to generate memory location.
66namespace {
67class IslGenerator {
68public:
Tobias Grosser7242ad92013-02-22 08:07:06 +000069 IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS)
70 : Builder(Builder), IVS(IVS) {}
Hongbin Zheng3b11a162012-04-25 13:16:49 +000071 Value *generateIslInt(__isl_take isl_int Int);
72 Value *generateIslAff(__isl_take isl_aff *Aff);
73 Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff);
74
75private:
76 typedef struct {
77 Value *Result;
78 class IslGenerator *Generator;
79 } IslGenInfo;
80
81 IRBuilder<> &Builder;
82 std::vector<Value *> &IVS;
Tobias Grosser1bb59b02012-12-29 23:47:38 +000083 static int mergeIslAffValues(__isl_take isl_set *Set, __isl_take isl_aff *Aff,
84 void *User);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000085};
86}
87
Hongbin Zheng3b11a162012-04-25 13:16:49 +000088Value *IslGenerator::generateIslInt(isl_int Int) {
89 mpz_t IntMPZ;
90 mpz_init(IntMPZ);
91 isl_int_get_gmp(Int, IntMPZ);
92 Value *IntValue = Builder.getInt(APInt_from_MPZ(IntMPZ));
93 mpz_clear(IntMPZ);
94 return IntValue;
95}
96
97Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) {
98 Value *Result;
99 Value *ConstValue;
100 isl_int ConstIsl;
101
102 isl_int_init(ConstIsl);
103 isl_aff_get_constant(Aff, &ConstIsl);
104 ConstValue = generateIslInt(ConstIsl);
105 Type *Ty = Builder.getInt64Ty();
106
107 // FIXME: We should give the constant and coefficients the right type. Here
108 // we force it into i64.
109 Result = Builder.CreateSExtOrBitCast(ConstValue, Ty);
110
111 unsigned int NbInputDims = isl_aff_dim(Aff, isl_dim_in);
112
Tobias Grosser7242ad92013-02-22 08:07:06 +0000113 assert((IVS.size() == NbInputDims) &&
114 "The Dimension of Induction Variables must match the dimension of the "
115 "affine space.");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000116
117 isl_int CoefficientIsl;
118 isl_int_init(CoefficientIsl);
119
120 for (unsigned int i = 0; i < NbInputDims; ++i) {
121 Value *CoefficientValue;
122 isl_aff_get_coefficient(Aff, isl_dim_in, i, &CoefficientIsl);
123
124 if (isl_int_is_zero(CoefficientIsl))
125 continue;
126
127 CoefficientValue = generateIslInt(CoefficientIsl);
128 CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true);
129 Value *IV = Builder.CreateIntCast(IVS[i], Ty, true);
130 Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff");
131 Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff");
132 }
133
134 isl_int_clear(CoefficientIsl);
135 isl_int_clear(ConstIsl);
136 isl_aff_free(Aff);
137
138 return Result;
139}
140
141int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set,
142 __isl_take isl_aff *Aff, void *User) {
143 IslGenInfo *GenInfo = (IslGenInfo *)User;
144
Tobias Grosser7242ad92013-02-22 08:07:06 +0000145 assert((GenInfo->Result == NULL) &&
146 "Result is already set. Currently only single isl_aff is supported");
147 assert(isl_set_plain_is_universe(Set) &&
148 "Code generation failed because the set is not universe");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000149
150 GenInfo->Result = GenInfo->Generator->generateIslAff(Aff);
151
152 isl_set_free(Set);
153 return 0;
154}
155
156Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) {
157 IslGenInfo User;
158 User.Result = NULL;
159 User.Generator = this;
160 isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User);
161 assert(User.Result && "Code generation for isl_pw_aff failed");
162
163 isl_pw_aff_free(PwAff);
164 return User.Result;
165}
166
Tobias Grosser7242ad92013-02-22 08:07:06 +0000167BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P)
168 : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {
169}
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000170
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000171Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000172 ValueMapT &GlobalMap, LoopToScevMapT &LTS,
173 Loop *L) {
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() <
Tobias Grosserd7e58642013-04-10 06:55:45 +0000183 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 Grosser369430f2013-03-22 23:42:53 +0000194 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
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 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000208 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000209
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000210 if (const Instruction *Inst = dyn_cast<Instruction>(Old)) {
211 (void) Inst;
212 assert(!Statement.getParent()->getRegion().contains(Inst->getParent()) &&
213 "unexpected scalar dependence in region");
214 }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000215
216 // Everything else is probably a scop-constant value defined as global,
217 // function parameter or an instruction not within the scop.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000218 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000219}
220
221void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000222 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000223 Instruction *NewInst = Inst->clone();
224
225 // Replace old operands with the new ones.
226 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000227 OE = Inst->op_end();
228 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000229 Value *OldOperand = *OI;
Tobias Grosser369430f2013-03-22 23:42:53 +0000230 Value *NewOperand =
231 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000232
233 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000234 assert(!isa<StoreInst>(NewInst) &&
235 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000236 delete NewInst;
237 return;
238 }
239
240 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
241 }
242
243 Builder.Insert(NewInst);
244 BBMap[Inst] = NewInst;
245
246 if (!NewInst->getType()->isVoidTy())
247 NewInst->setName("p_" + Inst->getName());
248}
249
Tobias Grosserc14582f2013-02-05 18:01:29 +0000250std::vector<Value *> BlockGenerator::getMemoryAccessIndex(
251 __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000252 ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000253
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000254 assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) &&
255 "Only single dimensional access functions supported");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000256
257 std::vector<Value *> IVS;
258 for (unsigned i = 0; i < Statement.getNumIterators(); ++i) {
259 const Value *OriginalIV = Statement.getInductionVariableForDimension(i);
Tobias Grosser369430f2013-03-22 23:42:53 +0000260 Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000261 IVS.push_back(NewIV);
262 }
263
264 isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0);
265 IslGenerator IslGen(Builder, IVS);
266 Value *OffsetValue = IslGen.generateIslPwAff(PwAff);
267
268 Type *Ty = Builder.getInt64Ty();
269 OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true);
270
Tobias Grosserc14582f2013-02-05 18:01:29 +0000271 std::vector<Value *> IndexArray;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000272 Value *NullValue = Constant::getNullValue(Ty);
273 IndexArray.push_back(NullValue);
274 IndexArray.push_back(OffsetValue);
275 return IndexArray;
276}
277
278Value *BlockGenerator::getNewAccessOperand(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000279 __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000280 ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
Tobias Grosser7242ad92013-02-22 08:07:06 +0000281 std::vector<Value *> IndexArray = getMemoryAccessIndex(
Tobias Grosser369430f2013-03-22 23:42:53 +0000282 NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS, L);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000283 Value *NewOperand =
284 Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000285 return NewOperand;
286}
287
Tobias Grossere602a072013-05-07 07:30:56 +0000288Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
289 const Value *Pointer,
290 ValueMapT &BBMap,
291 ValueMapT &GlobalMap,
292 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000293 MemoryAccess &Access = Statement.getAccessFor(Inst);
294 isl_map *CurrentAccessRelation = Access.getAccessRelation();
295 isl_map *NewAccessRelation = Access.getNewAccessRelation();
296
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000297 assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) &&
298 "Current and new access function use different spaces");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000299
300 Value *NewPointer;
301
302 if (!NewAccessRelation) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000303 NewPointer =
304 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000305 } else {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000306 Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr());
Tobias Grosser7242ad92013-02-22 08:07:06 +0000307 NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000308 GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000309 }
310
311 isl_map_free(CurrentAccessRelation);
312 isl_map_free(NewAccessRelation);
313 return NewPointer;
314}
315
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000316Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000317 return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent());
318}
319
Tobias Grossere602a072013-05-07 07:30:56 +0000320Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
321 ValueMapT &BBMap,
322 ValueMapT &GlobalMap,
323 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000324 const Value *Pointer = Load->getPointerOperand();
325 const Instruction *Inst = dyn_cast<Instruction>(Load);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000326 Value *NewPointer =
327 generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000328 Value *ScalarLoad =
329 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000330 return ScalarLoad;
331}
332
Tobias Grossere602a072013-05-07 07:30:56 +0000333Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
334 ValueMapT &BBMap,
335 ValueMapT &GlobalMap,
336 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000337 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000338 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000339 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000340 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
341 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000342
343 return Builder.CreateStore(ValueOperand, NewPointer);
344}
345
Tobias Grossere602a072013-05-07 07:30:56 +0000346void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
347 ValueMapT &GlobalMap,
348 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000349 // Terminator instructions control the control flow. They are explicitly
350 // expressed in the clast and do not need to be copied.
351 if (Inst->isTerminator())
352 return;
353
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000354 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
355 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000356 return;
357
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000359 BBMap[Load] = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000360 return;
361 }
362
363 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000364 BBMap[Store] = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000365 return;
366 }
367
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000368 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000369}
370
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000371void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000372 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000373 BasicBlock *CopyBB =
374 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000375 CopyBB->setName("polly.stmt." + BB->getName());
376 Builder.SetInsertPoint(CopyBB->begin());
377
378 ValueMapT BBMap;
379
380 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
381 ++II)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000382 copyInstruction(II, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000383}
384
Tobias Grossere602a072013-05-07 07:30:56 +0000385VectorBlockGenerator::VectorBlockGenerator(IRBuilder<> &B,
386 VectorValueMapT &GlobalMaps,
387 std::vector<LoopToScevMapT> &VLTS,
388 ScopStmt &Stmt,
389 __isl_keep isl_map *Schedule,
390 Pass *P)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000391 : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS),
392 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000393 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
394 assert(Schedule && "No statement domain provided");
395}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000396
Tobias Grossere602a072013-05-07 07:30:56 +0000397Value *VectorBlockGenerator::getVectorValue(const Value *Old,
398 ValueMapT &VectorMap,
399 VectorValueMapT &ScalarMaps,
400 Loop *L) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000401 if (VectorMap.count(Old))
402 return VectorMap[Old];
403
404 int Width = getVectorWidth();
405
406 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
407
408 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000409 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000410 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000411 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000412 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000413
414 VectorMap[Old] = Vector;
415
416 return Vector;
417}
418
419Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
420 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
421 assert(PointerTy && "PointerType expected");
422
423 Type *ScalarType = PointerTy->getElementType();
424 VectorType *VectorType = VectorType::get(ScalarType, Width);
425
426 return PointerType::getUnqual(VectorType);
427}
428
429Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
430 ValueMapT &BBMap) {
431 const Value *Pointer = Load->getPointerOperand();
432 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Tobias Grosser369430f2013-03-22 23:42:53 +0000433 Value *NewPointer =
434 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000435 Value *VectorPtr =
436 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
437 LoadInst *VecLoad =
438 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000439 if (!Aligned)
440 VecLoad->setAlignment(8);
441
442 return VecLoad;
443}
444
445Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
446 ValueMapT &BBMap) {
447 const Value *Pointer = Load->getPointerOperand();
448 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000449 Value *NewPointer =
450 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000451 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
452 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000453 LoadInst *ScalarLoad =
454 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000455
456 if (!Aligned)
457 ScalarLoad->setAlignment(8);
458
Tobias Grosserc14582f2013-02-05 18:01:29 +0000459 Constant *SplatVector = Constant::getNullValue(
460 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000461
Tobias Grosserc14582f2013-02-05 18:01:29 +0000462 Value *VectorLoad = Builder.CreateShuffleVector(
463 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000464 return VectorLoad;
465}
466
Tobias Grossere602a072013-05-07 07:30:56 +0000467Value *
468VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
469 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000470 int VectorWidth = getVectorWidth();
471 const Value *Pointer = Load->getPointerOperand();
472 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000473 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000474
475 Value *Vector = UndefValue::get(VectorType);
476
477 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000478 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
479 VLTS[i], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000480 Value *ScalarLoad =
481 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
482 Vector = Builder.CreateInsertElement(
483 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000484 }
485
486 return Vector;
487}
488
Tobias Grossere602a072013-05-07 07:30:56 +0000489void VectorBlockGenerator::generateLoad(const LoadInst *Load,
490 ValueMapT &VectorMap,
491 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000492 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
493 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000494 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000495 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000496 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000497 return;
498 }
499
500 MemoryAccess &Access = Statement.getAccessFor(Load);
501
502 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000503 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000504 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000505 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000506 NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
507 else
508 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
509
510 VectorMap[Load] = NewLoad;
511}
512
513void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
514 ValueMapT &VectorMap,
515 VectorValueMapT &ScalarMaps) {
516 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000517 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
518 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000519
520 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
521
522 const CastInst *Cast = dyn_cast<CastInst>(Inst);
523 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
524 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
525}
526
527void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
528 ValueMapT &VectorMap,
529 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000530 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000531 Value *OpZero = Inst->getOperand(0);
532 Value *OpOne = Inst->getOperand(1);
533
534 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000535 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
536 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000537
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000538 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000539 Inst->getName() + "p_vec");
540 VectorMap[Inst] = NewInst;
541}
542
Tobias Grossere602a072013-05-07 07:30:56 +0000543void VectorBlockGenerator::copyStore(const StoreInst *Store,
544 ValueMapT &VectorMap,
545 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000546 int VectorWidth = getVectorWidth();
547
548 MemoryAccess &Access = Statement.getAccessFor(Store);
549
550 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000551 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
552 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000553
Sebastian Popa00a0292012-12-18 07:46:06 +0000554 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000555 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
Tobias Grosser369430f2013-03-22 23:42:53 +0000556 Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0],
557 VLTS[0], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000558
Tobias Grosserc14582f2013-02-05 18:01:29 +0000559 Value *VectorPtr =
560 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000561 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
562
563 if (!Aligned)
564 Store->setAlignment(8);
565 } else {
566 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000567 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser369430f2013-03-22 23:42:53 +0000568 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
569 VLTS[i], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000570 Builder.CreateStore(Scalar, NewPointer);
571 }
572 }
573}
574
575bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
576 ValueMapT &VectorMap) {
577 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000578 OE = Inst->op_end();
579 OI != OE; ++OI)
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000580 if (VectorMap.count(*OI))
581 return true;
582 return false;
583}
584
585bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
586 ValueMapT &VectorMap,
587 VectorValueMapT &ScalarMaps) {
588 bool HasVectorOperand = false;
589 int VectorWidth = getVectorWidth();
590
591 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000592 OE = Inst->op_end();
593 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000594 ValueMapT::iterator VecOp = VectorMap.find(*OI);
595
596 if (VecOp == VectorMap.end())
597 continue;
598
599 HasVectorOperand = true;
600 Value *NewVector = VecOp->second;
601
602 for (int i = 0; i < VectorWidth; ++i) {
603 ValueMapT &SM = ScalarMaps[i];
604
605 // If there is one scalar extracted, all scalar elements should have
606 // already been extracted by the code here. So no need to check for the
607 // existance of all of them.
608 if (SM.count(*OI))
609 break;
610
611 SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
612 }
613 }
614
615 return HasVectorOperand;
616}
617
618void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
619 ValueMapT &VectorMap,
620 VectorValueMapT &ScalarMaps) {
621 bool HasVectorOperand;
622 int VectorWidth = getVectorWidth();
623
624 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
625
626 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000627 copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
628 VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000629
630 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
631 return;
632
633 // Make the result available as vector value.
634 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
635 Value *Vector = UndefValue::get(VectorType);
636
637 for (int i = 0; i < VectorWidth; i++)
638 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
639 Builder.getInt32(i));
640
641 VectorMap[Inst] = Vector;
642}
643
Tobias Grosserc14582f2013-02-05 18:01:29 +0000644int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000645
646void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
647 ValueMapT &VectorMap,
648 VectorValueMapT &ScalarMaps) {
649 // Terminator instructions control the control flow. They are explicitly
650 // expressed in the clast and do not need to be copied.
651 if (Inst->isTerminator())
652 return;
653
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000654 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
655 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000656 return;
657
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000658 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
659 generateLoad(Load, VectorMap, ScalarMaps);
660 return;
661 }
662
663 if (hasVectorOperands(Inst, VectorMap)) {
664 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
665 copyStore(Store, VectorMap, ScalarMaps);
666 return;
667 }
668
669 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
670 copyUnaryInst(Unary, VectorMap, ScalarMaps);
671 return;
672 }
673
674 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
675 copyBinaryInst(Binary, VectorMap, ScalarMaps);
676 return;
677 }
678
679 // Falltrough: We generate scalar instructions, if we don't know how to
680 // generate vector code.
681 }
682
683 copyInstScalarized(Inst, VectorMap, ScalarMaps);
684}
685
686void VectorBlockGenerator::copyBB() {
687 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000688 BasicBlock *CopyBB =
689 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000690 CopyBB->setName("polly.stmt." + BB->getName());
691 Builder.SetInsertPoint(CopyBB->begin());
692
693 // Create two maps that store the mapping from the original instructions of
694 // the old basic block to their copies in the new basic block. Those maps
695 // are basic block local.
696 //
697 // As vector code generation is supported there is one map for scalar values
698 // and one for vector values.
699 //
700 // In case we just do scalar code generation, the vectorMap is not used and
701 // the scalarMap has just one dimension, which contains the mapping.
702 //
703 // In case vector code generation is done, an instruction may either appear
704 // in the vector map once (as it is calculating >vectorwidth< values at a
705 // time. Or (if the values are calculated using scalar operations), it
706 // appears once in every dimension of the scalarMap.
707 VectorValueMapT ScalarBlockMap(getVectorWidth());
708 ValueMapT VectorBlockMap;
709
Tobias Grosserc14582f2013-02-05 18:01:29 +0000710 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
711 ++II)
712 copyInstruction(II, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000713}