Skip to content

Instantly share code, notes, and snippets.

@aplund
Created June 4, 2014 01:12
Show Gist options
  • Save aplund/070a60f78ee503ef7ed2 to your computer and use it in GitHub Desktop.
Save aplund/070a60f78ee503ef7ed2 to your computer and use it in GitHub Desktop.
julia> f4(x::Int) = (x <= 1000 && x > 0) ? Base.checked_add(Base.checked_sub(x,1),1) : 0
f4 (generic function with 1 method)
julia> code_llvm(f4,(Int,))
define i64 @julia_f418717(i64) {
top:
%.off = add i64 %0, -1, !dbg !1590
%1 = icmp ugt i64 %.off, 999, !dbg !1590
br i1 %1, label %L5, label %if2, !dbg !1590
if2: ; preds = %top
%2 = call { i64, i1 } @llvm.ssub.with.overflow.i64(i64 %0, i64 1), !dbg !1590
%3 = extractvalue { i64, i1 } %2, 1, !dbg !1590
br i1 %3, label %fail, label %pass, !dbg !1590
fail: ; preds = %if2
%4 = load %jl_value_t** @jl_overflow_exception, align 8, !dbg !1590
call void @jl_throw_with_superfluous_argument(%jl_value_t* %4, i32 1), !dbg !1590
unreachable, !dbg !1590
pass: ; preds = %if2
%5 = extractvalue { i64, i1 } %2, 0, !dbg !1590
%6 = call { i64, i1 } @llvm.sadd.with.overflow.i64(i64 %5, i64 1), !dbg !1590
%7 = extractvalue { i64, i1 } %6, 1, !dbg !1590
br i1 %7, label %fail3, label %pass4, !dbg !1590
fail3: ; preds = %pass
%8 = load %jl_value_t** @jl_overflow_exception, align 8, !dbg !1590
call void @jl_throw_with_superfluous_argument(%jl_value_t* %8, i32 1), !dbg !1590
unreachable, !dbg !1590
pass4: ; preds = %pass
%9 = extractvalue { i64, i1 } %6, 0, !dbg !1590
ret i64 %9, !dbg !1590
L5: ; preds = %top
ret i64 0, !dbg !1590
}
julia> code_native(f4,(Int,))
.text
Filename: none
Source line: 1
push RBP
mov RBP, RSP
Source line: 1
lea RCX, QWORD PTR [RDI - 1]
cmp RCX, 999
jbe 4
xor EAX, EAX
pop RBP
ret
cmp RDI, 1
jo 16
lea RAX, QWORD PTR [RCX + 1]
add RCX, 1
jo 2
pop RBP
ret
movabs RAX, 139953980758368
mov RDI, QWORD PTR [RAX]
movabs RAX, 139953965933040
mov ESI, 1
call RAX
julia> f5(x::Int) = (x <= 1000 && x > 0) ? x : 0
f5 (generic function with 1 method)
julia> code_llvm(f5,(Int,))
define i64 @julia_f518718(i64) {
top:
%.off = add i64 %0, -1, !dbg !1593
%1 = icmp ugt i64 %.off, 999, !dbg !1593
%merge = select i1 %1, i64 0, i64 %0, !dbg !1593
ret i64 %merge, !dbg !1593
}
julia> code_native(f5,(Int,))
.text
Filename: none
Source line: 1
push RBP
mov RBP, RSP
Source line: 1
lea RCX, QWORD PTR [RDI - 1]
xor EAX, EAX
cmp RCX, 999
cmovbe RAX, RDI
pop RBP
ret
julia> versioninfo()
Julia Version 0.3.0-prerelease+3456
Commit dc53d93 (2014-06-03 22:37 UTC)
Platform Info:
System: Linux (x86_64-unknown-linux-gnu)
CPU: Intel(R) Core(TM) i7-3540M CPU @ 3.00GHz
WORD_SIZE: 64
BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY)
LAPACK: libopenblas
LIBM: libopenlibm
@aplund
Copy link
Author

aplund commented Jun 4, 2014

This is an example of the code that would be generated for a statement like length(1:n). If n is positive and less than the maximum value for integers, then this should just return n. However, the checks for overflow are not optimised away and remain as dead code paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment