================
@@ -4955,6 +4958,95 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, 
SourceLocation Loc,
     Fn->setSubprogram(SP);
 }
 
+bool CGDebugInfo::generateVirtualCallSite() const {
+  // Check general conditions for call site generation.
+  return (getCallSiteRelatedAttrs() != llvm::DINode::FlagZero);
+}
+
+// Set the 'call_target' metadata in the call instruction.
+void CGDebugInfo::addCallTargetMetadata(llvm::MDNode *MD, llvm::CallBase *CI) {
+  if (!MD || !CI)
+    return;
+  CI->setMetadata(llvm::LLVMContext::MD_call_target, MD);
+}
+
+// Finalize call_target generation.
+void CGDebugInfo::finalizeCallTarget() {
+  if (!generateVirtualCallSite())
+    return;
+
+  for (auto &E : CallTargetCache) {
+    for (const auto &WH : E.second.second) {
+      llvm::CallBase *CI = dyn_cast_or_null<llvm::CallBase>(WH);
+      addCallTargetMetadata(E.second.first, CI);
+    }
+  }
+}
+
+void CGDebugInfo::addCallTarget(StringRef Name, llvm::MDNode *MD,
+                                llvm::CallBase *CI) {
+  if (!generateVirtualCallSite())
+    return;
+
+  // Record only indirect calls.
+  if (CI && !CI->isIndirectCall())
+    return;
+
+  // Nothing to do.
+  if (Name.empty())
+    return;
+
+  auto It = CallTargetCache.find(Name);
+  if (It == CallTargetCache.end()) {
+    // First time we see 'Name'. Insert record for later finalize.
+    InstrList List;
+    if (CI)
+      List.push_back(CI);
+    CallTargetCache.try_emplace(Name, MD, std::move(List));
+  } else {
+    if (MD)
+      It->second.first.reset(MD);
+    if (CI) {
+      InstrList &List = It->second.second;
+      List.push_back(CI);
+    }
+  }
+}
+
+void CGDebugInfo::addCallTarget(llvm::Function *F, const FunctionDecl *FD,
+                                llvm::CallBase *CI) {
+  if (!generateVirtualCallSite())
+    return;
+
+  if (!F && !FD)
+    return;
+
+  // Ignore method types that never can be indirect calls.
+  if (!F && (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD) ||
+             FD->hasAttr<CUDAGlobalAttr>()))
+    return;
+
+  StringRef Name = (F && F->hasName()) ? F->getName() : CGM.getMangledName(FD);
+  addCallTarget(Name, /*MD=*/nullptr, CI);
+}
+
+void CGDebugInfo::removeCallTarget(StringRef Name) {
+  if (!generateVirtualCallSite())
+    return;
+
+  auto It = CallTargetCache.find(Name);
+  if (It != CallTargetCache.end())
+    CallTargetCache.erase(It);
----------------
CarlosAlbertoEnciso wrote:

Nice suggestion. Done it.

https://github.com/llvm/llvm-project/pull/167666
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to