diff --git a/pkgs/racket-test-core/tests/racket/optimize.rktl b/pkgs/racket-test-core/tests/racket/optimize.rktl index 22169fe51a..a420f46f1e 100644 --- a/pkgs/racket-test-core/tests/racket/optimize.rktl +++ b/pkgs/racket-test-core/tests/racket/optimize.rktl @@ -3387,6 +3387,20 @@ #t)) #f) +;; Make sure that `bitwise-and` is known to return a fixnum for non-negative +;; fixnum arguments but not for a negative one + +(test-comp '(lambda (x) + (bitwise-ior (bitwise-and x 7) 1)) + '(lambda (x) + (unsafe-fxior (bitwise-and x 7) 1))) +(test-comp '(lambda (x) + (bitwise-ior (bitwise-and x -7) 1)) + '(lambda (x) + (unsafe-fxior (bitwise-and x -7) 1)) + #f) + + (test-comp `(lambda (x) (thread (lambda () (set! x 5))) (if (pair? x) @@ -6260,7 +6274,6 @@ (set! f f) ((car f)))) - ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (report-errs) diff --git a/racket/src/racket/src/optimize.c b/racket/src/racket/src/optimize.c index aed58db676..7dc9bbcac7 100644 --- a/racket/src/racket/src/optimize.c +++ b/racket/src/racket/src/optimize.c @@ -3029,12 +3029,16 @@ static Scheme_Object *do_expr_implies_predicate(Scheme_Object *expr, Optimize_In && IS_NAMED_PRIM(app->rator, "bitwise-and")) { /* Assume that a fixnum argument to bitwise-and will never get lost, and so the validator will be able to confirm that a `bitwise-and` - combination produces a fixnum. */ + combination produces a fixnum if either argument is a literal, + nonnegative fixnum. */ if ((SCHEME_INTP(app->rand1) + && (SCHEME_INT_VAL(app->rand1) >= 0) && IN_FIXNUM_RANGE_ON_ALL_PLATFORMS(SCHEME_INT_VAL(app->rand1))) || (SCHEME_INTP(app->rand2) - && IN_FIXNUM_RANGE_ON_ALL_PLATFORMS(SCHEME_INT_VAL(app->rand2)))) + && (SCHEME_INT_VAL(app->rand2) >= 0) + && IN_FIXNUM_RANGE_ON_ALL_PLATFORMS(SCHEME_INT_VAL(app->rand2)))) { return scheme_fixnum_p_proc; + } } if (SCHEME_PRIMP(app->rator)