Commit 4ad2e109 authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added a small optimization example

parent d1d5c13d
...@@ -7,6 +7,7 @@ purpose of practicing OCaml LLVM bindings and seeing how well LLVM optimizes ...@@ -7,6 +7,7 @@ purpose of practicing OCaml LLVM bindings and seeing how well LLVM optimizes
arrays. Brainfuck commands are transformed to LLVM IR, which is generated in arrays. Brainfuck commands are transformed to LLVM IR, which is generated in
such a way that it is easy to optimize for LLVM's `opt` utility. such a way that it is easy to optimize for LLVM's `opt` utility.
Building and usage Building and usage
================== ==================
...@@ -15,7 +16,7 @@ Building the `bf` compiler (Debian dependencies): ...@@ -15,7 +16,7 @@ Building the `bf` compiler (Debian dependencies):
$ sudo apt-get install ocaml llvm-3.4 libllvm-3.4-ocaml-dev $ sudo apt-get install ocaml llvm-3.4 libllvm-3.4-ocaml-dev
$ make $ make
Building a brainfuck program (plain and optimized): Building a Brainfuck program (plain and optimized):
$ make hello hello-opt # compile file "hello.b" to binaries $ make hello hello-opt # compile file "hello.b" to binaries
$ ./hello $ ./hello
...@@ -35,3 +36,58 @@ Examining generated LLVM: ...@@ -35,3 +36,58 @@ Examining generated LLVM:
... ...
$ echo ++++++++++++. | ./bf | opt -O3 -S $ echo ++++++++++++. | ./bf | opt -O3 -S
... ...
Optimization example
--------------------
The `text.py` utility genrates single-cell Brainfuck code for a given text:
$ ./text.py Hello World!
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
+++++++++++++++++++++++++++++.
+++++++.
.
+++.
-------------------------------------------------------------------------------.
+++++++++++++++++++++++++++++++++++++++++++++++++++++++.
++++++++++++++++++++++++.
+++.
------.
--------.
-------------------------------------------------------------------.
-----------------------.
The LLVM optimization engine is able to completely optimize away array accesses
(in the absence of loops):
$ ./text.py Hello World! | ./bf | opt -O3 -S
; ModuleID = '<stdin>'
target datalayout = "e"
; Function Attrs: nounwind
declare i32 @putchar(i8) #0
declare void @exit(i32)
define void @_start() {
entry:
%0 = tail call i32 @putchar(i8 72)
%1 = tail call i32 @putchar(i8 101)
%2 = tail call i32 @putchar(i8 108)
%3 = tail call i32 @putchar(i8 108)
%4 = tail call i32 @putchar(i8 111)
%5 = tail call i32 @putchar(i8 32)
%6 = tail call i32 @putchar(i8 87)
%7 = tail call i32 @putchar(i8 111)
%8 = tail call i32 @putchar(i8 114)
%9 = tail call i32 @putchar(i8 108)
%10 = tail call i32 @putchar(i8 100)
%11 = tail call i32 @putchar(i8 33)
%12 = tail call i32 @putchar(i8 10)
tail call void @exit(i32 0)
ret void
}
attributes #0 = { nounwind }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment