Skip to content

Commit

Permalink
CombineOp: optimize (x ^ n) != 0 into x != n
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xavierleroy committed Aug 26, 2024
1 parent 7c87292 commit bd3813f
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions aarch64/CombineOp.v
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ Variable get: valnum -> option rhs.
Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
match get x with
| Some(Op (Ocmp c) ys) => Some (c, ys)
| Some(Op (Oxorimm n) ys) =>
if Int.eq n (Int.zero_ext 12 n) then Some (Ccompimm Cne n, ys) else None
| _ => None
end.

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

Expand Down
6 changes: 6 additions & 0 deletions aarch64/CombineOpproof.v
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ Proof.
(* of cmp *)
UseGetSound. rewrite <- H.
destruct (eval_condition cond (map valu args) m); simpl; auto. destruct b; auto.
(* of xorimm *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
Qed.

Lemma combine_compimm_eq_0_sound:
Expand All @@ -61,6 +64,9 @@ Proof.
UseGetSound. rewrite <- H.
rewrite eval_negate_condition.
destruct (eval_condition c (map valu args) m); simpl; auto. destruct b; auto.
(* of xorimm *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
Qed.

Lemma combine_compimm_eq_1_sound:
Expand Down
4 changes: 4 additions & 0 deletions arm/CombineOp.v
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ Variable get: valnum -> option rhs.
Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
match get x with
| Some(Op (Ocmp c) ys) => Some (c, ys)
| Some(Op (Oxorimm n) ys) =>
if Int.eq n (Int.zero_ext 8 n) then Some (Ccompimm Cne n, ys) else None
| _ => None
end.

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

Expand Down
6 changes: 6 additions & 0 deletions arm/CombineOpproof.v
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Proof.
(* of cmp *)
UseGetSound. rewrite <- H.
destruct (eval_condition cond (map valu args) m); simpl; auto. destruct b; auto.
(* of xorimm *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
Qed.

Lemma combine_compimm_eq_0_sound:
Expand All @@ -69,6 +72,9 @@ Proof.
UseGetSound. rewrite <- H.
rewrite eval_negate_condition.
destruct (eval_condition c (map valu args) m); simpl; auto. destruct b; auto.
(* of xorimm *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
Qed.

Lemma combine_compimm_eq_1_sound:
Expand Down
4 changes: 4 additions & 0 deletions powerpc/CombineOp.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,17 @@ Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
match get x with
| Some(Op (Ocmp c) ys) => Some (c, ys)
| Some(Op (Oandimm n) ys) => Some (Cmasknotzero n, ys)
| Some(Op (Oxorimm n) ys) =>
if Int.eq n (Int.sign_ext 16 n) then Some (Ccompimm Cne n, ys) else None
| _ => None
end.

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

Expand Down
6 changes: 6 additions & 0 deletions powerpc/CombineOpproof.v
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ Proof.
(* of and *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto.
(* of xorimm *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
Qed.

Lemma combine_compimm_eq_0_sound:
Expand All @@ -74,6 +77,9 @@ Proof.
destruct (eval_condition c (map valu args) m); simpl; auto. destruct b; auto.
(* of and *)
UseGetSound. rewrite <- H. destruct v; auto.
(* of xorimm *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
Qed.

Lemma combine_compimm_eq_1_sound:
Expand Down
2 changes: 2 additions & 0 deletions x86/CombineOp.v
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ Function combine_compimm_ne_0 (x: valnum) : option(condition * list valnum) :=
match get x with
| Some(Op (Ocmp c) ys) => Some (c, ys)
| Some(Op (Oandimm n) ys) => Some (Cmasknotzero n, ys)
| Some(Op (Oxorimm n) ys) => Some (Ccompimm Cne n, ys)
| _ => None
end.

Function combine_compimm_eq_0 (x: valnum) : option(condition * list valnum) :=
match get x with
| Some(Op (Ocmp c) ys) => Some (negate_condition c, ys)
| Some(Op (Oandimm n) ys) => Some (Cmaskzero n, ys)
| Some(Op (Oxorimm n) ys) => Some (Ccompimm Ceq n, ys)
| _ => None
end.

Expand Down
6 changes: 6 additions & 0 deletions x86/CombineOpproof.v
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Proof.
(* of and *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto.
(* of xorimm *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
Qed.

Lemma combine_compimm_eq_0_sound:
Expand All @@ -68,6 +71,9 @@ Proof.
destruct (eval_condition c (map valu args) m); simpl; auto. destruct b; auto.
(* of and *)
UseGetSound. rewrite <- H. destruct v; auto.
(* of xorimm *)
UseGetSound. rewrite <- H.
destruct v; simpl; auto. rewrite Int.xor_is_zero; auto.
Qed.

Lemma combine_compimm_eq_1_sound:
Expand Down

0 comments on commit bd3813f

Please sign in to comment.