Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:25

0001 //== llvm/CodeGen/GlobalISel/Localizer.h - Localizer -------------*- C++ -*-==//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// \file This file describes the interface of the Localizer pass.
0010 /// This pass moves/duplicates constant-like instructions close to their uses.
0011 /// Its primarily goal is to workaround the deficiencies of the fast register
0012 /// allocator.
0013 /// With GlobalISel constants are all materialized in the entry block of
0014 /// a function. However, the fast allocator cannot rematerialize constants and
0015 /// has a lot more live-ranges to deal with and will most likely end up
0016 /// spilling a lot.
0017 /// By pushing the constants close to their use, we only create small
0018 /// live-ranges.
0019 //===----------------------------------------------------------------------===//
0020 
0021 #ifndef LLVM_CODEGEN_GLOBALISEL_LOCALIZER_H
0022 #define LLVM_CODEGEN_GLOBALISEL_LOCALIZER_H
0023 
0024 #include "llvm/ADT/SetVector.h"
0025 #include "llvm/CodeGen/MachineFunctionPass.h"
0026 
0027 namespace llvm {
0028 // Forward declarations.
0029 class AnalysisUsage;
0030 class MachineBasicBlock;
0031 class MachineInstr;
0032 class MachineOperand;
0033 class MachineRegisterInfo;
0034 class TargetTransformInfo;
0035 
0036 /// This pass implements the localization mechanism described at the
0037 /// top of this file. One specificity of the implementation is that
0038 /// it will materialize one and only one instance of a constant per
0039 /// basic block, thus enabling reuse of that constant within that block.
0040 /// Moreover, it only materializes constants in blocks where they
0041 /// are used. PHI uses are considered happening at the end of the
0042 /// related predecessor.
0043 class Localizer : public MachineFunctionPass {
0044 public:
0045   static char ID;
0046 
0047 private:
0048   /// An input function to decide if the pass should run or not
0049   /// on the given MachineFunction.
0050   std::function<bool(const MachineFunction &)> DoNotRunPass;
0051 
0052   /// MRI contains all the register class/bank information that this
0053   /// pass uses and updates.
0054   MachineRegisterInfo *MRI = nullptr;
0055   /// TTI used for getting remat costs for instructions.
0056   TargetTransformInfo *TTI = nullptr;
0057 
0058   /// Check if \p MOUse is used in the same basic block as \p Def.
0059   /// If the use is in the same block, we say it is local.
0060   /// When the use is not local, \p InsertMBB will contain the basic
0061   /// block when to insert \p Def to have a local use.
0062   static bool isLocalUse(MachineOperand &MOUse, const MachineInstr &Def,
0063                          MachineBasicBlock *&InsertMBB);
0064 
0065   /// Initialize the field members using \p MF.
0066   void init(MachineFunction &MF);
0067 
0068   typedef SmallSetVector<MachineInstr *, 32> LocalizedSetVecT;
0069 
0070   /// If \p Op is a reg operand of a PHI, return the number of total
0071   /// operands in the PHI that are the same as \p Op, including itself.
0072   unsigned getNumPhiUses(MachineOperand &Op) const;
0073 
0074   /// Do inter-block localization from the entry block.
0075   bool localizeInterBlock(MachineFunction &MF,
0076                           LocalizedSetVecT &LocalizedInstrs);
0077 
0078   /// Do intra-block localization of already localized instructions.
0079   bool localizeIntraBlock(LocalizedSetVecT &LocalizedInstrs);
0080 
0081 public:
0082   Localizer();
0083   Localizer(std::function<bool(const MachineFunction &)>);
0084 
0085   StringRef getPassName() const override { return "Localizer"; }
0086 
0087   MachineFunctionProperties getRequiredProperties() const override {
0088     return MachineFunctionProperties()
0089         .set(MachineFunctionProperties::Property::IsSSA);
0090   }
0091 
0092   void getAnalysisUsage(AnalysisUsage &AU) const override;
0093 
0094   bool runOnMachineFunction(MachineFunction &MF) override;
0095 };
0096 
0097 } // End namespace llvm.
0098 
0099 #endif