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" 18a63b2579SJohannes Doerfert #include "isl/ast.h" 19a63b2579SJohannes Doerfert #include "isl/ast_build.h" 2083628182STobias Grosser #include "isl/set.h" 218a846610SHongbin Zheng #include "polly/CodeGen/BlockGenerators.h" 2283628182STobias Grosser #include "polly/CodeGen/CodeGeneration.h" 23a63b2579SJohannes Doerfert #include "polly/CodeGen/IslExprBuilder.h" 24637bd631STobias Grosser #include "polly/Options.h" 253b11a16aSHongbin Zheng #include "polly/Support/GICHelper.h" 2697cb813cSSebastian Pop #include "polly/Support/SCEVValidator.h" 27ecfe21b7STobias Grosser #include "polly/Support/ScopHelper.h" 28e71c6ab5STobias Grosser #include "llvm/Analysis/LoopInfo.h" 29e71c6ab5STobias Grosser #include "llvm/Analysis/ScalarEvolution.h" 30e71c6ab5STobias Grosser #include "llvm/Analysis/ScalarEvolutionExpander.h" 31030237d0STobias Grosser #include "llvm/IR/IntrinsicInst.h" 323b11a16aSHongbin Zheng #include "llvm/Transforms/Utils/BasicBlockUtils.h" 333b11a16aSHongbin Zheng 343b11a16aSHongbin Zheng using namespace llvm; 353b11a16aSHongbin Zheng using namespace polly; 363b11a16aSHongbin Zheng 373b11a16aSHongbin Zheng static cl::opt<bool> 38483a90d1STobias Grosser Aligned("enable-polly-aligned", 39483a90d1STobias Grosser cl::desc("Assumed aligned memory accesses."), cl::Hidden, 40483a90d1STobias Grosser cl::value_desc("OpenMP code generation enabled if true"), 41637bd631STobias Grosser cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 423b11a16aSHongbin Zheng 43e602a076STobias Grosser static cl::opt<bool, true> 44483a90d1STobias Grosser SCEVCodegenF("polly-codegen-scev", 45483a90d1STobias Grosser cl::desc("Use SCEV based code generation."), cl::Hidden, 46483a90d1STobias Grosser cl::location(SCEVCodegen), cl::init(false), cl::ZeroOrMore, 47483a90d1STobias Grosser cl::cat(PollyCategory)); 48ecfe21b7STobias Grosser 49ecfe21b7STobias Grosser bool polly::SCEVCodegen; 50ecfe21b7STobias Grosser 51ecfe21b7STobias Grosser bool polly::canSynthesize(const Instruction *I, const llvm::LoopInfo *LI, 52ecfe21b7STobias Grosser ScalarEvolution *SE, const Region *R) { 53ecfe21b7STobias Grosser if (SCEVCodegen) { 54ecfe21b7STobias Grosser if (!I || !SE->isSCEVable(I->getType())) 55ecfe21b7STobias Grosser return false; 56ecfe21b7STobias Grosser 57ecfe21b7STobias Grosser if (const SCEV *Scev = SE->getSCEV(const_cast<Instruction *>(I))) 58ecfe21b7STobias Grosser if (!isa<SCEVCouldNotCompute>(Scev)) 59ecfe21b7STobias Grosser if (!hasScalarDepsInsideRegion(Scev, R)) 60ecfe21b7STobias Grosser return true; 61ecfe21b7STobias Grosser 62ecfe21b7STobias Grosser return false; 63ecfe21b7STobias Grosser } 64ecfe21b7STobias Grosser 65ecfe21b7STobias Grosser Loop *L = LI->getLoopFor(I->getParent()); 66e8df5bd9STobias Grosser return L && I == L->getCanonicalInductionVariable() && R->contains(L); 67ecfe21b7STobias Grosser } 68ecfe21b7STobias Grosser 69a63b2579SJohannes Doerfert BlockGenerator::BlockGenerator(PollyIRBuilder &B, ScopStmt &Stmt, Pass *P, 70*2ef3f4fdSJohannes Doerfert LoopInfo &LI, ScalarEvolution &SE, 71a63b2579SJohannes Doerfert isl_ast_build *Build, 72a63b2579SJohannes Doerfert IslExprBuilder *ExprBuilder) 73*2ef3f4fdSJohannes Doerfert : Builder(B), Statement(Stmt), P(P), LI(LI), SE(SE), Build(Build), 74*2ef3f4fdSJohannes Doerfert ExprBuilder(ExprBuilder) {} 75e71c6ab5STobias Grosser 765b463ceaSHongbin Zheng Value *BlockGenerator::lookupAvailableValue(const Value *Old, ValueMapT &BBMap, 775b463ceaSHongbin Zheng ValueMapT &GlobalMap) const { 783b11a16aSHongbin Zheng // We assume constants never change. 793b11a16aSHongbin Zheng // This avoids map lookups for many calls to this function. 803b11a16aSHongbin Zheng if (isa<Constant>(Old)) 813b11a16aSHongbin Zheng return const_cast<Value *>(Old); 823b11a16aSHongbin Zheng 83fe11e287SHongbin Zheng if (Value *New = GlobalMap.lookup(Old)) { 84c14582f2STobias Grosser if (Old->getType()->getScalarSizeInBits() < 85c14582f2STobias Grosser New->getType()->getScalarSizeInBits()) 863b11a16aSHongbin Zheng New = Builder.CreateTruncOrBitCast(New, Old->getType()); 873b11a16aSHongbin Zheng 883b11a16aSHongbin Zheng return New; 893b11a16aSHongbin Zheng } 903b11a16aSHongbin Zheng 915b463ceaSHongbin Zheng // Or it is probably a scop-constant value defined as global, function 925b463ceaSHongbin Zheng // parameter or an instruction not within the scop. 935b463ceaSHongbin Zheng if (isa<GlobalValue>(Old) || isa<Argument>(Old)) 945b463ceaSHongbin Zheng return const_cast<Value *>(Old); 955b463ceaSHongbin Zheng 965b463ceaSHongbin Zheng if (const Instruction *Inst = dyn_cast<Instruction>(Old)) 975b463ceaSHongbin Zheng if (!Statement.getParent()->getRegion().contains(Inst->getParent())) 985b463ceaSHongbin Zheng return const_cast<Value *>(Old); 995b463ceaSHongbin Zheng 100fe11e287SHongbin Zheng if (Value *New = BBMap.lookup(Old)) 101fe11e287SHongbin Zheng return New; 1023b11a16aSHongbin Zheng 1035a56cbf4STobias Grosser return nullptr; 1045b463ceaSHongbin Zheng } 1055b463ceaSHongbin Zheng 1065b463ceaSHongbin Zheng Value *BlockGenerator::getNewValue(const Value *Old, ValueMapT &BBMap, 1075b463ceaSHongbin Zheng ValueMapT &GlobalMap, LoopToScevMapT <S, 1085b463ceaSHongbin Zheng Loop *L) { 1095b463ceaSHongbin Zheng if (Value *New = lookupAvailableValue(Old, BBMap, GlobalMap)) 1105b463ceaSHongbin Zheng return New; 1115b463ceaSHongbin Zheng 112e71c6ab5STobias Grosser if (SCEVCodegen && SE.isSCEVable(Old->getType())) 113369430ffSTobias Grosser if (const SCEV *Scev = SE.getSCEVAtScope(const_cast<Value *>(Old), L)) { 114e71c6ab5STobias Grosser if (!isa<SCEVCouldNotCompute>(Scev)) { 115637b23dcSSebastian Pop const SCEV *NewScev = apply(Scev, LTS, SE); 116637b23dcSSebastian Pop ValueToValueMap VTV; 117637b23dcSSebastian Pop VTV.insert(BBMap.begin(), BBMap.end()); 118637b23dcSSebastian Pop VTV.insert(GlobalMap.begin(), GlobalMap.end()); 11947d4ee3eSSebastian Pop NewScev = SCEVParameterRewriter::rewrite(NewScev, SE, VTV); 120e71c6ab5STobias Grosser SCEVExpander Expander(SE, "polly"); 121e71c6ab5STobias Grosser Value *Expanded = Expander.expandCodeFor(NewScev, Old->getType(), 122e71c6ab5STobias Grosser Builder.GetInsertPoint()); 123e71c6ab5STobias Grosser 124e71c6ab5STobias Grosser BBMap[Old] = Expanded; 125e71c6ab5STobias Grosser return Expanded; 126e71c6ab5STobias Grosser } 127369430ffSTobias Grosser } 128e71c6ab5STobias Grosser 1295b463ceaSHongbin Zheng // Now the scalar dependence is neither available nor SCEVCodegenable, this 1305b463ceaSHongbin Zheng // should never happen in the current code generator. 1315b463ceaSHongbin Zheng llvm_unreachable("Unexpected scalar dependence in region!"); 1325a56cbf4STobias Grosser return nullptr; 1333b11a16aSHongbin Zheng } 1343b11a16aSHongbin Zheng 1353b11a16aSHongbin Zheng void BlockGenerator::copyInstScalar(const Instruction *Inst, ValueMapT &BBMap, 1369d10fffaSSebastian Pop ValueMapT &GlobalMap, LoopToScevMapT <S) { 137030237d0STobias Grosser // We do not generate debug intrinsics as we did not investigate how to 138030237d0STobias Grosser // copy them correctly. At the current state, they just crash the code 139030237d0STobias Grosser // generation as the meta-data operands are not correctly copied. 140030237d0STobias Grosser if (isa<DbgInfoIntrinsic>(Inst)) 141030237d0STobias Grosser return; 142030237d0STobias Grosser 1433b11a16aSHongbin Zheng Instruction *NewInst = Inst->clone(); 1443b11a16aSHongbin Zheng 1453b11a16aSHongbin Zheng // Replace old operands with the new ones. 14691f5b262STobias Grosser for (Value *OldOperand : Inst->operands()) { 147369430ffSTobias Grosser Value *NewOperand = 148369430ffSTobias Grosser getNewValue(OldOperand, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); 1493b11a16aSHongbin Zheng 1503b11a16aSHongbin Zheng if (!NewOperand) { 151c14582f2STobias Grosser assert(!isa<StoreInst>(NewInst) && 152c14582f2STobias Grosser "Store instructions are always needed!"); 1533b11a16aSHongbin Zheng delete NewInst; 1543b11a16aSHongbin Zheng return; 1553b11a16aSHongbin Zheng } 1563b11a16aSHongbin Zheng 1573b11a16aSHongbin Zheng NewInst->replaceUsesOfWith(OldOperand, NewOperand); 1583b11a16aSHongbin Zheng } 1593b11a16aSHongbin Zheng 1603b11a16aSHongbin Zheng Builder.Insert(NewInst); 1613b11a16aSHongbin Zheng BBMap[Inst] = NewInst; 1623b11a16aSHongbin Zheng 1633b11a16aSHongbin Zheng if (!NewInst->getType()->isVoidTy()) 1643b11a16aSHongbin Zheng NewInst->setName("p_" + Inst->getName()); 1653b11a16aSHongbin Zheng } 1663b11a16aSHongbin Zheng 167a63b2579SJohannes Doerfert Value *BlockGenerator::getNewAccessOperand(const MemoryAccess &MA) { 168a63b2579SJohannes Doerfert isl_pw_multi_aff *PWSchedule, *PWAccRel; 169a63b2579SJohannes Doerfert isl_union_map *ScheduleU; 170a63b2579SJohannes Doerfert isl_map *Schedule, *AccRel; 171a63b2579SJohannes Doerfert isl_ast_expr *Expr; 1723b11a16aSHongbin Zheng 173a63b2579SJohannes Doerfert assert(ExprBuilder && Build && 174a63b2579SJohannes Doerfert "Cannot generate new value without IslExprBuilder!"); 1753b11a16aSHongbin Zheng 176a63b2579SJohannes Doerfert AccRel = MA.getNewAccessRelation(); 177a63b2579SJohannes Doerfert assert(AccRel && "We generate new code only for new access relations!"); 1783b11a16aSHongbin Zheng 179a63b2579SJohannes Doerfert ScheduleU = isl_ast_build_get_schedule(Build); 180a63b2579SJohannes Doerfert ScheduleU = isl_union_map_intersect_domain( 181a63b2579SJohannes Doerfert ScheduleU, isl_union_set_from_set(MA.getStatement()->getDomain())); 182a63b2579SJohannes Doerfert Schedule = isl_map_from_union_map(ScheduleU); 1833b11a16aSHongbin Zheng 184a63b2579SJohannes Doerfert PWSchedule = isl_pw_multi_aff_from_map(isl_map_reverse(Schedule)); 185a63b2579SJohannes Doerfert PWAccRel = isl_pw_multi_aff_from_map(AccRel); 186a63b2579SJohannes Doerfert PWAccRel = isl_pw_multi_aff_pullback_pw_multi_aff(PWAccRel, PWSchedule); 1873b11a16aSHongbin Zheng 188a63b2579SJohannes Doerfert Expr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel); 189a63b2579SJohannes Doerfert 190a63b2579SJohannes Doerfert return ExprBuilder->create(Expr); 1913b11a16aSHongbin Zheng } 1923b11a16aSHongbin Zheng 193e602a076STobias Grosser Value *BlockGenerator::generateLocationAccessed(const Instruction *Inst, 194e602a076STobias Grosser const Value *Pointer, 195e602a076STobias Grosser ValueMapT &BBMap, 196e602a076STobias Grosser ValueMapT &GlobalMap, 197e602a076STobias Grosser LoopToScevMapT <S) { 198a63b2579SJohannes Doerfert const MemoryAccess &MA = Statement.getAccessFor(Inst); 199a63b2579SJohannes Doerfert isl_map *NewAccRel = MA.getNewAccessRelation(); 2003b11a16aSHongbin Zheng 2013b11a16aSHongbin Zheng Value *NewPointer; 202a63b2579SJohannes Doerfert if (NewAccRel) 203a63b2579SJohannes Doerfert NewPointer = getNewAccessOperand(MA); 204a63b2579SJohannes Doerfert else 205369430ffSTobias Grosser NewPointer = 206369430ffSTobias Grosser getNewValue(Pointer, BBMap, GlobalMap, LTS, getLoopForInst(Inst)); 2073b11a16aSHongbin Zheng 208a63b2579SJohannes Doerfert isl_map_free(NewAccRel); 2093b11a16aSHongbin Zheng return NewPointer; 2103b11a16aSHongbin Zheng } 2113b11a16aSHongbin Zheng 2124d96c8d7STobias Grosser Loop *BlockGenerator::getLoopForInst(const llvm::Instruction *Inst) { 213*2ef3f4fdSJohannes Doerfert return LI.getLoopFor(Inst->getParent()); 214369430ffSTobias Grosser } 215369430ffSTobias Grosser 216e602a076STobias Grosser Value *BlockGenerator::generateScalarLoad(const LoadInst *Load, 217e602a076STobias Grosser ValueMapT &BBMap, 218e602a076STobias Grosser ValueMapT &GlobalMap, 219e602a076STobias Grosser LoopToScevMapT <S) { 2203b11a16aSHongbin Zheng const Value *Pointer = Load->getPointerOperand(); 2213b11a16aSHongbin Zheng const Instruction *Inst = dyn_cast<Instruction>(Load); 2227242ad92STobias Grosser Value *NewPointer = 2237242ad92STobias Grosser generateLocationAccessed(Inst, Pointer, BBMap, GlobalMap, LTS); 224c14582f2STobias Grosser Value *ScalarLoad = 225c14582f2STobias Grosser Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); 2263b11a16aSHongbin Zheng return ScalarLoad; 2273b11a16aSHongbin Zheng } 2283b11a16aSHongbin Zheng 229e602a076STobias Grosser Value *BlockGenerator::generateScalarStore(const StoreInst *Store, 230e602a076STobias Grosser ValueMapT &BBMap, 231e602a076STobias Grosser ValueMapT &GlobalMap, 232e602a076STobias Grosser LoopToScevMapT <S) { 2333b11a16aSHongbin Zheng const Value *Pointer = Store->getPointerOperand(); 234c14582f2STobias Grosser Value *NewPointer = 2359d10fffaSSebastian Pop generateLocationAccessed(Store, Pointer, BBMap, GlobalMap, LTS); 236369430ffSTobias Grosser Value *ValueOperand = getNewValue(Store->getValueOperand(), BBMap, GlobalMap, 237369430ffSTobias Grosser LTS, getLoopForInst(Store)); 2383b11a16aSHongbin Zheng 2393b11a16aSHongbin Zheng return Builder.CreateStore(ValueOperand, NewPointer); 2403b11a16aSHongbin Zheng } 2413b11a16aSHongbin Zheng 242e602a076STobias Grosser void BlockGenerator::copyInstruction(const Instruction *Inst, ValueMapT &BBMap, 243e602a076STobias Grosser ValueMapT &GlobalMap, 244e602a076STobias Grosser LoopToScevMapT <S) { 2453b11a16aSHongbin Zheng // Terminator instructions control the control flow. They are explicitly 2463b11a16aSHongbin Zheng // expressed in the clast and do not need to be copied. 2473b11a16aSHongbin Zheng if (Inst->isTerminator()) 2483b11a16aSHongbin Zheng return; 2493b11a16aSHongbin Zheng 250ecfe21b7STobias Grosser if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE, 251ecfe21b7STobias Grosser &Statement.getParent()->getRegion())) 252e71c6ab5STobias Grosser return; 253e71c6ab5STobias Grosser 2543b11a16aSHongbin Zheng if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { 255753d43f9SSebastian Pop Value *NewLoad = generateScalarLoad(Load, BBMap, GlobalMap, LTS); 2563d94fedfSSebastian Pop // Compute NewLoad before its insertion in BBMap to make the insertion 2573d94fedfSSebastian Pop // deterministic. 258753d43f9SSebastian Pop BBMap[Load] = NewLoad; 2593b11a16aSHongbin Zheng return; 2603b11a16aSHongbin Zheng } 2613b11a16aSHongbin Zheng 2623b11a16aSHongbin Zheng if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { 263753d43f9SSebastian Pop Value *NewStore = generateScalarStore(Store, BBMap, GlobalMap, LTS); 2643d94fedfSSebastian Pop // Compute NewStore before its insertion in BBMap to make the insertion 2653d94fedfSSebastian Pop // deterministic. 266753d43f9SSebastian Pop BBMap[Store] = NewStore; 2673b11a16aSHongbin Zheng return; 2683b11a16aSHongbin Zheng } 2693b11a16aSHongbin Zheng 2709d10fffaSSebastian Pop copyInstScalar(Inst, BBMap, GlobalMap, LTS); 2713b11a16aSHongbin Zheng } 2723b11a16aSHongbin Zheng 2739d10fffaSSebastian Pop void BlockGenerator::copyBB(ValueMapT &GlobalMap, LoopToScevMapT <S) { 2743b11a16aSHongbin Zheng BasicBlock *BB = Statement.getBasicBlock(); 275c14582f2STobias Grosser BasicBlock *CopyBB = 276c14582f2STobias Grosser SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); 2773b11a16aSHongbin Zheng CopyBB->setName("polly.stmt." + BB->getName()); 2783b11a16aSHongbin Zheng Builder.SetInsertPoint(CopyBB->begin()); 2793b11a16aSHongbin Zheng 2803b11a16aSHongbin Zheng ValueMapT BBMap; 2813b11a16aSHongbin Zheng 28291f5b262STobias Grosser for (Instruction &Inst : *BB) 28391f5b262STobias Grosser copyInstruction(&Inst, BBMap, GlobalMap, LTS); 2843b11a16aSHongbin Zheng } 2853b11a16aSHongbin Zheng 286*2ef3f4fdSJohannes Doerfert VectorBlockGenerator::VectorBlockGenerator( 287*2ef3f4fdSJohannes Doerfert PollyIRBuilder &B, VectorValueMapT &GlobalMaps, 288*2ef3f4fdSJohannes Doerfert std::vector<LoopToScevMapT> &VLTS, ScopStmt &Stmt, 289*2ef3f4fdSJohannes Doerfert __isl_keep isl_map *Schedule, Pass *P, LoopInfo &LI, ScalarEvolution &SE) 290*2ef3f4fdSJohannes Doerfert : BlockGenerator(B, Stmt, P, LI, SE, nullptr, nullptr), 291*2ef3f4fdSJohannes Doerfert GlobalMaps(GlobalMaps), VLTS(VLTS), Schedule(Schedule) { 2923b11a16aSHongbin Zheng assert(GlobalMaps.size() > 1 && "Only one vector lane found"); 293a00a0291SSebastian Pop assert(Schedule && "No statement domain provided"); 2943b11a16aSHongbin Zheng } 2953b11a16aSHongbin Zheng 296e602a076STobias Grosser Value *VectorBlockGenerator::getVectorValue(const Value *Old, 297e602a076STobias Grosser ValueMapT &VectorMap, 298e602a076STobias Grosser VectorValueMapT &ScalarMaps, 299e602a076STobias Grosser Loop *L) { 300fe11e287SHongbin Zheng if (Value *NewValue = VectorMap.lookup(Old)) 301fe11e287SHongbin Zheng return NewValue; 3023b11a16aSHongbin Zheng 3033b11a16aSHongbin Zheng int Width = getVectorWidth(); 3043b11a16aSHongbin Zheng 3053b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType::get(Old->getType(), Width)); 3063b11a16aSHongbin Zheng 3073b11a16aSHongbin Zheng for (int Lane = 0; Lane < Width; Lane++) 308c14582f2STobias Grosser Vector = Builder.CreateInsertElement( 3097242ad92STobias Grosser Vector, 310369430ffSTobias Grosser getNewValue(Old, ScalarMaps[Lane], GlobalMaps[Lane], VLTS[Lane], L), 3117242ad92STobias Grosser Builder.getInt32(Lane)); 3123b11a16aSHongbin Zheng 3133b11a16aSHongbin Zheng VectorMap[Old] = Vector; 3143b11a16aSHongbin Zheng 3153b11a16aSHongbin Zheng return Vector; 3163b11a16aSHongbin Zheng } 3173b11a16aSHongbin Zheng 3183b11a16aSHongbin Zheng Type *VectorBlockGenerator::getVectorPtrTy(const Value *Val, int Width) { 3193b11a16aSHongbin Zheng PointerType *PointerTy = dyn_cast<PointerType>(Val->getType()); 3203b11a16aSHongbin Zheng assert(PointerTy && "PointerType expected"); 3213b11a16aSHongbin Zheng 3223b11a16aSHongbin Zheng Type *ScalarType = PointerTy->getElementType(); 3233b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get(ScalarType, Width); 3243b11a16aSHongbin Zheng 3253b11a16aSHongbin Zheng return PointerType::getUnqual(VectorType); 3263b11a16aSHongbin Zheng } 3273b11a16aSHongbin Zheng 3280dd463faSTobias Grosser Value * 3290dd463faSTobias Grosser VectorBlockGenerator::generateStrideOneLoad(const LoadInst *Load, 3300dd463faSTobias Grosser VectorValueMapT &ScalarMaps, 3310dd463faSTobias Grosser bool NegativeStride = false) { 3320dd463faSTobias Grosser unsigned VectorWidth = getVectorWidth(); 3333b11a16aSHongbin Zheng const Value *Pointer = Load->getPointerOperand(); 3340dd463faSTobias Grosser Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); 3350dd463faSTobias Grosser unsigned Offset = NegativeStride ? VectorWidth - 1 : 0; 3360dd463faSTobias Grosser 3375a56cbf4STobias Grosser Value *NewPointer = nullptr; 3380dd463faSTobias Grosser NewPointer = getNewValue(Pointer, ScalarMaps[Offset], GlobalMaps[Offset], 3390dd463faSTobias Grosser VLTS[Offset], getLoopForInst(Load)); 340c14582f2STobias Grosser Value *VectorPtr = 341c14582f2STobias Grosser Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); 342c14582f2STobias Grosser LoadInst *VecLoad = 343c14582f2STobias Grosser Builder.CreateLoad(VectorPtr, Load->getName() + "_p_vec_full"); 3443b11a16aSHongbin Zheng if (!Aligned) 3453b11a16aSHongbin Zheng VecLoad->setAlignment(8); 3463b11a16aSHongbin Zheng 3470dd463faSTobias Grosser if (NegativeStride) { 3480dd463faSTobias Grosser SmallVector<Constant *, 16> Indices; 3490dd463faSTobias Grosser for (int i = VectorWidth - 1; i >= 0; i--) 3500dd463faSTobias Grosser Indices.push_back(ConstantInt::get(Builder.getInt32Ty(), i)); 3510dd463faSTobias Grosser Constant *SV = llvm::ConstantVector::get(Indices); 3520dd463faSTobias Grosser Value *RevVecLoad = Builder.CreateShuffleVector( 3530dd463faSTobias Grosser VecLoad, VecLoad, SV, Load->getName() + "_reverse"); 3540dd463faSTobias Grosser return RevVecLoad; 3550dd463faSTobias Grosser } 3560dd463faSTobias Grosser 3573b11a16aSHongbin Zheng return VecLoad; 3583b11a16aSHongbin Zheng } 3593b11a16aSHongbin Zheng 3603b11a16aSHongbin Zheng Value *VectorBlockGenerator::generateStrideZeroLoad(const LoadInst *Load, 3613b11a16aSHongbin Zheng ValueMapT &BBMap) { 3623b11a16aSHongbin Zheng const Value *Pointer = Load->getPointerOperand(); 3633b11a16aSHongbin Zheng Type *VectorPtrType = getVectorPtrTy(Pointer, 1); 364369430ffSTobias Grosser Value *NewPointer = 365369430ffSTobias Grosser getNewValue(Pointer, BBMap, GlobalMaps[0], VLTS[0], getLoopForInst(Load)); 3663b11a16aSHongbin Zheng Value *VectorPtr = Builder.CreateBitCast(NewPointer, VectorPtrType, 3673b11a16aSHongbin Zheng Load->getName() + "_p_vec_p"); 368c14582f2STobias Grosser LoadInst *ScalarLoad = 369c14582f2STobias Grosser Builder.CreateLoad(VectorPtr, Load->getName() + "_p_splat_one"); 3703b11a16aSHongbin Zheng 3713b11a16aSHongbin Zheng if (!Aligned) 3723b11a16aSHongbin Zheng ScalarLoad->setAlignment(8); 3733b11a16aSHongbin Zheng 374c14582f2STobias Grosser Constant *SplatVector = Constant::getNullValue( 375c14582f2STobias Grosser VectorType::get(Builder.getInt32Ty(), getVectorWidth())); 3763b11a16aSHongbin Zheng 377c14582f2STobias Grosser Value *VectorLoad = Builder.CreateShuffleVector( 378c14582f2STobias Grosser ScalarLoad, ScalarLoad, SplatVector, Load->getName() + "_p_splat"); 3793b11a16aSHongbin Zheng return VectorLoad; 3803b11a16aSHongbin Zheng } 3813b11a16aSHongbin Zheng 382e602a076STobias Grosser Value * 383e602a076STobias Grosser VectorBlockGenerator::generateUnknownStrideLoad(const LoadInst *Load, 384e602a076STobias Grosser VectorValueMapT &ScalarMaps) { 3853b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 3863b11a16aSHongbin Zheng const Value *Pointer = Load->getPointerOperand(); 3873b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get( 3883b11a16aSHongbin Zheng dyn_cast<PointerType>(Pointer->getType())->getElementType(), VectorWidth); 3893b11a16aSHongbin Zheng 3903b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType); 3913b11a16aSHongbin Zheng 3923b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; i++) { 393369430ffSTobias Grosser Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], 394369430ffSTobias Grosser VLTS[i], getLoopForInst(Load)); 395c14582f2STobias Grosser Value *ScalarLoad = 396c14582f2STobias Grosser Builder.CreateLoad(NewPointer, Load->getName() + "_p_scalar_"); 397c14582f2STobias Grosser Vector = Builder.CreateInsertElement( 398c14582f2STobias Grosser Vector, ScalarLoad, Builder.getInt32(i), Load->getName() + "_p_vec_"); 3993b11a16aSHongbin Zheng } 4003b11a16aSHongbin Zheng 4013b11a16aSHongbin Zheng return Vector; 4023b11a16aSHongbin Zheng } 4033b11a16aSHongbin Zheng 404e602a076STobias Grosser void VectorBlockGenerator::generateLoad(const LoadInst *Load, 405e602a076STobias Grosser ValueMapT &VectorMap, 406e602a076STobias Grosser VectorValueMapT &ScalarMaps) { 40768794217SHongbin Zheng if (PollyVectorizerChoice >= VECTORIZER_FIRST_NEED_GROUPED_UNROLL || 40868794217SHongbin Zheng !VectorType::isValidElementType(Load->getType())) { 4093b11a16aSHongbin Zheng for (int i = 0; i < getVectorWidth(); i++) 410c14582f2STobias Grosser ScalarMaps[i][Load] = 4119d10fffaSSebastian Pop generateScalarLoad(Load, ScalarMaps[i], GlobalMaps[i], VLTS[i]); 4123b11a16aSHongbin Zheng return; 4133b11a16aSHongbin Zheng } 4143b11a16aSHongbin Zheng 415b5f24a66SHongbin Zheng const MemoryAccess &Access = Statement.getAccessFor(Load); 4163b11a16aSHongbin Zheng 41795493984STobias Grosser // Make sure we have scalar values available to access the pointer to 41895493984STobias Grosser // the data location. 41995493984STobias Grosser extractScalarValues(Load, VectorMap, ScalarMaps); 42095493984STobias Grosser 4213b11a16aSHongbin Zheng Value *NewLoad; 422a00a0291SSebastian Pop if (Access.isStrideZero(isl_map_copy(Schedule))) 4233b11a16aSHongbin Zheng NewLoad = generateStrideZeroLoad(Load, ScalarMaps[0]); 424a00a0291SSebastian Pop else if (Access.isStrideOne(isl_map_copy(Schedule))) 4250dd463faSTobias Grosser NewLoad = generateStrideOneLoad(Load, ScalarMaps); 4260dd463faSTobias Grosser else if (Access.isStrideX(isl_map_copy(Schedule), -1)) 4270dd463faSTobias Grosser NewLoad = generateStrideOneLoad(Load, ScalarMaps, true); 4283b11a16aSHongbin Zheng else 4293b11a16aSHongbin Zheng NewLoad = generateUnknownStrideLoad(Load, ScalarMaps); 4303b11a16aSHongbin Zheng 4313b11a16aSHongbin Zheng VectorMap[Load] = NewLoad; 4323b11a16aSHongbin Zheng } 4333b11a16aSHongbin Zheng 4343b11a16aSHongbin Zheng void VectorBlockGenerator::copyUnaryInst(const UnaryInstruction *Inst, 4353b11a16aSHongbin Zheng ValueMapT &VectorMap, 4363b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 4373b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 438369430ffSTobias Grosser Value *NewOperand = getVectorValue(Inst->getOperand(0), VectorMap, ScalarMaps, 439369430ffSTobias Grosser getLoopForInst(Inst)); 4403b11a16aSHongbin Zheng 4413b11a16aSHongbin Zheng assert(isa<CastInst>(Inst) && "Can not generate vector code for instruction"); 4423b11a16aSHongbin Zheng 4433b11a16aSHongbin Zheng const CastInst *Cast = dyn_cast<CastInst>(Inst); 4443b11a16aSHongbin Zheng VectorType *DestType = VectorType::get(Inst->getType(), VectorWidth); 4453b11a16aSHongbin Zheng VectorMap[Inst] = Builder.CreateCast(Cast->getOpcode(), NewOperand, DestType); 4463b11a16aSHongbin Zheng } 4473b11a16aSHongbin Zheng 4483b11a16aSHongbin Zheng void VectorBlockGenerator::copyBinaryInst(const BinaryOperator *Inst, 4493b11a16aSHongbin Zheng ValueMapT &VectorMap, 4503b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 451369430ffSTobias Grosser Loop *L = getLoopForInst(Inst); 4523b11a16aSHongbin Zheng Value *OpZero = Inst->getOperand(0); 4533b11a16aSHongbin Zheng Value *OpOne = Inst->getOperand(1); 4543b11a16aSHongbin Zheng 4553b11a16aSHongbin Zheng Value *NewOpZero, *NewOpOne; 456369430ffSTobias Grosser NewOpZero = getVectorValue(OpZero, VectorMap, ScalarMaps, L); 457369430ffSTobias Grosser NewOpOne = getVectorValue(OpOne, VectorMap, ScalarMaps, L); 4583b11a16aSHongbin Zheng 4591bb59b0dSTobias Grosser Value *NewInst = Builder.CreateBinOp(Inst->getOpcode(), NewOpZero, NewOpOne, 4603b11a16aSHongbin Zheng Inst->getName() + "p_vec"); 4613b11a16aSHongbin Zheng VectorMap[Inst] = NewInst; 4623b11a16aSHongbin Zheng } 4633b11a16aSHongbin Zheng 464e602a076STobias Grosser void VectorBlockGenerator::copyStore(const StoreInst *Store, 465e602a076STobias Grosser ValueMapT &VectorMap, 466e602a076STobias Grosser VectorValueMapT &ScalarMaps) { 4673b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 4683b11a16aSHongbin Zheng 469b5f24a66SHongbin Zheng const MemoryAccess &Access = Statement.getAccessFor(Store); 4703b11a16aSHongbin Zheng 4713b11a16aSHongbin Zheng const Value *Pointer = Store->getPointerOperand(); 472369430ffSTobias Grosser Value *Vector = getVectorValue(Store->getValueOperand(), VectorMap, 473369430ffSTobias Grosser ScalarMaps, getLoopForInst(Store)); 4743b11a16aSHongbin Zheng 47550fd7010STobias Grosser // Make sure we have scalar values available to access the pointer to 47650fd7010STobias Grosser // the data location. 47750fd7010STobias Grosser extractScalarValues(Store, VectorMap, ScalarMaps); 47850fd7010STobias Grosser 479a00a0291SSebastian Pop if (Access.isStrideOne(isl_map_copy(Schedule))) { 4803b11a16aSHongbin Zheng Type *VectorPtrType = getVectorPtrTy(Pointer, VectorWidth); 481369430ffSTobias Grosser Value *NewPointer = getNewValue(Pointer, ScalarMaps[0], GlobalMaps[0], 482369430ffSTobias Grosser VLTS[0], getLoopForInst(Store)); 4833b11a16aSHongbin Zheng 484c14582f2STobias Grosser Value *VectorPtr = 485c14582f2STobias Grosser Builder.CreateBitCast(NewPointer, VectorPtrType, "vector_ptr"); 4863b11a16aSHongbin Zheng StoreInst *Store = Builder.CreateStore(Vector, VectorPtr); 4873b11a16aSHongbin Zheng 4883b11a16aSHongbin Zheng if (!Aligned) 4893b11a16aSHongbin Zheng Store->setAlignment(8); 4903b11a16aSHongbin Zheng } else { 4913b11a16aSHongbin Zheng for (unsigned i = 0; i < ScalarMaps.size(); i++) { 4921bb59b0dSTobias Grosser Value *Scalar = Builder.CreateExtractElement(Vector, Builder.getInt32(i)); 493369430ffSTobias Grosser Value *NewPointer = getNewValue(Pointer, ScalarMaps[i], GlobalMaps[i], 494369430ffSTobias Grosser VLTS[i], getLoopForInst(Store)); 4953b11a16aSHongbin Zheng Builder.CreateStore(Scalar, NewPointer); 4963b11a16aSHongbin Zheng } 4973b11a16aSHongbin Zheng } 4983b11a16aSHongbin Zheng } 4993b11a16aSHongbin Zheng 5003b11a16aSHongbin Zheng bool VectorBlockGenerator::hasVectorOperands(const Instruction *Inst, 5013b11a16aSHongbin Zheng ValueMapT &VectorMap) { 50291f5b262STobias Grosser for (Value *Operand : Inst->operands()) 50391f5b262STobias Grosser if (VectorMap.count(Operand)) 5043b11a16aSHongbin Zheng return true; 5053b11a16aSHongbin Zheng return false; 5063b11a16aSHongbin Zheng } 5073b11a16aSHongbin Zheng 5083b11a16aSHongbin Zheng bool VectorBlockGenerator::extractScalarValues(const Instruction *Inst, 5093b11a16aSHongbin Zheng ValueMapT &VectorMap, 5103b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 5113b11a16aSHongbin Zheng bool HasVectorOperand = false; 5123b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 5133b11a16aSHongbin Zheng 51491f5b262STobias Grosser for (Value *Operand : Inst->operands()) { 51591f5b262STobias Grosser ValueMapT::iterator VecOp = VectorMap.find(Operand); 5163b11a16aSHongbin Zheng 5173b11a16aSHongbin Zheng if (VecOp == VectorMap.end()) 5183b11a16aSHongbin Zheng continue; 5193b11a16aSHongbin Zheng 5203b11a16aSHongbin Zheng HasVectorOperand = true; 5213b11a16aSHongbin Zheng Value *NewVector = VecOp->second; 5223b11a16aSHongbin Zheng 5233b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; ++i) { 5243b11a16aSHongbin Zheng ValueMapT &SM = ScalarMaps[i]; 5253b11a16aSHongbin Zheng 5263b11a16aSHongbin Zheng // If there is one scalar extracted, all scalar elements should have 5273b11a16aSHongbin Zheng // already been extracted by the code here. So no need to check for the 5283b11a16aSHongbin Zheng // existance of all of them. 52991f5b262STobias Grosser if (SM.count(Operand)) 5303b11a16aSHongbin Zheng break; 5313b11a16aSHongbin Zheng 53291f5b262STobias Grosser SM[Operand] = 53391f5b262STobias Grosser Builder.CreateExtractElement(NewVector, Builder.getInt32(i)); 5343b11a16aSHongbin Zheng } 5353b11a16aSHongbin Zheng } 5363b11a16aSHongbin Zheng 5373b11a16aSHongbin Zheng return HasVectorOperand; 5383b11a16aSHongbin Zheng } 5393b11a16aSHongbin Zheng 5403b11a16aSHongbin Zheng void VectorBlockGenerator::copyInstScalarized(const Instruction *Inst, 5413b11a16aSHongbin Zheng ValueMapT &VectorMap, 5423b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 5433b11a16aSHongbin Zheng bool HasVectorOperand; 5443b11a16aSHongbin Zheng int VectorWidth = getVectorWidth(); 5453b11a16aSHongbin Zheng 5463b11a16aSHongbin Zheng HasVectorOperand = extractScalarValues(Inst, VectorMap, ScalarMaps); 5473b11a16aSHongbin Zheng 5483b11a16aSHongbin Zheng for (int VectorLane = 0; VectorLane < getVectorWidth(); VectorLane++) 5499d10fffaSSebastian Pop copyInstScalar(Inst, ScalarMaps[VectorLane], GlobalMaps[VectorLane], 5509d10fffaSSebastian Pop VLTS[VectorLane]); 5513b11a16aSHongbin Zheng 5523b11a16aSHongbin Zheng if (!VectorType::isValidElementType(Inst->getType()) || !HasVectorOperand) 5533b11a16aSHongbin Zheng return; 5543b11a16aSHongbin Zheng 5553b11a16aSHongbin Zheng // Make the result available as vector value. 5563b11a16aSHongbin Zheng VectorType *VectorType = VectorType::get(Inst->getType(), VectorWidth); 5573b11a16aSHongbin Zheng Value *Vector = UndefValue::get(VectorType); 5583b11a16aSHongbin Zheng 5593b11a16aSHongbin Zheng for (int i = 0; i < VectorWidth; i++) 5603b11a16aSHongbin Zheng Vector = Builder.CreateInsertElement(Vector, ScalarMaps[i][Inst], 5613b11a16aSHongbin Zheng Builder.getInt32(i)); 5623b11a16aSHongbin Zheng 5633b11a16aSHongbin Zheng VectorMap[Inst] = Vector; 5643b11a16aSHongbin Zheng } 5653b11a16aSHongbin Zheng 566c14582f2STobias Grosser int VectorBlockGenerator::getVectorWidth() { return GlobalMaps.size(); } 5673b11a16aSHongbin Zheng 5683b11a16aSHongbin Zheng void VectorBlockGenerator::copyInstruction(const Instruction *Inst, 5693b11a16aSHongbin Zheng ValueMapT &VectorMap, 5703b11a16aSHongbin Zheng VectorValueMapT &ScalarMaps) { 5713b11a16aSHongbin Zheng // Terminator instructions control the control flow. They are explicitly 5723b11a16aSHongbin Zheng // expressed in the clast and do not need to be copied. 5733b11a16aSHongbin Zheng if (Inst->isTerminator()) 5743b11a16aSHongbin Zheng return; 5753b11a16aSHongbin Zheng 576ecfe21b7STobias Grosser if (canSynthesize(Inst, &P->getAnalysis<LoopInfo>(), &SE, 577ecfe21b7STobias Grosser &Statement.getParent()->getRegion())) 578e71c6ab5STobias Grosser return; 579e71c6ab5STobias Grosser 5803b11a16aSHongbin Zheng if (const LoadInst *Load = dyn_cast<LoadInst>(Inst)) { 5813b11a16aSHongbin Zheng generateLoad(Load, VectorMap, ScalarMaps); 5823b11a16aSHongbin Zheng return; 5833b11a16aSHongbin Zheng } 5843b11a16aSHongbin Zheng 5853b11a16aSHongbin Zheng if (hasVectorOperands(Inst, VectorMap)) { 5863b11a16aSHongbin Zheng if (const StoreInst *Store = dyn_cast<StoreInst>(Inst)) { 5873b11a16aSHongbin Zheng copyStore(Store, VectorMap, ScalarMaps); 5883b11a16aSHongbin Zheng return; 5893b11a16aSHongbin Zheng } 5903b11a16aSHongbin Zheng 5913b11a16aSHongbin Zheng if (const UnaryInstruction *Unary = dyn_cast<UnaryInstruction>(Inst)) { 5923b11a16aSHongbin Zheng copyUnaryInst(Unary, VectorMap, ScalarMaps); 5933b11a16aSHongbin Zheng return; 5943b11a16aSHongbin Zheng } 5953b11a16aSHongbin Zheng 5963b11a16aSHongbin Zheng if (const BinaryOperator *Binary = dyn_cast<BinaryOperator>(Inst)) { 5973b11a16aSHongbin Zheng copyBinaryInst(Binary, VectorMap, ScalarMaps); 5983b11a16aSHongbin Zheng return; 5993b11a16aSHongbin Zheng } 6003b11a16aSHongbin Zheng 6013b11a16aSHongbin Zheng // Falltrough: We generate scalar instructions, if we don't know how to 6023b11a16aSHongbin Zheng // generate vector code. 6033b11a16aSHongbin Zheng } 6043b11a16aSHongbin Zheng 6053b11a16aSHongbin Zheng copyInstScalarized(Inst, VectorMap, ScalarMaps); 6063b11a16aSHongbin Zheng } 6073b11a16aSHongbin Zheng 6083b11a16aSHongbin Zheng void VectorBlockGenerator::copyBB() { 6093b11a16aSHongbin Zheng BasicBlock *BB = Statement.getBasicBlock(); 610c14582f2STobias Grosser BasicBlock *CopyBB = 611c14582f2STobias Grosser SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), P); 6123b11a16aSHongbin Zheng CopyBB->setName("polly.stmt." + BB->getName()); 6133b11a16aSHongbin Zheng Builder.SetInsertPoint(CopyBB->begin()); 6143b11a16aSHongbin Zheng 6153b11a16aSHongbin Zheng // Create two maps that store the mapping from the original instructions of 6163b11a16aSHongbin Zheng // the old basic block to their copies in the new basic block. Those maps 6173b11a16aSHongbin Zheng // are basic block local. 6183b11a16aSHongbin Zheng // 6193b11a16aSHongbin Zheng // As vector code generation is supported there is one map for scalar values 6203b11a16aSHongbin Zheng // and one for vector values. 6213b11a16aSHongbin Zheng // 6223b11a16aSHongbin Zheng // In case we just do scalar code generation, the vectorMap is not used and 6233b11a16aSHongbin Zheng // the scalarMap has just one dimension, which contains the mapping. 6243b11a16aSHongbin Zheng // 6253b11a16aSHongbin Zheng // In case vector code generation is done, an instruction may either appear 6263b11a16aSHongbin Zheng // in the vector map once (as it is calculating >vectorwidth< values at a 6273b11a16aSHongbin Zheng // time. Or (if the values are calculated using scalar operations), it 6283b11a16aSHongbin Zheng // appears once in every dimension of the scalarMap. 6293b11a16aSHongbin Zheng VectorValueMapT ScalarBlockMap(getVectorWidth()); 6303b11a16aSHongbin Zheng ValueMapT VectorBlockMap; 6313b11a16aSHongbin Zheng 63291f5b262STobias Grosser for (Instruction &Inst : *BB) 63391f5b262STobias Grosser copyInstruction(&Inst, VectorBlockMap, ScalarBlockMap); 6343b11a16aSHongbin Zheng } 635