[PATCH] D21657: [MachineDominatorTree] Add a verifier for the MDT. NFC.

Chad Rosier via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 23 13:00:50 PDT 2016


mcrosier created this revision.
mcrosier added reviewers: iteratee, haicheng, gberry.
mcrosier added subscribers: echristo, llvm-commits.

For the most part, this mirrors the verified for the IR level Dominator tree.

By default the verifier is off until we can make sure the MDT is being properly preserved.  Once any/all issues have been address we should default 'VerifyMachineDomInfo' to true when expensive checks are enabled.

I still need to add test cases, which will soon follow.  This is done by adding '-verify-machine-dom-info' to a RUN command for an existing (or new) lit test.

Hopefully, this will help shake out any bugs.

 Chad

http://reviews.llvm.org/D21657

Files:
  include/llvm/CodeGen/MachineDominators.h
  lib/CodeGen/MachineDominators.cpp

Index: lib/CodeGen/MachineDominators.cpp
===================================================================
--- lib/CodeGen/MachineDominators.cpp
+++ lib/CodeGen/MachineDominators.cpp
@@ -15,9 +15,20 @@
 #include "llvm/CodeGen/MachineDominators.h"
 #include "llvm/CodeGen/Passes.h"
 #include "llvm/ADT/SmallBitVector.h"
+#include "llvm/Support/CommandLine.h"
 
 using namespace llvm;
 
+// FIXME: Always verify dominfo if expensive checking is enabled.
+#ifdef EXPENSIVE_CHECKS
+static bool VerifyMachineDomInfo = false;
+#else
+static bool VerifyMachineDomInfo = false;
+#endif
+static cl::opt<bool,true>
+VerifyMachineDomInfoX("verify-machine-dom-info", cl::location(VerifyMachineDomInfo),
+               cl::desc("Verify machine dominator info (time consuming)"));
+
 namespace llvm {
 template class DomTreeNodeBase<MachineBasicBlock>;
 template class DominatorTreeBase<MachineBasicBlock>;
@@ -57,6 +68,11 @@
   DT->releaseMemory();
 }
 
+void MachineDominatorTree::verifyAnalysis() const {
+  if (VerifyMachineDomInfo)
+    verifyDomTree();
+}
+
 void MachineDominatorTree::print(raw_ostream &OS, const Module*) const {
   DT->print(OS);
 }
@@ -125,3 +141,17 @@
   NewBBs.clear();
   CriticalEdgesToSplit.clear();
 }
+
+void MachineDominatorTree::verifyDomTree() const {
+  MachineFunction &F = *getRoot()->getParent();
+
+  MachineDominatorTree OtherDT;
+  OtherDT.DT->recalculate(F);
+  if (compare(OtherDT)) {
+    errs() << "MachineDominatorTree is not up to date!\nComputed:\n";
+    print(errs(), nullptr);
+    errs() << "\nActual:\n";
+    OtherDT.print(errs(), nullptr);
+    abort();
+  }
+}
Index: include/llvm/CodeGen/MachineDominators.h
===================================================================
--- include/llvm/CodeGen/MachineDominators.h
+++ include/llvm/CodeGen/MachineDominators.h
@@ -216,6 +216,8 @@
 
   void releaseMemory() override;
 
+  void verifyAnalysis() const override;
+
   void print(raw_ostream &OS, const Module*) const override;
 
   /// \brief Record that the critical edge (FromBB, ToBB) has been
@@ -239,6 +241,27 @@
            "A basic block inserted via edge splitting cannot appear twice");
     CriticalEdgesToSplit.push_back({FromBB, ToBB, NewBB});
   }
+
+  /// \brief Returns *false* if the other dominator tree matches this dominator
+  /// tree.
+  inline bool compare(const MachineDominatorTree &Other) const {
+    const MachineDomTreeNode *R = getRootNode();
+    const MachineDomTreeNode *OtherR = Other.getRootNode();
+
+    if (!R || !OtherR || R->getBlock() != OtherR->getBlock())
+      return true;
+
+    if (DT->compare(*Other.DT))
+      return true;
+
+    return false;
+  }
+
+  /// \brief Verify the correctness of the domtree by re-computing it.
+  ///
+  /// This should only be used for debugging as it aborts the program if the
+  /// verification fails.
+  void verifyDomTree() const;
 };
 
 //===-------------------------------------


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21657.61710.patch
Type: text/x-patch
Size: 2918 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160623/48bde7fd/attachment.bin>


More information about the llvm-commits mailing list