[llvm] r233585 - IR: Use the new DebugLoc API, NFC

Duncan P. N. Exon Smith dexonsmith at apple.com
Mon Mar 30 12:40:05 PDT 2015


Author: dexonsmith
Date: Mon Mar 30 14:40:05 2015
New Revision: 233585

URL: http://llvm.org/viewvc/llvm-project?rev=233585&view=rev
Log:
IR: Use the new DebugLoc API, NFC

Update lib/IR and lib/Bitcode to use the new `DebugLoc` API.  Added an
explicit conversion to `bool` (avoiding a conversion to `MDLocation`),
since a couple of these use cases need to handle broken code.

Modified:
    llvm/trunk/include/llvm/IR/DebugInfo.h
    llvm/trunk/include/llvm/IR/DebugLoc.h
    llvm/trunk/include/llvm/IR/IRBuilder.h
    llvm/trunk/include/llvm/IR/Instruction.h
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
    llvm/trunk/lib/IR/Core.cpp
    llvm/trunk/lib/IR/DebugInfo.cpp
    llvm/trunk/lib/IR/DiagnosticInfo.cpp
    llvm/trunk/lib/IR/Metadata.cpp

Modified: llvm/trunk/include/llvm/IR/DebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/DebugInfo.h?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/DebugInfo.h (original)
+++ llvm/trunk/include/llvm/IR/DebugInfo.h Mon Mar 30 14:40:05 2015
@@ -1207,6 +1207,7 @@ public:
 class DILocation : public DIDescriptor {
 public:
   explicit DILocation(const MDNode *N) : DIDescriptor(N) {}
+  DILocation(const MDLocation *N) : DIDescriptor(N) {}
 
   MDLocation *get() const {
     return cast_or_null<MDLocation>(DIDescriptor::get());

Modified: llvm/trunk/include/llvm/IR/DebugLoc.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/DebugLoc.h?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/DebugLoc.h (original)
+++ llvm/trunk/include/llvm/IR/DebugLoc.h Mon Mar 30 14:40:05 2015
@@ -58,10 +58,23 @@ namespace llvm {
     /// IR.
     explicit DebugLoc(MDNode *N);
 
+    /// \brief Get the underlying \a MDLocation.
+    ///
+    /// \pre !*this or \c isa<MDLocation>(getAsMDNode()).
+    /// @{
     MDLocation *get() const;
     operator MDLocation *() const { return get(); }
     MDLocation *operator->() const { return get(); }
     MDLocation &operator*() const { return *get(); }
+    /// @}
+
+    /// \brief Check for null.
+    ///
+    /// Check for null in a way that is safe with broken debug info.  Unlike
+    /// the conversion to \c MDLocation, this doesn't require that \c Loc is of
+    /// the right type.  Important for cases like \a llvm::StripDebugInfo() and
+    /// \a Instruction::hasMetadata().
+    explicit operator bool() const { return Loc; }
 
     /// \brief Check whether this has a trivial destructor.
     bool hasTrivialDestructor() const { return Loc.hasTrivialDestructor(); }

Modified: llvm/trunk/include/llvm/IR/IRBuilder.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/IRBuilder.h?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/IRBuilder.h (original)
+++ llvm/trunk/include/llvm/IR/IRBuilder.h Mon Mar 30 14:40:05 2015
@@ -123,7 +123,7 @@ public:
   /// \brief If this builder has a current debug location, set it on the
   /// specified instruction.
   void SetInstDebugLocation(Instruction *I) const {
-    if (!CurDbgLocation.isUnknown())
+    if (CurDbgLocation)
       I->setDebugLoc(CurDbgLocation);
   }
 

Modified: llvm/trunk/include/llvm/IR/Instruction.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Instruction.h?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Instruction.h (original)
+++ llvm/trunk/include/llvm/IR/Instruction.h Mon Mar 30 14:40:05 2015
@@ -134,9 +134,7 @@ public:
 
   /// hasMetadata() - Return true if this instruction has any metadata attached
   /// to it.
-  bool hasMetadata() const {
-    return !DbgLoc.isUnknown() || hasMetadataHashEntry();
-  }
+  bool hasMetadata() const { return DbgLoc || hasMetadataHashEntry(); }
 
   /// hasMetadataOtherThanDebugLoc - Return true if this instruction has
   /// metadata attached to it other than a debug location.

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Mon Mar 30 14:40:05 2015
@@ -2089,7 +2089,7 @@ static void WriteFunction(const Function
 
   bool NeedsMetadataAttachment = false;
 
-  DebugLoc LastDL;
+  MDLocation *LastDL = nullptr;
 
   // Finally, emit all the instructions, in order.
   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
@@ -2104,10 +2104,9 @@ static void WriteFunction(const Function
       NeedsMetadataAttachment |= I->hasMetadataOtherThanDebugLoc();
 
       // If the instruction has a debug location, emit it.
-      DebugLoc DL = I->getDebugLoc();
-      if (DL.isUnknown()) {
+      MDLocation *DL = I->getDebugLoc();
+      if (!DL)
         continue;
-      }
 
       if (DL == LastDL) {
         // Just repeat the same debug loc as last time.
@@ -2115,18 +2114,12 @@ static void WriteFunction(const Function
         continue;
       }
 
-      MDNode *Scope, *IA;
-      DL.getScopeAndInlinedAt(Scope, IA, I->getContext());
-      assert(Scope && "Expected valid scope");
-
-      Vals.push_back(DL.getLine());
-      Vals.push_back(DL.getCol());
-      Vals.push_back(VE.getMetadataOrNullID(Scope));
-      Vals.push_back(VE.getMetadataOrNullID(IA));
+      Vals.push_back(DL->getLine());
+      Vals.push_back(DL->getColumn());
+      Vals.push_back(VE.getMetadataOrNullID(DL->getScope()));
+      Vals.push_back(VE.getMetadataOrNullID(DL->getInlinedAt()));
       Stream.EmitRecord(bitc::FUNC_CODE_DEBUG_LOC, Vals);
       Vals.clear();
-
-      LastDL = DL;
     }
 
   // Emit names for all the instructions etc.

Modified: llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp Mon Mar 30 14:40:05 2015
@@ -373,12 +373,10 @@ ValueEnumerator::ValueEnumerator(const M
         for (unsigned i = 0, e = MDs.size(); i != e; ++i)
           EnumerateMetadata(MDs[i].second);
 
-        if (!I.getDebugLoc().isUnknown()) {
-          MDNode *Scope, *IA;
-          I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
-          if (Scope) EnumerateMetadata(Scope);
-          if (IA) EnumerateMetadata(IA);
-        }
+        // Don't enumerate the location directly -- it has a special record
+        // type -- but enumerate its operands.
+        if (MDLocation *L = I.getDebugLoc())
+          EnumerateMDNodeOperands(L);
       }
   }
 

Modified: llvm/trunk/lib/IR/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Core.cpp?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Core.cpp (original)
+++ llvm/trunk/lib/IR/Core.cpp Mon Mar 30 14:40:05 2015
@@ -2181,14 +2181,13 @@ void LLVMDisposeBuilder(LLVMBuilderRef B
 void LLVMSetCurrentDebugLocation(LLVMBuilderRef Builder, LLVMValueRef L) {
   MDNode *Loc =
       L ? cast<MDNode>(unwrap<MetadataAsValue>(L)->getMetadata()) : nullptr;
-  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc::getFromDILocation(Loc));
+  unwrap(Builder)->SetCurrentDebugLocation(DebugLoc(Loc));
 }
 
 LLVMValueRef LLVMGetCurrentDebugLocation(LLVMBuilderRef Builder) {
   LLVMContext &Context = unwrap(Builder)->getContext();
   return wrap(MetadataAsValue::get(
-      Context,
-      unwrap(Builder)->getCurrentDebugLocation().getAsMDNode(Context)));
+      Context, unwrap(Builder)->getCurrentDebugLocation().getAsMDNode()));
 }
 
 void LLVMSetInstDebugLocation(LLVMBuilderRef Builder, LLVMValueRef Inst) {

Modified: llvm/trunk/lib/IR/DebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/DebugInfo.cpp?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/lib/IR/DebugInfo.cpp (original)
+++ llvm/trunk/lib/IR/DebugInfo.cpp Mon Mar 30 14:40:05 2015
@@ -343,8 +343,7 @@ bool DISubprogram::Verify() const {
   if (auto *F = getFunction()) {
     for (auto &BB : *F) {
       for (auto &I : BB) {
-        MDLocation *DL =
-            cast_or_null<MDLocation>(I.getDebugLoc().getAsMDNode());
+        MDLocation *DL = I.getDebugLoc();
         if (!DL)
           continue;
 
@@ -585,12 +584,12 @@ DISubprogram llvm::getDISubprogram(const
   // We look for the first instr that has a debug annotation leading back to F.
   for (auto &BB : *F) {
     auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
-      return !Inst.getDebugLoc().isUnknown();
+      return Inst.getDebugLoc();
     });
     if (Inst == BB.end())
       continue;
     DebugLoc DLoc = Inst->getDebugLoc();
-    const MDNode *Scope = DLoc.getScopeNode();
+    const MDNode *Scope = DLoc.getInlinedAtScope();
     DISubprogram Subprogram = getDISubprogram(Scope);
     return Subprogram.describes(F) ? Subprogram : DISubprogram();
   }
@@ -889,10 +888,10 @@ void DIDescriptor::print(raw_ostream &OS
 
 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
                           const LLVMContext &Ctx) {
-  if (DL.isUnknown())
+  if (!DL)
     return;
 
-  DIScope Scope(DL.getScope(Ctx));
+  DIScope Scope(DL.getScope());
   assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
   // Omit the directory, because it's likely to be long and uninteresting.
   CommentOS << Scope.getFilename();
@@ -900,8 +899,8 @@ static void printDebugLoc(DebugLoc DL, r
   if (DL.getCol() != 0)
     CommentOS << ':' << DL.getCol();
 
-  DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
-  if (InlinedAtDL.isUnknown())
+  DebugLoc InlinedAtDL = DL.getInlinedAt();
+  if (!InlinedAtDL)
     return;
 
   CommentOS << " @[ ";
@@ -914,9 +913,8 @@ void DIVariable::printExtendedName(raw_o
   StringRef Res = getName();
   if (!Res.empty())
     OS << Res << "," << getLineNumber();
-  if (MDNode *InlinedAt = getInlinedAt()) {
-    DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
-    if (!InlinedAtDL.isUnknown()) {
+  if (auto *InlinedAt = get()->getInlinedAt()) {
+    if (DebugLoc InlinedAtDL = InlinedAt) {
       OS << " @[";
       printDebugLoc(InlinedAtDL, OS, Ctx);
       OS << "]";
@@ -985,7 +983,7 @@ bool llvm::StripDebugInfo(Module &M) {
          ++FI)
       for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
            ++BI) {
-        if (!BI->getDebugLoc().isUnknown()) {
+        if (BI->getDebugLoc()) {
           Changed = true;
           BI->setDebugLoc(DebugLoc());
         }

Modified: llvm/trunk/lib/IR/DiagnosticInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/DiagnosticInfo.cpp?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/lib/IR/DiagnosticInfo.cpp (original)
+++ llvm/trunk/lib/IR/DiagnosticInfo.cpp Mon Mar 30 14:40:05 2015
@@ -129,13 +129,14 @@ void DiagnosticInfoSampleProfile::print(
 }
 
 bool DiagnosticInfoOptimizationBase::isLocationAvailable() const {
-  return !getDebugLoc().isUnknown();
+  return getDebugLoc();
 }
 
 void DiagnosticInfoOptimizationBase::getLocation(StringRef *Filename,
                                                  unsigned *Line,
                                                  unsigned *Column) const {
-  DILocation DIL(getDebugLoc().getAsMDNode(getFunction().getContext()));
+  MDLocation *L = getDebugLoc();
+  DILocation DIL = L;
   *Filename = DIL.getFilename();
   *Line = DIL.getLineNumber();
   *Column = DIL.getColumnNumber();

Modified: llvm/trunk/lib/IR/Metadata.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Metadata.cpp?rev=233585&r1=233584&r2=233585&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Metadata.cpp (original)
+++ llvm/trunk/lib/IR/Metadata.cpp Mon Mar 30 14:40:05 2015
@@ -1035,7 +1035,7 @@ void Instruction::setMetadata(unsigned K
 
   // Handle 'dbg' as a special case since it is not stored in the hash table.
   if (KindID == LLVMContext::MD_dbg) {
-    DbgLoc = DebugLoc::getFromDILocation(Node);
+    DbgLoc = DebugLoc(Node);
     return;
   }
   
@@ -1114,7 +1114,7 @@ void Instruction::getAllMetadataImpl(
   Result.clear();
   
   // Handle 'dbg' as a special case since it is not stored in the hash table.
-  if (!DbgLoc.isUnknown()) {
+  if (DbgLoc) {
     Result.push_back(
         std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
     if (!hasMetadataHashEntry()) return;





More information about the llvm-commits mailing list