Skip to content

Commit b440a43

Browse files
committed
llvm-reduce: Add values to return reduction
In void functions, try to replace instruction uses with a new non-void return. If the return type matches the instruction, also try to directly return it. This handles most of the cases, but doesn't try to handle all of the weird exception related terminators. Also doesn't try to replace argument uses, although it could. We could also handle cases where we can insert a simple cast to an original return value. I didn't think too hard about where to put this in the default pass order. In many cases it obviates the need for most of the CFG folds, but I've left it near the end initially. I also think this is too aggressive about removing dead code, and should leave existing dead code alone. I'm also not sure why we have both "removeUnreachableBlocks" and EliminateUnreachableBlocks" in Utils. Fixes #66039, fixes #107327
1 parent 76b999d commit b440a43

File tree

10 files changed

+1318
-3
lines changed

10 files changed

+1318
-3
lines changed

llvm/include/llvm/IR/Function.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,14 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
10481048
void setValueSubclassDataBit(unsigned Bit, bool On);
10491049
};
10501050

1051+
namespace CallingConv {
1052+
1053+
// TODO: Need similar function for support of argument in position. General
1054+
// version on FunctionType + Attributes + CallingConv::ID?
1055+
LLVM_READNONE
1056+
bool supportsNonVoidReturnType(CallingConv::ID CC);
1057+
} // namespace CallingConv
1058+
10511059
/// Check whether null pointer dereferencing is considered undefined behavior
10521060
/// for a given function or an address space.
10531061
/// Null pointer access in non-zero address space is not considered undefined.

llvm/lib/IR/Function.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,3 +1177,85 @@ bool llvm::NullPointerIsDefined(const Function *F, unsigned AS) {
11771177

11781178
return false;
11791179
}
1180+
1181+
bool llvm::CallingConv::supportsNonVoidReturnType(CallingConv::ID CC) {
1182+
switch (CC) {
1183+
case CallingConv::C:
1184+
case CallingConv::Fast:
1185+
case CallingConv::Cold:
1186+
case CallingConv::GHC:
1187+
case CallingConv::HiPE:
1188+
case CallingConv::AnyReg:
1189+
case CallingConv::PreserveMost:
1190+
case CallingConv::PreserveAll:
1191+
case CallingConv::Swift:
1192+
case CallingConv::CXX_FAST_TLS:
1193+
case CallingConv::Tail:
1194+
case CallingConv::CFGuard_Check:
1195+
case CallingConv::SwiftTail:
1196+
case CallingConv::PreserveNone:
1197+
case CallingConv::X86_StdCall:
1198+
case CallingConv::X86_FastCall:
1199+
case CallingConv::ARM_APCS:
1200+
case CallingConv::ARM_AAPCS:
1201+
case CallingConv::ARM_AAPCS_VFP:
1202+
case CallingConv::MSP430_INTR:
1203+
case CallingConv::X86_ThisCall:
1204+
case CallingConv::PTX_Device:
1205+
case CallingConv::SPIR_FUNC:
1206+
case CallingConv::Intel_OCL_BI:
1207+
case CallingConv::X86_64_SysV:
1208+
case CallingConv::Win64:
1209+
case CallingConv::X86_VectorCall:
1210+
case CallingConv::DUMMY_HHVM:
1211+
case CallingConv::DUMMY_HHVM_C:
1212+
case CallingConv::X86_INTR:
1213+
case CallingConv::AVR_INTR:
1214+
case CallingConv::AVR_SIGNAL:
1215+
case CallingConv::AVR_BUILTIN:
1216+
return true;
1217+
case CallingConv::AMDGPU_KERNEL:
1218+
case CallingConv::SPIR_KERNEL:
1219+
case CallingConv::AMDGPU_CS_Chain:
1220+
case CallingConv::AMDGPU_CS_ChainPreserve:
1221+
return false;
1222+
case CallingConv::AMDGPU_VS:
1223+
case CallingConv::AMDGPU_HS:
1224+
case CallingConv::AMDGPU_GS:
1225+
case CallingConv::AMDGPU_PS:
1226+
case CallingConv::AMDGPU_CS:
1227+
case CallingConv::AMDGPU_LS:
1228+
case CallingConv::AMDGPU_ES:
1229+
case CallingConv::MSP430_BUILTIN:
1230+
case CallingConv::AArch64_VectorCall:
1231+
case CallingConv::AArch64_SVE_VectorCall:
1232+
case CallingConv::WASM_EmscriptenInvoke:
1233+
case CallingConv::AMDGPU_Gfx:
1234+
case CallingConv::M68k_INTR:
1235+
case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0:
1236+
case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2:
1237+
case CallingConv::M68k_RTD:
1238+
case CallingConv::GRAAL:
1239+
case CallingConv::ARM64EC_Thunk_X64:
1240+
case CallingConv::ARM64EC_Thunk_Native:
1241+
case CallingConv::RISCV_VectorCall:
1242+
case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1:
1243+
case CallingConv::RISCV_VLSCall_32:
1244+
case CallingConv::RISCV_VLSCall_64:
1245+
case CallingConv::RISCV_VLSCall_128:
1246+
case CallingConv::RISCV_VLSCall_256:
1247+
case CallingConv::RISCV_VLSCall_512:
1248+
case CallingConv::RISCV_VLSCall_1024:
1249+
case CallingConv::RISCV_VLSCall_2048:
1250+
case CallingConv::RISCV_VLSCall_4096:
1251+
case CallingConv::RISCV_VLSCall_8192:
1252+
case CallingConv::RISCV_VLSCall_16384:
1253+
case CallingConv::RISCV_VLSCall_32768:
1254+
case CallingConv::RISCV_VLSCall_65536:
1255+
return true;
1256+
default:
1257+
return false;
1258+
}
1259+
1260+
llvm_unreachable("covered callingconv switch");
1261+
}

llvm/test/tools/llvm-reduce/reduce-operands-fp.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
; CHECK-INTERESTINGNESS: = fadd <2 x float>
2828
; CHECK-INTERESTINGNESS: = fadd <2 x float>
2929

30-
; CHECK-LABEL: define void @foo(
30+
; CHECK-LABEL: define {{(void|<2 x float>)}} @foo(
3131

3232

3333
; ONE: %fadd0 = fadd float %arg0, 1.000000e+00

llvm/test/tools/llvm-reduce/reduce-operands-int.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
; CHECK-INTERESTINGNESS: = add <2 x i32>
2323
; CHECK-INTERESTINGNESS: = add <2 x i32>
2424

25-
; CHECK-LABEL: define void @foo(
25+
; CHECK-LABEL: define {{(void|<2 x i32>)}} @foo(
2626

2727

2828
; ONE: %add0 = add i32 %arg0, 1

llvm/test/tools/llvm-reduce/reduce-operands-ptr.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
; RUN: llvm-reduce --abort-on-invalid-reduction --test FileCheck --test-arg --check-prefixes=CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
99
; RUN: FileCheck --check-prefixes=CHECK,ZERO %s < %t
1010

11-
; CHECK-LABEL: define void @foo(
11+
; CHECK-LABEL: define {{(void|ptr)}} @foo(
1212

1313
; ONE: load i32, ptr %a0
1414
; ONE: load i32, ptr @g

0 commit comments

Comments
 (0)