Skip to content

Commit 2d037f5

Browse files
committed
[Inliner] Use "best" ret attribute when propagating attributes during inlining
For attributes assosiated with a value (like `dereferenceable(N)`) instead of always using the attribute from the to-be inlined caller, it should keep using the value at existing callsites that have the attribute if the value is higher (provides more information).
1 parent 733f373 commit 2d037f5

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

llvm/lib/Transforms/Utils/InlineFunction.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,11 @@ static void AddReturnAttributes(CallBase &CB, ValueToValueMapTy &VMap) {
14171417
// existing attribute value (i.e. attributes such as dereferenceable,
14181418
// dereferenceable_or_null etc). See AttrBuilder::merge for more details.
14191419
AttributeList AL = NewRetVal->getAttributes();
1420+
if (ValidUB.getDereferenceableBytes() < AL.getRetDereferenceableBytes())
1421+
ValidUB.removeAttribute(Attribute::Dereferenceable);
1422+
if (ValidUB.getDereferenceableOrNullBytes() <
1423+
AL.getRetDereferenceableOrNullBytes())
1424+
ValidUB.removeAttribute(Attribute::DereferenceableOrNull);
14201425
AttributeList NewAL = AL.addRetAttributes(Context, ValidUB);
14211426
// Attributes that may generate poison returns are a bit tricky. If we
14221427
// propagate them, other uses of the callsite might have their behavior

llvm/test/Transforms/Inline/ret_attr_align_and_noundef.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ define ptr @callee6() {
132132

133133
define ptr @caller6_fail() {
134134
; CHECK-LABEL: define ptr @caller6_fail() {
135-
; CHECK-NEXT: [[R_I:%.*]] = call dereferenceable(8) ptr @foo()
135+
; CHECK-NEXT: [[R_I:%.*]] = call dereferenceable(16) ptr @foo()
136136
; CHECK-NEXT: ret ptr [[R_I]]
137137
;
138138
%r = call dereferenceable(8) ptr @callee6()
@@ -159,7 +159,7 @@ define ptr @callee7() {
159159

160160
define ptr @caller7_fail() {
161161
; CHECK-LABEL: define ptr @caller7_fail() {
162-
; CHECK-NEXT: [[R_I:%.*]] = call dereferenceable_or_null(8) ptr @foo()
162+
; CHECK-NEXT: [[R_I:%.*]] = call dereferenceable_or_null(16) ptr @foo()
163163
; CHECK-NEXT: ret ptr [[R_I]]
164164
;
165165
%r = call dereferenceable_or_null(8) ptr @callee7()

llvm/test/Transforms/Inline/ret_attr_update.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ define internal ptr @callee6(ptr %p) alwaysinline {
153153
define ptr @test6(ptr %ptr, i64 %x) {
154154
; CHECK-LABEL: @test6(
155155
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i8, ptr [[PTR:%.*]], i64 [[X:%.*]]
156-
; CHECK-NEXT: [[R_I:%.*]] = call dereferenceable_or_null(12) ptr @foo(ptr [[GEP]])
156+
; CHECK-NEXT: [[R_I:%.*]] = call dereferenceable_or_null(16) ptr @foo(ptr [[GEP]])
157157
; CHECK-NEXT: [[V_I:%.*]] = call ptr @baz(ptr [[GEP]])
158158
; CHECK-NEXT: ret ptr [[R_I]]
159159
;

0 commit comments

Comments
 (0)