blob: f6787e973ed761b5f4cd66d849905023491e0aea [file] [log] [blame]
Chandler Carruth5da53432012-07-02 18:37:59 +00001; RUN: opt -basicaa -lint -disable-output < %s 2>&1 | FileCheck %s
Dan Gohman98bc4372010-04-08 18:47:09 +00002target datalayout = "e-p:64:64:64"
3
4declare fastcc void @bar()
Dan Gohmana20a5cd2010-05-26 22:21:25 +00005declare void @llvm.stackrestore(i8*)
Dan Gohmanc575ec62010-05-28 17:44:00 +00006declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
Dan Gohman0fa67e42010-05-28 21:43:57 +00007declare void @has_sret(i8* sret %p)
8declare void @has_noaliases(i32* noalias %p, i32* %q)
9declare void @one_arg(i32)
Dan Gohman98bc4372010-04-08 18:47:09 +000010
Dan Gohman299e7b92010-04-30 19:05:00 +000011@CG = constant i32 7
12
Dan Gohman98bc4372010-04-08 18:47:09 +000013define i32 @foo() noreturn {
Duncan Sands3f4d0b12012-09-25 10:00:49 +000014 %buf = alloca i8
15 %buf2 = alloca {i8, i8}, align 2
Dan Gohman98bc4372010-04-08 18:47:09 +000016; CHECK: Caller and callee calling convention differ
17 call void @bar()
18; CHECK: Null pointer dereference
19 store i32 0, i32* null
20; CHECK: Null pointer dereference
21 %t = load i32* null
Dan Gohman7808d492010-04-08 23:05:57 +000022; CHECK: Undef pointer dereference
23 store i32 0, i32* undef
24; CHECK: Undef pointer dereference
25 %u = load i32* undef
Dan Gohman0fa67e42010-05-28 21:43:57 +000026; CHECK: All-ones pointer dereference
27 store i32 0, i32* inttoptr (i64 -1 to i32*)
28; CHECK: Address one pointer dereference
29 store i32 0, i32* inttoptr (i64 1 to i32*)
Dan Gohman98bc4372010-04-08 18:47:09 +000030; CHECK: Memory reference address is misaligned
Duncan Sands3f4d0b12012-09-25 10:00:49 +000031 store i8 0, i8* %buf, align 2
32; CHECK: Memory reference address is misaligned
33 %gep = getelementptr {i8, i8}* %buf2, i32 0, i32 1
34 store i8 0, i8* %gep, align 2
Dan Gohman98bc4372010-04-08 18:47:09 +000035; CHECK: Division by zero
36 %sd = sdiv i32 2, 0
37; CHECK: Division by zero
38 %ud = udiv i32 2, 0
39; CHECK: Division by zero
40 %sr = srem i32 2, 0
41; CHECK: Division by zero
42 %ur = urem i32 2, 0
Dan Gohman7808d492010-04-08 23:05:57 +000043; CHECK: extractelement index out of range
44 %ee = extractelement <4 x i32> zeroinitializer, i32 4
45; CHECK: insertelement index out of range
46 %ie = insertelement <4 x i32> zeroinitializer, i32 0, i32 4
47; CHECK: Shift count out of range
48 %r = lshr i32 0, 32
49; CHECK: Shift count out of range
50 %q = ashr i32 0, 32
51; CHECK: Shift count out of range
52 %l = shl i32 0, 32
Dan Gohman9ba08a42010-04-09 01:39:53 +000053; CHECK: xor(undef, undef)
54 %xx = xor i32 undef, undef
55; CHECK: sub(undef, undef)
56 %xs = sub i32 undef, undef
Dan Gohman299e7b92010-04-30 19:05:00 +000057
58; CHECK: Write to read-only memory
59 store i32 8, i32* @CG
60; CHECK: Write to text section
61 store i32 8, i32* bitcast (i32()* @foo to i32*)
62; CHECK: Load from block address
63 %lb = load i32* bitcast (i8* blockaddress(@foo, %next) to i32*)
64; CHECK: Call to block address
65 call void()* bitcast (i8* blockaddress(@foo, %next) to void()*)()
Dan Gohmana20a5cd2010-05-26 22:21:25 +000066; CHECK: Undefined behavior: Null pointer dereference
67 call void @llvm.stackrestore(i8* null)
Dan Gohman0fa67e42010-05-28 21:43:57 +000068; CHECK: Undefined behavior: Null pointer dereference
69 call void @has_sret(i8* null)
70; CHECK: Unusual: noalias argument aliases another argument
71 call void @has_noaliases(i32* @CG, i32* @CG)
72; CHECK: Call argument count mismatches callee argument count
73 call void (i32, i32)* bitcast (void (i32)* @one_arg to void (i32, i32)*)(i32 0, i32 0)
74; CHECK: Call argument count mismatches callee argument count
75 call void ()* bitcast (void (i32)* @one_arg to void ()*)()
76; CHECK: Call argument type mismatches callee parameter type
77 call void (float)* bitcast (void (i32)* @one_arg to void (float)*)(float 0.0)
Dan Gohman299e7b92010-04-30 19:05:00 +000078
Dan Gohmanc575ec62010-05-28 17:44:00 +000079; CHECK: Write to read-only memory
80 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)
81
Dan Gohman98bc4372010-04-08 18:47:09 +000082 br label %next
83
84next:
85; CHECK: Static alloca outside of entry block
86 %a = alloca i32
87; CHECK: Return statement in function with noreturn attribute
88 ret i32 0
Dan Gohman9ba08a42010-04-09 01:39:53 +000089
90foo:
91 %z = add i32 0, 0
92; CHECK: unreachable immediately preceded by instruction without side effects
93 unreachable
94}
95
96; CHECK: Unnamed function with non-local linkage
97define void @0() nounwind {
98 ret void
99}
100
101; CHECK: va_start called in a non-varargs function
102declare void @llvm.va_start(i8*)
103define void @not_vararg(i8* %p) nounwind {
104 call void @llvm.va_start(i8* %p)
105 ret void
Dan Gohman98bc4372010-04-08 18:47:09 +0000106}
Dan Gohman299e7b92010-04-30 19:05:00 +0000107
Dan Gohman1249adf2010-05-26 21:46:36 +0000108; CHECK: Undefined behavior: Branch to non-blockaddress
Dan Gohman299e7b92010-04-30 19:05:00 +0000109define void @use_indbr() {
110 indirectbr i8* bitcast (i32()* @foo to i8*), [label %block]
111block:
112 unreachable
113}
Dan Gohman1249adf2010-05-26 21:46:36 +0000114
Dan Gohmancef9fc32010-05-28 16:34:49 +0000115; CHECK: Undefined behavior: Call with "tail" keyword references alloca
Dan Gohman1249adf2010-05-26 21:46:36 +0000116declare void @tailcallee(i8*)
117define void @use_tail(i8* %valist) {
118 %t = alloca i8
119 tail call void @tailcallee(i8* %t)
Dan Gohman1249adf2010-05-26 21:46:36 +0000120 ret void
121}
Dan Gohmanddba4b72010-05-28 04:33:42 +0000122
Dan Gohmancef9fc32010-05-28 16:34:49 +0000123; CHECK: Unusual: Returning alloca value
Dan Gohmanddba4b72010-05-28 04:33:42 +0000124define i8* @return_local(i32 %n, i32 %m) {
125 %t = alloca i8, i32 %n
126 %s = getelementptr i8* %t, i32 %m
127 ret i8* %s
128}
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000129
Dan Gohmancef9fc32010-05-28 16:34:49 +0000130; CHECK: Unusual: Returning alloca value
Dan Gohman54d7aaa2010-05-28 16:21:24 +0000131define i32* @return_obscured_local() {
132entry:
133 %retval = alloca i32*
134 %x = alloca i32
135 store i32* %x, i32** %retval
136 br label %next
137next:
138 %t0 = load i32** %retval
139 %t1 = insertvalue { i32, i32, i32* } zeroinitializer, i32* %t0, 2
140 %t2 = extractvalue { i32, i32, i32* } %t1, 2
141 br label %exit
142exit:
143 %t3 = phi i32* [ %t2, %next ]
144 %t4 = bitcast i32* %t3 to i32*
145 %t5 = ptrtoint i32* %t4 to i64
146 %t6 = add i64 %t5, 0
147 %t7 = inttoptr i64 %t6 to i32*
148 ret i32* %t7
149}
Dan Gohman862f0342010-05-28 16:45:33 +0000150
151; CHECK: Undefined behavior: Undef pointer dereference
152define i32* @self_reference() {
153entry:
154 unreachable
155exit:
156 %t3 = phi i32* [ %t4, %exit ]
157 %t4 = bitcast i32* %t3 to i32*
Chris Lattner6a144a22011-11-27 06:54:59 +0000158 %x = load volatile i32* %t3
Dan Gohman862f0342010-05-28 16:45:33 +0000159 br label %exit
160}
Dan Gohmanc128e702010-07-12 18:02:04 +0000161
162; CHECK: Call return type mismatches callee return type
163%struct = type { double, double }
164declare i32 @nonstruct_callee() nounwind
165define void @struct_caller() nounwind {
166entry:
167 call %struct bitcast (i32 ()* @foo to %struct ()*)()
Dan Gohmand8968da2010-08-02 23:06:43 +0000168
169 ; CHECK: Undefined behavior: indirectbr with no destinations
170 indirectbr i8* null, []
Dan Gohmanc128e702010-07-12 18:02:04 +0000171}