LLVM  13.0.0git
DemandedBits.h
Go to the documentation of this file.
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/Optional.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/IR/PassManager.h"
29 #include "llvm/Pass.h"
30 
31 namespace llvm {
32 
33 class AssumptionCache;
34 class DominatorTree;
35 class Function;
36 class Instruction;
37 struct KnownBits;
38 class raw_ostream;
39 
40 class DemandedBits {
41 public:
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.
55 
56  /// Return the bits demanded from use U.
58 
59  /// Return true if, during analysis, I could not be reached.
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).
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 
103 private:
104  mutable Optional<DemandedBits> DB;
105 
106 public:
107  static char ID; // Pass identification, replacement for typeid
108 
110 
111  bool runOnFunction(Function &F) override;
112  void getAnalysisUsage(AnalysisUsage &AU) const override;
113 
114  /// Clean up memory in between runs
115  void releaseMemory() override;
116 
117  DemandedBits &getDemandedBits() { return *DB; }
118 
119  void print(raw_ostream &OS, const Module *M) const override;
120 };
121 
122 /// An analysis that produces \c DemandedBits for a function.
123 class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
125 
126  static AnalysisKey Key;
127 
128 public:
129  /// Provide the result type for this analysis pass.
131 
132  /// Run the analysis pass over a function and produce demanded bits
133  /// information.
135 };
136 
137 /// Printer pass for DemandedBits
138 class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
139  raw_ostream &OS;
140 
141 public:
142  explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
143 
145 };
146 
147 /// Create a demanded bits analysis pass.
148 FunctionPass *createDemandedBitsWrapperPass();
149 
150 } // end namespace llvm
151 
152 #endif // LLVM_ANALYSIS_DEMANDEDBITS_H
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm
Definition: AllocatorList.h:23
Optional.h
llvm::PassInfoMixin
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:374
llvm::Function
Definition: Function.h:61
Pass.h
llvm::DemandedBitsPrinterPass
Printer pass for DemandedBits.
Definition: DemandedBits.h:138
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
APInt.h
llvm::DemandedBitsAnalysis
An analysis that produces DemandedBits for a function.
Definition: DemandedBits.h:123
DenseMap.h
llvm::Optional
Definition: APInt.h:33
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:449
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::DemandedBitsWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition: DemandedBits.cpp:319
llvm::DemandedBitsPrinterPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: DemandedBits.cpp:629
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::DemandedBitsWrapperPass::print
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
Definition: DemandedBits.cpp:77
llvm::createDemandedBitsWrapperPass
FunctionPass * createDemandedBitsWrapperPass()
Create a demanded bits analysis pass.
Definition: DemandedBits.cpp:616
llvm::Instruction
Definition: Instruction.h:45
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
SmallPtrSet.h
llvm::DemandedBitsWrapperPass::ID
static char ID
Definition: DemandedBits.h:107
llvm::DemandedBits::print
void print(raw_ostream &OS)
Definition: DemandedBits.cpp:514
llvm::DemandedBits::determineLiveOperandBitsSub
static APInt determineLiveOperandBitsSub(unsigned OperandNo, const APInt &AOut, const KnownBits &LHS, const KnownBits &RHS)
Compute alive bits of one subtraction operand from alive output and known operand bits.
Definition: DemandedBits.cpp:605
llvm::DenseMap
Definition: DenseMap.h:714
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:72
llvm::DemandedBits
Definition: DemandedBits.h:40
llvm::DemandedBits::DemandedBits
DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT)
Definition: DemandedBits.h:42
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::AnalysisInfoMixin
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:391
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:70
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition: AssumptionCache.h:41
llvm::DemandedBitsWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: DemandedBits.cpp:70
llvm::DemandedBits::isInstructionDead
bool isInstructionDead(Instruction *I)
Return true if, during analysis, I could not be reached.
Definition: DemandedBits.cpp:482
llvm::KnownBits
Definition: KnownBits.h:23
llvm::DemandedBitsWrapperPass
Definition: DemandedBits.h:102
llvm::DemandedBits::determineLiveOperandBitsAdd
static APInt determineLiveOperandBitsAdd(unsigned OperandNo, const APInt &AOut, const KnownBits &LHS, const KnownBits &RHS)
Compute alive bits of one addition operand from alive output and known operand bits.
Definition: DemandedBits.cpp:597
PassManager.h
llvm::DemandedBitsWrapperPass::DemandedBitsWrapperPass
DemandedBitsWrapperPass()
Definition: DemandedBits.cpp:66
llvm::DemandedBitsWrapperPass::releaseMemory
void releaseMemory() override
Clean up memory in between runs.
Definition: DemandedBits.cpp:326
llvm::DemandedBits::getDemandedBits
APInt getDemandedBits(Instruction *I)
Return the bits demanded from instruction I.
Definition: DemandedBits.cpp:443
llvm::DemandedBitsWrapperPass::getDemandedBits
DemandedBits & getDemandedBits()
Definition: DemandedBits.h:117
llvm::DemandedBits::isUseDead
bool isUseDead(Use *U)
Return whether this use is dead by means of not having any demanded bits.
Definition: DemandedBits.cpp:489
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::DemandedBitsAnalysis::run
DemandedBits run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce demanded bits information.
Definition: DemandedBits.cpp:622
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::DemandedBitsPrinterPass::DemandedBitsPrinterPass
DemandedBitsPrinterPass(raw_ostream &OS)
Definition: DemandedBits.h:142
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44