LLVM  8.0.1
DemandedBits.h
Go to the documentation of this file.
1 //===- llvm/Analysis/DemandedBits.h - Determine demanded bits ---*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass implements a demanded bits analysis. A demanded bit is one that
11 // contributes to a result; bits that are not demanded can be either zero or
12 // one without affecting control or data flow. For example in this sequence:
13 //
14 // %1 = add i32 %x, %y
15 // %2 = trunc i32 %1 to i16
16 //
17 // Only the lowest 16 bits of %1 are demanded; the rest are removed by the
18 // trunc.
19 //
20 //===----------------------------------------------------------------------===//
21 
22 #ifndef LLVM_ANALYSIS_DEMANDED_BITS_H
23 #define LLVM_ANALYSIS_DEMANDED_BITS_H
24 
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/Optional.h"
28 #include "llvm/ADT/SmallPtrSet.h"
29 #include "llvm/IR/PassManager.h"
30 #include "llvm/Pass.h"
31 
32 namespace llvm {
33 
34 class AssumptionCache;
35 class DominatorTree;
36 class Function;
37 class Instruction;
38 struct KnownBits;
39 class raw_ostream;
40 
41 class DemandedBits {
42 public:
44  F(F), AC(AC), DT(DT) {}
45 
46  /// Return the bits demanded from instruction I.
47  ///
48  /// For vector instructions individual vector elements are not distinguished:
49  /// A bit is demanded if it is demanded for any of the vector elements. The
50  /// size of the return value corresponds to the type size in bits of the
51  /// scalar type.
52  ///
53  /// Instructions that do not have integer or vector of integer type are
54  /// accepted, but will always produce a mask with all bits set.
56 
57  /// Return true if, during analysis, I could not be reached.
59 
60  /// Return whether this use is dead by means of not having any demanded bits.
61  bool isUseDead(Use *U);
62 
63  void print(raw_ostream &OS);
64 
65 private:
66  void performAnalysis();
67  void determineLiveOperandBits(const Instruction *UserI,
68  const Value *Val, unsigned OperandNo,
69  const APInt &AOut, APInt &AB,
70  KnownBits &Known, KnownBits &Known2, bool &KnownBitsComputed);
71 
72  Function &F;
73  AssumptionCache ∾
74  DominatorTree &DT;
75 
76  bool Analyzed = false;
77 
78  // The set of visited instructions (non-integer-typed only).
81  // Uses with no demanded bits. If the user also has no demanded bits, the use
82  // might not be stored explicitly in this map, to save memory during analysis.
83  SmallPtrSet<Use *, 16> DeadUses;
84 };
85 
87 private:
88  mutable Optional<DemandedBits> DB;
89 
90 public:
91  static char ID; // Pass identification, replacement for typeid
92 
94 
95  bool runOnFunction(Function &F) override;
96  void getAnalysisUsage(AnalysisUsage &AU) const override;
97 
98  /// Clean up memory in between runs
99  void releaseMemory() override;
100 
101  DemandedBits &getDemandedBits() { return *DB; }
102 
103  void print(raw_ostream &OS, const Module *M) const override;
104 };
105 
106 /// An analysis that produces \c DemandedBits for a function.
107 class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
109 
110  static AnalysisKey Key;
111 
112 public:
113  /// Provide the result type for this analysis pass.
115 
116  /// Run the analysis pass over a function and produce demanded bits
117  /// information.
119 };
120 
121 /// Printer pass for DemandedBits
122 class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
123  raw_ostream &OS;
124 
125 public:
126  explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
127 
129 };
130 
131 /// Create a demanded bits analysis pass.
133 
134 } // end namespace llvm
135 
136 #endif // LLVM_ANALYSIS_DEMANDED_BITS_H
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
bool isInstructionDead(Instruction *I)
Return true if, during analysis, I could not be reached.
DemandedBitsPrinterPass(raw_ostream &OS)
Definition: DemandedBits.h:126
A cache of @llvm.assume calls within a function.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
An analysis that produces DemandedBits for a function.
Definition: DemandedBits.h:107
This file implements a class to represent arbitrary precision integral constant values and operations...
Key
PAL metadata keys.
#define F(x, y, z)
Definition: MD5.cpp:55
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:366
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:145
static bool runOnFunction(Function &F, bool PostInlining)
Printer pass for DemandedBits.
Definition: DemandedBits.h:122
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:383
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:418
DemandedBits & getDemandedBits()
Definition: DemandedBits.h:101
DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT)
Definition: DemandedBits.h:43
APInt getDemandedBits(Instruction *I)
Return the bits demanded from instruction I.
Class for arbitrary precision integers.
Definition: APInt.h:70
#define I(x, y, z)
Definition: MD5.cpp:58
bool isUseDead(Use *U)
Return whether this use is dead by means of not having any demanded bits.
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
A container for analyses that lazily runs them and caches their results.
This header defines various interfaces for pass management in LLVM.
FunctionPass * createDemandedBitsWrapperPass()
Create a demanded bits analysis pass.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: PassManager.h:71
void print(raw_ostream &OS)