1//===-- RISCVTargetMachine.h - Define TargetMachine for RISC-V --*- 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// This file declares the RISC-V specific subclass of TargetMachine.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_TARGET_RISCV_RISCVTARGETMACHINE_H
14#define LLVM_LIB_TARGET_RISCV_RISCVTARGETMACHINE_H
15
16#include "MCTargetDesc/RISCVMCTargetDesc.h"
17#include "RISCVSubtarget.h"
18#include "llvm/CodeGen/CodeGenTargetMachineImpl.h"
19#include "llvm/IR/Attributes.h"
20#include "llvm/IR/DataLayout.h"
21#include "llvm/Target/LastSubtargetCache.h"
22#include <optional>
23#include <utility>
24
25namespace llvm {
26class RISCVTargetMachine : public CodeGenTargetMachineImpl {
27 const RISCVOptions &CLOpts;
28 std::unique_ptr<TargetLoweringObjectFile> TLOF;
29 mutable StringMap<std::unique_ptr<RISCVSubtarget>> SubtargetMap;
30
31 /// The attributes that select a subtarget.
32 struct SubtargetAttrs {
33 Attribute CPU, TuneCPU, FS, VScaleRange;
34
35 bool operator==(const SubtargetAttrs &RHS) const {
36 return CPU == RHS.CPU && TuneCPU == RHS.TuneCPU && FS == RHS.FS &&
37 VScaleRange == RHS.VScaleRange;
38 }
39
40 bool operator!=(const SubtargetAttrs &RHS) const { return !(*this == RHS); }
41 };
42 mutable LastSubtargetCache<RISCVSubtarget, SubtargetAttrs> LastSubtarget;
43
44public:
45 RISCVTargetMachine(const Target &T, const Triple &TT, StringRef CPU,
46 StringRef FS, const TargetOptions &Options,
47 std::optional<Reloc::Model> RM,
48 std::optional<CodeModel::Model> CM, CodeGenOptLevel OL,
49 bool JIT);
50
51 const RISCVOptions &getCLOpts() const { return CLOpts; }
52 const RISCVSubtarget *getSubtargetImpl(const Function &F) const override;
53 // DO NOT IMPLEMENT: There is no such thing as a valid default subtarget,
54 // subtargets are per-function entities based on the target-specific
55 // attributes of each function.
56 const RISCVSubtarget *getSubtargetImpl() const = delete;
57
58 TargetPassConfig *createPassConfig(PassManagerBase &PM) override;
59
60 TargetLoweringObjectFile *getObjFileLowering() const override {
61 return TLOF.get();
62 }
63
64 MachineFunctionInfo *
65 createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
66 const TargetSubtargetInfo *STI) const override;
67
68 TargetTransformInfo getTargetTransformInfo(const Function &F) const override;
69
70 bool isNoopAddrSpaceCast(const DataLayout &DL, unsigned SrcAS,
71 unsigned DstAS) const override;
72
73 yaml::MachineFunctionInfo *createDefaultFuncInfoYAML() const override;
74 yaml::MachineFunctionInfo *
75 convertFuncInfoToYAML(const MachineFunction &MF) const override;
76 bool parseMachineFunctionInfo(const yaml::MachineFunctionInfo &,
77 PerFunctionMIParsingState &PFS,
78 SMDiagnostic &Error,
79 SMRange &SourceRange) const override;
80 void registerPassBuilderCallbacks(PassBuilder &PB) override;
81 Error buildCodeGenPipeline(ModulePassManager &MPM, ModuleAnalysisManager &MAM,
82 raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
83 CodeGenFileType FileType,
84 const CGPassBuilderOption &Opt, MCContext &Ctx,
85 PassInstrumentationCallbacks *PIC) override;
86 ScheduleDAGInstrs *
87 createMachineScheduler(MachineSchedContext *C) const override;
88 ScheduleDAGInstrs *
89 createPostMachineScheduler(MachineSchedContext *C) const override;
90};
91
92std::unique_ptr<ScheduleDAGMutation>
93createRISCVVectorMaskDAGMutation(const TargetRegisterInfo *TRI);
94
95} // namespace llvm
96
97#endif
98