@@ -773,6 +773,14 @@ fn fn_abi_adjust_for_abi<'tcx>(
773
773
774
774
match arg. layout . abi {
775
775
Abi :: Aggregate { .. } => { }
776
+ Abi :: ScalarPair ( ..) => {
777
+ // FIXME: It is strange that small struct optimizations are enabled
778
+ // for 3-fields and more, but disabled for 2-fields (represented by ScalarPair)
779
+ // Here 2-fields optimizations are enabled only for return value
780
+ if !tcx. sess . opts . unstable_opts . reg_struct_return || arg_idx. is_some ( ) {
781
+ return ;
782
+ }
783
+ }
776
784
777
785
// This is a fun case! The gist of what this is doing is
778
786
// that we want callers and callees to always agree on the
@@ -804,12 +812,22 @@ fn fn_abi_adjust_for_abi<'tcx>(
804
812
}
805
813
// Compute `Aggregate` ABI.
806
814
807
- let is_indirect_not_on_stack =
808
- matches ! ( arg. mode, PassMode :: Indirect { on_stack: false , .. } ) ;
809
- assert ! ( is_indirect_not_on_stack, "{:?}" , arg) ;
815
+ let is_indirect_not_on_stack = ! matches ! ( arg . layout . abi , Abi :: Aggregate { .. } )
816
+ || matches ! ( arg. mode, PassMode :: Indirect { on_stack: false , .. } ) ;
817
+ assert ! ( is_indirect_not_on_stack, "is_indirect_not_on_stack: {:?}" , arg) ;
810
818
811
819
let size = arg. layout . size ;
812
- if !arg. layout . is_unsized ( ) && size <= Pointer ( AddressSpace :: DATA ) . size ( cx) {
820
+ let ptr_size = Pointer ( AddressSpace :: DATA ) . size ( cx) ;
821
+
822
+ // In x86 we may return 2x pointer sized struct as i64
823
+ let reg_struct_return_case = tcx. sess . target . arch == "x86"
824
+ && arg_idx. is_none ( )
825
+ && tcx. sess . opts . unstable_opts . reg_struct_return
826
+ && abi != SpecAbi :: RustIntrinsic ;
827
+
828
+ if !arg. layout . is_unsized ( )
829
+ && ( size <= ptr_size || ( reg_struct_return_case && ( size <= 2 * ptr_size) ) )
830
+ {
813
831
// We want to pass small aggregates as immediates, but using
814
832
// an LLVM aggregate type for this leads to bad optimizations,
815
833
// so we pick an appropriately sized integer type instead.
0 commit comments