Created
June 22, 2021 00:27
-
-
Save Kixiron/fa35903186b7304241eb2e35b346d08a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare i32 @printf(i8* noalias nocapture, ...) | |
@.str = private unnamed_addr constant [4 x i8] c"%i\0A\00", align 1 | |
define i32 @main() { | |
entry: | |
br label %header | |
header: | |
%x = phi i32 [ 10, %entry ], [ %next_x, %header ] | |
%fib_res = call i32 @fib(i32 %x) | |
call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i32 %fib_res) | |
%next_x = sub nsw i32 %x, 1 | |
%should_break = icmp eq i32 %fib_res, 0 | |
br i1 %should_break, label %tail, label %header | |
tail: | |
ret i32 0 | |
} | |
define i32 @fib(i32 %n) inlinehint nofree willreturn nosync nounwind readnone speculatable { | |
switch i32 %n, label %fib [ i32 0, label %identity | |
i32 1, label %identity ] | |
identity: | |
ret i32 %n | |
fib: | |
%minus_one = sub nsw i32 %n, 1 | |
%minus_two = sub nsw i32 %n, 2 | |
%first = tail call i32 @fib(i32 %minus_one) | |
%second = tail call i32 @fib(i32 %minus_two) | |
%sum = add nsw i32 %first, %second | |
ret i32 %sum | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment