1; RUN: opt -passes=print-memderefs -S < %s -disable-output -use-dereferenceable-at-point-semantics=0 2>&1 | FileCheck %s --check-prefixes=CHECK,GLOBAL 2; RUN: opt -passes=print-memderefs -S < %s -disable-output -use-dereferenceable-at-point-semantics=1 2>&1 | FileCheck %s --check-prefixes=CHECK,POINT 3 4 5; Uses the print-deref (+ analyze to print) pass to run 6; isDereferenceablePointer() on many load instruction operands 7 8target datalayout = "e-i32:32:64" 9 10%TypeOpaque = type opaque 11 12declare zeroext i1 @return_i1() 13 14declare i32* @foo() 15@globalstr = global [6 x i8] c"hello\00" 16@globali32ptr = external global i32* 17 18%struct.A = type { [8 x i8], [5 x i8] } 19@globalstruct = external global %struct.A 20 21@globalptr.align1 = external global i8, align 1 22@globalptr.align16 = external global i8, align 16 23 24; Loads from sret arguments 25; CHECK-LABEL: 'test_sret' 26; CHECK: %sret_gep{{.*}}(aligned) 27; CHECK-NOT: %sret_gep_outside 28define void @test_sret(%struct.A* sret(%struct.A) %result) { 29 %sret_gep = getelementptr inbounds %struct.A, %struct.A* %result, i64 0, i32 1, i64 2 30 load i8, i8* %sret_gep 31 32 %sret_gep_outside = getelementptr %struct.A, %struct.A* %result, i64 0, i32 1, i64 7 33 load i8, i8* %sret_gep_outside 34 ret void 35} 36 37; CHECK-LABEL: 'test' 38define void @test(i32 addrspace(1)* dereferenceable(8) %dparam, 39 i8 addrspace(1)* dereferenceable(32) align 1 %dparam.align1, 40 i8 addrspace(1)* dereferenceable(32) align 16 %dparam.align16) 41 gc "statepoint-example" { 42; CHECK: The following are dereferenceable: 43entry: 44 call void @mayfree() 45 46; GLOBAL: %dparam{{.*}}(unaligned) 47; POINT-NOT: %dparam{{.*}}(unaligned) 48 %load3 = load i32, i32 addrspace(1)* %dparam 49 50; GLOBAL: %relocate{{.*}}(unaligned) 51; POINT-NOT: %relocate{{.*}}(unaligned) 52 %tok = tail call token (i64, i32, i1 ()*, i32, i32, ...) @llvm.experimental.gc.statepoint.p0f_i1f(i64 0, i32 0, i1 ()* elementtype(i1 ()) @return_i1, i32 0, i32 0, i32 0, i32 0) ["gc-live" (i32 addrspace(1)* %dparam)] 53 %relocate = call i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token %tok, i32 0, i32 0) 54 %load4 = load i32, i32 addrspace(1)* %relocate 55 56; CHECK-NOT: %nparam 57 %dpa = call i32 addrspace(1)* @func1(i32 addrspace(1)* %dparam) 58 %nparam = getelementptr i32, i32 addrspace(1)* %dpa, i32 5 59 %load5 = load i32, i32 addrspace(1)* %nparam 60 61 ; Load from a non-dereferenceable load 62; CHECK-NOT: %nd_load 63 %nd_load = load i32*, i32** @globali32ptr 64 %load6 = load i32, i32* %nd_load 65 66 ; Load from a dereferenceable load 67; GLOBAL: %d4_load{{.*}}(unaligned) 68; POINT-NOT: %d4_load{{.*}}(unaligned) 69 %d4_load = load i32*, i32** @globali32ptr, !dereferenceable !0 70 %load7 = load i32, i32* %d4_load 71 72 ; Load from an offset not covered by the dereferenceable portion 73; CHECK-NOT: %d2_load 74 %d2_load = load i32*, i32** @globali32ptr, !dereferenceable !1 75 %load8 = load i32, i32* %d2_load 76 77 ; Load from a potentially null pointer with dereferenceable_or_null 78; CHECK-NOT: %d_or_null_load 79 %d_or_null_load = load i32*, i32** @globali32ptr, !dereferenceable_or_null !0 80 %load9 = load i32, i32* %d_or_null_load 81 82 ; Load from a non-null pointer with dereferenceable_or_null 83; GLOBAL: %d_or_null_non_null_load{{.*}}(unaligned) 84; POINT-NOT: %d_or_null_non_null_load{{.*}}(unaligned) 85 %d_or_null_non_null_load = load i32*, i32** @globali32ptr, !nonnull !2, !dereferenceable_or_null !0 86 %load10 = load i32, i32* %d_or_null_non_null_load 87 88 ; Loads from aligned arguments 89; GLOBAL: %dparam.align1{{.*}}(unaligned) 90; POINT-NOT: %dparam.align1{{.*}}(unaligned) 91; POINT-NOT: %dparam.align16{{.*}}(aligned) 92; GLOBAL: %dparam.align16{{.*}}(aligned) 93 %load15 = load i8, i8 addrspace(1)* %dparam.align1, align 16 94 %load16 = load i8, i8 addrspace(1)* %dparam.align16, align 16 95 96 ; Loads from GEPs 97; GLOBAL: %gep.align1.offset1{{.*}}(unaligned) 98; GLOBAL: %gep.align16.offset1{{.*}}(unaligned) 99; GLOBAL: %gep.align1.offset16{{.*}}(unaligned) 100; GLOBAL: %gep.align16.offset16{{.*}}(aligned) 101; POINT-NOT: %gep.align1.offset1{{.*}}(unaligned) 102; POINT-NOT: %gep.align16.offset1{{.*}}(unaligned) 103; POINT-NOT: %gep.align1.offset16{{.*}}(unaligned) 104; POINT-NOT: %gep.align16.offset16{{.*}}(aligned) 105 %gep.align1.offset1 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align1, i32 1 106 %gep.align16.offset1 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align16, i32 1 107 %gep.align1.offset16 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align1, i32 16 108 %gep.align16.offset16 = getelementptr inbounds i8, i8 addrspace(1)* %dparam.align16, i32 16 109 %load19 = load i8, i8 addrspace(1)* %gep.align1.offset1, align 16 110 %load20 = load i8, i8 addrspace(1)* %gep.align16.offset1, align 16 111 %load21 = load i8, i8 addrspace(1)* %gep.align1.offset16, align 16 112 %load22 = load i8, i8 addrspace(1)* %gep.align16.offset16, align 16 113 114; CHECK-NOT: %no_deref_return 115; GLOBAL: %deref_return{{.*}}(unaligned) 116; GLOBAL: %deref_and_aligned_return{{.*}}(aligned) 117; POINT-NOT: %deref_return{{.*}}(unaligned) 118; POINT-NOT: %deref_and_aligned_return{{.*}}(aligned) 119 %no_deref_return = call i32* @foo() 120 %deref_return = call dereferenceable(32) i32* @foo() 121 %deref_and_aligned_return = call dereferenceable(32) align 16 i32* @foo() 122 %load23 = load i32, i32* %no_deref_return 123 %load24 = load i32, i32* %deref_return, align 16 124 %load25 = load i32, i32* %deref_and_aligned_return, align 16 125 126 ; Load from a dereferenceable and aligned load 127; GLOBAL: %d4_unaligned_load{{.*}}(unaligned) 128; GLOBAL: %d4_aligned_load{{.*}}(aligned) 129; POINT-NOT: %d4_unaligned_load{{.*}}(unaligned) 130; POINT-NOT: %d4_aligned_load{{.*}}(aligned) 131 %d4_unaligned_load = load i32*, i32** @globali32ptr, !dereferenceable !0 132 %d4_aligned_load = load i32*, i32** @globali32ptr, !dereferenceable !0, !align !{i64 16} 133 %load26 = load i32, i32* %d4_unaligned_load, align 16 134 %load27 = load i32, i32* %d4_aligned_load, align 16 135 ret void 136} 137 138; Loads from aligned allocas 139; CHECK-LABEL: 'alloca_aligned' 140; CHECK: %alloca.align1{{.*}}(unaligned) 141; CHECK: %alloca.align16{{.*}}(aligned) 142define void @alloca_aligned() { 143 %alloca.align1 = alloca i1, align 1 144 %alloca.align16 = alloca i1, align 16 145 call void @mayfree() 146 %load17 = load i1, i1* %alloca.align1, align 16 147 %load18 = load i1, i1* %alloca.align16, align 16 148 ret void 149} 150 151; CHECK-LABEL: 'alloca_basic' 152; CHECK: %alloca{{.*}}(aligned) 153define void @alloca_basic() { 154 %alloca = alloca i1 155 call void @mayfree() 156 %load2 = load i1, i1* %alloca 157 ret void 158} 159 160; Load from empty array alloca 161; CHECK-LABEL: 'alloca_empty' 162; CHECK-NOT: %empty_alloca 163define void @alloca_empty() { 164 %empty_alloca = alloca i8, i64 0 165 call void @mayfree() 166 %empty_load = load i8, i8* %empty_alloca 167 ret void 168} 169 170; Alloca with no explicit alignment is aligned to preferred alignment of 171; the type (specified by datalayout string). 172; CHECK-LABEL: 'alloca_perfalign' 173; CHECK: %alloca.noalign{{.*}}(aligned) 174define void @alloca_perfalign() { 175 %alloca.noalign = alloca i32 176 call void @mayfree() 177 %load28 = load i32, i32* %alloca.noalign, align 8 178 ret void 179} 180 181; CHECK-LABEL: 'global' 182; CHECK: @globalptr.align1{{.*}}(unaligned) 183; CHECK: @globalptr.align16{{.*}}(aligned) 184; CHECK: %globalptr{{.*}}(aligned) 185define void @global() { 186 %load13 = load i8, i8* @globalptr.align1, align 16 187 %load14 = load i8, i8* @globalptr.align16, align 16 188 189 %globalptr = getelementptr inbounds [6 x i8], [6 x i8]* @globalstr, i32 0, i32 0 190 %load1 = load i8, i8* %globalptr 191 ret void 192} 193 194; It's OK to overrun static array size as long as we stay within underlying 195; object size 196; CHECK-LABEL: 'global_allocationsize' 197; CHECK: %within_allocation{{.*}}(aligned) 198; CHECK-NOT: %outside_allocation 199define void @global_allocationsize() { 200 %within_allocation = getelementptr inbounds %struct.A, %struct.A* @globalstruct, i64 0, i32 0, i64 10 201 %load11 = load i8, i8* %within_allocation 202 203 %outside_allocation = getelementptr inbounds %struct.A, %struct.A* @globalstruct, i64 0, i32 1, i64 10 204 %load12 = load i8, i8* %outside_allocation 205 ret void 206} 207 208; Loads from byval arguments 209; CHECK-LABEL: 'byval' 210; CHECK: %i8_byval{{.*}}(aligned) 211; CHECK-NOT: %bad_byval_cast 212; CHECK: %byval_gep{{.*}}(aligned) 213; CHECK: %good_byval_cast{{.*}}(unaligned) 214define void @byval(i8* byval(i8) %i8_byval, 215 %struct.A* byval(%struct.A) %A_byval) { 216 call void @mayfree() 217 load i8, i8* %i8_byval 218 219 %bad_byval_cast = bitcast i8* %i8_byval to i32* 220 load i32, i32* %bad_byval_cast 221 222 %byval_gep = getelementptr inbounds %struct.A, %struct.A* %A_byval, i64 0, i32 1, i64 2 223 load i8, i8* %byval_gep 224 %good_byval_cast = bitcast %struct.A* %A_byval to i32* 225 load i32, i32* %good_byval_cast 226 227 ret void 228} 229 230; CHECK-LABEL: 'f_0' 231; GLOBAL: %ptr = inttoptr i32 %val to i32*, !dereferenceable !0 232; POINT-NOT: %ptr = inttoptr i32 %val to i32*, !dereferenceable !0 233define i32 @f_0(i32 %val) { 234 %ptr = inttoptr i32 %val to i32*, !dereferenceable !0 235 call void @mayfree() 236 %load29 = load i32, i32* %ptr, align 8 237 ret i32 %load29 238} 239 240 241; The most basic case showing the difference between legacy global deref 242; attribute semantics and the new point-in-time semantics. 243; CHECK-LABEL: 'negative' 244; GLOBAL: %p 245; POINT-NOT: %p 246define void @negative(i32* dereferenceable(8) %p) { 247 call void @mayfree() 248 %v = load i32, i32* %p 249 ret void 250} 251 252; CHECK-LABEL: 'infer_func_attrs1' 253; CHECK: %p 254define void @infer_func_attrs1(i32* dereferenceable(8) %p) nofree nosync { 255 call void @mayfree() 256 %v = load i32, i32* %p 257 ret void 258} 259 260; CHECK-LABEL: 'infer_func_attrs2' 261; GLOBAL: %p 262; POINT-NOT: %p 263; FIXME: Can be inferred from attributes 264define void @infer_func_attrs2(i32* dereferenceable(8) %p) readonly { 265 call void @mayfree() 266 %v = load i32, i32* %p 267 ret void 268} 269 270; CHECK-LABEL: 'infer_noalias1' 271; GLOBAL: %p 272; POINT-NOT: %p 273; FIXME: Can be inferred from attributes 274define void @infer_noalias1(i32* dereferenceable(8) noalias nofree %p) { 275 call void @mayfree() 276 %v = load i32, i32* %p 277 ret void 278} 279 280; CHECK-LABEL: 'infer_noalias2' 281; GLOBAL: %p 282; POINT-NOT: %p 283; FIXME: Can be inferred from attributes 284define void @infer_noalias2(i32* dereferenceable(8) noalias readonly %p) nosync { 285 call void @mayfree() 286 %v = load i32, i32* %p 287 ret void 288} 289 290 291; Just check that we don't crash. 292; CHECK-LABEL: 'opaque_type_crasher' 293define void @opaque_type_crasher(%TypeOpaque* dereferenceable(16) %a) { 294entry: 295 %bc = bitcast %TypeOpaque* %a to i8* 296 %ptr8 = getelementptr inbounds i8, i8* %bc, i32 8 297 %ptr32 = bitcast i8* %ptr8 to i32* 298 br i1 undef, label %if.then, label %if.end 299 300if.then: 301 %res = load i32, i32* %ptr32, align 4 302 br label %if.end 303 304if.end: 305 ret void 306} 307 308declare token @llvm.experimental.gc.statepoint.p0f_i1f(i64, i32, i1 ()*, i32, i32, ...) 309declare i32 addrspace(1)* @llvm.experimental.gc.relocate.p1i32(token, i32, i32) 310 311declare i32 addrspace(1)* @func1(i32 addrspace(1)* returned) nounwind argmemonly 312 313; Can free any object accessible in memory 314declare void @mayfree() 315 316!0 = !{i64 4} 317!1 = !{i64 2} 318!2 = !{} 319