130 #include "llvm/IR/IntrinsicsHexagon.h" 150 using namespace llvm;
152 #define DEBUG_TYPE "hexagon-vlcr" 154 STATISTIC(HexagonNumVectorLoopCarriedReuse,
155 "Number of values that were reused from a previous iteration.");
159 cl::desc(
"Maximum distance of loop carried dependences that are handled"),
175 ChainOfDependences Chain;
178 bool isIdentical(DepChain &
Other)
const {
179 if (Other.size() !=
size())
181 ChainOfDependences &OtherChain = Other.getChain();
182 for (
int i = 0; i <
size(); ++i) {
183 if (Chain[i] != OtherChain[i])
189 ChainOfDependences &getChain() {
205 int iterations()
const {
210 return Chain.front();
226 const ChainOfDependences &CD = D.Chain;
227 int ChainSize = CD.size();
228 OS <<
"**DepChain Start::**\n";
229 for (
int i = 0; i < ChainSize -1; ++i) {
230 OS << *(CD[i]) <<
" -->\n";
232 OS << *CD[ChainSize-1] <<
"\n";
243 std::map<Instruction *, DepChain *> DepChains;
246 ReuseValue() =
default;
249 Inst2Replace =
nullptr;
250 BackedgeInst =
nullptr;
254 bool isDefined() {
return Inst2Replace !=
nullptr; }
259 OS <<
"** ReuseValue ***\n";
260 OS <<
"Instruction to Replace: " << *(RU.Inst2Replace) <<
"\n";
261 OS <<
"Backedge Instruction: " << *(RU.BackedgeInst) <<
"\n";
265 class HexagonVectorLoopCarriedReuse :
public LoopPass {
269 explicit HexagonVectorLoopCarriedReuse() :
LoopPass(ID) {
275 return "Hexagon-specific loop carried reuse for HVX vectors";
290 std::set<Instruction *> ReplacedInsts;
292 ReuseValue ReuseCandidate;
295 void findLoopCarriedDeps();
296 void findValueToReuse();
297 void findDepChainFromPHI(
Instruction *I, DepChain &D);
311 "Hexagon-specific predictive commoning for HVX vectors",
false,
false)
322 if (!L->getLoopPreheader())
326 if (!L->getSubLoops().empty())
330 if (L->getNumBlocks() != 1)
338 bool HexagonVectorLoopCarriedReuse::isCallInstCommutative(
CallInst *
C) {
340 case Intrinsic::hexagon_V6_vaddb:
341 case Intrinsic::hexagon_V6_vaddb_128B:
342 case Intrinsic::hexagon_V6_vaddh:
343 case Intrinsic::hexagon_V6_vaddh_128B:
344 case Intrinsic::hexagon_V6_vaddw:
345 case Intrinsic::hexagon_V6_vaddw_128B:
346 case Intrinsic::hexagon_V6_vaddubh:
347 case Intrinsic::hexagon_V6_vaddubh_128B:
348 case Intrinsic::hexagon_V6_vadduhw:
349 case Intrinsic::hexagon_V6_vadduhw_128B:
350 case Intrinsic::hexagon_V6_vaddhw:
351 case Intrinsic::hexagon_V6_vaddhw_128B:
352 case Intrinsic::hexagon_V6_vmaxb:
353 case Intrinsic::hexagon_V6_vmaxb_128B:
354 case Intrinsic::hexagon_V6_vmaxh:
355 case Intrinsic::hexagon_V6_vmaxh_128B:
356 case Intrinsic::hexagon_V6_vmaxw:
357 case Intrinsic::hexagon_V6_vmaxw_128B:
358 case Intrinsic::hexagon_V6_vmaxub:
359 case Intrinsic::hexagon_V6_vmaxub_128B:
360 case Intrinsic::hexagon_V6_vmaxuh:
361 case Intrinsic::hexagon_V6_vmaxuh_128B:
362 case Intrinsic::hexagon_V6_vminub:
363 case Intrinsic::hexagon_V6_vminub_128B:
364 case Intrinsic::hexagon_V6_vminuh:
365 case Intrinsic::hexagon_V6_vminuh_128B:
366 case Intrinsic::hexagon_V6_vminb:
367 case Intrinsic::hexagon_V6_vminb_128B:
368 case Intrinsic::hexagon_V6_vminh:
369 case Intrinsic::hexagon_V6_vminh_128B:
370 case Intrinsic::hexagon_V6_vminw:
371 case Intrinsic::hexagon_V6_vminw_128B:
372 case Intrinsic::hexagon_V6_vmpyub:
373 case Intrinsic::hexagon_V6_vmpyub_128B:
374 case Intrinsic::hexagon_V6_vmpyuh:
375 case Intrinsic::hexagon_V6_vmpyuh_128B:
376 case Intrinsic::hexagon_V6_vavgub:
377 case Intrinsic::hexagon_V6_vavgub_128B:
378 case Intrinsic::hexagon_V6_vavgh:
379 case Intrinsic::hexagon_V6_vavgh_128B:
380 case Intrinsic::hexagon_V6_vavguh:
381 case Intrinsic::hexagon_V6_vavguh_128B:
382 case Intrinsic::hexagon_V6_vavgw:
383 case Intrinsic::hexagon_V6_vavgw_128B:
384 case Intrinsic::hexagon_V6_vavgb:
385 case Intrinsic::hexagon_V6_vavgb_128B:
386 case Intrinsic::hexagon_V6_vavguw:
387 case Intrinsic::hexagon_V6_vavguw_128B:
388 case Intrinsic::hexagon_V6_vabsdiffh:
389 case Intrinsic::hexagon_V6_vabsdiffh_128B:
390 case Intrinsic::hexagon_V6_vabsdiffub:
391 case Intrinsic::hexagon_V6_vabsdiffub_128B:
392 case Intrinsic::hexagon_V6_vabsdiffuh:
393 case Intrinsic::hexagon_V6_vabsdiffuh_128B:
394 case Intrinsic::hexagon_V6_vabsdiffw:
395 case Intrinsic::hexagon_V6_vabsdiffw_128B:
402 bool HexagonVectorLoopCarriedReuse::isEquivalentOperation(
Instruction *I1,
410 if (
CallInst *C1 = dyn_cast<CallInst>(I1)) {
411 if (
CallInst *C2 = dyn_cast<CallInst>(I2)) {
412 if (C1->getCalledFunction() != C2->getCalledFunction())
421 for (
unsigned i = 0; i < NumOperands; ++i) {
434 bool HexagonVectorLoopCarriedReuse::canReplace(
Instruction *I) {
440 case Intrinsic::hexagon_V6_hi:
441 case Intrinsic::hexagon_V6_lo:
442 case Intrinsic::hexagon_V6_hi_128B:
443 case Intrinsic::hexagon_V6_lo_128B:
444 LLVM_DEBUG(
dbgs() <<
"Not considering for reuse: " << *II <<
"\n");
450 void HexagonVectorLoopCarriedReuse::findValueToReuse() {
451 for (
auto *D : Dependences) {
452 LLVM_DEBUG(
dbgs() <<
"Processing dependence " << *(D->front()) <<
"\n");
456 <<
".. Skipping because number of iterations > than the limit\n");
460 PHINode *PN = cast<PHINode>(D->front());
462 int Iters = D->iterations();
465 <<
" can be reused\n");
474 if (ReplacedInsts.count(User)) {
476 <<
" has already been replaced. Skipping...\n");
479 if (isa<PHINode>(User))
483 if (!canReplace(User))
497 for (
auto UI = BEInst->use_begin(),
E = BEInst->use_end(); UI !=
E;
504 if (!isEquivalentOperation(I, BEUser))
518 std::map<Instruction *, DepChain *> DepChains;
520 if ((I && I->
isCommutative()) || (C1 && isCallInstCommutative(C1))) {
522 for (
int OpNo = 0; OpNo < NumOperands; ++OpNo) {
526 for (
int T = 0;
T < NumOperands; ++
T) {
529 if (!OpInst && !BEOpInst) {
536 if ((OpInst && !BEOpInst) || (!OpInst && BEOpInst))
539 DepChain *D = getDepChainBtwn(OpInst, BEOpInst, Iters);
543 DepChains[OpInst] =
D;
554 for (
int OpNo = 0; OpNo < NumOperands; ++OpNo) {
569 DepChain *D = getDepChainBtwn(OpInst, BEOpInst, Iters);
572 DepChains[OpInst] =
D;
581 ReuseCandidate.Inst2Replace =
I;
582 ReuseCandidate.BackedgeInst = BEUser;
583 ReuseCandidate.DepChains = DepChains;
584 ReuseCandidate.Iterations = Iters;
587 ReuseCandidate.reset();
591 ReuseCandidate.reset();
594 Value *HexagonVectorLoopCarriedReuse::findValueInBlock(
Value *
Op,
602 void HexagonVectorLoopCarriedReuse::reuseValue() {
604 Instruction *Inst2Replace = ReuseCandidate.Inst2Replace;
607 std::map<Instruction *, DepChain *> &DepChains = ReuseCandidate.DepChains;
608 int Iterations = ReuseCandidate.Iterations;
609 BasicBlock *LoopPH = CurLoop->getLoopPreheader();
610 assert(!DepChains.empty() &&
"No DepChains");
611 LLVM_DEBUG(
dbgs() <<
"reuseValue is making the following changes\n");
614 for (
int i = 0; i < Iterations; ++i) {
617 for (
int j = 0; j < NumOperands; ++j) {
622 DepChain &D = *DepChains[
I];
626 Value *ValInPreheader = findValueInBlock(D[i], LoopPH);
627 InstInPreheader->
setOperand(j, ValInPreheader);
629 InstsInPreheader.
push_back(InstInPreheader);
630 InstInPreheader->
setName(Inst2Replace->
getName() +
".hexagon.vlcr");
638 Value *BEVal = BEInst;
640 for (
int i = Iterations-1; i >=0 ; --i) {
641 Instruction *InstInPreheader = InstsInPreheader[i];
652 ReplacedInsts.insert(Inst2Replace);
653 ++HexagonNumVectorLoopCarriedReuse;
656 bool HexagonVectorLoopCarriedReuse::doVLCR() {
657 assert(CurLoop->getSubLoops().empty() &&
658 "Can do VLCR on the innermost loop only");
659 assert((CurLoop->getNumBlocks() == 1) &&
660 "Can do VLCR only on single block loops");
662 bool Changed =
false;
665 LLVM_DEBUG(
dbgs() <<
"Working on Loop: " << *CurLoop->getHeader() <<
"\n");
671 findLoopCarriedDeps();
673 if (ReuseCandidate.isDefined()) {
683 void HexagonVectorLoopCarriedReuse::findDepChainFromPHI(
Instruction *I,
691 if (NumIncomingValues != 2) {
697 if (BB != CurLoop->getHeader()) {
706 assert(BEInst &&
"There should be a value over the backedge");
710 if(!PreHdrVal || !isa<Instruction>(PreHdrVal)) {
715 findDepChainFromPHI(BEInst, D);
719 DepChain *HexagonVectorLoopCarriedReuse::getDepChainBtwn(
Instruction *I1,
722 for (
auto *D : Dependences) {
723 if (D->front() == I1 && D->back() == I2 && D->iterations() == Iters)
729 void HexagonVectorLoopCarriedReuse::findLoopCarriedDeps() {
731 for (
auto I = BB->
begin(),
E = BB->
end(); I !=
E && isa<PHINode>(
I); ++
I) {
732 auto *PN = cast<PHINode>(
I);
733 if (!isa<VectorType>(PN->getType()))
736 DepChain *D =
new DepChain();
737 findDepChainFromPHI(PN, *D);
739 Dependences.insert(D);
743 LLVM_DEBUG(
dbgs() <<
"Found " << Dependences.size() <<
" dependences\n");
744 LLVM_DEBUG(
for (
size_t i = 0; i < Dependences.size();
745 ++i) {
dbgs() << *Dependences[i] <<
"\n"; });
749 return new HexagonVectorLoopCarriedReuse();
Pass interface - Implemented by all 'passes'.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
bool isSameOperationAs(const Instruction *I, unsigned flags=0) const
This function determines if the specified instruction executes the same operation as the current one...
This class represents lattice values for constants.
void push_back(const T &Elt)
This class represents a function call, abstracting a target machine's calling convention.
static cl::opt< int > HexagonVLCRIterationLim("hexagon-vlcr-iteration-lim", cl::Hidden, cl::desc("Maximum distance of loop carried dependences that are handled"), cl::init(2), cl::ZeroOrMore)
STATISTIC(NumFunctions, "Total number of functions")
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isVectorTy() const
True if this is an instance of VectorType.
This defines the Use class.
iterator begin()
Instruction iterator methods.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
hexagon Hexagon specific predictive commoning for HVX vectors
A Use represents the edge between a Value definition and its users.
INITIALIZE_PASS_BEGIN(HexagonVectorLoopCarriedReuse, "hexagon-vlcr", "Hexagon-specific predictive commoning for HVX vectors", false, false) INITIALIZE_PASS_END(HexagonVectorLoopCarriedReuse
void setName(const Twine &Name)
Change the name of the value.
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Type * getType() const
All values are typed, get the type of this value.
AnalysisUsage & addPreservedID(const void *ID)
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Value * getOperand(unsigned i) const
void initializeHexagonVectorLoopCarriedReusePass(PassRegistry &)
initializer< Ty > init(const Ty &Val)
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction...
LLVM Basic Block Representation.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
Represent the analysis usage information of a pass.
#define LLVM_ATTRIBUTE_UNUSED
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
unsigned getNumOperands() const
This is the shared class of boolean and integer constants.
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
AnalysisUsage & addRequiredID(const void *ID)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
void setPreservesCFG()
This function should be called by the pass, iff they do not:
unsigned getNumIncomingValues() const
Return the number of incoming edges.
bool isCommutative() const
Return true if the instruction is commutative:
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static void clear(coro::Shape &Shape)
Pass * createHexagonVectorLoopCarriedReusePass()
Represents a single loop in the control flow graph.
StringRef getName() const
Return a constant reference to the value's name.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
A vector that has set insertion semantics.
This class implements an extremely fast bulk output stream that can only output to a stream...
The legacy pass manager's analysis pass to compute loop information.
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
UnaryPredicate for_each(R &&Range, UnaryPredicate P)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly...
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const