[llvm] Fix: Distinguish CFI Metadata Checks in MergeFunctions Pass (PR #65963)

Duncan P. N. Exon Smith via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 11 07:15:37 PDT 2023


================
@@ -836,6 +837,54 @@ int FunctionComparator::cmpValues(const Value *L, const Value *R) const {
   return cmpNumbers(LeftSN.first->second, RightSN.first->second);
 }
 
+int FunctionComparator::cmpDistinctMetadata(const Function *L,
+                                            const Function *R) {
+  // Iterate over the basic blocks and instructions in Function L
+  for (const BasicBlock &BBL : *L) {
+    for (const Instruction &InstL : BBL) {
+      for (unsigned i = 0, e = InstL.getNumOperands(); i != e; ++i) {
+        if (MetadataAsValue *MDL =
+                dyn_cast<MetadataAsValue>(InstL.getOperand(i))) {
+          if (MDNode *MDNodeL = dyn_cast<MDNode>(MDL->getMetadata())) {
+            // Now iterate over the basic blocks and instructions in Function R
+            // to compare against each distinct metadata value in Function L
+            for (const BasicBlock &BBR : *R) {
+              for (const Instruction &InstR : BBR) {
+                for (unsigned j = 0, f = InstR.getNumOperands(); j != f; ++j) {
+                  if (MetadataAsValue *MDR =
+                          dyn_cast<MetadataAsValue>(InstR.getOperand(j))) {
+                    if (MDNode *MDNodeR =
+                            dyn_cast<MDNode>(MDR->getMetadata())) {
+                      // Compare distinct metadata
+                      if (MDNodeL->isDistinct() && MDNodeR->isDistinct()) {
+                        if (isa<DINode>(MDNodeL) && isa<DINode>(MDNodeR)) {
----------------
dexonsmith wrote:

I wonder, can/should you skip over debug info intrinsics entirely? They are specially marked. Also, I believe there are instruction iterators you can grab that will do this automatically.

@adrian-prantl, do you have a pointer for that?

https://github.com/llvm/llvm-project/pull/65963


More information about the llvm-commits mailing list