[SimplifyCFG] FoldTwoEntryPHINode(): consider *total* speculation cost, not per-BB...
[llvm-complete.git] / test / Transforms / InstCombine / deref-alloc-fns.ll
blobf188510dfdf025ef4f7b90a8976d16d807297537
1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -instcombine  -S < %s | FileCheck %s
4 declare noalias i8* @malloc(i64)
5 declare noalias i8* @calloc(i64, i64)
6 declare noalias i8* @realloc(i8* nocapture, i64)
7 declare noalias nonnull i8* @_Znam(i64) ; throwing version of 'new'
8 declare noalias nonnull i8* @_Znwm(i64) ; throwing version of 'new'
10 define noalias i8* @malloc_nonconstant_size(i64 %n) {
11 ; CHECK-LABEL: @malloc_nonconstant_size(
12 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @malloc(i64 [[N:%.*]])
13 ; CHECK-NEXT:    ret i8* [[CALL]]
15   %call = tail call noalias i8* @malloc(i64 %n)
16   ret i8* %call
19 define noalias i8* @malloc_constant_size() {
20 ; CHECK-LABEL: @malloc_constant_size(
21 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias dereferenceable_or_null(40) i8* @malloc(i64 40)
22 ; CHECK-NEXT:    ret i8* [[CALL]]
24   %call = tail call noalias i8* @malloc(i64 40)
25   ret i8* %call
28 define noalias i8* @malloc_constant_size2() {
29 ; CHECK-LABEL: @malloc_constant_size2(
30 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias dereferenceable_or_null(80) i8* @malloc(i64 40)
31 ; CHECK-NEXT:    ret i8* [[CALL]]
33   %call = tail call noalias dereferenceable_or_null(80) i8* @malloc(i64 40)
34   ret i8* %call
37 define noalias i8* @malloc_constant_size3() {
38 ; CHECK-LABEL: @malloc_constant_size3(
39 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias dereferenceable(80) dereferenceable_or_null(40) i8* @malloc(i64 40)
40 ; CHECK-NEXT:    ret i8* [[CALL]]
42   %call = tail call noalias dereferenceable(80) i8* @malloc(i64 40)
43   ret i8* %call
47 define noalias i8* @malloc_constant_zero_size() {
48 ; CHECK-LABEL: @malloc_constant_zero_size(
49 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @malloc(i64 0)
50 ; CHECK-NEXT:    ret i8* [[CALL]]
52   %call = tail call noalias i8* @malloc(i64 0)
53   ret i8* %call
56 define noalias i8* @realloc_nonconstant_size(i8* %p, i64 %n) {
57 ; CHECK-LABEL: @realloc_nonconstant_size(
58 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @realloc(i8* [[P:%.*]], i64 [[N:%.*]])
59 ; CHECK-NEXT:    ret i8* [[CALL]]
61   %call = tail call noalias i8* @realloc(i8* %p, i64 %n)
62   ret i8* %call
65 define noalias i8* @realloc_constant_zero_size(i8* %p) {
66 ; CHECK-LABEL: @realloc_constant_zero_size(
67 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @realloc(i8* [[P:%.*]], i64 0)
68 ; CHECK-NEXT:    ret i8* [[CALL]]
70   %call = tail call noalias i8* @realloc(i8* %p, i64 0)
71   ret i8* %call
74 define noalias i8* @realloc_constant_size(i8* %p) {
75 ; CHECK-LABEL: @realloc_constant_size(
76 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias dereferenceable_or_null(40) i8* @realloc(i8* [[P:%.*]], i64 40)
77 ; CHECK-NEXT:    ret i8* [[CALL]]
79   %call = tail call noalias i8* @realloc(i8* %p, i64 40)
80   ret i8* %call
83 define noalias i8* @calloc_nonconstant_size(i64 %n) {
84 ; CHECK-LABEL: @calloc_nonconstant_size(
85 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 1, i64 [[N:%.*]])
86 ; CHECK-NEXT:    ret i8* [[CALL]]
88   %call = tail call noalias i8* @calloc(i64 1, i64 %n)
89   ret i8* %call
92 define noalias i8* @calloc_nonconstant_size2(i64 %n) {
93 ; CHECK-LABEL: @calloc_nonconstant_size2(
94 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 [[N:%.*]], i64 0)
95 ; CHECK-NEXT:    ret i8* [[CALL]]
97   %call = tail call noalias i8* @calloc(i64 %n, i64 0)
98   ret i8* %call
101 define noalias i8* @calloc_nonconstant_size3(i64 %n) {
102 ; CHECK-LABEL: @calloc_nonconstant_size3(
103 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 [[N:%.*]], i64 [[N]])
104 ; CHECK-NEXT:    ret i8* [[CALL]]
106   %call = tail call noalias i8* @calloc(i64 %n, i64 %n)
107   ret i8* %call
110 define noalias i8* @calloc_constant_zero_size() {
111 ; CHECK-LABEL: @calloc_constant_zero_size(
112 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 0, i64 0)
113 ; CHECK-NEXT:    ret i8* [[CALL]]
115   %call = tail call noalias i8* @calloc(i64 0, i64 0)
116   ret i8* %call
119 define noalias i8* @calloc_constant_zero_size2(i64 %n) {
120 ; CHECK-LABEL: @calloc_constant_zero_size2(
121 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 [[N:%.*]], i64 0)
122 ; CHECK-NEXT:    ret i8* [[CALL]]
124   %call = tail call noalias i8* @calloc(i64 %n, i64 0)
125   ret i8* %call
129 define noalias i8* @calloc_constant_zero_size3(i64 %n) {
130 ; CHECK-LABEL: @calloc_constant_zero_size3(
131 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 0, i64 [[N:%.*]])
132 ; CHECK-NEXT:    ret i8* [[CALL]]
134   %call = tail call noalias i8* @calloc(i64 0, i64 %n)
135   ret i8* %call
138 define noalias i8* @calloc_constant_zero_size4(i64 %n) {
139 ; CHECK-LABEL: @calloc_constant_zero_size4(
140 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 0, i64 1)
141 ; CHECK-NEXT:    ret i8* [[CALL]]
143   %call = tail call noalias i8* @calloc(i64 0, i64 1)
144   ret i8* %call
147 define noalias i8* @calloc_constant_zero_size5(i64 %n) {
148 ; CHECK-LABEL: @calloc_constant_zero_size5(
149 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 1, i64 0)
150 ; CHECK-NEXT:    ret i8* [[CALL]]
152   %call = tail call noalias i8* @calloc(i64 1, i64 0)
153   ret i8* %call
156 define noalias i8* @calloc_constant_size() {
157 ; CHECK-LABEL: @calloc_constant_size(
158 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias dereferenceable_or_null(128) i8* @calloc(i64 16, i64 8)
159 ; CHECK-NEXT:    ret i8* [[CALL]]
161   %call = tail call noalias i8* @calloc(i64 16, i64 8)
162   ret i8* %call
165 define noalias i8* @calloc_constant_size_overflow() {
166 ; CHECK-LABEL: @calloc_constant_size_overflow(
167 ; CHECK-NEXT:    [[CALL:%.*]] = tail call noalias i8* @calloc(i64 2000000000000, i64 80000000000)
168 ; CHECK-NEXT:    ret i8* [[CALL]]
170   %call = tail call noalias i8* @calloc(i64 2000000000000, i64 80000000000)
171   ret i8* %call
174 define noalias i8* @op_new_nonconstant_size(i64 %n) {
175 ; CHECK-LABEL: @op_new_nonconstant_size(
176 ; CHECK-NEXT:    [[CALL:%.*]] = tail call i8* @_Znam(i64 [[N:%.*]])
177 ; CHECK-NEXT:    ret i8* [[CALL]]
179   %call = tail call i8* @_Znam(i64 %n)
180   ret i8* %call
183 define noalias i8* @op_new_constant_size() {
184 ; CHECK-LABEL: @op_new_constant_size(
185 ; CHECK-NEXT:    [[CALL:%.*]] = tail call dereferenceable(40) i8* @_Znam(i64 40)
186 ; CHECK-NEXT:    ret i8* [[CALL]]
188   %call = tail call i8* @_Znam(i64 40)
189   ret i8* %call
192 define noalias i8* @op_new_constant_size2() {
193 ; CHECK-LABEL: @op_new_constant_size2(
194 ; CHECK-NEXT:    [[CALL:%.*]] = tail call dereferenceable(40) i8* @_Znwm(i64 40)
195 ; CHECK-NEXT:    ret i8* [[CALL]]
197   %call = tail call i8* @_Znwm(i64 40)
198   ret i8* %call
201 define noalias i8* @op_new_constant_zero_size() {
202 ; CHECK-LABEL: @op_new_constant_zero_size(
203 ; CHECK-NEXT:    [[CALL:%.*]] = tail call i8* @_Znam(i64 0)
204 ; CHECK-NEXT:    ret i8* [[CALL]]
206   %call = tail call i8* @_Znam(i64 0)
207   ret i8* %call