Implemented the approach you proposed on IRC, and it seems to work.
Testcase:
int myffs(int x)
{
return ffs(x) + 1;
}
Before the patch:
0000000000000000 <myffs>:
0: 0f bc c7 bsf %edi,%eax 3: b9 20 00 00 00 mov $0x20,%ecx 8: 0f 45 c8 cmovne %eax,%ecx b: 83 c1 02 add $0x2,%ecx e: b8 01 00 00 00 mov $0x1,%eax 13: 85 ff test %edi,%edi 15: 0f 45 c1 cmovne %ecx,%eax 18: c3 retq
After the patch:
0000000000000000 <myffs>:
0: 0f bc cf bsf %edi,%ecx 3: 83 c1 02 add $0x2,%ecx 6: 85 ff test %edi,%edi 8: b8 01 00 00 00 mov $0x1,%eax d: 0f 45 c1 cmovne %ecx,%eax 10: c3 retq
We can still probably use CMOVE and save another test instruction, but I feel like that can be implemented later.