1 //===-- LTOModule.cpp - LLVM Link Time Optimizer --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
13 //===----------------------------------------------------------------------===//
15 #include "LTOModule.h"
17 #include "llvm/Constants.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Bitcode/ReaderWriter.h"
23 #include "llvm/Support/SystemUtils.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/Host.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/Support/Process.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include "llvm/Support/TargetRegistry.h"
31 #include "llvm/Support/TargetSelect.h"
32 #include "llvm/Support/system_error.h"
33 #include "llvm/MC/MCAsmInfo.h"
34 #include "llvm/MC/MCExpr.h"
35 #include "llvm/MC/MCInst.h"
36 #include "llvm/MC/MCParser/MCAsmParser.h"
37 #include "llvm/MC/MCStreamer.h"
38 #include "llvm/MC/MCSubtargetInfo.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/MC/SubtargetFeature.h"
41 #include "llvm/MC/MCTargetAsmParser.h"
42 #include "llvm/Target/TargetMachine.h"
43 #include "llvm/Target/TargetRegisterInfo.h"
47 bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
48 return llvm::sys::IdentifyFileType((char*)mem, length)
49 == llvm::sys::Bitcode_FileType;
52 bool LTOModule::isBitcodeFile(const char *path) {
53 return llvm::sys::Path(path).isBitcodeFile();
56 bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
57 const char *triplePrefix) {
58 MemoryBuffer *buffer = makeBuffer(mem, length);
61 return isTargetMatch(buffer, triplePrefix);
64 bool LTOModule::isBitcodeFileForTarget(const char *path,
65 const char *triplePrefix) {
66 OwningPtr<MemoryBuffer> buffer;
67 if (MemoryBuffer::getFile(path, buffer))
69 return isTargetMatch(buffer.take(), triplePrefix);
72 // Takes ownership of buffer.
73 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
74 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
76 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
80 LTOModule::LTOModule(Module *m, TargetMachine *t)
81 : _module(m), _target(t),
82 _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL),
83 _mangler(_context, *_target->getTargetData())
87 LTOModule *LTOModule::makeLTOModule(const char *path,
88 std::string &errMsg) {
89 OwningPtr<MemoryBuffer> buffer;
90 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
91 errMsg = ec.message();
94 return makeLTOModule(buffer.take(), errMsg);
97 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
98 size_t size, std::string &errMsg) {
99 return makeLTOModule(fd, path, size, size, 0, errMsg);
102 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
106 std::string &errMsg) {
107 OwningPtr<MemoryBuffer> buffer;
108 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
109 map_size, offset, false)) {
110 errMsg = ec.message();
113 return makeLTOModule(buffer.take(), errMsg);
116 /// makeBuffer - Create a MemoryBuffer from a memory range.
117 MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
118 const char *startPtr = (char*)mem;
119 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
122 LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
123 std::string &errMsg) {
124 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
127 return makeLTOModule(buffer.take(), errMsg);
130 LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
131 std::string &errMsg) {
132 static bool Initialized = false;
134 InitializeAllTargets();
135 InitializeAllTargetMCs();
136 InitializeAllAsmParsers();
140 // parse bitcode buffer
141 OwningPtr<Module> m(getLazyBitcodeModule(buffer, getGlobalContext(),
148 std::string Triple = m->getTargetTriple();
150 Triple = sys::getDefaultTargetTriple();
152 // find machine architecture for this module
153 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
157 // construct LTOModule, hand over ownership of module and target
158 SubtargetFeatures Features;
159 Features.getDefaultSubtargetFeatures(llvm::Triple(Triple));
160 std::string FeatureStr = Features.getString();
162 TargetMachine *target = march->createTargetMachine(Triple, CPU, FeatureStr);
163 LTOModule *Ret = new LTOModule(m.take(), target);
164 if (Ret->ParseSymbols(errMsg)) {
172 const char *LTOModule::getTargetTriple() {
173 return _module->getTargetTriple().c_str();
176 void LTOModule::setTargetTriple(const char *triple) {
177 _module->setTargetTriple(triple);
180 void LTOModule::addDefinedFunctionSymbol(Function *f) {
181 // add to list of defined symbols
182 addDefinedSymbol(f, true);
185 // Get string that data pointer points to.
186 bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
187 if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
188 Constant *op = ce->getOperand(0);
189 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
190 Constant *cn = gvn->getInitializer();
191 if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) {
192 if (ca->isCString()) {
193 name = ".objc_class_name_" + ca->getAsCString();
202 // Parse i386/ppc ObjC class data structure.
203 void LTOModule::addObjCClass(GlobalVariable *clgv) {
204 ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
207 // second slot in __OBJC,__class is pointer to superclass name
208 std::string superclassName;
209 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
210 NameAndAttributes info;
211 StringMap<NameAndAttributes>::value_type &entry =
212 _undefines.GetOrCreateValue(superclassName);
213 if (!entry.getValue().name) {
214 const char *symbolName = entry.getKey().data();
215 info.name = symbolName;
216 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
217 entry.setValue(info);
221 // third slot in __OBJC,__class is pointer to class name
222 std::string className;
223 if (objcClassNameFromExpression(c->getOperand(2), className)) {
224 StringSet::value_type &entry = _defines.GetOrCreateValue(className);
226 NameAndAttributes info;
227 info.name = entry.getKey().data();
228 info.attributes = lto_symbol_attributes(LTO_SYMBOL_PERMISSIONS_DATA |
229 LTO_SYMBOL_DEFINITION_REGULAR |
230 LTO_SYMBOL_SCOPE_DEFAULT);
231 _symbols.push_back(info);
236 // Parse i386/ppc ObjC category data structure.
237 void LTOModule::addObjCCategory(GlobalVariable *clgv) {
238 ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer());
241 // second slot in __OBJC,__category is pointer to target class name
242 std::string targetclassName;
243 if (!objcClassNameFromExpression(c->getOperand(1), targetclassName))
246 NameAndAttributes info;
247 StringMap<NameAndAttributes>::value_type &entry =
248 _undefines.GetOrCreateValue(targetclassName);
250 if (entry.getValue().name)
253 const char *symbolName = entry.getKey().data();
254 info.name = symbolName;
255 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
256 entry.setValue(info);
260 // Parse i386/ppc ObjC class list data structure.
261 void LTOModule::addObjCClassRef(GlobalVariable *clgv) {
262 std::string targetclassName;
263 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
266 NameAndAttributes info;
267 StringMap<NameAndAttributes>::value_type &entry =
268 _undefines.GetOrCreateValue(targetclassName);
269 if (entry.getValue().name)
272 const char *symbolName = entry.getKey().data();
273 info.name = symbolName;
274 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
275 entry.setValue(info);
279 void LTOModule::addDefinedDataSymbol(GlobalValue *v) {
280 // Add to list of defined symbols.
281 addDefinedSymbol(v, false);
283 // Special case i386/ppc ObjC data structures in magic sections:
284 // The issue is that the old ObjC object format did some strange
285 // contortions to avoid real linker symbols. For instance, the
286 // ObjC class data structure is allocated statically in the executable
287 // that defines that class. That data structures contains a pointer to
288 // its superclass. But instead of just initializing that part of the
289 // struct to the address of its superclass, and letting the static and
290 // dynamic linkers do the rest, the runtime works by having that field
291 // instead point to a C-string that is the name of the superclass.
292 // At runtime the objc initialization updates that pointer and sets
293 // it to point to the actual super class. As far as the linker
294 // knows it is just a pointer to a string. But then someone wanted the
295 // linker to issue errors at build time if the superclass was not found.
296 // So they figured out a way in mach-o object format to use an absolute
297 // symbols (.objc_class_name_Foo = 0) and a floating reference
298 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
299 // a class was missing.
300 // The following synthesizes the implicit .objc_* symbols for the linker
301 // from the ObjC data structures generated by the front end.
302 if (v->hasSection() /* && isTargetDarwin */) {
303 // special case if this data blob is an ObjC class definition
304 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
305 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
310 // special case if this data blob is an ObjC category definition
311 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
312 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
317 // special case if this data blob is the list of referenced classes
318 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
319 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
326 void LTOModule::addDefinedSymbol(GlobalValue *def, bool isFunction) {
327 // ignore all llvm.* symbols
328 if (def->getName().startswith("llvm."))
331 // string is owned by _defines
332 SmallString<64> Buffer;
333 _mangler.getNameWithPrefix(Buffer, def, false);
335 // set alignment part log2() can have rounding errors
336 uint32_t align = def->getAlignment();
337 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
339 // set permissions part
341 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
343 GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
344 if (gv && gv->isConstant())
345 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
347 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
350 // set definition part
351 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
352 def->hasLinkerPrivateWeakLinkage() ||
353 def->hasLinkerPrivateWeakDefAutoLinkage())
354 attr |= LTO_SYMBOL_DEFINITION_WEAK;
355 else if (def->hasCommonLinkage())
356 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
358 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
361 if (def->hasHiddenVisibility())
362 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
363 else if (def->hasProtectedVisibility())
364 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
365 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
366 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
367 def->hasLinkerPrivateWeakLinkage())
368 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
369 else if (def->hasLinkerPrivateWeakDefAutoLinkage())
370 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
372 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
374 // add to table of symbols
375 NameAndAttributes info;
376 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer);
379 StringRef Name = entry.getKey();
380 info.name = Name.data();
381 assert(info.name[Name.size()] == '\0');
382 info.attributes = (lto_symbol_attributes)attr;
383 _symbols.push_back(info);
386 void LTOModule::addAsmGlobalSymbol(const char *name,
387 lto_symbol_attributes scope) {
388 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
390 // only add new define if not already defined
391 if (entry.getValue())
395 const char *symbolName = entry.getKey().data();
396 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
398 NameAndAttributes info;
399 info.name = symbolName;
400 info.attributes = (lto_symbol_attributes)attr;
401 _symbols.push_back(info);
404 void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
405 StringMap<NameAndAttributes>::value_type &entry =
406 _undefines.GetOrCreateValue(name);
408 _asm_undefines.push_back(entry.getKey().data());
410 // we already have the symbol
411 if (entry.getValue().name)
414 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
415 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
416 NameAndAttributes info;
417 info.name = entry.getKey().data();
418 info.attributes = (lto_symbol_attributes)attr;
420 entry.setValue(info);
423 void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl) {
424 // ignore all llvm.* symbols
425 if (decl->getName().startswith("llvm."))
428 // ignore all aliases
429 if (isa<GlobalAlias>(decl))
432 SmallString<64> name;
433 _mangler.getNameWithPrefix(name, decl, false);
435 StringMap<NameAndAttributes>::value_type &entry =
436 _undefines.GetOrCreateValue(name);
438 // we already have the symbol
439 if (entry.getValue().name)
442 NameAndAttributes info;
444 info.name = entry.getKey().data();
445 if (decl->hasExternalWeakLinkage())
446 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
448 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
450 entry.setValue(info);
454 class RecordStreamer : public MCStreamer {
456 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used};
459 StringMap<State> Symbols;
461 void markDefined(const MCSymbol &Symbol) {
462 State &S = Symbols[Symbol.getName()];
475 void markGlobal(const MCSymbol &Symbol) {
476 State &S = Symbols[Symbol.getName()];
490 void markUsed(const MCSymbol &Symbol) {
491 State &S = Symbols[Symbol.getName()];
505 // FIXME: mostly copied for the obj streamer.
506 void AddValueSymbols(const MCExpr *Value) {
507 switch (Value->getKind()) {
509 // FIXME: What should we do in here?
512 case MCExpr::Constant:
515 case MCExpr::Binary: {
516 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
517 AddValueSymbols(BE->getLHS());
518 AddValueSymbols(BE->getRHS());
522 case MCExpr::SymbolRef:
523 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
527 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
533 typedef StringMap<State>::const_iterator const_iterator;
535 const_iterator begin() {
536 return Symbols.begin();
539 const_iterator end() {
540 return Symbols.end();
543 RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
545 virtual void ChangeSection(const MCSection *Section) {}
546 virtual void InitSections() {}
547 virtual void EmitLabel(MCSymbol *Symbol) {
548 Symbol->setSection(*getCurrentSection());
549 markDefined(*Symbol);
551 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
552 virtual void EmitThumbFunc(MCSymbol *Func) {}
553 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
554 // FIXME: should we handle aliases?
555 markDefined(*Symbol);
557 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
558 if (Attribute == MCSA_Global)
561 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
562 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
563 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
564 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
565 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
566 unsigned Size , unsigned ByteAlignment) {
567 markDefined(*Symbol);
569 virtual void EmitCOFFSymbolType(int Type) {}
570 virtual void EndCOFFSymbolDef() {}
571 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
572 unsigned ByteAlignment) {
573 markDefined(*Symbol);
575 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
576 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
577 unsigned ByteAlignment) {}
578 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
579 uint64_t Size, unsigned ByteAlignment) {}
580 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {}
581 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
582 unsigned AddrSpace) {}
583 virtual void EmitULEB128Value(const MCExpr *Value) {}
584 virtual void EmitSLEB128Value(const MCExpr *Value) {}
585 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
587 unsigned MaxBytesToEmit) {}
588 virtual void EmitCodeAlignment(unsigned ByteAlignment,
589 unsigned MaxBytesToEmit) {}
590 virtual void EmitValueToOffset(const MCExpr *Offset,
591 unsigned char Value ) {}
592 virtual void EmitFileDirective(StringRef Filename) {}
593 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
594 const MCSymbol *LastLabel,
595 const MCSymbol *Label,
596 unsigned PointerSize) {}
598 virtual void EmitInstruction(const MCInst &Inst) {
600 for (unsigned i = Inst.getNumOperands(); i--; )
601 if (Inst.getOperand(i).isExpr())
602 AddValueSymbols(Inst.getOperand(i).getExpr());
604 virtual void Finish() {}
608 bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
609 const std::string &inlineAsm = _module->getModuleInlineAsm();
610 if (inlineAsm.empty())
613 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(_context));
614 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
616 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
617 OwningPtr<MCAsmParser> Parser(createMCAsmParser(SrcMgr,
619 *_target->getMCAsmInfo()));
620 OwningPtr<MCSubtargetInfo> STI(_target->getTarget().
621 createMCSubtargetInfo(_target->getTargetTriple(),
622 _target->getTargetCPU(),
623 _target->getTargetFeatureString()));
624 OwningPtr<MCTargetAsmParser>
625 TAP(_target->getTarget().createMCAsmParser(*STI, *Parser.get()));
627 errMsg = "target " + std::string(_target->getTarget().getName()) +
628 " does not define AsmParser.";
632 Parser->setTargetParser(*TAP);
633 int Res = Parser->Run(false);
637 for (RecordStreamer::const_iterator i = Streamer->begin(),
638 e = Streamer->end(); i != e; ++i) {
639 StringRef Key = i->first();
640 RecordStreamer::State Value = i->second;
641 if (Value == RecordStreamer::DefinedGlobal)
642 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
643 else if (Value == RecordStreamer::Defined)
644 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
645 else if (Value == RecordStreamer::Global ||
646 Value == RecordStreamer::Used)
647 addAsmGlobalSymbolUndef(Key.data());
652 static bool isDeclaration(const GlobalValue &V) {
653 if (V.hasAvailableExternallyLinkage())
655 if (V.isMaterializable())
657 return V.isDeclaration();
660 static bool isAliasToDeclaration(const GlobalAlias &V) {
661 return isDeclaration(*V.getAliasedGlobal());
664 bool LTOModule::ParseSymbols(std::string &errMsg) {
666 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
667 if (isDeclaration(*f))
668 addPotentialUndefinedSymbol(f);
670 addDefinedFunctionSymbol(f);
674 for (Module::global_iterator v = _module->global_begin(),
675 e = _module->global_end(); v != e; ++v) {
676 if (isDeclaration(*v))
677 addPotentialUndefinedSymbol(v);
679 addDefinedDataSymbol(v);
683 if (addAsmGlobalSymbols(errMsg))
687 for (Module::alias_iterator i = _module->alias_begin(),
688 e = _module->alias_end(); i != e; ++i) {
689 if (isAliasToDeclaration(*i))
690 addPotentialUndefinedSymbol(i);
692 addDefinedDataSymbol(i);
695 // make symbols for all undefines
696 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
697 it != _undefines.end(); ++it) {
698 // if this symbol also has a definition, then don't make an undefine
699 // because it is a tentative definition
700 if (_defines.count(it->getKey()) == 0) {
701 NameAndAttributes info = it->getValue();
702 _symbols.push_back(info);
708 uint32_t LTOModule::getSymbolCount() {
709 return _symbols.size();
712 lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) {
713 if (index < _symbols.size())
714 return _symbols[index].attributes;
716 return lto_symbol_attributes(0);
719 const char *LTOModule::getSymbolName(uint32_t index) {
720 if (index < _symbols.size())
721 return _symbols[index].name;