blob: 4019e015a5d88b50da808c3a80ddd4b03e8b6f9a [file] [log] [blame]
Nick Kledzik6d886992008-02-26 20:26:43 +00001//===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares the LTOModule class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LTO_MODULE_H
15#define LTO_MODULE_H
16
17#include "llvm/Module.h"
Nick Kledzik79d0a052008-02-27 22:25:36 +000018#include "llvm/ADT/OwningPtr.h"
Nick Kledzik6d886992008-02-26 20:26:43 +000019#include "llvm/Target/TargetMachine.h"
20#include "llvm/ADT/StringMap.h"
21
22#include "llvm-c/lto.h"
23
24#include <vector>
Nick Kledzik79d0a052008-02-27 22:25:36 +000025#include <string>
26
27
28// forward references to llvm classes
29namespace llvm {
30 class Mangler;
31 class MemoryBuffer;
32 class GlobalValue;
33 class Value;
34 class Function;
35}
Nick Kledzik6d886992008-02-26 20:26:43 +000036
37
38//
39// C++ class which implements the opaque lto_module_t
40//
41class LTOModule {
42public:
43
44 static bool isBitcodeFile(const void* mem, size_t length);
45 static bool isBitcodeFile(const char* path);
46
47 static bool isBitcodeFileForTarget(const void* mem,
48 size_t length, const char* triplePrefix);
49
50 static bool isBitcodeFileForTarget(const char* path,
51 const char* triplePrefix);
52
Owen Anderson25209b42009-07-01 16:58:40 +000053 static LTOModule* makeLTOModule(const char* path,
Owen Anderson25209b42009-07-01 16:58:40 +000054 std::string& errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +000055 static LTOModule* makeLTOModule(const void* mem, size_t length,
Owen Anderson25209b42009-07-01 16:58:40 +000056 std::string& errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +000057
58 const char* getTargetTriple();
59 uint32_t getSymbolCount();
60 lto_symbol_attributes getSymbolAttributes(uint32_t index);
61 const char* getSymbolName(uint32_t index);
62
Nick Kledzik79d0a052008-02-27 22:25:36 +000063 llvm::Module * getLLVVMModule() { return _module.get(); }
Nick Kledzik6d886992008-02-26 20:26:43 +000064
65private:
66 LTOModule(llvm::Module* m, llvm::TargetMachine* t);
67
Nick Kledzik79d0a052008-02-27 22:25:36 +000068 void lazyParseSymbols();
Nick Kledzik6d886992008-02-26 20:26:43 +000069 void addDefinedSymbol(llvm::GlobalValue* def,
70 llvm::Mangler& mangler,
71 bool isFunction);
Nick Kledzik79d0a052008-02-27 22:25:36 +000072 void addPotentialUndefinedSymbol(llvm::GlobalValue* decl,
73 llvm::Mangler &mangler);
Nick Kledzik6d886992008-02-26 20:26:43 +000074 void findExternalRefs(llvm::Value* value,
75 llvm::Mangler& mangler);
Nick Kledzik79d0a052008-02-27 22:25:36 +000076 void addDefinedFunctionSymbol(llvm::Function* f,
77 llvm::Mangler &mangler);
78 void addDefinedDataSymbol(llvm::GlobalValue* v,
79 llvm::Mangler &mangler);
Devang Patelb96e0372008-07-16 18:06:52 +000080 void addAsmGlobalSymbol(const char *);
Nick Kledzikb4c75512009-06-01 20:33:09 +000081 void addObjCClass(llvm::GlobalVariable* clgv);
82 void addObjCCategory(llvm::GlobalVariable* clgv);
83 void addObjCClassRef(llvm::GlobalVariable* clgv);
84 bool objcClassNameFromExpression(llvm::Constant* c,
85 std::string& name);
86
Owen Anderson25209b42009-07-01 16:58:40 +000087 static bool isTargetMatch(llvm::MemoryBuffer* memBuffer,
Nick Kledzik79d0a052008-02-27 22:25:36 +000088 const char* triplePrefix);
Nick Kledzikb4c75512009-06-01 20:33:09 +000089
Owen Anderson25209b42009-07-01 16:58:40 +000090 static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer,
Nick Kledzik79d0a052008-02-27 22:25:36 +000091 std::string& errMsg);
Evan Chengce914302009-05-30 00:48:34 +000092 static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
Nick Kledzikb4c75512009-06-01 20:33:09 +000093
Nick Kledzik6d886992008-02-26 20:26:43 +000094 typedef llvm::StringMap<uint8_t> StringSet;
95
96 struct NameAndAttributes {
97 const char* name;
98 lto_symbol_attributes attributes;
99 };
100
Nick Kledzik79d0a052008-02-27 22:25:36 +0000101 llvm::OwningPtr<llvm::Module> _module;
102 llvm::OwningPtr<llvm::TargetMachine> _target;
103 bool _symbolsParsed;
104 std::vector<NameAndAttributes> _symbols;
105 // _defines and _undefines only needed to disambiguate tentative definitions
106 StringSet _defines;
Rafael Espindolab4d596f2009-04-24 16:55:21 +0000107 llvm::StringMap<NameAndAttributes> _undefines;
Nick Kledzik6d886992008-02-26 20:26:43 +0000108};
109
Nick Kledzik6d886992008-02-26 20:26:43 +0000110#endif // LTO_MODULE_H
111