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