Path: blob/main/contrib/llvm-project/llvm/lib/Analysis/DemandedBits.cpp
35233 views
//===- DemandedBits.cpp - Determine demanded bits -------------------------===//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 pass implements a demanded bits analysis. A demanded bit is one that9// contributes to a result; bits that are not demanded can be either zero or10// one without affecting control or data flow. For example in this sequence:11//12// %1 = add i32 %x, %y13// %2 = trunc i32 %1 to i1614//15// Only the lowest 16 bits of %1 are demanded; the rest are removed by the16// trunc.17//18//===----------------------------------------------------------------------===//1920#include "llvm/Analysis/DemandedBits.h"21#include "llvm/ADT/APInt.h"22#include "llvm/ADT/SetVector.h"23#include "llvm/Analysis/AssumptionCache.h"24#include "llvm/Analysis/ValueTracking.h"25#include "llvm/IR/DataLayout.h"26#include "llvm/IR/Dominators.h"27#include "llvm/IR/InstIterator.h"28#include "llvm/IR/Instruction.h"29#include "llvm/IR/IntrinsicInst.h"30#include "llvm/IR/Module.h"31#include "llvm/IR/Operator.h"32#include "llvm/IR/PassManager.h"33#include "llvm/IR/PatternMatch.h"34#include "llvm/IR/Type.h"35#include "llvm/IR/Use.h"36#include "llvm/Support/Casting.h"37#include "llvm/Support/Debug.h"38#include "llvm/Support/KnownBits.h"39#include "llvm/Support/raw_ostream.h"40#include <algorithm>41#include <cstdint>4243using namespace llvm;44using namespace llvm::PatternMatch;4546#define DEBUG_TYPE "demanded-bits"4748static bool isAlwaysLive(Instruction *I) {49return I->isTerminator() || isa<DbgInfoIntrinsic>(I) || I->isEHPad() ||50I->mayHaveSideEffects();51}5253void DemandedBits::determineLiveOperandBits(54const Instruction *UserI, const Value *Val, unsigned OperandNo,55const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2,56bool &KnownBitsComputed) {57unsigned BitWidth = AB.getBitWidth();5859// We're called once per operand, but for some instructions, we need to60// compute known bits of both operands in order to determine the live bits of61// either (when both operands are instructions themselves). We don't,62// however, want to do this twice, so we cache the result in APInts that live63// in the caller. For the two-relevant-operands case, both operand values are64// provided here.65auto ComputeKnownBits =66[&](unsigned BitWidth, const Value *V1, const Value *V2) {67if (KnownBitsComputed)68return;69KnownBitsComputed = true;7071const DataLayout &DL = UserI->getDataLayout();72Known = KnownBits(BitWidth);73computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);7475if (V2) {76Known2 = KnownBits(BitWidth);77computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);78}79};8081switch (UserI->getOpcode()) {82default: break;83case Instruction::Call:84case Instruction::Invoke:85if (const auto *II = dyn_cast<IntrinsicInst>(UserI)) {86switch (II->getIntrinsicID()) {87default: break;88case Intrinsic::bswap:89// The alive bits of the input are the swapped alive bits of90// the output.91AB = AOut.byteSwap();92break;93case Intrinsic::bitreverse:94// The alive bits of the input are the reversed alive bits of95// the output.96AB = AOut.reverseBits();97break;98case Intrinsic::ctlz:99if (OperandNo == 0) {100// We need some output bits, so we need all bits of the101// input to the left of, and including, the leftmost bit102// known to be one.103ComputeKnownBits(BitWidth, Val, nullptr);104AB = APInt::getHighBitsSet(BitWidth,105std::min(BitWidth, Known.countMaxLeadingZeros()+1));106}107break;108case Intrinsic::cttz:109if (OperandNo == 0) {110// We need some output bits, so we need all bits of the111// input to the right of, and including, the rightmost bit112// known to be one.113ComputeKnownBits(BitWidth, Val, nullptr);114AB = APInt::getLowBitsSet(BitWidth,115std::min(BitWidth, Known.countMaxTrailingZeros()+1));116}117break;118case Intrinsic::fshl:119case Intrinsic::fshr: {120const APInt *SA;121if (OperandNo == 2) {122// Shift amount is modulo the bitwidth. For powers of two we have123// SA % BW == SA & (BW - 1).124if (isPowerOf2_32(BitWidth))125AB = BitWidth - 1;126} else if (match(II->getOperand(2), m_APInt(SA))) {127// Normalize to funnel shift left. APInt shifts of BitWidth are well-128// defined, so no need to special-case zero shifts here.129uint64_t ShiftAmt = SA->urem(BitWidth);130if (II->getIntrinsicID() == Intrinsic::fshr)131ShiftAmt = BitWidth - ShiftAmt;132133if (OperandNo == 0)134AB = AOut.lshr(ShiftAmt);135else if (OperandNo == 1)136AB = AOut.shl(BitWidth - ShiftAmt);137}138break;139}140case Intrinsic::umax:141case Intrinsic::umin:142case Intrinsic::smax:143case Intrinsic::smin:144// If low bits of result are not demanded, they are also not demanded145// for the min/max operands.146AB = APInt::getBitsSetFrom(BitWidth, AOut.countr_zero());147break;148}149}150break;151case Instruction::Add:152if (AOut.isMask()) {153AB = AOut;154} else {155ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));156AB = determineLiveOperandBitsAdd(OperandNo, AOut, Known, Known2);157}158break;159case Instruction::Sub:160if (AOut.isMask()) {161AB = AOut;162} else {163ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));164AB = determineLiveOperandBitsSub(OperandNo, AOut, Known, Known2);165}166break;167case Instruction::Mul:168// Find the highest live output bit. We don't need any more input169// bits than that (adds, and thus subtracts, ripple only to the170// left).171AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits());172break;173case Instruction::Shl:174if (OperandNo == 0) {175const APInt *ShiftAmtC;176if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {177uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);178AB = AOut.lshr(ShiftAmt);179180// If the shift is nuw/nsw, then the high bits are not dead181// (because we've promised that they *must* be zero).182const auto *S = cast<ShlOperator>(UserI);183if (S->hasNoSignedWrap())184AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);185else if (S->hasNoUnsignedWrap())186AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);187}188}189break;190case Instruction::LShr:191if (OperandNo == 0) {192const APInt *ShiftAmtC;193if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {194uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);195AB = AOut.shl(ShiftAmt);196197// If the shift is exact, then the low bits are not dead198// (they must be zero).199if (cast<LShrOperator>(UserI)->isExact())200AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);201}202}203break;204case Instruction::AShr:205if (OperandNo == 0) {206const APInt *ShiftAmtC;207if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {208uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);209AB = AOut.shl(ShiftAmt);210// Because the high input bit is replicated into the211// high-order bits of the result, if we need any of those212// bits, then we must keep the highest input bit.213if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))214.getBoolValue())215AB.setSignBit();216217// If the shift is exact, then the low bits are not dead218// (they must be zero).219if (cast<AShrOperator>(UserI)->isExact())220AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);221}222}223break;224case Instruction::And:225AB = AOut;226227// For bits that are known zero, the corresponding bits in the228// other operand are dead (unless they're both zero, in which229// case they can't both be dead, so just mark the LHS bits as230// dead).231ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));232if (OperandNo == 0)233AB &= ~Known2.Zero;234else235AB &= ~(Known.Zero & ~Known2.Zero);236break;237case Instruction::Or:238AB = AOut;239240// For bits that are known one, the corresponding bits in the241// other operand are dead (unless they're both one, in which242// case they can't both be dead, so just mark the LHS bits as243// dead).244ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));245if (OperandNo == 0)246AB &= ~Known2.One;247else248AB &= ~(Known.One & ~Known2.One);249break;250case Instruction::Xor:251case Instruction::PHI:252AB = AOut;253break;254case Instruction::Trunc:255AB = AOut.zext(BitWidth);256break;257case Instruction::ZExt:258AB = AOut.trunc(BitWidth);259break;260case Instruction::SExt:261AB = AOut.trunc(BitWidth);262// Because the high input bit is replicated into the263// high-order bits of the result, if we need any of those264// bits, then we must keep the highest input bit.265if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),266AOut.getBitWidth() - BitWidth))267.getBoolValue())268AB.setSignBit();269break;270case Instruction::Select:271if (OperandNo != 0)272AB = AOut;273break;274case Instruction::ExtractElement:275if (OperandNo == 0)276AB = AOut;277break;278case Instruction::InsertElement:279case Instruction::ShuffleVector:280if (OperandNo == 0 || OperandNo == 1)281AB = AOut;282break;283}284}285286void DemandedBits::performAnalysis() {287if (Analyzed)288// Analysis already completed for this function.289return;290Analyzed = true;291292Visited.clear();293AliveBits.clear();294DeadUses.clear();295296SmallSetVector<Instruction*, 16> Worklist;297298// Collect the set of "root" instructions that are known live.299for (Instruction &I : instructions(F)) {300if (!isAlwaysLive(&I))301continue;302303LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");304// For integer-valued instructions, set up an initial empty set of alive305// bits and add the instruction to the work list. For other instructions306// add their operands to the work list (for integer values operands, mark307// all bits as live).308Type *T = I.getType();309if (T->isIntOrIntVectorTy()) {310if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second)311Worklist.insert(&I);312313continue;314}315316// Non-integer-typed instructions...317for (Use &OI : I.operands()) {318if (auto *J = dyn_cast<Instruction>(OI)) {319Type *T = J->getType();320if (T->isIntOrIntVectorTy())321AliveBits[J] = APInt::getAllOnes(T->getScalarSizeInBits());322else323Visited.insert(J);324Worklist.insert(J);325}326}327// To save memory, we don't add I to the Visited set here. Instead, we328// check isAlwaysLive on every instruction when searching for dead329// instructions later (we need to check isAlwaysLive for the330// integer-typed instructions anyway).331}332333// Propagate liveness backwards to operands.334while (!Worklist.empty()) {335Instruction *UserI = Worklist.pop_back_val();336337LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);338APInt AOut;339bool InputIsKnownDead = false;340if (UserI->getType()->isIntOrIntVectorTy()) {341AOut = AliveBits[UserI];342LLVM_DEBUG(dbgs() << " Alive Out: 0x"343<< Twine::utohexstr(AOut.getLimitedValue()));344345// If all bits of the output are dead, then all bits of the input346// are also dead.347InputIsKnownDead = !AOut && !isAlwaysLive(UserI);348}349LLVM_DEBUG(dbgs() << "\n");350351KnownBits Known, Known2;352bool KnownBitsComputed = false;353// Compute the set of alive bits for each operand. These are anded into the354// existing set, if any, and if that changes the set of alive bits, the355// operand is added to the work-list.356for (Use &OI : UserI->operands()) {357// We also want to detect dead uses of arguments, but will only store358// demanded bits for instructions.359auto *I = dyn_cast<Instruction>(OI);360if (!I && !isa<Argument>(OI))361continue;362363Type *T = OI->getType();364if (T->isIntOrIntVectorTy()) {365unsigned BitWidth = T->getScalarSizeInBits();366APInt AB = APInt::getAllOnes(BitWidth);367if (InputIsKnownDead) {368AB = APInt(BitWidth, 0);369} else {370// Bits of each operand that are used to compute alive bits of the371// output are alive, all others are dead.372determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB,373Known, Known2, KnownBitsComputed);374375// Keep track of uses which have no demanded bits.376if (AB.isZero())377DeadUses.insert(&OI);378else379DeadUses.erase(&OI);380}381382if (I) {383// If we've added to the set of alive bits (or the operand has not384// been previously visited), then re-queue the operand to be visited385// again.386auto Res = AliveBits.try_emplace(I);387if (Res.second || (AB |= Res.first->second) != Res.first->second) {388Res.first->second = std::move(AB);389Worklist.insert(I);390}391}392} else if (I && Visited.insert(I).second) {393Worklist.insert(I);394}395}396}397}398399APInt DemandedBits::getDemandedBits(Instruction *I) {400performAnalysis();401402auto Found = AliveBits.find(I);403if (Found != AliveBits.end())404return Found->second;405406const DataLayout &DL = I->getDataLayout();407return APInt::getAllOnes(DL.getTypeSizeInBits(I->getType()->getScalarType()));408}409410APInt DemandedBits::getDemandedBits(Use *U) {411Type *T = (*U)->getType();412auto *UserI = cast<Instruction>(U->getUser());413const DataLayout &DL = UserI->getDataLayout();414unsigned BitWidth = DL.getTypeSizeInBits(T->getScalarType());415416// We only track integer uses, everything else produces a mask with all bits417// set418if (!T->isIntOrIntVectorTy())419return APInt::getAllOnes(BitWidth);420421if (isUseDead(U))422return APInt(BitWidth, 0);423424performAnalysis();425426APInt AOut = getDemandedBits(UserI);427APInt AB = APInt::getAllOnes(BitWidth);428KnownBits Known, Known2;429bool KnownBitsComputed = false;430431determineLiveOperandBits(UserI, *U, U->getOperandNo(), AOut, AB, Known,432Known2, KnownBitsComputed);433434return AB;435}436437bool DemandedBits::isInstructionDead(Instruction *I) {438performAnalysis();439440return !Visited.count(I) && !AliveBits.contains(I) && !isAlwaysLive(I);441}442443bool DemandedBits::isUseDead(Use *U) {444// We only track integer uses, everything else is assumed live.445if (!(*U)->getType()->isIntOrIntVectorTy())446return false;447448// Uses by always-live instructions are never dead.449auto *UserI = cast<Instruction>(U->getUser());450if (isAlwaysLive(UserI))451return false;452453performAnalysis();454if (DeadUses.count(U))455return true;456457// If no output bits are demanded, no input bits are demanded and the use458// is dead. These uses might not be explicitly present in the DeadUses map.459if (UserI->getType()->isIntOrIntVectorTy()) {460auto Found = AliveBits.find(UserI);461if (Found != AliveBits.end() && Found->second.isZero())462return true;463}464465return false;466}467468void DemandedBits::print(raw_ostream &OS) {469auto PrintDB = [&](const Instruction *I, const APInt &A, Value *V = nullptr) {470OS << "DemandedBits: 0x" << Twine::utohexstr(A.getLimitedValue())471<< " for ";472if (V) {473V->printAsOperand(OS, false);474OS << " in ";475}476OS << *I << '\n';477};478479OS << "Printing analysis 'Demanded Bits Analysis' for function '" << F.getName() << "':\n";480performAnalysis();481for (auto &KV : AliveBits) {482Instruction *I = KV.first;483PrintDB(I, KV.second);484485for (Use &OI : I->operands()) {486PrintDB(I, getDemandedBits(&OI), OI);487}488}489}490491static APInt determineLiveOperandBitsAddCarry(unsigned OperandNo,492const APInt &AOut,493const KnownBits &LHS,494const KnownBits &RHS,495bool CarryZero, bool CarryOne) {496assert(!(CarryZero && CarryOne) &&497"Carry can't be zero and one at the same time");498499// The following check should be done by the caller, as it also indicates500// that LHS and RHS don't need to be computed.501//502// if (AOut.isMask())503// return AOut;504505// Boundary bits' carry out is unaffected by their carry in.506APInt Bound = (LHS.Zero & RHS.Zero) | (LHS.One & RHS.One);507508// First, the alive carry bits are determined from the alive output bits:509// Let demand ripple to the right but only up to any set bit in Bound.510// AOut = -1----511// Bound = ----1-512// ACarry&~AOut = --111-513APInt RBound = Bound.reverseBits();514APInt RAOut = AOut.reverseBits();515APInt RProp = RAOut + (RAOut | ~RBound);516APInt RACarry = RProp ^ ~RBound;517APInt ACarry = RACarry.reverseBits();518519// Then, the alive input bits are determined from the alive carry bits:520APInt NeededToMaintainCarryZero;521APInt NeededToMaintainCarryOne;522if (OperandNo == 0) {523NeededToMaintainCarryZero = LHS.Zero | ~RHS.Zero;524NeededToMaintainCarryOne = LHS.One | ~RHS.One;525} else {526NeededToMaintainCarryZero = RHS.Zero | ~LHS.Zero;527NeededToMaintainCarryOne = RHS.One | ~LHS.One;528}529530// As in computeForAddCarry531APInt PossibleSumZero = ~LHS.Zero + ~RHS.Zero + !CarryZero;532APInt PossibleSumOne = LHS.One + RHS.One + CarryOne;533534// The below is simplified from535//536// APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);537// APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;538// APInt CarryUnknown = ~(CarryKnownZero | CarryKnownOne);539//540// APInt NeededToMaintainCarry =541// (CarryKnownZero & NeededToMaintainCarryZero) |542// (CarryKnownOne & NeededToMaintainCarryOne) |543// CarryUnknown;544545APInt NeededToMaintainCarry = (~PossibleSumZero | NeededToMaintainCarryZero) &546(PossibleSumOne | NeededToMaintainCarryOne);547548APInt AB = AOut | (ACarry & NeededToMaintainCarry);549return AB;550}551552APInt DemandedBits::determineLiveOperandBitsAdd(unsigned OperandNo,553const APInt &AOut,554const KnownBits &LHS,555const KnownBits &RHS) {556return determineLiveOperandBitsAddCarry(OperandNo, AOut, LHS, RHS, true,557false);558}559560APInt DemandedBits::determineLiveOperandBitsSub(unsigned OperandNo,561const APInt &AOut,562const KnownBits &LHS,563const KnownBits &RHS) {564KnownBits NRHS;565NRHS.Zero = RHS.One;566NRHS.One = RHS.Zero;567return determineLiveOperandBitsAddCarry(OperandNo, AOut, LHS, NRHS, false,568true);569}570571AnalysisKey DemandedBitsAnalysis::Key;572573DemandedBits DemandedBitsAnalysis::run(Function &F,574FunctionAnalysisManager &AM) {575auto &AC = AM.getResult<AssumptionAnalysis>(F);576auto &DT = AM.getResult<DominatorTreeAnalysis>(F);577return DemandedBits(F, AC, DT);578}579580PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,581FunctionAnalysisManager &AM) {582AM.getResult<DemandedBitsAnalysis>(F).print(OS);583return PreservedAnalyses::all();584}585586587