Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:12

0001 //===- llvm/Analysis/DemandedBits.h - Determine demanded bits ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This pass implements a demanded bits analysis. A demanded bit is one that
0010 // contributes to a result; bits that are not demanded can be either zero or
0011 // one without affecting control or data flow. For example in this sequence:
0012 //
0013 //   %1 = add i32 %x, %y
0014 //   %2 = trunc i32 %1 to i16
0015 //
0016 // Only the lowest 16 bits of %1 are demanded; the rest are removed by the
0017 // trunc.
0018 //
0019 //===----------------------------------------------------------------------===//
0020 
0021 #ifndef LLVM_ANALYSIS_DEMANDEDBITS_H
0022 #define LLVM_ANALYSIS_DEMANDEDBITS_H
0023 
0024 #include "llvm/ADT/APInt.h"
0025 #include "llvm/ADT/DenseMap.h"
0026 #include "llvm/ADT/SmallPtrSet.h"
0027 #include "llvm/IR/PassManager.h"
0028 
0029 namespace llvm {
0030 
0031 class AssumptionCache;
0032 class DominatorTree;
0033 class Function;
0034 class Instruction;
0035 struct KnownBits;
0036 class raw_ostream;
0037 class Use;
0038 class Value;
0039 
0040 class DemandedBits {
0041 public:
0042   DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT) :
0043     F(F), AC(AC), DT(DT) {}
0044 
0045   /// Return the bits demanded from instruction I.
0046   ///
0047   /// For vector instructions individual vector elements are not distinguished:
0048   /// A bit is demanded if it is demanded for any of the vector elements. The
0049   /// size of the return value corresponds to the type size in bits of the
0050   /// scalar type.
0051   ///
0052   /// Instructions that do not have integer or vector of integer type are
0053   /// accepted, but will always produce a mask with all bits set.
0054   APInt getDemandedBits(Instruction *I);
0055 
0056   /// Return the bits demanded from use U.
0057   APInt getDemandedBits(Use *U);
0058 
0059   /// Return true if, during analysis, I could not be reached.
0060   bool isInstructionDead(Instruction *I);
0061 
0062   /// Return whether this use is dead by means of not having any demanded bits.
0063   bool isUseDead(Use *U);
0064 
0065   void print(raw_ostream &OS);
0066 
0067   /// Compute alive bits of one addition operand from alive output and known
0068   /// operand bits
0069   static APInt determineLiveOperandBitsAdd(unsigned OperandNo,
0070                                            const APInt &AOut,
0071                                            const KnownBits &LHS,
0072                                            const KnownBits &RHS);
0073 
0074   /// Compute alive bits of one subtraction operand from alive output and known
0075   /// operand bits
0076   static APInt determineLiveOperandBitsSub(unsigned OperandNo,
0077                                            const APInt &AOut,
0078                                            const KnownBits &LHS,
0079                                            const KnownBits &RHS);
0080 
0081 private:
0082   void performAnalysis();
0083   void determineLiveOperandBits(const Instruction *UserI,
0084     const Value *Val, unsigned OperandNo,
0085     const APInt &AOut, APInt &AB,
0086     KnownBits &Known, KnownBits &Known2, bool &KnownBitsComputed);
0087 
0088   Function &F;
0089   AssumptionCache ∾
0090   DominatorTree &DT;
0091 
0092   bool Analyzed = false;
0093 
0094   // The set of visited instructions (non-integer-typed only).
0095   SmallPtrSet<Instruction*, 32> Visited;
0096   DenseMap<Instruction *, APInt> AliveBits;
0097   // Uses with no demanded bits. If the user also has no demanded bits, the use
0098   // might not be stored explicitly in this map, to save memory during analysis.
0099   SmallPtrSet<Use *, 16> DeadUses;
0100 };
0101 
0102 /// An analysis that produces \c DemandedBits for a function.
0103 class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
0104   friend AnalysisInfoMixin<DemandedBitsAnalysis>;
0105 
0106   static AnalysisKey Key;
0107 
0108 public:
0109   /// Provide the result type for this analysis pass.
0110   using Result = DemandedBits;
0111 
0112   /// Run the analysis pass over a function and produce demanded bits
0113   /// information.
0114   DemandedBits run(Function &F, FunctionAnalysisManager &AM);
0115 };
0116 
0117 /// Printer pass for DemandedBits
0118 class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
0119   raw_ostream &OS;
0120 
0121 public:
0122   explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
0123 
0124   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
0125 
0126   static bool isRequired() { return true; }
0127 };
0128 
0129 } // end namespace llvm
0130 
0131 #endif // LLVM_ANALYSIS_DEMANDEDBITS_H