Skip to content

Instantly share code, notes, and snippets.

@dongjinahn
Created January 13, 2019 02:56
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 dongjinahn/288556fac5eff7b5d0b3913abb81bc4f to your computer and use it in GitHub Desktop.
Save dongjinahn/288556fac5eff7b5d0b3913abb81bc4f to your computer and use it in GitHub Desktop.
; ModuleID = 'factorial.mem2reg.bc'
source_filename = "tests/factorial.c"
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@g = dso_local global i32 5, align 4
; Function Attrs: noinline nounwind uwtable
define dso_local i32 @factorial(i32 %n) #0 {
entry:
%cmp = icmp eq i32 %n, 1
br i1 %cmp, label %if.then, label %if.end
if.then: ; preds = %entry
br label %return
if.end: ; preds = %entry
%sub = sub nsw i32 %n, 1
%call = call i32 @factorial(i32 %sub)
%mul = mul nsw i32 %n, %call
br label %return
return: ; preds = %if.end, %if.then
%retval.0 = phi i32 [ 1, %if.then ], [ %mul, %if.end ]
ret i32 %retval.0
}
attributes #0 = { noinline nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.module.flags = !{!0}
!llvm.ident = !{!1}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{!"clang version 7.0.1-svn348686-1~exp1~20190104180606.53 (branches/release_70)"}
@g = dso_local global i32 5, align 4
define dso_local i32 @factorial(i32 %n) #0 {
entry:
%cmp = icmp eq i32 %n, 1
br i1 %cmp, label %if.then, label %if.end
if.then: ; preds = %entry
br label %return
if.end: ; preds = %entry
%sub = sub nsw i32 %n, 1
%call = call i32 @factorial(i32 %sub)
%mul = mul nsw i32 %n, %call
br label %return
return: ; preds = %if.end, %if.then
%retval.0 = phi i32 [ 1, %if.then ], [ %mul, %if.end ]
ret i32 %retval.0
}
clang-7 -O0 -Xclang -disable-O0-optnone -fno-discard-value-names -emit-llvm -c factorial.c
opt-7 -mem2reg factorial.bc -o factorial.mem2reg.bc
llvm-dis-7 factorial.mem2reg.bc -o factorial.mem2reg.ll
int g = 5;
int factorial(int n) {
if (n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int a = 1;
int b = 2;
int c = a + b;
int d = 3;
int e = c + d;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment