Add a lint check for an indirectbr destination which has not
[oota-llvm.git] / test / Other / lint.ll
1 ; RUN: opt -lint -disable-output < %s |& FileCheck %s
2 target datalayout = "e-p:64:64:64"
3
4 declare fastcc void @bar()
5 declare void @llvm.stackrestore(i8*)
6 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
7 declare void @has_sret(i8* sret %p)
8 declare void @has_noaliases(i32* noalias %p, i32* %q)
9 declare void @one_arg(i32)
10
11 @CG = constant i32 7
12
13 define i32 @foo() noreturn {
14 ; CHECK: Caller and callee calling convention differ
15   call void @bar()
16 ; CHECK: Null pointer dereference
17   store i32 0, i32* null
18 ; CHECK: Null pointer dereference
19   %t = load i32* null
20 ; CHECK: Undef pointer dereference
21   store i32 0, i32* undef
22 ; CHECK: Undef pointer dereference
23   %u = load i32* undef
24 ; CHECK: All-ones pointer dereference
25   store i32 0, i32* inttoptr (i64 -1 to i32*)
26 ; CHECK: Address one pointer dereference
27   store i32 0, i32* inttoptr (i64 1 to i32*)
28 ; CHECK: Memory reference address is misaligned
29   %x = inttoptr i32 1 to i32*
30   load i32* %x, align 4
31 ; CHECK: Division by zero
32   %sd = sdiv i32 2, 0
33 ; CHECK: Division by zero
34   %ud = udiv i32 2, 0
35 ; CHECK: Division by zero
36   %sr = srem i32 2, 0
37 ; CHECK: Division by zero
38   %ur = urem i32 2, 0
39 ; CHECK: extractelement index out of range
40   %ee = extractelement <4 x i32> zeroinitializer, i32 4
41 ; CHECK: insertelement index out of range
42   %ie = insertelement <4 x i32> zeroinitializer, i32 0, i32 4
43 ; CHECK: Shift count out of range
44   %r = lshr i32 0, 32
45 ; CHECK: Shift count out of range
46   %q = ashr i32 0, 32
47 ; CHECK: Shift count out of range
48   %l = shl i32 0, 32
49 ; CHECK: xor(undef, undef)
50   %xx = xor i32 undef, undef
51 ; CHECK: sub(undef, undef)
52   %xs = sub i32 undef, undef
53
54 ; CHECK: Write to read-only memory
55   store i32 8, i32* @CG
56 ; CHECK: Write to text section
57   store i32 8, i32* bitcast (i32()* @foo to i32*)
58 ; CHECK: Load from block address
59   %lb = load i32* bitcast (i8* blockaddress(@foo, %next) to i32*)
60 ; CHECK: Call to block address
61   call void()* bitcast (i8* blockaddress(@foo, %next) to void()*)()
62 ; CHECK: Undefined behavior: Null pointer dereference
63   call void @llvm.stackrestore(i8* null)
64 ; CHECK: Undefined behavior: Null pointer dereference
65   call void @has_sret(i8* null)
66 ; CHECK: Unusual: noalias argument aliases another argument
67   call void @has_noaliases(i32* @CG, i32* @CG)
68 ; CHECK: Call argument count mismatches callee argument count
69   call void (i32, i32)* bitcast (void (i32)* @one_arg to void (i32, i32)*)(i32 0, i32 0)
70 ; CHECK: Call argument count mismatches callee argument count
71   call void ()* bitcast (void (i32)* @one_arg to void ()*)()
72 ; CHECK: Call argument type mismatches callee parameter type
73   call void (float)* bitcast (void (i32)* @one_arg to void (float)*)(float 0.0)
74
75 ; CHECK: Write to read-only memory
76   call void @llvm.memcpy.p0i8.p0i8.i64(i8* bitcast (i32* @CG to i8*), i8* bitcast (i32* @CG to i8*), i64 1, i32 1, i1 0)
77
78   br label %next
79
80 next:
81 ; CHECK: Static alloca outside of entry block
82   %a = alloca i32
83 ; CHECK: Return statement in function with noreturn attribute
84   ret i32 0
85
86 foo:
87   %z = add i32 0, 0
88 ; CHECK: unreachable immediately preceded by instruction without side effects
89   unreachable
90 }
91
92 ; CHECK: Unnamed function with non-local linkage
93 define void @0() nounwind {
94   ret void
95 }
96
97 ; CHECK: va_start called in a non-varargs function
98 declare void @llvm.va_start(i8*)
99 define void @not_vararg(i8* %p) nounwind {
100   call void @llvm.va_start(i8* %p)
101   ret void
102 }
103
104 ; CHECK: Undefined behavior: Branch to non-blockaddress
105 ; CHECK: Unusual: indirectbr destination has not had its address taken
106 define void @use_indbr() {
107   indirectbr i8* bitcast (i32()* @foo to i8*), [label %block]
108 block:
109   unreachable
110 }
111
112 ; CHECK: Undefined behavior: Call with "tail" keyword references alloca
113 declare void @tailcallee(i8*)
114 define void @use_tail(i8* %valist) {
115   %t = alloca i8
116   tail call void @tailcallee(i8* %t)
117   ret void
118 }
119
120 ; CHECK: Unusual: Returning alloca value
121 define i8* @return_local(i32 %n, i32 %m) {
122   %t = alloca i8, i32 %n
123   %s = getelementptr i8* %t, i32 %m
124   ret i8* %s
125 }
126
127 ; CHECK: Unusual: Returning alloca value
128 define i32* @return_obscured_local() {
129 entry:
130   %retval = alloca i32*
131   %x = alloca i32
132   store i32* %x, i32** %retval
133   br label %next
134 next:
135   %t0 = load i32** %retval
136   %t1 = insertvalue { i32, i32, i32* } zeroinitializer, i32* %t0, 2
137   %t2 = extractvalue { i32, i32, i32* } %t1, 2
138   br label %exit
139 exit:
140   %t3 = phi i32* [ %t2, %next ]
141   %t4 = bitcast i32* %t3 to i32*
142   %t5 = ptrtoint i32* %t4 to i64
143   %t6 = add i64 %t5, 0
144   %t7 = inttoptr i64 %t6 to i32*
145   ret i32* %t7
146 }
147
148 ; CHECK: Undefined behavior: Undef pointer dereference
149 define i32* @self_reference() {
150 entry:
151   unreachable
152 exit:
153   %t3 = phi i32* [ %t4, %exit ]
154   %t4 = bitcast i32* %t3 to i32*
155   %x = volatile load i32* %t3
156   br label %exit
157 }
158
159 ; CHECK: Call return type mismatches callee return type
160 %struct = type { double, double }
161 declare i32 @nonstruct_callee() nounwind
162 define void @struct_caller() nounwind {
163 entry:
164   call %struct bitcast (i32 ()* @foo to %struct ()*)()
165
166   ; CHECK: Undefined behavior: indirectbr with no destinations
167   indirectbr i8* null, []
168 }