From d0adbb5b7da2d1238fdf1a30734a001a0103aab0 Mon Sep 17 00:00:00 2001 From: Evan Cheng Date: Mon, 26 Jan 2009 07:41:49 +0000 Subject: [PATCH] Add data structure to define and track debug location during codegen. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@63008 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/CodeGen/DebugLoc.h | 109 +++++++++++++++++++++++++ include/llvm/CodeGen/MachineFunction.h | 15 +++- lib/CodeGen/MachineFunction.cpp | 27 ++++++ 3 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 include/llvm/CodeGen/DebugLoc.h diff --git a/include/llvm/CodeGen/DebugLoc.h b/include/llvm/CodeGen/DebugLoc.h new file mode 100644 index 00000000000..ad68839b054 --- /dev/null +++ b/include/llvm/CodeGen/DebugLoc.h @@ -0,0 +1,109 @@ +//===---- llvm/CodeGen/DebugLoc.h - Debug Location Information --*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines a number of light weight data structures used by the code +// generator to describe and track debug location information. + +#ifndef LLVM_CODEGEN_DEBUGLOC_H +#define LLVM_CODEGEN_DEBUGLOC_H + +#include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/StringMap.h" +#include + +namespace llvm { + + /// DebugLocTuple - Debug location tuple of filename id, line and column. + /// + struct DebugLocTuple { + unsigned FileId, Line, Col; + + DebugLocTuple(unsigned fi, unsigned l, unsigned c) + : FileId(fi), Line(l), Col(c) {}; + }; + + /// DebugLoc - Debug location id. This is carried by SDNode and + /// MachineInstr to index into a vector of unique debug location tuples. + class DebugLoc { + unsigned Idx; + + public: + DebugLoc() : Idx(~0U) {} + + static DebugLoc getNoDebugLoc() { DebugLoc L; L.Idx = 0; return L; } + static DebugLoc get(unsigned idx) { DebugLoc L; L.Idx = idx; return L; } + + bool isInvalid() { return Idx == ~0U; } + bool isUnknown() { return Idx == 0; } + }; + + struct DebugLocTupleDenseMapInfo { + static inline DebugLocTuple getEmptyKey() { + return DebugLocTuple(~0U, ~0U, ~0U); + } + static inline DebugLocTuple getTombstoneKey() { + return DebugLocTuple(~1U, ~1U, ~1U); + } + static unsigned getHashValue(const DebugLocTuple &Val) { + return DenseMapInfo::getHashValue(Val.FileId) ^ + DenseMapInfo::getHashValue(Val.Line) ^ + DenseMapInfo::getHashValue(Val.Col); + } + static bool isEqual(const DebugLocTuple &LHS, const DebugLocTuple &RHS) { + return LHS.FileId == RHS.FileId && + LHS.Line == RHS.Line && + LHS.Col == RHS.Col; + } + + static bool isPod() { return true; } + }; + + typedef DenseMap + DebugIdMapType; + + /// DebugLocTracker - This class tracks debug location information. + /// + struct DebugLocTracker { + // NumFilenames - Size of the DebugFilenames vector. + // + unsigned NumFilenames; + + // DebugFilenames - A vector of unique file names. + // + std::vector DebugFilenames; + + // DebugFilenamesMap - File name to DebugFilenames index map. + // + StringMap DebugFilenamesMap; + + // NumDebugLocations - Size of the DebugLocations vector. + unsigned NumDebugLocations; + + // DebugLocations - A vector of unique DebugLocTuple's. + // + std::vector DebugLocations; + + // DebugIdsMap - This maps DebugLocTuple's to indices into + // DebugLocations vector. + DebugIdMapType DebugIdMap; + + DebugLocTracker() : NumFilenames(0), NumDebugLocations(0) {} + + ~DebugLocTracker() { + NumFilenames = 0; + DebugFilenames.clear(); + DebugFilenamesMap.clear(); + DebugLocations.clear(); + DebugIdMap.clear(); + } + }; + +} // end namespace llvm + +#endif /* LLVM_CODEGEN_DEBUGLOC_H */ diff --git a/include/llvm/CodeGen/MachineFunction.h b/include/llvm/CodeGen/MachineFunction.h index 835c8a37c56..1397b846779 100644 --- a/include/llvm/CodeGen/MachineFunction.h +++ b/include/llvm/CodeGen/MachineFunction.h @@ -19,6 +19,7 @@ #define LLVM_CODEGEN_MACHINEFUNCTION_H #include "llvm/ADT/ilist.h" +#include "llvm/CodeGen/DebugLoc.h" #include "llvm/CodeGen/MachineBasicBlock.h" #include "llvm/Support/Annotation.h" #include "llvm/Support/Allocator.h" @@ -27,11 +28,11 @@ namespace llvm { class Function; -class TargetMachine; class MachineRegisterInfo; class MachineFrameInfo; class MachineConstantPool; class MachineJumpTableInfo; +class TargetMachine; template <> struct ilist_traits @@ -94,6 +95,9 @@ class MachineFunction : private Annotation { typedef ilist BasicBlockListType; BasicBlockListType BasicBlocks; + // Tracks debug locations. + DebugLocTracker DebugLocInfo; + public: MachineFunction(const Function *Fn, const TargetMachine &TM); ~MachineFunction(); @@ -302,6 +306,15 @@ public: /// DeleteMachineBasicBlock - Delete the given MachineBasicBlock. /// void DeleteMachineBasicBlock(MachineBasicBlock *MBB); + + //===--------------------------------------------------------------------===// + // Debug location. + // + + /// lookUpDebugLocId - Look up the DebugLocTuple index with the given + /// filename, line, and column. It may add a new filename and / or + /// a new DebugLocTuple. + unsigned lookUpDebugLocId(const char *Filename, unsigned Line, unsigned Col); }; //===--------------------------------------------------------------------===// diff --git a/lib/CodeGen/MachineFunction.cpp b/lib/CodeGen/MachineFunction.cpp index 8bae7bbb92c..0d442af7eda 100644 --- a/lib/CodeGen/MachineFunction.cpp +++ b/lib/CodeGen/MachineFunction.cpp @@ -378,6 +378,33 @@ MachineFunction& MachineFunction::get(const Function *F) return *mc; } +/// lookUpDebugLocId - Look up the DebugLocTuple index with the given +/// filename, line, and column. It may add a new filename and / or +/// a new DebugLocTuple. +unsigned MachineFunction::lookUpDebugLocId(const char *Filename, unsigned Line, + unsigned Col) { + unsigned FileId; + StringMap::iterator I = + DebugLocInfo.DebugFilenamesMap.find(Filename); + if (I != DebugLocInfo.DebugFilenamesMap.end()) + FileId = I->second; + else { + // Add a new filename. + FileId = DebugLocInfo.NumFilenames++; + DebugLocInfo.DebugFilenames.push_back(Filename); + DebugLocInfo.DebugFilenamesMap[Filename] = FileId; + } + + struct DebugLocTuple Tuple(FileId, Line, Col); + DebugIdMapType::iterator II = DebugLocInfo.DebugIdMap.find(Tuple); + if (II != DebugLocInfo.DebugIdMap.end()) + return II->second; + // Add a new tuple. + DebugLocInfo.DebugLocations.push_back(Tuple); + DebugLocInfo.DebugIdMap[Tuple] = DebugLocInfo.NumDebugLocations; + return DebugLocInfo.NumDebugLocations++; +} + //===----------------------------------------------------------------------===// // MachineFrameInfo implementation //===----------------------------------------------------------------------===// -- 2.34.1