aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/CodeGenModule.cpp
diff options
context:
space:
mode:
authorDaniel Dunbar <daniel@zuster.org>2009-02-13 20:29:50 +0000
committerDaniel Dunbar <daniel@zuster.org>2009-02-13 20:29:50 +0000
commit0269871c9cba493f76237175ab60313406f3bafa (patch)
treec447f60d3869082a1276bfc87936722e58fdd119 /lib/CodeGen/CodeGenModule.cpp
parent7d6dc4ff45917c480d8b3350385337f34511ebaf (diff)
Rename EmitStatics (etc) to EmitDeferred; provide basic infrastructure
for attribute used support. - No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64487 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CodeGenModule.cpp')
-rw-r--r--lib/CodeGen/CodeGenModule.cpp49
1 files changed, 36 insertions, 13 deletions
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index 091158daba..69af3d265c 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -60,7 +60,7 @@ CodeGenModule::~CodeGenModule() {
}
void CodeGenModule::Release() {
- EmitStatics();
+ EmitDeferred();
EmitAliases();
if (Runtime)
if (llvm::Function *ObjCInitFunction = Runtime->ModuleInitFunction())
@@ -68,6 +68,7 @@ void CodeGenModule::Release() {
EmitCtorList(GlobalCtors, "llvm.global_ctors");
EmitCtorList(GlobalDtors, "llvm.global_dtors");
EmitAnnotations();
+ EmitLLVMUsed();
BindRuntimeFunctions();
}
@@ -388,16 +389,40 @@ void CodeGenModule::EmitAliases() {
}
}
-void CodeGenModule::EmitStatics() {
- // Emit code for each used static decl encountered. Since a previously unused
- // static decl may become used during the generation of code for a static
- // function, iterate until no changes are made.
+void CodeGenModule::AddUsedGlobal(llvm::GlobalValue *GV) {
+ assert(!GV->isDeclaration() &&
+ "Only globals with definition can force usage.");
+ llvm::Type *i8PTy = llvm::PointerType::getUnqual(llvm::Type::Int8Ty);
+ LLVMUsed.push_back(llvm::ConstantExpr::getBitCast(GV, i8PTy));
+}
+
+void CodeGenModule::EmitLLVMUsed() {
+ // Don't create llvm.used if there is no need.
+ if (LLVMUsed.empty())
+ return;
+
+ llvm::ArrayType *ATy = llvm::ArrayType::get(LLVMUsed[0]->getType(),
+ LLVMUsed.size());
+ llvm::GlobalVariable *GV =
+ new llvm::GlobalVariable(ATy, false,
+ llvm::GlobalValue::AppendingLinkage,
+ llvm::ConstantArray::get(ATy, LLVMUsed),
+ "llvm.used", &getModule());
+
+ GV->setSection("llvm.metadata");
+}
+
+void CodeGenModule::EmitDeferred() {
+ // Emit code for any deferred decl which was used. Since a
+ // previously unused static decl may become used during the
+ // generation of code for a static function, iterate until no
+ // changes are made.
bool Changed;
do {
Changed = false;
- for (std::list<const ValueDecl*>::iterator i = StaticDecls.begin(),
- e = StaticDecls.end(); i != e; ) {
+ for (std::list<const ValueDecl*>::iterator i = DeferredDecls.begin(),
+ e = DeferredDecls.end(); i != e; ) {
const ValueDecl *D = *i;
// Check if we have used a decl with the same name
@@ -414,7 +439,7 @@ void CodeGenModule::EmitStatics() {
EmitGlobalDefinition(D);
// Erase the used decl from the list.
- i = StaticDecls.erase(i);
+ i = DeferredDecls.erase(i);
// Remember that we made a change.
Changed = true;
@@ -481,16 +506,14 @@ void CodeGenModule::EmitGlobal(const ValueDecl *Global) {
isDef = FD->isThisDeclarationADefinition();
isStatic = FD->getStorageClass() == FunctionDecl::Static;
- } else if (const VarDecl *VD = cast<VarDecl>(Global)) {
+ } else {
+ const VarDecl *VD = cast<VarDecl>(Global);
assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
isDef = !((VD->getStorageClass() == VarDecl::Extern ||
VD->getStorageClass() == VarDecl::PrivateExtern) &&
VD->getInit() == 0);
isStatic = VD->getStorageClass() == VarDecl::Static;
- } else {
- assert(0 && "Invalid argument to EmitGlobal");
- return;
}
// Forward declarations are emitted lazily on first use.
@@ -500,7 +523,7 @@ void CodeGenModule::EmitGlobal(const ValueDecl *Global) {
// If the global is a static, defer code generation until later so
// we can easily omit unused statics.
if (isStatic && !Features.EmitAllDecls) {
- StaticDecls.push_back(Global);
+ DeferredDecls.push_back(Global);
return;
}