1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=mem2reg -S | FileCheck %s
; This tests that mem2reg preserves the !nonnull metadata on loads
; from allocas that get optimized out.
; Check the case where the alloca in question has a single store.
define ptr @single_store_noundef(ptr %arg) {
; CHECK-LABEL: @single_store_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 8
; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr [[ARG_LOAD]], null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
; CHECK-NEXT: ret ptr [[ARG_LOAD]]
;
entry:
%buf = alloca ptr
%arg.load = load ptr, ptr %arg, align 8
store ptr %arg.load, ptr %buf, align 8
%buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !0
ret ptr %buf.load
}
define ptr @single_store_missing_noundef(ptr %arg) {
; CHECK-LABEL: @single_store_missing_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 8
; CHECK-NEXT: ret ptr [[ARG_LOAD]]
;
entry:
%buf = alloca ptr
%arg.load = load ptr, ptr %arg, align 8
store ptr %arg.load, ptr %buf, align 8
%buf.load = load ptr, ptr %buf, !nonnull !0
ret ptr %buf.load
}
; Check the case where the alloca in question has more than one
; store but still within one basic block.
define ptr @single_block_noundef(ptr %arg) {
; CHECK-LABEL: @single_block_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 8
; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr [[ARG_LOAD]], null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
; CHECK-NEXT: ret ptr [[ARG_LOAD]]
;
entry:
%buf = alloca ptr
%arg.load = load ptr, ptr %arg, align 8
store ptr null, ptr %buf, align 8
store ptr %arg.load, ptr %buf, align 8
%buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !0
ret ptr %buf.load
}
define ptr @single_block_missing_noundef(ptr %arg) {
; CHECK-LABEL: @single_block_missing_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 8
; CHECK-NEXT: ret ptr [[ARG_LOAD]]
;
entry:
%buf = alloca ptr
%arg.load = load ptr, ptr %arg, align 8
store ptr null, ptr %buf, align 8
store ptr %arg.load, ptr %buf, align 8
%buf.load = load ptr, ptr %buf, !nonnull !0
ret ptr %buf.load
}
; Check the case where the alloca in question has more than one
; store and also reads ands writes in multiple blocks.
define ptr @multi_block_noundef(ptr %arg) {
; CHECK-LABEL: @multi_block_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 8
; CHECK-NEXT: br label [[NEXT:%.*]]
; CHECK: next:
; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr [[ARG_LOAD]], null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
; CHECK-NEXT: ret ptr [[ARG_LOAD]]
;
entry:
%buf = alloca ptr
%arg.load = load ptr, ptr %arg, align 8
store ptr null, ptr %buf, align 8
br label %next
next:
store ptr %arg.load, ptr %buf, align 8
%buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !0
ret ptr %buf.load
}
define ptr @multi_block_missing_noundef(ptr %arg) {
; CHECK-LABEL: @multi_block_missing_noundef(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 8
; CHECK-NEXT: br label [[NEXT:%.*]]
; CHECK: next:
; CHECK-NEXT: ret ptr [[ARG_LOAD]]
;
entry:
%buf = alloca ptr
%arg.load = load ptr, ptr %arg, align 8
store ptr null, ptr %buf, align 8
br label %next
next:
store ptr %arg.load, ptr %buf, align 8
%buf.load = load ptr, ptr %buf, !nonnull !0
ret ptr %buf.load
}
; Check that we don't add an assume if it's not
; necessary i.e. the value is already implied to be nonnull
define ptr @no_assume(ptr %arg) {
; CHECK-LABEL: @no_assume(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 8
; CHECK-NEXT: [[CN:%.*]] = icmp ne ptr [[ARG_LOAD]], null
; CHECK-NEXT: br i1 [[CN]], label [[NEXT:%.*]], label [[FIN:%.*]]
; CHECK: next:
; CHECK-NEXT: ret ptr [[ARG_LOAD]]
; CHECK: fin:
; CHECK-NEXT: ret ptr null
;
entry:
%buf = alloca ptr
%arg.load = load ptr, ptr %arg, align 8
%cn = icmp ne ptr %arg.load, null
br i1 %cn, label %next, label %fin
next:
; At this point the above nonnull check ensures that
; the value %arg.load is nonnull in this block and thus
; we need not add the assume.
store ptr %arg.load, ptr %buf, align 8
%buf.load = load ptr, ptr %buf, !nonnull !0
ret ptr %buf.load
fin:
ret ptr null
}
define ptr @no_store_single_load() {
; CHECK-LABEL: @no_store_single_load(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr undef, null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
; CHECK-NEXT: ret ptr undef
;
entry:
%buf = alloca ptr
%buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !0
ret ptr %buf.load
}
define ptr @no_store_multiple_loads(i1 %c) {
; CHECK-LABEL: @no_store_multiple_loads(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
; CHECK: if:
; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr undef, null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])
; CHECK-NEXT: ret ptr undef
; CHECK: else:
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne ptr undef, null
; CHECK-NEXT: call void @llvm.assume(i1 [[TMP1]])
; CHECK-NEXT: ret ptr undef
;
entry:
%buf = alloca ptr
br i1 %c, label %if, label %else
if:
%buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !0
ret ptr %buf.load
else:
%buf.load2 = load ptr, ptr %buf, !nonnull !0, !noundef !0
ret ptr %buf.load2
}
!0 = !{}
|