@@ -466,6 +466,122 @@ define i32 @shl_sub_lshr(i32 %x, i32 %c, i32 %y) {
466
466
ret i32 %lshr
467
467
}
468
468
469
+ define i32 @shl_sub_lshr_reverse (i32 %x , i32 %c , i32 %y ) {
470
+ ; CHECK-LABEL: @shl_sub_lshr_reverse(
471
+ ; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
472
+ ; CHECK-NEXT: [[SUB:%.*]] = sub nuw nsw i32 [[Y:%.*]], [[SHL]]
473
+ ; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
474
+ ; CHECK-NEXT: ret i32 [[LSHR]]
475
+ ;
476
+ %shl = shl nuw i32 %x , %c
477
+ %sub = sub nuw nsw i32 %y , %shl
478
+ %lshr = lshr exact i32 %sub , %c
479
+ ret i32 %lshr
480
+ }
481
+
482
+ define i32 @shl_sub_lshr_reverse_no_nsw (i32 %x , i32 %c , i32 %y ) {
483
+ ; CHECK-LABEL: @shl_sub_lshr_reverse_no_nsw(
484
+ ; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
485
+ ; CHECK-NEXT: [[SUB:%.*]] = sub nuw i32 [[Y:%.*]], [[SHL]]
486
+ ; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
487
+ ; CHECK-NEXT: ret i32 [[LSHR]]
488
+ ;
489
+ %shl = shl nuw i32 %x , %c
490
+ %sub = sub nuw i32 %y , %shl
491
+ %lshr = lshr exact i32 %sub , %c
492
+ ret i32 %lshr
493
+ }
494
+
495
+ define i32 @shl_sub_lshr_reverse_nsw_on_op1 (i32 %x , i32 %c , i32 %y ) {
496
+ ; CHECK-LABEL: @shl_sub_lshr_reverse_nsw_on_op1(
497
+ ; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i32 [[X:%.*]], [[C:%.*]]
498
+ ; CHECK-NEXT: [[SUB:%.*]] = sub nuw i32 [[Y:%.*]], [[SHL]]
499
+ ; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
500
+ ; CHECK-NEXT: ret i32 [[LSHR]]
501
+ ;
502
+ %shl = shl nuw nsw i32 %x , %c
503
+ %sub = sub nuw i32 %y , %shl
504
+ %lshr = lshr exact i32 %sub , %c
505
+ ret i32 %lshr
506
+ }
507
+
508
+ ; Negative test
509
+
510
+ define i32 @shl_sub_lshr_reverse_no_exact (i32 %x , i32 %c , i32 %y ) {
511
+ ; CHECK-LABEL: @shl_sub_lshr_reverse_no_exact(
512
+ ; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
513
+ ; CHECK-NEXT: [[SUB:%.*]] = sub nuw nsw i32 [[Y:%.*]], [[SHL]]
514
+ ; CHECK-NEXT: [[LSHR:%.*]] = lshr i32 [[SUB]], [[C]]
515
+ ; CHECK-NEXT: ret i32 [[LSHR]]
516
+ ;
517
+ %shl = shl nuw i32 %x , %c
518
+ %sub = sub nuw nsw i32 %y , %shl
519
+ %lshr = lshr i32 %sub , %c
520
+ ret i32 %lshr
521
+ }
522
+
523
+ ; Negative test
524
+
525
+ define i32 @shl_sub_lshr_reverse_multiuse (i32 %x , i32 %c , i32 %y ) {
526
+ ; CHECK-LABEL: @shl_sub_lshr_reverse_multiuse(
527
+ ; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
528
+ ; CHECK-NEXT: [[SUB:%.*]] = sub nuw i32 [[Y:%.*]], [[SHL]]
529
+ ; CHECK-NEXT: call void @use(i32 [[SUB]])
530
+ ; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
531
+ ; CHECK-NEXT: ret i32 [[LSHR]]
532
+ ;
533
+ %shl = shl nuw i32 %x , %c
534
+ %sub = sub nuw i32 %y , %shl
535
+ call void @use (i32 %sub )
536
+ %lshr = lshr exact i32 %sub , %c
537
+ ret i32 %lshr
538
+ }
539
+
540
+ define i32 @shl_sub_lshr_reverse_multiuse2 (i32 %x , i32 %c , i32 %y ) {
541
+ ; CHECK-LABEL: @shl_sub_lshr_reverse_multiuse2(
542
+ ; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[X:%.*]], [[C:%.*]]
543
+ ; CHECK-NEXT: call void @use(i32 [[SHL]])
544
+ ; CHECK-NEXT: [[SUB:%.*]] = sub nuw i32 [[Y:%.*]], [[SHL]]
545
+ ; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
546
+ ; CHECK-NEXT: ret i32 [[LSHR]]
547
+ ;
548
+ %shl = shl nuw i32 %x , %c
549
+ call void @use (i32 %shl )
550
+ %sub = sub nuw i32 %y , %shl
551
+ %lshr = lshr exact i32 %sub , %c
552
+ ret i32 %lshr
553
+ }
554
+
555
+ define i32 @shl_sub_lshr_reverse_no_nuw (i32 %x , i32 %c , i32 %y ) {
556
+ ; CHECK-LABEL: @shl_sub_lshr_reverse_no_nuw(
557
+ ; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[X:%.*]], [[C:%.*]]
558
+ ; CHECK-NEXT: call void @use(i32 [[SHL]])
559
+ ; CHECK-NEXT: [[SUB:%.*]] = sub nuw i32 [[Y:%.*]], [[SHL]]
560
+ ; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
561
+ ; CHECK-NEXT: ret i32 [[LSHR]]
562
+ ;
563
+ %shl = shl i32 %x , %c
564
+ call void @use (i32 %shl )
565
+ %sub = sub nuw i32 %y , %shl
566
+ %lshr = lshr exact i32 %sub , %c
567
+ ret i32 %lshr
568
+ }
569
+
570
+ define i32 @shl_sub_lshr_reverse_no_nsw_2 (i32 %x , i32 %c , i32 %y ) {
571
+ ; CHECK-LABEL: @shl_sub_lshr_reverse_no_nsw_2(
572
+ ; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i32 [[X:%.*]], [[C:%.*]]
573
+ ; CHECK-NEXT: call void @use(i32 [[SHL]])
574
+ ; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[Y:%.*]], [[SHL]]
575
+ ; CHECK-NEXT: [[LSHR:%.*]] = lshr exact i32 [[SUB]], [[C]]
576
+ ; CHECK-NEXT: ret i32 [[LSHR]]
577
+ ;
578
+ %shl = shl nuw nsw i32 %x , %c
579
+ call void @use (i32 %shl )
580
+ %sub = sub i32 %y , %shl
581
+ %lshr = lshr exact i32 %sub , %c
582
+ ret i32 %lshr
583
+ }
584
+
469
585
define i32 @shl_or_lshr (i32 %x , i32 %c , i32 %y ) {
470
586
; CHECK-LABEL: @shl_or_lshr(
471
587
; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[Y:%.*]], [[C:%.*]]
0 commit comments