Fix pruning invoke environment for StringBuilder append pattern.

Test: Extra test in 697-checker-string-append
Test: testrunner.py --host --optimizing
Bug: 19575890
Bug: 144300699
Change-Id: I3934eb01a62dbbed8b8f9f94ce3ba02051538bb0
diff --git a/compiler/optimizing/instruction_simplifier.cc b/compiler/optimizing/instruction_simplifier.cc
index 63c5965..d272bfa 100644
--- a/compiler/optimizing/instruction_simplifier.cc
+++ b/compiler/optimizing/instruction_simplifier.cc
@@ -2630,10 +2630,12 @@
   block->InsertInstructionBefore(append, invoke);
   invoke->ReplaceWith(append);
   // Copy environment, except for the StringBuilder uses.
-  for (size_t i = 0, size = invoke->GetEnvironment()->Size(); i != size; ++i) {
-    if (invoke->GetEnvironment()->GetInstructionAt(i) == sb) {
-      invoke->GetEnvironment()->RemoveAsUserOfInput(i);
-      invoke->GetEnvironment()->SetRawEnvAt(i, nullptr);
+  for (HEnvironment* env = invoke->GetEnvironment(); env != nullptr; env = env->GetParent()) {
+    for (size_t i = 0, size = env->Size(); i != size; ++i) {
+      if (env->GetInstructionAt(i) == sb) {
+        env->RemoveAsUserOfInput(i);
+        env->SetRawEnvAt(i, /*instruction=*/ nullptr);
+      }
     }
   }
   append->CopyEnvironmentFrom(invoke->GetEnvironment());
diff --git a/test/697-checker-string-append/src/Main.java b/test/697-checker-string-append/src/Main.java
index 4f2fa19..9017433 100644
--- a/test/697-checker-string-append/src/Main.java
+++ b/test/697-checker-string-append/src/Main.java
@@ -21,6 +21,7 @@
         testAppendStringAndString();
         testMiscelaneous();
         testNoArgs();
+        testInline();
         System.out.println("passed");
     }
 
@@ -233,6 +234,24 @@
                      $noinline$appendSLILC("x", 1L, 7, -1L, '\u0131'));
     }
 
+    public static String $inline$testInlineInner(StringBuilder sb, String s, int i) {
+        return sb.append(s).append(i).toString();
+    }
+
+    /// CHECK-START: java.lang.String Main.$noinline$testInlineOuter(java.lang.String, int) instruction_simplifier$after_inlining (before)
+    /// CHECK-NOT:              StringBuilderAppend
+
+    /// CHECK-START: java.lang.String Main.$noinline$testInlineOuter(java.lang.String, int) instruction_simplifier$after_inlining (after)
+    /// CHECK:                  StringBuilderAppend
+    public static String $noinline$testInlineOuter(String s, int i) {
+        StringBuilder sb = new StringBuilder();
+        return $inline$testInlineInner(sb, s, i);
+    }
+
+    public static void testInline() {
+        assertEquals("x42", $noinline$testInlineOuter("x", 42));
+    }
+
     /// CHECK-START: java.lang.String Main.$noinline$appendNothing() instruction_simplifier (before)
     /// CHECK-NOT:              StringBuilderAppend