xref: /llvm-project/clang/test/CodeGen/delete-null-pointer-checks.c (revision da08f6ac16b6a53ab25ecb56e1be800c5f0095d9)
1 // RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux-gnu -O2 -o - %s | FileCheck -check-prefix=NULL-POINTER-INVALID  %s
2 // RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux-gnu -O2 -o - %s -fno-delete-null-pointer-checks | FileCheck -check-prefix=NULL-POINTER-VALID  %s
3 
4 // Test that clang does not remove the null pointer check with
5 // -fno-delete-null-pointer-checks.
6 int null_check(int *P) {
7 // NULL-POINTER-VALID: %[[TOBOOL:.*]] = icmp eq i32* %P, null
8 // NULL-POINTER-INVALID-NOT: icmp eq
9 // NULL-POINTER-VALID: %[[SEL:.*]] = select i1 %[[TOBOOL:.*]], i32* null, i32*
10 // NULL-POINTER-INVALID-NOT: select i1
11 // NULL-POINTER-VALID: load i32, i32* %[[SEL:.*]]
12   int *Q = P;
13   if (P) {
14     Q = P + 2;
15   }
16   return *Q;
17 }
18 
19 // NULL-POINTER-INVALID-NOT: attributes #0 = {{.*}} "null-pointer-is-valid"="true"
20 // NULL-POINTER-VALID: attributes #0 = {{.*}} "null-pointer-is-valid"="true"
21