Skip to content

Commit bd3813f

Browse files
committed
CombineOp: optimize (x ^ n) != 0 into x != n
Likewise for `(x ^ n) == 0` that becomes `x == n`. Do this only when we're sure it results in smaller code: - On RISC-V, the optimization is useless, as there is no conditional branch instruction for != n and == n conditions, just for != 0 and == 0. - On ARM, AArch64, PowerPC: we limit the optimization to values of n that are small enough to not need extra instructions to load the immediate n in a register.
1 parent 7c87292 commit bd3813f

8 files changed

+38
-0
lines changed

aarch64/CombineOp.v

+4
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ Variable get: valnum -> option rhs.
2525
Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
2626
match get x with
2727
| Some(Op (Ocmp c) ys) => Some (c, ys)
28+
| Some(Op (Oxorimm n) ys) =>
29+
if Int.eq n (Int.zero_ext 12 n) then Some (Ccompimm Cne n, ys) else None
2830
| _ => None
2931
end.
3032

3133
Function combine_compimm_eq_0 (x: valnum) : option(condition * list valnum) :=
3234
match get x with
3335
| Some(Op (Ocmp c) ys) => Some (negate_condition c, ys)
36+
| Some(Op (Oxorimm n) ys) =>
37+
if Int.eq n (Int.zero_ext 12 n) then Some (Ccompimm Ceq n, ys) else None
3438
| _ => None
3539
end.
3640

aarch64/CombineOpproof.v

+6
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ Proof.
4848
(* of cmp *)
4949
UseGetSound. rewrite <- H.
5050
destruct (eval_condition cond (map valu args) m); simpl; auto. destruct b; auto.
51+
(* of xorimm *)
52+
UseGetSound. rewrite <- H.
53+
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
5154
Qed.
5255

5356
Lemma combine_compimm_eq_0_sound:
@@ -61,6 +64,9 @@ Proof.
6164
UseGetSound. rewrite <- H.
6265
rewrite eval_negate_condition.
6366
destruct (eval_condition c (map valu args) m); simpl; auto. destruct b; auto.
67+
(* of xorimm *)
68+
UseGetSound. rewrite <- H.
69+
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
6470
Qed.
6571

6672
Lemma combine_compimm_eq_1_sound:

arm/CombineOp.v

+4
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ Variable get: valnum -> option rhs.
2626
Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
2727
match get x with
2828
| Some(Op (Ocmp c) ys) => Some (c, ys)
29+
| Some(Op (Oxorimm n) ys) =>
30+
if Int.eq n (Int.zero_ext 8 n) then Some (Ccompimm Cne n, ys) else None
2931
| _ => None
3032
end.
3133

3234
Function combine_compimm_eq_0 (x: valnum) : option(condition * list valnum) :=
3335
match get x with
3436
| Some(Op (Ocmp c) ys) => Some (negate_condition c, ys)
37+
| Some(Op (Oxorimm n) ys) =>
38+
if Int.eq n (Int.zero_ext 8 n) then Some (Ccompimm Ceq n, ys) else None
3539
| _ => None
3640
end.
3741

arm/CombineOpproof.v

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ Proof.
5656
(* of cmp *)
5757
UseGetSound. rewrite <- H.
5858
destruct (eval_condition cond (map valu args) m); simpl; auto. destruct b; auto.
59+
(* of xorimm *)
60+
UseGetSound. rewrite <- H.
61+
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
5962
Qed.
6063

6164
Lemma combine_compimm_eq_0_sound:
@@ -69,6 +72,9 @@ Proof.
6972
UseGetSound. rewrite <- H.
7073
rewrite eval_negate_condition.
7174
destruct (eval_condition c (map valu args) m); simpl; auto. destruct b; auto.
75+
(* of xorimm *)
76+
UseGetSound. rewrite <- H.
77+
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
7278
Qed.
7379

7480
Lemma combine_compimm_eq_1_sound:

powerpc/CombineOp.v

+4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
2727
match get x with
2828
| Some(Op (Ocmp c) ys) => Some (c, ys)
2929
| Some(Op (Oandimm n) ys) => Some (Cmasknotzero n, ys)
30+
| Some(Op (Oxorimm n) ys) =>
31+
if Int.eq n (Int.sign_ext 16 n) then Some (Ccompimm Cne n, ys) else None
3032
| _ => None
3133
end.
3234

3335
Function combine_compimm_eq_0 (x: valnum) : option(condition * list valnum) :=
3436
match get x with
3537
| Some(Op (Ocmp c) ys) => Some (negate_condition c, ys)
3638
| Some(Op (Oandimm n) ys) => Some (Cmaskzero n, ys)
39+
| Some(Op (Oxorimm n) ys) =>
40+
if Int.eq n (Int.sign_ext 16 n) then Some (Ccompimm Ceq n, ys) else None
3741
| _ => None
3842
end.
3943

powerpc/CombineOpproof.v

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ Proof.
5959
(* of and *)
6060
UseGetSound. rewrite <- H.
6161
destruct v; simpl; auto.
62+
(* of xorimm *)
63+
UseGetSound. rewrite <- H.
64+
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
6265
Qed.
6366

6467
Lemma combine_compimm_eq_0_sound:
@@ -74,6 +77,9 @@ Proof.
7477
destruct (eval_condition c (map valu args) m); simpl; auto. destruct b; auto.
7578
(* of and *)
7679
UseGetSound. rewrite <- H. destruct v; auto.
80+
(* of xorimm *)
81+
UseGetSound. rewrite <- H.
82+
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
7783
Qed.
7884

7985
Lemma combine_compimm_eq_1_sound:

x86/CombineOp.v

+2
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
2727
match get x with
2828
| Some(Op (Ocmp c) ys) => Some (c, ys)
2929
| Some(Op (Oandimm n) ys) => Some (Cmasknotzero n, ys)
30+
| Some(Op (Oxorimm n) ys) => Some (Ccompimm Cne n, ys)
3031
| _ => None
3132
end.
3233

3334
Function combine_compimm_eq_0 (x: valnum) : option(condition * list valnum) :=
3435
match get x with
3536
| Some(Op (Ocmp c) ys) => Some (negate_condition c, ys)
3637
| Some(Op (Oandimm n) ys) => Some (Cmaskzero n, ys)
38+
| Some(Op (Oxorimm n) ys) => Some (Ccompimm Ceq n, ys)
3739
| _ => None
3840
end.
3941

x86/CombineOpproof.v

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ Proof.
5353
(* of and *)
5454
UseGetSound. rewrite <- H.
5555
destruct v; simpl; auto.
56+
(* of xorimm *)
57+
UseGetSound. rewrite <- H.
58+
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
5659
Qed.
5760

5861
Lemma combine_compimm_eq_0_sound:
@@ -68,6 +71,9 @@ Proof.
6871
destruct (eval_condition c (map valu args) m); simpl; auto. destruct b; auto.
6972
(* of and *)
7073
UseGetSound. rewrite <- H. destruct v; auto.
74+
(* of xorimm *)
75+
UseGetSound. rewrite <- H.
76+
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
7177
Qed.
7278

7379
Lemma combine_compimm_eq_1_sound:

0 commit comments

Comments
 (0)