//==--- MachineLateInstrsCleanup.cpp - Late Instructions Cleanup Pass -----===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This simple pass removes any identical and redundant immediate or address // loads to the same register. The immediate loads removed can originally be // the result of rematerialization, while the addresses are redundant frame // addressing anchor points created during Frame Indices elimination. // //===----------------------------------------------------------------------===// #include "llvm/ADT/BitVector.h" #include "llvm/ADT/PostOrderIterator.h" #include "llvm/ADT/Statistic.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineInstr.h" #include "llvm/CodeGen/MachineOperand.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/TargetInstrInfo.h" #include "llvm/CodeGen/TargetRegisterInfo.h" #include "llvm/CodeGen/TargetSubtargetInfo.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" #include "llvm/Support/Debug.h" usingnamespacellvm; #define DEBUG_TYPE … STATISTIC(NumRemoved, "Number of redundant instructions removed."); namespace { class MachineLateInstrsCleanup : public MachineFunctionPass { … }; } // end anonymous namespace char MachineLateInstrsCleanup::ID = …; char &llvm::MachineLateInstrsCleanupID = …; INITIALIZE_PASS(…) bool MachineLateInstrsCleanup::runOnMachineFunction(MachineFunction &MF) { … } // Clear any previous kill flag on Reg found before I in MBB. Walk backwards // in MBB and if needed continue in predecessors until a use/def of Reg is // encountered. This seems to be faster in practice than tracking kill flags // in a map. void MachineLateInstrsCleanup:: clearKillsForDef(Register Reg, MachineBasicBlock *MBB, MachineBasicBlock::iterator I, BitVector &VisitedPreds) { … } void MachineLateInstrsCleanup::removeRedundantDef(MachineInstr *MI) { … } // Return true if MI is a potential candidate for reuse/removal and if so // also the register it defines in DefedReg. A candidate is a simple // instruction that does not touch memory, has only one register definition // and the only reg it may use is FrameReg. Typically this is an immediate // load or a load-address instruction. static bool isCandidate(const MachineInstr *MI, Register &DefedReg, Register FrameReg) { … } bool MachineLateInstrsCleanup::processBlock(MachineBasicBlock *MBB) { … }