LLVM 20.0.0git
RemoveLoadsIntoFakeUses.cpp
Go to the documentation of this file.
1//===---- RemoveLoadsIntoFakeUses.cpp - Remove loads with no real uses ----===//
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/// \file
10/// The FAKE_USE instruction is used to preserve certain values through
11/// optimizations for the sake of debugging. This may result in spilled values
12/// being loaded into registers that are only used by FAKE_USEs; this is not
13/// necessary for debugging purposes, because at that point the value must be on
14/// the stack and hence available for debugging. Therefore, this pass removes
15/// loads that are only used by FAKE_USEs.
16///
17/// This pass should run very late, to ensure that we don't inadvertently
18/// shorten stack lifetimes by removing these loads, since the FAKE_USEs will
19/// also no longer be in effect. Running immediately before LiveDebugValues
20/// ensures that LDV will have accurate information of the machine location of
21/// debug values.
22///
23//===----------------------------------------------------------------------===//
24
26#include "llvm/ADT/Statistic.h"
32#include "llvm/IR/Function.h"
34#include "llvm/Support/Debug.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "remove-loads-into-fake-uses"
39
40STATISTIC(NumLoadsDeleted, "Number of dead load instructions deleted");
41STATISTIC(NumFakeUsesDeleted, "Number of FAKE_USE instructions deleted");
42
44public:
45 static char ID;
46
49 }
50
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.setPreservesCFG();
54 }
55
58 MachineFunctionProperties::Property::NoVRegs);
59 }
60
61 StringRef getPassName() const override {
62 return "Remove Loads Into Fake Uses";
63 }
64
65 bool runOnMachineFunction(MachineFunction &MF) override;
66};
67
70
72 "Remove Loads Into Fake Uses", false, false)
74 "Remove Loads Into Fake Uses", false, false)
75
76bool RemoveLoadsIntoFakeUses::runOnMachineFunction(MachineFunction &MF) {
77 // Only run this for functions that have fake uses.
78 if (!MF.hasFakeUses() || skipFunction(MF.getFunction()))
79 return false;
80
81 bool AnyChanges = false;
82
84 const MachineRegisterInfo *MRI = &MF.getRegInfo();
85 const TargetSubtargetInfo &ST = MF.getSubtarget();
86 const TargetInstrInfo *TII = ST.getInstrInfo();
87 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
88
92 for (MachineBasicBlock *MBB : post_order(&MF)) {
94
96 if (MI.isFakeUse()) {
97 for (const MachineOperand &MO : MI.operands()) {
98 // Track the Fake Uses that use this register so that we can delete
99 // them if we delete the corresponding load.
100 if (MO.isReg())
101 RegFakeUses[MO.getReg()].push_back(&MI);
102 }
103 // Do not record FAKE_USE uses in LivePhysRegs so that we can recognize
104 // otherwise-unused loads.
105 continue;
106 }
107
108 // If the restore size is not std::nullopt then we are dealing with a
109 // reload of a spilled register.
110 if (MI.getRestoreSize(TII)) {
111 Register Reg = MI.getOperand(0).getReg();
112 assert(Reg.isPhysical() && "VReg seen in function with NoVRegs set?");
113 // Don't delete live physreg defs, or any reserved register defs.
114 if (!LivePhysRegs.available(Reg) || MRI->isReserved(Reg))
115 continue;
116 // There should be an exact match between the loaded register and the
117 // FAKE_USE use. If not, this is a load that is unused by anything? It
118 // should probably be deleted, but that's outside of this pass' scope.
119 if (RegFakeUses.contains(Reg)) {
120 LLVM_DEBUG(dbgs() << "RemoveLoadsIntoFakeUses: DELETING: " << MI);
121 // It is possible that some DBG_VALUE instructions refer to this
122 // instruction. They will be deleted in the live debug variable
123 // analysis.
124 MI.eraseFromParent();
125 AnyChanges = true;
126 ++NumLoadsDeleted;
127 // Each FAKE_USE now appears to be a fake use of the previous value
128 // of the loaded register; delete them to avoid incorrectly
129 // interpreting them as such.
130 for (MachineInstr *FakeUse : RegFakeUses[Reg]) {
132 << "RemoveLoadsIntoFakeUses: DELETING: " << *FakeUse);
133 FakeUse->eraseFromParent();
134 }
135 NumFakeUsesDeleted += RegFakeUses[Reg].size();
136 RegFakeUses[Reg].clear();
137 }
138 continue;
139 }
140
141 // In addition to tracking LivePhysRegs, we need to clear RegFakeUses each
142 // time a register is defined, as existing FAKE_USEs no longer apply to
143 // that register.
144 if (!RegFakeUses.empty()) {
145 for (const MachineOperand &MO : MI.operands()) {
146 if (MO.isReg() && MO.isDef()) {
147 Register Reg = MO.getReg();
148 assert(Reg.isPhysical() &&
149 "VReg seen in function with NoVRegs set?");
150 for (MCRegUnit Unit : TRI->regunits(Reg))
151 RegFakeUses.erase(Unit);
152 }
153 }
154 }
156 }
157 }
158
159 return AnyChanges;
160}
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock & MBB
#define LLVM_DEBUG(...)
Definition: Debug.h:106
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
A set of register units.
unsigned const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
Remove Loads Into Fake Uses
#define DEBUG_TYPE
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
MachineFunctionProperties getRequiredProperties() const override
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
bool erase(const KeyT &Val)
Definition: DenseMap.h:321
unsigned size() const
Definition: DenseMap.h:99
bool empty() const
Definition: DenseMap.h:98
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition: DenseMap.h:147
A set of physical registers with utility functions to track liveness when walking backward/forward th...
Definition: LivePhysRegs.h:52
bool available(const MachineRegisterInfo &MRI, MCPhysReg Reg) const
Returns true if register Reg and no aliasing register is in the set.
void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle).
void init(const TargetRegisterInfo &TRI)
(re-)initializes and clears the set.
Definition: LivePhysRegs.h:70
void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
A set of register units used to track register liveness.
Definition: LiveRegUnits.h:30
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
char & RemoveLoadsIntoFakeUsesID
RemoveLoadsIntoFakeUses pass.
iterator_range< po_iterator< T > > post_order(const T &G)
void initializeRemoveLoadsIntoFakeUsesPass(PassRegistry &)
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163