@@ -773,6 +773,14 @@ fn fn_abi_adjust_for_abi<'tcx>(
773773
774774 match arg. layout . abi {
775775 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+ }
776784
777785 // This is a fun case! The gist of what this is doing is
778786 // that we want callers and callees to always agree on the
@@ -804,12 +812,22 @@ fn fn_abi_adjust_for_abi<'tcx>(
804812 }
805813 // Compute `Aggregate` ABI.
806814
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) ;
810818
811819 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+ {
813831 // We want to pass small aggregates as immediates, but using
814832 // an LLVM aggregate type for this leads to bad optimizations,
815833 // so we pick an appropriately sized integer type instead.
0 commit comments