ExtractFunction.cpp revision d0fde30ce850b78371fd1386338350591f9ff494
1afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg//===- ExtractFunction.cpp - Extract a function from Program --------------===//
2be1b8e5d6c6692010a3ec117035d9b218929e2b3Brian//
325be043940b25a5fe6eb8058070b3e8f12e92039Brian Paul//                     The LLVM Compiler Infrastructure
45e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen//
59f6022d0567dc1288888212d7128acc48795b306Brian// This file was developed by the LLVM research group and is distributed under
680684649a6d01f0e0517b14f61cbcad6fa101929Brian Paul// the University of Illinois Open Source License. See LICENSE.TXT for details.
75e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen//
8afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg//===----------------------------------------------------------------------===//
9afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg//
10afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// This file implements a method that extracts a function from program, cleans
11afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg// it up, and returns it as a new module.
12afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg//
13afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg//===----------------------------------------------------------------------===//
145e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen
15afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "BugDriver.h"
16afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Constant.h"
175e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansen#include "llvm/Module.h"
18afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/PassManager.h"
19afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Pass.h"
20afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Type.h"
21afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Analysis/Verifier.h"
22afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Transforms/IPO.h"
23afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Transforms/Scalar.h"
24afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Transforms/Utils/Cloning.h"
25afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg#include "llvm/Target/TargetData.h"
266dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell#include "Support/CommandLine.h"
27add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul
28add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul
2977ee31930a1b0cc7766939415f4f04ed6a1fa4acBrian Paulnamespace llvm {
30add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul
31add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paulbool DisableSimplifyCFG = false;
32add9f2168a5d6b15eb9955ee761246c4f4cf8458Brian Paul
33fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul} // End llvm namespace
34b46712ca9d379d9c091f5543500088d82cf9776cBrian Paul
35afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtgusing namespace llvm;
36d100cbf721010f4eacc87507cc87c5314150d493Maciej Cencora
3734bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paulnamespace {
3834bd1233a9874fe12a822c4fcb926d48456e1f29Brian Paul  cl::opt<bool>
392897cee99fb877e1f3cd9a881a61418c9c31867fBrian Paul  NoADCE("disable-adce",
40afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         cl::desc("Do not use the -adce pass to reduce testcases"));
413c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul  cl::opt<bool>
42ebb248aa5c018dc676d389221d76ed329059789eBrian Paul  NoDCE ("disable-dce",
43fa4525e289b475b928a7b2c4055af9dd7fe46600Brian Paul         cl::desc("Do not use the -dce pass to reduce testcases"));
4489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul  cl::opt<bool, true>
451a2bb37264b4448d33f2948fe1702c9dc936395dBrian Paul  NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
46afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg         cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
47afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
487179a822628963d8cfa0817cf072c5acb70638a7Kristian Høgsberg
495e3bc0c2a2bcdf59949410f94c9b705fc1281ce8Jouk Jansennamespace llvm {
50afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
51afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/// deleteInstructionFromProgram - This method clones the current Program and
524cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul/// deletes the specified instruction from the cloned module.  It then runs a
5363f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
5463f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul/// depends on the value.  The modified module is then returned.
5563f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul///
5663f01309801c5a900d8d7f5ccd63413e33ff9bffBrian PaulModule *BugDriver::deleteInstructionFromProgram(Instruction *I,
5763f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul                                                unsigned Simplification) const {
5863f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul  Module *Result = CloneModule(Program);
5963f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul
6063f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul  BasicBlock *PBB = I->getParent();
6163f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul  Function *PF = PBB->getParent();
6263f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul
6363f01309801c5a900d8d7f5ccd63413e33ff9bffBrian Paul  Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
644cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
654cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
664cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
674cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
684cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
694cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
704cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
714cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  I = RI;                                 // Got the corresponding instruction!
724cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
734cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  // If this instruction produces a value, replace any users with null values
744cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  if (I->getType() != Type::VoidTy)
754cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul    I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
764cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
774cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  // Remove the instruction from the program.
784cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  I->getParent()->getInstList().erase(I);
794cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul
804cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  // Spiff up the output a little bit.
814cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  PassManager Passes;
824cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  // Make sure that the appropriate target data is always used...
834cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul  Passes.add(new TargetData("bugpoint", Result));
84f7b5707d66678f09bec652ecce024a0da6cc4a4bBrian Paul
85887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul  if (Simplification > 2 && !NoADCE)
86973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul    Passes.add(createAggressiveDCEPass());          // Remove dead code...
87afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  //Passes.add(createInstructionCombiningPass());
88fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul  if (Simplification > 1 && !NoDCE)
89fbd8f212c3866ec98c1d8c9d3db3ddb7e7c479a5Brian Paul    Passes.add(createDeadCodeEliminationPass());
90afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  if (Simplification && !DisableSimplifyCFG)
91afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    Passes.add(createCFGSimplificationPass());      // Delete dead control flow
92afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
93afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  Passes.add(createVerifierPass());
94973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul  Passes.run(*Result);
95afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  return Result;
96973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul}
97973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paul
98973da83f6237b5af4a9ee77f32fdfa5c04ecabc8Brian Paulstatic const PassInfo *getPI(Pass *P) {
99afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  const PassInfo *PI = P->getPassInfo();
100afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  delete P;
101afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  return PI;
102afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
103afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
104afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/// performFinalCleanups - This method clones the current Program and performs
105887e2cf01a99f7fe1b7c94320b7bdbbf0d6ad2f8Brian Paul/// a series of cleanups intended to get rid of extra cruft on the module
106afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg/// before handing it to the user...
107afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg///
108afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtgModule *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
109afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  // Make all functions external, so GlobalDCE doesn't delete them...
110afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
111afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    I->setLinkage(GlobalValue::ExternalLinkage);
112afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
113afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  std::vector<const PassInfo*> CleanupPasses;
1146dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell  CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
1151749a25ca889d514889b34cf6311c8014d97bf66Brian Paul  CleanupPasses.push_back(getPI(createGlobalDCEPass()));
1161749a25ca889d514889b34cf6311c8014d97bf66Brian Paul  CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
1176dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell  CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
1186dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell
1191749a25ca889d514889b34cf6311c8014d97bf66Brian Paul  std::swap(Program, M);
1206dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell  std::string Filename;
1216dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell  bool Failed = runPasses(CleanupPasses, Filename);
1226dc85575000127630489b407c50a4b3ea87c9acbKeith Whitwell  std::swap(Program, M);
12389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
12489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul  if (Failed) {
12589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul    std::cerr << "Final cleanups failed.  Sorry.  :(\n";
12673b150c816c46a88e3e5d97f9b73ab0095f2bc60Brian Paul  } else {
12773b150c816c46a88e3e5d97f9b73ab0095f2bc60Brian Paul    delete M;
128afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    M = ParseInputFile(Filename);
129b132e8da5e5f2b7da1f2141e0322e66bb0608e02Brian Paul    if (M == 0) {
130f9995b30756140724f41daf963fa06167912be7fKristian Høgsberg      std::cerr << getToolName() << ": Error reading bytecode file '"
131afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg                << Filename << "'!\n";
1321749a25ca889d514889b34cf6311c8014d97bf66Brian Paul      exit(1);
133afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg    }
134afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  }
135afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg  return M;
136afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg}
137afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg
138afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg} // End llvm namespace
139afb833d4e89c312460a4ab9ed6a7a8ca4ebbfe1cjtg