diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 21adec98ceb9307ea26fe36fb24319a8bffe09e1..2ef626d05c8e7a03990b8714e252d3ba7461f518 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,5 +1,9 @@
 2009-11-03  Jason Merrill  <jason@redhat.com>
 
+	PR c++/36959
+	* decl2.c (cxx_callgraph_analyze_expr): Don't reference a function
+	just because a static variable in it is needed unless -frepo.
+
 	PR c++/41876
 	* parser.c (cp_parser_type_specifier_seq): Rename is_condition to
 	is_declaration.
diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c
index b1fe4b9e31c0b6962d886bb259205aae3c9fead7..dbb9fb47a6434fc6b5f85f3527d910c0cf574339 100644
--- a/gcc/cp/decl2.c
+++ b/gcc/cp/decl2.c
@@ -3310,6 +3310,7 @@ cxx_callgraph_analyze_expr (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED)
 	    mark_decl_referenced (vtbl);
 	}
       else if (DECL_CONTEXT (t)
+	       && flag_use_repository
 	       && TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)
 	/* If we need a static variable in a function, then we
 	   need the containing function.  */
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 4a499e28f8a5c3f43d9d770c352a075d62fa7a4f..f525db0b2662c04f215c935e17dbc5ba76c6b91b 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2009-11-03  Jason Merrill  <jason@redhat.com>
+
+	PR c++/36959
+	* g++.dg/opt/inline16.C: New.
+
 2009-11-03  Eric Botcazou  <ebotcazou@adacore.com>
 
 	* gnat.dg/stack_check.adb1: New test.
diff --git a/gcc/testsuite/g++.dg/opt/inline16.C b/gcc/testsuite/g++.dg/opt/inline16.C
new file mode 100644
index 0000000000000000000000000000000000000000..6ee6d76d925dfbf3e8bf31df6a1649d54e477e60
--- /dev/null
+++ b/gcc/testsuite/g++.dg/opt/inline16.C
@@ -0,0 +1,19 @@
+// PR c++/36959
+// We shouldn't have to emit fromSlotB just because we need shuf_BZZZ.
+// { dg-options -O }
+// { dg-final { scan-assembler-not "_ZL9fromSlotBv" } }
+
+static inline int *fromSlotB(void)
+{
+  static int shuf_BZZZ = 1;
+  return &shuf_BZZZ;
+}
+
+int *p;
+
+int main(void)
+{
+  p = fromSlotB();
+  return (*p != 1);
+}
+