LLVM API Documentation
00001 //===-- ExtractGV.cpp - Global Value extraction pass ----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This pass extracts global values 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Instructions.h" 00015 #include "llvm/LLVMContext.h" 00016 #include "llvm/Module.h" 00017 #include "llvm/Pass.h" 00018 #include "llvm/Constants.h" 00019 #include "llvm/Transforms/IPO.h" 00020 #include "llvm/Support/Compiler.h" 00021 #include <algorithm> 00022 using namespace llvm; 00023 00024 namespace { 00025 /// @brief A pass to extract specific functions and their dependencies. 00026 class VISIBILITY_HIDDEN GVExtractorPass : public ModulePass { 00027 std::vector<GlobalValue*> Named; 00028 bool deleteStuff; 00029 bool reLink; 00030 public: 00031 static char ID; // Pass identification, replacement for typeid 00032 00033 /// FunctionExtractorPass - If deleteFn is true, this pass deletes as the 00034 /// specified function. Otherwise, it deletes as much of the module as 00035 /// possible, except for the function specified. 00036 /// 00037 explicit GVExtractorPass(std::vector<GlobalValue*>& GVs, bool deleteS = true, 00038 bool relinkCallees = false) 00039 : ModulePass(&ID), Named(GVs), deleteStuff(deleteS), 00040 reLink(relinkCallees) {} 00041 00042 bool runOnModule(Module &M) { 00043 if (Named.size() == 0) { 00044 return false; // Nothing to extract 00045 } 00046 00047 00048 if (deleteStuff) 00049 return deleteGV(); 00050 M.setModuleInlineAsm(""); 00051 return isolateGV(M); 00052 } 00053 00054 bool deleteGV() { 00055 for (std::vector<GlobalValue*>::iterator GI = Named.begin(), 00056 GE = Named.end(); GI != GE; ++GI) { 00057 if (Function* NamedFunc = dyn_cast<Function>(*GI)) { 00058 // If we're in relinking mode, set linkage of all internal callees to 00059 // external. This will allow us extract function, and then - link 00060 // everything together 00061 if (reLink) { 00062 for (Function::iterator B = NamedFunc->begin(), BE = NamedFunc->end(); 00063 B != BE; ++B) { 00064 for (BasicBlock::iterator I = B->begin(), E = B->end(); 00065 I != E; ++I) { 00066 if (CallInst* callInst = dyn_cast<CallInst>(&*I)) { 00067 Function* Callee = callInst->getCalledFunction(); 00068 if (Callee && Callee->hasLocalLinkage()) 00069 Callee->setLinkage(GlobalValue::ExternalLinkage); 00070 } 00071 } 00072 } 00073 } 00074 00075 NamedFunc->setLinkage(GlobalValue::ExternalLinkage); 00076 NamedFunc->deleteBody(); 00077 assert(NamedFunc->isDeclaration() && "This didn't make the function external!"); 00078 } else { 00079 if (!(*GI)->isDeclaration()) { 00080 cast<GlobalVariable>(*GI)->setInitializer(0); //clear the initializer 00081 (*GI)->setLinkage(GlobalValue::ExternalLinkage); 00082 } 00083 } 00084 } 00085 return true; 00086 } 00087 00088 bool isolateGV(Module &M) { 00089 // Mark all globals internal 00090 // FIXME: what should we do with private linkage? 00091 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) 00092 if (!I->isDeclaration()) { 00093 I->setLinkage(GlobalValue::InternalLinkage); 00094 } 00095 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) 00096 if (!I->isDeclaration()) { 00097 I->setLinkage(GlobalValue::InternalLinkage); 00098 } 00099 00100 // Make sure our result is globally accessible... 00101 // by putting them in the used array 00102 { 00103 std::vector<Constant *> AUGs; 00104 const Type *SBP= 00105 PointerType::getUnqual(Type::getInt8Ty(M.getContext())); 00106 for (std::vector<GlobalValue*>::iterator GI = Named.begin(), 00107 GE = Named.end(); GI != GE; ++GI) { 00108 (*GI)->setLinkage(GlobalValue::ExternalLinkage); 00109 AUGs.push_back(ConstantExpr::getBitCast(*GI, SBP)); 00110 } 00111 ArrayType *AT = ArrayType::get(SBP, AUGs.size()); 00112 Constant *Init = ConstantArray::get(AT, AUGs); 00113 GlobalValue *gv = new GlobalVariable(M, AT, false, 00114 GlobalValue::AppendingLinkage, 00115 Init, "llvm.used"); 00116 gv->setSection("llvm.metadata"); 00117 } 00118 00119 // All of the functions may be used by global variables or the named 00120 // globals. Loop through them and create a new, external functions that 00121 // can be "used", instead of ones with bodies. 00122 std::vector<Function*> NewFunctions; 00123 00124 Function *Last = --M.end(); // Figure out where the last real fn is. 00125 00126 for (Module::iterator I = M.begin(); ; ++I) { 00127 if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) { 00128 Function *New = Function::Create(I->getFunctionType(), 00129 GlobalValue::ExternalLinkage); 00130 New->copyAttributesFrom(I); 00131 00132 // If it's not the named function, delete the body of the function 00133 I->dropAllReferences(); 00134 00135 M.getFunctionList().push_back(New); 00136 NewFunctions.push_back(New); 00137 New->takeName(I); 00138 } 00139 00140 if (&*I == Last) break; // Stop after processing the last function 00141 } 00142 00143 // Now that we have replacements all set up, loop through the module, 00144 // deleting the old functions, replacing them with the newly created 00145 // functions. 00146 if (!NewFunctions.empty()) { 00147 unsigned FuncNum = 0; 00148 Module::iterator I = M.begin(); 00149 do { 00150 if (std::find(Named.begin(), Named.end(), &*I) == Named.end()) { 00151 // Make everything that uses the old function use the new dummy fn 00152 I->replaceAllUsesWith(NewFunctions[FuncNum++]); 00153 00154 Function *Old = I; 00155 ++I; // Move the iterator to the new function 00156 00157 // Delete the old function! 00158 M.getFunctionList().erase(Old); 00159 00160 } else { 00161 ++I; // Skip the function we are extracting 00162 } 00163 } while (&*I != NewFunctions[0]); 00164 } 00165 00166 return true; 00167 } 00168 }; 00169 00170 char GVExtractorPass::ID = 0; 00171 } 00172 00173 ModulePass *llvm::createGVExtractionPass(std::vector<GlobalValue*>& GVs, 00174 bool deleteFn, bool relinkCallees) { 00175 return new GVExtractorPass(GVs, deleteFn, relinkCallees); 00176 }