Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Last active August 16, 2017 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewrk/7697fef9b5a60bc7467f41197c63bae4 to your computer and use it in GitHub Desktop.
Save andrewrk/7697fef9b5a60bc7467f41197c63bae4 to your computer and use it in GitHub Desktop.
can LLVM optimize out checking if op2 of a right shift is greater than bit count of op1?
define i32 @main(i32, i32) {
%ok = icmp ult i32 %1, 32
br i1 %ok, label %ok_label, label %bad_label
ok_label:
ret i32 0
bad_label:
%a = lshr i32 %0, %1
ret i32 %a
}
main: # @main
# BB#0:
cmpl $31, %esi
ja .LBB0_2
# BB#1: # %ok_label
xorl %eax, %eax
retq
.LBB0_2: # %bad_label
movl %esi, %ecx
shrl %cl, %edi
movl %edi, %eax
retq
define i32 @main(i32, i32) {
%ok = icmp ult i32 %1, 32
%a = lshr i32 %0, %1
%result = select i1 %ok, i32 %a, i32 0
ret i32 %result
}
main: # @main
# BB#0:
movl %esi, %ecx
shrl %cl, %edi
xorl %eax, %eax
cmpl $32, %esi
cmovbl %edi, %eax
retq
define i32 @main(i32, i32) {
%a = lshr i32 %0, %1
ret i32 %a
}
main: # @main
# BB#0:
movl %esi, %ecx
shrl %cl, %edi
movl %edi, %eax
retq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment