[ArgumentPromotion] Bail if any callers are minsize

Argument promotion mostly works on functions with more than one caller (otherwise the function would be inlined or is dead), so there's a good chance that performing this increases code size since we introduce loads at every call site. If any caller is marked minsize, bail.

We could compare the number of loads/stores removed from the function with the number of loads introduced in callers, but that's TODO.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D149768
This commit is contained in:
Arthur Eubanks
2023-05-03 09:56:22 -07:00
parent 7e6a15892a
commit 8b8466fd31
2 changed files with 72 additions and 0 deletions

View File

@@ -764,6 +764,15 @@ static Function *promoteArguments(Function *F, FunctionAnalysisManager &FAM,
if (CB->isMustTailCall())
return nullptr;
// If the caller is marked minsize, this transformation may increase code
// size. We assume that there is more than one call to this function since
// otherwise this function would be inlined or is dead.
// TODO: compare the number of loads/stores removed from the function with
// the number of introduced loads in callees to see if this is profitable
// code-size-wise.
if (CB->getFunction()->hasMinSize())
return nullptr;
if (CB->getFunction() == F)
IsRecursive = true;
}

View File

@@ -0,0 +1,63 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
; RUN: opt -passes=argpromotion -S < %s | FileCheck %s
define internal i32 @f1(ptr %p) {
; CHECK-LABEL: define internal i32 @f1
; CHECK-SAME: (i32 [[P_0_VAL:%.*]]) {
; CHECK-NEXT: ret i32 [[P_0_VAL]]
;
%i = load i32, ptr %p
ret i32 %i
}
define i32 @g1(ptr %p) {
; CHECK-LABEL: define i32 @g1
; CHECK-SAME: (ptr [[P:%.*]]) {
; CHECK-NEXT: [[P_VAL:%.*]] = load i32, ptr [[P]], align 4
; CHECK-NEXT: [[I:%.*]] = call i32 @f1(i32 [[P_VAL]])
; CHECK-NEXT: ret i32 [[I]]
;
%i = call i32 @f1(ptr %p)
ret i32 %i
}
define i32 @g2(ptr %p) {
; CHECK-LABEL: define i32 @g2
; CHECK-SAME: (ptr [[P:%.*]]) {
; CHECK-NEXT: [[P_VAL:%.*]] = load i32, ptr [[P]], align 4
; CHECK-NEXT: [[I:%.*]] = call i32 @f1(i32 [[P_VAL]])
; CHECK-NEXT: ret i32 [[I]]
;
%i = call i32 @f1(ptr %p)
ret i32 %i
}
define internal i32 @f2(ptr %p) {
; CHECK-LABEL: define internal i32 @f2
; CHECK-SAME: (ptr [[P:%.*]]) {
; CHECK-NEXT: [[I:%.*]] = load i32, ptr [[P]], align 4
; CHECK-NEXT: ret i32 [[I]]
;
%i = load i32, ptr %p
ret i32 %i
}
define i32 @h1(ptr %p) minsize {
; CHECK-LABEL: define i32 @h1
; CHECK-SAME: (ptr [[P:%.*]]) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: [[I:%.*]] = call i32 @f2(ptr [[P]])
; CHECK-NEXT: ret i32 [[I]]
;
%i = call i32 @f2(ptr %p)
ret i32 %i
}
define i32 @h2(ptr %p) {
; CHECK-LABEL: define i32 @h2
; CHECK-SAME: (ptr [[P:%.*]]) {
; CHECK-NEXT: [[I:%.*]] = call i32 @f2(ptr [[P]])
; CHECK-NEXT: ret i32 [[I]]
;
%i = call i32 @f2(ptr %p)
ret i32 %i
}