blob: 051e52dd4f9706b445efc00139bebd2298fde3aa [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:
46 IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS) :
47 Builder(Builder), IVS(IVS) {}
48 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
90 assert((IVS.size() == NbInputDims) && "The Dimension of Induction Variables"
91 "must match the dimension of the affine space.");
92
93 isl_int CoefficientIsl;
94 isl_int_init(CoefficientIsl);
95
96 for (unsigned int i = 0; i < NbInputDims; ++i) {
97 Value *CoefficientValue;
98 isl_aff_get_coefficient(Aff, isl_dim_in, i, &CoefficientIsl);
99
100 if (isl_int_is_zero(CoefficientIsl))
101 continue;
102
103 CoefficientValue = generateIslInt(CoefficientIsl);
104 CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true);
105 Value *IV = Builder.CreateIntCast(IVS[i], Ty, true);
106 Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff");
107 Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff");
108 }
109
110 isl_int_clear(CoefficientIsl);
111 isl_int_clear(ConstIsl);
112 isl_aff_free(Aff);
113
114 return Result;
115}
116
117int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set,
118 __isl_take isl_aff *Aff, void *User) {
119 IslGenInfo *GenInfo = (IslGenInfo *)User;
120
121 assert((GenInfo->Result == NULL) && "Result is already set."
122 "Currently only single isl_aff is supported");
123 assert(isl_set_plain_is_universe(Set)
124 && "Code generation failed because the set is not universe");
125
126 GenInfo->Result = GenInfo->Generator->generateIslAff(Aff);
127
128 isl_set_free(Set);
129 return 0;
130}
131
132Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) {
133 IslGenInfo User;
134 User.Result = NULL;
135 User.Generator = this;
136 isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User);
137 assert(User.Result && "Code generation for isl_pw_aff failed");
138
139 isl_pw_aff_free(PwAff);
140 return User.Result;
141}
142
143
144BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P):
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000145 Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {}
146
147bool BlockGenerator::isSCEVIgnore(const Instruction *Inst) {
148 if (SCEVCodegen && SE.isSCEVable(Inst->getType()))
149 if (const SCEV *Scev = SE.getSCEV(const_cast<Instruction*>(Inst)))
150 if (!isa<SCEVCouldNotCompute>(Scev)) {
151 if (const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Scev)) {
152 if (Unknown->getValue() != Inst)
153 return true;
154 } else {
155 return true;
156 }
157 }
158
159 return false;
160}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000161
162Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000163 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000164 // We assume constants never change.
165 // This avoids map lookups for many calls to this function.
166 if (isa<Constant>(Old))
Tobias Grosserc14582f2013-02-05 18:01:29 +0000167 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000168
169 if (GlobalMap.count(Old)) {
170 Value *New = GlobalMap[Old];
171
Tobias Grosserc14582f2013-02-05 18:01:29 +0000172 if (Old->getType()->getScalarSizeInBits() <
173 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000174 New = Builder.CreateTruncOrBitCast(New, Old->getType());
175
176 return New;
177 }
178
179 if (BBMap.count(Old)) {
180 return BBMap[Old];
181 }
182
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000183 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosserc14582f2013-02-05 18:01:29 +0000184 if (const SCEV *Scev = SE.getSCEV(const_cast<Value *>(Old)))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000185 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000186 const SCEV *NewScev = apply(Scev, LTS, SE);
187 ValueToValueMap VTV;
188 VTV.insert(BBMap.begin(), BBMap.end());
189 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000190 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000191 SCEVExpander Expander(SE, "polly");
192 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
193 Builder.GetInsertPoint());
194
195 BBMap[Old] = Expanded;
196 return Expanded;
197 }
198
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000199 // 'Old' is within the original SCoP, but was not rewritten.
200 //
201 // Such values appear, if they only calculate information already available in
202 // the polyhedral description (e.g. an induction variable increment). They
203 // can be safely ignored.
204 if (const Instruction *Inst = dyn_cast<Instruction>(Old))
205 if (Statement.getParent()->getRegion().contains(Inst->getParent()))
206 return NULL;
207
208 // Everything else is probably a scop-constant value defined as global,
209 // function parameter or an instruction not within the scop.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000210 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000211}
212
213void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000214 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000215 Instruction *NewInst = Inst->clone();
216
217 // Replace old operands with the new ones.
218 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000219 OE = Inst->op_end();
220 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000221 Value *OldOperand = *OI;
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000222 Value *NewOperand = getNewValue(OldOperand, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000223
224 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000225 assert(!isa<StoreInst>(NewInst) &&
226 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000227 delete NewInst;
228 return;
229 }
230
231 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
232 }
233
234 Builder.Insert(NewInst);
235 BBMap[Inst] = NewInst;
236
237 if (!NewInst->getType()->isVoidTy())
238 NewInst->setName("p_" + Inst->getName());
239}
240
Tobias Grosserc14582f2013-02-05 18:01:29 +0000241std::vector<Value *> BlockGenerator::getMemoryAccessIndex(
242 __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000243 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000244
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000245 assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) &&
246 "Only single dimensional access functions supported");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000247
248 std::vector<Value *> IVS;
249 for (unsigned i = 0; i < Statement.getNumIterators(); ++i) {
250 const Value *OriginalIV = Statement.getInductionVariableForDimension(i);
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000251 Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000252 IVS.push_back(NewIV);
253 }
254
255 isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0);
256 IslGenerator IslGen(Builder, IVS);
257 Value *OffsetValue = IslGen.generateIslPwAff(PwAff);
258
259 Type *Ty = Builder.getInt64Ty();
260 OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true);
261
Tobias Grosserc14582f2013-02-05 18:01:29 +0000262 std::vector<Value *> IndexArray;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000263 Value *NullValue = Constant::getNullValue(Ty);
264 IndexArray.push_back(NullValue);
265 IndexArray.push_back(OffsetValue);
266 return IndexArray;
267}
268
269Value *BlockGenerator::getNewAccessOperand(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000270 __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000271 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000272 std::vector<Value *> IndexArray =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000273 getMemoryAccessIndex(NewAccessRelation, BaseAddress, BBMap, GlobalMap,
274 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());
296 NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000297 getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap, GlobalMap,
298 LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000299 }
300
301 isl_map_free(CurrentAccessRelation);
302 isl_map_free(NewAccessRelation);
303 return NewPointer;
304}
305
Tobias Grosserc14582f2013-02-05 18:01:29 +0000306Value *BlockGenerator::generateScalarLoad(
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000307 const LoadInst *Load, ValueMapT &BBMap, ValueMapT &GlobalMap,
308 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000309 const Value *Pointer = Load->getPointerOperand();
310 const Instruction *Inst = dyn_cast<Instruction>(Load);
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000311 Value *NewPointer = generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap,
312 LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000313 Value *ScalarLoad =
314 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000315 return ScalarLoad;
316}
317
Tobias Grosserc14582f2013-02-05 18:01:29 +0000318Value *BlockGenerator::generateScalarStore(
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000319 const StoreInst *Store, ValueMapT &BBMap, ValueMapT &GlobalMap,
320 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000321 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000322 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000323 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
324 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
325 LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000326
327 return Builder.CreateStore(ValueOperand, NewPointer);
328}
329
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000330void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000331 ValueMapT &GlobalMap,
332 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000333 // Terminator instructions control the control flow. They are explicitly
334 // expressed in the clast and do not need to be copied.
335 if (Inst->isTerminator())
336 return;
337
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000338 if (isSCEVIgnore(Inst))
339 return;
340
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000341 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000342 BBMap[Load] = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000343 return;
344 }
345
346 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000347 BBMap[Store] = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000348 return;
349 }
350
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000351 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000352}
353
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000354void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000355 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000356 BasicBlock *CopyBB =
357 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358 CopyBB->setName("polly.stmt." + BB->getName());
359 Builder.SetInsertPoint(CopyBB->begin());
360
361 ValueMapT BBMap;
362
363 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
364 ++II)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000365 copyInstruction(II, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000366}
367
Tobias Grosserc14582f2013-02-05 18:01:29 +0000368VectorBlockGenerator::VectorBlockGenerator(
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000369 IRBuilder<> &B, VectorValueMapT &GlobalMaps, std::vector<LoopToScevMapT> &VLTS,
370 ScopStmt &Stmt, __isl_keep isl_map *Schedule, Pass *P)
371 : 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(
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000388 Vector, getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane],
389 VLTS[Lane]), Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000390
391 VectorMap[Old] = Vector;
392
393 return Vector;
394}
395
396Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
397 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
398 assert(PointerTy && "PointerType expected");
399
400 Type *ScalarType = PointerTy->getElementType();
401 VectorType *VectorType = VectorType::get(ScalarType, Width);
402
403 return PointerType::getUnqual(VectorType);
404}
405
406Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
407 ValueMapT &BBMap) {
408 const Value *Pointer = Load->getPointerOperand();
409 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000410 Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000411 Value *VectorPtr =
412 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
413 LoadInst *VecLoad =
414 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000415 if (!Aligned)
416 VecLoad->setAlignment(8);
417
418 return VecLoad;
419}
420
421Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
422 ValueMapT &BBMap) {
423 const Value *Pointer = Load->getPointerOperand();
424 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000425 Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000426 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
427 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000428 LoadInst *ScalarLoad =
429 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000430
431 if (!Aligned)
432 ScalarLoad->setAlignment(8);
433
Tobias Grosserc14582f2013-02-05 18:01:29 +0000434 Constant *SplatVector = Constant::getNullValue(
435 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000436
Tobias Grosserc14582f2013-02-05 18:01:29 +0000437 Value *VectorLoad = Builder.CreateShuffleVector(
438 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000439 return VectorLoad;
440}
441
Tobias Grosserc14582f2013-02-05 18:01:29 +0000442Value *VectorBlockGenerator::generateUnknownStrideLoad(
443 const LoadInst *Load, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000444 int VectorWidth = getVectorWidth();
445 const Value *Pointer = Load->getPointerOperand();
446 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000447 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000448
449 Value *Vector = UndefValue::get(VectorType);
450
451 for (int i = 0; i < VectorWidth; i++) {
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000452 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
453 VLTS[i]);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000454 Value *ScalarLoad =
455 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
456 Vector = Builder.CreateInsertElement(
457 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000458 }
459
460 return Vector;
461}
462
Tobias Grosserc14582f2013-02-05 18:01:29 +0000463void VectorBlockGenerator::generateLoad(
464 const LoadInst *Load, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000465 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
466 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000467 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000468 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000469 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000470 return;
471 }
472
473 MemoryAccess &Access = Statement.getAccessFor(Load);
474
475 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000476 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000477 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000478 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000479 NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
480 else
481 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
482
483 VectorMap[Load] = NewLoad;
484}
485
486void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
487 ValueMapT &VectorMap,
488 VectorValueMapT &ScalarMaps) {
489 int VectorWidth = getVectorWidth();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000490 Value *NewOperand =
491 getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000492
493 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
494
495 const CastInst *Cast = dyn_cast<CastInst>(Inst);
496 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
497 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
498}
499
500void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
501 ValueMapT &VectorMap,
502 VectorValueMapT &ScalarMaps) {
503 Value *OpZero = Inst->getOperand(0);
504 Value *OpOne = Inst->getOperand(1);
505
506 Value *NewOpZero, *NewOpOne;
507 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps);
508 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps);
509
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000510 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000511 Inst->getName() + "p_vec");
512 VectorMap[Inst] = NewInst;
513}
514
Tobias Grosserc14582f2013-02-05 18:01:29 +0000515void VectorBlockGenerator::copyStore(
516 const StoreInst *Store, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000517 int VectorWidth = getVectorWidth();
518
519 MemoryAccess &Access = Statement.getAccessFor(Store);
520
521 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000522 Value *Vector =
523 getVectorValue(Store->getValueOperand(), VectorMap, ScalarMaps);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000524
Sebastian Popa00a0292012-12-18 07:46:06 +0000525 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000526 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000527 Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0],
528 VLTS[0]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000529
Tobias Grosserc14582f2013-02-05 18:01:29 +0000530 Value *VectorPtr =
531 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000532 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
533
534 if (!Aligned)
535 Store->setAlignment(8);
536 } else {
537 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000538 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000539 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
540 VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000541 Builder.CreateStore(Scalar, NewPointer);
542 }
543 }
544}
545
546bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
547 ValueMapT &VectorMap) {
548 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000549 OE = Inst->op_end();
550 OI != OE; ++OI)
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000551 if (VectorMap.count(*OI))
552 return true;
553 return false;
554}
555
556bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
557 ValueMapT &VectorMap,
558 VectorValueMapT &ScalarMaps) {
559 bool HasVectorOperand = false;
560 int VectorWidth = getVectorWidth();
561
562 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000563 OE = Inst->op_end();
564 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000565 ValueMapT::iterator VecOp = VectorMap.find(*OI);
566
567 if (VecOp == VectorMap.end())
568 continue;
569
570 HasVectorOperand = true;
571 Value *NewVector = VecOp->second;
572
573 for (int i = 0; i < VectorWidth; ++i) {
574 ValueMapT &SM = ScalarMaps[i];
575
576 // If there is one scalar extracted, all scalar elements should have
577 // already been extracted by the code here. So no need to check for the
578 // existance of all of them.
579 if (SM.count(*OI))
580 break;
581
582 SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
583 }
584 }
585
586 return HasVectorOperand;
587}
588
589void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
590 ValueMapT &VectorMap,
591 VectorValueMapT &ScalarMaps) {
592 bool HasVectorOperand;
593 int VectorWidth = getVectorWidth();
594
595 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
596
597 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000598 copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
599 VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000600
601 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
602 return;
603
604 // Make the result available as vector value.
605 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
606 Value *Vector = UndefValue::get(VectorType);
607
608 for (int i = 0; i < VectorWidth; i++)
609 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
610 Builder.getInt32(i));
611
612 VectorMap[Inst] = Vector;
613}
614
Tobias Grosserc14582f2013-02-05 18:01:29 +0000615int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000616
617void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
618 ValueMapT &VectorMap,
619 VectorValueMapT &ScalarMaps) {
620 // Terminator instructions control the control flow. They are explicitly
621 // expressed in the clast and do not need to be copied.
622 if (Inst->isTerminator())
623 return;
624
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000625 if (isSCEVIgnore(Inst))
626 return;
627
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000628 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
629 generateLoad(Load, VectorMap, ScalarMaps);
630 return;
631 }
632
633 if (hasVectorOperands(Inst, VectorMap)) {
634 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
635 copyStore(Store, VectorMap, ScalarMaps);
636 return;
637 }
638
639 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
640 copyUnaryInst(Unary, VectorMap, ScalarMaps);
641 return;
642 }
643
644 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
645 copyBinaryInst(Binary, VectorMap, ScalarMaps);
646 return;
647 }
648
649 // Falltrough: We generate scalar instructions, if we don't know how to
650 // generate vector code.
651 }
652
653 copyInstScalarized(Inst, VectorMap, ScalarMaps);
654}
655
656void VectorBlockGenerator::copyBB() {
657 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000658 BasicBlock *CopyBB =
659 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000660 CopyBB->setName("polly.stmt." + BB->getName());
661 Builder.SetInsertPoint(CopyBB->begin());
662
663 // Create two maps that store the mapping from the original instructions of
664 // the old basic block to their copies in the new basic block. Those maps
665 // are basic block local.
666 //
667 // As vector code generation is supported there is one map for scalar values
668 // and one for vector values.
669 //
670 // In case we just do scalar code generation, the vectorMap is not used and
671 // the scalarMap has just one dimension, which contains the mapping.
672 //
673 // In case vector code generation is done, an instruction may either appear
674 // in the vector map once (as it is calculating >vectorwidth< values at a
675 // time. Or (if the values are calculated using scalar operations), it
676 // appears once in every dimension of the scalarMap.
677 VectorValueMapT ScalarBlockMap(getVectorWidth());
678 ValueMapT VectorBlockMap;
679
Tobias Grosserc14582f2013-02-05 18:01:29 +0000680 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
681 ++II)
682 copyInstruction(II, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000683}