reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -instcombine -S | FileCheck %s

; These patterns are all just traditional clamp pattern.
; But they are not canonical, the and/or/xor is more canonically represented
; as an add+icmp.

define i32 @t0_select_cond_and_v0(i32 %X) {
; CHECK-LABEL: @t0_select_cond_and_v0(
; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
; CHECK-NEXT:    [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
; CHECK-NEXT:    ret i32 [[R]]
;
  %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
  %dont_need_to_clamp_negative = icmp sge i32 %X, -32768
  %clamp_limit = select i1 %dont_need_to_clamp_positive, i32 -32768, i32 32767
  %dont_need_to_clamp = and i1 %dont_need_to_clamp_positive, %dont_need_to_clamp_negative
  %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
  ret i32 %R
}
define i32 @t1_select_cond_and_v1(i32 %X) {
; CHECK-LABEL: @t1_select_cond_and_v1(
; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
; CHECK-NEXT:    [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
; CHECK-NEXT:    ret i32 [[R]]
;
  %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
  %dont_need_to_clamp_negative = icmp sge i32 %X, -32768
  %clamp_limit = select i1 %dont_need_to_clamp_negative, i32 32767, i32 -32768
  %dont_need_to_clamp = and i1 %dont_need_to_clamp_positive, %dont_need_to_clamp_negative
  %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
  ret i32 %R
}

;-------------------------------------------------------------------------------

define i32 @t2_select_cond_or_v0(i32 %X) {
; CHECK-LABEL: @t2_select_cond_or_v0(
; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
; CHECK-NEXT:    [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
; CHECK-NEXT:    ret i32 [[R]]
;
  %need_to_clamp_positive = icmp sgt i32 %X, 32767
  %need_to_clamp_negative = icmp slt i32 %X, -32768
  %clamp_limit = select i1 %need_to_clamp_positive, i32 32767, i32 -32768
  %need_to_clamp = or i1 %need_to_clamp_positive, %need_to_clamp_negative
  %R = select i1 %need_to_clamp, i32 %clamp_limit, i32 %X
  ret i32 %R
}
define i32 @t3_select_cond_or_v1(i32 %X) {
; CHECK-LABEL: @t3_select_cond_or_v1(
; CHECK-NEXT:    [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
; CHECK-NEXT:    [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -32768
; CHECK-NEXT:    [[TMP3:%.*]] = icmp slt i32 [[TMP2]], 32767
; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 32767
; CHECK-NEXT:    ret i32 [[R]]
;
  %need_to_clamp_positive = icmp sgt i32 %X, 32767
  %need_to_clamp_negative = icmp slt i32 %X, -32768
  %clamp_limit = select i1 %need_to_clamp_negative, i32 -32768, i32 32767
  %need_to_clamp = or i1 %need_to_clamp_positive, %need_to_clamp_negative
  %R = select i1 %need_to_clamp, i32 %clamp_limit, i32 %X
  ret i32 %R
}

;-------------------------------------------------------------------------------

define i32 @t4_select_cond_xor_v0(i32 %X) {
; CHECK-LABEL: @t4_select_cond_xor_v0(
; CHECK-NEXT:    [[DOTINV1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[DOTINV1]], i32 [[X]], i32 -32768
; CHECK-NEXT:    [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 32767
; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 32767
; CHECK-NEXT:    ret i32 [[R]]
;
  %need_to_clamp_positive = icmp sgt i32 %X, 32767
  %dont_need_to_clamp_negative = icmp sgt i32 %X, -32768
  %clamp_limit = select i1 %need_to_clamp_positive, i32 32767, i32 -32768
  %dont_need_to_clamp = xor i1 %need_to_clamp_positive, %dont_need_to_clamp_negative
  %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
  ret i32 %R
}
define i32 @t4_select_cond_xor_v1(i32 %X) {
; CHECK-LABEL: @t4_select_cond_xor_v1(
; CHECK-NEXT:    [[DOTINV1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[DOTINV1]], i32 [[X]], i32 -32768
; CHECK-NEXT:    [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 32767
; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 32767
; CHECK-NEXT:    ret i32 [[R]]
;
  %need_to_clamp_positive = icmp sgt i32 %X, 32767
  %dont_need_to_clamp_negative = icmp sgt i32 %X, -32768
  %clamp_limit = select i1 %dont_need_to_clamp_negative, i32 32767, i32 -32768
  %dont_need_to_clamp = xor i1 %need_to_clamp_positive, %dont_need_to_clamp_negative
  %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
  ret i32 %R
}

define i32 @t5_select_cond_xor_v2(i32 %X) {
; CHECK-LABEL: @t5_select_cond_xor_v2(
; CHECK-NEXT:    [[DOTINV1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[DOTINV1]], i32 [[X]], i32 -32768
; CHECK-NEXT:    [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 32767
; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 32767
; CHECK-NEXT:    ret i32 [[R]]
;
  %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
  %need_to_clamp_negative = icmp sle i32 %X, -32768
  %clamp_limit = select i1 %need_to_clamp_negative, i32 -32768, i32 32767
  %dont_need_to_clamp = xor i1 %dont_need_to_clamp_positive, %need_to_clamp_negative
  %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
  ret i32 %R
}
define i32 @t5_select_cond_xor_v3(i32 %X) {
; CHECK-LABEL: @t5_select_cond_xor_v3(
; CHECK-NEXT:    [[DOTINV1:%.*]] = icmp sgt i32 [[X:%.*]], -32768
; CHECK-NEXT:    [[TMP1:%.*]] = select i1 [[DOTINV1]], i32 [[X]], i32 -32768
; CHECK-NEXT:    [[TMP2:%.*]] = icmp slt i32 [[TMP1]], 32767
; CHECK-NEXT:    [[R:%.*]] = select i1 [[TMP2]], i32 [[TMP1]], i32 32767
; CHECK-NEXT:    ret i32 [[R]]
;
  %dont_need_to_clamp_positive = icmp sle i32 %X, 32767
  %need_to_clamp_negative = icmp sle i32 %X, -32768
  %clamp_limit = select i1 %dont_need_to_clamp_positive, i32 -32768, i32 32767
  %dont_need_to_clamp = xor i1 %dont_need_to_clamp_positive, %need_to_clamp_negative
  %R = select i1 %dont_need_to_clamp, i32 %X, i32 %clamp_limit
  ret i32 %R
}