blob: a89de743a2c7b4ea975a2f576790f5a3f0ea22b9 [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"
Tobias Grosser83628182013-05-07 08:11:54 +000017#include "isl/aff.h"
18#include "isl/set.h"
Hongbin Zheng8a846612012-04-25 13:18:28 +000019#include "polly/CodeGen/BlockGenerators.h"
Tobias Grosser83628182013-05-07 08:11:54 +000020#include "polly/CodeGen/CodeGeneration.h"
Tobias Grosser637bd632013-05-07 07:31:10 +000021#include "polly/Options.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000022#include "polly/Support/GICHelper.h"
Sebastian Pop97cb8132013-03-18 20:21:13 +000023#include "polly/Support/SCEVValidator.h"
Tobias Grosserecfe21b2013-03-20 18:03:18 +000024#include "polly/Support/ScopHelper.h"
Tobias Grossere71c6ab2012-04-27 16:36:14 +000025#include "llvm/Analysis/LoopInfo.h"
26#include "llvm/Analysis/ScalarEvolution.h"
27#include "llvm/Analysis/ScalarEvolutionExpander.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000028#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Hongbin Zheng3b11a162012-04-25 13:16:49 +000029
Hongbin Zheng3b11a162012-04-25 13:16:49 +000030using 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"),
Tobias Grosser637bd632013-05-07 07:31:10 +000036 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000037
Tobias Grossere602a072013-05-07 07:30:56 +000038static cl::opt<bool, true>
39SCEVCodegenF("polly-codegen-scev", cl::desc("Use SCEV based code generation."),
40 cl::Hidden, cl::location(SCEVCodegen), cl::init(false),
Tobias Grosser637bd632013-05-07 07:31:10 +000041 cl::ZeroOrMore, cl::cat(PollyCategory));
Tobias Grosserecfe21b2013-03-20 18:03:18 +000042
43bool polly::SCEVCodegen;
44
45bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
46 ScalarEvolution *SE, const Region *R) {
47 if (SCEVCodegen) {
48 if (!I || !SE->isSCEVable(I->getType()))
49 return false;
50
51 if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
52 if (!isa<SCEVCouldNotCompute>(Scev))
53 if (!hasScalarDepsInsideRegion(Scev, R))
54 return true;
55
56 return false;
57 }
58
59 Loop *L = LI->getLoopFor(I->getParent());
Tobias Grossere8df5bd2013-04-17 07:20:36 +000060 return L && I == L->getCanonicalInductionVariable() && R->contains(L);
Tobias Grosserecfe21b2013-03-20 18:03:18 +000061}
62
Hongbin Zheng3b11a162012-04-25 13:16:49 +000063// Helper class to generate memory location.
64namespace {
65class IslGenerator {
66public:
Tobias Grosser7242ad92013-02-22 08:07:06 +000067 IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS)
68 : Builder(Builder), IVS(IVS) {}
Tobias Grosseredab1352013-06-21 06:41:31 +000069 Value *generateIslVal(__isl_take isl_val *Val);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000070 Value *generateIslAff(__isl_take isl_aff *Aff);
71 Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff);
72
73private:
74 typedef struct {
75 Value *Result;
76 class IslGenerator *Generator;
77 } IslGenInfo;
78
79 IRBuilder<> &Builder;
80 std::vector<Value *> &IVS;
Tobias Grosser1bb59b02012-12-29 23:47:38 +000081 static int mergeIslAffValues(__isl_take isl_set *Set, __isl_take isl_aff *Aff,
82 void *User);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000083};
84}
85
Tobias Grosseredab1352013-06-21 06:41:31 +000086Value *IslGenerator::generateIslVal(__isl_take isl_val *Val) {
87 Value *IntValue = Builder.getInt(APIntFromVal(Val));
Hongbin Zheng3b11a162012-04-25 13:16:49 +000088 return IntValue;
89}
90
91Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) {
92 Value *Result;
93 Value *ConstValue;
Tobias Grosseredab1352013-06-21 06:41:31 +000094 isl_val *Val;
Hongbin Zheng3b11a162012-04-25 13:16:49 +000095
Tobias Grosseredab1352013-06-21 06:41:31 +000096 Val = isl_aff_get_constant_val(Aff);
97 ConstValue = generateIslVal(Val);
Hongbin Zheng3b11a162012-04-25 13:16:49 +000098 Type *Ty = Builder.getInt64Ty();
99
100 // FIXME: We should give the constant and coefficients the right type. Here
101 // we force it into i64.
102 Result = Builder.CreateSExtOrBitCast(ConstValue, Ty);
103
104 unsigned int NbInputDims = isl_aff_dim(Aff, isl_dim_in);
105
Tobias Grosser7242ad92013-02-22 08:07:06 +0000106 assert((IVS.size() == NbInputDims) &&
107 "The Dimension of Induction Variables must match the dimension of the "
108 "affine space.");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000109
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000110 for (unsigned int i = 0; i < NbInputDims; ++i) {
111 Value *CoefficientValue;
Tobias Grosseredab1352013-06-21 06:41:31 +0000112 Val = isl_aff_get_coefficient_val(Aff, isl_dim_in, i);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000113
Tobias Grosseredab1352013-06-21 06:41:31 +0000114 if (isl_val_is_zero(Val)) {
115 isl_val_free(Val);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000116 continue;
Tobias Grosseredab1352013-06-21 06:41:31 +0000117 }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000118
Tobias Grosseredab1352013-06-21 06:41:31 +0000119 CoefficientValue = generateIslVal(Val);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000120 CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true);
121 Value *IV = Builder.CreateIntCast(IVS[i], Ty, true);
122 Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff");
123 Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff");
124 }
125
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000126 isl_aff_free(Aff);
127
128 return Result;
129}
130
131int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set,
132 __isl_take isl_aff *Aff, void *User) {
133 IslGenInfo *GenInfo = (IslGenInfo *)User;
134
Tobias Grosser7242ad92013-02-22 08:07:06 +0000135 assert((GenInfo->Result == NULL) &&
136 "Result is already set. Currently only single isl_aff is supported");
137 assert(isl_set_plain_is_universe(Set) &&
138 "Code generation failed because the set is not universe");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000139
140 GenInfo->Result = GenInfo->Generator->generateIslAff(Aff);
141
142 isl_set_free(Set);
143 return 0;
144}
145
146Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) {
147 IslGenInfo User;
148 User.Result = NULL;
149 User.Generator = this;
150 isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User);
151 assert(User.Result && "Code generation for isl_pw_aff failed");
152
153 isl_pw_aff_free(PwAff);
154 return User.Result;
155}
156
Tobias Grosser7242ad92013-02-22 08:07:06 +0000157BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P)
158 : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {
159}
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000160
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000161Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000162 ValueMapT &GlobalMap, LoopToScevMapT &LTS,
163 Loop *L) {
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() <
Tobias Grosserd7e58642013-04-10 06:55:45 +0000173 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)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000180 assert(BBMap[Old] && "BBMap[Old] should not be NULL!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000181 return BBMap[Old];
182 }
183
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000184 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000185 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
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 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000199 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000200
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000201 if (const Instruction *Inst = dyn_cast<Instruction>(Old)) {
Tobias Grosser58032cb2013-06-23 01:29:29 +0000202 (void)Inst;
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000203 assert(!Statement.getParent()->getRegion().contains(Inst->getParent()) &&
204 "unexpected scalar dependence in region");
205 }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000206
207 // Everything else is probably a scop-constant value defined as global,
208 // function parameter or an instruction not within the scop.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000209 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000210}
211
212void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000213 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000214 Instruction *NewInst = Inst->clone();
215
216 // Replace old operands with the new ones.
217 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000218 OE = Inst->op_end();
219 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000220 Value *OldOperand = *OI;
Tobias Grosser369430f2013-03-22 23:42:53 +0000221 Value *NewOperand =
222 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
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,
Tobias Grosser369430f2013-03-22 23:42:53 +0000243 ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
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);
Tobias Grosser369430f2013-03-22 23:42:53 +0000251 Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS, L);
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,
Tobias Grosser369430f2013-03-22 23:42:53 +0000271 ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
Tobias Grosser7242ad92013-02-22 08:07:06 +0000272 std::vector<Value *> IndexArray = getMemoryAccessIndex(
Tobias Grosser369430f2013-03-22 23:42:53 +0000273 NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS, L);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000274 Value *NewOperand =
275 Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000276 return NewOperand;
277}
278
Tobias Grossere602a072013-05-07 07:30:56 +0000279Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
280 const Value *Pointer,
281 ValueMapT &BBMap,
282 ValueMapT &GlobalMap,
283 LoopToScevMapT &LTS) {
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000284 const MemoryAccess &Access = Statement.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000285 isl_map *CurrentAccessRelation = Access.getAccessRelation();
286 isl_map *NewAccessRelation = Access.getNewAccessRelation();
287
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000288 assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) &&
289 "Current and new access function use different spaces");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000290
291 Value *NewPointer;
292
293 if (!NewAccessRelation) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000294 NewPointer =
295 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000296 } else {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000297 Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr());
Tobias Grosser7242ad92013-02-22 08:07:06 +0000298 NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000299 GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000300 }
301
302 isl_map_free(CurrentAccessRelation);
303 isl_map_free(NewAccessRelation);
304 return NewPointer;
305}
306
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000307Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000308 return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent());
309}
310
Tobias Grossere602a072013-05-07 07:30:56 +0000311Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
312 ValueMapT &BBMap,
313 ValueMapT &GlobalMap,
314 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000315 const Value *Pointer = Load->getPointerOperand();
316 const Instruction *Inst = dyn_cast<Instruction>(Load);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000317 Value *NewPointer =
318 generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000319 Value *ScalarLoad =
320 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000321 return ScalarLoad;
322}
323
Tobias Grossere602a072013-05-07 07:30:56 +0000324Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
325 ValueMapT &BBMap,
326 ValueMapT &GlobalMap,
327 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000328 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000329 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000330 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000331 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
332 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000333
334 return Builder.CreateStore(ValueOperand, NewPointer);
335}
336
Tobias Grossere602a072013-05-07 07:30:56 +0000337void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
338 ValueMapT &GlobalMap,
339 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000340 // Terminator instructions control the control flow. They are explicitly
341 // expressed in the clast and do not need to be copied.
342 if (Inst->isTerminator())
343 return;
344
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000345 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
346 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000347 return;
348
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000349 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000350 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000351 // Compute NewLoad before its insertion in BBMap to make the insertion
352 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000353 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000354 return;
355 }
356
357 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000358 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000359 // Compute NewStore before its insertion in BBMap to make the insertion
360 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000361 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000362 return;
363 }
364
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000365 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000366}
367
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000368void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000369 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000370 BasicBlock *CopyBB =
371 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000372 CopyBB->setName("polly.stmt." + BB->getName());
373 Builder.SetInsertPoint(CopyBB->begin());
374
375 ValueMapT BBMap;
376
377 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
378 ++II)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000379 copyInstruction(II, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000380}
381
Tobias Grossere602a072013-05-07 07:30:56 +0000382VectorBlockGenerator::VectorBlockGenerator(IRBuilder<> &B,
383 VectorValueMapT &GlobalMaps,
384 std::vector<LoopToScevMapT> &VLTS,
385 ScopStmt &Stmt,
386 __isl_keep isl_map *Schedule,
387 Pass *P)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000388 : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS),
389 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000390 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
391 assert(Schedule && "No statement domain provided");
392}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000393
Tobias Grossere602a072013-05-07 07:30:56 +0000394Value *VectorBlockGenerator::getVectorValue(const Value *Old,
395 ValueMapT &VectorMap,
396 VectorValueMapT &ScalarMaps,
397 Loop *L) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000398 if (VectorMap.count(Old))
399 return VectorMap[Old];
400
401 int Width = getVectorWidth();
402
403 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
404
405 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000406 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000407 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000408 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000409 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000410
411 VectorMap[Old] = Vector;
412
413 return Vector;
414}
415
416Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
417 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
418 assert(PointerTy && "PointerType expected");
419
420 Type *ScalarType = PointerTy->getElementType();
421 VectorType *VectorType = VectorType::get(ScalarType, Width);
422
423 return PointerType::getUnqual(VectorType);
424}
425
426Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
427 ValueMapT &BBMap) {
428 const Value *Pointer = Load->getPointerOperand();
429 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Tobias Grosser369430f2013-03-22 23:42:53 +0000430 Value *NewPointer =
431 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000432 Value *VectorPtr =
433 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
434 LoadInst *VecLoad =
435 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000436 if (!Aligned)
437 VecLoad->setAlignment(8);
438
439 return VecLoad;
440}
441
442Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
443 ValueMapT &BBMap) {
444 const Value *Pointer = Load->getPointerOperand();
445 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000446 Value *NewPointer =
447 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000448 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
449 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000450 LoadInst *ScalarLoad =
451 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000452
453 if (!Aligned)
454 ScalarLoad->setAlignment(8);
455
Tobias Grosserc14582f2013-02-05 18:01:29 +0000456 Constant *SplatVector = Constant::getNullValue(
457 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000458
Tobias Grosserc14582f2013-02-05 18:01:29 +0000459 Value *VectorLoad = Builder.CreateShuffleVector(
460 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000461 return VectorLoad;
462}
463
Tobias Grossere602a072013-05-07 07:30:56 +0000464Value *
465VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
466 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000467 int VectorWidth = getVectorWidth();
468 const Value *Pointer = Load->getPointerOperand();
469 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000470 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000471
472 Value *Vector = UndefValue::get(VectorType);
473
474 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000475 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
476 VLTS[i], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000477 Value *ScalarLoad =
478 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
479 Vector = Builder.CreateInsertElement(
480 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000481 }
482
483 return Vector;
484}
485
Tobias Grossere602a072013-05-07 07:30:56 +0000486void VectorBlockGenerator::generateLoad(const LoadInst *Load,
487 ValueMapT &VectorMap,
488 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000489 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
490 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000491 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000492 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000493 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000494 return;
495 }
496
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000497 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000498
499 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000500 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000501 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000502 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000503 NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
504 else
505 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
506
507 VectorMap[Load] = NewLoad;
508}
509
510void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
511 ValueMapT &VectorMap,
512 VectorValueMapT &ScalarMaps) {
513 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000514 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
515 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000516
517 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
518
519 const CastInst *Cast = dyn_cast<CastInst>(Inst);
520 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
521 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
522}
523
524void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
525 ValueMapT &VectorMap,
526 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000527 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000528 Value *OpZero = Inst->getOperand(0);
529 Value *OpOne = Inst->getOperand(1);
530
531 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000532 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
533 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000534
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000535 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000536 Inst->getName() + "p_vec");
537 VectorMap[Inst] = NewInst;
538}
539
Tobias Grossere602a072013-05-07 07:30:56 +0000540void VectorBlockGenerator::copyStore(const StoreInst *Store,
541 ValueMapT &VectorMap,
542 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000543 int VectorWidth = getVectorWidth();
544
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000545 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000546
547 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000548 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
549 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000550
Sebastian Popa00a0292012-12-18 07:46:06 +0000551 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000552 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
Tobias Grosser369430f2013-03-22 23:42:53 +0000553 Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0],
554 VLTS[0], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000555
Tobias Grosserc14582f2013-02-05 18:01:29 +0000556 Value *VectorPtr =
557 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000558 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
559
560 if (!Aligned)
561 Store->setAlignment(8);
562 } else {
563 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000564 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser369430f2013-03-22 23:42:53 +0000565 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
566 VLTS[i], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000567 Builder.CreateStore(Scalar, NewPointer);
568 }
569 }
570}
571
572bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
573 ValueMapT &VectorMap) {
574 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000575 OE = Inst->op_end();
576 OI != OE; ++OI)
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000577 if (VectorMap.count(*OI))
578 return true;
579 return false;
580}
581
582bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
583 ValueMapT &VectorMap,
584 VectorValueMapT &ScalarMaps) {
585 bool HasVectorOperand = false;
586 int VectorWidth = getVectorWidth();
587
588 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000589 OE = Inst->op_end();
590 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000591 ValueMapT::iterator VecOp = VectorMap.find(*OI);
592
593 if (VecOp == VectorMap.end())
594 continue;
595
596 HasVectorOperand = true;
597 Value *NewVector = VecOp->second;
598
599 for (int i = 0; i < VectorWidth; ++i) {
600 ValueMapT &SM = ScalarMaps[i];
601
602 // If there is one scalar extracted, all scalar elements should have
603 // already been extracted by the code here. So no need to check for the
604 // existance of all of them.
605 if (SM.count(*OI))
606 break;
607
608 SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
609 }
610 }
611
612 return HasVectorOperand;
613}
614
615void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
616 ValueMapT &VectorMap,
617 VectorValueMapT &ScalarMaps) {
618 bool HasVectorOperand;
619 int VectorWidth = getVectorWidth();
620
621 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
622
623 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000624 copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
625 VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000626
627 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
628 return;
629
630 // Make the result available as vector value.
631 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
632 Value *Vector = UndefValue::get(VectorType);
633
634 for (int i = 0; i < VectorWidth; i++)
635 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
636 Builder.getInt32(i));
637
638 VectorMap[Inst] = Vector;
639}
640
Tobias Grosserc14582f2013-02-05 18:01:29 +0000641int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000642
643void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
644 ValueMapT &VectorMap,
645 VectorValueMapT &ScalarMaps) {
646 // Terminator instructions control the control flow. They are explicitly
647 // expressed in the clast and do not need to be copied.
648 if (Inst->isTerminator())
649 return;
650
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000651 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
652 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000653 return;
654
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000655 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
656 generateLoad(Load, VectorMap, ScalarMaps);
657 return;
658 }
659
660 if (hasVectorOperands(Inst, VectorMap)) {
661 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
662 copyStore(Store, VectorMap, ScalarMaps);
663 return;
664 }
665
666 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
667 copyUnaryInst(Unary, VectorMap, ScalarMaps);
668 return;
669 }
670
671 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
672 copyBinaryInst(Binary, VectorMap, ScalarMaps);
673 return;
674 }
675
676 // Falltrough: We generate scalar instructions, if we don't know how to
677 // generate vector code.
678 }
679
680 copyInstScalarized(Inst, VectorMap, ScalarMaps);
681}
682
683void VectorBlockGenerator::copyBB() {
684 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000685 BasicBlock *CopyBB =
686 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000687 CopyBB->setName("polly.stmt." + BB->getName());
688 Builder.SetInsertPoint(CopyBB->begin());
689
690 // Create two maps that store the mapping from the original instructions of
691 // the old basic block to their copies in the new basic block. Those maps
692 // are basic block local.
693 //
694 // As vector code generation is supported there is one map for scalar values
695 // and one for vector values.
696 //
697 // In case we just do scalar code generation, the vectorMap is not used and
698 // the scalarMap has just one dimension, which contains the mapping.
699 //
700 // In case vector code generation is done, an instruction may either appear
701 // in the vector map once (as it is calculating >vectorwidth< values at a
702 // time. Or (if the values are calculated using scalar operations), it
703 // appears once in every dimension of the scalarMap.
704 VectorValueMapT ScalarBlockMap(getVectorWidth());
705 ValueMapT VectorBlockMap;
706
Tobias Grosserc14582f2013-02-05 18:01:29 +0000707 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
708 ++II)
709 copyInstruction(II, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000710}