Skip to content

Instantly share code, notes, and snippets.

@nddrylliog
Created December 11, 2012 17:13
Show Gist options
  • Save nddrylliog/4260376 to your computer and use it in GitHub Desktop.
Save nddrylliog/4260376 to your computer and use it in GitHub Desktop.
Scissors conditionals with variables, optimized by LLVM
===========================
Dice roll... 4!
===========================
---------------------------
old body = {4 }
new body = {a : <unknown type> = 1, b : <unknown type> = 2, c : <unknown type> = 3, b > a ? (c > b ? c : b) : a }
---------------------------
[scissors] Done tinkering
[scissors] resolved body = {a : SSizeT = 1, b : SSizeT = 2, c : SSizeT = 3, return b > a ? (c > b ? c : b) : a as Int }
; ModuleID = 'scissors'
define i32 @scissors() {
entry:
%a = alloca i64
store i64 1, i64* %a
%b = alloca i64
store i64 2, i64* %b
%c = alloca i64
store i64 3, i64* %c
%bLoad = load i64* %b
%aLoad = load i64* %a
%comptmp = icmp sgt i64 %bLoad, %aLoad
%cast = sitofp i1 %comptmp to double
%ternCond = fcmp one double %cast, 0.000000e+00
br i1 %ternCond, label %then, label %else
then: ; preds = %entry
%cLoad = load i64* %c
%bLoad1 = load i64* %b
%comptmp2 = icmp sgt i64 %cLoad, %bLoad1
%cast3 = sitofp i1 %comptmp2 to double
%ternCond4 = fcmp one double %cast3, 0.000000e+00
br i1 %ternCond4, label %then5, label %else6
else: ; preds = %entry
%aLoad10 = load i64* %a
br label %merge
merge: ; preds = %else, %merge7
%iftmp11 = phi i64 [ %iftmp, %merge7 ], [ %aLoad10, %else ]
%castResult = trunc i64 %iftmp11 to i32
ret i32 %castResult
then5: ; preds = %then
%cLoad8 = load i64* %c
br label %merge7
else6: ; preds = %then
%bLoad9 = load i64* %b
br label %merge7
merge7: ; preds = %else6, %then5
%iftmp = phi i64 [ %cLoad8, %then5 ], [ %bLoad9, %else6 ]
br label %merge
}
==================================
Post-optimization, module =
==================================
; ModuleID = 'scissors'
define i32 @scissors() {
entry:
ret i32 3
}
Got function at address 0xd649010
===========================
Dice roll... 3!
===========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment