xref: /llvm-project/clang/test/CodeGen/delete-null-pointer-checks.c (revision 39db5e1ed87363a9ffea81e53520b542201b3262)
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.
null_check(int * P)6 int null_check(int *P) {
7 // NULL-POINTER-VALID: %[[TOBOOL:.*]] = icmp eq ptr %P, null
8 // NULL-POINTER-INVALID-NOT: icmp eq
9 // NULL-POINTER-VALID: %[[SEL:.*]] = select i1 %[[TOBOOL:.*]], ptr null, ptr
10 // NULL-POINTER-INVALID-NOT: select i1
11 // NULL-POINTER-VALID: load i32, ptr %[[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
20 // NULL-POINTER-VALID: attributes #0 = {{.*}} null_pointer_is_valid
21