13b11a16aSHongbin Zheng //===--- BlockGenerators.cpp - Generate code for statements -----*- C++ -*-===//
23b11a16aSHongbin Zheng //
33b11a16aSHongbin Zheng //                     The LLVM Compiler Infrastructure
43b11a16aSHongbin Zheng //
53b11a16aSHongbin Zheng // This file is distributed under the University of Illinois Open Source
63b11a16aSHongbin Zheng // License. See LICENSE.TXT for details.
73b11a16aSHongbin Zheng //
83b11a16aSHongbin Zheng //===----------------------------------------------------------------------===//
93b11a16aSHongbin Zheng //
103b11a16aSHongbin Zheng // This file implements the BlockGenerator and VectorBlockGenerator classes,
113b11a16aSHongbin Zheng // which generate sequential code and vectorized code for a polyhedral
123b11a16aSHongbin Zheng // statement, respectively.
133b11a16aSHongbin Zheng //
143b11a16aSHongbin Zheng //===----------------------------------------------------------------------===//
153b11a16aSHongbin Zheng 
163b11a16aSHongbin Zheng #include "polly/ScopInfo.h"
1783628182STobias Grosser #include "isl/aff.h"
1883628182STobias Grosser #include "isl/set.h"
198a846610SHongbin Zheng #include "polly/CodeGen/BlockGenerators.h"
2083628182STobias Grosser #include "polly/CodeGen/CodeGeneration.h"
21637bd631STobias Grosser #include "polly/Options.h"
223b11a16aSHongbin Zheng #include "polly/Support/GICHelper.h"
2397cb813cSSebastian Pop #include "polly/Support/SCEVValidator.h"
24ecfe21b7STobias Grosser #include "polly/Support/ScopHelper.h"
25e71c6ab5STobias Grosser #include "llvm/Analysis/LoopInfo.h"
26e71c6ab5STobias Grosser #include "llvm/Analysis/ScalarEvolution.h"
27e71c6ab5STobias Grosser #include "llvm/Analysis/ScalarEvolutionExpander.h"
283b11a16aSHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h"
293b11a16aSHongbin Zheng 
303b11a16aSHongbin Zheng using namespace llvm;
313b11a16aSHongbin Zheng using namespace polly;
323b11a16aSHongbin Zheng 
333b11a16aSHongbin Zheng static cl::opt<bool>
34c14582f2STobias Grosser Aligned("enable-polly-aligned", cl::desc("Assumed aligned memory accesses."),
35c14582f2STobias Grosser         cl::Hidden, cl::value_desc("OpenMP code generation enabled if true"),
36637bd631STobias Grosser         cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
373b11a16aSHongbin Zheng 
38e602a076STobias Grosser static cl::opt<bool, true>
39e602a076STobias Grosser SCEVCodegenF("polly-codegen-scev", cl::desc("Use SCEV based code generation."),
40e602a076STobias Grosser              cl::Hidden, cl::location(SCEVCodegen), cl::init(false),
41637bd631STobias Grosser              cl::ZeroOrMore, cl::cat(PollyCategory));
42ecfe21b7STobias Grosser 
43ecfe21b7STobias Grosser bool polly::SCEVCodegen;
44ecfe21b7STobias Grosser 
45ecfe21b7STobias Grosser bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
46ecfe21b7STobias Grosser                           ScalarEvolution *SE, const Region *R) {
47ecfe21b7STobias Grosser   if (SCEVCodegen) {
48ecfe21b7STobias Grosser     if (!I || !SE->isSCEVable(I->getType()))
49ecfe21b7STobias Grosser       return false;
50ecfe21b7STobias Grosser 
51ecfe21b7STobias Grosser     if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
52ecfe21b7STobias Grosser       if (!isa<SCEVCouldNotCompute>(Scev))
53ecfe21b7STobias Grosser         if (!hasScalarDepsInsideRegion(Scev, R))
54ecfe21b7STobias Grosser           return true;
55ecfe21b7STobias Grosser 
56ecfe21b7STobias Grosser     return false;
57ecfe21b7STobias Grosser   }
58ecfe21b7STobias Grosser 
59ecfe21b7STobias Grosser   Loop *L = LI->getLoopFor(I->getParent());
60e8df5bd9STobias Grosser   return L && I == L->getCanonicalInductionVariable() && R->contains(L);
61ecfe21b7STobias Grosser }
62ecfe21b7STobias Grosser 
633b11a16aSHongbin Zheng // Helper class to generate memory location.
643b11a16aSHongbin Zheng namespace {
653b11a16aSHongbin Zheng class IslGenerator {
663b11a16aSHongbin Zheng public:
677242ad92STobias Grosser   IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS)
687242ad92STobias Grosser       : Builder(Builder), IVS(IVS) {}
69edab1359STobias Grosser   Value *generateIslVal(__isl_take isl_val *Val);
703b11a16aSHongbin Zheng   Value *generateIslAff(__isl_take isl_aff *Aff);
713b11a16aSHongbin Zheng   Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff);
723b11a16aSHongbin Zheng 
733b11a16aSHongbin Zheng private:
743b11a16aSHongbin Zheng   typedef struct {
753b11a16aSHongbin Zheng     Value *Result;
763b11a16aSHongbin Zheng     class IslGenerator *Generator;
773b11a16aSHongbin Zheng   } IslGenInfo;
783b11a16aSHongbin Zheng 
793b11a16aSHongbin Zheng   IRBuilder<> &Builder;
803b11a16aSHongbin Zheng   std::vector<Value *> &IVS;
811bb59b0dSTobias Grosser   static int mergeIslAffValues(__isl_take isl_set *Set, __isl_take isl_aff *Aff,
821bb59b0dSTobias Grosser                                void *User);
833b11a16aSHongbin Zheng };
843b11a16aSHongbin Zheng }
853b11a16aSHongbin Zheng 
86edab1359STobias Grosser Value *IslGenerator::generateIslVal(__isl_take isl_val *Val) {
87edab1359STobias Grosser   Value *IntValue = Builder.getInt(APIntFromVal(Val));
883b11a16aSHongbin Zheng   return IntValue;
893b11a16aSHongbin Zheng }
903b11a16aSHongbin Zheng 
913b11a16aSHongbin Zheng Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) {
923b11a16aSHongbin Zheng   Value *Result;
933b11a16aSHongbin Zheng   Value *ConstValue;
94edab1359STobias Grosser   isl_val *Val;
953b11a16aSHongbin Zheng 
96edab1359STobias Grosser   Val = isl_aff_get_constant_val(Aff);
97edab1359STobias Grosser   ConstValue = generateIslVal(Val);
983b11a16aSHongbin Zheng   Type *Ty = Builder.getInt64Ty();
993b11a16aSHongbin Zheng 
1003b11a16aSHongbin Zheng   // FIXME: We should give the constant and coefficients the right type. Here
1013b11a16aSHongbin Zheng   // we force it into i64.
1023b11a16aSHongbin Zheng   Result = Builder.CreateSExtOrBitCast(ConstValue, Ty);
1033b11a16aSHongbin Zheng 
1043b11a16aSHongbin Zheng   unsigned int NbInputDims = isl_aff_dim(Aff, isl_dim_in);
1053b11a16aSHongbin Zheng 
1067242ad92STobias Grosser   assert((IVS.size() == NbInputDims) &&
1077242ad92STobias Grosser          "The Dimension of Induction Variables must match the dimension of the "
1087242ad92STobias Grosser          "affine space.");
1093b11a16aSHongbin Zheng 
1103b11a16aSHongbin Zheng   for (unsigned int i = 0; i < NbInputDims; ++i) {
1113b11a16aSHongbin Zheng     Value *CoefficientValue;
112edab1359STobias Grosser     Val = isl_aff_get_coefficient_val(Aff, isl_dim_in, i);
1133b11a16aSHongbin Zheng 
114edab1359STobias Grosser     if (isl_val_is_zero(Val)) {
115edab1359STobias Grosser       isl_val_free(Val);
1163b11a16aSHongbin Zheng       continue;
117edab1359STobias Grosser     }
1183b11a16aSHongbin Zheng 
119edab1359STobias Grosser     CoefficientValue = generateIslVal(Val);
1203b11a16aSHongbin Zheng     CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true);
1213b11a16aSHongbin Zheng     Value *IV = Builder.CreateIntCast(IVS[i], Ty, true);
1223b11a16aSHongbin Zheng     Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff");
1233b11a16aSHongbin Zheng     Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff");
1243b11a16aSHongbin Zheng   }
1253b11a16aSHongbin Zheng 
1263b11a16aSHongbin Zheng   isl_aff_free(Aff);
1273b11a16aSHongbin Zheng 
1283b11a16aSHongbin Zheng   return Result;
1293b11a16aSHongbin Zheng }
1303b11a16aSHongbin Zheng 
1313b11a16aSHongbin Zheng int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set,
1323b11a16aSHongbin Zheng                                     __isl_take isl_aff *Aff, void *User) {
1333b11a16aSHongbin Zheng   IslGenInfo *GenInfo = (IslGenInfo *)User;
1343b11a16aSHongbin Zheng 
1357242ad92STobias Grosser   assert((GenInfo->Result == NULL) &&
1367242ad92STobias Grosser          "Result is already set. Currently only single isl_aff is supported");
1377242ad92STobias Grosser   assert(isl_set_plain_is_universe(Set) &&
1387242ad92STobias Grosser          "Code generation failed because the set is not universe");
1393b11a16aSHongbin Zheng 
1403b11a16aSHongbin Zheng   GenInfo->Result = GenInfo->Generator->generateIslAff(Aff);
1413b11a16aSHongbin Zheng 
1423b11a16aSHongbin Zheng   isl_set_free(Set);
1433b11a16aSHongbin Zheng   return 0;
1443b11a16aSHongbin Zheng }
1453b11a16aSHongbin Zheng 
1463b11a16aSHongbin Zheng Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) {
1473b11a16aSHongbin Zheng   IslGenInfo User;
1483b11a16aSHongbin Zheng   User.Result = NULL;
1493b11a16aSHongbin Zheng   User.Generator = this;
1503b11a16aSHongbin Zheng   isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User);
1513b11a16aSHongbin Zheng   assert(User.Result && "Code generation for isl_pw_aff failed");
1523b11a16aSHongbin Zheng 
1533b11a16aSHongbin Zheng   isl_pw_aff_free(PwAff);
1543b11a16aSHongbin Zheng   return User.Result;
1553b11a16aSHongbin Zheng }
1563b11a16aSHongbin Zheng 
1577242ad92STobias Grosser BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P)
1587242ad92STobias Grosser     : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {
1597242ad92STobias Grosser }
160e71c6ab5STobias Grosser 
161*5b463ceaSHongbin Zheng Value *BlockGenerator::lookupAvailableValue(const Value *Old, ValueMapT &BBMap,
162*5b463ceaSHongbin Zheng                                             ValueMapT &GlobalMap) const {
1633b11a16aSHongbin Zheng   // We assume constants never change.
1643b11a16aSHongbin Zheng   // This avoids map lookups for many calls to this function.
1653b11a16aSHongbin Zheng   if (isa<Constant>(Old))
1663b11a16aSHongbin Zheng     return const_cast<Value *>(Old);
1673b11a16aSHongbin Zheng 
168fe11e287SHongbin Zheng   if (Value *New = GlobalMap.lookup(Old)) {
169c14582f2STobias Grosser     if (Old->getType()->getScalarSizeInBits() <
170c14582f2STobias Grosser         New->getType()->getScalarSizeInBits())
1713b11a16aSHongbin Zheng       New = Builder.CreateTruncOrBitCast(New, Old->getType());
1723b11a16aSHongbin Zheng 
1733b11a16aSHongbin Zheng     return New;
1743b11a16aSHongbin Zheng   }
1753b11a16aSHongbin Zheng 
176*5b463ceaSHongbin Zheng   // Or it is probably a scop-constant value defined as global, function
177*5b463ceaSHongbin Zheng   // parameter or an instruction not within the scop.
178*5b463ceaSHongbin Zheng   if (isa<GlobalValue>(Old) || isa<Argument>(Old))
179*5b463ceaSHongbin Zheng     return const_cast<Value *>(Old);
180*5b463ceaSHongbin Zheng 
181*5b463ceaSHongbin Zheng   if (const Instruction *Inst = dyn_cast<Instruction>(Old))
182*5b463ceaSHongbin Zheng     if (!Statement.getParent()->getRegion().contains(Inst->getParent()))
183*5b463ceaSHongbin Zheng       return const_cast<Value *>(Old);
184*5b463ceaSHongbin Zheng 
185fe11e287SHongbin Zheng   if (Value *New = BBMap.lookup(Old))
186fe11e287SHongbin Zheng     return New;
1873b11a16aSHongbin Zheng 
188*5b463ceaSHongbin Zheng   return NULL;
189*5b463ceaSHongbin Zheng }
190*5b463ceaSHongbin Zheng 
191*5b463ceaSHongbin Zheng Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
192*5b463ceaSHongbin Zheng                                    ValueMapT &GlobalMap, LoopToScevMapT &LTS,
193*5b463ceaSHongbin Zheng                                    Loop *L) {
194*5b463ceaSHongbin Zheng   if (Value *New = lookupAvailableValue(Old, BBMap, GlobalMap))
195*5b463ceaSHongbin Zheng     return New;
196*5b463ceaSHongbin Zheng 
197e71c6ab5STobias Grosser   if (SCEVCodegen && SE.isSCEVable(Old->getType()))
198369430ffSTobias Grosser     if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
199e71c6ab5STobias Grosser       if (!isa<SCEVCouldNotCompute>(Scev)) {
200637b23dcSSebastian Pop         const SCEV *NewScev = apply(Scev, LTS, SE);
201637b23dcSSebastian Pop         ValueToValueMap VTV;
202637b23dcSSebastian Pop         VTV.insert(BBMap.begin(), BBMap.end());
203637b23dcSSebastian Pop         VTV.insert(GlobalMap.begin(), GlobalMap.end());
20447d4ee3eSSebastian Pop         NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
205e71c6ab5STobias Grosser         SCEVExpander Expander(SE, "polly");
206e71c6ab5STobias Grosser         Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
207e71c6ab5STobias Grosser                                                  Builder.GetInsertPoint());
208e71c6ab5STobias Grosser 
209e71c6ab5STobias Grosser         BBMap[Old] = Expanded;
210e71c6ab5STobias Grosser         return Expanded;
211e71c6ab5STobias Grosser       }
212369430ffSTobias Grosser     }
213e71c6ab5STobias Grosser 
214*5b463ceaSHongbin Zheng   // Now the scalar dependence is neither available nor SCEVCodegenable, this
215*5b463ceaSHongbin Zheng   // should never happen in the current code generator.
216*5b463ceaSHongbin Zheng   llvm_unreachable("Unexpected scalar dependence in region!");
217*5b463ceaSHongbin Zheng   return NULL;
2183b11a16aSHongbin Zheng }
2193b11a16aSHongbin Zheng 
2203b11a16aSHongbin Zheng void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
2219d10fffaSSebastian Pop                                     ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
2223b11a16aSHongbin Zheng   Instruction *NewInst = Inst->clone();
2233b11a16aSHongbin Zheng 
2243b11a16aSHongbin Zheng   // Replace old operands with the new ones.
2253b11a16aSHongbin Zheng   for (Instruction::const_op_iterator OI = Inst->op_begin(),
226c14582f2STobias Grosser                                       OE = Inst->op_end();
227c14582f2STobias Grosser        OI != OE; ++OI) {
2283b11a16aSHongbin Zheng     Value *OldOperand = *OI;
229369430ffSTobias Grosser     Value *NewOperand =
230369430ffSTobias Grosser         getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
2313b11a16aSHongbin Zheng 
2323b11a16aSHongbin Zheng     if (!NewOperand) {
233c14582f2STobias Grosser       assert(!isa<StoreInst>(NewInst) &&
234c14582f2STobias Grosser              "Store instructions are always needed!");
2353b11a16aSHongbin Zheng       delete NewInst;
2363b11a16aSHongbin Zheng       return;
2373b11a16aSHongbin Zheng     }
2383b11a16aSHongbin Zheng 
2393b11a16aSHongbin Zheng     NewInst->replaceUsesOfWith(OldOperand, NewOperand);
2403b11a16aSHongbin Zheng   }
2413b11a16aSHongbin Zheng 
2423b11a16aSHongbin Zheng   Builder.Insert(NewInst);
2433b11a16aSHongbin Zheng   BBMap[Inst] = NewInst;
2443b11a16aSHongbin Zheng 
2453b11a16aSHongbin Zheng   if (!NewInst->getType()->isVoidTy())
2463b11a16aSHongbin Zheng     NewInst->setName("p_" + Inst->getName());
2473b11a16aSHongbin Zheng }
2483b11a16aSHongbin Zheng 
2493b11a16aSHongbin Zheng std::vector<Value *> BlockGenerator::getMemoryAccessIndex(
250c14582f2STobias Grosser     __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap,
251369430ffSTobias Grosser     ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
2523b11a16aSHongbin Zheng 
253ae2d83ecSTobias Grosser   assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) &&
254ae2d83ecSTobias Grosser          "Only single dimensional access functions supported");
2553b11a16aSHongbin Zheng 
2563b11a16aSHongbin Zheng   std::vector<Value *> IVS;
2573b11a16aSHongbin Zheng   for (unsigned i = 0; i < Statement.getNumIterators(); ++i) {
2583b11a16aSHongbin Zheng     const Value *OriginalIV = Statement.getInductionVariableForDimension(i);
259369430ffSTobias Grosser     Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS, L);
2603b11a16aSHongbin Zheng     IVS.push_back(NewIV);
2613b11a16aSHongbin Zheng   }
2623b11a16aSHongbin Zheng 
2633b11a16aSHongbin Zheng   isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0);
2643b11a16aSHongbin Zheng   IslGenerator IslGen(Builder, IVS);
2653b11a16aSHongbin Zheng   Value *OffsetValue = IslGen.generateIslPwAff(PwAff);
2663b11a16aSHongbin Zheng 
2673b11a16aSHongbin Zheng   Type *Ty = Builder.getInt64Ty();
2683b11a16aSHongbin Zheng   OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true);
2693b11a16aSHongbin Zheng 
2703b11a16aSHongbin Zheng   std::vector<Value *> IndexArray;
2713b11a16aSHongbin Zheng   Value *NullValue = Constant::getNullValue(Ty);
2723b11a16aSHongbin Zheng   IndexArray.push_back(NullValue);
2733b11a16aSHongbin Zheng   IndexArray.push_back(OffsetValue);
2743b11a16aSHongbin Zheng   return IndexArray;
2753b11a16aSHongbin Zheng }
2763b11a16aSHongbin Zheng 
2773b11a16aSHongbin Zheng Value *BlockGenerator::getNewAccessOperand(
278c14582f2STobias Grosser     __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap,
279369430ffSTobias Grosser     ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
2807242ad92STobias Grosser   std::vector<Value *> IndexArray = getMemoryAccessIndex(
281369430ffSTobias Grosser       NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS, L);
282c14582f2STobias Grosser   Value *NewOperand =
283c14582f2STobias Grosser       Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_");
2843b11a16aSHongbin Zheng   return NewOperand;
2853b11a16aSHongbin Zheng }
2863b11a16aSHongbin Zheng 
287e602a076STobias Grosser Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
288e602a076STobias Grosser                                                 const Value *Pointer,
289e602a076STobias Grosser                                                 ValueMapT &BBMap,
290e602a076STobias Grosser                                                 ValueMapT &GlobalMap,
291e602a076STobias Grosser                                                 LoopToScevMapT &LTS) {
292b5f24a66SHongbin Zheng   const MemoryAccess &Access = Statement.getAccessFor(Inst);
2933b11a16aSHongbin Zheng   isl_map *CurrentAccessRelation = Access.getAccessRelation();
2943b11a16aSHongbin Zheng   isl_map *NewAccessRelation = Access.getNewAccessRelation();
2953b11a16aSHongbin Zheng 
296ae2d83ecSTobias Grosser   assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) &&
297ae2d83ecSTobias Grosser          "Current and new access function use different spaces");
2983b11a16aSHongbin Zheng 
2993b11a16aSHongbin Zheng   Value *NewPointer;
3003b11a16aSHongbin Zheng 
3013b11a16aSHongbin Zheng   if (!NewAccessRelation) {
302369430ffSTobias Grosser     NewPointer =
303369430ffSTobias Grosser         getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
3043b11a16aSHongbin Zheng   } else {
3053b11a16aSHongbin Zheng     Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr());
3067242ad92STobias Grosser     NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap,
307369430ffSTobias Grosser                                      GlobalMap, LTS, getLoopForInst(Inst));
3083b11a16aSHongbin Zheng   }
3093b11a16aSHongbin Zheng 
3103b11a16aSHongbin Zheng   isl_map_free(CurrentAccessRelation);
3113b11a16aSHongbin Zheng   isl_map_free(NewAccessRelation);
3123b11a16aSHongbin Zheng   return NewPointer;
3133b11a16aSHongbin Zheng }
3143b11a16aSHongbin Zheng 
3154d96c8d7STobias Grosser Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
316369430ffSTobias Grosser   return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent());
317369430ffSTobias Grosser }
318369430ffSTobias Grosser 
319e602a076STobias Grosser Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
320e602a076STobias Grosser                                           ValueMapT &BBMap,
321e602a076STobias Grosser                                           ValueMapT &GlobalMap,
322e602a076STobias Grosser                                           LoopToScevMapT &LTS) {
3233b11a16aSHongbin Zheng   const Value *Pointer = Load->getPointerOperand();
3243b11a16aSHongbin Zheng   const Instruction *Inst = dyn_cast<Instruction>(Load);
3257242ad92STobias Grosser   Value *NewPointer =
3267242ad92STobias Grosser       generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
327c14582f2STobias Grosser   Value *ScalarLoad =
328c14582f2STobias Grosser       Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
3293b11a16aSHongbin Zheng   return ScalarLoad;
3303b11a16aSHongbin Zheng }
3313b11a16aSHongbin Zheng 
332e602a076STobias Grosser Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
333e602a076STobias Grosser                                            ValueMapT &BBMap,
334e602a076STobias Grosser                                            ValueMapT &GlobalMap,
335e602a076STobias Grosser                                            LoopToScevMapT &LTS) {
3363b11a16aSHongbin Zheng   const Value *Pointer = Store->getPointerOperand();
337c14582f2STobias Grosser   Value *NewPointer =
3389d10fffaSSebastian Pop       generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
339369430ffSTobias Grosser   Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
340369430ffSTobias Grosser                                     LTS, getLoopForInst(Store));
3413b11a16aSHongbin Zheng 
3423b11a16aSHongbin Zheng   return Builder.CreateStore(ValueOperand, NewPointer);
3433b11a16aSHongbin Zheng }
3443b11a16aSHongbin Zheng 
345e602a076STobias Grosser void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
346e602a076STobias Grosser                                      ValueMapT &GlobalMap,
347e602a076STobias Grosser                                      LoopToScevMapT &LTS) {
3483b11a16aSHongbin Zheng   // Terminator instructions control the control flow. They are explicitly
3493b11a16aSHongbin Zheng   // expressed in the clast and do not need to be copied.
3503b11a16aSHongbin Zheng   if (Inst->isTerminator())
3513b11a16aSHongbin Zheng     return;
3523b11a16aSHongbin Zheng 
353ecfe21b7STobias Grosser   if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
354ecfe21b7STobias Grosser                     &Statement.getParent()->getRegion()))
355e71c6ab5STobias Grosser     return;
356e71c6ab5STobias Grosser 
3573b11a16aSHongbin Zheng   if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
358753d43f9SSebastian Pop     Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
3593d94fedfSSebastian Pop     // Compute NewLoad before its insertion in BBMap to make the insertion
3603d94fedfSSebastian Pop     // deterministic.
361753d43f9SSebastian Pop     BBMap[Load] = NewLoad;
3623b11a16aSHongbin Zheng     return;
3633b11a16aSHongbin Zheng   }
3643b11a16aSHongbin Zheng 
3653b11a16aSHongbin Zheng   if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
366753d43f9SSebastian Pop     Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
3673d94fedfSSebastian Pop     // Compute NewStore before its insertion in BBMap to make the insertion
3683d94fedfSSebastian Pop     // deterministic.
369753d43f9SSebastian Pop     BBMap[Store] = NewStore;
3703b11a16aSHongbin Zheng     return;
3713b11a16aSHongbin Zheng   }
3723b11a16aSHongbin Zheng 
3739d10fffaSSebastian Pop   copyInstScalar(Inst, BBMap, GlobalMap, LTS);
3743b11a16aSHongbin Zheng }
3753b11a16aSHongbin Zheng 
3769d10fffaSSebastian Pop void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
3773b11a16aSHongbin Zheng   BasicBlock *BB = Statement.getBasicBlock();
378c14582f2STobias Grosser   BasicBlock *CopyBB =
379c14582f2STobias Grosser       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
3803b11a16aSHongbin Zheng   CopyBB->setName("polly.stmt." + BB->getName());
3813b11a16aSHongbin Zheng   Builder.SetInsertPoint(CopyBB->begin());
3823b11a16aSHongbin Zheng 
3833b11a16aSHongbin Zheng   ValueMapT BBMap;
3843b11a16aSHongbin Zheng 
3853b11a16aSHongbin Zheng   for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
3863b11a16aSHongbin Zheng        ++II)
3879d10fffaSSebastian Pop     copyInstruction(II, BBMap, GlobalMap, LTS);
3883b11a16aSHongbin Zheng }
3893b11a16aSHongbin Zheng 
390e602a076STobias Grosser VectorBlockGenerator::VectorBlockGenerator(IRBuilder<> &B,
391e602a076STobias Grosser                                            VectorValueMapT &GlobalMaps,
392e602a076STobias Grosser                                            std::vector<LoopToScevMapT> &VLTS,
393e602a076STobias Grosser                                            ScopStmt &Stmt,
394e602a076STobias Grosser                                            __isl_keep isl_map *Schedule,
395e602a076STobias Grosser                                            Pass *P)
3969d10fffaSSebastian Pop     : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS),
3979d10fffaSSebastian Pop       Schedule(Schedule) {
3983b11a16aSHongbin Zheng   assert(GlobalMaps.size() > 1 && "Only one vector lane found");
399a00a0291SSebastian Pop   assert(Schedule && "No statement domain provided");
4003b11a16aSHongbin Zheng }
4013b11a16aSHongbin Zheng 
402e602a076STobias Grosser Value *VectorBlockGenerator::getVectorValue(const Value *Old,
403e602a076STobias Grosser                                             ValueMapT &VectorMap,
404e602a076STobias Grosser                                             VectorValueMapT &ScalarMaps,
405e602a076STobias Grosser                                             Loop *L) {
406fe11e287SHongbin Zheng   if (Value *NewValue = VectorMap.lookup(Old))
407fe11e287SHongbin Zheng     return NewValue;
4083b11a16aSHongbin Zheng 
4093b11a16aSHongbin Zheng   int Width = getVectorWidth();
4103b11a16aSHongbin Zheng 
4113b11a16aSHongbin Zheng   Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
4123b11a16aSHongbin Zheng 
4133b11a16aSHongbin Zheng   for (int Lane = 0; Lane < Width; Lane++)
414c14582f2STobias Grosser     Vector = Builder.CreateInsertElement(
4157242ad92STobias Grosser         Vector,
416369430ffSTobias Grosser         getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
4177242ad92STobias Grosser         Builder.getInt32(Lane));
4183b11a16aSHongbin Zheng 
4193b11a16aSHongbin Zheng   VectorMap[Old] = Vector;
4203b11a16aSHongbin Zheng 
4213b11a16aSHongbin Zheng   return Vector;
4223b11a16aSHongbin Zheng }
4233b11a16aSHongbin Zheng 
4243b11a16aSHongbin Zheng Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
4253b11a16aSHongbin Zheng   PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
4263b11a16aSHongbin Zheng   assert(PointerTy && "PointerType expected");
4273b11a16aSHongbin Zheng 
4283b11a16aSHongbin Zheng   Type *ScalarType = PointerTy->getElementType();
4293b11a16aSHongbin Zheng   VectorType *VectorType = VectorType::get(ScalarType, Width);
4303b11a16aSHongbin Zheng 
4313b11a16aSHongbin Zheng   return PointerType::getUnqual(VectorType);
4323b11a16aSHongbin Zheng }
4333b11a16aSHongbin Zheng 
4343b11a16aSHongbin Zheng Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
4353b11a16aSHongbin Zheng                                                    ValueMapT &BBMap) {
4363b11a16aSHongbin Zheng   const Value *Pointer = Load->getPointerOperand();
4373b11a16aSHongbin Zheng   Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
438369430ffSTobias Grosser   Value *NewPointer =
439369430ffSTobias Grosser       getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
440c14582f2STobias Grosser   Value *VectorPtr =
441c14582f2STobias Grosser       Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
442c14582f2STobias Grosser   LoadInst *VecLoad =
443c14582f2STobias Grosser       Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
4443b11a16aSHongbin Zheng   if (!Aligned)
4453b11a16aSHongbin Zheng     VecLoad->setAlignment(8);
4463b11a16aSHongbin Zheng 
4473b11a16aSHongbin Zheng   return VecLoad;
4483b11a16aSHongbin Zheng }
4493b11a16aSHongbin Zheng 
4503b11a16aSHongbin Zheng Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
4513b11a16aSHongbin Zheng                                                     ValueMapT &BBMap) {
4523b11a16aSHongbin Zheng   const Value *Pointer = Load->getPointerOperand();
4533b11a16aSHongbin Zheng   Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
454369430ffSTobias Grosser   Value *NewPointer =
455369430ffSTobias Grosser       getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
4563b11a16aSHongbin Zheng   Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
4573b11a16aSHongbin Zheng                                            Load->getName() + "_p_vec_p");
458c14582f2STobias Grosser   LoadInst *ScalarLoad =
459c14582f2STobias Grosser       Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
4603b11a16aSHongbin Zheng 
4613b11a16aSHongbin Zheng   if (!Aligned)
4623b11a16aSHongbin Zheng     ScalarLoad->setAlignment(8);
4633b11a16aSHongbin Zheng 
464c14582f2STobias Grosser   Constant *SplatVector = Constant::getNullValue(
465c14582f2STobias Grosser       VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
4663b11a16aSHongbin Zheng 
467c14582f2STobias Grosser   Value *VectorLoad = Builder.CreateShuffleVector(
468c14582f2STobias Grosser       ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
4693b11a16aSHongbin Zheng   return VectorLoad;
4703b11a16aSHongbin Zheng }
4713b11a16aSHongbin Zheng 
472e602a076STobias Grosser Value *
473e602a076STobias Grosser VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
474e602a076STobias Grosser                                                 VectorValueMapT &ScalarMaps) {
4753b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
4763b11a16aSHongbin Zheng   const Value *Pointer = Load->getPointerOperand();
4773b11a16aSHongbin Zheng   VectorType *VectorType = VectorType::get(
4783b11a16aSHongbin Zheng       dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
4793b11a16aSHongbin Zheng 
4803b11a16aSHongbin Zheng   Value *Vector = UndefValue::get(VectorType);
4813b11a16aSHongbin Zheng 
4823b11a16aSHongbin Zheng   for (int i = 0; i < VectorWidth; i++) {
483369430ffSTobias Grosser     Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
484369430ffSTobias Grosser                                     VLTS[i], getLoopForInst(Load));
485c14582f2STobias Grosser     Value *ScalarLoad =
486c14582f2STobias Grosser         Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
487c14582f2STobias Grosser     Vector = Builder.CreateInsertElement(
488c14582f2STobias Grosser         Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
4893b11a16aSHongbin Zheng   }
4903b11a16aSHongbin Zheng 
4913b11a16aSHongbin Zheng   return Vector;
4923b11a16aSHongbin Zheng }
4933b11a16aSHongbin Zheng 
494e602a076STobias Grosser void VectorBlockGenerator::generateLoad(const LoadInst *Load,
495e602a076STobias Grosser                                         ValueMapT &VectorMap,
496e602a076STobias Grosser                                         VectorValueMapT &ScalarMaps) {
49768794217SHongbin Zheng   if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
49868794217SHongbin Zheng       !VectorType::isValidElementType(Load->getType())) {
4993b11a16aSHongbin Zheng     for (int i = 0; i < getVectorWidth(); i++)
500c14582f2STobias Grosser       ScalarMaps[i][Load] =
5019d10fffaSSebastian Pop           generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
5023b11a16aSHongbin Zheng     return;
5033b11a16aSHongbin Zheng   }
5043b11a16aSHongbin Zheng 
505b5f24a66SHongbin Zheng   const MemoryAccess &Access = Statement.getAccessFor(Load);
5063b11a16aSHongbin Zheng 
5073b11a16aSHongbin Zheng   Value *NewLoad;
508a00a0291SSebastian Pop   if (Access.isStrideZero(isl_map_copy(Schedule)))
5093b11a16aSHongbin Zheng     NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
510a00a0291SSebastian Pop   else if (Access.isStrideOne(isl_map_copy(Schedule)))
5113b11a16aSHongbin Zheng     NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
5123b11a16aSHongbin Zheng   else
5133b11a16aSHongbin Zheng     NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
5143b11a16aSHongbin Zheng 
5153b11a16aSHongbin Zheng   VectorMap[Load] = NewLoad;
5163b11a16aSHongbin Zheng }
5173b11a16aSHongbin Zheng 
5183b11a16aSHongbin Zheng void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
5193b11a16aSHongbin Zheng                                          ValueMapT &VectorMap,
5203b11a16aSHongbin Zheng                                          VectorValueMapT &ScalarMaps) {
5213b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
522369430ffSTobias Grosser   Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
523369430ffSTobias Grosser                                      getLoopForInst(Inst));
5243b11a16aSHongbin Zheng 
5253b11a16aSHongbin Zheng   assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
5263b11a16aSHongbin Zheng 
5273b11a16aSHongbin Zheng   const CastInst *Cast = dyn_cast<CastInst>(Inst);
5283b11a16aSHongbin Zheng   VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
5293b11a16aSHongbin Zheng   VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
5303b11a16aSHongbin Zheng }
5313b11a16aSHongbin Zheng 
5323b11a16aSHongbin Zheng void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
5333b11a16aSHongbin Zheng                                           ValueMapT &VectorMap,
5343b11a16aSHongbin Zheng                                           VectorValueMapT &ScalarMaps) {
535369430ffSTobias Grosser   Loop *L = getLoopForInst(Inst);
5363b11a16aSHongbin Zheng   Value *OpZero = Inst->getOperand(0);
5373b11a16aSHongbin Zheng   Value *OpOne = Inst->getOperand(1);
5383b11a16aSHongbin Zheng 
5393b11a16aSHongbin Zheng   Value *NewOpZero, *NewOpOne;
540369430ffSTobias Grosser   NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
541369430ffSTobias Grosser   NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
5423b11a16aSHongbin Zheng 
5431bb59b0dSTobias Grosser   Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
5443b11a16aSHongbin Zheng                                        Inst->getName() + "p_vec");
5453b11a16aSHongbin Zheng   VectorMap[Inst] = NewInst;
5463b11a16aSHongbin Zheng }
5473b11a16aSHongbin Zheng 
548e602a076STobias Grosser void VectorBlockGenerator::copyStore(const StoreInst *Store,
549e602a076STobias Grosser                                      ValueMapT &VectorMap,
550e602a076STobias Grosser                                      VectorValueMapT &ScalarMaps) {
5513b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
5523b11a16aSHongbin Zheng 
553b5f24a66SHongbin Zheng   const MemoryAccess &Access = Statement.getAccessFor(Store);
5543b11a16aSHongbin Zheng 
5553b11a16aSHongbin Zheng   const Value *Pointer = Store->getPointerOperand();
556369430ffSTobias Grosser   Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
557369430ffSTobias Grosser                                  ScalarMaps, getLoopForInst(Store));
5583b11a16aSHongbin Zheng 
559a00a0291SSebastian Pop   if (Access.isStrideOne(isl_map_copy(Schedule))) {
5603b11a16aSHongbin Zheng     Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
561369430ffSTobias Grosser     Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0],
562369430ffSTobias Grosser                                     VLTS[0], getLoopForInst(Store));
5633b11a16aSHongbin Zheng 
564c14582f2STobias Grosser     Value *VectorPtr =
565c14582f2STobias Grosser         Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
5663b11a16aSHongbin Zheng     StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
5673b11a16aSHongbin Zheng 
5683b11a16aSHongbin Zheng     if (!Aligned)
5693b11a16aSHongbin Zheng       Store->setAlignment(8);
5703b11a16aSHongbin Zheng   } else {
5713b11a16aSHongbin Zheng     for (unsigned i = 0; i < ScalarMaps.size(); i++) {
5721bb59b0dSTobias Grosser       Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
573369430ffSTobias Grosser       Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
574369430ffSTobias Grosser                                       VLTS[i], getLoopForInst(Store));
5753b11a16aSHongbin Zheng       Builder.CreateStore(Scalar, NewPointer);
5763b11a16aSHongbin Zheng     }
5773b11a16aSHongbin Zheng   }
5783b11a16aSHongbin Zheng }
5793b11a16aSHongbin Zheng 
5803b11a16aSHongbin Zheng bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
5813b11a16aSHongbin Zheng                                              ValueMapT &VectorMap) {
5823b11a16aSHongbin Zheng   for (Instruction::const_op_iterator OI = Inst->op_begin(),
583c14582f2STobias Grosser                                       OE = Inst->op_end();
584c14582f2STobias Grosser        OI != OE; ++OI)
5853b11a16aSHongbin Zheng     if (VectorMap.count(*OI))
5863b11a16aSHongbin Zheng       return true;
5873b11a16aSHongbin Zheng   return false;
5883b11a16aSHongbin Zheng }
5893b11a16aSHongbin Zheng 
5903b11a16aSHongbin Zheng bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
5913b11a16aSHongbin Zheng                                                ValueMapT &VectorMap,
5923b11a16aSHongbin Zheng                                                VectorValueMapT &ScalarMaps) {
5933b11a16aSHongbin Zheng   bool HasVectorOperand = false;
5943b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
5953b11a16aSHongbin Zheng 
5963b11a16aSHongbin Zheng   for (Instruction::const_op_iterator OI = Inst->op_begin(),
597c14582f2STobias Grosser                                       OE = Inst->op_end();
598c14582f2STobias Grosser        OI != OE; ++OI) {
5993b11a16aSHongbin Zheng     ValueMapT::iterator VecOp = VectorMap.find(*OI);
6003b11a16aSHongbin Zheng 
6013b11a16aSHongbin Zheng     if (VecOp == VectorMap.end())
6023b11a16aSHongbin Zheng       continue;
6033b11a16aSHongbin Zheng 
6043b11a16aSHongbin Zheng     HasVectorOperand = true;
6053b11a16aSHongbin Zheng     Value *NewVector = VecOp->second;
6063b11a16aSHongbin Zheng 
6073b11a16aSHongbin Zheng     for (int i = 0; i < VectorWidth; ++i) {
6083b11a16aSHongbin Zheng       ValueMapT &SM = ScalarMaps[i];
6093b11a16aSHongbin Zheng 
6103b11a16aSHongbin Zheng       // If there is one scalar extracted, all scalar elements should have
6113b11a16aSHongbin Zheng       // already been extracted by the code here. So no need to check for the
6123b11a16aSHongbin Zheng       // existance of all of them.
6133b11a16aSHongbin Zheng       if (SM.count(*OI))
6143b11a16aSHongbin Zheng         break;
6153b11a16aSHongbin Zheng 
6163b11a16aSHongbin Zheng       SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
6173b11a16aSHongbin Zheng     }
6183b11a16aSHongbin Zheng   }
6193b11a16aSHongbin Zheng 
6203b11a16aSHongbin Zheng   return HasVectorOperand;
6213b11a16aSHongbin Zheng }
6223b11a16aSHongbin Zheng 
6233b11a16aSHongbin Zheng void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
6243b11a16aSHongbin Zheng                                               ValueMapT &VectorMap,
6253b11a16aSHongbin Zheng                                               VectorValueMapT &ScalarMaps) {
6263b11a16aSHongbin Zheng   bool HasVectorOperand;
6273b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
6283b11a16aSHongbin Zheng 
6293b11a16aSHongbin Zheng   HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
6303b11a16aSHongbin Zheng 
6313b11a16aSHongbin Zheng   for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
6329d10fffaSSebastian Pop     copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
6339d10fffaSSebastian Pop                    VLTS[VectorLane]);
6343b11a16aSHongbin Zheng 
6353b11a16aSHongbin Zheng   if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
6363b11a16aSHongbin Zheng     return;
6373b11a16aSHongbin Zheng 
6383b11a16aSHongbin Zheng   // Make the result available as vector value.
6393b11a16aSHongbin Zheng   VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
6403b11a16aSHongbin Zheng   Value *Vector = UndefValue::get(VectorType);
6413b11a16aSHongbin Zheng 
6423b11a16aSHongbin Zheng   for (int i = 0; i < VectorWidth; i++)
6433b11a16aSHongbin Zheng     Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
6443b11a16aSHongbin Zheng                                          Builder.getInt32(i));
6453b11a16aSHongbin Zheng 
6463b11a16aSHongbin Zheng   VectorMap[Inst] = Vector;
6473b11a16aSHongbin Zheng }
6483b11a16aSHongbin Zheng 
649c14582f2STobias Grosser int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
6503b11a16aSHongbin Zheng 
6513b11a16aSHongbin Zheng void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
6523b11a16aSHongbin Zheng                                            ValueMapT &VectorMap,
6533b11a16aSHongbin Zheng                                            VectorValueMapT &ScalarMaps) {
6543b11a16aSHongbin Zheng   // Terminator instructions control the control flow. They are explicitly
6553b11a16aSHongbin Zheng   // expressed in the clast and do not need to be copied.
6563b11a16aSHongbin Zheng   if (Inst->isTerminator())
6573b11a16aSHongbin Zheng     return;
6583b11a16aSHongbin Zheng 
659ecfe21b7STobias Grosser   if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
660ecfe21b7STobias Grosser                     &Statement.getParent()->getRegion()))
661e71c6ab5STobias Grosser     return;
662e71c6ab5STobias Grosser 
6633b11a16aSHongbin Zheng   if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
6643b11a16aSHongbin Zheng     generateLoad(Load, VectorMap, ScalarMaps);
6653b11a16aSHongbin Zheng     return;
6663b11a16aSHongbin Zheng   }
6673b11a16aSHongbin Zheng 
6683b11a16aSHongbin Zheng   if (hasVectorOperands(Inst, VectorMap)) {
6693b11a16aSHongbin Zheng     if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
6703b11a16aSHongbin Zheng       copyStore(Store, VectorMap, ScalarMaps);
6713b11a16aSHongbin Zheng       return;
6723b11a16aSHongbin Zheng     }
6733b11a16aSHongbin Zheng 
6743b11a16aSHongbin Zheng     if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
6753b11a16aSHongbin Zheng       copyUnaryInst(Unary, VectorMap, ScalarMaps);
6763b11a16aSHongbin Zheng       return;
6773b11a16aSHongbin Zheng     }
6783b11a16aSHongbin Zheng 
6793b11a16aSHongbin Zheng     if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
6803b11a16aSHongbin Zheng       copyBinaryInst(Binary, VectorMap, ScalarMaps);
6813b11a16aSHongbin Zheng       return;
6823b11a16aSHongbin Zheng     }
6833b11a16aSHongbin Zheng 
6843b11a16aSHongbin Zheng     // Falltrough: We generate scalar instructions, if we don't know how to
6853b11a16aSHongbin Zheng     // generate vector code.
6863b11a16aSHongbin Zheng   }
6873b11a16aSHongbin Zheng 
6883b11a16aSHongbin Zheng   copyInstScalarized(Inst, VectorMap, ScalarMaps);
6893b11a16aSHongbin Zheng }
6903b11a16aSHongbin Zheng 
6913b11a16aSHongbin Zheng void VectorBlockGenerator::copyBB() {
6923b11a16aSHongbin Zheng   BasicBlock *BB = Statement.getBasicBlock();
693c14582f2STobias Grosser   BasicBlock *CopyBB =
694c14582f2STobias Grosser       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
6953b11a16aSHongbin Zheng   CopyBB->setName("polly.stmt." + BB->getName());
6963b11a16aSHongbin Zheng   Builder.SetInsertPoint(CopyBB->begin());
6973b11a16aSHongbin Zheng 
6983b11a16aSHongbin Zheng   // Create two maps that store the mapping from the original instructions of
6993b11a16aSHongbin Zheng   // the old basic block to their copies in the new basic block. Those maps
7003b11a16aSHongbin Zheng   // are basic block local.
7013b11a16aSHongbin Zheng   //
7023b11a16aSHongbin Zheng   // As vector code generation is supported there is one map for scalar values
7033b11a16aSHongbin Zheng   // and one for vector values.
7043b11a16aSHongbin Zheng   //
7053b11a16aSHongbin Zheng   // In case we just do scalar code generation, the vectorMap is not used and
7063b11a16aSHongbin Zheng   // the scalarMap has just one dimension, which contains the mapping.
7073b11a16aSHongbin Zheng   //
7083b11a16aSHongbin Zheng   // In case vector code generation is done, an instruction may either appear
7093b11a16aSHongbin Zheng   // in the vector map once (as it is calculating >vectorwidth< values at a
7103b11a16aSHongbin Zheng   // time. Or (if the values are calculated using scalar operations), it
7113b11a16aSHongbin Zheng   // appears once in every dimension of the scalarMap.
7123b11a16aSHongbin Zheng   VectorValueMapT ScalarBlockMap(getVectorWidth());
7133b11a16aSHongbin Zheng   ValueMapT VectorBlockMap;
7143b11a16aSHongbin Zheng 
715c14582f2STobias Grosser   for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
716c14582f2STobias Grosser        ++II)
7173b11a16aSHongbin Zheng     copyInstruction(II, VectorBlockMap, ScalarBlockMap);
7183b11a16aSHongbin Zheng }
719