diff --git a/llvm/lib/Transforms/InstCombine/CMakeLists.txt b/llvm/lib/Transforms/InstCombine/CMakeLists.txt --- a/llvm/lib/Transforms/InstCombine/CMakeLists.txt +++ b/llvm/lib/Transforms/InstCombine/CMakeLists.txt @@ -12,6 +12,7 @@ InstCombineCompares.cpp InstCombineLoadStoreAlloca.cpp InstCombineMulDivRem.cpp + InstCombineNegator.cpp InstCombinePHI.cpp InstCombineSelect.cpp InstCombineShifts.cpp diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp --- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp @@ -1676,28 +1676,19 @@ if (Instruction *X = foldVectorBinop(I)) return X; + Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); + + // First, let's try to interpret `sub a, b` as `add a, (sub 0, b)`, + // and let's try to sink `(sub 0, b)` into `b` itself. + if (Value *NegOp1 = Negator::Negate(Op1, *this)) + return BinaryOperator::CreateAdd(NegOp1, Op0); + if (match(Op0, m_ZeroInt())) + return nullptr; // Should have been handled in Negator! + // (A*B)-(A*C) -> A*(B-C) etc if (Value *V = SimplifyUsingDistributiveLaws(I)) return replaceInstUsesWith(I, V); - // If this is a 'B = x-(-A)', change to B = x+A. - Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); - if (Value *V = dyn_castNegVal(Op1)) { - BinaryOperator *Res = BinaryOperator::CreateAdd(Op0, V); - - if (const auto *BO = dyn_cast(Op1)) { - assert(BO->getOpcode() == Instruction::Sub && - "Expected a subtraction operator!"); - if (BO->hasNoSignedWrap() && I.hasNoSignedWrap()) - Res->setHasNoSignedWrap(true); - } else { - if (cast(Op1)->isNotMinSignedValue() && I.hasNoSignedWrap()) - Res->setHasNoSignedWrap(true); - } - - return Res; - } - if (I.getType()->isIntOrIntVectorTy(1)) return BinaryOperator::CreateXor(Op0, Op1); @@ -1714,33 +1705,15 @@ if (match(Op0, m_OneUse(m_Add(m_Value(X), m_AllOnes())))) return BinaryOperator::CreateAdd(Builder.CreateNot(Op1), X); - // Y - (X + 1) --> ~X + Y - if (match(Op1, m_OneUse(m_Add(m_Value(X), m_One())))) - return BinaryOperator::CreateAdd(Builder.CreateNot(X), Op0); - - // Y - ~X --> (X + 1) + Y - if (match(Op1, m_OneUse(m_Not(m_Value(X))))) { - return BinaryOperator::CreateAdd( - Builder.CreateAdd(Op0, ConstantInt::get(I.getType(), 1)), X); - } - if (Constant *C = dyn_cast(Op0)) { - bool IsNegate = match(C, m_ZeroInt()); Value *X; - if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { - // 0 - (zext bool) --> sext bool - // C - (zext bool) --> bool ? C - 1 : C - if (IsNegate) - return CastInst::CreateSExtOrBitCast(X, I.getType()); + + // C - (zext bool) --> bool ? C - 1 : C + if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) return SelectInst::Create(X, SubOne(C), C); - } - if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) { - // 0 - (sext bool) --> zext bool - // C - (sext bool) --> bool ? C + 1 : C - if (IsNegate) - return CastInst::CreateZExtOrBitCast(X, I.getType()); + // C - (sext bool) --> bool ? C + 1 : C + if (match(Op1, m_SExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) return SelectInst::Create(X, AddOne(C), C); - } // C - ~X == X + (1+C) if (match(Op1, m_Not(m_Value(X)))) @@ -1768,55 +1741,7 @@ } const APInt *Op0C; - if (match(Op0, m_APInt(Op0C))) { - - if (Op0C->isNullValue()) { - Value *Op1Wide; - match(Op1, m_TruncOrSelf(m_Value(Op1Wide))); - bool HadTrunc = Op1Wide != Op1; - bool NoTruncOrTruncIsOneUse = !HadTrunc || Op1->hasOneUse(); - unsigned BitWidth = Op1Wide->getType()->getScalarSizeInBits(); - - Value *X; - const APInt *ShAmt; - // -(X >>u 31) -> (X >>s 31) - if (NoTruncOrTruncIsOneUse && - match(Op1Wide, m_LShr(m_Value(X), m_APInt(ShAmt))) && - *ShAmt == BitWidth - 1) { - Value *ShAmtOp = cast(Op1Wide)->getOperand(1); - Instruction *NewShift = BinaryOperator::CreateAShr(X, ShAmtOp); - NewShift->copyIRFlags(Op1Wide); - if (!HadTrunc) - return NewShift; - Builder.Insert(NewShift); - return TruncInst::CreateTruncOrBitCast(NewShift, Op1->getType()); - } - // -(X >>s 31) -> (X >>u 31) - if (NoTruncOrTruncIsOneUse && - match(Op1Wide, m_AShr(m_Value(X), m_APInt(ShAmt))) && - *ShAmt == BitWidth - 1) { - Value *ShAmtOp = cast(Op1Wide)->getOperand(1); - Instruction *NewShift = BinaryOperator::CreateLShr(X, ShAmtOp); - NewShift->copyIRFlags(Op1Wide); - if (!HadTrunc) - return NewShift; - Builder.Insert(NewShift); - return TruncInst::CreateTruncOrBitCast(NewShift, Op1->getType()); - } - - if (!HadTrunc && Op1->hasOneUse()) { - Value *LHS, *RHS; - SelectPatternFlavor SPF = matchSelectPattern(Op1, LHS, RHS).Flavor; - if (SPF == SPF_ABS || SPF == SPF_NABS) { - // This is a negate of an ABS/NABS pattern. Just swap the operands - // of the select. - cast(Op1)->swapValues(); - // Don't swap prof metadata, we didn't change the branch behavior. - return replaceInstUsesWith(I, Op1); - } - } - } - + if (match(Op0, m_APInt(Op0C)) && Op0C->isMask()) { // Turn this into a xor if LHS is 2^n-1 and the remaining bits are known // zero. if (Op0C->isMask()) { @@ -1879,70 +1804,12 @@ Y, Builder.CreateNot(Op1, Op1->getName() + ".not")); } - if (Op1->hasOneUse()) { - Value *X = nullptr, *Y = nullptr, *Z = nullptr; - Constant *C = nullptr; - - // (X - (Y - Z)) --> (X + (Z - Y)). - if (match(Op1, m_Sub(m_Value(Y), m_Value(Z)))) - return BinaryOperator::CreateAdd(Op0, - Builder.CreateSub(Z, Y, Op1->getName())); - + { // (X - (X & Y)) --> (X & ~Y) - if (match(Op1, m_c_And(m_Value(Y), m_Specific(Op0)))) - return BinaryOperator::CreateAnd(Op0, - Builder.CreateNot(Y, Y->getName() + ".not")); - - // 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow. - if (match(Op0, m_Zero())) { - Constant *Op11C; - if (match(Op1, m_SDiv(m_Value(X), m_Constant(Op11C))) && - !Op11C->containsUndefElement() && Op11C->isNotMinSignedValue() && - Op11C->isNotOneValue()) { - Instruction *BO = - BinaryOperator::CreateSDiv(X, ConstantExpr::getNeg(Op11C)); - BO->setIsExact(cast(Op1)->isExact()); - return BO; - } - } - - // 0 - (X << Y) -> (-X << Y) when X is freely negatable. - if (match(Op1, m_Shl(m_Value(X), m_Value(Y))) && match(Op0, m_Zero())) - if (Value *XNeg = dyn_castNegVal(X)) - return BinaryOperator::CreateShl(XNeg, Y); - - // Subtracting -1/0 is the same as adding 1/0: - // sub [nsw] Op0, sext(bool Y) -> add [nsw] Op0, zext(bool Y) - // 'nuw' is dropped in favor of the canonical form. - if (match(Op1, m_SExt(m_Value(Y))) && - Y->getType()->getScalarSizeInBits() == 1) { - Value *Zext = Builder.CreateZExt(Y, I.getType()); - BinaryOperator *Add = BinaryOperator::CreateAdd(Op0, Zext); - Add->setHasNoSignedWrap(I.hasNoSignedWrap()); - return Add; - } - // sub [nsw] X, zext(bool Y) -> add [nsw] X, sext(bool Y) - // 'nuw' is dropped in favor of the canonical form. - if (match(Op1, m_ZExt(m_Value(Y))) && Y->getType()->isIntOrIntVectorTy(1)) { - Value *Sext = Builder.CreateSExt(Y, I.getType()); - BinaryOperator *Add = BinaryOperator::CreateAdd(Op0, Sext); - Add->setHasNoSignedWrap(I.hasNoSignedWrap()); - return Add; - } - - // X - A*-B -> X + A*B - // X - -A*B -> X + A*B - Value *A, *B; - if (match(Op1, m_c_Mul(m_Value(A), m_Neg(m_Value(B))))) - return BinaryOperator::CreateAdd(Op0, Builder.CreateMul(A, B)); - - // X - A*C -> X + A*-C - // No need to handle commuted multiply because multiply handling will - // ensure constant will be move to the right hand side. - if (match(Op1, m_Mul(m_Value(A), m_Constant(C))) && !isa(C)) { - Value *NewMul = Builder.CreateMul(A, ConstantExpr::getNeg(C)); - return BinaryOperator::CreateAdd(Op0, NewMul); - } + Value *Y; + if (match(Op1, m_OneUse(m_c_And(m_Value(Y), m_Specific(Op0))))) + return BinaryOperator::CreateAnd( + Op0, Builder.CreateNot(Y, Y->getName() + ".not")); } { diff --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h --- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h +++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h @@ -16,6 +16,7 @@ #define LLVM_LIB_TRANSFORMS_INSTCOMBINE_INSTCOMBINEINTERNAL_H #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Statistic.h" #include "llvm/Analysis/AliasAnalysis.h" #include "llvm/Analysis/InstructionSimplify.h" #include "llvm/Analysis/TargetFolder.h" @@ -511,7 +512,7 @@ Instruction *simplifyMaskedStore(IntrinsicInst &II); Instruction *simplifyMaskedGather(IntrinsicInst &II); Instruction *simplifyMaskedScatter(IntrinsicInst &II); - + /// Transform (zext icmp) to bitwise / integer operations in order to /// eliminate it. /// @@ -1000,6 +1001,52 @@ Value *Descale(Value *Val, APInt Scale, bool &NoSignedWrap); }; +namespace { + +// As a default, let's assume that we want to be somewhat aggressive, +// and attempt to traverse up to 32 layers in attempt to sink negation. +static constexpr unsigned NegatorDefaultMaxDepth = 32; + +// Let's guesstimate that most often we will end up producing fairly small +// number of new instructions. +static constexpr unsigned NegatorMaxNewNodesSSO = 16; + +} // namespace + +class Negator final { + /// Top-to-bottom, def-to-use negated instruction tree we produced. + SmallVector NewInstructions; + + using BuilderTy = IRBuilder; + BuilderTy Builder; + + Negator(LLVMContext &C, const DataLayout &DL); + +#if LLVM_ENABLE_STATS + unsigned NumValuesVisitedInThisNegator = 0; + ~Negator(); +#endif + + using Result = std::pair /*NewInstructions*/, + Value * /*NegatedRoot*/>; + + LLVM_NODISCARD Value *visit(Value *V, unsigned Depth); + + /// Recurse depth-first and attempt to sink the negation. + /// FIXME: use worklist? + LLVM_NODISCARD Optional run(Value *Root); + + Negator(const Negator &) = delete; + Negator(Negator &&) = delete; + Negator &operator=(const Negator &) = delete; + Negator &operator=(Negator &&) = delete; + +public: + /// Attempt to negate \p Root. Retuns nullptr if negation can't be performed, + /// otherwise returns negated value. + LLVM_NODISCARD static Value *Negate(Value *Root, InstCombiner &IC); +}; + } // end namespace llvm #undef DEBUG_TYPE diff --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp @@ -0,0 +1,334 @@ +//===- InstCombineNegator.cpp -----------------------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This file implements sinking of negation into expression trees, +// as long as that can be done without increasing instruction count. +// +//===----------------------------------------------------------------------===// + +#include "InstCombineInternal.h" +#include "llvm/ADT/APInt.h" +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/None.h" +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/Statistic.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/Twine.h" +#include "llvm/ADT/iterator_range.h" +#include "llvm/Analysis/TargetFolder.h" +#include "llvm/Analysis/ValueTracking.h" +#include "llvm/IR/Constant.h" +#include "llvm/IR/Constants.h" +#include "llvm/IR/DataLayout.h" +#include "llvm/IR/DebugLoc.h" +#include "llvm/IR/DerivedTypes.h" +#include "llvm/IR/IRBuilder.h" +#include "llvm/IR/Instruction.h" +#include "llvm/IR/Instructions.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/PatternMatch.h" +#include "llvm/IR/Type.h" +#include "llvm/IR/Value.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Compiler.h" +#include "llvm/Support/DebugCounter.h" +#include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" +#include +#include + +using namespace llvm; + +#define DEBUG_TYPE "instcombine" + +STATISTIC(NegatorTotalNegationsAttempted, + "Negator: Number of negations attempted to be sinked"); +STATISTIC(NegatorNumTreesNegated, + "Negator: Number of negations successfully sinked"); +STATISTIC(NegatorMaxDepthVisited, "Negator: Maximal traversal depth ever " + "reached while attempting to sink negation"); +STATISTIC(NegatorTimesDepthLimitReached, + "Negator: How many times did the traversal depth limit was reached " + "during sinking"); +STATISTIC( + NegatorNumValuesVisited, + "Negator: Total number of values visited during attempts to sink negation"); +STATISTIC(NegatorMaxTotalValuesVisited, + "Negator: Maximal number of values ever visited while attempting to " + "sink negation"); +STATISTIC(NegatorNumInstructionsCreatedTotal, + "Negator: Number of new negated instructions created, total"); +STATISTIC(NegatorMaxInstructionsCreated, + "Negator: Maximal number of new instructions created during negation " + "attempt"); +STATISTIC(NegatorNumInstructionsNegatedSuccess, + "Negator: Number of new negated instructions created in successful " + "negation sinking attempts"); + +DEBUG_COUNTER(NegatorCounter, "instcombine-negator", + "Controls Negator transformations in InstCombine pass"); + +static cl::opt + NegatorEnabled("instcombine-negator-enabled", cl::init(true), + cl::desc("Should we attempt to sink negations?")); + +static cl::opt + NegatorMaxDepth("instcombine-negator-max-depth", + cl::init(NegatorDefaultMaxDepth), + cl::desc("What is the maximal lookup depth when trying to " + "check for viability of negation sinking.")); + +Negator::Negator(LLVMContext &C, const DataLayout &DL) + : Builder(C, TargetFolder(DL), + IRBuilderCallbackInserter([&](Instruction *I) { + ++NegatorNumInstructionsCreatedTotal; + NewInstructions.push_back(I); + })) {} + +#if LLVM_ENABLE_STATS +Negator::~Negator() { + NegatorMaxTotalValuesVisited.updateMax(NumValuesVisitedInThisNegator); +} +#endif + +// FIXME: can this be reworked into a worklist-based algorithm while preserving +// the depth-first, early bailout traversal? +LLVM_NODISCARD Value *Negator::visit(Value *V, unsigned Depth) { + NegatorMaxDepthVisited.updateMax(Depth); + ++NegatorNumValuesVisited; + +#if LLVM_ENABLE_STATS + ++NumValuesVisitedInThisNegator; +#endif + + Value *X; + + // -(-(X)) -> X. + if (match(V, m_Neg(m_Value(X)))) + return X; + + // Integral constants can be freely negated. + if (match(V, m_AnyIntegralConstant())) + return ConstantExpr::getNeg(cast(V), /*HasNUW=*/false, + /*HasNSW=*/false); + + // If we have a non-instruction, or it has other uses, then give up. + if (!isa(V) || !V->hasOneUse()) + return nullptr; + + auto *I = cast(V); + unsigned BitWidth = I->getType()->getScalarSizeInBits(); + const APInt *Op1Val; + + // We must preserve the insertion point and debug info that is set in the + // builder at the time this function is called. + InstCombiner::BuilderTy::InsertPointGuard Guard(Builder); + // And since we are trying to negate instruction I, that tells us about the + // insertion point and the debug info that we need to keep. + Builder.SetInsertPoint(I); + + // In some cases we can give the answer without further recursion. + switch (I->getOpcode()) { + case Instruction::PHI: + // `phi` is negatible if all the incoming values are negatible. We'd need to + // ensure that we won't deadloop (pr12338.ll), so let's not bother for now. + return nullptr; // FIXME: handle PHI! + case Instruction::Add: + // `inc` is always negatible. + if (match(I->getOperand(1), m_One())) + return Builder.CreateNot(I->getOperand(0), I->getName() + ".neg"); + break; + case Instruction::Sub: + // `sub` is always negatible. + return Builder.CreateSub(I->getOperand(1), I->getOperand(0), + I->getName() + ".neg"); + case Instruction::Xor: + // `not` is always negatible. + if (match(I, m_Not(m_Value(X)))) + return Builder.CreateAdd(X, ConstantInt::get(X->getType(), 1), + I->getName() + ".neg"); + break; + case Instruction::AShr: + case Instruction::LShr: + // Right-shift sign bit smear is negatible. + if (match(I->getOperand(1), m_APInt(Op1Val)) && *Op1Val == BitWidth - 1) { + Value *BO = I->getOpcode() == Instruction::AShr + ? Builder.CreateLShr(I->getOperand(0), I->getOperand(1)) + : Builder.CreateAShr(I->getOperand(0), I->getOperand(1)); + if (auto *NewInstr = dyn_cast(BO)) { + NewInstr->copyIRFlags(I); + NewInstr->setName(I->getName() + ".neg"); + } + return BO; + } + break; + case Instruction::SDiv: + // `sdiv` is negatible if divisor is not undef/INT_MIN/1. + if (auto *Op1C = dyn_cast(I->getOperand(1))) { + if (!Op1C->containsUndefElement() && Op1C->isNotMinSignedValue() && + Op1C->isNotOneValue()) { + Value *BO = + Builder.CreateSDiv(I->getOperand(0), ConstantExpr::getNeg(Op1C), + I->getName() + ".neg"); + if (auto *NewInstr = dyn_cast(BO)) + NewInstr->setIsExact(I->isExact()); + return BO; + } + } + break; + case Instruction::SExt: + // `sext` of i1 is always negatible + if (I->getOperand(0)->getType()->isIntOrIntVectorTy(1)) + return Builder.CreateZExt(I->getOperand(0), I->getType(), + I->getName() + ".neg"); + break; + case Instruction::ZExt: + // `zext` of i1 is always negatible + if (I->getOperand(0)->getType()->isIntOrIntVectorTy(1)) + return Builder.CreateSExt(I->getOperand(0), I->getType(), + I->getName() + ".neg"); + break; + default: + break; // Other instructions require recursive reasoning. + } + + // Rest of the logic is recursive, so if it's time to give up then it's time. + if (Depth > NegatorMaxDepth) { + LLVM_DEBUG(dbgs() << "Negator: reached maximal allowed traversal depth in " + << *V << ". Giving up.\n"); + ++NegatorTimesDepthLimitReached; + return nullptr; + } + + switch (I->getOpcode()) { + case Instruction::Select: { + { + // `abs`/`nabs` is always negatible. + Value *LHS, *RHS; + SelectPatternFlavor SPF = + matchSelectPattern(I, LHS, RHS, /*CastOp=*/nullptr, Depth).Flavor; + if (SPF == SPF_ABS || SPF == SPF_NABS) { + auto *NewSelect = cast(I->clone()); + // Just swap the operands of the select. + NewSelect->swapValues(); + // Don't swap prof metadata, we didn't change the branch behavior. + NewSelect->setName(I->getName() + ".neg"); + Builder.Insert(NewSelect); + return NewSelect; + } + } + // `select` is negatible if both hands of `select` are negatible. + Value *NegOp1 = visit(I->getOperand(1), Depth + 1); + if (!NegOp1) // Early return. + return nullptr; + Value *NegOp2 = visit(I->getOperand(2), Depth + 1); + if (!NegOp2) + return nullptr; + // Do preserve the metadata! + return Builder.CreateSelect(I->getOperand(0), NegOp1, NegOp2, + I->getName() + ".neg", /*MDFrom=*/I); + } + case Instruction::Trunc: { + // `trunc` is negatible if its operand is negatible. + Value *NegOp = visit(I->getOperand(0), Depth + 1); + if (!NegOp) // Early return. + return nullptr; + return Builder.CreateTrunc(NegOp, I->getType(), I->getName() + ".neg"); + } + case Instruction::Shl: { + // `shl` is negatible if the first operand is negatible. + Value *NegOp0 = visit(I->getOperand(0), Depth + 1); + if (!NegOp0) // Early return. + return nullptr; + return Builder.CreateShl(NegOp0, I->getOperand(1), I->getName() + ".neg"); + } + case Instruction::Add: { + // `add` is negatible if both of its operands are negatible. + Value *NegOp0 = visit(I->getOperand(0), Depth + 1); + if (!NegOp0) // Early return. + return nullptr; + Value *NegOp1 = visit(I->getOperand(1), Depth + 1); + if (!NegOp1) + return nullptr; + return Builder.CreateAdd(NegOp0, NegOp1, I->getName() + ".neg"); + } + case Instruction::Mul: { + // `mul` is negatible if one of its operands is negatible. + Value *NegatedOp, *OtherOp; + if (Value *NegOp0 = visit(I->getOperand(0), Depth + 1)) { + NegatedOp = NegOp0; + OtherOp = I->getOperand(1); + } else if (Value *NegOp1 = visit(I->getOperand(1), Depth + 1)) { + NegatedOp = NegOp1; + OtherOp = I->getOperand(0); + } else // Can't negate either of them. + return nullptr; + return Builder.CreateMul(NegatedOp, OtherOp, I->getName() + ".neg"); + } + default: + return nullptr; // Don't know, likely not negatible for free. + } + + llvm_unreachable("Can't get here. We always return from switch."); +}; + +LLVM_NODISCARD Optional Negator::run(Value *Root) { + Value *Negated = visit(Root, /*Depth=*/0); + if (!Negated) { + // We must cleanup newly-inserted instructions, to avoid any potential + // endless combine looping. + llvm::for_each(llvm::reverse(NewInstructions), + [&](Instruction *I) { I->eraseFromParent(); }); + return llvm::None; + } + return std::make_pair(ArrayRef(NewInstructions), Negated); +}; + +LLVM_NODISCARD Value *Negator::Negate(Value *Root, InstCombiner &IC) { + ++NegatorTotalNegationsAttempted; + LLVM_DEBUG(dbgs() << "Negator: attempting to sink negation into " << *Root + << "\n"); + + if (!NegatorEnabled || !DebugCounter::shouldExecute(NegatorCounter)) + return nullptr; + + Negator N(Root->getContext(), IC.getDataLayout()); + Optional Res = N.run(Root); + if (!Res) { // Negation failed. + LLVM_DEBUG(dbgs() << "Negator: failed to sink negation into " << *Root + << "\n"); + return nullptr; + } + + LLVM_DEBUG(dbgs() << "Negator: successfully sunk negation into " << *Root + << "\n NEW: " << *Res->second << "\n"); + ++NegatorNumTreesNegated; + + // We must temporarily unset the 'current' insertion point and DebugLoc of the + // InstCombine's IRBuilder so that it won't interfere with the ones we have + // already specified when producing negated instructions. + InstCombiner::BuilderTy::InsertPointGuard Guard(IC.Builder); + IC.Builder.ClearInsertionPoint(); + IC.Builder.SetCurrentDebugLocation(DebugLoc()); + + // And finally, we must add newly-created instructions into the InstCombine's + // worklist (in a proper order!) so it can attempt to combine them. + LLVM_DEBUG(dbgs() << "Negator: Propagating " << Res->first.size() + << " instrs to InstCombine\n"); + NegatorMaxInstructionsCreated.updateMax(Res->first.size()); + NegatorNumInstructionsNegatedSuccess += Res->first.size(); + + // They are in def-use order, so nothing fancy, just insert them in order. + llvm::for_each(Res->first, [&](Instruction *I) { IC.Builder.Insert(I); }); + + // And return the new root. + return Res->second; +}; diff --git a/llvm/test/Transforms/InstCombine/ARM/strcmp.ll b/llvm/test/Transforms/InstCombine/ARM/strcmp.ll --- a/llvm/test/Transforms/InstCombine/ARM/strcmp.ll +++ b/llvm/test/Transforms/InstCombine/ARM/strcmp.ll @@ -16,7 +16,7 @@ ; CHECK-LABEL: @test1( ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR2:%.*]], align 1 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32 -; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; CHECK-NEXT: [[TMP2:%.*]] = sub i32 0, [[TMP1]] ; CHECK-NEXT: ret i32 [[TMP2]] ; @@ -94,7 +94,7 @@ ; CHECK-LABEL: @test1_vfp( ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR2:%.*]], align 1 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32 -; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; CHECK-NEXT: [[TMP2:%.*]] = sub i32 0, [[TMP1]] ; CHECK-NEXT: ret i32 [[TMP2]] ; diff --git a/llvm/test/Transforms/InstCombine/abs-1.ll b/llvm/test/Transforms/InstCombine/abs-1.ll --- a/llvm/test/Transforms/InstCombine/abs-1.ll +++ b/llvm/test/Transforms/InstCombine/abs-1.ll @@ -120,7 +120,7 @@ ; CHECK-LABEL: @abs_canonical_5( ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[X]] to i32 -; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[CONV]] +; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[CONV]] ; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i32 [[NEG]], i32 [[CONV]] ; CHECK-NEXT: ret i32 [[ABS]] ; @@ -284,7 +284,7 @@ ; CHECK-LABEL: @nabs_canonical_5( ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i8 [[X:%.*]], 0 ; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[X]] to i32 -; CHECK-NEXT: [[NEG:%.*]] = sub nsw i32 0, [[CONV]] +; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[CONV]] ; CHECK-NEXT: [[ABS:%.*]] = select i1 [[CMP]], i32 [[CONV]], i32 [[NEG]] ; CHECK-NEXT: ret i32 [[ABS]] ; @@ -534,8 +534,8 @@ ; CHECK-LABEL: @negate_abs( ; CHECK-NEXT: [[N:%.*]] = sub i8 0, [[X:%.*]] ; CHECK-NEXT: [[C:%.*]] = icmp slt i8 [[X]], 0 -; CHECK-NEXT: [[S:%.*]] = select i1 [[C]], i8 [[X]], i8 [[N]] -; CHECK-NEXT: ret i8 [[S]] +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C]], i8 [[X]], i8 [[N]] +; CHECK-NEXT: ret i8 [[TMP1]] ; %n = sub i8 0, %x %c = icmp slt i8 %x, 0 @@ -548,8 +548,8 @@ ; CHECK-LABEL: @negate_nabs( ; CHECK-NEXT: [[N:%.*]] = sub <2 x i8> zeroinitializer, [[X:%.*]] ; CHECK-NEXT: [[C:%.*]] = icmp slt <2 x i8> [[X]], zeroinitializer -; CHECK-NEXT: [[S:%.*]] = select <2 x i1> [[C]], <2 x i8> [[N]], <2 x i8> [[X]] -; CHECK-NEXT: ret <2 x i8> [[S]] +; CHECK-NEXT: [[TMP1:%.*]] = select <2 x i1> [[C]], <2 x i8> [[N]], <2 x i8> [[X]] +; CHECK-NEXT: ret <2 x i8> [[TMP1]] ; %n = sub <2 x i8> zeroinitializer, %x %c = icmp slt <2 x i8> %x, zeroinitializer diff --git a/llvm/test/Transforms/InstCombine/and-or-icmps.ll b/llvm/test/Transforms/InstCombine/and-or-icmps.ll --- a/llvm/test/Transforms/InstCombine/and-or-icmps.ll +++ b/llvm/test/Transforms/InstCombine/and-or-icmps.ll @@ -208,12 +208,20 @@ ; CHECK-LABEL: @simplify_before_foldAndOfICmps( ; CHECK-NEXT: [[A8:%.*]] = alloca i16, align 2 ; CHECK-NEXT: [[L7:%.*]] = load i16, i16* [[A8]], align 2 -; CHECK-NEXT: [[C10:%.*]] = icmp ult i16 [[L7]], 2 +; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i16 [[L7]], -1 +; CHECK-NEXT: [[B11:%.*]] = zext i1 [[TMP1]] to i16 +; CHECK-NEXT: [[C10:%.*]] = icmp ugt i16 [[L7]], [[B11]] +; CHECK-NEXT: [[C5:%.*]] = icmp slt i16 [[L7]], 1 +; CHECK-NEXT: [[C11:%.*]] = icmp ne i16 [[L7]], 0 ; CHECK-NEXT: [[C7:%.*]] = icmp slt i16 [[L7]], 0 -; CHECK-NEXT: [[C18:%.*]] = or i1 [[C7]], [[C10]] -; CHECK-NEXT: [[L7_LOBIT:%.*]] = ashr i16 [[L7]], 15 -; CHECK-NEXT: [[TMP1:%.*]] = sext i16 [[L7_LOBIT]] to i64 -; CHECK-NEXT: [[G26:%.*]] = getelementptr i1, i1* null, i64 [[TMP1]] +; CHECK-NEXT: [[B15:%.*]] = xor i1 [[C7]], [[C10]] +; CHECK-NEXT: [[B19:%.*]] = xor i1 [[C11]], [[B15]] +; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[C10]], [[C5]] +; CHECK-NEXT: [[C3:%.*]] = and i1 [[B19]], [[TMP2]] +; CHECK-NEXT: [[TMP3:%.*]] = xor i1 [[C10]], true +; CHECK-NEXT: [[C18:%.*]] = or i1 [[C7]], [[TMP3]] +; CHECK-NEXT: [[TMP4:%.*]] = sext i1 [[C3]] to i64 +; CHECK-NEXT: [[G26:%.*]] = getelementptr i1, i1* null, i64 [[TMP4]] ; CHECK-NEXT: store i16 [[L7]], i16* undef, align 2 ; CHECK-NEXT: store i1 [[C18]], i1* undef, align 1 ; CHECK-NEXT: store i1* [[G26]], i1** undef, align 8 diff --git a/llvm/test/Transforms/InstCombine/high-bit-signmask-with-trunc.ll b/llvm/test/Transforms/InstCombine/high-bit-signmask-with-trunc.ll --- a/llvm/test/Transforms/InstCombine/high-bit-signmask-with-trunc.ll +++ b/llvm/test/Transforms/InstCombine/high-bit-signmask-with-trunc.ll @@ -4,8 +4,8 @@ define i32 @t0(i64 %x) { ; CHECK-LABEL: @t0( ; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[X:%.*]], 63 -; CHECK-NEXT: [[R:%.*]] = trunc i64 [[TMP1]] to i32 -; CHECK-NEXT: ret i32 [[R]] +; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32 +; CHECK-NEXT: ret i32 [[TMP2]] ; %t0 = lshr i64 %x, 63 %t1 = trunc i64 %t0 to i32 @@ -15,8 +15,8 @@ define i32 @t1_exact(i64 %x) { ; CHECK-LABEL: @t1_exact( ; CHECK-NEXT: [[TMP1:%.*]] = ashr exact i64 [[X:%.*]], 63 -; CHECK-NEXT: [[R:%.*]] = trunc i64 [[TMP1]] to i32 -; CHECK-NEXT: ret i32 [[R]] +; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32 +; CHECK-NEXT: ret i32 [[TMP2]] ; %t0 = lshr exact i64 %x, 63 %t1 = trunc i64 %t0 to i32 @@ -26,8 +26,8 @@ define i32 @t2(i64 %x) { ; CHECK-LABEL: @t2( ; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[X:%.*]], 63 -; CHECK-NEXT: [[R:%.*]] = trunc i64 [[TMP1]] to i32 -; CHECK-NEXT: ret i32 [[R]] +; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32 +; CHECK-NEXT: ret i32 [[TMP2]] ; %t0 = ashr i64 %x, 63 %t1 = trunc i64 %t0 to i32 @@ -37,8 +37,8 @@ define i32 @t3_exact(i64 %x) { ; CHECK-LABEL: @t3_exact( ; CHECK-NEXT: [[TMP1:%.*]] = lshr exact i64 [[X:%.*]], 63 -; CHECK-NEXT: [[R:%.*]] = trunc i64 [[TMP1]] to i32 -; CHECK-NEXT: ret i32 [[R]] +; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32 +; CHECK-NEXT: ret i32 [[TMP2]] ; %t0 = ashr exact i64 %x, 63 %t1 = trunc i64 %t0 to i32 @@ -49,8 +49,8 @@ define <2 x i32> @t4(<2 x i64> %x) { ; CHECK-LABEL: @t4( ; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i64> [[X:%.*]], -; CHECK-NEXT: [[R:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i32> -; CHECK-NEXT: ret <2 x i32> [[R]] +; CHECK-NEXT: [[TMP2:%.*]] = trunc <2 x i64> [[TMP1]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP2]] ; %t0 = lshr <2 x i64> %x, %t1 = trunc <2 x i64> %t0 to <2 x i32> @@ -78,8 +78,8 @@ ; CHECK-LABEL: @t6( ; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 63 ; CHECK-NEXT: call void @use64(i64 [[T0]]) -; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[X]], 63 -; CHECK-NEXT: [[R:%.*]] = trunc i64 [[TMP1]] to i32 +; CHECK-NEXT: [[T1:%.*]] = trunc i64 [[T0]] to i32 +; CHECK-NEXT: [[R:%.*]] = sub i32 0, [[T1]] ; CHECK-NEXT: ret i32 [[R]] ; %t0 = lshr i64 %x, 63 @@ -94,7 +94,7 @@ ; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 63 ; CHECK-NEXT: [[T1:%.*]] = trunc i64 [[T0]] to i32 ; CHECK-NEXT: call void @use32(i32 [[T1]]) -; CHECK-NEXT: [[R:%.*]] = sub nsw i32 0, [[T1]] +; CHECK-NEXT: [[R:%.*]] = sub i32 0, [[T1]] ; CHECK-NEXT: ret i32 [[R]] ; %t0 = lshr i64 %x, 63 @@ -110,7 +110,7 @@ ; CHECK-NEXT: call void @use64(i64 [[T0]]) ; CHECK-NEXT: [[T1:%.*]] = trunc i64 [[T0]] to i32 ; CHECK-NEXT: call void @use32(i32 [[T1]]) -; CHECK-NEXT: [[R:%.*]] = sub nsw i32 0, [[T1]] +; CHECK-NEXT: [[R:%.*]] = sub i32 0, [[T1]] ; CHECK-NEXT: ret i32 [[R]] ; %t0 = lshr i64 %x, 63 @@ -125,7 +125,7 @@ ; CHECK-LABEL: @n9( ; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 62 ; CHECK-NEXT: [[T1:%.*]] = trunc i64 [[T0]] to i32 -; CHECK-NEXT: [[R:%.*]] = sub nsw i32 0, [[T1]] +; CHECK-NEXT: [[R:%.*]] = sub i32 0, [[T1]] ; CHECK-NEXT: ret i32 [[R]] ; %t0 = lshr i64 %x, 62 @@ -136,9 +136,9 @@ define i32 @n10(i64 %x) { ; CHECK-LABEL: @n10( -; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 63 -; CHECK-NEXT: [[T1:%.*]] = trunc i64 [[T0]] to i32 -; CHECK-NEXT: [[R:%.*]] = xor i32 [[T1]], 1 +; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[X:%.*]], 63 +; CHECK-NEXT: [[TMP2:%.*]] = trunc i64 [[TMP1]] to i32 +; CHECK-NEXT: [[R:%.*]] = add i32 [[TMP2]], 1 ; CHECK-NEXT: ret i32 [[R]] ; %t0 = lshr i64 %x, 63 diff --git a/llvm/test/Transforms/InstCombine/high-bit-signmask.ll b/llvm/test/Transforms/InstCombine/high-bit-signmask.ll --- a/llvm/test/Transforms/InstCombine/high-bit-signmask.ll +++ b/llvm/test/Transforms/InstCombine/high-bit-signmask.ll @@ -3,8 +3,8 @@ define i64 @t0(i64 %x) { ; CHECK-LABEL: @t0( -; CHECK-NEXT: [[R:%.*]] = ashr i64 [[X:%.*]], 63 -; CHECK-NEXT: ret i64 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[X:%.*]], 63 +; CHECK-NEXT: ret i64 [[TMP1]] ; %t0 = lshr i64 %x, 63 %r = sub i64 0, %t0 @@ -12,8 +12,8 @@ } define i64 @t0_exact(i64 %x) { ; CHECK-LABEL: @t0_exact( -; CHECK-NEXT: [[R:%.*]] = ashr exact i64 [[X:%.*]], 63 -; CHECK-NEXT: ret i64 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = ashr exact i64 [[X:%.*]], 63 +; CHECK-NEXT: ret i64 [[TMP1]] ; %t0 = lshr exact i64 %x, 63 %r = sub i64 0, %t0 @@ -21,8 +21,8 @@ } define i64 @t2(i64 %x) { ; CHECK-LABEL: @t2( -; CHECK-NEXT: [[R:%.*]] = lshr i64 [[X:%.*]], 63 -; CHECK-NEXT: ret i64 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = lshr i64 [[X:%.*]], 63 +; CHECK-NEXT: ret i64 [[TMP1]] ; %t0 = ashr i64 %x, 63 %r = sub i64 0, %t0 @@ -30,8 +30,8 @@ } define i64 @t3_exact(i64 %x) { ; CHECK-LABEL: @t3_exact( -; CHECK-NEXT: [[R:%.*]] = lshr exact i64 [[X:%.*]], 63 -; CHECK-NEXT: ret i64 [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = lshr exact i64 [[X:%.*]], 63 +; CHECK-NEXT: ret i64 [[TMP1]] ; %t0 = ashr exact i64 %x, 63 %r = sub i64 0, %t0 @@ -40,8 +40,8 @@ define <2 x i64> @t4(<2 x i64> %x) { ; CHECK-LABEL: @t4( -; CHECK-NEXT: [[R:%.*]] = ashr <2 x i64> [[X:%.*]], -; CHECK-NEXT: ret <2 x i64> [[R]] +; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i64> [[X:%.*]], +; CHECK-NEXT: ret <2 x i64> [[TMP1]] ; %t0 = lshr <2 x i64> %x, %r = sub <2 x i64> zeroinitializer, %t0 @@ -66,7 +66,7 @@ ; CHECK-LABEL: @t6( ; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 63 ; CHECK-NEXT: call void @use64(i64 [[T0]]) -; CHECK-NEXT: [[R:%.*]] = ashr i64 [[X]], 63 +; CHECK-NEXT: [[R:%.*]] = sub i64 0, [[T0]] ; CHECK-NEXT: ret i64 [[R]] ; %t0 = lshr i64 %x, 63 @@ -79,7 +79,7 @@ ; CHECK-LABEL: @n7( ; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 63 ; CHECK-NEXT: call void @use32(i64 [[T0]]) -; CHECK-NEXT: [[R:%.*]] = ashr i64 [[X]], 63 +; CHECK-NEXT: [[R:%.*]] = sub i64 0, [[T0]] ; CHECK-NEXT: ret i64 [[R]] ; %t0 = lshr i64 %x, 63 @@ -93,7 +93,7 @@ ; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 63 ; CHECK-NEXT: call void @use64(i64 [[T0]]) ; CHECK-NEXT: call void @use32(i64 [[T0]]) -; CHECK-NEXT: [[R:%.*]] = ashr i64 [[X]], 63 +; CHECK-NEXT: [[R:%.*]] = sub i64 0, [[T0]] ; CHECK-NEXT: ret i64 [[R]] ; %t0 = lshr i64 %x, 63 @@ -106,7 +106,7 @@ define i64 @n9(i64 %x) { ; CHECK-LABEL: @n9( ; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 62 -; CHECK-NEXT: [[R:%.*]] = sub nsw i64 0, [[T0]] +; CHECK-NEXT: [[R:%.*]] = sub i64 0, [[T0]] ; CHECK-NEXT: ret i64 [[R]] ; %t0 = lshr i64 %x, 62 @@ -116,8 +116,8 @@ define i64 @n10(i64 %x) { ; CHECK-LABEL: @n10( -; CHECK-NEXT: [[T0:%.*]] = lshr i64 [[X:%.*]], 63 -; CHECK-NEXT: [[R:%.*]] = xor i64 [[T0]], 1 +; CHECK-NEXT: [[TMP1:%.*]] = ashr i64 [[X:%.*]], 63 +; CHECK-NEXT: [[R:%.*]] = add nsw i64 [[TMP1]], 1 ; CHECK-NEXT: ret i64 [[R]] ; %t0 = lshr i64 %x, 63 diff --git a/llvm/test/Transforms/InstCombine/icmp.ll b/llvm/test/Transforms/InstCombine/icmp.ll --- a/llvm/test/Transforms/InstCombine/icmp.ll +++ b/llvm/test/Transforms/InstCombine/icmp.ll @@ -98,8 +98,8 @@ define i32 @test6(i32 %a, i32 %b) { ; CHECK-LABEL: @test6( -; CHECK-NEXT: [[E:%.*]] = ashr i32 [[A:%.*]], 31 -; CHECK-NEXT: [[F:%.*]] = and i32 [[E]], [[B:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A:%.*]], 31 +; CHECK-NEXT: [[F:%.*]] = and i32 [[TMP1]], [[B:%.*]] ; CHECK-NEXT: ret i32 [[F]] ; %c = icmp sle i32 %a, -1 @@ -512,7 +512,8 @@ ; unsigned overflow does not happen during offset computation define i1 @test24_neg_offs(i32* %p, i64 %offs) { ; CHECK-LABEL: @test24_neg_offs( -; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[OFFS:%.*]], -2 +; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[OFFS:%.*]], -4 +; CHECK-NEXT: [[CMP:%.*]] = icmp eq i64 [[TMP1]], 8 ; CHECK-NEXT: ret i1 [[CMP]] ; %p1 = getelementptr inbounds i32, i32* %p, i64 %offs @@ -2906,7 +2907,8 @@ define i1 @cmp_sgt_rhs_dec(float %x, i32 %i) { ; CHECK-LABEL: @cmp_sgt_rhs_dec( ; CHECK-NEXT: [[CONV:%.*]] = fptosi float [[X:%.*]] to i32 -; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[CONV]], [[I:%.*]] +; CHECK-NEXT: [[DEC:%.*]] = add i32 [[I:%.*]], -1 +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[DEC]], [[CONV]] ; CHECK-NEXT: ret i1 [[CMP]] ; %conv = fptosi float %x to i32 @@ -2918,7 +2920,8 @@ define i1 @cmp_sle_rhs_dec(float %x, i32 %i) { ; CHECK-LABEL: @cmp_sle_rhs_dec( ; CHECK-NEXT: [[CONV:%.*]] = fptosi float [[X:%.*]] to i32 -; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[CONV]], [[I:%.*]] +; CHECK-NEXT: [[DEC:%.*]] = add i32 [[I:%.*]], -1 +; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[DEC]], [[CONV]] ; CHECK-NEXT: ret i1 [[CMP]] ; %conv = fptosi float %x to i32 @@ -3140,7 +3143,7 @@ define i1 @cmp_sgt_lhs_dec(i32 %x, i32 %y) { ; CHECK-LABEL: @cmp_sgt_lhs_dec( -; CHECK-NEXT: [[DEC:%.*]] = add nsw i32 [[X:%.*]], -1 +; CHECK-NEXT: [[DEC:%.*]] = add i32 [[X:%.*]], -1 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[DEC]], [[Y:%.*]] ; CHECK-NEXT: ret i1 [[CMP]] ; @@ -3189,7 +3192,7 @@ define i1 @cmp_slt_rhs_dec(float %x, i32 %y) { ; CHECK-LABEL: @cmp_slt_rhs_dec( ; CHECK-NEXT: [[CONV:%.*]] = fptosi float [[X:%.*]] to i32 -; CHECK-NEXT: [[DEC:%.*]] = add nsw i32 [[Y:%.*]], -1 +; CHECK-NEXT: [[DEC:%.*]] = add i32 [[Y:%.*]], -1 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[DEC]], [[CONV]] ; CHECK-NEXT: ret i1 [[CMP]] ; diff --git a/llvm/test/Transforms/InstCombine/mul.ll b/llvm/test/Transforms/InstCombine/mul.ll --- a/llvm/test/Transforms/InstCombine/mul.ll +++ b/llvm/test/Transforms/InstCombine/mul.ll @@ -456,10 +456,9 @@ define i32 @test_mul_canonicalize_op1(i32 %x, i32 %z) { ; CHECK-LABEL: @test_mul_canonicalize_op1( -; CHECK-NEXT: [[Y:%.*]] = mul i32 [[Z:%.*]], 3 -; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[Y]], [[X:%.*]] -; CHECK-NEXT: [[MUL:%.*]] = sub i32 0, [[TMP1]] -; CHECK-NEXT: ret i32 [[MUL]] +; CHECK-NEXT: [[TMP1:%.*]] = mul i32 [[Z:%.*]], -3 +; CHECK-NEXT: [[TMP2:%.*]] = mul i32 [[TMP1]], [[X:%.*]] +; CHECK-NEXT: ret i32 [[TMP2]] ; %y = mul i32 %z, 3 %neg = sub i32 0, %x @@ -543,7 +542,7 @@ define <2 x i8> @negate_if_true_commute(<2 x i8> %px, i1 %cond) { ; CHECK-LABEL: @negate_if_true_commute( ; CHECK-NEXT: [[X:%.*]] = sdiv <2 x i8> , [[PX:%.*]] -; CHECK-NEXT: [[TMP1:%.*]] = sub nsw <2 x i8> zeroinitializer, [[X]] +; CHECK-NEXT: [[TMP1:%.*]] = sub <2 x i8> zeroinitializer, [[X]] ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[COND:%.*]], <2 x i8> [[TMP1]], <2 x i8> [[X]] ; CHECK-NEXT: ret <2 x i8> [[TMP2]] ; diff --git a/llvm/test/Transforms/InstCombine/sadd-with-overflow.ll b/llvm/test/Transforms/InstCombine/sadd-with-overflow.ll --- a/llvm/test/Transforms/InstCombine/sadd-with-overflow.ll +++ b/llvm/test/Transforms/InstCombine/sadd-with-overflow.ll @@ -115,8 +115,9 @@ define { i32, i1 } @fold_sub_simple(i32 %x) { ; CHECK-LABEL: @fold_sub_simple( -; CHECK-NEXT: [[TMP1:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 42) -; CHECK-NEXT: ret { i32, i1 } [[TMP1]] +; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], 12 +; CHECK-NEXT: [[B:%.*]] = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A]], i32 30) +; CHECK-NEXT: ret { i32, i1 } [[B]] ; %a = sub nsw i32 %x, -12 %b = tail call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 %a, i32 30) diff --git a/llvm/test/Transforms/InstCombine/ssub-with-overflow.ll b/llvm/test/Transforms/InstCombine/ssub-with-overflow.ll --- a/llvm/test/Transforms/InstCombine/ssub-with-overflow.ll +++ b/llvm/test/Transforms/InstCombine/ssub-with-overflow.ll @@ -11,7 +11,8 @@ define { i32, i1 } @simple_fold(i32 %x) { ; CHECK-LABEL: @simple_fold( -; CHECK-NEXT: [[TMP1:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 -20) +; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -7 +; CHECK-NEXT: [[TMP1:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A]], i32 -13) ; CHECK-NEXT: ret { i32, i1 } [[TMP1]] ; %a = sub nsw i32 %x, 7 @@ -21,8 +22,8 @@ define { i32, i1 } @fold_mixed_signs(i32 %x) { ; CHECK-LABEL: @fold_mixed_signs( -; CHECK-NEXT: [[B:%.*]] = add nsw i32 [[X:%.*]], -6 -; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 undef, i1 false }, i32 [[B]], 0 +; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -13 +; CHECK-NEXT: [[TMP1:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A]], i32 7) ; CHECK-NEXT: ret { i32, i1 } [[TMP1]] ; %a = sub nsw i32 %x, 13 @@ -32,7 +33,8 @@ define { i8, i1 } @fold_on_constant_sub_no_overflow(i8 %x) { ; CHECK-LABEL: @fold_on_constant_sub_no_overflow( -; CHECK-NEXT: [[TMP1:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[X:%.*]], i8 -128) +; CHECK-NEXT: [[A:%.*]] = add i8 [[X:%.*]], -100 +; CHECK-NEXT: [[TMP1:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[A]], i8 -28) ; CHECK-NEXT: ret { i8, i1 } [[TMP1]] ; %a = sub nsw i8 %x, 100 @@ -42,7 +44,7 @@ define { i8, i1 } @no_fold_on_constant_sub_overflow(i8 %x) { ; CHECK-LABEL: @no_fold_on_constant_sub_overflow( -; CHECK-NEXT: [[A:%.*]] = add nsw i8 [[X:%.*]], -100 +; CHECK-NEXT: [[A:%.*]] = add i8 [[X:%.*]], -100 ; CHECK-NEXT: [[TMP1:%.*]] = call { i8, i1 } @llvm.sadd.with.overflow.i8(i8 [[A]], i8 -29) ; CHECK-NEXT: ret { i8, i1 } [[TMP1]] ; @@ -53,7 +55,8 @@ define { <2 x i32>, <2 x i1> } @fold_simple_splat_constant(<2 x i32> %x) { ; CHECK-LABEL: @fold_simple_splat_constant( -; CHECK-NEXT: [[TMP1:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.sadd.with.overflow.v2i32(<2 x i32> [[X:%.*]], <2 x i32> ) +; CHECK-NEXT: [[A:%.*]] = add <2 x i32> [[X:%.*]], +; CHECK-NEXT: [[TMP1:%.*]] = call { <2 x i32>, <2 x i1> } @llvm.sadd.with.overflow.v2i32(<2 x i32> [[A]], <2 x i32> ) ; CHECK-NEXT: ret { <2 x i32>, <2 x i1> } [[TMP1]] ; %a = sub nsw <2 x i32> %x, @@ -85,7 +88,8 @@ define { i32, i1 } @fold_nuwnsw(i32 %x) { ; CHECK-LABEL: @fold_nuwnsw( -; CHECK-NEXT: [[TMP1:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[X:%.*]], i32 -42) +; CHECK-NEXT: [[A:%.*]] = add i32 [[X:%.*]], -12 +; CHECK-NEXT: [[TMP1:%.*]] = call { i32, i1 } @llvm.sadd.with.overflow.i32(i32 [[A]], i32 -30) ; CHECK-NEXT: ret { i32, i1 } [[TMP1]] ; %a = sub nuw nsw i32 %x, 12 diff --git a/llvm/test/Transforms/InstCombine/strcmp-1.ll b/llvm/test/Transforms/InstCombine/strcmp-1.ll --- a/llvm/test/Transforms/InstCombine/strcmp-1.ll +++ b/llvm/test/Transforms/InstCombine/strcmp-1.ll @@ -22,13 +22,13 @@ ; NOBCMP-LABEL: @test1( ; NOBCMP-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR2:%.*]], align 1 ; NOBCMP-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32 -; NOBCMP-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; NOBCMP-NEXT: [[TMP2:%.*]] = sub i32 0, [[TMP1]] ; NOBCMP-NEXT: ret i32 [[TMP2]] ; ; BCMP-LABEL: @test1( ; BCMP-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR2:%.*]], align 1 ; BCMP-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32 -; BCMP-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; BCMP-NEXT: [[TMP2:%.*]] = sub i32 0, [[TMP1]] ; BCMP-NEXT: ret i32 [[TMP2]] ; %str1 = getelementptr inbounds [1 x i8], [1 x i8]* @null, i32 0, i32 0 diff --git a/llvm/test/Transforms/InstCombine/strncmp-1.ll b/llvm/test/Transforms/InstCombine/strncmp-1.ll --- a/llvm/test/Transforms/InstCombine/strncmp-1.ll +++ b/llvm/test/Transforms/InstCombine/strncmp-1.ll @@ -16,7 +16,7 @@ ; CHECK-LABEL: @test1( ; CHECK-NEXT: [[STRCMPLOAD:%.*]] = load i8, i8* [[STR2:%.*]], align 1 ; CHECK-NEXT: [[TMP1:%.*]] = zext i8 [[STRCMPLOAD]] to i32 -; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; CHECK-NEXT: [[TMP2:%.*]] = sub i32 0, [[TMP1]] ; CHECK-NEXT: ret i32 [[TMP2]] ; diff --git a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll --- a/llvm/test/Transforms/InstCombine/sub-of-negatible.ll +++ b/llvm/test/Transforms/InstCombine/sub-of-negatible.ll @@ -18,7 +18,7 @@ ; CHECK-LABEL: @t1( ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]] ; CHECK-NEXT: call void @use8(i8 [[T0]]) -; CHECK-NEXT: [[T1:%.*]] = add i8 [[X:%.*]], [[Y]] +; CHECK-NEXT: [[T1:%.*]] = add i8 [[Y]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T1]] ; %t0 = sub i8 0, %y @@ -30,8 +30,8 @@ ; Shift-left can be negated if all uses can be updated define i8 @t2(i8 %x, i8 %y) { ; CHECK-LABEL: @t2( -; CHECK-NEXT: [[T0:%.*]] = shl i8 -42, [[Y:%.*]] -; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]] +; CHECK-NEXT: [[TMP1:%.*]] = shl i8 42, [[Y:%.*]] +; CHECK-NEXT: [[T1:%.*]] = add i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T1]] ; %t0 = shl i8 -42, %y @@ -54,8 +54,8 @@ ; CHECK-LABEL: @t3( ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Z:%.*]] ; CHECK-NEXT: call void @use8(i8 [[T0]]) -; CHECK-NEXT: [[T1:%.*]] = shl i8 [[T0]], [[Y:%.*]] -; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]] +; CHECK-NEXT: [[TMP1:%.*]] = shl i8 [[Z]], [[Y:%.*]] +; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T2]] ; %t0 = sub i8 0, %z @@ -84,8 +84,8 @@ ; Select can be negated if all it's operands can be negated and all the users of select can be updated define i8 @t4(i8 %x, i1 %y) { ; CHECK-LABEL: @t4( -; CHECK-NEXT: [[T0:%.*]] = select i1 [[Y:%.*]], i8 -42, i8 44 -; CHECK-NEXT: [[T1:%.*]] = sub i8 [[X:%.*]], [[T0]] +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[Y:%.*]], i8 42, i8 -44 +; CHECK-NEXT: [[T1:%.*]] = add i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T1]] ; %t0 = select i1 %y, i8 -42, i8 44 @@ -118,8 +118,8 @@ ; CHECK-LABEL: @t6( ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Z:%.*]] ; CHECK-NEXT: call void @use8(i8 [[T0]]) -; CHECK-NEXT: [[T1:%.*]] = select i1 [[Y:%.*]], i8 -42, i8 [[T0]] -; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]] +; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[Y:%.*]], i8 42, i8 [[Z]] +; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T2]] ; %t0 = sub i8 0, %z @@ -130,9 +130,9 @@ } define i8 @t7(i8 %x, i1 %y, i8 %z) { ; CHECK-LABEL: @t7( -; CHECK-NEXT: [[T0:%.*]] = shl i8 1, [[Z:%.*]] -; CHECK-NEXT: [[T1:%.*]] = select i1 [[Y:%.*]], i8 0, i8 [[T0]] -; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]] +; CHECK-NEXT: [[TMP1:%.*]] = shl i8 -1, [[Z:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[Y:%.*]], i8 0, i8 [[TMP1]] +; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP2]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T2]] ; %t0 = shl i8 1, %z @@ -159,8 +159,8 @@ ; x - (y - z) -> x - y + z -> x + (z - y) define i8 @t9(i8 %x, i8 %y) { ; CHECK-LABEL: @t9( -; CHECK-NEXT: [[T01:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]] -; CHECK-NEXT: ret i8 [[T01]] +; CHECK-NEXT: [[TMP1:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]] +; CHECK-NEXT: ret i8 [[TMP1]] ; %t0 = sub i8 %y, %x %t1 = sub i8 0, %t0 @@ -203,8 +203,8 @@ ; CHECK-LABEL: @n13( ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]] ; CHECK-NEXT: call void @use8(i8 [[T0]]) -; CHECK-NEXT: [[T11:%.*]] = sub i8 [[Y]], [[Z:%.*]] -; CHECK-NEXT: [[T2:%.*]] = add i8 [[T11]], [[X:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = sub i8 [[Y]], [[Z:%.*]] +; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T2]] ; %t0 = sub i8 0, %y @@ -241,7 +241,7 @@ ; CHECK-LABEL: @t15( ; CHECK-NEXT: [[T0:%.*]] = sub i8 0, [[Y:%.*]] ; CHECK-NEXT: call void @use8(i8 [[T0]]) -; CHECK-NEXT: [[TMP1:%.*]] = mul i8 [[Z:%.*]], [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = mul i8 [[Y]], [[Z:%.*]] ; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T2]] ; @@ -274,12 +274,14 @@ ; CHECK-NEXT: begin: ; CHECK-NEXT: br i1 [[C:%.*]], label [[THEN:%.*]], label [[ELSE:%.*]] ; CHECK: then: +; CHECK-NEXT: [[Y:%.*]] = sub i8 0, [[X:%.*]] ; CHECK-NEXT: br label [[END:%.*]] ; CHECK: else: ; CHECK-NEXT: br label [[END]] ; CHECK: end: -; CHECK-NEXT: [[Z:%.*]] = phi i8 [ [[X:%.*]], [[THEN]] ], [ 42, [[ELSE]] ] -; CHECK-NEXT: ret i8 [[Z]] +; CHECK-NEXT: [[Z:%.*]] = phi i8 [ [[Y]], [[THEN]] ], [ -42, [[ELSE]] ] +; CHECK-NEXT: [[N:%.*]] = sub i8 0, [[Z]] +; CHECK-NEXT: ret i8 [[N]] ; begin: br i1 %c, label %then, label %else @@ -351,9 +353,9 @@ ; truncation can be negated if it's operand can be negated define i8 @t20(i8 %x, i16 %y) { ; CHECK-LABEL: @t20( -; CHECK-NEXT: [[T0:%.*]] = shl i16 -42, [[Y:%.*]] -; CHECK-NEXT: [[T1:%.*]] = trunc i16 [[T0]] to i8 -; CHECK-NEXT: [[T2:%.*]] = sub i8 [[X:%.*]], [[T1]] +; CHECK-NEXT: [[TMP1:%.*]] = shl i16 42, [[Y:%.*]] +; CHECK-NEXT: [[TMP2:%.*]] = trunc i16 [[TMP1]] to i8 +; CHECK-NEXT: [[T2:%.*]] = add i8 [[TMP2]], [[X:%.*]] ; CHECK-NEXT: ret i8 [[T2]] ; %t0 = shl i16 -42, %y diff --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll --- a/llvm/test/Transforms/InstCombine/sub.ll +++ b/llvm/test/Transforms/InstCombine/sub.ll @@ -52,7 +52,7 @@ define i32 @neg_sub(i32 %x, i32 %y) { ; CHECK-LABEL: @neg_sub( -; CHECK-NEXT: [[R:%.*]] = add i32 [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i32 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret i32 [[R]] ; %neg = sub i32 0, %x @@ -62,7 +62,7 @@ define i32 @neg_nsw_sub(i32 %x, i32 %y) { ; CHECK-LABEL: @neg_nsw_sub( -; CHECK-NEXT: [[R:%.*]] = add i32 [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i32 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret i32 [[R]] ; %neg = sub nsw i32 0, %x @@ -72,7 +72,7 @@ define i32 @neg_sub_nsw(i32 %x, i32 %y) { ; CHECK-LABEL: @neg_sub_nsw( -; CHECK-NEXT: [[R:%.*]] = add i32 [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i32 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret i32 [[R]] ; %neg = sub i32 0, %x @@ -82,7 +82,7 @@ define i32 @neg_nsw_sub_nsw(i32 %x, i32 %y) { ; CHECK-LABEL: @neg_nsw_sub_nsw( -; CHECK-NEXT: [[R:%.*]] = add nsw i32 [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add i32 [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret i32 [[R]] ; %neg = sub nsw i32 0, %x @@ -92,7 +92,7 @@ define <2 x i32> @neg_sub_vec(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @neg_sub_vec( -; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %neg = sub <2 x i32> zeroinitializer, %x @@ -102,7 +102,7 @@ define <2 x i32> @neg_nsw_sub_vec(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @neg_nsw_sub_vec( -; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %neg = sub nsw <2 x i32> zeroinitializer, %x @@ -112,7 +112,7 @@ define <2 x i32> @neg_sub_nsw_vec(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @neg_sub_nsw_vec( -; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %neg = sub <2 x i32> zeroinitializer, %x @@ -122,7 +122,7 @@ define <2 x i32> @neg_nsw_sub_nsw_vec(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @neg_nsw_sub_nsw_vec( -; CHECK-NEXT: [[R:%.*]] = add nsw <2 x i32> [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %neg = sub nsw <2 x i32> zeroinitializer, %x @@ -132,7 +132,7 @@ define <2 x i32> @neg_sub_vec_undef(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @neg_sub_vec_undef( -; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %neg = sub <2 x i32> , %x @@ -142,7 +142,7 @@ define <2 x i32> @neg_nsw_sub_vec_undef(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @neg_nsw_sub_vec_undef( -; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %neg = sub nsw <2 x i32> , %x @@ -152,7 +152,7 @@ define <2 x i32> @neg_sub_nsw_vec_undef(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @neg_sub_nsw_vec_undef( -; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %neg = sub <2 x i32> , %x @@ -164,7 +164,7 @@ define <2 x i32> @neg_nsw_sub_nsw_vec_undef(<2 x i32> %x, <2 x i32> %y) { ; CHECK-LABEL: @neg_nsw_sub_nsw_vec_undef( -; CHECK-NEXT: [[R:%.*]] = add nsw <2 x i32> [[Y:%.*]], [[X:%.*]] +; CHECK-NEXT: [[R:%.*]] = add <2 x i32> [[X:%.*]], [[Y:%.*]] ; CHECK-NEXT: ret <2 x i32> [[R]] ; %neg = sub nsw <2 x i32> , %x @@ -218,8 +218,8 @@ define i32 @test5(i32 %A, i32 %B, i32 %C) { ; CHECK-LABEL: @test5( -; CHECK-NEXT: [[D1:%.*]] = sub i32 [[C:%.*]], [[B:%.*]] -; CHECK-NEXT: [[E:%.*]] = add i32 [[D1]], [[A:%.*]] +; CHECK-NEXT: [[TMP1:%.*]] = sub i32 [[C:%.*]], [[B:%.*]] +; CHECK-NEXT: [[E:%.*]] = add i32 [[TMP1]], [[A:%.*]] ; CHECK-NEXT: ret i32 [[E]] ; %D = sub i32 %B, %C @@ -300,8 +300,8 @@ define i32 @test12(i32 %A) { ; CHECK-LABEL: @test12( -; CHECK-NEXT: [[C:%.*]] = lshr i32 [[A:%.*]], 31 -; CHECK-NEXT: ret i32 [[C]] +; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[A:%.*]], 31 +; CHECK-NEXT: ret i32 [[TMP1]] ; %B = ashr i32 %A, 31 %C = sub i32 0, %B @@ -310,8 +310,8 @@ define i32 @test13(i32 %A) { ; CHECK-LABEL: @test13( -; CHECK-NEXT: [[C:%.*]] = ashr i32 [[A:%.*]], 31 -; CHECK-NEXT: ret i32 [[C]] +; CHECK-NEXT: [[TMP1:%.*]] = ashr i32 [[A:%.*]], 31 +; CHECK-NEXT: ret i32 [[TMP1]] ; %B = lshr i32 %A, 31 %C = sub i32 0, %B @@ -320,8 +320,8 @@ define <2 x i32> @test12vec(<2 x i32> %A) { ; CHECK-LABEL: @test12vec( -; CHECK-NEXT: [[C:%.*]] = lshr <2 x i32> [[A:%.*]], -; CHECK-NEXT: ret <2 x i32> [[C]] +; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> [[A:%.*]], +; CHECK-NEXT: ret <2 x i32> [[TMP1]] ; %B = ashr <2 x i32> %A, %C = sub <2 x i32> zeroinitializer, %B @@ -330,8 +330,8 @@ define <2 x i32> @test13vec(<2 x i32> %A) { ; CHECK-LABEL: @test13vec( -; CHECK-NEXT: [[C:%.*]] = ashr <2 x i32> [[A:%.*]], -; CHECK-NEXT: ret <2 x i32> [[C]] +; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i32> [[A:%.*]], +; CHECK-NEXT: ret <2 x i32> [[TMP1]] ; %B = lshr <2 x i32> %A, %C = sub <2 x i32> zeroinitializer, %B @@ -351,8 +351,8 @@ define i32 @test16(i32 %A) { ; CHECK-LABEL: @test16( -; CHECK-NEXT: [[Y:%.*]] = sdiv i32 [[A:%.*]], -1123 -; CHECK-NEXT: ret i32 [[Y]] +; CHECK-NEXT: [[TMP1:%.*]] = sdiv i32 [[A:%.*]], -1123 +; CHECK-NEXT: ret i32 [[TMP1]] ; %X = sdiv i32 %A, 1123 %Y = sub i32 0, %X @@ -507,8 +507,8 @@ define i64 @test25(i8* %P, i64 %A){ ; CHECK-LABEL: @test25( ; CHECK-NEXT: [[B_IDX:%.*]] = shl nsw i64 [[A:%.*]], 1 -; CHECK-NEXT: [[DIFF_NEG:%.*]] = add i64 [[B_IDX]], -84 -; CHECK-NEXT: ret i64 [[DIFF_NEG]] +; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[B_IDX]], -84 +; CHECK-NEXT: ret i64 [[TMP1]] ; %B = getelementptr inbounds [42 x i16], [42 x i16]* @Arr, i64 0, i64 %A %C = ptrtoint i16* %B to i64 @@ -522,8 +522,8 @@ ; CHECK-LABEL: @test25_as1( ; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[A:%.*]] to i16 ; CHECK-NEXT: [[B_IDX:%.*]] = shl nsw i16 [[TMP1]], 1 -; CHECK-NEXT: [[DIFF_NEG:%.*]] = add i16 [[B_IDX]], -84 -; CHECK-NEXT: ret i16 [[DIFF_NEG]] +; CHECK-NEXT: [[TMP2:%.*]] = add i16 [[B_IDX]], -84 +; CHECK-NEXT: ret i16 [[TMP2]] ; %B = getelementptr inbounds [42 x i16], [42 x i16] addrspace(1)* @Arr_as1, i64 0, i64 %A %C = ptrtoint i16 addrspace(1)* %B to i16 @@ -533,8 +533,8 @@ define i32 @test26(i32 %x) { ; CHECK-LABEL: @test26( -; CHECK-NEXT: [[NEG:%.*]] = shl i32 -3, [[X:%.*]] -; CHECK-NEXT: ret i32 [[NEG]] +; CHECK-NEXT: [[TMP1:%.*]] = shl i32 -3, [[X:%.*]] +; CHECK-NEXT: ret i32 [[TMP1]] ; %shl = shl i32 3, %x %neg = sub i32 0, %shl @@ -739,8 +739,8 @@ define <2 x i32> @test37(<2 x i32> %A) { ; CHECK-LABEL: @test37( ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i32> [[A:%.*]], -; CHECK-NEXT: [[SUB:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i32> -; CHECK-NEXT: ret <2 x i32> [[SUB]] +; CHECK-NEXT: [[TMP2:%.*]] = sext <2 x i1> [[TMP1]] to <2 x i32> +; CHECK-NEXT: ret <2 x i32> [[TMP2]] ; %div = sdiv <2 x i32> %A, %sub = sub nsw <2 x i32> zeroinitializer, %div @@ -750,8 +750,8 @@ define i32 @test38(i32 %A) { ; CHECK-LABEL: @test38( ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[A:%.*]], -2147483648 -; CHECK-NEXT: [[SUB:%.*]] = sext i1 [[TMP1]] to i32 -; CHECK-NEXT: ret i32 [[SUB]] +; CHECK-NEXT: [[TMP2:%.*]] = sext i1 [[TMP1]] to i32 +; CHECK-NEXT: ret i32 [[TMP2]] ; %div = sdiv i32 %A, -2147483648 %sub = sub nsw i32 0, %div @@ -812,7 +812,7 @@ define i32 @test44(i32 %x) { ; CHECK-LABEL: @test44( -; CHECK-NEXT: [[SUB:%.*]] = add nsw i32 [[X:%.*]], -32768 +; CHECK-NEXT: [[SUB:%.*]] = add i32 [[X:%.*]], -32768 ; CHECK-NEXT: ret i32 [[SUB]] ; %sub = sub nsw i32 %x, 32768 @@ -1185,8 +1185,8 @@ ; CHECK-LABEL: @test64( ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[X:%.*]], 255 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 255 -; CHECK-NEXT: [[RES:%.*]] = add nsw i32 [[TMP2]], 1 -; CHECK-NEXT: ret i32 [[RES]] +; CHECK-NEXT: [[TMP3:%.*]] = add nsw i32 [[TMP2]], 1 +; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = xor i32 %x, -1 %2 = icmp sgt i32 %1, -256 @@ -1199,8 +1199,8 @@ ; CHECK-LABEL: @test65( ; CHECK-NEXT: [[TMP1:%.*]] = icmp sgt i32 [[X:%.*]], -256 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -256 -; CHECK-NEXT: [[RES:%.*]] = add i32 [[TMP2]], 1 -; CHECK-NEXT: ret i32 [[RES]] +; CHECK-NEXT: [[TMP3:%.*]] = add i32 [[TMP2]], 1 +; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = xor i32 %x, -1 %2 = icmp slt i32 %1, 255 @@ -1213,8 +1213,8 @@ ; CHECK-LABEL: @test66( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 [[X:%.*]], -101 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 -101 -; CHECK-NEXT: [[RES:%.*]] = add nuw i32 [[TMP2]], 1 -; CHECK-NEXT: ret i32 [[RES]] +; CHECK-NEXT: [[TMP3:%.*]] = add nuw i32 [[TMP2]], 1 +; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = xor i32 %x, -1 %2 = icmp ugt i32 %1, 100 @@ -1227,8 +1227,8 @@ ; CHECK-LABEL: @test67( ; CHECK-NEXT: [[TMP1:%.*]] = icmp ugt i32 [[X:%.*]], 100 ; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[TMP1]], i32 [[X]], i32 100 -; CHECK-NEXT: [[RES:%.*]] = add i32 [[TMP2]], 1 -; CHECK-NEXT: ret i32 [[RES]] +; CHECK-NEXT: [[TMP3:%.*]] = add i32 [[TMP2]], 1 +; CHECK-NEXT: ret i32 [[TMP3]] ; %1 = xor i32 %x, -1 %2 = icmp ult i32 %1, -101 @@ -1242,8 +1242,8 @@ ; CHECK-LABEL: @test68( ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], ; CHECK-NEXT: [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> -; CHECK-NEXT: [[RES:%.*]] = add nsw <2 x i32> [[TMP2]], -; CHECK-NEXT: ret <2 x i32> [[RES]] +; CHECK-NEXT: [[TMP3:%.*]] = add nsw <2 x i32> [[TMP2]], +; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %1 = xor <2 x i32> %x, %2 = icmp sgt <2 x i32> %1, @@ -1257,8 +1257,8 @@ ; CHECK-LABEL: @test69( ; CHECK-NEXT: [[TMP1:%.*]] = icmp slt <2 x i32> [[X:%.*]], ; CHECK-NEXT: [[TMP2:%.*]] = select <2 x i1> [[TMP1]], <2 x i32> [[X]], <2 x i32> -; CHECK-NEXT: [[RES:%.*]] = add <2 x i32> [[TMP2]], -; CHECK-NEXT: ret <2 x i32> [[RES]] +; CHECK-NEXT: [[TMP3:%.*]] = add <2 x i32> [[TMP2]], +; CHECK-NEXT: ret <2 x i32> [[TMP3]] ; %1 = xor <2 x i32> %x, %2 = icmp sgt <2 x i32> %1, diff --git a/llvm/test/Transforms/InstCombine/unsigned_saturated_sub.ll b/llvm/test/Transforms/InstCombine/unsigned_saturated_sub.ll --- a/llvm/test/Transforms/InstCombine/unsigned_saturated_sub.ll +++ b/llvm/test/Transforms/InstCombine/unsigned_saturated_sub.ll @@ -351,7 +351,7 @@ define i32 @max_sub_ult_c2(i32 %a) { ; CHECK-LABEL: @max_sub_ult_c2( ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.usub.sat.i32(i32 2, i32 [[A:%.*]]) -; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; CHECK-NEXT: [[TMP2:%.*]] = sub i32 0, [[TMP1]] ; CHECK-NEXT: ret i32 [[TMP2]] ; %cmp = icmp ult i32 %a, 2 @@ -364,7 +364,7 @@ ; CHECK-LABEL: @max_sub_ult_c2_oneuseicmp( ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[A:%.*]], 2 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.usub.sat.i32(i32 2, i32 [[A]]) -; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; CHECK-NEXT: [[TMP2:%.*]] = sub i32 0, [[TMP1]] ; CHECK-NEXT: call void @usei1(i1 [[CMP]]) ; CHECK-NEXT: ret i32 [[TMP2]] ; @@ -379,7 +379,7 @@ ; CHECK-LABEL: @max_sub_ult_c2_oneusesub( ; CHECK-NEXT: [[SUB:%.*]] = add i32 [[A:%.*]], -2 ; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.usub.sat.i32(i32 2, i32 [[A]]) -; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i32 0, [[TMP1]] +; CHECK-NEXT: [[TMP2:%.*]] = sub i32 0, [[TMP1]] ; CHECK-NEXT: call void @usei32(i32 [[SUB]]) ; CHECK-NEXT: ret i32 [[TMP2]] ; diff --git a/llvm/test/Transforms/InstCombine/zext-bool-add-sub.ll b/llvm/test/Transforms/InstCombine/zext-bool-add-sub.ll --- a/llvm/test/Transforms/InstCombine/zext-bool-add-sub.ll +++ b/llvm/test/Transforms/InstCombine/zext-bool-add-sub.ll @@ -5,9 +5,9 @@ define i32 @a(i1 zeroext %x, i1 zeroext %y) { ; CHECK-LABEL: @a( -; CHECK-NEXT: [[CONV3_NEG:%.*]] = sext i1 [[Y:%.*]] to i32 +; CHECK-NEXT: [[TMP1:%.*]] = sext i1 [[Y:%.*]] to i32 ; CHECK-NEXT: [[SUB:%.*]] = select i1 [[X:%.*]], i32 2, i32 1 -; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], [[CONV3_NEG]] +; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[SUB]], [[TMP1]] ; CHECK-NEXT: ret i32 [[ADD]] ; %conv = zext i1 %x to i32 @@ -95,8 +95,8 @@ define i64 @zext_negate(i1 %A) { ; CHECK-LABEL: @zext_negate( -; CHECK-NEXT: [[SUB:%.*]] = sext i1 [[A:%.*]] to i64 -; CHECK-NEXT: ret i64 [[SUB]] +; CHECK-NEXT: [[TMP1:%.*]] = sext i1 [[A:%.*]] to i64 +; CHECK-NEXT: ret i64 [[TMP1]] ; %ext = zext i1 %A to i64 %sub = sub i64 0, %ext @@ -106,7 +106,7 @@ define i64 @zext_negate_extra_use(i1 %A) { ; CHECK-LABEL: @zext_negate_extra_use( ; CHECK-NEXT: [[EXT:%.*]] = zext i1 [[A:%.*]] to i64 -; CHECK-NEXT: [[SUB:%.*]] = sext i1 [[A]] to i64 +; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[EXT]] ; CHECK-NEXT: call void @use(i64 [[EXT]]) ; CHECK-NEXT: ret i64 [[SUB]] ; @@ -118,8 +118,8 @@ define <2 x i64> @zext_negate_vec(<2 x i1> %A) { ; CHECK-LABEL: @zext_negate_vec( -; CHECK-NEXT: [[SUB:%.*]] = sext <2 x i1> [[A:%.*]] to <2 x i64> -; CHECK-NEXT: ret <2 x i64> [[SUB]] +; CHECK-NEXT: [[TMP1:%.*]] = sext <2 x i1> [[A:%.*]] to <2 x i64> +; CHECK-NEXT: ret <2 x i64> [[TMP1]] ; %ext = zext <2 x i1> %A to <2 x i64> %sub = sub <2 x i64> zeroinitializer, %ext @@ -128,8 +128,8 @@ define <2 x i64> @zext_negate_vec_undef_elt(<2 x i1> %A) { ; CHECK-LABEL: @zext_negate_vec_undef_elt( -; CHECK-NEXT: [[SUB:%.*]] = sext <2 x i1> [[A:%.*]] to <2 x i64> -; CHECK-NEXT: ret <2 x i64> [[SUB]] +; CHECK-NEXT: [[TMP1:%.*]] = sext <2 x i1> [[A:%.*]] to <2 x i64> +; CHECK-NEXT: ret <2 x i64> [[TMP1]] ; %ext = zext <2 x i1> %A to <2 x i64> %sub = sub <2 x i64> , %ext @@ -181,8 +181,8 @@ define i64 @sext_negate(i1 %A) { ; CHECK-LABEL: @sext_negate( -; CHECK-NEXT: [[SUB:%.*]] = zext i1 [[A:%.*]] to i64 -; CHECK-NEXT: ret i64 [[SUB]] +; CHECK-NEXT: [[TMP1:%.*]] = zext i1 [[A:%.*]] to i64 +; CHECK-NEXT: ret i64 [[TMP1]] ; %ext = sext i1 %A to i64 %sub = sub i64 0, %ext @@ -192,7 +192,7 @@ define i64 @sext_negate_extra_use(i1 %A) { ; CHECK-LABEL: @sext_negate_extra_use( ; CHECK-NEXT: [[EXT:%.*]] = sext i1 [[A:%.*]] to i64 -; CHECK-NEXT: [[SUB:%.*]] = zext i1 [[A]] to i64 +; CHECK-NEXT: [[SUB:%.*]] = sub i64 0, [[EXT]] ; CHECK-NEXT: call void @use(i64 [[EXT]]) ; CHECK-NEXT: ret i64 [[SUB]] ; @@ -204,8 +204,8 @@ define <2 x i64> @sext_negate_vec(<2 x i1> %A) { ; CHECK-LABEL: @sext_negate_vec( -; CHECK-NEXT: [[SUB:%.*]] = zext <2 x i1> [[A:%.*]] to <2 x i64> -; CHECK-NEXT: ret <2 x i64> [[SUB]] +; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i1> [[A:%.*]] to <2 x i64> +; CHECK-NEXT: ret <2 x i64> [[TMP1]] ; %ext = sext <2 x i1> %A to <2 x i64> %sub = sub <2 x i64> zeroinitializer, %ext @@ -214,8 +214,8 @@ define <2 x i64> @sext_negate_vec_undef_elt(<2 x i1> %A) { ; CHECK-LABEL: @sext_negate_vec_undef_elt( -; CHECK-NEXT: [[SUB:%.*]] = zext <2 x i1> [[A:%.*]] to <2 x i64> -; CHECK-NEXT: ret <2 x i64> [[SUB]] +; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i1> [[A:%.*]] to <2 x i64> +; CHECK-NEXT: ret <2 x i64> [[TMP1]] ; %ext = sext <2 x i1> %A to <2 x i64> %sub = sub <2 x i64> , %ext @@ -294,7 +294,7 @@ define <2 x i8> @sext_sub_vec_nsw(<2 x i8> %x, <2 x i1> %y) { ; CHECK-LABEL: @sext_sub_vec_nsw( ; CHECK-NEXT: [[TMP1:%.*]] = zext <2 x i1> [[Y:%.*]] to <2 x i8> -; CHECK-NEXT: [[SUB:%.*]] = add nsw <2 x i8> [[TMP1]], [[X:%.*]] +; CHECK-NEXT: [[SUB:%.*]] = add <2 x i8> [[TMP1]], [[X:%.*]] ; CHECK-NEXT: ret <2 x i8> [[SUB]] ; %sext = sext <2 x i1> %y to <2 x i8>