1 //===- MachineSSAUpdater.h - Unstructured SSA Update Tool -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the MachineSSAUpdater class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CODEGEN_MACHINESSAUPDATER_H
14 #define LLVM_CODEGEN_MACHINESSAUPDATER_H
15 
16 #include "llvm/CodeGen/Register.h"
17 
18 namespace llvm {
19 
20 class MachineBasicBlock;
21 class MachineFunction;
22 class MachineInstr;
23 class MachineOperand;
24 class MachineRegisterInfo;
25 class TargetInstrInfo;
26 class TargetRegisterClass;
27 template<typename T> class SmallVectorImpl;
28 template<typename T> class SSAUpdaterTraits;
29 
30 /// MachineSSAUpdater - This class updates SSA form for a set of virtual
31 /// registers defined in multiple blocks.  This is used when code duplication
32 /// or another unstructured transformation wants to rewrite a set of uses of one
33 /// vreg with uses of a set of vregs.
34 class MachineSSAUpdater {
35   friend class SSAUpdaterTraits<MachineSSAUpdater>;
36 
37 private:
38   /// AvailableVals - This keeps track of which value to use on a per-block
39   /// basis.  When we insert PHI nodes, we keep track of them here.
40   //typedef DenseMap<MachineBasicBlock*, Register> AvailableValsTy;
41   void *AV = nullptr;
42 
43   /// VR - Current virtual register whose uses are being updated.
44   Register VR;
45 
46   /// VRC - Register class of the current virtual register.
47   const TargetRegisterClass *VRC;
48 
49   /// InsertedPHIs - If this is non-null, the MachineSSAUpdater adds all PHI
50   /// nodes that it creates to the vector.
51   SmallVectorImpl<MachineInstr*> *InsertedPHIs;
52 
53   const TargetInstrInfo *TII;
54   MachineRegisterInfo *MRI;
55 
56 public:
57   /// MachineSSAUpdater constructor.  If InsertedPHIs is specified, it will be
58   /// filled in with all PHI Nodes created by rewriting.
59   explicit MachineSSAUpdater(MachineFunction &MF,
60                         SmallVectorImpl<MachineInstr*> *NewPHI = nullptr);
61   MachineSSAUpdater(const MachineSSAUpdater &) = delete;
62   MachineSSAUpdater &operator=(const MachineSSAUpdater &) = delete;
63   ~MachineSSAUpdater();
64 
65   /// Initialize - Reset this object to get ready for a new set of SSA
66   /// updates.
67   void Initialize(Register V);
68 
69   /// AddAvailableValue - Indicate that a rewritten value is available at the
70   /// end of the specified block with the specified value.
71   void AddAvailableValue(MachineBasicBlock *BB, Register V);
72 
73   /// HasValueForBlock - Return true if the MachineSSAUpdater already has a
74   /// value for the specified block.
75   bool HasValueForBlock(MachineBasicBlock *BB) const;
76 
77   /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
78   /// live at the end of the specified block.
79   Register GetValueAtEndOfBlock(MachineBasicBlock *BB);
80 
81   /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
82   /// is live in the middle of the specified block.
83   ///
84   /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
85   /// important case: if there is a definition of the rewritten value after the
86   /// 'use' in BB.  Consider code like this:
87   ///
88   ///      X1 = ...
89   ///   SomeBB:
90   ///      use(X)
91   ///      X2 = ...
92   ///      br Cond, SomeBB, OutBB
93   ///
94   /// In this case, there are two values (X1 and X2) added to the AvailableVals
95   /// set by the client of the rewriter, and those values are both live out of
96   /// their respective blocks.  However, the use of X happens in the *middle* of
97   /// a block.  Because of this, we need to insert a new PHI node in SomeBB to
98   /// merge the appropriate values, and this value isn't live out of the block.
99   Register GetValueInMiddleOfBlock(MachineBasicBlock *BB);
100 
101   /// RewriteUse - Rewrite a use of the symbolic value.  This handles PHI nodes,
102   /// which use their value in the corresponding predecessor.  Note that this
103   /// will not work if the use is supposed to be rewritten to a value defined in
104   /// the same block as the use, but above it.  Any 'AddAvailableValue's added
105   /// for the use's block will be considered to be below it.
106   void RewriteUse(MachineOperand &U);
107 
108 private:
109   Register GetValueAtEndOfBlockInternal(MachineBasicBlock *BB);
110 };
111 
112 } // end namespace llvm
113 
114 #endif // LLVM_CODEGEN_MACHINESSAUPDATER_H
115