diff options
author | Chris Lattner <sabre@nondot.org> | 2005-09-26 02:31:18 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2005-09-26 02:31:18 +0000 |
commit | db973e60ced33cfa0332462b1a2d5ae1ae15ad25 (patch) | |
tree | 9d050381480a895ade74f679f82c55f3d2463704 /lib/Transforms | |
parent | 7d8e58f38425692e203edc45e87dcf3dcd65dae6 (diff) | |
download | external_llvm-db973e60ced33cfa0332462b1a2d5ae1ae15ad25.zip external_llvm-db973e60ced33cfa0332462b1a2d5ae1ae15ad25.tar.gz external_llvm-db973e60ced33cfa0332462b1a2d5ae1ae15ad25.tar.bz2 |
factor some code into a InstallGlobalCtors method, add comments. No functionality change.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@23435 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
-rw-r--r-- | lib/Transforms/IPO/GlobalOpt.cpp | 87 |
1 files changed, 52 insertions, 35 deletions
diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index b016314..0b4b351 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -1164,6 +1164,8 @@ GlobalVariable *GlobalOpt::FindGlobalCtors(Module &M) { return 0; } +/// ParseGlobalCtors - Given a llvm.global_ctors list that we can understand, +/// return a list of the functions and null terminator as a vector. static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) { ConstantArray *CA = cast<ConstantArray>(GV->getInitializer()); std::vector<Function*> Result; @@ -1175,38 +1177,11 @@ static std::vector<Function*> ParseGlobalCtors(GlobalVariable *GV) { return Result; } -/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible. -/// Return true if anything changed. -bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) { - std::vector<Function*> Ctors = ParseGlobalCtors(GCL); - bool MadeChange = false; - if (Ctors.empty()) return false; - - // Loop over global ctors, optimizing them when we can. - for (unsigned i = 0; i != Ctors.size(); ++i) { - Function *F = Ctors[i]; - // Found a null terminator in the middle of the list, prune off the rest of - // the list. - if (F == 0) { - if (i != Ctors.size()-1) { - Ctors.resize(i+1); - MadeChange = true; - } - break; - } - - // If the function is empty, just remove it from the ctor list. - if (!F->empty() && isa<ReturnInst>(F->begin()->getTerminator()) && - &F->begin()->front() == F->begin()->getTerminator()) { - Ctors.erase(Ctors.begin()+i); - MadeChange = true; - --i; - ++NumEmptyCtor; - } - } - - if (!MadeChange) return false; - +/// InstallGlobalCtors - Given a specified llvm.global_ctors list, install the +/// specified array, returning the new global to use. +static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL, + const std::vector<Function*> &Ctors) { + // If we made a change, reassemble the initializer list. std::vector<Constant*> CSVals; CSVals.push_back(ConstantSInt::get(Type::IntTy, 65535)); CSVals.push_back(0); @@ -1225,13 +1200,19 @@ bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) { } CAList.push_back(ConstantStruct::get(CSVals)); } - + // Create the array initializer. const Type *StructTy = cast<ArrayType>(GCL->getType()->getElementType())->getElementType(); Constant *CA = ConstantArray::get(ArrayType::get(StructTy, CAList.size()), CAList); + // If we didn't change the number of elements, don't create a new GV. + if (CA->getType() == GCL->getInitializer()->getType()) { + GCL->setInitializer(CA); + return GCL; + } + // Create the new global and insert it next to the existing list. GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(), GCL->getLinkage(), CA, @@ -1249,9 +1230,45 @@ bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) { GCL->eraseFromParent(); if (Ctors.size()) - GCL = NGV; + return NGV; else - GCL = 0; + return 0; +} + + +/// OptimizeGlobalCtorsList - Simplify and evaluation global ctors if possible. +/// Return true if anything changed. +bool GlobalOpt::OptimizeGlobalCtorsList(GlobalVariable *&GCL) { + std::vector<Function*> Ctors = ParseGlobalCtors(GCL); + bool MadeChange = false; + if (Ctors.empty()) return false; + + // Loop over global ctors, optimizing them when we can. + for (unsigned i = 0; i != Ctors.size(); ++i) { + Function *F = Ctors[i]; + // Found a null terminator in the middle of the list, prune off the rest of + // the list. + if (F == 0) { + if (i != Ctors.size()-1) { + Ctors.resize(i+1); + MadeChange = true; + } + break; + } + + // If the function is empty, just remove it from the ctor list. + if (!F->empty() && isa<ReturnInst>(F->begin()->getTerminator()) && + &F->begin()->front() == F->begin()->getTerminator()) { + Ctors.erase(Ctors.begin()+i); + MadeChange = true; + --i; + ++NumEmptyCtor; + } + } + + if (!MadeChange) return false; + + GCL = InstallGlobalCtors(GCL, Ctors); return true; } |