Skip to content

Commit 04b717c

Browse files
committed
[TLI] Check that malloc argument has type size_t
DSE assumes that this is the case when forming a calloc from a malloc + memset pair. For tests, either update the malloc signature or change the data layout.
1 parent de5022c commit 04b717c

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,9 +1110,11 @@ bool TargetLibraryInfoImpl::isValidProtoForLibFunc(const FunctionType &FTy,
11101110
case LibFunc_system:
11111111
return (NumParams == 1 && FTy.getParamType(0)->isPointerTy());
11121112
case LibFunc___kmpc_alloc_shared:
1113+
return NumParams == 1 && FTy.getReturnType()->isPointerTy();
11131114
case LibFunc_malloc:
11141115
case LibFunc_vec_malloc:
1115-
return (NumParams == 1 && FTy.getReturnType()->isPointerTy());
1116+
return NumParams == 1 && FTy.getParamType(0)->isIntegerTy(SizeTBits) &&
1117+
FTy.getReturnType()->isPointerTy();
11161118
case LibFunc_memcmp:
11171119
return NumParams == 3 && FTy.getReturnType()->isIntegerTy(32) &&
11181120
FTy.getParamType(0)->isPointerTy() &&

llvm/test/Analysis/GlobalsModRef/indirect-global.ll

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
; Note that this test relies on an unsafe feature of GlobalsModRef. While this
66
; test is correct and safe, GMR's technique for handling this isn't generally.
77

8+
target datalayout = "p:32:32:32"
9+
810
@G = internal global i32* null ; <i32**> [#uses=3]
911

1012
declare noalias i8* @malloc(i32)
1113
define void @malloc_init() {
1214
; CHECK-LABEL: @malloc_init(
1315
; CHECK-NEXT: [[A:%.*]] = call dereferenceable_or_null(4) i8* @malloc(i32 4)
14-
; CHECK-NEXT: store i8* [[A]], i8** bitcast (i32** @G to i8**), align 8
16+
; CHECK-NEXT: store i8* [[A]], i8** bitcast (i32** @G to i8**), align 4
1517
; CHECK-NEXT: ret void
1618
;
1719
%a = call i8* @malloc(i32 4)
@@ -40,7 +42,7 @@ declare noalias i8* @calloc(i32, i32)
4042
define void @calloc_init() {
4143
; CHECK-LABEL: @calloc_init(
4244
; CHECK-NEXT: [[A:%.*]] = call dereferenceable_or_null(4) i8* @calloc(i32 4, i32 1)
43-
; CHECK-NEXT: store i8* [[A]], i8** bitcast (i32** @G2 to i8**), align 8
45+
; CHECK-NEXT: store i8* [[A]], i8** bitcast (i32** @G2 to i8**), align 4
4446
; CHECK-NEXT: ret void
4547
;
4648
%a = call i8* @calloc(i32 4, i32 1)
@@ -69,7 +71,7 @@ declare noalias i8* @my_alloc(i32)
6971
define void @my_alloc_init() {
7072
; CHECK-LABEL: @my_alloc_init(
7173
; CHECK-NEXT: [[A:%.*]] = call i8* @my_alloc(i32 4)
72-
; CHECK-NEXT: store i8* [[A]], i8** bitcast (i32** @G3 to i8**), align 8
74+
; CHECK-NEXT: store i8* [[A]], i8** bitcast (i32** @G3 to i8**), align 4
7375
; CHECK-NEXT: ret void
7476
;
7577
%a = call i8* @my_alloc(i32 4)

llvm/test/Transforms/DeadStoreElimination/simple.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,13 @@ define void @test_matrix_store(i64 %stride) {
209209
declare void @may_unwind()
210210
define i32* @test_malloc_no_escape_before_return() {
211211
; CHECK-LABEL: @test_malloc_no_escape_before_return(
212-
; CHECK-NEXT: [[PTR:%.*]] = tail call i8* @malloc(i32 4)
212+
; CHECK-NEXT: [[PTR:%.*]] = tail call i8* @malloc(i64 4)
213213
; CHECK-NEXT: [[P:%.*]] = bitcast i8* [[PTR]] to i32*
214214
; CHECK-NEXT: call void @may_unwind()
215215
; CHECK-NEXT: store i32 0, i32* [[P]], align 4
216216
; CHECK-NEXT: ret i32* [[P]]
217217
;
218-
%ptr = tail call i8* @malloc(i32 4)
218+
%ptr = tail call i8* @malloc(i64 4)
219219
%P = bitcast i8* %ptr to i32*
220220
%DEAD = load i32, i32* %P
221221
%DEAD2 = add i32 %DEAD, 1
@@ -245,14 +245,14 @@ define i32* @test_custom_malloc_no_escape_before_return() {
245245

246246
define i32 addrspace(1)* @test13_addrspacecast() {
247247
; CHECK-LABEL: @test13_addrspacecast(
248-
; CHECK-NEXT: [[P:%.*]] = tail call i8* @malloc(i32 4)
248+
; CHECK-NEXT: [[P:%.*]] = tail call i8* @malloc(i64 4)
249249
; CHECK-NEXT: [[P_BC:%.*]] = bitcast i8* [[P]] to i32*
250250
; CHECK-NEXT: [[P:%.*]] = addrspacecast i32* [[P_BC]] to i32 addrspace(1)*
251251
; CHECK-NEXT: call void @may_unwind()
252252
; CHECK-NEXT: store i32 0, i32 addrspace(1)* [[P]], align 4
253253
; CHECK-NEXT: ret i32 addrspace(1)* [[P]]
254254
;
255-
%p = tail call i8* @malloc(i32 4)
255+
%p = tail call i8* @malloc(i64 4)
256256
%p.bc = bitcast i8* %p to i32*
257257
%P = addrspacecast i32* %p.bc to i32 addrspace(1)*
258258
%DEAD = load i32, i32 addrspace(1)* %P
@@ -264,7 +264,7 @@ define i32 addrspace(1)* @test13_addrspacecast() {
264264
}
265265

266266

267-
declare noalias i8* @malloc(i32) willreturn
267+
declare noalias i8* @malloc(i64) willreturn
268268
declare noalias i8* @custom_malloc(i32) willreturn
269269
declare noalias i8* @calloc(i32, i32) willreturn
270270

@@ -302,7 +302,7 @@ define void @malloc_no_escape() {
302302
; CHECK-LABEL: @malloc_no_escape(
303303
; CHECK-NEXT: ret void
304304
;
305-
%m = call i8* @malloc(i32 24)
305+
%m = call i8* @malloc(i64 24)
306306
store i8 0, i8* %m
307307
ret void
308308
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -dse < %s | FileCheck %s
3+
4+
; malloc should have i64 argument under default data layout
5+
declare noalias i8* @malloc(i32)
6+
7+
define i8* @malloc_and_memset_intrinsic(i32 %n) {
8+
; CHECK-LABEL: @malloc_and_memset_intrinsic(
9+
; CHECK-NEXT: [[CALL:%.*]] = call i8* @malloc(i32 [[N:%.*]])
10+
; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* align 1 [[CALL]], i8 0, i32 [[N]], i1 false)
11+
; CHECK-NEXT: ret i8* [[CALL]]
12+
;
13+
%call = call i8* @malloc(i32 %n)
14+
call void @llvm.memset.p0i8.i32(i8* align 1 %call, i8 0, i32 %n, i1 false)
15+
ret i8* %call
16+
}
17+
18+
declare void @llvm.memset.p0i8.i32(i8* nocapture writeonly, i8, i32, i1 immarg) #2

llvm/test/Transforms/InstCombine/malloc-free.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
33
; PR1201
4+
5+
target datalayout = "p:32:32:32"
6+
47
define i32 @main(i32 %argc, i8** %argv) {
58
; CHECK-LABEL: @main(
69
; CHECK-NEXT: ret i32 0
@@ -85,7 +88,7 @@ define void @test5(i8* %ptr, i8** %esc) {
8588
; CHECK-NEXT: [[G:%.*]] = call dereferenceable_or_null(700) i8* @malloc(i32 700)
8689
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(32) [[PTR:%.*]], i8* noundef nonnull align 1 dereferenceable(32) [[A]], i32 32, i1 false)
8790
; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i32(i8* noundef nonnull align 1 dereferenceable(32) [[PTR]], i8* noundef nonnull align 1 dereferenceable(32) [[B]], i32 32, i1 false)
88-
; CHECK-NEXT: store i8* [[C]], i8** [[ESC:%.*]], align 8
91+
; CHECK-NEXT: store i8* [[C]], i8** [[ESC:%.*]], align 4
8992
; CHECK-NEXT: call void @llvm.memcpy.p0i8.p0i8.i32(i8* [[D]], i8* [[PTR]], i32 32, i1 true)
9093
; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i32(i8* [[E]], i8* [[PTR]], i32 32, i1 true)
9194
; CHECK-NEXT: call void @llvm.memset.p0i8.i32(i8* [[F]], i8 5, i32 32, i1 true)

llvm/test/Transforms/InstCombine/objsize-64.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
33
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
44

5-
declare noalias i8* @malloc(i32) nounwind
5+
declare noalias i8* @malloc(i64) nounwind
66
declare noalias nonnull i8* @_Znwm(i64) ; new(unsigned long)
77
declare i32 @__gxx_personality_v0(...)
88
declare void @__cxa_call_unexpected(i8*)
99
declare i64 @llvm.objectsize.i64(i8*, i1) nounwind readonly
1010

1111
define i64 @f1(i8 **%esc) {
1212
; CHECK-LABEL: @f1(
13-
; CHECK-NEXT: [[CALL:%.*]] = call dereferenceable_or_null(4) i8* @malloc(i32 4)
13+
; CHECK-NEXT: [[CALL:%.*]] = call dereferenceable_or_null(4) i8* @malloc(i64 4)
1414
; CHECK-NEXT: store i8* [[CALL]], i8** [[ESC:%.*]], align 8
1515
; CHECK-NEXT: ret i64 4
1616
;
17-
%call = call i8* @malloc(i32 4)
17+
%call = call i8* @malloc(i64 4)
1818
store i8* %call, i8** %esc
1919
%size = call i64 @llvm.objectsize.i64(i8* %call, i1 false)
2020
ret i64 %size

llvm/test/Transforms/MetaRenamer/metarenamer.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,17 @@ define i32 @varargs_func_6_xxx(i32 %arg_1_xxx, i32 %arg_2_xxx, ...) nounwind uwt
9797
ret i32 6
9898
}
9999

100-
declare noalias i8* @malloc(i32)
100+
declare noalias i8* @malloc(i64)
101101
declare void @free(i8* nocapture)
102102

103103
define void @dont_rename_lib_funcs() {
104104
; CHECK-LABEL: @foo(
105105
; CHECK-NEXT: bb:
106-
; CHECK-NEXT: [[TMP:%.*]] = call i8* @malloc(i32 23)
106+
; CHECK-NEXT: [[TMP:%.*]] = call i8* @malloc(i64 23)
107107
; CHECK-NEXT: call void @free(i8* [[TMP]])
108108
; CHECK-NEXT: ret void
109109
;
110-
%x = call i8* @malloc(i32 23)
110+
%x = call i8* @malloc(i64 23)
111111
call void @free(i8* %x)
112112
ret void
113113
}

0 commit comments

Comments
 (0)