Run a simplification pass before code generation.

The code generators assume things that only the instruction
simplier ensures. So it has to be run last in case previous
optimiziations broke those assumptions.

bug:21865464

(cherry picked from commit b2bdfce7f805b00668a2521b1c939a0aafb2be49)

Change-Id: Ibf4384a911e400eb7586bbf6b4edd6351034cbd8
diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc
index 64d6023..810b4f8 100644
--- a/compiler/optimizing/optimizing_compiler.cc
+++ b/compiler/optimizing/optimizing_compiler.cc
@@ -337,6 +337,7 @@
   BoundsCheckElimination bce(graph);
   ReferenceTypePropagation type_propagation(graph, dex_file, dex_compilation_unit, handles);
   InstructionSimplifier simplify2(graph, stats, "instruction_simplifier_after_types");
+  InstructionSimplifier simplify3(graph, stats, "instruction_simplifier_before_codegen");
 
   IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver);
 
@@ -357,6 +358,10 @@
     &type_propagation,
     &simplify2,
     &dce2,
+    // The codegen has a few assumptions that only the instruction simplifier can
+    // satisfy. For example, the code generator does not expect to see a
+    // HTypeConversion from a type to the same type.
+    &simplify3,
   };
 
   RunOptimizations(optimizations, arraysize(optimizations), pass_info_printer);
diff --git a/test/505-simplifier-type-propagation/expected.txt b/test/505-simplifier-type-propagation/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/505-simplifier-type-propagation/expected.txt
diff --git a/test/505-simplifier-type-propagation/info.txt b/test/505-simplifier-type-propagation/info.txt
new file mode 100644
index 0000000..cd84432
--- /dev/null
+++ b/test/505-simplifier-type-propagation/info.txt
@@ -0,0 +1,3 @@
+Regression test for the optimizing compiler, where
+the code generators did not expect type conversion
+instructions from one type to the same type.
diff --git a/test/505-simplifier-type-propagation/src/Main.java b/test/505-simplifier-type-propagation/src/Main.java
new file mode 100644
index 0000000..780cb34
--- /dev/null
+++ b/test/505-simplifier-type-propagation/src/Main.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+class Main {
+  public static void main(String[] args) {
+    byte result = bar((byte)2);
+    if (result != 2) {
+      throw new Error("Expected 2, got " + result);
+    }
+  }
+
+  public static byte bar(byte myByte) {
+    int a = 0;
+    // The following call will be inlined, which will make
+    // the type conversion below from byte to byte.
+    if ($inline$foo()) {
+      a = myByte;
+    }
+    return (byte)a;
+  }
+
+  public static boolean $inline$foo() {
+    return true;
+  }
+}