[llvm] [ExtendLifetimes] Implement llvm.fake.use to extend variable lifetimes (PR #86149)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 7 07:52:45 PDT 2024
================
@@ -0,0 +1,170 @@
+//===---- RemoveLoadsIntoFakeUses.cpp - Remove loads with no real uses ----===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// The FAKE_USE instruction is used to preserve certain values through
+/// optimizations for the sake of debugging. This may result in spilled values
+/// being loaded into registers that are only used by FAKE_USEs; this is not
+/// necessary for debugging purposes, because at that point the value must be on
+/// the stack and hence available for debugging. Therefore, this pass removes
+/// loads that are only used by FAKE_USEs.
+///
+/// This pass should run as late as possible, to ensure that we don't
+/// inadvertently shorten stack lifetimes by removing these loads, since the
+/// FAKE_USEs will also no longer be in effect.
+///
+//===----------------------------------------------------------------------===//
+
+#include "llvm/ADT/PostOrderIterator.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/CodeGen/LiveRegUnits.h"
+#include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/CodeGen/TargetSubtargetInfo.h"
+#include "llvm/IR/Function.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/Support/Debug.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "remove-loads-into-fake-uses"
+
+STATISTIC(NumLoadsDeleted, "Number of dead load instructions deleted");
+STATISTIC(NumFakeUsesDeleted, "Number of FAKE_USE instructions deleted");
+
+class RemoveLoadsIntoFakeUses : public MachineFunctionPass {
+public:
+ static char ID;
+
+ RemoveLoadsIntoFakeUses() : MachineFunctionPass(ID) {
+ initializeRemoveLoadsIntoFakeUsesPass(*PassRegistry::getPassRegistry());
+ }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.setPreservesCFG();
+ MachineFunctionPass::getAnalysisUsage(AU);
+ }
+
+ StringRef getPassName() const override {
+ return "Remove Loads Into Fake Uses";
+ }
+
+ bool runOnMachineFunction(MachineFunction &MF) override;
+};
+
+char RemoveLoadsIntoFakeUses::ID = 0;
+char &llvm::RemoveLoadsIntoFakeUsesID = RemoveLoadsIntoFakeUses::ID;
+
+INITIALIZE_PASS_BEGIN(RemoveLoadsIntoFakeUses, DEBUG_TYPE,
+ "Remove Loads Into Fake Uses", false, false)
+INITIALIZE_PASS_END(RemoveLoadsIntoFakeUses, DEBUG_TYPE,
+ "Remove Loads Into Fake Uses", false, false)
+
+bool RemoveLoadsIntoFakeUses::runOnMachineFunction(MachineFunction &MF) {
+ // Only `optdebug` functions should contain FAKE_USEs, so don't try to run
+ // this for other functions.
+ dbgs() << "We might run the pass!\n";
+ if (!MF.getFunction().hasFnAttribute(Attribute::OptimizeForDebugging) ||
+ skipFunction(MF.getFunction()))
+ return false;
+
+ dbgs() << "We could run the pass!\n";
+ // This implementation assumes we are post-RA.
+ if (!MF.getProperties().hasProperty(
+ MachineFunctionProperties::Property::NoVRegs))
+ return false;
+
+ bool AnyChanges = false;
+
+ LiveRegUnits LivePhysRegs;
+ const MachineRegisterInfo *MRI = &MF.getRegInfo();
+ const TargetSubtargetInfo &ST = MF.getSubtarget();
+ const TargetInstrInfo *TII = ST.getInstrInfo();
+ const TargetRegisterInfo *TRI = ST.getRegisterInfo();
+
+ SmallDenseMap<Register, SmallVector<MachineInstr *>> RegFakeUses;
+ LivePhysRegs.init(*TRI);
+ dbgs() << "Running the pass!\n";
----------------
arsenm wrote:
Stray debug printing
https://github.com/llvm/llvm-project/pull/86149
More information about the llvm-commits
mailing list