Add support for int-to-short in the optimizing compiler.

- Add support for the int-to-short Dex instruction in the
  optimizing compiler.
- Generate x86, x86-64 and ARM (but not ARM64) code for
  byte to short, int to short and char to short
  HTypeConversion nodes.
- Add related tests to test/422-type-conversion.

Change-Id: If1829549708d9c3473efaa641f7f0bcfa6080ae9
diff --git a/compiler/optimizing/builder.cc b/compiler/optimizing/builder.cc
index b51b6e7..b5aff4b 100644
--- a/compiler/optimizing/builder.cc
+++ b/compiler/optimizing/builder.cc
@@ -1007,6 +1007,11 @@
       break;
     }
 
+    case Instruction::INT_TO_SHORT: {
+      Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimShort);
+      break;
+    }
+
     case Instruction::INT_TO_CHAR: {
       Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimChar);
       break;
diff --git a/compiler/optimizing/code_generator_arm.cc b/compiler/optimizing/code_generator_arm.cc
index 09e1b97..da51a37 100644
--- a/compiler/optimizing/code_generator_arm.cc
+++ b/compiler/optimizing/code_generator_arm.cc
@@ -1367,6 +1367,22 @@
       }
       break;
 
+    case Primitive::kPrimShort:
+      switch (input_type) {
+        case Primitive::kPrimByte:
+        case Primitive::kPrimInt:
+        case Primitive::kPrimChar:
+          // Processing a Dex `int-to-short' instruction.
+          locations->SetInAt(0, Location::RequiresRegister());
+          locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
+          break;
+
+        default:
+          LOG(FATAL) << "Unexpected type conversion from " << input_type
+                     << " to " << result_type;
+      }
+      break;
+
     case Primitive::kPrimInt:
       switch (input_type) {
         case Primitive::kPrimLong:
@@ -1461,6 +1477,21 @@
       }
       break;
 
+    case Primitive::kPrimShort:
+      switch (input_type) {
+        case Primitive::kPrimByte:
+        case Primitive::kPrimInt:
+        case Primitive::kPrimChar:
+          // Processing a Dex `int-to-short' instruction.
+          __ sbfx(out.As<Register>(), in.As<Register>(), 0, 16);
+          break;
+
+        default:
+          LOG(FATAL) << "Unexpected type conversion from " << input_type
+                     << " to " << result_type;
+      }
+      break;
+
     case Primitive::kPrimInt:
       switch (input_type) {
         case Primitive::kPrimLong:
diff --git a/compiler/optimizing/code_generator_x86.cc b/compiler/optimizing/code_generator_x86.cc
index 8a8fec2..b6549b2 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -1306,6 +1306,22 @@
       }
       break;
 
+    case Primitive::kPrimShort:
+      switch (input_type) {
+        case Primitive::kPrimByte:
+        case Primitive::kPrimInt:
+        case Primitive::kPrimChar:
+          // Processing a Dex `int-to-short' instruction.
+          locations->SetInAt(0, Location::Any());
+          locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
+          break;
+
+        default:
+          LOG(FATAL) << "Unexpected type conversion from " << input_type
+                     << " to " << result_type;
+      }
+      break;
+
     case Primitive::kPrimInt:
       switch (input_type) {
         case Primitive::kPrimLong:
@@ -1408,6 +1424,29 @@
       }
       break;
 
+    case Primitive::kPrimShort:
+      switch (input_type) {
+        case Primitive::kPrimByte:
+        case Primitive::kPrimInt:
+        case Primitive::kPrimChar:
+          // Processing a Dex `int-to-short' instruction.
+          if (in.IsRegister()) {
+            __ movsxw(out.As<Register>(), in.As<Register>());
+          } else if (in.IsStackSlot()) {
+            __ movsxw(out.As<Register>(), Address(ESP, in.GetStackIndex()));
+          } else {
+            DCHECK(in.GetConstant()->IsIntConstant());
+            int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
+            __ movl(out.As<Register>(), Immediate(static_cast<int16_t>(value)));
+          }
+          break;
+
+        default:
+          LOG(FATAL) << "Unexpected type conversion from " << input_type
+                     << " to " << result_type;
+      }
+      break;
+
     case Primitive::kPrimInt:
       switch (input_type) {
         case Primitive::kPrimLong:
diff --git a/compiler/optimizing/code_generator_x86_64.cc b/compiler/optimizing/code_generator_x86_64.cc
index 5aa1c4a..f7cdee9 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -1304,6 +1304,22 @@
       }
       break;
 
+    case Primitive::kPrimShort:
+      switch (input_type) {
+        case Primitive::kPrimByte:
+        case Primitive::kPrimInt:
+        case Primitive::kPrimChar:
+          // Processing a Dex `int-to-short' instruction.
+          locations->SetInAt(0, Location::Any());
+          locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
+          break;
+
+        default:
+          LOG(FATAL) << "Unexpected type conversion from " << input_type
+                     << " to " << result_type;
+      }
+      break;
+
     case Primitive::kPrimInt:
       switch (input_type) {
         case Primitive::kPrimLong:
@@ -1409,6 +1425,30 @@
       }
       break;
 
+    case Primitive::kPrimShort:
+      switch (input_type) {
+        case Primitive::kPrimByte:
+        case Primitive::kPrimInt:
+        case Primitive::kPrimChar:
+          // Processing a Dex `int-to-short' instruction.
+          if (in.IsRegister()) {
+            __ movsxw(out.As<CpuRegister>(), in.As<CpuRegister>());
+          } else if (in.IsStackSlot()) {
+            __ movsxw(out.As<CpuRegister>(),
+                      Address(CpuRegister(RSP), in.GetStackIndex()));
+          } else {
+            DCHECK(in.GetConstant()->IsIntConstant());
+            __ movl(out.As<CpuRegister>(),
+                    Immediate(static_cast<int16_t>(in.GetConstant()->AsIntConstant()->GetValue())));
+          }
+          break;
+
+        default:
+          LOG(FATAL) << "Unexpected type conversion from " << input_type
+                     << " to " << result_type;
+      }
+      break;
+
     case Primitive::kPrimInt:
       switch (input_type) {
         case Primitive::kPrimLong:
diff --git a/test/422-type-conversion/src/Main.java b/test/422-type-conversion/src/Main.java
index 88b4528..0bf9585 100644
--- a/test/422-type-conversion/src/Main.java
+++ b/test/422-type-conversion/src/Main.java
@@ -24,6 +24,12 @@
     }
   }
 
+  public static void assertShortEquals(short expected, short result) {
+    if (expected != result) {
+      throw new Error("Expected: " + expected + ", found: " + result);
+    }
+  }
+
   public static void assertIntEquals(int expected, int result) {
     if (expected != result) {
       throw new Error("Expected: " + expected + ", found: " + result);
@@ -39,23 +45,32 @@
   public static void assertCharEquals(char expected, char result) {
     if (expected != result) {
       // Values are cast to int to display numeric values instead of
-      // (Unicode) characters.
+      // (UTF-16 encoded) characters.
       throw new Error("Expected: " + (int)expected + ", found: " + (int)result);
     }
   }
 
   public static void main(String[] args) {
+    // Generate, compile and check int-to-long Dex instructions.
     byteToLong();
     shortToLong();
     intToLong();
     charToLong();
 
+    // Generate, compile and check long-to-int Dex instructions.
     longToInt();
 
+    // Generate, compile and check int-to-byte Dex instructions.
     shortToByte();
     intToByte();
     charToByte();
 
+    // Generate, compile and check int-to-short Dex instructions.
+    byteToShort();
+    intToShort();
+    charToShort();
+
+    // Generate, compile and check int-to-char Dex instructions.
     byteToChar();
     shortToChar();
     intToChar();
@@ -100,10 +115,6 @@
     assertLongEquals(51L, $opt$CharToLong((char)51));
     assertLongEquals(32767L, $opt$CharToLong((char)32767));  // 2^15 - 1
     assertLongEquals(65535L, $opt$CharToLong((char)65535));  // 2^16 - 1
-
-    assertLongEquals(0L, $opt$CharToLong('\u0000'));
-    assertLongEquals(65535L, $opt$CharToLong('\uFFFF'));  // 2^16 - 1
-
     assertLongEquals(65535L, $opt$CharToLong((char)-1));
     assertLongEquals(65485L, $opt$CharToLong((char)-51));
     assertLongEquals(32769L, $opt$CharToLong((char)-32767));  // -(2^15 - 1)
@@ -175,10 +186,6 @@
     assertByteEquals((byte)-128, $opt$CharToByte((char)128));  // 2^7
     assertByteEquals((byte)-1, $opt$CharToByte((char)32767));  // 2^15 - 1
     assertByteEquals((byte)-1, $opt$CharToByte((char)65535));  // 2^16 - 1
-
-    assertByteEquals((byte)0, $opt$CharToByte('\u0000'));
-    assertByteEquals((byte)-1, $opt$CharToByte('\uFFFF'));   // 2^16 - 1
-
     assertByteEquals((byte)-1, $opt$CharToByte((char)-1));
     assertByteEquals((byte)-51, $opt$CharToByte((char)-51));
     assertByteEquals((byte)-127, $opt$CharToByte((char)-127));  // -(2^7 - 1)
@@ -186,6 +193,47 @@
     assertByteEquals((byte)127, $opt$CharToByte((char)-129));  // -(2^7 + 1)
   }
 
+  private static void byteToShort() {
+    assertShortEquals((short)1, $opt$ByteToShort((byte)1));
+    assertShortEquals((short)0, $opt$ByteToShort((byte)0));
+    assertShortEquals((short)-1, $opt$ByteToShort((byte)-1));
+    assertShortEquals((short)51, $opt$ByteToShort((byte)51));
+    assertShortEquals((short)-51, $opt$ByteToShort((byte)-51));
+    assertShortEquals((short)127, $opt$ByteToShort((byte)127));  // 2^7 - 1
+    assertShortEquals((short)-127, $opt$ByteToShort((byte)-127));  // -(2^7 - 1)
+    assertShortEquals((short)-128, $opt$ByteToShort((byte)-128));  // -(2^7)
+  }
+
+  private static void intToShort() {
+    assertShortEquals((short)1, $opt$IntToShort(1));
+    assertShortEquals((short)0, $opt$IntToShort(0));
+    assertShortEquals((short)-1, $opt$IntToShort(-1));
+    assertShortEquals((short)51, $opt$IntToShort(51));
+    assertShortEquals((short)-51, $opt$IntToShort(-51));
+    assertShortEquals((short)32767, $opt$IntToShort(32767));  // 2^15 - 1
+    assertShortEquals((short)-32767, $opt$IntToShort(-32767));  // -(2^15 - 1)
+    assertShortEquals((short)-32768, $opt$IntToShort(-32768));  // -(2^15)
+    assertShortEquals((short)-32768, $opt$IntToShort(32768));  // 2^15
+    assertShortEquals((short)32767, $opt$IntToShort(-32769));  // -(2^15 + 1)
+    assertShortEquals((short)-1, $opt$IntToShort(2147483647));  // 2^31 - 1
+    assertShortEquals((short)0, $opt$IntToShort(-2147483648));  // -(2^31)
+  }
+
+  private static void charToShort() {
+    assertShortEquals((short)1, $opt$CharToShort((char)1));
+    assertShortEquals((short)0, $opt$CharToShort((char)0));
+    assertShortEquals((short)51, $opt$CharToShort((char)51));
+    assertShortEquals((short)32767, $opt$CharToShort((char)32767));  // 2^15 - 1
+    assertShortEquals((short)-32768, $opt$CharToShort((char)32768));  // 2^15
+    assertShortEquals((short)-32767, $opt$CharToShort((char)32769));  // 2^15
+    assertShortEquals((short)-1, $opt$CharToShort((char)65535));  // 2^16 - 1
+    assertShortEquals((short)-1, $opt$CharToShort((char)-1));
+    assertShortEquals((short)-51, $opt$CharToShort((char)-51));
+    assertShortEquals((short)-32767, $opt$CharToShort((char)-32767));  // -(2^15 - 1)
+    assertShortEquals((short)-32768, $opt$CharToShort((char)-32768));  // -(2^15)
+    assertShortEquals((short)32767, $opt$CharToShort((char)-32769));  // -(2^15 + 1)
+  }
+
   private static void byteToChar() {
     assertCharEquals((char)1, $opt$ByteToChar((byte)1));
     assertCharEquals((char)0, $opt$ByteToChar((byte)0));
@@ -242,6 +290,11 @@
   static byte $opt$IntToByte(int a){ return (byte)a; }
   static byte $opt$CharToByte(char a){ return (byte)a; }
 
+  // These methods produce int-to-short Dex instructions.
+  static short $opt$ByteToShort(byte a){ return (short)a; }
+  static short $opt$IntToShort(int a){ return (short)a; }
+  static short $opt$CharToShort(char a){ return (short)a; }
+
   // These methods produce int-to-char Dex instructions.
   static char $opt$ByteToChar(byte a){ return (char)a; }
   static char $opt$ShortToChar(short a){ return (char)a; }