1 | // Add a few Bogus backend classes so we can create MachineInstrs without |
2 | // depending on a real target. |
3 | class BogusTargetLowering : public TargetLowering { |
4 | public: |
5 | BogusTargetLowering(TargetMachine &TM) : TargetLowering(TM) {} |
6 | }; |
7 | |
8 | class BogusFrameLowering : public TargetFrameLowering { |
9 | public: |
10 | BogusFrameLowering() |
11 | : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, Align(4), 4) {} |
12 | |
13 | void emitPrologue(MachineFunction &MF, |
14 | MachineBasicBlock &MBB) const override {} |
15 | void emitEpilogue(MachineFunction &MF, |
16 | MachineBasicBlock &MBB) const override {} |
17 | bool hasFP(const MachineFunction &MF) const override { return false; } |
18 | }; |
19 | |
20 | static TargetRegisterClass *const BogusRegisterClasses[] = {nullptr}; |
21 | |
22 | class BogusRegisterInfo : public TargetRegisterInfo { |
23 | public: |
24 | BogusRegisterInfo() |
25 | : TargetRegisterInfo(nullptr, BogusRegisterClasses, BogusRegisterClasses, |
26 | nullptr, nullptr, nullptr, LaneBitmask(~0u), nullptr, |
27 | nullptr) { |
28 | InitMCRegisterInfo(D: nullptr, NR: 0, RA: 0, PC: 0, C: nullptr, NC: 0, RURoots: nullptr, NRU: 0, DL: nullptr, |
29 | RUMS: nullptr, Strings: nullptr, ClassStrings: nullptr, SubIndices: nullptr, NumIndices: 0, RET: nullptr); |
30 | } |
31 | |
32 | const MCPhysReg * |
33 | getCalleeSavedRegs(const MachineFunction *MF) const override { |
34 | return nullptr; |
35 | } |
36 | ArrayRef<const uint32_t *> getRegMasks() const override { return std::nullopt; } |
37 | ArrayRef<const char *> getRegMaskNames() const override { return std::nullopt; } |
38 | BitVector getReservedRegs(const MachineFunction &MF) const override { |
39 | return BitVector(); |
40 | } |
41 | const RegClassWeight & |
42 | getRegClassWeight(const TargetRegisterClass *RC) const override { |
43 | static RegClassWeight Bogus{.RegWeight: 1, .WeightLimit: 16}; |
44 | return Bogus; |
45 | } |
46 | unsigned getRegUnitWeight(unsigned RegUnit) const override { return 1; } |
47 | unsigned getNumRegPressureSets() const override { return 0; } |
48 | const char *getRegPressureSetName(unsigned Idx) const override { |
49 | return "bogus" ; |
50 | } |
51 | unsigned getRegPressureSetLimit(const MachineFunction &MF, |
52 | unsigned Idx) const override { |
53 | return 0; |
54 | } |
55 | const int * |
56 | getRegClassPressureSets(const TargetRegisterClass *RC) const override { |
57 | static const int Bogus[] = {0, -1}; |
58 | return &Bogus[0]; |
59 | } |
60 | const int *getRegUnitPressureSets(unsigned RegUnit) const override { |
61 | static const int Bogus[] = {0, -1}; |
62 | return &Bogus[0]; |
63 | } |
64 | |
65 | Register getFrameRegister(const MachineFunction &MF) const override { |
66 | return 0; |
67 | } |
68 | bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, |
69 | unsigned FIOperandNum, |
70 | RegScavenger *RS = nullptr) const override { |
71 | return false; |
72 | |
73 | } |
74 | }; |
75 | |
76 | class BogusSubtarget : public TargetSubtargetInfo { |
77 | public: |
78 | BogusSubtarget(TargetMachine &TM) |
79 | : TargetSubtargetInfo(Triple("" ), "" , "" , "" , {}, {}, nullptr, nullptr, |
80 | nullptr, nullptr, nullptr, nullptr), |
81 | FL(), TL(TM) {} |
82 | ~BogusSubtarget() override {} |
83 | |
84 | const TargetFrameLowering *getFrameLowering() const override { return &FL; } |
85 | |
86 | const TargetLowering *getTargetLowering() const override { return &TL; } |
87 | |
88 | const TargetInstrInfo *getInstrInfo() const override { return &TII; } |
89 | |
90 | const TargetRegisterInfo *getRegisterInfo() const override { return &TRI; } |
91 | |
92 | private: |
93 | BogusFrameLowering FL; |
94 | BogusRegisterInfo TRI; |
95 | BogusTargetLowering TL; |
96 | TargetInstrInfo TII; |
97 | }; |
98 | |
99 | static TargetOptions getTargetOptionsForBogusMachine() { |
100 | TargetOptions Opts; |
101 | Opts.EmitCallSiteInfo = true; |
102 | return Opts; |
103 | } |
104 | |
105 | class BogusTargetMachine : public LLVMTargetMachine { |
106 | public: |
107 | BogusTargetMachine() |
108 | : LLVMTargetMachine(Target(), "" , Triple("" ), "" , "" , |
109 | getTargetOptionsForBogusMachine(), Reloc::Static, |
110 | CodeModel::Small, CodeGenOptLevel::Default), |
111 | ST(*this) {} |
112 | |
113 | ~BogusTargetMachine() override {} |
114 | |
115 | const TargetSubtargetInfo *getSubtargetImpl(const Function &) const override { |
116 | return &ST; |
117 | } |
118 | |
119 | private: |
120 | BogusSubtarget ST; |
121 | }; |
122 | |
123 | BogusTargetMachine *createTargetMachine() { |
124 | static BogusTargetMachine BogusTM; |
125 | return &BogusTM; |
126 | } |
127 | |
128 | std::unique_ptr<MachineFunction> createMachineFunction(LLVMContext &Ctx, |
129 | Module &M) { |
130 | auto Type = FunctionType::get(Result: Type::getVoidTy(C&: Ctx), isVarArg: false); |
131 | auto F = Function::Create(Ty: Type, Linkage: GlobalValue::ExternalLinkage, N: "Test" , M: &M); |
132 | |
133 | auto TM = createTargetMachine(); |
134 | unsigned FunctionNum = 42; |
135 | MachineModuleInfo MMI(TM); |
136 | const TargetSubtargetInfo &STI = *TM->getSubtargetImpl(*F); |
137 | |
138 | return std::make_unique<MachineFunction>(args&: *F, args&: *TM, args: STI, args&: FunctionNum, args&: MMI); |
139 | } |
140 | |
141 | |