Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //==--- llvm/CodeGen/ReachingDefAnalysis.h - Reaching Def Analysis -*- 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 Reaching Defs Analysis pass.
0010 ///
0011 /// This pass tracks for each instruction what is the "closest" reaching def of
0012 /// a given register. It is used by BreakFalseDeps (for clearance calculation)
0013 /// and ExecutionDomainFix (for arbitrating conflicting domains).
0014 ///
0015 /// Note that this is different from the usual definition notion of liveness.
0016 /// The CPU doesn't care whether or not we consider a register killed.
0017 ///
0018 //
0019 //===----------------------------------------------------------------------===//
0020 
0021 #ifndef LLVM_CODEGEN_REACHINGDEFANALYSIS_H
0022 #define LLVM_CODEGEN_REACHINGDEFANALYSIS_H
0023 
0024 #include "llvm/ADT/DenseMap.h"
0025 #include "llvm/ADT/SmallVector.h"
0026 #include "llvm/ADT/TinyPtrVector.h"
0027 #include "llvm/CodeGen/LoopTraversal.h"
0028 #include "llvm/CodeGen/MachineFunctionPass.h"
0029 #include "llvm/InitializePasses.h"
0030 
0031 namespace llvm {
0032 
0033 class MachineBasicBlock;
0034 class MachineInstr;
0035 
0036 /// Thin wrapper around "int" used to store reaching definitions,
0037 /// using an encoding that makes it compatible with TinyPtrVector.
0038 /// The 0th LSB is forced zero (and will be used for pointer union tagging),
0039 /// The 1st LSB is forced one (to make sure the value is non-zero).
0040 class ReachingDef {
0041   uintptr_t Encoded;
0042   friend struct PointerLikeTypeTraits<ReachingDef>;
0043   explicit ReachingDef(uintptr_t Encoded) : Encoded(Encoded) {}
0044 
0045 public:
0046   ReachingDef(std::nullptr_t) : Encoded(0) {}
0047   ReachingDef(int Instr) : Encoded(((uintptr_t) Instr << 2) | 2) {}
0048   operator int() const { return ((int) Encoded) >> 2; }
0049 };
0050 
0051 template<>
0052 struct PointerLikeTypeTraits<ReachingDef> {
0053   static constexpr int NumLowBitsAvailable = 1;
0054 
0055   static inline void *getAsVoidPointer(const ReachingDef &RD) {
0056     return reinterpret_cast<void *>(RD.Encoded);
0057   }
0058 
0059   static inline ReachingDef getFromVoidPointer(void *P) {
0060     return ReachingDef(reinterpret_cast<uintptr_t>(P));
0061   }
0062 
0063   static inline ReachingDef getFromVoidPointer(const void *P) {
0064     return ReachingDef(reinterpret_cast<uintptr_t>(P));
0065   }
0066 };
0067 
0068 // The storage for all reaching definitions.
0069 class MBBReachingDefsInfo {
0070 public:
0071   void init(unsigned NumBlockIDs) { AllReachingDefs.resize(NumBlockIDs); }
0072 
0073   unsigned numBlockIDs() const { return AllReachingDefs.size(); }
0074 
0075   void startBasicBlock(unsigned MBBNumber, unsigned NumRegUnits) {
0076     AllReachingDefs[MBBNumber].resize(NumRegUnits);
0077   }
0078 
0079   void append(unsigned MBBNumber, unsigned Unit, int Def) {
0080     AllReachingDefs[MBBNumber][Unit].push_back(Def);
0081   }
0082 
0083   void prepend(unsigned MBBNumber, unsigned Unit, int Def) {
0084     auto &Defs = AllReachingDefs[MBBNumber][Unit];
0085     Defs.insert(Defs.begin(), Def);
0086   }
0087 
0088   void replaceFront(unsigned MBBNumber, unsigned Unit, int Def) {
0089     assert(!AllReachingDefs[MBBNumber][Unit].empty());
0090     *AllReachingDefs[MBBNumber][Unit].begin() = Def;
0091   }
0092 
0093   void clear() { AllReachingDefs.clear(); }
0094 
0095   ArrayRef<ReachingDef> defs(unsigned MBBNumber, unsigned Unit) const {
0096     if (AllReachingDefs[MBBNumber].empty())
0097       // Block IDs are not necessarily dense.
0098       return ArrayRef<ReachingDef>();
0099     return AllReachingDefs[MBBNumber][Unit];
0100   }
0101 
0102 private:
0103   /// All reaching defs of a given RegUnit for a given MBB.
0104   using MBBRegUnitDefs = TinyPtrVector<ReachingDef>;
0105   /// All reaching defs of all reg units for a given MBB
0106   using MBBDefsInfo = std::vector<MBBRegUnitDefs>;
0107 
0108   /// All reaching defs of all reg units for all MBBs
0109   SmallVector<MBBDefsInfo, 4> AllReachingDefs;
0110 };
0111 
0112 /// This class provides the reaching def analysis.
0113 class ReachingDefAnalysis : public MachineFunctionPass {
0114 private:
0115   MachineFunction *MF = nullptr;
0116   const TargetRegisterInfo *TRI = nullptr;
0117   LoopTraversal::TraversalOrder TraversedMBBOrder;
0118   unsigned NumRegUnits = 0;
0119   /// Instruction that defined each register, relative to the beginning of the
0120   /// current basic block.  When a LiveRegsDefInfo is used to represent a
0121   /// live-out register, this value is relative to the end of the basic block,
0122   /// so it will be a negative number.
0123   using LiveRegsDefInfo = std::vector<int>;
0124   LiveRegsDefInfo LiveRegs;
0125 
0126   /// Keeps clearance information for all registers. Note that this
0127   /// is different from the usual definition notion of liveness. The CPU
0128   /// doesn't care whether or not we consider a register killed.
0129   using OutRegsInfoMap = SmallVector<LiveRegsDefInfo, 4>;
0130   OutRegsInfoMap MBBOutRegsInfos;
0131 
0132   /// Current instruction number.
0133   /// The first instruction in each basic block is 0.
0134   int CurInstr = -1;
0135 
0136   /// Maps instructions to their instruction Ids, relative to the beginning of
0137   /// their basic blocks.
0138   DenseMap<MachineInstr *, int> InstIds;
0139 
0140   MBBReachingDefsInfo MBBReachingDefs;
0141 
0142   /// Default values are 'nothing happened a long time ago'.
0143   const int ReachingDefDefaultVal = -(1 << 21);
0144 
0145   using InstSet = SmallPtrSetImpl<MachineInstr*>;
0146   using BlockSet = SmallPtrSetImpl<MachineBasicBlock*>;
0147 
0148 public:
0149   static char ID; // Pass identification, replacement for typeid
0150 
0151   ReachingDefAnalysis() : MachineFunctionPass(ID) {
0152     initializeReachingDefAnalysisPass(*PassRegistry::getPassRegistry());
0153   }
0154   void releaseMemory() override;
0155 
0156   void getAnalysisUsage(AnalysisUsage &AU) const override {
0157     AU.setPreservesAll();
0158     MachineFunctionPass::getAnalysisUsage(AU);
0159   }
0160 
0161   bool runOnMachineFunction(MachineFunction &MF) override;
0162 
0163   MachineFunctionProperties getRequiredProperties() const override {
0164     return MachineFunctionProperties().set(
0165         MachineFunctionProperties::Property::NoVRegs).set(
0166           MachineFunctionProperties::Property::TracksLiveness);
0167   }
0168 
0169   /// Re-run the analysis.
0170   void reset();
0171 
0172   /// Initialize data structures.
0173   void init();
0174 
0175   /// Traverse the machine function, mapping definitions.
0176   void traverse();
0177 
0178   /// Provides the instruction id of the closest reaching def instruction of
0179   /// Reg that reaches MI, relative to the begining of MI's basic block.
0180   int getReachingDef(MachineInstr *MI, Register Reg) const;
0181 
0182   /// Return whether A and B use the same def of Reg.
0183   bool hasSameReachingDef(MachineInstr *A, MachineInstr *B, Register Reg) const;
0184 
0185   /// Return whether the reaching def for MI also is live out of its parent
0186   /// block.
0187   bool isReachingDefLiveOut(MachineInstr *MI, Register Reg) const;
0188 
0189   /// Return the local MI that produces the live out value for Reg, or
0190   /// nullptr for a non-live out or non-local def.
0191   MachineInstr *getLocalLiveOutMIDef(MachineBasicBlock *MBB,
0192                                      Register Reg) const;
0193 
0194   /// If a single MachineInstr creates the reaching definition, then return it.
0195   /// Otherwise return null.
0196   MachineInstr *getUniqueReachingMIDef(MachineInstr *MI, Register Reg) const;
0197 
0198   /// If a single MachineInstr creates the reaching definition, for MIs operand
0199   /// at Idx, then return it. Otherwise return null.
0200   MachineInstr *getMIOperand(MachineInstr *MI, unsigned Idx) const;
0201 
0202   /// If a single MachineInstr creates the reaching definition, for MIs MO,
0203   /// then return it. Otherwise return null.
0204   MachineInstr *getMIOperand(MachineInstr *MI, MachineOperand &MO) const;
0205 
0206   /// Provide whether the register has been defined in the same basic block as,
0207   /// and before, MI.
0208   bool hasLocalDefBefore(MachineInstr *MI, Register Reg) const;
0209 
0210   /// Return whether the given register is used after MI, whether it's a local
0211   /// use or a live out.
0212   bool isRegUsedAfter(MachineInstr *MI, Register Reg) const;
0213 
0214   /// Return whether the given register is defined after MI.
0215   bool isRegDefinedAfter(MachineInstr *MI, Register Reg) const;
0216 
0217   /// Provides the clearance - the number of instructions since the closest
0218   /// reaching def instuction of Reg that reaches MI.
0219   int getClearance(MachineInstr *MI, Register Reg) const;
0220 
0221   /// Provides the uses, in the same block as MI, of register that MI defines.
0222   /// This does not consider live-outs.
0223   void getReachingLocalUses(MachineInstr *MI, Register Reg,
0224                             InstSet &Uses) const;
0225 
0226   /// Search MBB for a definition of Reg and insert it into Defs. If no
0227   /// definition is found, recursively search the predecessor blocks for them.
0228   void getLiveOuts(MachineBasicBlock *MBB, Register Reg, InstSet &Defs,
0229                    BlockSet &VisitedBBs) const;
0230   void getLiveOuts(MachineBasicBlock *MBB, Register Reg, InstSet &Defs) const;
0231 
0232   /// For the given block, collect the instructions that use the live-in
0233   /// value of the provided register. Return whether the value is still
0234   /// live on exit.
0235   bool getLiveInUses(MachineBasicBlock *MBB, Register Reg, InstSet &Uses) const;
0236 
0237   /// Collect the users of the value stored in Reg, which is defined
0238   /// by MI.
0239   void getGlobalUses(MachineInstr *MI, Register Reg, InstSet &Uses) const;
0240 
0241   /// Collect all possible definitions of the value stored in Reg, which is
0242   /// used by MI.
0243   void getGlobalReachingDefs(MachineInstr *MI, Register Reg,
0244                              InstSet &Defs) const;
0245 
0246   /// Return whether From can be moved forwards to just before To.
0247   bool isSafeToMoveForwards(MachineInstr *From, MachineInstr *To) const;
0248 
0249   /// Return whether From can be moved backwards to just after To.
0250   bool isSafeToMoveBackwards(MachineInstr *From, MachineInstr *To) const;
0251 
0252   /// Assuming MI is dead, recursively search the incoming operands which are
0253   /// killed by MI and collect those that would become dead.
0254   void collectKilledOperands(MachineInstr *MI, InstSet &Dead) const;
0255 
0256   /// Return whether removing this instruction will have no effect on the
0257   /// program, returning the redundant use-def chain.
0258   bool isSafeToRemove(MachineInstr *MI, InstSet &ToRemove) const;
0259 
0260   /// Return whether removing this instruction will have no effect on the
0261   /// program, ignoring the possible effects on some instructions, returning
0262   /// the redundant use-def chain.
0263   bool isSafeToRemove(MachineInstr *MI, InstSet &ToRemove,
0264                       InstSet &Ignore) const;
0265 
0266   /// Return whether a MachineInstr could be inserted at MI and safely define
0267   /// the given register without affecting the program.
0268   bool isSafeToDefRegAt(MachineInstr *MI, Register Reg) const;
0269 
0270   /// Return whether a MachineInstr could be inserted at MI and safely define
0271   /// the given register without affecting the program, ignoring any effects
0272   /// on the provided instructions.
0273   bool isSafeToDefRegAt(MachineInstr *MI, Register Reg, InstSet &Ignore) const;
0274 
0275 private:
0276   /// Set up LiveRegs by merging predecessor live-out values.
0277   void enterBasicBlock(MachineBasicBlock *MBB);
0278 
0279   /// Update live-out values.
0280   void leaveBasicBlock(MachineBasicBlock *MBB);
0281 
0282   /// Process he given basic block.
0283   void processBasicBlock(const LoopTraversal::TraversedMBBInfo &TraversedMBB);
0284 
0285   /// Process block that is part of a loop again.
0286   void reprocessBasicBlock(MachineBasicBlock *MBB);
0287 
0288   /// Update def-ages for registers defined by MI.
0289   /// Also break dependencies on partial defs and undef uses.
0290   void processDefs(MachineInstr *);
0291 
0292   /// Utility function for isSafeToMoveForwards/Backwards.
0293   template<typename Iterator>
0294   bool isSafeToMove(MachineInstr *From, MachineInstr *To) const;
0295 
0296   /// Return whether removing this instruction will have no effect on the
0297   /// program, ignoring the possible effects on some instructions, returning
0298   /// the redundant use-def chain.
0299   bool isSafeToRemove(MachineInstr *MI, InstSet &Visited,
0300                       InstSet &ToRemove, InstSet &Ignore) const;
0301 
0302   /// Provides the MI, from the given block, corresponding to the Id or a
0303   /// nullptr if the id does not refer to the block.
0304   MachineInstr *getInstFromId(MachineBasicBlock *MBB, int InstId) const;
0305 
0306   /// Provides the instruction of the closest reaching def instruction of
0307   /// Reg that reaches MI, relative to the begining of MI's basic block.
0308   MachineInstr *getReachingLocalMIDef(MachineInstr *MI, Register Reg) const;
0309 };
0310 
0311 } // namespace llvm
0312 
0313 #endif // LLVM_CODEGEN_REACHINGDEFANALYSIS_H