Add constant folding for OPCODE_ROTATE_LEFT

This commit is contained in:
chss95cs@gmail.com 2019-08-03 13:26:54 -07:00 committed by illusion
parent b47948b7cf
commit 895a0e8703
3 changed files with 32 additions and 1 deletions

View File

@ -712,7 +712,14 @@ bool ConstantPropagationPass::Run(HIRBuilder* builder, bool& result) {
result = true; result = true;
} }
break; break;
// TODO(benvanik): ROTATE_LEFT case OPCODE_ROTATE_LEFT:
if (i->src1.value->IsConstant() && i->src2.value->IsConstant()) {
v->set_from(i->src1.value);
v->RotateLeft(i->src2.value);
i->Remove();
result = true;
}
break;
case OPCODE_BYTE_SWAP: case OPCODE_BYTE_SWAP:
if (i->src1.value->IsConstant()) { if (i->src1.value->IsConstant()) {
v->set_from(i->src1.value); v->set_from(i->src1.value);

View File

@ -843,6 +843,29 @@ void Value::Sha(Value* other) {
} }
} }
void Value::RotateLeft(Value* other) {
assert_true(other->type == INT8_TYPE);
auto rotation = other->constant.u8;
switch (type) {
case INT8_TYPE:
constant.u8 = rotate_left<uint8_t>(constant.u8, rotation);
break;
case INT16_TYPE:
constant.u16 = rotate_left<uint16_t>(constant.u16, rotation);
break;
case INT32_TYPE:
constant.u32 = rotate_left<uint32_t>(constant.u32, rotation);
break;
case INT64_TYPE:
constant.u64 = rotate_left<uint64_t>(constant.u64, rotation);
break;
default:
assert_unhandled_case(type);
break;
}
}
void Value::Extract(Value* vec, Value* index) { void Value::Extract(Value* vec, Value* index) {
assert_true(vec->type == VEC128_TYPE); assert_true(vec->type == VEC128_TYPE);
switch (type) { switch (type) {

View File

@ -519,6 +519,7 @@ class Value {
void Shl(Value* other); void Shl(Value* other);
void Shr(Value* other); void Shr(Value* other);
void Sha(Value* other); void Sha(Value* other);
void RotateLeft(Value* other);
void Extract(Value* vec, Value* index); void Extract(Value* vec, Value* index);
void Select(Value* other, Value* ctrl); void Select(Value* other, Value* ctrl);
void Splat(Value* other); void Splat(Value* other);