brintos

brintos / llvm-project-archived public Read only

0
0
Text · 10.1 KiB · 5358f7b Raw
289 lines · c
1//===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- 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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H10#define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H11 12#include "DebugLocStream.h"13#include "llvm/Config/llvm-config.h"14#include "llvm/IR/Constants.h"15#include "llvm/IR/DebugInfo.h"16#include "llvm/MC/MCSymbol.h"17#include "llvm/MC/MachineLocation.h"18#include "llvm/Support/Debug.h"19 20namespace llvm {21class AsmPrinter;22 23/// This struct describes target specific location.24struct TargetIndexLocation {25  int Index;26  int Offset;27 28  TargetIndexLocation() = default;29  TargetIndexLocation(unsigned Idx, int64_t Offset)30      : Index(Idx), Offset(Offset) {}31 32  bool operator==(const TargetIndexLocation &Other) const {33    return Index == Other.Index && Offset == Other.Offset;34  }35};36 37/// A single location or constant within a variable location description, with38/// either a single entry (with an optional DIExpression) used for a DBG_VALUE,39/// or a list of entries used for a DBG_VALUE_LIST.40class DbgValueLocEntry {41 42  /// Type of entry that this represents.43  enum EntryType {44    E_Location,45    E_Integer,46    E_ConstantFP,47    E_ConstantInt,48    E_TargetIndexLocation49  };50  enum EntryType EntryKind;51 52  /// Either a constant,53  union {54    int64_t Int;55    const ConstantFP *CFP;56    const ConstantInt *CIP;57  } Constant;58 59  union {60    /// Or a location in the machine frame.61    MachineLocation Loc;62    /// Or a location from target specific location.63    TargetIndexLocation TIL;64  };65 66public:67  DbgValueLocEntry(int64_t i) : EntryKind(E_Integer) { Constant.Int = i; }68  DbgValueLocEntry(const ConstantFP *CFP) : EntryKind(E_ConstantFP) {69    Constant.CFP = CFP;70  }71  DbgValueLocEntry(const ConstantInt *CIP) : EntryKind(E_ConstantInt) {72    Constant.CIP = CIP;73  }74  DbgValueLocEntry(MachineLocation Loc) : EntryKind(E_Location), Loc(Loc) {}75  DbgValueLocEntry(TargetIndexLocation Loc)76      : EntryKind(E_TargetIndexLocation), TIL(Loc) {}77 78  bool isLocation() const { return EntryKind == E_Location; }79  bool isIndirectLocation() const {80    return EntryKind == E_Location && Loc.isIndirect();81  }82  bool isTargetIndexLocation() const {83    return EntryKind == E_TargetIndexLocation;84  }85  bool isInt() const { return EntryKind == E_Integer; }86  bool isConstantFP() const { return EntryKind == E_ConstantFP; }87  bool isConstantInt() const { return EntryKind == E_ConstantInt; }88  int64_t getInt() const { return Constant.Int; }89  const ConstantFP *getConstantFP() const { return Constant.CFP; }90  const ConstantInt *getConstantInt() const { return Constant.CIP; }91  MachineLocation getLoc() const { return Loc; }92  TargetIndexLocation getTargetIndexLocation() const { return TIL; }93  friend bool operator==(const DbgValueLocEntry &, const DbgValueLocEntry &);94#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)95  LLVM_DUMP_METHOD void dump() const {96    if (isLocation()) {97      llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " ";98      if (Loc.isIndirect())99        llvm::dbgs() << "+0";100      llvm::dbgs() << "} ";101    } else if (isConstantInt())102      Constant.CIP->dump();103    else if (isConstantFP())104      Constant.CFP->dump();105  }106#endif107};108 109/// The location of a single variable, composed of an expression and 0 or more110/// DbgValueLocEntries.111class DbgValueLoc {112  /// Any complex address location expression for this DbgValueLoc.113  const DIExpression *Expression;114 115  SmallVector<DbgValueLocEntry, 2> ValueLocEntries;116 117  bool IsVariadic;118 119public:120  DbgValueLoc(const DIExpression *Expr, ArrayRef<DbgValueLocEntry> Locs)121      : Expression(Expr), ValueLocEntries(Locs), IsVariadic(true) {}122 123  DbgValueLoc(const DIExpression *Expr, ArrayRef<DbgValueLocEntry> Locs,124              bool IsVariadic)125      : Expression(Expr), ValueLocEntries(Locs), IsVariadic(IsVariadic) {126#ifndef NDEBUG127    assert(Expr->isValid() ||128           !any_of(Locs, [](auto LE) { return LE.isLocation(); }));129    if (!IsVariadic) {130      assert(ValueLocEntries.size() == 1);131    }132#endif133  }134 135  DbgValueLoc(const DIExpression *Expr, DbgValueLocEntry Loc)136      : Expression(Expr), ValueLocEntries(1, Loc), IsVariadic(false) {137    assert(((Expr && Expr->isValid()) || !Loc.isLocation()) &&138           "DBG_VALUE with a machine location must have a valid expression.");139  }140 141  bool isFragment() const { return getExpression()->isFragment(); }142  bool isEntryVal() const { return getExpression()->isEntryValue(); }143  bool isVariadic() const { return IsVariadic; }144  bool isEquivalent(const DbgValueLoc &Other) const {145    // Cannot be equivalent with different numbers of entries.146    if (ValueLocEntries.size() != Other.ValueLocEntries.size())147      return false;148    bool ThisIsIndirect =149        !IsVariadic && ValueLocEntries[0].isIndirectLocation();150    bool OtherIsIndirect =151        !Other.IsVariadic && Other.ValueLocEntries[0].isIndirectLocation();152    // Check equivalence of DIExpressions + Directness together.153    if (!DIExpression::isEqualExpression(Expression, ThisIsIndirect,154                                         Other.Expression, OtherIsIndirect))155      return false;156    // Indirectness should have been accounted for in the above check, so just157    // compare register values directly here.158    if (ThisIsIndirect || OtherIsIndirect) {159      DbgValueLocEntry ThisOp = ValueLocEntries[0];160      DbgValueLocEntry OtherOp = Other.ValueLocEntries[0];161      return ThisOp.isLocation() && OtherOp.isLocation() &&162             ThisOp.getLoc().getReg() == OtherOp.getLoc().getReg();163    }164    // If neither are indirect, then just compare the loc entries directly.165    return ValueLocEntries == Other.ValueLocEntries;166  }167  const DIExpression *getExpression() const { return Expression; }168  ArrayRef<DbgValueLocEntry> getLocEntries() const { return ValueLocEntries; }169  friend bool operator==(const DbgValueLoc &, const DbgValueLoc &);170  friend bool operator<(const DbgValueLoc &, const DbgValueLoc &);171#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)172  LLVM_DUMP_METHOD void dump() const {173    for (const DbgValueLocEntry &DV : ValueLocEntries)174      DV.dump();175    if (Expression)176      Expression->dump();177  }178#endif179};180 181/// This struct describes location entries emitted in the .debug_loc182/// section.183class DebugLocEntry {184  /// Begin and end symbols for the address range that this location is valid.185  const MCSymbol *Begin;186  const MCSymbol *End;187 188  /// A nonempty list of locations/constants belonging to this entry,189  /// sorted by offset.190  SmallVector<DbgValueLoc, 1> Values;191 192public:193  /// Create a location list entry for the range [\p Begin, \p End).194  ///195  /// \param Vals One or more values describing (parts of) the variable.196  DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End,197                ArrayRef<DbgValueLoc> Vals)198      : Begin(Begin), End(End) {199    addValues(Vals);200  }201 202  /// Attempt to merge this DebugLocEntry with Next and return203  /// true if the merge was successful. Entries can be merged if they204  /// share the same Loc/Constant and if Next immediately follows this205  /// Entry.206  bool MergeRanges(const DebugLocEntry &Next) {207    // If this and Next are describing the same variable, merge them.208    if (End != Next.Begin)209      return false;210    if (Values.size() != Next.Values.size())211      return false;212    for (unsigned EntryIdx = 0; EntryIdx < Values.size(); ++EntryIdx)213      if (!Values[EntryIdx].isEquivalent(Next.Values[EntryIdx]))214        return false;215    End = Next.End;216    return true;217  }218 219  const MCSymbol *getBeginSym() const { return Begin; }220  const MCSymbol *getEndSym() const { return End; }221  ArrayRef<DbgValueLoc> getValues() const { return Values; }222  void addValues(ArrayRef<DbgValueLoc> Vals) {223    Values.append(Vals.begin(), Vals.end());224    sortUniqueValues();225    assert((Values.size() == 1 || all_of(Values, [](DbgValueLoc V) {226              return V.isFragment();227            })) && "must either have a single value or multiple pieces");228  }229 230  // Sort the pieces by offset.231  // Remove any duplicate entries by dropping all but the first.232  void sortUniqueValues() {233    // Values is either 1 item that does not have a fragment, or many items234    // that all do. No need to sort if the former and also prevents operator<235    // being called on a non fragment item when _GLIBCXX_DEBUG is defined.236    if (Values.size() == 1)237      return;238    llvm::sort(Values);239    Values.erase(llvm::unique(Values,240                              [](const DbgValueLoc &A, const DbgValueLoc &B) {241                                return A.getExpression() == B.getExpression();242                              }),243                 Values.end());244  }245 246  /// Lower this entry into a DWARF expression.247  void finalize(const AsmPrinter &AP,248                DebugLocStream::ListBuilder &List,249                const DIBasicType *BT,250                DwarfCompileUnit &TheCU);251};252 253/// Compare two DbgValueLocEntries for equality.254inline bool operator==(const DbgValueLocEntry &A, const DbgValueLocEntry &B) {255  if (A.EntryKind != B.EntryKind)256    return false;257 258  switch (A.EntryKind) {259  case DbgValueLocEntry::E_Location:260    return A.Loc == B.Loc;261  case DbgValueLocEntry::E_TargetIndexLocation:262    return A.TIL == B.TIL;263  case DbgValueLocEntry::E_Integer:264    return A.Constant.Int == B.Constant.Int;265  case DbgValueLocEntry::E_ConstantFP:266    return A.Constant.CFP == B.Constant.CFP;267  case DbgValueLocEntry::E_ConstantInt:268    return A.Constant.CIP == B.Constant.CIP;269  }270  llvm_unreachable("unhandled EntryKind");271}272 273/// Compare two DbgValueLocs for equality.274inline bool operator==(const DbgValueLoc &A, const DbgValueLoc &B) {275  return A.ValueLocEntries == B.ValueLocEntries &&276         A.Expression == B.Expression && A.IsVariadic == B.IsVariadic;277}278 279/// Compare two fragments based on their offset.280inline bool operator<(const DbgValueLoc &A,281                      const DbgValueLoc &B) {282  return A.getExpression()->getFragmentInfo()->OffsetInBits <283         B.getExpression()->getFragmentInfo()->OffsetInBits;284}285 286}287 288#endif289