Home | History | Annotate | Line # | Download | only in legacy
      1 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 //
      9 // This file declares the LTOCodeGenerator class.
     10 //
     11 //   LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
     12 //
     13 //   The Pre-IPO phase compiles source code into bitcode file. The resulting
     14 // bitcode files, along with object files and libraries, will be fed to the
     15 // linker to through the IPO and Post-IPO phases. By using obj-file extension,
     16 // the resulting bitcode file disguises itself as an object file, and therefore
     17 // obviates the need of writing a special set of the make-rules only for LTO
     18 // compilation.
     19 //
     20 //   The IPO phase perform inter-procedural analyses and optimizations, and
     21 // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations
     22 // (SOPT), and intra-procedural target-dependent code generator (CG).
     23 //
     24 //   As of this writing, we don't separate IPO and the Post-IPO SOPT. They
     25 // are intermingled together, and are driven by a single pass manager (see
     26 // PassManagerBuilder::populateLTOPassManager()).
     27 //
     28 //   The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages.
     29 // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator"
     30 // with the machine specific code generator.
     31 //
     32 //===----------------------------------------------------------------------===//
     33 
     34 #ifndef LLVM_LTO_LEGACY_LTOCODEGENERATOR_H
     35 #define LLVM_LTO_LEGACY_LTOCODEGENERATOR_H
     36 
     37 #include "llvm-c/lto.h"
     38 #include "llvm/ADT/ArrayRef.h"
     39 #include "llvm/ADT/SmallPtrSet.h"
     40 #include "llvm/ADT/StringMap.h"
     41 #include "llvm/ADT/StringSet.h"
     42 #include "llvm/IR/GlobalValue.h"
     43 #include "llvm/IR/Module.h"
     44 #include "llvm/LTO/Config.h"
     45 #include "llvm/LTO/LTO.h"
     46 #include "llvm/Support/CommandLine.h"
     47 #include "llvm/Support/Error.h"
     48 #include "llvm/Support/ToolOutputFile.h"
     49 #include "llvm/Target/TargetMachine.h"
     50 #include "llvm/Target/TargetOptions.h"
     51 #include <string>
     52 #include <vector>
     53 
     54 /// Enable global value internalization in LTO.
     55 extern llvm::cl::opt<bool> EnableLTOInternalization;
     56 
     57 namespace llvm {
     58 template <typename T> class ArrayRef;
     59   class LLVMContext;
     60   class DiagnosticInfo;
     61   class Linker;
     62   class Mangler;
     63   class MemoryBuffer;
     64   class TargetLibraryInfo;
     65   class TargetMachine;
     66   class raw_ostream;
     67   class raw_pwrite_stream;
     68 
     69 //===----------------------------------------------------------------------===//
     70 /// C++ class which implements the opaque lto_code_gen_t type.
     71 ///
     72 struct LTOCodeGenerator {
     73   static const char *getVersionString();
     74 
     75   LTOCodeGenerator(LLVMContext &Context);
     76   ~LTOCodeGenerator();
     77 
     78   /// Merge given module.  Return true on success.
     79   ///
     80   /// Resets \a HasVerifiedInput.
     81   bool addModule(struct LTOModule *);
     82 
     83   /// Set the destination module.
     84   ///
     85   /// Resets \a HasVerifiedInput.
     86   void setModule(std::unique_ptr<LTOModule> M);
     87 
     88   void setAsmUndefinedRefs(struct LTOModule *);
     89   void setTargetOptions(const TargetOptions &Options);
     90   void setDebugInfo(lto_debug_model);
     91   void setCodePICModel(Optional<Reloc::Model> Model) {
     92     Config.RelocModel = Model;
     93   }
     94 
     95   /// Set the file type to be emitted (assembly or object code).
     96   /// The default is CGFT_ObjectFile.
     97   void setFileType(CodeGenFileType FT) { Config.CGFileType = FT; }
     98 
     99   void setCpu(StringRef MCpu) { Config.CPU = std::string(MCpu); }
    100   void setAttrs(std::vector<std::string> MAttrs) { Config.MAttrs = MAttrs; }
    101   void setOptLevel(unsigned OptLevel);
    102 
    103   void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
    104   void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
    105 
    106   /// Restore linkage of globals
    107   ///
    108   /// When set, the linkage of globals will be restored prior to code
    109   /// generation. That is, a global symbol that had external linkage prior to
    110   /// LTO will be emitted with external linkage again; and a local will remain
    111   /// local. Note that this option only affects the end result - globals may
    112   /// still be internalized in the process of LTO and may be modified and/or
    113   /// deleted where legal.
    114   ///
    115   /// The default behavior will internalize globals (unless on the preserve
    116   /// list) and, if parallel code generation is enabled, will externalize
    117   /// all locals.
    118   void setShouldRestoreGlobalsLinkage(bool Value) {
    119     ShouldRestoreGlobalsLinkage = Value;
    120   }
    121 
    122   void addMustPreserveSymbol(StringRef Sym) { MustPreserveSymbols.insert(Sym); }
    123 
    124   /// Pass options to the driver and optimization passes.
    125   ///
    126   /// These options are not necessarily for debugging purpose (the function
    127   /// name is misleading).  This function should be called before
    128   /// LTOCodeGenerator::compilexxx(), and
    129   /// LTOCodeGenerator::writeMergedModules().
    130   void setCodeGenDebugOptions(ArrayRef<StringRef> Opts);
    131 
    132   /// Parse the options set in setCodeGenDebugOptions.
    133   ///
    134   /// Like \a setCodeGenDebugOptions(), this must be called before
    135   /// LTOCodeGenerator::compilexxx() and
    136   /// LTOCodeGenerator::writeMergedModules().
    137   void parseCodeGenDebugOptions();
    138 
    139   /// Write the merged module to the file specified by the given path.  Return
    140   /// true on success.
    141   ///
    142   /// Calls \a verifyMergedModuleOnce().
    143   bool writeMergedModules(StringRef Path);
    144 
    145   /// Compile the merged module into a *single* output file; the path to output
    146   /// file is returned to the caller via argument "name". Return true on
    147   /// success.
    148   ///
    149   /// \note It is up to the linker to remove the intermediate output file.  Do
    150   /// not try to remove the object file in LTOCodeGenerator's destructor as we
    151   /// don't who (LTOCodeGenerator or the output file) will last longer.
    152   bool compile_to_file(const char **Name);
    153 
    154   /// As with compile_to_file(), this function compiles the merged module into
    155   /// single output file. Instead of returning the output file path to the
    156   /// caller (linker), it brings the output to a buffer, and returns the buffer
    157   /// to the caller. This function should delete the intermediate file once
    158   /// its content is brought to memory. Return NULL if the compilation was not
    159   /// successful.
    160   std::unique_ptr<MemoryBuffer> compile();
    161 
    162   /// Optimizes the merged module.  Returns true on success.
    163   ///
    164   /// Calls \a verifyMergedModuleOnce().
    165   bool optimize();
    166 
    167   /// Compiles the merged optimized module into a single output file. It brings
    168   /// the output to a buffer, and returns the buffer to the caller. Return NULL
    169   /// if the compilation was not successful.
    170   std::unique_ptr<MemoryBuffer> compileOptimized();
    171 
    172   /// Compile the merged optimized module \p ParallelismLevel output files each
    173   /// representing a linkable partition of the module. If out contains more
    174   /// than one element, code generation is done in parallel with \p
    175   /// ParallelismLevel threads.  Output files will be written to the streams
    176   /// created using the \p AddStream callback. Returns true on success.
    177   ///
    178   /// Calls \a verifyMergedModuleOnce().
    179   bool compileOptimized(lto::AddStreamFn AddStream, unsigned ParallelismLevel);
    180 
    181   /// Enable the Freestanding mode: indicate that the optimizer should not
    182   /// assume builtins are present on the target.
    183   void setFreestanding(bool Enabled) { Config.Freestanding = Enabled; }
    184 
    185   void setDisableVerify(bool Value) { Config.DisableVerify = Value; }
    186 
    187   void setUseNewPM(bool Value) { Config.UseNewPM = Value; }
    188 
    189   void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
    190 
    191   LLVMContext &getContext() { return Context; }
    192 
    193   void resetMergedModule() { MergedModule.reset(); }
    194   void DiagnosticHandler(const DiagnosticInfo &DI);
    195 
    196 private:
    197   /// Verify the merged module on first call.
    198   ///
    199   /// Sets \a HasVerifiedInput on first call and doesn't run again on the same
    200   /// input.
    201   void verifyMergedModuleOnce();
    202 
    203   bool compileOptimizedToFile(const char **Name);
    204   void restoreLinkageForExternals();
    205   void applyScopeRestrictions();
    206   void preserveDiscardableGVs(
    207       Module &TheModule,
    208       llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV);
    209 
    210   bool determineTarget();
    211   std::unique_ptr<TargetMachine> createTargetMachine();
    212 
    213   void emitError(const std::string &ErrMsg);
    214   void emitWarning(const std::string &ErrMsg);
    215 
    216   void finishOptimizationRemarks();
    217 
    218   LLVMContext &Context;
    219   std::unique_ptr<Module> MergedModule;
    220   std::unique_ptr<Linker> TheLinker;
    221   std::unique_ptr<TargetMachine> TargetMach;
    222   bool EmitDwarfDebugInfo = false;
    223   bool ScopeRestrictionsDone = false;
    224   bool HasVerifiedInput = false;
    225   StringSet<> MustPreserveSymbols;
    226   StringSet<> AsmUndefinedRefs;
    227   StringMap<GlobalValue::LinkageTypes> ExternalSymbols;
    228   std::vector<std::string> CodegenOptions;
    229   std::string FeatureStr;
    230   std::string NativeObjectPath;
    231   const Target *MArch = nullptr;
    232   std::string TripleStr;
    233   lto_diagnostic_handler_t DiagHandler = nullptr;
    234   void *DiagContext = nullptr;
    235   bool ShouldInternalize = EnableLTOInternalization;
    236   bool ShouldEmbedUselists = false;
    237   bool ShouldRestoreGlobalsLinkage = false;
    238   std::unique_ptr<ToolOutputFile> DiagnosticOutputFile;
    239   std::unique_ptr<ToolOutputFile> StatsFile = nullptr;
    240 
    241   lto::Config Config;
    242 };
    243 
    244 /// A convenience function that calls cl::ParseCommandLineOptions on the given
    245 /// set of options.
    246 void parseCommandLineOptions(std::vector<std::string> &Options);
    247 }
    248 #endif
    249