216 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py2; RUN: opt < %s -passes=mem2reg -S | FileCheck %s3 4; This tests that mem2reg preserves the !nonnull metadata on loads5; from allocas that get optimized out.6 7; Check the case where the alloca in question has a single store.8define ptr @single_store_noundef(ptr %arg) {9; CHECK-LABEL: @single_store_noundef(10; CHECK-NEXT: entry:11; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 812; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr [[ARG_LOAD]], null13; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])14; CHECK-NEXT: ret ptr [[ARG_LOAD]]15;16entry:17 %buf = alloca ptr18 %arg.load = load ptr, ptr %arg, align 819 store ptr %arg.load, ptr %buf, align 820 %buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !021 ret ptr %buf.load22}23 24define ptr @single_store_missing_noundef(ptr %arg) {25; CHECK-LABEL: @single_store_missing_noundef(26; CHECK-NEXT: entry:27; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 828; CHECK-NEXT: ret ptr [[ARG_LOAD]]29;30entry:31 %buf = alloca ptr32 %arg.load = load ptr, ptr %arg, align 833 store ptr %arg.load, ptr %buf, align 834 %buf.load = load ptr, ptr %buf, !nonnull !035 ret ptr %buf.load36}37 38; Check the case where the alloca in question has more than one39; store but still within one basic block.40define ptr @single_block_noundef(ptr %arg) {41; CHECK-LABEL: @single_block_noundef(42; CHECK-NEXT: entry:43; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 844; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr [[ARG_LOAD]], null45; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])46; CHECK-NEXT: ret ptr [[ARG_LOAD]]47;48entry:49 %buf = alloca ptr50 %arg.load = load ptr, ptr %arg, align 851 store ptr null, ptr %buf, align 852 store ptr %arg.load, ptr %buf, align 853 %buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !054 ret ptr %buf.load55}56 57define ptr @single_block_missing_noundef(ptr %arg) {58; CHECK-LABEL: @single_block_missing_noundef(59; CHECK-NEXT: entry:60; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 861; CHECK-NEXT: ret ptr [[ARG_LOAD]]62;63entry:64 %buf = alloca ptr65 %arg.load = load ptr, ptr %arg, align 866 store ptr null, ptr %buf, align 867 store ptr %arg.load, ptr %buf, align 868 %buf.load = load ptr, ptr %buf, !nonnull !069 ret ptr %buf.load70}71 72; Check the case where the alloca in question has more than one73; store and also reads ands writes in multiple blocks.74define ptr @multi_block_noundef(ptr %arg) {75; CHECK-LABEL: @multi_block_noundef(76; CHECK-NEXT: entry:77; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 878; CHECK-NEXT: br label [[NEXT:%.*]]79; CHECK: next:80; CHECK-NEXT: [[TMP0:%.*]] = icmp ne ptr [[ARG_LOAD]], null81; CHECK-NEXT: call void @llvm.assume(i1 [[TMP0]])82; CHECK-NEXT: ret ptr [[ARG_LOAD]]83;84entry:85 %buf = alloca ptr86 %arg.load = load ptr, ptr %arg, align 887 store ptr null, ptr %buf, align 888 br label %next89next:90 store ptr %arg.load, ptr %buf, align 891 %buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !092 ret ptr %buf.load93}94 95define ptr @multi_block_missing_noundef(ptr %arg) {96; CHECK-LABEL: @multi_block_missing_noundef(97; CHECK-NEXT: entry:98; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 899; CHECK-NEXT: br label [[NEXT:%.*]]100; CHECK: next:101; CHECK-NEXT: ret ptr [[ARG_LOAD]]102;103entry:104 %buf = alloca ptr105 %arg.load = load ptr, ptr %arg, align 8106 store ptr null, ptr %buf, align 8107 br label %next108next:109 store ptr %arg.load, ptr %buf, align 8110 %buf.load = load ptr, ptr %buf, !nonnull !0111 ret ptr %buf.load112}113 114; Check that we don't add an assume if it's not115; necessary i.e. the value is already implied to be nonnull116define ptr @no_assume(ptr %arg) {117; CHECK-LABEL: @no_assume(118; CHECK-NEXT: entry:119; CHECK-NEXT: [[ARG_LOAD:%.*]] = load ptr, ptr [[ARG:%.*]], align 8120; CHECK-NEXT: [[CN:%.*]] = icmp ne ptr [[ARG_LOAD]], null121; CHECK-NEXT: br i1 [[CN]], label [[NEXT:%.*]], label [[FIN:%.*]]122; CHECK: next:123; CHECK-NEXT: ret ptr [[ARG_LOAD]]124; CHECK: fin:125; CHECK-NEXT: ret ptr null126;127entry:128 %buf = alloca ptr129 %arg.load = load ptr, ptr %arg, align 8130 %cn = icmp ne ptr %arg.load, null131 br i1 %cn, label %next, label %fin132next:133; At this point the above nonnull check ensures that134; the value %arg.load is nonnull in this block and thus135; we need not add the assume.136 store ptr %arg.load, ptr %buf, align 8137 %buf.load = load ptr, ptr %buf, !nonnull !0138 ret ptr %buf.load139fin:140 ret ptr null141}142 143define ptr @no_store_single_load_noundef() {144; CHECK-LABEL: @no_store_single_load_noundef(145; CHECK-NEXT: entry:146; CHECK-NEXT: store i1 true, ptr poison, align 1147; CHECK-NEXT: ret ptr undef148;149entry:150 %buf = alloca ptr151 %buf.load = load ptr, ptr %buf, !noundef !0152 ret ptr %buf.load153}154 155define ptr @no_store_multiple_loads_noundef(i1 %c) {156; CHECK-LABEL: @no_store_multiple_loads_noundef(157; CHECK-NEXT: entry:158; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]159; CHECK: if:160; CHECK-NEXT: store i1 true, ptr poison, align 1161; CHECK-NEXT: ret ptr undef162; CHECK: else:163; CHECK-NEXT: store i1 true, ptr poison, align 1164; CHECK-NEXT: ret ptr undef165;166entry:167 %buf = alloca ptr168 br i1 %c, label %if, label %else169 170if:171 %buf.load = load ptr, ptr %buf, !noundef !0172 ret ptr %buf.load173 174 else:175 %buf.load2 = load ptr, ptr %buf, !noundef !0176 ret ptr %buf.load2177}178 179define ptr @no_store_single_load_nonnull_noundef() {180; CHECK-LABEL: @no_store_single_load_nonnull_noundef(181; CHECK-NEXT: entry:182; CHECK-NEXT: store i1 true, ptr poison, align 1183; CHECK-NEXT: ret ptr undef184;185entry:186 %buf = alloca ptr187 %buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !0188 ret ptr %buf.load189}190 191define ptr @no_store_multiple_loads_nonnull_noundef(i1 %c) {192; CHECK-LABEL: @no_store_multiple_loads_nonnull_noundef(193; CHECK-NEXT: entry:194; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]195; CHECK: if:196; CHECK-NEXT: store i1 true, ptr poison, align 1197; CHECK-NEXT: ret ptr undef198; CHECK: else:199; CHECK-NEXT: store i1 true, ptr poison, align 1200; CHECK-NEXT: ret ptr undef201;202entry:203 %buf = alloca ptr204 br i1 %c, label %if, label %else205 206if:207 %buf.load = load ptr, ptr %buf, !nonnull !0, !noundef !0208 ret ptr %buf.load209 210 else:211 %buf.load2 = load ptr, ptr %buf, !nonnull !0, !noundef !0212 ret ptr %buf.load2213}214 215!0 = !{}216