OverflowInstAnalysis.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "llvm/Analysis/OverflowInstAnalysis.h"
  14. #include "llvm/IR/Constants.h"
  15. #include "llvm/IR/Instructions.h"
  16. #include "llvm/IR/PatternMatch.h"
  17. using namespace llvm;
  18. using namespace llvm::PatternMatch;
  19. bool llvm::isCheckForZeroAndMulWithOverflow(Value *Op0, Value *Op1, bool IsAnd,
  20. Use *&Y) {
  21. ICmpInst::Predicate Pred;
  22. Value *X, *NotOp1;
  23. int XIdx;
  24. IntrinsicInst *II;
  25. if (!match(Op0, m_ICmp(Pred, m_Value(X), m_Zero())))
  26. return false;
  27. /// %Agg = call { i4, i1 } @llvm.[us]mul.with.overflow.i4(i4 %X, i4 %???)
  28. /// %V = extractvalue { i4, i1 } %Agg, 1
  29. auto matchMulOverflowCheck = [X, &II, &XIdx](Value *V) {
  30. auto *Extract = dyn_cast<ExtractValueInst>(V);
  31. // We should only be extracting the overflow bit.
  32. if (!Extract || !Extract->getIndices().equals(1))
  33. return false;
  34. II = dyn_cast<IntrinsicInst>(Extract->getAggregateOperand());
  35. if (!II ||
  36. !match(II, m_CombineOr(m_Intrinsic<Intrinsic::umul_with_overflow>(),
  37. m_Intrinsic<Intrinsic::smul_with_overflow>())))
  38. return false;
  39. if (II->getArgOperand(0) == X)
  40. XIdx = 0;
  41. else if (II->getArgOperand(1) == X)
  42. XIdx = 1;
  43. else
  44. return false;
  45. return true;
  46. };
  47. bool Matched =
  48. (IsAnd && Pred == ICmpInst::Predicate::ICMP_NE &&
  49. matchMulOverflowCheck(Op1)) ||
  50. (!IsAnd && Pred == ICmpInst::Predicate::ICMP_EQ &&
  51. match(Op1, m_Not(m_Value(NotOp1))) && matchMulOverflowCheck(NotOp1));
  52. if (!Matched)
  53. return false;
  54. Y = &II->getArgOperandUse(!XIdx);
  55. return true;
  56. }
  57. bool llvm::isCheckForZeroAndMulWithOverflow(Value *Op0, Value *Op1,
  58. bool IsAnd) {
  59. Use *Y;
  60. return isCheckForZeroAndMulWithOverflow(Op0, Op1, IsAnd, Y);
  61. }