| 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, 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 std::nullopt; } |
| 36 |
ArrayRef<const char *> getRegMaskNames() const override { return std::nullopt; } |
| 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 |
bool eliminateFrameIndex(MachineBasicBlock::iterator MI, int SPAdj, |
| 68 |
unsigned FIOperandNum, |
| 69 |
RegScavenger *RS = nullptr) const override { |
| 70 |
return false; |
| 71 |
|
| 72 |
} |
| 73 |
}; |
| 74 |
|
| 75 |
class BogusSubtarget : public TargetSubtargetInfo { |
| 76 |
public: |
| 77 |
BogusSubtarget(TargetMachine &TM) |
| 78 |
: TargetSubtargetInfo(Triple(""), "", "", "", {}, {}, nullptr, nullptr, |
| 79 |
nullptr, nullptr, nullptr, nullptr), |
| 80 |
FL(), TL(TM) {} |
| 81 |
~BogusSubtarget() override {} |
| 82 |
|
| 83 |
const TargetFrameLowering *getFrameLowering() const override { return &FL; } |
| 84 |
|
| 85 |
const TargetLowering *getTargetLowering() const override { return &TL; } |
| 86 |
|
| 87 |
const TargetInstrInfo *getInstrInfo() const override { return &TII; } |
| 88 |
|
| 89 |
const TargetRegisterInfo *getRegisterInfo() const override { return &TRI; } |
| 90 |
|
| 91 |
private: |
| 92 |
BogusFrameLowering FL; |
| 93 |
BogusRegisterInfo TRI; |
| 94 |
BogusTargetLowering TL; |
| 95 |
TargetInstrInfo TII; |
| 96 |
}; |
| 97 |
|
| 98 |
static TargetOptions getTargetOptionsForBogusMachine() { |
| 99 |
TargetOptions Opts; |
| 100 |
Opts.EmitCallSiteInfo = true; |
| 101 |
return Opts; |
| 102 |
} |
| 103 |
|
| 104 |
class BogusTargetMachine : public LLVMTargetMachine { |
| 105 |
public: |
| 106 |
BogusTargetMachine() |
| 107 |
: LLVMTargetMachine(Target(), "", Triple(""), "", "", |
| 108 |
getTargetOptionsForBogusMachine(), Reloc::Static, |
| 109 |
CodeModel::Small, CodeGenOpt::Default), |
| 110 |
ST(*this) {} |
| 111 |
|
| 112 |
~BogusTargetMachine() override {} |
| 113 |
|
| 114 |
const TargetSubtargetInfo *getSubtargetImpl(const Function &) const override { |
| 115 |
return &ST; |
| 116 |
} |
| 117 |
|
| 118 |
private: |
| 119 |
BogusSubtarget ST; |
| 120 |
}; |
| 121 |
|
| 122 |
BogusTargetMachine *createTargetMachine() { |
| 123 |
static BogusTargetMachine BogusTM; |
| 124 |
return &BogusTM; |
| 125 |
} |
| 126 |
|
| 127 |
std::unique_ptr<MachineFunction> createMachineFunction(LLVMContext &Ctx, |
| 128 |
Module &M) { |
| 129 |
auto Type = FunctionType::get(Type::getVoidTy(Ctx), false); |
| 130 |
auto F = Function::Create(Type, GlobalValue::ExternalLinkage, "Test", &M); |
| 131 |
|
| 132 |
auto TM = createTargetMachine(); |
| 133 |
unsigned FunctionNum = 42; |
| 134 |
MachineModuleInfo MMI(TM); |
| 135 |
const TargetSubtargetInfo &STI = *TM->getSubtargetImpl(*F); |
| 136 |
|
| 137 |
return std::make_unique<MachineFunction>(*F, *TM, STI, FunctionNum, MMI); |
| 138 |
} |
| 139 |
|