LLVM  14.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 
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Instructions.h"
17 #include "llvm/IR/PatternMatch.h"
18 
19 using namespace llvm;
20 using namespace llvm::PatternMatch;
21 
23  Use *&Y) {
25  Value *X, *NotOp1;
26  int XIdx;
27  IntrinsicInst *II;
28 
29  if (!match(Op0, m_ICmp(Pred, m_Value(X), m_Zero())))
30  return false;
31 
32  /// %Agg = call { i4, i1 } @llvm.[us]mul.with.overflow.i4(i4 %X, i4 %???)
33  /// %V = extractvalue { i4, i1 } %Agg, 1
34  auto matchMulOverflowCheck = [X, &II, &XIdx](Value *V) {
35  auto *Extract = dyn_cast<ExtractValueInst>(V);
36  // We should only be extracting the overflow bit.
37  if (!Extract || !Extract->getIndices().equals(1))
38  return false;
39 
40  II = dyn_cast<IntrinsicInst>(Extract->getAggregateOperand());
41  if (!II ||
42  !match(II, m_CombineOr(m_Intrinsic<Intrinsic::umul_with_overflow>(),
43  m_Intrinsic<Intrinsic::smul_with_overflow>())))
44  return false;
45 
46  if (II->getArgOperand(0) == X)
47  XIdx = 0;
48  else if (II->getArgOperand(1) == X)
49  XIdx = 1;
50  else
51  return false;
52  return true;
53  };
54 
55  bool Matched =
56  (IsAnd && Pred == ICmpInst::Predicate::ICMP_NE &&
57  matchMulOverflowCheck(Op1)) ||
58  (!IsAnd && Pred == ICmpInst::Predicate::ICMP_EQ &&
59  match(Op1, m_Not(m_Value(NotOp1))) && matchMulOverflowCheck(NotOp1));
60 
61  if (!Matched)
62  return false;
63 
64  Y = &II->getArgOperandUse(!XIdx);
65  return true;
66 }
67 
69  bool IsAnd) {
70  Use *Y;
71  return isCheckForZeroAndMulWithOverflow(Op0, Op1, IsAnd, Y);
72 }
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::isCheckForZeroAndMulWithOverflow
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,...
Definition: OverflowInstAnalysis.cpp:22
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:720
llvm::PatternMatch::m_CombineOr
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:210
llvm::PatternMatch::m_Not
BinaryOp_match< ValTy, cst_pred_ty< is_all_ones >, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
Definition: PatternMatch.h:2280
Constants.h
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
PatternMatch.h
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
llvm::PatternMatch::m_Zero
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:535
llvm::PatternMatch::m_Value
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:76
OverflowInstAnalysis.h
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:45
Instructions.h
llvm::PatternMatch::m_ICmp
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Definition: PatternMatch.h:1404
llvm::PatternMatch
Definition: PatternMatch.h:47
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44