blob: 0af356e376ab8d9389998b468e810099be12aa6d [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MachineSSAUpdater.h - Unstructured SSA Update Tool -------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020016#include "llvm/CodeGen/Register.h"
17
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010018namespace llvm {
19
20class MachineBasicBlock;
21class MachineFunction;
22class MachineInstr;
23class MachineOperand;
24class MachineRegisterInfo;
25class TargetInstrInfo;
26class TargetRegisterClass;
27template<typename T> class SmallVectorImpl;
28template<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.
34class MachineSSAUpdater {
35 friend class SSAUpdaterTraits<MachineSSAUpdater>;
36
37private:
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.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020040 //typedef DenseMap<MachineBasicBlock*, Register> AvailableValsTy;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010041 void *AV = nullptr;
42
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010043 /// VRC - Register class of the current virtual register.
44 const TargetRegisterClass *VRC;
45
46 /// InsertedPHIs - If this is non-null, the MachineSSAUpdater adds all PHI
47 /// nodes that it creates to the vector.
48 SmallVectorImpl<MachineInstr*> *InsertedPHIs;
49
50 const TargetInstrInfo *TII;
51 MachineRegisterInfo *MRI;
52
53public:
54 /// MachineSSAUpdater constructor. If InsertedPHIs is specified, it will be
55 /// filled in with all PHI Nodes created by rewriting.
56 explicit MachineSSAUpdater(MachineFunction &MF,
Andrew Scullcdfcccc2018-10-05 20:58:37 +010057 SmallVectorImpl<MachineInstr*> *NewPHI = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010058 MachineSSAUpdater(const MachineSSAUpdater &) = delete;
59 MachineSSAUpdater &operator=(const MachineSSAUpdater &) = delete;
60 ~MachineSSAUpdater();
61
62 /// Initialize - Reset this object to get ready for a new set of SSA
63 /// updates.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020064 void Initialize(Register V);
65 void Initialize(const TargetRegisterClass *RC);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066
67 /// AddAvailableValue - Indicate that a rewritten value is available at the
68 /// end of the specified block with the specified value.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020069 void AddAvailableValue(MachineBasicBlock *BB, Register V);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010070
71 /// HasValueForBlock - Return true if the MachineSSAUpdater already has a
72 /// value for the specified block.
73 bool HasValueForBlock(MachineBasicBlock *BB) const;
74
75 /// GetValueAtEndOfBlock - Construct SSA form, materializing a value that is
76 /// live at the end of the specified block.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020077 Register GetValueAtEndOfBlock(MachineBasicBlock *BB);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010078
79 /// GetValueInMiddleOfBlock - Construct SSA form, materializing a value that
80 /// is live in the middle of the specified block.
81 ///
82 /// GetValueInMiddleOfBlock is the same as GetValueAtEndOfBlock except in one
83 /// important case: if there is a definition of the rewritten value after the
84 /// 'use' in BB. Consider code like this:
85 ///
86 /// X1 = ...
87 /// SomeBB:
88 /// use(X)
89 /// X2 = ...
90 /// br Cond, SomeBB, OutBB
91 ///
92 /// In this case, there are two values (X1 and X2) added to the AvailableVals
93 /// set by the client of the rewriter, and those values are both live out of
94 /// their respective blocks. However, the use of X happens in the *middle* of
95 /// a block. Because of this, we need to insert a new PHI node in SomeBB to
96 /// merge the appropriate values, and this value isn't live out of the block.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020097 Register GetValueInMiddleOfBlock(MachineBasicBlock *BB);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010098
99 /// RewriteUse - Rewrite a use of the symbolic value. This handles PHI nodes,
100 /// which use their value in the corresponding predecessor. Note that this
101 /// will not work if the use is supposed to be rewritten to a value defined in
102 /// the same block as the use, but above it. Any 'AddAvailableValue's added
103 /// for the use's block will be considered to be below it.
104 void RewriteUse(MachineOperand &U);
105
106private:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200107 Register GetValueAtEndOfBlockInternal(MachineBasicBlock *BB);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100108};
109
110} // end namespace llvm
111
112#endif // LLVM_CODEGEN_MACHINESSAUPDATER_H