From 60fe67bb4bb971af7b9814285b173dd641ec9e7c Mon Sep 17 00:00:00 2001 From: Ryan Macnak Date: Mon, 23 Feb 2026 13:01:19 -0800 Subject: [PATCH] [vm] Let the C++ compiler know it can assume the low bit of a Smi is clear. TEST=ci Change-Id: I89f60532a6fe7923a5d4d7bbcd624228ab899854 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/481300 Reviewed-by: Alexander Markov Commit-Queue: Ryan Macnak --- runtime/platform/globals.h | 11 +++++++++++ runtime/vm/tagged_pointer.h | 3 +++ 2 files changed, 14 insertions(+) diff --git a/runtime/platform/globals.h b/runtime/platform/globals.h index 9078987dba9..5de25a94722 100644 --- a/runtime/platform/globals.h +++ b/runtime/platform/globals.h @@ -250,6 +250,17 @@ struct simd128_value_t { #error Automatic compiler detection failed. #endif +#if defined(__clang__) +#define DART_ASSUME(expr) __builtin_assume(expr) +#elif defined(__GNUC__) +#define DART_ASSUME(expr) \ + if (!(expr)) __builtin_unreachable() +#elif defined(_MSC_VER) +#define DART_ASSUME(expr) __assume(expr) +#else +#error Automatic compiler detection failed. +#endif + #ifdef _MSC_VER #elif __GNUC__ #define DART_HAS_COMPUTED_GOTO 1 diff --git a/runtime/vm/tagged_pointer.h b/runtime/vm/tagged_pointer.h index 8712990f69b..47601b6037c 100644 --- a/runtime/vm/tagged_pointer.h +++ b/runtime/vm/tagged_pointer.h @@ -457,6 +457,9 @@ inline intptr_t RawSmiValue(const SmiPtr raw_value) { static_cast(static_cast(raw_value)))); #endif ASSERT((value & kSmiTagMask) == kSmiTag); + // Let the C++ compiler know that it is safe to combine this right shift with + // a following left shift, such as often occurs for index scaling. + DART_ASSUME((value & kSmiTagMask) == kSmiTag); return (value >> kSmiTagShift); }