Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Last active August 29, 2015 14:07
Show Gist options
  • Save UplinkCoder/00f67944fe110178829f to your computer and use it in GitHub Desktop.
Save UplinkCoder/00f67944fe110178829f to your computer and use it in GitHub Desktop.
TernaryOperator test and output
//T compiles:yes
//T has-passed:yes
//T retval:42
//? Tests TernaryOperator
import std.stdio;
int main() {
auto s = 0;
ubyte right = cast(ubyte) 42;
bool b_wrong = cast(bool) 0;
int i_wrong;
uint ui_wrong = 0;
bool[12] bs;// initialized in the loop below.
for(int i=0;i<bs.length;i++) {
*(&bs[0]+i) = cast(bool) (i%2);
}
int ret=18;
foreach(b;bs) {
(b||(true&&false))?(ret+=7):(ret-=3);
}
if ((i_wrong?right:b_wrong)?5:ret != 42) {
ret=0;
}
return ret;
}
; ModuleID = 'test0168.d'
@.str = private constant [10 x i8] c"test0168.d"
@.cstr = private unnamed_addr constant [11 x i8] c"test0168.d\00"
define weak_odr i32 @_Dmain() {
br label %body
body: ; preds = %0
%1 = call i32 @_D8test01684mainFMZi()
ret i32 %1
}
define weak_odr i32 @_D8test01684mainFMZi() {
%s = alloca i32
%right = alloca i8
%b_wrong = alloca i1
%i_wrong = alloca i32
%ui_wrong = alloca i32
%bs = alloca [12 x i1]
%i = alloca i32
%ret = alloca i32
%1 = alloca i32
%b = alloca i1
br label %body
body: ; preds = %0
store i32 0, i32* %s
store i8 42, i8* %right
store i1 false, i1* %b_wrong
store i32 0, i32* %i_wrong
store i32 0, i32* %ui_wrong
store [12 x i1] undef, [12 x i1]* %bs
store i32 0, i32* %i
br label %for
for: ; preds = %increment, %body
%2 = load i32* %i
%3 = load [12 x i1]* %bs
%4 = icmp ult i32 %2, 12
br i1 %4, label %do, label %done
increment: ; preds = %bound_ok
%5 = load i32* %i
%6 = add i32 %5, 1
store i32 %6, i32* %i
br label %for
do: ; preds = %for
br i1 true, label %bound_ok, label %bound_fail, !prof !0
bound_fail: ; preds = %do
call void @_d_arraybounds({ i32, i8* } { i32 10, i8* getelementptr inbounds ([11 x i8]* @.cstr, i32 0, i32 0) }, i32 17)
unreachable
bound_ok: ; preds = %do
%7 = getelementptr inbounds [12 x i1]* %bs, i32 0, i32 0
%8 = load i32* %i
%9 = getelementptr inbounds i1* %7, i32 %8
%10 = load i32* %i
%11 = srem i32 %10, 2
%12 = icmp ne i32 %11, 0
store i1 %12, i1* %9
br label %increment
done: ; preds = %for
store i32 18, i32* %ret
store i32 0, i32* %1
%13 = load i32* %1
br label %for1
for1: ; preds = %increment2, %done
%14 = load i32* %1
%15 = icmp ult i32 %14, 12
br i1 %15, label %do3, label %done4
increment2: ; preds = %result
%16 = load i32* %1
%17 = add i32 %16, 1
store i32 %17, i32* %1
br label %for1
do3: ; preds = %for1
store i1 false, i1* %b
%18 = load i32* %1
%19 = icmp ult i32 %18, 12
br i1 %19, label %bound_ok6, label %bound_fail5, !prof !0
bound_fail5: ; preds = %do3
call void @_d_arraybounds({ i32, i8* } { i32 10, i8* getelementptr inbounds ([11 x i8]* @.cstr, i32 0, i32 0) }, i32 22)
unreachable
bound_ok6: ; preds = %do3
%20 = getelementptr inbounds [12 x i1]* %bs, i32 0, i32 %18
%21 = load i1* %20
store i1 %21, i1* %b
%22 = load i1* %b
br i1 %22, label %ifTrue, label %ifFalse
ifTrue: ; preds = %bound_ok6
%23 = load i32* %ret
%24 = add i32 %23, 7
store i32 %24, i32* %ret
br label %result
ifFalse: ; preds = %bound_ok6
%25 = load i32* %ret
%26 = sub i32 %25, 3
store i32 %26, i32* %ret
br label %result
result: ; preds = %ifFalse, %ifTrue
%27 = phi i32 [ %24, %ifTrue ], [ %26, %ifFalse ]
br label %increment2
done4: ; preds = %for1
%28 = load i32* %i_wrong
%29 = icmp ne i32 %28, 0
br i1 %29, label %ifTrue7, label %ifFalse8
ifTrue7: ; preds = %done4
%30 = load i8* %right
%31 = zext i8 %30 to i32
br label %result9
ifFalse8: ; preds = %done4
%32 = load i1* %b_wrong
%33 = zext i1 %32 to i32
br label %result9
result9: ; preds = %ifFalse8, %ifTrue7
%34 = phi i32 [ %31, %ifTrue7 ], [ %33, %ifFalse8 ]
%35 = icmp ne i32 %34, 0
br i1 %35, label %ifTrue10, label %ifFalse11
ifTrue10: ; preds = %result9
br label %result12
ifFalse11: ; preds = %result9
%36 = load i32* %ret
%37 = icmp ne i32 %36, 42
%38 = zext i1 %37 to i32
br label %result12
result12: ; preds = %ifFalse11, %ifTrue10
%39 = phi i32 [ 5, %ifTrue10 ], [ %38, %ifFalse11 ]
%40 = icmp ne i32 %39, 0
br i1 %40, label %then, label %else
then: ; preds = %result12
store i32 0, i32* %ret
br label %merge
else: ; preds = %result12
br label %merge
merge: ; preds = %else, %then
%41 = load i32* %ret
ret i32 %41
}
declare void @_d_arraybounds({ i32, i8* }, i32)
!0 = metadata !{metadata !"branch_weights", i32 65536, i32 0}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment