Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
Created January 1, 2016 03:13
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 harlanhaskins/b539bb9235d25225c313 to your computer and use it in GitHub Desktop.
Save harlanhaskins/b539bb9235d25225c313 to your computer and use it in GitHub Desktop.
define private i64 @fact(i64 %n) {
entry:
%n1 = alloca i64
store i64 %n, i64* %n1
%n2 = load i64, i64* %n1
%eqtmp = icmp eq i64 %n2, 0
%0 = sext i1 %eqtmp to i64
%ifcond = icmp ne i64 %0, 0
br i1 %ifcond, label %then, label %else
then: ; preds = %entry
br label %ifcont
else: ; preds = %entry
%n3 = load i64, i64* %n1
%n4 = load i64, i64* %n1
%subtmp = sub i64 %n4, 1
%calltmp = call i64 @fact(i64 %subtmp)
%multmp = mul i64 %n3, %calltmp
br label %ifcont
ifcont: ; preds = %else, %then
%iftmp = phi i64 [ 1, %then ], [ %multmp, %else ]
ret i64 %iftmp
}
(def fact (n)
(if (= n 0)
1
(* n (fact (- n 1)))))
define private i64 @fact(i64 %n) {
entry:
br label %tailrecurse
tailrecurse: ; preds = %else, %entry
%accumulator.tr = phi i64 [ 1, %entry ], [ %multmp, %else ]
%n.tr = phi i64 [ %n, %entry ], [ %subtmp, %else ]
%eqtmp = icmp eq i64 %n.tr, 0
br i1 %eqtmp, label %ifcont, label %else
else: ; preds = %tailrecurse
%subtmp = add i64 %n.tr, -1
%multmp = mul i64 %accumulator.tr, %n.tr
br label %tailrecurse
ifcont: ; preds = %tailrecurse
ret i64 %accumulator.tr
}
.align 4, 0x90
l_fact: ## @fact
.cfi_startproc
## BB#0: ## %entry
movl $1, %eax
jmp LBB1_1
.align 4, 0x90
LBB1_2: ## %else
## in Loop: Header=BB1_1 Depth=1
imulq %rdi, %rax
decq %rdi
LBB1_1: ## %tailrecurse
## =>This Inner Loop Header: Depth=1
testq %rdi, %rdi
jne LBB1_2
## BB#3: ## %ifcont
retq
.cfi_endproc
.align 4, 0x90
l_fact: ## @fact
.cfi_startproc
## BB#0: ## %entry
pushq %rbx
Ltmp2:
.cfi_def_cfa_offset 16
subq $16, %rsp
Ltmp3:
.cfi_def_cfa_offset 32
Ltmp4:
.cfi_offset %rbx, -16
movq %rdi, 8(%rsp)
cmpq $1, %rdi
movl $1, %eax
jb LBB1_2
## BB#1: ## %else
movq 8(%rsp), %rbx
leaq -1(%rbx), %rdi
callq l_fact
imulq %rbx, %rax
LBB1_2: ## %ifcont
addq $16, %rsp
popq %rbx
retq
.cfi_endproc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment