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 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 BlockList; + typedef std::pair FuncInfo; + typedef std::map 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); + } +} +