blob: 0cb9cdc8b0cff78fbc00b273459210b1f765bc13 [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
53 static LTOModule* makeLTOModule(const char* path, std::string& errMsg);
54 static LTOModule* makeLTOModule(const void* mem, size_t length,
55 std::string& errMsg);
Nick Kledzik6d886992008-02-26 20:26:43 +000056
57 const char* getTargetTriple();
58 uint32_t getSymbolCount();
59 lto_symbol_attributes getSymbolAttributes(uint32_t index);
60 const char* getSymbolName(uint32_t index);
61
Nick Kledzik79d0a052008-02-27 22:25:36 +000062 llvm::Module * getLLVVMModule() { return _module.get(); }
Nick Kledzik6d886992008-02-26 20:26:43 +000063
64private:
65 LTOModule(llvm::Module* m, llvm::TargetMachine* t);
66
Nick Kledzik79d0a052008-02-27 22:25:36 +000067 void lazyParseSymbols();
Nick Kledzik6d886992008-02-26 20:26:43 +000068 void addDefinedSymbol(llvm::GlobalValue* def,
69 llvm::Mangler& mangler,
70 bool isFunction);
Nick Kledzik79d0a052008-02-27 22:25:36 +000071 void addPotentialUndefinedSymbol(llvm::GlobalValue* decl,
72 llvm::Mangler &mangler);
Nick Kledzik6d886992008-02-26 20:26:43 +000073 void findExternalRefs(llvm::Value* value,
74 llvm::Mangler& mangler);
Nick Kledzik79d0a052008-02-27 22:25:36 +000075 void addDefinedFunctionSymbol(llvm::Function* f,
76 llvm::Mangler &mangler);
77 void addDefinedDataSymbol(llvm::GlobalValue* v,
78 llvm::Mangler &mangler);
Devang Patelb96e0372008-07-16 18:06:52 +000079 void addAsmGlobalSymbol(const char *);
Nick Kledzik79d0a052008-02-27 22:25:36 +000080 static bool isTargetMatch(llvm::MemoryBuffer* memBuffer,
81 const char* triplePrefix);
Nick Kledzik6d886992008-02-26 20:26:43 +000082
Nick Kledzik79d0a052008-02-27 22:25:36 +000083 static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer,
84 std::string& errMsg);
Nick Kledzike31b6102008-05-09 01:09:59 +000085 static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
Nick Kledzik79d0a052008-02-27 22:25:36 +000086
Nick Kledzik6d886992008-02-26 20:26:43 +000087 typedef llvm::StringMap<uint8_t> StringSet;
88
89 struct NameAndAttributes {
90 const char* name;
91 lto_symbol_attributes attributes;
92 };
93
Nick Kledzik79d0a052008-02-27 22:25:36 +000094 llvm::OwningPtr<llvm::Module> _module;
95 llvm::OwningPtr<llvm::TargetMachine> _target;
96 bool _symbolsParsed;
97 std::vector<NameAndAttributes> _symbols;
98 // _defines and _undefines only needed to disambiguate tentative definitions
99 StringSet _defines;
100 StringSet _undefines;
Nick Kledzik6d886992008-02-26 20:26:43 +0000101};
102
Bill Wendling0478d1a2008-06-18 21:39:02 +0000103extern std::string getFeatureString(const char *TargetTriple);
104
Nick Kledzik6d886992008-02-26 20:26:43 +0000105#endif // LTO_MODULE_H
106