1 //===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- C++ -*-===//
3 // This file implements the LiveVariable analysis pass. For each machine
4 // instruction in the function, this pass calculates the set of registers that
5 // are immediately dead after the instruction (i.e., the instruction calculates
6 // the value, but it is never used) and the set of registers that are used by
7 // the instruction, but are never used after the instruction (i.e., they are
10 // This class computes live variables using are sparse implementation based on
11 // the machine code SSA form. This class computes live variable information for
12 // each virtual and _register allocatable_ physical register in a function. It
13 // uses the dominance properties of SSA form to efficiently compute live
14 // variables for virtual registers, and assumes that physical registers are only
15 // live within a single basic block (allowing it to do a single local analysis
16 // to resolve physical register lifetimes in each basic block). If a physical
17 // register is not register allocatable, it is not tracked. This is useful for
18 // things like the stack pointer and condition codes.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_CODEGEN_LIVEVARIABLES_H
23 #define LLVM_CODEGEN_LIVEVARIABLES_H
25 #include "llvm/CodeGen/MachineFunctionPass.h"
30 class LiveVariables : public MachineFunctionPass {
32 /// DefBlock - The basic block which defines this value...
33 MachineBasicBlock *DefBlock;
34 MachineInstr *DefInst;
36 /// AliveBlocks - Set of blocks of which this value is alive completely
37 /// through. This is a bit set which uses the basic block number as an
40 std::vector<bool> AliveBlocks;
42 /// Kills - List of MachineBasicblock's which contain the last use of this
43 /// virtual register (kill it). This also includes the specific instruction
44 /// which kills the value.
46 std::vector<std::pair<MachineBasicBlock*, MachineInstr*> > Kills;
48 VarInfo() : DefBlock(0), DefInst(0) {}
51 /// VirtRegInfo - This list is a mapping from virtual register number to
52 /// variable information. FirstVirtualRegister is subtracted from the virtual
53 /// register number before indexing into this list.
55 std::vector<VarInfo> VirtRegInfo;
57 /// RegistersKilled - This multimap keeps track of all of the registers that
58 /// are dead immediately after an instruction reads its operands. If an
59 /// instruction does not have an entry in this map, it kills no registers.
61 std::multimap<MachineInstr*, unsigned> RegistersKilled;
63 /// RegistersDead - This multimap keeps track of all of the registers that are
64 /// dead immediately after an instruction executes, which are not dead after
65 /// the operands are evaluated. In practice, this only contains registers
66 /// which are defined by an instruction, but never used.
68 std::multimap<MachineInstr*, unsigned> RegistersDead;
70 /// AllocatablePhysicalRegisters - This vector keeps track of which registers
71 /// are actually register allocatable by the target machine. We can not track
72 /// liveness for values that are not in this set.
74 std::vector<bool> AllocatablePhysicalRegisters;
75 private: // Intermediate data structures
77 /// BBMap - Maps LLVM basic blocks to their corresponding machine basic block.
78 /// This also provides a numbering of the basic blocks in the function.
79 std::map<const BasicBlock*, std::pair<MachineBasicBlock*, unsigned> > BBMap;
81 const MRegisterInfo *RegInfo;
83 MachineInstr **PhysRegInfo;
88 virtual bool runOnMachineFunction(MachineFunction &MF);
90 /// killed_iterator - Iterate over registers killed by a machine instruction
92 typedef std::multimap<MachineInstr*,
93 unsigned>::const_iterator killed_iterator;
95 /// killed_begin/end - Get access to the range of registers killed by a
96 /// machine instruction.
97 killed_iterator killed_begin(MachineInstr *MI) const {
98 return RegistersKilled.lower_bound(MI);
100 killed_iterator killed_end(MachineInstr *MI) const {
101 return RegistersKilled.upper_bound(MI);
104 killed_iterator dead_begin(MachineInstr *MI) const {
105 return RegistersDead.lower_bound(MI);
107 killed_iterator dead_end(MachineInstr *MI) const {
108 return RegistersDead.upper_bound(MI);
111 /// addVirtualRegisterKill - Add information about the fact that the specified
112 /// register is dead after being used by the specified instruction.
114 void addVirtualRegisterKill(unsigned IncomingReg, MachineInstr *MI) {
115 RegistersDead.insert(std::make_pair(MI, IncomingReg));
118 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
119 AU.setPreservesAll();
122 virtual void releaseMemory() {
124 RegistersKilled.clear();
125 RegistersDead.clear();
128 VarInfo &getVarInfo(unsigned RegIdx) {
129 if (RegIdx >= VirtRegInfo.size()) {
130 if (RegIdx >= 2*VirtRegInfo.size())
131 VirtRegInfo.resize(RegIdx*2);
133 VirtRegInfo.resize(2*VirtRegInfo.size());
135 return VirtRegInfo[RegIdx];
138 void MarkVirtRegAliveInBlock(VarInfo &VRInfo, const BasicBlock *BB);
139 void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
141 void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
142 void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);