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