136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines//===- DebugLoc.h - Debug Location Information ------------------*- C++ -*-===//
2d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng//
3d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng//                     The LLVM Compiler Infrastructure
4d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng//
5d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng// This file is distributed under the University of Illinois Open Source
6d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng// License. See LICENSE.TXT for details.
7d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng//
8d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng//===----------------------------------------------------------------------===//
9d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng//
101e86a66b00b94adc4ad6977ef6b47c516ac62cecDevang Patel// This file defines a number of light weight data structures used
111e86a66b00b94adc4ad6977ef6b47c516ac62cecDevang Patel// to describe and track debug location information.
124a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak//
1342ce8eaa8c5af9c29825737ce337e6e2397ab6a6Bill Wendling//===----------------------------------------------------------------------===//
14d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng
1536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#ifndef LLVM_IR_DEBUGLOC_H
1636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#define LLVM_IR_DEBUGLOC_H
17d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng
1836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/Support/DataTypes.h"
19e5a694ab578416d8d280836937c7d2714b1a83e6Yaron Keren
20d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Chengnamespace llvm {
214cdd17ef47e71f34f9fc50caf78c51b9c7105ed1Benjamin Kramer  template <typename T> struct DenseMapInfo;
22e4b275610a7a05b7ee4c0378a906a6330e4c4ab0Devang Patel  class MDNode;
23b227925fa313428045f554187b0136d084d723f6Chris Lattner  class LLVMContext;
24dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  class raw_ostream;
254a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
26b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// DebugLoc - Debug location id.  This is carried by Instruction, SDNode,
27b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// and MachineInstr to compactly encode file/line/scope information for an
28b227925fa313428045f554187b0136d084d723f6Chris Lattner  /// operation.
2984e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner  class DebugLoc {
30af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky    friend struct DenseMapInfo<DebugLoc>;
31af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky
324841121580df86b327013d1fab2a9339be487bceNick Lewycky    /// getEmptyKey() - A private constructor that returns an unknown that is
334841121580df86b327013d1fab2a9339be487bceNick Lewycky    /// not equal to the tombstone key or DebugLoc().
344841121580df86b327013d1fab2a9339be487bceNick Lewycky    static DebugLoc getEmptyKey() {
354841121580df86b327013d1fab2a9339be487bceNick Lewycky      DebugLoc DL;
36b90e1d5aaf5b43309a0643b3248dd95001b3d196Nick Lewycky      DL.LineCol = 1;
374841121580df86b327013d1fab2a9339be487bceNick Lewycky      return DL;
384841121580df86b327013d1fab2a9339be487bceNick Lewycky    }
394841121580df86b327013d1fab2a9339be487bceNick Lewycky
40af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky    /// getTombstoneKey() - A private constructor that returns an unknown that
414841121580df86b327013d1fab2a9339be487bceNick Lewycky    /// is not equal to the empty key or DebugLoc().
42af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky    static DebugLoc getTombstoneKey() {
43af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky      DebugLoc DL;
44b90e1d5aaf5b43309a0643b3248dd95001b3d196Nick Lewycky      DL.LineCol = 2;
45af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky      return DL;
46af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky    }
47af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky
48b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// LineCol - This 32-bit value encodes the line and column number for the
49b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// location, encoded as 24-bits for line and 8 bits for col.  A value of 0
50b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// for either means unknown.
51e5a694ab578416d8d280836937c7d2714b1a83e6Yaron Keren    uint32_t LineCol;
524a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
53b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// ScopeIdx - This is an opaque ID# for Scope/InlinedAt information,
54b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// decoded by LLVMContext.  0 is unknown.
55b227925fa313428045f554187b0136d084d723f6Chris Lattner    int ScopeIdx;
56b227925fa313428045f554187b0136d084d723f6Chris Lattner  public:
5784e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner    DebugLoc() : LineCol(0), ScopeIdx(0) {}  // Defaults to unknown.
584a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
59f0100ff3f6adb41d257ab5bff5fef390a8f458e2Chris Lattner    /// get - Get a new DebugLoc that corresponds to the specified line/col
60f0100ff3f6adb41d257ab5bff5fef390a8f458e2Chris Lattner    /// scope/inline location.
6184e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner    static DebugLoc get(unsigned Line, unsigned Col,
62dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines                        MDNode *Scope, MDNode *InlinedAt = nullptr);
634a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
6484e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner    /// getFromDILocation - Translate the DILocation quad into a DebugLoc.
6584e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner    static DebugLoc getFromDILocation(MDNode *N);
66d77ec6208cef2a8e615181c2bf0fc5a50a37a20bDevang Patel
67d77ec6208cef2a8e615181c2bf0fc5a50a37a20bDevang Patel    /// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
68d77ec6208cef2a8e615181c2bf0fc5a50a37a20bDevang Patel    static DebugLoc getFromDILexicalBlock(MDNode *N);
69d77ec6208cef2a8e615181c2bf0fc5a50a37a20bDevang Patel
70b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// isUnknown - Return true if this is an unknown location.
71b227925fa313428045f554187b0136d084d723f6Chris Lattner    bool isUnknown() const { return ScopeIdx == 0; }
724a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
73b227925fa313428045f554187b0136d084d723f6Chris Lattner    unsigned getLine() const {
74b227925fa313428045f554187b0136d084d723f6Chris Lattner      return (LineCol << 8) >> 8;  // Mask out column.
75b227925fa313428045f554187b0136d084d723f6Chris Lattner    }
764a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
77b227925fa313428045f554187b0136d084d723f6Chris Lattner    unsigned getCol() const {
78b227925fa313428045f554187b0136d084d723f6Chris Lattner      return LineCol >> 24;
79b227925fa313428045f554187b0136d084d723f6Chris Lattner    }
804a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
81b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// getScope - This returns the scope pointer for this DebugLoc, or null if
82b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// invalid.
83b227925fa313428045f554187b0136d084d723f6Chris Lattner    MDNode *getScope(const LLVMContext &Ctx) const;
844a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
85b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// getInlinedAt - This returns the InlinedAt pointer for this DebugLoc, or
86b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// null if invalid or not present.
87b227925fa313428045f554187b0136d084d723f6Chris Lattner    MDNode *getInlinedAt(const LLVMContext &Ctx) const;
884a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
89b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// getScopeAndInlinedAt - Return both the Scope and the InlinedAt values.
90b227925fa313428045f554187b0136d084d723f6Chris Lattner    void getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
91b227925fa313428045f554187b0136d084d723f6Chris Lattner                              const LLVMContext &Ctx) const;
924a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
9336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    /// getScopeNode - Get MDNode for DebugLoc's scope, or null if invalid.
9436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    MDNode *getScopeNode(const LLVMContext &Ctx) const;
9536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines
9636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // getFnDebugLoc - Walk up the scope chain of given debug loc and find line
9736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    // number info for the function.
98cd81d94322a39503e4a3e87b6ee03d4fcb3465fbStephen Hines    DebugLoc getFnDebugLoc(const LLVMContext &Ctx) const;
994a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
100b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// getAsMDNode - This method converts the compressed DebugLoc node into a
101b227925fa313428045f554187b0136d084d723f6Chris Lattner    /// DILocation compatible MDNode.
102b227925fa313428045f554187b0136d084d723f6Chris Lattner    MDNode *getAsMDNode(const LLVMContext &Ctx) const;
1034a8dbb7da30bfe389ac5470cba98035451f329daJakub Staszak
10484e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner    bool operator==(const DebugLoc &DL) const {
105b227925fa313428045f554187b0136d084d723f6Chris Lattner      return LineCol == DL.LineCol && ScopeIdx == DL.ScopeIdx;
106b227925fa313428045f554187b0136d084d723f6Chris Lattner    }
10784e679beea11ac55ed7871eec4deaccdf393de3eChris Lattner    bool operator!=(const DebugLoc &DL) const { return !(*this == DL); }
1083dcb4ef757127d69c3748dec92787520e039bd53Devang Patel
1093dcb4ef757127d69c3748dec92787520e039bd53Devang Patel    void dump(const LLVMContext &Ctx) const;
110dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    /// \brief prints source location /path/to/file.exe:line:col @[inlined at]
111dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    void print(const LLVMContext &Ctx, raw_ostream &OS) const;
112b227925fa313428045f554187b0136d084d723f6Chris Lattner  };
113af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky
114af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky  template <>
115af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky  struct DenseMapInfo<DebugLoc> {
1164cdd17ef47e71f34f9fc50caf78c51b9c7105ed1Benjamin Kramer    static DebugLoc getEmptyKey() { return DebugLoc::getEmptyKey(); }
1174cdd17ef47e71f34f9fc50caf78c51b9c7105ed1Benjamin Kramer    static DebugLoc getTombstoneKey() { return DebugLoc::getTombstoneKey(); }
118af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky    static unsigned getHashValue(const DebugLoc &Key);
1194cdd17ef47e71f34f9fc50caf78c51b9c7105ed1Benjamin Kramer    static bool isEqual(DebugLoc LHS, DebugLoc RHS) { return LHS == RHS; }
120af4db5fc95f5d846c48e80f4266687a34a2975f4Nick Lewycky  };
121d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng} // end namespace llvm
122d0adbb5b7da2d1238fdf1a30734a001a0103aab0Evan Cheng
123674be02d525d4e24bc6943ed9274958c580bcfbcJakub Staszak#endif /* LLVM_SUPPORT_DEBUGLOC_H */
124