blob: 6eb0a81d3adb7e802605e4ab540453968a53682b [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
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000169 if (Value *New = GlobalMap.lookup(Old)) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000170 if (Old->getType()->getScalarSizeInBits() <
Tobias Grosserd7e58642013-04-10 06:55:45 +0000171 New->getType()->getScalarSizeInBits())
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000172 New = Builder.CreateTruncOrBitCast(New, Old->getType());
173
174 return New;
175 }
176
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000177 if (Value *New = BBMap.lookup(Old))
178 return New;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000179
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000180 if (SCEVCodegen && SE.isSCEVable(Old->getType()))
Tobias Grosser369430f2013-03-22 23:42:53 +0000181 if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000182 if (!isa<SCEVCouldNotCompute>(Scev)) {
Sebastian Pop637b23d2013-02-15 20:56:01 +0000183 const SCEV *NewScev = apply(Scev, LTS, SE);
184 ValueToValueMap VTV;
185 VTV.insert(BBMap.begin(), BBMap.end());
186 VTV.insert(GlobalMap.begin(), GlobalMap.end());
Sebastian Pop47d4ee32013-02-15 21:26:53 +0000187 NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000188 SCEVExpander Expander(SE, "polly");
189 Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
190 Builder.GetInsertPoint());
191
192 BBMap[Old] = Expanded;
193 return Expanded;
194 }
Tobias Grosser369430f2013-03-22 23:42:53 +0000195 }
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000196
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000197 if (const Instruction *Inst = dyn_cast<Instruction>(Old)) {
Tobias Grosser58032cb2013-06-23 01:29:29 +0000198 (void)Inst;
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000199 assert(!Statement.getParent()->getRegion().contains(Inst->getParent()) &&
200 "unexpected scalar dependence in region");
201 }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000202
203 // Everything else is probably a scop-constant value defined as global,
204 // function parameter or an instruction not within the scop.
Tobias Grosserc14582f2013-02-05 18:01:29 +0000205 return const_cast<Value *>(Old);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000206}
207
208void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000209 ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000210 Instruction *NewInst = Inst->clone();
211
212 // Replace old operands with the new ones.
213 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000214 OE = Inst->op_end();
215 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000216 Value *OldOperand = *OI;
Tobias Grosser369430f2013-03-22 23:42:53 +0000217 Value *NewOperand =
218 getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000219
220 if (!NewOperand) {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000221 assert(!isa<StoreInst>(NewInst) &&
222 "Store instructions are always needed!");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000223 delete NewInst;
224 return;
225 }
226
227 NewInst->replaceUsesOfWith(OldOperand, NewOperand);
228 }
229
230 Builder.Insert(NewInst);
231 BBMap[Inst] = NewInst;
232
233 if (!NewInst->getType()->isVoidTy())
234 NewInst->setName("p_" + Inst->getName());
235}
236
Tobias Grosserc14582f2013-02-05 18:01:29 +0000237std::vector<Value *> BlockGenerator::getMemoryAccessIndex(
238 __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000239 ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000240
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000241 assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) &&
242 "Only single dimensional access functions supported");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000243
244 std::vector<Value *> IVS;
245 for (unsigned i = 0; i < Statement.getNumIterators(); ++i) {
246 const Value *OriginalIV = Statement.getInductionVariableForDimension(i);
Tobias Grosser369430f2013-03-22 23:42:53 +0000247 Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000248 IVS.push_back(NewIV);
249 }
250
251 isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0);
252 IslGenerator IslGen(Builder, IVS);
253 Value *OffsetValue = IslGen.generateIslPwAff(PwAff);
254
255 Type *Ty = Builder.getInt64Ty();
256 OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true);
257
Tobias Grosserc14582f2013-02-05 18:01:29 +0000258 std::vector<Value *> IndexArray;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000259 Value *NullValue = Constant::getNullValue(Ty);
260 IndexArray.push_back(NullValue);
261 IndexArray.push_back(OffsetValue);
262 return IndexArray;
263}
264
265Value *BlockGenerator::getNewAccessOperand(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000266 __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000267 ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
Tobias Grosser7242ad92013-02-22 08:07:06 +0000268 std::vector<Value *> IndexArray = getMemoryAccessIndex(
Tobias Grosser369430f2013-03-22 23:42:53 +0000269 NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS, L);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000270 Value *NewOperand =
271 Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000272 return NewOperand;
273}
274
Tobias Grossere602a072013-05-07 07:30:56 +0000275Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
276 const Value *Pointer,
277 ValueMapT &BBMap,
278 ValueMapT &GlobalMap,
279 LoopToScevMapT &LTS) {
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000280 const MemoryAccess &Access = Statement.getAccessFor(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000281 isl_map *CurrentAccessRelation = Access.getAccessRelation();
282 isl_map *NewAccessRelation = Access.getNewAccessRelation();
283
Tobias Grosserae2d83e2012-12-29 23:57:18 +0000284 assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) &&
285 "Current and new access function use different spaces");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000286
287 Value *NewPointer;
288
289 if (!NewAccessRelation) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000290 NewPointer =
291 getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000292 } else {
Tobias Grosserc14582f2013-02-05 18:01:29 +0000293 Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr());
Tobias Grosser7242ad92013-02-22 08:07:06 +0000294 NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap,
Tobias Grosser369430f2013-03-22 23:42:53 +0000295 GlobalMap, LTS, getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000296 }
297
298 isl_map_free(CurrentAccessRelation);
299 isl_map_free(NewAccessRelation);
300 return NewPointer;
301}
302
Tobias Grosser4d96c8d2013-03-23 01:05:07 +0000303Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000304 return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent());
305}
306
Tobias Grossere602a072013-05-07 07:30:56 +0000307Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
308 ValueMapT &BBMap,
309 ValueMapT &GlobalMap,
310 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000311 const Value *Pointer = Load->getPointerOperand();
312 const Instruction *Inst = dyn_cast<Instruction>(Load);
Tobias Grosser7242ad92013-02-22 08:07:06 +0000313 Value *NewPointer =
314 generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosserc14582f2013-02-05 18:01:29 +0000315 Value *ScalarLoad =
316 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000317 return ScalarLoad;
318}
319
Tobias Grossere602a072013-05-07 07:30:56 +0000320Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
321 ValueMapT &BBMap,
322 ValueMapT &GlobalMap,
323 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000324 const Value *Pointer = Store->getPointerOperand();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000325 Value *NewPointer =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000326 generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
Tobias Grosser369430f2013-03-22 23:42:53 +0000327 Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
328 LTS, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000329
330 return Builder.CreateStore(ValueOperand, NewPointer);
331}
332
Tobias Grossere602a072013-05-07 07:30:56 +0000333void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
334 ValueMapT &GlobalMap,
335 LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000336 // Terminator instructions control the control flow. They are explicitly
337 // expressed in the clast and do not need to be copied.
338 if (Inst->isTerminator())
339 return;
340
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000341 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
342 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000343 return;
344
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000345 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000346 Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000347 // Compute NewLoad before its insertion in BBMap to make the insertion
348 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000349 BBMap[Load] = NewLoad;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000350 return;
351 }
352
353 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
Sebastian Pop753d43f2013-05-24 17:16:02 +0000354 Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
Sebastian Pop3d94fed2013-05-24 18:46:02 +0000355 // Compute NewStore before its insertion in BBMap to make the insertion
356 // deterministic.
Sebastian Pop753d43f2013-05-24 17:16:02 +0000357 BBMap[Store] = NewStore;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000358 return;
359 }
360
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000361 copyInstScalar(Inst, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000362}
363
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000364void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000365 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000366 BasicBlock *CopyBB =
367 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000368 CopyBB->setName("polly.stmt." + BB->getName());
369 Builder.SetInsertPoint(CopyBB->begin());
370
371 ValueMapT BBMap;
372
373 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
374 ++II)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000375 copyInstruction(II, BBMap, GlobalMap, LTS);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000376}
377
Tobias Grossere602a072013-05-07 07:30:56 +0000378VectorBlockGenerator::VectorBlockGenerator(IRBuilder<> &B,
379 VectorValueMapT &GlobalMaps,
380 std::vector<LoopToScevMapT> &VLTS,
381 ScopStmt &Stmt,
382 __isl_keep isl_map *Schedule,
383 Pass *P)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000384 : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS),
385 Schedule(Schedule) {
Sebastian Popa00a0292012-12-18 07:46:06 +0000386 assert(GlobalMaps.size() > 1 && "Only one vector lane found");
387 assert(Schedule && "No statement domain provided");
388}
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000389
Tobias Grossere602a072013-05-07 07:30:56 +0000390Value *VectorBlockGenerator::getVectorValue(const Value *Old,
391 ValueMapT &VectorMap,
392 VectorValueMapT &ScalarMaps,
393 Loop *L) {
Hongbin Zhengfe11e282013-06-29 13:22:15 +0000394 if (Value *NewValue = VectorMap.lookup(Old))
395 return NewValue;
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000396
397 int Width = getVectorWidth();
398
399 Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
400
401 for (int Lane = 0; Lane < Width; Lane++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000402 Vector = Builder.CreateInsertElement(
Tobias Grosser7242ad92013-02-22 08:07:06 +0000403 Vector,
Tobias Grosser369430f2013-03-22 23:42:53 +0000404 getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
Tobias Grosser7242ad92013-02-22 08:07:06 +0000405 Builder.getInt32(Lane));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000406
407 VectorMap[Old] = Vector;
408
409 return Vector;
410}
411
412Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
413 PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
414 assert(PointerTy && "PointerType expected");
415
416 Type *ScalarType = PointerTy->getElementType();
417 VectorType *VectorType = VectorType::get(ScalarType, Width);
418
419 return PointerType::getUnqual(VectorType);
420}
421
422Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
423 ValueMapT &BBMap) {
424 const Value *Pointer = Load->getPointerOperand();
425 Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
Tobias Grosser369430f2013-03-22 23:42:53 +0000426 Value *NewPointer =
427 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000428 Value *VectorPtr =
429 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
430 LoadInst *VecLoad =
431 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000432 if (!Aligned)
433 VecLoad->setAlignment(8);
434
435 return VecLoad;
436}
437
438Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
439 ValueMapT &BBMap) {
440 const Value *Pointer = Load->getPointerOperand();
441 Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
Tobias Grosser369430f2013-03-22 23:42:53 +0000442 Value *NewPointer =
443 getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000444 Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
445 Load->getName() + "_p_vec_p");
Tobias Grosserc14582f2013-02-05 18:01:29 +0000446 LoadInst *ScalarLoad =
447 Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000448
449 if (!Aligned)
450 ScalarLoad->setAlignment(8);
451
Tobias Grosserc14582f2013-02-05 18:01:29 +0000452 Constant *SplatVector = Constant::getNullValue(
453 VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000454
Tobias Grosserc14582f2013-02-05 18:01:29 +0000455 Value *VectorLoad = Builder.CreateShuffleVector(
456 ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000457 return VectorLoad;
458}
459
Tobias Grossere602a072013-05-07 07:30:56 +0000460Value *
461VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
462 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000463 int VectorWidth = getVectorWidth();
464 const Value *Pointer = Load->getPointerOperand();
465 VectorType *VectorType = VectorType::get(
Tobias Grosserc14582f2013-02-05 18:01:29 +0000466 dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000467
468 Value *Vector = UndefValue::get(VectorType);
469
470 for (int i = 0; i < VectorWidth; i++) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000471 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
472 VLTS[i], getLoopForInst(Load));
Tobias Grosserc14582f2013-02-05 18:01:29 +0000473 Value *ScalarLoad =
474 Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
475 Vector = Builder.CreateInsertElement(
476 Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000477 }
478
479 return Vector;
480}
481
Tobias Grossere602a072013-05-07 07:30:56 +0000482void VectorBlockGenerator::generateLoad(const LoadInst *Load,
483 ValueMapT &VectorMap,
484 VectorValueMapT &ScalarMaps) {
Hongbin Zheng68794212012-05-06 10:22:19 +0000485 if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
486 !VectorType::isValidElementType(Load->getType())) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000487 for (int i = 0; i < getVectorWidth(); i++)
Tobias Grosserc14582f2013-02-05 18:01:29 +0000488 ScalarMaps[i][Load] =
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000489 generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000490 return;
491 }
492
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000493 const MemoryAccess &Access = Statement.getAccessFor(Load);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000494
495 Value *NewLoad;
Sebastian Popa00a0292012-12-18 07:46:06 +0000496 if (Access.isStrideZero(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000497 NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
Sebastian Popa00a0292012-12-18 07:46:06 +0000498 else if (Access.isStrideOne(isl_map_copy(Schedule)))
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000499 NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
500 else
501 NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
502
503 VectorMap[Load] = NewLoad;
504}
505
506void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
507 ValueMapT &VectorMap,
508 VectorValueMapT &ScalarMaps) {
509 int VectorWidth = getVectorWidth();
Tobias Grosser369430f2013-03-22 23:42:53 +0000510 Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
511 getLoopForInst(Inst));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000512
513 assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
514
515 const CastInst *Cast = dyn_cast<CastInst>(Inst);
516 VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
517 VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
518}
519
520void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
521 ValueMapT &VectorMap,
522 VectorValueMapT &ScalarMaps) {
Tobias Grosser369430f2013-03-22 23:42:53 +0000523 Loop *L = getLoopForInst(Inst);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000524 Value *OpZero = Inst->getOperand(0);
525 Value *OpOne = Inst->getOperand(1);
526
527 Value *NewOpZero, *NewOpOne;
Tobias Grosser369430f2013-03-22 23:42:53 +0000528 NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
529 NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000530
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000531 Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000532 Inst->getName() + "p_vec");
533 VectorMap[Inst] = NewInst;
534}
535
Tobias Grossere602a072013-05-07 07:30:56 +0000536void VectorBlockGenerator::copyStore(const StoreInst *Store,
537 ValueMapT &VectorMap,
538 VectorValueMapT &ScalarMaps) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000539 int VectorWidth = getVectorWidth();
540
Hongbin Zhengb5f24a62013-06-29 06:31:39 +0000541 const MemoryAccess &Access = Statement.getAccessFor(Store);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000542
543 const Value *Pointer = Store->getPointerOperand();
Tobias Grosser369430f2013-03-22 23:42:53 +0000544 Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
545 ScalarMaps, getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000546
Sebastian Popa00a0292012-12-18 07:46:06 +0000547 if (Access.isStrideOne(isl_map_copy(Schedule))) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000548 Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
Tobias Grosser369430f2013-03-22 23:42:53 +0000549 Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0],
550 VLTS[0], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000551
Tobias Grosserc14582f2013-02-05 18:01:29 +0000552 Value *VectorPtr =
553 Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000554 StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
555
556 if (!Aligned)
557 Store->setAlignment(8);
558 } else {
559 for (unsigned i = 0; i < ScalarMaps.size(); i++) {
Tobias Grosser1bb59b02012-12-29 23:47:38 +0000560 Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
Tobias Grosser369430f2013-03-22 23:42:53 +0000561 Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
562 VLTS[i], getLoopForInst(Store));
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000563 Builder.CreateStore(Scalar, NewPointer);
564 }
565 }
566}
567
568bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
569 ValueMapT &VectorMap) {
570 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000571 OE = Inst->op_end();
572 OI != OE; ++OI)
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000573 if (VectorMap.count(*OI))
574 return true;
575 return false;
576}
577
578bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
579 ValueMapT &VectorMap,
580 VectorValueMapT &ScalarMaps) {
581 bool HasVectorOperand = false;
582 int VectorWidth = getVectorWidth();
583
584 for (Instruction::const_op_iterator OI = Inst->op_begin(),
Tobias Grosserc14582f2013-02-05 18:01:29 +0000585 OE = Inst->op_end();
586 OI != OE; ++OI) {
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000587 ValueMapT::iterator VecOp = VectorMap.find(*OI);
588
589 if (VecOp == VectorMap.end())
590 continue;
591
592 HasVectorOperand = true;
593 Value *NewVector = VecOp->second;
594
595 for (int i = 0; i < VectorWidth; ++i) {
596 ValueMapT &SM = ScalarMaps[i];
597
598 // If there is one scalar extracted, all scalar elements should have
599 // already been extracted by the code here. So no need to check for the
600 // existance of all of them.
601 if (SM.count(*OI))
602 break;
603
604 SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
605 }
606 }
607
608 return HasVectorOperand;
609}
610
611void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
612 ValueMapT &VectorMap,
613 VectorValueMapT &ScalarMaps) {
614 bool HasVectorOperand;
615 int VectorWidth = getVectorWidth();
616
617 HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
618
619 for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
Sebastian Pop9d10fff2013-02-15 20:55:59 +0000620 copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
621 VLTS[VectorLane]);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000622
623 if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
624 return;
625
626 // Make the result available as vector value.
627 VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
628 Value *Vector = UndefValue::get(VectorType);
629
630 for (int i = 0; i < VectorWidth; i++)
631 Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
632 Builder.getInt32(i));
633
634 VectorMap[Inst] = Vector;
635}
636
Tobias Grosserc14582f2013-02-05 18:01:29 +0000637int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000638
639void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
640 ValueMapT &VectorMap,
641 VectorValueMapT &ScalarMaps) {
642 // Terminator instructions control the control flow. They are explicitly
643 // expressed in the clast and do not need to be copied.
644 if (Inst->isTerminator())
645 return;
646
Tobias Grosserecfe21b2013-03-20 18:03:18 +0000647 if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
648 &Statement.getParent()->getRegion()))
Tobias Grossere71c6ab2012-04-27 16:36:14 +0000649 return;
650
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000651 if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
652 generateLoad(Load, VectorMap, ScalarMaps);
653 return;
654 }
655
656 if (hasVectorOperands(Inst, VectorMap)) {
657 if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
658 copyStore(Store, VectorMap, ScalarMaps);
659 return;
660 }
661
662 if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
663 copyUnaryInst(Unary, VectorMap, ScalarMaps);
664 return;
665 }
666
667 if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
668 copyBinaryInst(Binary, VectorMap, ScalarMaps);
669 return;
670 }
671
672 // Falltrough: We generate scalar instructions, if we don't know how to
673 // generate vector code.
674 }
675
676 copyInstScalarized(Inst, VectorMap, ScalarMaps);
677}
678
679void VectorBlockGenerator::copyBB() {
680 BasicBlock *BB = Statement.getBasicBlock();
Tobias Grosserc14582f2013-02-05 18:01:29 +0000681 BasicBlock *CopyBB =
682 SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000683 CopyBB->setName("polly.stmt." + BB->getName());
684 Builder.SetInsertPoint(CopyBB->begin());
685
686 // Create two maps that store the mapping from the original instructions of
687 // the old basic block to their copies in the new basic block. Those maps
688 // are basic block local.
689 //
690 // As vector code generation is supported there is one map for scalar values
691 // and one for vector values.
692 //
693 // In case we just do scalar code generation, the vectorMap is not used and
694 // the scalarMap has just one dimension, which contains the mapping.
695 //
696 // In case vector code generation is done, an instruction may either appear
697 // in the vector map once (as it is calculating >vectorwidth< values at a
698 // time. Or (if the values are calculated using scalar operations), it
699 // appears once in every dimension of the scalarMap.
700 VectorValueMapT ScalarBlockMap(getVectorWidth());
701 ValueMapT VectorBlockMap;
702
Tobias Grosserc14582f2013-02-05 18:01:29 +0000703 for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
704 ++II)
705 copyInstruction(II, VectorBlockMap, ScalarBlockMap);
Hongbin Zheng3b11a162012-04-25 13:16:49 +0000706}