11977c53bSJuneyoung Lee //==-- OverflowInstAnalysis.cpp - Utils to fold overflow insts ----*- C++ -*-=// 21977c53bSJuneyoung Lee // 31977c53bSJuneyoung Lee // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 41977c53bSJuneyoung Lee // See https://llvm.org/LICENSE.txt for license information. 51977c53bSJuneyoung Lee // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 61977c53bSJuneyoung Lee // 71977c53bSJuneyoung Lee //===----------------------------------------------------------------------===// 81977c53bSJuneyoung Lee // 91977c53bSJuneyoung Lee // This file holds routines to help analyse overflow instructions 101977c53bSJuneyoung Lee // and fold them into constants or other overflow instructions 111977c53bSJuneyoung Lee // 121977c53bSJuneyoung Lee //===----------------------------------------------------------------------===// 131977c53bSJuneyoung Lee 141977c53bSJuneyoung Lee #include "llvm/Analysis/OverflowInstAnalysis.h" 151977c53bSJuneyoung Lee #include "llvm/IR/Instructions.h" 161977c53bSJuneyoung Lee #include "llvm/IR/PatternMatch.h" 171977c53bSJuneyoung Lee 181977c53bSJuneyoung Lee using namespace llvm; 191977c53bSJuneyoung Lee using namespace llvm::PatternMatch; 201977c53bSJuneyoung Lee 211977c53bSJuneyoung Lee bool llvm::isCheckForZeroAndMulWithOverflow(Value *Op0, Value *Op1, bool IsAnd, 221977c53bSJuneyoung Lee Use *&Y) { 23*4a0d53a0SRamkumar Ramachandra CmpPredicate Pred; 241977c53bSJuneyoung Lee Value *X, *NotOp1; 251977c53bSJuneyoung Lee int XIdx; 261977c53bSJuneyoung Lee IntrinsicInst *II; 271977c53bSJuneyoung Lee 281977c53bSJuneyoung Lee if (!match(Op0, m_ICmp(Pred, m_Value(X), m_Zero()))) 291977c53bSJuneyoung Lee return false; 301977c53bSJuneyoung Lee 311977c53bSJuneyoung Lee /// %Agg = call { i4, i1 } @llvm.[us]mul.with.overflow.i4(i4 %X, i4 %???) 321977c53bSJuneyoung Lee /// %V = extractvalue { i4, i1 } %Agg, 1 331977c53bSJuneyoung Lee auto matchMulOverflowCheck = [X, &II, &XIdx](Value *V) { 341977c53bSJuneyoung Lee auto *Extract = dyn_cast<ExtractValueInst>(V); 351977c53bSJuneyoung Lee // We should only be extracting the overflow bit. 361977c53bSJuneyoung Lee if (!Extract || !Extract->getIndices().equals(1)) 371977c53bSJuneyoung Lee return false; 381977c53bSJuneyoung Lee 391977c53bSJuneyoung Lee II = dyn_cast<IntrinsicInst>(Extract->getAggregateOperand()); 40d4d1caafSJuneyoung Lee if (!II || 41d4d1caafSJuneyoung Lee !match(II, m_CombineOr(m_Intrinsic<Intrinsic::umul_with_overflow>(), 421977c53bSJuneyoung Lee m_Intrinsic<Intrinsic::smul_with_overflow>()))) 431977c53bSJuneyoung Lee return false; 441977c53bSJuneyoung Lee 451977c53bSJuneyoung Lee if (II->getArgOperand(0) == X) 461977c53bSJuneyoung Lee XIdx = 0; 471977c53bSJuneyoung Lee else if (II->getArgOperand(1) == X) 481977c53bSJuneyoung Lee XIdx = 1; 491977c53bSJuneyoung Lee else 501977c53bSJuneyoung Lee return false; 511977c53bSJuneyoung Lee return true; 521977c53bSJuneyoung Lee }; 531977c53bSJuneyoung Lee 541977c53bSJuneyoung Lee bool Matched = 551977c53bSJuneyoung Lee (IsAnd && Pred == ICmpInst::Predicate::ICMP_NE && 561977c53bSJuneyoung Lee matchMulOverflowCheck(Op1)) || 571977c53bSJuneyoung Lee (!IsAnd && Pred == ICmpInst::Predicate::ICMP_EQ && 581977c53bSJuneyoung Lee match(Op1, m_Not(m_Value(NotOp1))) && matchMulOverflowCheck(NotOp1)); 591977c53bSJuneyoung Lee 601977c53bSJuneyoung Lee if (!Matched) 611977c53bSJuneyoung Lee return false; 621977c53bSJuneyoung Lee 631977c53bSJuneyoung Lee Y = &II->getArgOperandUse(!XIdx); 641977c53bSJuneyoung Lee return true; 651977c53bSJuneyoung Lee } 661977c53bSJuneyoung Lee 671977c53bSJuneyoung Lee bool llvm::isCheckForZeroAndMulWithOverflow(Value *Op0, Value *Op1, 681977c53bSJuneyoung Lee bool IsAnd) { 691977c53bSJuneyoung Lee Use *Y; 701977c53bSJuneyoung Lee return isCheckForZeroAndMulWithOverflow(Op0, Op1, IsAnd, Y); 711977c53bSJuneyoung Lee } 72