Skip to content

Instantly share code, notes, and snippets.

@jgouly
Created March 3, 2013 00:14
Show Gist options
  • Save jgouly/5073863 to your computer and use it in GitHub Desktop.
Save jgouly/5073863 to your computer and use it in GitHub Desktop.
; ModuleID = 'llvm-re'
@transitiontable = internal constant [3 x [2 x i32]] [[2 x i32] [i32 1, i32 -1], [2 x i32] [i32 -1, i32 2], [2 x i32] [i32 3, i32 -1]]
define i32 @match(i8*) {
entry:
%Index = alloca i32
store i32 0, i32* %Index
%IsMatch = alloca i32
store i32 0, i32* %IsMatch
%CurState = alloca i32
store i32 0, i32* %CurState
br label %loop.start
fail: ; preds = %loop.start
ret i32 0
success: ; preds = %loop.end
ret i32 1
loop.start: ; preds = %loop.end, %entry
%1 = load i32* %Index
%CurChar = getelementptr i8* %0, i32 %1
%2 = load i8* %CurChar
%3 = call i32 @getcharindex(i8 %2)
%4 = load i32* %CurState
%5 = getelementptr [3 x [2 x i32]]* @transitiontable, i32 0, i32 %4, i32 %3
%6 = load i32* %5
%7 = icmp eq i32 %6, -1
br i1 %7, label %fail, label %loop.end
loop.end: ; preds = %loop.start
store i32 %6, i32* %CurState
%8 = load i32* %Index
%9 = add i32 %8, 1
store i32 %9, i32* %Index
%10 = icmp eq i32 %6, 3
br i1 %10, label %success, label %loop.start
}
define internal i32 @getcharindex(i8) {
entry:
switch i8 %0, label %default [
i8 97, label %1
i8 98, label %2
]
default: ; preds = %entry
ret i32 -1
; <label>:1 ; preds = %entry
ret i32 0
; <label>:2 ; preds = %entry
ret i32 1
}
optimised:
@transitiontable = internal unnamed_addr constant [3 x [2 x i32]] [[2 x i32] [i32 1, i32 -1], [2 x i32] [i32 -1, i32 2], [2 x i32] [i32 3, i32 -1]]
define i32 @match(i8* nocapture) #0 {
entry:
br label %loop.start
fail: ; preds = %loop.end, %getcharindex.exit
%merge = phi i32 [ 0, %getcharindex.exit ], [ 1, %loop.end ]
ret i32 %merge
loop.start: ; preds = %loop.end, %entry
%1 = phi i32 [ %7, %loop.end ], [ 0, %entry ]
%2 = phi i32 [ %9, %loop.end ], [ 0, %entry ]
%CurChar = getelementptr i8* %0, i32 %2
%3 = load i8* %CurChar
switch i8 %3, label %getcharindex.exit [
i8 97, label %4
i8 98, label %5
]
; <label>:4 ; preds = %loop.start
br label %getcharindex.exit
; <label>:5 ; preds = %loop.start
br label %getcharindex.exit
getcharindex.exit: ; preds = %loop.start, %4, %5
%merge.i = phi i32 [ -1, %loop.start ], [ 0, %4 ], [ 1, %5 ]
%6 = getelementptr [3 x [2 x i32]]* @transitiontable, i32 0, i32 %1, i32 %merge.i
%7 = load i32* %6
%8 = icmp eq i32 %7, -1
br i1 %8, label %fail, label %loop.end
loop.end: ; preds = %getcharindex.exit
%9 = add i32 %2, 1
%10 = icmp eq i32 %7, 3
br i1 %10, label %fail, label %loop.start
}
attributes #0 = { nounwind readonly }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment