ExtractFunction.cpp revision baa26395ccf17fc988bb9cf62d6659ca8415ece9
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" 3274382b7c699120fbec5cb5603c9cf4212eb37f06Chris Lattner#include "llvm/Support/raw_ostream.h" 336fa98b13206583e6eb90b8304758b35548914944Nick Lewycky#include "llvm/System/Path.h" 346fa98b13206583e6eb90b8304758b35548914944Nick Lewycky#include "llvm/System/Signals.h" 35fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner#include <set> 36c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattnerusing namespace llvm; 37d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 38d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm { 39c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner bool DisableSimplifyCFG = false; 40d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace 41d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke 426db70ef879916e6115ac97eb76e4fea973652e2cChris Lattnernamespace { 436db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::opt<bool> 446db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner NoDCE ("disable-dce", 456db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::desc("Do not use the -dce pass to reduce testcases")); 4647ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner cl::opt<bool, true> 4747ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG), 486db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner cl::desc("Do not use the -simplifycfg pass to reduce testcases")); 496db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner} 50afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner 516520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deleteInstructionFromProgram - This method clones the current Program and 526520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deletes the specified instruction from the cloned module. It then runs a 536520785dcd22012535934098942d57c07c7631c2Chris Lattner/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which 546520785dcd22012535934098942d57c07c7631c2Chris Lattner/// depends on the value. The modified module is then returned. 556520785dcd22012535934098942d57c07c7631c2Chris Lattner/// 560cc8807029f577996a442b96d24c3346ed6de091Chris LattnerModule *BugDriver::deleteInstructionFromProgram(const Instruction *I, 576520785dcd22012535934098942d57c07c7631c2Chris Lattner unsigned Simplification) const { 586520785dcd22012535934098942d57c07c7631c2Chris Lattner Module *Result = CloneModule(Program); 596520785dcd22012535934098942d57c07c7631c2Chris Lattner 600cc8807029f577996a442b96d24c3346ed6de091Chris Lattner const BasicBlock *PBB = I->getParent(); 610cc8807029f577996a442b96d24c3346ed6de091Chris Lattner const Function *PF = PBB->getParent(); 626520785dcd22012535934098942d57c07c7631c2Chris Lattner 636520785dcd22012535934098942d57c07c7631c2Chris Lattner Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn 640cc8807029f577996a442b96d24c3346ed6de091Chris Lattner std::advance(RFI, std::distance(PF->getParent()->begin(), 650cc8807029f577996a442b96d24c3346ed6de091Chris Lattner Module::const_iterator(PF))); 666520785dcd22012535934098942d57c07c7631c2Chris Lattner 676520785dcd22012535934098942d57c07c7631c2Chris Lattner Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB 680cc8807029f577996a442b96d24c3346ed6de091Chris Lattner std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB))); 696520785dcd22012535934098942d57c07c7631c2Chris Lattner 706520785dcd22012535934098942d57c07c7631c2Chris Lattner BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst 710cc8807029f577996a442b96d24c3346ed6de091Chris Lattner std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I))); 720cc8807029f577996a442b96d24c3346ed6de091Chris Lattner Instruction *TheInst = RI; // Got the corresponding instruction! 736520785dcd22012535934098942d57c07c7631c2Chris Lattner 746520785dcd22012535934098942d57c07c7631c2Chris Lattner // If this instruction produces a value, replace any users with null values 75e78109eb3a3d177d03cea40b46d9dcfc9bf32210Chris Lattner if (isa<StructType>(TheInst->getType())) 769e9a0d5fc26878e51a58a8b57900fcbf952c2691Owen Anderson TheInst->replaceAllUsesWith(UndefValue::get(TheInst->getType())); 771d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson else if (TheInst->getType() != Type::getVoidTy(I->getContext())) 78a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType())); 796520785dcd22012535934098942d57c07c7631c2Chris Lattner 806520785dcd22012535934098942d57c07c7631c2Chris Lattner // Remove the instruction from the program. 810cc8807029f577996a442b96d24c3346ed6de091Chris Lattner TheInst->getParent()->getInstList().erase(TheInst); 826520785dcd22012535934098942d57c07c7631c2Chris Lattner 835a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 845a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner //writeProgramToFile("current.bc", Result); 855a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner 8644be25716628941b4cccccf56a28ee0ba2606850Chris Lattner // Spiff up the output a little bit. 876520785dcd22012535934098942d57c07c7631c2Chris Lattner PassManager Passes; 885da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner // Make sure that the appropriate target data is always used... 89831b1210390b3a00f68de9a79be0f4e13d6287b0Chris Lattner Passes.add(new TargetData(Result)); 905da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner 91efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner /// FIXME: If this used runPasses() like the methods below, we could get rid 92efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner /// of the -disable-* options! 936db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner if (Simplification > 1 && !NoDCE) 946520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.add(createDeadCodeEliminationPass()); 9547ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner if (Simplification && !DisableSimplifyCFG) 966520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.add(createCFGSimplificationPass()); // Delete dead control flow 9710f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner 9810f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner Passes.add(createVerifierPass()); 996520785dcd22012535934098942d57c07c7631c2Chris Lattner Passes.run(*Result); 1006520785dcd22012535934098942d57c07c7631c2Chris Lattner return Result; 1016520785dcd22012535934098942d57c07c7631c2Chris Lattner} 102ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner 103fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattnerstatic const PassInfo *getPI(Pass *P) { 104fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner const PassInfo *PI = P->getPassInfo(); 105fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner delete P; 106fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner return PI; 107fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner} 108fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner 109ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// performFinalCleanups - This method clones the current Program and performs 110ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// a series of cleanups intended to get rid of extra cruft on the module 1119b5b1905dbad19de374e7617f615ba522ffe0468Chris Lattner/// before handing it to the user. 112ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// 113fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris LattnerModule *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) { 11428b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner // Make all functions external, so GlobalDCE doesn't delete them... 11528b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) 11628b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner I->setLinkage(GlobalValue::ExternalLinkage); 1173da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 118fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner std::vector<const PassInfo*> CleanupPasses; 119fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner CleanupPasses.push_back(getPI(createGlobalDCEPass())); 120fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner CleanupPasses.push_back(getPI(createDeadTypeEliminationPass())); 121fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner 122c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner if (MayModifySemantics) 123c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner CleanupPasses.push_back(getPI(createDeadArgHackingPass())); 124c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner else 125c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner CleanupPasses.push_back(getPI(createDeadArgEliminationPass())); 126fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner 127a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner Module *New = runPassesOn(M, CleanupPasses); 128a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner if (New == 0) { 12965f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n"; 1309b5b1905dbad19de374e7617f615ba522ffe0468Chris Lattner return M; 131fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner } 132a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner delete M; 133a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner return New; 134ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner} 135be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 136be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 1377546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// ExtractLoop - Given a module, extract up to one loop from it into a new 1387546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// function. This returns null if there are no extractable loops in the 1397546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// program or if the loop extractor crashes. 1407546c3884a400b72d10fc19f120c6798b294a39dChris LattnerModule *BugDriver::ExtractLoop(Module *M) { 1417546c3884a400b72d10fc19f120c6798b294a39dChris Lattner std::vector<const PassInfo*> LoopExtractPasses; 1427546c3884a400b72d10fc19f120c6798b294a39dChris Lattner LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass())); 1437546c3884a400b72d10fc19f120c6798b294a39dChris Lattner 144a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner Module *NewM = runPassesOn(M, LoopExtractPasses); 145a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner if (NewM == 0) { 146a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner Module *Old = swapProgramIn(M); 147ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "*** Loop extraction failed: "; 1488ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif EmitProgressBitcode("loopextraction", true); 149ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "*** Sorry. :( Please report a bug!\n"; 150a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner swapProgramIn(Old); 1517546c3884a400b72d10fc19f120c6798b294a39dChris Lattner return 0; 1527546c3884a400b72d10fc19f120c6798b294a39dChris Lattner } 153a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner 154a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner // Check to see if we created any new functions. If not, no loops were 155a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner // extracted and we should return null. Limit the number of loops we extract 156a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner // to avoid taking forever. 157a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner static unsigned NumExtracted = 32; 15890c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner if (M->size() == NewM->size() || --NumExtracted == 0) { 159a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner delete NewM; 160a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner return 0; 16190c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner } else { 16290c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner assert(M->size() < NewM->size() && "Loop extract removed functions?"); 16390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner Module::iterator MI = NewM->begin(); 16490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner for (unsigned i = 0, e = M->size(); i != e; ++i) 16590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner ++MI; 166a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner } 1673da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman 168a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner return NewM; 1697546c3884a400b72d10fc19f120c6798b294a39dChris Lattner} 1707546c3884a400b72d10fc19f120c6798b294a39dChris Lattner 1717546c3884a400b72d10fc19f120c6798b294a39dChris Lattner 172be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of its basic 173be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// blocks, making it external. 174be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// 175be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnervoid llvm::DeleteFunctionBody(Function *F) { 176be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner // delete the body of the function... 177be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner F->deleteBody(); 1785cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer assert(F->isDeclaration() && "This didn't make the function external!"); 179be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner} 180be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner 1815a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// GetTorInit - Given a list of entries for static ctors/dtors, return them 1825a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// as a constant array. 1835a7a9e53861188ad84cb0e08e34562da744a907aChris Lattnerstatic Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) { 1845a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner assert(!TorList.empty() && "Don't create empty tor list!"); 1855a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner std::vector<Constant*> ArrayElts; 1865a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner for (unsigned i = 0, e = TorList.size(); i != e; ++i) { 1875a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner std::vector<Constant*> Elts; 1881d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson Elts.push_back(ConstantInt::get( 1891d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson Type::getInt32Ty(TorList[i].first->getContext()), TorList[i].second)); 1905a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner Elts.push_back(TorList[i].first); 191d7f2a6cb3fbc012763adb42fd967f6fefbb22a37Owen Anderson ArrayElts.push_back(ConstantStruct::get( 192d7f2a6cb3fbc012763adb42fd967f6fefbb22a37Owen Anderson TorList[i].first->getContext(), Elts)); 1935a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner } 194debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson return ConstantArray::get(ArrayType::get(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) { 2743f53fa9a51c4ce7ba81170ca7ab2e49bd37281b0Daniel Dunbar if (I->hasName() && I->getName()[0] == '\01') 2753f53fa9a51c4ce7ba81170ca7ab2e49bd37281b0Daniel Dunbar I->setName(I->getName().substr(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)) { 330ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "*** 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 339ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman std::string ErrorInfo; 340baa26395ccf17fc988bb9cf62d6659ca8415ece9Dan Gohman raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo); 341ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman if (!ErrorInfo.empty()) { 342ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "*** Basic Block extraction failed!\n"; 343ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman errs() << "Error writing list of blocks to not extract: " << ErrorInfo 34465f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman << "\n"; 3456fa98b13206583e6eb90b8304758b35548914944Nick Lewycky M = swapProgramIn(M); 3466fa98b13206583e6eb90b8304758b35548914944Nick Lewycky EmitProgressBitcode("basicblockextractfail", true); 3476fa98b13206583e6eb90b8304758b35548914944Nick Lewycky swapProgramIn(M); 3486fa98b13206583e6eb90b8304758b35548914944Nick Lewycky return 0; 3496fa98b13206583e6eb90b8304758b35548914944Nick Lewycky } 3506fa98b13206583e6eb90b8304758b35548914944Nick Lewycky for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end(); 3516fa98b13206583e6eb90b8304758b35548914944Nick Lewycky I != E; ++I) { 3526fa98b13206583e6eb90b8304758b35548914944Nick Lewycky BasicBlock *BB = *I; 3534a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner // If the BB doesn't have a name, give it one so we have something to key 3544a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner // off of. 3554a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner if (!BB->hasName()) BB->setName("tmpbb"); 356f6ccee5a9d2b9573f679bca6266ade3eb8cd3f88Daniel Dunbar BlocksToNotExtractFile << BB->getParent()->getNameStr() << " " 3576fa98b13206583e6eb90b8304758b35548914944Nick Lewycky << BB->getName() << "\n"; 3586fa98b13206583e6eb90b8304758b35548914944Nick Lewycky } 3596fa98b13206583e6eb90b8304758b35548914944Nick Lewycky BlocksToNotExtractFile.close(); 3606fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 3616fa98b13206583e6eb90b8304758b35548914944Nick Lewycky const char *uniqueFN = uniqueFilename.c_str(); 3626fa98b13206583e6eb90b8304758b35548914944Nick Lewycky ExtraArg = (char*)malloc(23 + strlen(uniqueFN)); 3636fa98b13206583e6eb90b8304758b35548914944Nick Lewycky strcat(strcpy(ExtraArg, "--extract-blocks-file="), uniqueFN); 3646fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 3655e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner std::vector<const PassInfo*> PI; 3666fa98b13206583e6eb90b8304758b35548914944Nick Lewycky std::vector<BasicBlock *> EmptyBBs; // This parameter is ignored. 3676fa98b13206583e6eb90b8304758b35548914944Nick Lewycky PI.push_back(getPI(createBlockExtractorPass(EmptyBBs))); 3686fa98b13206583e6eb90b8304758b35548914944Nick Lewycky Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg); 3696fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 3706fa98b13206583e6eb90b8304758b35548914944Nick Lewycky if (uniqueFilename.exists()) 3716fa98b13206583e6eb90b8304758b35548914944Nick Lewycky uniqueFilename.eraseFromDisk(); // Free disk space 3726fa98b13206583e6eb90b8304758b35548914944Nick Lewycky free(ExtraArg); 3736fa98b13206583e6eb90b8304758b35548914944Nick Lewycky 374891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner if (Ret == 0) { 375ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman outs() << "*** Basic Block extraction failed, please report a bug!\n"; 376891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner M = swapProgramIn(M); 3778ff70c2635bfd4e02c0140a5dc9ca909fffba35aGabor Greif EmitProgressBitcode("basicblockextractfail", true); 378b923b2e04bdc8597de24a756d7e073bd5bfe1e44Chris Lattner swapProgramIn(M); 379891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner } 3805e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner return Ret; 3815e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner} 382