1//===- ReduceRegisterUses.cpp - Specialized Delta Pass --------------------===//
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 implements a function which calls the Generic Delta pass in order
10// to reduce uninteresting register uses from the MachineFunction.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ReduceRegisterUses.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineModuleInfo.h"
17#include "llvm/CodeGen/MachineRegisterInfo.h"
18
19using namespace llvm;
20
21static void removeUsesFromFunction(Oracle &O, MachineFunction &MF) {
22 MachineRegisterInfo &MRI = MF.getRegInfo();
23
24 for (MachineBasicBlock &MBB : MF) {
25 for (MachineInstr &MI : MBB) {
26 // Generic instructions are not supposed to have undef operands.
27 if (isPreISelGenericOpcode(Opcode: MI.getOpcode()))
28 continue;
29
30 int NumOperands = MI.getNumOperands();
31 int NumRequiredOps = MI.getNumExplicitOperands() +
32 MI.getDesc().implicit_defs().size() +
33 MI.getDesc().implicit_uses().size();
34
35 for (int I = NumOperands - 1; I >= 0; --I) {
36 MachineOperand &MO = MI.getOperand(i: I);
37 if (!MO.isReg() || !MO.readsReg())
38 continue;
39
40 Register Reg = MO.getReg();
41 if (Reg.isPhysical() && MRI.isReserved(PhysReg: Reg))
42 continue;
43
44 if (O.shouldKeep())
45 continue;
46
47 // Remove implicit operands. If the register is part of the fixed
48 // operand list, set to undef.
49 if (I >= NumRequiredOps)
50 MI.removeOperand(OpNo: I);
51 else
52 MO.setIsUndef();
53 }
54 }
55 }
56}
57
58void llvm::reduceRegisterUsesMIRDeltaPass(Oracle &O,
59 ReducerWorkItem &WorkItem) {
60 for (const Function &F : WorkItem.getModule()) {
61 if (auto *MF = WorkItem.MMI->getMachineFunction(F))
62 removeUsesFromFunction(O, MF&: *MF);
63 }
64}
65