190 lines · plain
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 42; RUN: opt -S -passes=dse < %s | FileCheck %s3;4; DSE kills `store i32 44, ptr %struct.byte.4, align 4` but should not kill5; `call void @llvm.memset.p0.i64(...)` because it has a clobber read:6; `%ret = load ptr, ptr %struct.byte.8`7 8 9%struct.type = type { ptr, ptr }10 11define ptr @foo(ptr noundef %ptr) {12; CHECK-LABEL: define ptr @foo(13; CHECK-SAME: ptr noundef [[PTR:%.*]]) {14; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 815; CHECK-NEXT: call void @llvm.lifetime.start.p0(ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6:[0-9]+]]16; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 817; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 418; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false)19; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 420; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 821; CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]22; CHECK-NEXT: ret ptr [[RET]]23;24 %struct.alloca = alloca %struct.type, align 825 call void @llvm.lifetime.start.p0(ptr nonnull %struct.alloca) nounwind26 %struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 827 ; Set %struct.alloca[8, 16) to 42.28 call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false)29 ; Set %struct.alloca[8, 12) to 43.30 store i32 43, ptr %struct.byte.8, align 431 ; Set %struct.alloca[4, 8) to 44.32 %struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 433 store i32 44, ptr %struct.byte.4, align 434 ; Return %struct.alloca[8, 16).35 %ret = load ptr, ptr %struct.byte.836 call void @llvm.lifetime.end.p0(ptr nonnull %struct.alloca) nounwind37 ret ptr %ret38}39 40; Set of tests based on @foo, but where the memset's operands cannot be erased41; due to other uses. Instead, they contain a number of removable MemoryDefs;42; with non-void types result types.43 44define ptr @foo_with_removable_malloc() {45; CHECK-LABEL: define ptr @foo_with_removable_malloc() {46; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 847; CHECK-NEXT: call void @llvm.lifetime.start.p0(ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]48; CHECK-NEXT: [[STRUCT_BYTE_4:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 449; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 850; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 451; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false)52; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 453; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 854; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_4]])55; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_8]])56; CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]57; CHECK-NEXT: ret ptr [[RET]]58;59 %struct.alloca = alloca %struct.type, align 860 call void @llvm.lifetime.start.p0(ptr nonnull %struct.alloca) nounwind61 %struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 462 %struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 863 64 ; Set of removable memory deffs65 %m2 = tail call ptr @malloc(i64 4)66 %m1 = tail call ptr @malloc(i64 4)67 store i32 0, ptr %struct.byte.868 store i32 0, ptr %struct.byte.869 store i32 123, ptr %m170 store i32 123, ptr %m271 72 ; Set %struct.alloca[8, 16) to 42.73 call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false)74 ; Set %struct.alloca[8, 12) to 43.75 store i32 43, ptr %struct.byte.8, align 476 ; Set %struct.alloca[4, 8) to 44.77 store i32 44, ptr %struct.byte.4, align 478 ; Return %struct.alloca[8, 16).79 %ret = load ptr, ptr %struct.byte.880 call void @readnone(ptr %struct.byte.4);81 call void @readnone(ptr %struct.byte.8);82 call void @llvm.lifetime.end.p0(ptr nonnull %struct.alloca) nounwind83 ret ptr %ret84}85 86define ptr @foo_with_removable_malloc_free() {87; CHECK-LABEL: define ptr @foo_with_removable_malloc_free() {88; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 889; CHECK-NEXT: [[M1:%.*]] = tail call ptr @malloc(i64 4)90; CHECK-NEXT: call void @llvm.lifetime.start.p0(ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]91; CHECK-NEXT: [[STRUCT_BYTE_4:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 492; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 893; CHECK-NEXT: [[M2:%.*]] = tail call ptr @malloc(i64 4)94; CHECK-NEXT: call void @free(ptr [[M1]])95; CHECK-NEXT: call void @free(ptr [[M2]])96; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 497; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false)98; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 499; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 8100; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_4]])101; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_8]])102; CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]103; CHECK-NEXT: ret ptr [[RET]]104;105 %struct.alloca = alloca %struct.type, align 8106 %m1 = tail call ptr @malloc(i64 4)107 call void @llvm.lifetime.start.p0(ptr nonnull %struct.alloca) nounwind108 %struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 4109 %struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 8110 111 store i32 0, ptr %struct.byte.4112 store i32 0, ptr %struct.byte.8113 %m2 = tail call ptr @malloc(i64 4)114 store i32 123, ptr %m1115 call void @free(ptr %m1);116 store i32 123, ptr %m2117 call void @free(ptr %m2);118 119 ; Set %struct.alloca[8, 16) to 42.120 call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false)121 ; Set %struct.alloca[8, 12) to 43.122 store i32 43, ptr %struct.byte.8, align 4123 ; Set %struct.alloca[4, 8) to 44.124 store i32 44, ptr %struct.byte.4, align 4125 ; Return %struct.alloca[8, 16).126 %ret = load ptr, ptr %struct.byte.8127 call void @readnone(ptr %struct.byte.4);128 call void @readnone(ptr %struct.byte.8);129 call void @llvm.lifetime.end.p0(ptr nonnull %struct.alloca) nounwind130 ret ptr %ret131}132 133define ptr @foo_with_malloc_to_calloc() {134; CHECK-LABEL: define ptr @foo_with_malloc_to_calloc() {135; CHECK-NEXT: [[STRUCT_ALLOCA:%.*]] = alloca [[STRUCT_TYPE:%.*]], align 8136; CHECK-NEXT: call void @llvm.lifetime.start.p0(ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]137; CHECK-NEXT: [[STRUCT_BYTE_8:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 8138; CHECK-NEXT: [[STRUCT_BYTE_4:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_ALLOCA]], i64 4139; CHECK-NEXT: [[CALLOC1:%.*]] = call ptr @calloc(i64 1, i64 4)140; CHECK-NEXT: [[CALLOC:%.*]] = call ptr @calloc(i64 1, i64 4)141; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i8, ptr [[STRUCT_BYTE_8]], i64 4142; CHECK-NEXT: call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 [[TMP1]], i8 42, i64 4, i1 false)143; CHECK-NEXT: store i32 43, ptr [[STRUCT_BYTE_8]], align 4144; CHECK-NEXT: [[RET:%.*]] = load ptr, ptr [[STRUCT_BYTE_8]], align 8145; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_4]])146; CHECK-NEXT: call void @readnone(ptr [[STRUCT_BYTE_8]])147; CHECK-NEXT: call void @llvm.lifetime.end.p0(ptr nonnull [[STRUCT_ALLOCA]]) #[[ATTR6]]148; CHECK-NEXT: call void @use(ptr [[CALLOC1]])149; CHECK-NEXT: call void @use(ptr [[CALLOC]])150; CHECK-NEXT: ret ptr [[RET]]151;152 %struct.alloca = alloca %struct.type, align 8153 call void @llvm.lifetime.start.p0(ptr nonnull %struct.alloca) nounwind154 %struct.byte.8 = getelementptr inbounds i8, ptr %struct.alloca, i64 8155 %struct.byte.4 = getelementptr inbounds i8, ptr %struct.alloca, i64 4156 157 ; Set of removable memory deffs158 %m1 = tail call ptr @malloc(i64 4)159 %m2 = tail call ptr @malloc(i64 4)160 store i32 0, ptr %struct.byte.4161 store i32 0, ptr %struct.byte.8162 call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %m2, i8 0, i64 4, i1 false)163 call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %m1, i8 0, i64 4, i1 false)164 165 ; Set %struct.alloca[8, 16) to 42.166 call void @llvm.memset.p0.i64(ptr noundef nonnull align 4 %struct.byte.8, i8 42, i64 8, i1 false)167 ; Set %struct.alloca[8, 12) to 43.168 store i32 43, ptr %struct.byte.8, align 4169 ; Set %struct.alloca[4, 8) to 44.170 store i32 44, ptr %struct.byte.4, align 4171 ; Return %struct.alloca[8, 16).172 %ret = load ptr, ptr %struct.byte.8173 call void @readnone(ptr %struct.byte.4);174 call void @readnone(ptr %struct.byte.8);175 call void @llvm.lifetime.end.p0(ptr nonnull %struct.alloca) nounwind176 call void @use(ptr %m1)177 call void @use(ptr %m2)178 ret ptr %ret179}180 181declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg)182declare void @llvm.lifetime.end.p0(ptr nocapture)183declare void @llvm.lifetime.start.p0(ptr nocapture)184 185declare noalias ptr @malloc(i64) willreturn allockind("alloc,uninitialized") "alloc-family"="malloc"186declare void @readnone(ptr) readnone nounwind187declare void @free(ptr nocapture) allockind("free") "alloc-family"="malloc"188 189declare void @use(ptr)190