1 //===-------------- BPFMIChecking.cpp - MI Checking Legality -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass performs checking to signal errors for certain illegal usages at
11 // MachineInstruction layer. Specially, the result of XADD{32,64} insn should
12 // not be used. The pass is done at the PreEmit pass right before the
13 // machine code is emitted at which point the register liveness information
14 // is still available.
16 //===----------------------------------------------------------------------===//
19 #include "BPFInstrInfo.h"
20 #include "BPFTargetMachine.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #define DEBUG_TYPE "bpf-mi-checking"
30 struct BPFMIPreEmitChecking : public MachineFunctionPass {
34 const TargetRegisterInfo *TRI;
36 BPFMIPreEmitChecking() : MachineFunctionPass(ID) {
37 initializeBPFMIPreEmitCheckingPass(*PassRegistry::getPassRegistry());
41 // Initialize class variables.
42 void initialize(MachineFunction &MFParm);
44 void checkingIllegalXADD(void);
48 // Main entry point for this pass.
49 bool runOnMachineFunction(MachineFunction &MF) override {
50 if (!skipFunction(MF.getFunction())) {
52 checkingIllegalXADD();
58 // Initialize class variables.
59 void BPFMIPreEmitChecking::initialize(MachineFunction &MFParm) {
61 TRI = MF->getSubtarget<BPFSubtarget>().getRegisterInfo();
62 LLVM_DEBUG(dbgs() << "*** BPF PreEmit checking pass ***\n\n");
65 void BPFMIPreEmitChecking::checkingIllegalXADD(void) {
66 for (MachineBasicBlock &MBB : *MF) {
67 for (MachineInstr &MI : MBB) {
68 if (MI.getOpcode() != BPF::XADD32 && MI.getOpcode() != BPF::XADD64)
71 LLVM_DEBUG(MI.dump());
72 if (!MI.allDefsAreDead()) {
74 const DebugLoc &DL = MI.getDebugLoc();
76 report_fatal_error("line " + std::to_string(DL.getLine()) +
77 ": Invalid usage of the XADD return value", false);
79 report_fatal_error("Invalid usage of the XADD return value", false);
87 } // end default namespace
89 INITIALIZE_PASS(BPFMIPreEmitChecking, "bpf-mi-pemit-checking",
90 "BPF PreEmit Checking", false, false)
92 char BPFMIPreEmitChecking::ID = 0;
93 FunctionPass* llvm::createBPFMIPreEmitCheckingPass()
95 return new BPFMIPreEmitChecking();