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" 1768794217SHongbin Zheng #include "polly/CodeGen/CodeGeneration.h" 188a846610SHongbin Zheng #include "polly/CodeGen/BlockGenerators.h" 193b11a16aSHongbin Zheng #include "polly/Support/GICHelper.h" 203b11a16aSHongbin Zheng 21e71c6ab5STobias Grosser #include "llvm/Analysis/LoopInfo.h" 22e71c6ab5STobias Grosser #include "llvm/Analysis/ScalarEvolution.h" 23e71c6ab5STobias Grosser #include "llvm/Analysis/ScalarEvolutionExpander.h" 243b11a16aSHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h" 253b11a16aSHongbin Zheng #include "llvm/Support/CommandLine.h" 263b11a16aSHongbin Zheng 273b11a16aSHongbin Zheng #include "isl/aff.h" 283b11a16aSHongbin Zheng #include "isl/set.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"), 363b11a16aSHongbin Zheng cl::init(false), cl::ZeroOrMore); 373b11a16aSHongbin Zheng 383b11a16aSHongbin Zheng static cl::opt<bool> 39c14582f2STobias Grosser SCEVCodegen("polly-codegen-scev", cl::desc("Use SCEV based code generation."), 40c14582f2STobias Grosser cl::Hidden, cl::init(false), cl::ZeroOrMore); 41e71c6ab5STobias Grosser 423b11a16aSHongbin Zheng // Helper class to generate memory location. 433b11a16aSHongbin Zheng namespace { 443b11a16aSHongbin Zheng class IslGenerator { 453b11a16aSHongbin Zheng public: 463b11a16aSHongbin Zheng IslGenerator(IRBuilder<> &Builder, std::vector<Value *> &IVS) : 473b11a16aSHongbin Zheng Builder(Builder), IVS(IVS) {} 483b11a16aSHongbin Zheng Value *generateIslInt(__isl_take isl_int Int); 493b11a16aSHongbin Zheng Value *generateIslAff(__isl_take isl_aff *Aff); 503b11a16aSHongbin Zheng Value *generateIslPwAff(__isl_take isl_pw_aff *PwAff); 513b11a16aSHongbin Zheng 523b11a16aSHongbin Zheng private: 533b11a16aSHongbin Zheng typedef struct { 543b11a16aSHongbin Zheng Value *Result; 553b11a16aSHongbin Zheng class IslGenerator *Generator; 563b11a16aSHongbin Zheng } IslGenInfo; 573b11a16aSHongbin Zheng 583b11a16aSHongbin Zheng IRBuilder<> &Builder; 593b11a16aSHongbin Zheng std::vector<Value *> &IVS; 601bb59b0dSTobias Grosser static int mergeIslAffValues(__isl_take isl_set *Set, __isl_take isl_aff *Aff, 611bb59b0dSTobias Grosser void *User); 623b11a16aSHongbin Zheng }; 633b11a16aSHongbin Zheng } 643b11a16aSHongbin Zheng 653b11a16aSHongbin Zheng Value *IslGenerator::generateIslInt(isl_int Int) { 663b11a16aSHongbin Zheng mpz_t IntMPZ; 673b11a16aSHongbin Zheng mpz_init(IntMPZ); 683b11a16aSHongbin Zheng isl_int_get_gmp(Int, IntMPZ); 693b11a16aSHongbin Zheng Value *IntValue = Builder.getInt(APInt_from_MPZ(IntMPZ)); 703b11a16aSHongbin Zheng mpz_clear(IntMPZ); 713b11a16aSHongbin Zheng return IntValue; 723b11a16aSHongbin Zheng } 733b11a16aSHongbin Zheng 743b11a16aSHongbin Zheng Value *IslGenerator::generateIslAff(__isl_take isl_aff *Aff) { 753b11a16aSHongbin Zheng Value *Result; 763b11a16aSHongbin Zheng Value *ConstValue; 773b11a16aSHongbin Zheng isl_int ConstIsl; 783b11a16aSHongbin Zheng 793b11a16aSHongbin Zheng isl_int_init(ConstIsl); 803b11a16aSHongbin Zheng isl_aff_get_constant(Aff, &ConstIsl); 813b11a16aSHongbin Zheng ConstValue = generateIslInt(ConstIsl); 823b11a16aSHongbin Zheng Type *Ty = Builder.getInt64Ty(); 833b11a16aSHongbin Zheng 843b11a16aSHongbin Zheng // FIXME: We should give the constant and coefficients the right type. Here 853b11a16aSHongbin Zheng // we force it into i64. 863b11a16aSHongbin Zheng Result = Builder.CreateSExtOrBitCast(ConstValue, Ty); 873b11a16aSHongbin Zheng 883b11a16aSHongbin Zheng unsigned int NbInputDims = isl_aff_dim(Aff, isl_dim_in); 893b11a16aSHongbin Zheng 903b11a16aSHongbin Zheng assert((IVS.size() == NbInputDims) && "The Dimension of Induction Variables" 913b11a16aSHongbin Zheng "must match the dimension of the affine space."); 923b11a16aSHongbin Zheng 933b11a16aSHongbin Zheng isl_int CoefficientIsl; 943b11a16aSHongbin Zheng isl_int_init(CoefficientIsl); 953b11a16aSHongbin Zheng 963b11a16aSHongbin Zheng for (unsigned int i = 0; i < NbInputDims; ++i) { 973b11a16aSHongbin Zheng Value *CoefficientValue; 983b11a16aSHongbin Zheng isl_aff_get_coefficient(Aff, isl_dim_in, i, &CoefficientIsl); 993b11a16aSHongbin Zheng 1003b11a16aSHongbin Zheng if (isl_int_is_zero(CoefficientIsl)) 1013b11a16aSHongbin Zheng continue; 1023b11a16aSHongbin Zheng 1033b11a16aSHongbin Zheng CoefficientValue = generateIslInt(CoefficientIsl); 1043b11a16aSHongbin Zheng CoefficientValue = Builder.CreateIntCast(CoefficientValue, Ty, true); 1053b11a16aSHongbin Zheng Value *IV = Builder.CreateIntCast(IVS[i], Ty, true); 1063b11a16aSHongbin Zheng Value *PAdd = Builder.CreateMul(CoefficientValue, IV, "p_mul_coeff"); 1073b11a16aSHongbin Zheng Result = Builder.CreateAdd(Result, PAdd, "p_sum_coeff"); 1083b11a16aSHongbin Zheng } 1093b11a16aSHongbin Zheng 1103b11a16aSHongbin Zheng isl_int_clear(CoefficientIsl); 1113b11a16aSHongbin Zheng isl_int_clear(ConstIsl); 1123b11a16aSHongbin Zheng isl_aff_free(Aff); 1133b11a16aSHongbin Zheng 1143b11a16aSHongbin Zheng return Result; 1153b11a16aSHongbin Zheng } 1163b11a16aSHongbin Zheng 1173b11a16aSHongbin Zheng int IslGenerator::mergeIslAffValues(__isl_take isl_set *Set, 1183b11a16aSHongbin Zheng __isl_take isl_aff *Aff, void *User) { 1193b11a16aSHongbin Zheng IslGenInfo *GenInfo = (IslGenInfo *)User; 1203b11a16aSHongbin Zheng 1213b11a16aSHongbin Zheng assert((GenInfo->Result == NULL) && "Result is already set." 1223b11a16aSHongbin Zheng "Currently only single isl_aff is supported"); 1233b11a16aSHongbin Zheng assert(isl_set_plain_is_universe(Set) 1243b11a16aSHongbin Zheng && "Code generation failed because the set is not universe"); 1253b11a16aSHongbin Zheng 1263b11a16aSHongbin Zheng GenInfo->Result = GenInfo->Generator->generateIslAff(Aff); 1273b11a16aSHongbin Zheng 1283b11a16aSHongbin Zheng isl_set_free(Set); 1293b11a16aSHongbin Zheng return 0; 1303b11a16aSHongbin Zheng } 1313b11a16aSHongbin Zheng 1323b11a16aSHongbin Zheng Value *IslGenerator::generateIslPwAff(__isl_take isl_pw_aff *PwAff) { 1333b11a16aSHongbin Zheng IslGenInfo User; 1343b11a16aSHongbin Zheng User.Result = NULL; 1353b11a16aSHongbin Zheng User.Generator = this; 1363b11a16aSHongbin Zheng isl_pw_aff_foreach_piece(PwAff, mergeIslAffValues, &User); 1373b11a16aSHongbin Zheng assert(User.Result && "Code generation for isl_pw_aff failed"); 1383b11a16aSHongbin Zheng 1393b11a16aSHongbin Zheng isl_pw_aff_free(PwAff); 1403b11a16aSHongbin Zheng return User.Result; 1413b11a16aSHongbin Zheng } 1423b11a16aSHongbin Zheng 1433b11a16aSHongbin Zheng 1443b11a16aSHongbin Zheng BlockGenerator::BlockGenerator(IRBuilder<> &B, ScopStmt &Stmt, Pass *P): 145e71c6ab5STobias Grosser Builder(B), Statement(Stmt), P(P), SE(P->getAnalysis<ScalarEvolution>()) {} 146e71c6ab5STobias Grosser 147e71c6ab5STobias Grosser bool BlockGenerator::isSCEVIgnore(const Instruction *Inst) { 148e71c6ab5STobias Grosser if (SCEVCodegen && SE.isSCEVable(Inst->getType())) 149e71c6ab5STobias Grosser if (const SCEV *Scev = SE.getSCEV(const_cast<Instruction*>(Inst))) 150e71c6ab5STobias Grosser if (!isa<SCEVCouldNotCompute>(Scev)) { 151e71c6ab5STobias Grosser if (const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Scev)) { 152e71c6ab5STobias Grosser if (Unknown->getValue() != Inst) 153e71c6ab5STobias Grosser return true; 154e71c6ab5STobias Grosser } else { 155e71c6ab5STobias Grosser return true; 156e71c6ab5STobias Grosser } 157e71c6ab5STobias Grosser } 158e71c6ab5STobias Grosser 159e71c6ab5STobias Grosser return false; 160e71c6ab5STobias Grosser } 1613b11a16aSHongbin Zheng 1623b11a16aSHongbin Zheng Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap, 1639d10fffaSSebastian Pop ValueMapT &GlobalMap, LoopToScevMapT <S) { 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 1693b11a16aSHongbin Zheng if (GlobalMap.count(Old)) { 1703b11a16aSHongbin Zheng Value *New = GlobalMap[Old]; 1713b11a16aSHongbin Zheng 172c14582f2STobias Grosser if (Old->getType()->getScalarSizeInBits() < 173c14582f2STobias Grosser New->getType()->getScalarSizeInBits()) 1743b11a16aSHongbin Zheng New = Builder.CreateTruncOrBitCast(New, Old->getType()); 1753b11a16aSHongbin Zheng 1763b11a16aSHongbin Zheng return New; 1773b11a16aSHongbin Zheng } 1783b11a16aSHongbin Zheng 1793b11a16aSHongbin Zheng if (BBMap.count(Old)) { 1803b11a16aSHongbin Zheng return BBMap[Old]; 1813b11a16aSHongbin Zheng } 1823b11a16aSHongbin Zheng 183e71c6ab5STobias Grosser if (SCEVCodegen && SE.isSCEVable(Old->getType())) 184e71c6ab5STobias Grosser if (const SCEV *Scev = SE.getSCEV(const_cast<Value *>(Old))) 185e71c6ab5STobias Grosser if (!isa<SCEVCouldNotCompute>(Scev)) { 186637b23dcSSebastian Pop const SCEV *NewScev = apply(Scev, LTS, SE); 187637b23dcSSebastian Pop ValueToValueMap VTV; 188637b23dcSSebastian Pop VTV.insert(BBMap.begin(), BBMap.end()); 189637b23dcSSebastian Pop VTV.insert(GlobalMap.begin(), GlobalMap.end()); 190*47d4ee3eSSebastian Pop NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV); 191e71c6ab5STobias Grosser SCEVExpander Expander(SE, "polly"); 192e71c6ab5STobias Grosser Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(), 193e71c6ab5STobias Grosser Builder.GetInsertPoint()); 194e71c6ab5STobias Grosser 195e71c6ab5STobias Grosser BBMap[Old] = Expanded; 196e71c6ab5STobias Grosser return Expanded; 197e71c6ab5STobias Grosser } 198e71c6ab5STobias Grosser 1993b11a16aSHongbin Zheng // 'Old' is within the original SCoP, but was not rewritten. 2003b11a16aSHongbin Zheng // 2013b11a16aSHongbin Zheng // Such values appear, if they only calculate information already available in 2023b11a16aSHongbin Zheng // the polyhedral description (e.g. an induction variable increment). They 2033b11a16aSHongbin Zheng // can be safely ignored. 2043b11a16aSHongbin Zheng if (const Instruction *Inst = dyn_cast<Instruction>(Old)) 2053b11a16aSHongbin Zheng if (Statement.getParent()->getRegion().contains(Inst->getParent())) 2063b11a16aSHongbin Zheng return NULL; 2073b11a16aSHongbin Zheng 2083b11a16aSHongbin Zheng // Everything else is probably a scop-constant value defined as global, 2093b11a16aSHongbin Zheng // function parameter or an instruction not within the scop. 2103b11a16aSHongbin Zheng return const_cast<Value *>(Old); 2113b11a16aSHongbin Zheng } 2123b11a16aSHongbin Zheng 2133b11a16aSHongbin Zheng void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap, 2149d10fffaSSebastian Pop ValueMapT &GlobalMap, LoopToScevMapT <S) { 2153b11a16aSHongbin Zheng Instruction *NewInst = Inst->clone(); 2163b11a16aSHongbin Zheng 2173b11a16aSHongbin Zheng // Replace old operands with the new ones. 2183b11a16aSHongbin Zheng for (Instruction::const_op_iterator OI = Inst->op_begin(), 219c14582f2STobias Grosser OE = Inst->op_end(); 220c14582f2STobias Grosser OI != OE; ++OI) { 2213b11a16aSHongbin Zheng Value *OldOperand = *OI; 2229d10fffaSSebastian Pop Value *NewOperand = getNewValue(OldOperand, BBMap, GlobalMap, LTS); 2233b11a16aSHongbin Zheng 2243b11a16aSHongbin Zheng if (!NewOperand) { 225c14582f2STobias Grosser assert(!isa<StoreInst>(NewInst) && 226c14582f2STobias Grosser "Store instructions are always needed!"); 2273b11a16aSHongbin Zheng delete NewInst; 2283b11a16aSHongbin Zheng return; 2293b11a16aSHongbin Zheng } 2303b11a16aSHongbin Zheng 2313b11a16aSHongbin Zheng NewInst->replaceUsesOfWith(OldOperand, NewOperand); 2323b11a16aSHongbin Zheng } 2333b11a16aSHongbin Zheng 2343b11a16aSHongbin Zheng Builder.Insert(NewInst); 2353b11a16aSHongbin Zheng BBMap[Inst] = NewInst; 2363b11a16aSHongbin Zheng 2373b11a16aSHongbin Zheng if (!NewInst->getType()->isVoidTy()) 2383b11a16aSHongbin Zheng NewInst->setName("p_" + Inst->getName()); 2393b11a16aSHongbin Zheng } 2403b11a16aSHongbin Zheng 2413b11a16aSHongbin Zheng std::vector<Value *> BlockGenerator::getMemoryAccessIndex( 242c14582f2STobias Grosser __isl_keep isl_map *AccessRelation, Value *BaseAddress, ValueMapT &BBMap, 2439d10fffaSSebastian Pop ValueMapT &GlobalMap, LoopToScevMapT <S) { 2443b11a16aSHongbin Zheng 245ae2d83ecSTobias Grosser assert((isl_map_dim(AccessRelation, isl_dim_out) == 1) && 246ae2d83ecSTobias Grosser "Only single dimensional access functions supported"); 2473b11a16aSHongbin Zheng 2483b11a16aSHongbin Zheng std::vector<Value *> IVS; 2493b11a16aSHongbin Zheng for (unsigned i = 0; i < Statement.getNumIterators(); ++i) { 2503b11a16aSHongbin Zheng const Value *OriginalIV = Statement.getInductionVariableForDimension(i); 2519d10fffaSSebastian Pop Value *NewIV = getNewValue(OriginalIV, BBMap, GlobalMap, LTS); 2523b11a16aSHongbin Zheng IVS.push_back(NewIV); 2533b11a16aSHongbin Zheng } 2543b11a16aSHongbin Zheng 2553b11a16aSHongbin Zheng isl_pw_aff *PwAff = isl_map_dim_max(isl_map_copy(AccessRelation), 0); 2563b11a16aSHongbin Zheng IslGenerator IslGen(Builder, IVS); 2573b11a16aSHongbin Zheng Value *OffsetValue = IslGen.generateIslPwAff(PwAff); 2583b11a16aSHongbin Zheng 2593b11a16aSHongbin Zheng Type *Ty = Builder.getInt64Ty(); 2603b11a16aSHongbin Zheng OffsetValue = Builder.CreateIntCast(OffsetValue, Ty, true); 2613b11a16aSHongbin Zheng 2623b11a16aSHongbin Zheng std::vector<Value *> IndexArray; 2633b11a16aSHongbin Zheng Value *NullValue = Constant::getNullValue(Ty); 2643b11a16aSHongbin Zheng IndexArray.push_back(NullValue); 2653b11a16aSHongbin Zheng IndexArray.push_back(OffsetValue); 2663b11a16aSHongbin Zheng return IndexArray; 2673b11a16aSHongbin Zheng } 2683b11a16aSHongbin Zheng 2693b11a16aSHongbin Zheng Value *BlockGenerator::getNewAccessOperand( 270c14582f2STobias Grosser __isl_keep isl_map *NewAccessRelation, Value *BaseAddress, ValueMapT &BBMap, 2719d10fffaSSebastian Pop ValueMapT &GlobalMap, LoopToScevMapT <S) { 272c14582f2STobias Grosser std::vector<Value *> IndexArray = 2739d10fffaSSebastian Pop getMemoryAccessIndex(NewAccessRelation, BaseAddress, BBMap, GlobalMap, 2749d10fffaSSebastian Pop LTS); 275c14582f2STobias Grosser Value *NewOperand = 276c14582f2STobias Grosser Builder.CreateGEP(BaseAddress, IndexArray, "p_newarrayidx_"); 2773b11a16aSHongbin Zheng return NewOperand; 2783b11a16aSHongbin Zheng } 2793b11a16aSHongbin Zheng 280c14582f2STobias Grosser Value *BlockGenerator::generateLocationAccessed( 281c14582f2STobias Grosser const Instruction *Inst, const Value *Pointer, ValueMapT &BBMap, 2829d10fffaSSebastian Pop ValueMapT &GlobalMap, LoopToScevMapT <S) { 2833b11a16aSHongbin Zheng MemoryAccess &Access = Statement.getAccessFor(Inst); 2843b11a16aSHongbin Zheng isl_map *CurrentAccessRelation = Access.getAccessRelation(); 2853b11a16aSHongbin Zheng isl_map *NewAccessRelation = Access.getNewAccessRelation(); 2863b11a16aSHongbin Zheng 287ae2d83ecSTobias Grosser assert(isl_map_has_equal_space(CurrentAccessRelation, NewAccessRelation) && 288ae2d83ecSTobias Grosser "Current and new access function use different spaces"); 2893b11a16aSHongbin Zheng 2903b11a16aSHongbin Zheng Value *NewPointer; 2913b11a16aSHongbin Zheng 2923b11a16aSHongbin Zheng if (!NewAccessRelation) { 2939d10fffaSSebastian Pop NewPointer = getNewValue(Pointer, BBMap, GlobalMap, LTS); 2943b11a16aSHongbin Zheng } else { 2953b11a16aSHongbin Zheng Value *BaseAddress = const_cast<Value *>(Access.getBaseAddr()); 296c14582f2STobias Grosser NewPointer = 2979d10fffaSSebastian Pop getNewAccessOperand(NewAccessRelation, BaseAddress, BBMap, GlobalMap, 2989d10fffaSSebastian Pop LTS); 2993b11a16aSHongbin Zheng } 3003b11a16aSHongbin Zheng 3013b11a16aSHongbin Zheng isl_map_free(CurrentAccessRelation); 3023b11a16aSHongbin Zheng isl_map_free(NewAccessRelation); 3033b11a16aSHongbin Zheng return NewPointer; 3043b11a16aSHongbin Zheng } 3053b11a16aSHongbin Zheng 306c14582f2STobias Grosser Value *BlockGenerator::generateScalarLoad( 3079d10fffaSSebastian Pop const LoadInst *Load, ValueMapT &BBMap, ValueMapT &GlobalMap, 3089d10fffaSSebastian Pop LoopToScevMapT <S) { 3093b11a16aSHongbin Zheng const Value *Pointer = Load->getPointerOperand(); 3103b11a16aSHongbin Zheng const Instruction *Inst = dyn_cast<Instruction>(Load); 3119d10fffaSSebastian Pop Value *NewPointer = generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, 3129d10fffaSSebastian Pop LTS); 313c14582f2STobias Grosser Value *ScalarLoad = 314c14582f2STobias Grosser Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); 3153b11a16aSHongbin Zheng return ScalarLoad; 3163b11a16aSHongbin Zheng } 3173b11a16aSHongbin Zheng 318c14582f2STobias Grosser Value *BlockGenerator::generateScalarStore( 3199d10fffaSSebastian Pop const StoreInst *Store, ValueMapT &BBMap, ValueMapT &GlobalMap, 3209d10fffaSSebastian Pop LoopToScevMapT <S) { 3213b11a16aSHongbin Zheng const Value *Pointer = Store->getPointerOperand(); 322c14582f2STobias Grosser Value *NewPointer = 3239d10fffaSSebastian Pop generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS); 3249d10fffaSSebastian Pop Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap, 3259d10fffaSSebastian Pop LTS); 3263b11a16aSHongbin Zheng 3273b11a16aSHongbin Zheng return Builder.CreateStore(ValueOperand, NewPointer); 3283b11a16aSHongbin Zheng } 3293b11a16aSHongbin Zheng 3301bb59b0dSTobias Grosser void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap, 3319d10fffaSSebastian Pop ValueMapT &GlobalMap, 3329d10fffaSSebastian Pop LoopToScevMapT <S) { 3333b11a16aSHongbin Zheng // Terminator instructions control the control flow. They are explicitly 3343b11a16aSHongbin Zheng // expressed in the clast and do not need to be copied. 3353b11a16aSHongbin Zheng if (Inst->isTerminator()) 3363b11a16aSHongbin Zheng return; 3373b11a16aSHongbin Zheng 338e71c6ab5STobias Grosser if (isSCEVIgnore(Inst)) 339e71c6ab5STobias Grosser return; 340e71c6ab5STobias Grosser 3413b11a16aSHongbin Zheng if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { 3429d10fffaSSebastian Pop BBMap[Load] = generateScalarLoad(Load, BBMap, GlobalMap, LTS); 3433b11a16aSHongbin Zheng return; 3443b11a16aSHongbin Zheng } 3453b11a16aSHongbin Zheng 3463b11a16aSHongbin Zheng if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { 3479d10fffaSSebastian Pop BBMap[Store] = generateScalarStore(Store, BBMap, GlobalMap, LTS); 3483b11a16aSHongbin Zheng return; 3493b11a16aSHongbin Zheng } 3503b11a16aSHongbin Zheng 3519d10fffaSSebastian Pop copyInstScalar(Inst, BBMap, GlobalMap, LTS); 3523b11a16aSHongbin Zheng } 3533b11a16aSHongbin Zheng 3549d10fffaSSebastian Pop void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT <S) { 3553b11a16aSHongbin Zheng BasicBlock *BB = Statement.getBasicBlock(); 356c14582f2STobias Grosser BasicBlock *CopyBB = 357c14582f2STobias Grosser SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); 3583b11a16aSHongbin Zheng CopyBB->setName("polly.stmt." + BB->getName()); 3593b11a16aSHongbin Zheng Builder.SetInsertPoint(CopyBB->begin()); 3603b11a16aSHongbin Zheng 3613b11a16aSHongbin Zheng ValueMapT BBMap; 3623b11a16aSHongbin Zheng 3633b11a16aSHongbin Zheng for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE; 3643b11a16aSHongbin Zheng ++II) 3659d10fffaSSebastian Pop copyInstruction(II, BBMap, GlobalMap, LTS); 3663b11a16aSHongbin Zheng } 3673b11a16aSHongbin Zheng 368c14582f2STobias Grosser VectorBlockGenerator::VectorBlockGenerator( 3699d10fffaSSebastian Pop IRBuilder<> &B, VectorValueMapT &GlobalMaps, std::vector<LoopToScevMapT> &VLTS, 3709d10fffaSSebastian Pop ScopStmt &Stmt, __isl_keep isl_map *Schedule, Pass *P) 3719d10fffaSSebastian Pop : BlockGenerator(B, Stmt, P), GlobalMaps(GlobalMaps), VLTS(VLTS), 3729d10fffaSSebastian Pop Schedule(Schedule) { 3733b11a16aSHongbin Zheng assert(GlobalMaps.size() > 1 && "Only one vector lane found"); 374a00a0291SSebastian Pop assert(Schedule && "No statement domain provided"); 3753b11a16aSHongbin Zheng } 3763b11a16aSHongbin Zheng 377c14582f2STobias Grosser Value *VectorBlockGenerator::getVectorValue( 378c14582f2STobias Grosser const Value *Old, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) { 3793b11a16aSHongbin Zheng if (VectorMap.count(Old)) 3803b11a16aSHongbin Zheng return VectorMap[Old]; 3813b11a16aSHongbin Zheng 3823b11a16aSHongbin Zheng int Width = getVectorWidth(); 3833b11a16aSHongbin Zheng 3843b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); 3853b11a16aSHongbin Zheng 3863b11a16aSHongbin Zheng for (int Lane = 0; Lane < Width; Lane++) 387c14582f2STobias Grosser Vector = Builder.CreateInsertElement( 3889d10fffaSSebastian Pop Vector, getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], 3899d10fffaSSebastian Pop VLTS[Lane]), Builder.getInt32(Lane)); 3903b11a16aSHongbin Zheng 3913b11a16aSHongbin Zheng VectorMap[Old] = Vector; 3923b11a16aSHongbin Zheng 3933b11a16aSHongbin Zheng return Vector; 3943b11a16aSHongbin Zheng } 3953b11a16aSHongbin Zheng 3963b11a16aSHongbin Zheng Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { 3973b11a16aSHongbin Zheng PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); 3983b11a16aSHongbin Zheng assert(PointerTy && "PointerType expected"); 3993b11a16aSHongbin Zheng 4003b11a16aSHongbin Zheng Type *ScalarType = PointerTy->getElementType(); 4013b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get(ScalarType, Width); 4023b11a16aSHongbin Zheng 4033b11a16aSHongbin Zheng return PointerType::getUnqual(VectorType); 4043b11a16aSHongbin Zheng } 4053b11a16aSHongbin Zheng 4063b11a16aSHongbin Zheng Value *VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load, 4073b11a16aSHongbin Zheng ValueMapT &BBMap) { 4083b11a16aSHongbin Zheng const Value *Pointer = Load->getPointerOperand(); 4093b11a16aSHongbin Zheng Type *VectorPtrType = getVectorPtrTy(Pointer, getVectorWidth()); 4109d10fffaSSebastian Pop Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]); 411c14582f2STobias Grosser Value *VectorPtr = 412c14582f2STobias Grosser Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); 413c14582f2STobias Grosser LoadInst *VecLoad = 414c14582f2STobias Grosser Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); 4153b11a16aSHongbin Zheng if (!Aligned) 4163b11a16aSHongbin Zheng VecLoad->setAlignment(8); 4173b11a16aSHongbin Zheng 4183b11a16aSHongbin Zheng return VecLoad; 4193b11a16aSHongbin Zheng } 4203b11a16aSHongbin Zheng 4213b11a16aSHongbin Zheng Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load, 4223b11a16aSHongbin Zheng ValueMapT &BBMap) { 4233b11a16aSHongbin Zheng const Value *Pointer = Load->getPointerOperand(); 4243b11a16aSHongbin Zheng Type *VectorPtrType = getVectorPtrTy(Pointer, 1); 4259d10fffaSSebastian Pop Value *NewPointer = getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0]); 4263b11a16aSHongbin Zheng Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, 4273b11a16aSHongbin Zheng Load->getName() + "_p_vec_p"); 428c14582f2STobias Grosser LoadInst *ScalarLoad = 429c14582f2STobias Grosser Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); 4303b11a16aSHongbin Zheng 4313b11a16aSHongbin Zheng if (!Aligned) 4323b11a16aSHongbin Zheng ScalarLoad->setAlignment(8); 4333b11a16aSHongbin Zheng 434c14582f2STobias Grosser Constant *SplatVector = Constant::getNullValue( 435c14582f2STobias Grosser VectorType::get(Builder.getInt32Ty(), getVectorWidth())); 4363b11a16aSHongbin Zheng 437c14582f2STobias Grosser Value *VectorLoad = Builder.CreateShuffleVector( 438c14582f2STobias Grosser ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); 4393b11a16aSHongbin Zheng return VectorLoad; 4403b11a16aSHongbin Zheng } 4413b11a16aSHongbin Zheng 442c14582f2STobias Grosser Value *VectorBlockGenerator::generateUnknownStrideLoad( 443c14582f2STobias Grosser const LoadInst *Load, VectorValueMapT &ScalarMaps) { 4443b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 4453b11a16aSHongbin Zheng const Value *Pointer = Load->getPointerOperand(); 4463b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get( 4473b11a16aSHongbin Zheng dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); 4483b11a16aSHongbin Zheng 4493b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType); 4503b11a16aSHongbin Zheng 4513b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; i++) { 4529d10fffaSSebastian Pop Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], 4539d10fffaSSebastian Pop VLTS[i]); 454c14582f2STobias Grosser Value *ScalarLoad = 455c14582f2STobias Grosser Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); 456c14582f2STobias Grosser Vector = Builder.CreateInsertElement( 457c14582f2STobias Grosser Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); 4583b11a16aSHongbin Zheng } 4593b11a16aSHongbin Zheng 4603b11a16aSHongbin Zheng return Vector; 4613b11a16aSHongbin Zheng } 4623b11a16aSHongbin Zheng 463c14582f2STobias Grosser void VectorBlockGenerator::generateLoad( 464c14582f2STobias Grosser const LoadInst *Load, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) { 46568794217SHongbin Zheng if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL || 46668794217SHongbin Zheng !VectorType::isValidElementType(Load->getType())) { 4673b11a16aSHongbin Zheng for (int i = 0; i < getVectorWidth(); i++) 468c14582f2STobias Grosser ScalarMaps[i][Load] = 4699d10fffaSSebastian Pop generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]); 4703b11a16aSHongbin Zheng return; 4713b11a16aSHongbin Zheng } 4723b11a16aSHongbin Zheng 4733b11a16aSHongbin Zheng MemoryAccess &Access = Statement.getAccessFor(Load); 4743b11a16aSHongbin Zheng 4753b11a16aSHongbin Zheng Value *NewLoad; 476a00a0291SSebastian Pop if (Access.isStrideZero(isl_map_copy(Schedule))) 4773b11a16aSHongbin Zheng NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]); 478a00a0291SSebastian Pop else if (Access.isStrideOne(isl_map_copy(Schedule))) 4793b11a16aSHongbin Zheng NewLoad = generateStrideOneLoad(Load, ScalarMaps[0]); 4803b11a16aSHongbin Zheng else 4813b11a16aSHongbin Zheng NewLoad = generateUnknownStrideLoad(Load, ScalarMaps); 4823b11a16aSHongbin Zheng 4833b11a16aSHongbin Zheng VectorMap[Load] = NewLoad; 4843b11a16aSHongbin Zheng } 4853b11a16aSHongbin Zheng 4863b11a16aSHongbin Zheng void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst, 4873b11a16aSHongbin Zheng ValueMapT &VectorMap, 4883b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 4893b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 490c14582f2STobias Grosser Value *NewOperand = 491c14582f2STobias Grosser getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps); 4923b11a16aSHongbin Zheng 4933b11a16aSHongbin Zheng assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); 4943b11a16aSHongbin Zheng 4953b11a16aSHongbin Zheng const CastInst *Cast = dyn_cast<CastInst>(Inst); 4963b11a16aSHongbin Zheng VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); 4973b11a16aSHongbin Zheng VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); 4983b11a16aSHongbin Zheng } 4993b11a16aSHongbin Zheng 5003b11a16aSHongbin Zheng void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst, 5013b11a16aSHongbin Zheng ValueMapT &VectorMap, 5023b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 5033b11a16aSHongbin Zheng Value *OpZero = Inst->getOperand(0); 5043b11a16aSHongbin Zheng Value *OpOne = Inst->getOperand(1); 5053b11a16aSHongbin Zheng 5063b11a16aSHongbin Zheng Value *NewOpZero, *NewOpOne; 5073b11a16aSHongbin Zheng NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps); 5083b11a16aSHongbin Zheng NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps); 5093b11a16aSHongbin Zheng 5101bb59b0dSTobias Grosser Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, 5113b11a16aSHongbin Zheng Inst->getName() + "p_vec"); 5123b11a16aSHongbin Zheng VectorMap[Inst] = NewInst; 5133b11a16aSHongbin Zheng } 5143b11a16aSHongbin Zheng 515c14582f2STobias Grosser void VectorBlockGenerator::copyStore( 516c14582f2STobias Grosser const StoreInst *Store, ValueMapT &VectorMap, VectorValueMapT &ScalarMaps) { 5173b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 5183b11a16aSHongbin Zheng 5193b11a16aSHongbin Zheng MemoryAccess &Access = Statement.getAccessFor(Store); 5203b11a16aSHongbin Zheng 5213b11a16aSHongbin Zheng const Value *Pointer = Store->getPointerOperand(); 522c14582f2STobias Grosser Value *Vector = 523c14582f2STobias Grosser getVectorValue(Store->getValueOperand(), VectorMap, ScalarMaps); 5243b11a16aSHongbin Zheng 525a00a0291SSebastian Pop if (Access.isStrideOne(isl_map_copy(Schedule))) { 5263b11a16aSHongbin Zheng Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); 5279d10fffaSSebastian Pop Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0], 5289d10fffaSSebastian Pop VLTS[0]); 5293b11a16aSHongbin Zheng 530c14582f2STobias Grosser Value *VectorPtr = 531c14582f2STobias Grosser Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); 5323b11a16aSHongbin Zheng StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); 5333b11a16aSHongbin Zheng 5343b11a16aSHongbin Zheng if (!Aligned) 5353b11a16aSHongbin Zheng Store->setAlignment(8); 5363b11a16aSHongbin Zheng } else { 5373b11a16aSHongbin Zheng for (unsigned i = 0; i < ScalarMaps.size(); i++) { 5381bb59b0dSTobias Grosser Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); 5399d10fffaSSebastian Pop Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], 5409d10fffaSSebastian Pop VLTS[i]); 5413b11a16aSHongbin Zheng Builder.CreateStore(Scalar, NewPointer); 5423b11a16aSHongbin Zheng } 5433b11a16aSHongbin Zheng } 5443b11a16aSHongbin Zheng } 5453b11a16aSHongbin Zheng 5463b11a16aSHongbin Zheng bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, 5473b11a16aSHongbin Zheng ValueMapT &VectorMap) { 5483b11a16aSHongbin Zheng for (Instruction::const_op_iterator OI = Inst->op_begin(), 549c14582f2STobias Grosser OE = Inst->op_end(); 550c14582f2STobias Grosser OI != OE; ++OI) 5513b11a16aSHongbin Zheng if (VectorMap.count(*OI)) 5523b11a16aSHongbin Zheng return true; 5533b11a16aSHongbin Zheng return false; 5543b11a16aSHongbin Zheng } 5553b11a16aSHongbin Zheng 5563b11a16aSHongbin Zheng bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, 5573b11a16aSHongbin Zheng ValueMapT &VectorMap, 5583b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 5593b11a16aSHongbin Zheng bool HasVectorOperand = false; 5603b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 5613b11a16aSHongbin Zheng 5623b11a16aSHongbin Zheng for (Instruction::const_op_iterator OI = Inst->op_begin(), 563c14582f2STobias Grosser OE = Inst->op_end(); 564c14582f2STobias Grosser OI != OE; ++OI) { 5653b11a16aSHongbin Zheng ValueMapT::iterator VecOp = VectorMap.find(*OI); 5663b11a16aSHongbin Zheng 5673b11a16aSHongbin Zheng if (VecOp == VectorMap.end()) 5683b11a16aSHongbin Zheng continue; 5693b11a16aSHongbin Zheng 5703b11a16aSHongbin Zheng HasVectorOperand = true; 5713b11a16aSHongbin Zheng Value *NewVector = VecOp->second; 5723b11a16aSHongbin Zheng 5733b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; ++i) { 5743b11a16aSHongbin Zheng ValueMapT &SM = ScalarMaps[i]; 5753b11a16aSHongbin Zheng 5763b11a16aSHongbin Zheng // If there is one scalar extracted, all scalar elements should have 5773b11a16aSHongbin Zheng // already been extracted by the code here. So no need to check for the 5783b11a16aSHongbin Zheng // existance of all of them. 5793b11a16aSHongbin Zheng if (SM.count(*OI)) 5803b11a16aSHongbin Zheng break; 5813b11a16aSHongbin Zheng 5823b11a16aSHongbin Zheng SM[*OI] = Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); 5833b11a16aSHongbin Zheng } 5843b11a16aSHongbin Zheng } 5853b11a16aSHongbin Zheng 5863b11a16aSHongbin Zheng return HasVectorOperand; 5873b11a16aSHongbin Zheng } 5883b11a16aSHongbin Zheng 5893b11a16aSHongbin Zheng void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst, 5903b11a16aSHongbin Zheng ValueMapT &VectorMap, 5913b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 5923b11a16aSHongbin Zheng bool HasVectorOperand; 5933b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 5943b11a16aSHongbin Zheng 5953b11a16aSHongbin Zheng HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); 5963b11a16aSHongbin Zheng 5973b11a16aSHongbin Zheng for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) 5989d10fffaSSebastian Pop copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane], 5999d10fffaSSebastian Pop VLTS[VectorLane]); 6003b11a16aSHongbin Zheng 6013b11a16aSHongbin Zheng if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) 6023b11a16aSHongbin Zheng return; 6033b11a16aSHongbin Zheng 6043b11a16aSHongbin Zheng // Make the result available as vector value. 6053b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); 6063b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType); 6073b11a16aSHongbin Zheng 6083b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; i++) 6093b11a16aSHongbin Zheng Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], 6103b11a16aSHongbin Zheng Builder.getInt32(i)); 6113b11a16aSHongbin Zheng 6123b11a16aSHongbin Zheng VectorMap[Inst] = Vector; 6133b11a16aSHongbin Zheng } 6143b11a16aSHongbin Zheng 615c14582f2STobias Grosser int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); } 6163b11a16aSHongbin Zheng 6173b11a16aSHongbin Zheng void VectorBlockGenerator::copyInstruction(const Instruction *Inst, 6183b11a16aSHongbin Zheng ValueMapT &VectorMap, 6193b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 6203b11a16aSHongbin Zheng // Terminator instructions control the control flow. They are explicitly 6213b11a16aSHongbin Zheng // expressed in the clast and do not need to be copied. 6223b11a16aSHongbin Zheng if (Inst->isTerminator()) 6233b11a16aSHongbin Zheng return; 6243b11a16aSHongbin Zheng 625e71c6ab5STobias Grosser if (isSCEVIgnore(Inst)) 626e71c6ab5STobias Grosser return; 627e71c6ab5STobias Grosser 6283b11a16aSHongbin Zheng if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { 6293b11a16aSHongbin Zheng generateLoad(Load, VectorMap, ScalarMaps); 6303b11a16aSHongbin Zheng return; 6313b11a16aSHongbin Zheng } 6323b11a16aSHongbin Zheng 6333b11a16aSHongbin Zheng if (hasVectorOperands(Inst, VectorMap)) { 6343b11a16aSHongbin Zheng if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { 6353b11a16aSHongbin Zheng copyStore(Store, VectorMap, ScalarMaps); 6363b11a16aSHongbin Zheng return; 6373b11a16aSHongbin Zheng } 6383b11a16aSHongbin Zheng 6393b11a16aSHongbin Zheng if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { 6403b11a16aSHongbin Zheng copyUnaryInst(Unary, VectorMap, ScalarMaps); 6413b11a16aSHongbin Zheng return; 6423b11a16aSHongbin Zheng } 6433b11a16aSHongbin Zheng 6443b11a16aSHongbin Zheng if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { 6453b11a16aSHongbin Zheng copyBinaryInst(Binary, VectorMap, ScalarMaps); 6463b11a16aSHongbin Zheng return; 6473b11a16aSHongbin Zheng } 6483b11a16aSHongbin Zheng 6493b11a16aSHongbin Zheng // Falltrough: We generate scalar instructions, if we don't know how to 6503b11a16aSHongbin Zheng // generate vector code. 6513b11a16aSHongbin Zheng } 6523b11a16aSHongbin Zheng 6533b11a16aSHongbin Zheng copyInstScalarized(Inst, VectorMap, ScalarMaps); 6543b11a16aSHongbin Zheng } 6553b11a16aSHongbin Zheng 6563b11a16aSHongbin Zheng void VectorBlockGenerator::copyBB() { 6573b11a16aSHongbin Zheng BasicBlock *BB = Statement.getBasicBlock(); 658c14582f2STobias Grosser BasicBlock *CopyBB = 659c14582f2STobias Grosser SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); 6603b11a16aSHongbin Zheng CopyBB->setName("polly.stmt." + BB->getName()); 6613b11a16aSHongbin Zheng Builder.SetInsertPoint(CopyBB->begin()); 6623b11a16aSHongbin Zheng 6633b11a16aSHongbin Zheng // Create two maps that store the mapping from the original instructions of 6643b11a16aSHongbin Zheng // the old basic block to their copies in the new basic block. Those maps 6653b11a16aSHongbin Zheng // are basic block local. 6663b11a16aSHongbin Zheng // 6673b11a16aSHongbin Zheng // As vector code generation is supported there is one map for scalar values 6683b11a16aSHongbin Zheng // and one for vector values. 6693b11a16aSHongbin Zheng // 6703b11a16aSHongbin Zheng // In case we just do scalar code generation, the vectorMap is not used and 6713b11a16aSHongbin Zheng // the scalarMap has just one dimension, which contains the mapping. 6723b11a16aSHongbin Zheng // 6733b11a16aSHongbin Zheng // In case vector code generation is done, an instruction may either appear 6743b11a16aSHongbin Zheng // in the vector map once (as it is calculating >vectorwidth< values at a 6753b11a16aSHongbin Zheng // time. Or (if the values are calculated using scalar operations), it 6763b11a16aSHongbin Zheng // appears once in every dimension of the scalarMap. 6773b11a16aSHongbin Zheng VectorValueMapT ScalarBlockMap(getVectorWidth()); 6783b11a16aSHongbin Zheng ValueMapT VectorBlockMap; 6793b11a16aSHongbin Zheng 680c14582f2STobias Grosser for (BasicBlock::const_iterator II = BB->begin(), IE = BB->end(); II != IE; 681c14582f2STobias Grosser ++II) 6823b11a16aSHongbin Zheng copyInstruction(II, VectorBlockMap, ScalarBlockMap); 6833b11a16aSHongbin Zheng } 684