DebugInfo: Gut DIType and subclasses
[oota-llvm.git] / lib / IR / DIBuilder.cpp
index ba314b4546240539ead084772812bc480adcef46..067a7ee820ff1f81c87c29fe6f4cd25f405b4df1 100644 (file)
@@ -75,7 +75,7 @@ void DIBuilder::trackIfUnresolved(MDNode *N) {
 
 void DIBuilder::finalize() {
   DIArray Enums = getOrCreateArray(AllEnumTypes);
-  TempEnumTypes->replaceAllUsesWith(Enums);
+  TempEnumTypes->replaceAllUsesWith(Enums.get());
 
   SmallVector<Metadata *, 16> RetainValues;
   // Declarations and definitions of the same type may be retained. Some
@@ -87,27 +87,27 @@ void DIBuilder::finalize() {
     if (RetainSet.insert(AllRetainTypes[I]).second)
       RetainValues.push_back(AllRetainTypes[I]);
   DIArray RetainTypes = getOrCreateArray(RetainValues);
-  TempRetainTypes->replaceAllUsesWith(RetainTypes);
+  TempRetainTypes->replaceAllUsesWith(RetainTypes.get());
 
   DIArray SPs = getOrCreateArray(AllSubprograms);
-  TempSubprograms->replaceAllUsesWith(SPs);
-  for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
-    DISubprogram SP = cast<MDSubprogram>(SPs.getElement(i));
-    if (MDNode *Temp = SP.getVariablesNodes()) {
+  TempSubprograms->replaceAllUsesWith(SPs.get());
+  for (unsigned i = 0, e = SPs.size(); i != e; ++i) {
+    DISubprogram SP = cast<MDSubprogram>(SPs[i]);
+    if (MDTuple *Temp = SP->getVariables().get()) {
       const auto &PV = PreservedVariables.lookup(SP);
       SmallVector<Metadata *, 4> Variables(PV.begin(), PV.end());
       DIArray AV = getOrCreateArray(Variables);
-      Temp->replaceAllUsesWith(AV);
+      TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
     }
   }
 
   DIArray GVs = getOrCreateArray(AllGVs);
-  TempGVs->replaceAllUsesWith(GVs);
+  TempGVs->replaceAllUsesWith(GVs.get());
 
   SmallVector<Metadata *, 16> RetainValuesI(AllImportedModules.begin(),
                                             AllImportedModules.end());
   DIArray IMs = getOrCreateArray(RetainValuesI);
-  TempImportedModules->replaceAllUsesWith(IMs);
+  TempImportedModules->replaceAllUsesWith(IMs.get());
 
   // Now that all temp nodes have been replaced or deleted, resolve remaining
   // cycles.
@@ -143,18 +143,19 @@ DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
 
   // TODO: Once we make MDCompileUnit distinct, stop using temporaries here
   // (just start with operands assigned to nullptr).
-  TempEnumTypes = MDTuple::getTemporary(VMContext, None).release();
-  TempRetainTypes = MDTuple::getTemporary(VMContext, None).release();
-  TempSubprograms = MDTuple::getTemporary(VMContext, None).release();
-  TempGVs = MDTuple::getTemporary(VMContext, None).release();
-  TempImportedModules = MDTuple::getTemporary(VMContext, None).release();
+  TempEnumTypes = MDTuple::getTemporary(VMContext, None);
+  TempRetainTypes = MDTuple::getTemporary(VMContext, None);
+  TempSubprograms = MDTuple::getTemporary(VMContext, None);
+  TempGVs = MDTuple::getTemporary(VMContext, None);
+  TempImportedModules = MDTuple::getTemporary(VMContext, None);
 
   // TODO: Switch to getDistinct().  We never want to merge compile units based
   // on contents.
   MDCompileUnit *CUNode = MDCompileUnit::get(
       VMContext, Lang, MDFile::get(VMContext, Filename, Directory), Producer,
-      isOptimized, Flags, RunTimeVer, SplitName, Kind, TempEnumTypes,
-      TempRetainTypes, TempSubprograms, TempGVs, TempImportedModules);
+      isOptimized, Flags, RunTimeVer, SplitName, Kind, TempEnumTypes.get(),
+      TempRetainTypes.get(), TempSubprograms.get(), TempGVs.get(),
+      TempImportedModules.get());
 
   // Create a named metadata so that it is easier to find cu in a module.
   // Note that we only generate this when the caller wants to actually
@@ -176,7 +177,7 @@ createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
                      SmallVectorImpl<TrackingMDNodeRef> &AllImportedModules) {
   DIImportedEntity M =
       MDImportedEntity::get(C, Tag, Context, DebugNodeRef(NS), Line, Name);
-  AllImportedModules.emplace_back(M.get());
+  AllImportedModules.emplace_back(M);
   return M;
 }
 
@@ -383,7 +384,7 @@ DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
                                        DIType Ty, DIArray Val) {
   return createTemplateValueParameterHelper(
       VMContext, dwarf::DW_TAG_GNU_template_parameter_pack, Context, Name, Ty,
-      Val);
+      Val.get());
 }
 
 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
@@ -486,7 +487,7 @@ DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
   auto *R =
       MDCompositeType::get(VMContext, dwarf::DW_TAG_array_type, "", nullptr, 0,
                            nullptr, MDTypeRef::get(Ty), Size, AlignInBits, 0,
-                           DIType::FlagVector, Subscripts, 0, nullptr);
+                           DebugNode::FlagVector, Subscripts, 0, nullptr);
   trackIfUnresolved(R);
   return R;
 }
@@ -500,27 +501,25 @@ static DIType createTypeWithFlags(LLVMContext &Context, DIType Ty,
 
 DIType DIBuilder::createArtificialType(DIType Ty) {
   // FIXME: Restrict this to the nodes where it's valid.
-  if (Ty.isArtificial())
+  if (Ty->isArtificial())
     return Ty;
-  return createTypeWithFlags(VMContext, Ty, DIType::FlagArtificial);
+  return createTypeWithFlags(VMContext, Ty, DebugNode::FlagArtificial);
 }
 
 DIType DIBuilder::createObjectPointerType(DIType Ty) {
   // FIXME: Restrict this to the nodes where it's valid.
-  if (Ty.isObjectPointer())
+  if (Ty->isObjectPointer())
     return Ty;
-  unsigned Flags = DIType::FlagObjectPointer | DIType::FlagArtificial;
+  unsigned Flags = DebugNode::FlagObjectPointer | DebugNode::FlagArtificial;
   return createTypeWithFlags(VMContext, Ty, Flags);
 }
 
 void DIBuilder::retainType(DIType T) {
-  assert(T.get() && "Expected non-null type");
+  assert(T && "Expected non-null type");
   AllRetainTypes.emplace_back(T);
 }
 
-DIBasicType DIBuilder::createUnspecifiedParameter() {
-  return DIBasicType();
-}
+DIBasicType DIBuilder::createUnspecifiedParameter() { return nullptr; }
 
 DICompositeType
 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
@@ -578,9 +577,9 @@ DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
 
 static void checkGlobalVariableScope(DIDescriptor Context) {
 #ifndef NDEBUG
-  if (DICompositeType CT =
+  if (auto *CT =
           dyn_cast_or_null<MDCompositeType>(getNonCompileUnitScope(Context)))
-    assert(!CT.getIdentifier() &&
+    assert(CT->getIdentifier().empty() &&
            "Context of a global variable should not be a type with identifier");
 #endif
 }
@@ -593,8 +592,8 @@ DIGlobalVariable DIBuilder::createGlobalVariable(
 
   auto *N = MDGlobalVariable::get(
       VMContext, cast_or_null<MDScope>(Context.get()), Name, LinkageName, F,
-      LineNumber, MDTypeRef::get(Ty), isLocalToUnit, true,
-      getConstantOrNull(Val), cast_or_null<MDDerivedType>(Decl));
+      LineNumber, MDTypeRef::get(Ty), isLocalToUnit, true, Val,
+      cast_or_null<MDDerivedType>(Decl));
   AllGVs.push_back(N);
   return N;
 }
@@ -607,7 +606,7 @@ DIGlobalVariable DIBuilder::createTempGlobalVariableFwdDecl(
 
   return MDGlobalVariable::getTemporary(
              VMContext, cast_or_null<MDScope>(Context.get()), Name, LinkageName,
-             F, LineNumber, MDTypeRef::get(Ty), isLocalToUnit, false, getConstantOrNull(Val),
+             F, LineNumber, MDTypeRef::get(Ty), isLocalToUnit, false, Val,
              cast_or_null<MDDerivedType>(Decl)).release();
 }
 
@@ -674,14 +673,13 @@ DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
                                        unsigned ScopeLine, unsigned Flags,
                                        bool isOptimized, Function *Fn,
                                        MDNode *TParams, MDNode *Decl) {
-  assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
+  assert(Ty->getTag() == dwarf::DW_TAG_subroutine_type &&
          "function types should be subroutines");
   auto *Node = MDSubprogram::get(
       VMContext, MDScopeRef::get(DIScope(getNonCompileUnitScope(Context))),
-      Name, LinkageName, File.get(), LineNo,
-      cast_or_null<MDSubroutineType>(Ty.get()), isLocalToUnit, isDefinition,
-      ScopeLine, nullptr, 0, 0, Flags, isOptimized, getConstantOrNull(Fn),
-      cast_or_null<MDTuple>(TParams), cast_or_null<MDSubprogram>(Decl),
+      Name, LinkageName, File, LineNo, cast_or_null<MDSubroutineType>(Ty),
+      isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, Flags, isOptimized,
+      Fn, cast_or_null<MDTuple>(TParams), cast_or_null<MDSubprogram>(Decl),
       MDTuple::getTemporary(VMContext, None).release());
 
   if (isDefinition)
@@ -701,10 +699,9 @@ DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
   return MDSubprogram::getTemporary(
              VMContext,
              MDScopeRef::get(DIScope(getNonCompileUnitScope(Context))), Name,
-             LinkageName, File.get(), LineNo,
-             cast_or_null<MDSubroutineType>(Ty.get()), isLocalToUnit,
-             isDefinition, ScopeLine, nullptr, 0, 0, Flags, isOptimized,
-             getConstantOrNull(Fn), cast_or_null<MDTuple>(TParams),
+             LinkageName, File, LineNo, cast_or_null<MDSubroutineType>(Ty),
+             isLocalToUnit, isDefinition, ScopeLine, nullptr, 0, 0, Flags,
+             isOptimized, Fn, cast_or_null<MDTuple>(TParams),
              cast_or_null<MDSubprogram>(Decl), nullptr)
       .release();
 }
@@ -717,18 +714,17 @@ DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
                                      DIType VTableHolder, unsigned Flags,
                                      bool isOptimized, Function *Fn,
                                      MDNode *TParam) {
-  assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
+  assert(Ty->getTag() == dwarf::DW_TAG_subroutine_type &&
          "function types should be subroutines");
   assert(getNonCompileUnitScope(Context) &&
          "Methods should have both a Context and a context that isn't "
          "the compile unit.");
   // FIXME: Do we want to use different scope/lines?
   auto *SP = MDSubprogram::get(
-      VMContext, MDScopeRef::get(cast<MDScope>(Context)), Name, LinkageName,
-      F.get(), LineNo, cast_or_null<MDSubroutineType>(Ty.get()), isLocalToUnit,
-      isDefinition, LineNo, MDTypeRef::get(VTableHolder), VK, VIndex, Flags,
-      isOptimized, getConstantOrNull(Fn), cast_or_null<MDTuple>(TParam),
-      nullptr, nullptr);
+      VMContext, MDScopeRef::get(cast<MDScope>(Context)), Name, LinkageName, F,
+      LineNo, cast_or_null<MDSubroutineType>(Ty), isLocalToUnit, isDefinition,
+      LineNo, MDTypeRef::get(VTableHolder), VK, VIndex, Flags, isOptimized, Fn,
+      cast_or_null<MDTuple>(TParam), nullptr, nullptr);
 
   if (isDefinition)
     AllSubprograms.push_back(SP);
@@ -745,8 +741,7 @@ DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
                                                      DIFile File,
                                                      unsigned Discriminator) {
-  return MDLexicalBlockFile::get(VMContext, Scope, File.getFileNode(),
-                                 Discriminator);
+  return MDLexicalBlockFile::get(VMContext, Scope, File, Discriminator);
 }
 
 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
@@ -754,7 +749,7 @@ DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
   // Make these distinct, to avoid merging two lexical blocks on the same
   // file/line/column.
   return MDLexicalBlock::getDistinct(VMContext, getNonCompileUnitScope(Scope),
-                                     File.getFileNode(), Line, Col);
+                                     File, Line, Col);
 }
 
 static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
@@ -762,10 +757,19 @@ static Value *getDbgIntrinsicValueImpl(LLVMContext &VMContext, Value *V) {
   return MetadataAsValue::get(VMContext, ValueAsMetadata::get(V));
 }
 
+static Instruction *withDebugLoc(Instruction *I, const MDLocation *DL) {
+  I->setDebugLoc(const_cast<MDLocation *>(DL));
+  return I;
+}
+
 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
-                                      DIExpression Expr,
+                                      DIExpression Expr, const MDLocation *DL,
                                       Instruction *InsertBefore) {
   assert(VarInfo && "empty or invalid DIVariable passed to dbg.declare");
+  assert(DL && "Expected debug loc");
+  assert(DL->getScope()->getSubprogram() ==
+             VarInfo->getScope()->getSubprogram() &&
+         "Expected matching subprograms");
   if (!DeclareFn)
     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
 
@@ -774,13 +778,17 @@ Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
   Value *Args[] = {getDbgIntrinsicValueImpl(VMContext, Storage),
                    MetadataAsValue::get(VMContext, VarInfo),
                    MetadataAsValue::get(VMContext, Expr)};
-  return CallInst::Create(DeclareFn, Args, "", InsertBefore);
+  return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertBefore), DL);
 }
 
 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
-                                      DIExpression Expr,
+                                      DIExpression Expr, const MDLocation *DL,
                                       BasicBlock *InsertAtEnd) {
   assert(VarInfo && "empty or invalid DIVariable passed to dbg.declare");
+  assert(DL && "Expected debug loc");
+  assert(DL->getScope()->getSubprogram() ==
+             VarInfo->getScope()->getSubprogram() &&
+         "Expected matching subprograms");
   if (!DeclareFn)
     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
 
@@ -793,17 +801,21 @@ Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
   // If this block already has a terminator then insert this intrinsic
   // before the terminator.
   if (TerminatorInst *T = InsertAtEnd->getTerminator())
-    return CallInst::Create(DeclareFn, Args, "", T);
-  else
-    return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
+    return withDebugLoc(CallInst::Create(DeclareFn, Args, "", T), DL);
+  return withDebugLoc(CallInst::Create(DeclareFn, Args, "", InsertAtEnd), DL);
 }
 
 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
                                                 DIVariable VarInfo,
                                                 DIExpression Expr,
+                                                const MDLocation *DL,
                                                 Instruction *InsertBefore) {
   assert(V && "no value passed to dbg.value");
   assert(VarInfo && "empty or invalid DIVariable passed to dbg.value");
+  assert(DL && "Expected debug loc");
+  assert(DL->getScope()->getSubprogram() ==
+             VarInfo->getScope()->getSubprogram() &&
+         "Expected matching subprograms");
   if (!ValueFn)
     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
 
@@ -813,15 +825,20 @@ Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
                    ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
                    MetadataAsValue::get(VMContext, VarInfo),
                    MetadataAsValue::get(VMContext, Expr)};
-  return CallInst::Create(ValueFn, Args, "", InsertBefore);
+  return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertBefore), DL);
 }
 
 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
                                                 DIVariable VarInfo,
                                                 DIExpression Expr,
+                                                const MDLocation *DL,
                                                 BasicBlock *InsertAtEnd) {
   assert(V && "no value passed to dbg.value");
   assert(VarInfo && "empty or invalid DIVariable passed to dbg.value");
+  assert(DL && "Expected debug loc");
+  assert(DL->getScope()->getSubprogram() ==
+             VarInfo->getScope()->getSubprogram() &&
+         "Expected matching subprograms");
   if (!ValueFn)
     ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
 
@@ -831,11 +848,16 @@ Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
                    ConstantInt::get(Type::getInt64Ty(VMContext), Offset),
                    MetadataAsValue::get(VMContext, VarInfo),
                    MetadataAsValue::get(VMContext, Expr)};
-  return CallInst::Create(ValueFn, Args, "", InsertAtEnd);
+
+  return withDebugLoc(CallInst::Create(ValueFn, Args, "", InsertAtEnd), DL);
 }
 
 void DIBuilder::replaceVTableHolder(DICompositeType &T, DICompositeType VTableHolder) {
-  T.setContainingType(VTableHolder);
+  {
+    TypedTrackingMDRef<MDCompositeTypeBase> N(T);
+    N->replaceVTableHolder(MDTypeRef::get(VTableHolder));
+    T = N.get();
+  }
 
   // If this didn't create a self-reference, just return.
   if (T != VTableHolder)
@@ -851,7 +873,14 @@ void DIBuilder::replaceVTableHolder(DICompositeType &T, DICompositeType VTableHo
 
 void DIBuilder::replaceArrays(DICompositeType &T, DIArray Elements,
                               DIArray TParams) {
-  T.setArrays(Elements, TParams);
+  {
+    TypedTrackingMDRef<MDCompositeTypeBase> N(T);
+    if (Elements)
+      N->replaceElements(Elements);
+    if (TParams)
+      N->replaceTemplateParams(MDTemplateParameterArray(TParams));
+    T = N.get();
+  }
 
   // If T isn't resolved, there's no problem.
   if (!T->isResolved())
@@ -861,7 +890,7 @@ void DIBuilder::replaceArrays(DICompositeType &T, DIArray Elements,
   // arrays explicitly if they're unresolved, or else the cycles will be
   // orphaned.
   if (Elements)
-    trackIfUnresolved(Elements);
+    trackIfUnresolved(Elements.get());
   if (TParams)
-    trackIfUnresolved(TParams);
+    trackIfUnresolved(TParams.get());
 }