11use crate :: mir:: interpret:: Scalar ;
22use crate :: ty:: { self , Ty , TyCtxt } ;
33use rustc_ast:: { InlineAsmOptions , InlineAsmTemplatePiece } ;
4+ use smallvec:: { smallvec, SmallVec } ;
45
56use super :: {
67 AssertMessage , BasicBlock , InlineAsmOperand , Operand , Place , SourceInfo , Successors ,
@@ -17,10 +18,10 @@ use std::slice;
1718pub use super :: query:: * ;
1819
1920#[ derive( Debug , Clone , TyEncodable , TyDecodable , HashStable , PartialEq ) ]
20- pub struct SwitchTargets < ' tcx > {
21+ pub struct SwitchTargets {
2122 /// Possible values. The locations to branch to in each case
2223 /// are found in the corresponding indices from the `targets` vector.
23- values : Cow < ' tcx , [ u128 ] > ,
24+ values : SmallVec < [ u128 ; 1 ] > ,
2425
2526 /// Possible branch sites. The last element of this vector is used
2627 /// for the otherwise branch, so targets.len() == values.len() + 1
@@ -34,24 +35,24 @@ pub struct SwitchTargets<'tcx> {
3435 //
3536 // However we’ve decided to keep this as-is until we figure a case
3637 // where some other approach seems to be strictly better than other.
37- targets : Vec < BasicBlock > ,
38+ targets : SmallVec < [ BasicBlock ; 2 ] > ,
3839}
3940
40- impl < ' tcx > SwitchTargets < ' tcx > {
41+ impl SwitchTargets {
4142 /// Creates switch targets from an iterator of values and target blocks.
4243 ///
4344 /// The iterator may be empty, in which case the `SwitchInt` instruction is equivalent to
4445 /// `goto otherwise;`.
4546 pub fn new ( targets : impl Iterator < Item = ( u128 , BasicBlock ) > , otherwise : BasicBlock ) -> Self {
46- let ( values, mut targets) : ( Vec < _ > , Vec < _ > ) = targets. unzip ( ) ;
47+ let ( values, mut targets) : ( SmallVec < _ > , SmallVec < _ > ) = targets. unzip ( ) ;
4748 targets. push ( otherwise) ;
4849 Self { values : values. into ( ) , targets }
4950 }
5051
5152 /// Builds a switch targets definition that jumps to `then` if the tested value equals `value`,
5253 /// and to `else_` if not.
53- pub fn static_if ( value : & ' static [ u128 ; 1 ] , then : BasicBlock , else_ : BasicBlock ) -> Self {
54- Self { values : Cow :: Borrowed ( value) , targets : vec ! [ then, else_] }
54+ pub fn static_if ( value : u128 , then : BasicBlock , else_ : BasicBlock ) -> Self {
55+ Self { values : smallvec ! [ value] , targets : smallvec ! [ then, else_] }
5556 }
5657
5758 /// Returns the fallback target that is jumped to when none of the values match the operand.
@@ -113,7 +114,7 @@ pub enum TerminatorKind<'tcx> {
113114 /// FIXME: remove this redundant information. Currently, it is relied on by pretty-printing.
114115 switch_ty : Ty < ' tcx > ,
115116
116- targets : SwitchTargets < ' tcx > ,
117+ targets : SwitchTargets ,
117118 } ,
118119
119120 /// Indicates that the landing pad is finished and unwinding should
@@ -295,7 +296,7 @@ impl<'tcx> TerminatorKind<'tcx> {
295296 TerminatorKind :: SwitchInt {
296297 discr : cond,
297298 switch_ty : tcx. types . bool ,
298- targets : SwitchTargets :: static_if ( & [ 0 ] , f, t) ,
299+ targets : SwitchTargets :: static_if ( 0 , f, t) ,
299300 }
300301 }
301302
0 commit comments