[PATCH] D18524: Dump function as the only defined function in its module.

Yin Ma via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 28 13:49:44 PDT 2016


yinma created this revision.
yinma added a subscriber: llvm-commits.

This procedure could dump the module, where the function becomes the only defined function in its module. This is very useful to debug large code base where reducing from source code is difficult, like LTO.

http://reviews.llvm.org/D18524

Files:
  include/llvm/IR/Function.h
  lib/IR/Function.cpp

Index: lib/IR/Function.cpp
===================================================================
--- lib/IR/Function.cpp
+++ lib/IR/Function.cpp
@@ -1005,3 +1005,41 @@
       }
   return None;
 }
+
+void Function::dumpAsSingleDefine()
+{
+  typedef std::vector<BasicBlock*> BlockList;
+  typedef std::pair<BlockList*, GlobalValue::LinkageTypes> FuncInfo;
+  typedef std::map<Function*, FuncInfo> FuncMap;
+  FuncMap funcList;
+  // Temporarily change all defined functions to be declaration
+  // except for this one.
+  Module* M = getParent();
+  for (Function &F : *M)
+    if (!F.isDeclaration() && &F != this) {
+      BlockList* blockList = new BlockList;
+      Function::BasicBlockListType &oldBlocks = F.getBasicBlockList();
+      while (!oldBlocks.empty()) {
+        blockList->push_back(&oldBlocks.back());
+        oldBlocks.remove(oldBlocks.back());
+      }
+      funcList[&F]= std::make_pair(blockList, F.getLinkage());
+      F.setLinkage(ExternalLinkage);
+    }
+
+  M->dump();
+
+  // Restore everything back.
+  for (auto it : funcList) {
+    Function* F = it.first;
+    BlockList* list = it.second.first;
+    GlobalValue::LinkageTypes LT = it.second.second;
+    Function::BasicBlockListType &oldBlocks = F->getBasicBlockList();
+    while(!list->empty()) {
+      oldBlocks.push_back(list->back());
+      list->pop_back();
+    }
+    F->setLinkage(LT);
+  }
+}
+
Index: include/llvm/IR/Function.h
===================================================================
--- include/llvm/IR/Function.h
+++ include/llvm/IR/Function.h
@@ -555,6 +555,9 @@
              bool ShouldPreserveUseListOrder = false,
              bool IsForDebug = false) const;
 
+  /// Dump the function as it is the only defined function in module.
+  void dumpAsSingleDefine();
+
   /// viewCFG - This function is meant for use from the debugger.  You can just
   /// say 'call F->viewCFG()' and a ghostview window should pop up from the
   /// program, displaying the CFG of the current function with the code for each


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18524.51833.patch
Type: text/x-patch
Size: 2021 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160328/2e93672e/attachment.bin>


More information about the llvm-commits mailing list