LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LTOCodeGenerator.h
Go to the documentation of this file.
1 //===-LTOCodeGenerator.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 LTOCodeGenerator class.
11 //
12 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
13 //
14 // The Pre-IPO phase compiles source code into bitcode file. The resulting
15 // bitcode files, along with object files and libraries, will be fed to the
16 // linker to through the IPO and Post-IPO phases. By using obj-file extension,
17 // the resulting bitcode file disguises itself as an object file, and therefore
18 // obviates the need of writing a special set of the make-rules only for LTO
19 // compilation.
20 //
21 // The IPO phase perform inter-procedural analyses and optimizations, and
22 // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations
23 // (SOPT), and intra-procedural target-dependent code generator (CG).
24 //
25 // As of this writing, we don't separate IPO and the Post-IPO SOPT. They
26 // are intermingled together, and are driven by a single pass manager (see
27 // PassManagerBuilder::populateLTOPassManager()).
28 //
29 // The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages.
30 // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator"
31 // with the machine specific code generator.
32 //
33 //===----------------------------------------------------------------------===//
34 
35 #ifndef LTO_CODE_GENERATOR_H
36 #define LTO_CODE_GENERATOR_H
37 
38 #include "llvm-c/lto.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/StringMap.h"
41 #include "llvm/ADT/ArrayRef.h"
42 #include "llvm/Linker.h"
44 #include <string>
45 #include <vector>
46 
47 namespace llvm {
48  class LLVMContext;
49  class GlobalValue;
50  class Mangler;
51  class MemoryBuffer;
52  class TargetLibraryInfo;
53  class TargetMachine;
54  class raw_ostream;
55 }
56 
57 //===----------------------------------------------------------------------===//
58 /// LTOCodeGenerator - C++ class which implements the opaque lto_code_gen_t
59 /// type.
60 ///
62  static const char *getVersionString();
63 
66 
67  // Merge given module, return true on success.
68  bool addModule(struct LTOModule*, std::string &errMsg);
69 
73 
74  void setCpu(const char *mCpu) { MCpu = mCpu; }
75 
76  void addMustPreserveSymbol(const char *sym) { MustPreserveSymbols[sym] = 1; }
77 
78  // To pass options to the driver and optimization passes. These options are
79  // not necessarily for debugging purpose (The function name is misleading).
80  // This function should be called before LTOCodeGenerator::compilexxx(),
81  // and LTOCodeGenerator::writeMergedModules().
82  //
83  void setCodeGenDebugOptions(const char *opts);
84 
85  // Parse the options set in setCodeGenDebugOptions. Like
86  // setCodeGenDebugOptions, this must be called before
87  // LTOCodeGenerator::compilexxx() and LTOCodeGenerator::writeMergedModules()
89 
90  // Write the merged module to the file specified by the given path.
91  // Return true on success.
92  bool writeMergedModules(const char *path, std::string &errMsg);
93 
94  // Compile the merged module into a *single* object file; the path to object
95  // file is returned to the caller via argument "name". Return true on
96  // success.
97  //
98  // NOTE that it is up to the linker to remove the intermediate object file.
99  // Do not try to remove the object file in LTOCodeGenerator's destructor
100  // as we don't who (LTOCodeGenerator or the obj file) will last longer.
101  //
102  bool compile_to_file(const char **name,
103  bool disableOpt,
104  bool disableInline,
105  bool disableGVNLoadPRE,
106  std::string &errMsg);
107 
108  // As with compile_to_file(), this function compiles the merged module into
109  // single object file. Instead of returning the object-file-path to the caller
110  // (linker), it brings the object to a buffer, and return the buffer to the
111  // caller. This function should delete intermediate object file once its content
112  // is brought to memory. Return NULL if the compilation was not successful.
113  //
114  const void *compile(size_t *length,
115  bool disableOpt,
116  bool disableInline,
117  bool disableGVNLoadPRE,
118  std::string &errMsg);
119 
120 private:
121  void initializeLTOPasses();
122 
123  bool generateObjectFile(llvm::raw_ostream &out,
124  bool disableOpt,
125  bool disableInline,
126  bool disableGVNLoadPRE,
127  std::string &errMsg);
128  void applyScopeRestrictions();
129  void applyRestriction(llvm::GlobalValue &GV,
130  const llvm::ArrayRef<llvm::StringRef> &Libcalls,
131  std::vector<const char*> &MustPreserveList,
133  llvm::Mangler &Mangler);
134  bool determineTarget(std::string &errMsg);
135 
136  typedef llvm::StringMap<uint8_t> StringSet;
137 
138  llvm::LLVMContext &Context;
139  llvm::Linker Linker;
140  llvm::TargetMachine *TargetMach;
141  bool EmitDwarfDebugInfo;
142  bool ScopeRestrictionsDone;
143  lto_codegen_model CodeModel;
144  StringSet MustPreserveSymbols;
145  StringSet AsmUndefinedRefs;
146  llvm::MemoryBuffer *NativeObjectFile;
147  std::vector<char *> CodegenOptions;
148  std::string MCpu;
149  std::string NativeObjectPath;
150  llvm::TargetOptions Options;
151 };
152 
153 #endif // LTO_CODE_GENERATOR_H
void setTargetOptions(llvm::TargetOptions options)
peephole opts
bool writeMergedModules(const char *path, std::string &errMsg)
void addMustPreserveSymbol(const char *sym)
void setCodeGenDebugOptions(const char *opts)
lto_debug_model
Definition: lto.h:65
void setDebugInfo(lto_debug_model)
bool compile_to_file(const char **name, bool disableOpt, bool disableInline, bool disableGVNLoadPRE, std::string &errMsg)
const void * compile(size_t *length, bool disableOpt, bool disableInline, bool disableGVNLoadPRE, std::string &errMsg)
void setCpu(const char *mCpu)
lto_codegen_model
Definition: lto.h:70
void setCodePICModel(lto_codegen_model)
static const char * getVersionString()
bool addModule(struct LTOModule *, std::string &errMsg)