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/system_error.h"
31 #include "llvm/Target/Mangler.h"
32 #include "llvm/Target/SubtargetFeature.h"
33 #include "llvm/MC/MCAsmInfo.h"
34 #include "llvm/MC/MCContext.h"
35 #include "llvm/MC/MCExpr.h"
36 #include "llvm/MC/MCInst.h"
37 #include "llvm/MC/MCParser/MCAsmParser.h"
38 #include "llvm/MC/MCStreamer.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/Target/TargetAsmParser.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetRegistry.h"
43 #include "llvm/Target/TargetSelect.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);
65 bool LTOModule::isBitcodeFileForTarget(const char *path,
66 const char *triplePrefix) {
67 OwningPtr<MemoryBuffer> buffer;
68 if (MemoryBuffer::getFile(path, buffer))
70 return isTargetMatch(buffer.take(), triplePrefix);
73 // Takes ownership of buffer.
74 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
75 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
77 return (strncmp(Triple.c_str(), triplePrefix,
78 strlen(triplePrefix)) == 0);
82 LTOModule::LTOModule(Module *m, TargetMachine *t)
83 : _module(m), _target(t)
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.get(), errMsg);
97 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
99 std::string &errMsg) {
100 return makeLTOModule(fd, path, size, size, 0, errMsg);
103 LTOModule *LTOModule::makeLTOModule(int fd, const char *path,
107 std::string &errMsg) {
108 OwningPtr<MemoryBuffer> buffer;
109 if (error_code ec = MemoryBuffer::getOpenFile(fd, path, buffer, file_size,
110 map_size, offset, false)) {
111 errMsg = ec.message();
114 return makeLTOModule(buffer.get(), errMsg);
117 /// makeBuffer - Create a MemoryBuffer from a memory range.
118 MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
119 const char *startPtr = (char*)mem;
120 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
124 LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
125 std::string &errMsg) {
126 OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length));
129 return makeLTOModule(buffer.get(), errMsg);
132 LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer,
133 std::string &errMsg) {
134 static bool Initialized = false;
136 InitializeAllTargets();
137 InitializeAllAsmParsers();
141 // parse bitcode buffer
142 OwningPtr<Module> m(ParseBitcodeFile(buffer, getGlobalContext(), &errMsg));
146 std::string Triple = m->getTargetTriple();
148 Triple = sys::getHostTriple();
150 // find machine architecture for this module
151 const Target *march = TargetRegistry::lookupTarget(Triple, errMsg);
155 // construct LTModule, hand over ownership of module and target
156 SubtargetFeatures Features;
157 Features.getDefaultSubtargetFeatures("" /* cpu */, llvm::Triple(Triple));
158 std::string FeatureStr = Features.getString();
159 TargetMachine *target = march->createTargetMachine(Triple, FeatureStr);
160 LTOModule *Ret = new LTOModule(m.take(), target);
161 bool Err = Ret->ParseSymbols();
170 const char *LTOModule::getTargetTriple() {
171 return _module->getTargetTriple().c_str();
174 void LTOModule::setTargetTriple(const char *triple) {
175 _module->setTargetTriple(triple);
178 void LTOModule::addDefinedFunctionSymbol(Function *f, Mangler &mangler) {
179 // add to list of defined symbols
180 addDefinedSymbol(f, mangler, true);
183 // Get string that data pointer points to.
184 bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
185 if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) {
186 Constant *op = ce->getOperand(0);
187 if (GlobalVariable *gvn = dyn_cast<GlobalVariable>(op)) {
188 Constant *cn = gvn->getInitializer();
189 if (ConstantArray *ca = dyn_cast<ConstantArray>(cn)) {
190 if (ca->isCString()) {
191 name = ".objc_class_name_" + ca->getAsString();
200 // Parse i386/ppc ObjC class data structure.
201 void LTOModule::addObjCClass(GlobalVariable *clgv) {
202 if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
203 // second slot in __OBJC,__class is pointer to superclass name
204 std::string superclassName;
205 if (objcClassNameFromExpression(c->getOperand(1), superclassName)) {
206 NameAndAttributes info;
207 StringMap<NameAndAttributes>::value_type &entry =
208 _undefines.GetOrCreateValue(superclassName.c_str());
209 if (!entry.getValue().name) {
210 const char *symbolName = entry.getKey().data();
211 info.name = symbolName;
212 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
213 entry.setValue(info);
216 // third slot in __OBJC,__class is pointer to class name
217 std::string className;
218 if (objcClassNameFromExpression(c->getOperand(2), className)) {
219 StringSet::value_type &entry =
220 _defines.GetOrCreateValue(className.c_str());
222 NameAndAttributes info;
223 info.name = entry.getKey().data();
224 info.attributes = (lto_symbol_attributes)
225 (LTO_SYMBOL_PERMISSIONS_DATA |
226 LTO_SYMBOL_DEFINITION_REGULAR |
227 LTO_SYMBOL_SCOPE_DEFAULT);
228 _symbols.push_back(info);
234 // Parse i386/ppc ObjC category data structure.
235 void LTOModule::addObjCCategory(GlobalVariable *clgv) {
236 if (ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer())) {
237 // second slot in __OBJC,__category is pointer to target class name
238 std::string targetclassName;
239 if (objcClassNameFromExpression(c->getOperand(1), targetclassName)) {
240 NameAndAttributes info;
242 StringMap<NameAndAttributes>::value_type &entry =
243 _undefines.GetOrCreateValue(targetclassName.c_str());
245 if (entry.getValue().name)
248 const char *symbolName = entry.getKey().data();
249 info.name = symbolName;
250 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
251 entry.setValue(info);
257 // Parse i386/ppc ObjC class list data structure.
258 void LTOModule::addObjCClassRef(GlobalVariable *clgv) {
259 std::string targetclassName;
260 if (objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) {
261 NameAndAttributes info;
263 StringMap<NameAndAttributes>::value_type &entry =
264 _undefines.GetOrCreateValue(targetclassName.c_str());
265 if (entry.getValue().name)
268 const char *symbolName = entry.getKey().data();
269 info.name = symbolName;
270 info.attributes = LTO_SYMBOL_DEFINITION_UNDEFINED;
271 entry.setValue(info);
276 void LTOModule::addDefinedDataSymbol(GlobalValue *v, Mangler &mangler) {
277 // Add to list of defined symbols.
278 addDefinedSymbol(v, mangler, false);
280 // Special case i386/ppc ObjC data structures in magic sections:
281 // The issue is that the old ObjC object format did some strange
282 // contortions to avoid real linker symbols. For instance, the
283 // ObjC class data structure is allocated statically in the executable
284 // that defines that class. That data structures contains a pointer to
285 // its superclass. But instead of just initializing that part of the
286 // struct to the address of its superclass, and letting the static and
287 // dynamic linkers do the rest, the runtime works by having that field
288 // instead point to a C-string that is the name of the superclass.
289 // At runtime the objc initialization updates that pointer and sets
290 // it to point to the actual super class. As far as the linker
291 // knows it is just a pointer to a string. But then someone wanted the
292 // linker to issue errors at build time if the superclass was not found.
293 // So they figured out a way in mach-o object format to use an absolute
294 // symbols (.objc_class_name_Foo = 0) and a floating reference
295 // (.reference .objc_class_name_Bar) to cause the linker into erroring when
296 // a class was missing.
297 // The following synthesizes the implicit .objc_* symbols for the linker
298 // from the ObjC data structures generated by the front end.
299 if (v->hasSection() /* && isTargetDarwin */) {
300 // special case if this data blob is an ObjC class definition
301 if (v->getSection().compare(0, 15, "__OBJC,__class,") == 0) {
302 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
307 // special case if this data blob is an ObjC category definition
308 else if (v->getSection().compare(0, 18, "__OBJC,__category,") == 0) {
309 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
314 // special case if this data blob is the list of referenced classes
315 else if (v->getSection().compare(0, 18, "__OBJC,__cls_refs,") == 0) {
316 if (GlobalVariable *gv = dyn_cast<GlobalVariable>(v)) {
324 void LTOModule::addDefinedSymbol(GlobalValue *def, Mangler &mangler,
326 // ignore all llvm.* symbols
327 if (def->getName().startswith("llvm."))
330 // string is owned by _defines
331 SmallString<64> Buffer;
332 mangler.getNameWithPrefix(Buffer, def, false);
334 // set alignment part log2() can have rounding errors
335 uint32_t align = def->getAlignment();
336 uint32_t attr = align ? CountTrailingZeros_32(def->getAlignment()) : 0;
338 // set permissions part
340 attr |= LTO_SYMBOL_PERMISSIONS_CODE;
342 GlobalVariable *gv = dyn_cast<GlobalVariable>(def);
343 if (gv && gv->isConstant())
344 attr |= LTO_SYMBOL_PERMISSIONS_RODATA;
346 attr |= LTO_SYMBOL_PERMISSIONS_DATA;
349 // set definition part
350 if (def->hasWeakLinkage() || def->hasLinkOnceLinkage() ||
351 def->hasLinkerPrivateWeakLinkage() ||
352 def->hasLinkerPrivateWeakDefAutoLinkage())
353 attr |= LTO_SYMBOL_DEFINITION_WEAK;
354 else if (def->hasCommonLinkage())
355 attr |= LTO_SYMBOL_DEFINITION_TENTATIVE;
357 attr |= LTO_SYMBOL_DEFINITION_REGULAR;
360 if (def->hasHiddenVisibility())
361 attr |= LTO_SYMBOL_SCOPE_HIDDEN;
362 else if (def->hasProtectedVisibility())
363 attr |= LTO_SYMBOL_SCOPE_PROTECTED;
364 else if (def->hasExternalLinkage() || def->hasWeakLinkage() ||
365 def->hasLinkOnceLinkage() || def->hasCommonLinkage() ||
366 def->hasLinkerPrivateWeakLinkage())
367 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
368 else if (def->hasLinkerPrivateWeakDefAutoLinkage())
369 attr |= LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN;
371 attr |= LTO_SYMBOL_SCOPE_INTERNAL;
373 // add to table of symbols
374 NameAndAttributes info;
375 StringSet::value_type &entry = _defines.GetOrCreateValue(Buffer.c_str());
378 StringRef Name = entry.getKey();
379 info.name = Name.data();
380 assert(info.name[Name.size()] == '\0');
381 info.attributes = (lto_symbol_attributes)attr;
382 _symbols.push_back(info);
385 void LTOModule::addAsmGlobalSymbol(const char *name,
386 lto_symbol_attributes scope) {
387 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
389 // only add new define if not already defined
390 if (entry.getValue())
394 const char *symbolName = entry.getKey().data();
395 uint32_t attr = LTO_SYMBOL_DEFINITION_REGULAR;
397 NameAndAttributes info;
398 info.name = symbolName;
399 info.attributes = (lto_symbol_attributes)attr;
400 _symbols.push_back(info);
403 void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
404 StringMap<NameAndAttributes>::value_type &entry =
405 _undefines.GetOrCreateValue(name);
407 _asm_undefines.push_back(entry.getKey().data());
409 // we already have the symbol
410 if (entry.getValue().name)
413 uint32_t attr = LTO_SYMBOL_DEFINITION_UNDEFINED;;
414 attr |= LTO_SYMBOL_SCOPE_DEFAULT;
415 NameAndAttributes info;
416 info.name = entry.getKey().data();
417 info.attributes = (lto_symbol_attributes)attr;
419 entry.setValue(info);
422 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.c_str());
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);
455 class RecordStreamer : public MCStreamer {
457 enum State { NeverSeen, Global, Defined, DefinedGlobal, Used};
460 StringMap<State> Symbols;
462 void markDefined(const MCSymbol &Symbol) {
463 State &S = Symbols[Symbol.getName()];
476 void markGlobal(const MCSymbol &Symbol) {
477 State &S = Symbols[Symbol.getName()];
491 void markUsed(const MCSymbol &Symbol) {
492 State &S = Symbols[Symbol.getName()];
506 // FIXME: mostly copied for the obj streamer.
507 void AddValueSymbols(const MCExpr *Value) {
508 switch (Value->getKind()) {
510 // FIXME: What should we do in here?
513 case MCExpr::Constant:
516 case MCExpr::Binary: {
517 const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
518 AddValueSymbols(BE->getLHS());
519 AddValueSymbols(BE->getRHS());
523 case MCExpr::SymbolRef:
524 markUsed(cast<MCSymbolRefExpr>(Value)->getSymbol());
528 AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
534 typedef StringMap<State>::const_iterator const_iterator;
536 const_iterator begin() {
537 return Symbols.begin();
540 const_iterator end() {
541 return Symbols.end();
544 RecordStreamer(MCContext &Context) : MCStreamer(Context) {}
546 virtual void ChangeSection(const MCSection *Section) {}
547 virtual void InitSections() {}
548 virtual void EmitLabel(MCSymbol *Symbol) {
549 Symbol->setSection(*getCurrentSection());
550 markDefined(*Symbol);
552 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {}
553 virtual void EmitThumbFunc(MCSymbol *Func) {}
554 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
555 // FIXME: should we handle aliases?
556 markDefined(*Symbol);
558 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) {
559 if (Attribute == MCSA_Global)
562 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
563 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
564 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {}
565 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {}
566 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
567 unsigned Size , unsigned ByteAlignment) {
568 markDefined(*Symbol);
570 virtual void EmitCOFFSymbolType(int Type) {}
571 virtual void EndCOFFSymbolDef() {}
572 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
573 unsigned ByteAlignment) {
574 markDefined(*Symbol);
576 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
577 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {}
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 bool isPCRel, unsigned AddrSpace) {}
583 virtual void EmitULEB128Value(const MCExpr *Value,
584 unsigned AddrSpace = 0) {}
585 virtual void EmitSLEB128Value(const MCExpr *Value,
586 unsigned AddrSpace = 0) {}
587 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value,
589 unsigned MaxBytesToEmit) {}
590 virtual void EmitCodeAlignment(unsigned ByteAlignment,
591 unsigned MaxBytesToEmit) {}
592 virtual void EmitValueToOffset(const MCExpr *Offset,
593 unsigned char Value ) {}
594 virtual void EmitFileDirective(StringRef Filename) {}
595 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta,
596 const MCSymbol *LastLabel,
597 const MCSymbol *Label) {}
599 virtual void EmitInstruction(const MCInst &Inst) {
601 for (unsigned i = Inst.getNumOperands(); i--; )
602 if (Inst.getOperand(i).isExpr())
603 AddValueSymbols(Inst.getOperand(i).getExpr());
605 virtual void Finish() {}
609 bool LTOModule::addAsmGlobalSymbols(MCContext &Context) {
610 const std::string &inlineAsm = _module->getModuleInlineAsm();
612 OwningPtr<RecordStreamer> Streamer(new RecordStreamer(Context));
613 MemoryBuffer *Buffer = MemoryBuffer::getMemBuffer(inlineAsm);
615 SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
616 OwningPtr<MCAsmParser> Parser(createMCAsmParser(_target->getTarget(), SrcMgr,
618 *_target->getMCAsmInfo()));
619 OwningPtr<TargetAsmParser>
620 TAP(_target->getTarget().createAsmParser(*Parser.get(), *_target.get()));
621 Parser->setTargetParser(*TAP);
622 int Res = Parser->Run(false);
626 for (RecordStreamer::const_iterator i = Streamer->begin(),
627 e = Streamer->end(); i != e; ++i) {
628 StringRef Key = i->first();
629 RecordStreamer::State Value = i->second;
630 if (Value == RecordStreamer::DefinedGlobal)
631 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_DEFAULT);
632 else if (Value == RecordStreamer::Defined)
633 addAsmGlobalSymbol(Key.data(), LTO_SYMBOL_SCOPE_INTERNAL);
634 else if (Value == RecordStreamer::Global ||
635 Value == RecordStreamer::Used)
636 addAsmGlobalSymbolUndef(Key.data());
641 bool LTOModule::ParseSymbols() {
642 // Use mangler to add GlobalPrefix to names to match linker names.
643 MCContext Context(*_target->getMCAsmInfo(), NULL);
644 Mangler mangler(Context, *_target->getTargetData());
647 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
648 if (f->isDeclaration() || f->hasAvailableExternallyLinkage())
649 addPotentialUndefinedSymbol(f, mangler);
651 addDefinedFunctionSymbol(f, mangler);
655 for (Module::global_iterator v = _module->global_begin(),
656 e = _module->global_end(); v != e; ++v) {
657 if (v->isDeclaration() || v->hasAvailableExternallyLinkage())
658 addPotentialUndefinedSymbol(v, mangler);
660 addDefinedDataSymbol(v, mangler);
664 if (addAsmGlobalSymbols(Context))
668 for (Module::alias_iterator i = _module->alias_begin(),
669 e = _module->alias_end(); i != e; ++i) {
670 if (i->isDeclaration())
671 addPotentialUndefinedSymbol(i, mangler);
673 addDefinedDataSymbol(i, mangler);
676 // make symbols for all undefines
677 for (StringMap<NameAndAttributes>::iterator it=_undefines.begin();
678 it != _undefines.end(); ++it) {
679 // if this symbol also has a definition, then don't make an undefine
680 // because it is a tentative definition
681 if (_defines.count(it->getKey()) == 0) {
682 NameAndAttributes info = it->getValue();
683 _symbols.push_back(info);
690 uint32_t LTOModule::getSymbolCount() {
691 return _symbols.size();
695 lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) {
696 if (index < _symbols.size())
697 return _symbols[index].attributes;
699 return lto_symbol_attributes(0);
702 const char *LTOModule::getSymbolName(uint32_t index) {
703 if (index < _symbols.size())
704 return _symbols[index].name;