1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- C++ -*-===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file implements the LiveVariables analysis pass.  For each machine
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// instruction in the function, this pass calculates the set of registers that
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// are immediately dead after the instruction (i.e., the instruction calculates
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the value, but it is never used) and the set of registers that are used by
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the instruction, but are never used after the instruction (i.e., they are
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// killed).
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This class computes live variables using a sparse implementation based on
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// the machine code SSA form.  This class computes live variable information for
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// each virtual and _register allocatable_ physical register in a function.  It
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// uses the dominance properties of SSA form to efficiently compute live
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// variables for virtual registers, and assumes that physical registers are only
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// live within a single basic block (allowing it to do a single local analysis
23894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// to resolve physical register lifetimes in each basic block).  If a physical
24894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// register is not register allocatable, it is not tracked.  This is useful for
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// things like the stack pointer and condition codes.
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#ifndef LLVM_CODEGEN_LIVEVARIABLES_H
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#define LLVM_CODEGEN_LIVEVARIABLES_H
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineBasicBlock.h"
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineFunctionPass.h"
34894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/CodeGen/MachineInstr.h"
3519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Target/TargetRegisterInfo.h"
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/BitVector.h"
37894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/DenseMap.h"
3819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/ADT/IndexedMap.h"
39894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallSet.h"
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SmallVector.h"
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/ADT/SparseBitVector.h"
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace llvm {
44894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass MachineRegisterInfo;
46894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass TargetRegisterInfo;
47894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass LiveVariables : public MachineFunctionPass {
49894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  static char ID; // Pass identification, replacement for typeid
5119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  LiveVariables() : MachineFunctionPass(ID) {
5219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    initializeLiveVariablesPass(*PassRegistry::getPassRegistry());
5319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  }
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// VarInfo - This represents the regions where a virtual register is live in
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the program.  We represent this with three different pieces of
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// information: the set of blocks in which the instruction is live
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// throughout, the set of blocks in which the instruction is actually used,
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// and the set of non-phi instructions that are the last users of the value.
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// In the common case where a value is defined and killed in the same block,
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// There is one killing instruction, and AliveBlocks is empty.
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// Otherwise, the value is live out of the block.  If the value is live
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// throughout any blocks, these blocks are listed in AliveBlocks.  Blocks
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// where the liveness range ends are not included in AliveBlocks, instead
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// being captured by the Kills set.  In these blocks, the value is live into
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the block (unless the value is defined and killed in the same block) and
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// lives until the specified instruction.  Note that there cannot ever be a
70894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// value whose Kills set contains two instructions from the same basic block.
71894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// PHI nodes complicate things a bit.  If a PHI node is the last user of a
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// value in one of its predecessor blocks, it is not listed in the kills set,
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// but does include the predecessor block in the AliveBlocks set (unless that
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// block also defines the value).  This leads to the (perfectly sensical)
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// situation where a value is defined in a block, and the last use is a phi
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// node in the successor.  In this case, AliveBlocks is empty (the value is
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// not live across any  blocks) and Kills is empty (phi nodes are not
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// included). This is sensical because the value must be live to the end of
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the block, but is not live in any successor blocks.
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  struct VarInfo {
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// AliveBlocks - Set of blocks in which this value is alive completely
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// through.  This is a bit set which uses the basic block number as an
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// index.
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ///
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SparseBitVector<> AliveBlocks;
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// NumUses - Number of uses of this register across the entire function.
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ///
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    unsigned NumUses;
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// Kills - List of MachineInstruction's which are the last use of this
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// virtual register (kill it) in their basic block.
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ///
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    std::vector<MachineInstr*> Kills;
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    VarInfo() : NumUses(0) {}
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
99894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// removeKill - Delete a kill corresponding to the specified
100894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// machine instruction. Returns true if there was a kill
101894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// corresponding to this instruction, false otherwise.
102894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool removeKill(MachineInstr *MI) {
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      std::vector<MachineInstr*>::iterator
104894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        I = std::find(Kills.begin(), Kills.end(), MI);
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (I == Kills.end())
106894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        return false;
107894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      Kills.erase(I);
108894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return true;
109894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
110894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
111894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// findKill - Find a kill instruction in MBB. Return NULL if none is found.
112894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MachineInstr *findKill(const MachineBasicBlock *MBB) const;
113894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
114894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// isLiveIn - Is Reg live in to MBB? This means that Reg is live through
115894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// MBB, or it is killed in MBB. If Reg is only used by PHI instructions in
116894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    /// MBB, it is not considered live in.
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool isLiveIn(const MachineBasicBlock &MBB,
118894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  unsigned Reg,
119894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  MachineRegisterInfo &MRI);
120894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
121894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    void dump() const;
122894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  };
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
125894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// VirtRegInfo - This list is a mapping from virtual register number to
12619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  /// variable information.
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
12819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  IndexedMap<VarInfo, VirtReg2IndexFunctor> VirtRegInfo;
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
130894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// PHIJoins - list of virtual registers that are PHI joins. These registers
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// may have multiple definitions, and they require special handling when
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// building live intervals.
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SparseBitVector<> PHIJoins;
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// ReservedRegisters - This vector keeps track of which registers
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// are reserved register which are not allocatable by the target machine.
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// We can not track liveness for values that are in this set.
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  ///
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  BitVector ReservedRegisters;
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:   // Intermediate data structures
142894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineFunction *MF;
143894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
144894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineRegisterInfo* MRI;
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
146894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  const TargetRegisterInfo *TRI;
147894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
148894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // PhysRegInfo - Keep track of which instruction was the last def of a
149894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // physical register. This is a purely local property, because all physical
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // register references are presumed dead across basic blocks.
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr **PhysRegDef;
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
153894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // PhysRegInfo - Keep track of which instruction was the last use of a
154894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // physical register. This is a purely local property, because all physical
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // register references are presumed dead across basic blocks.
156894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr **PhysRegUse;
157894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
158894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<unsigned, 4> *PHIVarInfo;
159894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // DistanceMap - Keep track the distance of a MI from the start of the
161894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // current basic block.
162894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DenseMap<MachineInstr*, unsigned> DistanceMap;
163894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
164894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// HandlePhysRegKill - Add kills of Reg and its sub-registers to the
165894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// uses. Pay special attention to the sub-register uses which may come below
166894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the last use of the whole register.
167894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool HandlePhysRegKill(unsigned Reg, MachineInstr *MI);
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void HandlePhysRegDef(unsigned Reg, MachineInstr *MI,
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        SmallVector<unsigned, 4> &Defs);
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void UpdatePhysRegDefs(MachineInstr *MI, SmallVector<unsigned, 4> &Defs);
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// FindLastRefOrPartRef - Return the last reference or partial reference of
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// the specified register.
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr *FindLastRefOrPartRef(unsigned Reg);
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// FindLastPartialDef - Return the last partial def of the specified
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// register. Also returns the sub-registers that're defined by the
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// instruction.
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MachineInstr *FindLastPartialDef(unsigned Reg,
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   SmallSet<unsigned,4> &PartDefRegs);
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// analyzePHINodes - Gather information about the PHI nodes in here. In
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// particular, we want to map the variable information of a virtual
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// register which is used in a PHI node. We map that to the BB the vreg
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// is coming from.
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void analyzePHINodes(const MachineFunction& Fn);
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual bool runOnMachineFunction(MachineFunction &MF);
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// RegisterDefIsDead - Return true if the specified instruction defines the
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// specified register, but that definition is dead.
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const;
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //===--------------------------------------------------------------------===//
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //  API to update live variable information
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// replaceKillInstruction - Update register kill info by replacing a kill
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// instruction with a new one.
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void replaceKillInstruction(unsigned Reg, MachineInstr *OldMI,
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              MachineInstr *NewMI);
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// addVirtualRegisterKilled - Add information about the fact that the
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// specified register is killed after being used by the specified
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// instruction. If AddIfNotFound is true, add a implicit operand if it's
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// not found.
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI,
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                bool AddIfNotFound = false) {
211894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MI->addRegisterKilled(IncomingReg, TRI, AddIfNotFound))
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      getVarInfo(IncomingReg).Kills.push_back(MI);
213894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// removeVirtualRegisterKilled - Remove the specified kill of the virtual
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// register from the live variable information. Returns true if the
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// variable was marked as killed by the specified instruction,
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// false otherwise.
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool removeVirtualRegisterKilled(unsigned reg, MachineInstr *MI) {
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!getVarInfo(reg).removeKill(MI))
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool Removed = false;
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineOperand &MO = MI->getOperand(i);
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MO.isReg() && MO.isKill() && MO.getReg() == reg) {
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MO.setIsKill(false);
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Removed = true;
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(Removed && "Register is not used by this instruction!");
23419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    (void)Removed;
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// removeVirtualRegistersKilled - Remove all killed info for the specified
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// instruction.
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void removeVirtualRegistersKilled(MachineInstr *MI);
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// addVirtualRegisterDead - Add information about the fact that the specified
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// register is dead after being used by the specified instruction. If
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// AddIfNotFound is true, add a implicit operand if it's not found.
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI,
246894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              bool AddIfNotFound = false) {
247894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (MI->addRegisterDead(IncomingReg, TRI, AddIfNotFound))
248894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      getVarInfo(IncomingReg).Kills.push_back(MI);
249894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// removeVirtualRegisterDead - Remove the specified kill of the virtual
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// register from the live variable information. Returns true if the
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// variable was marked dead at the specified instruction, false
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// otherwise.
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool removeVirtualRegisterDead(unsigned reg, MachineInstr *MI) {
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (!getVarInfo(reg).removeKill(MI))
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      return false;
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    bool Removed = false;
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      MachineOperand &MO = MI->getOperand(i);
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MO.isReg() && MO.isDef() && MO.getReg() == reg) {
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        MO.setIsDead(false);
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        Removed = true;
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        break;
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      }
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(Removed && "Register is not defined by this instruction!");
26919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    (void)Removed;
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return true;
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void getAnalysisUsage(AnalysisUsage &AU) const;
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void releaseMemory() {
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    VirtRegInfo.clear();
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// register.
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  VarInfo &getVarInfo(unsigned RegIdx);
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void MarkVirtRegAliveInBlock(VarInfo& VRInfo, MachineBasicBlock* DefBlock,
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               MachineBasicBlock *BB);
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void MarkVirtRegAliveInBlock(VarInfo& VRInfo, MachineBasicBlock* DefBlock,
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               MachineBasicBlock *BB,
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                               std::vector<MachineBasicBlock*> &WorkList);
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void HandleVirtRegDef(unsigned reg, MachineInstr *MI);
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB,
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                        MachineInstr *MI);
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isLiveIn(unsigned Reg, const MachineBasicBlock &MBB) {
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return getVarInfo(Reg).isLiveIn(MBB, Reg, *MRI);
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// isLiveOut - Determine if Reg is live out from MBB, when not considering
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// PHI nodes. This means that Reg is either killed by a successor block or
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// passed through one.
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isLiveOut(unsigned Reg, const MachineBasicBlock &MBB);
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// addNewBlock - Add a new basic block BB between DomBB and SuccBB. All
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// variables that are live out of DomBB and live into SuccBB will be marked
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// as passing live through BB. This method assumes that the machine code is
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// still in SSA form.
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void addNewBlock(MachineBasicBlock *BB,
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                   MachineBasicBlock *DomBB,
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                   MachineBasicBlock *SuccBB);
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// isPHIJoin - Return true if Reg is a phi join register.
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  bool isPHIJoin(unsigned Reg) { return PHIJoins.test(Reg); }
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// setPHIJoin - Mark Reg as a phi join register.
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  void setPHIJoin(unsigned Reg) { PHIJoins.set(Reg); }
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // End llvm namespace
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#endif
319