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"
28*030237d0STobias Grosser #include "llvm/IR/IntrinsicInst.h"
293b11a16aSHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h"
303b11a16aSHongbin Zheng 
313b11a16aSHongbin Zheng using namespace llvm;
323b11a16aSHongbin Zheng using namespace polly;
333b11a16aSHongbin Zheng 
343b11a16aSHongbin Zheng static cl::opt<bool>
35c14582f2STobias Grosser Aligned("enable-polly-aligned", cl::desc("Assumed aligned memory accesses."),
36c14582f2STobias Grosser         cl::Hidden, cl::value_desc("OpenMP code generation enabled if true"),
37637bd631STobias Grosser         cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
383b11a16aSHongbin Zheng 
39e602a076STobias Grosser static cl::opt<bool, true>
40e602a076STobias Grosser SCEVCodegenF("polly-codegen-scev", cl::desc("Use SCEV based code generation."),
41e602a076STobias Grosser              cl::Hidden, cl::location(SCEVCodegen), cl::init(false),
42637bd631STobias Grosser              cl::ZeroOrMore, cl::cat(PollyCategory));
43ecfe21b7STobias Grosser 
44ecfe21b7STobias Grosser bool polly::SCEVCodegen;
45ecfe21b7STobias Grosser 
46ecfe21b7STobias Grosser bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI,
47ecfe21b7STobias Grosser                           ScalarEvolution *SE, const Region *R) {
48ecfe21b7STobias Grosser   if (SCEVCodegen) {
49ecfe21b7STobias Grosser     if (!I || !SE->isSCEVable(I->getType()))
50ecfe21b7STobias Grosser       return false;
51ecfe21b7STobias Grosser 
52ecfe21b7STobias Grosser     if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I)))
53ecfe21b7STobias Grosser       if (!isa<SCEVCouldNotCompute>(Scev))
54ecfe21b7STobias Grosser         if (!hasScalarDepsInsideRegion(Scev, R))
55ecfe21b7STobias Grosser           return true;
56ecfe21b7STobias Grosser 
57ecfe21b7STobias Grosser     return false;
58ecfe21b7STobias Grosser   }
59ecfe21b7STobias Grosser 
60ecfe21b7STobias Grosser   Loop *L = LI->getLoopFor(I->getParent());
61e8df5bd9STobias Grosser   return L && I == L->getCanonicalInductionVariable() && R->contains(L);
62ecfe21b7STobias Grosser }
63ecfe21b7STobias Grosser 
643b11a16aSHongbin Zheng // Helper class to generate memory location.
653b11a16aSHongbin Zheng namespace {
663b11a16aSHongbin Zheng class IslGenerator {
673b11a16aSHongbin Zheng public:
687242ad92STobias Grosser   IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS)
697242ad92STobias Grosser       : Builder(Builder), IVS(IVS) {}
70edab1359STobias Grosser   Value *generateIslVal(__isl_take isl_val *Val);
713b11a16aSHongbin Zheng   Value *generateIslAff(__isl_take isl_aff *Aff);
723b11a16aSHongbin Zheng   Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff);
733b11a16aSHongbin Zheng 
743b11a16aSHongbin Zheng private:
753b11a16aSHongbin Zheng   typedef struct {
763b11a16aSHongbin Zheng     Value *Result;
773b11a16aSHongbin Zheng     class IslGenerator *Generator;
783b11a16aSHongbin Zheng   } IslGenInfo;
793b11a16aSHongbin Zheng 
803b11a16aSHongbin Zheng   IRBuilder<> &Builder;
813b11a16aSHongbin Zheng   std::vector<Value *> &IVS;
821bb59b0dSTobias Grosser   static int mergeIslAffValues(__isl_take isl_set *Set, __isl_take isl_aff *Aff,
831bb59b0dSTobias Grosser                                void *User);
843b11a16aSHongbin Zheng };
853b11a16aSHongbin Zheng }
863b11a16aSHongbin Zheng 
87edab1359STobias Grosser Value *IslGenerator::generateIslVal(__isl_take isl_val *Val) {
88edab1359STobias Grosser   Value *IntValue = Builder.getInt(APIntFromVal(Val));
893b11a16aSHongbin Zheng   return IntValue;
903b11a16aSHongbin Zheng }
913b11a16aSHongbin Zheng 
923b11a16aSHongbin Zheng Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) {
933b11a16aSHongbin Zheng   Value *Result;
943b11a16aSHongbin Zheng   Value *ConstValue;
95edab1359STobias Grosser   isl_val *Val;
963b11a16aSHongbin Zheng 
97edab1359STobias Grosser   Val = isl_aff_get_constant_val(Aff);
98edab1359STobias Grosser   ConstValue = generateIslVal(Val);
993b11a16aSHongbin Zheng   Type *Ty = Builder.getInt64Ty();
1003b11a16aSHongbin Zheng 
1013b11a16aSHongbin Zheng   // FIXME: We should give the constant and coefficients the right type. Here
1023b11a16aSHongbin Zheng   // we force it into i64.
1033b11a16aSHongbin Zheng   Result = Builder.CreateSExtOrBitCast(ConstValue, Ty);
1043b11a16aSHongbin Zheng 
1053b11a16aSHongbin Zheng   unsigned int NbInputDims = isl_aff_dim(Aff, isl_dim_in);
1063b11a16aSHongbin Zheng 
1077242ad92STobias Grosser   assert((IVS.size() == NbInputDims) &&
1087242ad92STobias Grosser          "The Dimension of Induction Variables must match the dimension of the "
1097242ad92STobias Grosser          "affine space.");
1103b11a16aSHongbin Zheng 
1113b11a16aSHongbin Zheng   for (unsigned int i = 0; i < NbInputDims; ++i) {
1123b11a16aSHongbin Zheng     Value *CoefficientValue;
113edab1359STobias Grosser     Val = isl_aff_get_coefficient_val(Aff, isl_dim_in, i);
1143b11a16aSHongbin Zheng 
115edab1359STobias Grosser     if (isl_val_is_zero(Val)) {
116edab1359STobias Grosser       isl_val_free(Val);
1173b11a16aSHongbin Zheng       continue;
118edab1359STobias Grosser     }
1193b11a16aSHongbin Zheng 
120edab1359STobias Grosser     CoefficientValue = generateIslVal(Val);
1213b11a16aSHongbin Zheng     CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true);
1223b11a16aSHongbin Zheng     Value *IV = Builder.CreateIntCast(IVS[i], Ty, true);
1233b11a16aSHongbin Zheng     Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff");
1243b11a16aSHongbin Zheng     Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff");
1253b11a16aSHongbin Zheng   }
1263b11a16aSHongbin Zheng 
1273b11a16aSHongbin Zheng   isl_aff_free(Aff);
1283b11a16aSHongbin Zheng 
1293b11a16aSHongbin Zheng   return Result;
1303b11a16aSHongbin Zheng }
1313b11a16aSHongbin Zheng 
1323b11a16aSHongbin Zheng int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set,
1333b11a16aSHongbin Zheng                                     __isl_take isl_aff *Aff, void *User) {
1343b11a16aSHongbin Zheng   IslGenInfo *GenInfo = (IslGenInfo *)User;
1353b11a16aSHongbin Zheng 
1367242ad92STobias Grosser   assert((GenInfo->Result == NULL) &&
1377242ad92STobias Grosser          "Result is already set. Currently only single isl_aff is supported");
1387242ad92STobias Grosser   assert(isl_set_plain_is_universe(Set) &&
1397242ad92STobias Grosser          "Code generation failed because the set is not universe");
1403b11a16aSHongbin Zheng 
1413b11a16aSHongbin Zheng   GenInfo->Result = GenInfo->Generator->generateIslAff(Aff);
1423b11a16aSHongbin Zheng 
1433b11a16aSHongbin Zheng   isl_set_free(Set);
1443b11a16aSHongbin Zheng   return 0;
1453b11a16aSHongbin Zheng }
1463b11a16aSHongbin Zheng 
1473b11a16aSHongbin Zheng Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) {
1483b11a16aSHongbin Zheng   IslGenInfo User;
1493b11a16aSHongbin Zheng   User.Result = NULL;
1503b11a16aSHongbin Zheng   User.Generator = this;
1513b11a16aSHongbin Zheng   isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User);
1523b11a16aSHongbin Zheng   assert(User.Result && "Code generation for isl_pw_aff failed");
1533b11a16aSHongbin Zheng 
1543b11a16aSHongbin Zheng   isl_pw_aff_free(PwAff);
1553b11a16aSHongbin Zheng   return User.Result;
1563b11a16aSHongbin Zheng }
1573b11a16aSHongbin Zheng 
1587242ad92STobias Grosser BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P)
1597242ad92STobias Grosser     : Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {
1607242ad92STobias Grosser }
161e71c6ab5STobias Grosser 
1625b463ceaSHongbin Zheng Value *BlockGenerator::lookupAvailableValue(const Value *Old, ValueMapT &BBMap,
1635b463ceaSHongbin Zheng                                             ValueMapT &GlobalMap) const {
1643b11a16aSHongbin Zheng   // We assume constants never change.
1653b11a16aSHongbin Zheng   // This avoids map lookups for many calls to this function.
1663b11a16aSHongbin Zheng   if (isa<Constant>(Old))
1673b11a16aSHongbin Zheng     return const_cast<Value *>(Old);
1683b11a16aSHongbin Zheng 
169fe11e287SHongbin Zheng   if (Value *New = GlobalMap.lookup(Old)) {
170c14582f2STobias Grosser     if (Old->getType()->getScalarSizeInBits() <
171c14582f2STobias Grosser         New->getType()->getScalarSizeInBits())
1723b11a16aSHongbin Zheng       New = Builder.CreateTruncOrBitCast(New, Old->getType());
1733b11a16aSHongbin Zheng 
1743b11a16aSHongbin Zheng     return New;
1753b11a16aSHongbin Zheng   }
1763b11a16aSHongbin Zheng 
1775b463ceaSHongbin Zheng   // Or it is probably a scop-constant value defined as global, function
1785b463ceaSHongbin Zheng   // parameter or an instruction not within the scop.
1795b463ceaSHongbin Zheng   if (isa<GlobalValue>(Old) || isa<Argument>(Old))
1805b463ceaSHongbin Zheng     return const_cast<Value *>(Old);
1815b463ceaSHongbin Zheng 
1825b463ceaSHongbin Zheng   if (const Instruction *Inst = dyn_cast<Instruction>(Old))
1835b463ceaSHongbin Zheng     if (!Statement.getParent()->getRegion().contains(Inst->getParent()))
1845b463ceaSHongbin Zheng       return const_cast<Value *>(Old);
1855b463ceaSHongbin Zheng 
186fe11e287SHongbin Zheng   if (Value *New = BBMap.lookup(Old))
187fe11e287SHongbin Zheng     return New;
1883b11a16aSHongbin Zheng 
1895b463ceaSHongbin Zheng   return NULL;
1905b463ceaSHongbin Zheng }
1915b463ceaSHongbin Zheng 
1925b463ceaSHongbin Zheng Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap,
1935b463ceaSHongbin Zheng                                    ValueMapT &GlobalMap, LoopToScevMapT &LTS,
1945b463ceaSHongbin Zheng                                    Loop *L) {
1955b463ceaSHongbin Zheng   if (Value *New = lookupAvailableValue(Old, BBMap, GlobalMap))
1965b463ceaSHongbin Zheng     return New;
1975b463ceaSHongbin Zheng 
198e71c6ab5STobias Grosser   if (SCEVCodegen && SE.isSCEVable(Old->getType()))
199369430ffSTobias Grosser     if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) {
200e71c6ab5STobias Grosser       if (!isa<SCEVCouldNotCompute>(Scev)) {
201637b23dcSSebastian Pop         const SCEV *NewScev = apply(Scev, LTS, SE);
202637b23dcSSebastian Pop         ValueToValueMap VTV;
203637b23dcSSebastian Pop         VTV.insert(BBMap.begin(), BBMap.end());
204637b23dcSSebastian Pop         VTV.insert(GlobalMap.begin(), GlobalMap.end());
20547d4ee3eSSebastian Pop         NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV);
206e71c6ab5STobias Grosser         SCEVExpander Expander(SE, "polly");
207e71c6ab5STobias Grosser         Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(),
208e71c6ab5STobias Grosser                                                  Builder.GetInsertPoint());
209e71c6ab5STobias Grosser 
210e71c6ab5STobias Grosser         BBMap[Old] = Expanded;
211e71c6ab5STobias Grosser         return Expanded;
212e71c6ab5STobias Grosser       }
213369430ffSTobias Grosser     }
214e71c6ab5STobias Grosser 
2155b463ceaSHongbin Zheng   // Now the scalar dependence is neither available nor SCEVCodegenable, this
2165b463ceaSHongbin Zheng   // should never happen in the current code generator.
2175b463ceaSHongbin Zheng   llvm_unreachable("Unexpected scalar dependence in region!");
2185b463ceaSHongbin Zheng   return NULL;
2193b11a16aSHongbin Zheng }
2203b11a16aSHongbin Zheng 
2213b11a16aSHongbin Zheng void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap,
2229d10fffaSSebastian Pop                                     ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
223*030237d0STobias Grosser 
224*030237d0STobias Grosser   // We do not generate debug intrinsics as we did not investigate how to
225*030237d0STobias Grosser   // copy them correctly. At the current state, they just crash the code
226*030237d0STobias Grosser   // generation as the meta-data operands are not correctly copied.
227*030237d0STobias Grosser   if (isa<DbgInfoIntrinsic>(Inst))
228*030237d0STobias Grosser     return;
229*030237d0STobias Grosser 
2303b11a16aSHongbin Zheng   Instruction *NewInst = Inst->clone();
2313b11a16aSHongbin Zheng 
2323b11a16aSHongbin Zheng   // Replace old operands with the new ones.
2333b11a16aSHongbin Zheng   for (Instruction::const_op_iterator OI = Inst->op_begin(),
234c14582f2STobias Grosser                                       OE = Inst->op_end();
235c14582f2STobias Grosser        OI != OE; ++OI) {
2363b11a16aSHongbin Zheng     Value *OldOperand = *OI;
237369430ffSTobias Grosser     Value *NewOperand =
238369430ffSTobias Grosser         getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
2393b11a16aSHongbin Zheng 
2403b11a16aSHongbin Zheng     if (!NewOperand) {
241c14582f2STobias Grosser       assert(!isa<StoreInst>(NewInst) &&
242c14582f2STobias Grosser              "Store instructions are always needed!");
2433b11a16aSHongbin Zheng       delete NewInst;
2443b11a16aSHongbin Zheng       return;
2453b11a16aSHongbin Zheng     }
2463b11a16aSHongbin Zheng 
2473b11a16aSHongbin Zheng     NewInst->replaceUsesOfWith(OldOperand, NewOperand);
2483b11a16aSHongbin Zheng   }
2493b11a16aSHongbin Zheng 
2503b11a16aSHongbin Zheng   Builder.Insert(NewInst);
2513b11a16aSHongbin Zheng   BBMap[Inst] = NewInst;
2523b11a16aSHongbin Zheng 
2533b11a16aSHongbin Zheng   if (!NewInst->getType()->isVoidTy())
2543b11a16aSHongbin Zheng     NewInst->setName("p_" + Inst->getName());
2553b11a16aSHongbin Zheng }
2563b11a16aSHongbin Zheng 
2573b11a16aSHongbin Zheng std::vector<Value *> BlockGenerator::getMemoryAccessIndex(
258c14582f2STobias Grosser     __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap,
259369430ffSTobias Grosser     ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
2603b11a16aSHongbin Zheng 
261ae2d83ecSTobias Grosser   assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) &&
262ae2d83ecSTobias Grosser          "Only single dimensional access functions supported");
2633b11a16aSHongbin Zheng 
2643b11a16aSHongbin Zheng   std::vector<Value *> IVS;
2653b11a16aSHongbin Zheng   for (unsigned i = 0; i < Statement.getNumIterators(); ++i) {
2663b11a16aSHongbin Zheng     const Value *OriginalIV = Statement.getInductionVariableForDimension(i);
267369430ffSTobias Grosser     Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS, L);
2683b11a16aSHongbin Zheng     IVS.push_back(NewIV);
2693b11a16aSHongbin Zheng   }
2703b11a16aSHongbin Zheng 
2713b11a16aSHongbin Zheng   isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0);
2723b11a16aSHongbin Zheng   IslGenerator IslGen(Builder, IVS);
2733b11a16aSHongbin Zheng   Value *OffsetValue = IslGen.generateIslPwAff(PwAff);
2743b11a16aSHongbin Zheng 
2753b11a16aSHongbin Zheng   Type *Ty = Builder.getInt64Ty();
2763b11a16aSHongbin Zheng   OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true);
2773b11a16aSHongbin Zheng 
2783b11a16aSHongbin Zheng   std::vector<Value *> IndexArray;
2793b11a16aSHongbin Zheng   Value *NullValue = Constant::getNullValue(Ty);
2803b11a16aSHongbin Zheng   IndexArray.push_back(NullValue);
2813b11a16aSHongbin Zheng   IndexArray.push_back(OffsetValue);
2823b11a16aSHongbin Zheng   return IndexArray;
2833b11a16aSHongbin Zheng }
2843b11a16aSHongbin Zheng 
2853b11a16aSHongbin Zheng Value *BlockGenerator::getNewAccessOperand(
286c14582f2STobias Grosser     __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap,
287369430ffSTobias Grosser     ValueMapT &GlobalMap, LoopToScevMapT &LTS, Loop *L) {
2887242ad92STobias Grosser   std::vector<Value *> IndexArray = getMemoryAccessIndex(
289369430ffSTobias Grosser       NewAccessRelation, BaseAddress, BBMap, GlobalMap, LTS, L);
290c14582f2STobias Grosser   Value *NewOperand =
291c14582f2STobias Grosser       Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_");
2923b11a16aSHongbin Zheng   return NewOperand;
2933b11a16aSHongbin Zheng }
2943b11a16aSHongbin Zheng 
295e602a076STobias Grosser Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst,
296e602a076STobias Grosser                                                 const Value *Pointer,
297e602a076STobias Grosser                                                 ValueMapT &BBMap,
298e602a076STobias Grosser                                                 ValueMapT &GlobalMap,
299e602a076STobias Grosser                                                 LoopToScevMapT &LTS) {
300b5f24a66SHongbin Zheng   const MemoryAccess &Access = Statement.getAccessFor(Inst);
3013b11a16aSHongbin Zheng   isl_map *CurrentAccessRelation = Access.getAccessRelation();
3023b11a16aSHongbin Zheng   isl_map *NewAccessRelation = Access.getNewAccessRelation();
3033b11a16aSHongbin Zheng 
304ae2d83ecSTobias Grosser   assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) &&
305ae2d83ecSTobias Grosser          "Current and new access function use different spaces");
3063b11a16aSHongbin Zheng 
3073b11a16aSHongbin Zheng   Value *NewPointer;
3083b11a16aSHongbin Zheng 
3093b11a16aSHongbin Zheng   if (!NewAccessRelation) {
310369430ffSTobias Grosser     NewPointer =
311369430ffSTobias Grosser         getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst));
3123b11a16aSHongbin Zheng   } else {
3133b11a16aSHongbin Zheng     Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr());
3147242ad92STobias Grosser     NewPointer = getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap,
315369430ffSTobias Grosser                                      GlobalMap, LTS, getLoopForInst(Inst));
3163b11a16aSHongbin Zheng   }
3173b11a16aSHongbin Zheng 
3183b11a16aSHongbin Zheng   isl_map_free(CurrentAccessRelation);
3193b11a16aSHongbin Zheng   isl_map_free(NewAccessRelation);
3203b11a16aSHongbin Zheng   return NewPointer;
3213b11a16aSHongbin Zheng }
3223b11a16aSHongbin Zheng 
3234d96c8d7STobias Grosser Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) {
324369430ffSTobias Grosser   return P->getAnalysis<LoopInfo>().getLoopFor(Inst->getParent());
325369430ffSTobias Grosser }
326369430ffSTobias Grosser 
327e602a076STobias Grosser Value *BlockGenerator::generateScalarLoad(const LoadInst *Load,
328e602a076STobias Grosser                                           ValueMapT &BBMap,
329e602a076STobias Grosser                                           ValueMapT &GlobalMap,
330e602a076STobias Grosser                                           LoopToScevMapT &LTS) {
3313b11a16aSHongbin Zheng   const Value *Pointer = Load->getPointerOperand();
3323b11a16aSHongbin Zheng   const Instruction *Inst = dyn_cast<Instruction>(Load);
3337242ad92STobias Grosser   Value *NewPointer =
3347242ad92STobias Grosser       generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS);
335c14582f2STobias Grosser   Value *ScalarLoad =
336c14582f2STobias Grosser       Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
3373b11a16aSHongbin Zheng   return ScalarLoad;
3383b11a16aSHongbin Zheng }
3393b11a16aSHongbin Zheng 
340e602a076STobias Grosser Value *BlockGenerator::generateScalarStore(const StoreInst *Store,
341e602a076STobias Grosser                                            ValueMapT &BBMap,
342e602a076STobias Grosser                                            ValueMapT &GlobalMap,
343e602a076STobias Grosser                                            LoopToScevMapT &LTS) {
3443b11a16aSHongbin Zheng   const Value *Pointer = Store->getPointerOperand();
345c14582f2STobias Grosser   Value *NewPointer =
3469d10fffaSSebastian Pop       generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS);
347369430ffSTobias Grosser   Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap,
348369430ffSTobias Grosser                                     LTS, getLoopForInst(Store));
3493b11a16aSHongbin Zheng 
3503b11a16aSHongbin Zheng   return Builder.CreateStore(ValueOperand, NewPointer);
3513b11a16aSHongbin Zheng }
3523b11a16aSHongbin Zheng 
353e602a076STobias Grosser void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap,
354e602a076STobias Grosser                                      ValueMapT &GlobalMap,
355e602a076STobias Grosser                                      LoopToScevMapT &LTS) {
3563b11a16aSHongbin Zheng   // Terminator instructions control the control flow. They are explicitly
3573b11a16aSHongbin Zheng   // expressed in the clast and do not need to be copied.
3583b11a16aSHongbin Zheng   if (Inst->isTerminator())
3593b11a16aSHongbin Zheng     return;
3603b11a16aSHongbin Zheng 
361ecfe21b7STobias Grosser   if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
362ecfe21b7STobias Grosser                     &Statement.getParent()->getRegion()))
363e71c6ab5STobias Grosser     return;
364e71c6ab5STobias Grosser 
3653b11a16aSHongbin Zheng   if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
366753d43f9SSebastian Pop     Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS);
3673d94fedfSSebastian Pop     // Compute NewLoad before its insertion in BBMap to make the insertion
3683d94fedfSSebastian Pop     // deterministic.
369753d43f9SSebastian Pop     BBMap[Load] = NewLoad;
3703b11a16aSHongbin Zheng     return;
3713b11a16aSHongbin Zheng   }
3723b11a16aSHongbin Zheng 
3733b11a16aSHongbin Zheng   if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
374753d43f9SSebastian Pop     Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS);
3753d94fedfSSebastian Pop     // Compute NewStore before its insertion in BBMap to make the insertion
3763d94fedfSSebastian Pop     // deterministic.
377753d43f9SSebastian Pop     BBMap[Store] = NewStore;
3783b11a16aSHongbin Zheng     return;
3793b11a16aSHongbin Zheng   }
3803b11a16aSHongbin Zheng 
3819d10fffaSSebastian Pop   copyInstScalar(Inst, BBMap, GlobalMap, LTS);
3823b11a16aSHongbin Zheng }
3833b11a16aSHongbin Zheng 
3849d10fffaSSebastian Pop void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT &LTS) {
3853b11a16aSHongbin Zheng   BasicBlock *BB = Statement.getBasicBlock();
386c14582f2STobias Grosser   BasicBlock *CopyBB =
387c14582f2STobias Grosser       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
3883b11a16aSHongbin Zheng   CopyBB->setName("polly.stmt." + BB->getName());
3893b11a16aSHongbin Zheng   Builder.SetInsertPoint(CopyBB->begin());
3903b11a16aSHongbin Zheng 
3913b11a16aSHongbin Zheng   ValueMapT BBMap;
3923b11a16aSHongbin Zheng 
3933b11a16aSHongbin Zheng   for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
3943b11a16aSHongbin Zheng        ++II)
3959d10fffaSSebastian Pop     copyInstruction(II, BBMap, GlobalMap, LTS);
3963b11a16aSHongbin Zheng }
3973b11a16aSHongbin Zheng 
398e602a076STobias Grosser VectorBlockGenerator::VectorBlockGenerator(IRBuilder<> &B,
399e602a076STobias Grosser                                            VectorValueMapT &GlobalMaps,
400e602a076STobias Grosser                                            std::vector<LoopToScevMapT> &VLTS,
401e602a076STobias Grosser                                            ScopStmt &Stmt,
402e602a076STobias Grosser                                            __isl_keep isl_map *Schedule,
403e602a076STobias Grosser                                            Pass *P)
4049d10fffaSSebastian Pop     : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS),
4059d10fffaSSebastian Pop       Schedule(Schedule) {
4063b11a16aSHongbin Zheng   assert(GlobalMaps.size() > 1 && "Only one vector lane found");
407a00a0291SSebastian Pop   assert(Schedule && "No statement domain provided");
4083b11a16aSHongbin Zheng }
4093b11a16aSHongbin Zheng 
410e602a076STobias Grosser Value *VectorBlockGenerator::getVectorValue(const Value *Old,
411e602a076STobias Grosser                                             ValueMapT &VectorMap,
412e602a076STobias Grosser                                             VectorValueMapT &ScalarMaps,
413e602a076STobias Grosser                                             Loop *L) {
414fe11e287SHongbin Zheng   if (Value *NewValue = VectorMap.lookup(Old))
415fe11e287SHongbin Zheng     return NewValue;
4163b11a16aSHongbin Zheng 
4173b11a16aSHongbin Zheng   int Width = getVectorWidth();
4183b11a16aSHongbin Zheng 
4193b11a16aSHongbin Zheng   Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width));
4203b11a16aSHongbin Zheng 
4213b11a16aSHongbin Zheng   for (int Lane = 0; Lane < Width; Lane++)
422c14582f2STobias Grosser     Vector = Builder.CreateInsertElement(
4237242ad92STobias Grosser         Vector,
424369430ffSTobias Grosser         getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L),
4257242ad92STobias Grosser         Builder.getInt32(Lane));
4263b11a16aSHongbin Zheng 
4273b11a16aSHongbin Zheng   VectorMap[Old] = Vector;
4283b11a16aSHongbin Zheng 
4293b11a16aSHongbin Zheng   return Vector;
4303b11a16aSHongbin Zheng }
4313b11a16aSHongbin Zheng 
4323b11a16aSHongbin Zheng Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) {
4333b11a16aSHongbin Zheng   PointerType *PointerTy = dyn_cast<PointerType>(Val->getType());
4343b11a16aSHongbin Zheng   assert(PointerTy && "PointerType expected");
4353b11a16aSHongbin Zheng 
4363b11a16aSHongbin Zheng   Type *ScalarType = PointerTy->getElementType();
4373b11a16aSHongbin Zheng   VectorType *VectorType = VectorType::get(ScalarType, Width);
4383b11a16aSHongbin Zheng 
4393b11a16aSHongbin Zheng   return PointerType::getUnqual(VectorType);
4403b11a16aSHongbin Zheng }
4413b11a16aSHongbin Zheng 
4423b11a16aSHongbin Zheng Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load,
4433b11a16aSHongbin Zheng                                                    ValueMapT &BBMap) {
4443b11a16aSHongbin Zheng   const Value *Pointer = Load->getPointerOperand();
4453b11a16aSHongbin Zheng   Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth());
446369430ffSTobias Grosser   Value *NewPointer =
447369430ffSTobias Grosser       getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
448c14582f2STobias Grosser   Value *VectorPtr =
449c14582f2STobias Grosser       Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
450c14582f2STobias Grosser   LoadInst *VecLoad =
451c14582f2STobias Grosser       Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full");
4523b11a16aSHongbin Zheng   if (!Aligned)
4533b11a16aSHongbin Zheng     VecLoad->setAlignment(8);
4543b11a16aSHongbin Zheng 
4553b11a16aSHongbin Zheng   return VecLoad;
4563b11a16aSHongbin Zheng }
4573b11a16aSHongbin Zheng 
4583b11a16aSHongbin Zheng Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load,
4593b11a16aSHongbin Zheng                                                     ValueMapT &BBMap) {
4603b11a16aSHongbin Zheng   const Value *Pointer = Load->getPointerOperand();
4613b11a16aSHongbin Zheng   Type *VectorPtrType = getVectorPtrTy(Pointer, 1);
462369430ffSTobias Grosser   Value *NewPointer =
463369430ffSTobias Grosser       getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load));
4643b11a16aSHongbin Zheng   Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType,
4653b11a16aSHongbin Zheng                                            Load->getName() + "_p_vec_p");
466c14582f2STobias Grosser   LoadInst *ScalarLoad =
467c14582f2STobias Grosser       Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one");
4683b11a16aSHongbin Zheng 
4693b11a16aSHongbin Zheng   if (!Aligned)
4703b11a16aSHongbin Zheng     ScalarLoad->setAlignment(8);
4713b11a16aSHongbin Zheng 
472c14582f2STobias Grosser   Constant *SplatVector = Constant::getNullValue(
473c14582f2STobias Grosser       VectorType::get(Builder.getInt32Ty(), getVectorWidth()));
4743b11a16aSHongbin Zheng 
475c14582f2STobias Grosser   Value *VectorLoad = Builder.CreateShuffleVector(
476c14582f2STobias Grosser       ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat");
4773b11a16aSHongbin Zheng   return VectorLoad;
4783b11a16aSHongbin Zheng }
4793b11a16aSHongbin Zheng 
480e602a076STobias Grosser Value *
481e602a076STobias Grosser VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load,
482e602a076STobias Grosser                                                 VectorValueMapT &ScalarMaps) {
4833b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
4843b11a16aSHongbin Zheng   const Value *Pointer = Load->getPointerOperand();
4853b11a16aSHongbin Zheng   VectorType *VectorType = VectorType::get(
4863b11a16aSHongbin Zheng       dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth);
4873b11a16aSHongbin Zheng 
4883b11a16aSHongbin Zheng   Value *Vector = UndefValue::get(VectorType);
4893b11a16aSHongbin Zheng 
4903b11a16aSHongbin Zheng   for (int i = 0; i < VectorWidth; i++) {
491369430ffSTobias Grosser     Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
492369430ffSTobias Grosser                                     VLTS[i], getLoopForInst(Load));
493c14582f2STobias Grosser     Value *ScalarLoad =
494c14582f2STobias Grosser         Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_");
495c14582f2STobias Grosser     Vector = Builder.CreateInsertElement(
496c14582f2STobias Grosser         Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_");
4973b11a16aSHongbin Zheng   }
4983b11a16aSHongbin Zheng 
4993b11a16aSHongbin Zheng   return Vector;
5003b11a16aSHongbin Zheng }
5013b11a16aSHongbin Zheng 
502e602a076STobias Grosser void VectorBlockGenerator::generateLoad(const LoadInst *Load,
503e602a076STobias Grosser                                         ValueMapT &VectorMap,
504e602a076STobias Grosser                                         VectorValueMapT &ScalarMaps) {
50568794217SHongbin Zheng   if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL ||
50668794217SHongbin Zheng       !VectorType::isValidElementType(Load->getType())) {
5073b11a16aSHongbin Zheng     for (int i = 0; i < getVectorWidth(); i++)
508c14582f2STobias Grosser       ScalarMaps[i][Load] =
5099d10fffaSSebastian Pop           generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]);
5103b11a16aSHongbin Zheng     return;
5113b11a16aSHongbin Zheng   }
5123b11a16aSHongbin Zheng 
513b5f24a66SHongbin Zheng   const MemoryAccess &Access = Statement.getAccessFor(Load);
5143b11a16aSHongbin Zheng 
5153b11a16aSHongbin Zheng   Value *NewLoad;
516a00a0291SSebastian Pop   if (Access.isStrideZero(isl_map_copy(Schedule)))
5173b11a16aSHongbin Zheng     NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]);
518a00a0291SSebastian Pop   else if (Access.isStrideOne(isl_map_copy(Schedule)))
5193b11a16aSHongbin Zheng     NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]);
5203b11a16aSHongbin Zheng   else
5213b11a16aSHongbin Zheng     NewLoad = generateUnknownStrideLoad(Load, ScalarMaps);
5223b11a16aSHongbin Zheng 
5233b11a16aSHongbin Zheng   VectorMap[Load] = NewLoad;
5243b11a16aSHongbin Zheng }
5253b11a16aSHongbin Zheng 
5263b11a16aSHongbin Zheng void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst,
5273b11a16aSHongbin Zheng                                          ValueMapT &VectorMap,
5283b11a16aSHongbin Zheng                                          VectorValueMapT &ScalarMaps) {
5293b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
530369430ffSTobias Grosser   Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps,
531369430ffSTobias Grosser                                      getLoopForInst(Inst));
5323b11a16aSHongbin Zheng 
5333b11a16aSHongbin Zheng   assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction");
5343b11a16aSHongbin Zheng 
5353b11a16aSHongbin Zheng   const CastInst *Cast = dyn_cast<CastInst>(Inst);
5363b11a16aSHongbin Zheng   VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth);
5373b11a16aSHongbin Zheng   VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType);
5383b11a16aSHongbin Zheng }
5393b11a16aSHongbin Zheng 
5403b11a16aSHongbin Zheng void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst,
5413b11a16aSHongbin Zheng                                           ValueMapT &VectorMap,
5423b11a16aSHongbin Zheng                                           VectorValueMapT &ScalarMaps) {
543369430ffSTobias Grosser   Loop *L = getLoopForInst(Inst);
5443b11a16aSHongbin Zheng   Value *OpZero = Inst->getOperand(0);
5453b11a16aSHongbin Zheng   Value *OpOne = Inst->getOperand(1);
5463b11a16aSHongbin Zheng 
5473b11a16aSHongbin Zheng   Value *NewOpZero, *NewOpOne;
548369430ffSTobias Grosser   NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L);
549369430ffSTobias Grosser   NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L);
5503b11a16aSHongbin Zheng 
5511bb59b0dSTobias Grosser   Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne,
5523b11a16aSHongbin Zheng                                        Inst->getName() + "p_vec");
5533b11a16aSHongbin Zheng   VectorMap[Inst] = NewInst;
5543b11a16aSHongbin Zheng }
5553b11a16aSHongbin Zheng 
556e602a076STobias Grosser void VectorBlockGenerator::copyStore(const StoreInst *Store,
557e602a076STobias Grosser                                      ValueMapT &VectorMap,
558e602a076STobias Grosser                                      VectorValueMapT &ScalarMaps) {
5593b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
5603b11a16aSHongbin Zheng 
561b5f24a66SHongbin Zheng   const MemoryAccess &Access = Statement.getAccessFor(Store);
5623b11a16aSHongbin Zheng 
5633b11a16aSHongbin Zheng   const Value *Pointer = Store->getPointerOperand();
564369430ffSTobias Grosser   Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap,
565369430ffSTobias Grosser                                  ScalarMaps, getLoopForInst(Store));
5663b11a16aSHongbin Zheng 
567a00a0291SSebastian Pop   if (Access.isStrideOne(isl_map_copy(Schedule))) {
5683b11a16aSHongbin Zheng     Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth);
569369430ffSTobias Grosser     Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0],
570369430ffSTobias Grosser                                     VLTS[0], getLoopForInst(Store));
5713b11a16aSHongbin Zheng 
572c14582f2STobias Grosser     Value *VectorPtr =
573c14582f2STobias Grosser         Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr");
5743b11a16aSHongbin Zheng     StoreInst *Store = Builder.CreateStore(Vector, VectorPtr);
5753b11a16aSHongbin Zheng 
5763b11a16aSHongbin Zheng     if (!Aligned)
5773b11a16aSHongbin Zheng       Store->setAlignment(8);
5783b11a16aSHongbin Zheng   } else {
5793b11a16aSHongbin Zheng     for (unsigned i = 0; i < ScalarMaps.size(); i++) {
5801bb59b0dSTobias Grosser       Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i));
581369430ffSTobias Grosser       Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i],
582369430ffSTobias Grosser                                       VLTS[i], getLoopForInst(Store));
5833b11a16aSHongbin Zheng       Builder.CreateStore(Scalar, NewPointer);
5843b11a16aSHongbin Zheng     }
5853b11a16aSHongbin Zheng   }
5863b11a16aSHongbin Zheng }
5873b11a16aSHongbin Zheng 
5883b11a16aSHongbin Zheng bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst,
5893b11a16aSHongbin Zheng                                              ValueMapT &VectorMap) {
5903b11a16aSHongbin Zheng   for (Instruction::const_op_iterator OI = Inst->op_begin(),
591c14582f2STobias Grosser                                       OE = Inst->op_end();
592c14582f2STobias Grosser        OI != OE; ++OI)
5933b11a16aSHongbin Zheng     if (VectorMap.count(*OI))
5943b11a16aSHongbin Zheng       return true;
5953b11a16aSHongbin Zheng   return false;
5963b11a16aSHongbin Zheng }
5973b11a16aSHongbin Zheng 
5983b11a16aSHongbin Zheng bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst,
5993b11a16aSHongbin Zheng                                                ValueMapT &VectorMap,
6003b11a16aSHongbin Zheng                                                VectorValueMapT &ScalarMaps) {
6013b11a16aSHongbin Zheng   bool HasVectorOperand = false;
6023b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
6033b11a16aSHongbin Zheng 
6043b11a16aSHongbin Zheng   for (Instruction::const_op_iterator OI = Inst->op_begin(),
605c14582f2STobias Grosser                                       OE = Inst->op_end();
606c14582f2STobias Grosser        OI != OE; ++OI) {
6073b11a16aSHongbin Zheng     ValueMapT::iterator VecOp = VectorMap.find(*OI);
6083b11a16aSHongbin Zheng 
6093b11a16aSHongbin Zheng     if (VecOp == VectorMap.end())
6103b11a16aSHongbin Zheng       continue;
6113b11a16aSHongbin Zheng 
6123b11a16aSHongbin Zheng     HasVectorOperand = true;
6133b11a16aSHongbin Zheng     Value *NewVector = VecOp->second;
6143b11a16aSHongbin Zheng 
6153b11a16aSHongbin Zheng     for (int i = 0; i < VectorWidth; ++i) {
6163b11a16aSHongbin Zheng       ValueMapT &SM = ScalarMaps[i];
6173b11a16aSHongbin Zheng 
6183b11a16aSHongbin Zheng       // If there is one scalar extracted, all scalar elements should have
6193b11a16aSHongbin Zheng       // already been extracted by the code here. So no need to check for the
6203b11a16aSHongbin Zheng       // existance of all of them.
6213b11a16aSHongbin Zheng       if (SM.count(*OI))
6223b11a16aSHongbin Zheng         break;
6233b11a16aSHongbin Zheng 
6243b11a16aSHongbin Zheng       SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i));
6253b11a16aSHongbin Zheng     }
6263b11a16aSHongbin Zheng   }
6273b11a16aSHongbin Zheng 
6283b11a16aSHongbin Zheng   return HasVectorOperand;
6293b11a16aSHongbin Zheng }
6303b11a16aSHongbin Zheng 
6313b11a16aSHongbin Zheng void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst,
6323b11a16aSHongbin Zheng                                               ValueMapT &VectorMap,
6333b11a16aSHongbin Zheng                                               VectorValueMapT &ScalarMaps) {
6343b11a16aSHongbin Zheng   bool HasVectorOperand;
6353b11a16aSHongbin Zheng   int VectorWidth = getVectorWidth();
6363b11a16aSHongbin Zheng 
6373b11a16aSHongbin Zheng   HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps);
6383b11a16aSHongbin Zheng 
6393b11a16aSHongbin Zheng   for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++)
6409d10fffaSSebastian Pop     copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane],
6419d10fffaSSebastian Pop                    VLTS[VectorLane]);
6423b11a16aSHongbin Zheng 
6433b11a16aSHongbin Zheng   if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand)
6443b11a16aSHongbin Zheng     return;
6453b11a16aSHongbin Zheng 
6463b11a16aSHongbin Zheng   // Make the result available as vector value.
6473b11a16aSHongbin Zheng   VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth);
6483b11a16aSHongbin Zheng   Value *Vector = UndefValue::get(VectorType);
6493b11a16aSHongbin Zheng 
6503b11a16aSHongbin Zheng   for (int i = 0; i < VectorWidth; i++)
6513b11a16aSHongbin Zheng     Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst],
6523b11a16aSHongbin Zheng                                          Builder.getInt32(i));
6533b11a16aSHongbin Zheng 
6543b11a16aSHongbin Zheng   VectorMap[Inst] = Vector;
6553b11a16aSHongbin Zheng }
6563b11a16aSHongbin Zheng 
657c14582f2STobias Grosser int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); }
6583b11a16aSHongbin Zheng 
6593b11a16aSHongbin Zheng void VectorBlockGenerator::copyInstruction(const Instruction *Inst,
6603b11a16aSHongbin Zheng                                            ValueMapT &VectorMap,
6613b11a16aSHongbin Zheng                                            VectorValueMapT &ScalarMaps) {
6623b11a16aSHongbin Zheng   // Terminator instructions control the control flow. They are explicitly
6633b11a16aSHongbin Zheng   // expressed in the clast and do not need to be copied.
6643b11a16aSHongbin Zheng   if (Inst->isTerminator())
6653b11a16aSHongbin Zheng     return;
6663b11a16aSHongbin Zheng 
667ecfe21b7STobias Grosser   if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE,
668ecfe21b7STobias Grosser                     &Statement.getParent()->getRegion()))
669e71c6ab5STobias Grosser     return;
670e71c6ab5STobias Grosser 
6713b11a16aSHongbin Zheng   if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) {
6723b11a16aSHongbin Zheng     generateLoad(Load, VectorMap, ScalarMaps);
6733b11a16aSHongbin Zheng     return;
6743b11a16aSHongbin Zheng   }
6753b11a16aSHongbin Zheng 
6763b11a16aSHongbin Zheng   if (hasVectorOperands(Inst, VectorMap)) {
6773b11a16aSHongbin Zheng     if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) {
6783b11a16aSHongbin Zheng       copyStore(Store, VectorMap, ScalarMaps);
6793b11a16aSHongbin Zheng       return;
6803b11a16aSHongbin Zheng     }
6813b11a16aSHongbin Zheng 
6823b11a16aSHongbin Zheng     if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) {
6833b11a16aSHongbin Zheng       copyUnaryInst(Unary, VectorMap, ScalarMaps);
6843b11a16aSHongbin Zheng       return;
6853b11a16aSHongbin Zheng     }
6863b11a16aSHongbin Zheng 
6873b11a16aSHongbin Zheng     if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) {
6883b11a16aSHongbin Zheng       copyBinaryInst(Binary, VectorMap, ScalarMaps);
6893b11a16aSHongbin Zheng       return;
6903b11a16aSHongbin Zheng     }
6913b11a16aSHongbin Zheng 
6923b11a16aSHongbin Zheng     // Falltrough: We generate scalar instructions, if we don't know how to
6933b11a16aSHongbin Zheng     // generate vector code.
6943b11a16aSHongbin Zheng   }
6953b11a16aSHongbin Zheng 
6963b11a16aSHongbin Zheng   copyInstScalarized(Inst, VectorMap, ScalarMaps);
6973b11a16aSHongbin Zheng }
6983b11a16aSHongbin Zheng 
6993b11a16aSHongbin Zheng void VectorBlockGenerator::copyBB() {
7003b11a16aSHongbin Zheng   BasicBlock *BB = Statement.getBasicBlock();
701c14582f2STobias Grosser   BasicBlock *CopyBB =
702c14582f2STobias Grosser       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P);
7033b11a16aSHongbin Zheng   CopyBB->setName("polly.stmt." + BB->getName());
7043b11a16aSHongbin Zheng   Builder.SetInsertPoint(CopyBB->begin());
7053b11a16aSHongbin Zheng 
7063b11a16aSHongbin Zheng   // Create two maps that store the mapping from the original instructions of
7073b11a16aSHongbin Zheng   // the old basic block to their copies in the new basic block. Those maps
7083b11a16aSHongbin Zheng   // are basic block local.
7093b11a16aSHongbin Zheng   //
7103b11a16aSHongbin Zheng   // As vector code generation is supported there is one map for scalar values
7113b11a16aSHongbin Zheng   // and one for vector values.
7123b11a16aSHongbin Zheng   //
7133b11a16aSHongbin Zheng   // In case we just do scalar code generation, the vectorMap is not used and
7143b11a16aSHongbin Zheng   // the scalarMap has just one dimension, which contains the mapping.
7153b11a16aSHongbin Zheng   //
7163b11a16aSHongbin Zheng   // In case vector code generation is done, an instruction may either appear
7173b11a16aSHongbin Zheng   // in the vector map once (as it is calculating >vectorwidth< values at a
7183b11a16aSHongbin Zheng   // time. Or (if the values are calculated using scalar operations), it
7193b11a16aSHongbin Zheng   // appears once in every dimension of the scalarMap.
7203b11a16aSHongbin Zheng   VectorValueMapT ScalarBlockMap(getVectorWidth());
7213b11a16aSHongbin Zheng   ValueMapT VectorBlockMap;
7223b11a16aSHongbin Zheng 
723c14582f2STobias Grosser   for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE;
724c14582f2STobias Grosser        ++II)
7253b11a16aSHongbin Zheng     copyInstruction(II, VectorBlockMap, ScalarBlockMap);
7263b11a16aSHongbin Zheng }
727