Skip to content

Commit aeb42bd

Browse files
Lef IoannidisCopilot
andcommitted
Avoid redundant upcasts in UInt8/UInt16 comparisons
When both operands of a comparison are atomic (variables, field accesses, function results — not arithmetic expressions), strip the unnecessary Cast(_, UInt32) wrappers and compare at native width. Before: (uint32_t)a == (uint32_t)b (redundant casts) After: a == b (clean, no casts) Arithmetic comparisons are unaffected — they still widen and mask: ((uint32_t)a + (uint32_t)b & 0xFFU) == (uint32_t)c Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent aa23aa4 commit aeb42bd

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

lib/AstToCStar.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,11 @@ and mk_arith env e =
343343
CStar.Call (Op op, [ mask_if a1 w e1; mask_if a2 w e2 ])
344344
| BShiftR ->
345345
CStar.Call (Op op, [ mask_if a1 w e1; e2 ])
346+
| Eq | Neq | Lt | Lte | Gt | Gte when a1 && a2 ->
347+
(* Both operands are atomic (no arithmetic widening) — compare at
348+
native width without redundant upcasts. *)
349+
let strip e = match e with CStar.Cast (inner, _) -> inner | _ -> e in
350+
CStar.Call (Op op, [ strip e1; strip e2 ])
346351
| Eq | Neq | Lt | Lte | Gt | Gte ->
347352
CStar.Call (Op op, [ mask_if a1 w e1; mask_if a2 w e2 ])
348353
| _ ->

test/pulse/Break.expected.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ void Break_break_continue_and_return(uint8_t which)
4242
while (cond)
4343
{
4444
bool _continue = false;
45-
if ((uint32_t)which == 0U)
45+
if (which == 0U)
4646
_break = true;
4747
bool _break1 = _break;
4848
if (!_break1)
4949
{
50-
if ((uint32_t)which == 1U)
50+
if (which == 1U)
5151
_continue = true;
5252
bool _continue1 = _continue;
5353
if (!_continue1)
5454
{
55-
if ((uint32_t)which == 2U)
55+
if (which == 2U)
5656
_return = true;
5757
bool _return1 = _return;
5858
if (!_return1)

0 commit comments

Comments
 (0)