I tried d8 with --allow-natives-syntax --turbofan --print-opt-code flags 
and following code but don't get any output.

function my_mod(n) {
  if (n  % 2 == 1)
    return true;
  return false;
}

my_mod(2);
my_mod(1);
my_mod(3);

%OptimizeFunctionOnNextCall(my_mod);

my_mod(100)

I'm not sure which flags to use here. 
On Tuesday, March 11, 2025 at 5:35:11 PM UTC+7 Jakob Kummerow wrote:

> Why don't you test it and find out yourself?
>
>
> On Tue, Mar 11, 2025 at 10:20 AM Sỹ Trần Dũng <[email protected]> wrote:
>
>> I have a question regarding V8's compiler optimization, specifically 
>> concerning the modulo 2 operation. In compilers like GCC and Clang, it's 
>> common to see the operation n % 2 optimized to a bitwise AND (n & 1) or a 
>> bit check instruction, as these are generally more efficient.
>>
>> I've been examining the bytecode generated by V8, and I've observed that 
>> a modulo instruction is used for n % 2. 
>>
>> [generated bytecode for function: my_mod (0x3de244c5b401 
>> <SharedFunctionInfo my_mod>)]
>> Bytecode length: 17
>> Parameter count 2
>> Register count 1
>> Frame size 8
>>    23 S> 0x32c69b60dc80 @    0 : 0b 03             Ldar a0
>>    29 E> 0x32c69b60dc82 @    2 : 4b 02 00          ModSmi [2], [0]
>>          0x32c69b60dc85 @    5 : c9                Star0
>>          0x32c69b60dc86 @    6 : 0d 01             LdaSmi [1]
>>    33 E> 0x32c69b60dc88 @    8 : 6f f9 01          TestEqual r0, [1]
>>          0x32c69b60dc8b @   11 : 9e 04             JumpIfFalse [4] 
>> (0x32c69b60dc8f @ 15)
>>    45 S> 0x32c69b60dc8d @   13 : 11                LdaTrue
>>    57 S> 0x32c69b60dc8e @   14 : ae                Return
>>    64 S> 0x32c69b60dc8f @   15 : 12                LdaFalse
>>    77 S> 0x32c69b60dc90 @   16 : ae                Return
>>
>> I'm curious if this behavior changes when the code is "heated" and 
>> optimized by Turbofan.
>>
>> Could someone please tell whether Turbofan performs this particular 
>> optimization?
>>
>> Thank you for your time and expertise.
>>
>> --
>>
>

-- 
-- 
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- 
You received this message because you are subscribed to the Google Groups 
"v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/v8-dev/31622d82-a07b-47fc-904c-d1be58eef089n%40googlegroups.com.

Reply via email to