ExtractFunction.cpp revision 5cbf985dcbc89fba3208e7baf8b6f488b06d3ec9
1afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===- ExtractFunction.cpp - Extract a function from Program --------------===//
23da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//                     The LLVM Compiler Infrastructure
47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
57c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// This file was developed by the LLVM research group and is distributed under
67c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details.
73da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
87c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//===----------------------------------------------------------------------===//
9afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
10efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner// This file implements several methods that are used to extract functions,
11efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner// loops, or portions of a module from the rest of the module.
12afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
13afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===----------------------------------------------------------------------===//
14afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
15afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "BugDriver.h"
165a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner#include "llvm/Constants.h"
175a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner#include "llvm/DerivedTypes.h"
18afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Module.h"
19afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/PassManager.h"
20d1a85a744cc1001c2b7fc37cf37aca266964f519Brian Gaeke#include "llvm/Pass.h"
21e49603d79d220a795bd50684c8b1f503ee40f97fMisha Brukman#include "llvm/Analysis/Verifier.h"
22afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/IPO.h"
236520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Transforms/Scalar.h"
24afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/Utils/Cloning.h"
255e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner#include "llvm/Transforms/Utils/FunctionUtils.h"
265da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner#include "llvm/Target/TargetData.h"
27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
28551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
29551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/FileUtilities.h"
30fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner#include <set>
31e31a9ccb9c7568e6f185f667b53c274c0be9e603Chris Lattner#include <iostream>
32c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattnerusing namespace llvm;
33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
34d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
35c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  bool DisableSimplifyCFG = false;
36d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
37d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
386db70ef879916e6115ac97eb76e4fea973652e2cChris Lattnernamespace {
396db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  cl::opt<bool>
406db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  NoDCE ("disable-dce",
416db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -dce pass to reduce testcases"));
4247ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  cl::opt<bool, true>
4347ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
446db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
456db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner}
46afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
476520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deleteInstructionFromProgram - This method clones the current Program and
486520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deletes the specified instruction from the cloned module.  It then runs a
496520785dcd22012535934098942d57c07c7631c2Chris Lattner/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
506520785dcd22012535934098942d57c07c7631c2Chris Lattner/// depends on the value.  The modified module is then returned.
516520785dcd22012535934098942d57c07c7631c2Chris Lattner///
520cc8807029f577996a442b96d24c3346ed6de091Chris LattnerModule *BugDriver::deleteInstructionFromProgram(const Instruction *I,
536520785dcd22012535934098942d57c07c7631c2Chris Lattner                                                unsigned Simplification) const {
546520785dcd22012535934098942d57c07c7631c2Chris Lattner  Module *Result = CloneModule(Program);
556520785dcd22012535934098942d57c07c7631c2Chris Lattner
560cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  const BasicBlock *PBB = I->getParent();
570cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  const Function *PF = PBB->getParent();
586520785dcd22012535934098942d57c07c7631c2Chris Lattner
596520785dcd22012535934098942d57c07c7631c2Chris Lattner  Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
600cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RFI, std::distance(PF->getParent()->begin(),
610cc8807029f577996a442b96d24c3346ed6de091Chris Lattner                                  Module::const_iterator(PF)));
626520785dcd22012535934098942d57c07c7631c2Chris Lattner
636520785dcd22012535934098942d57c07c7631c2Chris Lattner  Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
640cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
656520785dcd22012535934098942d57c07c7631c2Chris Lattner
666520785dcd22012535934098942d57c07c7631c2Chris Lattner  BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
670cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
680cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  Instruction *TheInst = RI;              // Got the corresponding instruction!
696520785dcd22012535934098942d57c07c7631c2Chris Lattner
706520785dcd22012535934098942d57c07c7631c2Chris Lattner  // If this instruction produces a value, replace any users with null values
710cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  if (TheInst->getType() != Type::VoidTy)
720cc8807029f577996a442b96d24c3346ed6de091Chris Lattner    TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
736520785dcd22012535934098942d57c07c7631c2Chris Lattner
746520785dcd22012535934098942d57c07c7631c2Chris Lattner  // Remove the instruction from the program.
750cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  TheInst->getParent()->getInstList().erase(TheInst);
766520785dcd22012535934098942d57c07c7631c2Chris Lattner
775a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
785a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  //writeProgramToFile("current.bc", Result);
795a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
8044be25716628941b4cccccf56a28ee0ba2606850Chris Lattner  // Spiff up the output a little bit.
816520785dcd22012535934098942d57c07c7631c2Chris Lattner  PassManager Passes;
825da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner  // Make sure that the appropriate target data is always used...
83831b1210390b3a00f68de9a79be0f4e13d6287b0Chris Lattner  Passes.add(new TargetData(Result));
845da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner
85efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// FIXME: If this used runPasses() like the methods below, we could get rid
86efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// of the -disable-* options!
876db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  if (Simplification > 1 && !NoDCE)
886520785dcd22012535934098942d57c07c7631c2Chris Lattner    Passes.add(createDeadCodeEliminationPass());
8947ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  if (Simplification && !DisableSimplifyCFG)
906520785dcd22012535934098942d57c07c7631c2Chris Lattner    Passes.add(createCFGSimplificationPass());      // Delete dead control flow
9110f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner
9210f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner  Passes.add(createVerifierPass());
936520785dcd22012535934098942d57c07c7631c2Chris Lattner  Passes.run(*Result);
946520785dcd22012535934098942d57c07c7631c2Chris Lattner  return Result;
956520785dcd22012535934098942d57c07c7631c2Chris Lattner}
96ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner
97fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattnerstatic const PassInfo *getPI(Pass *P) {
98fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  const PassInfo *PI = P->getPassInfo();
99fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  delete P;
100fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  return PI;
101fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner}
102fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner
103ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// performFinalCleanups - This method clones the current Program and performs
104ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// a series of cleanups intended to get rid of extra cruft on the module
1059b5b1905dbad19de374e7617f615ba522ffe0468Chris Lattner/// before handing it to the user.
106ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner///
107fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris LattnerModule *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
10828b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner  // Make all functions external, so GlobalDCE doesn't delete them...
10928b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
11028b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
1113da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
112fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  std::vector<const PassInfo*> CleanupPasses;
113fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
114fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  CleanupPasses.push_back(getPI(createGlobalDCEPass()));
115fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
116fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner
117c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  if (MayModifySemantics)
118c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner    CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
119c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  else
120c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner    CleanupPasses.push_back(getPI(createDeadArgEliminationPass()));
121fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner
122a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  Module *New = runPassesOn(M, CleanupPasses);
123a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  if (New == 0) {
1247546c3884a400b72d10fc19f120c6798b294a39dChris Lattner    std::cerr << "Final cleanups failed.  Sorry. :(  Please report a bug!\n";
1259b5b1905dbad19de374e7617f615ba522ffe0468Chris Lattner    return M;
126fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  }
127a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  delete M;
128a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  return New;
129ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner}
130be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
131be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
1327546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// ExtractLoop - Given a module, extract up to one loop from it into a new
1337546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// function.  This returns null if there are no extractable loops in the
1347546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// program or if the loop extractor crashes.
1357546c3884a400b72d10fc19f120c6798b294a39dChris LattnerModule *BugDriver::ExtractLoop(Module *M) {
1367546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  std::vector<const PassInfo*> LoopExtractPasses;
1377546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass()));
1387546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
139a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  Module *NewM = runPassesOn(M, LoopExtractPasses);
140a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  if (NewM == 0) {
141a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *Old = swapProgramIn(M);
142a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "*** Loop extraction failed: ";
143a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    EmitProgressBytecode("loopextraction", true);
144a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "*** Sorry. :(  Please report a bug!\n";
145a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    swapProgramIn(Old);
1467546c3884a400b72d10fc19f120c6798b294a39dChris Lattner    return 0;
1477546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  }
148a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner
149a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  // Check to see if we created any new functions.  If not, no loops were
150a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  // extracted and we should return null.  Limit the number of loops we extract
151a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  // to avoid taking forever.
152a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  static unsigned NumExtracted = 32;
15390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  if (M->size() == NewM->size() || --NumExtracted == 0) {
154a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner    delete NewM;
155a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner    return 0;
15690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  } else {
15790c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    assert(M->size() < NewM->size() && "Loop extract removed functions?");
15890c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    Module::iterator MI = NewM->begin();
15990c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    for (unsigned i = 0, e = M->size(); i != e; ++i)
16090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner      ++MI;
161a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  }
1623da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
163a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  return NewM;
1647546c3884a400b72d10fc19f120c6798b294a39dChris Lattner}
1657546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
1667546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
167be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of its basic
168be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// blocks, making it external.
169be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner//
170be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnervoid llvm::DeleteFunctionBody(Function *F) {
171be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // delete the body of the function...
172be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  F->deleteBody();
1735cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer  assert(F->isDeclaration() && "This didn't make the function external!");
174be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner}
175be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
1765a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// GetTorInit - Given a list of entries for static ctors/dtors, return them
1775a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// as a constant array.
1785a7a9e53861188ad84cb0e08e34562da744a907aChris Lattnerstatic Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) {
1795a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  assert(!TorList.empty() && "Don't create empty tor list!");
1805a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  std::vector<Constant*> ArrayElts;
1815a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  for (unsigned i = 0, e = TorList.size(); i != e; ++i) {
1825a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    std::vector<Constant*> Elts;
18371d2ec9e6d20bd8701471bc6e6db620838ce3539Reid Spencer    Elts.push_back(ConstantInt::get(Type::Int32Ty, TorList[i].second));
1845a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    Elts.push_back(TorList[i].first);
1855a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    ArrayElts.push_back(ConstantStruct::get(Elts));
1865a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  }
1875a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(),
1885a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner                                           ArrayElts.size()),
1895a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner                            ArrayElts);
1905a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner}
1915a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
1925a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and
1935a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// M1 has all of the global variables.  If M2 contains any functions that are
1945a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and
1955a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// prune appropriate entries out of M1s list.
1965a7a9e53861188ad84cb0e08e34562da744a907aChris Lattnerstatic void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2){
1975a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  GlobalVariable *GV = M1->getNamedGlobal(GlobalName);
1985cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer  if (!GV || GV->isDeclaration() || GV->hasInternalLinkage() ||
1995a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      !GV->use_empty()) return;
2005a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2015a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  std::vector<std::pair<Function*, int> > M1Tors, M2Tors;
2025a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
2035a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  if (!InitList) return;
2045a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2055a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
2065a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
2075a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      if (CS->getNumOperands() != 2) return;  // Not array of 2-element structs.
2085a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2095a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      if (CS->getOperand(1)->isNullValue())
2105a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner        break;  // Found a null terminator, stop here.
2115a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
212b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer      ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0));
213b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer      int Priority = CI ? CI->getSExtValue() : 0;
2145a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2155a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      Constant *FP = CS->getOperand(1);
2165a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
2173da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer        if (CE->isCast())
2185a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          FP = CE->getOperand(0);
2195a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      if (Function *F = dyn_cast<Function>(FP)) {
2205cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer        if (!F->isDeclaration())
2215a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          M1Tors.push_back(std::make_pair(F, Priority));
2225a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner        else {
2235a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          // Map to M2's version of the function.
2245a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          F = M2->getFunction(F->getName(), F->getFunctionType());
2255a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          M2Tors.push_back(std::make_pair(F, Priority));
2265a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner        }
2275a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      }
2285a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    }
2295a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  }
2305a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2315a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  GV->eraseFromParent();
2325a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  if (!M1Tors.empty()) {
2335a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    Constant *M1Init = GetTorInit(M1Tors);
2345a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    new GlobalVariable(M1Init->getType(), false, GlobalValue::AppendingLinkage,
2355a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner                       M1Init, GlobalName, M1);
2365a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  }
2375a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2385a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  GV = M2->getNamedGlobal(GlobalName);
2395a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  assert(GV && "Not a clone of M1?");
2405a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  assert(GV->use_empty() && "llvm.ctors shouldn't have uses!");
2415a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2425a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  GV->eraseFromParent();
2435a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  if (!M2Tors.empty()) {
2445a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    Constant *M2Init = GetTorInit(M2Tors);
2455a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    new GlobalVariable(M2Init->getType(), false, GlobalValue::AppendingLinkage,
2465a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner                       M2Init, GlobalName, M2);
2475a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  }
2485a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner}
2495a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
250e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins
251be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
252be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// module, split the functions OUT of the specified module, and place them in
253be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// the new module.
254be21ca54e08339ede5dd4bbb882182d22e274988Chris LattnerModule *llvm::SplitFunctionsOutOfModule(Module *M,
255be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner                                        const std::vector<Function*> &F) {
256be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Make sure functions & globals are all external so that linkage
257be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // between the two modules will work.
258be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
259be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
2605a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  for (Module::global_iterator I = M->global_begin(), E = M->global_end();
2615a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner       I != E; ++I)
262be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
263be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
264fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner  Module *New = CloneModule(M);
265be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
266fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner  // Make sure global initializers exist only in the safe module (CBE->.so)
267fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner  for (Module::global_iterator I = New->global_begin(), E = New->global_end();
268fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner       I != E; ++I)
269fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner    I->setInitializer(0);  // Delete the initializer to make it external
270be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
271fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner  // Remove the Test functions from the Safe module
272fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner  std::set<std::pair<std::string, const PointerType*> > TestFunctions;
273be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  for (unsigned i = 0, e = F.size(); i != e; ++i) {
274e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins    TestFunctions.insert(std::make_pair(F[i]->getName(), F[i]->getType()));
275fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner    Function *TNOF = M->getFunction(F[i]->getName(), F[i]->getFunctionType());
276fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner    DEBUG(std::cerr << "Removing function " << F[i]->getName() << "\n");
277fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner    assert(TNOF && "Function doesn't exist in module!");
278fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner    DeleteFunctionBody(TNOF);       // Function is now external in this module!
279e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins  }
280e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins
2815a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
282fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner  // Remove the Safe functions from the Test module
283fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner  for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I)
284fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner    if (!TestFunctions.count(std::make_pair(I->getName(), I->getType())))
285fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner      DeleteFunctionBody(I);
286fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner
287e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins
2885a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  // Make sure that there is a global ctor/dtor array in both halves of the
2895a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  // module if they both have static ctor/dtor functions.
2905a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  SplitStaticCtorDtor("llvm.global_ctors", M, New);
2915a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  SplitStaticCtorDtor("llvm.global_dtors", M, New);
2925a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
293be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  return New;
294be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner}
2955e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2965e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner//===----------------------------------------------------------------------===//
2975e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner// Basic Block Extraction Code
2985e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner//===----------------------------------------------------------------------===//
2995e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3005e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnernamespace {
3015e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::vector<BasicBlock*> BlocksToNotExtract;
3025e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3035e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
3045e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// from the module into their own functions except for those specified by the
3055e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// BlocksToNotExtract list.
306b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner  class BlockExtractorPass : public ModulePass {
307b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner    bool runOnModule(Module &M);
3085e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  };
3097f8897f22e88271cfa114998a4d6088e7c8e8e11Chris Lattner  RegisterPass<BlockExtractorPass>
3105e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  XX("extract-bbs", "Extract Basic Blocks From Module (for bugpoint use)");
3115e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
3125e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
313b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattnerbool BlockExtractorPass::runOnModule(Module &M) {
3145e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::set<BasicBlock*> TranslatedBlocksToNotExtract;
3155e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
3165e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    BasicBlock *BB = BlocksToNotExtract[i];
3175e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    Function *F = BB->getParent();
3185e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3195e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Map the corresponding function in this module.
3205e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    Function *MF = M.getFunction(F->getName(), F->getFunctionType());
3215e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3225e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Figure out which index the basic block is in its function.
3235e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    Function::iterator BBI = MF->begin();
3245e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
3255e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    TranslatedBlocksToNotExtract.insert(BBI);
3265e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  }
3275e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3285e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Now that we know which blocks to not extract, figure out which ones we WANT
3295e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // to extract.
3305e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::vector<BasicBlock*> BlocksToExtract;
3315e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
3325e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
3335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      if (!TranslatedBlocksToNotExtract.count(BB))
3345e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner        BlocksToExtract.push_back(BB);
3355e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3365e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
3375e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    ExtractBasicBlock(BlocksToExtract[i]);
3383da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
3395e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  return !BlocksToExtract.empty();
3405e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
3415e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3425e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
3435e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// into their own functions.  The only detail is that M is actually a module
3445e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// cloned from the one the BBs are in, so some mapping needs to be performed.
3455e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// If this operation fails for some reason (ie the implementation is buggy),
3465e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// this function should return null, otherwise it returns a new Module.
3475e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris LattnerModule *BugDriver::ExtractMappedBlocksFromModule(const
3485e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                                 std::vector<BasicBlock*> &BBs,
3495e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                                 Module *M) {
3505e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Set the global list so that pass will be able to access it.
3515e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  BlocksToNotExtract = BBs;
3525e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3535e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::vector<const PassInfo*> PI;
3545e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  PI.push_back(getPI(new BlockExtractorPass()));
3555e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *Ret = runPassesOn(M, PI);
3565e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  BlocksToNotExtract.clear();
357891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner  if (Ret == 0) {
3585e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    std::cout << "*** Basic Block extraction failed, please report a bug!\n";
359891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner    M = swapProgramIn(M);
360891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner    EmitProgressBytecode("basicblockextractfail", true);
361b923b2e04bdc8597de24a756d7e073bd5bfe1e44Chris Lattner    swapProgramIn(M);
362891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner  }
3635e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  return Ret;
3645e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
365