diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h b/llvm/include/llvm/CodeGen/MachineInstr.h
--- a/llvm/include/llvm/CodeGen/MachineInstr.h
+++ b/llvm/include/llvm/CodeGen/MachineInstr.h
@@ -44,6 +44,7 @@
 template <typename T> class ArrayRef;
 class DIExpression;
 class DILocalVariable;
+class Function;
 class MachineBasicBlock;
 class MachineFunction;
 class MachineRegisterInfo;
@@ -872,6 +873,9 @@
   bool isCall(QueryType Type = AnyInBundle) const {
     return hasProperty(MCID::Call, Type);
   }
+  /// Return a pointer to the Function being called, or nullptr if MI is not
+  /// a call instruction or is an indirect call.
+  const Function *getCalledFunction() const;
 
   /// Return true if this is a call instruction that may have an associated
   /// call site entry in the debug info.
diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp
--- a/llvm/lib/CodeGen/MachineInstr.cpp
+++ b/llvm/lib/CodeGen/MachineInstr.cpp
@@ -2378,3 +2378,11 @@
     DebugInstrNum = MF.getNewDebugInstrNum();
   return DebugInstrNum;
 }
+
+const Function *MachineInstr::getCalledFunction() const {
+  for (const MachineOperand &MO : operands())
+    if (MO.isGlobal())
+      if (const auto *F = dyn_cast<Function>(MO.getGlobal()))
+        return F;
+  return nullptr;
+}
diff --git a/llvm/lib/CodeGen/MachineRegisterInfo.cpp b/llvm/lib/CodeGen/MachineRegisterInfo.cpp
--- a/llvm/lib/CodeGen/MachineRegisterInfo.cpp
+++ b/llvm/lib/CodeGen/MachineRegisterInfo.cpp
@@ -542,17 +542,6 @@
   }
 }
 
-static const Function *getCalledFunction(const MachineInstr &MI) {
-  for (const MachineOperand &MO : MI.operands()) {
-    if (!MO.isGlobal())
-      continue;
-    const Function *Func = dyn_cast<Function>(MO.getGlobal());
-    if (Func != nullptr)
-      return Func;
-  }
-  return nullptr;
-}
-
 static bool isNoReturnDef(const MachineOperand &MO) {
   // Anything which is not a noreturn function is a real def.
   const MachineInstr &MI = *MO.getParent();
@@ -566,7 +555,7 @@
   // not return, since the runtime may need it.
   if (MF.getFunction().hasFnAttribute(Attribute::UWTable))
     return false;
-  const Function *Called = getCalledFunction(MI);
+  const Function *Called = MI.getCalledFunction();
   return !(Called == nullptr || !Called->hasFnAttribute(Attribute::NoReturn) ||
            !Called->hasFnAttribute(Attribute::NoUnwind));
 }