109467b48Spatrick //===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- C++ -*-===// 209467b48Spatrick // 309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information. 509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 609467b48Spatrick // 709467b48Spatrick //===----------------------------------------------------------------------===// 809467b48Spatrick // 909467b48Spatrick // This file implements the LiveVariables analysis pass. For each machine 1009467b48Spatrick // instruction in the function, this pass calculates the set of registers that 1109467b48Spatrick // are immediately dead after the instruction (i.e., the instruction calculates 1209467b48Spatrick // the value, but it is never used) and the set of registers that are used by 1309467b48Spatrick // the instruction, but are never used after the instruction (i.e., they are 1409467b48Spatrick // killed). 1509467b48Spatrick // 1609467b48Spatrick // This class computes live variables using a sparse implementation based on 1709467b48Spatrick // the machine code SSA form. This class computes live variable information for 1809467b48Spatrick // each virtual and _register allocatable_ physical register in a function. It 1909467b48Spatrick // uses the dominance properties of SSA form to efficiently compute live 2009467b48Spatrick // variables for virtual registers, and assumes that physical registers are only 2109467b48Spatrick // live within a single basic block (allowing it to do a single local analysis 2209467b48Spatrick // to resolve physical register lifetimes in each basic block). If a physical 2309467b48Spatrick // register is not register allocatable, it is not tracked. This is useful for 2409467b48Spatrick // things like the stack pointer and condition codes. 2509467b48Spatrick // 2609467b48Spatrick //===----------------------------------------------------------------------===// 2709467b48Spatrick 2809467b48Spatrick #ifndef LLVM_CODEGEN_LIVEVARIABLES_H 2909467b48Spatrick #define LLVM_CODEGEN_LIVEVARIABLES_H 3009467b48Spatrick 3109467b48Spatrick #include "llvm/ADT/DenseMap.h" 3209467b48Spatrick #include "llvm/ADT/IndexedMap.h" 3309467b48Spatrick #include "llvm/ADT/SmallSet.h" 3409467b48Spatrick #include "llvm/ADT/SmallVector.h" 3509467b48Spatrick #include "llvm/ADT/SparseBitVector.h" 3609467b48Spatrick #include "llvm/CodeGen/MachineFunctionPass.h" 3709467b48Spatrick #include "llvm/CodeGen/MachineInstr.h" 3809467b48Spatrick #include "llvm/CodeGen/TargetRegisterInfo.h" 3909467b48Spatrick #include "llvm/InitializePasses.h" 40*d415bd75Srobert #include "llvm/PassRegistry.h" 4109467b48Spatrick 4209467b48Spatrick namespace llvm { 4309467b48Spatrick 4409467b48Spatrick class MachineBasicBlock; 4509467b48Spatrick class MachineRegisterInfo; 4609467b48Spatrick 4709467b48Spatrick class LiveVariables : public MachineFunctionPass { 4809467b48Spatrick public: 4909467b48Spatrick static char ID; // Pass identification, replacement for typeid LiveVariables()5009467b48Spatrick LiveVariables() : MachineFunctionPass(ID) { 5109467b48Spatrick initializeLiveVariablesPass(*PassRegistry::getPassRegistry()); 5209467b48Spatrick } 5309467b48Spatrick 5409467b48Spatrick /// VarInfo - This represents the regions where a virtual register is live in 5509467b48Spatrick /// the program. We represent this with three different pieces of 5609467b48Spatrick /// information: the set of blocks in which the instruction is live 5709467b48Spatrick /// throughout, the set of blocks in which the instruction is actually used, 5809467b48Spatrick /// and the set of non-phi instructions that are the last users of the value. 5909467b48Spatrick /// 6009467b48Spatrick /// In the common case where a value is defined and killed in the same block, 6109467b48Spatrick /// There is one killing instruction, and AliveBlocks is empty. 6209467b48Spatrick /// 6309467b48Spatrick /// Otherwise, the value is live out of the block. If the value is live 6409467b48Spatrick /// throughout any blocks, these blocks are listed in AliveBlocks. Blocks 6509467b48Spatrick /// where the liveness range ends are not included in AliveBlocks, instead 6609467b48Spatrick /// being captured by the Kills set. In these blocks, the value is live into 6709467b48Spatrick /// the block (unless the value is defined and killed in the same block) and 6809467b48Spatrick /// lives until the specified instruction. Note that there cannot ever be a 6909467b48Spatrick /// value whose Kills set contains two instructions from the same basic block. 7009467b48Spatrick /// 7109467b48Spatrick /// PHI nodes complicate things a bit. If a PHI node is the last user of a 7209467b48Spatrick /// value in one of its predecessor blocks, it is not listed in the kills set, 7309467b48Spatrick /// but does include the predecessor block in the AliveBlocks set (unless that 7409467b48Spatrick /// block also defines the value). This leads to the (perfectly sensical) 7509467b48Spatrick /// situation where a value is defined in a block, and the last use is a phi 7609467b48Spatrick /// node in the successor. In this case, AliveBlocks is empty (the value is 7709467b48Spatrick /// not live across any blocks) and Kills is empty (phi nodes are not 7809467b48Spatrick /// included). This is sensical because the value must be live to the end of 7909467b48Spatrick /// the block, but is not live in any successor blocks. 8009467b48Spatrick struct VarInfo { 8109467b48Spatrick /// AliveBlocks - Set of blocks in which this value is alive completely 8209467b48Spatrick /// through. This is a bit set which uses the basic block number as an 8309467b48Spatrick /// index. 8409467b48Spatrick /// 8509467b48Spatrick SparseBitVector<> AliveBlocks; 8609467b48Spatrick 8709467b48Spatrick /// Kills - List of MachineInstruction's which are the last use of this 8809467b48Spatrick /// virtual register (kill it) in their basic block. 8909467b48Spatrick /// 9009467b48Spatrick std::vector<MachineInstr*> Kills; 9109467b48Spatrick 9209467b48Spatrick /// removeKill - Delete a kill corresponding to the specified 9309467b48Spatrick /// machine instruction. Returns true if there was a kill 9409467b48Spatrick /// corresponding to this instruction, false otherwise. removeKillVarInfo9509467b48Spatrick bool removeKill(MachineInstr &MI) { 9609467b48Spatrick std::vector<MachineInstr *>::iterator I = find(Kills, &MI); 9709467b48Spatrick if (I == Kills.end()) 9809467b48Spatrick return false; 9909467b48Spatrick Kills.erase(I); 10009467b48Spatrick return true; 10109467b48Spatrick } 10209467b48Spatrick 10309467b48Spatrick /// findKill - Find a kill instruction in MBB. Return NULL if none is found. 10409467b48Spatrick MachineInstr *findKill(const MachineBasicBlock *MBB) const; 10509467b48Spatrick 10609467b48Spatrick /// isLiveIn - Is Reg live in to MBB? This means that Reg is live through 10709467b48Spatrick /// MBB, or it is killed in MBB. If Reg is only used by PHI instructions in 10809467b48Spatrick /// MBB, it is not considered live in. 10973471bf0Spatrick bool isLiveIn(const MachineBasicBlock &MBB, Register Reg, 11009467b48Spatrick MachineRegisterInfo &MRI); 11109467b48Spatrick 11209467b48Spatrick void dump() const; 11309467b48Spatrick }; 11409467b48Spatrick 11509467b48Spatrick private: 11609467b48Spatrick /// VirtRegInfo - This list is a mapping from virtual register number to 11709467b48Spatrick /// variable information. 11809467b48Spatrick /// 11909467b48Spatrick IndexedMap<VarInfo, VirtReg2IndexFunctor> VirtRegInfo; 12009467b48Spatrick 12109467b48Spatrick /// PHIJoins - list of virtual registers that are PHI joins. These registers 12209467b48Spatrick /// may have multiple definitions, and they require special handling when 12309467b48Spatrick /// building live intervals. 12409467b48Spatrick SparseBitVector<> PHIJoins; 12509467b48Spatrick 12609467b48Spatrick private: // Intermediate data structures 12709467b48Spatrick MachineFunction *MF; 12809467b48Spatrick 12909467b48Spatrick MachineRegisterInfo* MRI; 13009467b48Spatrick 13109467b48Spatrick const TargetRegisterInfo *TRI; 13209467b48Spatrick 13309467b48Spatrick // PhysRegInfo - Keep track of which instruction was the last def of a 13409467b48Spatrick // physical register. This is a purely local property, because all physical 13509467b48Spatrick // register references are presumed dead across basic blocks. 13609467b48Spatrick std::vector<MachineInstr *> PhysRegDef; 13709467b48Spatrick 13809467b48Spatrick // PhysRegInfo - Keep track of which instruction was the last use of a 13909467b48Spatrick // physical register. This is a purely local property, because all physical 14009467b48Spatrick // register references are presumed dead across basic blocks. 14109467b48Spatrick std::vector<MachineInstr *> PhysRegUse; 14209467b48Spatrick 14309467b48Spatrick std::vector<SmallVector<unsigned, 4>> PHIVarInfo; 14409467b48Spatrick 14509467b48Spatrick // DistanceMap - Keep track the distance of a MI from the start of the 14609467b48Spatrick // current basic block. 14709467b48Spatrick DenseMap<MachineInstr*, unsigned> DistanceMap; 14809467b48Spatrick 14909467b48Spatrick /// HandlePhysRegKill - Add kills of Reg and its sub-registers to the 15009467b48Spatrick /// uses. Pay special attention to the sub-register uses which may come below 15109467b48Spatrick /// the last use of the whole register. 15273471bf0Spatrick bool HandlePhysRegKill(Register Reg, MachineInstr *MI); 15309467b48Spatrick 15409467b48Spatrick /// HandleRegMask - Call HandlePhysRegKill for all registers clobbered by Mask. 15509467b48Spatrick void HandleRegMask(const MachineOperand&); 15609467b48Spatrick 15773471bf0Spatrick void HandlePhysRegUse(Register Reg, MachineInstr &MI); 15873471bf0Spatrick void HandlePhysRegDef(Register Reg, MachineInstr *MI, 15909467b48Spatrick SmallVectorImpl<unsigned> &Defs); 16009467b48Spatrick void UpdatePhysRegDefs(MachineInstr &MI, SmallVectorImpl<unsigned> &Defs); 16109467b48Spatrick 16209467b48Spatrick /// FindLastRefOrPartRef - Return the last reference or partial reference of 16309467b48Spatrick /// the specified register. 16473471bf0Spatrick MachineInstr *FindLastRefOrPartRef(Register Reg); 16509467b48Spatrick 16609467b48Spatrick /// FindLastPartialDef - Return the last partial def of the specified 16709467b48Spatrick /// register. Also returns the sub-registers that're defined by the 16809467b48Spatrick /// instruction. 16973471bf0Spatrick MachineInstr *FindLastPartialDef(Register Reg, 17009467b48Spatrick SmallSet<unsigned, 4> &PartDefRegs); 17109467b48Spatrick 17209467b48Spatrick /// analyzePHINodes - Gather information about the PHI nodes in here. In 17309467b48Spatrick /// particular, we want to map the variable information of a virtual 17409467b48Spatrick /// register which is used in a PHI node. We map that to the BB the vreg 17509467b48Spatrick /// is coming from. 17609467b48Spatrick void analyzePHINodes(const MachineFunction& Fn); 17709467b48Spatrick 17809467b48Spatrick void runOnInstr(MachineInstr &MI, SmallVectorImpl<unsigned> &Defs); 17909467b48Spatrick 18009467b48Spatrick void runOnBlock(MachineBasicBlock *MBB, unsigned NumRegs); 18109467b48Spatrick public: 18209467b48Spatrick 18309467b48Spatrick bool runOnMachineFunction(MachineFunction &MF) override; 18409467b48Spatrick 18509467b48Spatrick /// RegisterDefIsDead - Return true if the specified instruction defines the 18609467b48Spatrick /// specified register, but that definition is dead. 18773471bf0Spatrick bool RegisterDefIsDead(MachineInstr &MI, Register Reg) const; 18809467b48Spatrick 18909467b48Spatrick //===--------------------------------------------------------------------===// 19009467b48Spatrick // API to update live variable information 19109467b48Spatrick 192*d415bd75Srobert /// Recompute liveness from scratch for a virtual register \p Reg that is 193*d415bd75Srobert /// known to have a single def that dominates all uses. This can be useful 194*d415bd75Srobert /// after removing some uses of \p Reg. It is not necessary for the whole 195*d415bd75Srobert /// machine function to be in SSA form. 196*d415bd75Srobert void recomputeForSingleDefVirtReg(Register Reg); 197*d415bd75Srobert 19809467b48Spatrick /// replaceKillInstruction - Update register kill info by replacing a kill 19909467b48Spatrick /// instruction with a new one. 20073471bf0Spatrick void replaceKillInstruction(Register Reg, MachineInstr &OldMI, 20109467b48Spatrick MachineInstr &NewMI); 20209467b48Spatrick 20309467b48Spatrick /// addVirtualRegisterKilled - Add information about the fact that the 20409467b48Spatrick /// specified register is killed after being used by the specified 20509467b48Spatrick /// instruction. If AddIfNotFound is true, add a implicit operand if it's 20609467b48Spatrick /// not found. 20773471bf0Spatrick void addVirtualRegisterKilled(Register IncomingReg, MachineInstr &MI, 20809467b48Spatrick bool AddIfNotFound = false) { 20909467b48Spatrick if (MI.addRegisterKilled(IncomingReg, TRI, AddIfNotFound)) 21009467b48Spatrick getVarInfo(IncomingReg).Kills.push_back(&MI); 21109467b48Spatrick } 21209467b48Spatrick 21309467b48Spatrick /// removeVirtualRegisterKilled - Remove the specified kill of the virtual 21409467b48Spatrick /// register from the live variable information. Returns true if the 21509467b48Spatrick /// variable was marked as killed by the specified instruction, 21609467b48Spatrick /// false otherwise. removeVirtualRegisterKilled(Register Reg,MachineInstr & MI)21773471bf0Spatrick bool removeVirtualRegisterKilled(Register Reg, MachineInstr &MI) { 21873471bf0Spatrick if (!getVarInfo(Reg).removeKill(MI)) 21909467b48Spatrick return false; 22009467b48Spatrick 22109467b48Spatrick bool Removed = false; 222*d415bd75Srobert for (MachineOperand &MO : MI.operands()) { 22373471bf0Spatrick if (MO.isReg() && MO.isKill() && MO.getReg() == Reg) { 22409467b48Spatrick MO.setIsKill(false); 22509467b48Spatrick Removed = true; 22609467b48Spatrick break; 22709467b48Spatrick } 22809467b48Spatrick } 22909467b48Spatrick 23009467b48Spatrick assert(Removed && "Register is not used by this instruction!"); 23109467b48Spatrick (void)Removed; 23209467b48Spatrick return true; 23309467b48Spatrick } 23409467b48Spatrick 23509467b48Spatrick /// removeVirtualRegistersKilled - Remove all killed info for the specified 23609467b48Spatrick /// instruction. 23709467b48Spatrick void removeVirtualRegistersKilled(MachineInstr &MI); 23809467b48Spatrick 23909467b48Spatrick /// addVirtualRegisterDead - Add information about the fact that the specified 24009467b48Spatrick /// register is dead after being used by the specified instruction. If 24109467b48Spatrick /// AddIfNotFound is true, add a implicit operand if it's not found. 24273471bf0Spatrick void addVirtualRegisterDead(Register IncomingReg, MachineInstr &MI, 24309467b48Spatrick bool AddIfNotFound = false) { 24409467b48Spatrick if (MI.addRegisterDead(IncomingReg, TRI, AddIfNotFound)) 24509467b48Spatrick getVarInfo(IncomingReg).Kills.push_back(&MI); 24609467b48Spatrick } 24709467b48Spatrick 24809467b48Spatrick /// removeVirtualRegisterDead - Remove the specified kill of the virtual 24909467b48Spatrick /// register from the live variable information. Returns true if the 25009467b48Spatrick /// variable was marked dead at the specified instruction, false 25109467b48Spatrick /// otherwise. removeVirtualRegisterDead(Register Reg,MachineInstr & MI)25273471bf0Spatrick bool removeVirtualRegisterDead(Register Reg, MachineInstr &MI) { 25373471bf0Spatrick if (!getVarInfo(Reg).removeKill(MI)) 25409467b48Spatrick return false; 25509467b48Spatrick 25609467b48Spatrick bool Removed = false; 257*d415bd75Srobert for (MachineOperand &MO : MI.operands()) { 25873471bf0Spatrick if (MO.isReg() && MO.isDef() && MO.getReg() == Reg) { 25909467b48Spatrick MO.setIsDead(false); 26009467b48Spatrick Removed = true; 26109467b48Spatrick break; 26209467b48Spatrick } 26309467b48Spatrick } 26409467b48Spatrick assert(Removed && "Register is not defined by this instruction!"); 26509467b48Spatrick (void)Removed; 26609467b48Spatrick return true; 26709467b48Spatrick } 26809467b48Spatrick 26909467b48Spatrick void getAnalysisUsage(AnalysisUsage &AU) const override; 27009467b48Spatrick releaseMemory()27109467b48Spatrick void releaseMemory() override { 27209467b48Spatrick VirtRegInfo.clear(); 27309467b48Spatrick } 27409467b48Spatrick 27509467b48Spatrick /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL 27609467b48Spatrick /// register. 27773471bf0Spatrick VarInfo &getVarInfo(Register Reg); 27809467b48Spatrick 27909467b48Spatrick void MarkVirtRegAliveInBlock(VarInfo& VRInfo, MachineBasicBlock* DefBlock, 28009467b48Spatrick MachineBasicBlock *BB); 28109467b48Spatrick void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *DefBlock, 28209467b48Spatrick MachineBasicBlock *BB, 28373471bf0Spatrick SmallVectorImpl<MachineBasicBlock *> &WorkList); 28409467b48Spatrick 28573471bf0Spatrick void HandleVirtRegDef(Register reg, MachineInstr &MI); 28673471bf0Spatrick void HandleVirtRegUse(Register reg, MachineBasicBlock *MBB, MachineInstr &MI); 28773471bf0Spatrick isLiveIn(Register Reg,const MachineBasicBlock & MBB)28873471bf0Spatrick bool isLiveIn(Register Reg, const MachineBasicBlock &MBB) { 28909467b48Spatrick return getVarInfo(Reg).isLiveIn(MBB, Reg, *MRI); 29009467b48Spatrick } 29109467b48Spatrick 29209467b48Spatrick /// isLiveOut - Determine if Reg is live out from MBB, when not considering 29309467b48Spatrick /// PHI nodes. This means that Reg is either killed by a successor block or 29409467b48Spatrick /// passed through one. 29573471bf0Spatrick bool isLiveOut(Register Reg, const MachineBasicBlock &MBB); 29609467b48Spatrick 29709467b48Spatrick /// addNewBlock - Add a new basic block BB between DomBB and SuccBB. All 29809467b48Spatrick /// variables that are live out of DomBB and live into SuccBB will be marked 29909467b48Spatrick /// as passing live through BB. This method assumes that the machine code is 30009467b48Spatrick /// still in SSA form. 30109467b48Spatrick void addNewBlock(MachineBasicBlock *BB, 30209467b48Spatrick MachineBasicBlock *DomBB, 30309467b48Spatrick MachineBasicBlock *SuccBB); 30409467b48Spatrick 305097a140dSpatrick void addNewBlock(MachineBasicBlock *BB, 306097a140dSpatrick MachineBasicBlock *DomBB, 307097a140dSpatrick MachineBasicBlock *SuccBB, 308097a140dSpatrick std::vector<SparseBitVector<>> &LiveInSets); 309097a140dSpatrick 31009467b48Spatrick /// isPHIJoin - Return true if Reg is a phi join register. isPHIJoin(Register Reg)31173471bf0Spatrick bool isPHIJoin(Register Reg) { return PHIJoins.test(Reg.id()); } 31209467b48Spatrick 31309467b48Spatrick /// setPHIJoin - Mark Reg as a phi join register. setPHIJoin(Register Reg)31473471bf0Spatrick void setPHIJoin(Register Reg) { PHIJoins.set(Reg.id()); } 31509467b48Spatrick }; 31609467b48Spatrick 31709467b48Spatrick } // End llvm namespace 31809467b48Spatrick 31909467b48Spatrick #endif 320