xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Analysis/DemandedBits.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
29 namespace llvm {
30 
31 class AssumptionCache;
32 class DominatorTree;
33 class Function;
34 class Instruction;
35 struct KnownBits;
36 class raw_ostream;
37 class Use;
38 class Value;
39 
40 class DemandedBits {
41 public:
DemandedBits(Function & F,AssumptionCache & AC,DominatorTree & DT)42   DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT) :
43     F(F), AC(AC), DT(DT) {}
44 
45   /// Return the bits demanded from instruction I.
46   ///
47   /// For vector instructions individual vector elements are not distinguished:
48   /// A bit is demanded if it is demanded for any of the vector elements. The
49   /// size of the return value corresponds to the type size in bits of the
50   /// scalar type.
51   ///
52   /// Instructions that do not have integer or vector of integer type are
53   /// accepted, but will always produce a mask with all bits set.
54   APInt getDemandedBits(Instruction *I);
55 
56   /// Return the bits demanded from use U.
57   APInt getDemandedBits(Use *U);
58 
59   /// Return true if, during analysis, I could not be reached.
60   bool isInstructionDead(Instruction *I);
61 
62   /// Return whether this use is dead by means of not having any demanded bits.
63   bool isUseDead(Use *U);
64 
65   void print(raw_ostream &OS);
66 
67   /// Compute alive bits of one addition operand from alive output and known
68   /// operand bits
69   static APInt determineLiveOperandBitsAdd(unsigned OperandNo,
70                                            const APInt &AOut,
71                                            const KnownBits &LHS,
72                                            const KnownBits &RHS);
73 
74   /// Compute alive bits of one subtraction operand from alive output and known
75   /// operand bits
76   static APInt determineLiveOperandBitsSub(unsigned OperandNo,
77                                            const APInt &AOut,
78                                            const KnownBits &LHS,
79                                            const KnownBits &RHS);
80 
81 private:
82   void performAnalysis();
83   void determineLiveOperandBits(const Instruction *UserI,
84     const Value *Val, unsigned OperandNo,
85     const APInt &AOut, APInt &AB,
86     KnownBits &Known, KnownBits &Known2, bool &KnownBitsComputed);
87 
88   Function &F;
89   AssumptionCache ∾
90   DominatorTree &DT;
91 
92   bool Analyzed = false;
93 
94   // The set of visited instructions (non-integer-typed only).
95   SmallPtrSet<Instruction*, 32> Visited;
96   DenseMap<Instruction *, APInt> AliveBits;
97   // Uses with no demanded bits. If the user also has no demanded bits, the use
98   // might not be stored explicitly in this map, to save memory during analysis.
99   SmallPtrSet<Use *, 16> DeadUses;
100 };
101 
102 /// An analysis that produces \c DemandedBits for a function.
103 class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
104   friend AnalysisInfoMixin<DemandedBitsAnalysis>;
105 
106   static AnalysisKey Key;
107 
108 public:
109   /// Provide the result type for this analysis pass.
110   using Result = DemandedBits;
111 
112   /// Run the analysis pass over a function and produce demanded bits
113   /// information.
114   DemandedBits run(Function &F, FunctionAnalysisManager &AM);
115 };
116 
117 /// Printer pass for DemandedBits
118 class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
119   raw_ostream &OS;
120 
121 public:
DemandedBitsPrinterPass(raw_ostream & OS)122   explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
123 
124   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
125 
isRequired()126   static bool isRequired() { return true; }
127 };
128 
129 } // end namespace llvm
130 
131 #endif // LLVM_ANALYSIS_DEMANDEDBITS_H
132