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