Skip to content

Instantly share code, notes, and snippets.

@rhysd
Created February 20, 2017 11:11
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 rhysd/af81c1c746e831ebd9341fa770171cbd to your computer and use it in GitHub Desktop.
Save rhysd/af81c1c746e831ebd9341fa770171cbd to your computer and use it in GitHub Desktop.
let rec fact_ i acc = if i = 0 then acc else fact_ (i-1) (i+acc) in
let rec fact i = fact_ i 0 in
println_int (fact x)
; ModuleID = 'foo.ml'
source_filename = "foo.ml"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin15.6.0"
@x = external global i64
declare void @println_int(i64) #0
; Function Attrs: inlinehint nounwind ssp uwtable
define i64 @"fact$t4"(i64 %"i$t5") #1 {
entry:
%0 = call i64 @"fact_$t1"(i64 %"i$t5", i64 0)
ret i64 %0
}
; Function Attrs: inlinehint nounwind ssp uwtable
define i64 @"fact_$t1"(i64 %"i$t2", i64 %"acc$t3") #1 {
entry:
%eql = icmp eq i64 %"i$t2", 0
br i1 %eql, label %if.then, label %if.else
if.then: ; preds = %entry
br label %if.end
if.else: ; preds = %entry
%sub = sub i64 %"i$t2", 1
%add = add i64 %"i$t2", %"acc$t3"
%0 = call i64 @"fact_$t1"(i64 %sub, i64 %add)
br label %if.end
if.end: ; preds = %if.else, %if.then
%if.merge = phi i64 [ %"acc$t3", %if.then ], [ %0, %if.else ]
ret i64 %if.merge
}
; Function Attrs: ssp uwtable
define i32 @main() #2 {
entry:
%x = load i64, i64* @x
%0 = call i64 @"fact$t4"(i64 %x)
call void @println_int(i64 %0)
ret i32 0
}
attributes #0 = { "disable-tail-calls"="false" }
attributes #1 = { inlinehint nounwind ssp uwtable "disable-tail-calls"="false" }
attributes #2 = { ssp uwtable "disable-tail-calls"="false" }
; ModuleID = 'foo.ml'
source_filename = "foo.ml"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin15.6.0"
@x = external local_unnamed_addr global i64
declare void @println_int(i64) local_unnamed_addr #0
; Function Attrs: inlinehint norecurse nounwind readnone ssp uwtable
define i64 @"fact$t4"(i64 %"i$t5") local_unnamed_addr #1 {
entry:
%eql1.i = icmp eq i64 %"i$t5", 0
br i1 %eql1.i, label %"fact_$t1.exit", label %if.else.preheader.i
if.else.preheader.i: ; preds = %entry
%0 = add i64 %"i$t5", -1
%1 = mul i64 %0, %0
%2 = zext i64 %0 to i65
%3 = add i64 %"i$t5", -2
%4 = zext i64 %3 to i65
%5 = mul i65 %2, %4
%6 = lshr i65 %5, 1
%7 = trunc i65 %6 to i64
%8 = add i64 %1, %"i$t5"
%9 = sub i64 %8, %7
br label %"fact_$t1.exit"
"fact_$t1.exit": ; preds = %entry, %if.else.preheader.i
%"acc$t3.tr.lcssa.i" = phi i64 [ 0, %entry ], [ %9, %if.else.preheader.i ]
ret i64 %"acc$t3.tr.lcssa.i"
}
; Function Attrs: inlinehint nounwind readnone ssp uwtable
define i64 @"fact_$t1"(i64 %"i$t2", i64 %"acc$t3") local_unnamed_addr #2 {
entry:
%eql1 = icmp eq i64 %"i$t2", 0
br i1 %eql1, label %if.end, label %if.else.preheader
if.else.preheader: ; preds = %entry
%0 = add i64 %"i$t2", -1
%1 = mul i64 %0, %0
%2 = zext i64 %0 to i65
%3 = add i64 %"i$t2", -2
%4 = zext i64 %3 to i65
%5 = mul i65 %2, %4
%6 = lshr i65 %5, 1
%7 = trunc i65 %6 to i64
%8 = add i64 %"acc$t3", %"i$t2"
%9 = add i64 %8, %1
%10 = sub i64 %9, %7
br label %if.end
if.end: ; preds = %if.else.preheader, %entry
%"acc$t3.tr.lcssa" = phi i64 [ %"acc$t3", %entry ], [ %10, %if.else.preheader ]
ret i64 %"acc$t3.tr.lcssa"
}
; Function Attrs: ssp uwtable
define i32 @main() local_unnamed_addr #3 {
entry:
%x = load i64, i64* @x, align 8
%eql1.i.i = icmp eq i64 %x, 0
br i1 %eql1.i.i, label %"fact$t4.exit", label %if.else.preheader.i.i
if.else.preheader.i.i: ; preds = %entry
%0 = add i64 %x, -1
%1 = mul i64 %0, %0
%2 = zext i64 %0 to i65
%3 = add i64 %x, -2
%4 = zext i64 %3 to i65
%5 = mul i65 %2, %4
%6 = lshr i65 %5, 1
%7 = trunc i65 %6 to i64
%8 = add i64 %1, %x
%9 = sub i64 %8, %7
br label %"fact$t4.exit"
"fact$t4.exit": ; preds = %entry, %if.else.preheader.i.i
%"acc$t3.tr.lcssa.i.i" = phi i64 [ 0, %entry ], [ %9, %if.else.preheader.i.i ]
tail call void @println_int(i64 %"acc$t3.tr.lcssa.i.i")
ret i32 0
}
attributes #0 = { "disable-tail-calls"="false" }
attributes #1 = { inlinehint norecurse nounwind readnone ssp uwtable "disable-tail-calls"="false" }
attributes #2 = { inlinehint nounwind readnone ssp uwtable "disable-tail-calls"="false" }
attributes #3 = { ssp uwtable "disable-tail-calls"="false" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment