blob: cb7937e6c566c2a085a52d3d2b5d58d11bf4c7c8 [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"
20
Tobias Grossere71c6ab2012-04-27 16:36:14 +000021#include "llvm/Analysis/LoopInfo.h"
22#include "llvm/Analysis/ScalarEvolution.h"
23#include "llvm/Analysis/ScalarEvolutionExpander.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000024#include "llvm/Transforms/Utils/BasicBlockUtils.h"
25#include "llvm/Support/CommandLine.h"
26
27#include "isl/aff.h"
28#include "isl/set.h"
29
30using namespace llvm;
31using namespace polly;
32
33static cl::opt<bool>
Tobias Grosserc14582f2013-02-05 18:01:29 +000034Aligned("enable-polly-aligned", cl::desc("Assumed aligned memory accesses."),
35 cl::Hidden, cl::value_desc("OpenMP code generation enabled if true"),
36 cl::init(false), cl::ZeroOrMore);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000037
38static cl::opt<bool>
Tobias Grosserc14582f2013-02-05 18:01:29 +000039SCEVCodegen("polly-codegen-scev", cl::desc("Use SCEV based code generation."),
40 cl::Hidden, cl::init(false), cl::ZeroOrMore);
Tobias Grossere71c6ab2012-04-27 16:36:14 +000041
Hongbin Zheng3b11a162012-04-25 13:16:49 +000042// Helper class to generate memory location.
43namespace {
44class IslGenerator {
45public:
Tobias Grosser7242ad92013-02-22 08:07:06 +000046 IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS)
47 : Builder(Builder), IVS(IVS) {}
Hongbin Zheng3b11a162012-04-25 13:16:49 +000048 Value *generateIslInt(__isl_take isl_int Int);
49 Value *generateIslAff(__isl_take isl_aff *Aff);
50 Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff);
51
52private:
53 typedef struct {
54 Value *Result;
55 class IslGenerator *Generator;
56 } IslGenInfo;
57
58 IRBuilder<> &Builder;
59 std::vector<Value *> &IVS;
Tobias Grosser1bb59b02012-12-29 23:47:38 +000060 static int mergeIslAffValues(__isl_take isl_set *Set, __isl_take isl_aff *Aff,
61 void *User);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000062};
63}
64
Hongbin Zheng3b11a162012-04-25 13:16:49 +000065Value *IslGenerator::generateIslInt(isl_int Int) {
66 mpz_t IntMPZ;
67 mpz_init(IntMPZ);
68 isl_int_get_gmp(Int, IntMPZ);
69 Value *IntValue = Builder.getInt(APInt_from_MPZ(IntMPZ));
70 mpz_clear(IntMPZ);
71 return IntValue;
72}
73
74Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) {
75 Value *Result;
76 Value *ConstValue;
77 isl_int ConstIsl;
78
79 isl_int_init(ConstIsl);
80 isl_aff_get_constant(Aff, &ConstIsl);
81 ConstValue = generateIslInt(ConstIsl);
82 Type *Ty = Builder.getInt64Ty();
83
84 // FIXME: We should give the constant and coefficients the right type. Here
85 // we force it into i64.
86 Result = Builder.CreateSExtOrBitCast(ConstValue, Ty);
87
88 unsigned int NbInputDims = isl_aff_dim(Aff, isl_dim_in);
89
Tobias Grosser7242ad92013-02-22 08:07:06 +000090 assert((IVS.size() == NbInputDims) &&
91 "The Dimension of Induction Variables must match the dimension of the "
92 "affine space.");
Hongbin Zheng3b11a162012-04-25 13:16:49 +000093
94 isl_int CoefficientIsl;
95 isl_int_init(CoefficientIsl);
96
97 for (unsigned int i = 0; i < NbInputDims; ++i) {
98 Value *CoefficientValue;
99 isl_aff_get_coefficient(Aff, isl_dim_in, i, &CoefficientIsl);
100
101 if (isl_int_is_zero(CoefficientIsl))
102 continue;
103
104 CoefficientValue = generateIslInt(CoefficientIsl);
105 CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true);
106 Value *IV = Builder.CreateIntCast(IVS[i], Ty, true);
107 Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff");
108 Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff");
109 }
110
111 isl_int_clear(CoefficientIsl);
112 isl_int_clear(ConstIsl);
113 isl_aff_free(Aff);
114
115 return Result;
116}
117
118int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set,
119 __isl_take isl_aff *Aff, void *User) {
120 IslGenInfo *GenInfo = (IslGenInfo *)User;
121
Tobias Grosser7242ad92013-02-22 08:07:06 +0000122 assert((GenInfo->Result == NULL) &&
123 "Result is already set. Currently only single isl_aff is supported");
124 assert(isl_set_plain_is_universe(Set) &&
125 "Code generation failed because the set is not universe");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000126
127 GenInfo->Result = GenInfo->Generator->generateIslAff(Aff);
128
129 isl_set_free(Set);
130 return 0;
131}
132
133Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) {
134 IslGenInfo User;
135 User.Result = NULL;
136 User.Generator = this;
137 isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User);
138 assert(User.Result && "Code generation for isl_pw_aff failed");
139
140 isl_pw_aff_free(PwAff);
141 return User.Result;
142}
143
Tobias Grosser7242ad92013-02-22 08:07:06 +0000144BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P)
145 : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {
146}
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000147
148bool BlockGenerator::isSCEVIgnore(const Instruction *Inst) {
149 if (SCEVCodegen && SE.isSCEVable(Inst->getType()))
150 if (const SCEV *Scev = SE.getSCEV(const_cast<Instruction*>(Inst)))
151 if (!isa<SCEVCouldNotCompute>(Scev)) {
152 if (const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Scev)) {
153 if (Unknown->getValue() != Inst)
154 return true;
155 } else {
156 return true;
157 }
158 }
159
160 return false;
161}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000162
163Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000164 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000165 // We assume constants never change.
166 // This avoids map lookups for many calls to this function.
167 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +0000168 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000169
170 if (GlobalMap.count(Old)) {
171 Value *New = GlobalMap[Old];
172
Tobias Grosserc14582f2013-02-05 18:01:29 +0000173 if (Old->getType()->getScalarSizeInBits() <
174 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000175 New = Builder.CreateTruncOrBitCast(New, Old->getType());
176
177 return New;
178 }
179
180 if (BBMap.count(Old)) {
181 return BBMap[Old];
182 }
183
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000184 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosserc14582f2013-02-05 18:01:29 +0000185 if (const SCEV *Scev = SE.getSCEV(const_cast<Value *>(Old)))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000186 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000187 const SCEV *NewScev = apply(Scev, LTS, SE);
188 ValueToValueMap VTV;
189 VTV.insert(BBMap.begin(), BBMap.end());
190 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000191 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000192 SCEVExpander Expander(SE, "polly");
193 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
194 Builder.GetInsertPoint());
195
196 BBMap[Old] = Expanded;
197 return Expanded;
198 }
199
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000200 // 'Old' is within the original SCoP, but was not rewritten.
201 //
202 // Such values appear, if they only calculate information already available in
203 // the polyhedral description (e.g. an induction variable increment). They
204 // can be safely ignored.
205 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
206 if (Statement.getParent()->getRegion().contains(Inst->getParent()))
207 return NULL;
208
209 // Everything else is probably a scop-constant value defined as global,
210 // function parameter or an instruction not within the scop.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000211 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000212}
213
214void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000215 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000216 Instruction *NewInst = Inst->clone();
217
218 // Replace old operands with the new ones.
219 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000220 OE = Inst->op_end();
221 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000222 Value *OldOperand = *OI;
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000223 Value *NewOperand = getNewValue(OldOperand, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000224
225 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000226 assert(!isa<StoreInst>(NewInst) &&
227 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000228 delete NewInst;
229 return;
230 }
231
232 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
233 }
234
235 Builder.Insert(NewInst);
236 BBMap[Inst] = NewInst;
237
238 if (!NewInst->getType()->isVoidTy())
239 NewInst->setName("p_" + Inst->getName());
240}
241
Tobias Grosserc14582f2013-02-05 18:01:29 +0000242std::vector<Value *> BlockGenerator::getMemoryAccessIndex(
243 __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000244 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000245
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000246 assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) &&
247 "Only single dimensional access functions supported");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000248
249 std::vector<Value *> IVS;
250 for (unsigned i = 0; i < Statement.getNumIterators(); ++i) {
251 const Value *OriginalIV = Statement.getInductionVariableForDimension(i);
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000252 Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000253 IVS.push_back(NewIV);
254 }
255
256 isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0);
257 IslGenerator IslGen(Builder, IVS);
258 Value *OffsetValue = IslGen.generateIslPwAff(PwAff);
259
260 Type *Ty = Builder.getInt64Ty();
261 OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true);
262
Tobias Grosserc14582f2013-02-05 18:01:29 +0000263 std::vector<Value *> IndexArray;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000264 Value *NullValue = Constant::getNullValue(Ty);
265 IndexArray.push_back(NullValue);
266 IndexArray.push_back(OffsetValue);
267 return IndexArray;
268}
269
270Value *BlockGenerator::getNewAccessOperand(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000271 __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000272 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosser7242ad92013-02-22 08:07:06 +0000273 std::vector<Value *> IndexArray = getMemoryAccessIndex(
274 NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000275 Value *NewOperand =
276 Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000277 return NewOperand;
278}
279
Tobias Grosserc14582f2013-02-05 18:01:29 +0000280Value *BlockGenerator::generateLocationAccessed(
281 const Instruction *Inst, const Value *Pointer, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000282 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000283 MemoryAccess &Access = Statement.getAccessFor(Inst);
284 isl_map *CurrentAccessRelation = Access.getAccessRelation();
285 isl_map *NewAccessRelation = Access.getNewAccessRelation();
286
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000287 assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) &&
288 "Current and new access function use different spaces");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000289
290 Value *NewPointer;
291
292 if (!NewAccessRelation) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000293 NewPointer = getNewValue(Pointer, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000294 } else {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000295 Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr());
Tobias Grosser7242ad92013-02-22 08:07:06 +0000296 NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap,
297 GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000298 }
299
300 isl_map_free(CurrentAccessRelation);
301 isl_map_free(NewAccessRelation);
302 return NewPointer;
303}
304
Tobias Grosser7242ad92013-02-22 08:07:06 +0000305Value *
306BlockGenerator::generateScalarLoad(const LoadInst *Load, ValueMapT &BBMap,
307 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000308 const Value *Pointer = Load->getPointerOperand();
309 const Instruction *Inst = dyn_cast<Instruction>(Load);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000310 Value *NewPointer =
311 generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000312 Value *ScalarLoad =
313 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000314 return ScalarLoad;
315}
316
Tobias Grosser7242ad92013-02-22 08:07:06 +0000317Value *
318BlockGenerator::generateScalarStore(const StoreInst *Store, ValueMapT &BBMap,
319 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000320 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000321 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000322 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000323 Value *ValueOperand =
324 getNewValue(Store->getValueOperand(), BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000325
326 return Builder.CreateStore(ValueOperand, NewPointer);
327}
328
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000329void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000330 ValueMapT &GlobalMap,
331 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000332 // Terminator instructions control the control flow. They are explicitly
333 // expressed in the clast and do not need to be copied.
334 if (Inst->isTerminator())
335 return;
336
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000337 if (isSCEVIgnore(Inst))
338 return;
339
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000340 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000341 BBMap[Load] = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000342 return;
343 }
344
345 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000346 BBMap[Store] = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000347 return;
348 }
349
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000350 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000351}
352
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000353void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000354 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000355 BasicBlock *CopyBB =
356 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000357 CopyBB->setName("polly.stmt." + BB->getName());
358 Builder.SetInsertPoint(CopyBB->begin());
359
360 ValueMapT BBMap;
361
362 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
363 ++II)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000364 copyInstruction(II, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000365}
366
Tobias Grosserc14582f2013-02-05 18:01:29 +0000367VectorBlockGenerator::VectorBlockGenerator(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000368 IRBuilder<> &B, VectorValueMapT &GlobalMaps,
369 std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt,
370 __isl_keep isl_map *Schedule, Pass *P)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000371 : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS),
372 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000373 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
374 assert(Schedule && "No statement domain provided");
375}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000376
Tobias Grosserc14582f2013-02-05 18:01:29 +0000377Value *VectorBlockGenerator::getVectorValue(
378 const Value *Old, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000379 if (VectorMap.count(Old))
380 return VectorMap[Old];
381
382 int Width = getVectorWidth();
383
384 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
385
386 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000387 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000388 Vector,
389 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane]),
390 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000391
392 VectorMap[Old] = Vector;
393
394 return Vector;
395}
396
397Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
398 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
399 assert(PointerTy && "PointerType expected");
400
401 Type *ScalarType = PointerTy->getElementType();
402 VectorType *VectorType = VectorType::get(ScalarType, Width);
403
404 return PointerType::getUnqual(VectorType);
405}
406
407Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
408 ValueMapT &BBMap) {
409 const Value *Pointer = Load->getPointerOperand();
410 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000411 Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000412 Value *VectorPtr =
413 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
414 LoadInst *VecLoad =
415 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000416 if (!Aligned)
417 VecLoad->setAlignment(8);
418
419 return VecLoad;
420}
421
422Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
423 ValueMapT &BBMap) {
424 const Value *Pointer = Load->getPointerOperand();
425 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000426 Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000427 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
428 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000429 LoadInst *ScalarLoad =
430 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000431
432 if (!Aligned)
433 ScalarLoad->setAlignment(8);
434
Tobias Grosserc14582f2013-02-05 18:01:29 +0000435 Constant *SplatVector = Constant::getNullValue(
436 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000437
Tobias Grosserc14582f2013-02-05 18:01:29 +0000438 Value *VectorLoad = Builder.CreateShuffleVector(
439 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000440 return VectorLoad;
441}
442
Tobias Grosserc14582f2013-02-05 18:01:29 +0000443Value *VectorBlockGenerator::generateUnknownStrideLoad(
444 const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000445 int VectorWidth = getVectorWidth();
446 const Value *Pointer = Load->getPointerOperand();
447 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000448 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000449
450 Value *Vector = UndefValue::get(VectorType);
451
452 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser7242ad92013-02-22 08:07:06 +0000453 Value *NewPointer =
454 getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000455 Value *ScalarLoad =
456 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
457 Vector = Builder.CreateInsertElement(
458 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000459 }
460
461 return Vector;
462}
463
Tobias Grosserc14582f2013-02-05 18:01:29 +0000464void VectorBlockGenerator::generateLoad(
465 const LoadInst *Load, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000466 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
467 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000468 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000469 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000470 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000471 return;
472 }
473
474 MemoryAccess &Access = Statement.getAccessFor(Load);
475
476 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000477 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000478 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000479 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000480 NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
481 else
482 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
483
484 VectorMap[Load] = NewLoad;
485}
486
487void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
488 ValueMapT &VectorMap,
489 VectorValueMapT &ScalarMaps) {
490 int VectorWidth = getVectorWidth();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000491 Value *NewOperand =
492 getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000493
494 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
495
496 const CastInst *Cast = dyn_cast<CastInst>(Inst);
497 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
498 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
499}
500
501void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
502 ValueMapT &VectorMap,
503 VectorValueMapT &ScalarMaps) {
504 Value *OpZero = Inst->getOperand(0);
505 Value *OpOne = Inst->getOperand(1);
506
507 Value *NewOpZero, *NewOpOne;
508 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps);
509 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps);
510
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000511 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000512 Inst->getName() + "p_vec");
513 VectorMap[Inst] = NewInst;
514}
515
Tobias Grosserc14582f2013-02-05 18:01:29 +0000516void VectorBlockGenerator::copyStore(
517 const StoreInst *Store, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000518 int VectorWidth = getVectorWidth();
519
520 MemoryAccess &Access = Statement.getAccessFor(Store);
521
522 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000523 Value *Vector =
524 getVectorValue(Store->getValueOperand(), VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000525
Sebastian Popa00a0292012-12-18 07:46:06 +0000526 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000527 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000528 Value *NewPointer =
529 getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000530
Tobias Grosserc14582f2013-02-05 18:01:29 +0000531 Value *VectorPtr =
532 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000533 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
534
535 if (!Aligned)
536 Store->setAlignment(8);
537 } else {
538 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000539 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser7242ad92013-02-22 08:07:06 +0000540 Value *NewPointer =
541 getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000542 Builder.CreateStore(Scalar, NewPointer);
543 }
544 }
545}
546
547bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
548 ValueMapT &VectorMap) {
549 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000550 OE = Inst->op_end();
551 OI != OE; ++OI)
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000552 if (VectorMap.count(*OI))
553 return true;
554 return false;
555}
556
557bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
558 ValueMapT &VectorMap,
559 VectorValueMapT &ScalarMaps) {
560 bool HasVectorOperand = false;
561 int VectorWidth = getVectorWidth();
562
563 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000564 OE = Inst->op_end();
565 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000566 ValueMapT::iterator VecOp = VectorMap.find(*OI);
567
568 if (VecOp == VectorMap.end())
569 continue;
570
571 HasVectorOperand = true;
572 Value *NewVector = VecOp->second;
573
574 for (int i = 0; i < VectorWidth; ++i) {
575 ValueMapT &SM = ScalarMaps[i];
576
577 // If there is one scalar extracted, all scalar elements should have
578 // already been extracted by the code here. So no need to check for the
579 // existance of all of them.
580 if (SM.count(*OI))
581 break;
582
583 SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
584 }
585 }
586
587 return HasVectorOperand;
588}
589
590void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
591 ValueMapT &VectorMap,
592 VectorValueMapT &ScalarMaps) {
593 bool HasVectorOperand;
594 int VectorWidth = getVectorWidth();
595
596 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
597
598 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000599 copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
600 VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000601
602 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
603 return;
604
605 // Make the result available as vector value.
606 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
607 Value *Vector = UndefValue::get(VectorType);
608
609 for (int i = 0; i < VectorWidth; i++)
610 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
611 Builder.getInt32(i));
612
613 VectorMap[Inst] = Vector;
614}
615
Tobias Grosserc14582f2013-02-05 18:01:29 +0000616int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000617
618void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
619 ValueMapT &VectorMap,
620 VectorValueMapT &ScalarMaps) {
621 // Terminator instructions control the control flow. They are explicitly
622 // expressed in the clast and do not need to be copied.
623 if (Inst->isTerminator())
624 return;
625
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000626 if (isSCEVIgnore(Inst))
627 return;
628
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000629 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
630 generateLoad(Load, VectorMap, ScalarMaps);
631 return;
632 }
633
634 if (hasVectorOperands(Inst, VectorMap)) {
635 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
636 copyStore(Store, VectorMap, ScalarMaps);
637 return;
638 }
639
640 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
641 copyUnaryInst(Unary, VectorMap, ScalarMaps);
642 return;
643 }
644
645 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
646 copyBinaryInst(Binary, VectorMap, ScalarMaps);
647 return;
648 }
649
650 // Falltrough: We generate scalar instructions, if we don't know how to
651 // generate vector code.
652 }
653
654 copyInstScalarized(Inst, VectorMap, ScalarMaps);
655}
656
657void VectorBlockGenerator::copyBB() {
658 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000659 BasicBlock *CopyBB =
660 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000661 CopyBB->setName("polly.stmt." + BB->getName());
662 Builder.SetInsertPoint(CopyBB->begin());
663
664 // Create two maps that store the mapping from the original instructions of
665 // the old basic block to their copies in the new basic block. Those maps
666 // are basic block local.
667 //
668 // As vector code generation is supported there is one map for scalar values
669 // and one for vector values.
670 //
671 // In case we just do scalar code generation, the vectorMap is not used and
672 // the scalarMap has just one dimension, which contains the mapping.
673 //
674 // In case vector code generation is done, an instruction may either appear
675 // in the vector map once (as it is calculating >vectorwidth< values at a
676 // time. Or (if the values are calculated using scalar operations), it
677 // appears once in every dimension of the scalarMap.
678 VectorValueMapT ScalarBlockMap(getVectorWidth());
679 ValueMapT VectorBlockMap;
680
Tobias Grosserc14582f2013-02-05 18:01:29 +0000681 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
682 ++II)
683 copyInstruction(II, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000684}