Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
35266 views
//===- InstCombineNegator.cpp -----------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements sinking of negation into expression trees,9// as long as that can be done without increasing instruction count.10//11//===----------------------------------------------------------------------===//1213#include "InstCombineInternal.h"14#include "llvm/ADT/APInt.h"15#include "llvm/ADT/ArrayRef.h"16#include "llvm/ADT/DenseMap.h"17#include "llvm/ADT/STLExtras.h"18#include "llvm/ADT/SmallVector.h"19#include "llvm/ADT/Statistic.h"20#include "llvm/ADT/StringRef.h"21#include "llvm/ADT/Twine.h"22#include "llvm/Analysis/TargetFolder.h"23#include "llvm/Analysis/ValueTracking.h"24#include "llvm/IR/Constant.h"25#include "llvm/IR/Constants.h"26#include "llvm/IR/DebugLoc.h"27#include "llvm/IR/IRBuilder.h"28#include "llvm/IR/Instruction.h"29#include "llvm/IR/Instructions.h"30#include "llvm/IR/PatternMatch.h"31#include "llvm/IR/Type.h"32#include "llvm/IR/Use.h"33#include "llvm/IR/User.h"34#include "llvm/IR/Value.h"35#include "llvm/Support/Casting.h"36#include "llvm/Support/CommandLine.h"37#include "llvm/Support/Compiler.h"38#include "llvm/Support/DebugCounter.h"39#include "llvm/Support/ErrorHandling.h"40#include "llvm/Support/raw_ostream.h"41#include "llvm/Transforms/InstCombine/InstCombiner.h"42#include <cassert>43#include <cstdint>44#include <functional>45#include <type_traits>46#include <utility>4748namespace llvm {49class DataLayout;50class LLVMContext;51} // namespace llvm5253using namespace llvm;5455#define DEBUG_TYPE "instcombine"5657STATISTIC(NegatorTotalNegationsAttempted,58"Negator: Number of negations attempted to be sinked");59STATISTIC(NegatorNumTreesNegated,60"Negator: Number of negations successfully sinked");61STATISTIC(NegatorMaxDepthVisited, "Negator: Maximal traversal depth ever "62"reached while attempting to sink negation");63STATISTIC(NegatorTimesDepthLimitReached,64"Negator: How many times did the traversal depth limit was reached "65"during sinking");66STATISTIC(67NegatorNumValuesVisited,68"Negator: Total number of values visited during attempts to sink negation");69STATISTIC(NegatorNumNegationsFoundInCache,70"Negator: How many negations did we retrieve/reuse from cache");71STATISTIC(NegatorMaxTotalValuesVisited,72"Negator: Maximal number of values ever visited while attempting to "73"sink negation");74STATISTIC(NegatorNumInstructionsCreatedTotal,75"Negator: Number of new negated instructions created, total");76STATISTIC(NegatorMaxInstructionsCreated,77"Negator: Maximal number of new instructions created during negation "78"attempt");79STATISTIC(NegatorNumInstructionsNegatedSuccess,80"Negator: Number of new negated instructions created in successful "81"negation sinking attempts");8283DEBUG_COUNTER(NegatorCounter, "instcombine-negator",84"Controls Negator transformations in InstCombine pass");8586static cl::opt<bool>87NegatorEnabled("instcombine-negator-enabled", cl::init(true),88cl::desc("Should we attempt to sink negations?"));8990static cl::opt<unsigned>91NegatorMaxDepth("instcombine-negator-max-depth",92cl::init(NegatorDefaultMaxDepth),93cl::desc("What is the maximal lookup depth when trying to "94"check for viability of negation sinking."));9596Negator::Negator(LLVMContext &C, const DataLayout &DL, bool IsTrulyNegation_)97: Builder(C, TargetFolder(DL),98IRBuilderCallbackInserter([&](Instruction *I) {99++NegatorNumInstructionsCreatedTotal;100NewInstructions.push_back(I);101})),102IsTrulyNegation(IsTrulyNegation_) {}103104#if LLVM_ENABLE_STATS105Negator::~Negator() {106NegatorMaxTotalValuesVisited.updateMax(NumValuesVisitedInThisNegator);107}108#endif109110// Due to the InstCombine's worklist management, there are no guarantees that111// each instruction we'll encounter has been visited by InstCombine already.112// In particular, most importantly for us, that means we have to canonicalize113// constants to RHS ourselves, since that is helpful sometimes.114std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {115assert(I->getNumOperands() == 2 && "Only for binops!");116std::array<Value *, 2> Ops{I->getOperand(0), I->getOperand(1)};117if (I->isCommutative() && InstCombiner::getComplexity(I->getOperand(0)) <118InstCombiner::getComplexity(I->getOperand(1)))119std::swap(Ops[0], Ops[1]);120return Ops;121}122123// FIXME: can this be reworked into a worklist-based algorithm while preserving124// the depth-first, early bailout traversal?125[[nodiscard]] Value *Negator::visitImpl(Value *V, bool IsNSW, unsigned Depth) {126// -(undef) -> undef.127if (match(V, m_Undef()))128return V;129130// In i1, negation can simply be ignored.131if (V->getType()->isIntOrIntVectorTy(1))132return V;133134Value *X;135136// -(-(X)) -> X.137if (match(V, m_Neg(m_Value(X))))138return X;139140// Integral constants can be freely negated.141if (match(V, m_AnyIntegralConstant()))142return ConstantExpr::getNeg(cast<Constant>(V),143/*HasNSW=*/false);144145// If we have a non-instruction, then give up.146if (!isa<Instruction>(V))147return nullptr;148149// If we have started with a true negation (i.e. `sub 0, %y`), then if we've150// got instruction that does not require recursive reasoning, we can still151// negate it even if it has other uses, without increasing instruction count.152if (!V->hasOneUse() && !IsTrulyNegation)153return nullptr;154155auto *I = cast<Instruction>(V);156unsigned BitWidth = I->getType()->getScalarSizeInBits();157158// We must preserve the insertion point and debug info that is set in the159// builder at the time this function is called.160InstCombiner::BuilderTy::InsertPointGuard Guard(Builder);161// And since we are trying to negate instruction I, that tells us about the162// insertion point and the debug info that we need to keep.163Builder.SetInsertPoint(I);164165// In some cases we can give the answer without further recursion.166switch (I->getOpcode()) {167case Instruction::Add: {168std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);169// `inc` is always negatible.170if (match(Ops[1], m_One()))171return Builder.CreateNot(Ops[0], I->getName() + ".neg");172break;173}174case Instruction::Xor:175// `not` is always negatible.176if (match(I, m_Not(m_Value(X))))177return Builder.CreateAdd(X, ConstantInt::get(X->getType(), 1),178I->getName() + ".neg");179break;180case Instruction::AShr:181case Instruction::LShr: {182// Right-shift sign bit smear is negatible.183const APInt *Op1Val;184if (match(I->getOperand(1), m_APInt(Op1Val)) && *Op1Val == BitWidth - 1) {185Value *BO = I->getOpcode() == Instruction::AShr186? Builder.CreateLShr(I->getOperand(0), I->getOperand(1))187: Builder.CreateAShr(I->getOperand(0), I->getOperand(1));188if (auto *NewInstr = dyn_cast<Instruction>(BO)) {189NewInstr->copyIRFlags(I);190NewInstr->setName(I->getName() + ".neg");191}192return BO;193}194// While we could negate exact arithmetic shift:195// ashr exact %x, C --> sdiv exact i8 %x, -1<<C196// iff C != 0 and C u< bitwidth(%x), we don't want to,197// because division is *THAT* much worse than a shift.198break;199}200case Instruction::SExt:201case Instruction::ZExt:202// `*ext` of i1 is always negatible203if (I->getOperand(0)->getType()->isIntOrIntVectorTy(1))204return I->getOpcode() == Instruction::SExt205? Builder.CreateZExt(I->getOperand(0), I->getType(),206I->getName() + ".neg")207: Builder.CreateSExt(I->getOperand(0), I->getType(),208I->getName() + ".neg");209break;210case Instruction::Select: {211// If both arms of the select are constants, we don't need to recurse.212// Therefore, this transform is not limited by uses.213auto *Sel = cast<SelectInst>(I);214Constant *TrueC, *FalseC;215if (match(Sel->getTrueValue(), m_ImmConstant(TrueC)) &&216match(Sel->getFalseValue(), m_ImmConstant(FalseC))) {217Constant *NegTrueC = ConstantExpr::getNeg(TrueC);218Constant *NegFalseC = ConstantExpr::getNeg(FalseC);219return Builder.CreateSelect(Sel->getCondition(), NegTrueC, NegFalseC,220I->getName() + ".neg", /*MDFrom=*/I);221}222break;223}224case Instruction::Call:225if (auto *CI = dyn_cast<CmpIntrinsic>(I); CI && CI->hasOneUse())226return Builder.CreateIntrinsic(CI->getType(), CI->getIntrinsicID(),227{CI->getRHS(), CI->getLHS()});228break;229default:230break; // Other instructions require recursive reasoning.231}232233if (I->getOpcode() == Instruction::Sub &&234(I->hasOneUse() || match(I->getOperand(0), m_ImmConstant()))) {235// `sub` is always negatible.236// However, only do this either if the old `sub` doesn't stick around, or237// it was subtracting from a constant. Otherwise, this isn't profitable.238return Builder.CreateSub(I->getOperand(1), I->getOperand(0),239I->getName() + ".neg", /* HasNUW */ false,240IsNSW && I->hasNoSignedWrap());241}242243// Some other cases, while still don't require recursion,244// are restricted to the one-use case.245if (!V->hasOneUse())246return nullptr;247248switch (I->getOpcode()) {249case Instruction::ZExt: {250// Negation of zext of signbit is signbit splat:251// 0 - (zext (i8 X u>> 7) to iN) --> sext (i8 X s>> 7) to iN252Value *SrcOp = I->getOperand(0);253unsigned SrcWidth = SrcOp->getType()->getScalarSizeInBits();254const APInt &FullShift = APInt(SrcWidth, SrcWidth - 1);255if (IsTrulyNegation &&256match(SrcOp, m_LShr(m_Value(X), m_SpecificIntAllowPoison(FullShift)))) {257Value *Ashr = Builder.CreateAShr(X, FullShift);258return Builder.CreateSExt(Ashr, I->getType());259}260break;261}262case Instruction::And: {263Constant *ShAmt;264// sub(y,and(lshr(x,C),1)) --> add(ashr(shl(x,(BW-1)-C),BW-1),y)265if (match(I, m_And(m_OneUse(m_TruncOrSelf(266m_LShr(m_Value(X), m_ImmConstant(ShAmt)))),267m_One()))) {268unsigned BW = X->getType()->getScalarSizeInBits();269Constant *BWMinusOne = ConstantInt::get(X->getType(), BW - 1);270Value *R = Builder.CreateShl(X, Builder.CreateSub(BWMinusOne, ShAmt));271R = Builder.CreateAShr(R, BWMinusOne);272return Builder.CreateTruncOrBitCast(R, I->getType());273}274break;275}276case Instruction::SDiv:277// `sdiv` is negatible if divisor is not undef/INT_MIN/1.278// While this is normally not behind a use-check,279// let's consider division to be special since it's costly.280if (auto *Op1C = dyn_cast<Constant>(I->getOperand(1))) {281if (!Op1C->containsUndefOrPoisonElement() &&282Op1C->isNotMinSignedValue() && Op1C->isNotOneValue()) {283Value *BO =284Builder.CreateSDiv(I->getOperand(0), ConstantExpr::getNeg(Op1C),285I->getName() + ".neg");286if (auto *NewInstr = dyn_cast<Instruction>(BO))287NewInstr->setIsExact(I->isExact());288return BO;289}290}291break;292}293294// Rest of the logic is recursive, so if it's time to give up then it's time.295if (Depth > NegatorMaxDepth) {296LLVM_DEBUG(dbgs() << "Negator: reached maximal allowed traversal depth in "297<< *V << ". Giving up.\n");298++NegatorTimesDepthLimitReached;299return nullptr;300}301302switch (I->getOpcode()) {303case Instruction::Freeze: {304// `freeze` is negatible if its operand is negatible.305Value *NegOp = negate(I->getOperand(0), IsNSW, Depth + 1);306if (!NegOp) // Early return.307return nullptr;308return Builder.CreateFreeze(NegOp, I->getName() + ".neg");309}310case Instruction::PHI: {311// `phi` is negatible if all the incoming values are negatible.312auto *PHI = cast<PHINode>(I);313SmallVector<Value *, 4> NegatedIncomingValues(PHI->getNumOperands());314for (auto I : zip(PHI->incoming_values(), NegatedIncomingValues)) {315if (!(std::get<1>(I) =316negate(std::get<0>(I), IsNSW, Depth + 1))) // Early return.317return nullptr;318}319// All incoming values are indeed negatible. Create negated PHI node.320PHINode *NegatedPHI = Builder.CreatePHI(321PHI->getType(), PHI->getNumOperands(), PHI->getName() + ".neg");322for (auto I : zip(NegatedIncomingValues, PHI->blocks()))323NegatedPHI->addIncoming(std::get<0>(I), std::get<1>(I));324return NegatedPHI;325}326case Instruction::Select: {327if (isKnownNegation(I->getOperand(1), I->getOperand(2), /*NeedNSW=*/false,328/*AllowPoison=*/false)) {329// Of one hand of select is known to be negation of another hand,330// just swap the hands around.331auto *NewSelect = cast<SelectInst>(I->clone());332// Just swap the operands of the select.333NewSelect->swapValues();334// Don't swap prof metadata, we didn't change the branch behavior.335NewSelect->setName(I->getName() + ".neg");336// Poison-generating flags should be dropped337Value *TV = NewSelect->getTrueValue();338Value *FV = NewSelect->getFalseValue();339if (match(TV, m_Neg(m_Specific(FV))))340cast<Instruction>(TV)->dropPoisonGeneratingFlags();341else if (match(FV, m_Neg(m_Specific(TV))))342cast<Instruction>(FV)->dropPoisonGeneratingFlags();343else {344cast<Instruction>(TV)->dropPoisonGeneratingFlags();345cast<Instruction>(FV)->dropPoisonGeneratingFlags();346}347Builder.Insert(NewSelect);348return NewSelect;349}350// `select` is negatible if both hands of `select` are negatible.351Value *NegOp1 = negate(I->getOperand(1), IsNSW, Depth + 1);352if (!NegOp1) // Early return.353return nullptr;354Value *NegOp2 = negate(I->getOperand(2), IsNSW, Depth + 1);355if (!NegOp2)356return nullptr;357// Do preserve the metadata!358return Builder.CreateSelect(I->getOperand(0), NegOp1, NegOp2,359I->getName() + ".neg", /*MDFrom=*/I);360}361case Instruction::ShuffleVector: {362// `shufflevector` is negatible if both operands are negatible.363auto *Shuf = cast<ShuffleVectorInst>(I);364Value *NegOp0 = negate(I->getOperand(0), IsNSW, Depth + 1);365if (!NegOp0) // Early return.366return nullptr;367Value *NegOp1 = negate(I->getOperand(1), IsNSW, Depth + 1);368if (!NegOp1)369return nullptr;370return Builder.CreateShuffleVector(NegOp0, NegOp1, Shuf->getShuffleMask(),371I->getName() + ".neg");372}373case Instruction::ExtractElement: {374// `extractelement` is negatible if source operand is negatible.375auto *EEI = cast<ExtractElementInst>(I);376Value *NegVector = negate(EEI->getVectorOperand(), IsNSW, Depth + 1);377if (!NegVector) // Early return.378return nullptr;379return Builder.CreateExtractElement(NegVector, EEI->getIndexOperand(),380I->getName() + ".neg");381}382case Instruction::InsertElement: {383// `insertelement` is negatible if both the source vector and384// element-to-be-inserted are negatible.385auto *IEI = cast<InsertElementInst>(I);386Value *NegVector = negate(IEI->getOperand(0), IsNSW, Depth + 1);387if (!NegVector) // Early return.388return nullptr;389Value *NegNewElt = negate(IEI->getOperand(1), IsNSW, Depth + 1);390if (!NegNewElt) // Early return.391return nullptr;392return Builder.CreateInsertElement(NegVector, NegNewElt, IEI->getOperand(2),393I->getName() + ".neg");394}395case Instruction::Trunc: {396// `trunc` is negatible if its operand is negatible.397Value *NegOp = negate(I->getOperand(0), /* IsNSW */ false, Depth + 1);398if (!NegOp) // Early return.399return nullptr;400return Builder.CreateTrunc(NegOp, I->getType(), I->getName() + ".neg");401}402case Instruction::Shl: {403// `shl` is negatible if the first operand is negatible.404IsNSW &= I->hasNoSignedWrap();405if (Value *NegOp0 = negate(I->getOperand(0), IsNSW, Depth + 1))406return Builder.CreateShl(NegOp0, I->getOperand(1), I->getName() + ".neg",407/* HasNUW */ false, IsNSW);408// Otherwise, `shl %x, C` can be interpreted as `mul %x, 1<<C`.409Constant *Op1C;410if (!match(I->getOperand(1), m_ImmConstant(Op1C)) || !IsTrulyNegation)411return nullptr;412return Builder.CreateMul(413I->getOperand(0),414Builder.CreateShl(Constant::getAllOnesValue(Op1C->getType()), Op1C),415I->getName() + ".neg", /* HasNUW */ false, IsNSW);416}417case Instruction::Or: {418if (!cast<PossiblyDisjointInst>(I)->isDisjoint())419return nullptr; // Don't know how to handle `or` in general.420std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);421// `or`/`add` are interchangeable when operands have no common bits set.422// `inc` is always negatible.423if (match(Ops[1], m_One()))424return Builder.CreateNot(Ops[0], I->getName() + ".neg");425// Else, just defer to Instruction::Add handling.426[[fallthrough]];427}428case Instruction::Add: {429// `add` is negatible if both of its operands are negatible.430SmallVector<Value *, 2> NegatedOps, NonNegatedOps;431for (Value *Op : I->operands()) {432// Can we sink the negation into this operand?433if (Value *NegOp = negate(Op, /* IsNSW */ false, Depth + 1)) {434NegatedOps.emplace_back(NegOp); // Successfully negated operand!435continue;436}437// Failed to sink negation into this operand. IFF we started from negation438// and we manage to sink negation into one operand, we can still do this.439if (!IsTrulyNegation)440return nullptr;441NonNegatedOps.emplace_back(Op); // Just record which operand that was.442}443assert((NegatedOps.size() + NonNegatedOps.size()) == 2 &&444"Internal consistency check failed.");445// Did we manage to sink negation into both of the operands?446if (NegatedOps.size() == 2) // Then we get to keep the `add`!447return Builder.CreateAdd(NegatedOps[0], NegatedOps[1],448I->getName() + ".neg");449assert(IsTrulyNegation && "We should have early-exited then.");450// Completely failed to sink negation?451if (NonNegatedOps.size() == 2)452return nullptr;453// 0-(a+b) --> (-a)-b454return Builder.CreateSub(NegatedOps[0], NonNegatedOps[0],455I->getName() + ".neg");456}457case Instruction::Xor: {458std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);459// `xor` is negatible if one of its operands is invertible.460// FIXME: InstCombineInverter? But how to connect Inverter and Negator?461if (auto *C = dyn_cast<Constant>(Ops[1])) {462if (IsTrulyNegation) {463Value *Xor = Builder.CreateXor(Ops[0], ConstantExpr::getNot(C));464return Builder.CreateAdd(Xor, ConstantInt::get(Xor->getType(), 1),465I->getName() + ".neg");466}467}468return nullptr;469}470case Instruction::Mul: {471std::array<Value *, 2> Ops = getSortedOperandsOfBinOp(I);472// `mul` is negatible if one of its operands is negatible.473Value *NegatedOp, *OtherOp;474// First try the second operand, in case it's a constant it will be best to475// just invert it instead of sinking the `neg` deeper.476if (Value *NegOp1 = negate(Ops[1], /* IsNSW */ false, Depth + 1)) {477NegatedOp = NegOp1;478OtherOp = Ops[0];479} else if (Value *NegOp0 = negate(Ops[0], /* IsNSW */ false, Depth + 1)) {480NegatedOp = NegOp0;481OtherOp = Ops[1];482} else483// Can't negate either of them.484return nullptr;485return Builder.CreateMul(NegatedOp, OtherOp, I->getName() + ".neg",486/* HasNUW */ false, IsNSW && I->hasNoSignedWrap());487}488default:489return nullptr; // Don't know, likely not negatible for free.490}491492llvm_unreachable("Can't get here. We always return from switch.");493}494495[[nodiscard]] Value *Negator::negate(Value *V, bool IsNSW, unsigned Depth) {496NegatorMaxDepthVisited.updateMax(Depth);497++NegatorNumValuesVisited;498499#if LLVM_ENABLE_STATS500++NumValuesVisitedInThisNegator;501#endif502503#ifndef NDEBUG504// We can't ever have a Value with such an address.505Value *Placeholder = reinterpret_cast<Value *>(static_cast<uintptr_t>(-1));506#endif507508// Did we already try to negate this value?509auto NegationsCacheIterator = NegationsCache.find(V);510if (NegationsCacheIterator != NegationsCache.end()) {511++NegatorNumNegationsFoundInCache;512Value *NegatedV = NegationsCacheIterator->second;513assert(NegatedV != Placeholder && "Encountered a cycle during negation.");514return NegatedV;515}516517#ifndef NDEBUG518// We did not find a cached result for negation of V. While there,519// let's temporairly cache a placeholder value, with the idea that if later520// during negation we fetch it from cache, we'll know we're in a cycle.521NegationsCache[V] = Placeholder;522#endif523524// No luck. Try negating it for real.525Value *NegatedV = visitImpl(V, IsNSW, Depth);526// And cache the (real) result for the future.527NegationsCache[V] = NegatedV;528529return NegatedV;530}531532[[nodiscard]] std::optional<Negator::Result> Negator::run(Value *Root,533bool IsNSW) {534Value *Negated = negate(Root, IsNSW, /*Depth=*/0);535if (!Negated) {536// We must cleanup newly-inserted instructions, to avoid any potential537// endless combine looping.538for (Instruction *I : llvm::reverse(NewInstructions))539I->eraseFromParent();540return std::nullopt;541}542return std::make_pair(ArrayRef<Instruction *>(NewInstructions), Negated);543}544545[[nodiscard]] Value *Negator::Negate(bool LHSIsZero, bool IsNSW, Value *Root,546InstCombinerImpl &IC) {547++NegatorTotalNegationsAttempted;548LLVM_DEBUG(dbgs() << "Negator: attempting to sink negation into " << *Root549<< "\n");550551if (!NegatorEnabled || !DebugCounter::shouldExecute(NegatorCounter))552return nullptr;553554Negator N(Root->getContext(), IC.getDataLayout(), LHSIsZero);555std::optional<Result> Res = N.run(Root, IsNSW);556if (!Res) { // Negation failed.557LLVM_DEBUG(dbgs() << "Negator: failed to sink negation into " << *Root558<< "\n");559return nullptr;560}561562LLVM_DEBUG(dbgs() << "Negator: successfully sunk negation into " << *Root563<< "\n NEW: " << *Res->second << "\n");564++NegatorNumTreesNegated;565566// We must temporarily unset the 'current' insertion point and DebugLoc of the567// InstCombine's IRBuilder so that it won't interfere with the ones we have568// already specified when producing negated instructions.569InstCombiner::BuilderTy::InsertPointGuard Guard(IC.Builder);570IC.Builder.ClearInsertionPoint();571IC.Builder.SetCurrentDebugLocation(DebugLoc());572573// And finally, we must add newly-created instructions into the InstCombine's574// worklist (in a proper order!) so it can attempt to combine them.575LLVM_DEBUG(dbgs() << "Negator: Propagating " << Res->first.size()576<< " instrs to InstCombine\n");577NegatorMaxInstructionsCreated.updateMax(Res->first.size());578NegatorNumInstructionsNegatedSuccess += Res->first.size();579580// They are in def-use order, so nothing fancy, just insert them in order.581for (Instruction *I : Res->first)582IC.Builder.Insert(I, I->getName());583584// And return the new root.585return Res->second;586}587588589