LLVM 22.0.0git
OverflowInstAnalysis.cpp
Go to the documentation of this file.
1//==-- OverflowInstAnalysis.cpp - Utils to fold overflow insts ----*- 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 holds routines to help analyse overflow instructions
10// and fold them into constants or other overflow instructions
11//
12//===----------------------------------------------------------------------===//
13
17
18using namespace llvm;
19using namespace llvm::PatternMatch;
20
22 Use *&Y) {
23 CmpPredicate Pred;
24 Value *X, *NotOp1;
25 int XIdx;
27
28 if (!match(Op0, m_ICmp(Pred, m_Value(X), m_Zero())))
29 return false;
30
31 /// %Agg = call { i4, i1 } @llvm.[us]mul.with.overflow.i4(i4 %X, i4 %???)
32 /// %V = extractvalue { i4, i1 } %Agg, 1
33 auto matchMulOverflowCheck = [X, &II, &XIdx](Value *V) {
34 auto *Extract = dyn_cast<ExtractValueInst>(V);
35 // We should only be extracting the overflow bit.
36 if (!Extract || !Extract->getIndices().equals(1))
37 return false;
38
39 II = dyn_cast<IntrinsicInst>(Extract->getAggregateOperand());
40 if (!II ||
43 return false;
44
45 if (II->getArgOperand(0) == X)
46 XIdx = 0;
47 else if (II->getArgOperand(1) == X)
48 XIdx = 1;
49 else
50 return false;
51 return true;
52 };
53
54 bool Matched =
55 (IsAnd && Pred == ICmpInst::Predicate::ICMP_NE &&
56 matchMulOverflowCheck(Op1)) ||
57 (!IsAnd && Pred == ICmpInst::Predicate::ICMP_EQ &&
58 match(Op1, m_Not(m_Value(NotOp1))) && matchMulOverflowCheck(NotOp1));
59
60 if (!Matched)
61 return false;
62
63 Y = &II->getArgOperandUse(!XIdx);
64 return true;
65}
66
68 bool IsAnd) {
69 Use *Y;
70 return isCheckForZeroAndMulWithOverflow(Op0, Op1, IsAnd, Y);
71}
uint64_t IntrinsicInst * II
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
@ ICMP_NE
not equal
Definition InstrTypes.h:700
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
A wrapper class for inspecting calls to intrinsic functions.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
bool match(Val *V, const Pattern &P)
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
bool isCheckForZeroAndMulWithOverflow(Value *Op0, Value *Op1, bool IsAnd, Use *&Y)
Match one of the patterns up to the select/logic op: Op0 = icmp ne i4 X, 0 Agg = call { i4,...