File: bstrins_d.ll

package info (click to toggle)
llvm-toolchain-17 1%3A17.0.6-22
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,799,624 kB
  • sloc: cpp: 6,428,607; ansic: 1,383,196; asm: 793,408; python: 223,504; objc: 75,364; f90: 60,502; lisp: 33,869; pascal: 15,282; sh: 9,684; perl: 7,453; ml: 4,937; awk: 3,523; makefile: 2,889; javascript: 2,149; xml: 888; fortran: 619; cs: 573
file content (207 lines) | stat: -rw-r--r-- 6,277 bytes parent folder | download | duplicates (5)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc --mtriple=loongarch64 < %s | FileCheck %s

;; Test generation of the bstrins.d instruction.
;; There are 8 patterns that can be matched to bstrins.d. See performORCombine
;; for details.

;; Pattern 1
;; R = or (and X, mask0), (and (shl Y, lsb), mask1)
;; =>
;; R = BSTRINS X, Y, msb, lsb
define i64 @pat1(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: pat1:
; CHECK:       # %bb.0:
; CHECK-NEXT:    bstrins.d $a0, $a1, 39, 16
; CHECK-NEXT:    ret
  %and1 = and i64 %a, -1099511562241  ; 0xffffff000000ffff
  %shl = shl i64 %b, 16
  %and2 = and i64 %shl, 1099511562240 ; 0x000000ffffff0000
  %or = or i64 %and1, %and2
  ret i64 %or
}

define i64 @pat1_swap(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: pat1_swap:
; CHECK:       # %bb.0:
; CHECK-NEXT:    bstrins.d $a0, $a1, 39, 16
; CHECK-NEXT:    ret
  %and1 = and i64 %a, -1099511562241  ; 0xffffff000000ffff
  %shl = shl i64 %b, 16
  %and2 = and i64 %shl, 1099511562240 ; 0x000000ffffff0000
  %or = or i64 %and2, %and1
  ret i64 %or
}

;; Pattern 2
;; R = or (and X, mask0), (shl (and Y, mask1), lsb)
;; =>
;; R = BSTRINS X, Y, msb, lsb
define i64 @pat2(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: pat2:
; CHECK:       # %bb.0:
; CHECK-NEXT:    bstrins.d $a0, $a1, 39, 16
; CHECK-NEXT:    ret
  %and1 = and i64 %a, -1099511562241 ; 0xffffff000000ffff
  %and2 = and i64 %b, 16777215       ; 0x0000000000ffffff
  %shl = shl i64 %and2, 16
  %or = or i64 %and1, %shl
  ret i64 %or
}

define i64 @pat2_swap(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: pat2_swap:
; CHECK:       # %bb.0:
; CHECK-NEXT:    bstrins.d $a0, $a1, 39, 16
; CHECK-NEXT:    ret
  %and1 = and i64 %a, -1099511562241 ; 0xffffff000000ffff
  %and2 = and i64 %b, 16777215       ; 0x0000000000ffffff
  %shl = shl i64 %and2, 16
  %or = or i64 %shl, %and1
  ret i64 %or
}

;; Pattern 3
;; R = or (and X, mask0), (and Y, mask1)
;; =>
;; R = BSTRINS X, (srl (and Y, mask1), lsb), msb, lsb
define i64 @pat3(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: pat3:
; CHECK:       # %bb.0:
; CHECK-NEXT:    andi $a1, $a1, 288
; CHECK-NEXT:    srli.d $a1, $a1, 4
; CHECK-NEXT:    bstrins.d $a0, $a1, 11, 4
; CHECK-NEXT:    ret
  %and1 = and i64 %a, -4081 ; 0xfffffffffffff00f
  %and2 = and i64 %b, 288   ; 0x0000000000000120
  %or = or i64 %and1, %and2
  ret i64 %or
}

define i64 @pat3_swap(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: pat3_swap:
; CHECK:       # %bb.0:
; CHECK-NEXT:    andi $a1, $a1, 288
; CHECK-NEXT:    srli.d $a1, $a1, 4
; CHECK-NEXT:    bstrins.d $a0, $a1, 11, 4
; CHECK-NEXT:    ret
  %and1 = and i64 %a, -4081 ; 0xfffffffffffff00f
  %and2 = and i64 %b, 288   ; 0x0000000000000120
  %or = or i64 %and2, %and1
  ret i64 %or
}

;; Pattern 4
;; R = or (and X, mask), (shl Y, shamt)
;; =>
;; R = BSTRINS X, Y, 63, shamt
define i64 @pat4(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: pat4:
; CHECK:       # %bb.0:
; CHECK-NEXT:    bstrins.d $a0, $a1, 63, 8
; CHECK-NEXT:    ret
  %and = and i64 %a, 255
  %shl = shl i64 %b, 8
  %or = or i64 %and, %shl
  ret i64 %or
}

define i64 @pat4_swap(i64 %a, i64 %b) nounwind {
; CHECK-LABEL: pat4_swap:
; CHECK:       # %bb.0:
; CHECK-NEXT:    bstrins.d $a0, $a1, 63, 8
; CHECK-NEXT:    ret
  %and = and i64 %a, 255
  %shl = shl i64 %b, 8
  %or = or i64 %shl, %and
  ret i64 %or
}

;; Pattern 5
;; R = or (and X, mask0), const
;; =>
;; R = BSTRINS X, (const >> lsb), msb, lsb
define i64 @pat5(i64 %a) nounwind {
; CHECK-LABEL: pat5:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lu12i.w $a1, 74565
; CHECK-NEXT:    ori $a1, $a1, 1656
; CHECK-NEXT:    bstrins.d $a0, $a1, 47, 16
; CHECK-NEXT:    ret
  %and = and i64 %a, 18446462598732906495 ; 0xffff00000000ffff
  %or = or i64 %and, 20015998304256       ; 0x0000123456780000
  ret i64 %or
}

;; Pattern 6: a = b | ((c & mask) << shamt)
;; In this testcase b is 0x123456000000789a, but in fact we do not require b
;; being a constant. As long as all positions in b to be overwritten by the
;; incoming bits are known to be zero, the pattern could be matched.
define i64 @pat6(i64 %c) nounwind {
; CHECK-LABEL: pat6:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lu12i.w $a1, 7
; CHECK-NEXT:    ori $a1, $a1, 2202
; CHECK-NEXT:    lu32i.d $a1, 284160
; CHECK-NEXT:    lu52i.d $a1, $a1, 291
; CHECK-NEXT:    bstrins.d $a1, $a0, 39, 16
; CHECK-NEXT:    move $a0, $a1
; CHECK-NEXT:    ret
  %and = and i64 %c, 16777215            ; 0x0000000000ffffff
  %shl = shl i64 %and, 16
  %or = or i64 %shl, 1311767949471676570 ; 0x123456000000789a
  ret i64 %or
}

;; Pattern 7: a = b | ((c << shamt) & shifted_mask)
;; Similar to pattern 6.
define i64 @pat7(i64 %c) nounwind {
; CHECK-LABEL: pat7:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lu12i.w $a1, 7
; CHECK-NEXT:    ori $a1, $a1, 2202
; CHECK-NEXT:    lu32i.d $a1, 284160
; CHECK-NEXT:    lu52i.d $a1, $a1, 291
; CHECK-NEXT:    bstrins.d $a1, $a0, 39, 16
; CHECK-NEXT:    move $a0, $a1
; CHECK-NEXT:    ret
  %shl = shl i64 %c, 16
  %and = and i64 %shl, 1099511562240     ; 0x000000ffffff0000
  %or = or i64 %and, 1311767949471676570 ; 0x123456000000789a
  ret i64 %or
}

;; Pattern 8: a = b | (c & shifted_mask)
;; Similar to pattern 7 but without shift to c.
define i64 @pat8(i64 %c) nounwind {
; CHECK-LABEL: pat8:
; CHECK:       # %bb.0:
; CHECK-NEXT:    srli.d $a1, $a0, 16
; CHECK-NEXT:    lu12i.w $a0, 7
; CHECK-NEXT:    ori $a0, $a0, 2202
; CHECK-NEXT:    lu32i.d $a0, 284160
; CHECK-NEXT:    lu52i.d $a0, $a0, 291
; CHECK-NEXT:    bstrins.d $a0, $a1, 39, 16
; CHECK-NEXT:    ret
  %and = and i64 %c, 1099511562240       ; 0x000000ffffff0000
  %or = or i64 %and, 1311767949471676570 ; 0x123456000000789a
  ret i64 %or
}

;; Test that bstrins.d is not generated because constant OR operand
;; doesn't fit into bits cleared by constant AND operand.
define i64 @no_bstrins_d(i64 %a) nounwind {
; CHECK-LABEL: no_bstrins_d:
; CHECK:       # %bb.0:
; CHECK-NEXT:    lu12i.w $a1, 354185
; CHECK-NEXT:    lu32i.d $a1, 4660
; CHECK-NEXT:    or $a0, $a0, $a1
; CHECK-NEXT:    lu12i.w $a1, 354191
; CHECK-NEXT:    ori $a1, $a1, 4095
; CHECK-NEXT:    lu32i.d $a1, -60876
; CHECK-NEXT:    and $a0, $a0, $a1
; CHECK-NEXT:    ret
  %and = and i64 %a, 18446462598732906495 ; 0xffff00000000ffff
  %or = or i64 %and, 20015998341120       ; 0x0000123456789000
  ret i64 %or
}