@@ -407,23 +407,21 @@ impl Assembler {
407
407
kind : DivKind ,
408
408
size : OperandSize ,
409
409
) {
410
- // Check for division by 0
411
- self . emit ( Inst :: TrapIf {
412
- kind : CondBrKind :: Zero ( divisor. into ( ) ) ,
413
- trap_code : TrapCode :: INTEGER_DIVISION_BY_ZERO ,
414
- } ) ;
410
+ // Check for division by 0.
411
+ self . trapz ( divisor, TrapCode :: INTEGER_DIVISION_BY_ZERO ) ;
415
412
416
413
// check for overflow
417
414
if kind == DivKind :: Signed {
418
- // we first check whether the divisor is -1
419
- self . emit ( Inst :: AluRRImm12 {
420
- alu_op : ALUOp :: AddS ,
421
- size : size. into ( ) ,
422
- rd : writable ! ( zero( ) . into( ) ) ,
423
- rn : divisor. into ( ) ,
424
- imm12 : Imm12 :: maybe_from_u64 ( 1 ) . expect ( "1 fits in 12 bits" ) ,
425
- } ) ;
426
- // if it is -1, then we check if the dividend is MIN
415
+ // Check for divisor overflow.
416
+ self . emit_alu_rri (
417
+ ALUOp :: AddS ,
418
+ Imm12 :: maybe_from_u64 ( 1 ) . expect ( "1 to fit in 12 bits" ) ,
419
+ divisor,
420
+ writable ! ( zero( ) ) ,
421
+ size,
422
+ ) ;
423
+
424
+ // Check if the dividend is 1.
427
425
self . emit ( Inst :: CCmpImm {
428
426
size : size. into ( ) ,
429
427
rn : dividend. into ( ) ,
@@ -432,31 +430,22 @@ impl Assembler {
432
430
cond : Cond :: Eq ,
433
431
} ) ;
434
432
435
- // Finally, trap if the previous operation overflowed
436
- self . emit ( Inst :: TrapIf {
437
- kind : CondBrKind :: Cond ( Cond :: Vs ) ,
438
- trap_code : TrapCode :: INTEGER_OVERFLOW ,
439
- } )
433
+ // Finally, trap if the previous operation overflowed.
434
+ self . trapif ( Cond :: Vs , TrapCode :: INTEGER_OVERFLOW ) ;
440
435
}
441
436
442
437
// `cranelift-codegen` doesn't support emitting u/sdiv for anything but I64,
443
438
// we therefore sign-extend the operand.
444
439
// see: https://github.com/bytecodealliance/wasmtime/issues/9766
445
440
if size == OperandSize :: S32 {
446
- self . emit ( Inst :: Extend {
447
- rd : writable ! ( divisor. into( ) ) ,
448
- rn : divisor. into ( ) ,
449
- signed : true ,
450
- from_bits : 32 ,
451
- to_bits : 64 ,
452
- } ) ;
453
- self . emit ( Inst :: Extend {
454
- rd : writable ! ( dividend. into( ) ) ,
455
- rn : dividend. into ( ) ,
456
- signed : true ,
457
- from_bits : 32 ,
458
- to_bits : 64 ,
459
- } ) ;
441
+ let extend_kind = if kind == DivKind :: Signed {
442
+ ExtendKind :: I64Extend32S
443
+ } else {
444
+ ExtendKind :: I64ExtendI32U
445
+ } ;
446
+
447
+ self . extend ( divisor, writable ! ( divisor) , extend_kind) ;
448
+ self . extend ( dividend, writable ! ( dividend) , extend_kind) ;
460
449
}
461
450
462
451
let op = match kind {
0 commit comments