@@ -52,11 +52,23 @@ void DIBuilder::trackIfUnresolved(MDNode *N) {
52
52
}
53
53
54
54
void DIBuilder::finalizeSubprogram (DISubprogram *SP) {
55
- auto PN = SubprogramTrackedNodes.find (SP);
56
- if (PN != SubprogramTrackedNodes.end ())
57
- SP->replaceRetainedNodes (
58
- MDTuple::get (VMContext, SmallVector<Metadata *, 16 >(PN->second .begin (),
59
- PN->second .end ())));
55
+ MDTuple *Temp = SP->getRetainedNodes ().get ();
56
+ if (!Temp || !Temp->isTemporary ())
57
+ return ;
58
+
59
+ SmallVector<Metadata *, 16 > RetainedNodes;
60
+
61
+ auto PV = PreservedVariables.find (SP);
62
+ if (PV != PreservedVariables.end ())
63
+ RetainedNodes.append (PV->second .begin (), PV->second .end ());
64
+
65
+ auto PL = PreservedLabels.find (SP);
66
+ if (PL != PreservedLabels.end ())
67
+ RetainedNodes.append (PL->second .begin (), PL->second .end ());
68
+
69
+ DINodeArray Node = getOrCreateArray (RetainedNodes);
70
+
71
+ TempMDTuple (Temp)->replaceAllUsesWith (Node.get ());
60
72
}
61
73
62
74
void DIBuilder::finalize () {
@@ -754,20 +766,26 @@ DIGlobalVariable *DIBuilder::createTempGlobalVariableFwdDecl(
754
766
755
767
static DILocalVariable *createLocalVariable (
756
768
LLVMContext &VMContext,
757
- SmallVectorImpl< TrackingMDNodeRef> &PreservedNodes ,
758
- DIScope *Context , StringRef Name, unsigned ArgNo, DIFile *File,
769
+ DenseMap<MDNode *, SmallVector< TrackingMDNodeRef, 1 >> &PreservedVariables ,
770
+ DIScope *Scope , StringRef Name, unsigned ArgNo, DIFile *File,
759
771
unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
760
772
uint32_t AlignInBits, DINodeArray Annotations = nullptr ) {
773
+ // FIXME: Why getNonCompileUnitScope()?
774
+ // FIXME: Why is "!Context" okay here?
761
775
// FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
762
776
// the only valid scopes)?
763
- auto *Scope = cast<DILocalScope>(Context);
764
- auto *Node = DILocalVariable::get (VMContext, Scope, Name, File, LineNo, Ty,
765
- ArgNo, Flags, AlignInBits, Annotations);
777
+ DIScope *Context = getNonCompileUnitScope (Scope);
778
+
779
+ auto *Node = DILocalVariable::get (
780
+ VMContext, cast_or_null<DILocalScope>(Context), Name, File, LineNo, Ty,
781
+ ArgNo, Flags, AlignInBits, Annotations);
766
782
if (AlwaysPreserve) {
767
783
// The optimizer may remove local variables. If there is an interest
768
784
// to preserve variable info in such situation then stash it in a
769
785
// named mdnode.
770
- PreservedNodes.emplace_back (Node);
786
+ DISubprogram *Fn = getDISubprogram (Scope);
787
+ assert (Fn && " Missing subprogram for local variable" );
788
+ PreservedVariables[Fn].emplace_back (Node);
771
789
}
772
790
return Node;
773
791
}
@@ -777,35 +795,35 @@ DILocalVariable *DIBuilder::createAutoVariable(DIScope *Scope, StringRef Name,
777
795
DIType *Ty, bool AlwaysPreserve,
778
796
DINode::DIFlags Flags,
779
797
uint32_t AlignInBits) {
780
- assert (Scope && isa<DILocalScope>(Scope) &&
781
- " Unexpected scope for a local variable." );
782
- return createLocalVariable (
783
- VMContext, getSubprogramNodesTrackingVector (Scope), Scope, Name,
784
- /* ArgNo */ 0 , File, LineNo, Ty, AlwaysPreserve, Flags, AlignInBits);
798
+ return createLocalVariable (VMContext, PreservedVariables, Scope, Name,
799
+ /* ArgNo */ 0 , File, LineNo, Ty, AlwaysPreserve,
800
+ Flags, AlignInBits);
785
801
}
786
802
787
803
DILocalVariable *DIBuilder::createParameterVariable (
788
804
DIScope *Scope, StringRef Name, unsigned ArgNo, DIFile *File,
789
805
unsigned LineNo, DIType *Ty, bool AlwaysPreserve, DINode::DIFlags Flags,
790
806
DINodeArray Annotations) {
791
807
assert (ArgNo && " Expected non-zero argument number for parameter" );
792
- assert (Scope && isa<DILocalScope>(Scope) &&
793
- " Unexpected scope for a local variable." );
794
- return createLocalVariable (
795
- VMContext, getSubprogramNodesTrackingVector (Scope), Scope, Name, ArgNo,
796
- File, LineNo, Ty, AlwaysPreserve, Flags, /* AlignInBits=*/ 0 , Annotations);
808
+ return createLocalVariable (VMContext, PreservedVariables, Scope, Name, ArgNo,
809
+ File, LineNo, Ty, AlwaysPreserve, Flags,
810
+ /* AlignInBits=*/ 0 , Annotations);
797
811
}
798
812
799
- DILabel *DIBuilder::createLabel (DIScope *Context, StringRef Name, DIFile *File,
800
- unsigned LineNo, bool AlwaysPreserve) {
801
- auto *Scope = cast<DILocalScope>(Context);
802
- auto *Node = DILabel::get (VMContext, Scope, Name, File, LineNo);
813
+ DILabel *DIBuilder::createLabel (DIScope *Scope, StringRef Name, DIFile *File,
814
+ unsigned LineNo, bool AlwaysPreserve) {
815
+ DIScope *Context = getNonCompileUnitScope (Scope);
816
+
817
+ auto *Node = DILabel::get (VMContext, cast_or_null<DILocalScope>(Context),
818
+ Name, File, LineNo);
803
819
804
820
if (AlwaysPreserve) {
805
821
// / The optimizer may remove labels. If there is an interest
806
822
// / to preserve label info in such situation then append it to
807
823
// / the list of retained nodes of the DISubprogram.
808
- getSubprogramNodesTrackingVector (Scope).emplace_back (Node);
824
+ DISubprogram *Fn = getDISubprogram (Scope);
825
+ assert (Fn && " Missing subprogram for label" );
826
+ PreservedLabels[Fn].emplace_back (Node);
809
827
}
810
828
return Node;
811
829
}
@@ -832,8 +850,9 @@ DISubprogram *DIBuilder::createFunction(
832
850
auto *Node = getSubprogram (
833
851
/* IsDistinct=*/ IsDefinition, VMContext, getNonCompileUnitScope (Context),
834
852
Name, LinkageName, File, LineNo, Ty, ScopeLine, nullptr , 0 , 0 , Flags,
835
- SPFlags, IsDefinition ? CUNode : nullptr , TParams, Decl, nullptr ,
836
- ThrownTypes, Annotations, TargetFuncName);
853
+ SPFlags, IsDefinition ? CUNode : nullptr , TParams, Decl,
854
+ MDTuple::getTemporary (VMContext, std::nullopt).release (), ThrownTypes,
855
+ Annotations, TargetFuncName);
837
856
838
857
if (IsDefinition)
839
858
AllSubprograms.push_back (Node);
0 commit comments