1 //===- llvm/Analysis/DemandedBits.h - Determine demanded bits ---*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass implements a demanded bits analysis. A demanded bit is one that
10 // contributes to a result; bits that are not demanded can be either zero or
11 // one without affecting control or data flow. For example in this sequence:
12 //
13 //   %1 = add i32 %x, %y
14 //   %2 = trunc i32 %1 to i16
15 //
16 // Only the lowest 16 bits of %1 are demanded; the rest are removed by the
17 // trunc.
18 //
19 //===----------------------------------------------------------------------===//
20 
21 #ifndef LLVM_ANALYSIS_DEMANDEDBITS_H
22 #define LLVM_ANALYSIS_DEMANDEDBITS_H
23 
24 #include "llvm/ADT/APInt.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/SmallPtrSet.h"
27 #include "llvm/IR/PassManager.h"
28 #include <optional>
29 
30 namespace llvm {
31 
32 class AssumptionCache;
33 class DominatorTree;
34 class Function;
35 class Instruction;
36 struct KnownBits;
37 class raw_ostream;
38 
39 class DemandedBits {
40 public:
41   DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT) :
42     F(F), AC(AC), DT(DT) {}
43 
44   /// Return the bits demanded from instruction I.
45   ///
46   /// For vector instructions individual vector elements are not distinguished:
47   /// A bit is demanded if it is demanded for any of the vector elements. The
48   /// size of the return value corresponds to the type size in bits of the
49   /// scalar type.
50   ///
51   /// Instructions that do not have integer or vector of integer type are
52   /// accepted, but will always produce a mask with all bits set.
53   APInt getDemandedBits(Instruction *I);
54 
55   /// Return the bits demanded from use U.
56   APInt getDemandedBits(Use *U);
57 
58   /// Return true if, during analysis, I could not be reached.
59   bool isInstructionDead(Instruction *I);
60 
61   /// Return whether this use is dead by means of not having any demanded bits.
62   bool isUseDead(Use *U);
63 
64   void print(raw_ostream &OS);
65 
66   /// Compute alive bits of one addition operand from alive output and known
67   /// operand bits
68   static APInt determineLiveOperandBitsAdd(unsigned OperandNo,
69                                            const APInt &AOut,
70                                            const KnownBits &LHS,
71                                            const KnownBits &RHS);
72 
73   /// Compute alive bits of one subtraction operand from alive output and known
74   /// operand bits
75   static APInt determineLiveOperandBitsSub(unsigned OperandNo,
76                                            const APInt &AOut,
77                                            const KnownBits &LHS,
78                                            const KnownBits &RHS);
79 
80 private:
81   void performAnalysis();
82   void determineLiveOperandBits(const Instruction *UserI,
83     const Value *Val, unsigned OperandNo,
84     const APInt &AOut, APInt &AB,
85     KnownBits &Known, KnownBits &Known2, bool &KnownBitsComputed);
86 
87   Function &F;
88   AssumptionCache &AC;
89   DominatorTree &DT;
90 
91   bool Analyzed = false;
92 
93   // The set of visited instructions (non-integer-typed only).
94   SmallPtrSet<Instruction*, 32> Visited;
95   DenseMap<Instruction *, APInt> AliveBits;
96   // Uses with no demanded bits. If the user also has no demanded bits, the use
97   // might not be stored explicitly in this map, to save memory during analysis.
98   SmallPtrSet<Use *, 16> DeadUses;
99 };
100 
101 /// An analysis that produces \c DemandedBits for a function.
102 class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
103   friend AnalysisInfoMixin<DemandedBitsAnalysis>;
104 
105   static AnalysisKey Key;
106 
107 public:
108   /// Provide the result type for this analysis pass.
109   using Result = DemandedBits;
110 
111   /// Run the analysis pass over a function and produce demanded bits
112   /// information.
113   DemandedBits run(Function &F, FunctionAnalysisManager &AM);
114 };
115 
116 /// Printer pass for DemandedBits
117 class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
118   raw_ostream &OS;
119 
120 public:
121   explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
122 
123   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
124 };
125 
126 } // end namespace llvm
127 
128 #endif // LLVM_ANALYSIS_DEMANDEDBITS_H
129