| 1 | //===-- llvm/CodeGen/SDNodeDbgValue.h - SelectionDAG dbg_value --*- 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 SDDbgValue class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_LIB_CODEGEN_SELECTIONDAG_SDNODEDBGVALUE_H |
| 14 | #define LLVM_LIB_CODEGEN_SELECTIONDAG_SDNODEDBGVALUE_H |
| 15 | |
| 16 | #include "llvm/CodeGen/Register.h" |
| 17 | #include "llvm/IR/DebugLoc.h" |
| 18 | #include "llvm/Support/Allocator.h" |
| 19 | #include "llvm/Support/DataTypes.h" |
| 20 | #include <utility> |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | class DIVariable; |
| 25 | class DIExpression; |
| 26 | class GlobalValue; |
| 27 | class SDNode; |
| 28 | class Value; |
| 29 | class raw_ostream; |
| 30 | |
| 31 | /// Holds the information for a single machine location through SDISel; either |
| 32 | /// an SDNode, a constant, a stack location, a virtual register, or the address |
| 33 | /// of a global. |
| 34 | class SDDbgOperand { |
| 35 | public: |
| 36 | enum Kind { |
| 37 | SDNODE = 0, ///< Value is the result of an expression. |
| 38 | CONST = 1, ///< Value is a constant. |
| 39 | FRAMEIX = 2, ///< Value is contents of a stack location. |
| 40 | VREG = 3, ///< Value is a virtual register. |
| 41 | GLOBALADDR = 4 ///< Value is the address of a global. |
| 42 | }; |
| 43 | Kind getKind() const { return kind; } |
| 44 | |
| 45 | /// Returns the SDNode* for a register ref |
| 46 | SDNode *getSDNode() const { |
| 47 | assert(kind == SDNODE); |
| 48 | return u.s.Node; |
| 49 | } |
| 50 | |
| 51 | /// Returns the ResNo for a register ref |
| 52 | unsigned getResNo() const { |
| 53 | assert(kind == SDNODE); |
| 54 | return u.s.ResNo; |
| 55 | } |
| 56 | |
| 57 | /// Returns the Value* for a constant |
| 58 | const Value *getConst() const { |
| 59 | assert(kind == CONST); |
| 60 | return u.Const; |
| 61 | } |
| 62 | |
| 63 | /// Returns the FrameIx for a stack object |
| 64 | unsigned getFrameIx() const { |
| 65 | assert(kind == FRAMEIX); |
| 66 | return u.FrameIx; |
| 67 | } |
| 68 | |
| 69 | /// Returns the Virtual Register for a VReg |
| 70 | Register getVReg() const { |
| 71 | assert(kind == VREG); |
| 72 | return u.VReg; |
| 73 | } |
| 74 | |
| 75 | /// Returns the GlobalValue whose address describes the variable. |
| 76 | const GlobalValue *getGlobal() const { |
| 77 | assert(kind == GLOBALADDR); |
| 78 | return u.GA; |
| 79 | } |
| 80 | |
| 81 | static SDDbgOperand fromNode(SDNode *Node, unsigned ResNo) { |
| 82 | return SDDbgOperand(Node, ResNo); |
| 83 | } |
| 84 | static SDDbgOperand fromFrameIdx(unsigned FrameIdx) { |
| 85 | return SDDbgOperand(FrameIdx, FRAMEIX); |
| 86 | } |
| 87 | static SDDbgOperand fromVReg(Register VReg) { |
| 88 | return SDDbgOperand(VReg.id(), VREG); |
| 89 | } |
| 90 | static SDDbgOperand fromConst(const Value *Const) { |
| 91 | return SDDbgOperand(Const); |
| 92 | } |
| 93 | static SDDbgOperand fromGlobalAddr(const GlobalValue *GV) { |
| 94 | return SDDbgOperand(GV, GLOBALADDR); |
| 95 | } |
| 96 | |
| 97 | bool operator!=(const SDDbgOperand &Other) const { return !(*this == Other); } |
| 98 | bool operator==(const SDDbgOperand &Other) const { |
| 99 | if (kind != Other.kind) |
| 100 | return false; |
| 101 | switch (kind) { |
| 102 | case SDNODE: |
| 103 | return getSDNode() == Other.getSDNode() && getResNo() == Other.getResNo(); |
| 104 | case CONST: |
| 105 | return getConst() == Other.getConst(); |
| 106 | case VREG: |
| 107 | return getVReg() == Other.getVReg(); |
| 108 | case FRAMEIX: |
| 109 | return getFrameIx() == Other.getFrameIx(); |
| 110 | case GLOBALADDR: |
| 111 | return getGlobal() == Other.getGlobal(); |
| 112 | } |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | private: |
| 117 | Kind kind; |
| 118 | union { |
| 119 | struct { |
| 120 | SDNode *Node; ///< Valid for expressions. |
| 121 | unsigned ResNo; ///< Valid for expressions. |
| 122 | } s; |
| 123 | const Value *Const; ///< Valid for constants. |
| 124 | unsigned FrameIx; ///< Valid for stack objects. |
| 125 | unsigned VReg; ///< Valid for registers. |
| 126 | const GlobalValue *GA; ///< Valid for global addresses. |
| 127 | } u; |
| 128 | |
| 129 | /// Constructor for non-constants. |
| 130 | SDDbgOperand(SDNode *N, unsigned R) : kind(SDNODE) { |
| 131 | u.s.Node = N; |
| 132 | u.s.ResNo = R; |
| 133 | } |
| 134 | /// Constructor for constants. |
| 135 | SDDbgOperand(const Value *C) : kind(CONST) { u.Const = C; } |
| 136 | /// Constructor for global addresses. Takes an explicit Kind because a |
| 137 | /// GlobalValue would otherwise be ambiguous with the Value constructor. |
| 138 | SDDbgOperand(const GlobalValue *GV, Kind Kind) : kind(Kind) { |
| 139 | assert(Kind == GLOBALADDR && "Invalid SDDbgValue constructor" ); |
| 140 | u.GA = GV; |
| 141 | } |
| 142 | /// Constructor for virtual registers and frame indices. |
| 143 | SDDbgOperand(unsigned VRegOrFrameIdx, Kind Kind) : kind(Kind) { |
| 144 | assert((Kind == VREG || Kind == FRAMEIX) && |
| 145 | "Invalid SDDbgValue constructor" ); |
| 146 | if (kind == VREG) |
| 147 | u.VReg = VRegOrFrameIdx; |
| 148 | else |
| 149 | u.FrameIx = VRegOrFrameIdx; |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | /// Holds the information from a dbg_value node through SDISel. |
| 154 | /// We do not use SDValue here to avoid including its header. |
| 155 | class SDDbgValue { |
| 156 | public: |
| 157 | |
| 158 | private: |
| 159 | // SDDbgValues are allocated by a BumpPtrAllocator, which means the destructor |
| 160 | // may not be called; therefore all member arrays must also be allocated by |
| 161 | // that BumpPtrAllocator, to ensure that they are correctly freed. |
| 162 | size_t NumLocationOps; |
| 163 | SDDbgOperand *LocationOps; |
| 164 | // SDNode dependencies will be calculated as SDNodes that appear in |
| 165 | // LocationOps plus these AdditionalDependencies. |
| 166 | size_t NumAdditionalDependencies; |
| 167 | SDNode **AdditionalDependencies; |
| 168 | DIVariable *Var; |
| 169 | DIExpression *Expr; |
| 170 | DebugLoc DL; |
| 171 | unsigned Order; |
| 172 | bool IsIndirect; |
| 173 | bool IsVariadic; |
| 174 | bool Invalid = false; |
| 175 | bool Emitted = false; |
| 176 | |
| 177 | public: |
| 178 | SDDbgValue(BumpPtrAllocator &Alloc, DIVariable *Var, DIExpression *Expr, |
| 179 | ArrayRef<SDDbgOperand> L, ArrayRef<SDNode *> Dependencies, |
| 180 | bool IsIndirect, DebugLoc DL, unsigned O, bool IsVariadic) |
| 181 | : NumLocationOps(L.size()), |
| 182 | LocationOps(Alloc.Allocate<SDDbgOperand>(Num: L.size())), |
| 183 | NumAdditionalDependencies(Dependencies.size()), |
| 184 | AdditionalDependencies(Alloc.Allocate<SDNode *>(Num: Dependencies.size())), |
| 185 | Var(Var), Expr(Expr), DL(DL), Order(O), IsIndirect(IsIndirect), |
| 186 | IsVariadic(IsVariadic) { |
| 187 | assert(IsVariadic || L.size() == 1); |
| 188 | assert(!(IsVariadic && IsIndirect)); |
| 189 | llvm::copy(Range&: L, Out: LocationOps); |
| 190 | llvm::copy(Range&: Dependencies, Out: AdditionalDependencies); |
| 191 | } |
| 192 | |
| 193 | // We allocate arrays with the BumpPtrAllocator and never free or copy them, |
| 194 | // for LocationOps and AdditionalDependencies, as we never expect to copy or |
| 195 | // destroy an SDDbgValue. If we ever start copying or destroying instances, we |
| 196 | // should manage the allocated memory appropriately. |
| 197 | SDDbgValue(const SDDbgValue &Other) = delete; |
| 198 | SDDbgValue &operator=(const SDDbgValue &Other) = delete; |
| 199 | ~SDDbgValue() = delete; |
| 200 | |
| 201 | /// Returns the DIVariable pointer for the variable. |
| 202 | DIVariable *getVariable() const { return Var; } |
| 203 | |
| 204 | /// Returns the DIExpression pointer for the expression. |
| 205 | DIExpression *getExpression() const { return Expr; } |
| 206 | |
| 207 | ArrayRef<SDDbgOperand> getLocationOps() const { |
| 208 | return ArrayRef<SDDbgOperand>(LocationOps, NumLocationOps); |
| 209 | } |
| 210 | |
| 211 | SmallVector<SDDbgOperand> copyLocationOps() const { |
| 212 | return SmallVector<SDDbgOperand>(LocationOps, LocationOps + NumLocationOps); |
| 213 | } |
| 214 | |
| 215 | // Returns the SDNodes which this SDDbgValue depends on. |
| 216 | SmallVector<SDNode *> getSDNodes() const { |
| 217 | SmallVector<SDNode *> Dependencies; |
| 218 | for (const SDDbgOperand &DbgOp : getLocationOps()) |
| 219 | if (DbgOp.getKind() == SDDbgOperand::SDNODE) |
| 220 | Dependencies.push_back(Elt: DbgOp.getSDNode()); |
| 221 | llvm::append_range(C&: Dependencies, R: getAdditionalDependencies()); |
| 222 | return Dependencies; |
| 223 | } |
| 224 | |
| 225 | ArrayRef<SDNode *> getAdditionalDependencies() const { |
| 226 | return ArrayRef<SDNode *>(AdditionalDependencies, |
| 227 | NumAdditionalDependencies); |
| 228 | } |
| 229 | |
| 230 | /// Returns whether this is an indirect value. |
| 231 | bool isIndirect() const { return IsIndirect; } |
| 232 | |
| 233 | bool isVariadic() const { return IsVariadic; } |
| 234 | |
| 235 | /// Returns the DebugLoc. |
| 236 | const DebugLoc &getDebugLoc() const { return DL; } |
| 237 | |
| 238 | /// Returns the SDNodeOrder. This is the order of the preceding node in the |
| 239 | /// input. |
| 240 | unsigned getOrder() const { return Order; } |
| 241 | |
| 242 | /// setIsInvalidated / isInvalidated - Setter / getter of the "Invalidated" |
| 243 | /// property. A SDDbgValue is invalid if the SDNode that produces the value is |
| 244 | /// deleted. |
| 245 | void setIsInvalidated() { Invalid = true; } |
| 246 | bool isInvalidated() const { return Invalid; } |
| 247 | |
| 248 | /// setIsEmitted / isEmitted - Getter/Setter for flag indicating that this |
| 249 | /// SDDbgValue has been emitted to an MBB. |
| 250 | void setIsEmitted() { Emitted = true; } |
| 251 | bool isEmitted() const { return Emitted; } |
| 252 | |
| 253 | /// clearIsEmitted - Reset Emitted flag, for certain special cases where |
| 254 | /// SDDbgValue is emitted twice. DBG_INSTR_REF depends on this behaviour. |
| 255 | void clearIsEmitted() { Emitted = false; } |
| 256 | |
| 257 | LLVM_DUMP_METHOD void dump() const; |
| 258 | LLVM_DUMP_METHOD void print(raw_ostream &OS) const; |
| 259 | }; |
| 260 | |
| 261 | /// Holds the information from a dbg_label node through SDISel. |
| 262 | /// We do not use SDValue here to avoid including its header. |
| 263 | class SDDbgLabel { |
| 264 | MDNode *Label; |
| 265 | DebugLoc DL; |
| 266 | unsigned Order; |
| 267 | |
| 268 | public: |
| 269 | SDDbgLabel(MDNode *Label, DebugLoc dl, unsigned O) |
| 270 | : Label(Label), DL(std::move(dl)), Order(O) {} |
| 271 | |
| 272 | /// Returns the MDNode pointer for the label. |
| 273 | MDNode *getLabel() const { return Label; } |
| 274 | |
| 275 | /// Returns the DebugLoc. |
| 276 | const DebugLoc &getDebugLoc() const { return DL; } |
| 277 | |
| 278 | /// Returns the SDNodeOrder. This is the order of the preceding node in the |
| 279 | /// input. |
| 280 | unsigned getOrder() const { return Order; } |
| 281 | }; |
| 282 | |
| 283 | } // end llvm namespace |
| 284 | |
| 285 | #endif |
| 286 | |