diff --git a/llvm/include/llvm/CodeGen/TargetRegisterInfo.h b/llvm/include/llvm/CodeGen/TargetRegisterInfo.h --- a/llvm/include/llvm/CodeGen/TargetRegisterInfo.h +++ b/llvm/include/llvm/CodeGen/TargetRegisterInfo.h @@ -415,6 +415,16 @@ virtual Register lookThruCopyLike(Register SrcReg, const MachineRegisterInfo *MRI) const; + /// Find the original SrcReg unless it is the target of a copy-like operation, + /// in which case we chain backwards through all such operations to the + /// ultimate source register. If a physical register is encountered, we stop + /// the search. + /// Return the original SrcReg if all the definitions in the chain only have + /// one user and not a physical register. + virtual Register + lookThruSingleUseCopyChain(Register SrcReg, + const MachineRegisterInfo *MRI) const; + /// Return a null-terminated list of all of the callee-saved registers on /// this target. The register should be in the order of desired callee-save /// stack frame offset. The first register is closest to the incoming stack diff --git a/llvm/lib/CodeGen/TargetRegisterInfo.cpp b/llvm/lib/CodeGen/TargetRegisterInfo.cpp --- a/llvm/lib/CodeGen/TargetRegisterInfo.cpp +++ b/llvm/lib/CodeGen/TargetRegisterInfo.cpp @@ -533,6 +533,31 @@ } } +Register TargetRegisterInfo::lookThruSingleUseCopyChain( + Register SrcReg, const MachineRegisterInfo *MRI) const { + while (true) { + const MachineInstr *MI = MRI->getVRegDef(SrcReg); + // Found the real definition, return it if it has a single use. + if (!MI->isCopyLike()) + return MRI->hasOneNonDBGUse(SrcReg) ? SrcReg : Register(); + + Register CopySrcReg; + if (MI->isCopy()) + CopySrcReg = MI->getOperand(1).getReg(); + else { + assert(MI->isSubregToReg() && "Bad opcode for lookThruCopyLike"); + CopySrcReg = MI->getOperand(2).getReg(); + } + + // Continue only if the next definition in the chain is for a virtual + // register that has a single use. + if (!CopySrcReg.isVirtual() || !MRI->hasOneNonDBGUse(CopySrcReg)) + return Register(); + + SrcReg = CopySrcReg; + } +} + void TargetRegisterInfo::getOffsetOpcodes( const StackOffset &Offset, SmallVectorImpl &Ops) const { assert(!Offset.getScalable() && "Scalable offsets are not handled");