1200581Srdivacky//===-- MachineSSAUpdater.h - Unstructured SSA Update Tool ------*- C++ -*-===//
2200581Srdivacky//
3200581Srdivacky//                     The LLVM Compiler Infrastructure
4200581Srdivacky//
5200581Srdivacky// This file is distributed under the University of Illinois Open Source
6200581Srdivacky// License. See LICENSE.TXT for details.
7200581Srdivacky//
8200581Srdivacky//===----------------------------------------------------------------------===//
9200581Srdivacky//
10200581Srdivacky// This file declares the MachineSSAUpdater class.
11200581Srdivacky//
12200581Srdivacky//===----------------------------------------------------------------------===//
13200581Srdivacky
14200581Srdivacky#ifndef LLVM_CODEGEN_MACHINESSAUPDATER_H
15200581Srdivacky#define LLVM_CODEGEN_MACHINESSAUPDATER_H
16200581Srdivacky
17245431Sdim#include "llvm/Support/Compiler.h"
18245431Sdim
19200581Srdivackynamespace llvm {
20200581Srdivacky  class MachineBasicBlock;
21200581Srdivacky  class MachineFunction;
22200581Srdivacky  class MachineInstr;
23200581Srdivacky  class MachineOperand;
24200581Srdivacky  class MachineRegisterInfo;
25200581Srdivacky  class TargetInstrInfo;
26200581Srdivacky  class TargetRegisterClass;
27200581Srdivacky  template<typename T> class SmallVectorImpl;
28208599Srdivacky  template<typename T> class SSAUpdaterTraits;
29207618Srdivacky  class BumpPtrAllocator;
30200581Srdivacky
31200581Srdivacky/// MachineSSAUpdater - This class updates SSA form for a set of virtual
32200581Srdivacky/// registers defined in multiple blocks.  This is used when code duplication
33200581Srdivacky/// or another unstructured transformation wants to rewrite a set of uses of one
34200581Srdivacky/// vreg with uses of a set of vregs.
35200581Srdivackyclass MachineSSAUpdater {
36208599Srdivacky  friend class SSAUpdaterTraits<MachineSSAUpdater>;
37207618Srdivacky
38207618Srdivackyprivate:
39200581Srdivacky  /// AvailableVals - This keeps track of which value to use on a per-block
40200581Srdivacky  /// basis.  When we insert PHI nodes, we keep track of them here.
41200581Srdivacky  //typedef DenseMap<MachineBasicBlock*, unsigned > AvailableValsTy;
42200581Srdivacky  void *AV;
43200581Srdivacky
44200581Srdivacky  /// VR - Current virtual register whose uses are being updated.
45200581Srdivacky  unsigned VR;
46200581Srdivacky
47200581Srdivacky  /// VRC - Register class of the current virtual register.
48200581Srdivacky  const TargetRegisterClass *VRC;
49200581Srdivacky
50200581Srdivacky  /// InsertedPHIs - If this is non-null, the MachineSSAUpdater adds all PHI
51200581Srdivacky  /// nodes that it creates to the vector.
52200581Srdivacky  SmallVectorImpl<MachineInstr*> *InsertedPHIs;
53200581Srdivacky
54200581Srdivacky  const TargetInstrInfo *TII;
55200581Srdivacky  MachineRegisterInfo *MRI;
56200581Srdivackypublic:
57200581Srdivacky  /// MachineSSAUpdater constructor.  If InsertedPHIs is specified, it will be
58200581Srdivacky  /// filled in with all PHI Nodes created by rewriting.
59200581Srdivacky  explicit MachineSSAUpdater(MachineFunction &MF,
60200581Srdivacky                             SmallVectorImpl<MachineInstr*> *InsertedPHIs = 0);
61200581Srdivacky  ~MachineSSAUpdater();
62200581Srdivacky
63200581Srdivacky  /// Initialize - Reset this object to get ready for a new set of SSA
64200581Srdivacky  /// updates.
65200581Srdivacky  void Initialize(unsigned V);
66200581Srdivacky
67200581Srdivacky  /// AddAvailableValue - Indicate that a rewritten value is available at the
68200581Srdivacky  /// end of the specified block with the specified value.
69200581Srdivacky  void AddAvailableValue(MachineBasicBlock *BB, unsigned V);
70200581Srdivacky
71200581Srdivacky  /// HasValueForBlock - Return true if the MachineSSAUpdater already has a
72200581Srdivacky  /// value for the specified block.
73200581Srdivacky  bool HasValueForBlock(MachineBasicBlock *BB) const;
74200581Srdivacky
75200581Srdivacky  /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
76200581Srdivacky  /// live at the end of the specified block.
77200581Srdivacky  unsigned GetValueAtEndOfBlock(MachineBasicBlock *BB);
78200581Srdivacky
79200581Srdivacky  /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
80200581Srdivacky  /// is live in the middle of the specified block.
81200581Srdivacky  ///
82200581Srdivacky  /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
83200581Srdivacky  /// important case: if there is a definition of the rewritten value after the
84200581Srdivacky  /// 'use' in BB.  Consider code like this:
85200581Srdivacky  ///
86200581Srdivacky  ///      X1 = ...
87200581Srdivacky  ///   SomeBB:
88200581Srdivacky  ///      use(X)
89200581Srdivacky  ///      X2 = ...
90200581Srdivacky  ///      br Cond, SomeBB, OutBB
91200581Srdivacky  ///
92200581Srdivacky  /// In this case, there are two values (X1 and X2) added to the AvailableVals
93200581Srdivacky  /// set by the client of the rewriter, and those values are both live out of
94200581Srdivacky  /// their respective blocks.  However, the use of X happens in the *middle* of
95200581Srdivacky  /// a block.  Because of this, we need to insert a new PHI node in SomeBB to
96200581Srdivacky  /// merge the appropriate values, and this value isn't live out of the block.
97200581Srdivacky  ///
98200581Srdivacky  unsigned GetValueInMiddleOfBlock(MachineBasicBlock *BB);
99200581Srdivacky
100200581Srdivacky  /// RewriteUse - Rewrite a use of the symbolic value.  This handles PHI nodes,
101200581Srdivacky  /// which use their value in the corresponding predecessor.  Note that this
102200581Srdivacky  /// will not work if the use is supposed to be rewritten to a value defined in
103200581Srdivacky  /// the same block as the use, but above it.  Any 'AddAvailableValue's added
104200581Srdivacky  /// for the use's block will be considered to be below it.
105200581Srdivacky  void RewriteUse(MachineOperand &U);
106200581Srdivacky
107200581Srdivackyprivate:
108200581Srdivacky  void ReplaceRegWith(unsigned OldReg, unsigned NewReg);
109200581Srdivacky  unsigned GetValueAtEndOfBlockInternal(MachineBasicBlock *BB);
110207618Srdivacky
111245431Sdim  void operator=(const MachineSSAUpdater&) LLVM_DELETED_FUNCTION;
112245431Sdim  MachineSSAUpdater(const MachineSSAUpdater&) LLVM_DELETED_FUNCTION;
113200581Srdivacky};
114200581Srdivacky
115200581Srdivacky} // End llvm namespace
116200581Srdivacky
117200581Srdivacky#endif
118