10b57cec5SDimitry Andric //===- DemandedBits.cpp - Determine demanded bits -------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass implements a demanded bits analysis. A demanded bit is one that
100b57cec5SDimitry Andric // contributes to a result; bits that are not demanded can be either zero or
110b57cec5SDimitry Andric // one without affecting control or data flow. For example in this sequence:
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // %1 = add i32 %x, %y
140b57cec5SDimitry Andric // %2 = trunc i32 %1 to i16
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric // Only the lowest 16 bits of %1 are demanded; the rest are removed by the
170b57cec5SDimitry Andric // trunc.
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
200b57cec5SDimitry Andric
210b57cec5SDimitry Andric #include "llvm/Analysis/DemandedBits.h"
220b57cec5SDimitry Andric #include "llvm/ADT/APInt.h"
230b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
240b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
250b57cec5SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
260b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h"
270b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
280b57cec5SDimitry Andric #include "llvm/IR/InstIterator.h"
290b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
300b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
310b57cec5SDimitry Andric #include "llvm/IR/Module.h"
320b57cec5SDimitry Andric #include "llvm/IR/Operator.h"
330b57cec5SDimitry Andric #include "llvm/IR/PassManager.h"
340b57cec5SDimitry Andric #include "llvm/IR/PatternMatch.h"
350b57cec5SDimitry Andric #include "llvm/IR/Type.h"
360b57cec5SDimitry Andric #include "llvm/IR/Use.h"
370b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
380b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
390b57cec5SDimitry Andric #include "llvm/Support/KnownBits.h"
400b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
410b57cec5SDimitry Andric #include <algorithm>
420b57cec5SDimitry Andric #include <cstdint>
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric using namespace llvm;
450b57cec5SDimitry Andric using namespace llvm::PatternMatch;
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric #define DEBUG_TYPE "demanded-bits"
480b57cec5SDimitry Andric
isAlwaysLive(Instruction * I)490b57cec5SDimitry Andric static bool isAlwaysLive(Instruction *I) {
500b57cec5SDimitry Andric return I->isTerminator() || isa<DbgInfoIntrinsic>(I) || I->isEHPad() ||
5128a41182SDimitry Andric I->mayHaveSideEffects();
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric
determineLiveOperandBits(const Instruction * UserI,const Value * Val,unsigned OperandNo,const APInt & AOut,APInt & AB,KnownBits & Known,KnownBits & Known2,bool & KnownBitsComputed)540b57cec5SDimitry Andric void DemandedBits::determineLiveOperandBits(
550b57cec5SDimitry Andric const Instruction *UserI, const Value *Val, unsigned OperandNo,
560b57cec5SDimitry Andric const APInt &AOut, APInt &AB, KnownBits &Known, KnownBits &Known2,
570b57cec5SDimitry Andric bool &KnownBitsComputed) {
580b57cec5SDimitry Andric unsigned BitWidth = AB.getBitWidth();
590b57cec5SDimitry Andric
600b57cec5SDimitry Andric // We're called once per operand, but for some instructions, we need to
610b57cec5SDimitry Andric // compute known bits of both operands in order to determine the live bits of
620b57cec5SDimitry Andric // either (when both operands are instructions themselves). We don't,
630b57cec5SDimitry Andric // however, want to do this twice, so we cache the result in APInts that live
640b57cec5SDimitry Andric // in the caller. For the two-relevant-operands case, both operand values are
650b57cec5SDimitry Andric // provided here.
660b57cec5SDimitry Andric auto ComputeKnownBits =
670b57cec5SDimitry Andric [&](unsigned BitWidth, const Value *V1, const Value *V2) {
680b57cec5SDimitry Andric if (KnownBitsComputed)
690b57cec5SDimitry Andric return;
700b57cec5SDimitry Andric KnownBitsComputed = true;
710b57cec5SDimitry Andric
72*0fca6ea1SDimitry Andric const DataLayout &DL = UserI->getDataLayout();
730b57cec5SDimitry Andric Known = KnownBits(BitWidth);
740b57cec5SDimitry Andric computeKnownBits(V1, Known, DL, 0, &AC, UserI, &DT);
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric if (V2) {
770b57cec5SDimitry Andric Known2 = KnownBits(BitWidth);
780b57cec5SDimitry Andric computeKnownBits(V2, Known2, DL, 0, &AC, UserI, &DT);
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric };
810b57cec5SDimitry Andric
820b57cec5SDimitry Andric switch (UserI->getOpcode()) {
830b57cec5SDimitry Andric default: break;
840b57cec5SDimitry Andric case Instruction::Call:
850b57cec5SDimitry Andric case Instruction::Invoke:
8606c3fb27SDimitry Andric if (const auto *II = dyn_cast<IntrinsicInst>(UserI)) {
870b57cec5SDimitry Andric switch (II->getIntrinsicID()) {
880b57cec5SDimitry Andric default: break;
890b57cec5SDimitry Andric case Intrinsic::bswap:
900b57cec5SDimitry Andric // The alive bits of the input are the swapped alive bits of
910b57cec5SDimitry Andric // the output.
920b57cec5SDimitry Andric AB = AOut.byteSwap();
930b57cec5SDimitry Andric break;
940b57cec5SDimitry Andric case Intrinsic::bitreverse:
950b57cec5SDimitry Andric // The alive bits of the input are the reversed alive bits of
960b57cec5SDimitry Andric // the output.
970b57cec5SDimitry Andric AB = AOut.reverseBits();
980b57cec5SDimitry Andric break;
990b57cec5SDimitry Andric case Intrinsic::ctlz:
1000b57cec5SDimitry Andric if (OperandNo == 0) {
1010b57cec5SDimitry Andric // We need some output bits, so we need all bits of the
1020b57cec5SDimitry Andric // input to the left of, and including, the leftmost bit
1030b57cec5SDimitry Andric // known to be one.
1040b57cec5SDimitry Andric ComputeKnownBits(BitWidth, Val, nullptr);
1050b57cec5SDimitry Andric AB = APInt::getHighBitsSet(BitWidth,
1060b57cec5SDimitry Andric std::min(BitWidth, Known.countMaxLeadingZeros()+1));
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric break;
1090b57cec5SDimitry Andric case Intrinsic::cttz:
1100b57cec5SDimitry Andric if (OperandNo == 0) {
1110b57cec5SDimitry Andric // We need some output bits, so we need all bits of the
1120b57cec5SDimitry Andric // input to the right of, and including, the rightmost bit
1130b57cec5SDimitry Andric // known to be one.
1140b57cec5SDimitry Andric ComputeKnownBits(BitWidth, Val, nullptr);
1150b57cec5SDimitry Andric AB = APInt::getLowBitsSet(BitWidth,
1160b57cec5SDimitry Andric std::min(BitWidth, Known.countMaxTrailingZeros()+1));
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric break;
1190b57cec5SDimitry Andric case Intrinsic::fshl:
1200b57cec5SDimitry Andric case Intrinsic::fshr: {
1210b57cec5SDimitry Andric const APInt *SA;
1220b57cec5SDimitry Andric if (OperandNo == 2) {
1230b57cec5SDimitry Andric // Shift amount is modulo the bitwidth. For powers of two we have
1240b57cec5SDimitry Andric // SA % BW == SA & (BW - 1).
1250b57cec5SDimitry Andric if (isPowerOf2_32(BitWidth))
1260b57cec5SDimitry Andric AB = BitWidth - 1;
1270b57cec5SDimitry Andric } else if (match(II->getOperand(2), m_APInt(SA))) {
1280b57cec5SDimitry Andric // Normalize to funnel shift left. APInt shifts of BitWidth are well-
1290b57cec5SDimitry Andric // defined, so no need to special-case zero shifts here.
1300b57cec5SDimitry Andric uint64_t ShiftAmt = SA->urem(BitWidth);
1310b57cec5SDimitry Andric if (II->getIntrinsicID() == Intrinsic::fshr)
1320b57cec5SDimitry Andric ShiftAmt = BitWidth - ShiftAmt;
1330b57cec5SDimitry Andric
1340b57cec5SDimitry Andric if (OperandNo == 0)
1350b57cec5SDimitry Andric AB = AOut.lshr(ShiftAmt);
1360b57cec5SDimitry Andric else if (OperandNo == 1)
1370b57cec5SDimitry Andric AB = AOut.shl(BitWidth - ShiftAmt);
1380b57cec5SDimitry Andric }
1390b57cec5SDimitry Andric break;
1400b57cec5SDimitry Andric }
141e8d8bef9SDimitry Andric case Intrinsic::umax:
142e8d8bef9SDimitry Andric case Intrinsic::umin:
143e8d8bef9SDimitry Andric case Intrinsic::smax:
144e8d8bef9SDimitry Andric case Intrinsic::smin:
145e8d8bef9SDimitry Andric // If low bits of result are not demanded, they are also not demanded
146e8d8bef9SDimitry Andric // for the min/max operands.
14706c3fb27SDimitry Andric AB = APInt::getBitsSetFrom(BitWidth, AOut.countr_zero());
148e8d8bef9SDimitry Andric break;
149e8d8bef9SDimitry Andric }
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric break;
1520b57cec5SDimitry Andric case Instruction::Add:
153e8d8bef9SDimitry Andric if (AOut.isMask()) {
154e8d8bef9SDimitry Andric AB = AOut;
155e8d8bef9SDimitry Andric } else {
156e8d8bef9SDimitry Andric ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
157e8d8bef9SDimitry Andric AB = determineLiveOperandBitsAdd(OperandNo, AOut, Known, Known2);
158e8d8bef9SDimitry Andric }
159e8d8bef9SDimitry Andric break;
1600b57cec5SDimitry Andric case Instruction::Sub:
161e8d8bef9SDimitry Andric if (AOut.isMask()) {
162e8d8bef9SDimitry Andric AB = AOut;
163e8d8bef9SDimitry Andric } else {
164e8d8bef9SDimitry Andric ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
165e8d8bef9SDimitry Andric AB = determineLiveOperandBitsSub(OperandNo, AOut, Known, Known2);
166e8d8bef9SDimitry Andric }
167e8d8bef9SDimitry Andric break;
1680b57cec5SDimitry Andric case Instruction::Mul:
1690b57cec5SDimitry Andric // Find the highest live output bit. We don't need any more input
1700b57cec5SDimitry Andric // bits than that (adds, and thus subtracts, ripple only to the
1710b57cec5SDimitry Andric // left).
1720b57cec5SDimitry Andric AB = APInt::getLowBitsSet(BitWidth, AOut.getActiveBits());
1730b57cec5SDimitry Andric break;
1740b57cec5SDimitry Andric case Instruction::Shl:
1750b57cec5SDimitry Andric if (OperandNo == 0) {
1760b57cec5SDimitry Andric const APInt *ShiftAmtC;
1770b57cec5SDimitry Andric if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
1780b57cec5SDimitry Andric uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
1790b57cec5SDimitry Andric AB = AOut.lshr(ShiftAmt);
1800b57cec5SDimitry Andric
1810b57cec5SDimitry Andric // If the shift is nuw/nsw, then the high bits are not dead
1820b57cec5SDimitry Andric // (because we've promised that they *must* be zero).
18306c3fb27SDimitry Andric const auto *S = cast<ShlOperator>(UserI);
1840b57cec5SDimitry Andric if (S->hasNoSignedWrap())
1850b57cec5SDimitry Andric AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt+1);
1860b57cec5SDimitry Andric else if (S->hasNoUnsignedWrap())
1870b57cec5SDimitry Andric AB |= APInt::getHighBitsSet(BitWidth, ShiftAmt);
1880b57cec5SDimitry Andric }
1890b57cec5SDimitry Andric }
1900b57cec5SDimitry Andric break;
1910b57cec5SDimitry Andric case Instruction::LShr:
1920b57cec5SDimitry Andric if (OperandNo == 0) {
1930b57cec5SDimitry Andric const APInt *ShiftAmtC;
1940b57cec5SDimitry Andric if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
1950b57cec5SDimitry Andric uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
1960b57cec5SDimitry Andric AB = AOut.shl(ShiftAmt);
1970b57cec5SDimitry Andric
1980b57cec5SDimitry Andric // If the shift is exact, then the low bits are not dead
1990b57cec5SDimitry Andric // (they must be zero).
2000b57cec5SDimitry Andric if (cast<LShrOperator>(UserI)->isExact())
2010b57cec5SDimitry Andric AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
2020b57cec5SDimitry Andric }
2030b57cec5SDimitry Andric }
2040b57cec5SDimitry Andric break;
2050b57cec5SDimitry Andric case Instruction::AShr:
2060b57cec5SDimitry Andric if (OperandNo == 0) {
2070b57cec5SDimitry Andric const APInt *ShiftAmtC;
2080b57cec5SDimitry Andric if (match(UserI->getOperand(1), m_APInt(ShiftAmtC))) {
2090b57cec5SDimitry Andric uint64_t ShiftAmt = ShiftAmtC->getLimitedValue(BitWidth - 1);
2100b57cec5SDimitry Andric AB = AOut.shl(ShiftAmt);
2110b57cec5SDimitry Andric // Because the high input bit is replicated into the
2120b57cec5SDimitry Andric // high-order bits of the result, if we need any of those
2130b57cec5SDimitry Andric // bits, then we must keep the highest input bit.
2140b57cec5SDimitry Andric if ((AOut & APInt::getHighBitsSet(BitWidth, ShiftAmt))
2150b57cec5SDimitry Andric .getBoolValue())
2160b57cec5SDimitry Andric AB.setSignBit();
2170b57cec5SDimitry Andric
2180b57cec5SDimitry Andric // If the shift is exact, then the low bits are not dead
2190b57cec5SDimitry Andric // (they must be zero).
2200b57cec5SDimitry Andric if (cast<AShrOperator>(UserI)->isExact())
2210b57cec5SDimitry Andric AB |= APInt::getLowBitsSet(BitWidth, ShiftAmt);
2220b57cec5SDimitry Andric }
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric break;
2250b57cec5SDimitry Andric case Instruction::And:
2260b57cec5SDimitry Andric AB = AOut;
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric // For bits that are known zero, the corresponding bits in the
2290b57cec5SDimitry Andric // other operand are dead (unless they're both zero, in which
2300b57cec5SDimitry Andric // case they can't both be dead, so just mark the LHS bits as
2310b57cec5SDimitry Andric // dead).
2320b57cec5SDimitry Andric ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
2330b57cec5SDimitry Andric if (OperandNo == 0)
2340b57cec5SDimitry Andric AB &= ~Known2.Zero;
2350b57cec5SDimitry Andric else
2360b57cec5SDimitry Andric AB &= ~(Known.Zero & ~Known2.Zero);
2370b57cec5SDimitry Andric break;
2380b57cec5SDimitry Andric case Instruction::Or:
2390b57cec5SDimitry Andric AB = AOut;
2400b57cec5SDimitry Andric
2410b57cec5SDimitry Andric // For bits that are known one, the corresponding bits in the
2420b57cec5SDimitry Andric // other operand are dead (unless they're both one, in which
2430b57cec5SDimitry Andric // case they can't both be dead, so just mark the LHS bits as
2440b57cec5SDimitry Andric // dead).
2450b57cec5SDimitry Andric ComputeKnownBits(BitWidth, UserI->getOperand(0), UserI->getOperand(1));
2460b57cec5SDimitry Andric if (OperandNo == 0)
2470b57cec5SDimitry Andric AB &= ~Known2.One;
2480b57cec5SDimitry Andric else
2490b57cec5SDimitry Andric AB &= ~(Known.One & ~Known2.One);
2500b57cec5SDimitry Andric break;
2510b57cec5SDimitry Andric case Instruction::Xor:
2520b57cec5SDimitry Andric case Instruction::PHI:
2530b57cec5SDimitry Andric AB = AOut;
2540b57cec5SDimitry Andric break;
2550b57cec5SDimitry Andric case Instruction::Trunc:
2560b57cec5SDimitry Andric AB = AOut.zext(BitWidth);
2570b57cec5SDimitry Andric break;
2580b57cec5SDimitry Andric case Instruction::ZExt:
2590b57cec5SDimitry Andric AB = AOut.trunc(BitWidth);
2600b57cec5SDimitry Andric break;
2610b57cec5SDimitry Andric case Instruction::SExt:
2620b57cec5SDimitry Andric AB = AOut.trunc(BitWidth);
2630b57cec5SDimitry Andric // Because the high input bit is replicated into the
2640b57cec5SDimitry Andric // high-order bits of the result, if we need any of those
2650b57cec5SDimitry Andric // bits, then we must keep the highest input bit.
2660b57cec5SDimitry Andric if ((AOut & APInt::getHighBitsSet(AOut.getBitWidth(),
2670b57cec5SDimitry Andric AOut.getBitWidth() - BitWidth))
2680b57cec5SDimitry Andric .getBoolValue())
2690b57cec5SDimitry Andric AB.setSignBit();
2700b57cec5SDimitry Andric break;
2710b57cec5SDimitry Andric case Instruction::Select:
2720b57cec5SDimitry Andric if (OperandNo != 0)
2730b57cec5SDimitry Andric AB = AOut;
2740b57cec5SDimitry Andric break;
2750b57cec5SDimitry Andric case Instruction::ExtractElement:
2760b57cec5SDimitry Andric if (OperandNo == 0)
2770b57cec5SDimitry Andric AB = AOut;
2780b57cec5SDimitry Andric break;
2790b57cec5SDimitry Andric case Instruction::InsertElement:
2800b57cec5SDimitry Andric case Instruction::ShuffleVector:
2810b57cec5SDimitry Andric if (OperandNo == 0 || OperandNo == 1)
2820b57cec5SDimitry Andric AB = AOut;
2830b57cec5SDimitry Andric break;
2840b57cec5SDimitry Andric }
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric
performAnalysis()2870b57cec5SDimitry Andric void DemandedBits::performAnalysis() {
2880b57cec5SDimitry Andric if (Analyzed)
2890b57cec5SDimitry Andric // Analysis already completed for this function.
2900b57cec5SDimitry Andric return;
2910b57cec5SDimitry Andric Analyzed = true;
2920b57cec5SDimitry Andric
2930b57cec5SDimitry Andric Visited.clear();
2940b57cec5SDimitry Andric AliveBits.clear();
2950b57cec5SDimitry Andric DeadUses.clear();
2960b57cec5SDimitry Andric
2970b57cec5SDimitry Andric SmallSetVector<Instruction*, 16> Worklist;
2980b57cec5SDimitry Andric
2990b57cec5SDimitry Andric // Collect the set of "root" instructions that are known live.
3000b57cec5SDimitry Andric for (Instruction &I : instructions(F)) {
3010b57cec5SDimitry Andric if (!isAlwaysLive(&I))
3020b57cec5SDimitry Andric continue;
3030b57cec5SDimitry Andric
3040b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "DemandedBits: Root: " << I << "\n");
3050b57cec5SDimitry Andric // For integer-valued instructions, set up an initial empty set of alive
3060b57cec5SDimitry Andric // bits and add the instruction to the work list. For other instructions
3070b57cec5SDimitry Andric // add their operands to the work list (for integer values operands, mark
3080b57cec5SDimitry Andric // all bits as live).
3090b57cec5SDimitry Andric Type *T = I.getType();
3100b57cec5SDimitry Andric if (T->isIntOrIntVectorTy()) {
3110b57cec5SDimitry Andric if (AliveBits.try_emplace(&I, T->getScalarSizeInBits(), 0).second)
3120b57cec5SDimitry Andric Worklist.insert(&I);
3130b57cec5SDimitry Andric
3140b57cec5SDimitry Andric continue;
3150b57cec5SDimitry Andric }
3160b57cec5SDimitry Andric
3170b57cec5SDimitry Andric // Non-integer-typed instructions...
3180b57cec5SDimitry Andric for (Use &OI : I.operands()) {
31906c3fb27SDimitry Andric if (auto *J = dyn_cast<Instruction>(OI)) {
3200b57cec5SDimitry Andric Type *T = J->getType();
3210b57cec5SDimitry Andric if (T->isIntOrIntVectorTy())
322349cc55cSDimitry Andric AliveBits[J] = APInt::getAllOnes(T->getScalarSizeInBits());
3230b57cec5SDimitry Andric else
3240b57cec5SDimitry Andric Visited.insert(J);
3250b57cec5SDimitry Andric Worklist.insert(J);
3260b57cec5SDimitry Andric }
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric // To save memory, we don't add I to the Visited set here. Instead, we
3290b57cec5SDimitry Andric // check isAlwaysLive on every instruction when searching for dead
3300b57cec5SDimitry Andric // instructions later (we need to check isAlwaysLive for the
3310b57cec5SDimitry Andric // integer-typed instructions anyway).
3320b57cec5SDimitry Andric }
3330b57cec5SDimitry Andric
3340b57cec5SDimitry Andric // Propagate liveness backwards to operands.
3350b57cec5SDimitry Andric while (!Worklist.empty()) {
3360b57cec5SDimitry Andric Instruction *UserI = Worklist.pop_back_val();
3370b57cec5SDimitry Andric
3380b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "DemandedBits: Visiting: " << *UserI);
3390b57cec5SDimitry Andric APInt AOut;
3400b57cec5SDimitry Andric bool InputIsKnownDead = false;
3410b57cec5SDimitry Andric if (UserI->getType()->isIntOrIntVectorTy()) {
3420b57cec5SDimitry Andric AOut = AliveBits[UserI];
3430b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Alive Out: 0x"
3440b57cec5SDimitry Andric << Twine::utohexstr(AOut.getLimitedValue()));
3450b57cec5SDimitry Andric
3460b57cec5SDimitry Andric // If all bits of the output are dead, then all bits of the input
3470b57cec5SDimitry Andric // are also dead.
3480b57cec5SDimitry Andric InputIsKnownDead = !AOut && !isAlwaysLive(UserI);
3490b57cec5SDimitry Andric }
3500b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n");
3510b57cec5SDimitry Andric
3520b57cec5SDimitry Andric KnownBits Known, Known2;
3530b57cec5SDimitry Andric bool KnownBitsComputed = false;
3540b57cec5SDimitry Andric // Compute the set of alive bits for each operand. These are anded into the
3550b57cec5SDimitry Andric // existing set, if any, and if that changes the set of alive bits, the
3560b57cec5SDimitry Andric // operand is added to the work-list.
3570b57cec5SDimitry Andric for (Use &OI : UserI->operands()) {
3580b57cec5SDimitry Andric // We also want to detect dead uses of arguments, but will only store
3590b57cec5SDimitry Andric // demanded bits for instructions.
36006c3fb27SDimitry Andric auto *I = dyn_cast<Instruction>(OI);
3610b57cec5SDimitry Andric if (!I && !isa<Argument>(OI))
3620b57cec5SDimitry Andric continue;
3630b57cec5SDimitry Andric
3640b57cec5SDimitry Andric Type *T = OI->getType();
3650b57cec5SDimitry Andric if (T->isIntOrIntVectorTy()) {
3660b57cec5SDimitry Andric unsigned BitWidth = T->getScalarSizeInBits();
367349cc55cSDimitry Andric APInt AB = APInt::getAllOnes(BitWidth);
3680b57cec5SDimitry Andric if (InputIsKnownDead) {
3690b57cec5SDimitry Andric AB = APInt(BitWidth, 0);
3700b57cec5SDimitry Andric } else {
3710b57cec5SDimitry Andric // Bits of each operand that are used to compute alive bits of the
3720b57cec5SDimitry Andric // output are alive, all others are dead.
3730b57cec5SDimitry Andric determineLiveOperandBits(UserI, OI, OI.getOperandNo(), AOut, AB,
3740b57cec5SDimitry Andric Known, Known2, KnownBitsComputed);
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andric // Keep track of uses which have no demanded bits.
377349cc55cSDimitry Andric if (AB.isZero())
3780b57cec5SDimitry Andric DeadUses.insert(&OI);
3790b57cec5SDimitry Andric else
3800b57cec5SDimitry Andric DeadUses.erase(&OI);
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric
3830b57cec5SDimitry Andric if (I) {
3840b57cec5SDimitry Andric // If we've added to the set of alive bits (or the operand has not
3850b57cec5SDimitry Andric // been previously visited), then re-queue the operand to be visited
3860b57cec5SDimitry Andric // again.
3870b57cec5SDimitry Andric auto Res = AliveBits.try_emplace(I);
3880b57cec5SDimitry Andric if (Res.second || (AB |= Res.first->second) != Res.first->second) {
3890b57cec5SDimitry Andric Res.first->second = std::move(AB);
3900b57cec5SDimitry Andric Worklist.insert(I);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric }
3930b57cec5SDimitry Andric } else if (I && Visited.insert(I).second) {
3940b57cec5SDimitry Andric Worklist.insert(I);
3950b57cec5SDimitry Andric }
3960b57cec5SDimitry Andric }
3970b57cec5SDimitry Andric }
3980b57cec5SDimitry Andric }
3990b57cec5SDimitry Andric
getDemandedBits(Instruction * I)4000b57cec5SDimitry Andric APInt DemandedBits::getDemandedBits(Instruction *I) {
4010b57cec5SDimitry Andric performAnalysis();
4020b57cec5SDimitry Andric
4030b57cec5SDimitry Andric auto Found = AliveBits.find(I);
4040b57cec5SDimitry Andric if (Found != AliveBits.end())
4050b57cec5SDimitry Andric return Found->second;
4060b57cec5SDimitry Andric
407*0fca6ea1SDimitry Andric const DataLayout &DL = I->getDataLayout();
408349cc55cSDimitry Andric return APInt::getAllOnes(DL.getTypeSizeInBits(I->getType()->getScalarType()));
4090b57cec5SDimitry Andric }
4100b57cec5SDimitry Andric
getDemandedBits(Use * U)411fe6060f1SDimitry Andric APInt DemandedBits::getDemandedBits(Use *U) {
412fe6060f1SDimitry Andric Type *T = (*U)->getType();
41306c3fb27SDimitry Andric auto *UserI = cast<Instruction>(U->getUser());
414*0fca6ea1SDimitry Andric const DataLayout &DL = UserI->getDataLayout();
415fe6060f1SDimitry Andric unsigned BitWidth = DL.getTypeSizeInBits(T->getScalarType());
416fe6060f1SDimitry Andric
417fe6060f1SDimitry Andric // We only track integer uses, everything else produces a mask with all bits
418fe6060f1SDimitry Andric // set
419fe6060f1SDimitry Andric if (!T->isIntOrIntVectorTy())
420349cc55cSDimitry Andric return APInt::getAllOnes(BitWidth);
421fe6060f1SDimitry Andric
422fe6060f1SDimitry Andric if (isUseDead(U))
423fe6060f1SDimitry Andric return APInt(BitWidth, 0);
424fe6060f1SDimitry Andric
425fe6060f1SDimitry Andric performAnalysis();
426fe6060f1SDimitry Andric
427fe6060f1SDimitry Andric APInt AOut = getDemandedBits(UserI);
428349cc55cSDimitry Andric APInt AB = APInt::getAllOnes(BitWidth);
429fe6060f1SDimitry Andric KnownBits Known, Known2;
430fe6060f1SDimitry Andric bool KnownBitsComputed = false;
431fe6060f1SDimitry Andric
432fe6060f1SDimitry Andric determineLiveOperandBits(UserI, *U, U->getOperandNo(), AOut, AB, Known,
433fe6060f1SDimitry Andric Known2, KnownBitsComputed);
434fe6060f1SDimitry Andric
435fe6060f1SDimitry Andric return AB;
436fe6060f1SDimitry Andric }
437fe6060f1SDimitry Andric
isInstructionDead(Instruction * I)4380b57cec5SDimitry Andric bool DemandedBits::isInstructionDead(Instruction *I) {
4390b57cec5SDimitry Andric performAnalysis();
4400b57cec5SDimitry Andric
44106c3fb27SDimitry Andric return !Visited.count(I) && !AliveBits.contains(I) && !isAlwaysLive(I);
4420b57cec5SDimitry Andric }
4430b57cec5SDimitry Andric
isUseDead(Use * U)4440b57cec5SDimitry Andric bool DemandedBits::isUseDead(Use *U) {
4450b57cec5SDimitry Andric // We only track integer uses, everything else is assumed live.
4460b57cec5SDimitry Andric if (!(*U)->getType()->isIntOrIntVectorTy())
4470b57cec5SDimitry Andric return false;
4480b57cec5SDimitry Andric
4490b57cec5SDimitry Andric // Uses by always-live instructions are never dead.
45006c3fb27SDimitry Andric auto *UserI = cast<Instruction>(U->getUser());
4510b57cec5SDimitry Andric if (isAlwaysLive(UserI))
4520b57cec5SDimitry Andric return false;
4530b57cec5SDimitry Andric
4540b57cec5SDimitry Andric performAnalysis();
4550b57cec5SDimitry Andric if (DeadUses.count(U))
4560b57cec5SDimitry Andric return true;
4570b57cec5SDimitry Andric
4580b57cec5SDimitry Andric // If no output bits are demanded, no input bits are demanded and the use
4590b57cec5SDimitry Andric // is dead. These uses might not be explicitly present in the DeadUses map.
4600b57cec5SDimitry Andric if (UserI->getType()->isIntOrIntVectorTy()) {
4610b57cec5SDimitry Andric auto Found = AliveBits.find(UserI);
462349cc55cSDimitry Andric if (Found != AliveBits.end() && Found->second.isZero())
4630b57cec5SDimitry Andric return true;
4640b57cec5SDimitry Andric }
4650b57cec5SDimitry Andric
4660b57cec5SDimitry Andric return false;
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric
print(raw_ostream & OS)4690b57cec5SDimitry Andric void DemandedBits::print(raw_ostream &OS) {
470fe6060f1SDimitry Andric auto PrintDB = [&](const Instruction *I, const APInt &A, Value *V = nullptr) {
471fe6060f1SDimitry Andric OS << "DemandedBits: 0x" << Twine::utohexstr(A.getLimitedValue())
472fe6060f1SDimitry Andric << " for ";
473fe6060f1SDimitry Andric if (V) {
474fe6060f1SDimitry Andric V->printAsOperand(OS, false);
475fe6060f1SDimitry Andric OS << " in ";
476fe6060f1SDimitry Andric }
477fe6060f1SDimitry Andric OS << *I << '\n';
478fe6060f1SDimitry Andric };
479fe6060f1SDimitry Andric
48006c3fb27SDimitry Andric OS << "Printing analysis 'Demanded Bits Analysis' for function '" << F.getName() << "':\n";
4810b57cec5SDimitry Andric performAnalysis();
4820b57cec5SDimitry Andric for (auto &KV : AliveBits) {
483fe6060f1SDimitry Andric Instruction *I = KV.first;
484fe6060f1SDimitry Andric PrintDB(I, KV.second);
485fe6060f1SDimitry Andric
486fe6060f1SDimitry Andric for (Use &OI : I->operands()) {
487fe6060f1SDimitry Andric PrintDB(I, getDemandedBits(&OI), OI);
488fe6060f1SDimitry Andric }
4890b57cec5SDimitry Andric }
4900b57cec5SDimitry Andric }
4910b57cec5SDimitry Andric
determineLiveOperandBitsAddCarry(unsigned OperandNo,const APInt & AOut,const KnownBits & LHS,const KnownBits & RHS,bool CarryZero,bool CarryOne)492e8d8bef9SDimitry Andric static APInt determineLiveOperandBitsAddCarry(unsigned OperandNo,
493e8d8bef9SDimitry Andric const APInt &AOut,
494e8d8bef9SDimitry Andric const KnownBits &LHS,
495e8d8bef9SDimitry Andric const KnownBits &RHS,
496e8d8bef9SDimitry Andric bool CarryZero, bool CarryOne) {
497e8d8bef9SDimitry Andric assert(!(CarryZero && CarryOne) &&
498e8d8bef9SDimitry Andric "Carry can't be zero and one at the same time");
499e8d8bef9SDimitry Andric
500e8d8bef9SDimitry Andric // The following check should be done by the caller, as it also indicates
501e8d8bef9SDimitry Andric // that LHS and RHS don't need to be computed.
502e8d8bef9SDimitry Andric //
503e8d8bef9SDimitry Andric // if (AOut.isMask())
504e8d8bef9SDimitry Andric // return AOut;
505e8d8bef9SDimitry Andric
506e8d8bef9SDimitry Andric // Boundary bits' carry out is unaffected by their carry in.
507e8d8bef9SDimitry Andric APInt Bound = (LHS.Zero & RHS.Zero) | (LHS.One & RHS.One);
508e8d8bef9SDimitry Andric
509e8d8bef9SDimitry Andric // First, the alive carry bits are determined from the alive output bits:
510e8d8bef9SDimitry Andric // Let demand ripple to the right but only up to any set bit in Bound.
511e8d8bef9SDimitry Andric // AOut = -1----
512e8d8bef9SDimitry Andric // Bound = ----1-
513e8d8bef9SDimitry Andric // ACarry&~AOut = --111-
514e8d8bef9SDimitry Andric APInt RBound = Bound.reverseBits();
515e8d8bef9SDimitry Andric APInt RAOut = AOut.reverseBits();
516e8d8bef9SDimitry Andric APInt RProp = RAOut + (RAOut | ~RBound);
517e8d8bef9SDimitry Andric APInt RACarry = RProp ^ ~RBound;
518e8d8bef9SDimitry Andric APInt ACarry = RACarry.reverseBits();
519e8d8bef9SDimitry Andric
520e8d8bef9SDimitry Andric // Then, the alive input bits are determined from the alive carry bits:
521e8d8bef9SDimitry Andric APInt NeededToMaintainCarryZero;
522e8d8bef9SDimitry Andric APInt NeededToMaintainCarryOne;
523e8d8bef9SDimitry Andric if (OperandNo == 0) {
524e8d8bef9SDimitry Andric NeededToMaintainCarryZero = LHS.Zero | ~RHS.Zero;
525e8d8bef9SDimitry Andric NeededToMaintainCarryOne = LHS.One | ~RHS.One;
526e8d8bef9SDimitry Andric } else {
527e8d8bef9SDimitry Andric NeededToMaintainCarryZero = RHS.Zero | ~LHS.Zero;
528e8d8bef9SDimitry Andric NeededToMaintainCarryOne = RHS.One | ~LHS.One;
529e8d8bef9SDimitry Andric }
530e8d8bef9SDimitry Andric
531e8d8bef9SDimitry Andric // As in computeForAddCarry
532e8d8bef9SDimitry Andric APInt PossibleSumZero = ~LHS.Zero + ~RHS.Zero + !CarryZero;
533e8d8bef9SDimitry Andric APInt PossibleSumOne = LHS.One + RHS.One + CarryOne;
534e8d8bef9SDimitry Andric
535e8d8bef9SDimitry Andric // The below is simplified from
536e8d8bef9SDimitry Andric //
537e8d8bef9SDimitry Andric // APInt CarryKnownZero = ~(PossibleSumZero ^ LHS.Zero ^ RHS.Zero);
538e8d8bef9SDimitry Andric // APInt CarryKnownOne = PossibleSumOne ^ LHS.One ^ RHS.One;
539e8d8bef9SDimitry Andric // APInt CarryUnknown = ~(CarryKnownZero | CarryKnownOne);
540e8d8bef9SDimitry Andric //
541e8d8bef9SDimitry Andric // APInt NeededToMaintainCarry =
542e8d8bef9SDimitry Andric // (CarryKnownZero & NeededToMaintainCarryZero) |
543e8d8bef9SDimitry Andric // (CarryKnownOne & NeededToMaintainCarryOne) |
544e8d8bef9SDimitry Andric // CarryUnknown;
545e8d8bef9SDimitry Andric
546e8d8bef9SDimitry Andric APInt NeededToMaintainCarry = (~PossibleSumZero | NeededToMaintainCarryZero) &
547e8d8bef9SDimitry Andric (PossibleSumOne | NeededToMaintainCarryOne);
548e8d8bef9SDimitry Andric
549e8d8bef9SDimitry Andric APInt AB = AOut | (ACarry & NeededToMaintainCarry);
550e8d8bef9SDimitry Andric return AB;
551e8d8bef9SDimitry Andric }
552e8d8bef9SDimitry Andric
determineLiveOperandBitsAdd(unsigned OperandNo,const APInt & AOut,const KnownBits & LHS,const KnownBits & RHS)553e8d8bef9SDimitry Andric APInt DemandedBits::determineLiveOperandBitsAdd(unsigned OperandNo,
554e8d8bef9SDimitry Andric const APInt &AOut,
555e8d8bef9SDimitry Andric const KnownBits &LHS,
556e8d8bef9SDimitry Andric const KnownBits &RHS) {
557e8d8bef9SDimitry Andric return determineLiveOperandBitsAddCarry(OperandNo, AOut, LHS, RHS, true,
558e8d8bef9SDimitry Andric false);
559e8d8bef9SDimitry Andric }
560e8d8bef9SDimitry Andric
determineLiveOperandBitsSub(unsigned OperandNo,const APInt & AOut,const KnownBits & LHS,const KnownBits & RHS)561e8d8bef9SDimitry Andric APInt DemandedBits::determineLiveOperandBitsSub(unsigned OperandNo,
562e8d8bef9SDimitry Andric const APInt &AOut,
563e8d8bef9SDimitry Andric const KnownBits &LHS,
564e8d8bef9SDimitry Andric const KnownBits &RHS) {
565e8d8bef9SDimitry Andric KnownBits NRHS;
566e8d8bef9SDimitry Andric NRHS.Zero = RHS.One;
567e8d8bef9SDimitry Andric NRHS.One = RHS.Zero;
568e8d8bef9SDimitry Andric return determineLiveOperandBitsAddCarry(OperandNo, AOut, LHS, NRHS, false,
569e8d8bef9SDimitry Andric true);
570e8d8bef9SDimitry Andric }
571e8d8bef9SDimitry Andric
5720b57cec5SDimitry Andric AnalysisKey DemandedBitsAnalysis::Key;
5730b57cec5SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)5740b57cec5SDimitry Andric DemandedBits DemandedBitsAnalysis::run(Function &F,
5750b57cec5SDimitry Andric FunctionAnalysisManager &AM) {
5760b57cec5SDimitry Andric auto &AC = AM.getResult<AssumptionAnalysis>(F);
5770b57cec5SDimitry Andric auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
5780b57cec5SDimitry Andric return DemandedBits(F, AC, DT);
5790b57cec5SDimitry Andric }
5800b57cec5SDimitry Andric
run(Function & F,FunctionAnalysisManager & AM)5810b57cec5SDimitry Andric PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
5820b57cec5SDimitry Andric FunctionAnalysisManager &AM) {
5830b57cec5SDimitry Andric AM.getResult<DemandedBitsAnalysis>(F).print(OS);
5840b57cec5SDimitry Andric return PreservedAnalyses::all();
5850b57cec5SDimitry Andric }
586