================
@@ -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);
+    }
+  }
----------------
CarlosAlbertoEnciso wrote:

Nice suggestion, with just a minor change:
```
  InstrList List;
  auto It = CallTargetCache.insert(
      std::make_pair(Name, CallTargetEntry(MD, std::move(List))));
  if (MD)
    It.first->second.first.reset(MD);
  if (CI)
    It.first->second.second.push_back(CI);
```

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