ExtractFunction.cpp revision afade9294af43c6b947b9aeaa1555883d5f853e3
1//===- ExtractFunction.cpp - Extract a function from Program --------------===//
2//
3// This file implements a method that extracts a function from program, cleans
4// it up, and returns it as a new module.
5//
6//===----------------------------------------------------------------------===//
7
8#include "BugDriver.h"
9#include "llvm/Module.h"
10#include "llvm/PassManager.h"
11#include "llvm/Transforms/IPO.h"
12#include "llvm/Transforms/Utils/Cloning.h"
13
14/// extractFunctionFromModule - This method is used to extract the specified
15/// (non-external) function from the current program, slim down the module, and
16/// then return it.  This does not modify Program at all, it modifies a copy,
17/// which it returns.
18Module *BugDriver::extractFunctionFromModule(Function *F) const {
19  Module *Result = CloneModule(Program);
20
21  // Translate from the old module to the new copied module...
22  F = Result->getFunction(F->getName(), F->getFunctionType());
23
24  // In addition to just parsing the input from GCC, we also want to spiff it up
25  // a little bit.  Do this now.
26  //
27  PassManager Passes;
28  Passes.add(createFunctionExtractionPass(F));    // Extract the function
29  Passes.add(createGlobalDCEPass());              // Delete unreachable globals
30  Passes.add(createFunctionResolvingPass());      // Delete prototypes
31  Passes.add(createDeadTypeEliminationPass());    // Remove dead types...
32  Passes.run(*Result);
33  return Result;
34}
35