1 ; RUN: opt -instcombine -S < %s | FileCheck %s
3 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6 declare void @bar(i8 addrspace(1)*)
8 define void @nonnullAfterBitCast() {
10 %i = alloca i32, align 4
11 %tmp1 = bitcast i32* %i to i8*
12 ; CHECK: call void @foo(i8* nonnull %tmp1)
13 call void @foo(i8* %tmp1)
17 define void @nonnullAfterSExt(i8 %a) {
19 %b = zext i8 %a to i32 ; <- %b is >= 0
20 %c = add nsw nuw i32 %b, 2 ; <- %c is > 0
21 %sext = sext i32 %c to i64 ; <- %sext cannot be 0 because %c is not 0
22 %i2p = inttoptr i64 %sext to i8* ; <- no-op int2ptr cast
23 ; CHECK: call void @foo(i8* nonnull %i2p)
24 call void @foo(i8* %i2p)
28 define void @nonnullAfterZExt(i8 %a) {
30 %b = zext i8 %a to i32 ; <- %b is >= 0
31 %c = add nsw nuw i32 %b, 2 ; <- %c is > 0
32 %zext = zext i32 %c to i64 ; <- %zext cannot be 0 because %c is not 0
33 %i2p = inttoptr i64 %zext to i8* ; <- no-op int2ptr cast
34 ; CHECK: call void @foo(i8* nonnull %i2p)
35 call void @foo(i8* %i2p)
39 declare void @llvm.assume(i1 %b)
41 define void @nonnullAfterInt2Ptr(i32 %u, i64 %lu) {
43 %nz = sdiv exact i32 100, %u ; %nz cannot be null
44 %i2p = inttoptr i32 %nz to i8* ; extending int2ptr as sizeof(i32) < sizeof(i8*)
45 ; CHECK: call void @foo(i8* nonnull %i2p)
46 call void @foo(i8* %i2p)
48 %nz.2 = sdiv exact i64 100, %lu ; %nz.2 cannot be null
49 %i2p.2 = inttoptr i64 %nz.2 to i8* ; no-op int2ptr as sizeof(i64) == sizeof(i8*)
50 ; CHECK: call void @foo(i8* nonnull %i2p.2)
51 call void @foo(i8* %i2p.2)
55 define void @nonnullAfterPtr2Int() {
58 %p2i = ptrtoint i32* %a to i64 ; no-op ptr2int as sizeof(i32*) == sizeof(i64)
59 %i2p = inttoptr i64 %p2i to i8*
60 ; CHECK: call void @foo(i8* nonnull %i2p)
61 call void @foo(i8* %i2p)
65 define void @maybenullAfterInt2Ptr(i128 %llu) {
67 %cmp = icmp ne i128 %llu, 0
68 call void @llvm.assume(i1 %cmp) ; %llu != 0
69 %i2p = inttoptr i128 %llu to i8* ; truncating int2ptr as sizeof(i128) > sizeof(i8*)
70 ; CHECK: call void @foo(i8* %i2p)
71 call void @foo(i8* %i2p)
75 define void @maybenullAfterPtr2Int() {
78 %p2i = ptrtoint i32* %a to i32 ; truncating ptr2int as sizeof(i32*) > sizeof(i32)
79 %i2p = inttoptr i32 %p2i to i8*
80 ; CHECK: call void @foo(i8* %i2p)
81 call void @foo(i8* %i2p)
85 define void @maybenullAfterAddrspacecast(i8* nonnull %p) {
87 %addrspcast = addrspacecast i8* %p to i8 addrspace(1)*
89 ; An address space cast can be "a no-op cast or a complex value modification,
90 ; depending on the target and the address space pair". As a consequence, we
91 ; cannot simply assume non-nullness of %p is preserved by the cast.
93 ; CHECK: call void @bar(i8 addrspace(1)* %addrspcast)
94 call void @bar(i8 addrspace(1)* %addrspcast)
96 ; CHECK: call void @foo(i8* nonnull %p)
97 call void @foo(i8* %p)