@@ -150,12 +150,18 @@ pub(crate) fn codegen_int_binop<'tcx>(
150
150
BinOp :: BitXor => b. bxor ( lhs, rhs) ,
151
151
BinOp :: BitAnd => b. band ( lhs, rhs) ,
152
152
BinOp :: BitOr => b. bor ( lhs, rhs) ,
153
- BinOp :: Shl => b. ishl ( lhs, rhs) ,
153
+ BinOp :: Shl => {
154
+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
155
+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
156
+ fx. bcx . ins ( ) . ishl ( lhs, actual_shift)
157
+ }
154
158
BinOp :: Shr => {
159
+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
160
+ let actual_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
155
161
if signed {
156
- b . sshr ( lhs, rhs )
162
+ fx . bcx . ins ( ) . sshr ( lhs, actual_shift )
157
163
} else {
158
- b . ushr ( lhs, rhs )
164
+ fx . bcx . ins ( ) . ushr ( lhs, actual_shift )
159
165
}
160
166
}
161
167
// Compare binops handles by `codegen_binop`.
@@ -273,15 +279,22 @@ pub(crate) fn codegen_checked_int_binop<'tcx>(
273
279
}
274
280
}
275
281
BinOp :: Shl => {
276
- let val = fx. bcx . ins ( ) . ishl ( lhs, rhs) ;
282
+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
283
+ let masked_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
284
+ let val = fx. bcx . ins ( ) . ishl ( lhs, masked_shift) ;
277
285
let ty = fx. bcx . func . dfg . value_type ( val) ;
278
286
let max_shift = i64:: from ( ty. bits ( ) ) - 1 ;
279
287
let has_overflow = fx. bcx . ins ( ) . icmp_imm ( IntCC :: UnsignedGreaterThan , rhs, max_shift) ;
280
288
( val, has_overflow)
281
289
}
282
290
BinOp :: Shr => {
283
- let val =
284
- if !signed { fx. bcx . ins ( ) . ushr ( lhs, rhs) } else { fx. bcx . ins ( ) . sshr ( lhs, rhs) } ;
291
+ let lhs_ty = fx. bcx . func . dfg . value_type ( lhs) ;
292
+ let masked_shift = fx. bcx . ins ( ) . band_imm ( rhs, i64:: from ( lhs_ty. bits ( ) - 1 ) ) ;
293
+ let val = if !signed {
294
+ fx. bcx . ins ( ) . ushr ( lhs, masked_shift)
295
+ } else {
296
+ fx. bcx . ins ( ) . sshr ( lhs, masked_shift)
297
+ } ;
285
298
let ty = fx. bcx . func . dfg . value_type ( val) ;
286
299
let max_shift = i64:: from ( ty. bits ( ) ) - 1 ;
287
300
let has_overflow = fx. bcx . ins ( ) . icmp_imm ( IntCC :: UnsignedGreaterThan , rhs, max_shift) ;
0 commit comments