1274483SdimPull in r221709 from upstream llvm trunk (by Fr��d��ric Riss):
2274483Sdim
3274483Sdim  Totally forget deallocated SDNodes in SDDbgInfo.
4274483Sdim
5274483Sdim  What would happen before that commit is that the SDDbgValues associated with
6274483Sdim  a deallocated SDNode would be marked Invalidated, but SDDbgInfo would keep
7274483Sdim  a map entry keyed by the SDNode pointer pointing to this list of invalidated
8274483Sdim  SDDbgNodes. As the memory gets reused, the list might get wrongly associated
9274483Sdim  with another new SDNode. As the SDDbgValues are cloned when they are transfered,
10274483Sdim  this can lead to an exponential number of SDDbgValues being produced during
11274483Sdim  DAGCombine like in http://llvm.org/bugs/show_bug.cgi?id=20893
12274483Sdim
13274483Sdim  Note that the previous behavior wasn't really buggy as the invalidation made
14274483Sdim  sure that the SDDbgValues won't be used. This commit can be considered a
15274483Sdim  memory optimization and as such is really hard to validate in a unit-test.
16274483Sdim
17274483SdimThis should fix abnormally large memory usage and resulting OOM crashes
18274483Sdimwhen compiling certain ports with debug information.
19274483Sdim
20274483SdimReported by:	Dmitry Marakasov <amdmi3@amdmi3.ru>
21274483SdimUpstream PRs:	http://llvm.org/PR19031 http://llvm.org/PR20893
22274483Sdim
23274483SdimIntroduced here: http://svnweb.freebsd.org/changeset/base/274442
24274483Sdim
25274483SdimIndex: include/llvm/CodeGen/SelectionDAG.h
26274483Sdim===================================================================
27274483Sdim--- include/llvm/CodeGen/SelectionDAG.h
28274483Sdim+++ include/llvm/CodeGen/SelectionDAG.h
29274483Sdim@@ -127,6 +127,10 @@ class SDDbgInfo {
30274483Sdim       DbgValMap[Node].push_back(V);
31274483Sdim   }
32274483Sdim 
33274483Sdim+  /// \brief Invalidate all DbgValues attached to the node and remove
34274483Sdim+  /// it from the Node-to-DbgValues map.
35274483Sdim+  void erase(const SDNode *Node);
36274483Sdim+
37274483Sdim   void clear() {
38274483Sdim     DbgValMap.clear();
39274483Sdim     DbgValues.clear();
40274483SdimIndex: lib/CodeGen/SelectionDAG/SelectionDAG.cpp
41274483Sdim===================================================================
42274483Sdim--- lib/CodeGen/SelectionDAG/SelectionDAG.cpp
43274483Sdim+++ lib/CodeGen/SelectionDAG/SelectionDAG.cpp
44274483Sdim@@ -625,6 +625,15 @@ void SelectionDAG::DeleteNodeNotInCSEMaps(SDNode *
45274483Sdim   DeallocateNode(N);
46274483Sdim }
47274483Sdim 
48274483Sdim+void SDDbgInfo::erase(const SDNode *Node) {
49274483Sdim+  DbgValMapType::iterator I = DbgValMap.find(Node);
50274483Sdim+  if (I == DbgValMap.end())
51274483Sdim+    return;
52274483Sdim+  for (unsigned J = 0, N = I->second.size(); J != N; ++J)
53274483Sdim+    I->second[J]->setIsInvalidated();
54274483Sdim+  DbgValMap.erase(I);
55274483Sdim+}
56274483Sdim+
57274483Sdim void SelectionDAG::DeallocateNode(SDNode *N) {
58274483Sdim   if (N->OperandsNeedDelete)
59274483Sdim     delete[] N->OperandList;
60274483Sdim@@ -635,10 +644,9 @@ void SelectionDAG::DeallocateNode(SDNode *N) {
61274483Sdim 
62274483Sdim   NodeAllocator.Deallocate(AllNodes.remove(N));
63274483Sdim 
64274483Sdim-  // If any of the SDDbgValue nodes refer to this SDNode, invalidate them.
65274483Sdim-  ArrayRef<SDDbgValue*> DbgVals = DbgInfo->getSDDbgValues(N);
66274483Sdim-  for (unsigned i = 0, e = DbgVals.size(); i != e; ++i)
67274483Sdim-    DbgVals[i]->setIsInvalidated();
68274483Sdim+  // If any of the SDDbgValue nodes refer to this SDNode, invalidate
69274483Sdim+  // them and forget about that node.
70274483Sdim+  DbgInfo->erase(N);
71274483Sdim }
72274483Sdim 
73274483Sdim /// RemoveNodeFromCSEMaps - Take the specified node out of the CSE map that
74