File: | build/source/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp |
Warning: | line 197, column 7 1st function call argument is an uninitialized value |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- InstCombineShifts.cpp ----------------------------------------------===// | ||||
2 | // | ||||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||||
4 | // See https://llvm.org/LICENSE.txt for license information. | ||||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||||
6 | // | ||||
7 | //===----------------------------------------------------------------------===// | ||||
8 | // | ||||
9 | // This file implements the visitShl, visitLShr, and visitAShr functions. | ||||
10 | // | ||||
11 | //===----------------------------------------------------------------------===// | ||||
12 | |||||
13 | #include "InstCombineInternal.h" | ||||
14 | #include "llvm/Analysis/InstructionSimplify.h" | ||||
15 | #include "llvm/IR/IntrinsicInst.h" | ||||
16 | #include "llvm/IR/PatternMatch.h" | ||||
17 | #include "llvm/Transforms/InstCombine/InstCombiner.h" | ||||
18 | using namespace llvm; | ||||
19 | using namespace PatternMatch; | ||||
20 | |||||
21 | #define DEBUG_TYPE"instcombine" "instcombine" | ||||
22 | |||||
23 | bool canTryToConstantAddTwoShiftAmounts(Value *Sh0, Value *ShAmt0, Value *Sh1, | ||||
24 | Value *ShAmt1) { | ||||
25 | // We have two shift amounts from two different shifts. The types of those | ||||
26 | // shift amounts may not match. If that's the case let's bailout now.. | ||||
27 | if (ShAmt0->getType() != ShAmt1->getType()) | ||||
28 | return false; | ||||
29 | |||||
30 | // As input, we have the following pattern: | ||||
31 | // Sh0 (Sh1 X, Q), K | ||||
32 | // We want to rewrite that as: | ||||
33 | // Sh x, (Q+K) iff (Q+K) u< bitwidth(x) | ||||
34 | // While we know that originally (Q+K) would not overflow | ||||
35 | // (because 2 * (N-1) u<= iN -1), we have looked past extensions of | ||||
36 | // shift amounts. so it may now overflow in smaller bitwidth. | ||||
37 | // To ensure that does not happen, we need to ensure that the total maximal | ||||
38 | // shift amount is still representable in that smaller bit width. | ||||
39 | unsigned MaximalPossibleTotalShiftAmount = | ||||
40 | (Sh0->getType()->getScalarSizeInBits() - 1) + | ||||
41 | (Sh1->getType()->getScalarSizeInBits() - 1); | ||||
42 | APInt MaximalRepresentableShiftAmount = | ||||
43 | APInt::getAllOnes(ShAmt0->getType()->getScalarSizeInBits()); | ||||
44 | return MaximalRepresentableShiftAmount.uge(MaximalPossibleTotalShiftAmount); | ||||
45 | } | ||||
46 | |||||
47 | // Given pattern: | ||||
48 | // (x shiftopcode Q) shiftopcode K | ||||
49 | // we should rewrite it as | ||||
50 | // x shiftopcode (Q+K) iff (Q+K) u< bitwidth(x) and | ||||
51 | // | ||||
52 | // This is valid for any shift, but they must be identical, and we must be | ||||
53 | // careful in case we have (zext(Q)+zext(K)) and look past extensions, | ||||
54 | // (Q+K) must not overflow or else (Q+K) u< bitwidth(x) is bogus. | ||||
55 | // | ||||
56 | // AnalyzeForSignBitExtraction indicates that we will only analyze whether this | ||||
57 | // pattern has any 2 right-shifts that sum to 1 less than original bit width. | ||||
58 | Value *InstCombinerImpl::reassociateShiftAmtsOfTwoSameDirectionShifts( | ||||
59 | BinaryOperator *Sh0, const SimplifyQuery &SQ, | ||||
60 | bool AnalyzeForSignBitExtraction) { | ||||
61 | // Look for a shift of some instruction, ignore zext of shift amount if any. | ||||
62 | Instruction *Sh0Op0; | ||||
63 | Value *ShAmt0; | ||||
64 | if (!match(Sh0, | ||||
65 | m_Shift(m_Instruction(Sh0Op0), m_ZExtOrSelf(m_Value(ShAmt0))))) | ||||
66 | return nullptr; | ||||
67 | |||||
68 | // If there is a truncation between the two shifts, we must make note of it | ||||
69 | // and look through it. The truncation imposes additional constraints on the | ||||
70 | // transform. | ||||
71 | Instruction *Sh1; | ||||
72 | Value *Trunc = nullptr; | ||||
73 | match(Sh0Op0, | ||||
74 | m_CombineOr(m_CombineAnd(m_Trunc(m_Instruction(Sh1)), m_Value(Trunc)), | ||||
75 | m_Instruction(Sh1))); | ||||
76 | |||||
77 | // Inner shift: (x shiftopcode ShAmt1) | ||||
78 | // Like with other shift, ignore zext of shift amount if any. | ||||
79 | Value *X, *ShAmt1; | ||||
80 | if (!match(Sh1, m_Shift(m_Value(X), m_ZExtOrSelf(m_Value(ShAmt1))))) | ||||
81 | return nullptr; | ||||
82 | |||||
83 | // Verify that it would be safe to try to add those two shift amounts. | ||||
84 | if (!canTryToConstantAddTwoShiftAmounts(Sh0, ShAmt0, Sh1, ShAmt1)) | ||||
85 | return nullptr; | ||||
86 | |||||
87 | // We are only looking for signbit extraction if we have two right shifts. | ||||
88 | bool HadTwoRightShifts = match(Sh0, m_Shr(m_Value(), m_Value())) && | ||||
89 | match(Sh1, m_Shr(m_Value(), m_Value())); | ||||
90 | // ... and if it's not two right-shifts, we know the answer already. | ||||
91 | if (AnalyzeForSignBitExtraction && !HadTwoRightShifts) | ||||
92 | return nullptr; | ||||
93 | |||||
94 | // The shift opcodes must be identical, unless we are just checking whether | ||||
95 | // this pattern can be interpreted as a sign-bit-extraction. | ||||
96 | Instruction::BinaryOps ShiftOpcode = Sh0->getOpcode(); | ||||
97 | bool IdenticalShOpcodes = Sh0->getOpcode() == Sh1->getOpcode(); | ||||
98 | if (!IdenticalShOpcodes && !AnalyzeForSignBitExtraction) | ||||
99 | return nullptr; | ||||
100 | |||||
101 | // If we saw truncation, we'll need to produce extra instruction, | ||||
102 | // and for that one of the operands of the shift must be one-use, | ||||
103 | // unless of course we don't actually plan to produce any instructions here. | ||||
104 | if (Trunc && !AnalyzeForSignBitExtraction && | ||||
105 | !match(Sh0, m_c_BinOp(m_OneUse(m_Value()), m_Value()))) | ||||
106 | return nullptr; | ||||
107 | |||||
108 | // Can we fold (ShAmt0+ShAmt1) ? | ||||
109 | auto *NewShAmt = dyn_cast_or_null<Constant>( | ||||
110 | simplifyAddInst(ShAmt0, ShAmt1, /*isNSW=*/false, /*isNUW=*/false, | ||||
111 | SQ.getWithInstruction(Sh0))); | ||||
112 | if (!NewShAmt) | ||||
113 | return nullptr; // Did not simplify. | ||||
114 | unsigned NewShAmtBitWidth = NewShAmt->getType()->getScalarSizeInBits(); | ||||
115 | unsigned XBitWidth = X->getType()->getScalarSizeInBits(); | ||||
116 | // Is the new shift amount smaller than the bit width of inner/new shift? | ||||
117 | if (!match(NewShAmt, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT, | ||||
118 | APInt(NewShAmtBitWidth, XBitWidth)))) | ||||
119 | return nullptr; // FIXME: could perform constant-folding. | ||||
120 | |||||
121 | // If there was a truncation, and we have a right-shift, we can only fold if | ||||
122 | // we are left with the original sign bit. Likewise, if we were just checking | ||||
123 | // that this is a sighbit extraction, this is the place to check it. | ||||
124 | // FIXME: zero shift amount is also legal here, but we can't *easily* check | ||||
125 | // more than one predicate so it's not really worth it. | ||||
126 | if (HadTwoRightShifts && (Trunc || AnalyzeForSignBitExtraction)) { | ||||
127 | // If it's not a sign bit extraction, then we're done. | ||||
128 | if (!match(NewShAmt, | ||||
129 | m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, | ||||
130 | APInt(NewShAmtBitWidth, XBitWidth - 1)))) | ||||
131 | return nullptr; | ||||
132 | // If it is, and that was the question, return the base value. | ||||
133 | if (AnalyzeForSignBitExtraction) | ||||
134 | return X; | ||||
135 | } | ||||
136 | |||||
137 | assert(IdenticalShOpcodes && "Should not get here with different shifts.")(static_cast <bool> (IdenticalShOpcodes && "Should not get here with different shifts." ) ? void (0) : __assert_fail ("IdenticalShOpcodes && \"Should not get here with different shifts.\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 137 , __extension__ __PRETTY_FUNCTION__)); | ||||
138 | |||||
139 | // All good, we can do this fold. | ||||
140 | NewShAmt = ConstantExpr::getZExtOrBitCast(NewShAmt, X->getType()); | ||||
141 | |||||
142 | BinaryOperator *NewShift = BinaryOperator::Create(ShiftOpcode, X, NewShAmt); | ||||
143 | |||||
144 | // The flags can only be propagated if there wasn't a trunc. | ||||
145 | if (!Trunc) { | ||||
146 | // If the pattern did not involve trunc, and both of the original shifts | ||||
147 | // had the same flag set, preserve the flag. | ||||
148 | if (ShiftOpcode == Instruction::BinaryOps::Shl) { | ||||
149 | NewShift->setHasNoUnsignedWrap(Sh0->hasNoUnsignedWrap() && | ||||
150 | Sh1->hasNoUnsignedWrap()); | ||||
151 | NewShift->setHasNoSignedWrap(Sh0->hasNoSignedWrap() && | ||||
152 | Sh1->hasNoSignedWrap()); | ||||
153 | } else { | ||||
154 | NewShift->setIsExact(Sh0->isExact() && Sh1->isExact()); | ||||
155 | } | ||||
156 | } | ||||
157 | |||||
158 | Instruction *Ret = NewShift; | ||||
159 | if (Trunc) { | ||||
160 | Builder.Insert(NewShift); | ||||
161 | Ret = CastInst::Create(Instruction::Trunc, NewShift, Sh0->getType()); | ||||
162 | } | ||||
163 | |||||
164 | return Ret; | ||||
165 | } | ||||
166 | |||||
167 | // If we have some pattern that leaves only some low bits set, and then performs | ||||
168 | // left-shift of those bits, if none of the bits that are left after the final | ||||
169 | // shift are modified by the mask, we can omit the mask. | ||||
170 | // | ||||
171 | // There are many variants to this pattern: | ||||
172 | // a) (x & ((1 << MaskShAmt) - 1)) << ShiftShAmt | ||||
173 | // b) (x & (~(-1 << MaskShAmt))) << ShiftShAmt | ||||
174 | // c) (x & (-1 l>> MaskShAmt)) << ShiftShAmt | ||||
175 | // d) (x & ((-1 << MaskShAmt) l>> MaskShAmt)) << ShiftShAmt | ||||
176 | // e) ((x << MaskShAmt) l>> MaskShAmt) << ShiftShAmt | ||||
177 | // f) ((x << MaskShAmt) a>> MaskShAmt) << ShiftShAmt | ||||
178 | // All these patterns can be simplified to just: | ||||
179 | // x << ShiftShAmt | ||||
180 | // iff: | ||||
181 | // a,b) (MaskShAmt+ShiftShAmt) u>= bitwidth(x) | ||||
182 | // c,d,e,f) (ShiftShAmt-MaskShAmt) s>= 0 (i.e. ShiftShAmt u>= MaskShAmt) | ||||
183 | static Instruction * | ||||
184 | dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift, | ||||
185 | const SimplifyQuery &Q, | ||||
186 | InstCombiner::BuilderTy &Builder) { | ||||
187 | assert(OuterShift->getOpcode() == Instruction::BinaryOps::Shl &&(static_cast <bool> (OuterShift->getOpcode() == Instruction ::BinaryOps::Shl && "The input must be 'shl'!") ? void (0) : __assert_fail ("OuterShift->getOpcode() == Instruction::BinaryOps::Shl && \"The input must be 'shl'!\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 188 , __extension__ __PRETTY_FUNCTION__)) | ||||
188 | "The input must be 'shl'!")(static_cast <bool> (OuterShift->getOpcode() == Instruction ::BinaryOps::Shl && "The input must be 'shl'!") ? void (0) : __assert_fail ("OuterShift->getOpcode() == Instruction::BinaryOps::Shl && \"The input must be 'shl'!\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 188 , __extension__ __PRETTY_FUNCTION__)); | ||||
189 | |||||
190 | Value *Masked, *ShiftShAmt; | ||||
191 | match(OuterShift, | ||||
| |||||
| |||||
192 | m_Shift(m_Value(Masked), m_ZExtOrSelf(m_Value(ShiftShAmt)))); | ||||
193 | |||||
194 | // *If* there is a truncation between an outer shift and a possibly-mask, | ||||
195 | // then said truncation *must* be one-use, else we can't perform the fold. | ||||
196 | Value *Trunc; | ||||
197 | if (match(Masked, m_CombineAnd(m_Trunc(m_Value(Masked)), m_Value(Trunc))) && | ||||
| |||||
198 | !Trunc->hasOneUse()) | ||||
199 | return nullptr; | ||||
200 | |||||
201 | Type *NarrowestTy = OuterShift->getType(); | ||||
202 | Type *WidestTy = Masked->getType(); | ||||
203 | bool HadTrunc = WidestTy != NarrowestTy; | ||||
204 | |||||
205 | // The mask must be computed in a type twice as wide to ensure | ||||
206 | // that no bits are lost if the sum-of-shifts is wider than the base type. | ||||
207 | Type *ExtendedTy = WidestTy->getExtendedType(); | ||||
208 | |||||
209 | Value *MaskShAmt; | ||||
210 | |||||
211 | // ((1 << MaskShAmt) - 1) | ||||
212 | auto MaskA = m_Add(m_Shl(m_One(), m_Value(MaskShAmt)), m_AllOnes()); | ||||
213 | // (~(-1 << maskNbits)) | ||||
214 | auto MaskB = m_Xor(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_AllOnes()); | ||||
215 | // (-1 l>> MaskShAmt) | ||||
216 | auto MaskC = m_LShr(m_AllOnes(), m_Value(MaskShAmt)); | ||||
217 | // ((-1 << MaskShAmt) l>> MaskShAmt) | ||||
218 | auto MaskD = | ||||
219 | m_LShr(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_Deferred(MaskShAmt)); | ||||
220 | |||||
221 | Value *X; | ||||
222 | Constant *NewMask; | ||||
223 | |||||
224 | if (match(Masked, m_c_And(m_CombineOr(MaskA, MaskB), m_Value(X)))) { | ||||
225 | // Peek through an optional zext of the shift amount. | ||||
226 | match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt))); | ||||
227 | |||||
228 | // Verify that it would be safe to try to add those two shift amounts. | ||||
229 | if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked, | ||||
230 | MaskShAmt)) | ||||
231 | return nullptr; | ||||
232 | |||||
233 | // Can we simplify (MaskShAmt+ShiftShAmt) ? | ||||
234 | auto *SumOfShAmts = dyn_cast_or_null<Constant>(simplifyAddInst( | ||||
235 | MaskShAmt, ShiftShAmt, /*IsNSW=*/false, /*IsNUW=*/false, Q)); | ||||
236 | if (!SumOfShAmts) | ||||
237 | return nullptr; // Did not simplify. | ||||
238 | // In this pattern SumOfShAmts correlates with the number of low bits | ||||
239 | // that shall remain in the root value (OuterShift). | ||||
240 | |||||
241 | // An extend of an undef value becomes zero because the high bits are never | ||||
242 | // completely unknown. Replace the `undef` shift amounts with final | ||||
243 | // shift bitwidth to ensure that the value remains undef when creating the | ||||
244 | // subsequent shift op. | ||||
245 | SumOfShAmts = Constant::replaceUndefsWith( | ||||
246 | SumOfShAmts, ConstantInt::get(SumOfShAmts->getType()->getScalarType(), | ||||
247 | ExtendedTy->getScalarSizeInBits())); | ||||
248 | auto *ExtendedSumOfShAmts = ConstantExpr::getZExt(SumOfShAmts, ExtendedTy); | ||||
249 | // And compute the mask as usual: ~(-1 << (SumOfShAmts)) | ||||
250 | auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy); | ||||
251 | auto *ExtendedInvertedMask = | ||||
252 | ConstantExpr::getShl(ExtendedAllOnes, ExtendedSumOfShAmts); | ||||
253 | NewMask = ConstantExpr::getNot(ExtendedInvertedMask); | ||||
254 | } else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X))) || | ||||
255 | match(Masked, m_Shr(m_Shl(m_Value(X), m_Value(MaskShAmt)), | ||||
256 | m_Deferred(MaskShAmt)))) { | ||||
257 | // Peek through an optional zext of the shift amount. | ||||
258 | match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt))); | ||||
259 | |||||
260 | // Verify that it would be safe to try to add those two shift amounts. | ||||
261 | if (!canTryToConstantAddTwoShiftAmounts(OuterShift, ShiftShAmt, Masked, | ||||
262 | MaskShAmt)) | ||||
263 | return nullptr; | ||||
264 | |||||
265 | // Can we simplify (ShiftShAmt-MaskShAmt) ? | ||||
266 | auto *ShAmtsDiff = dyn_cast_or_null<Constant>(simplifySubInst( | ||||
267 | ShiftShAmt, MaskShAmt, /*IsNSW=*/false, /*IsNUW=*/false, Q)); | ||||
268 | if (!ShAmtsDiff) | ||||
269 | return nullptr; // Did not simplify. | ||||
270 | // In this pattern ShAmtsDiff correlates with the number of high bits that | ||||
271 | // shall be unset in the root value (OuterShift). | ||||
272 | |||||
273 | // An extend of an undef value becomes zero because the high bits are never | ||||
274 | // completely unknown. Replace the `undef` shift amounts with negated | ||||
275 | // bitwidth of innermost shift to ensure that the value remains undef when | ||||
276 | // creating the subsequent shift op. | ||||
277 | unsigned WidestTyBitWidth = WidestTy->getScalarSizeInBits(); | ||||
278 | ShAmtsDiff = Constant::replaceUndefsWith( | ||||
279 | ShAmtsDiff, ConstantInt::get(ShAmtsDiff->getType()->getScalarType(), | ||||
280 | -WidestTyBitWidth)); | ||||
281 | auto *ExtendedNumHighBitsToClear = ConstantExpr::getZExt( | ||||
282 | ConstantExpr::getSub(ConstantInt::get(ShAmtsDiff->getType(), | ||||
283 | WidestTyBitWidth, | ||||
284 | /*isSigned=*/false), | ||||
285 | ShAmtsDiff), | ||||
286 | ExtendedTy); | ||||
287 | // And compute the mask as usual: (-1 l>> (NumHighBitsToClear)) | ||||
288 | auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy); | ||||
289 | NewMask = | ||||
290 | ConstantExpr::getLShr(ExtendedAllOnes, ExtendedNumHighBitsToClear); | ||||
291 | } else | ||||
292 | return nullptr; // Don't know anything about this pattern. | ||||
293 | |||||
294 | NewMask = ConstantExpr::getTrunc(NewMask, NarrowestTy); | ||||
295 | |||||
296 | // Does this mask has any unset bits? If not then we can just not apply it. | ||||
297 | bool NeedMask = !match(NewMask, m_AllOnes()); | ||||
298 | |||||
299 | // If we need to apply a mask, there are several more restrictions we have. | ||||
300 | if (NeedMask) { | ||||
301 | // The old masking instruction must go away. | ||||
302 | if (!Masked->hasOneUse()) | ||||
303 | return nullptr; | ||||
304 | // The original "masking" instruction must not have been`ashr`. | ||||
305 | if (match(Masked, m_AShr(m_Value(), m_Value()))) | ||||
306 | return nullptr; | ||||
307 | } | ||||
308 | |||||
309 | // If we need to apply truncation, let's do it first, since we can. | ||||
310 | // We have already ensured that the old truncation will go away. | ||||
311 | if (HadTrunc) | ||||
312 | X = Builder.CreateTrunc(X, NarrowestTy); | ||||
313 | |||||
314 | // No 'NUW'/'NSW'! We no longer know that we won't shift-out non-0 bits. | ||||
315 | // We didn't change the Type of this outermost shift, so we can just do it. | ||||
316 | auto *NewShift = BinaryOperator::Create(OuterShift->getOpcode(), X, | ||||
317 | OuterShift->getOperand(1)); | ||||
318 | if (!NeedMask) | ||||
319 | return NewShift; | ||||
320 | |||||
321 | Builder.Insert(NewShift); | ||||
322 | return BinaryOperator::Create(Instruction::And, NewShift, NewMask); | ||||
323 | } | ||||
324 | |||||
325 | /// If we have a shift-by-constant of a bin op (bitwise logic op or add/sub w/ | ||||
326 | /// shl) that itself has a shift-by-constant operand with identical opcode, we | ||||
327 | /// may be able to convert that into 2 independent shifts followed by the logic | ||||
328 | /// op. This eliminates a use of an intermediate value (reduces dependency | ||||
329 | /// chain). | ||||
330 | static Instruction *foldShiftOfShiftedBinOp(BinaryOperator &I, | ||||
331 | InstCombiner::BuilderTy &Builder) { | ||||
332 | assert(I.isShift() && "Expected a shift as input")(static_cast <bool> (I.isShift() && "Expected a shift as input" ) ? void (0) : __assert_fail ("I.isShift() && \"Expected a shift as input\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 332 , __extension__ __PRETTY_FUNCTION__)); | ||||
333 | auto *BinInst = dyn_cast<BinaryOperator>(I.getOperand(0)); | ||||
334 | if (!BinInst || | ||||
335 | (!BinInst->isBitwiseLogicOp() && | ||||
336 | BinInst->getOpcode() != Instruction::Add && | ||||
337 | BinInst->getOpcode() != Instruction::Sub) || | ||||
338 | !BinInst->hasOneUse()) | ||||
339 | return nullptr; | ||||
340 | |||||
341 | Constant *C0, *C1; | ||||
342 | if (!match(I.getOperand(1), m_Constant(C1))) | ||||
343 | return nullptr; | ||||
344 | |||||
345 | Instruction::BinaryOps ShiftOpcode = I.getOpcode(); | ||||
346 | // Transform for add/sub only works with shl. | ||||
347 | if ((BinInst->getOpcode() == Instruction::Add || | ||||
348 | BinInst->getOpcode() == Instruction::Sub) && | ||||
349 | ShiftOpcode != Instruction::Shl) | ||||
350 | return nullptr; | ||||
351 | |||||
352 | Type *Ty = I.getType(); | ||||
353 | |||||
354 | // Find a matching one-use shift by constant. The fold is not valid if the sum | ||||
355 | // of the shift values equals or exceeds bitwidth. | ||||
356 | // TODO: Remove the one-use check if the other logic operand (Y) is constant. | ||||
357 | Value *X, *Y; | ||||
358 | auto matchFirstShift = [&](Value *V) { | ||||
359 | APInt Threshold(Ty->getScalarSizeInBits(), Ty->getScalarSizeInBits()); | ||||
360 | return match(V, | ||||
361 | m_OneUse(m_BinOp(ShiftOpcode, m_Value(X), m_Constant(C0)))) && | ||||
362 | match(ConstantExpr::getAdd(C0, C1), | ||||
363 | m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold)); | ||||
364 | }; | ||||
365 | |||||
366 | // Logic ops and Add are commutative, so check each operand for a match. Sub | ||||
367 | // is not so we cannot reoder if we match operand(1) and need to keep the | ||||
368 | // operands in their original positions. | ||||
369 | bool FirstShiftIsOp1 = false; | ||||
370 | if (matchFirstShift(BinInst->getOperand(0))) | ||||
371 | Y = BinInst->getOperand(1); | ||||
372 | else if (matchFirstShift(BinInst->getOperand(1))) { | ||||
373 | Y = BinInst->getOperand(0); | ||||
374 | FirstShiftIsOp1 = BinInst->getOpcode() == Instruction::Sub; | ||||
375 | } else | ||||
376 | return nullptr; | ||||
377 | |||||
378 | // shift (binop (shift X, C0), Y), C1 -> binop (shift X, C0+C1), (shift Y, C1) | ||||
379 | Constant *ShiftSumC = ConstantExpr::getAdd(C0, C1); | ||||
380 | Value *NewShift1 = Builder.CreateBinOp(ShiftOpcode, X, ShiftSumC); | ||||
381 | Value *NewShift2 = Builder.CreateBinOp(ShiftOpcode, Y, C1); | ||||
382 | Value *Op1 = FirstShiftIsOp1 ? NewShift2 : NewShift1; | ||||
383 | Value *Op2 = FirstShiftIsOp1 ? NewShift1 : NewShift2; | ||||
384 | return BinaryOperator::Create(BinInst->getOpcode(), Op1, Op2); | ||||
385 | } | ||||
386 | |||||
387 | Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) { | ||||
388 | if (Instruction *Phi = foldBinopWithPhiOperands(I)) | ||||
389 | return Phi; | ||||
390 | |||||
391 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
392 | assert(Op0->getType() == Op1->getType())(static_cast <bool> (Op0->getType() == Op1->getType ()) ? void (0) : __assert_fail ("Op0->getType() == Op1->getType()" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 392 , __extension__ __PRETTY_FUNCTION__)); | ||||
393 | Type *Ty = I.getType(); | ||||
394 | |||||
395 | // If the shift amount is a one-use `sext`, we can demote it to `zext`. | ||||
396 | Value *Y; | ||||
397 | if (match(Op1, m_OneUse(m_SExt(m_Value(Y))))) { | ||||
398 | Value *NewExt = Builder.CreateZExt(Y, Ty, Op1->getName()); | ||||
399 | return BinaryOperator::Create(I.getOpcode(), Op0, NewExt); | ||||
400 | } | ||||
401 | |||||
402 | // See if we can fold away this shift. | ||||
403 | if (SimplifyDemandedInstructionBits(I)) | ||||
404 | return &I; | ||||
405 | |||||
406 | // Try to fold constant and into select arguments. | ||||
407 | if (isa<Constant>(Op0)) | ||||
408 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) | ||||
409 | if (Instruction *R = FoldOpIntoSelect(I, SI)) | ||||
410 | return R; | ||||
411 | |||||
412 | if (Constant *CUI = dyn_cast<Constant>(Op1)) | ||||
413 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) | ||||
414 | return Res; | ||||
415 | |||||
416 | if (auto *NewShift = cast_or_null<Instruction>( | ||||
417 | reassociateShiftAmtsOfTwoSameDirectionShifts(&I, SQ))) | ||||
418 | return NewShift; | ||||
419 | |||||
420 | // Pre-shift a constant shifted by a variable amount with constant offset: | ||||
421 | // C shift (A add nuw C1) --> (C shift C1) shift A | ||||
422 | Value *A; | ||||
423 | Constant *C, *C1; | ||||
424 | if (match(Op0, m_Constant(C)) && | ||||
425 | match(Op1, m_NUWAdd(m_Value(A), m_Constant(C1)))) { | ||||
426 | Value *NewC = Builder.CreateBinOp(I.getOpcode(), C, C1); | ||||
427 | return BinaryOperator::Create(I.getOpcode(), NewC, A); | ||||
428 | } | ||||
429 | |||||
430 | unsigned BitWidth = Ty->getScalarSizeInBits(); | ||||
431 | |||||
432 | const APInt *AC, *AddC; | ||||
433 | // Try to pre-shift a constant shifted by a variable amount added with a | ||||
434 | // negative number: | ||||
435 | // C << (X - AddC) --> (C >> AddC) << X | ||||
436 | // and | ||||
437 | // C >> (X - AddC) --> (C << AddC) >> X | ||||
438 | if (match(Op0, m_APInt(AC)) && match(Op1, m_Add(m_Value(A), m_APInt(AddC))) && | ||||
439 | AddC->isNegative() && (-*AddC).ult(BitWidth)) { | ||||
440 | assert(!AC->isZero() && "Expected simplify of shifted zero")(static_cast <bool> (!AC->isZero() && "Expected simplify of shifted zero" ) ? void (0) : __assert_fail ("!AC->isZero() && \"Expected simplify of shifted zero\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 440 , __extension__ __PRETTY_FUNCTION__)); | ||||
441 | unsigned PosOffset = (-*AddC).getZExtValue(); | ||||
442 | |||||
443 | auto isSuitableForPreShift = [PosOffset, &I, AC]() { | ||||
444 | switch (I.getOpcode()) { | ||||
445 | default: | ||||
446 | return false; | ||||
447 | case Instruction::Shl: | ||||
448 | return (I.hasNoSignedWrap() || I.hasNoUnsignedWrap()) && | ||||
449 | AC->eq(AC->lshr(PosOffset).shl(PosOffset)); | ||||
450 | case Instruction::LShr: | ||||
451 | return I.isExact() && AC->eq(AC->shl(PosOffset).lshr(PosOffset)); | ||||
452 | case Instruction::AShr: | ||||
453 | return I.isExact() && AC->eq(AC->shl(PosOffset).ashr(PosOffset)); | ||||
454 | } | ||||
455 | }; | ||||
456 | if (isSuitableForPreShift()) { | ||||
457 | Constant *NewC = ConstantInt::get(Ty, I.getOpcode() == Instruction::Shl | ||||
458 | ? AC->lshr(PosOffset) | ||||
459 | : AC->shl(PosOffset)); | ||||
460 | BinaryOperator *NewShiftOp = | ||||
461 | BinaryOperator::Create(I.getOpcode(), NewC, A); | ||||
462 | if (I.getOpcode() == Instruction::Shl) { | ||||
463 | NewShiftOp->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); | ||||
464 | } else { | ||||
465 | NewShiftOp->setIsExact(); | ||||
466 | } | ||||
467 | return NewShiftOp; | ||||
468 | } | ||||
469 | } | ||||
470 | |||||
471 | // X shift (A srem C) -> X shift (A and (C - 1)) iff C is a power of 2. | ||||
472 | // Because shifts by negative values (which could occur if A were negative) | ||||
473 | // are undefined. | ||||
474 | if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Constant(C))) && | ||||
475 | match(C, m_Power2())) { | ||||
476 | // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't | ||||
477 | // demand the sign bit (and many others) here?? | ||||
478 | Constant *Mask = ConstantExpr::getSub(C, ConstantInt::get(Ty, 1)); | ||||
479 | Value *Rem = Builder.CreateAnd(A, Mask, Op1->getName()); | ||||
480 | return replaceOperand(I, 1, Rem); | ||||
481 | } | ||||
482 | |||||
483 | if (Instruction *Logic = foldShiftOfShiftedBinOp(I, Builder)) | ||||
484 | return Logic; | ||||
485 | |||||
486 | if (match(Op1, m_Or(m_Value(), m_SpecificInt(BitWidth - 1)))) | ||||
487 | return replaceOperand(I, 1, ConstantInt::get(Ty, BitWidth - 1)); | ||||
488 | |||||
489 | return nullptr; | ||||
490 | } | ||||
491 | |||||
492 | /// Return true if we can simplify two logical (either left or right) shifts | ||||
493 | /// that have constant shift amounts: OuterShift (InnerShift X, C1), C2. | ||||
494 | static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl, | ||||
495 | Instruction *InnerShift, | ||||
496 | InstCombinerImpl &IC, Instruction *CxtI) { | ||||
497 | assert(InnerShift->isLogicalShift() && "Unexpected instruction type")(static_cast <bool> (InnerShift->isLogicalShift() && "Unexpected instruction type") ? void (0) : __assert_fail ("InnerShift->isLogicalShift() && \"Unexpected instruction type\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 497 , __extension__ __PRETTY_FUNCTION__)); | ||||
498 | |||||
499 | // We need constant scalar or constant splat shifts. | ||||
500 | const APInt *InnerShiftConst; | ||||
501 | if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst))) | ||||
502 | return false; | ||||
503 | |||||
504 | // Two logical shifts in the same direction: | ||||
505 | // shl (shl X, C1), C2 --> shl X, C1 + C2 | ||||
506 | // lshr (lshr X, C1), C2 --> lshr X, C1 + C2 | ||||
507 | bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl; | ||||
508 | if (IsInnerShl == IsOuterShl) | ||||
509 | return true; | ||||
510 | |||||
511 | // Equal shift amounts in opposite directions become bitwise 'and': | ||||
512 | // lshr (shl X, C), C --> and X, C' | ||||
513 | // shl (lshr X, C), C --> and X, C' | ||||
514 | if (*InnerShiftConst == OuterShAmt) | ||||
515 | return true; | ||||
516 | |||||
517 | // If the 2nd shift is bigger than the 1st, we can fold: | ||||
518 | // lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3 | ||||
519 | // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3 | ||||
520 | // but it isn't profitable unless we know the and'd out bits are already zero. | ||||
521 | // Also, check that the inner shift is valid (less than the type width) or | ||||
522 | // we'll crash trying to produce the bit mask for the 'and'. | ||||
523 | unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits(); | ||||
524 | if (InnerShiftConst->ugt(OuterShAmt) && InnerShiftConst->ult(TypeWidth)) { | ||||
525 | unsigned InnerShAmt = InnerShiftConst->getZExtValue(); | ||||
526 | unsigned MaskShift = | ||||
527 | IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt; | ||||
528 | APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift; | ||||
529 | if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI)) | ||||
530 | return true; | ||||
531 | } | ||||
532 | |||||
533 | return false; | ||||
534 | } | ||||
535 | |||||
536 | /// See if we can compute the specified value, but shifted logically to the left | ||||
537 | /// or right by some number of bits. This should return true if the expression | ||||
538 | /// can be computed for the same cost as the current expression tree. This is | ||||
539 | /// used to eliminate extraneous shifting from things like: | ||||
540 | /// %C = shl i128 %A, 64 | ||||
541 | /// %D = shl i128 %B, 96 | ||||
542 | /// %E = or i128 %C, %D | ||||
543 | /// %F = lshr i128 %E, 64 | ||||
544 | /// where the client will ask if E can be computed shifted right by 64-bits. If | ||||
545 | /// this succeeds, getShiftedValue() will be called to produce the value. | ||||
546 | static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift, | ||||
547 | InstCombinerImpl &IC, Instruction *CxtI) { | ||||
548 | // We can always evaluate constants shifted. | ||||
549 | if (isa<Constant>(V)) | ||||
550 | return true; | ||||
551 | |||||
552 | Instruction *I = dyn_cast<Instruction>(V); | ||||
553 | if (!I) return false; | ||||
554 | |||||
555 | // We can't mutate something that has multiple uses: doing so would | ||||
556 | // require duplicating the instruction in general, which isn't profitable. | ||||
557 | if (!I->hasOneUse()) return false; | ||||
558 | |||||
559 | switch (I->getOpcode()) { | ||||
560 | default: return false; | ||||
561 | case Instruction::And: | ||||
562 | case Instruction::Or: | ||||
563 | case Instruction::Xor: | ||||
564 | // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted. | ||||
565 | return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) && | ||||
566 | canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I); | ||||
567 | |||||
568 | case Instruction::Shl: | ||||
569 | case Instruction::LShr: | ||||
570 | return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI); | ||||
571 | |||||
572 | case Instruction::Select: { | ||||
573 | SelectInst *SI = cast<SelectInst>(I); | ||||
574 | Value *TrueVal = SI->getTrueValue(); | ||||
575 | Value *FalseVal = SI->getFalseValue(); | ||||
576 | return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) && | ||||
577 | canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI); | ||||
578 | } | ||||
579 | case Instruction::PHI: { | ||||
580 | // We can change a phi if we can change all operands. Note that we never | ||||
581 | // get into trouble with cyclic PHIs here because we only consider | ||||
582 | // instructions with a single use. | ||||
583 | PHINode *PN = cast<PHINode>(I); | ||||
584 | for (Value *IncValue : PN->incoming_values()) | ||||
585 | if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN)) | ||||
586 | return false; | ||||
587 | return true; | ||||
588 | } | ||||
589 | case Instruction::Mul: { | ||||
590 | const APInt *MulConst; | ||||
591 | // We can fold (shr (mul X, -(1 << C)), C) -> (and (neg X), C`) | ||||
592 | return !IsLeftShift && match(I->getOperand(1), m_APInt(MulConst)) && | ||||
593 | MulConst->isNegatedPowerOf2() && MulConst->countr_zero() == NumBits; | ||||
594 | } | ||||
595 | } | ||||
596 | } | ||||
597 | |||||
598 | /// Fold OuterShift (InnerShift X, C1), C2. | ||||
599 | /// See canEvaluateShiftedShift() for the constraints on these instructions. | ||||
600 | static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt, | ||||
601 | bool IsOuterShl, | ||||
602 | InstCombiner::BuilderTy &Builder) { | ||||
603 | bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl; | ||||
604 | Type *ShType = InnerShift->getType(); | ||||
605 | unsigned TypeWidth = ShType->getScalarSizeInBits(); | ||||
606 | |||||
607 | // We only accept shifts-by-a-constant in canEvaluateShifted(). | ||||
608 | const APInt *C1; | ||||
609 | match(InnerShift->getOperand(1), m_APInt(C1)); | ||||
610 | unsigned InnerShAmt = C1->getZExtValue(); | ||||
611 | |||||
612 | // Change the shift amount and clear the appropriate IR flags. | ||||
613 | auto NewInnerShift = [&](unsigned ShAmt) { | ||||
614 | InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt)); | ||||
615 | if (IsInnerShl) { | ||||
616 | InnerShift->setHasNoUnsignedWrap(false); | ||||
617 | InnerShift->setHasNoSignedWrap(false); | ||||
618 | } else { | ||||
619 | InnerShift->setIsExact(false); | ||||
620 | } | ||||
621 | return InnerShift; | ||||
622 | }; | ||||
623 | |||||
624 | // Two logical shifts in the same direction: | ||||
625 | // shl (shl X, C1), C2 --> shl X, C1 + C2 | ||||
626 | // lshr (lshr X, C1), C2 --> lshr X, C1 + C2 | ||||
627 | if (IsInnerShl == IsOuterShl) { | ||||
628 | // If this is an oversized composite shift, then unsigned shifts get 0. | ||||
629 | if (InnerShAmt + OuterShAmt >= TypeWidth) | ||||
630 | return Constant::getNullValue(ShType); | ||||
631 | |||||
632 | return NewInnerShift(InnerShAmt + OuterShAmt); | ||||
633 | } | ||||
634 | |||||
635 | // Equal shift amounts in opposite directions become bitwise 'and': | ||||
636 | // lshr (shl X, C), C --> and X, C' | ||||
637 | // shl (lshr X, C), C --> and X, C' | ||||
638 | if (InnerShAmt == OuterShAmt) { | ||||
639 | APInt Mask = IsInnerShl | ||||
640 | ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt) | ||||
641 | : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt); | ||||
642 | Value *And = Builder.CreateAnd(InnerShift->getOperand(0), | ||||
643 | ConstantInt::get(ShType, Mask)); | ||||
644 | if (auto *AndI = dyn_cast<Instruction>(And)) { | ||||
645 | AndI->moveBefore(InnerShift); | ||||
646 | AndI->takeName(InnerShift); | ||||
647 | } | ||||
648 | return And; | ||||
649 | } | ||||
650 | |||||
651 | assert(InnerShAmt > OuterShAmt &&(static_cast <bool> (InnerShAmt > OuterShAmt && "Unexpected opposite direction logical shift pair") ? void ( 0) : __assert_fail ("InnerShAmt > OuterShAmt && \"Unexpected opposite direction logical shift pair\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 652 , __extension__ __PRETTY_FUNCTION__)) | ||||
652 | "Unexpected opposite direction logical shift pair")(static_cast <bool> (InnerShAmt > OuterShAmt && "Unexpected opposite direction logical shift pair") ? void ( 0) : __assert_fail ("InnerShAmt > OuterShAmt && \"Unexpected opposite direction logical shift pair\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 652 , __extension__ __PRETTY_FUNCTION__)); | ||||
653 | |||||
654 | // In general, we would need an 'and' for this transform, but | ||||
655 | // canEvaluateShiftedShift() guarantees that the masked-off bits are not used. | ||||
656 | // lshr (shl X, C1), C2 --> shl X, C1 - C2 | ||||
657 | // shl (lshr X, C1), C2 --> lshr X, C1 - C2 | ||||
658 | return NewInnerShift(InnerShAmt - OuterShAmt); | ||||
659 | } | ||||
660 | |||||
661 | /// When canEvaluateShifted() returns true for an expression, this function | ||||
662 | /// inserts the new computation that produces the shifted value. | ||||
663 | static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, | ||||
664 | InstCombinerImpl &IC, const DataLayout &DL) { | ||||
665 | // We can always evaluate constants shifted. | ||||
666 | if (Constant *C = dyn_cast<Constant>(V)) { | ||||
667 | if (isLeftShift) | ||||
668 | return IC.Builder.CreateShl(C, NumBits); | ||||
669 | else | ||||
670 | return IC.Builder.CreateLShr(C, NumBits); | ||||
671 | } | ||||
672 | |||||
673 | Instruction *I = cast<Instruction>(V); | ||||
674 | IC.addToWorklist(I); | ||||
675 | |||||
676 | switch (I->getOpcode()) { | ||||
677 | default: llvm_unreachable("Inconsistency with CanEvaluateShifted")::llvm::llvm_unreachable_internal("Inconsistency with CanEvaluateShifted" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 677 ); | ||||
678 | case Instruction::And: | ||||
679 | case Instruction::Or: | ||||
680 | case Instruction::Xor: | ||||
681 | // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted. | ||||
682 | I->setOperand( | ||||
683 | 0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL)); | ||||
684 | I->setOperand( | ||||
685 | 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL)); | ||||
686 | return I; | ||||
687 | |||||
688 | case Instruction::Shl: | ||||
689 | case Instruction::LShr: | ||||
690 | return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift, | ||||
691 | IC.Builder); | ||||
692 | |||||
693 | case Instruction::Select: | ||||
694 | I->setOperand( | ||||
695 | 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL)); | ||||
696 | I->setOperand( | ||||
697 | 2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL)); | ||||
698 | return I; | ||||
699 | case Instruction::PHI: { | ||||
700 | // We can change a phi if we can change all operands. Note that we never | ||||
701 | // get into trouble with cyclic PHIs here because we only consider | ||||
702 | // instructions with a single use. | ||||
703 | PHINode *PN = cast<PHINode>(I); | ||||
704 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) | ||||
705 | PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits, | ||||
706 | isLeftShift, IC, DL)); | ||||
707 | return PN; | ||||
708 | } | ||||
709 | case Instruction::Mul: { | ||||
710 | assert(!isLeftShift && "Unexpected shift direction!")(static_cast <bool> (!isLeftShift && "Unexpected shift direction!" ) ? void (0) : __assert_fail ("!isLeftShift && \"Unexpected shift direction!\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 710 , __extension__ __PRETTY_FUNCTION__)); | ||||
711 | auto *Neg = BinaryOperator::CreateNeg(I->getOperand(0)); | ||||
712 | IC.InsertNewInstWith(Neg, *I); | ||||
713 | unsigned TypeWidth = I->getType()->getScalarSizeInBits(); | ||||
714 | APInt Mask = APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits); | ||||
715 | auto *And = BinaryOperator::CreateAnd(Neg, | ||||
716 | ConstantInt::get(I->getType(), Mask)); | ||||
717 | And->takeName(I); | ||||
718 | return IC.InsertNewInstWith(And, *I); | ||||
719 | } | ||||
720 | } | ||||
721 | } | ||||
722 | |||||
723 | // If this is a bitwise operator or add with a constant RHS we might be able | ||||
724 | // to pull it through a shift. | ||||
725 | static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift, | ||||
726 | BinaryOperator *BO) { | ||||
727 | switch (BO->getOpcode()) { | ||||
728 | default: | ||||
729 | return false; // Do not perform transform! | ||||
730 | case Instruction::Add: | ||||
731 | return Shift.getOpcode() == Instruction::Shl; | ||||
732 | case Instruction::Or: | ||||
733 | case Instruction::And: | ||||
734 | return true; | ||||
735 | case Instruction::Xor: | ||||
736 | // Do not change a 'not' of logical shift because that would create a normal | ||||
737 | // 'xor'. The 'not' is likely better for analysis, SCEV, and codegen. | ||||
738 | return !(Shift.isLogicalShift() && match(BO, m_Not(m_Value()))); | ||||
739 | } | ||||
740 | } | ||||
741 | |||||
742 | Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *C1, | ||||
743 | BinaryOperator &I) { | ||||
744 | // (C2 << X) << C1 --> (C2 << C1) << X | ||||
745 | // (C2 >> X) >> C1 --> (C2 >> C1) >> X | ||||
746 | Constant *C2; | ||||
747 | Value *X; | ||||
748 | if (match(Op0, m_BinOp(I.getOpcode(), m_Constant(C2), m_Value(X)))) | ||||
749 | return BinaryOperator::Create( | ||||
750 | I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), C2, C1), X); | ||||
751 | |||||
752 | bool IsLeftShift = I.getOpcode() == Instruction::Shl; | ||||
753 | Type *Ty = I.getType(); | ||||
754 | unsigned TypeBits = Ty->getScalarSizeInBits(); | ||||
755 | |||||
756 | // (X / +DivC) >> (Width - 1) --> ext (X <= -DivC) | ||||
757 | // (X / -DivC) >> (Width - 1) --> ext (X >= +DivC) | ||||
758 | const APInt *DivC; | ||||
759 | if (!IsLeftShift && match(C1, m_SpecificIntAllowUndef(TypeBits - 1)) && | ||||
760 | match(Op0, m_SDiv(m_Value(X), m_APInt(DivC))) && !DivC->isZero() && | ||||
761 | !DivC->isMinSignedValue()) { | ||||
762 | Constant *NegDivC = ConstantInt::get(Ty, -(*DivC)); | ||||
763 | ICmpInst::Predicate Pred = | ||||
764 | DivC->isNegative() ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_SLE; | ||||
765 | Value *Cmp = Builder.CreateICmp(Pred, X, NegDivC); | ||||
766 | auto ExtOpcode = (I.getOpcode() == Instruction::AShr) ? Instruction::SExt | ||||
767 | : Instruction::ZExt; | ||||
768 | return CastInst::Create(ExtOpcode, Cmp, Ty); | ||||
769 | } | ||||
770 | |||||
771 | const APInt *Op1C; | ||||
772 | if (!match(C1, m_APInt(Op1C))) | ||||
773 | return nullptr; | ||||
774 | |||||
775 | assert(!Op1C->uge(TypeBits) &&(static_cast <bool> (!Op1C->uge(TypeBits) && "Shift over the type width should have been removed already" ) ? void (0) : __assert_fail ("!Op1C->uge(TypeBits) && \"Shift over the type width should have been removed already\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 776 , __extension__ __PRETTY_FUNCTION__)) | ||||
776 | "Shift over the type width should have been removed already")(static_cast <bool> (!Op1C->uge(TypeBits) && "Shift over the type width should have been removed already" ) ? void (0) : __assert_fail ("!Op1C->uge(TypeBits) && \"Shift over the type width should have been removed already\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 776 , __extension__ __PRETTY_FUNCTION__)); | ||||
777 | |||||
778 | // See if we can propagate this shift into the input, this covers the trivial | ||||
779 | // cast of lshr(shl(x,c1),c2) as well as other more complex cases. | ||||
780 | if (I.getOpcode() != Instruction::AShr && | ||||
781 | canEvaluateShifted(Op0, Op1C->getZExtValue(), IsLeftShift, *this, &I)) { | ||||
782 | LLVM_DEBUG(do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("instcombine")) { dbgs() << "ICE: GetShiftedValue propagating shift through expression" " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I << "\n"; } } while (false) | ||||
783 | dbgs() << "ICE: GetShiftedValue propagating shift through expression"do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("instcombine")) { dbgs() << "ICE: GetShiftedValue propagating shift through expression" " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I << "\n"; } } while (false) | ||||
784 | " to eliminate shift:\n IN: "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("instcombine")) { dbgs() << "ICE: GetShiftedValue propagating shift through expression" " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I << "\n"; } } while (false) | ||||
785 | << *Op0 << "\n SH: " << I << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("instcombine")) { dbgs() << "ICE: GetShiftedValue propagating shift through expression" " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I << "\n"; } } while (false); | ||||
786 | |||||
787 | return replaceInstUsesWith( | ||||
788 | I, getShiftedValue(Op0, Op1C->getZExtValue(), IsLeftShift, *this, DL)); | ||||
789 | } | ||||
790 | |||||
791 | if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I)) | ||||
792 | return FoldedShift; | ||||
793 | |||||
794 | if (!Op0->hasOneUse()) | ||||
795 | return nullptr; | ||||
796 | |||||
797 | if (auto *Op0BO = dyn_cast<BinaryOperator>(Op0)) { | ||||
798 | // If the operand is a bitwise operator with a constant RHS, and the | ||||
799 | // shift is the only use, we can pull it out of the shift. | ||||
800 | const APInt *Op0C; | ||||
801 | if (match(Op0BO->getOperand(1), m_APInt(Op0C))) { | ||||
802 | if (canShiftBinOpWithConstantRHS(I, Op0BO)) { | ||||
803 | Value *NewRHS = | ||||
804 | Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(1), C1); | ||||
805 | |||||
806 | Value *NewShift = | ||||
807 | Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), C1); | ||||
808 | NewShift->takeName(Op0BO); | ||||
809 | |||||
810 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, NewRHS); | ||||
811 | } | ||||
812 | } | ||||
813 | } | ||||
814 | |||||
815 | // If we have a select that conditionally executes some binary operator, | ||||
816 | // see if we can pull it the select and operator through the shift. | ||||
817 | // | ||||
818 | // For example, turning: | ||||
819 | // shl (select C, (add X, C1), X), C2 | ||||
820 | // Into: | ||||
821 | // Y = shl X, C2 | ||||
822 | // select C, (add Y, C1 << C2), Y | ||||
823 | Value *Cond; | ||||
824 | BinaryOperator *TBO; | ||||
825 | Value *FalseVal; | ||||
826 | if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)), | ||||
827 | m_Value(FalseVal)))) { | ||||
828 | const APInt *C; | ||||
829 | if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal && | ||||
830 | match(TBO->getOperand(1), m_APInt(C)) && | ||||
831 | canShiftBinOpWithConstantRHS(I, TBO)) { | ||||
832 | Value *NewRHS = | ||||
833 | Builder.CreateBinOp(I.getOpcode(), TBO->getOperand(1), C1); | ||||
834 | |||||
835 | Value *NewShift = Builder.CreateBinOp(I.getOpcode(), FalseVal, C1); | ||||
836 | Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift, NewRHS); | ||||
837 | return SelectInst::Create(Cond, NewOp, NewShift); | ||||
838 | } | ||||
839 | } | ||||
840 | |||||
841 | BinaryOperator *FBO; | ||||
842 | Value *TrueVal; | ||||
843 | if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal), | ||||
844 | m_OneUse(m_BinOp(FBO))))) { | ||||
845 | const APInt *C; | ||||
846 | if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal && | ||||
847 | match(FBO->getOperand(1), m_APInt(C)) && | ||||
848 | canShiftBinOpWithConstantRHS(I, FBO)) { | ||||
849 | Value *NewRHS = | ||||
850 | Builder.CreateBinOp(I.getOpcode(), FBO->getOperand(1), C1); | ||||
851 | |||||
852 | Value *NewShift = Builder.CreateBinOp(I.getOpcode(), TrueVal, C1); | ||||
853 | Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift, NewRHS); | ||||
854 | return SelectInst::Create(Cond, NewShift, NewOp); | ||||
855 | } | ||||
856 | } | ||||
857 | |||||
858 | return nullptr; | ||||
859 | } | ||||
860 | |||||
861 | // Tries to perform | ||||
862 | // (lshr (add (zext X), (zext Y)), K) | ||||
863 | // -> (icmp ult (add X, Y), X) | ||||
864 | // where | ||||
865 | // - The add's operands are zexts from a K-bits integer to a bigger type. | ||||
866 | // - The add is only used by the shr, or by iK (or narrower) truncates. | ||||
867 | // - The lshr type has more than 2 bits (other types are boolean math). | ||||
868 | // - K > 1 | ||||
869 | // note that | ||||
870 | // - The resulting add cannot have nuw/nsw, else on overflow we get a | ||||
871 | // poison value and the transform isn't legal anymore. | ||||
872 | Instruction *InstCombinerImpl::foldLShrOverflowBit(BinaryOperator &I) { | ||||
873 | assert(I.getOpcode() == Instruction::LShr)(static_cast <bool> (I.getOpcode() == Instruction::LShr ) ? void (0) : __assert_fail ("I.getOpcode() == Instruction::LShr" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 873 , __extension__ __PRETTY_FUNCTION__)); | ||||
874 | |||||
875 | Value *Add = I.getOperand(0); | ||||
876 | Value *ShiftAmt = I.getOperand(1); | ||||
877 | Type *Ty = I.getType(); | ||||
878 | |||||
879 | if (Ty->getScalarSizeInBits() < 3) | ||||
880 | return nullptr; | ||||
881 | |||||
882 | const APInt *ShAmtAPInt = nullptr; | ||||
883 | Value *X = nullptr, *Y = nullptr; | ||||
884 | if (!match(ShiftAmt, m_APInt(ShAmtAPInt)) || | ||||
885 | !match(Add, | ||||
886 | m_Add(m_OneUse(m_ZExt(m_Value(X))), m_OneUse(m_ZExt(m_Value(Y)))))) | ||||
887 | return nullptr; | ||||
888 | |||||
889 | const unsigned ShAmt = ShAmtAPInt->getZExtValue(); | ||||
890 | if (ShAmt == 1) | ||||
891 | return nullptr; | ||||
892 | |||||
893 | // X/Y are zexts from `ShAmt`-sized ints. | ||||
894 | if (X->getType()->getScalarSizeInBits() != ShAmt || | ||||
895 | Y->getType()->getScalarSizeInBits() != ShAmt) | ||||
896 | return nullptr; | ||||
897 | |||||
898 | // Make sure that `Add` is only used by `I` and `ShAmt`-truncates. | ||||
899 | if (!Add->hasOneUse()) { | ||||
900 | for (User *U : Add->users()) { | ||||
901 | if (U == &I) | ||||
902 | continue; | ||||
903 | |||||
904 | TruncInst *Trunc = dyn_cast<TruncInst>(U); | ||||
905 | if (!Trunc || Trunc->getType()->getScalarSizeInBits() > ShAmt) | ||||
906 | return nullptr; | ||||
907 | } | ||||
908 | } | ||||
909 | |||||
910 | // Insert at Add so that the newly created `NarrowAdd` will dominate it's | ||||
911 | // users (i.e. `Add`'s users). | ||||
912 | Instruction *AddInst = cast<Instruction>(Add); | ||||
913 | Builder.SetInsertPoint(AddInst); | ||||
914 | |||||
915 | Value *NarrowAdd = Builder.CreateAdd(X, Y, "add.narrowed"); | ||||
916 | Value *Overflow = | ||||
917 | Builder.CreateICmpULT(NarrowAdd, X, "add.narrowed.overflow"); | ||||
918 | |||||
919 | // Replace the uses of the original add with a zext of the | ||||
920 | // NarrowAdd's result. Note that all users at this stage are known to | ||||
921 | // be ShAmt-sized truncs, or the lshr itself. | ||||
922 | if (!Add->hasOneUse()) | ||||
923 | replaceInstUsesWith(*AddInst, Builder.CreateZExt(NarrowAdd, Ty)); | ||||
924 | |||||
925 | // Replace the LShr with a zext of the overflow check. | ||||
926 | return new ZExtInst(Overflow, Ty); | ||||
927 | } | ||||
928 | |||||
929 | Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) { | ||||
930 | const SimplifyQuery Q = SQ.getWithInstruction(&I); | ||||
931 | |||||
932 | if (Value *V = simplifyShlInst(I.getOperand(0), I.getOperand(1), | ||||
| |||||
933 | I.hasNoSignedWrap(), I.hasNoUnsignedWrap(), Q)) | ||||
934 | return replaceInstUsesWith(I, V); | ||||
935 | |||||
936 | if (Instruction *X = foldVectorBinop(I)) | ||||
937 | return X; | ||||
938 | |||||
939 | if (Instruction *V
| ||||
940 | return V; | ||||
941 | |||||
942 | if (Instruction *V = dropRedundantMaskingOfLeftShiftInput(&I, Q, Builder)) | ||||
943 | return V; | ||||
944 | |||||
945 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
946 | Type *Ty = I.getType(); | ||||
947 | unsigned BitWidth = Ty->getScalarSizeInBits(); | ||||
948 | |||||
949 | const APInt *C; | ||||
950 | if (match(Op1, m_APInt(C))) { | ||||
951 | unsigned ShAmtC = C->getZExtValue(); | ||||
952 | |||||
953 | // shl (zext X), C --> zext (shl X, C) | ||||
954 | // This is only valid if X would have zeros shifted out. | ||||
955 | Value *X; | ||||
956 | if (match(Op0, m_OneUse(m_ZExt(m_Value(X))))) { | ||||
957 | unsigned SrcWidth = X->getType()->getScalarSizeInBits(); | ||||
958 | if (ShAmtC < SrcWidth && | ||||
959 | MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmtC), 0, &I)) | ||||
960 | return new ZExtInst(Builder.CreateShl(X, ShAmtC), Ty); | ||||
961 | } | ||||
962 | |||||
963 | // (X >> C) << C --> X & (-1 << C) | ||||
964 | if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) { | ||||
965 | APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC)); | ||||
966 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask)); | ||||
967 | } | ||||
968 | |||||
969 | const APInt *C1; | ||||
970 | if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(C1)))) && | ||||
971 | C1->ult(BitWidth)) { | ||||
972 | unsigned ShrAmt = C1->getZExtValue(); | ||||
973 | if (ShrAmt < ShAmtC) { | ||||
974 | // If C1 < C: (X >>?,exact C1) << C --> X << (C - C1) | ||||
975 | Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt); | ||||
976 | auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff); | ||||
977 | NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); | ||||
978 | NewShl->setHasNoSignedWrap(I.hasNoSignedWrap()); | ||||
979 | return NewShl; | ||||
980 | } | ||||
981 | if (ShrAmt > ShAmtC) { | ||||
982 | // If C1 > C: (X >>?exact C1) << C --> X >>?exact (C1 - C) | ||||
983 | Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC); | ||||
984 | auto *NewShr = BinaryOperator::Create( | ||||
985 | cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff); | ||||
986 | NewShr->setIsExact(true); | ||||
987 | return NewShr; | ||||
988 | } | ||||
989 | } | ||||
990 | |||||
991 | if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_APInt(C1)))) && | ||||
992 | C1->ult(BitWidth)) { | ||||
993 | unsigned ShrAmt = C1->getZExtValue(); | ||||
994 | if (ShrAmt < ShAmtC) { | ||||
995 | // If C1 < C: (X >>? C1) << C --> (X << (C - C1)) & (-1 << C) | ||||
996 | Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShrAmt); | ||||
997 | auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff); | ||||
998 | NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); | ||||
999 | NewShl->setHasNoSignedWrap(I.hasNoSignedWrap()); | ||||
1000 | Builder.Insert(NewShl); | ||||
1001 | APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC)); | ||||
1002 | return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask)); | ||||
1003 | } | ||||
1004 | if (ShrAmt > ShAmtC) { | ||||
1005 | // If C1 > C: (X >>? C1) << C --> (X >>? (C1 - C)) & (-1 << C) | ||||
1006 | Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmtC); | ||||
1007 | auto *OldShr = cast<BinaryOperator>(Op0); | ||||
1008 | auto *NewShr = | ||||
1009 | BinaryOperator::Create(OldShr->getOpcode(), X, ShiftDiff); | ||||
1010 | NewShr->setIsExact(OldShr->isExact()); | ||||
1011 | Builder.Insert(NewShr); | ||||
1012 | APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC)); | ||||
1013 | return BinaryOperator::CreateAnd(NewShr, ConstantInt::get(Ty, Mask)); | ||||
1014 | } | ||||
1015 | } | ||||
1016 | |||||
1017 | // Similar to above, but look through an intermediate trunc instruction. | ||||
1018 | BinaryOperator *Shr; | ||||
1019 | if (match(Op0, m_OneUse(m_Trunc(m_OneUse(m_BinOp(Shr))))) && | ||||
1020 | match(Shr, m_Shr(m_Value(X), m_APInt(C1)))) { | ||||
1021 | // The larger shift direction survives through the transform. | ||||
1022 | unsigned ShrAmtC = C1->getZExtValue(); | ||||
1023 | unsigned ShDiff = ShrAmtC > ShAmtC ? ShrAmtC - ShAmtC : ShAmtC - ShrAmtC; | ||||
1024 | Constant *ShiftDiffC = ConstantInt::get(X->getType(), ShDiff); | ||||
1025 | auto ShiftOpc = ShrAmtC > ShAmtC ? Shr->getOpcode() : Instruction::Shl; | ||||
1026 | |||||
1027 | // If C1 > C: | ||||
1028 | // (trunc (X >> C1)) << C --> (trunc (X >> (C1 - C))) && (-1 << C) | ||||
1029 | // If C > C1: | ||||
1030 | // (trunc (X >> C1)) << C --> (trunc (X << (C - C1))) && (-1 << C) | ||||
1031 | Value *NewShift = Builder.CreateBinOp(ShiftOpc, X, ShiftDiffC, "sh.diff"); | ||||
1032 | Value *Trunc = Builder.CreateTrunc(NewShift, Ty, "tr.sh.diff"); | ||||
1033 | APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmtC)); | ||||
1034 | return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, Mask)); | ||||
1035 | } | ||||
1036 | |||||
1037 | if (match(Op0, m_Shl(m_Value(X), m_APInt(C1))) && C1->ult(BitWidth)) { | ||||
1038 | unsigned AmtSum = ShAmtC + C1->getZExtValue(); | ||||
1039 | // Oversized shifts are simplified to zero in InstSimplify. | ||||
1040 | if (AmtSum < BitWidth) | ||||
1041 | // (X << C1) << C2 --> X << (C1 + C2) | ||||
1042 | return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum)); | ||||
1043 | } | ||||
1044 | |||||
1045 | // If we have an opposite shift by the same amount, we may be able to | ||||
1046 | // reorder binops and shifts to eliminate math/logic. | ||||
1047 | auto isSuitableBinOpcode = [](Instruction::BinaryOps BinOpcode) { | ||||
1048 | switch (BinOpcode) { | ||||
1049 | default: | ||||
1050 | return false; | ||||
1051 | case Instruction::Add: | ||||
1052 | case Instruction::And: | ||||
1053 | case Instruction::Or: | ||||
1054 | case Instruction::Xor: | ||||
1055 | case Instruction::Sub: | ||||
1056 | // NOTE: Sub is not commutable and the tranforms below may not be valid | ||||
1057 | // when the shift-right is operand 1 (RHS) of the sub. | ||||
1058 | return true; | ||||
1059 | } | ||||
1060 | }; | ||||
1061 | BinaryOperator *Op0BO; | ||||
1062 | if (match(Op0, m_OneUse(m_BinOp(Op0BO))) && | ||||
1063 | isSuitableBinOpcode(Op0BO->getOpcode())) { | ||||
1064 | // Commute so shift-right is on LHS of the binop. | ||||
1065 | // (Y bop (X >> C)) << C -> ((X >> C) bop Y) << C | ||||
1066 | // (Y bop ((X >> C) & CC)) << C -> (((X >> C) & CC) bop Y) << C | ||||
1067 | Value *Shr = Op0BO->getOperand(0); | ||||
1068 | Value *Y = Op0BO->getOperand(1); | ||||
1069 | Value *X; | ||||
1070 | const APInt *CC; | ||||
1071 | if (Op0BO->isCommutative() && Y->hasOneUse() && | ||||
1072 | (match(Y, m_Shr(m_Value(), m_Specific(Op1))) || | ||||
1073 | match(Y, m_And(m_OneUse(m_Shr(m_Value(), m_Specific(Op1))), | ||||
1074 | m_APInt(CC))))) | ||||
1075 | std::swap(Shr, Y); | ||||
1076 | |||||
1077 | // ((X >> C) bop Y) << C -> (X bop (Y << C)) & (~0 << C) | ||||
1078 | if (match(Shr, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) { | ||||
1079 | // Y << C | ||||
1080 | Value *YS = Builder.CreateShl(Y, Op1, Op0BO->getName()); | ||||
1081 | // (X bop (Y << C)) | ||||
1082 | Value *B = | ||||
1083 | Builder.CreateBinOp(Op0BO->getOpcode(), X, YS, Shr->getName()); | ||||
1084 | unsigned Op1Val = C->getLimitedValue(BitWidth); | ||||
1085 | APInt Bits = APInt::getHighBitsSet(BitWidth, BitWidth - Op1Val); | ||||
1086 | Constant *Mask = ConstantInt::get(Ty, Bits); | ||||
1087 | return BinaryOperator::CreateAnd(B, Mask); | ||||
1088 | } | ||||
1089 | |||||
1090 | // (((X >> C) & CC) bop Y) << C -> (X & (CC << C)) bop (Y << C) | ||||
1091 | if (match(Shr, | ||||
1092 | m_OneUse(m_And(m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))), | ||||
1093 | m_APInt(CC))))) { | ||||
1094 | // Y << C | ||||
1095 | Value *YS = Builder.CreateShl(Y, Op1, Op0BO->getName()); | ||||
1096 | // X & (CC << C) | ||||
1097 | Value *M = Builder.CreateAnd(X, ConstantInt::get(Ty, CC->shl(*C)), | ||||
1098 | X->getName() + ".mask"); | ||||
1099 | return BinaryOperator::Create(Op0BO->getOpcode(), M, YS); | ||||
1100 | } | ||||
1101 | } | ||||
1102 | |||||
1103 | // (C1 - X) << C --> (C1 << C) - (X << C) | ||||
1104 | if (match(Op0, m_OneUse(m_Sub(m_APInt(C1), m_Value(X))))) { | ||||
1105 | Constant *NewLHS = ConstantInt::get(Ty, C1->shl(*C)); | ||||
1106 | Value *NewShift = Builder.CreateShl(X, Op1); | ||||
1107 | return BinaryOperator::CreateSub(NewLHS, NewShift); | ||||
1108 | } | ||||
1109 | |||||
1110 | // If the shifted-out value is known-zero, then this is a NUW shift. | ||||
1111 | if (!I.hasNoUnsignedWrap() && | ||||
1112 | MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmtC), 0, | ||||
1113 | &I)) { | ||||
1114 | I.setHasNoUnsignedWrap(); | ||||
1115 | return &I; | ||||
1116 | } | ||||
1117 | |||||
1118 | // If the shifted-out value is all signbits, then this is a NSW shift. | ||||
1119 | if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmtC) { | ||||
1120 | I.setHasNoSignedWrap(); | ||||
1121 | return &I; | ||||
1122 | } | ||||
1123 | } | ||||
1124 | |||||
1125 | // Transform (x >> y) << y to x & (-1 << y) | ||||
1126 | // Valid for any type of right-shift. | ||||
1127 | Value *X; | ||||
1128 | if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) { | ||||
1129 | Constant *AllOnes = ConstantInt::getAllOnesValue(Ty); | ||||
1130 | Value *Mask = Builder.CreateShl(AllOnes, Op1); | ||||
1131 | return BinaryOperator::CreateAnd(Mask, X); | ||||
1132 | } | ||||
1133 | |||||
1134 | Constant *C1; | ||||
1135 | if (match(Op1, m_Constant(C1))) { | ||||
1136 | Constant *C2; | ||||
1137 | Value *X; | ||||
1138 | // (X * C2) << C1 --> X * (C2 << C1) | ||||
1139 | if (match(Op0, m_Mul(m_Value(X), m_Constant(C2)))) | ||||
1140 | return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1)); | ||||
1141 | |||||
1142 | // shl (zext i1 X), C1 --> select (X, 1 << C1, 0) | ||||
1143 | if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { | ||||
1144 | auto *NewC = ConstantExpr::getShl(ConstantInt::get(Ty, 1), C1); | ||||
1145 | return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty)); | ||||
1146 | } | ||||
1147 | } | ||||
1148 | |||||
1149 | if (match(Op0, m_One())) { | ||||
1150 | // (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1 | ||||
1151 | if (match(Op1, m_Sub(m_SpecificInt(BitWidth - 1), m_Value(X)))) | ||||
1152 | return BinaryOperator::CreateLShr( | ||||
1153 | ConstantInt::get(Ty, APInt::getSignMask(BitWidth)), X); | ||||
1154 | |||||
1155 | // Canonicalize "extract lowest set bit" using cttz to and-with-negate: | ||||
1156 | // 1 << (cttz X) --> -X & X | ||||
1157 | if (match(Op1, | ||||
1158 | m_OneUse(m_Intrinsic<Intrinsic::cttz>(m_Value(X), m_Value())))) { | ||||
1159 | Value *NegX = Builder.CreateNeg(X, "neg"); | ||||
1160 | return BinaryOperator::CreateAnd(NegX, X); | ||||
1161 | } | ||||
1162 | |||||
1163 | // The only way to shift out the 1 is with an over-shift, so that would | ||||
1164 | // be poison with or without "nuw". Undef is excluded because (undef << X) | ||||
1165 | // is not undef (it is zero). | ||||
1166 | Constant *ConstantOne = cast<Constant>(Op0); | ||||
1167 | if (!I.hasNoUnsignedWrap() && !ConstantOne->containsUndefElement()) { | ||||
1168 | I.setHasNoUnsignedWrap(); | ||||
1169 | return &I; | ||||
1170 | } | ||||
1171 | } | ||||
1172 | |||||
1173 | return nullptr; | ||||
1174 | } | ||||
1175 | |||||
1176 | Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) { | ||||
1177 | if (Value *V = simplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(), | ||||
1178 | SQ.getWithInstruction(&I))) | ||||
1179 | return replaceInstUsesWith(I, V); | ||||
1180 | |||||
1181 | if (Instruction *X = foldVectorBinop(I)) | ||||
1182 | return X; | ||||
1183 | |||||
1184 | if (Instruction *R = commonShiftTransforms(I)) | ||||
1185 | return R; | ||||
1186 | |||||
1187 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
1188 | Type *Ty = I.getType(); | ||||
1189 | Value *X; | ||||
1190 | const APInt *C; | ||||
1191 | unsigned BitWidth = Ty->getScalarSizeInBits(); | ||||
1192 | |||||
1193 | // (iN (~X) u>> (N - 1)) --> zext (X > -1) | ||||
1194 | if (match(Op0, m_OneUse(m_Not(m_Value(X)))) && | ||||
1195 | match(Op1, m_SpecificIntAllowUndef(BitWidth - 1))) | ||||
1196 | return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty); | ||||
1197 | |||||
1198 | if (match(Op1, m_APInt(C))) { | ||||
1199 | unsigned ShAmtC = C->getZExtValue(); | ||||
1200 | auto *II = dyn_cast<IntrinsicInst>(Op0); | ||||
1201 | if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmtC && | ||||
1202 | (II->getIntrinsicID() == Intrinsic::ctlz || | ||||
1203 | II->getIntrinsicID() == Intrinsic::cttz || | ||||
1204 | II->getIntrinsicID() == Intrinsic::ctpop)) { | ||||
1205 | // ctlz.i32(x)>>5 --> zext(x == 0) | ||||
1206 | // cttz.i32(x)>>5 --> zext(x == 0) | ||||
1207 | // ctpop.i32(x)>>5 --> zext(x == -1) | ||||
1208 | bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop; | ||||
1209 | Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0); | ||||
1210 | Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS); | ||||
1211 | return new ZExtInst(Cmp, Ty); | ||||
1212 | } | ||||
1213 | |||||
1214 | Value *X; | ||||
1215 | const APInt *C1; | ||||
1216 | if (match(Op0, m_Shl(m_Value(X), m_APInt(C1))) && C1->ult(BitWidth)) { | ||||
1217 | if (C1->ult(ShAmtC)) { | ||||
1218 | unsigned ShlAmtC = C1->getZExtValue(); | ||||
1219 | Constant *ShiftDiff = ConstantInt::get(Ty, ShAmtC - ShlAmtC); | ||||
1220 | if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) { | ||||
1221 | // (X <<nuw C1) >>u C --> X >>u (C - C1) | ||||
1222 | auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff); | ||||
1223 | NewLShr->setIsExact(I.isExact()); | ||||
1224 | return NewLShr; | ||||
1225 | } | ||||
1226 | if (Op0->hasOneUse()) { | ||||
1227 | // (X << C1) >>u C --> (X >>u (C - C1)) & (-1 >> C) | ||||
1228 | Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact()); | ||||
1229 | APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC)); | ||||
1230 | return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask)); | ||||
1231 | } | ||||
1232 | } else if (C1->ugt(ShAmtC)) { | ||||
1233 | unsigned ShlAmtC = C1->getZExtValue(); | ||||
1234 | Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmtC - ShAmtC); | ||||
1235 | if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) { | ||||
1236 | // (X <<nuw C1) >>u C --> X <<nuw (C1 - C) | ||||
1237 | auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff); | ||||
1238 | NewShl->setHasNoUnsignedWrap(true); | ||||
1239 | return NewShl; | ||||
1240 | } | ||||
1241 | if (Op0->hasOneUse()) { | ||||
1242 | // (X << C1) >>u C --> X << (C1 - C) & (-1 >> C) | ||||
1243 | Value *NewShl = Builder.CreateShl(X, ShiftDiff); | ||||
1244 | APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC)); | ||||
1245 | return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask)); | ||||
1246 | } | ||||
1247 | } else { | ||||
1248 | assert(*C1 == ShAmtC)(static_cast <bool> (*C1 == ShAmtC) ? void (0) : __assert_fail ("*C1 == ShAmtC", "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp" , 1248, __extension__ __PRETTY_FUNCTION__)); | ||||
1249 | // (X << C) >>u C --> X & (-1 >>u C) | ||||
1250 | APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC)); | ||||
1251 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask)); | ||||
1252 | } | ||||
1253 | } | ||||
1254 | |||||
1255 | // ((X << C) + Y) >>u C --> (X + (Y >>u C)) & (-1 >>u C) | ||||
1256 | // TODO: Consolidate with the more general transform that starts from shl | ||||
1257 | // (the shifts are in the opposite order). | ||||
1258 | Value *Y; | ||||
1259 | if (match(Op0, | ||||
1260 | m_OneUse(m_c_Add(m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))), | ||||
1261 | m_Value(Y))))) { | ||||
1262 | Value *NewLshr = Builder.CreateLShr(Y, Op1); | ||||
1263 | Value *NewAdd = Builder.CreateAdd(NewLshr, X); | ||||
1264 | unsigned Op1Val = C->getLimitedValue(BitWidth); | ||||
1265 | APInt Bits = APInt::getLowBitsSet(BitWidth, BitWidth - Op1Val); | ||||
1266 | Constant *Mask = ConstantInt::get(Ty, Bits); | ||||
1267 | return BinaryOperator::CreateAnd(NewAdd, Mask); | ||||
1268 | } | ||||
1269 | |||||
1270 | if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) && | ||||
1271 | (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) { | ||||
1272 | assert(ShAmtC < X->getType()->getScalarSizeInBits() &&(static_cast <bool> (ShAmtC < X->getType()->getScalarSizeInBits () && "Big shift not simplified to zero?") ? void (0) : __assert_fail ("ShAmtC < X->getType()->getScalarSizeInBits() && \"Big shift not simplified to zero?\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 1273 , __extension__ __PRETTY_FUNCTION__)) | ||||
1273 | "Big shift not simplified to zero?")(static_cast <bool> (ShAmtC < X->getType()->getScalarSizeInBits () && "Big shift not simplified to zero?") ? void (0) : __assert_fail ("ShAmtC < X->getType()->getScalarSizeInBits() && \"Big shift not simplified to zero?\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 1273 , __extension__ __PRETTY_FUNCTION__)); | ||||
1274 | // lshr (zext iM X to iN), C --> zext (lshr X, C) to iN | ||||
1275 | Value *NewLShr = Builder.CreateLShr(X, ShAmtC); | ||||
1276 | return new ZExtInst(NewLShr, Ty); | ||||
1277 | } | ||||
1278 | |||||
1279 | if (match(Op0, m_SExt(m_Value(X)))) { | ||||
1280 | unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits(); | ||||
1281 | // lshr (sext i1 X to iN), C --> select (X, -1 >> C, 0) | ||||
1282 | if (SrcTyBitWidth == 1) { | ||||
1283 | auto *NewC = ConstantInt::get( | ||||
1284 | Ty, APInt::getLowBitsSet(BitWidth, BitWidth - ShAmtC)); | ||||
1285 | return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty)); | ||||
1286 | } | ||||
1287 | |||||
1288 | if ((!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType())) && | ||||
1289 | Op0->hasOneUse()) { | ||||
1290 | // Are we moving the sign bit to the low bit and widening with high | ||||
1291 | // zeros? lshr (sext iM X to iN), N-1 --> zext (lshr X, M-1) to iN | ||||
1292 | if (ShAmtC == BitWidth - 1) { | ||||
1293 | Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1); | ||||
1294 | return new ZExtInst(NewLShr, Ty); | ||||
1295 | } | ||||
1296 | |||||
1297 | // lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN | ||||
1298 | if (ShAmtC == BitWidth - SrcTyBitWidth) { | ||||
1299 | // The new shift amount can't be more than the narrow source type. | ||||
1300 | unsigned NewShAmt = std::min(ShAmtC, SrcTyBitWidth - 1); | ||||
1301 | Value *AShr = Builder.CreateAShr(X, NewShAmt); | ||||
1302 | return new ZExtInst(AShr, Ty); | ||||
1303 | } | ||||
1304 | } | ||||
1305 | } | ||||
1306 | |||||
1307 | if (ShAmtC == BitWidth - 1) { | ||||
1308 | // lshr i32 or(X,-X), 31 --> zext (X != 0) | ||||
1309 | if (match(Op0, m_OneUse(m_c_Or(m_Neg(m_Value(X)), m_Deferred(X))))) | ||||
1310 | return new ZExtInst(Builder.CreateIsNotNull(X), Ty); | ||||
1311 | |||||
1312 | // lshr i32 (X -nsw Y), 31 --> zext (X < Y) | ||||
1313 | if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y))))) | ||||
1314 | return new ZExtInst(Builder.CreateICmpSLT(X, Y), Ty); | ||||
1315 | |||||
1316 | // Check if a number is negative and odd: | ||||
1317 | // lshr i32 (srem X, 2), 31 --> and (X >> 31), X | ||||
1318 | if (match(Op0, m_OneUse(m_SRem(m_Value(X), m_SpecificInt(2))))) { | ||||
1319 | Value *Signbit = Builder.CreateLShr(X, ShAmtC); | ||||
1320 | return BinaryOperator::CreateAnd(Signbit, X); | ||||
1321 | } | ||||
1322 | } | ||||
1323 | |||||
1324 | // (X >>u C1) >>u C --> X >>u (C1 + C) | ||||
1325 | if (match(Op0, m_LShr(m_Value(X), m_APInt(C1)))) { | ||||
1326 | // Oversized shifts are simplified to zero in InstSimplify. | ||||
1327 | unsigned AmtSum = ShAmtC + C1->getZExtValue(); | ||||
1328 | if (AmtSum < BitWidth) | ||||
1329 | return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum)); | ||||
1330 | } | ||||
1331 | |||||
1332 | Instruction *TruncSrc; | ||||
1333 | if (match(Op0, m_OneUse(m_Trunc(m_Instruction(TruncSrc)))) && | ||||
1334 | match(TruncSrc, m_LShr(m_Value(X), m_APInt(C1)))) { | ||||
1335 | unsigned SrcWidth = X->getType()->getScalarSizeInBits(); | ||||
1336 | unsigned AmtSum = ShAmtC + C1->getZExtValue(); | ||||
1337 | |||||
1338 | // If the combined shift fits in the source width: | ||||
1339 | // (trunc (X >>u C1)) >>u C --> and (trunc (X >>u (C1 + C)), MaskC | ||||
1340 | // | ||||
1341 | // If the first shift covers the number of bits truncated, then the | ||||
1342 | // mask instruction is eliminated (and so the use check is relaxed). | ||||
1343 | if (AmtSum < SrcWidth && | ||||
1344 | (TruncSrc->hasOneUse() || C1->uge(SrcWidth - BitWidth))) { | ||||
1345 | Value *SumShift = Builder.CreateLShr(X, AmtSum, "sum.shift"); | ||||
1346 | Value *Trunc = Builder.CreateTrunc(SumShift, Ty, I.getName()); | ||||
1347 | |||||
1348 | // If the first shift does not cover the number of bits truncated, then | ||||
1349 | // we require a mask to get rid of high bits in the result. | ||||
1350 | APInt MaskC = APInt::getAllOnes(BitWidth).lshr(ShAmtC); | ||||
1351 | return BinaryOperator::CreateAnd(Trunc, ConstantInt::get(Ty, MaskC)); | ||||
1352 | } | ||||
1353 | } | ||||
1354 | |||||
1355 | const APInt *MulC; | ||||
1356 | if (match(Op0, m_NUWMul(m_Value(X), m_APInt(MulC)))) { | ||||
1357 | // Look for a "splat" mul pattern - it replicates bits across each half of | ||||
1358 | // a value, so a right shift is just a mask of the low bits: | ||||
1359 | // lshr i[2N] (mul nuw X, (2^N)+1), N --> and iN X, (2^N)-1 | ||||
1360 | // TODO: Generalize to allow more than just half-width shifts? | ||||
1361 | if (BitWidth > 2 && ShAmtC * 2 == BitWidth && (*MulC - 1).isPowerOf2() && | ||||
1362 | MulC->logBase2() == ShAmtC) | ||||
1363 | return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2)); | ||||
1364 | |||||
1365 | // The one-use check is not strictly necessary, but codegen may not be | ||||
1366 | // able to invert the transform and perf may suffer with an extra mul | ||||
1367 | // instruction. | ||||
1368 | if (Op0->hasOneUse()) { | ||||
1369 | APInt NewMulC = MulC->lshr(ShAmtC); | ||||
1370 | // if c is divisible by (1 << ShAmtC): | ||||
1371 | // lshr (mul nuw x, MulC), ShAmtC -> mul nuw x, (MulC >> ShAmtC) | ||||
1372 | if (MulC->eq(NewMulC.shl(ShAmtC))) { | ||||
1373 | auto *NewMul = | ||||
1374 | BinaryOperator::CreateNUWMul(X, ConstantInt::get(Ty, NewMulC)); | ||||
1375 | BinaryOperator *OrigMul = cast<BinaryOperator>(Op0); | ||||
1376 | NewMul->setHasNoSignedWrap(OrigMul->hasNoSignedWrap()); | ||||
1377 | return NewMul; | ||||
1378 | } | ||||
1379 | } | ||||
1380 | } | ||||
1381 | |||||
1382 | // Try to narrow bswap. | ||||
1383 | // In the case where the shift amount equals the bitwidth difference, the | ||||
1384 | // shift is eliminated. | ||||
1385 | if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::bswap>( | ||||
1386 | m_OneUse(m_ZExt(m_Value(X))))))) { | ||||
1387 | unsigned SrcWidth = X->getType()->getScalarSizeInBits(); | ||||
1388 | unsigned WidthDiff = BitWidth - SrcWidth; | ||||
1389 | if (SrcWidth % 16 == 0) { | ||||
1390 | Value *NarrowSwap = Builder.CreateUnaryIntrinsic(Intrinsic::bswap, X); | ||||
1391 | if (ShAmtC >= WidthDiff) { | ||||
1392 | // (bswap (zext X)) >> C --> zext (bswap X >> C') | ||||
1393 | Value *NewShift = Builder.CreateLShr(NarrowSwap, ShAmtC - WidthDiff); | ||||
1394 | return new ZExtInst(NewShift, Ty); | ||||
1395 | } else { | ||||
1396 | // (bswap (zext X)) >> C --> (zext (bswap X)) << C' | ||||
1397 | Value *NewZExt = Builder.CreateZExt(NarrowSwap, Ty); | ||||
1398 | Constant *ShiftDiff = ConstantInt::get(Ty, WidthDiff - ShAmtC); | ||||
1399 | return BinaryOperator::CreateShl(NewZExt, ShiftDiff); | ||||
1400 | } | ||||
1401 | } | ||||
1402 | } | ||||
1403 | |||||
1404 | // Reduce add-carry of bools to logic: | ||||
1405 | // ((zext BoolX) + (zext BoolY)) >> 1 --> zext (BoolX && BoolY) | ||||
1406 | Value *BoolX, *BoolY; | ||||
1407 | if (ShAmtC == 1 && match(Op0, m_Add(m_Value(X), m_Value(Y))) && | ||||
1408 | match(X, m_ZExt(m_Value(BoolX))) && match(Y, m_ZExt(m_Value(BoolY))) && | ||||
1409 | BoolX->getType()->isIntOrIntVectorTy(1) && | ||||
1410 | BoolY->getType()->isIntOrIntVectorTy(1) && | ||||
1411 | (X->hasOneUse() || Y->hasOneUse() || Op0->hasOneUse())) { | ||||
1412 | Value *And = Builder.CreateAnd(BoolX, BoolY); | ||||
1413 | return new ZExtInst(And, Ty); | ||||
1414 | } | ||||
1415 | |||||
1416 | // If the shifted-out value is known-zero, then this is an exact shift. | ||||
1417 | if (!I.isExact() && | ||||
1418 | MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmtC), 0, &I)) { | ||||
1419 | I.setIsExact(); | ||||
1420 | return &I; | ||||
1421 | } | ||||
1422 | } | ||||
1423 | |||||
1424 | // Transform (x << y) >> y to x & (-1 >> y) | ||||
1425 | if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) { | ||||
1426 | Constant *AllOnes = ConstantInt::getAllOnesValue(Ty); | ||||
1427 | Value *Mask = Builder.CreateLShr(AllOnes, Op1); | ||||
1428 | return BinaryOperator::CreateAnd(Mask, X); | ||||
1429 | } | ||||
1430 | |||||
1431 | if (Instruction *Overflow = foldLShrOverflowBit(I)) | ||||
1432 | return Overflow; | ||||
1433 | |||||
1434 | return nullptr; | ||||
1435 | } | ||||
1436 | |||||
1437 | Instruction * | ||||
1438 | InstCombinerImpl::foldVariableSignZeroExtensionOfVariableHighBitExtract( | ||||
1439 | BinaryOperator &OldAShr) { | ||||
1440 | assert(OldAShr.getOpcode() == Instruction::AShr &&(static_cast <bool> (OldAShr.getOpcode() == Instruction ::AShr && "Must be called with arithmetic right-shift instruction only." ) ? void (0) : __assert_fail ("OldAShr.getOpcode() == Instruction::AShr && \"Must be called with arithmetic right-shift instruction only.\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 1441 , __extension__ __PRETTY_FUNCTION__)) | ||||
1441 | "Must be called with arithmetic right-shift instruction only.")(static_cast <bool> (OldAShr.getOpcode() == Instruction ::AShr && "Must be called with arithmetic right-shift instruction only." ) ? void (0) : __assert_fail ("OldAShr.getOpcode() == Instruction::AShr && \"Must be called with arithmetic right-shift instruction only.\"" , "llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp", 1441 , __extension__ __PRETTY_FUNCTION__)); | ||||
1442 | |||||
1443 | // Check that constant C is a splat of the element-wise bitwidth of V. | ||||
1444 | auto BitWidthSplat = [](Constant *C, Value *V) { | ||||
1445 | return match( | ||||
1446 | C, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ, | ||||
1447 | APInt(C->getType()->getScalarSizeInBits(), | ||||
1448 | V->getType()->getScalarSizeInBits()))); | ||||
1449 | }; | ||||
1450 | |||||
1451 | // It should look like variable-length sign-extension on the outside: | ||||
1452 | // (Val << (bitwidth(Val)-Nbits)) a>> (bitwidth(Val)-Nbits) | ||||
1453 | Value *NBits; | ||||
1454 | Instruction *MaybeTrunc; | ||||
1455 | Constant *C1, *C2; | ||||
1456 | if (!match(&OldAShr, | ||||
1457 | m_AShr(m_Shl(m_Instruction(MaybeTrunc), | ||||
1458 | m_ZExtOrSelf(m_Sub(m_Constant(C1), | ||||
1459 | m_ZExtOrSelf(m_Value(NBits))))), | ||||
1460 | m_ZExtOrSelf(m_Sub(m_Constant(C2), | ||||
1461 | m_ZExtOrSelf(m_Deferred(NBits)))))) || | ||||
1462 | !BitWidthSplat(C1, &OldAShr) || !BitWidthSplat(C2, &OldAShr)) | ||||
1463 | return nullptr; | ||||
1464 | |||||
1465 | // There may or may not be a truncation after outer two shifts. | ||||
1466 | Instruction *HighBitExtract; | ||||
1467 | match(MaybeTrunc, m_TruncOrSelf(m_Instruction(HighBitExtract))); | ||||
1468 | bool HadTrunc = MaybeTrunc != HighBitExtract; | ||||
1469 | |||||
1470 | // And finally, the innermost part of the pattern must be a right-shift. | ||||
1471 | Value *X, *NumLowBitsToSkip; | ||||
1472 | if (!match(HighBitExtract, m_Shr(m_Value(X), m_Value(NumLowBitsToSkip)))) | ||||
1473 | return nullptr; | ||||
1474 | |||||
1475 | // Said right-shift must extract high NBits bits - C0 must be it's bitwidth. | ||||
1476 | Constant *C0; | ||||
1477 | if (!match(NumLowBitsToSkip, | ||||
1478 | m_ZExtOrSelf( | ||||
1479 | m_Sub(m_Constant(C0), m_ZExtOrSelf(m_Specific(NBits))))) || | ||||
1480 | !BitWidthSplat(C0, HighBitExtract)) | ||||
1481 | return nullptr; | ||||
1482 | |||||
1483 | // Since the NBits is identical for all shifts, if the outermost and | ||||
1484 | // innermost shifts are identical, then outermost shifts are redundant. | ||||
1485 | // If we had truncation, do keep it though. | ||||
1486 | if (HighBitExtract->getOpcode() == OldAShr.getOpcode()) | ||||
1487 | return replaceInstUsesWith(OldAShr, MaybeTrunc); | ||||
1488 | |||||
1489 | // Else, if there was a truncation, then we need to ensure that one | ||||
1490 | // instruction will go away. | ||||
1491 | if (HadTrunc && !match(&OldAShr, m_c_BinOp(m_OneUse(m_Value()), m_Value()))) | ||||
1492 | return nullptr; | ||||
1493 | |||||
1494 | // Finally, bypass two innermost shifts, and perform the outermost shift on | ||||
1495 | // the operands of the innermost shift. | ||||
1496 | Instruction *NewAShr = | ||||
1497 | BinaryOperator::Create(OldAShr.getOpcode(), X, NumLowBitsToSkip); | ||||
1498 | NewAShr->copyIRFlags(HighBitExtract); // We can preserve 'exact'-ness. | ||||
1499 | if (!HadTrunc) | ||||
1500 | return NewAShr; | ||||
1501 | |||||
1502 | Builder.Insert(NewAShr); | ||||
1503 | return TruncInst::CreateTruncOrBitCast(NewAShr, OldAShr.getType()); | ||||
1504 | } | ||||
1505 | |||||
1506 | Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) { | ||||
1507 | if (Value *V = simplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(), | ||||
1508 | SQ.getWithInstruction(&I))) | ||||
1509 | return replaceInstUsesWith(I, V); | ||||
1510 | |||||
1511 | if (Instruction *X = foldVectorBinop(I)) | ||||
1512 | return X; | ||||
1513 | |||||
1514 | if (Instruction *R = commonShiftTransforms(I)) | ||||
1515 | return R; | ||||
1516 | |||||
1517 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); | ||||
1518 | Type *Ty = I.getType(); | ||||
1519 | unsigned BitWidth = Ty->getScalarSizeInBits(); | ||||
1520 | const APInt *ShAmtAPInt; | ||||
1521 | if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) { | ||||
1522 | unsigned ShAmt = ShAmtAPInt->getZExtValue(); | ||||
1523 | |||||
1524 | // If the shift amount equals the difference in width of the destination | ||||
1525 | // and source scalar types: | ||||
1526 | // ashr (shl (zext X), C), C --> sext X | ||||
1527 | Value *X; | ||||
1528 | if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) && | ||||
1529 | ShAmt == BitWidth - X->getType()->getScalarSizeInBits()) | ||||
1530 | return new SExtInst(X, Ty); | ||||
1531 | |||||
1532 | // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However, | ||||
1533 | // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits. | ||||
1534 | const APInt *ShOp1; | ||||
1535 | if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) && | ||||
1536 | ShOp1->ult(BitWidth)) { | ||||
1537 | unsigned ShlAmt = ShOp1->getZExtValue(); | ||||
1538 | if (ShlAmt < ShAmt) { | ||||
1539 | // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1) | ||||
1540 | Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt); | ||||
1541 | auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff); | ||||
1542 | NewAShr->setIsExact(I.isExact()); | ||||
1543 | return NewAShr; | ||||
1544 | } | ||||
1545 | if (ShlAmt > ShAmt) { | ||||
1546 | // (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2) | ||||
1547 | Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt); | ||||
1548 | auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff); | ||||
1549 | NewShl->setHasNoSignedWrap(true); | ||||
1550 | return NewShl; | ||||
1551 | } | ||||
1552 | } | ||||
1553 | |||||
1554 | if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) && | ||||
1555 | ShOp1->ult(BitWidth)) { | ||||
1556 | unsigned AmtSum = ShAmt + ShOp1->getZExtValue(); | ||||
1557 | // Oversized arithmetic shifts replicate the sign bit. | ||||
1558 | AmtSum = std::min(AmtSum, BitWidth - 1); | ||||
1559 | // (X >>s C1) >>s C2 --> X >>s (C1 + C2) | ||||
1560 | return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum)); | ||||
1561 | } | ||||
1562 | |||||
1563 | if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) && | ||||
1564 | (Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) { | ||||
1565 | // ashr (sext X), C --> sext (ashr X, C') | ||||
1566 | Type *SrcTy = X->getType(); | ||||
1567 | ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1); | ||||
1568 | Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt)); | ||||
1569 | return new SExtInst(NewSh, Ty); | ||||
1570 | } | ||||
1571 | |||||
1572 | if (ShAmt == BitWidth - 1) { | ||||
1573 | // ashr i32 or(X,-X), 31 --> sext (X != 0) | ||||
1574 | if (match(Op0, m_OneUse(m_c_Or(m_Neg(m_Value(X)), m_Deferred(X))))) | ||||
1575 | return new SExtInst(Builder.CreateIsNotNull(X), Ty); | ||||
1576 | |||||
1577 | // ashr i32 (X -nsw Y), 31 --> sext (X < Y) | ||||
1578 | Value *Y; | ||||
1579 | if (match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y))))) | ||||
1580 | return new SExtInst(Builder.CreateICmpSLT(X, Y), Ty); | ||||
1581 | } | ||||
1582 | |||||
1583 | // If the shifted-out value is known-zero, then this is an exact shift. | ||||
1584 | if (!I.isExact() && | ||||
1585 | MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) { | ||||
1586 | I.setIsExact(); | ||||
1587 | return &I; | ||||
1588 | } | ||||
1589 | } | ||||
1590 | |||||
1591 | // Prefer `-(x & 1)` over `(x << (bitwidth(x)-1)) a>> (bitwidth(x)-1)` | ||||
1592 | // as the pattern to splat the lowest bit. | ||||
1593 | // FIXME: iff X is already masked, we don't need the one-use check. | ||||
1594 | Value *X; | ||||
1595 | if (match(Op1, m_SpecificIntAllowUndef(BitWidth - 1)) && | ||||
1596 | match(Op0, m_OneUse(m_Shl(m_Value(X), | ||||
1597 | m_SpecificIntAllowUndef(BitWidth - 1))))) { | ||||
1598 | Constant *Mask = ConstantInt::get(Ty, 1); | ||||
1599 | // Retain the knowledge about the ignored lanes. | ||||
1600 | Mask = Constant::mergeUndefsWith( | ||||
1601 | Constant::mergeUndefsWith(Mask, cast<Constant>(Op1)), | ||||
1602 | cast<Constant>(cast<Instruction>(Op0)->getOperand(1))); | ||||
1603 | X = Builder.CreateAnd(X, Mask); | ||||
1604 | return BinaryOperator::CreateNeg(X); | ||||
1605 | } | ||||
1606 | |||||
1607 | if (Instruction *R = foldVariableSignZeroExtensionOfVariableHighBitExtract(I)) | ||||
1608 | return R; | ||||
1609 | |||||
1610 | // See if we can turn a signed shr into an unsigned shr. | ||||
1611 | if (MaskedValueIsZero(Op0, APInt::getSignMask(BitWidth), 0, &I)) { | ||||
1612 | Instruction *Lshr = BinaryOperator::CreateLShr(Op0, Op1); | ||||
1613 | Lshr->setIsExact(I.isExact()); | ||||
1614 | return Lshr; | ||||
1615 | } | ||||
1616 | |||||
1617 | // ashr (xor %x, -1), %y --> xor (ashr %x, %y), -1 | ||||
1618 | if (match(Op0, m_OneUse(m_Not(m_Value(X))))) { | ||||
1619 | // Note that we must drop 'exact'-ness of the shift! | ||||
1620 | // Note that we can't keep undef's in -1 vector constant! | ||||
1621 | auto *NewAShr = Builder.CreateAShr(X, Op1, Op0->getName() + ".not"); | ||||
1622 | return BinaryOperator::CreateNot(NewAShr); | ||||
1623 | } | ||||
1624 | |||||
1625 | return nullptr; | ||||
1626 | } |
1 | //===- PatternMatch.h - Match on the LLVM IR --------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file provides a simple and efficient mechanism for performing general |
10 | // tree-based pattern matches on the LLVM IR. The power of these routines is |
11 | // that it allows you to write concise patterns that are expressive and easy to |
12 | // understand. The other major advantage of this is that it allows you to |
13 | // trivially capture/bind elements in the pattern to variables. For example, |
14 | // you can do something like this: |
15 | // |
16 | // Value *Exp = ... |
17 | // Value *X, *Y; ConstantInt *C1, *C2; // (X & C1) | (Y & C2) |
18 | // if (match(Exp, m_Or(m_And(m_Value(X), m_ConstantInt(C1)), |
19 | // m_And(m_Value(Y), m_ConstantInt(C2))))) { |
20 | // ... Pattern is matched and variables are bound ... |
21 | // } |
22 | // |
23 | // This is primarily useful to things like the instruction combiner, but can |
24 | // also be useful for static analysis tools or code generators. |
25 | // |
26 | //===----------------------------------------------------------------------===// |
27 | |
28 | #ifndef LLVM_IR_PATTERNMATCH_H |
29 | #define LLVM_IR_PATTERNMATCH_H |
30 | |
31 | #include "llvm/ADT/APFloat.h" |
32 | #include "llvm/ADT/APInt.h" |
33 | #include "llvm/IR/Constant.h" |
34 | #include "llvm/IR/Constants.h" |
35 | #include "llvm/IR/DataLayout.h" |
36 | #include "llvm/IR/InstrTypes.h" |
37 | #include "llvm/IR/Instruction.h" |
38 | #include "llvm/IR/Instructions.h" |
39 | #include "llvm/IR/IntrinsicInst.h" |
40 | #include "llvm/IR/Intrinsics.h" |
41 | #include "llvm/IR/Operator.h" |
42 | #include "llvm/IR/Value.h" |
43 | #include "llvm/Support/Casting.h" |
44 | #include <cstdint> |
45 | |
46 | namespace llvm { |
47 | namespace PatternMatch { |
48 | |
49 | template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) { |
50 | return const_cast<Pattern &>(P).match(V); |
51 | } |
52 | |
53 | template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) { |
54 | return const_cast<Pattern &>(P).match(Mask); |
55 | } |
56 | |
57 | template <typename SubPattern_t> struct OneUse_match { |
58 | SubPattern_t SubPattern; |
59 | |
60 | OneUse_match(const SubPattern_t &SP) : SubPattern(SP) {} |
61 | |
62 | template <typename OpTy> bool match(OpTy *V) { |
63 | return V->hasOneUse() && SubPattern.match(V); |
64 | } |
65 | }; |
66 | |
67 | template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) { |
68 | return SubPattern; |
69 | } |
70 | |
71 | template <typename Class> struct class_match { |
72 | template <typename ITy> bool match(ITy *V) { return isa<Class>(V); } |
73 | }; |
74 | |
75 | /// Match an arbitrary value and ignore it. |
76 | inline class_match<Value> m_Value() { return class_match<Value>(); } |
77 | |
78 | /// Match an arbitrary unary operation and ignore it. |
79 | inline class_match<UnaryOperator> m_UnOp() { |
80 | return class_match<UnaryOperator>(); |
81 | } |
82 | |
83 | /// Match an arbitrary binary operation and ignore it. |
84 | inline class_match<BinaryOperator> m_BinOp() { |
85 | return class_match<BinaryOperator>(); |
86 | } |
87 | |
88 | /// Matches any compare instruction and ignore it. |
89 | inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); } |
90 | |
91 | struct undef_match { |
92 | static bool check(const Value *V) { |
93 | if (isa<UndefValue>(V)) |
94 | return true; |
95 | |
96 | const auto *CA = dyn_cast<ConstantAggregate>(V); |
97 | if (!CA) |
98 | return false; |
99 | |
100 | SmallPtrSet<const ConstantAggregate *, 8> Seen; |
101 | SmallVector<const ConstantAggregate *, 8> Worklist; |
102 | |
103 | // Either UndefValue, PoisonValue, or an aggregate that only contains |
104 | // these is accepted by matcher. |
105 | // CheckValue returns false if CA cannot satisfy this constraint. |
106 | auto CheckValue = [&](const ConstantAggregate *CA) { |
107 | for (const Value *Op : CA->operand_values()) { |
108 | if (isa<UndefValue>(Op)) |
109 | continue; |
110 | |
111 | const auto *CA = dyn_cast<ConstantAggregate>(Op); |
112 | if (!CA) |
113 | return false; |
114 | if (Seen.insert(CA).second) |
115 | Worklist.emplace_back(CA); |
116 | } |
117 | |
118 | return true; |
119 | }; |
120 | |
121 | if (!CheckValue(CA)) |
122 | return false; |
123 | |
124 | while (!Worklist.empty()) { |
125 | if (!CheckValue(Worklist.pop_back_val())) |
126 | return false; |
127 | } |
128 | return true; |
129 | } |
130 | template <typename ITy> bool match(ITy *V) { return check(V); } |
131 | }; |
132 | |
133 | /// Match an arbitrary undef constant. This matches poison as well. |
134 | /// If this is an aggregate and contains a non-aggregate element that is |
135 | /// neither undef nor poison, the aggregate is not matched. |
136 | inline auto m_Undef() { return undef_match(); } |
137 | |
138 | /// Match an arbitrary poison constant. |
139 | inline class_match<PoisonValue> m_Poison() { |
140 | return class_match<PoisonValue>(); |
141 | } |
142 | |
143 | /// Match an arbitrary Constant and ignore it. |
144 | inline class_match<Constant> m_Constant() { return class_match<Constant>(); } |
145 | |
146 | /// Match an arbitrary ConstantInt and ignore it. |
147 | inline class_match<ConstantInt> m_ConstantInt() { |
148 | return class_match<ConstantInt>(); |
149 | } |
150 | |
151 | /// Match an arbitrary ConstantFP and ignore it. |
152 | inline class_match<ConstantFP> m_ConstantFP() { |
153 | return class_match<ConstantFP>(); |
154 | } |
155 | |
156 | struct constantexpr_match { |
157 | template <typename ITy> bool match(ITy *V) { |
158 | auto *C = dyn_cast<Constant>(V); |
159 | return C && (isa<ConstantExpr>(C) || C->containsConstantExpression()); |
160 | } |
161 | }; |
162 | |
163 | /// Match a constant expression or a constant that contains a constant |
164 | /// expression. |
165 | inline constantexpr_match m_ConstantExpr() { return constantexpr_match(); } |
166 | |
167 | /// Match an arbitrary basic block value and ignore it. |
168 | inline class_match<BasicBlock> m_BasicBlock() { |
169 | return class_match<BasicBlock>(); |
170 | } |
171 | |
172 | /// Inverting matcher |
173 | template <typename Ty> struct match_unless { |
174 | Ty M; |
175 | |
176 | match_unless(const Ty &Matcher) : M(Matcher) {} |
177 | |
178 | template <typename ITy> bool match(ITy *V) { return !M.match(V); } |
179 | }; |
180 | |
181 | /// Match if the inner matcher does *NOT* match. |
182 | template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) { |
183 | return match_unless<Ty>(M); |
184 | } |
185 | |
186 | /// Matching combinators |
187 | template <typename LTy, typename RTy> struct match_combine_or { |
188 | LTy L; |
189 | RTy R; |
190 | |
191 | match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} |
192 | |
193 | template <typename ITy> bool match(ITy *V) { |
194 | if (L.match(V)) |
195 | return true; |
196 | if (R.match(V)) |
197 | return true; |
198 | return false; |
199 | } |
200 | }; |
201 | |
202 | template <typename LTy, typename RTy> struct match_combine_and { |
203 | LTy L; |
204 | RTy R; |
205 | |
206 | match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {} |
207 | |
208 | template <typename ITy> bool match(ITy *V) { |
209 | if (L.match(V)) |
210 | if (R.match(V)) |
211 | return true; |
212 | return false; |
213 | } |
214 | }; |
215 | |
216 | /// Combine two pattern matchers matching L || R |
217 | template <typename LTy, typename RTy> |
218 | inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) { |
219 | return match_combine_or<LTy, RTy>(L, R); |
220 | } |
221 | |
222 | /// Combine two pattern matchers matching L && R |
223 | template <typename LTy, typename RTy> |
224 | inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) { |
225 | return match_combine_and<LTy, RTy>(L, R); |
226 | } |
227 | |
228 | struct apint_match { |
229 | const APInt *&Res; |
230 | bool AllowUndef; |
231 | |
232 | apint_match(const APInt *&Res, bool AllowUndef) |
233 | : Res(Res), AllowUndef(AllowUndef) {} |
234 | |
235 | template <typename ITy> bool match(ITy *V) { |
236 | if (auto *CI = dyn_cast<ConstantInt>(V)) { |
237 | Res = &CI->getValue(); |
238 | return true; |
239 | } |
240 | if (V->getType()->isVectorTy()) |
241 | if (const auto *C = dyn_cast<Constant>(V)) |
242 | if (auto *CI = |
243 | dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndef))) { |
244 | Res = &CI->getValue(); |
245 | return true; |
246 | } |
247 | return false; |
248 | } |
249 | }; |
250 | // Either constexpr if or renaming ConstantFP::getValueAPF to |
251 | // ConstantFP::getValue is needed to do it via single template |
252 | // function for both apint/apfloat. |
253 | struct apfloat_match { |
254 | const APFloat *&Res; |
255 | bool AllowUndef; |
256 | |
257 | apfloat_match(const APFloat *&Res, bool AllowUndef) |
258 | : Res(Res), AllowUndef(AllowUndef) {} |
259 | |
260 | template <typename ITy> bool match(ITy *V) { |
261 | if (auto *CI = dyn_cast<ConstantFP>(V)) { |
262 | Res = &CI->getValueAPF(); |
263 | return true; |
264 | } |
265 | if (V->getType()->isVectorTy()) |
266 | if (const auto *C = dyn_cast<Constant>(V)) |
267 | if (auto *CI = |
268 | dyn_cast_or_null<ConstantFP>(C->getSplatValue(AllowUndef))) { |
269 | Res = &CI->getValueAPF(); |
270 | return true; |
271 | } |
272 | return false; |
273 | } |
274 | }; |
275 | |
276 | /// Match a ConstantInt or splatted ConstantVector, binding the |
277 | /// specified pointer to the contained APInt. |
278 | inline apint_match m_APInt(const APInt *&Res) { |
279 | // Forbid undefs by default to maintain previous behavior. |
280 | return apint_match(Res, /* AllowUndef */ false); |
281 | } |
282 | |
283 | /// Match APInt while allowing undefs in splat vector constants. |
284 | inline apint_match m_APIntAllowUndef(const APInt *&Res) { |
285 | return apint_match(Res, /* AllowUndef */ true); |
286 | } |
287 | |
288 | /// Match APInt while forbidding undefs in splat vector constants. |
289 | inline apint_match m_APIntForbidUndef(const APInt *&Res) { |
290 | return apint_match(Res, /* AllowUndef */ false); |
291 | } |
292 | |
293 | /// Match a ConstantFP or splatted ConstantVector, binding the |
294 | /// specified pointer to the contained APFloat. |
295 | inline apfloat_match m_APFloat(const APFloat *&Res) { |
296 | // Forbid undefs by default to maintain previous behavior. |
297 | return apfloat_match(Res, /* AllowUndef */ false); |
298 | } |
299 | |
300 | /// Match APFloat while allowing undefs in splat vector constants. |
301 | inline apfloat_match m_APFloatAllowUndef(const APFloat *&Res) { |
302 | return apfloat_match(Res, /* AllowUndef */ true); |
303 | } |
304 | |
305 | /// Match APFloat while forbidding undefs in splat vector constants. |
306 | inline apfloat_match m_APFloatForbidUndef(const APFloat *&Res) { |
307 | return apfloat_match(Res, /* AllowUndef */ false); |
308 | } |
309 | |
310 | template <int64_t Val> struct constantint_match { |
311 | template <typename ITy> bool match(ITy *V) { |
312 | if (const auto *CI = dyn_cast<ConstantInt>(V)) { |
313 | const APInt &CIV = CI->getValue(); |
314 | if (Val >= 0) |
315 | return CIV == static_cast<uint64_t>(Val); |
316 | // If Val is negative, and CI is shorter than it, truncate to the right |
317 | // number of bits. If it is larger, then we have to sign extend. Just |
318 | // compare their negated values. |
319 | return -CIV == -Val; |
320 | } |
321 | return false; |
322 | } |
323 | }; |
324 | |
325 | /// Match a ConstantInt with a specific value. |
326 | template <int64_t Val> inline constantint_match<Val> m_ConstantInt() { |
327 | return constantint_match<Val>(); |
328 | } |
329 | |
330 | /// This helper class is used to match constant scalars, vector splats, |
331 | /// and fixed width vectors that satisfy a specified predicate. |
332 | /// For fixed width vector constants, undefined elements are ignored. |
333 | template <typename Predicate, typename ConstantVal> |
334 | struct cstval_pred_ty : public Predicate { |
335 | template <typename ITy> bool match(ITy *V) { |
336 | if (const auto *CV = dyn_cast<ConstantVal>(V)) |
337 | return this->isValue(CV->getValue()); |
338 | if (const auto *VTy = dyn_cast<VectorType>(V->getType())) { |
339 | if (const auto *C = dyn_cast<Constant>(V)) { |
340 | if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue())) |
341 | return this->isValue(CV->getValue()); |
342 | |
343 | // Number of elements of a scalable vector unknown at compile time |
344 | auto *FVTy = dyn_cast<FixedVectorType>(VTy); |
345 | if (!FVTy) |
346 | return false; |
347 | |
348 | // Non-splat vector constant: check each element for a match. |
349 | unsigned NumElts = FVTy->getNumElements(); |
350 | assert(NumElts != 0 && "Constant vector with no elements?")(static_cast <bool> (NumElts != 0 && "Constant vector with no elements?" ) ? void (0) : __assert_fail ("NumElts != 0 && \"Constant vector with no elements?\"" , "llvm/include/llvm/IR/PatternMatch.h", 350, __extension__ __PRETTY_FUNCTION__ )); |
351 | bool HasNonUndefElements = false; |
352 | for (unsigned i = 0; i != NumElts; ++i) { |
353 | Constant *Elt = C->getAggregateElement(i); |
354 | if (!Elt) |
355 | return false; |
356 | if (isa<UndefValue>(Elt)) |
357 | continue; |
358 | auto *CV = dyn_cast<ConstantVal>(Elt); |
359 | if (!CV || !this->isValue(CV->getValue())) |
360 | return false; |
361 | HasNonUndefElements = true; |
362 | } |
363 | return HasNonUndefElements; |
364 | } |
365 | } |
366 | return false; |
367 | } |
368 | }; |
369 | |
370 | /// specialization of cstval_pred_ty for ConstantInt |
371 | template <typename Predicate> |
372 | using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt>; |
373 | |
374 | /// specialization of cstval_pred_ty for ConstantFP |
375 | template <typename Predicate> |
376 | using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP>; |
377 | |
378 | /// This helper class is used to match scalar and vector constants that |
379 | /// satisfy a specified predicate, and bind them to an APInt. |
380 | template <typename Predicate> struct api_pred_ty : public Predicate { |
381 | const APInt *&Res; |
382 | |
383 | api_pred_ty(const APInt *&R) : Res(R) {} |
384 | |
385 | template <typename ITy> bool match(ITy *V) { |
386 | if (const auto *CI = dyn_cast<ConstantInt>(V)) |
387 | if (this->isValue(CI->getValue())) { |
388 | Res = &CI->getValue(); |
389 | return true; |
390 | } |
391 | if (V->getType()->isVectorTy()) |
392 | if (const auto *C = dyn_cast<Constant>(V)) |
393 | if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue())) |
394 | if (this->isValue(CI->getValue())) { |
395 | Res = &CI->getValue(); |
396 | return true; |
397 | } |
398 | |
399 | return false; |
400 | } |
401 | }; |
402 | |
403 | /// This helper class is used to match scalar and vector constants that |
404 | /// satisfy a specified predicate, and bind them to an APFloat. |
405 | /// Undefs are allowed in splat vector constants. |
406 | template <typename Predicate> struct apf_pred_ty : public Predicate { |
407 | const APFloat *&Res; |
408 | |
409 | apf_pred_ty(const APFloat *&R) : Res(R) {} |
410 | |
411 | template <typename ITy> bool match(ITy *V) { |
412 | if (const auto *CI = dyn_cast<ConstantFP>(V)) |
413 | if (this->isValue(CI->getValue())) { |
414 | Res = &CI->getValue(); |
415 | return true; |
416 | } |
417 | if (V->getType()->isVectorTy()) |
418 | if (const auto *C = dyn_cast<Constant>(V)) |
419 | if (auto *CI = dyn_cast_or_null<ConstantFP>( |
420 | C->getSplatValue(/* AllowUndef */ true))) |
421 | if (this->isValue(CI->getValue())) { |
422 | Res = &CI->getValue(); |
423 | return true; |
424 | } |
425 | |
426 | return false; |
427 | } |
428 | }; |
429 | |
430 | /////////////////////////////////////////////////////////////////////////////// |
431 | // |
432 | // Encapsulate constant value queries for use in templated predicate matchers. |
433 | // This allows checking if constants match using compound predicates and works |
434 | // with vector constants, possibly with relaxed constraints. For example, ignore |
435 | // undef values. |
436 | // |
437 | /////////////////////////////////////////////////////////////////////////////// |
438 | |
439 | struct is_any_apint { |
440 | bool isValue(const APInt &C) { return true; } |
441 | }; |
442 | /// Match an integer or vector with any integral constant. |
443 | /// For vectors, this includes constants with undefined elements. |
444 | inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() { |
445 | return cst_pred_ty<is_any_apint>(); |
446 | } |
447 | |
448 | struct is_all_ones { |
449 | bool isValue(const APInt &C) { return C.isAllOnes(); } |
450 | }; |
451 | /// Match an integer or vector with all bits set. |
452 | /// For vectors, this includes constants with undefined elements. |
453 | inline cst_pred_ty<is_all_ones> m_AllOnes() { |
454 | return cst_pred_ty<is_all_ones>(); |
455 | } |
456 | |
457 | struct is_maxsignedvalue { |
458 | bool isValue(const APInt &C) { return C.isMaxSignedValue(); } |
459 | }; |
460 | /// Match an integer or vector with values having all bits except for the high |
461 | /// bit set (0x7f...). |
462 | /// For vectors, this includes constants with undefined elements. |
463 | inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { |
464 | return cst_pred_ty<is_maxsignedvalue>(); |
465 | } |
466 | inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { |
467 | return V; |
468 | } |
469 | |
470 | struct is_negative { |
471 | bool isValue(const APInt &C) { return C.isNegative(); } |
472 | }; |
473 | /// Match an integer or vector of negative values. |
474 | /// For vectors, this includes constants with undefined elements. |
475 | inline cst_pred_ty<is_negative> m_Negative() { |
476 | return cst_pred_ty<is_negative>(); |
477 | } |
478 | inline api_pred_ty<is_negative> m_Negative(const APInt *&V) { return V; } |
479 | |
480 | struct is_nonnegative { |
481 | bool isValue(const APInt &C) { return C.isNonNegative(); } |
482 | }; |
483 | /// Match an integer or vector of non-negative values. |
484 | /// For vectors, this includes constants with undefined elements. |
485 | inline cst_pred_ty<is_nonnegative> m_NonNegative() { |
486 | return cst_pred_ty<is_nonnegative>(); |
487 | } |
488 | inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) { return V; } |
489 | |
490 | struct is_strictlypositive { |
491 | bool isValue(const APInt &C) { return C.isStrictlyPositive(); } |
492 | }; |
493 | /// Match an integer or vector of strictly positive values. |
494 | /// For vectors, this includes constants with undefined elements. |
495 | inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() { |
496 | return cst_pred_ty<is_strictlypositive>(); |
497 | } |
498 | inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) { |
499 | return V; |
500 | } |
501 | |
502 | struct is_nonpositive { |
503 | bool isValue(const APInt &C) { return C.isNonPositive(); } |
504 | }; |
505 | /// Match an integer or vector of non-positive values. |
506 | /// For vectors, this includes constants with undefined elements. |
507 | inline cst_pred_ty<is_nonpositive> m_NonPositive() { |
508 | return cst_pred_ty<is_nonpositive>(); |
509 | } |
510 | inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; } |
511 | |
512 | struct is_one { |
513 | bool isValue(const APInt &C) { return C.isOne(); } |
514 | }; |
515 | /// Match an integer 1 or a vector with all elements equal to 1. |
516 | /// For vectors, this includes constants with undefined elements. |
517 | inline cst_pred_ty<is_one> m_One() { return cst_pred_ty<is_one>(); } |
518 | |
519 | struct is_zero_int { |
520 | bool isValue(const APInt &C) { return C.isZero(); } |
521 | }; |
522 | /// Match an integer 0 or a vector with all elements equal to 0. |
523 | /// For vectors, this includes constants with undefined elements. |
524 | inline cst_pred_ty<is_zero_int> m_ZeroInt() { |
525 | return cst_pred_ty<is_zero_int>(); |
526 | } |
527 | |
528 | struct is_zero { |
529 | template <typename ITy> bool match(ITy *V) { |
530 | auto *C = dyn_cast<Constant>(V); |
531 | // FIXME: this should be able to do something for scalable vectors |
532 | return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C)); |
533 | } |
534 | }; |
535 | /// Match any null constant or a vector with all elements equal to 0. |
536 | /// For vectors, this includes constants with undefined elements. |
537 | inline is_zero m_Zero() { return is_zero(); } |
538 | |
539 | struct is_power2 { |
540 | bool isValue(const APInt &C) { return C.isPowerOf2(); } |
541 | }; |
542 | /// Match an integer or vector power-of-2. |
543 | /// For vectors, this includes constants with undefined elements. |
544 | inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); } |
545 | inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; } |
546 | |
547 | struct is_negated_power2 { |
548 | bool isValue(const APInt &C) { return C.isNegatedPowerOf2(); } |
549 | }; |
550 | /// Match a integer or vector negated power-of-2. |
551 | /// For vectors, this includes constants with undefined elements. |
552 | inline cst_pred_ty<is_negated_power2> m_NegatedPower2() { |
553 | return cst_pred_ty<is_negated_power2>(); |
554 | } |
555 | inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) { |
556 | return V; |
557 | } |
558 | |
559 | struct is_power2_or_zero { |
560 | bool isValue(const APInt &C) { return !C || C.isPowerOf2(); } |
561 | }; |
562 | /// Match an integer or vector of 0 or power-of-2 values. |
563 | /// For vectors, this includes constants with undefined elements. |
564 | inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() { |
565 | return cst_pred_ty<is_power2_or_zero>(); |
566 | } |
567 | inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) { |
568 | return V; |
569 | } |
570 | |
571 | struct is_sign_mask { |
572 | bool isValue(const APInt &C) { return C.isSignMask(); } |
573 | }; |
574 | /// Match an integer or vector with only the sign bit(s) set. |
575 | /// For vectors, this includes constants with undefined elements. |
576 | inline cst_pred_ty<is_sign_mask> m_SignMask() { |
577 | return cst_pred_ty<is_sign_mask>(); |
578 | } |
579 | |
580 | struct is_lowbit_mask { |
581 | bool isValue(const APInt &C) { return C.isMask(); } |
582 | }; |
583 | /// Match an integer or vector with only the low bit(s) set. |
584 | /// For vectors, this includes constants with undefined elements. |
585 | inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() { |
586 | return cst_pred_ty<is_lowbit_mask>(); |
587 | } |
588 | inline api_pred_ty<is_lowbit_mask> m_LowBitMask(const APInt *&V) { return V; } |
589 | |
590 | struct icmp_pred_with_threshold { |
591 | ICmpInst::Predicate Pred; |
592 | const APInt *Thr; |
593 | bool isValue(const APInt &C) { return ICmpInst::compare(C, *Thr, Pred); } |
594 | }; |
595 | /// Match an integer or vector with every element comparing 'pred' (eg/ne/...) |
596 | /// to Threshold. For vectors, this includes constants with undefined elements. |
597 | inline cst_pred_ty<icmp_pred_with_threshold> |
598 | m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) { |
599 | cst_pred_ty<icmp_pred_with_threshold> P; |
600 | P.Pred = Predicate; |
601 | P.Thr = &Threshold; |
602 | return P; |
603 | } |
604 | |
605 | struct is_nan { |
606 | bool isValue(const APFloat &C) { return C.isNaN(); } |
607 | }; |
608 | /// Match an arbitrary NaN constant. This includes quiet and signalling nans. |
609 | /// For vectors, this includes constants with undefined elements. |
610 | inline cstfp_pred_ty<is_nan> m_NaN() { return cstfp_pred_ty<is_nan>(); } |
611 | |
612 | struct is_nonnan { |
613 | bool isValue(const APFloat &C) { return !C.isNaN(); } |
614 | }; |
615 | /// Match a non-NaN FP constant. |
616 | /// For vectors, this includes constants with undefined elements. |
617 | inline cstfp_pred_ty<is_nonnan> m_NonNaN() { |
618 | return cstfp_pred_ty<is_nonnan>(); |
619 | } |
620 | |
621 | struct is_inf { |
622 | bool isValue(const APFloat &C) { return C.isInfinity(); } |
623 | }; |
624 | /// Match a positive or negative infinity FP constant. |
625 | /// For vectors, this includes constants with undefined elements. |
626 | inline cstfp_pred_ty<is_inf> m_Inf() { return cstfp_pred_ty<is_inf>(); } |
627 | |
628 | struct is_noninf { |
629 | bool isValue(const APFloat &C) { return !C.isInfinity(); } |
630 | }; |
631 | /// Match a non-infinity FP constant, i.e. finite or NaN. |
632 | /// For vectors, this includes constants with undefined elements. |
633 | inline cstfp_pred_ty<is_noninf> m_NonInf() { |
634 | return cstfp_pred_ty<is_noninf>(); |
635 | } |
636 | |
637 | struct is_finite { |
638 | bool isValue(const APFloat &C) { return C.isFinite(); } |
639 | }; |
640 | /// Match a finite FP constant, i.e. not infinity or NaN. |
641 | /// For vectors, this includes constants with undefined elements. |
642 | inline cstfp_pred_ty<is_finite> m_Finite() { |
643 | return cstfp_pred_ty<is_finite>(); |
644 | } |
645 | inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; } |
646 | |
647 | struct is_finitenonzero { |
648 | bool isValue(const APFloat &C) { return C.isFiniteNonZero(); } |
649 | }; |
650 | /// Match a finite non-zero FP constant. |
651 | /// For vectors, this includes constants with undefined elements. |
652 | inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() { |
653 | return cstfp_pred_ty<is_finitenonzero>(); |
654 | } |
655 | inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) { |
656 | return V; |
657 | } |
658 | |
659 | struct is_any_zero_fp { |
660 | bool isValue(const APFloat &C) { return C.isZero(); } |
661 | }; |
662 | /// Match a floating-point negative zero or positive zero. |
663 | /// For vectors, this includes constants with undefined elements. |
664 | inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() { |
665 | return cstfp_pred_ty<is_any_zero_fp>(); |
666 | } |
667 | |
668 | struct is_pos_zero_fp { |
669 | bool isValue(const APFloat &C) { return C.isPosZero(); } |
670 | }; |
671 | /// Match a floating-point positive zero. |
672 | /// For vectors, this includes constants with undefined elements. |
673 | inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() { |
674 | return cstfp_pred_ty<is_pos_zero_fp>(); |
675 | } |
676 | |
677 | struct is_neg_zero_fp { |
678 | bool isValue(const APFloat &C) { return C.isNegZero(); } |
679 | }; |
680 | /// Match a floating-point negative zero. |
681 | /// For vectors, this includes constants with undefined elements. |
682 | inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() { |
683 | return cstfp_pred_ty<is_neg_zero_fp>(); |
684 | } |
685 | |
686 | struct is_non_zero_fp { |
687 | bool isValue(const APFloat &C) { return C.isNonZero(); } |
688 | }; |
689 | /// Match a floating-point non-zero. |
690 | /// For vectors, this includes constants with undefined elements. |
691 | inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() { |
692 | return cstfp_pred_ty<is_non_zero_fp>(); |
693 | } |
694 | |
695 | /////////////////////////////////////////////////////////////////////////////// |
696 | |
697 | template <typename Class> struct bind_ty { |
698 | Class *&VR; |
699 | |
700 | bind_ty(Class *&V) : VR(V) {} |
701 | |
702 | template <typename ITy> bool match(ITy *V) { |
703 | if (auto *CV = dyn_cast<Class>(V)) { |
704 | VR = CV; |
705 | return true; |
706 | } |
707 | return false; |
708 | } |
709 | }; |
710 | |
711 | /// Match a value, capturing it if we match. |
712 | inline bind_ty<Value> m_Value(Value *&V) { return V; } |
713 | inline bind_ty<const Value> m_Value(const Value *&V) { return V; } |
714 | |
715 | /// Match an instruction, capturing it if we match. |
716 | inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; } |
717 | /// Match a unary operator, capturing it if we match. |
718 | inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; } |
719 | /// Match a binary operator, capturing it if we match. |
720 | inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; } |
721 | /// Match a with overflow intrinsic, capturing it if we match. |
722 | inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) { |
723 | return I; |
724 | } |
725 | inline bind_ty<const WithOverflowInst> |
726 | m_WithOverflowInst(const WithOverflowInst *&I) { |
727 | return I; |
728 | } |
729 | |
730 | /// Match a Constant, capturing the value if we match. |
731 | inline bind_ty<Constant> m_Constant(Constant *&C) { return C; } |
732 | |
733 | /// Match a ConstantInt, capturing the value if we match. |
734 | inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; } |
735 | |
736 | /// Match a ConstantFP, capturing the value if we match. |
737 | inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; } |
738 | |
739 | /// Match a ConstantExpr, capturing the value if we match. |
740 | inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; } |
741 | |
742 | /// Match a basic block value, capturing it if we match. |
743 | inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; } |
744 | inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) { |
745 | return V; |
746 | } |
747 | |
748 | /// Match an arbitrary immediate Constant and ignore it. |
749 | inline match_combine_and<class_match<Constant>, |
750 | match_unless<constantexpr_match>> |
751 | m_ImmConstant() { |
752 | return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr())); |
753 | } |
754 | |
755 | /// Match an immediate Constant, capturing the value if we match. |
756 | inline match_combine_and<bind_ty<Constant>, |
757 | match_unless<constantexpr_match>> |
758 | m_ImmConstant(Constant *&C) { |
759 | return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr())); |
760 | } |
761 | |
762 | /// Match a specified Value*. |
763 | struct specificval_ty { |
764 | const Value *Val; |
765 | |
766 | specificval_ty(const Value *V) : Val(V) {} |
767 | |
768 | template <typename ITy> bool match(ITy *V) { return V == Val; } |
769 | }; |
770 | |
771 | /// Match if we have a specific specified value. |
772 | inline specificval_ty m_Specific(const Value *V) { return V; } |
773 | |
774 | /// Stores a reference to the Value *, not the Value * itself, |
775 | /// thus can be used in commutative matchers. |
776 | template <typename Class> struct deferredval_ty { |
777 | Class *const &Val; |
778 | |
779 | deferredval_ty(Class *const &V) : Val(V) {} |
780 | |
781 | template <typename ITy> bool match(ITy *const V) { return V == Val; } |
782 | }; |
783 | |
784 | /// Like m_Specific(), but works if the specific value to match is determined |
785 | /// as part of the same match() expression. For example: |
786 | /// m_Add(m_Value(X), m_Specific(X)) is incorrect, because m_Specific() will |
787 | /// bind X before the pattern match starts. |
788 | /// m_Add(m_Value(X), m_Deferred(X)) is correct, and will check against |
789 | /// whichever value m_Value(X) populated. |
790 | inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; } |
791 | inline deferredval_ty<const Value> m_Deferred(const Value *const &V) { |
792 | return V; |
793 | } |
794 | |
795 | /// Match a specified floating point value or vector of all elements of |
796 | /// that value. |
797 | struct specific_fpval { |
798 | double Val; |
799 | |
800 | specific_fpval(double V) : Val(V) {} |
801 | |
802 | template <typename ITy> bool match(ITy *V) { |
803 | if (const auto *CFP = dyn_cast<ConstantFP>(V)) |
804 | return CFP->isExactlyValue(Val); |
805 | if (V->getType()->isVectorTy()) |
806 | if (const auto *C = dyn_cast<Constant>(V)) |
807 | if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue())) |
808 | return CFP->isExactlyValue(Val); |
809 | return false; |
810 | } |
811 | }; |
812 | |
813 | /// Match a specific floating point value or vector with all elements |
814 | /// equal to the value. |
815 | inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); } |
816 | |
817 | /// Match a float 1.0 or vector with all elements equal to 1.0. |
818 | inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); } |
819 | |
820 | struct bind_const_intval_ty { |
821 | uint64_t &VR; |
822 | |
823 | bind_const_intval_ty(uint64_t &V) : VR(V) {} |
824 | |
825 | template <typename ITy> bool match(ITy *V) { |
826 | if (const auto *CV = dyn_cast<ConstantInt>(V)) |
827 | if (CV->getValue().ule(UINT64_MAX(18446744073709551615UL))) { |
828 | VR = CV->getZExtValue(); |
829 | return true; |
830 | } |
831 | return false; |
832 | } |
833 | }; |
834 | |
835 | /// Match a specified integer value or vector of all elements of that |
836 | /// value. |
837 | template <bool AllowUndefs> struct specific_intval { |
838 | APInt Val; |
839 | |
840 | specific_intval(APInt V) : Val(std::move(V)) {} |
841 | |
842 | template <typename ITy> bool match(ITy *V) { |
843 | const auto *CI = dyn_cast<ConstantInt>(V); |
844 | if (!CI && V->getType()->isVectorTy()) |
845 | if (const auto *C = dyn_cast<Constant>(V)) |
846 | CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs)); |
847 | |
848 | return CI && APInt::isSameValue(CI->getValue(), Val); |
849 | } |
850 | }; |
851 | |
852 | /// Match a specific integer value or vector with all elements equal to |
853 | /// the value. |
854 | inline specific_intval<false> m_SpecificInt(APInt V) { |
855 | return specific_intval<false>(std::move(V)); |
856 | } |
857 | |
858 | inline specific_intval<false> m_SpecificInt(uint64_t V) { |
859 | return m_SpecificInt(APInt(64, V)); |
860 | } |
861 | |
862 | inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) { |
863 | return specific_intval<true>(std::move(V)); |
864 | } |
865 | |
866 | inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) { |
867 | return m_SpecificIntAllowUndef(APInt(64, V)); |
868 | } |
869 | |
870 | /// Match a ConstantInt and bind to its value. This does not match |
871 | /// ConstantInts wider than 64-bits. |
872 | inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; } |
873 | |
874 | /// Match a specified basic block value. |
875 | struct specific_bbval { |
876 | BasicBlock *Val; |
877 | |
878 | specific_bbval(BasicBlock *Val) : Val(Val) {} |
879 | |
880 | template <typename ITy> bool match(ITy *V) { |
881 | const auto *BB = dyn_cast<BasicBlock>(V); |
882 | return BB && BB == Val; |
883 | } |
884 | }; |
885 | |
886 | /// Match a specific basic block value. |
887 | inline specific_bbval m_SpecificBB(BasicBlock *BB) { |
888 | return specific_bbval(BB); |
889 | } |
890 | |
891 | /// A commutative-friendly version of m_Specific(). |
892 | inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) { |
893 | return BB; |
894 | } |
895 | inline deferredval_ty<const BasicBlock> |
896 | m_Deferred(const BasicBlock *const &BB) { |
897 | return BB; |
898 | } |
899 | |
900 | //===----------------------------------------------------------------------===// |
901 | // Matcher for any binary operator. |
902 | // |
903 | template <typename LHS_t, typename RHS_t, bool Commutable = false> |
904 | struct AnyBinaryOp_match { |
905 | LHS_t L; |
906 | RHS_t R; |
907 | |
908 | // The evaluation order is always stable, regardless of Commutability. |
909 | // The LHS is always matched first. |
910 | AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} |
911 | |
912 | template <typename OpTy> bool match(OpTy *V) { |
913 | if (auto *I = dyn_cast<BinaryOperator>(V)) |
914 | return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || |
915 | (Commutable && L.match(I->getOperand(1)) && |
916 | R.match(I->getOperand(0))); |
917 | return false; |
918 | } |
919 | }; |
920 | |
921 | template <typename LHS, typename RHS> |
922 | inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) { |
923 | return AnyBinaryOp_match<LHS, RHS>(L, R); |
924 | } |
925 | |
926 | //===----------------------------------------------------------------------===// |
927 | // Matcher for any unary operator. |
928 | // TODO fuse unary, binary matcher into n-ary matcher |
929 | // |
930 | template <typename OP_t> struct AnyUnaryOp_match { |
931 | OP_t X; |
932 | |
933 | AnyUnaryOp_match(const OP_t &X) : X(X) {} |
934 | |
935 | template <typename OpTy> bool match(OpTy *V) { |
936 | if (auto *I = dyn_cast<UnaryOperator>(V)) |
937 | return X.match(I->getOperand(0)); |
938 | return false; |
939 | } |
940 | }; |
941 | |
942 | template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) { |
943 | return AnyUnaryOp_match<OP_t>(X); |
944 | } |
945 | |
946 | //===----------------------------------------------------------------------===// |
947 | // Matchers for specific binary operators. |
948 | // |
949 | |
950 | template <typename LHS_t, typename RHS_t, unsigned Opcode, |
951 | bool Commutable = false> |
952 | struct BinaryOp_match { |
953 | LHS_t L; |
954 | RHS_t R; |
955 | |
956 | // The evaluation order is always stable, regardless of Commutability. |
957 | // The LHS is always matched first. |
958 | BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} |
959 | |
960 | template <typename OpTy> inline bool match(unsigned Opc, OpTy *V) { |
961 | if (V->getValueID() == Value::InstructionVal + Opc) { |
962 | auto *I = cast<BinaryOperator>(V); |
963 | return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) || |
964 | (Commutable && L.match(I->getOperand(1)) && |
965 | R.match(I->getOperand(0))); |
966 | } |
967 | if (auto *CE = dyn_cast<ConstantExpr>(V)) |
968 | return CE->getOpcode() == Opc && |
969 | ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) || |
970 | (Commutable && L.match(CE->getOperand(1)) && |
971 | R.match(CE->getOperand(0)))); |
972 | return false; |
973 | } |
974 | |
975 | template <typename OpTy> bool match(OpTy *V) { return match(Opcode, V); } |
976 | }; |
977 | |
978 | template <typename LHS, typename RHS> |
979 | inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L, |
980 | const RHS &R) { |
981 | return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R); |
982 | } |
983 | |
984 | template <typename LHS, typename RHS> |
985 | inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L, |
986 | const RHS &R) { |
987 | return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R); |
988 | } |
989 | |
990 | template <typename LHS, typename RHS> |
991 | inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L, |
992 | const RHS &R) { |
993 | return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R); |
994 | } |
995 | |
996 | template <typename LHS, typename RHS> |
997 | inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L, |
998 | const RHS &R) { |
999 | return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R); |
1000 | } |
1001 | |
1002 | template <typename Op_t> struct FNeg_match { |
1003 | Op_t X; |
1004 | |
1005 | FNeg_match(const Op_t &Op) : X(Op) {} |
1006 | template <typename OpTy> bool match(OpTy *V) { |
1007 | auto *FPMO = dyn_cast<FPMathOperator>(V); |
1008 | if (!FPMO) |
1009 | return false; |
1010 | |
1011 | if (FPMO->getOpcode() == Instruction::FNeg) |
1012 | return X.match(FPMO->getOperand(0)); |
1013 | |
1014 | if (FPMO->getOpcode() == Instruction::FSub) { |
1015 | if (FPMO->hasNoSignedZeros()) { |
1016 | // With 'nsz', any zero goes. |
1017 | if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0))) |
1018 | return false; |
1019 | } else { |
1020 | // Without 'nsz', we need fsub -0.0, X exactly. |
1021 | if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0))) |
1022 | return false; |
1023 | } |
1024 | |
1025 | return X.match(FPMO->getOperand(1)); |
1026 | } |
1027 | |
1028 | return false; |
1029 | } |
1030 | }; |
1031 | |
1032 | /// Match 'fneg X' as 'fsub -0.0, X'. |
1033 | template <typename OpTy> inline FNeg_match<OpTy> m_FNeg(const OpTy &X) { |
1034 | return FNeg_match<OpTy>(X); |
1035 | } |
1036 | |
1037 | /// Match 'fneg X' as 'fsub +-0.0, X'. |
1038 | template <typename RHS> |
1039 | inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub> |
1040 | m_FNegNSZ(const RHS &X) { |
1041 | return m_FSub(m_AnyZeroFP(), X); |
1042 | } |
1043 | |
1044 | template <typename LHS, typename RHS> |
1045 | inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L, |
1046 | const RHS &R) { |
1047 | return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R); |
1048 | } |
1049 | |
1050 | template <typename LHS, typename RHS> |
1051 | inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L, |
1052 | const RHS &R) { |
1053 | return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R); |
1054 | } |
1055 | |
1056 | template <typename LHS, typename RHS> |
1057 | inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L, |
1058 | const RHS &R) { |
1059 | return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R); |
1060 | } |
1061 | |
1062 | template <typename LHS, typename RHS> |
1063 | inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L, |
1064 | const RHS &R) { |
1065 | return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R); |
1066 | } |
1067 | |
1068 | template <typename LHS, typename RHS> |
1069 | inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L, |
1070 | const RHS &R) { |
1071 | return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R); |
1072 | } |
1073 | |
1074 | template <typename LHS, typename RHS> |
1075 | inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L, |
1076 | const RHS &R) { |
1077 | return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R); |
1078 | } |
1079 | |
1080 | template <typename LHS, typename RHS> |
1081 | inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L, |
1082 | const RHS &R) { |
1083 | return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R); |
1084 | } |
1085 | |
1086 | template <typename LHS, typename RHS> |
1087 | inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L, |
1088 | const RHS &R) { |
1089 | return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R); |
1090 | } |
1091 | |
1092 | template <typename LHS, typename RHS> |
1093 | inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L, |
1094 | const RHS &R) { |
1095 | return BinaryOp_match<LHS, RHS, Instruction::And>(L, R); |
1096 | } |
1097 | |
1098 | template <typename LHS, typename RHS> |
1099 | inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L, |
1100 | const RHS &R) { |
1101 | return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R); |
1102 | } |
1103 | |
1104 | template <typename LHS, typename RHS> |
1105 | inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L, |
1106 | const RHS &R) { |
1107 | return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R); |
1108 | } |
1109 | |
1110 | template <typename LHS, typename RHS> |
1111 | inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L, |
1112 | const RHS &R) { |
1113 | return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R); |
1114 | } |
1115 | |
1116 | template <typename LHS, typename RHS> |
1117 | inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L, |
1118 | const RHS &R) { |
1119 | return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R); |
1120 | } |
1121 | |
1122 | template <typename LHS, typename RHS> |
1123 | inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L, |
1124 | const RHS &R) { |
1125 | return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R); |
1126 | } |
1127 | |
1128 | template <typename LHS_t, typename RHS_t, unsigned Opcode, |
1129 | unsigned WrapFlags = 0> |
1130 | struct OverflowingBinaryOp_match { |
1131 | LHS_t L; |
1132 | RHS_t R; |
1133 | |
1134 | OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) |
1135 | : L(LHS), R(RHS) {} |
1136 | |
1137 | template <typename OpTy> bool match(OpTy *V) { |
1138 | if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) { |
1139 | if (Op->getOpcode() != Opcode) |
1140 | return false; |
1141 | if ((WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap) && |
1142 | !Op->hasNoUnsignedWrap()) |
1143 | return false; |
1144 | if ((WrapFlags & OverflowingBinaryOperator::NoSignedWrap) && |
1145 | !Op->hasNoSignedWrap()) |
1146 | return false; |
1147 | return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1)); |
1148 | } |
1149 | return false; |
1150 | } |
1151 | }; |
1152 | |
1153 | template <typename LHS, typename RHS> |
1154 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, |
1155 | OverflowingBinaryOperator::NoSignedWrap> |
1156 | m_NSWAdd(const LHS &L, const RHS &R) { |
1157 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, |
1158 | OverflowingBinaryOperator::NoSignedWrap>(L, |
1159 | R); |
1160 | } |
1161 | template <typename LHS, typename RHS> |
1162 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, |
1163 | OverflowingBinaryOperator::NoSignedWrap> |
1164 | m_NSWSub(const LHS &L, const RHS &R) { |
1165 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, |
1166 | OverflowingBinaryOperator::NoSignedWrap>(L, |
1167 | R); |
1168 | } |
1169 | template <typename LHS, typename RHS> |
1170 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, |
1171 | OverflowingBinaryOperator::NoSignedWrap> |
1172 | m_NSWMul(const LHS &L, const RHS &R) { |
1173 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, |
1174 | OverflowingBinaryOperator::NoSignedWrap>(L, |
1175 | R); |
1176 | } |
1177 | template <typename LHS, typename RHS> |
1178 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, |
1179 | OverflowingBinaryOperator::NoSignedWrap> |
1180 | m_NSWShl(const LHS &L, const RHS &R) { |
1181 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, |
1182 | OverflowingBinaryOperator::NoSignedWrap>(L, |
1183 | R); |
1184 | } |
1185 | |
1186 | template <typename LHS, typename RHS> |
1187 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, |
1188 | OverflowingBinaryOperator::NoUnsignedWrap> |
1189 | m_NUWAdd(const LHS &L, const RHS &R) { |
1190 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add, |
1191 | OverflowingBinaryOperator::NoUnsignedWrap>( |
1192 | L, R); |
1193 | } |
1194 | template <typename LHS, typename RHS> |
1195 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, |
1196 | OverflowingBinaryOperator::NoUnsignedWrap> |
1197 | m_NUWSub(const LHS &L, const RHS &R) { |
1198 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub, |
1199 | OverflowingBinaryOperator::NoUnsignedWrap>( |
1200 | L, R); |
1201 | } |
1202 | template <typename LHS, typename RHS> |
1203 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, |
1204 | OverflowingBinaryOperator::NoUnsignedWrap> |
1205 | m_NUWMul(const LHS &L, const RHS &R) { |
1206 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul, |
1207 | OverflowingBinaryOperator::NoUnsignedWrap>( |
1208 | L, R); |
1209 | } |
1210 | template <typename LHS, typename RHS> |
1211 | inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, |
1212 | OverflowingBinaryOperator::NoUnsignedWrap> |
1213 | m_NUWShl(const LHS &L, const RHS &R) { |
1214 | return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl, |
1215 | OverflowingBinaryOperator::NoUnsignedWrap>( |
1216 | L, R); |
1217 | } |
1218 | |
1219 | template <typename LHS_t, typename RHS_t, bool Commutable = false> |
1220 | struct SpecificBinaryOp_match |
1221 | : public BinaryOp_match<LHS_t, RHS_t, 0, Commutable> { |
1222 | unsigned Opcode; |
1223 | |
1224 | SpecificBinaryOp_match(unsigned Opcode, const LHS_t &LHS, const RHS_t &RHS) |
1225 | : BinaryOp_match<LHS_t, RHS_t, 0, Commutable>(LHS, RHS), Opcode(Opcode) {} |
1226 | |
1227 | template <typename OpTy> bool match(OpTy *V) { |
1228 | return BinaryOp_match<LHS_t, RHS_t, 0, Commutable>::match(Opcode, V); |
1229 | } |
1230 | }; |
1231 | |
1232 | /// Matches a specific opcode. |
1233 | template <typename LHS, typename RHS> |
1234 | inline SpecificBinaryOp_match<LHS, RHS> m_BinOp(unsigned Opcode, const LHS &L, |
1235 | const RHS &R) { |
1236 | return SpecificBinaryOp_match<LHS, RHS>(Opcode, L, R); |
1237 | } |
1238 | |
1239 | //===----------------------------------------------------------------------===// |
1240 | // Class that matches a group of binary opcodes. |
1241 | // |
1242 | template <typename LHS_t, typename RHS_t, typename Predicate> |
1243 | struct BinOpPred_match : Predicate { |
1244 | LHS_t L; |
1245 | RHS_t R; |
1246 | |
1247 | BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} |
1248 | |
1249 | template <typename OpTy> bool match(OpTy *V) { |
1250 | if (auto *I = dyn_cast<Instruction>(V)) |
1251 | return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) && |
1252 | R.match(I->getOperand(1)); |
1253 | if (auto *CE = dyn_cast<ConstantExpr>(V)) |
1254 | return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) && |
1255 | R.match(CE->getOperand(1)); |
1256 | return false; |
1257 | } |
1258 | }; |
1259 | |
1260 | struct is_shift_op { |
1261 | bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); } |
1262 | }; |
1263 | |
1264 | struct is_right_shift_op { |
1265 | bool isOpType(unsigned Opcode) { |
1266 | return Opcode == Instruction::LShr || Opcode == Instruction::AShr; |
1267 | } |
1268 | }; |
1269 | |
1270 | struct is_logical_shift_op { |
1271 | bool isOpType(unsigned Opcode) { |
1272 | return Opcode == Instruction::LShr || Opcode == Instruction::Shl; |
1273 | } |
1274 | }; |
1275 | |
1276 | struct is_bitwiselogic_op { |
1277 | bool isOpType(unsigned Opcode) { |
1278 | return Instruction::isBitwiseLogicOp(Opcode); |
1279 | } |
1280 | }; |
1281 | |
1282 | struct is_idiv_op { |
1283 | bool isOpType(unsigned Opcode) { |
1284 | return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv; |
1285 | } |
1286 | }; |
1287 | |
1288 | struct is_irem_op { |
1289 | bool isOpType(unsigned Opcode) { |
1290 | return Opcode == Instruction::SRem || Opcode == Instruction::URem; |
1291 | } |
1292 | }; |
1293 | |
1294 | /// Matches shift operations. |
1295 | template <typename LHS, typename RHS> |
1296 | inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L, |
1297 | const RHS &R) { |
1298 | return BinOpPred_match<LHS, RHS, is_shift_op>(L, R); |
1299 | } |
1300 | |
1301 | /// Matches logical shift operations. |
1302 | template <typename LHS, typename RHS> |
1303 | inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L, |
1304 | const RHS &R) { |
1305 | return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R); |
1306 | } |
1307 | |
1308 | /// Matches logical shift operations. |
1309 | template <typename LHS, typename RHS> |
1310 | inline BinOpPred_match<LHS, RHS, is_logical_shift_op> |
1311 | m_LogicalShift(const LHS &L, const RHS &R) { |
1312 | return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R); |
1313 | } |
1314 | |
1315 | /// Matches bitwise logic operations. |
1316 | template <typename LHS, typename RHS> |
1317 | inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op> |
1318 | m_BitwiseLogic(const LHS &L, const RHS &R) { |
1319 | return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R); |
1320 | } |
1321 | |
1322 | /// Matches integer division operations. |
1323 | template <typename LHS, typename RHS> |
1324 | inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L, |
1325 | const RHS &R) { |
1326 | return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R); |
1327 | } |
1328 | |
1329 | /// Matches integer remainder operations. |
1330 | template <typename LHS, typename RHS> |
1331 | inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L, |
1332 | const RHS &R) { |
1333 | return BinOpPred_match<LHS, RHS, is_irem_op>(L, R); |
1334 | } |
1335 | |
1336 | //===----------------------------------------------------------------------===// |
1337 | // Class that matches exact binary ops. |
1338 | // |
1339 | template <typename SubPattern_t> struct Exact_match { |
1340 | SubPattern_t SubPattern; |
1341 | |
1342 | Exact_match(const SubPattern_t &SP) : SubPattern(SP) {} |
1343 | |
1344 | template <typename OpTy> bool match(OpTy *V) { |
1345 | if (auto *PEO = dyn_cast<PossiblyExactOperator>(V)) |
1346 | return PEO->isExact() && SubPattern.match(V); |
1347 | return false; |
1348 | } |
1349 | }; |
1350 | |
1351 | template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) { |
1352 | return SubPattern; |
1353 | } |
1354 | |
1355 | //===----------------------------------------------------------------------===// |
1356 | // Matchers for CmpInst classes |
1357 | // |
1358 | |
1359 | template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy, |
1360 | bool Commutable = false> |
1361 | struct CmpClass_match { |
1362 | PredicateTy &Predicate; |
1363 | LHS_t L; |
1364 | RHS_t R; |
1365 | |
1366 | // The evaluation order is always stable, regardless of Commutability. |
1367 | // The LHS is always matched first. |
1368 | CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS) |
1369 | : Predicate(Pred), L(LHS), R(RHS) {} |
1370 | |
1371 | template <typename OpTy> bool match(OpTy *V) { |
1372 | if (auto *I = dyn_cast<Class>(V)) { |
1373 | if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) { |
1374 | Predicate = I->getPredicate(); |
1375 | return true; |
1376 | } else if (Commutable && L.match(I->getOperand(1)) && |
1377 | R.match(I->getOperand(0))) { |
1378 | Predicate = I->getSwappedPredicate(); |
1379 | return true; |
1380 | } |
1381 | } |
1382 | return false; |
1383 | } |
1384 | }; |
1385 | |
1386 | template <typename LHS, typename RHS> |
1387 | inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate> |
1388 | m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
1389 | return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R); |
1390 | } |
1391 | |
1392 | template <typename LHS, typename RHS> |
1393 | inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate> |
1394 | m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
1395 | return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R); |
1396 | } |
1397 | |
1398 | template <typename LHS, typename RHS> |
1399 | inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate> |
1400 | m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
1401 | return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R); |
1402 | } |
1403 | |
1404 | //===----------------------------------------------------------------------===// |
1405 | // Matchers for instructions with a given opcode and number of operands. |
1406 | // |
1407 | |
1408 | /// Matches instructions with Opcode and three operands. |
1409 | template <typename T0, unsigned Opcode> struct OneOps_match { |
1410 | T0 Op1; |
1411 | |
1412 | OneOps_match(const T0 &Op1) : Op1(Op1) {} |
1413 | |
1414 | template <typename OpTy> bool match(OpTy *V) { |
1415 | if (V->getValueID() == Value::InstructionVal + Opcode) { |
1416 | auto *I = cast<Instruction>(V); |
1417 | return Op1.match(I->getOperand(0)); |
1418 | } |
1419 | return false; |
1420 | } |
1421 | }; |
1422 | |
1423 | /// Matches instructions with Opcode and three operands. |
1424 | template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match { |
1425 | T0 Op1; |
1426 | T1 Op2; |
1427 | |
1428 | TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {} |
1429 | |
1430 | template <typename OpTy> bool match(OpTy *V) { |
1431 | if (V->getValueID() == Value::InstructionVal + Opcode) { |
1432 | auto *I = cast<Instruction>(V); |
1433 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)); |
1434 | } |
1435 | return false; |
1436 | } |
1437 | }; |
1438 | |
1439 | /// Matches instructions with Opcode and three operands. |
1440 | template <typename T0, typename T1, typename T2, unsigned Opcode> |
1441 | struct ThreeOps_match { |
1442 | T0 Op1; |
1443 | T1 Op2; |
1444 | T2 Op3; |
1445 | |
1446 | ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3) |
1447 | : Op1(Op1), Op2(Op2), Op3(Op3) {} |
1448 | |
1449 | template <typename OpTy> bool match(OpTy *V) { |
1450 | if (V->getValueID() == Value::InstructionVal + Opcode) { |
1451 | auto *I = cast<Instruction>(V); |
1452 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && |
1453 | Op3.match(I->getOperand(2)); |
1454 | } |
1455 | return false; |
1456 | } |
1457 | }; |
1458 | |
1459 | /// Matches SelectInst. |
1460 | template <typename Cond, typename LHS, typename RHS> |
1461 | inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select> |
1462 | m_Select(const Cond &C, const LHS &L, const RHS &R) { |
1463 | return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R); |
1464 | } |
1465 | |
1466 | /// This matches a select of two constants, e.g.: |
1467 | /// m_SelectCst<-1, 0>(m_Value(V)) |
1468 | template <int64_t L, int64_t R, typename Cond> |
1469 | inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>, |
1470 | Instruction::Select> |
1471 | m_SelectCst(const Cond &C) { |
1472 | return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>()); |
1473 | } |
1474 | |
1475 | /// Matches FreezeInst. |
1476 | template <typename OpTy> |
1477 | inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) { |
1478 | return OneOps_match<OpTy, Instruction::Freeze>(Op); |
1479 | } |
1480 | |
1481 | /// Matches InsertElementInst. |
1482 | template <typename Val_t, typename Elt_t, typename Idx_t> |
1483 | inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement> |
1484 | m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) { |
1485 | return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>( |
1486 | Val, Elt, Idx); |
1487 | } |
1488 | |
1489 | /// Matches ExtractElementInst. |
1490 | template <typename Val_t, typename Idx_t> |
1491 | inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement> |
1492 | m_ExtractElt(const Val_t &Val, const Idx_t &Idx) { |
1493 | return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx); |
1494 | } |
1495 | |
1496 | /// Matches shuffle. |
1497 | template <typename T0, typename T1, typename T2> struct Shuffle_match { |
1498 | T0 Op1; |
1499 | T1 Op2; |
1500 | T2 Mask; |
1501 | |
1502 | Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask) |
1503 | : Op1(Op1), Op2(Op2), Mask(Mask) {} |
1504 | |
1505 | template <typename OpTy> bool match(OpTy *V) { |
1506 | if (auto *I = dyn_cast<ShuffleVectorInst>(V)) { |
1507 | return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) && |
1508 | Mask.match(I->getShuffleMask()); |
1509 | } |
1510 | return false; |
1511 | } |
1512 | }; |
1513 | |
1514 | struct m_Mask { |
1515 | ArrayRef<int> &MaskRef; |
1516 | m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {} |
1517 | bool match(ArrayRef<int> Mask) { |
1518 | MaskRef = Mask; |
1519 | return true; |
1520 | } |
1521 | }; |
1522 | |
1523 | struct m_ZeroMask { |
1524 | bool match(ArrayRef<int> Mask) { |
1525 | return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; }); |
1526 | } |
1527 | }; |
1528 | |
1529 | struct m_SpecificMask { |
1530 | ArrayRef<int> &MaskRef; |
1531 | m_SpecificMask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {} |
1532 | bool match(ArrayRef<int> Mask) { return MaskRef == Mask; } |
1533 | }; |
1534 | |
1535 | struct m_SplatOrUndefMask { |
1536 | int &SplatIndex; |
1537 | m_SplatOrUndefMask(int &SplatIndex) : SplatIndex(SplatIndex) {} |
1538 | bool match(ArrayRef<int> Mask) { |
1539 | const auto *First = find_if(Mask, [](int Elem) { return Elem != -1; }); |
1540 | if (First == Mask.end()) |
1541 | return false; |
1542 | SplatIndex = *First; |
1543 | return all_of(Mask, |
1544 | [First](int Elem) { return Elem == *First || Elem == -1; }); |
1545 | } |
1546 | }; |
1547 | |
1548 | /// Matches ShuffleVectorInst independently of mask value. |
1549 | template <typename V1_t, typename V2_t> |
1550 | inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector> |
1551 | m_Shuffle(const V1_t &v1, const V2_t &v2) { |
1552 | return TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>(v1, v2); |
1553 | } |
1554 | |
1555 | template <typename V1_t, typename V2_t, typename Mask_t> |
1556 | inline Shuffle_match<V1_t, V2_t, Mask_t> |
1557 | m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) { |
1558 | return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask); |
1559 | } |
1560 | |
1561 | /// Matches LoadInst. |
1562 | template <typename OpTy> |
1563 | inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) { |
1564 | return OneOps_match<OpTy, Instruction::Load>(Op); |
1565 | } |
1566 | |
1567 | /// Matches StoreInst. |
1568 | template <typename ValueOpTy, typename PointerOpTy> |
1569 | inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store> |
1570 | m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) { |
1571 | return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp, |
1572 | PointerOp); |
1573 | } |
1574 | |
1575 | //===----------------------------------------------------------------------===// |
1576 | // Matchers for CastInst classes |
1577 | // |
1578 | |
1579 | template <typename Op_t, unsigned Opcode> struct CastClass_match { |
1580 | Op_t Op; |
1581 | |
1582 | CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {} |
1583 | |
1584 | template <typename OpTy> bool match(OpTy *V) { |
1585 | if (auto *O = dyn_cast<Operator>(V)) |
1586 | return O->getOpcode() == Opcode && Op.match(O->getOperand(0)); |
1587 | return false; |
1588 | } |
1589 | }; |
1590 | |
1591 | /// Matches BitCast. |
1592 | template <typename OpTy> |
1593 | inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) { |
1594 | return CastClass_match<OpTy, Instruction::BitCast>(Op); |
1595 | } |
1596 | |
1597 | /// Matches PtrToInt. |
1598 | template <typename OpTy> |
1599 | inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) { |
1600 | return CastClass_match<OpTy, Instruction::PtrToInt>(Op); |
1601 | } |
1602 | |
1603 | /// Matches IntToPtr. |
1604 | template <typename OpTy> |
1605 | inline CastClass_match<OpTy, Instruction::IntToPtr> m_IntToPtr(const OpTy &Op) { |
1606 | return CastClass_match<OpTy, Instruction::IntToPtr>(Op); |
1607 | } |
1608 | |
1609 | /// Matches Trunc. |
1610 | template <typename OpTy> |
1611 | inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) { |
1612 | return CastClass_match<OpTy, Instruction::Trunc>(Op); |
1613 | } |
1614 | |
1615 | template <typename OpTy> |
1616 | inline match_combine_or<CastClass_match<OpTy, Instruction::Trunc>, OpTy> |
1617 | m_TruncOrSelf(const OpTy &Op) { |
1618 | return m_CombineOr(m_Trunc(Op), Op); |
1619 | } |
1620 | |
1621 | /// Matches SExt. |
1622 | template <typename OpTy> |
1623 | inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) { |
1624 | return CastClass_match<OpTy, Instruction::SExt>(Op); |
1625 | } |
1626 | |
1627 | /// Matches ZExt. |
1628 | template <typename OpTy> |
1629 | inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) { |
1630 | return CastClass_match<OpTy, Instruction::ZExt>(Op); |
1631 | } |
1632 | |
1633 | template <typename OpTy> |
1634 | inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, OpTy> |
1635 | m_ZExtOrSelf(const OpTy &Op) { |
1636 | return m_CombineOr(m_ZExt(Op), Op); |
1637 | } |
1638 | |
1639 | template <typename OpTy> |
1640 | inline match_combine_or<CastClass_match<OpTy, Instruction::SExt>, OpTy> |
1641 | m_SExtOrSelf(const OpTy &Op) { |
1642 | return m_CombineOr(m_SExt(Op), Op); |
1643 | } |
1644 | |
1645 | template <typename OpTy> |
1646 | inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, |
1647 | CastClass_match<OpTy, Instruction::SExt>> |
1648 | m_ZExtOrSExt(const OpTy &Op) { |
1649 | return m_CombineOr(m_ZExt(Op), m_SExt(Op)); |
1650 | } |
1651 | |
1652 | template <typename OpTy> |
1653 | inline match_combine_or< |
1654 | match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, |
1655 | CastClass_match<OpTy, Instruction::SExt>>, |
1656 | OpTy> |
1657 | m_ZExtOrSExtOrSelf(const OpTy &Op) { |
1658 | return m_CombineOr(m_ZExtOrSExt(Op), Op); |
1659 | } |
1660 | |
1661 | template <typename OpTy> |
1662 | inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) { |
1663 | return CastClass_match<OpTy, Instruction::UIToFP>(Op); |
1664 | } |
1665 | |
1666 | template <typename OpTy> |
1667 | inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) { |
1668 | return CastClass_match<OpTy, Instruction::SIToFP>(Op); |
1669 | } |
1670 | |
1671 | template <typename OpTy> |
1672 | inline CastClass_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) { |
1673 | return CastClass_match<OpTy, Instruction::FPToUI>(Op); |
1674 | } |
1675 | |
1676 | template <typename OpTy> |
1677 | inline CastClass_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) { |
1678 | return CastClass_match<OpTy, Instruction::FPToSI>(Op); |
1679 | } |
1680 | |
1681 | template <typename OpTy> |
1682 | inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) { |
1683 | return CastClass_match<OpTy, Instruction::FPTrunc>(Op); |
1684 | } |
1685 | |
1686 | template <typename OpTy> |
1687 | inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) { |
1688 | return CastClass_match<OpTy, Instruction::FPExt>(Op); |
1689 | } |
1690 | |
1691 | //===----------------------------------------------------------------------===// |
1692 | // Matchers for control flow. |
1693 | // |
1694 | |
1695 | struct br_match { |
1696 | BasicBlock *&Succ; |
1697 | |
1698 | br_match(BasicBlock *&Succ) : Succ(Succ) {} |
1699 | |
1700 | template <typename OpTy> bool match(OpTy *V) { |
1701 | if (auto *BI = dyn_cast<BranchInst>(V)) |
1702 | if (BI->isUnconditional()) { |
1703 | Succ = BI->getSuccessor(0); |
1704 | return true; |
1705 | } |
1706 | return false; |
1707 | } |
1708 | }; |
1709 | |
1710 | inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); } |
1711 | |
1712 | template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> |
1713 | struct brc_match { |
1714 | Cond_t Cond; |
1715 | TrueBlock_t T; |
1716 | FalseBlock_t F; |
1717 | |
1718 | brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f) |
1719 | : Cond(C), T(t), F(f) {} |
1720 | |
1721 | template <typename OpTy> bool match(OpTy *V) { |
1722 | if (auto *BI = dyn_cast<BranchInst>(V)) |
1723 | if (BI->isConditional() && Cond.match(BI->getCondition())) |
1724 | return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1)); |
1725 | return false; |
1726 | } |
1727 | }; |
1728 | |
1729 | template <typename Cond_t> |
1730 | inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>> |
1731 | m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) { |
1732 | return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>( |
1733 | C, m_BasicBlock(T), m_BasicBlock(F)); |
1734 | } |
1735 | |
1736 | template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t> |
1737 | inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t> |
1738 | m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) { |
1739 | return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F); |
1740 | } |
1741 | |
1742 | //===----------------------------------------------------------------------===// |
1743 | // Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y). |
1744 | // |
1745 | |
1746 | template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t, |
1747 | bool Commutable = false> |
1748 | struct MaxMin_match { |
1749 | using PredType = Pred_t; |
1750 | LHS_t L; |
1751 | RHS_t R; |
1752 | |
1753 | // The evaluation order is always stable, regardless of Commutability. |
1754 | // The LHS is always matched first. |
1755 | MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {} |
1756 | |
1757 | template <typename OpTy> bool match(OpTy *V) { |
1758 | if (auto *II = dyn_cast<IntrinsicInst>(V)) { |
1759 | Intrinsic::ID IID = II->getIntrinsicID(); |
1760 | if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) || |
1761 | (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) || |
1762 | (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) || |
1763 | (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) { |
1764 | Value *LHS = II->getOperand(0), *RHS = II->getOperand(1); |
1765 | return (L.match(LHS) && R.match(RHS)) || |
1766 | (Commutable && L.match(RHS) && R.match(LHS)); |
1767 | } |
1768 | } |
1769 | // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x". |
1770 | auto *SI = dyn_cast<SelectInst>(V); |
1771 | if (!SI) |
1772 | return false; |
1773 | auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition()); |
1774 | if (!Cmp) |
1775 | return false; |
1776 | // At this point we have a select conditioned on a comparison. Check that |
1777 | // it is the values returned by the select that are being compared. |
1778 | auto *TrueVal = SI->getTrueValue(); |
1779 | auto *FalseVal = SI->getFalseValue(); |
1780 | auto *LHS = Cmp->getOperand(0); |
1781 | auto *RHS = Cmp->getOperand(1); |
1782 | if ((TrueVal != LHS || FalseVal != RHS) && |
1783 | (TrueVal != RHS || FalseVal != LHS)) |
1784 | return false; |
1785 | typename CmpInst_t::Predicate Pred = |
1786 | LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate(); |
1787 | // Does "(x pred y) ? x : y" represent the desired max/min operation? |
1788 | if (!Pred_t::match(Pred)) |
1789 | return false; |
1790 | // It does! Bind the operands. |
1791 | return (L.match(LHS) && R.match(RHS)) || |
1792 | (Commutable && L.match(RHS) && R.match(LHS)); |
1793 | } |
1794 | }; |
1795 | |
1796 | /// Helper class for identifying signed max predicates. |
1797 | struct smax_pred_ty { |
1798 | static bool match(ICmpInst::Predicate Pred) { |
1799 | return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE; |
1800 | } |
1801 | }; |
1802 | |
1803 | /// Helper class for identifying signed min predicates. |
1804 | struct smin_pred_ty { |
1805 | static bool match(ICmpInst::Predicate Pred) { |
1806 | return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE; |
1807 | } |
1808 | }; |
1809 | |
1810 | /// Helper class for identifying unsigned max predicates. |
1811 | struct umax_pred_ty { |
1812 | static bool match(ICmpInst::Predicate Pred) { |
1813 | return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE; |
1814 | } |
1815 | }; |
1816 | |
1817 | /// Helper class for identifying unsigned min predicates. |
1818 | struct umin_pred_ty { |
1819 | static bool match(ICmpInst::Predicate Pred) { |
1820 | return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE; |
1821 | } |
1822 | }; |
1823 | |
1824 | /// Helper class for identifying ordered max predicates. |
1825 | struct ofmax_pred_ty { |
1826 | static bool match(FCmpInst::Predicate Pred) { |
1827 | return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE; |
1828 | } |
1829 | }; |
1830 | |
1831 | /// Helper class for identifying ordered min predicates. |
1832 | struct ofmin_pred_ty { |
1833 | static bool match(FCmpInst::Predicate Pred) { |
1834 | return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE; |
1835 | } |
1836 | }; |
1837 | |
1838 | /// Helper class for identifying unordered max predicates. |
1839 | struct ufmax_pred_ty { |
1840 | static bool match(FCmpInst::Predicate Pred) { |
1841 | return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE; |
1842 | } |
1843 | }; |
1844 | |
1845 | /// Helper class for identifying unordered min predicates. |
1846 | struct ufmin_pred_ty { |
1847 | static bool match(FCmpInst::Predicate Pred) { |
1848 | return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE; |
1849 | } |
1850 | }; |
1851 | |
1852 | template <typename LHS, typename RHS> |
1853 | inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L, |
1854 | const RHS &R) { |
1855 | return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R); |
1856 | } |
1857 | |
1858 | template <typename LHS, typename RHS> |
1859 | inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L, |
1860 | const RHS &R) { |
1861 | return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R); |
1862 | } |
1863 | |
1864 | template <typename LHS, typename RHS> |
1865 | inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L, |
1866 | const RHS &R) { |
1867 | return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R); |
1868 | } |
1869 | |
1870 | template <typename LHS, typename RHS> |
1871 | inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L, |
1872 | const RHS &R) { |
1873 | return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R); |
1874 | } |
1875 | |
1876 | template <typename LHS, typename RHS> |
1877 | inline match_combine_or< |
1878 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>, |
1879 | MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>, |
1880 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>, |
1881 | MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>> |
1882 | m_MaxOrMin(const LHS &L, const RHS &R) { |
1883 | return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)), |
1884 | m_CombineOr(m_UMax(L, R), m_UMin(L, R))); |
1885 | } |
1886 | |
1887 | /// Match an 'ordered' floating point maximum function. |
1888 | /// Floating point has one special value 'NaN'. Therefore, there is no total |
1889 | /// order. However, if we can ignore the 'NaN' value (for example, because of a |
1890 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' |
1891 | /// semantics. In the presence of 'NaN' we have to preserve the original |
1892 | /// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate. |
1893 | /// |
1894 | /// max(L, R) iff L and R are not NaN |
1895 | /// m_OrdFMax(L, R) = R iff L or R are NaN |
1896 | template <typename LHS, typename RHS> |
1897 | inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L, |
1898 | const RHS &R) { |
1899 | return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R); |
1900 | } |
1901 | |
1902 | /// Match an 'ordered' floating point minimum function. |
1903 | /// Floating point has one special value 'NaN'. Therefore, there is no total |
1904 | /// order. However, if we can ignore the 'NaN' value (for example, because of a |
1905 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' |
1906 | /// semantics. In the presence of 'NaN' we have to preserve the original |
1907 | /// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate. |
1908 | /// |
1909 | /// min(L, R) iff L and R are not NaN |
1910 | /// m_OrdFMin(L, R) = R iff L or R are NaN |
1911 | template <typename LHS, typename RHS> |
1912 | inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L, |
1913 | const RHS &R) { |
1914 | return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R); |
1915 | } |
1916 | |
1917 | /// Match an 'unordered' floating point maximum function. |
1918 | /// Floating point has one special value 'NaN'. Therefore, there is no total |
1919 | /// order. However, if we can ignore the 'NaN' value (for example, because of a |
1920 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum' |
1921 | /// semantics. In the presence of 'NaN' we have to preserve the original |
1922 | /// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate. |
1923 | /// |
1924 | /// max(L, R) iff L and R are not NaN |
1925 | /// m_UnordFMax(L, R) = L iff L or R are NaN |
1926 | template <typename LHS, typename RHS> |
1927 | inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty> |
1928 | m_UnordFMax(const LHS &L, const RHS &R) { |
1929 | return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R); |
1930 | } |
1931 | |
1932 | /// Match an 'unordered' floating point minimum function. |
1933 | /// Floating point has one special value 'NaN'. Therefore, there is no total |
1934 | /// order. However, if we can ignore the 'NaN' value (for example, because of a |
1935 | /// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum' |
1936 | /// semantics. In the presence of 'NaN' we have to preserve the original |
1937 | /// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate. |
1938 | /// |
1939 | /// min(L, R) iff L and R are not NaN |
1940 | /// m_UnordFMin(L, R) = L iff L or R are NaN |
1941 | template <typename LHS, typename RHS> |
1942 | inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty> |
1943 | m_UnordFMin(const LHS &L, const RHS &R) { |
1944 | return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R); |
1945 | } |
1946 | |
1947 | //===----------------------------------------------------------------------===// |
1948 | // Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b |
1949 | // Note that S might be matched to other instructions than AddInst. |
1950 | // |
1951 | |
1952 | template <typename LHS_t, typename RHS_t, typename Sum_t> |
1953 | struct UAddWithOverflow_match { |
1954 | LHS_t L; |
1955 | RHS_t R; |
1956 | Sum_t S; |
1957 | |
1958 | UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S) |
1959 | : L(L), R(R), S(S) {} |
1960 | |
1961 | template <typename OpTy> bool match(OpTy *V) { |
1962 | Value *ICmpLHS, *ICmpRHS; |
1963 | ICmpInst::Predicate Pred; |
1964 | if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V)) |
1965 | return false; |
1966 | |
1967 | Value *AddLHS, *AddRHS; |
1968 | auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS)); |
1969 | |
1970 | // (a + b) u< a, (a + b) u< b |
1971 | if (Pred == ICmpInst::ICMP_ULT) |
1972 | if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS)) |
1973 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); |
1974 | |
1975 | // a >u (a + b), b >u (a + b) |
1976 | if (Pred == ICmpInst::ICMP_UGT) |
1977 | if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS)) |
1978 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); |
1979 | |
1980 | Value *Op1; |
1981 | auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes())); |
1982 | // (a ^ -1) <u b |
1983 | if (Pred == ICmpInst::ICMP_ULT) { |
1984 | if (XorExpr.match(ICmpLHS)) |
1985 | return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS); |
1986 | } |
1987 | // b > u (a ^ -1) |
1988 | if (Pred == ICmpInst::ICMP_UGT) { |
1989 | if (XorExpr.match(ICmpRHS)) |
1990 | return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS); |
1991 | } |
1992 | |
1993 | // Match special-case for increment-by-1. |
1994 | if (Pred == ICmpInst::ICMP_EQ) { |
1995 | // (a + 1) == 0 |
1996 | // (1 + a) == 0 |
1997 | if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) && |
1998 | (m_One().match(AddLHS) || m_One().match(AddRHS))) |
1999 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS); |
2000 | // 0 == (a + 1) |
2001 | // 0 == (1 + a) |
2002 | if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) && |
2003 | (m_One().match(AddLHS) || m_One().match(AddRHS))) |
2004 | return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS); |
2005 | } |
2006 | |
2007 | return false; |
2008 | } |
2009 | }; |
2010 | |
2011 | /// Match an icmp instruction checking for unsigned overflow on addition. |
2012 | /// |
2013 | /// S is matched to the addition whose result is being checked for overflow, and |
2014 | /// L and R are matched to the LHS and RHS of S. |
2015 | template <typename LHS_t, typename RHS_t, typename Sum_t> |
2016 | UAddWithOverflow_match<LHS_t, RHS_t, Sum_t> |
2017 | m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) { |
2018 | return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S); |
2019 | } |
2020 | |
2021 | template <typename Opnd_t> struct Argument_match { |
2022 | unsigned OpI; |
2023 | Opnd_t Val; |
2024 | |
2025 | Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {} |
2026 | |
2027 | template <typename OpTy> bool match(OpTy *V) { |
2028 | // FIXME: Should likely be switched to use `CallBase`. |
2029 | if (const auto *CI = dyn_cast<CallInst>(V)) |
2030 | return Val.match(CI->getArgOperand(OpI)); |
2031 | return false; |
2032 | } |
2033 | }; |
2034 | |
2035 | /// Match an argument. |
2036 | template <unsigned OpI, typename Opnd_t> |
2037 | inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) { |
2038 | return Argument_match<Opnd_t>(OpI, Op); |
2039 | } |
2040 | |
2041 | /// Intrinsic matchers. |
2042 | struct IntrinsicID_match { |
2043 | unsigned ID; |
2044 | |
2045 | IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {} |
2046 | |
2047 | template <typename OpTy> bool match(OpTy *V) { |
2048 | if (const auto *CI = dyn_cast<CallInst>(V)) |
2049 | if (const auto *F = CI->getCalledFunction()) |
2050 | return F->getIntrinsicID() == ID; |
2051 | return false; |
2052 | } |
2053 | }; |
2054 | |
2055 | /// Intrinsic matches are combinations of ID matchers, and argument |
2056 | /// matchers. Higher arity matcher are defined recursively in terms of and-ing |
2057 | /// them with lower arity matchers. Here's some convenient typedefs for up to |
2058 | /// several arguments, and more can be added as needed |
2059 | template <typename T0 = void, typename T1 = void, typename T2 = void, |
2060 | typename T3 = void, typename T4 = void, typename T5 = void, |
2061 | typename T6 = void, typename T7 = void, typename T8 = void, |
2062 | typename T9 = void, typename T10 = void> |
2063 | struct m_Intrinsic_Ty; |
2064 | template <typename T0> struct m_Intrinsic_Ty<T0> { |
2065 | using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>; |
2066 | }; |
2067 | template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> { |
2068 | using Ty = |
2069 | match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>; |
2070 | }; |
2071 | template <typename T0, typename T1, typename T2> |
2072 | struct m_Intrinsic_Ty<T0, T1, T2> { |
2073 | using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty, |
2074 | Argument_match<T2>>; |
2075 | }; |
2076 | template <typename T0, typename T1, typename T2, typename T3> |
2077 | struct m_Intrinsic_Ty<T0, T1, T2, T3> { |
2078 | using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty, |
2079 | Argument_match<T3>>; |
2080 | }; |
2081 | |
2082 | template <typename T0, typename T1, typename T2, typename T3, typename T4> |
2083 | struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> { |
2084 | using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty, |
2085 | Argument_match<T4>>; |
2086 | }; |
2087 | |
2088 | template <typename T0, typename T1, typename T2, typename T3, typename T4, |
2089 | typename T5> |
2090 | struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> { |
2091 | using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty, |
2092 | Argument_match<T5>>; |
2093 | }; |
2094 | |
2095 | /// Match intrinsic calls like this: |
2096 | /// m_Intrinsic<Intrinsic::fabs>(m_Value(X)) |
2097 | template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() { |
2098 | return IntrinsicID_match(IntrID); |
2099 | } |
2100 | |
2101 | /// Matches MaskedLoad Intrinsic. |
2102 | template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> |
2103 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty |
2104 | m_MaskedLoad(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, |
2105 | const Opnd3 &Op3) { |
2106 | return m_Intrinsic<Intrinsic::masked_load>(Op0, Op1, Op2, Op3); |
2107 | } |
2108 | |
2109 | /// Matches MaskedGather Intrinsic. |
2110 | template <typename Opnd0, typename Opnd1, typename Opnd2, typename Opnd3> |
2111 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2, Opnd3>::Ty |
2112 | m_MaskedGather(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2, |
2113 | const Opnd3 &Op3) { |
2114 | return m_Intrinsic<Intrinsic::masked_gather>(Op0, Op1, Op2, Op3); |
2115 | } |
2116 | |
2117 | template <Intrinsic::ID IntrID, typename T0> |
2118 | inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) { |
2119 | return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0)); |
2120 | } |
2121 | |
2122 | template <Intrinsic::ID IntrID, typename T0, typename T1> |
2123 | inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0, |
2124 | const T1 &Op1) { |
2125 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1)); |
2126 | } |
2127 | |
2128 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2> |
2129 | inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty |
2130 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) { |
2131 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2)); |
2132 | } |
2133 | |
2134 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, |
2135 | typename T3> |
2136 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty |
2137 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) { |
2138 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3)); |
2139 | } |
2140 | |
2141 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, |
2142 | typename T3, typename T4> |
2143 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty |
2144 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, |
2145 | const T4 &Op4) { |
2146 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3), |
2147 | m_Argument<4>(Op4)); |
2148 | } |
2149 | |
2150 | template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2, |
2151 | typename T3, typename T4, typename T5> |
2152 | inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty |
2153 | m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3, |
2154 | const T4 &Op4, const T5 &Op5) { |
2155 | return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4), |
2156 | m_Argument<5>(Op5)); |
2157 | } |
2158 | |
2159 | // Helper intrinsic matching specializations. |
2160 | template <typename Opnd0> |
2161 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) { |
2162 | return m_Intrinsic<Intrinsic::bitreverse>(Op0); |
2163 | } |
2164 | |
2165 | template <typename Opnd0> |
2166 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) { |
2167 | return m_Intrinsic<Intrinsic::bswap>(Op0); |
2168 | } |
2169 | |
2170 | template <typename Opnd0> |
2171 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) { |
2172 | return m_Intrinsic<Intrinsic::fabs>(Op0); |
2173 | } |
2174 | |
2175 | template <typename Opnd0> |
2176 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) { |
2177 | return m_Intrinsic<Intrinsic::canonicalize>(Op0); |
2178 | } |
2179 | |
2180 | template <typename Opnd0, typename Opnd1> |
2181 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0, |
2182 | const Opnd1 &Op1) { |
2183 | return m_Intrinsic<Intrinsic::minnum>(Op0, Op1); |
2184 | } |
2185 | |
2186 | template <typename Opnd0, typename Opnd1> |
2187 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0, |
2188 | const Opnd1 &Op1) { |
2189 | return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1); |
2190 | } |
2191 | |
2192 | template <typename Opnd0, typename Opnd1, typename Opnd2> |
2193 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty |
2194 | m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { |
2195 | return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2); |
2196 | } |
2197 | |
2198 | template <typename Opnd0, typename Opnd1, typename Opnd2> |
2199 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty |
2200 | m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) { |
2201 | return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2); |
2202 | } |
2203 | |
2204 | template <typename Opnd0> |
2205 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_Sqrt(const Opnd0 &Op0) { |
2206 | return m_Intrinsic<Intrinsic::sqrt>(Op0); |
2207 | } |
2208 | |
2209 | template <typename Opnd0, typename Opnd1> |
2210 | inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_CopySign(const Opnd0 &Op0, |
2211 | const Opnd1 &Op1) { |
2212 | return m_Intrinsic<Intrinsic::copysign>(Op0, Op1); |
2213 | } |
2214 | |
2215 | template <typename Opnd0> |
2216 | inline typename m_Intrinsic_Ty<Opnd0>::Ty m_VecReverse(const Opnd0 &Op0) { |
2217 | return m_Intrinsic<Intrinsic::experimental_vector_reverse>(Op0); |
2218 | } |
2219 | |
2220 | //===----------------------------------------------------------------------===// |
2221 | // Matchers for two-operands operators with the operators in either order |
2222 | // |
2223 | |
2224 | /// Matches a BinaryOperator with LHS and RHS in either order. |
2225 | template <typename LHS, typename RHS> |
2226 | inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) { |
2227 | return AnyBinaryOp_match<LHS, RHS, true>(L, R); |
2228 | } |
2229 | |
2230 | /// Matches an ICmp with a predicate over LHS and RHS in either order. |
2231 | /// Swaps the predicate if operands are commuted. |
2232 | template <typename LHS, typename RHS> |
2233 | inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true> |
2234 | m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) { |
2235 | return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L, |
2236 | R); |
2237 | } |
2238 | |
2239 | /// Matches a specific opcode with LHS and RHS in either order. |
2240 | template <typename LHS, typename RHS> |
2241 | inline SpecificBinaryOp_match<LHS, RHS, true> |
2242 | m_c_BinOp(unsigned Opcode, const LHS &L, const RHS &R) { |
2243 | return SpecificBinaryOp_match<LHS, RHS, true>(Opcode, L, R); |
2244 | } |
2245 | |
2246 | /// Matches a Add with LHS and RHS in either order. |
2247 | template <typename LHS, typename RHS> |
2248 | inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L, |
2249 | const RHS &R) { |
2250 | return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R); |
2251 | } |
2252 | |
2253 | /// Matches a Mul with LHS and RHS in either order. |
2254 | template <typename LHS, typename RHS> |
2255 | inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L, |
2256 | const RHS &R) { |
2257 | return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R); |
2258 | } |
2259 | |
2260 | /// Matches an And with LHS and RHS in either order. |
2261 | template <typename LHS, typename RHS> |
2262 | inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L, |
2263 | const RHS &R) { |
2264 | return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R); |
2265 | } |
2266 | |
2267 | /// Matches an Or with LHS and RHS in either order. |
2268 | template <typename LHS, typename RHS> |
2269 | inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L, |
2270 | const RHS &R) { |
2271 | return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R); |
2272 | } |
2273 | |
2274 | /// Matches an Xor with LHS and RHS in either order. |
2275 | template <typename LHS, typename RHS> |
2276 | inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L, |
2277 | const RHS &R) { |
2278 | return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R); |
2279 | } |
2280 | |
2281 | /// Matches a 'Neg' as 'sub 0, V'. |
2282 | template <typename ValTy> |
2283 | inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub> |
2284 | m_Neg(const ValTy &V) { |
2285 | return m_Sub(m_ZeroInt(), V); |
2286 | } |
2287 | |
2288 | /// Matches a 'Neg' as 'sub nsw 0, V'. |
2289 | template <typename ValTy> |
2290 | inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, |
2291 | Instruction::Sub, |
2292 | OverflowingBinaryOperator::NoSignedWrap> |
2293 | m_NSWNeg(const ValTy &V) { |
2294 | return m_NSWSub(m_ZeroInt(), V); |
2295 | } |
2296 | |
2297 | /// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'. |
2298 | /// NOTE: we first match the 'Not' (by matching '-1'), |
2299 | /// and only then match the inner matcher! |
2300 | template <typename ValTy> |
2301 | inline BinaryOp_match<cst_pred_ty<is_all_ones>, ValTy, Instruction::Xor, true> |
2302 | m_Not(const ValTy &V) { |
2303 | return m_c_Xor(m_AllOnes(), V); |
2304 | } |
2305 | |
2306 | template <typename ValTy> struct NotForbidUndef_match { |
2307 | ValTy Val; |
2308 | NotForbidUndef_match(const ValTy &V) : Val(V) {} |
2309 | |
2310 | template <typename OpTy> bool match(OpTy *V) { |
2311 | // We do not use m_c_Xor because that could match an arbitrary APInt that is |
2312 | // not -1 as C and then fail to match the other operand if it is -1. |
2313 | // This code should still work even when both operands are constants. |
2314 | Value *X; |
2315 | const APInt *C; |
2316 | if (m_Xor(m_Value(X), m_APIntForbidUndef(C)).match(V) && C->isAllOnes()) |
2317 | return Val.match(X); |
2318 | if (m_Xor(m_APIntForbidUndef(C), m_Value(X)).match(V) && C->isAllOnes()) |
2319 | return Val.match(X); |
2320 | return false; |
2321 | } |
2322 | }; |
2323 | |
2324 | /// Matches a bitwise 'not' as 'xor V, -1' or 'xor -1, V'. For vectors, the |
2325 | /// constant value must be composed of only -1 scalar elements. |
2326 | template <typename ValTy> |
2327 | inline NotForbidUndef_match<ValTy> m_NotForbidUndef(const ValTy &V) { |
2328 | return NotForbidUndef_match<ValTy>(V); |
2329 | } |
2330 | |
2331 | /// Matches an SMin with LHS and RHS in either order. |
2332 | template <typename LHS, typename RHS> |
2333 | inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true> |
2334 | m_c_SMin(const LHS &L, const RHS &R) { |
2335 | return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R); |
2336 | } |
2337 | /// Matches an SMax with LHS and RHS in either order. |
2338 | template <typename LHS, typename RHS> |
2339 | inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true> |
2340 | m_c_SMax(const LHS &L, const RHS &R) { |
2341 | return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R); |
2342 | } |
2343 | /// Matches a UMin with LHS and RHS in either order. |
2344 | template <typename LHS, typename RHS> |
2345 | inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true> |
2346 | m_c_UMin(const LHS &L, const RHS &R) { |
2347 | return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R); |
2348 | } |
2349 | /// Matches a UMax with LHS and RHS in either order. |
2350 | template <typename LHS, typename RHS> |
2351 | inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true> |
2352 | m_c_UMax(const LHS &L, const RHS &R) { |
2353 | return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R); |
2354 | } |
2355 | |
2356 | template <typename LHS, typename RHS> |
2357 | inline match_combine_or< |
2358 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>, |
2359 | MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>, |
2360 | match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>, |
2361 | MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>> |
2362 | m_c_MaxOrMin(const LHS &L, const RHS &R) { |
2363 | return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)), |
2364 | m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R))); |
2365 | } |
2366 | |
2367 | template <Intrinsic::ID IntrID, typename T0, typename T1> |
2368 | inline match_combine_or<typename m_Intrinsic_Ty<T0, T1>::Ty, |
2369 | typename m_Intrinsic_Ty<T1, T0>::Ty> |
2370 | m_c_Intrinsic(const T0 &Op0, const T1 &Op1) { |
2371 | return m_CombineOr(m_Intrinsic<IntrID>(Op0, Op1), |
2372 | m_Intrinsic<IntrID>(Op1, Op0)); |
2373 | } |
2374 | |
2375 | /// Matches FAdd with LHS and RHS in either order. |
2376 | template <typename LHS, typename RHS> |
2377 | inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true> |
2378 | m_c_FAdd(const LHS &L, const RHS &R) { |
2379 | return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R); |
2380 | } |
2381 | |
2382 | /// Matches FMul with LHS and RHS in either order. |
2383 | template <typename LHS, typename RHS> |
2384 | inline BinaryOp_match<LHS, RHS, Instruction::FMul, true> |
2385 | m_c_FMul(const LHS &L, const RHS &R) { |
2386 | return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R); |
2387 | } |
2388 | |
2389 | template <typename Opnd_t> struct Signum_match { |
2390 | Opnd_t Val; |
2391 | Signum_match(const Opnd_t &V) : Val(V) {} |
2392 | |
2393 | template <typename OpTy> bool match(OpTy *V) { |
2394 | unsigned TypeSize = V->getType()->getScalarSizeInBits(); |
2395 | if (TypeSize == 0) |
2396 | return false; |
2397 | |
2398 | unsigned ShiftWidth = TypeSize - 1; |
2399 | Value *OpL = nullptr, *OpR = nullptr; |
2400 | |
2401 | // This is the representation of signum we match: |
2402 | // |
2403 | // signum(x) == (x >> 63) | (-x >>u 63) |
2404 | // |
2405 | // An i1 value is its own signum, so it's correct to match |
2406 | // |
2407 | // signum(x) == (x >> 0) | (-x >>u 0) |
2408 | // |
2409 | // for i1 values. |
2410 | |
2411 | auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth)); |
2412 | auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth)); |
2413 | auto Signum = m_Or(LHS, RHS); |
2414 | |
2415 | return Signum.match(V) && OpL == OpR && Val.match(OpL); |
2416 | } |
2417 | }; |
2418 | |
2419 | /// Matches a signum pattern. |
2420 | /// |
2421 | /// signum(x) = |
2422 | /// x > 0 -> 1 |
2423 | /// x == 0 -> 0 |
2424 | /// x < 0 -> -1 |
2425 | template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) { |
2426 | return Signum_match<Val_t>(V); |
2427 | } |
2428 | |
2429 | template <int Ind, typename Opnd_t> struct ExtractValue_match { |
2430 | Opnd_t Val; |
2431 | ExtractValue_match(const Opnd_t &V) : Val(V) {} |
2432 | |
2433 | template <typename OpTy> bool match(OpTy *V) { |
2434 | if (auto *I = dyn_cast<ExtractValueInst>(V)) { |
2435 | // If Ind is -1, don't inspect indices |
2436 | if (Ind != -1 && |
2437 | !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind)) |
2438 | return false; |
2439 | return Val.match(I->getAggregateOperand()); |
2440 | } |
2441 | return false; |
2442 | } |
2443 | }; |
2444 | |
2445 | /// Match a single index ExtractValue instruction. |
2446 | /// For example m_ExtractValue<1>(...) |
2447 | template <int Ind, typename Val_t> |
2448 | inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) { |
2449 | return ExtractValue_match<Ind, Val_t>(V); |
2450 | } |
2451 | |
2452 | /// Match an ExtractValue instruction with any index. |
2453 | /// For example m_ExtractValue(...) |
2454 | template <typename Val_t> |
2455 | inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) { |
2456 | return ExtractValue_match<-1, Val_t>(V); |
2457 | } |
2458 | |
2459 | /// Matcher for a single index InsertValue instruction. |
2460 | template <int Ind, typename T0, typename T1> struct InsertValue_match { |
2461 | T0 Op0; |
2462 | T1 Op1; |
2463 | |
2464 | InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {} |
2465 | |
2466 | template <typename OpTy> bool match(OpTy *V) { |
2467 | if (auto *I = dyn_cast<InsertValueInst>(V)) { |
2468 | return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) && |
2469 | I->getNumIndices() == 1 && Ind == I->getIndices()[0]; |
2470 | } |
2471 | return false; |
2472 | } |
2473 | }; |
2474 | |
2475 | /// Matches a single index InsertValue instruction. |
2476 | template <int Ind, typename Val_t, typename Elt_t> |
2477 | inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val, |
2478 | const Elt_t &Elt) { |
2479 | return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt); |
2480 | } |
2481 | |
2482 | /// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or |
2483 | /// the constant expression |
2484 | /// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>` |
2485 | /// under the right conditions determined by DataLayout. |
2486 | struct VScaleVal_match { |
2487 | template <typename ITy> bool match(ITy *V) { |
2488 | if (m_Intrinsic<Intrinsic::vscale>().match(V)) |
2489 | return true; |
2490 | |
2491 | Value *Ptr; |
2492 | if (m_PtrToInt(m_Value(Ptr)).match(V)) { |
2493 | if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { |
2494 | auto *DerefTy = |
2495 | dyn_cast<ScalableVectorType>(GEP->getSourceElementType()); |
2496 | if (GEP->getNumIndices() == 1 && DerefTy && |
2497 | DerefTy->getElementType()->isIntegerTy(8) && |
2498 | m_Zero().match(GEP->getPointerOperand()) && |
2499 | m_SpecificInt(1).match(GEP->idx_begin()->get())) |
2500 | return true; |
2501 | } |
2502 | } |
2503 | |
2504 | return false; |
2505 | } |
2506 | }; |
2507 | |
2508 | inline VScaleVal_match m_VScale() { |
2509 | return VScaleVal_match(); |
2510 | } |
2511 | |
2512 | template <typename LHS, typename RHS, unsigned Opcode, bool Commutable = false> |
2513 | struct LogicalOp_match { |
2514 | LHS L; |
2515 | RHS R; |
2516 | |
2517 | LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {} |
2518 | |
2519 | template <typename T> bool match(T *V) { |
2520 | auto *I = dyn_cast<Instruction>(V); |
2521 | if (!I || !I->getType()->isIntOrIntVectorTy(1)) |
2522 | return false; |
2523 | |
2524 | if (I->getOpcode() == Opcode) { |
2525 | auto *Op0 = I->getOperand(0); |
2526 | auto *Op1 = I->getOperand(1); |
2527 | return (L.match(Op0) && R.match(Op1)) || |
2528 | (Commutable && L.match(Op1) && R.match(Op0)); |
2529 | } |
2530 | |
2531 | if (auto *Select = dyn_cast<SelectInst>(I)) { |
2532 | auto *Cond = Select->getCondition(); |
2533 | auto *TVal = Select->getTrueValue(); |
2534 | auto *FVal = Select->getFalseValue(); |
2535 | |
2536 | // Don't match a scalar select of bool vectors. |
2537 | // Transforms expect a single type for operands if this matches. |
2538 | if (Cond->getType() != Select->getType()) |
2539 | return false; |
2540 | |
2541 | if (Opcode == Instruction::And) { |
2542 | auto *C = dyn_cast<Constant>(FVal); |
2543 | if (C && C->isNullValue()) |
2544 | return (L.match(Cond) && R.match(TVal)) || |
2545 | (Commutable && L.match(TVal) && R.match(Cond)); |
2546 | } else { |
2547 | assert(Opcode == Instruction::Or)(static_cast <bool> (Opcode == Instruction::Or) ? void ( 0) : __assert_fail ("Opcode == Instruction::Or", "llvm/include/llvm/IR/PatternMatch.h" , 2547, __extension__ __PRETTY_FUNCTION__)); |
2548 | auto *C = dyn_cast<Constant>(TVal); |
2549 | if (C && C->isOneValue()) |
2550 | return (L.match(Cond) && R.match(FVal)) || |
2551 | (Commutable && L.match(FVal) && R.match(Cond)); |
2552 | } |
2553 | } |
2554 | |
2555 | return false; |
2556 | } |
2557 | }; |
2558 | |
2559 | /// Matches L && R either in the form of L & R or L ? R : false. |
2560 | /// Note that the latter form is poison-blocking. |
2561 | template <typename LHS, typename RHS> |
2562 | inline LogicalOp_match<LHS, RHS, Instruction::And> m_LogicalAnd(const LHS &L, |
2563 | const RHS &R) { |
2564 | return LogicalOp_match<LHS, RHS, Instruction::And>(L, R); |
2565 | } |
2566 | |
2567 | /// Matches L && R where L and R are arbitrary values. |
2568 | inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); } |
2569 | |
2570 | /// Matches L && R with LHS and RHS in either order. |
2571 | template <typename LHS, typename RHS> |
2572 | inline LogicalOp_match<LHS, RHS, Instruction::And, true> |
2573 | m_c_LogicalAnd(const LHS &L, const RHS &R) { |
2574 | return LogicalOp_match<LHS, RHS, Instruction::And, true>(L, R); |
2575 | } |
2576 | |
2577 | /// Matches L || R either in the form of L | R or L ? true : R. |
2578 | /// Note that the latter form is poison-blocking. |
2579 | template <typename LHS, typename RHS> |
2580 | inline LogicalOp_match<LHS, RHS, Instruction::Or> m_LogicalOr(const LHS &L, |
2581 | const RHS &R) { |
2582 | return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R); |
2583 | } |
2584 | |
2585 | /// Matches L || R where L and R are arbitrary values. |
2586 | inline auto m_LogicalOr() { return m_LogicalOr(m_Value(), m_Value()); } |
2587 | |
2588 | /// Matches L || R with LHS and RHS in either order. |
2589 | template <typename LHS, typename RHS> |
2590 | inline LogicalOp_match<LHS, RHS, Instruction::Or, true> |
2591 | m_c_LogicalOr(const LHS &L, const RHS &R) { |
2592 | return LogicalOp_match<LHS, RHS, Instruction::Or, true>(L, R); |
2593 | } |
2594 | |
2595 | /// Matches either L && R or L || R, |
2596 | /// either one being in the either binary or logical form. |
2597 | /// Note that the latter form is poison-blocking. |
2598 | template <typename LHS, typename RHS, bool Commutable = false> |
2599 | inline auto m_LogicalOp(const LHS &L, const RHS &R) { |
2600 | return m_CombineOr( |
2601 | LogicalOp_match<LHS, RHS, Instruction::And, Commutable>(L, R), |
2602 | LogicalOp_match<LHS, RHS, Instruction::Or, Commutable>(L, R)); |
2603 | } |
2604 | |
2605 | /// Matches either L && R or L || R where L and R are arbitrary values. |
2606 | inline auto m_LogicalOp() { return m_LogicalOp(m_Value(), m_Value()); } |
2607 | |
2608 | /// Matches either L && R or L || R with LHS and RHS in either order. |
2609 | template <typename LHS, typename RHS> |
2610 | inline auto m_c_LogicalOp(const LHS &L, const RHS &R) { |
2611 | return m_LogicalOp<LHS, RHS, /*Commutable=*/true>(L, R); |
2612 | } |
2613 | |
2614 | } // end namespace PatternMatch |
2615 | } // end namespace llvm |
2616 | |
2617 | #endif // LLVM_IR_PATTERNMATCH_H |