ExtractFunction.cpp revision 65f57c233cd4499e2e8b52a503201e64edfd6a9e
1afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===- ExtractFunction.cpp - Extract a function from Program --------------===// 23da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman// 37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// The LLVM Compiler Infrastructure 47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// 521c62da287237d39d0d95004881ea4baae3be6daChris Lattner// This file is distributed under the University of Illinois Open Source 621c62da287237d39d0d95004881ea4baae3be6daChris Lattner// 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" 18db1cd5eba551bdd3d7767207891b578f61f8b39dOwen Anderson#include "llvm/LLVMContext.h" 19afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Module.h" 20afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/PassManager.h" 21d1a85a744cc1001c2b7fc37cf37aca266964f519Brian Gaeke#include "llvm/Pass.h" 22e49603d79d220a795bd50684c8b1f503ee40f97fMisha Brukman#include "llvm/Analysis/Verifier.h" 23e860dcb6e4eeb1ffae2299e5157a36c025260912Dan Gohman#include "llvm/Assembly/Writer.h" 24afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/IPO.h" 256520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Transforms/Scalar.h" 26afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/Utils/Cloning.h" 275e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner#include "llvm/Transforms/Utils/FunctionUtils.h" 285da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner#include "llvm/Target/TargetData.h" 29551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h" 30551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h" 31551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/FileUtilities.h" 326fa98b13206583e6eb90b8304758b35548914944Nick Lewycky#include "llvm/System/Path.h" 336fa98b13206583e6eb90b8304758b35548914944Nick Lewycky#include "llvm/System/Signals.h" 34fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner#include <set> 356fa98b13206583e6eb90b8304758b35548914944Nick Lewycky#include <fstream> 36e31a9ccb9c7568e6f185f667b53c274c0be9e603Chris Lattner#include <iostream> 37c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattnerusing namespace llvm; 38d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 39d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 40c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner bool DisableSimplifyCFG = false; 41d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 42d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 436db70ef879916e6115ac97eb76e4fea973652e2cChris Lattnernamespace { 446db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::opt<bool> 456db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner NoDCE ("disable-dce", 466db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::desc("Do not use the -dce pass to reduce testcases")); 4747ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner cl::opt<bool, true> 4847ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), 496db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::desc("Do not use the -simplifycfg pass to reduce testcases")); 506db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner} 51afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner 526520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deleteInstructionFromProgram - This method clones the current Program and 536520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deletes the specified instruction from the cloned module. It then runs a 546520785dcd22012535934098942d57c07c7631c2Chris Lattner/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which 556520785dcd22012535934098942d57c07c7631c2Chris Lattner/// depends on the value. The modified module is then returned. 566520785dcd22012535934098942d57c07c7631c2Chris Lattner/// 570cc8807029f577996a442b96d24c3346ed6de091Chris LattnerModule *BugDriver::deleteInstructionFromProgram(const Instruction *I, 586520785dcd22012535934098942d57c07c7631c2Chris Lattner unsigned Simplification) const { 596520785dcd22012535934098942d57c07c7631c2Chris Lattner Module *Result = CloneModule(Program); 606520785dcd22012535934098942d57c07c7631c2Chris Lattner 610cc8807029f577996a442b96d24c3346ed6de091Chris Lattner const BasicBlock *PBB = I->getParent(); 620cc8807029f577996a442b96d24c3346ed6de091Chris Lattner const Function *PF = PBB->getParent(); 636520785dcd22012535934098942d57c07c7631c2Chris Lattner 646520785dcd22012535934098942d57c07c7631c2Chris Lattner Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn 650cc8807029f577996a442b96d24c3346ed6de091Chris Lattner std::advance(RFI, std::distance(PF->getParent()->begin(), 660cc8807029f577996a442b96d24c3346ed6de091Chris Lattner Module::const_iterator(PF))); 676520785dcd22012535934098942d57c07c7631c2Chris Lattner 686520785dcd22012535934098942d57c07c7631c2Chris Lattner Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB 690cc8807029f577996a442b96d24c3346ed6de091Chris Lattner std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); 706520785dcd22012535934098942d57c07c7631c2Chris Lattner 716520785dcd22012535934098942d57c07c7631c2Chris Lattner BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst 720cc8807029f577996a442b96d24c3346ed6de091Chris Lattner std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); 730cc8807029f577996a442b96d24c3346ed6de091Chris Lattner Instruction *TheInst = RI; // Got the corresponding instruction! 746520785dcd22012535934098942d57c07c7631c2Chris Lattner 756520785dcd22012535934098942d57c07c7631c2Chris Lattner // If this instruction produces a value, replace any users with null values 76e78109eb3a3d177d03cea40b46d9dcfc9bf32210Chris Lattner if (isa<StructType>(TheInst->getType())) 779adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson TheInst->replaceAllUsesWith(Context.getUndef(TheInst->getType())); 78e78109eb3a3d177d03cea40b46d9dcfc9bf32210Chris Lattner else if (TheInst->getType() != Type::VoidTy) 790a5372ed3e8cda10d724feda3c1a1c998db05ca0Owen Anderson TheInst->replaceAllUsesWith(Context.getNullValue(TheInst->getType())); 806520785dcd22012535934098942d57c07c7631c2Chris Lattner 816520785dcd22012535934098942d57c07c7631c2Chris Lattner // Remove the instruction from the program. 820cc8807029f577996a442b96d24c3346ed6de091Chris Lattner TheInst->getParent()->getInstList().erase(TheInst); 836520785dcd22012535934098942d57c07c7631c2Chris Lattner 845a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 855a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner //writeProgramToFile("current.bc", Result); 865a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 8744be25716628941b4cccccf56a28ee0ba2606850Chris Lattner // Spiff up the output a little bit. 886520785dcd22012535934098942d57c07c7631c2Chris Lattner PassManager Passes; 895da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner // Make sure that the appropriate target data is always used... 90831b1210390b3a00f68de9a79be0f4e13d6287b0Chris Lattner Passes.add(new TargetData(Result)); 915da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner 92efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner /// FIXME: If this used runPasses() like the methods below, we could get rid 93efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner /// of the -disable-* options! 946db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner if (Simplification > 1 && !NoDCE) 956520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.add(createDeadCodeEliminationPass()); 9647ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner if (Simplification && !DisableSimplifyCFG) 976520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.add(createCFGSimplificationPass()); // Delete dead control flow 9810f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner 9910f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner Passes.add(createVerifierPass()); 1006520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.run(*Result); 1016520785dcd22012535934098942d57c07c7631c2Chris Lattner return Result; 1026520785dcd22012535934098942d57c07c7631c2Chris Lattner} 103ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner 104fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattnerstatic const PassInfo *getPI(Pass *P) { 105fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner const PassInfo *PI = P->getPassInfo(); 106fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner delete P; 107fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner return PI; 108fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner} 109fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner 110ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// performFinalCleanups - This method clones the current Program and performs 111ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// a series of cleanups intended to get rid of extra cruft on the module 1129b5b1905dbad19de374e7617f615ba522ffe0468Chris Lattner/// before handing it to the user. 113ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// 114fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris LattnerModule *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { 11528b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner // Make all functions external, so GlobalDCE doesn't delete them... 11628b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 11728b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner I->setLinkage(GlobalValue::ExternalLinkage); 1183da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 119fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner std::vector<const PassInfo*> CleanupPasses; 120fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner CleanupPasses.push_back(getPI(createGlobalDCEPass())); 121fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner CleanupPasses.push_back(getPI(createDeadTypeEliminationPass())); 122fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner 123c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner if (MayModifySemantics) 124c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner CleanupPasses.push_back(getPI(createDeadArgHackingPass())); 125c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner else 126c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner CleanupPasses.push_back(getPI(createDeadArgEliminationPass())); 127fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner 128a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner Module *New = runPassesOn(M, CleanupPasses); 129a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner if (New == 0) { 13065f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n"; 1319b5b1905dbad19de374e7617f615ba522ffe0468Chris Lattner return M; 132fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner } 133a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner delete M; 134a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner return New; 135ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner} 136be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 137be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 1387546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// ExtractLoop - Given a module, extract up to one loop from it into a new 1397546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// function. This returns null if there are no extractable loops in the 1407546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// program or if the loop extractor crashes. 1417546c3884a400b72d10fc19f120c6798b294a39dChris LattnerModule *BugDriver::ExtractLoop(Module *M) { 1427546c3884a400b72d10fc19f120c6798b294a39dChris Lattner std::vector<const PassInfo*> LoopExtractPasses; 1437546c3884a400b72d10fc19f120c6798b294a39dChris Lattner LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass())); 1447546c3884a400b72d10fc19f120c6798b294a39dChris Lattner 145a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner Module *NewM = runPassesOn(M, LoopExtractPasses); 146a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner if (NewM == 0) { 147a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner Module *Old = swapProgramIn(M); 148a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner std::cout << "*** Loop extraction failed: "; 1498ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif EmitProgressBitcode("loopextraction", true); 150a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner std::cout << "*** Sorry. :( Please report a bug!\n"; 151a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner swapProgramIn(Old); 1527546c3884a400b72d10fc19f120c6798b294a39dChris Lattner return 0; 1537546c3884a400b72d10fc19f120c6798b294a39dChris Lattner } 154a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner 155a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner // Check to see if we created any new functions. If not, no loops were 156a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner // extracted and we should return null. Limit the number of loops we extract 157a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner // to avoid taking forever. 158a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner static unsigned NumExtracted = 32; 15990c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner if (M->size() == NewM->size() || --NumExtracted == 0) { 160a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner delete NewM; 161a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner return 0; 16290c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner } else { 16390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner assert(M->size() < NewM->size() && "Loop extract removed functions?"); 16490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner Module::iterator MI = NewM->begin(); 16590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner for (unsigned i = 0, e = M->size(); i != e; ++i) 16690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner ++MI; 167a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner } 1683da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 169a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner return NewM; 1707546c3884a400b72d10fc19f120c6798b294a39dChris Lattner} 1717546c3884a400b72d10fc19f120c6798b294a39dChris Lattner 1727546c3884a400b72d10fc19f120c6798b294a39dChris Lattner 173be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of its basic 174be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// blocks, making it external. 175be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// 176be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnervoid llvm::DeleteFunctionBody(Function *F) { 177be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner // delete the body of the function... 178be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner F->deleteBody(); 1795cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer assert(F->isDeclaration() && "This didn't make the function external!"); 180be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner} 181be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 1825a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// GetTorInit - Given a list of entries for static ctors/dtors, return them 1835a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// as a constant array. 1845a7a9e53861188ad84cb0e08e34562da744a907aChris Lattnerstatic Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) { 1855a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner assert(!TorList.empty() && "Don't create empty tor list!"); 1869adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson LLVMContext &Context = *TorList[0].first->getContext(); 1875a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner std::vector<Constant*> ArrayElts; 1885a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner for (unsigned i = 0, e = TorList.size(); i != e; ++i) { 1895a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner std::vector<Constant*> Elts; 1909adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson Elts.push_back(Context.getConstantInt(Type::Int32Ty, TorList[i].second)); 1915a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner Elts.push_back(TorList[i].first); 1929adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson ArrayElts.push_back(Context.getConstantStruct(Elts)); 1935a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner } 1949adc0abad3c3ed40a268ccbcee0c74cb9e1359feOwen Anderson return Context.getConstantArray(Context.getArrayType(ArrayElts[0]->getType(), 1955a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner ArrayElts.size()), 1965a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner ArrayElts); 1975a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner} 1985a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 1995a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and 2005a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// M1 has all of the global variables. If M2 contains any functions that are 2015a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and 2025a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// prune appropriate entries out of M1s list. 203d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohmanstatic void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2, 204d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman DenseMap<const Value*, Value*> ValueMap) { 2055a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner GlobalVariable *GV = M1->getNamedGlobal(GlobalName); 206bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() || 2075a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner !GV->use_empty()) return; 2085a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 2095a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner std::vector<std::pair<Function*, int> > M1Tors, M2Tors; 2105a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer()); 2115a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner if (!InitList) return; 2125a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 2135a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) { 2145a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){ 2155a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner if (CS->getNumOperands() != 2) return; // Not array of 2-element structs. 2165a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 2175a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner if (CS->getOperand(1)->isNullValue()) 2185a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner break; // Found a null terminator, stop here. 2195a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 220b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0)); 221b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer int Priority = CI ? CI->getSExtValue() : 0; 2225a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 2235a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner Constant *FP = CS->getOperand(1); 2245a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP)) 2253da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer if (CE->isCast()) 2265a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner FP = CE->getOperand(0); 2275a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner if (Function *F = dyn_cast<Function>(FP)) { 2285cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer if (!F->isDeclaration()) 2295a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner M1Tors.push_back(std::make_pair(F, Priority)); 2305a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner else { 2315a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner // Map to M2's version of the function. 232d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman F = cast<Function>(ValueMap[F]); 2335a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner M2Tors.push_back(std::make_pair(F, Priority)); 2345a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner } 2355a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner } 2365a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner } 2375a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner } 2385a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 2395a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner GV->eraseFromParent(); 2405a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner if (!M1Tors.empty()) { 2415a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner Constant *M1Init = GetTorInit(M1Tors); 242e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson new GlobalVariable(*M1, M1Init->getType(), false, 2433d29df3e8a203b167d8071ea6f805b21db18a5afOwen Anderson GlobalValue::AppendingLinkage, 244e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson M1Init, GlobalName); 2455a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner } 2465a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 2475a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner GV = M2->getNamedGlobal(GlobalName); 2485a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner assert(GV && "Not a clone of M1?"); 2495a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner assert(GV->use_empty() && "llvm.ctors shouldn't have uses!"); 2505a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 2515a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner GV->eraseFromParent(); 2525a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner if (!M2Tors.empty()) { 2535a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner Constant *M2Init = GetTorInit(M2Tors); 254e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson new GlobalVariable(*M2, M2Init->getType(), false, 2553d29df3e8a203b167d8071ea6f805b21db18a5afOwen Anderson GlobalValue::AppendingLinkage, 256e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson M2Init, GlobalName); 2575a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner } 2585a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner} 2595a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 260e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins 261be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// SplitFunctionsOutOfModule - Given a module and a list of functions in the 262be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// module, split the functions OUT of the specified module, and place them in 263be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// the new module. 264d50330cd02b00c8e3de40e8544c45701b9891d87Dan GohmanModule * 265d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohmanllvm::SplitFunctionsOutOfModule(Module *M, 266d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman const std::vector<Function*> &F, 267d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman DenseMap<const Value*, Value*> &ValueMap) { 268be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner // Make sure functions & globals are all external so that linkage 269be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner // between the two modules will work. 270be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 271be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner I->setLinkage(GlobalValue::ExternalLinkage); 2725a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner for (Module::global_iterator I = M->global_begin(), E = M->global_end(); 2737220b814ed6f432d1d31d444b65e0c3b120942dfOwen Anderson I != E; ++I) { 2747220b814ed6f432d1d31d444b65e0c3b120942dfOwen Anderson if (I->hasName() && *I->getNameStart() == '\01') 2757220b814ed6f432d1d31d444b65e0c3b120942dfOwen Anderson I->setName(I->getNameStart()+1, I->getNameLen()-1); 276be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner I->setLinkage(GlobalValue::ExternalLinkage); 2777220b814ed6f432d1d31d444b65e0c3b120942dfOwen Anderson } 278be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 279d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman DenseMap<const Value*, Value*> NewValueMap; 280d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman Module *New = CloneModule(M, NewValueMap); 281be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 282fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner // Make sure global initializers exist only in the safe module (CBE->.so) 283fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner for (Module::global_iterator I = New->global_begin(), E = New->global_end(); 284fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner I != E; ++I) 285fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner I->setInitializer(0); // Delete the initializer to make it external 286be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 287fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner // Remove the Test functions from the Safe module 288d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman std::set<Function *> TestFunctions; 289be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner for (unsigned i = 0, e = F.size(); i != e; ++i) { 290d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman Function *TNOF = cast<Function>(ValueMap[F[i]]); 29165f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman DEBUG(errs() << "Removing function "); 29265f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman DEBUG(WriteAsOperand(errs(), TNOF, false)); 29365f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman DEBUG(errs() << "\n"); 294d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman TestFunctions.insert(cast<Function>(NewValueMap[TNOF])); 295fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner DeleteFunctionBody(TNOF); // Function is now external in this module! 296e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins } 297e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins 2985a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 299fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner // Remove the Safe functions from the Test module 300fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) 301d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman if (!TestFunctions.count(I)) 302fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner DeleteFunctionBody(I); 303fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner 304e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins 3055a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner // Make sure that there is a global ctor/dtor array in both halves of the 3065a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner // module if they both have static ctor/dtor functions. 307d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman SplitStaticCtorDtor("llvm.global_ctors", M, New, NewValueMap); 308d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman SplitStaticCtorDtor("llvm.global_dtors", M, New, NewValueMap); 3095a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 310be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner return New; 311be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner} 3125e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 3135e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner//===----------------------------------------------------------------------===// 3145e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner// Basic Block Extraction Code 3155e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner//===----------------------------------------------------------------------===// 3165e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner 3175e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks 3185e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// into their own functions. The only detail is that M is actually a module 3195e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// cloned from the one the BBs are in, so some mapping needs to be performed. 3205e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// If this operation fails for some reason (ie the implementation is buggy), 3215e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// this function should return null, otherwise it returns a new Module. 3225e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris LattnerModule *BugDriver::ExtractMappedBlocksFromModule(const 3235e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner std::vector<BasicBlock*> &BBs, 3245e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner Module *M) { 3256fa98b13206583e6eb90b8304758b35548914944Nick Lewycky char *ExtraArg = NULL; 3266fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 3276fa98b13206583e6eb90b8304758b35548914944Nick Lewycky sys::Path uniqueFilename("bugpoint-extractblocks"); 3286fa98b13206583e6eb90b8304758b35548914944Nick Lewycky std::string ErrMsg; 3296fa98b13206583e6eb90b8304758b35548914944Nick Lewycky if (uniqueFilename.createTemporaryFileOnDisk(true, &ErrMsg)) { 3306fa98b13206583e6eb90b8304758b35548914944Nick Lewycky std::cout << "*** Basic Block extraction failed!\n"; 33165f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << "Error creating temporary file: " << ErrMsg << "\n"; 3326fa98b13206583e6eb90b8304758b35548914944Nick Lewycky M = swapProgramIn(M); 3336fa98b13206583e6eb90b8304758b35548914944Nick Lewycky EmitProgressBitcode("basicblockextractfail", true); 3346fa98b13206583e6eb90b8304758b35548914944Nick Lewycky swapProgramIn(M); 3356fa98b13206583e6eb90b8304758b35548914944Nick Lewycky return 0; 3366fa98b13206583e6eb90b8304758b35548914944Nick Lewycky } 3376fa98b13206583e6eb90b8304758b35548914944Nick Lewycky sys::RemoveFileOnSignal(uniqueFilename); 3386fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 3396fa98b13206583e6eb90b8304758b35548914944Nick Lewycky std::ofstream BlocksToNotExtractFile(uniqueFilename.c_str()); 3406fa98b13206583e6eb90b8304758b35548914944Nick Lewycky if (!BlocksToNotExtractFile) { 3416fa98b13206583e6eb90b8304758b35548914944Nick Lewycky std::cout << "*** Basic Block extraction failed!\n"; 34265f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << "Error writing list of blocks to not extract: " << ErrMsg 34365f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << "\n"; 3446fa98b13206583e6eb90b8304758b35548914944Nick Lewycky M = swapProgramIn(M); 3456fa98b13206583e6eb90b8304758b35548914944Nick Lewycky EmitProgressBitcode("basicblockextractfail", true); 3466fa98b13206583e6eb90b8304758b35548914944Nick Lewycky swapProgramIn(M); 3476fa98b13206583e6eb90b8304758b35548914944Nick Lewycky return 0; 3486fa98b13206583e6eb90b8304758b35548914944Nick Lewycky } 3496fa98b13206583e6eb90b8304758b35548914944Nick Lewycky for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end(); 3506fa98b13206583e6eb90b8304758b35548914944Nick Lewycky I != E; ++I) { 3516fa98b13206583e6eb90b8304758b35548914944Nick Lewycky BasicBlock *BB = *I; 3524a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner // If the BB doesn't have a name, give it one so we have something to key 3534a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner // off of. 3544a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner if (!BB->hasName()) BB->setName("tmpbb"); 3556fa98b13206583e6eb90b8304758b35548914944Nick Lewycky BlocksToNotExtractFile << BB->getParent()->getName() << " " 3566fa98b13206583e6eb90b8304758b35548914944Nick Lewycky << BB->getName() << "\n"; 3576fa98b13206583e6eb90b8304758b35548914944Nick Lewycky } 3586fa98b13206583e6eb90b8304758b35548914944Nick Lewycky BlocksToNotExtractFile.close(); 3596fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 3606fa98b13206583e6eb90b8304758b35548914944Nick Lewycky const char *uniqueFN = uniqueFilename.c_str(); 3616fa98b13206583e6eb90b8304758b35548914944Nick Lewycky ExtraArg = (char*)malloc(23 + strlen(uniqueFN)); 3626fa98b13206583e6eb90b8304758b35548914944Nick Lewycky strcat(strcpy(ExtraArg, "--extract-blocks-file="), uniqueFN); 3636fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 3645e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner std::vector<const PassInfo*> PI; 3656fa98b13206583e6eb90b8304758b35548914944Nick Lewycky std::vector<BasicBlock *> EmptyBBs; // This parameter is ignored. 3666fa98b13206583e6eb90b8304758b35548914944Nick Lewycky PI.push_back(getPI(createBlockExtractorPass(EmptyBBs))); 3676fa98b13206583e6eb90b8304758b35548914944Nick Lewycky Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg); 3686fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 3696fa98b13206583e6eb90b8304758b35548914944Nick Lewycky if (uniqueFilename.exists()) 3706fa98b13206583e6eb90b8304758b35548914944Nick Lewycky uniqueFilename.eraseFromDisk(); // Free disk space 3716fa98b13206583e6eb90b8304758b35548914944Nick Lewycky free(ExtraArg); 3726fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 373891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner if (Ret == 0) { 3745e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner std::cout << "*** Basic Block extraction failed, please report a bug!\n"; 375891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner M = swapProgramIn(M); 3768ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif EmitProgressBitcode("basicblockextractfail", true); 377b923b2e04bdc8597de24a756d7e073bd5bfe1e44Chris Lattner swapProgramIn(M); 378891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner } 3795e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner return Ret; 3805e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner} 381