From 790af9180354a4d4ce3c088b4f4c2a8e626e527c Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Mon, 26 Nov 2018 22:00:41 +0000 Subject: [PATCH] [InstCombine] add helper function to reduce code duplication; NFC llvm-svn: 347604 --- .../Transforms/InstCombine/InstCombineCalls.cpp | 43 ++++++++++------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index cbaa0dd..d24733b 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1837,6 +1837,17 @@ Instruction *InstCombiner::visitVACopyInst(VACopyInst &I) { return nullptr; } +static Instruction *canonicalizeConstantArg0ToArg1(CallInst &Call) { + assert(Call.getNumArgOperands() > 1 && "Need at least 2 args to swap"); + Value *Arg0 = Call.getArgOperand(0), *Arg1 = Call.getArgOperand(1); + if (isa(Arg0) && !isa(Arg1)) { + Call.setArgOperand(0, Arg1); + Call.setArgOperand(1, Arg0); + return &Call; + } + return nullptr; +} + /// CallInst simplification. This mostly only handles folding of intrinsic /// instructions. For normal calls, it allows visitCallSite to do the heavy /// lifting. @@ -2031,14 +2042,8 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { case Intrinsic::sadd_with_overflow: case Intrinsic::umul_with_overflow: case Intrinsic::smul_with_overflow: - if (isa(II->getArgOperand(0)) && - !isa(II->getArgOperand(1))) { - // Canonicalize constants into the RHS. - Value *LHS = II->getArgOperand(0); - II->setArgOperand(0, II->getArgOperand(1)); - II->setArgOperand(1, LHS); - return II; - } + if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) + return I; LLVM_FALLTHROUGH; case Intrinsic::usub_with_overflow: @@ -2060,15 +2065,10 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { case Intrinsic::maxnum: case Intrinsic::minimum: case Intrinsic::maximum: { + if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) + return I; Value *Arg0 = II->getArgOperand(0); Value *Arg1 = II->getArgOperand(1); - // Canonicalize constants to the RHS. - if (isa(Arg0) && !isa(Arg1)) { - II->setArgOperand(0, Arg1); - II->setArgOperand(1, Arg0); - return II; - } - Intrinsic::ID IID = II->getIntrinsicID(); Value *X, *Y; if (match(Arg0, m_FNeg(m_Value(X))) && match(Arg1, m_FNeg(m_Value(Y))) && @@ -2148,17 +2148,12 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) { LLVM_FALLTHROUGH; } case Intrinsic::fma: { - Value *Src0 = II->getArgOperand(0); - Value *Src1 = II->getArgOperand(1); - - // Canonicalize constant multiply operand to Src1. - if (isa(Src0) && !isa(Src1)) { - II->setArgOperand(0, Src1); - II->setArgOperand(1, Src0); - std::swap(Src0, Src1); - } + if (Instruction *I = canonicalizeConstantArg0ToArg1(CI)) + return I; // fma fneg(x), fneg(y), z -> fma x, y, z + Value *Src0 = II->getArgOperand(0); + Value *Src1 = II->getArgOperand(1); Value *X, *Y; if (match(Src0, m_FNeg(m_Value(X))) && match(Src1, m_FNeg(m_Value(Y)))) { II->setArgOperand(0, X); -- 2.7.4