@@ -45,6 +45,14 @@ pub enum OperandValue<V> {
4545 /// as returned by [`LayoutTypeMethods::scalar_pair_element_backend_type`]
4646 /// with `immediate: true`.
4747 Pair ( V , V ) ,
48+ /// A value taking no bytes, and which therefore needs no LLVM value at all.
49+ ///
50+ /// If you ever need a `V` to pass to something, get a fresh poison value
51+ /// from [`ConstMethods::const_poison`].
52+ ///
53+ /// An `OperandValue` *must* be this variant for any type for which
54+ /// `is_zst` on its `Layout` returns `true`.
55+ ZeroSized ,
4856}
4957
5058/// An `OperandRef` is an "SSA" reference to a Rust value, along with
@@ -71,15 +79,9 @@ impl<V: CodegenObject> fmt::Debug for OperandRef<'_, V> {
7179}
7280
7381impl < ' a , ' tcx , V : CodegenObject > OperandRef < ' tcx , V > {
74- pub fn new_zst < Bx : BuilderMethods < ' a , ' tcx , Value = V > > (
75- bx : & mut Bx ,
76- layout : TyAndLayout < ' tcx > ,
77- ) -> OperandRef < ' tcx , V > {
82+ pub fn zero_sized ( layout : TyAndLayout < ' tcx > ) -> OperandRef < ' tcx , V > {
7883 assert ! ( layout. is_zst( ) ) ;
79- OperandRef {
80- val : OperandValue :: Immediate ( bx. const_poison ( bx. immediate_backend_type ( layout) ) ) ,
81- layout,
82- }
84+ OperandRef { val : OperandValue :: ZeroSized , layout }
8385 }
8486
8587 pub fn from_const < Bx : BuilderMethods < ' a , ' tcx , Value = V > > (
@@ -97,7 +99,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
9799 let llval = bx. scalar_to_backend ( x, scalar, bx. immediate_backend_type ( layout) ) ;
98100 OperandValue :: Immediate ( llval)
99101 }
100- ConstValue :: ZeroSized => return OperandRef :: new_zst ( bx , layout) ,
102+ ConstValue :: ZeroSized => return OperandRef :: zero_sized ( layout) ,
101103 ConstValue :: Slice { data, start, end } => {
102104 let Abi :: ScalarPair ( a_scalar, _) = layout. abi else {
103105 bug ! ( "from_const: invalid ScalarPair layout: {:#?}" , layout) ;
@@ -147,6 +149,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
147149 OperandValue :: Immediate ( llptr) => ( llptr, None ) ,
148150 OperandValue :: Pair ( llptr, llextra) => ( llptr, Some ( llextra) ) ,
149151 OperandValue :: Ref ( ..) => bug ! ( "Deref of by-Ref operand {:?}" , self ) ,
152+ OperandValue :: ZeroSized => bug ! ( "Deref of ZST operand {:?}" , self ) ,
150153 } ;
151154 let layout = cx. layout_of ( projected_ty) ;
152155 PlaceRef { llval : llptr, llextra, layout, align : layout. align . abi }
@@ -204,9 +207,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
204207
205208 let mut val = match ( self . val , self . layout . abi ) {
206209 // If the field is ZST, it has no data.
207- _ if field. is_zst ( ) => {
208- return OperandRef :: new_zst ( bx, field) ;
209- }
210+ _ if field. is_zst ( ) => OperandValue :: ZeroSized ,
210211
211212 // Newtype of a scalar, scalar pair or vector.
212213 ( OperandValue :: Immediate ( _) | OperandValue :: Pair ( ..) , _)
@@ -237,6 +238,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
237238 } ;
238239
239240 match ( & mut val, field. abi ) {
241+ ( OperandValue :: ZeroSized , _) => { }
240242 (
241243 OperandValue :: Immediate ( llval) ,
242244 Abi :: Scalar ( _) | Abi :: ScalarPair ( ..) | Abi :: Vector { .. } ,
@@ -290,16 +292,18 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
290292impl < ' a , ' tcx , V : CodegenObject > OperandValue < V > {
291293 /// Returns an `OperandValue` that's generally UB to use in any way.
292294 ///
293- /// Depending on the `layout`, returns an `Immediate` or `Pair` containing
294- /// poison value(s), or a `Ref` containing a poison pointer.
295+ /// Depending on the `layout`, returns `ZeroSized` for ZSTs, an `Immediate` or
296+ /// `Pair` containing poison value(s), or a `Ref` containing a poison pointer.
295297 ///
296298 /// Supports sized types only.
297299 pub fn poison < Bx : BuilderMethods < ' a , ' tcx , Value = V > > (
298300 bx : & mut Bx ,
299301 layout : TyAndLayout < ' tcx > ,
300302 ) -> OperandValue < V > {
301303 assert ! ( layout. is_sized( ) ) ;
302- if bx. cx ( ) . is_backend_immediate ( layout) {
304+ if layout. is_zst ( ) {
305+ OperandValue :: ZeroSized
306+ } else if bx. cx ( ) . is_backend_immediate ( layout) {
303307 let ibty = bx. cx ( ) . immediate_backend_type ( layout) ;
304308 OperandValue :: Immediate ( bx. const_poison ( ibty) )
305309 } else if bx. cx ( ) . is_backend_scalar_pair ( layout) {
@@ -352,12 +356,11 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
352356 flags : MemFlags ,
353357 ) {
354358 debug ! ( "OperandRef::store: operand={:?}, dest={:?}" , self , dest) ;
355- // Avoid generating stores of zero-sized values, because the only way to have a zero-sized
356- // value is through `undef`, and store itself is useless.
357- if dest. layout . is_zst ( ) {
358- return ;
359- }
360359 match self {
360+ OperandValue :: ZeroSized => {
361+ // Avoid generating stores of zero-sized values, because the only way to have a zero-sized
362+ // value is through `undef`/`poison`, and the store itself is useless.
363+ }
361364 OperandValue :: Ref ( r, None , source_align) => {
362365 if flags. contains ( MemFlags :: NONTEMPORAL ) {
363366 // HACK(nox): This is inefficient but there is no nontemporal memcpy.
@@ -458,7 +461,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
458461 // checks in `codegen_consume` and `extract_field`.
459462 let elem = o. layout . field ( bx. cx ( ) , 0 ) ;
460463 if elem. is_zst ( ) {
461- o = OperandRef :: new_zst ( bx , elem) ;
464+ o = OperandRef :: zero_sized ( elem) ;
462465 } else {
463466 return None ;
464467 }
@@ -492,7 +495,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
492495
493496 // ZSTs don't require any actual memory access.
494497 if layout. is_zst ( ) {
495- return OperandRef :: new_zst ( bx , layout) ;
498+ return OperandRef :: zero_sized ( layout) ;
496499 }
497500
498501 if let Some ( o) = self . maybe_codegen_consume_direct ( bx, place_ref) {
0 commit comments