Bug Summary

File:llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
Warning:line 225, column 33
Called C++ object pointer is uninitialized

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name InstCombineShifts.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/build-llvm/lib/Transforms/InstCombine -resource-dir /usr/lib/llvm-13/lib/clang/13.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/build-llvm/lib/Transforms/InstCombine -I /build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine -I /build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/build-llvm/include -I /build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-13/lib/clang/13.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/build-llvm/lib/Transforms/InstCombine -fdebug-prefix-map=/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd=. -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-03-15-022507-3198-1 -x c++ /build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

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/ConstantFolding.h"
15#include "llvm/Analysis/InstructionSimplify.h"
16#include "llvm/IR/IntrinsicInst.h"
17#include "llvm/IR/PatternMatch.h"
18#include "llvm/Transforms/InstCombine/InstCombiner.h"
19using namespace llvm;
20using namespace PatternMatch;
21
22#define DEBUG_TYPE"instcombine" "instcombine"
23
24// Given pattern:
25// (x shiftopcode Q) shiftopcode K
26// we should rewrite it as
27// x shiftopcode (Q+K) iff (Q+K) u< bitwidth(x) and
28//
29// This is valid for any shift, but they must be identical, and we must be
30// careful in case we have (zext(Q)+zext(K)) and look past extensions,
31// (Q+K) must not overflow or else (Q+K) u< bitwidth(x) is bogus.
32//
33// AnalyzeForSignBitExtraction indicates that we will only analyze whether this
34// pattern has any 2 right-shifts that sum to 1 less than original bit width.
35Value *InstCombinerImpl::reassociateShiftAmtsOfTwoSameDirectionShifts(
36 BinaryOperator *Sh0, const SimplifyQuery &SQ,
37 bool AnalyzeForSignBitExtraction) {
38 // Look for a shift of some instruction, ignore zext of shift amount if any.
39 Instruction *Sh0Op0;
40 Value *ShAmt0;
41 if (!match(Sh0,
42 m_Shift(m_Instruction(Sh0Op0), m_ZExtOrSelf(m_Value(ShAmt0)))))
43 return nullptr;
44
45 // If there is a truncation between the two shifts, we must make note of it
46 // and look through it. The truncation imposes additional constraints on the
47 // transform.
48 Instruction *Sh1;
49 Value *Trunc = nullptr;
50 match(Sh0Op0,
51 m_CombineOr(m_CombineAnd(m_Trunc(m_Instruction(Sh1)), m_Value(Trunc)),
52 m_Instruction(Sh1)));
53
54 // Inner shift: (x shiftopcode ShAmt1)
55 // Like with other shift, ignore zext of shift amount if any.
56 Value *X, *ShAmt1;
57 if (!match(Sh1, m_Shift(m_Value(X), m_ZExtOrSelf(m_Value(ShAmt1)))))
58 return nullptr;
59
60 // We have two shift amounts from two different shifts. The types of those
61 // shift amounts may not match. If that's the case let's bailout now..
62 if (ShAmt0->getType() != ShAmt1->getType())
63 return nullptr;
64
65 // As input, we have the following pattern:
66 // Sh0 (Sh1 X, Q), K
67 // We want to rewrite that as:
68 // Sh x, (Q+K) iff (Q+K) u< bitwidth(x)
69 // While we know that originally (Q+K) would not overflow
70 // (because 2 * (N-1) u<= iN -1), we have looked past extensions of
71 // shift amounts. so it may now overflow in smaller bitwidth.
72 // To ensure that does not happen, we need to ensure that the total maximal
73 // shift amount is still representable in that smaller bit width.
74 unsigned MaximalPossibleTotalShiftAmount =
75 (Sh0->getType()->getScalarSizeInBits() - 1) +
76 (Sh1->getType()->getScalarSizeInBits() - 1);
77 APInt MaximalRepresentableShiftAmount =
78 APInt::getAllOnesValue(ShAmt0->getType()->getScalarSizeInBits());
79 if (MaximalRepresentableShiftAmount.ult(MaximalPossibleTotalShiftAmount))
80 return nullptr;
81
82 // We are only looking for signbit extraction if we have two right shifts.
83 bool HadTwoRightShifts = match(Sh0, m_Shr(m_Value(), m_Value())) &&
84 match(Sh1, m_Shr(m_Value(), m_Value()));
85 // ... and if it's not two right-shifts, we know the answer already.
86 if (AnalyzeForSignBitExtraction && !HadTwoRightShifts)
87 return nullptr;
88
89 // The shift opcodes must be identical, unless we are just checking whether
90 // this pattern can be interpreted as a sign-bit-extraction.
91 Instruction::BinaryOps ShiftOpcode = Sh0->getOpcode();
92 bool IdenticalShOpcodes = Sh0->getOpcode() == Sh1->getOpcode();
93 if (!IdenticalShOpcodes && !AnalyzeForSignBitExtraction)
94 return nullptr;
95
96 // If we saw truncation, we'll need to produce extra instruction,
97 // and for that one of the operands of the shift must be one-use,
98 // unless of course we don't actually plan to produce any instructions here.
99 if (Trunc && !AnalyzeForSignBitExtraction &&
100 !match(Sh0, m_c_BinOp(m_OneUse(m_Value()), m_Value())))
101 return nullptr;
102
103 // Can we fold (ShAmt0+ShAmt1) ?
104 auto *NewShAmt = dyn_cast_or_null<Constant>(
105 SimplifyAddInst(ShAmt0, ShAmt1, /*isNSW=*/false, /*isNUW=*/false,
106 SQ.getWithInstruction(Sh0)));
107 if (!NewShAmt)
108 return nullptr; // Did not simplify.
109 unsigned NewShAmtBitWidth = NewShAmt->getType()->getScalarSizeInBits();
110 unsigned XBitWidth = X->getType()->getScalarSizeInBits();
111 // Is the new shift amount smaller than the bit width of inner/new shift?
112 if (!match(NewShAmt, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_ULT,
113 APInt(NewShAmtBitWidth, XBitWidth))))
114 return nullptr; // FIXME: could perform constant-folding.
115
116 // If there was a truncation, and we have a right-shift, we can only fold if
117 // we are left with the original sign bit. Likewise, if we were just checking
118 // that this is a sighbit extraction, this is the place to check it.
119 // FIXME: zero shift amount is also legal here, but we can't *easily* check
120 // more than one predicate so it's not really worth it.
121 if (HadTwoRightShifts && (Trunc || AnalyzeForSignBitExtraction)) {
122 // If it's not a sign bit extraction, then we're done.
123 if (!match(NewShAmt,
124 m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
125 APInt(NewShAmtBitWidth, XBitWidth - 1))))
126 return nullptr;
127 // If it is, and that was the question, return the base value.
128 if (AnalyzeForSignBitExtraction)
129 return X;
130 }
131
132 assert(IdenticalShOpcodes && "Should not get here with different shifts.")((IdenticalShOpcodes && "Should not get here with different shifts."
) ? static_cast<void> (0) : __assert_fail ("IdenticalShOpcodes && \"Should not get here with different shifts.\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 132, __PRETTY_FUNCTION__))
;
133
134 // All good, we can do this fold.
135 NewShAmt = ConstantExpr::getZExtOrBitCast(NewShAmt, X->getType());
136
137 BinaryOperator *NewShift = BinaryOperator::Create(ShiftOpcode, X, NewShAmt);
138
139 // The flags can only be propagated if there wasn't a trunc.
140 if (!Trunc) {
141 // If the pattern did not involve trunc, and both of the original shifts
142 // had the same flag set, preserve the flag.
143 if (ShiftOpcode == Instruction::BinaryOps::Shl) {
144 NewShift->setHasNoUnsignedWrap(Sh0->hasNoUnsignedWrap() &&
145 Sh1->hasNoUnsignedWrap());
146 NewShift->setHasNoSignedWrap(Sh0->hasNoSignedWrap() &&
147 Sh1->hasNoSignedWrap());
148 } else {
149 NewShift->setIsExact(Sh0->isExact() && Sh1->isExact());
150 }
151 }
152
153 Instruction *Ret = NewShift;
154 if (Trunc) {
155 Builder.Insert(NewShift);
156 Ret = CastInst::Create(Instruction::Trunc, NewShift, Sh0->getType());
157 }
158
159 return Ret;
160}
161
162// If we have some pattern that leaves only some low bits set, and then performs
163// left-shift of those bits, if none of the bits that are left after the final
164// shift are modified by the mask, we can omit the mask.
165//
166// There are many variants to this pattern:
167// a) (x & ((1 << MaskShAmt) - 1)) << ShiftShAmt
168// b) (x & (~(-1 << MaskShAmt))) << ShiftShAmt
169// c) (x & (-1 >> MaskShAmt)) << ShiftShAmt
170// d) (x & ((-1 << MaskShAmt) >> MaskShAmt)) << ShiftShAmt
171// e) ((x << MaskShAmt) l>> MaskShAmt) << ShiftShAmt
172// f) ((x << MaskShAmt) a>> MaskShAmt) << ShiftShAmt
173// All these patterns can be simplified to just:
174// x << ShiftShAmt
175// iff:
176// a,b) (MaskShAmt+ShiftShAmt) u>= bitwidth(x)
177// c,d,e,f) (ShiftShAmt-MaskShAmt) s>= 0 (i.e. ShiftShAmt u>= MaskShAmt)
178static Instruction *
179dropRedundantMaskingOfLeftShiftInput(BinaryOperator *OuterShift,
180 const SimplifyQuery &Q,
181 InstCombiner::BuilderTy &Builder) {
182 assert(OuterShift->getOpcode() == Instruction::BinaryOps::Shl &&((OuterShift->getOpcode() == Instruction::BinaryOps::Shl &&
"The input must be 'shl'!") ? static_cast<void> (0) : __assert_fail
("OuterShift->getOpcode() == Instruction::BinaryOps::Shl && \"The input must be 'shl'!\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 183, __PRETTY_FUNCTION__))
8
Assuming the condition is true
9
'?' condition is true
183 "The input must be 'shl'!")((OuterShift->getOpcode() == Instruction::BinaryOps::Shl &&
"The input must be 'shl'!") ? static_cast<void> (0) : __assert_fail
("OuterShift->getOpcode() == Instruction::BinaryOps::Shl && \"The input must be 'shl'!\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 183, __PRETTY_FUNCTION__))
;
184
185 Value *Masked, *ShiftShAmt;
10
'ShiftShAmt' declared without an initial value
186 match(OuterShift,
187 m_Shift(m_Value(Masked), m_ZExtOrSelf(m_Value(ShiftShAmt))));
11
Calling 'm_Value'
15
Returning from 'm_Value'
16
Calling 'm_ZExtOrSelf<llvm::PatternMatch::bind_ty<llvm::Value>>'
24
Returning from 'm_ZExtOrSelf<llvm::PatternMatch::bind_ty<llvm::Value>>'
25
Calling 'm_Shift<llvm::PatternMatch::bind_ty<llvm::Value>, llvm::PatternMatch::match_combine_or<llvm::PatternMatch::CastClass_match<llvm::PatternMatch::bind_ty<llvm::Value>, 39>, llvm::PatternMatch::bind_ty<llvm::Value>>>'
27
Returning from 'm_Shift<llvm::PatternMatch::bind_ty<llvm::Value>, llvm::PatternMatch::match_combine_or<llvm::PatternMatch::CastClass_match<llvm::PatternMatch::bind_ty<llvm::Value>, 39>, llvm::PatternMatch::bind_ty<llvm::Value>>>'
188
189 // *If* there is a truncation between an outer shift and a possibly-mask,
190 // then said truncation *must* be one-use, else we can't perform the fold.
191 Value *Trunc;
192 if (match(Masked, m_CombineAnd(m_Trunc(m_Value(Masked)), m_Value(Trunc))) &&
28
Taking false branch
193 !Trunc->hasOneUse())
194 return nullptr;
195
196 Type *NarrowestTy = OuterShift->getType();
197 Type *WidestTy = Masked->getType();
198 bool HadTrunc = WidestTy != NarrowestTy;
29
Assuming 'WidestTy' is equal to 'NarrowestTy'
199
200 // The mask must be computed in a type twice as wide to ensure
201 // that no bits are lost if the sum-of-shifts is wider than the base type.
202 Type *ExtendedTy = WidestTy->getExtendedType();
203
204 Value *MaskShAmt;
205
206 // ((1 << MaskShAmt) - 1)
207 auto MaskA = m_Add(m_Shl(m_One(), m_Value(MaskShAmt)), m_AllOnes());
208 // (~(-1 << maskNbits))
209 auto MaskB = m_Xor(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_AllOnes());
210 // (-1 >> MaskShAmt)
211 auto MaskC = m_Shr(m_AllOnes(), m_Value(MaskShAmt));
212 // ((-1 << MaskShAmt) >> MaskShAmt)
213 auto MaskD =
214 m_Shr(m_Shl(m_AllOnes(), m_Value(MaskShAmt)), m_Deferred(MaskShAmt));
215
216 Value *X;
217 Constant *NewMask;
218
219 if (match(Masked, m_c_And(m_CombineOr(MaskA, MaskB), m_Value(X)))) {
30
Calling 'match<llvm::Value, llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::match_combine_or<llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_one, llvm::ConstantInt>, llvm::PatternMatch::bind_ty<llvm::Value>, 25, false>, llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_all_ones, llvm::ConstantInt>, 13, false>, llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_all_ones, llvm::ConstantInt>, llvm::PatternMatch::bind_ty<llvm::Value>, 25, false>, llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_all_ones, llvm::ConstantInt>, 30, false>>, llvm::PatternMatch::bind_ty<llvm::Value>, 28, true>>'
38
Returning from 'match<llvm::Value, llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::match_combine_or<llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_one, llvm::ConstantInt>, llvm::PatternMatch::bind_ty<llvm::Value>, 25, false>, llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_all_ones, llvm::ConstantInt>, 13, false>, llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::BinaryOp_match<llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_all_ones, llvm::ConstantInt>, llvm::PatternMatch::bind_ty<llvm::Value>, 25, false>, llvm::PatternMatch::cstval_pred_ty<llvm::PatternMatch::is_all_ones, llvm::ConstantInt>, 30, false>>, llvm::PatternMatch::bind_ty<llvm::Value>, 28, true>>'
39
Taking true branch
220 // Peek through an optional zext of the shift amount.
221 match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
222
223 // We have two shift amounts from two different shifts. The types of those
224 // shift amounts may not match. If that's the case let's bailout now.
225 if (MaskShAmt->getType() != ShiftShAmt->getType())
40
Called C++ object pointer is uninitialized
226 return nullptr;
227
228 // Can we simplify (MaskShAmt+ShiftShAmt) ?
229 auto *SumOfShAmts = dyn_cast_or_null<Constant>(SimplifyAddInst(
230 MaskShAmt, ShiftShAmt, /*IsNSW=*/false, /*IsNUW=*/false, Q));
231 if (!SumOfShAmts)
232 return nullptr; // Did not simplify.
233 // In this pattern SumOfShAmts correlates with the number of low bits
234 // that shall remain in the root value (OuterShift).
235
236 // An extend of an undef value becomes zero because the high bits are never
237 // completely unknown. Replace the the `undef` shift amounts with final
238 // shift bitwidth to ensure that the value remains undef when creating the
239 // subsequent shift op.
240 SumOfShAmts = Constant::replaceUndefsWith(
241 SumOfShAmts, ConstantInt::get(SumOfShAmts->getType()->getScalarType(),
242 ExtendedTy->getScalarSizeInBits()));
243 auto *ExtendedSumOfShAmts = ConstantExpr::getZExt(SumOfShAmts, ExtendedTy);
244 // And compute the mask as usual: ~(-1 << (SumOfShAmts))
245 auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy);
246 auto *ExtendedInvertedMask =
247 ConstantExpr::getShl(ExtendedAllOnes, ExtendedSumOfShAmts);
248 NewMask = ConstantExpr::getNot(ExtendedInvertedMask);
249 } else if (match(Masked, m_c_And(m_CombineOr(MaskC, MaskD), m_Value(X))) ||
250 match(Masked, m_Shr(m_Shl(m_Value(X), m_Value(MaskShAmt)),
251 m_Deferred(MaskShAmt)))) {
252 // Peek through an optional zext of the shift amount.
253 match(MaskShAmt, m_ZExtOrSelf(m_Value(MaskShAmt)));
254
255 // We have two shift amounts from two different shifts. The types of those
256 // shift amounts may not match. If that's the case let's bailout now.
257 if (MaskShAmt->getType() != ShiftShAmt->getType())
258 return nullptr;
259
260 // Can we simplify (ShiftShAmt-MaskShAmt) ?
261 auto *ShAmtsDiff = dyn_cast_or_null<Constant>(SimplifySubInst(
262 ShiftShAmt, MaskShAmt, /*IsNSW=*/false, /*IsNUW=*/false, Q));
263 if (!ShAmtsDiff)
264 return nullptr; // Did not simplify.
265 // In this pattern ShAmtsDiff correlates with the number of high bits that
266 // shall be unset in the root value (OuterShift).
267
268 // An extend of an undef value becomes zero because the high bits are never
269 // completely unknown. Replace the the `undef` shift amounts with negated
270 // bitwidth of innermost shift to ensure that the value remains undef when
271 // creating the subsequent shift op.
272 unsigned WidestTyBitWidth = WidestTy->getScalarSizeInBits();
273 ShAmtsDiff = Constant::replaceUndefsWith(
274 ShAmtsDiff, ConstantInt::get(ShAmtsDiff->getType()->getScalarType(),
275 -WidestTyBitWidth));
276 auto *ExtendedNumHighBitsToClear = ConstantExpr::getZExt(
277 ConstantExpr::getSub(ConstantInt::get(ShAmtsDiff->getType(),
278 WidestTyBitWidth,
279 /*isSigned=*/false),
280 ShAmtsDiff),
281 ExtendedTy);
282 // And compute the mask as usual: (-1 l>> (NumHighBitsToClear))
283 auto *ExtendedAllOnes = ConstantExpr::getAllOnesValue(ExtendedTy);
284 NewMask =
285 ConstantExpr::getLShr(ExtendedAllOnes, ExtendedNumHighBitsToClear);
286 } else
287 return nullptr; // Don't know anything about this pattern.
288
289 NewMask = ConstantExpr::getTrunc(NewMask, NarrowestTy);
290
291 // Does this mask has any unset bits? If not then we can just not apply it.
292 bool NeedMask = !match(NewMask, m_AllOnes());
293
294 // If we need to apply a mask, there are several more restrictions we have.
295 if (NeedMask) {
296 // The old masking instruction must go away.
297 if (!Masked->hasOneUse())
298 return nullptr;
299 // The original "masking" instruction must not have been`ashr`.
300 if (match(Masked, m_AShr(m_Value(), m_Value())))
301 return nullptr;
302 }
303
304 // If we need to apply truncation, let's do it first, since we can.
305 // We have already ensured that the old truncation will go away.
306 if (HadTrunc)
307 X = Builder.CreateTrunc(X, NarrowestTy);
308
309 // No 'NUW'/'NSW'! We no longer know that we won't shift-out non-0 bits.
310 // We didn't change the Type of this outermost shift, so we can just do it.
311 auto *NewShift = BinaryOperator::Create(OuterShift->getOpcode(), X,
312 OuterShift->getOperand(1));
313 if (!NeedMask)
314 return NewShift;
315
316 Builder.Insert(NewShift);
317 return BinaryOperator::Create(Instruction::And, NewShift, NewMask);
318}
319
320/// If we have a shift-by-constant of a bitwise logic op that itself has a
321/// shift-by-constant operand with identical opcode, we may be able to convert
322/// that into 2 independent shifts followed by the logic op. This eliminates a
323/// a use of an intermediate value (reduces dependency chain).
324static Instruction *foldShiftOfShiftedLogic(BinaryOperator &I,
325 InstCombiner::BuilderTy &Builder) {
326 assert(I.isShift() && "Expected a shift as input")((I.isShift() && "Expected a shift as input") ? static_cast
<void> (0) : __assert_fail ("I.isShift() && \"Expected a shift as input\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 326, __PRETTY_FUNCTION__))
;
327 auto *LogicInst = dyn_cast<BinaryOperator>(I.getOperand(0));
328 if (!LogicInst || !LogicInst->isBitwiseLogicOp() || !LogicInst->hasOneUse())
329 return nullptr;
330
331 Constant *C0, *C1;
332 if (!match(I.getOperand(1), m_Constant(C1)))
333 return nullptr;
334
335 Instruction::BinaryOps ShiftOpcode = I.getOpcode();
336 Type *Ty = I.getType();
337
338 // Find a matching one-use shift by constant. The fold is not valid if the sum
339 // of the shift values equals or exceeds bitwidth.
340 // TODO: Remove the one-use check if the other logic operand (Y) is constant.
341 Value *X, *Y;
342 auto matchFirstShift = [&](Value *V) {
343 BinaryOperator *BO;
344 APInt Threshold(Ty->getScalarSizeInBits(), Ty->getScalarSizeInBits());
345 return match(V, m_BinOp(BO)) && BO->getOpcode() == ShiftOpcode &&
346 match(V, m_OneUse(m_Shift(m_Value(X), m_Constant(C0)))) &&
347 match(ConstantExpr::getAdd(C0, C1),
348 m_SpecificInt_ICMP(ICmpInst::ICMP_ULT, Threshold));
349 };
350
351 // Logic ops are commutative, so check each operand for a match.
352 if (matchFirstShift(LogicInst->getOperand(0)))
353 Y = LogicInst->getOperand(1);
354 else if (matchFirstShift(LogicInst->getOperand(1)))
355 Y = LogicInst->getOperand(0);
356 else
357 return nullptr;
358
359 // shift (logic (shift X, C0), Y), C1 -> logic (shift X, C0+C1), (shift Y, C1)
360 Constant *ShiftSumC = ConstantExpr::getAdd(C0, C1);
361 Value *NewShift1 = Builder.CreateBinOp(ShiftOpcode, X, ShiftSumC);
362 Value *NewShift2 = Builder.CreateBinOp(ShiftOpcode, Y, I.getOperand(1));
363 return BinaryOperator::Create(LogicInst->getOpcode(), NewShift1, NewShift2);
364}
365
366Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
367 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
368 assert(Op0->getType() == Op1->getType())((Op0->getType() == Op1->getType()) ? static_cast<void
> (0) : __assert_fail ("Op0->getType() == Op1->getType()"
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 368, __PRETTY_FUNCTION__))
;
369
370 // If the shift amount is a one-use `sext`, we can demote it to `zext`.
371 Value *Y;
372 if (match(Op1, m_OneUse(m_SExt(m_Value(Y))))) {
373 Value *NewExt = Builder.CreateZExt(Y, I.getType(), Op1->getName());
374 return BinaryOperator::Create(I.getOpcode(), Op0, NewExt);
375 }
376
377 // See if we can fold away this shift.
378 if (SimplifyDemandedInstructionBits(I))
379 return &I;
380
381 // Try to fold constant and into select arguments.
382 if (isa<Constant>(Op0))
383 if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
384 if (Instruction *R = FoldOpIntoSelect(I, SI))
385 return R;
386
387 if (Constant *CUI = dyn_cast<Constant>(Op1))
388 if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
389 return Res;
390
391 if (auto *NewShift = cast_or_null<Instruction>(
392 reassociateShiftAmtsOfTwoSameDirectionShifts(&I, SQ)))
393 return NewShift;
394
395 // (C1 shift (A add C2)) -> (C1 shift C2) shift A)
396 // iff A and C2 are both positive.
397 Value *A;
398 Constant *C;
399 if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C))))
400 if (isKnownNonNegative(A, DL, 0, &AC, &I, &DT) &&
401 isKnownNonNegative(C, DL, 0, &AC, &I, &DT))
402 return BinaryOperator::Create(
403 I.getOpcode(), Builder.CreateBinOp(I.getOpcode(), Op0, C), A);
404
405 // X shift (A srem C) -> X shift (A and (C - 1)) iff C is a power of 2.
406 // Because shifts by negative values (which could occur if A were negative)
407 // are undefined.
408 if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Constant(C))) &&
409 match(C, m_Power2())) {
410 // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't
411 // demand the sign bit (and many others) here??
412 Constant *Mask = ConstantExpr::getSub(C, ConstantInt::get(I.getType(), 1));
413 Value *Rem = Builder.CreateAnd(A, Mask, Op1->getName());
414 return replaceOperand(I, 1, Rem);
415 }
416
417 if (Instruction *Logic = foldShiftOfShiftedLogic(I, Builder))
418 return Logic;
419
420 return nullptr;
421}
422
423/// Return true if we can simplify two logical (either left or right) shifts
424/// that have constant shift amounts: OuterShift (InnerShift X, C1), C2.
425static bool canEvaluateShiftedShift(unsigned OuterShAmt, bool IsOuterShl,
426 Instruction *InnerShift,
427 InstCombinerImpl &IC, Instruction *CxtI) {
428 assert(InnerShift->isLogicalShift() && "Unexpected instruction type")((InnerShift->isLogicalShift() && "Unexpected instruction type"
) ? static_cast<void> (0) : __assert_fail ("InnerShift->isLogicalShift() && \"Unexpected instruction type\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 428, __PRETTY_FUNCTION__))
;
429
430 // We need constant scalar or constant splat shifts.
431 const APInt *InnerShiftConst;
432 if (!match(InnerShift->getOperand(1), m_APInt(InnerShiftConst)))
433 return false;
434
435 // Two logical shifts in the same direction:
436 // shl (shl X, C1), C2 --> shl X, C1 + C2
437 // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
438 bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
439 if (IsInnerShl == IsOuterShl)
440 return true;
441
442 // Equal shift amounts in opposite directions become bitwise 'and':
443 // lshr (shl X, C), C --> and X, C'
444 // shl (lshr X, C), C --> and X, C'
445 if (*InnerShiftConst == OuterShAmt)
446 return true;
447
448 // If the 2nd shift is bigger than the 1st, we can fold:
449 // lshr (shl X, C1), C2 --> and (shl X, C1 - C2), C3
450 // shl (lshr X, C1), C2 --> and (lshr X, C1 - C2), C3
451 // but it isn't profitable unless we know the and'd out bits are already zero.
452 // Also, check that the inner shift is valid (less than the type width) or
453 // we'll crash trying to produce the bit mask for the 'and'.
454 unsigned TypeWidth = InnerShift->getType()->getScalarSizeInBits();
455 if (InnerShiftConst->ugt(OuterShAmt) && InnerShiftConst->ult(TypeWidth)) {
456 unsigned InnerShAmt = InnerShiftConst->getZExtValue();
457 unsigned MaskShift =
458 IsInnerShl ? TypeWidth - InnerShAmt : InnerShAmt - OuterShAmt;
459 APInt Mask = APInt::getLowBitsSet(TypeWidth, OuterShAmt) << MaskShift;
460 if (IC.MaskedValueIsZero(InnerShift->getOperand(0), Mask, 0, CxtI))
461 return true;
462 }
463
464 return false;
465}
466
467/// See if we can compute the specified value, but shifted logically to the left
468/// or right by some number of bits. This should return true if the expression
469/// can be computed for the same cost as the current expression tree. This is
470/// used to eliminate extraneous shifting from things like:
471/// %C = shl i128 %A, 64
472/// %D = shl i128 %B, 96
473/// %E = or i128 %C, %D
474/// %F = lshr i128 %E, 64
475/// where the client will ask if E can be computed shifted right by 64-bits. If
476/// this succeeds, getShiftedValue() will be called to produce the value.
477static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift,
478 InstCombinerImpl &IC, Instruction *CxtI) {
479 // We can always evaluate constants shifted.
480 if (isa<Constant>(V))
481 return true;
482
483 Instruction *I = dyn_cast<Instruction>(V);
484 if (!I) return false;
485
486 // We can't mutate something that has multiple uses: doing so would
487 // require duplicating the instruction in general, which isn't profitable.
488 if (!I->hasOneUse()) return false;
489
490 switch (I->getOpcode()) {
491 default: return false;
492 case Instruction::And:
493 case Instruction::Or:
494 case Instruction::Xor:
495 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
496 return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) &&
497 canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I);
498
499 case Instruction::Shl:
500 case Instruction::LShr:
501 return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI);
502
503 case Instruction::Select: {
504 SelectInst *SI = cast<SelectInst>(I);
505 Value *TrueVal = SI->getTrueValue();
506 Value *FalseVal = SI->getFalseValue();
507 return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) &&
508 canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI);
509 }
510 case Instruction::PHI: {
511 // We can change a phi if we can change all operands. Note that we never
512 // get into trouble with cyclic PHIs here because we only consider
513 // instructions with a single use.
514 PHINode *PN = cast<PHINode>(I);
515 for (Value *IncValue : PN->incoming_values())
516 if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN))
517 return false;
518 return true;
519 }
520 }
521}
522
523/// Fold OuterShift (InnerShift X, C1), C2.
524/// See canEvaluateShiftedShift() for the constraints on these instructions.
525static Value *foldShiftedShift(BinaryOperator *InnerShift, unsigned OuterShAmt,
526 bool IsOuterShl,
527 InstCombiner::BuilderTy &Builder) {
528 bool IsInnerShl = InnerShift->getOpcode() == Instruction::Shl;
529 Type *ShType = InnerShift->getType();
530 unsigned TypeWidth = ShType->getScalarSizeInBits();
531
532 // We only accept shifts-by-a-constant in canEvaluateShifted().
533 const APInt *C1;
534 match(InnerShift->getOperand(1), m_APInt(C1));
535 unsigned InnerShAmt = C1->getZExtValue();
536
537 // Change the shift amount and clear the appropriate IR flags.
538 auto NewInnerShift = [&](unsigned ShAmt) {
539 InnerShift->setOperand(1, ConstantInt::get(ShType, ShAmt));
540 if (IsInnerShl) {
541 InnerShift->setHasNoUnsignedWrap(false);
542 InnerShift->setHasNoSignedWrap(false);
543 } else {
544 InnerShift->setIsExact(false);
545 }
546 return InnerShift;
547 };
548
549 // Two logical shifts in the same direction:
550 // shl (shl X, C1), C2 --> shl X, C1 + C2
551 // lshr (lshr X, C1), C2 --> lshr X, C1 + C2
552 if (IsInnerShl == IsOuterShl) {
553 // If this is an oversized composite shift, then unsigned shifts get 0.
554 if (InnerShAmt + OuterShAmt >= TypeWidth)
555 return Constant::getNullValue(ShType);
556
557 return NewInnerShift(InnerShAmt + OuterShAmt);
558 }
559
560 // Equal shift amounts in opposite directions become bitwise 'and':
561 // lshr (shl X, C), C --> and X, C'
562 // shl (lshr X, C), C --> and X, C'
563 if (InnerShAmt == OuterShAmt) {
564 APInt Mask = IsInnerShl
565 ? APInt::getLowBitsSet(TypeWidth, TypeWidth - OuterShAmt)
566 : APInt::getHighBitsSet(TypeWidth, TypeWidth - OuterShAmt);
567 Value *And = Builder.CreateAnd(InnerShift->getOperand(0),
568 ConstantInt::get(ShType, Mask));
569 if (auto *AndI = dyn_cast<Instruction>(And)) {
570 AndI->moveBefore(InnerShift);
571 AndI->takeName(InnerShift);
572 }
573 return And;
574 }
575
576 assert(InnerShAmt > OuterShAmt &&((InnerShAmt > OuterShAmt && "Unexpected opposite direction logical shift pair"
) ? static_cast<void> (0) : __assert_fail ("InnerShAmt > OuterShAmt && \"Unexpected opposite direction logical shift pair\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 577, __PRETTY_FUNCTION__))
577 "Unexpected opposite direction logical shift pair")((InnerShAmt > OuterShAmt && "Unexpected opposite direction logical shift pair"
) ? static_cast<void> (0) : __assert_fail ("InnerShAmt > OuterShAmt && \"Unexpected opposite direction logical shift pair\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 577, __PRETTY_FUNCTION__))
;
578
579 // In general, we would need an 'and' for this transform, but
580 // canEvaluateShiftedShift() guarantees that the masked-off bits are not used.
581 // lshr (shl X, C1), C2 --> shl X, C1 - C2
582 // shl (lshr X, C1), C2 --> lshr X, C1 - C2
583 return NewInnerShift(InnerShAmt - OuterShAmt);
584}
585
586/// When canEvaluateShifted() returns true for an expression, this function
587/// inserts the new computation that produces the shifted value.
588static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift,
589 InstCombinerImpl &IC, const DataLayout &DL) {
590 // We can always evaluate constants shifted.
591 if (Constant *C = dyn_cast<Constant>(V)) {
592 if (isLeftShift)
593 return IC.Builder.CreateShl(C, NumBits);
594 else
595 return IC.Builder.CreateLShr(C, NumBits);
596 }
597
598 Instruction *I = cast<Instruction>(V);
599 IC.addToWorklist(I);
600
601 switch (I->getOpcode()) {
602 default: llvm_unreachable("Inconsistency with CanEvaluateShifted")::llvm::llvm_unreachable_internal("Inconsistency with CanEvaluateShifted"
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 602)
;
603 case Instruction::And:
604 case Instruction::Or:
605 case Instruction::Xor:
606 // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted.
607 I->setOperand(
608 0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL));
609 I->setOperand(
610 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
611 return I;
612
613 case Instruction::Shl:
614 case Instruction::LShr:
615 return foldShiftedShift(cast<BinaryOperator>(I), NumBits, isLeftShift,
616 IC.Builder);
617
618 case Instruction::Select:
619 I->setOperand(
620 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL));
621 I->setOperand(
622 2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL));
623 return I;
624 case Instruction::PHI: {
625 // We can change a phi if we can change all operands. Note that we never
626 // get into trouble with cyclic PHIs here because we only consider
627 // instructions with a single use.
628 PHINode *PN = cast<PHINode>(I);
629 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
630 PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits,
631 isLeftShift, IC, DL));
632 return PN;
633 }
634 }
635}
636
637// If this is a bitwise operator or add with a constant RHS we might be able
638// to pull it through a shift.
639static bool canShiftBinOpWithConstantRHS(BinaryOperator &Shift,
640 BinaryOperator *BO) {
641 switch (BO->getOpcode()) {
642 default:
643 return false; // Do not perform transform!
644 case Instruction::Add:
645 return Shift.getOpcode() == Instruction::Shl;
646 case Instruction::Or:
647 case Instruction::And:
648 return true;
649 case Instruction::Xor:
650 // Do not change a 'not' of logical shift because that would create a normal
651 // 'xor'. The 'not' is likely better for analysis, SCEV, and codegen.
652 return !(Shift.isLogicalShift() && match(BO, m_Not(m_Value())));
653 }
654}
655
656Instruction *InstCombinerImpl::FoldShiftByConstant(Value *Op0, Constant *Op1,
657 BinaryOperator &I) {
658 bool isLeftShift = I.getOpcode() == Instruction::Shl;
659
660 const APInt *Op1C;
661 if (!match(Op1, m_APInt(Op1C)))
662 return nullptr;
663
664 // See if we can propagate this shift into the input, this covers the trivial
665 // cast of lshr(shl(x,c1),c2) as well as other more complex cases.
666 if (I.getOpcode() != Instruction::AShr &&
667 canEvaluateShifted(Op0, Op1C->getZExtValue(), isLeftShift, *this, &I)) {
668 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)
669 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)
670 " 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)
671 << *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)
;
672
673 return replaceInstUsesWith(
674 I, getShiftedValue(Op0, Op1C->getZExtValue(), isLeftShift, *this, DL));
675 }
676
677 // See if we can simplify any instructions used by the instruction whose sole
678 // purpose is to compute bits we don't care about.
679 Type *Ty = I.getType();
680 unsigned TypeBits = Ty->getScalarSizeInBits();
681 assert(!Op1C->uge(TypeBits) &&((!Op1C->uge(TypeBits) && "Shift over the type width should have been removed already"
) ? static_cast<void> (0) : __assert_fail ("!Op1C->uge(TypeBits) && \"Shift over the type width should have been removed already\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 682, __PRETTY_FUNCTION__))
682 "Shift over the type width should have been removed already")((!Op1C->uge(TypeBits) && "Shift over the type width should have been removed already"
) ? static_cast<void> (0) : __assert_fail ("!Op1C->uge(TypeBits) && \"Shift over the type width should have been removed already\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 682, __PRETTY_FUNCTION__))
;
683
684 if (Instruction *FoldedShift = foldBinOpIntoSelectOrPhi(I))
685 return FoldedShift;
686
687 // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2))
688 if (auto *TI = dyn_cast<TruncInst>(Op0)) {
689 // If 'shift2' is an ashr, we would have to get the sign bit into a funny
690 // place. Don't try to do this transformation in this case. Also, we
691 // require that the input operand is a shift-by-constant so that we have
692 // confidence that the shifts will get folded together. We could do this
693 // xform in more cases, but it is unlikely to be profitable.
694 const APInt *TrShiftAmt;
695 if (I.isLogicalShift() &&
696 match(TI->getOperand(0), m_Shift(m_Value(), m_APInt(TrShiftAmt)))) {
697 auto *TrOp = cast<Instruction>(TI->getOperand(0));
698 Type *SrcTy = TrOp->getType();
699
700 // Okay, we'll do this xform. Make the shift of shift.
701 Constant *ShAmt = ConstantExpr::getZExt(Op1, SrcTy);
702 // (shift2 (shift1 & 0x00FF), c2)
703 Value *NSh = Builder.CreateBinOp(I.getOpcode(), TrOp, ShAmt, I.getName());
704
705 // For logical shifts, the truncation has the effect of making the high
706 // part of the register be zeros. Emulate this by inserting an AND to
707 // clear the top bits as needed. This 'and' will usually be zapped by
708 // other xforms later if dead.
709 unsigned SrcSize = SrcTy->getScalarSizeInBits();
710 Constant *MaskV =
711 ConstantInt::get(SrcTy, APInt::getLowBitsSet(SrcSize, TypeBits));
712
713 // The mask we constructed says what the trunc would do if occurring
714 // between the shifts. We want to know the effect *after* the second
715 // shift. We know that it is a logical shift by a constant, so adjust the
716 // mask as appropriate.
717 MaskV = ConstantExpr::get(I.getOpcode(), MaskV, ShAmt);
718 // shift1 & 0x00FF
719 Value *And = Builder.CreateAnd(NSh, MaskV, TI->getName());
720 // Return the value truncated to the interesting size.
721 return new TruncInst(And, Ty);
722 }
723 }
724
725 if (Op0->hasOneUse()) {
726 if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) {
727 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
728 Value *V1;
729 const APInt *CC;
730 switch (Op0BO->getOpcode()) {
731 default: break;
732 case Instruction::Add:
733 case Instruction::And:
734 case Instruction::Or:
735 case Instruction::Xor: {
736 // These operators commute.
737 // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C)
738 if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() &&
739 match(Op0BO->getOperand(1), m_Shr(m_Value(V1),
740 m_Specific(Op1)))) {
741 Value *YS = // (Y << C)
742 Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
743 // (X + (Y << C))
744 Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), YS, V1,
745 Op0BO->getOperand(1)->getName());
746 unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
747 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
748 Constant *Mask = ConstantInt::get(Ty, Bits);
749 return BinaryOperator::CreateAnd(X, Mask);
750 }
751
752 // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C))
753 Value *Op0BOOp1 = Op0BO->getOperand(1);
754 if (isLeftShift && Op0BOOp1->hasOneUse() &&
755 match(Op0BOOp1, m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
756 m_APInt(CC)))) {
757 Value *YS = // (Y << C)
758 Builder.CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName());
759 // X & (CC << C)
760 Value *XM = Builder.CreateAnd(
761 V1, ConstantExpr::getShl(ConstantInt::get(Ty, *CC), Op1),
762 V1->getName() + ".mask");
763 return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM);
764 }
765 LLVM_FALLTHROUGH[[gnu::fallthrough]];
766 }
767
768 case Instruction::Sub: {
769 // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C)
770 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
771 match(Op0BO->getOperand(0), m_Shr(m_Value(V1),
772 m_Specific(Op1)))) {
773 Value *YS = // (Y << C)
774 Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
775 // (X + (Y << C))
776 Value *X = Builder.CreateBinOp(Op0BO->getOpcode(), V1, YS,
777 Op0BO->getOperand(0)->getName());
778 unsigned Op1Val = Op1C->getLimitedValue(TypeBits);
779 APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val);
780 Constant *Mask = ConstantInt::get(Ty, Bits);
781 return BinaryOperator::CreateAnd(X, Mask);
782 }
783
784 // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C)
785 if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() &&
786 match(Op0BO->getOperand(0),
787 m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))),
788 m_APInt(CC)))) {
789 Value *YS = // (Y << C)
790 Builder.CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName());
791 // X & (CC << C)
792 Value *XM = Builder.CreateAnd(
793 V1, ConstantExpr::getShl(ConstantInt::get(Ty, *CC), Op1),
794 V1->getName() + ".mask");
795 return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS);
796 }
797
798 break;
799 }
800 }
801
802 // If the operand is a bitwise operator with a constant RHS, and the
803 // shift is the only use, we can pull it out of the shift.
804 const APInt *Op0C;
805 if (match(Op0BO->getOperand(1), m_APInt(Op0C))) {
806 if (canShiftBinOpWithConstantRHS(I, Op0BO)) {
807 Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
808 cast<Constant>(Op0BO->getOperand(1)), Op1);
809
810 Value *NewShift =
811 Builder.CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1);
812 NewShift->takeName(Op0BO);
813
814 return BinaryOperator::Create(Op0BO->getOpcode(), NewShift,
815 NewRHS);
816 }
817 }
818
819 // If the operand is a subtract with a constant LHS, and the shift
820 // is the only use, we can pull it out of the shift.
821 // This folds (shl (sub C1, X), C2) -> (sub (C1 << C2), (shl X, C2))
822 if (isLeftShift && Op0BO->getOpcode() == Instruction::Sub &&
823 match(Op0BO->getOperand(0), m_APInt(Op0C))) {
824 Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
825 cast<Constant>(Op0BO->getOperand(0)), Op1);
826
827 Value *NewShift = Builder.CreateShl(Op0BO->getOperand(1), Op1);
828 NewShift->takeName(Op0BO);
829
830 return BinaryOperator::CreateSub(NewRHS, NewShift);
831 }
832 }
833
834 // If we have a select that conditionally executes some binary operator,
835 // see if we can pull it the select and operator through the shift.
836 //
837 // For example, turning:
838 // shl (select C, (add X, C1), X), C2
839 // Into:
840 // Y = shl X, C2
841 // select C, (add Y, C1 << C2), Y
842 Value *Cond;
843 BinaryOperator *TBO;
844 Value *FalseVal;
845 if (match(Op0, m_Select(m_Value(Cond), m_OneUse(m_BinOp(TBO)),
846 m_Value(FalseVal)))) {
847 const APInt *C;
848 if (!isa<Constant>(FalseVal) && TBO->getOperand(0) == FalseVal &&
849 match(TBO->getOperand(1), m_APInt(C)) &&
850 canShiftBinOpWithConstantRHS(I, TBO)) {
851 Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
852 cast<Constant>(TBO->getOperand(1)), Op1);
853
854 Value *NewShift =
855 Builder.CreateBinOp(I.getOpcode(), FalseVal, Op1);
856 Value *NewOp = Builder.CreateBinOp(TBO->getOpcode(), NewShift,
857 NewRHS);
858 return SelectInst::Create(Cond, NewOp, NewShift);
859 }
860 }
861
862 BinaryOperator *FBO;
863 Value *TrueVal;
864 if (match(Op0, m_Select(m_Value(Cond), m_Value(TrueVal),
865 m_OneUse(m_BinOp(FBO))))) {
866 const APInt *C;
867 if (!isa<Constant>(TrueVal) && FBO->getOperand(0) == TrueVal &&
868 match(FBO->getOperand(1), m_APInt(C)) &&
869 canShiftBinOpWithConstantRHS(I, FBO)) {
870 Constant *NewRHS = ConstantExpr::get(I.getOpcode(),
871 cast<Constant>(FBO->getOperand(1)), Op1);
872
873 Value *NewShift =
874 Builder.CreateBinOp(I.getOpcode(), TrueVal, Op1);
875 Value *NewOp = Builder.CreateBinOp(FBO->getOpcode(), NewShift,
876 NewRHS);
877 return SelectInst::Create(Cond, NewShift, NewOp);
878 }
879 }
880 }
881
882 return nullptr;
883}
884
885Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
886 const SimplifyQuery Q = SQ.getWithInstruction(&I);
887
888 if (Value *V = SimplifyShlInst(I.getOperand(0), I.getOperand(1),
1
Assuming 'V' is null
2
Taking false branch
889 I.hasNoSignedWrap(), I.hasNoUnsignedWrap(), Q))
890 return replaceInstUsesWith(I, V);
891
892 if (Instruction *X = foldVectorBinop(I))
3
Assuming 'X' is null
4
Taking false branch
893 return X;
894
895 if (Instruction *V = commonShiftTransforms(I))
5
Assuming 'V' is null
6
Taking false branch
896 return V;
897
898 if (Instruction *V = dropRedundantMaskingOfLeftShiftInput(&I, Q, Builder))
7
Calling 'dropRedundantMaskingOfLeftShiftInput'
899 return V;
900
901 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
902 Type *Ty = I.getType();
903 unsigned BitWidth = Ty->getScalarSizeInBits();
904
905 const APInt *ShAmtAPInt;
906 if (match(Op1, m_APInt(ShAmtAPInt))) {
907 unsigned ShAmt = ShAmtAPInt->getZExtValue();
908
909 // shl (zext X), ShAmt --> zext (shl X, ShAmt)
910 // This is only valid if X would have zeros shifted out.
911 Value *X;
912 if (match(Op0, m_OneUse(m_ZExt(m_Value(X))))) {
913 unsigned SrcWidth = X->getType()->getScalarSizeInBits();
914 if (ShAmt < SrcWidth &&
915 MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I))
916 return new ZExtInst(Builder.CreateShl(X, ShAmt), Ty);
917 }
918
919 // (X >> C) << C --> X & (-1 << C)
920 if (match(Op0, m_Shr(m_Value(X), m_Specific(Op1)))) {
921 APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt));
922 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
923 }
924
925 const APInt *ShOp1;
926 if (match(Op0, m_Exact(m_Shr(m_Value(X), m_APInt(ShOp1)))) &&
927 ShOp1->ult(BitWidth)) {
928 unsigned ShrAmt = ShOp1->getZExtValue();
929 if (ShrAmt < ShAmt) {
930 // If C1 < C2: (X >>?,exact C1) << C2 --> X << (C2 - C1)
931 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShrAmt);
932 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
933 NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
934 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
935 return NewShl;
936 }
937 if (ShrAmt > ShAmt) {
938 // If C1 > C2: (X >>?exact C1) << C2 --> X >>?exact (C1 - C2)
939 Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmt);
940 auto *NewShr = BinaryOperator::Create(
941 cast<BinaryOperator>(Op0)->getOpcode(), X, ShiftDiff);
942 NewShr->setIsExact(true);
943 return NewShr;
944 }
945 }
946
947 if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_APInt(ShOp1)))) &&
948 ShOp1->ult(BitWidth)) {
949 unsigned ShrAmt = ShOp1->getZExtValue();
950 if (ShrAmt < ShAmt) {
951 // If C1 < C2: (X >>? C1) << C2 --> X << (C2 - C1) & (-1 << C2)
952 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShrAmt);
953 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
954 NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap());
955 NewShl->setHasNoSignedWrap(I.hasNoSignedWrap());
956 Builder.Insert(NewShl);
957 APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt));
958 return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
959 }
960 if (ShrAmt > ShAmt) {
961 // If C1 > C2: (X >>? C1) << C2 --> X >>? (C1 - C2) & (-1 << C2)
962 Constant *ShiftDiff = ConstantInt::get(Ty, ShrAmt - ShAmt);
963 auto *OldShr = cast<BinaryOperator>(Op0);
964 auto *NewShr =
965 BinaryOperator::Create(OldShr->getOpcode(), X, ShiftDiff);
966 NewShr->setIsExact(OldShr->isExact());
967 Builder.Insert(NewShr);
968 APInt Mask(APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt));
969 return BinaryOperator::CreateAnd(NewShr, ConstantInt::get(Ty, Mask));
970 }
971 }
972
973 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1))) && ShOp1->ult(BitWidth)) {
974 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
975 // Oversized shifts are simplified to zero in InstSimplify.
976 if (AmtSum < BitWidth)
977 // (X << C1) << C2 --> X << (C1 + C2)
978 return BinaryOperator::CreateShl(X, ConstantInt::get(Ty, AmtSum));
979 }
980
981 // If the shifted-out value is known-zero, then this is a NUW shift.
982 if (!I.hasNoUnsignedWrap() &&
983 MaskedValueIsZero(Op0, APInt::getHighBitsSet(BitWidth, ShAmt), 0, &I)) {
984 I.setHasNoUnsignedWrap();
985 return &I;
986 }
987
988 // If the shifted-out value is all signbits, then this is a NSW shift.
989 if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) {
990 I.setHasNoSignedWrap();
991 return &I;
992 }
993 }
994
995 // Transform (x >> y) << y to x & (-1 << y)
996 // Valid for any type of right-shift.
997 Value *X;
998 if (match(Op0, m_OneUse(m_Shr(m_Value(X), m_Specific(Op1))))) {
999 Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
1000 Value *Mask = Builder.CreateShl(AllOnes, Op1);
1001 return BinaryOperator::CreateAnd(Mask, X);
1002 }
1003
1004 Constant *C1;
1005 if (match(Op1, m_Constant(C1))) {
1006 Constant *C2;
1007 Value *X;
1008 // (C2 << X) << C1 --> (C2 << C1) << X
1009 if (match(Op0, m_OneUse(m_Shl(m_Constant(C2), m_Value(X)))))
1010 return BinaryOperator::CreateShl(ConstantExpr::getShl(C2, C1), X);
1011
1012 // (X * C2) << C1 --> X * (C2 << C1)
1013 if (match(Op0, m_Mul(m_Value(X), m_Constant(C2))))
1014 return BinaryOperator::CreateMul(X, ConstantExpr::getShl(C2, C1));
1015
1016 // shl (zext i1 X), C1 --> select (X, 1 << C1, 0)
1017 if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
1018 auto *NewC = ConstantExpr::getShl(ConstantInt::get(Ty, 1), C1);
1019 return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
1020 }
1021 }
1022
1023 // (1 << (C - x)) -> ((1 << C) >> x) if C is bitwidth - 1
1024 if (match(Op0, m_One()) &&
1025 match(Op1, m_Sub(m_SpecificInt(BitWidth - 1), m_Value(X))))
1026 return BinaryOperator::CreateLShr(
1027 ConstantInt::get(Ty, APInt::getSignMask(BitWidth)), X);
1028
1029 return nullptr;
1030}
1031
1032Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
1033 if (Value *V = SimplifyLShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
1034 SQ.getWithInstruction(&I)))
1035 return replaceInstUsesWith(I, V);
1036
1037 if (Instruction *X = foldVectorBinop(I))
1038 return X;
1039
1040 if (Instruction *R = commonShiftTransforms(I))
1041 return R;
1042
1043 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1044 Type *Ty = I.getType();
1045 const APInt *ShAmtAPInt;
1046 if (match(Op1, m_APInt(ShAmtAPInt))) {
1047 unsigned ShAmt = ShAmtAPInt->getZExtValue();
1048 unsigned BitWidth = Ty->getScalarSizeInBits();
1049 auto *II = dyn_cast<IntrinsicInst>(Op0);
1050 if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt &&
1051 (II->getIntrinsicID() == Intrinsic::ctlz ||
1052 II->getIntrinsicID() == Intrinsic::cttz ||
1053 II->getIntrinsicID() == Intrinsic::ctpop)) {
1054 // ctlz.i32(x)>>5 --> zext(x == 0)
1055 // cttz.i32(x)>>5 --> zext(x == 0)
1056 // ctpop.i32(x)>>5 --> zext(x == -1)
1057 bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop;
1058 Constant *RHS = ConstantInt::getSigned(Ty, IsPop ? -1 : 0);
1059 Value *Cmp = Builder.CreateICmpEQ(II->getArgOperand(0), RHS);
1060 return new ZExtInst(Cmp, Ty);
1061 }
1062
1063 Value *X;
1064 const APInt *ShOp1;
1065 if (match(Op0, m_Shl(m_Value(X), m_APInt(ShOp1))) && ShOp1->ult(BitWidth)) {
1066 if (ShOp1->ult(ShAmt)) {
1067 unsigned ShlAmt = ShOp1->getZExtValue();
1068 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
1069 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
1070 // (X <<nuw C1) >>u C2 --> X >>u (C2 - C1)
1071 auto *NewLShr = BinaryOperator::CreateLShr(X, ShiftDiff);
1072 NewLShr->setIsExact(I.isExact());
1073 return NewLShr;
1074 }
1075 // (X << C1) >>u C2 --> (X >>u (C2 - C1)) & (-1 >> C2)
1076 Value *NewLShr = Builder.CreateLShr(X, ShiftDiff, "", I.isExact());
1077 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
1078 return BinaryOperator::CreateAnd(NewLShr, ConstantInt::get(Ty, Mask));
1079 }
1080 if (ShOp1->ugt(ShAmt)) {
1081 unsigned ShlAmt = ShOp1->getZExtValue();
1082 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
1083 if (cast<BinaryOperator>(Op0)->hasNoUnsignedWrap()) {
1084 // (X <<nuw C1) >>u C2 --> X <<nuw (C1 - C2)
1085 auto *NewShl = BinaryOperator::CreateShl(X, ShiftDiff);
1086 NewShl->setHasNoUnsignedWrap(true);
1087 return NewShl;
1088 }
1089 // (X << C1) >>u C2 --> X << (C1 - C2) & (-1 >> C2)
1090 Value *NewShl = Builder.CreateShl(X, ShiftDiff);
1091 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
1092 return BinaryOperator::CreateAnd(NewShl, ConstantInt::get(Ty, Mask));
1093 }
1094 assert(*ShOp1 == ShAmt)((*ShOp1 == ShAmt) ? static_cast<void> (0) : __assert_fail
("*ShOp1 == ShAmt", "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 1094, __PRETTY_FUNCTION__))
;
1095 // (X << C) >>u C --> X & (-1 >>u C)
1096 APInt Mask(APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt));
1097 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, Mask));
1098 }
1099
1100 if (match(Op0, m_OneUse(m_ZExt(m_Value(X)))) &&
1101 (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
1102 assert(ShAmt < X->getType()->getScalarSizeInBits() &&((ShAmt < X->getType()->getScalarSizeInBits() &&
"Big shift not simplified to zero?") ? static_cast<void>
(0) : __assert_fail ("ShAmt < X->getType()->getScalarSizeInBits() && \"Big shift not simplified to zero?\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 1103, __PRETTY_FUNCTION__))
1103 "Big shift not simplified to zero?")((ShAmt < X->getType()->getScalarSizeInBits() &&
"Big shift not simplified to zero?") ? static_cast<void>
(0) : __assert_fail ("ShAmt < X->getType()->getScalarSizeInBits() && \"Big shift not simplified to zero?\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 1103, __PRETTY_FUNCTION__))
;
1104 // lshr (zext iM X to iN), C --> zext (lshr X, C) to iN
1105 Value *NewLShr = Builder.CreateLShr(X, ShAmt);
1106 return new ZExtInst(NewLShr, Ty);
1107 }
1108
1109 if (match(Op0, m_SExt(m_Value(X))) &&
1110 (!Ty->isIntegerTy() || shouldChangeType(Ty, X->getType()))) {
1111 // Are we moving the sign bit to the low bit and widening with high zeros?
1112 unsigned SrcTyBitWidth = X->getType()->getScalarSizeInBits();
1113 if (ShAmt == BitWidth - 1) {
1114 // lshr (sext i1 X to iN), N-1 --> zext X to iN
1115 if (SrcTyBitWidth == 1)
1116 return new ZExtInst(X, Ty);
1117
1118 // lshr (sext iM X to iN), N-1 --> zext (lshr X, M-1) to iN
1119 if (Op0->hasOneUse()) {
1120 Value *NewLShr = Builder.CreateLShr(X, SrcTyBitWidth - 1);
1121 return new ZExtInst(NewLShr, Ty);
1122 }
1123 }
1124
1125 // lshr (sext iM X to iN), N-M --> zext (ashr X, min(N-M, M-1)) to iN
1126 if (ShAmt == BitWidth - SrcTyBitWidth && Op0->hasOneUse()) {
1127 // The new shift amount can't be more than the narrow source type.
1128 unsigned NewShAmt = std::min(ShAmt, SrcTyBitWidth - 1);
1129 Value *AShr = Builder.CreateAShr(X, NewShAmt);
1130 return new ZExtInst(AShr, Ty);
1131 }
1132 }
1133
1134 // lshr i32 (X -nsw Y), 31 --> zext (X < Y)
1135 Value *Y;
1136 if (ShAmt == BitWidth - 1 &&
1137 match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
1138 return new ZExtInst(Builder.CreateICmpSLT(X, Y), Ty);
1139
1140 if (match(Op0, m_LShr(m_Value(X), m_APInt(ShOp1)))) {
1141 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
1142 // Oversized shifts are simplified to zero in InstSimplify.
1143 if (AmtSum < BitWidth)
1144 // (X >>u C1) >>u C2 --> X >>u (C1 + C2)
1145 return BinaryOperator::CreateLShr(X, ConstantInt::get(Ty, AmtSum));
1146 }
1147
1148 // Look for a "splat" mul pattern - it replicates bits across each half of
1149 // a value, so a right shift is just a mask of the low bits:
1150 // lshr i32 (mul nuw X, Pow2+1), 16 --> and X, Pow2-1
1151 // TODO: Generalize to allow more than just half-width shifts?
1152 const APInt *MulC;
1153 if (match(Op0, m_NUWMul(m_Value(X), m_APInt(MulC))) &&
1154 ShAmt * 2 == BitWidth && (*MulC - 1).isPowerOf2() &&
1155 MulC->logBase2() == ShAmt)
1156 return BinaryOperator::CreateAnd(X, ConstantInt::get(Ty, *MulC - 2));
1157
1158 // If the shifted-out value is known-zero, then this is an exact shift.
1159 if (!I.isExact() &&
1160 MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
1161 I.setIsExact();
1162 return &I;
1163 }
1164 }
1165
1166 // Transform (x << y) >> y to x & (-1 >> y)
1167 Value *X;
1168 if (match(Op0, m_OneUse(m_Shl(m_Value(X), m_Specific(Op1))))) {
1169 Constant *AllOnes = ConstantInt::getAllOnesValue(Ty);
1170 Value *Mask = Builder.CreateLShr(AllOnes, Op1);
1171 return BinaryOperator::CreateAnd(Mask, X);
1172 }
1173
1174 return nullptr;
1175}
1176
1177Instruction *
1178InstCombinerImpl::foldVariableSignZeroExtensionOfVariableHighBitExtract(
1179 BinaryOperator &OldAShr) {
1180 assert(OldAShr.getOpcode() == Instruction::AShr &&((OldAShr.getOpcode() == Instruction::AShr && "Must be called with arithmetic right-shift instruction only."
) ? static_cast<void> (0) : __assert_fail ("OldAShr.getOpcode() == Instruction::AShr && \"Must be called with arithmetic right-shift instruction only.\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 1181, __PRETTY_FUNCTION__))
1181 "Must be called with arithmetic right-shift instruction only.")((OldAShr.getOpcode() == Instruction::AShr && "Must be called with arithmetic right-shift instruction only."
) ? static_cast<void> (0) : __assert_fail ("OldAShr.getOpcode() == Instruction::AShr && \"Must be called with arithmetic right-shift instruction only.\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp"
, 1181, __PRETTY_FUNCTION__))
;
1182
1183 // Check that constant C is a splat of the element-wise bitwidth of V.
1184 auto BitWidthSplat = [](Constant *C, Value *V) {
1185 return match(
1186 C, m_SpecificInt_ICMP(ICmpInst::Predicate::ICMP_EQ,
1187 APInt(C->getType()->getScalarSizeInBits(),
1188 V->getType()->getScalarSizeInBits())));
1189 };
1190
1191 // It should look like variable-length sign-extension on the outside:
1192 // (Val << (bitwidth(Val)-Nbits)) a>> (bitwidth(Val)-Nbits)
1193 Value *NBits;
1194 Instruction *MaybeTrunc;
1195 Constant *C1, *C2;
1196 if (!match(&OldAShr,
1197 m_AShr(m_Shl(m_Instruction(MaybeTrunc),
1198 m_ZExtOrSelf(m_Sub(m_Constant(C1),
1199 m_ZExtOrSelf(m_Value(NBits))))),
1200 m_ZExtOrSelf(m_Sub(m_Constant(C2),
1201 m_ZExtOrSelf(m_Deferred(NBits)))))) ||
1202 !BitWidthSplat(C1, &OldAShr) || !BitWidthSplat(C2, &OldAShr))
1203 return nullptr;
1204
1205 // There may or may not be a truncation after outer two shifts.
1206 Instruction *HighBitExtract;
1207 match(MaybeTrunc, m_TruncOrSelf(m_Instruction(HighBitExtract)));
1208 bool HadTrunc = MaybeTrunc != HighBitExtract;
1209
1210 // And finally, the innermost part of the pattern must be a right-shift.
1211 Value *X, *NumLowBitsToSkip;
1212 if (!match(HighBitExtract, m_Shr(m_Value(X), m_Value(NumLowBitsToSkip))))
1213 return nullptr;
1214
1215 // Said right-shift must extract high NBits bits - C0 must be it's bitwidth.
1216 Constant *C0;
1217 if (!match(NumLowBitsToSkip,
1218 m_ZExtOrSelf(
1219 m_Sub(m_Constant(C0), m_ZExtOrSelf(m_Specific(NBits))))) ||
1220 !BitWidthSplat(C0, HighBitExtract))
1221 return nullptr;
1222
1223 // Since the NBits is identical for all shifts, if the outermost and
1224 // innermost shifts are identical, then outermost shifts are redundant.
1225 // If we had truncation, do keep it though.
1226 if (HighBitExtract->getOpcode() == OldAShr.getOpcode())
1227 return replaceInstUsesWith(OldAShr, MaybeTrunc);
1228
1229 // Else, if there was a truncation, then we need to ensure that one
1230 // instruction will go away.
1231 if (HadTrunc && !match(&OldAShr, m_c_BinOp(m_OneUse(m_Value()), m_Value())))
1232 return nullptr;
1233
1234 // Finally, bypass two innermost shifts, and perform the outermost shift on
1235 // the operands of the innermost shift.
1236 Instruction *NewAShr =
1237 BinaryOperator::Create(OldAShr.getOpcode(), X, NumLowBitsToSkip);
1238 NewAShr->copyIRFlags(HighBitExtract); // We can preserve 'exact'-ness.
1239 if (!HadTrunc)
1240 return NewAShr;
1241
1242 Builder.Insert(NewAShr);
1243 return TruncInst::CreateTruncOrBitCast(NewAShr, OldAShr.getType());
1244}
1245
1246Instruction *InstCombinerImpl::visitAShr(BinaryOperator &I) {
1247 if (Value *V = SimplifyAShrInst(I.getOperand(0), I.getOperand(1), I.isExact(),
1248 SQ.getWithInstruction(&I)))
1249 return replaceInstUsesWith(I, V);
1250
1251 if (Instruction *X = foldVectorBinop(I))
1252 return X;
1253
1254 if (Instruction *R = commonShiftTransforms(I))
1255 return R;
1256
1257 Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1);
1258 Type *Ty = I.getType();
1259 unsigned BitWidth = Ty->getScalarSizeInBits();
1260 const APInt *ShAmtAPInt;
1261 if (match(Op1, m_APInt(ShAmtAPInt)) && ShAmtAPInt->ult(BitWidth)) {
1262 unsigned ShAmt = ShAmtAPInt->getZExtValue();
1263
1264 // If the shift amount equals the difference in width of the destination
1265 // and source scalar types:
1266 // ashr (shl (zext X), C), C --> sext X
1267 Value *X;
1268 if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) &&
1269 ShAmt == BitWidth - X->getType()->getScalarSizeInBits())
1270 return new SExtInst(X, Ty);
1271
1272 // We can't handle (X << C1) >>s C2. It shifts arbitrary bits in. However,
1273 // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits.
1274 const APInt *ShOp1;
1275 if (match(Op0, m_NSWShl(m_Value(X), m_APInt(ShOp1))) &&
1276 ShOp1->ult(BitWidth)) {
1277 unsigned ShlAmt = ShOp1->getZExtValue();
1278 if (ShlAmt < ShAmt) {
1279 // (X <<nsw C1) >>s C2 --> X >>s (C2 - C1)
1280 Constant *ShiftDiff = ConstantInt::get(Ty, ShAmt - ShlAmt);
1281 auto *NewAShr = BinaryOperator::CreateAShr(X, ShiftDiff);
1282 NewAShr->setIsExact(I.isExact());
1283 return NewAShr;
1284 }
1285 if (ShlAmt > ShAmt) {
1286 // (X <<nsw C1) >>s C2 --> X <<nsw (C1 - C2)
1287 Constant *ShiftDiff = ConstantInt::get(Ty, ShlAmt - ShAmt);
1288 auto *NewShl = BinaryOperator::Create(Instruction::Shl, X, ShiftDiff);
1289 NewShl->setHasNoSignedWrap(true);
1290 return NewShl;
1291 }
1292 }
1293
1294 if (match(Op0, m_AShr(m_Value(X), m_APInt(ShOp1))) &&
1295 ShOp1->ult(BitWidth)) {
1296 unsigned AmtSum = ShAmt + ShOp1->getZExtValue();
1297 // Oversized arithmetic shifts replicate the sign bit.
1298 AmtSum = std::min(AmtSum, BitWidth - 1);
1299 // (X >>s C1) >>s C2 --> X >>s (C1 + C2)
1300 return BinaryOperator::CreateAShr(X, ConstantInt::get(Ty, AmtSum));
1301 }
1302
1303 if (match(Op0, m_OneUse(m_SExt(m_Value(X)))) &&
1304 (Ty->isVectorTy() || shouldChangeType(Ty, X->getType()))) {
1305 // ashr (sext X), C --> sext (ashr X, C')
1306 Type *SrcTy = X->getType();
1307 ShAmt = std::min(ShAmt, SrcTy->getScalarSizeInBits() - 1);
1308 Value *NewSh = Builder.CreateAShr(X, ConstantInt::get(SrcTy, ShAmt));
1309 return new SExtInst(NewSh, Ty);
1310 }
1311
1312 // ashr i32 (X -nsw Y), 31 --> sext (X < Y)
1313 Value *Y;
1314 if (ShAmt == BitWidth - 1 &&
1315 match(Op0, m_OneUse(m_NSWSub(m_Value(X), m_Value(Y)))))
1316 return new SExtInst(Builder.CreateICmpSLT(X, Y), Ty);
1317
1318 // If the shifted-out value is known-zero, then this is an exact shift.
1319 if (!I.isExact() &&
1320 MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) {
1321 I.setIsExact();
1322 return &I;
1323 }
1324 }
1325
1326 if (Instruction *R = foldVariableSignZeroExtensionOfVariableHighBitExtract(I))
1327 return R;
1328
1329 // See if we can turn a signed shr into an unsigned shr.
1330 if (MaskedValueIsZero(Op0, APInt::getSignMask(BitWidth), 0, &I))
1331 return BinaryOperator::CreateLShr(Op0, Op1);
1332
1333 // ashr (xor %x, -1), %y --> xor (ashr %x, %y), -1
1334 Value *X;
1335 if (match(Op0, m_OneUse(m_Not(m_Value(X))))) {
1336 // Note that we must drop 'exact'-ness of the shift!
1337 // Note that we can't keep undef's in -1 vector constant!
1338 auto *NewAShr = Builder.CreateAShr(X, Op1, Op0->getName() + ".not");
1339 return BinaryOperator::CreateNot(NewAShr);
1340 }
1341
1342 return nullptr;
1343}

/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/include/llvm/IR/PatternMatch.h

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
46namespace llvm {
47namespace PatternMatch {
48
49template <typename Val, typename Pattern> bool match(Val *V, const Pattern &P) {
50 return const_cast<Pattern &>(P).match(V);
31
Calling 'BinaryOp_match::match'
36
Returning from 'BinaryOp_match::match'
37
Returning the value 1, which participates in a condition later
51}
52
53template <typename Pattern> bool match(ArrayRef<int> Mask, const Pattern &P) {
54 return const_cast<Pattern &>(P).match(Mask);
55}
56
57template <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
67template <typename T> inline OneUse_match<T> m_OneUse(const T &SubPattern) {
68 return SubPattern;
69}
70
71template <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.
76inline class_match<Value> m_Value() { return class_match<Value>(); }
77
78/// Match an arbitrary unary operation and ignore it.
79inline class_match<UnaryOperator> m_UnOp() {
80 return class_match<UnaryOperator>();
81}
82
83/// Match an arbitrary binary operation and ignore it.
84inline class_match<BinaryOperator> m_BinOp() {
85 return class_match<BinaryOperator>();
86}
87
88/// Matches any compare instruction and ignore it.
89inline class_match<CmpInst> m_Cmp() { return class_match<CmpInst>(); }
90
91/// Match an arbitrary undef constant.
92inline class_match<UndefValue> m_Undef() { return class_match<UndefValue>(); }
93
94/// Match an arbitrary poison constant.
95inline class_match<PoisonValue> m_Poison() { return class_match<PoisonValue>(); }
96
97/// Match an arbitrary Constant and ignore it.
98inline class_match<Constant> m_Constant() { return class_match<Constant>(); }
99
100/// Match an arbitrary ConstantInt and ignore it.
101inline class_match<ConstantInt> m_ConstantInt() {
102 return class_match<ConstantInt>();
103}
104
105/// Match an arbitrary ConstantFP and ignore it.
106inline class_match<ConstantFP> m_ConstantFP() {
107 return class_match<ConstantFP>();
108}
109
110/// Match an arbitrary ConstantExpr and ignore it.
111inline class_match<ConstantExpr> m_ConstantExpr() {
112 return class_match<ConstantExpr>();
113}
114
115/// Match an arbitrary basic block value and ignore it.
116inline class_match<BasicBlock> m_BasicBlock() {
117 return class_match<BasicBlock>();
118}
119
120/// Inverting matcher
121template <typename Ty> struct match_unless {
122 Ty M;
123
124 match_unless(const Ty &Matcher) : M(Matcher) {}
125
126 template <typename ITy> bool match(ITy *V) { return !M.match(V); }
127};
128
129/// Match if the inner matcher does *NOT* match.
130template <typename Ty> inline match_unless<Ty> m_Unless(const Ty &M) {
131 return match_unless<Ty>(M);
132}
133
134/// Matching combinators
135template <typename LTy, typename RTy> struct match_combine_or {
136 LTy L;
137 RTy R;
138
139 match_combine_or(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
140
141 template <typename ITy> bool match(ITy *V) {
142 if (L.match(V))
143 return true;
144 if (R.match(V))
145 return true;
146 return false;
147 }
148};
149
150template <typename LTy, typename RTy> struct match_combine_and {
151 LTy L;
152 RTy R;
153
154 match_combine_and(const LTy &Left, const RTy &Right) : L(Left), R(Right) {}
155
156 template <typename ITy> bool match(ITy *V) {
157 if (L.match(V))
158 if (R.match(V))
159 return true;
160 return false;
161 }
162};
163
164/// Combine two pattern matchers matching L || R
165template <typename LTy, typename RTy>
166inline match_combine_or<LTy, RTy> m_CombineOr(const LTy &L, const RTy &R) {
167 return match_combine_or<LTy, RTy>(L, R);
21
Returning without writing to 'L.Op.VR'
168}
169
170/// Combine two pattern matchers matching L && R
171template <typename LTy, typename RTy>
172inline match_combine_and<LTy, RTy> m_CombineAnd(const LTy &L, const RTy &R) {
173 return match_combine_and<LTy, RTy>(L, R);
174}
175
176struct apint_match {
177 const APInt *&Res;
178 bool AllowUndef;
179
180 apint_match(const APInt *&Res, bool AllowUndef)
181 : Res(Res), AllowUndef(AllowUndef) {}
182
183 template <typename ITy> bool match(ITy *V) {
184 if (auto *CI = dyn_cast<ConstantInt>(V)) {
185 Res = &CI->getValue();
186 return true;
187 }
188 if (V->getType()->isVectorTy())
189 if (const auto *C = dyn_cast<Constant>(V))
190 if (auto *CI = dyn_cast_or_null<ConstantInt>(
191 C->getSplatValue(AllowUndef))) {
192 Res = &CI->getValue();
193 return true;
194 }
195 return false;
196 }
197};
198// Either constexpr if or renaming ConstantFP::getValueAPF to
199// ConstantFP::getValue is needed to do it via single template
200// function for both apint/apfloat.
201struct apfloat_match {
202 const APFloat *&Res;
203 bool AllowUndef;
204
205 apfloat_match(const APFloat *&Res, bool AllowUndef)
206 : Res(Res), AllowUndef(AllowUndef) {}
207
208 template <typename ITy> bool match(ITy *V) {
209 if (auto *CI = dyn_cast<ConstantFP>(V)) {
210 Res = &CI->getValueAPF();
211 return true;
212 }
213 if (V->getType()->isVectorTy())
214 if (const auto *C = dyn_cast<Constant>(V))
215 if (auto *CI = dyn_cast_or_null<ConstantFP>(
216 C->getSplatValue(AllowUndef))) {
217 Res = &CI->getValueAPF();
218 return true;
219 }
220 return false;
221 }
222};
223
224/// Match a ConstantInt or splatted ConstantVector, binding the
225/// specified pointer to the contained APInt.
226inline apint_match m_APInt(const APInt *&Res) {
227 // Forbid undefs by default to maintain previous behavior.
228 return apint_match(Res, /* AllowUndef */ false);
229}
230
231/// Match APInt while allowing undefs in splat vector constants.
232inline apint_match m_APIntAllowUndef(const APInt *&Res) {
233 return apint_match(Res, /* AllowUndef */ true);
234}
235
236/// Match APInt while forbidding undefs in splat vector constants.
237inline apint_match m_APIntForbidUndef(const APInt *&Res) {
238 return apint_match(Res, /* AllowUndef */ false);
239}
240
241/// Match a ConstantFP or splatted ConstantVector, binding the
242/// specified pointer to the contained APFloat.
243inline apfloat_match m_APFloat(const APFloat *&Res) {
244 // Forbid undefs by default to maintain previous behavior.
245 return apfloat_match(Res, /* AllowUndef */ false);
246}
247
248/// Match APFloat while allowing undefs in splat vector constants.
249inline apfloat_match m_APFloatAllowUndef(const APFloat *&Res) {
250 return apfloat_match(Res, /* AllowUndef */ true);
251}
252
253/// Match APFloat while forbidding undefs in splat vector constants.
254inline apfloat_match m_APFloatForbidUndef(const APFloat *&Res) {
255 return apfloat_match(Res, /* AllowUndef */ false);
256}
257
258template <int64_t Val> struct constantint_match {
259 template <typename ITy> bool match(ITy *V) {
260 if (const auto *CI = dyn_cast<ConstantInt>(V)) {
261 const APInt &CIV = CI->getValue();
262 if (Val >= 0)
263 return CIV == static_cast<uint64_t>(Val);
264 // If Val is negative, and CI is shorter than it, truncate to the right
265 // number of bits. If it is larger, then we have to sign extend. Just
266 // compare their negated values.
267 return -CIV == -Val;
268 }
269 return false;
270 }
271};
272
273/// Match a ConstantInt with a specific value.
274template <int64_t Val> inline constantint_match<Val> m_ConstantInt() {
275 return constantint_match<Val>();
276}
277
278/// This helper class is used to match constant scalars, vector splats,
279/// and fixed width vectors that satisfy a specified predicate.
280/// For fixed width vector constants, undefined elements are ignored.
281template <typename Predicate, typename ConstantVal>
282struct cstval_pred_ty : public Predicate {
283 template <typename ITy> bool match(ITy *V) {
284 if (const auto *CV = dyn_cast<ConstantVal>(V))
285 return this->isValue(CV->getValue());
286 if (const auto *VTy = dyn_cast<VectorType>(V->getType())) {
287 if (const auto *C = dyn_cast<Constant>(V)) {
288 if (const auto *CV = dyn_cast_or_null<ConstantVal>(C->getSplatValue()))
289 return this->isValue(CV->getValue());
290
291 // Number of elements of a scalable vector unknown at compile time
292 auto *FVTy = dyn_cast<FixedVectorType>(VTy);
293 if (!FVTy)
294 return false;
295
296 // Non-splat vector constant: check each element for a match.
297 unsigned NumElts = FVTy->getNumElements();
298 assert(NumElts != 0 && "Constant vector with no elements?")((NumElts != 0 && "Constant vector with no elements?"
) ? static_cast<void> (0) : __assert_fail ("NumElts != 0 && \"Constant vector with no elements?\""
, "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/include/llvm/IR/PatternMatch.h"
, 298, __PRETTY_FUNCTION__))
;
299 bool HasNonUndefElements = false;
300 for (unsigned i = 0; i != NumElts; ++i) {
301 Constant *Elt = C->getAggregateElement(i);
302 if (!Elt)
303 return false;
304 if (isa<UndefValue>(Elt))
305 continue;
306 auto *CV = dyn_cast<ConstantVal>(Elt);
307 if (!CV || !this->isValue(CV->getValue()))
308 return false;
309 HasNonUndefElements = true;
310 }
311 return HasNonUndefElements;
312 }
313 }
314 return false;
315 }
316};
317
318/// specialization of cstval_pred_ty for ConstantInt
319template <typename Predicate>
320using cst_pred_ty = cstval_pred_ty<Predicate, ConstantInt>;
321
322/// specialization of cstval_pred_ty for ConstantFP
323template <typename Predicate>
324using cstfp_pred_ty = cstval_pred_ty<Predicate, ConstantFP>;
325
326/// This helper class is used to match scalar and vector constants that
327/// satisfy a specified predicate, and bind them to an APInt.
328template <typename Predicate> struct api_pred_ty : public Predicate {
329 const APInt *&Res;
330
331 api_pred_ty(const APInt *&R) : Res(R) {}
332
333 template <typename ITy> bool match(ITy *V) {
334 if (const auto *CI = dyn_cast<ConstantInt>(V))
335 if (this->isValue(CI->getValue())) {
336 Res = &CI->getValue();
337 return true;
338 }
339 if (V->getType()->isVectorTy())
340 if (const auto *C = dyn_cast<Constant>(V))
341 if (auto *CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue()))
342 if (this->isValue(CI->getValue())) {
343 Res = &CI->getValue();
344 return true;
345 }
346
347 return false;
348 }
349};
350
351/// This helper class is used to match scalar and vector constants that
352/// satisfy a specified predicate, and bind them to an APFloat.
353/// Undefs are allowed in splat vector constants.
354template <typename Predicate> struct apf_pred_ty : public Predicate {
355 const APFloat *&Res;
356
357 apf_pred_ty(const APFloat *&R) : Res(R) {}
358
359 template <typename ITy> bool match(ITy *V) {
360 if (const auto *CI = dyn_cast<ConstantFP>(V))
361 if (this->isValue(CI->getValue())) {
362 Res = &CI->getValue();
363 return true;
364 }
365 if (V->getType()->isVectorTy())
366 if (const auto *C = dyn_cast<Constant>(V))
367 if (auto *CI = dyn_cast_or_null<ConstantFP>(
368 C->getSplatValue(/* AllowUndef */ true)))
369 if (this->isValue(CI->getValue())) {
370 Res = &CI->getValue();
371 return true;
372 }
373
374 return false;
375 }
376};
377
378///////////////////////////////////////////////////////////////////////////////
379//
380// Encapsulate constant value queries for use in templated predicate matchers.
381// This allows checking if constants match using compound predicates and works
382// with vector constants, possibly with relaxed constraints. For example, ignore
383// undef values.
384//
385///////////////////////////////////////////////////////////////////////////////
386
387struct is_any_apint {
388 bool isValue(const APInt &C) { return true; }
389};
390/// Match an integer or vector with any integral constant.
391/// For vectors, this includes constants with undefined elements.
392inline cst_pred_ty<is_any_apint> m_AnyIntegralConstant() {
393 return cst_pred_ty<is_any_apint>();
394}
395
396struct is_all_ones {
397 bool isValue(const APInt &C) { return C.isAllOnesValue(); }
398};
399/// Match an integer or vector with all bits set.
400/// For vectors, this includes constants with undefined elements.
401inline cst_pred_ty<is_all_ones> m_AllOnes() {
402 return cst_pred_ty<is_all_ones>();
403}
404
405struct is_maxsignedvalue {
406 bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
407};
408/// Match an integer or vector with values having all bits except for the high
409/// bit set (0x7f...).
410/// For vectors, this includes constants with undefined elements.
411inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() {
412 return cst_pred_ty<is_maxsignedvalue>();
413}
414inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) {
415 return V;
416}
417
418struct is_negative {
419 bool isValue(const APInt &C) { return C.isNegative(); }
420};
421/// Match an integer or vector of negative values.
422/// For vectors, this includes constants with undefined elements.
423inline cst_pred_ty<is_negative> m_Negative() {
424 return cst_pred_ty<is_negative>();
425}
426inline api_pred_ty<is_negative> m_Negative(const APInt *&V) {
427 return V;
428}
429
430struct is_nonnegative {
431 bool isValue(const APInt &C) { return C.isNonNegative(); }
432};
433/// Match an integer or vector of non-negative values.
434/// For vectors, this includes constants with undefined elements.
435inline cst_pred_ty<is_nonnegative> m_NonNegative() {
436 return cst_pred_ty<is_nonnegative>();
437}
438inline api_pred_ty<is_nonnegative> m_NonNegative(const APInt *&V) {
439 return V;
440}
441
442struct is_strictlypositive {
443 bool isValue(const APInt &C) { return C.isStrictlyPositive(); }
444};
445/// Match an integer or vector of strictly positive values.
446/// For vectors, this includes constants with undefined elements.
447inline cst_pred_ty<is_strictlypositive> m_StrictlyPositive() {
448 return cst_pred_ty<is_strictlypositive>();
449}
450inline api_pred_ty<is_strictlypositive> m_StrictlyPositive(const APInt *&V) {
451 return V;
452}
453
454struct is_nonpositive {
455 bool isValue(const APInt &C) { return C.isNonPositive(); }
456};
457/// Match an integer or vector of non-positive values.
458/// For vectors, this includes constants with undefined elements.
459inline cst_pred_ty<is_nonpositive> m_NonPositive() {
460 return cst_pred_ty<is_nonpositive>();
461}
462inline api_pred_ty<is_nonpositive> m_NonPositive(const APInt *&V) { return V; }
463
464struct is_one {
465 bool isValue(const APInt &C) { return C.isOneValue(); }
466};
467/// Match an integer 1 or a vector with all elements equal to 1.
468/// For vectors, this includes constants with undefined elements.
469inline cst_pred_ty<is_one> m_One() {
470 return cst_pred_ty<is_one>();
471}
472
473struct is_zero_int {
474 bool isValue(const APInt &C) { return C.isNullValue(); }
475};
476/// Match an integer 0 or a vector with all elements equal to 0.
477/// For vectors, this includes constants with undefined elements.
478inline cst_pred_ty<is_zero_int> m_ZeroInt() {
479 return cst_pred_ty<is_zero_int>();
480}
481
482struct is_zero {
483 template <typename ITy> bool match(ITy *V) {
484 auto *C = dyn_cast<Constant>(V);
485 // FIXME: this should be able to do something for scalable vectors
486 return C && (C->isNullValue() || cst_pred_ty<is_zero_int>().match(C));
487 }
488};
489/// Match any null constant or a vector with all elements equal to 0.
490/// For vectors, this includes constants with undefined elements.
491inline is_zero m_Zero() {
492 return is_zero();
493}
494
495struct is_power2 {
496 bool isValue(const APInt &C) { return C.isPowerOf2(); }
497};
498/// Match an integer or vector power-of-2.
499/// For vectors, this includes constants with undefined elements.
500inline cst_pred_ty<is_power2> m_Power2() {
501 return cst_pred_ty<is_power2>();
502}
503inline api_pred_ty<is_power2> m_Power2(const APInt *&V) {
504 return V;
505}
506
507struct is_negated_power2 {
508 bool isValue(const APInt &C) { return (-C).isPowerOf2(); }
509};
510/// Match a integer or vector negated power-of-2.
511/// For vectors, this includes constants with undefined elements.
512inline cst_pred_ty<is_negated_power2> m_NegatedPower2() {
513 return cst_pred_ty<is_negated_power2>();
514}
515inline api_pred_ty<is_negated_power2> m_NegatedPower2(const APInt *&V) {
516 return V;
517}
518
519struct is_power2_or_zero {
520 bool isValue(const APInt &C) { return !C || C.isPowerOf2(); }
521};
522/// Match an integer or vector of 0 or power-of-2 values.
523/// For vectors, this includes constants with undefined elements.
524inline cst_pred_ty<is_power2_or_zero> m_Power2OrZero() {
525 return cst_pred_ty<is_power2_or_zero>();
526}
527inline api_pred_ty<is_power2_or_zero> m_Power2OrZero(const APInt *&V) {
528 return V;
529}
530
531struct is_sign_mask {
532 bool isValue(const APInt &C) { return C.isSignMask(); }
533};
534/// Match an integer or vector with only the sign bit(s) set.
535/// For vectors, this includes constants with undefined elements.
536inline cst_pred_ty<is_sign_mask> m_SignMask() {
537 return cst_pred_ty<is_sign_mask>();
538}
539
540struct is_lowbit_mask {
541 bool isValue(const APInt &C) { return C.isMask(); }
542};
543/// Match an integer or vector with only the low bit(s) set.
544/// For vectors, this includes constants with undefined elements.
545inline cst_pred_ty<is_lowbit_mask> m_LowBitMask() {
546 return cst_pred_ty<is_lowbit_mask>();
547}
548
549struct icmp_pred_with_threshold {
550 ICmpInst::Predicate Pred;
551 const APInt *Thr;
552 bool isValue(const APInt &C) {
553 switch (Pred) {
554 case ICmpInst::Predicate::ICMP_EQ:
555 return C.eq(*Thr);
556 case ICmpInst::Predicate::ICMP_NE:
557 return C.ne(*Thr);
558 case ICmpInst::Predicate::ICMP_UGT:
559 return C.ugt(*Thr);
560 case ICmpInst::Predicate::ICMP_UGE:
561 return C.uge(*Thr);
562 case ICmpInst::Predicate::ICMP_ULT:
563 return C.ult(*Thr);
564 case ICmpInst::Predicate::ICMP_ULE:
565 return C.ule(*Thr);
566 case ICmpInst::Predicate::ICMP_SGT:
567 return C.sgt(*Thr);
568 case ICmpInst::Predicate::ICMP_SGE:
569 return C.sge(*Thr);
570 case ICmpInst::Predicate::ICMP_SLT:
571 return C.slt(*Thr);
572 case ICmpInst::Predicate::ICMP_SLE:
573 return C.sle(*Thr);
574 default:
575 llvm_unreachable("Unhandled ICmp predicate")::llvm::llvm_unreachable_internal("Unhandled ICmp predicate",
"/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/include/llvm/IR/PatternMatch.h"
, 575)
;
576 }
577 }
578};
579/// Match an integer or vector with every element comparing 'pred' (eg/ne/...)
580/// to Threshold. For vectors, this includes constants with undefined elements.
581inline cst_pred_ty<icmp_pred_with_threshold>
582m_SpecificInt_ICMP(ICmpInst::Predicate Predicate, const APInt &Threshold) {
583 cst_pred_ty<icmp_pred_with_threshold> P;
584 P.Pred = Predicate;
585 P.Thr = &Threshold;
586 return P;
587}
588
589struct is_nan {
590 bool isValue(const APFloat &C) { return C.isNaN(); }
591};
592/// Match an arbitrary NaN constant. This includes quiet and signalling nans.
593/// For vectors, this includes constants with undefined elements.
594inline cstfp_pred_ty<is_nan> m_NaN() {
595 return cstfp_pred_ty<is_nan>();
596}
597
598struct is_nonnan {
599 bool isValue(const APFloat &C) { return !C.isNaN(); }
600};
601/// Match a non-NaN FP constant.
602/// For vectors, this includes constants with undefined elements.
603inline cstfp_pred_ty<is_nonnan> m_NonNaN() {
604 return cstfp_pred_ty<is_nonnan>();
605}
606
607struct is_inf {
608 bool isValue(const APFloat &C) { return C.isInfinity(); }
609};
610/// Match a positive or negative infinity FP constant.
611/// For vectors, this includes constants with undefined elements.
612inline cstfp_pred_ty<is_inf> m_Inf() {
613 return cstfp_pred_ty<is_inf>();
614}
615
616struct is_noninf {
617 bool isValue(const APFloat &C) { return !C.isInfinity(); }
618};
619/// Match a non-infinity FP constant, i.e. finite or NaN.
620/// For vectors, this includes constants with undefined elements.
621inline cstfp_pred_ty<is_noninf> m_NonInf() {
622 return cstfp_pred_ty<is_noninf>();
623}
624
625struct is_finite {
626 bool isValue(const APFloat &C) { return C.isFinite(); }
627};
628/// Match a finite FP constant, i.e. not infinity or NaN.
629/// For vectors, this includes constants with undefined elements.
630inline cstfp_pred_ty<is_finite> m_Finite() {
631 return cstfp_pred_ty<is_finite>();
632}
633inline apf_pred_ty<is_finite> m_Finite(const APFloat *&V) { return V; }
634
635struct is_finitenonzero {
636 bool isValue(const APFloat &C) { return C.isFiniteNonZero(); }
637};
638/// Match a finite non-zero FP constant.
639/// For vectors, this includes constants with undefined elements.
640inline cstfp_pred_ty<is_finitenonzero> m_FiniteNonZero() {
641 return cstfp_pred_ty<is_finitenonzero>();
642}
643inline apf_pred_ty<is_finitenonzero> m_FiniteNonZero(const APFloat *&V) {
644 return V;
645}
646
647struct is_any_zero_fp {
648 bool isValue(const APFloat &C) { return C.isZero(); }
649};
650/// Match a floating-point negative zero or positive zero.
651/// For vectors, this includes constants with undefined elements.
652inline cstfp_pred_ty<is_any_zero_fp> m_AnyZeroFP() {
653 return cstfp_pred_ty<is_any_zero_fp>();
654}
655
656struct is_pos_zero_fp {
657 bool isValue(const APFloat &C) { return C.isPosZero(); }
658};
659/// Match a floating-point positive zero.
660/// For vectors, this includes constants with undefined elements.
661inline cstfp_pred_ty<is_pos_zero_fp> m_PosZeroFP() {
662 return cstfp_pred_ty<is_pos_zero_fp>();
663}
664
665struct is_neg_zero_fp {
666 bool isValue(const APFloat &C) { return C.isNegZero(); }
667};
668/// Match a floating-point negative zero.
669/// For vectors, this includes constants with undefined elements.
670inline cstfp_pred_ty<is_neg_zero_fp> m_NegZeroFP() {
671 return cstfp_pred_ty<is_neg_zero_fp>();
672}
673
674struct is_non_zero_fp {
675 bool isValue(const APFloat &C) { return C.isNonZero(); }
676};
677/// Match a floating-point non-zero.
678/// For vectors, this includes constants with undefined elements.
679inline cstfp_pred_ty<is_non_zero_fp> m_NonZeroFP() {
680 return cstfp_pred_ty<is_non_zero_fp>();
681}
682
683///////////////////////////////////////////////////////////////////////////////
684
685template <typename Class> struct bind_ty {
686 Class *&VR;
687
688 bind_ty(Class *&V) : VR(V) {}
689
690 template <typename ITy> bool match(ITy *V) {
691 if (auto *CV = dyn_cast<Class>(V)) {
692 VR = CV;
693 return true;
694 }
695 return false;
696 }
697};
698
699/// Match a value, capturing it if we match.
700inline bind_ty<Value> m_Value(Value *&V) { return V; }
12
Calling constructor for 'bind_ty<llvm::Value>'
13
Returning from constructor for 'bind_ty<llvm::Value>'
14
Returning without writing to 'V'
701inline bind_ty<const Value> m_Value(const Value *&V) { return V; }
702
703/// Match an instruction, capturing it if we match.
704inline bind_ty<Instruction> m_Instruction(Instruction *&I) { return I; }
705/// Match a unary operator, capturing it if we match.
706inline bind_ty<UnaryOperator> m_UnOp(UnaryOperator *&I) { return I; }
707/// Match a binary operator, capturing it if we match.
708inline bind_ty<BinaryOperator> m_BinOp(BinaryOperator *&I) { return I; }
709/// Match a with overflow intrinsic, capturing it if we match.
710inline bind_ty<WithOverflowInst> m_WithOverflowInst(WithOverflowInst *&I) { return I; }
711inline bind_ty<const WithOverflowInst>
712m_WithOverflowInst(const WithOverflowInst *&I) {
713 return I;
714}
715
716/// Match a Constant, capturing the value if we match.
717inline bind_ty<Constant> m_Constant(Constant *&C) { return C; }
718
719/// Match a ConstantInt, capturing the value if we match.
720inline bind_ty<ConstantInt> m_ConstantInt(ConstantInt *&CI) { return CI; }
721
722/// Match a ConstantFP, capturing the value if we match.
723inline bind_ty<ConstantFP> m_ConstantFP(ConstantFP *&C) { return C; }
724
725/// Match a ConstantExpr, capturing the value if we match.
726inline bind_ty<ConstantExpr> m_ConstantExpr(ConstantExpr *&C) { return C; }
727
728/// Match a basic block value, capturing it if we match.
729inline bind_ty<BasicBlock> m_BasicBlock(BasicBlock *&V) { return V; }
730inline bind_ty<const BasicBlock> m_BasicBlock(const BasicBlock *&V) {
731 return V;
732}
733
734/// Match an arbitrary immediate Constant and ignore it.
735inline match_combine_and<class_match<Constant>,
736 match_unless<class_match<ConstantExpr>>>
737m_ImmConstant() {
738 return m_CombineAnd(m_Constant(), m_Unless(m_ConstantExpr()));
739}
740
741/// Match an immediate Constant, capturing the value if we match.
742inline match_combine_and<bind_ty<Constant>,
743 match_unless<class_match<ConstantExpr>>>
744m_ImmConstant(Constant *&C) {
745 return m_CombineAnd(m_Constant(C), m_Unless(m_ConstantExpr()));
746}
747
748/// Match a specified Value*.
749struct specificval_ty {
750 const Value *Val;
751
752 specificval_ty(const Value *V) : Val(V) {}
753
754 template <typename ITy> bool match(ITy *V) { return V == Val; }
755};
756
757/// Match if we have a specific specified value.
758inline specificval_ty m_Specific(const Value *V) { return V; }
759
760/// Stores a reference to the Value *, not the Value * itself,
761/// thus can be used in commutative matchers.
762template <typename Class> struct deferredval_ty {
763 Class *const &Val;
764
765 deferredval_ty(Class *const &V) : Val(V) {}
766
767 template <typename ITy> bool match(ITy *const V) { return V == Val; }
768};
769
770/// A commutative-friendly version of m_Specific().
771inline deferredval_ty<Value> m_Deferred(Value *const &V) { return V; }
772inline deferredval_ty<const Value> m_Deferred(const Value *const &V) {
773 return V;
774}
775
776/// Match a specified floating point value or vector of all elements of
777/// that value.
778struct specific_fpval {
779 double Val;
780
781 specific_fpval(double V) : Val(V) {}
782
783 template <typename ITy> bool match(ITy *V) {
784 if (const auto *CFP = dyn_cast<ConstantFP>(V))
785 return CFP->isExactlyValue(Val);
786 if (V->getType()->isVectorTy())
787 if (const auto *C = dyn_cast<Constant>(V))
788 if (auto *CFP = dyn_cast_or_null<ConstantFP>(C->getSplatValue()))
789 return CFP->isExactlyValue(Val);
790 return false;
791 }
792};
793
794/// Match a specific floating point value or vector with all elements
795/// equal to the value.
796inline specific_fpval m_SpecificFP(double V) { return specific_fpval(V); }
797
798/// Match a float 1.0 or vector with all elements equal to 1.0.
799inline specific_fpval m_FPOne() { return m_SpecificFP(1.0); }
800
801struct bind_const_intval_ty {
802 uint64_t &VR;
803
804 bind_const_intval_ty(uint64_t &V) : VR(V) {}
805
806 template <typename ITy> bool match(ITy *V) {
807 if (const auto *CV = dyn_cast<ConstantInt>(V))
808 if (CV->getValue().ule(UINT64_MAX(18446744073709551615UL))) {
809 VR = CV->getZExtValue();
810 return true;
811 }
812 return false;
813 }
814};
815
816/// Match a specified integer value or vector of all elements of that
817/// value.
818template <bool AllowUndefs>
819struct specific_intval {
820 APInt Val;
821
822 specific_intval(APInt V) : Val(std::move(V)) {}
823
824 template <typename ITy> bool match(ITy *V) {
825 const auto *CI = dyn_cast<ConstantInt>(V);
826 if (!CI && V->getType()->isVectorTy())
827 if (const auto *C = dyn_cast<Constant>(V))
828 CI = dyn_cast_or_null<ConstantInt>(C->getSplatValue(AllowUndefs));
829
830 return CI && APInt::isSameValue(CI->getValue(), Val);
831 }
832};
833
834/// Match a specific integer value or vector with all elements equal to
835/// the value.
836inline specific_intval<false> m_SpecificInt(APInt V) {
837 return specific_intval<false>(std::move(V));
838}
839
840inline specific_intval<false> m_SpecificInt(uint64_t V) {
841 return m_SpecificInt(APInt(64, V));
842}
843
844inline specific_intval<true> m_SpecificIntAllowUndef(APInt V) {
845 return specific_intval<true>(std::move(V));
846}
847
848inline specific_intval<true> m_SpecificIntAllowUndef(uint64_t V) {
849 return m_SpecificIntAllowUndef(APInt(64, V));
850}
851
852/// Match a ConstantInt and bind to its value. This does not match
853/// ConstantInts wider than 64-bits.
854inline bind_const_intval_ty m_ConstantInt(uint64_t &V) { return V; }
855
856/// Match a specified basic block value.
857struct specific_bbval {
858 BasicBlock *Val;
859
860 specific_bbval(BasicBlock *Val) : Val(Val) {}
861
862 template <typename ITy> bool match(ITy *V) {
863 const auto *BB = dyn_cast<BasicBlock>(V);
864 return BB && BB == Val;
865 }
866};
867
868/// Match a specific basic block value.
869inline specific_bbval m_SpecificBB(BasicBlock *BB) {
870 return specific_bbval(BB);
871}
872
873/// A commutative-friendly version of m_Specific().
874inline deferredval_ty<BasicBlock> m_Deferred(BasicBlock *const &BB) {
875 return BB;
876}
877inline deferredval_ty<const BasicBlock>
878m_Deferred(const BasicBlock *const &BB) {
879 return BB;
880}
881
882//===----------------------------------------------------------------------===//
883// Matcher for any binary operator.
884//
885template <typename LHS_t, typename RHS_t, bool Commutable = false>
886struct AnyBinaryOp_match {
887 LHS_t L;
888 RHS_t R;
889
890 // The evaluation order is always stable, regardless of Commutability.
891 // The LHS is always matched first.
892 AnyBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
893
894 template <typename OpTy> bool match(OpTy *V) {
895 if (auto *I = dyn_cast<BinaryOperator>(V))
896 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
897 (Commutable && L.match(I->getOperand(1)) &&
898 R.match(I->getOperand(0)));
899 return false;
900 }
901};
902
903template <typename LHS, typename RHS>
904inline AnyBinaryOp_match<LHS, RHS> m_BinOp(const LHS &L, const RHS &R) {
905 return AnyBinaryOp_match<LHS, RHS>(L, R);
906}
907
908//===----------------------------------------------------------------------===//
909// Matcher for any unary operator.
910// TODO fuse unary, binary matcher into n-ary matcher
911//
912template <typename OP_t> struct AnyUnaryOp_match {
913 OP_t X;
914
915 AnyUnaryOp_match(const OP_t &X) : X(X) {}
916
917 template <typename OpTy> bool match(OpTy *V) {
918 if (auto *I = dyn_cast<UnaryOperator>(V))
919 return X.match(I->getOperand(0));
920 return false;
921 }
922};
923
924template <typename OP_t> inline AnyUnaryOp_match<OP_t> m_UnOp(const OP_t &X) {
925 return AnyUnaryOp_match<OP_t>(X);
926}
927
928//===----------------------------------------------------------------------===//
929// Matchers for specific binary operators.
930//
931
932template <typename LHS_t, typename RHS_t, unsigned Opcode,
933 bool Commutable = false>
934struct BinaryOp_match {
935 LHS_t L;
936 RHS_t R;
937
938 // The evaluation order is always stable, regardless of Commutability.
939 // The LHS is always matched first.
940 BinaryOp_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
941
942 template <typename OpTy> bool match(OpTy *V) {
943 if (V->getValueID() == Value::InstructionVal + Opcode) {
32
Assuming the condition is true
33
Taking true branch
944 auto *I = cast<BinaryOperator>(V);
34
'V' is a 'BinaryOperator'
945 return (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) ||
35
Returning the value 1, which participates in a condition later
946 (Commutable && L.match(I->getOperand(1)) &&
947 R.match(I->getOperand(0)));
948 }
949 if (auto *CE = dyn_cast<ConstantExpr>(V))
950 return CE->getOpcode() == Opcode &&
951 ((L.match(CE->getOperand(0)) && R.match(CE->getOperand(1))) ||
952 (Commutable && L.match(CE->getOperand(1)) &&
953 R.match(CE->getOperand(0))));
954 return false;
955 }
956};
957
958template <typename LHS, typename RHS>
959inline BinaryOp_match<LHS, RHS, Instruction::Add> m_Add(const LHS &L,
960 const RHS &R) {
961 return BinaryOp_match<LHS, RHS, Instruction::Add>(L, R);
962}
963
964template <typename LHS, typename RHS>
965inline BinaryOp_match<LHS, RHS, Instruction::FAdd> m_FAdd(const LHS &L,
966 const RHS &R) {
967 return BinaryOp_match<LHS, RHS, Instruction::FAdd>(L, R);
968}
969
970template <typename LHS, typename RHS>
971inline BinaryOp_match<LHS, RHS, Instruction::Sub> m_Sub(const LHS &L,
972 const RHS &R) {
973 return BinaryOp_match<LHS, RHS, Instruction::Sub>(L, R);
974}
975
976template <typename LHS, typename RHS>
977inline BinaryOp_match<LHS, RHS, Instruction::FSub> m_FSub(const LHS &L,
978 const RHS &R) {
979 return BinaryOp_match<LHS, RHS, Instruction::FSub>(L, R);
980}
981
982template <typename Op_t> struct FNeg_match {
983 Op_t X;
984
985 FNeg_match(const Op_t &Op) : X(Op) {}
986 template <typename OpTy> bool match(OpTy *V) {
987 auto *FPMO = dyn_cast<FPMathOperator>(V);
988 if (!FPMO) return false;
989
990 if (FPMO->getOpcode() == Instruction::FNeg)
991 return X.match(FPMO->getOperand(0));
992
993 if (FPMO->getOpcode() == Instruction::FSub) {
994 if (FPMO->hasNoSignedZeros()) {
995 // With 'nsz', any zero goes.
996 if (!cstfp_pred_ty<is_any_zero_fp>().match(FPMO->getOperand(0)))
997 return false;
998 } else {
999 // Without 'nsz', we need fsub -0.0, X exactly.
1000 if (!cstfp_pred_ty<is_neg_zero_fp>().match(FPMO->getOperand(0)))
1001 return false;
1002 }
1003
1004 return X.match(FPMO->getOperand(1));
1005 }
1006
1007 return false;
1008 }
1009};
1010
1011/// Match 'fneg X' as 'fsub -0.0, X'.
1012template <typename OpTy>
1013inline FNeg_match<OpTy>
1014m_FNeg(const OpTy &X) {
1015 return FNeg_match<OpTy>(X);
1016}
1017
1018/// Match 'fneg X' as 'fsub +-0.0, X'.
1019template <typename RHS>
1020inline BinaryOp_match<cstfp_pred_ty<is_any_zero_fp>, RHS, Instruction::FSub>
1021m_FNegNSZ(const RHS &X) {
1022 return m_FSub(m_AnyZeroFP(), X);
1023}
1024
1025template <typename LHS, typename RHS>
1026inline BinaryOp_match<LHS, RHS, Instruction::Mul> m_Mul(const LHS &L,
1027 const RHS &R) {
1028 return BinaryOp_match<LHS, RHS, Instruction::Mul>(L, R);
1029}
1030
1031template <typename LHS, typename RHS>
1032inline BinaryOp_match<LHS, RHS, Instruction::FMul> m_FMul(const LHS &L,
1033 const RHS &R) {
1034 return BinaryOp_match<LHS, RHS, Instruction::FMul>(L, R);
1035}
1036
1037template <typename LHS, typename RHS>
1038inline BinaryOp_match<LHS, RHS, Instruction::UDiv> m_UDiv(const LHS &L,
1039 const RHS &R) {
1040 return BinaryOp_match<LHS, RHS, Instruction::UDiv>(L, R);
1041}
1042
1043template <typename LHS, typename RHS>
1044inline BinaryOp_match<LHS, RHS, Instruction::SDiv> m_SDiv(const LHS &L,
1045 const RHS &R) {
1046 return BinaryOp_match<LHS, RHS, Instruction::SDiv>(L, R);
1047}
1048
1049template <typename LHS, typename RHS>
1050inline BinaryOp_match<LHS, RHS, Instruction::FDiv> m_FDiv(const LHS &L,
1051 const RHS &R) {
1052 return BinaryOp_match<LHS, RHS, Instruction::FDiv>(L, R);
1053}
1054
1055template <typename LHS, typename RHS>
1056inline BinaryOp_match<LHS, RHS, Instruction::URem> m_URem(const LHS &L,
1057 const RHS &R) {
1058 return BinaryOp_match<LHS, RHS, Instruction::URem>(L, R);
1059}
1060
1061template <typename LHS, typename RHS>
1062inline BinaryOp_match<LHS, RHS, Instruction::SRem> m_SRem(const LHS &L,
1063 const RHS &R) {
1064 return BinaryOp_match<LHS, RHS, Instruction::SRem>(L, R);
1065}
1066
1067template <typename LHS, typename RHS>
1068inline BinaryOp_match<LHS, RHS, Instruction::FRem> m_FRem(const LHS &L,
1069 const RHS &R) {
1070 return BinaryOp_match<LHS, RHS, Instruction::FRem>(L, R);
1071}
1072
1073template <typename LHS, typename RHS>
1074inline BinaryOp_match<LHS, RHS, Instruction::And> m_And(const LHS &L,
1075 const RHS &R) {
1076 return BinaryOp_match<LHS, RHS, Instruction::And>(L, R);
1077}
1078
1079template <typename LHS, typename RHS>
1080inline BinaryOp_match<LHS, RHS, Instruction::Or> m_Or(const LHS &L,
1081 const RHS &R) {
1082 return BinaryOp_match<LHS, RHS, Instruction::Or>(L, R);
1083}
1084
1085template <typename LHS, typename RHS>
1086inline BinaryOp_match<LHS, RHS, Instruction::Xor> m_Xor(const LHS &L,
1087 const RHS &R) {
1088 return BinaryOp_match<LHS, RHS, Instruction::Xor>(L, R);
1089}
1090
1091template <typename LHS, typename RHS>
1092inline BinaryOp_match<LHS, RHS, Instruction::Shl> m_Shl(const LHS &L,
1093 const RHS &R) {
1094 return BinaryOp_match<LHS, RHS, Instruction::Shl>(L, R);
1095}
1096
1097template <typename LHS, typename RHS>
1098inline BinaryOp_match<LHS, RHS, Instruction::LShr> m_LShr(const LHS &L,
1099 const RHS &R) {
1100 return BinaryOp_match<LHS, RHS, Instruction::LShr>(L, R);
1101}
1102
1103template <typename LHS, typename RHS>
1104inline BinaryOp_match<LHS, RHS, Instruction::AShr> m_AShr(const LHS &L,
1105 const RHS &R) {
1106 return BinaryOp_match<LHS, RHS, Instruction::AShr>(L, R);
1107}
1108
1109template <typename LHS_t, typename RHS_t, unsigned Opcode,
1110 unsigned WrapFlags = 0>
1111struct OverflowingBinaryOp_match {
1112 LHS_t L;
1113 RHS_t R;
1114
1115 OverflowingBinaryOp_match(const LHS_t &LHS, const RHS_t &RHS)
1116 : L(LHS), R(RHS) {}
1117
1118 template <typename OpTy> bool match(OpTy *V) {
1119 if (auto *Op = dyn_cast<OverflowingBinaryOperator>(V)) {
1120 if (Op->getOpcode() != Opcode)
1121 return false;
1122 if (WrapFlags & OverflowingBinaryOperator::NoUnsignedWrap &&
1123 !Op->hasNoUnsignedWrap())
1124 return false;
1125 if (WrapFlags & OverflowingBinaryOperator::NoSignedWrap &&
1126 !Op->hasNoSignedWrap())
1127 return false;
1128 return L.match(Op->getOperand(0)) && R.match(Op->getOperand(1));
1129 }
1130 return false;
1131 }
1132};
1133
1134template <typename LHS, typename RHS>
1135inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1136 OverflowingBinaryOperator::NoSignedWrap>
1137m_NSWAdd(const LHS &L, const RHS &R) {
1138 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1139 OverflowingBinaryOperator::NoSignedWrap>(
1140 L, R);
1141}
1142template <typename LHS, typename RHS>
1143inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1144 OverflowingBinaryOperator::NoSignedWrap>
1145m_NSWSub(const LHS &L, const RHS &R) {
1146 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1147 OverflowingBinaryOperator::NoSignedWrap>(
1148 L, R);
1149}
1150template <typename LHS, typename RHS>
1151inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1152 OverflowingBinaryOperator::NoSignedWrap>
1153m_NSWMul(const LHS &L, const RHS &R) {
1154 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1155 OverflowingBinaryOperator::NoSignedWrap>(
1156 L, R);
1157}
1158template <typename LHS, typename RHS>
1159inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1160 OverflowingBinaryOperator::NoSignedWrap>
1161m_NSWShl(const LHS &L, const RHS &R) {
1162 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1163 OverflowingBinaryOperator::NoSignedWrap>(
1164 L, R);
1165}
1166
1167template <typename LHS, typename RHS>
1168inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1169 OverflowingBinaryOperator::NoUnsignedWrap>
1170m_NUWAdd(const LHS &L, const RHS &R) {
1171 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1172 OverflowingBinaryOperator::NoUnsignedWrap>(
1173 L, R);
1174}
1175template <typename LHS, typename RHS>
1176inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1177 OverflowingBinaryOperator::NoUnsignedWrap>
1178m_NUWSub(const LHS &L, const RHS &R) {
1179 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
1180 OverflowingBinaryOperator::NoUnsignedWrap>(
1181 L, R);
1182}
1183template <typename LHS, typename RHS>
1184inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1185 OverflowingBinaryOperator::NoUnsignedWrap>
1186m_NUWMul(const LHS &L, const RHS &R) {
1187 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Mul,
1188 OverflowingBinaryOperator::NoUnsignedWrap>(
1189 L, R);
1190}
1191template <typename LHS, typename RHS>
1192inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1193 OverflowingBinaryOperator::NoUnsignedWrap>
1194m_NUWShl(const LHS &L, const RHS &R) {
1195 return OverflowingBinaryOp_match<LHS, RHS, Instruction::Shl,
1196 OverflowingBinaryOperator::NoUnsignedWrap>(
1197 L, R);
1198}
1199
1200//===----------------------------------------------------------------------===//
1201// Class that matches a group of binary opcodes.
1202//
1203template <typename LHS_t, typename RHS_t, typename Predicate>
1204struct BinOpPred_match : Predicate {
1205 LHS_t L;
1206 RHS_t R;
1207
1208 BinOpPred_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1209
1210 template <typename OpTy> bool match(OpTy *V) {
1211 if (auto *I = dyn_cast<Instruction>(V))
1212 return this->isOpType(I->getOpcode()) && L.match(I->getOperand(0)) &&
1213 R.match(I->getOperand(1));
1214 if (auto *CE = dyn_cast<ConstantExpr>(V))
1215 return this->isOpType(CE->getOpcode()) && L.match(CE->getOperand(0)) &&
1216 R.match(CE->getOperand(1));
1217 return false;
1218 }
1219};
1220
1221struct is_shift_op {
1222 bool isOpType(unsigned Opcode) { return Instruction::isShift(Opcode); }
1223};
1224
1225struct is_right_shift_op {
1226 bool isOpType(unsigned Opcode) {
1227 return Opcode == Instruction::LShr || Opcode == Instruction::AShr;
1228 }
1229};
1230
1231struct is_logical_shift_op {
1232 bool isOpType(unsigned Opcode) {
1233 return Opcode == Instruction::LShr || Opcode == Instruction::Shl;
1234 }
1235};
1236
1237struct is_bitwiselogic_op {
1238 bool isOpType(unsigned Opcode) {
1239 return Instruction::isBitwiseLogicOp(Opcode);
1240 }
1241};
1242
1243struct is_idiv_op {
1244 bool isOpType(unsigned Opcode) {
1245 return Opcode == Instruction::SDiv || Opcode == Instruction::UDiv;
1246 }
1247};
1248
1249struct is_irem_op {
1250 bool isOpType(unsigned Opcode) {
1251 return Opcode == Instruction::SRem || Opcode == Instruction::URem;
1252 }
1253};
1254
1255/// Matches shift operations.
1256template <typename LHS, typename RHS>
1257inline BinOpPred_match<LHS, RHS, is_shift_op> m_Shift(const LHS &L,
1258 const RHS &R) {
1259 return BinOpPred_match<LHS, RHS, is_shift_op>(L, R);
26
Returning without writing to 'R.R.VR'
1260}
1261
1262/// Matches logical shift operations.
1263template <typename LHS, typename RHS>
1264inline BinOpPred_match<LHS, RHS, is_right_shift_op> m_Shr(const LHS &L,
1265 const RHS &R) {
1266 return BinOpPred_match<LHS, RHS, is_right_shift_op>(L, R);
1267}
1268
1269/// Matches logical shift operations.
1270template <typename LHS, typename RHS>
1271inline BinOpPred_match<LHS, RHS, is_logical_shift_op>
1272m_LogicalShift(const LHS &L, const RHS &R) {
1273 return BinOpPred_match<LHS, RHS, is_logical_shift_op>(L, R);
1274}
1275
1276/// Matches bitwise logic operations.
1277template <typename LHS, typename RHS>
1278inline BinOpPred_match<LHS, RHS, is_bitwiselogic_op>
1279m_BitwiseLogic(const LHS &L, const RHS &R) {
1280 return BinOpPred_match<LHS, RHS, is_bitwiselogic_op>(L, R);
1281}
1282
1283/// Matches integer division operations.
1284template <typename LHS, typename RHS>
1285inline BinOpPred_match<LHS, RHS, is_idiv_op> m_IDiv(const LHS &L,
1286 const RHS &R) {
1287 return BinOpPred_match<LHS, RHS, is_idiv_op>(L, R);
1288}
1289
1290/// Matches integer remainder operations.
1291template <typename LHS, typename RHS>
1292inline BinOpPred_match<LHS, RHS, is_irem_op> m_IRem(const LHS &L,
1293 const RHS &R) {
1294 return BinOpPred_match<LHS, RHS, is_irem_op>(L, R);
1295}
1296
1297//===----------------------------------------------------------------------===//
1298// Class that matches exact binary ops.
1299//
1300template <typename SubPattern_t> struct Exact_match {
1301 SubPattern_t SubPattern;
1302
1303 Exact_match(const SubPattern_t &SP) : SubPattern(SP) {}
1304
1305 template <typename OpTy> bool match(OpTy *V) {
1306 if (auto *PEO = dyn_cast<PossiblyExactOperator>(V))
1307 return PEO->isExact() && SubPattern.match(V);
1308 return false;
1309 }
1310};
1311
1312template <typename T> inline Exact_match<T> m_Exact(const T &SubPattern) {
1313 return SubPattern;
1314}
1315
1316//===----------------------------------------------------------------------===//
1317// Matchers for CmpInst classes
1318//
1319
1320template <typename LHS_t, typename RHS_t, typename Class, typename PredicateTy,
1321 bool Commutable = false>
1322struct CmpClass_match {
1323 PredicateTy &Predicate;
1324 LHS_t L;
1325 RHS_t R;
1326
1327 // The evaluation order is always stable, regardless of Commutability.
1328 // The LHS is always matched first.
1329 CmpClass_match(PredicateTy &Pred, const LHS_t &LHS, const RHS_t &RHS)
1330 : Predicate(Pred), L(LHS), R(RHS) {}
1331
1332 template <typename OpTy> bool match(OpTy *V) {
1333 if (auto *I = dyn_cast<Class>(V)) {
1334 if (L.match(I->getOperand(0)) && R.match(I->getOperand(1))) {
1335 Predicate = I->getPredicate();
1336 return true;
1337 } else if (Commutable && L.match(I->getOperand(1)) &&
1338 R.match(I->getOperand(0))) {
1339 Predicate = I->getSwappedPredicate();
1340 return true;
1341 }
1342 }
1343 return false;
1344 }
1345};
1346
1347template <typename LHS, typename RHS>
1348inline CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>
1349m_Cmp(CmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1350 return CmpClass_match<LHS, RHS, CmpInst, CmpInst::Predicate>(Pred, L, R);
1351}
1352
1353template <typename LHS, typename RHS>
1354inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>
1355m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1356 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate>(Pred, L, R);
1357}
1358
1359template <typename LHS, typename RHS>
1360inline CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>
1361m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
1362 return CmpClass_match<LHS, RHS, FCmpInst, FCmpInst::Predicate>(Pred, L, R);
1363}
1364
1365//===----------------------------------------------------------------------===//
1366// Matchers for instructions with a given opcode and number of operands.
1367//
1368
1369/// Matches instructions with Opcode and three operands.
1370template <typename T0, unsigned Opcode> struct OneOps_match {
1371 T0 Op1;
1372
1373 OneOps_match(const T0 &Op1) : Op1(Op1) {}
1374
1375 template <typename OpTy> bool match(OpTy *V) {
1376 if (V->getValueID() == Value::InstructionVal + Opcode) {
1377 auto *I = cast<Instruction>(V);
1378 return Op1.match(I->getOperand(0));
1379 }
1380 return false;
1381 }
1382};
1383
1384/// Matches instructions with Opcode and three operands.
1385template <typename T0, typename T1, unsigned Opcode> struct TwoOps_match {
1386 T0 Op1;
1387 T1 Op2;
1388
1389 TwoOps_match(const T0 &Op1, const T1 &Op2) : Op1(Op1), Op2(Op2) {}
1390
1391 template <typename OpTy> bool match(OpTy *V) {
1392 if (V->getValueID() == Value::InstructionVal + Opcode) {
1393 auto *I = cast<Instruction>(V);
1394 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1));
1395 }
1396 return false;
1397 }
1398};
1399
1400/// Matches instructions with Opcode and three operands.
1401template <typename T0, typename T1, typename T2, unsigned Opcode>
1402struct ThreeOps_match {
1403 T0 Op1;
1404 T1 Op2;
1405 T2 Op3;
1406
1407 ThreeOps_match(const T0 &Op1, const T1 &Op2, const T2 &Op3)
1408 : Op1(Op1), Op2(Op2), Op3(Op3) {}
1409
1410 template <typename OpTy> bool match(OpTy *V) {
1411 if (V->getValueID() == Value::InstructionVal + Opcode) {
1412 auto *I = cast<Instruction>(V);
1413 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1414 Op3.match(I->getOperand(2));
1415 }
1416 return false;
1417 }
1418};
1419
1420/// Matches SelectInst.
1421template <typename Cond, typename LHS, typename RHS>
1422inline ThreeOps_match<Cond, LHS, RHS, Instruction::Select>
1423m_Select(const Cond &C, const LHS &L, const RHS &R) {
1424 return ThreeOps_match<Cond, LHS, RHS, Instruction::Select>(C, L, R);
1425}
1426
1427/// This matches a select of two constants, e.g.:
1428/// m_SelectCst<-1, 0>(m_Value(V))
1429template <int64_t L, int64_t R, typename Cond>
1430inline ThreeOps_match<Cond, constantint_match<L>, constantint_match<R>,
1431 Instruction::Select>
1432m_SelectCst(const Cond &C) {
1433 return m_Select(C, m_ConstantInt<L>(), m_ConstantInt<R>());
1434}
1435
1436/// Matches FreezeInst.
1437template <typename OpTy>
1438inline OneOps_match<OpTy, Instruction::Freeze> m_Freeze(const OpTy &Op) {
1439 return OneOps_match<OpTy, Instruction::Freeze>(Op);
1440}
1441
1442/// Matches InsertElementInst.
1443template <typename Val_t, typename Elt_t, typename Idx_t>
1444inline ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>
1445m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx) {
1446 return ThreeOps_match<Val_t, Elt_t, Idx_t, Instruction::InsertElement>(
1447 Val, Elt, Idx);
1448}
1449
1450/// Matches ExtractElementInst.
1451template <typename Val_t, typename Idx_t>
1452inline TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>
1453m_ExtractElt(const Val_t &Val, const Idx_t &Idx) {
1454 return TwoOps_match<Val_t, Idx_t, Instruction::ExtractElement>(Val, Idx);
1455}
1456
1457/// Matches shuffle.
1458template <typename T0, typename T1, typename T2> struct Shuffle_match {
1459 T0 Op1;
1460 T1 Op2;
1461 T2 Mask;
1462
1463 Shuffle_match(const T0 &Op1, const T1 &Op2, const T2 &Mask)
1464 : Op1(Op1), Op2(Op2), Mask(Mask) {}
1465
1466 template <typename OpTy> bool match(OpTy *V) {
1467 if (auto *I = dyn_cast<ShuffleVectorInst>(V)) {
1468 return Op1.match(I->getOperand(0)) && Op2.match(I->getOperand(1)) &&
1469 Mask.match(I->getShuffleMask());
1470 }
1471 return false;
1472 }
1473};
1474
1475struct m_Mask {
1476 ArrayRef<int> &MaskRef;
1477 m_Mask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
1478 bool match(ArrayRef<int> Mask) {
1479 MaskRef = Mask;
1480 return true;
1481 }
1482};
1483
1484struct m_ZeroMask {
1485 bool match(ArrayRef<int> Mask) {
1486 return all_of(Mask, [](int Elem) { return Elem == 0 || Elem == -1; });
1487 }
1488};
1489
1490struct m_SpecificMask {
1491 ArrayRef<int> &MaskRef;
1492 m_SpecificMask(ArrayRef<int> &MaskRef) : MaskRef(MaskRef) {}
1493 bool match(ArrayRef<int> Mask) { return MaskRef == Mask; }
1494};
1495
1496struct m_SplatOrUndefMask {
1497 int &SplatIndex;
1498 m_SplatOrUndefMask(int &SplatIndex) : SplatIndex(SplatIndex) {}
1499 bool match(ArrayRef<int> Mask) {
1500 auto First = find_if(Mask, [](int Elem) { return Elem != -1; });
1501 if (First == Mask.end())
1502 return false;
1503 SplatIndex = *First;
1504 return all_of(Mask,
1505 [First](int Elem) { return Elem == *First || Elem == -1; });
1506 }
1507};
1508
1509/// Matches ShuffleVectorInst independently of mask value.
1510template <typename V1_t, typename V2_t>
1511inline TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>
1512m_Shuffle(const V1_t &v1, const V2_t &v2) {
1513 return TwoOps_match<V1_t, V2_t, Instruction::ShuffleVector>(v1, v2);
1514}
1515
1516template <typename V1_t, typename V2_t, typename Mask_t>
1517inline Shuffle_match<V1_t, V2_t, Mask_t>
1518m_Shuffle(const V1_t &v1, const V2_t &v2, const Mask_t &mask) {
1519 return Shuffle_match<V1_t, V2_t, Mask_t>(v1, v2, mask);
1520}
1521
1522/// Matches LoadInst.
1523template <typename OpTy>
1524inline OneOps_match<OpTy, Instruction::Load> m_Load(const OpTy &Op) {
1525 return OneOps_match<OpTy, Instruction::Load>(Op);
1526}
1527
1528/// Matches StoreInst.
1529template <typename ValueOpTy, typename PointerOpTy>
1530inline TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>
1531m_Store(const ValueOpTy &ValueOp, const PointerOpTy &PointerOp) {
1532 return TwoOps_match<ValueOpTy, PointerOpTy, Instruction::Store>(ValueOp,
1533 PointerOp);
1534}
1535
1536//===----------------------------------------------------------------------===//
1537// Matchers for CastInst classes
1538//
1539
1540template <typename Op_t, unsigned Opcode> struct CastClass_match {
1541 Op_t Op;
1542
1543 CastClass_match(const Op_t &OpMatch) : Op(OpMatch) {}
1544
1545 template <typename OpTy> bool match(OpTy *V) {
1546 if (auto *O = dyn_cast<Operator>(V))
1547 return O->getOpcode() == Opcode && Op.match(O->getOperand(0));
1548 return false;
1549 }
1550};
1551
1552/// Matches BitCast.
1553template <typename OpTy>
1554inline CastClass_match<OpTy, Instruction::BitCast> m_BitCast(const OpTy &Op) {
1555 return CastClass_match<OpTy, Instruction::BitCast>(Op);
1556}
1557
1558/// Matches PtrToInt.
1559template <typename OpTy>
1560inline CastClass_match<OpTy, Instruction::PtrToInt> m_PtrToInt(const OpTy &Op) {
1561 return CastClass_match<OpTy, Instruction::PtrToInt>(Op);
1562}
1563
1564/// Matches IntToPtr.
1565template <typename OpTy>
1566inline CastClass_match<OpTy, Instruction::IntToPtr> m_IntToPtr(const OpTy &Op) {
1567 return CastClass_match<OpTy, Instruction::IntToPtr>(Op);
1568}
1569
1570/// Matches Trunc.
1571template <typename OpTy>
1572inline CastClass_match<OpTy, Instruction::Trunc> m_Trunc(const OpTy &Op) {
1573 return CastClass_match<OpTy, Instruction::Trunc>(Op);
1574}
1575
1576template <typename OpTy>
1577inline match_combine_or<CastClass_match<OpTy, Instruction::Trunc>, OpTy>
1578m_TruncOrSelf(const OpTy &Op) {
1579 return m_CombineOr(m_Trunc(Op), Op);
1580}
1581
1582/// Matches SExt.
1583template <typename OpTy>
1584inline CastClass_match<OpTy, Instruction::SExt> m_SExt(const OpTy &Op) {
1585 return CastClass_match<OpTy, Instruction::SExt>(Op);
1586}
1587
1588/// Matches ZExt.
1589template <typename OpTy>
1590inline CastClass_match<OpTy, Instruction::ZExt> m_ZExt(const OpTy &Op) {
1591 return CastClass_match<OpTy, Instruction::ZExt>(Op);
18
Returning without writing to 'Op.VR'
1592}
1593
1594template <typename OpTy>
1595inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>, OpTy>
1596m_ZExtOrSelf(const OpTy &Op) {
1597 return m_CombineOr(m_ZExt(Op), Op);
17
Calling 'm_ZExt<llvm::PatternMatch::bind_ty<llvm::Value>>'
19
Returning from 'm_ZExt<llvm::PatternMatch::bind_ty<llvm::Value>>'
20
Calling 'm_CombineOr<llvm::PatternMatch::CastClass_match<llvm::PatternMatch::bind_ty<llvm::Value>, 39>, llvm::PatternMatch::bind_ty<llvm::Value>>'
22
Returning from 'm_CombineOr<llvm::PatternMatch::CastClass_match<llvm::PatternMatch::bind_ty<llvm::Value>, 39>, llvm::PatternMatch::bind_ty<llvm::Value>>'
23
Returning without writing to 'Op.VR'
1598}
1599
1600template <typename OpTy>
1601inline match_combine_or<CastClass_match<OpTy, Instruction::SExt>, OpTy>
1602m_SExtOrSelf(const OpTy &Op) {
1603 return m_CombineOr(m_SExt(Op), Op);
1604}
1605
1606template <typename OpTy>
1607inline match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1608 CastClass_match<OpTy, Instruction::SExt>>
1609m_ZExtOrSExt(const OpTy &Op) {
1610 return m_CombineOr(m_ZExt(Op), m_SExt(Op));
1611}
1612
1613template <typename OpTy>
1614inline match_combine_or<
1615 match_combine_or<CastClass_match<OpTy, Instruction::ZExt>,
1616 CastClass_match<OpTy, Instruction::SExt>>,
1617 OpTy>
1618m_ZExtOrSExtOrSelf(const OpTy &Op) {
1619 return m_CombineOr(m_ZExtOrSExt(Op), Op);
1620}
1621
1622template <typename OpTy>
1623inline CastClass_match<OpTy, Instruction::UIToFP> m_UIToFP(const OpTy &Op) {
1624 return CastClass_match<OpTy, Instruction::UIToFP>(Op);
1625}
1626
1627template <typename OpTy>
1628inline CastClass_match<OpTy, Instruction::SIToFP> m_SIToFP(const OpTy &Op) {
1629 return CastClass_match<OpTy, Instruction::SIToFP>(Op);
1630}
1631
1632template <typename OpTy>
1633inline CastClass_match<OpTy, Instruction::FPToUI> m_FPToUI(const OpTy &Op) {
1634 return CastClass_match<OpTy, Instruction::FPToUI>(Op);
1635}
1636
1637template <typename OpTy>
1638inline CastClass_match<OpTy, Instruction::FPToSI> m_FPToSI(const OpTy &Op) {
1639 return CastClass_match<OpTy, Instruction::FPToSI>(Op);
1640}
1641
1642template <typename OpTy>
1643inline CastClass_match<OpTy, Instruction::FPTrunc> m_FPTrunc(const OpTy &Op) {
1644 return CastClass_match<OpTy, Instruction::FPTrunc>(Op);
1645}
1646
1647template <typename OpTy>
1648inline CastClass_match<OpTy, Instruction::FPExt> m_FPExt(const OpTy &Op) {
1649 return CastClass_match<OpTy, Instruction::FPExt>(Op);
1650}
1651
1652//===----------------------------------------------------------------------===//
1653// Matchers for control flow.
1654//
1655
1656struct br_match {
1657 BasicBlock *&Succ;
1658
1659 br_match(BasicBlock *&Succ) : Succ(Succ) {}
1660
1661 template <typename OpTy> bool match(OpTy *V) {
1662 if (auto *BI = dyn_cast<BranchInst>(V))
1663 if (BI->isUnconditional()) {
1664 Succ = BI->getSuccessor(0);
1665 return true;
1666 }
1667 return false;
1668 }
1669};
1670
1671inline br_match m_UnconditionalBr(BasicBlock *&Succ) { return br_match(Succ); }
1672
1673template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
1674struct brc_match {
1675 Cond_t Cond;
1676 TrueBlock_t T;
1677 FalseBlock_t F;
1678
1679 brc_match(const Cond_t &C, const TrueBlock_t &t, const FalseBlock_t &f)
1680 : Cond(C), T(t), F(f) {}
1681
1682 template <typename OpTy> bool match(OpTy *V) {
1683 if (auto *BI = dyn_cast<BranchInst>(V))
1684 if (BI->isConditional() && Cond.match(BI->getCondition()))
1685 return T.match(BI->getSuccessor(0)) && F.match(BI->getSuccessor(1));
1686 return false;
1687 }
1688};
1689
1690template <typename Cond_t>
1691inline brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>
1692m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F) {
1693 return brc_match<Cond_t, bind_ty<BasicBlock>, bind_ty<BasicBlock>>(
1694 C, m_BasicBlock(T), m_BasicBlock(F));
1695}
1696
1697template <typename Cond_t, typename TrueBlock_t, typename FalseBlock_t>
1698inline brc_match<Cond_t, TrueBlock_t, FalseBlock_t>
1699m_Br(const Cond_t &C, const TrueBlock_t &T, const FalseBlock_t &F) {
1700 return brc_match<Cond_t, TrueBlock_t, FalseBlock_t>(C, T, F);
1701}
1702
1703//===----------------------------------------------------------------------===//
1704// Matchers for max/min idioms, eg: "select (sgt x, y), x, y" -> smax(x,y).
1705//
1706
1707template <typename CmpInst_t, typename LHS_t, typename RHS_t, typename Pred_t,
1708 bool Commutable = false>
1709struct MaxMin_match {
1710 LHS_t L;
1711 RHS_t R;
1712
1713 // The evaluation order is always stable, regardless of Commutability.
1714 // The LHS is always matched first.
1715 MaxMin_match(const LHS_t &LHS, const RHS_t &RHS) : L(LHS), R(RHS) {}
1716
1717 template <typename OpTy> bool match(OpTy *V) {
1718 if (auto *II = dyn_cast<IntrinsicInst>(V)) {
1719 Intrinsic::ID IID = II->getIntrinsicID();
1720 if ((IID == Intrinsic::smax && Pred_t::match(ICmpInst::ICMP_SGT)) ||
1721 (IID == Intrinsic::smin && Pred_t::match(ICmpInst::ICMP_SLT)) ||
1722 (IID == Intrinsic::umax && Pred_t::match(ICmpInst::ICMP_UGT)) ||
1723 (IID == Intrinsic::umin && Pred_t::match(ICmpInst::ICMP_ULT))) {
1724 Value *LHS = II->getOperand(0), *RHS = II->getOperand(1);
1725 return (L.match(LHS) && R.match(RHS)) ||
1726 (Commutable && L.match(RHS) && R.match(LHS));
1727 }
1728 }
1729 // Look for "(x pred y) ? x : y" or "(x pred y) ? y : x".
1730 auto *SI = dyn_cast<SelectInst>(V);
1731 if (!SI)
1732 return false;
1733 auto *Cmp = dyn_cast<CmpInst_t>(SI->getCondition());
1734 if (!Cmp)
1735 return false;
1736 // At this point we have a select conditioned on a comparison. Check that
1737 // it is the values returned by the select that are being compared.
1738 Value *TrueVal = SI->getTrueValue();
1739 Value *FalseVal = SI->getFalseValue();
1740 Value *LHS = Cmp->getOperand(0);
1741 Value *RHS = Cmp->getOperand(1);
1742 if ((TrueVal != LHS || FalseVal != RHS) &&
1743 (TrueVal != RHS || FalseVal != LHS))
1744 return false;
1745 typename CmpInst_t::Predicate Pred =
1746 LHS == TrueVal ? Cmp->getPredicate() : Cmp->getInversePredicate();
1747 // Does "(x pred y) ? x : y" represent the desired max/min operation?
1748 if (!Pred_t::match(Pred))
1749 return false;
1750 // It does! Bind the operands.
1751 return (L.match(LHS) && R.match(RHS)) ||
1752 (Commutable && L.match(RHS) && R.match(LHS));
1753 }
1754};
1755
1756/// Helper class for identifying signed max predicates.
1757struct smax_pred_ty {
1758 static bool match(ICmpInst::Predicate Pred) {
1759 return Pred == CmpInst::ICMP_SGT || Pred == CmpInst::ICMP_SGE;
1760 }
1761};
1762
1763/// Helper class for identifying signed min predicates.
1764struct smin_pred_ty {
1765 static bool match(ICmpInst::Predicate Pred) {
1766 return Pred == CmpInst::ICMP_SLT || Pred == CmpInst::ICMP_SLE;
1767 }
1768};
1769
1770/// Helper class for identifying unsigned max predicates.
1771struct umax_pred_ty {
1772 static bool match(ICmpInst::Predicate Pred) {
1773 return Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE;
1774 }
1775};
1776
1777/// Helper class for identifying unsigned min predicates.
1778struct umin_pred_ty {
1779 static bool match(ICmpInst::Predicate Pred) {
1780 return Pred == CmpInst::ICMP_ULT || Pred == CmpInst::ICMP_ULE;
1781 }
1782};
1783
1784/// Helper class for identifying ordered max predicates.
1785struct ofmax_pred_ty {
1786 static bool match(FCmpInst::Predicate Pred) {
1787 return Pred == CmpInst::FCMP_OGT || Pred == CmpInst::FCMP_OGE;
1788 }
1789};
1790
1791/// Helper class for identifying ordered min predicates.
1792struct ofmin_pred_ty {
1793 static bool match(FCmpInst::Predicate Pred) {
1794 return Pred == CmpInst::FCMP_OLT || Pred == CmpInst::FCMP_OLE;
1795 }
1796};
1797
1798/// Helper class for identifying unordered max predicates.
1799struct ufmax_pred_ty {
1800 static bool match(FCmpInst::Predicate Pred) {
1801 return Pred == CmpInst::FCMP_UGT || Pred == CmpInst::FCMP_UGE;
1802 }
1803};
1804
1805/// Helper class for identifying unordered min predicates.
1806struct ufmin_pred_ty {
1807 static bool match(FCmpInst::Predicate Pred) {
1808 return Pred == CmpInst::FCMP_ULT || Pred == CmpInst::FCMP_ULE;
1809 }
1810};
1811
1812template <typename LHS, typename RHS>
1813inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty> m_SMax(const LHS &L,
1814 const RHS &R) {
1815 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>(L, R);
1816}
1817
1818template <typename LHS, typename RHS>
1819inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty> m_SMin(const LHS &L,
1820 const RHS &R) {
1821 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>(L, R);
1822}
1823
1824template <typename LHS, typename RHS>
1825inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty> m_UMax(const LHS &L,
1826 const RHS &R) {
1827 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>(L, R);
1828}
1829
1830template <typename LHS, typename RHS>
1831inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty> m_UMin(const LHS &L,
1832 const RHS &R) {
1833 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>(L, R);
1834}
1835
1836template <typename LHS, typename RHS>
1837inline match_combine_or<
1838 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty>,
1839 MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty>>,
1840 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty>,
1841 MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty>>>
1842m_MaxOrMin(const LHS &L, const RHS &R) {
1843 return m_CombineOr(m_CombineOr(m_SMax(L, R), m_SMin(L, R)),
1844 m_CombineOr(m_UMax(L, R), m_UMin(L, R)));
1845}
1846
1847/// Match an 'ordered' floating point maximum function.
1848/// Floating point has one special value 'NaN'. Therefore, there is no total
1849/// order. However, if we can ignore the 'NaN' value (for example, because of a
1850/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1851/// semantics. In the presence of 'NaN' we have to preserve the original
1852/// select(fcmp(ogt/ge, L, R), L, R) semantics matched by this predicate.
1853///
1854/// max(L, R) iff L and R are not NaN
1855/// m_OrdFMax(L, R) = R iff L or R are NaN
1856template <typename LHS, typename RHS>
1857inline MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty> m_OrdFMax(const LHS &L,
1858 const RHS &R) {
1859 return MaxMin_match<FCmpInst, LHS, RHS, ofmax_pred_ty>(L, R);
1860}
1861
1862/// Match an 'ordered' floating point minimum function.
1863/// Floating point has one special value 'NaN'. Therefore, there is no total
1864/// order. However, if we can ignore the 'NaN' value (for example, because of a
1865/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1866/// semantics. In the presence of 'NaN' we have to preserve the original
1867/// select(fcmp(olt/le, L, R), L, R) semantics matched by this predicate.
1868///
1869/// min(L, R) iff L and R are not NaN
1870/// m_OrdFMin(L, R) = R iff L or R are NaN
1871template <typename LHS, typename RHS>
1872inline MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty> m_OrdFMin(const LHS &L,
1873 const RHS &R) {
1874 return MaxMin_match<FCmpInst, LHS, RHS, ofmin_pred_ty>(L, R);
1875}
1876
1877/// Match an 'unordered' floating point maximum function.
1878/// Floating point has one special value 'NaN'. Therefore, there is no total
1879/// order. However, if we can ignore the 'NaN' value (for example, because of a
1880/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'maximum'
1881/// semantics. In the presence of 'NaN' we have to preserve the original
1882/// select(fcmp(ugt/ge, L, R), L, R) semantics matched by this predicate.
1883///
1884/// max(L, R) iff L and R are not NaN
1885/// m_UnordFMax(L, R) = L iff L or R are NaN
1886template <typename LHS, typename RHS>
1887inline MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>
1888m_UnordFMax(const LHS &L, const RHS &R) {
1889 return MaxMin_match<FCmpInst, LHS, RHS, ufmax_pred_ty>(L, R);
1890}
1891
1892/// Match an 'unordered' floating point minimum function.
1893/// Floating point has one special value 'NaN'. Therefore, there is no total
1894/// order. However, if we can ignore the 'NaN' value (for example, because of a
1895/// 'no-nans-float-math' flag) a combination of a fcmp and select has 'minimum'
1896/// semantics. In the presence of 'NaN' we have to preserve the original
1897/// select(fcmp(ult/le, L, R), L, R) semantics matched by this predicate.
1898///
1899/// min(L, R) iff L and R are not NaN
1900/// m_UnordFMin(L, R) = L iff L or R are NaN
1901template <typename LHS, typename RHS>
1902inline MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>
1903m_UnordFMin(const LHS &L, const RHS &R) {
1904 return MaxMin_match<FCmpInst, LHS, RHS, ufmin_pred_ty>(L, R);
1905}
1906
1907//===----------------------------------------------------------------------===//
1908// Matchers for overflow check patterns: e.g. (a + b) u< a, (a ^ -1) <u b
1909// Note that S might be matched to other instructions than AddInst.
1910//
1911
1912template <typename LHS_t, typename RHS_t, typename Sum_t>
1913struct UAddWithOverflow_match {
1914 LHS_t L;
1915 RHS_t R;
1916 Sum_t S;
1917
1918 UAddWithOverflow_match(const LHS_t &L, const RHS_t &R, const Sum_t &S)
1919 : L(L), R(R), S(S) {}
1920
1921 template <typename OpTy> bool match(OpTy *V) {
1922 Value *ICmpLHS, *ICmpRHS;
1923 ICmpInst::Predicate Pred;
1924 if (!m_ICmp(Pred, m_Value(ICmpLHS), m_Value(ICmpRHS)).match(V))
1925 return false;
1926
1927 Value *AddLHS, *AddRHS;
1928 auto AddExpr = m_Add(m_Value(AddLHS), m_Value(AddRHS));
1929
1930 // (a + b) u< a, (a + b) u< b
1931 if (Pred == ICmpInst::ICMP_ULT)
1932 if (AddExpr.match(ICmpLHS) && (ICmpRHS == AddLHS || ICmpRHS == AddRHS))
1933 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1934
1935 // a >u (a + b), b >u (a + b)
1936 if (Pred == ICmpInst::ICMP_UGT)
1937 if (AddExpr.match(ICmpRHS) && (ICmpLHS == AddLHS || ICmpLHS == AddRHS))
1938 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1939
1940 Value *Op1;
1941 auto XorExpr = m_OneUse(m_Xor(m_Value(Op1), m_AllOnes()));
1942 // (a ^ -1) <u b
1943 if (Pred == ICmpInst::ICMP_ULT) {
1944 if (XorExpr.match(ICmpLHS))
1945 return L.match(Op1) && R.match(ICmpRHS) && S.match(ICmpLHS);
1946 }
1947 // b > u (a ^ -1)
1948 if (Pred == ICmpInst::ICMP_UGT) {
1949 if (XorExpr.match(ICmpRHS))
1950 return L.match(Op1) && R.match(ICmpLHS) && S.match(ICmpRHS);
1951 }
1952
1953 // Match special-case for increment-by-1.
1954 if (Pred == ICmpInst::ICMP_EQ) {
1955 // (a + 1) == 0
1956 // (1 + a) == 0
1957 if (AddExpr.match(ICmpLHS) && m_ZeroInt().match(ICmpRHS) &&
1958 (m_One().match(AddLHS) || m_One().match(AddRHS)))
1959 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpLHS);
1960 // 0 == (a + 1)
1961 // 0 == (1 + a)
1962 if (m_ZeroInt().match(ICmpLHS) && AddExpr.match(ICmpRHS) &&
1963 (m_One().match(AddLHS) || m_One().match(AddRHS)))
1964 return L.match(AddLHS) && R.match(AddRHS) && S.match(ICmpRHS);
1965 }
1966
1967 return false;
1968 }
1969};
1970
1971/// Match an icmp instruction checking for unsigned overflow on addition.
1972///
1973/// S is matched to the addition whose result is being checked for overflow, and
1974/// L and R are matched to the LHS and RHS of S.
1975template <typename LHS_t, typename RHS_t, typename Sum_t>
1976UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>
1977m_UAddWithOverflow(const LHS_t &L, const RHS_t &R, const Sum_t &S) {
1978 return UAddWithOverflow_match<LHS_t, RHS_t, Sum_t>(L, R, S);
1979}
1980
1981template <typename Opnd_t> struct Argument_match {
1982 unsigned OpI;
1983 Opnd_t Val;
1984
1985 Argument_match(unsigned OpIdx, const Opnd_t &V) : OpI(OpIdx), Val(V) {}
1986
1987 template <typename OpTy> bool match(OpTy *V) {
1988 // FIXME: Should likely be switched to use `CallBase`.
1989 if (const auto *CI = dyn_cast<CallInst>(V))
1990 return Val.match(CI->getArgOperand(OpI));
1991 return false;
1992 }
1993};
1994
1995/// Match an argument.
1996template <unsigned OpI, typename Opnd_t>
1997inline Argument_match<Opnd_t> m_Argument(const Opnd_t &Op) {
1998 return Argument_match<Opnd_t>(OpI, Op);
1999}
2000
2001/// Intrinsic matchers.
2002struct IntrinsicID_match {
2003 unsigned ID;
2004
2005 IntrinsicID_match(Intrinsic::ID IntrID) : ID(IntrID) {}
2006
2007 template <typename OpTy> bool match(OpTy *V) {
2008 if (const auto *CI = dyn_cast<CallInst>(V))
2009 if (const auto *F = CI->getCalledFunction())
2010 return F->getIntrinsicID() == ID;
2011 return false;
2012 }
2013};
2014
2015/// Intrinsic matches are combinations of ID matchers, and argument
2016/// matchers. Higher arity matcher are defined recursively in terms of and-ing
2017/// them with lower arity matchers. Here's some convenient typedefs for up to
2018/// several arguments, and more can be added as needed
2019template <typename T0 = void, typename T1 = void, typename T2 = void,
2020 typename T3 = void, typename T4 = void, typename T5 = void,
2021 typename T6 = void, typename T7 = void, typename T8 = void,
2022 typename T9 = void, typename T10 = void>
2023struct m_Intrinsic_Ty;
2024template <typename T0> struct m_Intrinsic_Ty<T0> {
2025 using Ty = match_combine_and<IntrinsicID_match, Argument_match<T0>>;
2026};
2027template <typename T0, typename T1> struct m_Intrinsic_Ty<T0, T1> {
2028 using Ty =
2029 match_combine_and<typename m_Intrinsic_Ty<T0>::Ty, Argument_match<T1>>;
2030};
2031template <typename T0, typename T1, typename T2>
2032struct m_Intrinsic_Ty<T0, T1, T2> {
2033 using Ty =
2034 match_combine_and<typename m_Intrinsic_Ty<T0, T1>::Ty,
2035 Argument_match<T2>>;
2036};
2037template <typename T0, typename T1, typename T2, typename T3>
2038struct m_Intrinsic_Ty<T0, T1, T2, T3> {
2039 using Ty =
2040 match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2>::Ty,
2041 Argument_match<T3>>;
2042};
2043
2044template <typename T0, typename T1, typename T2, typename T3, typename T4>
2045struct m_Intrinsic_Ty<T0, T1, T2, T3, T4> {
2046 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty,
2047 Argument_match<T4>>;
2048};
2049
2050template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
2051struct m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5> {
2052 using Ty = match_combine_and<typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty,
2053 Argument_match<T5>>;
2054};
2055
2056/// Match intrinsic calls like this:
2057/// m_Intrinsic<Intrinsic::fabs>(m_Value(X))
2058template <Intrinsic::ID IntrID> inline IntrinsicID_match m_Intrinsic() {
2059 return IntrinsicID_match(IntrID);
2060}
2061
2062template <Intrinsic::ID IntrID, typename T0>
2063inline typename m_Intrinsic_Ty<T0>::Ty m_Intrinsic(const T0 &Op0) {
2064 return m_CombineAnd(m_Intrinsic<IntrID>(), m_Argument<0>(Op0));
2065}
2066
2067template <Intrinsic::ID IntrID, typename T0, typename T1>
2068inline typename m_Intrinsic_Ty<T0, T1>::Ty m_Intrinsic(const T0 &Op0,
2069 const T1 &Op1) {
2070 return m_CombineAnd(m_Intrinsic<IntrID>(Op0), m_Argument<1>(Op1));
2071}
2072
2073template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2>
2074inline typename m_Intrinsic_Ty<T0, T1, T2>::Ty
2075m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2) {
2076 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1), m_Argument<2>(Op2));
2077}
2078
2079template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2080 typename T3>
2081inline typename m_Intrinsic_Ty<T0, T1, T2, T3>::Ty
2082m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3) {
2083 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2), m_Argument<3>(Op3));
2084}
2085
2086template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2087 typename T3, typename T4>
2088inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4>::Ty
2089m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2090 const T4 &Op4) {
2091 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3),
2092 m_Argument<4>(Op4));
2093}
2094
2095template <Intrinsic::ID IntrID, typename T0, typename T1, typename T2,
2096 typename T3, typename T4, typename T5>
2097inline typename m_Intrinsic_Ty<T0, T1, T2, T3, T4, T5>::Ty
2098m_Intrinsic(const T0 &Op0, const T1 &Op1, const T2 &Op2, const T3 &Op3,
2099 const T4 &Op4, const T5 &Op5) {
2100 return m_CombineAnd(m_Intrinsic<IntrID>(Op0, Op1, Op2, Op3, Op4),
2101 m_Argument<5>(Op5));
2102}
2103
2104// Helper intrinsic matching specializations.
2105template <typename Opnd0>
2106inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BitReverse(const Opnd0 &Op0) {
2107 return m_Intrinsic<Intrinsic::bitreverse>(Op0);
2108}
2109
2110template <typename Opnd0>
2111inline typename m_Intrinsic_Ty<Opnd0>::Ty m_BSwap(const Opnd0 &Op0) {
2112 return m_Intrinsic<Intrinsic::bswap>(Op0);
2113}
2114
2115template <typename Opnd0>
2116inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FAbs(const Opnd0 &Op0) {
2117 return m_Intrinsic<Intrinsic::fabs>(Op0);
2118}
2119
2120template <typename Opnd0>
2121inline typename m_Intrinsic_Ty<Opnd0>::Ty m_FCanonicalize(const Opnd0 &Op0) {
2122 return m_Intrinsic<Intrinsic::canonicalize>(Op0);
2123}
2124
2125template <typename Opnd0, typename Opnd1>
2126inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMin(const Opnd0 &Op0,
2127 const Opnd1 &Op1) {
2128 return m_Intrinsic<Intrinsic::minnum>(Op0, Op1);
2129}
2130
2131template <typename Opnd0, typename Opnd1>
2132inline typename m_Intrinsic_Ty<Opnd0, Opnd1>::Ty m_FMax(const Opnd0 &Op0,
2133 const Opnd1 &Op1) {
2134 return m_Intrinsic<Intrinsic::maxnum>(Op0, Op1);
2135}
2136
2137template <typename Opnd0, typename Opnd1, typename Opnd2>
2138inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
2139m_FShl(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2140 return m_Intrinsic<Intrinsic::fshl>(Op0, Op1, Op2);
2141}
2142
2143template <typename Opnd0, typename Opnd1, typename Opnd2>
2144inline typename m_Intrinsic_Ty<Opnd0, Opnd1, Opnd2>::Ty
2145m_FShr(const Opnd0 &Op0, const Opnd1 &Op1, const Opnd2 &Op2) {
2146 return m_Intrinsic<Intrinsic::fshr>(Op0, Op1, Op2);
2147}
2148
2149//===----------------------------------------------------------------------===//
2150// Matchers for two-operands operators with the operators in either order
2151//
2152
2153/// Matches a BinaryOperator with LHS and RHS in either order.
2154template <typename LHS, typename RHS>
2155inline AnyBinaryOp_match<LHS, RHS, true> m_c_BinOp(const LHS &L, const RHS &R) {
2156 return AnyBinaryOp_match<LHS, RHS, true>(L, R);
2157}
2158
2159/// Matches an ICmp with a predicate over LHS and RHS in either order.
2160/// Swaps the predicate if operands are commuted.
2161template <typename LHS, typename RHS>
2162inline CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>
2163m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R) {
2164 return CmpClass_match<LHS, RHS, ICmpInst, ICmpInst::Predicate, true>(Pred, L,
2165 R);
2166}
2167
2168/// Matches a Add with LHS and RHS in either order.
2169template <typename LHS, typename RHS>
2170inline BinaryOp_match<LHS, RHS, Instruction::Add, true> m_c_Add(const LHS &L,
2171 const RHS &R) {
2172 return BinaryOp_match<LHS, RHS, Instruction::Add, true>(L, R);
2173}
2174
2175/// Matches a Mul with LHS and RHS in either order.
2176template <typename LHS, typename RHS>
2177inline BinaryOp_match<LHS, RHS, Instruction::Mul, true> m_c_Mul(const LHS &L,
2178 const RHS &R) {
2179 return BinaryOp_match<LHS, RHS, Instruction::Mul, true>(L, R);
2180}
2181
2182/// Matches an And with LHS and RHS in either order.
2183template <typename LHS, typename RHS>
2184inline BinaryOp_match<LHS, RHS, Instruction::And, true> m_c_And(const LHS &L,
2185 const RHS &R) {
2186 return BinaryOp_match<LHS, RHS, Instruction::And, true>(L, R);
2187}
2188
2189/// Matches an Or with LHS and RHS in either order.
2190template <typename LHS, typename RHS>
2191inline BinaryOp_match<LHS, RHS, Instruction::Or, true> m_c_Or(const LHS &L,
2192 const RHS &R) {
2193 return BinaryOp_match<LHS, RHS, Instruction::Or, true>(L, R);
2194}
2195
2196/// Matches an Xor with LHS and RHS in either order.
2197template <typename LHS, typename RHS>
2198inline BinaryOp_match<LHS, RHS, Instruction::Xor, true> m_c_Xor(const LHS &L,
2199 const RHS &R) {
2200 return BinaryOp_match<LHS, RHS, Instruction::Xor, true>(L, R);
2201}
2202
2203/// Matches a 'Neg' as 'sub 0, V'.
2204template <typename ValTy>
2205inline BinaryOp_match<cst_pred_ty<is_zero_int>, ValTy, Instruction::Sub>
2206m_Neg(const ValTy &V) {
2207 return m_Sub(m_ZeroInt(), V);
2208}
2209
2210/// Matches a 'Neg' as 'sub nsw 0, V'.
2211template <typename ValTy>
2212inline OverflowingBinaryOp_match<cst_pred_ty<is_zero_int>, ValTy,
2213 Instruction::Sub,
2214 OverflowingBinaryOperator::NoSignedWrap>
2215m_NSWNeg(const ValTy &V) {
2216 return m_NSWSub(m_ZeroInt(), V);
2217}
2218
2219/// Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
2220template <typename ValTy>
2221inline BinaryOp_match<ValTy, cst_pred_ty<is_all_ones>, Instruction::Xor, true>
2222m_Not(const ValTy &V) {
2223 return m_c_Xor(V, m_AllOnes());
2224}
2225
2226/// Matches an SMin with LHS and RHS in either order.
2227template <typename LHS, typename RHS>
2228inline MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>
2229m_c_SMin(const LHS &L, const RHS &R) {
2230 return MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>(L, R);
2231}
2232/// Matches an SMax with LHS and RHS in either order.
2233template <typename LHS, typename RHS>
2234inline MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>
2235m_c_SMax(const LHS &L, const RHS &R) {
2236 return MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>(L, R);
2237}
2238/// Matches a UMin with LHS and RHS in either order.
2239template <typename LHS, typename RHS>
2240inline MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>
2241m_c_UMin(const LHS &L, const RHS &R) {
2242 return MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>(L, R);
2243}
2244/// Matches a UMax with LHS and RHS in either order.
2245template <typename LHS, typename RHS>
2246inline MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>
2247m_c_UMax(const LHS &L, const RHS &R) {
2248 return MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>(L, R);
2249}
2250
2251template <typename LHS, typename RHS>
2252inline match_combine_or<
2253 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, smax_pred_ty, true>,
2254 MaxMin_match<ICmpInst, LHS, RHS, smin_pred_ty, true>>,
2255 match_combine_or<MaxMin_match<ICmpInst, LHS, RHS, umax_pred_ty, true>,
2256 MaxMin_match<ICmpInst, LHS, RHS, umin_pred_ty, true>>>
2257m_c_MaxOrMin(const LHS &L, const RHS &R) {
2258 return m_CombineOr(m_CombineOr(m_c_SMax(L, R), m_c_SMin(L, R)),
2259 m_CombineOr(m_c_UMax(L, R), m_c_UMin(L, R)));
2260}
2261
2262/// Matches FAdd with LHS and RHS in either order.
2263template <typename LHS, typename RHS>
2264inline BinaryOp_match<LHS, RHS, Instruction::FAdd, true>
2265m_c_FAdd(const LHS &L, const RHS &R) {
2266 return BinaryOp_match<LHS, RHS, Instruction::FAdd, true>(L, R);
2267}
2268
2269/// Matches FMul with LHS and RHS in either order.
2270template <typename LHS, typename RHS>
2271inline BinaryOp_match<LHS, RHS, Instruction::FMul, true>
2272m_c_FMul(const LHS &L, const RHS &R) {
2273 return BinaryOp_match<LHS, RHS, Instruction::FMul, true>(L, R);
2274}
2275
2276template <typename Opnd_t> struct Signum_match {
2277 Opnd_t Val;
2278 Signum_match(const Opnd_t &V) : Val(V) {}
2279
2280 template <typename OpTy> bool match(OpTy *V) {
2281 unsigned TypeSize = V->getType()->getScalarSizeInBits();
2282 if (TypeSize == 0)
2283 return false;
2284
2285 unsigned ShiftWidth = TypeSize - 1;
2286 Value *OpL = nullptr, *OpR = nullptr;
2287
2288 // This is the representation of signum we match:
2289 //
2290 // signum(x) == (x >> 63) | (-x >>u 63)
2291 //
2292 // An i1 value is its own signum, so it's correct to match
2293 //
2294 // signum(x) == (x >> 0) | (-x >>u 0)
2295 //
2296 // for i1 values.
2297
2298 auto LHS = m_AShr(m_Value(OpL), m_SpecificInt(ShiftWidth));
2299 auto RHS = m_LShr(m_Neg(m_Value(OpR)), m_SpecificInt(ShiftWidth));
2300 auto Signum = m_Or(LHS, RHS);
2301
2302 return Signum.match(V) && OpL == OpR && Val.match(OpL);
2303 }
2304};
2305
2306/// Matches a signum pattern.
2307///
2308/// signum(x) =
2309/// x > 0 -> 1
2310/// x == 0 -> 0
2311/// x < 0 -> -1
2312template <typename Val_t> inline Signum_match<Val_t> m_Signum(const Val_t &V) {
2313 return Signum_match<Val_t>(V);
2314}
2315
2316template <int Ind, typename Opnd_t> struct ExtractValue_match {
2317 Opnd_t Val;
2318 ExtractValue_match(const Opnd_t &V) : Val(V) {}
2319
2320 template <typename OpTy> bool match(OpTy *V) {
2321 if (auto *I = dyn_cast<ExtractValueInst>(V)) {
2322 // If Ind is -1, don't inspect indices
2323 if (Ind != -1 &&
2324 !(I->getNumIndices() == 1 && I->getIndices()[0] == (unsigned)Ind))
2325 return false;
2326 return Val.match(I->getAggregateOperand());
2327 }
2328 return false;
2329 }
2330};
2331
2332/// Match a single index ExtractValue instruction.
2333/// For example m_ExtractValue<1>(...)
2334template <int Ind, typename Val_t>
2335inline ExtractValue_match<Ind, Val_t> m_ExtractValue(const Val_t &V) {
2336 return ExtractValue_match<Ind, Val_t>(V);
2337}
2338
2339/// Match an ExtractValue instruction with any index.
2340/// For example m_ExtractValue(...)
2341template <typename Val_t>
2342inline ExtractValue_match<-1, Val_t> m_ExtractValue(const Val_t &V) {
2343 return ExtractValue_match<-1, Val_t>(V);
2344}
2345
2346/// Matcher for a single index InsertValue instruction.
2347template <int Ind, typename T0, typename T1> struct InsertValue_match {
2348 T0 Op0;
2349 T1 Op1;
2350
2351 InsertValue_match(const T0 &Op0, const T1 &Op1) : Op0(Op0), Op1(Op1) {}
2352
2353 template <typename OpTy> bool match(OpTy *V) {
2354 if (auto *I = dyn_cast<InsertValueInst>(V)) {
2355 return Op0.match(I->getOperand(0)) && Op1.match(I->getOperand(1)) &&
2356 I->getNumIndices() == 1 && Ind == I->getIndices()[0];
2357 }
2358 return false;
2359 }
2360};
2361
2362/// Matches a single index InsertValue instruction.
2363template <int Ind, typename Val_t, typename Elt_t>
2364inline InsertValue_match<Ind, Val_t, Elt_t> m_InsertValue(const Val_t &Val,
2365 const Elt_t &Elt) {
2366 return InsertValue_match<Ind, Val_t, Elt_t>(Val, Elt);
2367}
2368
2369/// Matches patterns for `vscale`. This can either be a call to `llvm.vscale` or
2370/// the constant expression
2371/// `ptrtoint(gep <vscale x 1 x i8>, <vscale x 1 x i8>* null, i32 1>`
2372/// under the right conditions determined by DataLayout.
2373struct VScaleVal_match {
2374private:
2375 template <typename Base, typename Offset>
2376 inline BinaryOp_match<Base, Offset, Instruction::GetElementPtr>
2377 m_OffsetGep(const Base &B, const Offset &O) {
2378 return BinaryOp_match<Base, Offset, Instruction::GetElementPtr>(B, O);
2379 }
2380
2381public:
2382 const DataLayout &DL;
2383 VScaleVal_match(const DataLayout &DL) : DL(DL) {}
2384
2385 template <typename ITy> bool match(ITy *V) {
2386 if (m_Intrinsic<Intrinsic::vscale>().match(V))
2387 return true;
2388
2389 if (m_PtrToInt(m_OffsetGep(m_Zero(), m_SpecificInt(1))).match(V)) {
2390 Type *PtrTy = cast<Operator>(V)->getOperand(0)->getType();
2391 auto *DerefTy = PtrTy->getPointerElementType();
2392 if (isa<ScalableVectorType>(DerefTy) &&
2393 DL.getTypeAllocSizeInBits(DerefTy).getKnownMinSize() == 8)
2394 return true;
2395 }
2396
2397 return false;
2398 }
2399};
2400
2401inline VScaleVal_match m_VScale(const DataLayout &DL) {
2402 return VScaleVal_match(DL);
2403}
2404
2405template <typename LHS, typename RHS, unsigned Opcode>
2406struct LogicalOp_match {
2407 LHS L;
2408 RHS R;
2409
2410 LogicalOp_match(const LHS &L, const RHS &R) : L(L), R(R) {}
2411
2412 template <typename T> bool match(T *V) {
2413 if (auto *I = dyn_cast<Instruction>(V)) {
2414 if (!I->getType()->isIntOrIntVectorTy(1))
2415 return false;
2416
2417 if (I->getOpcode() == Opcode && L.match(I->getOperand(0)) &&
2418 R.match(I->getOperand(1)))
2419 return true;
2420
2421 if (auto *SI = dyn_cast<SelectInst>(I)) {
2422 if (Opcode == Instruction::And) {
2423 if (const auto *C = dyn_cast<Constant>(SI->getFalseValue()))
2424 if (C->isNullValue() && L.match(SI->getCondition()) &&
2425 R.match(SI->getTrueValue()))
2426 return true;
2427 } else {
2428 assert(Opcode == Instruction::Or)((Opcode == Instruction::Or) ? static_cast<void> (0) : __assert_fail
("Opcode == Instruction::Or", "/build/llvm-toolchain-snapshot-13~++20210314100619+a28facba1ccd/llvm/include/llvm/IR/PatternMatch.h"
, 2428, __PRETTY_FUNCTION__))
;
2429 if (const auto *C = dyn_cast<Constant>(SI->getTrueValue()))
2430 if (C->isOneValue() && L.match(SI->getCondition()) &&
2431 R.match(SI->getFalseValue()))
2432 return true;
2433 }
2434 }
2435 }
2436
2437 return false;
2438 }
2439};
2440
2441/// Matches L && R either in the form of L & R or L ? R : false.
2442/// Note that the latter form is poison-blocking.
2443template <typename LHS, typename RHS>
2444inline LogicalOp_match<LHS, RHS, Instruction::And>
2445m_LogicalAnd(const LHS &L, const RHS &R) {
2446 return LogicalOp_match<LHS, RHS, Instruction::And>(L, R);
2447}
2448
2449/// Matches L && R where L and R are arbitrary values.
2450inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
2451
2452/// Matches L || R either in the form of L | R or L ? true : R.
2453/// Note that the latter form is poison-blocking.
2454template <typename LHS, typename RHS>
2455inline LogicalOp_match<LHS, RHS, Instruction::Or>
2456m_LogicalOr(const LHS &L, const RHS &R) {
2457 return LogicalOp_match<LHS, RHS, Instruction::Or>(L, R);
2458}
2459
2460/// Matches L || R where L and R are arbitrary values.
2461inline auto m_LogicalOr() {
2462 return m_LogicalOr(m_Value(), m_Value());
2463}
2464
2465} // end namespace PatternMatch
2466} // end namespace llvm
2467
2468#endif // LLVM_IR_PATTERNMATCH_H