2006-08-03 15:44:57 +00:00
|
|
|
//===-- llvm/LinkTimeOptimizer.h - Public Interface ------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Devang Patel and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This header provides public interface to use LLVM link time optimization
|
|
|
|
// library. This is intended to be used by linker to do link time optimization.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef __LTO_H__
|
|
|
|
#define __LTO_H__
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <set>
|
|
|
|
#include <llvm/ADT/hash_map>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
class Module;
|
|
|
|
class GlobalValue;
|
2006-10-24 18:41:02 +00:00
|
|
|
class TargetMachine;
|
2006-08-03 15:44:57 +00:00
|
|
|
|
|
|
|
enum LTOStatus {
|
|
|
|
LTO_UNKNOWN,
|
|
|
|
LTO_OPT_SUCCESS,
|
|
|
|
LTO_READ_SUCCESS,
|
|
|
|
LTO_READ_FAILURE,
|
|
|
|
LTO_WRITE_FAILURE,
|
|
|
|
LTO_NO_TARGET,
|
|
|
|
LTO_NO_WORK,
|
|
|
|
LTO_MODULE_MERGE_FAILURE,
|
|
|
|
LTO_ASM_FAILURE
|
|
|
|
};
|
|
|
|
|
|
|
|
enum LTOLinkageTypes {
|
|
|
|
LTOExternalLinkage, // Externally visible function
|
|
|
|
LTOLinkOnceLinkage, // Keep one copy of named function when linking (inline)
|
|
|
|
LTOWeakLinkage, // Keep one copy of named function when linking (weak)
|
|
|
|
LTOInternalLinkage // Rename collisions when linking (static functions)
|
|
|
|
};
|
|
|
|
|
2006-08-03 16:34:35 +00:00
|
|
|
/// This class represents LLVM symbol information without exposing details
|
2006-08-03 15:44:57 +00:00
|
|
|
/// of LLVM global values. It encapsulates symbol linkage information. This
|
|
|
|
/// is typically used in hash_map where associated name identifies the
|
|
|
|
/// the symbol name.
|
|
|
|
class LLVMSymbol {
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
LTOLinkageTypes getLinkage() const { return linkage; }
|
|
|
|
void mayBeNotUsed();
|
|
|
|
|
2006-08-14 23:27:36 +00:00
|
|
|
LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, const std::string &n,
|
2006-10-25 18:10:07 +00:00
|
|
|
const std::string &m, int a) : linkage(lt), gv(g), name(n),
|
|
|
|
mangledName(m), alignment(a) {}
|
2006-08-14 22:36:16 +00:00
|
|
|
|
|
|
|
const char *getName() { return name.c_str(); }
|
|
|
|
const char *getMangledName() { return mangledName.c_str(); }
|
2006-10-25 18:10:07 +00:00
|
|
|
int getAlignment() { return alignment; }
|
2006-08-03 15:44:57 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum LTOLinkageTypes linkage;
|
|
|
|
GlobalValue *gv;
|
2006-08-14 22:36:16 +00:00
|
|
|
std::string name;
|
|
|
|
std::string mangledName;
|
2006-10-25 18:10:07 +00:00
|
|
|
int alignment;
|
2006-08-03 15:44:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class string_compare {
|
|
|
|
public:
|
|
|
|
bool operator()(const char* left, const char* right) const {
|
|
|
|
return (strcmp(left, right) == 0);
|
|
|
|
}
|
|
|
|
};
|
2006-09-21 17:22:55 +00:00
|
|
|
|
|
|
|
/// This is abstract class to facilitate dlopen() interface.
|
|
|
|
/// See LTO below for more info.
|
|
|
|
class LinkTimeOptimizer {
|
|
|
|
public:
|
|
|
|
typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
|
|
|
|
string_compare> NameToSymbolMap;
|
|
|
|
typedef hash_map<const char*, Module*, hash<const char*>,
|
|
|
|
string_compare> NameToModuleMap;
|
|
|
|
virtual enum LTOStatus readLLVMObjectFile(const std::string &,
|
|
|
|
NameToSymbolMap &,
|
|
|
|
std::set<std::string> &) = 0;
|
|
|
|
virtual enum LTOStatus optimizeModules(const std::string &,
|
2006-10-26 20:46:22 +00:00
|
|
|
std::vector<const char*> &,
|
|
|
|
std::string &, bool,
|
|
|
|
const char *) = 0;
|
2006-09-21 17:22:55 +00:00
|
|
|
virtual void getTargetTriple(const std::string &, std::string &) = 0;
|
2006-10-23 23:12:26 +00:00
|
|
|
virtual void removeModule (const std::string &InputFilename) = 0;
|
2007-02-01 01:46:06 +00:00
|
|
|
virtual void printVersion () = 0;
|
2006-09-21 17:22:55 +00:00
|
|
|
virtual ~LinkTimeOptimizer() = 0;
|
|
|
|
};
|
|
|
|
|
2006-08-03 15:44:57 +00:00
|
|
|
/// This is the main link time optimization class. It exposes simple API
|
|
|
|
/// to perform link time optimization using LLVM intermodular optimizer.
|
2006-09-21 17:22:55 +00:00
|
|
|
class LTO : public LinkTimeOptimizer {
|
2006-08-03 15:44:57 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
|
2006-09-06 00:28:22 +00:00
|
|
|
string_compare> NameToSymbolMap;
|
2006-09-06 18:50:26 +00:00
|
|
|
typedef hash_map<const char*, Module*, hash<const char*>,
|
|
|
|
string_compare> NameToModuleMap;
|
2006-08-03 15:44:57 +00:00
|
|
|
|
|
|
|
enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
|
2006-09-06 00:28:22 +00:00
|
|
|
NameToSymbolMap &symbols,
|
|
|
|
std::set<std::string> &references);
|
2006-08-03 15:44:57 +00:00
|
|
|
enum LTOStatus optimizeModules(const std::string &OutputFilename,
|
2006-09-06 00:28:22 +00:00
|
|
|
std::vector<const char*> &exportList,
|
2006-10-26 20:46:22 +00:00
|
|
|
std::string &targetTriple, bool saveTemps,
|
|
|
|
const char *);
|
|
|
|
void getTargetTriple(const std::string &InputFilename,
|
|
|
|
std::string &targetTriple);
|
2006-10-23 23:12:26 +00:00
|
|
|
void removeModule (const std::string &InputFilename);
|
2007-02-01 01:46:06 +00:00
|
|
|
void printVersion();
|
2006-08-03 15:44:57 +00:00
|
|
|
|
2006-10-24 18:41:02 +00:00
|
|
|
// Constructors and destructors
|
|
|
|
LTO() {
|
|
|
|
/// TODO: Use Target info, it is available at this time.
|
|
|
|
Target = NULL;
|
|
|
|
}
|
|
|
|
~LTO();
|
|
|
|
|
2006-09-06 18:50:26 +00:00
|
|
|
private:
|
|
|
|
Module *getModule (const std::string &InputFilename);
|
2006-10-24 18:41:02 +00:00
|
|
|
enum LTOStatus optimize(Module *, std::ostream &,
|
|
|
|
std::vector<const char *> &);
|
|
|
|
void getTarget(Module *);
|
2006-09-06 18:50:26 +00:00
|
|
|
|
2006-08-03 15:44:57 +00:00
|
|
|
private:
|
|
|
|
std::vector<Module *> modules;
|
|
|
|
NameToSymbolMap allSymbols;
|
2006-09-06 18:50:26 +00:00
|
|
|
NameToModuleMap allModules;
|
2006-10-24 18:41:02 +00:00
|
|
|
TargetMachine *Target;
|
2006-08-03 15:44:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
/// This provides C interface to initialize link time optimizer. This allows
|
|
|
|
/// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
|
|
|
|
/// extern "C" helps, because dlopen() interface uses name to find the symbol.
|
|
|
|
extern "C"
|
|
|
|
llvm::LinkTimeOptimizer *createLLVMOptimizer();
|
|
|
|
|
|
|
|
#endif
|