Modify the code that emits the module flags to use the new module flags accessor

method. This allows the target lowering code to not have to deal with MDNodes.

Also, avoid leaking memory like a sieve by not creating a global variable for
the image info section, but just emitting the code directly.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@150624 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Bill Wendling
2012-02-15 22:36:15 +00:00
parent f20f281368
commit 057d521e3d
4 changed files with 47 additions and 43 deletions

View File

@ -15,9 +15,9 @@
#ifndef LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H #ifndef LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
#define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H #define LLVM_CODEGEN_TARGETLOWERINGOBJECTFILEIMPL_H
#include "llvm/ADT/StringRef.h"
#include "llvm/MC/SectionKind.h" #include "llvm/MC/SectionKind.h"
#include "llvm/Target/TargetLoweringObjectFile.h" #include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/ADT/StringRef.h"
namespace llvm { namespace llvm {
class MachineModuleInfo; class MachineModuleInfo;
@ -80,7 +80,8 @@ public:
/// emitModuleFlags - Emit the module flags that specify the garbage /// emitModuleFlags - Emit the module flags that specify the garbage
/// collection information. /// collection information.
virtual void emitModuleFlags(MCStreamer &Streamer, NamedMDNode *ModFlags, virtual void emitModuleFlags(MCStreamer &Streamer,
ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Mangler *Mang, const TargetMachine &TM) const; Mangler *Mang, const TargetMachine &TM) const;
virtual const MCSection * virtual const MCSection *

View File

@ -15,9 +15,11 @@
#ifndef LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H #ifndef LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
#define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H #define LLVM_TARGET_TARGETLOWERINGOBJECTFILE_H
#include "llvm/ADT/StringRef.h" #include "llvm/Module.h"
#include "llvm/MC/MCObjectFileInfo.h" #include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/SectionKind.h" #include "llvm/MC/SectionKind.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
namespace llvm { namespace llvm {
class MachineModuleInfo; class MachineModuleInfo;
@ -56,8 +58,9 @@ public:
const MCSymbol *Sym) const; const MCSymbol *Sym) const;
/// emitModuleFlags - Emit the module flags that the platform cares about. /// emitModuleFlags - Emit the module flags that the platform cares about.
virtual void emitModuleFlags(MCStreamer &, NamedMDNode *, Mangler *, virtual void emitModuleFlags(MCStreamer &,
const TargetMachine &) const { ArrayRef<Module::ModuleFlagEntry>,
Mangler *, const TargetMachine &) const {
} }
/// shouldEmitUsedDirectiveFor - This hook allows targets to selectively /// shouldEmitUsedDirectiveFor - This hook allows targets to selectively

View File

@ -858,8 +858,10 @@ bool AsmPrinter::doFinalization(Module &M) {
} }
// Emit module flags. // Emit module flags.
if (NamedMDNode *ModFlags = M.getModuleFlagsMetadata()) SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
getObjFileLowering().emitModuleFlags(OutStreamer, ModFlags, Mang, TM); M.getModuleFlagsMetadata(ModuleFlags);
if (!ModuleFlags.empty())
getObjFileLowering().emitModuleFlags(OutStreamer, ModuleFlags, Mang, TM);
// Finalize debug and EH information. // Finalize debug and EH information.
if (DE) { if (DE) {

View File

@ -374,57 +374,55 @@ TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
/// emitModuleFlags - Emit the module flags that specify the garbage collection /// emitModuleFlags - Emit the module flags that specify the garbage collection
/// information. /// information.
void TargetLoweringObjectFileMachO:: void TargetLoweringObjectFileMachO::
emitModuleFlags(MCStreamer &Streamer, NamedMDNode *ModFlags, emitModuleFlags(MCStreamer &Streamer,
ArrayRef<Module::ModuleFlagEntry> ModuleFlags,
Mangler *Mang, const TargetMachine &TM) const { Mangler *Mang, const TargetMachine &TM) const {
StringRef Version("Objective-C Image Info Version");
StringRef SectionSpec("Objective-C Image Info Section");
StringRef GC("Objective-C Garbage Collection");
StringRef GCOnly("Objective-C GC Only");
unsigned VersionVal = 0; unsigned VersionVal = 0;
unsigned GCFlags = 0; unsigned GCFlags = 0;
StringRef Section; StringRef SectionVal;
for (unsigned i = 0, e = ModFlags->getNumOperands(); i != e; ++i) { for (ArrayRef<Module::ModuleFlagEntry>::iterator
MDNode *Flag = ModFlags->getOperand(i); i = ModuleFlags.begin(), e = ModuleFlags.end(); i != e; ++i) {
Value *Behavior = Flag->getOperand(0); const Module::ModuleFlagEntry &MFE = *i;
// Ignore flags with 'Require' behavior. // Ignore flags with 'Require' behavior.
if (cast<ConstantInt>(Behavior)->getZExtValue() == Module::Require) if (MFE.Behavior == Module::Require)
continue; continue;
Value *Key = Flag->getOperand(1); StringRef Key = MFE.Key->getString();
Value *Val = Flag->getOperand(2); Value *Val = MFE.Val;
StringRef KeyStr = cast<MDString>(Key)->getString(); if (Key == "Objective-C Image Info Version")
if (KeyStr == Version)
VersionVal = cast<ConstantInt>(Val)->getZExtValue(); VersionVal = cast<ConstantInt>(Val)->getZExtValue();
else if (KeyStr == GC || KeyStr == GCOnly) else if (Key == "Objective-C Garbage Collection" ||
Key == "Objective-C GC Only")
GCFlags |= cast<ConstantInt>(Val)->getZExtValue(); GCFlags |= cast<ConstantInt>(Val)->getZExtValue();
else if (KeyStr == SectionSpec) else if (Key == "Objective-C Image Info Section")
Section = cast<MDString>(Val)->getString(); SectionVal = cast<MDString>(Val)->getString();
} }
// The section is mandatory. If we don't have it, then we don't have image // The section is mandatory. If we don't have it, then we don't have GC info.
// info information. if (SectionVal.empty()) return;
if (Section.empty()) return;
uint32_t Values[2] = { VersionVal, GCFlags }; StringRef Segment, Section;
Module *M = ModFlags->getParent(); unsigned TAA = 0, StubSize = 0;
Constant *Init = ConstantDataArray::get(M->getContext(), Values); bool TAAParsed;
GlobalVariable *GV = new GlobalVariable(*M, Init->getType(), false, std::string ErrorCode =
GlobalValue::InternalLinkage, Init, MCSectionMachO::ParseSectionSpecifier(SectionVal, Segment, Section,
"\01L_OBJC_IMAGE_INFO"); TAA, TAAParsed, StubSize);
GV->setSection(Section); if (!ErrorCode.empty()) {
// If invalid, report the error with report_fatal_error.
MCSymbol *GVSym = Mang->getSymbol(GV); report_fatal_error("Invalid section specifier '" +
SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM); Section + "': " + ErrorCode + ".");
const MCSection *TheSection = SectionForGlobal(GV, GVKind, Mang, TM); }
Streamer.SwitchSection(TheSection);
Streamer.EmitLabel(GVSym);
// Get the section.
const MCSectionMachO *S =
getContext().getMachOSection(Segment, Section, TAA, StubSize,
SectionKind::getDataRel());
Streamer.SwitchSection(S);
Streamer.EmitLabel(getContext().
GetOrCreateSymbol(StringRef("L_OBJC_IMAGE_INFO")));
Streamer.EmitIntValue(VersionVal, 4); Streamer.EmitIntValue(VersionVal, 4);
Streamer.EmitIntValue(GCFlags, 4); Streamer.EmitIntValue(GCFlags, 4);
Streamer.AddBlankLine(); Streamer.AddBlankLine();