1 //===-- CodeGenTargetMachineImpl.h ------------------------------*- C++ -*-===// 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 /// \file This file describes the CodeGenTargetMachineImpl class, which 10 /// implements a set of functionality used by \c TargetMachine classes in 11 /// LLVM that make use of the target-independent code generator. 12 //===----------------------------------------------------------------------===// 13 #ifndef LLVM_CODEGEN_CODEGENTARGETMACHINEIMPL_H 14 #define LLVM_CODEGEN_CODEGENTARGETMACHINEIMPL_H 15 #include "llvm/Support/Compiler.h" 16 #include "llvm/Target/TargetMachine.h" 17 18 namespace llvm { 19 20 /// \brief implements a set of functionality in the \c TargetMachine class 21 /// for targets that make use of the independent code generator (CodeGen) 22 /// library. Must not be used directly in code unless to inherit its 23 /// implementation. 24 class LLVM_ABI CodeGenTargetMachineImpl : public TargetMachine { 25 protected: // Can only create subclasses. 26 CodeGenTargetMachineImpl(const Target &T, StringRef DataLayoutString, 27 const Triple &TT, StringRef CPU, StringRef FS, 28 const TargetOptions &Options, Reloc::Model RM, 29 CodeModel::Model CM, CodeGenOptLevel OL); 30 31 void initAsmInfo(); 32 33 /// Reset internal state. 34 virtual void reset() {}; 35 36 public: 37 /// Get a TargetTransformInfo implementation for the target. 38 /// 39 /// The TTI returned uses the common code generator to answer queries about 40 /// the IR. 41 TargetTransformInfo getTargetTransformInfo(const Function &F) const override; 42 43 /// Create a pass configuration object to be used by addPassToEmitX methods 44 /// for generating a pipeline of CodeGen passes. 45 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM) override; 46 47 /// Add passes to the specified pass manager to get the specified file 48 /// emitted. Typically this will involve several steps of code generation. 49 /// \p MMIWP is an optional parameter that, if set to non-nullptr, 50 /// will be used to set the MachineModuloInfo for this PM. 51 bool 52 addPassesToEmitFile(PassManagerBase &PM, raw_pwrite_stream &Out, 53 raw_pwrite_stream *DwoOut, CodeGenFileType FileType, 54 bool DisableVerify = true, 55 MachineModuleInfoWrapperPass *MMIWP = nullptr) override; 56 57 /// Add passes to the specified pass manager to get machine code emitted with 58 /// the MCJIT. This method returns true if machine code is not supported. It 59 /// fills the MCContext Ctx pointer which can be used to build custom 60 /// MCStreamer. 61 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, 62 raw_pwrite_stream &Out, 63 bool DisableVerify = true) override; 64 65 /// Adds an AsmPrinter pass to the pipeline that prints assembly or 66 /// machine code from the MI representation. 67 bool addAsmPrinter(PassManagerBase &PM, raw_pwrite_stream &Out, 68 raw_pwrite_stream *DwoOut, CodeGenFileType FileType, 69 MCContext &Context) override; 70 71 Expected<std::unique_ptr<MCStreamer>> 72 createMCStreamer(raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, 73 CodeGenFileType FileType, MCContext &Ctx) override; 74 }; 75 76 /// Helper method for getting the code model, returning Default if 77 /// CM does not have a value. The tiny and kernel models will produce 78 /// an error, so targets that support them or require more complex codemodel 79 /// selection logic should implement and call their own getEffectiveCodeModel. 80 inline CodeModel::Model 81 getEffectiveCodeModel(std::optional<CodeModel::Model> CM, 82 CodeModel::Model Default) { 83 if (CM) { 84 // By default, targets do not support the tiny and kernel models. 85 if (*CM == CodeModel::Tiny) 86 reportFatalUsageError("Target does not support the tiny CodeModel"); 87 if (*CM == CodeModel::Kernel) 88 reportFatalUsageError("Target does not support the kernel CodeModel"); 89 return *CM; 90 } 91 return Default; 92 } 93 94 } // namespace llvm 95 96 #endif 97