summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoland Levillain <rpl@google.com>2014-11-13 14:11:42 +0000
committerRoland Levillain <rpl@google.com>2014-11-13 17:30:06 +0000
commit51d3fc40637fc73d4156ad617cd451b844cbb75e (patch)
tree21669a66124a23dfc78a8c3f1d8b89415bfb0271
parentd94a0a1d2868baaab49f4d2835bca086d98cf763 (diff)
downloadart-51d3fc40637fc73d4156ad617cd451b844cbb75e.zip
art-51d3fc40637fc73d4156ad617cd451b844cbb75e.tar.gz
art-51d3fc40637fc73d4156ad617cd451b844cbb75e.tar.bz2
Add support for int-to-byte in the optimizing compiler.
- Add support for the int-to-byte Dex instruction in the optimizing compiler. - Implement the ARM and Thumb-2 SBFX instructions. - Generate x86, x86-64 and ARM (but not ARM64) code for char to byte, short to byte and int to byte HTypeConversion nodes. - Add related tests to test/422-type-conversion. Change-Id: Ic8b8911b90d4b5281fad15bcee96bc3ee85dc577
-rw-r--r--compiler/optimizing/builder.cc5
-rw-r--r--compiler/optimizing/code_generator_arm.cc31
-rw-r--r--compiler/optimizing/code_generator_x86.cc39
-rw-r--r--compiler/optimizing/code_generator_x86_64.cc40
-rw-r--r--compiler/utils/arm/assembler_arm.h3
-rw-r--r--compiler/utils/arm/assembler_arm32.cc19
-rw-r--r--compiler/utils/arm/assembler_arm32.h2
-rw-r--r--compiler/utils/arm/assembler_thumb2.cc21
-rw-r--r--compiler/utils/arm/assembler_thumb2.h2
-rw-r--r--test/422-type-conversion/src/Main.java65
10 files changed, 227 insertions, 0 deletions
diff --git a/compiler/optimizing/builder.cc b/compiler/optimizing/builder.cc
index 4e2f1cd..c29e283 100644
--- a/compiler/optimizing/builder.cc
+++ b/compiler/optimizing/builder.cc
@@ -1002,6 +1002,11 @@ bool HGraphBuilder::AnalyzeDexInstruction(const Instruction& instruction, uint32
break;
}
+ case Instruction::INT_TO_BYTE: {
+ Conversion_12x(instruction, Primitive::kPrimInt, Primitive::kPrimByte);
+ break;
+ }
+
case Instruction::ADD_INT: {
Binop_23x<HAdd>(instruction, Primitive::kPrimInt);
break;
diff --git a/compiler/optimizing/code_generator_arm.cc b/compiler/optimizing/code_generator_arm.cc
index be326dc..0403c5e 100644
--- a/compiler/optimizing/code_generator_arm.cc
+++ b/compiler/optimizing/code_generator_arm.cc
@@ -1348,6 +1348,22 @@ void LocationsBuilderARM::VisitTypeConversion(HTypeConversion* conversion) {
Primitive::Type result_type = conversion->GetResultType();
Primitive::Type input_type = conversion->GetInputType();
switch (result_type) {
+ case Primitive::kPrimByte:
+ switch (input_type) {
+ case Primitive::kPrimShort:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // int-to-byte conversion.
+ 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:
@@ -1410,6 +1426,21 @@ void InstructionCodeGeneratorARM::VisitTypeConversion(HTypeConversion* conversio
Primitive::Type result_type = conversion->GetResultType();
Primitive::Type input_type = conversion->GetInputType();
switch (result_type) {
+ case Primitive::kPrimByte:
+ switch (input_type) {
+ case Primitive::kPrimShort:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // int-to-byte conversion.
+ __ sbfx(out.As<Register>(), in.As<Register>(), 0, 8);
+ 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 904e627..0944f4c 100644
--- a/compiler/optimizing/code_generator_x86.cc
+++ b/compiler/optimizing/code_generator_x86.cc
@@ -1282,6 +1282,22 @@ void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Primitive::Type result_type = conversion->GetResultType();
Primitive::Type input_type = conversion->GetInputType();
switch (result_type) {
+ case Primitive::kPrimByte:
+ switch (input_type) {
+ case Primitive::kPrimShort:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // int-to-byte conversion.
+ 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:
@@ -1344,6 +1360,29 @@ void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversio
Primitive::Type result_type = conversion->GetResultType();
Primitive::Type input_type = conversion->GetInputType();
switch (result_type) {
+ case Primitive::kPrimByte:
+ switch (input_type) {
+ case Primitive::kPrimShort:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // int-to-byte conversion.
+ if (in.IsRegister()) {
+ __ movsxb(out.As<Register>(), in.As<ByteRegister>());
+ } else if (in.IsStackSlot()) {
+ __ movsxb(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<int8_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 f94c28f..a827f99 100644
--- a/compiler/optimizing/code_generator_x86_64.cc
+++ b/compiler/optimizing/code_generator_x86_64.cc
@@ -1280,6 +1280,22 @@ void LocationsBuilderX86_64::VisitTypeConversion(HTypeConversion* conversion) {
Primitive::Type result_type = conversion->GetResultType();
Primitive::Type input_type = conversion->GetInputType();
switch (result_type) {
+ case Primitive::kPrimByte:
+ switch (input_type) {
+ case Primitive::kPrimShort:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // int-to-byte conversion.
+ 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:
@@ -1344,6 +1360,30 @@ void InstructionCodeGeneratorX86_64::VisitTypeConversion(HTypeConversion* conver
Primitive::Type result_type = conversion->GetResultType();
Primitive::Type input_type = conversion->GetInputType();
switch (result_type) {
+ case Primitive::kPrimByte:
+ switch (input_type) {
+ case Primitive::kPrimShort:
+ case Primitive::kPrimInt:
+ case Primitive::kPrimChar:
+ // int-to-byte conversion.
+ if (in.IsRegister()) {
+ __ movsxb(out.As<CpuRegister>(), in.As<CpuRegister>());
+ } else if (in.IsStackSlot()) {
+ __ movsxb(out.As<CpuRegister>(),
+ Address(CpuRegister(RSP), in.GetStackIndex()));
+ } else {
+ DCHECK(in.GetConstant()->IsIntConstant());
+ __ movl(out.As<CpuRegister>(),
+ Immediate(static_cast<int8_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/compiler/utils/arm/assembler_arm.h b/compiler/utils/arm/assembler_arm.h
index dca2ab7..911000a 100644
--- a/compiler/utils/arm/assembler_arm.h
+++ b/compiler/utils/arm/assembler_arm.h
@@ -421,6 +421,9 @@ class ArmAssembler : public Assembler {
virtual void sdiv(Register rd, Register rn, Register rm, Condition cond = AL) = 0;
virtual void udiv(Register rd, Register rn, Register rm, Condition cond = AL) = 0;
+ virtual void sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width,
+ Condition cond = AL) = 0;
+
// Load/store instructions.
virtual void ldr(Register rd, const Address& ad, Condition cond = AL) = 0;
virtual void str(Register rd, const Address& ad, Condition cond = AL) = 0;
diff --git a/compiler/utils/arm/assembler_arm32.cc b/compiler/utils/arm/assembler_arm32.cc
index c8a57b1..29cbf58 100644
--- a/compiler/utils/arm/assembler_arm32.cc
+++ b/compiler/utils/arm/assembler_arm32.cc
@@ -208,6 +208,25 @@ void Arm32Assembler::udiv(Register rd, Register rn, Register rm, Condition cond)
}
+void Arm32Assembler::sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond) {
+ CHECK_NE(rd, kNoRegister);
+ CHECK_NE(rn, kNoRegister);
+ CHECK_NE(cond, kNoCondition);
+ CHECK_LE(lsb, 31U);
+ CHECK(1U <= width && width <= 32U) << width;
+ uint32_t widthminus1 = width - 1;
+
+ int32_t encoding = (static_cast<int32_t>(cond) << kConditionShift) |
+ B26 | B25 | B24 | B23 | B21 |
+ (widthminus1 << 16) |
+ (static_cast<uint32_t>(rd) << 12) |
+ (lsb << 7) |
+ B6 | B4 |
+ static_cast<uint32_t>(rn);
+ Emit(encoding);
+}
+
+
void Arm32Assembler::ldr(Register rd, const Address& ad, Condition cond) {
EmitMemOp(cond, true, false, rd, ad);
}
diff --git a/compiler/utils/arm/assembler_arm32.h b/compiler/utils/arm/assembler_arm32.h
index dbabb99..b582e9e 100644
--- a/compiler/utils/arm/assembler_arm32.h
+++ b/compiler/utils/arm/assembler_arm32.h
@@ -96,6 +96,8 @@ class Arm32Assembler FINAL : public ArmAssembler {
void sdiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
void udiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
+ void sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE;
+
// Load/store instructions.
void ldr(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
void str(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
diff --git a/compiler/utils/arm/assembler_thumb2.cc b/compiler/utils/arm/assembler_thumb2.cc
index 053e843..a309e18 100644
--- a/compiler/utils/arm/assembler_thumb2.cc
+++ b/compiler/utils/arm/assembler_thumb2.cc
@@ -264,6 +264,27 @@ void Thumb2Assembler::udiv(Register rd, Register rn, Register rm, Condition cond
}
+void Thumb2Assembler::sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond) {
+ CheckCondition(cond);
+ CHECK_LE(lsb, 31U);
+ CHECK(1U <= width && width <= 32U) << width;
+ uint32_t widthminus1 = width - 1;
+ uint32_t imm2 = lsb & (B1 | B0); // Bits 0-1 of `lsb`.
+ uint32_t imm3 = (lsb & (B4 | B3 | B2)) >> 2; // Bits 2-4 of `lsb`.
+
+ uint32_t op = 20U /* 0b10100 */;
+ int32_t encoding = B31 | B30 | B29 | B28 | B25 |
+ op << 20 |
+ static_cast<uint32_t>(rn) << 16 |
+ imm3 << 12 |
+ static_cast<uint32_t>(rd) << 8 |
+ imm2 << 6 |
+ widthminus1;
+
+ Emit32(encoding);
+}
+
+
void Thumb2Assembler::ldr(Register rd, const Address& ad, Condition cond) {
EmitLoadStore(cond, true, false, false, false, rd, ad);
}
diff --git a/compiler/utils/arm/assembler_thumb2.h b/compiler/utils/arm/assembler_thumb2.h
index 9ccdef7..1fc842c 100644
--- a/compiler/utils/arm/assembler_thumb2.h
+++ b/compiler/utils/arm/assembler_thumb2.h
@@ -118,6 +118,8 @@ class Thumb2Assembler FINAL : public ArmAssembler {
void sdiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
void udiv(Register rd, Register rn, Register rm, Condition cond = AL) OVERRIDE;
+ void sbfx(Register rd, Register rn, uint32_t lsb, uint32_t width, Condition cond = AL) OVERRIDE;
+
// Load/store instructions.
void ldr(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
void str(Register rd, const Address& ad, Condition cond = AL) OVERRIDE;
diff --git a/test/422-type-conversion/src/Main.java b/test/422-type-conversion/src/Main.java
index a4232ed..7c5ddba 100644
--- a/test/422-type-conversion/src/Main.java
+++ b/test/422-type-conversion/src/Main.java
@@ -18,6 +18,12 @@
// it does compile the method.
public class Main {
+ public static void assertByteEquals(byte expected, byte 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);
@@ -37,6 +43,10 @@ public class Main {
charToLong();
longToInt();
+
+ shortToByte();
+ intToByte();
+ charToByte();
}
private static void byteToLong() {
@@ -115,6 +125,56 @@ public class Main {
assertLongEquals(-1, $opt$IntToLong($opt$LongToInt(-4294967297L))); // -(2^32 + 1)
}
+ private static void shortToByte() {
+ assertByteEquals((byte)1, $opt$ShortToByte((short)1));
+ assertByteEquals((byte)0, $opt$ShortToByte((short)0));
+ assertByteEquals((byte)-1, $opt$ShortToByte((short)-1));
+ assertByteEquals((byte)51, $opt$ShortToByte((short)51));
+ assertByteEquals((byte)-51, $opt$ShortToByte((short)-51));
+ assertByteEquals((byte)127, $opt$ShortToByte((short)127)); // 2^7 - 1
+ assertByteEquals((byte)-127, $opt$ShortToByte((short)-127)); // -(2^7 - 1)
+ assertByteEquals((byte)-128, $opt$ShortToByte((short)-128)); // -(2^7)
+ assertByteEquals((byte)-128, $opt$ShortToByte((short)128)); // 2^7
+ assertByteEquals((byte)127, $opt$ShortToByte((short)-129)); // -(2^7 + 1)
+ assertByteEquals((byte)-1, $opt$ShortToByte((short)32767)); // 2^15 - 1
+ assertByteEquals((byte)0, $opt$ShortToByte((short)-32768)); // -(2^15)
+ }
+
+ private static void intToByte() {
+ assertByteEquals((byte)1, $opt$IntToByte(1));
+ assertByteEquals((byte)0, $opt$IntToByte(0));
+ assertByteEquals((byte)-1, $opt$IntToByte(-1));
+ assertByteEquals((byte)51, $opt$IntToByte(51));
+ assertByteEquals((byte)-51, $opt$IntToByte(-51));
+ assertByteEquals((byte)127, $opt$IntToByte(127)); // 2^7 - 1
+ assertByteEquals((byte)-127, $opt$IntToByte(-127)); // -(2^7 - 1)
+ assertByteEquals((byte)-128, $opt$IntToByte(-128)); // -(2^7)
+ assertByteEquals((byte)-128, $opt$IntToByte(128)); // 2^7
+ assertByteEquals((byte)127, $opt$IntToByte(-129)); // -(2^7 + 1)
+ assertByteEquals((byte)-1, $opt$IntToByte(2147483647)); // 2^31 - 1
+ assertByteEquals((byte)0, $opt$IntToByte(-2147483648)); // -(2^31)
+ }
+
+ private static void charToByte() {
+ assertByteEquals((byte)1, $opt$CharToByte((char)1));
+ assertByteEquals((byte)0, $opt$CharToByte((char)0));
+ assertByteEquals((byte)51, $opt$CharToByte((char)51));
+ assertByteEquals((byte)127, $opt$CharToByte((char)127)); // 2^7 - 1
+ 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)
+ assertByteEquals((byte)-128, $opt$CharToByte((char)-128)); // -(2^7)
+ assertByteEquals((byte)127, $opt$CharToByte((char)-129)); // -(2^7 + 1)
+ }
+
+
// These methods produce int-to-long Dex instructions.
static long $opt$ByteToLong(byte a) { return a; }
static long $opt$ShortToLong(short a) { return a; }
@@ -124,4 +184,9 @@ public class Main {
// These methods produce long-to-int Dex instructions.
static int $opt$LongToInt(long a){ return (int)a; }
static int $opt$LongLiteralToInt(){ return (int)42L; }
+
+ // These methods produce int-to-byte Dex instructions.
+ static byte $opt$ShortToByte(short a){ return (byte)a; }
+ static byte $opt$IntToByte(int a){ return (byte)a; }
+ static byte $opt$CharToByte(char a){ return (byte)a; }
}