Skip to content

Commit 8120cfe

Browse files
author
Chen Zheng
committed
[NFC] [TargetRegisterInfo] add another API to get srcreg through copy.
Reviewed By: nemanjai, jsji Differential Revision: https://reviews.llvm.org/D92069
1 parent 2de5ea3 commit 8120cfe

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

llvm/include/llvm/CodeGen/TargetRegisterInfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,16 @@ class TargetRegisterInfo : public MCRegisterInfo {
415415
virtual Register lookThruCopyLike(Register SrcReg,
416416
const MachineRegisterInfo *MRI) const;
417417

418+
/// Find the original SrcReg unless it is the target of a copy-like operation,
419+
/// in which case we chain backwards through all such operations to the
420+
/// ultimate source register. If a physical register is encountered, we stop
421+
/// the search.
422+
/// Return the original SrcReg if all the definitions in the chain only have
423+
/// one user and not a physical register.
424+
virtual Register
425+
lookThruSingleUseCopyChain(Register SrcReg,
426+
const MachineRegisterInfo *MRI) const;
427+
418428
/// Return a null-terminated list of all of the callee-saved registers on
419429
/// this target. The register should be in the order of desired callee-save
420430
/// stack frame offset. The first register is closest to the incoming stack

llvm/lib/CodeGen/TargetRegisterInfo.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,31 @@ TargetRegisterInfo::lookThruCopyLike(Register SrcReg,
533533
}
534534
}
535535

536+
Register TargetRegisterInfo::lookThruSingleUseCopyChain(
537+
Register SrcReg, const MachineRegisterInfo *MRI) const {
538+
while (true) {
539+
const MachineInstr *MI = MRI->getVRegDef(SrcReg);
540+
// Found the real definition, return it if it has a single use.
541+
if (!MI->isCopyLike())
542+
return MRI->hasOneNonDBGUse(SrcReg) ? SrcReg : Register();
543+
544+
Register CopySrcReg;
545+
if (MI->isCopy())
546+
CopySrcReg = MI->getOperand(1).getReg();
547+
else {
548+
assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike");
549+
CopySrcReg = MI->getOperand(2).getReg();
550+
}
551+
552+
// Continue only if the next definition in the chain is for a virtual
553+
// register that has a single use.
554+
if (!CopySrcReg.isVirtual() || !MRI->hasOneNonDBGUse(CopySrcReg))
555+
return Register();
556+
557+
SrcReg = CopySrcReg;
558+
}
559+
}
560+
536561
void TargetRegisterInfo::getOffsetOpcodes(
537562
const StackOffset &Offset, SmallVectorImpl<uint64_t> &Ops) const {
538563
assert(!Offset.getScalable() && "Scalable offsets are not handled");

0 commit comments

Comments
 (0)