1; RUN: llc -mtriple=x86_64-apple-macosx -O3 -debug-only=faultmaps -enable-implicit-null-checks < %s 2>&1 | FileCheck %s 2; REQUIRES: asserts 3 4; List cases where we should *not* be emitting implicit null checks. 5 6; CHECK-NOT: Fault Map Output 7 8define i32 @imp_null_check_load(i32* %x, i32* %y) { 9 entry: 10 %c = icmp eq i32* %x, null 11; It isn't legal to move the load from %x from "not_null" to here -- 12; the store to %y could be aliasing it. 13 br i1 %c, label %is_null, label %not_null, !make.implicit !0 14 15 is_null: 16 ret i32 42 17 18 not_null: 19 store i32 0, i32* %y 20 %t = load i32, i32* %x 21 ret i32 %t 22} 23 24define i32 @imp_null_check_gep_load(i32* %x) { 25 entry: 26 %c = icmp eq i32* %x, null 27 br i1 %c, label %is_null, label %not_null, !make.implicit !0 28 29 is_null: 30 ret i32 42 31 32 not_null: 33; null + 5000 * sizeof(i32) lies outside the null page and hence the 34; load to %t cannot be assumed to be reliably faulting. 35 %x.gep = getelementptr i32, i32* %x, i32 5000 36 %t = load i32, i32* %x.gep 37 ret i32 %t 38} 39 40define i32 @imp_null_check_neg_gep_load(i32* %x) { 41 entry: 42 %c = icmp eq i32* %x, null 43 br i1 %c, label %is_null, label %not_null, !make.implicit !0 44 45 is_null: 46 ret i32 42 47 48 not_null: 49; null - 5000 * sizeof(i32) lies outside the null page and hence the 50; load to %t cannot be assumed to be reliably faulting. 51 %x.gep = getelementptr i32, i32* %x, i32 -5000 52 %t = load i32, i32* %x.gep 53 ret i32 %t 54} 55 56define i32 @imp_null_check_load_no_md(i32* %x) { 57; This is fine, except it is missing the !make.implicit metadata. 58 entry: 59 %c = icmp eq i32* %x, null 60 br i1 %c, label %is_null, label %not_null 61 62 is_null: 63 ret i32 42 64 65 not_null: 66 %t = load i32, i32* %x 67 ret i32 %t 68} 69 70define i32 @imp_null_check_no_hoist_over_acquire_load(i32* %x, i32* %y) { 71; We cannot hoist %t1 over %t0 since %t0 is an acquire load 72 entry: 73 %c = icmp eq i32* %x, null 74 br i1 %c, label %is_null, label %not_null, !make.implicit !0 75 76 is_null: 77 ret i32 42 78 79 not_null: 80 %t0 = load atomic i32, i32* %y acquire, align 4 81 %t1 = load i32, i32* %x 82 %p = add i32 %t0, %t1 83 ret i32 %p 84} 85 86define i32 @imp_null_check_add_result(i32* %x, i32* %y) { 87; This will codegen to: 88; 89; movl (%rsi), %eax 90; addl (%rdi), %eax 91; 92; The load instruction we wish to hoist is the addl, but there is a 93; write-after-write hazard preventing that from happening. We could 94; get fancy here and exploit the commutativity of addition, but right 95; now -implicit-null-checks isn't that smart. 96; 97 98 entry: 99 %c = icmp eq i32* %x, null 100 br i1 %c, label %is_null, label %not_null, !make.implicit !0 101 102 is_null: 103 ret i32 42 104 105 not_null: 106 %t0 = load i32, i32* %y 107 %t1 = load i32, i32* %x 108 %p = add i32 %t0, %t1 109 ret i32 %p 110} 111 112; This redefines the null check reg by doing a zero-extend, a shift on 113; itself and then an add. 114; Cannot be converted to implicit check since the zero reg is no longer zero. 115define i64 @imp_null_check_load_shift_add_addr(i64* %x, i64 %r) { 116 entry: 117 %c = icmp eq i64* %x, null 118 br i1 %c, label %is_null, label %not_null, !make.implicit !0 119 120 is_null: 121 ret i64 42 122 123 not_null: 124 %y = ptrtoint i64* %x to i64 125 %shry = shl i64 %y, 6 126 %shry.add = add i64 %shry, %r 127 %y.ptr = inttoptr i64 %shry.add to i64* 128 %x.loc = getelementptr i64, i64* %y.ptr, i64 1 129 %t = load i64, i64* %x.loc 130 ret i64 %t 131} 132 133; the memory op is not within faulting page. 134define i64 @imp_null_check_load_addr_outside_faulting_page(i64* %x) { 135 entry: 136 %c = icmp eq i64* %x, null 137 br i1 %c, label %is_null, label %not_null, !make.implicit !0 138 139 is_null: 140 ret i64 42 141 142 not_null: 143 %y = ptrtoint i64* %x to i64 144 %shry = shl i64 %y, 3 145 %shry.add = add i64 %shry, 68719472640 146 %y.ptr = inttoptr i64 %shry.add to i64* 147 %x.loc = getelementptr i64, i64* %y.ptr, i64 1 148 %t = load i64, i64* %x.loc 149 ret i64 %t 150} 151 152!0 = !{} 153