1 //===-- llvm/Analysis/DemandedBits.h - Determine demanded bits --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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:
14 // %1 = add i32 %x, %y
15 // %2 = trunc i32 %1 to i16
17 // Only the lowest 16 bits of %1 are demanded; the rest are removed by the
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_ANALYSIS_DEMANDED_BITS_H
23 #define LLVM_ANALYSIS_DEMANDED_BITS_H
25 #include "llvm/ADT/APInt.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/IR/PassManager.h"
29 #include "llvm/Pass.h"
37 class AssumptionCache;
42 DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT) :
43 F(F), AC(AC), DT(DT), Analyzed(false) {}
45 /// Return the bits demanded from instruction I.
46 APInt getDemandedBits(Instruction *I);
48 /// Return true if, during analysis, I could not be reached.
49 bool isInstructionDead(Instruction *I);
51 void print(raw_ostream &OS);
58 void performAnalysis();
59 void determineLiveOperandBits(const Instruction *UserI,
60 const Instruction *I, unsigned OperandNo,
61 const APInt &AOut, APInt &AB,
62 KnownBits &Known, KnownBits &Known2);
66 // The set of visited instructions (non-integer-typed only).
67 SmallPtrSet<Instruction*, 32> Visited;
68 DenseMap<Instruction *, APInt> AliveBits;
71 class DemandedBitsWrapperPass : public FunctionPass {
73 mutable Optional<DemandedBits> DB;
75 static char ID; // Pass identification, replacement for typeid
76 DemandedBitsWrapperPass();
78 bool runOnFunction(Function &F) override;
79 void getAnalysisUsage(AnalysisUsage &AU) const override;
81 /// Clean up memory in between runs
82 void releaseMemory() override;
84 DemandedBits &getDemandedBits() { return *DB; }
86 void print(raw_ostream &OS, const Module *M) const override;
89 /// An analysis that produces \c DemandedBits for a function.
90 class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
91 friend AnalysisInfoMixin<DemandedBitsAnalysis>;
92 static AnalysisKey Key;
95 /// \brief Provide the result typedef for this analysis pass.
96 typedef DemandedBits Result;
98 /// \brief Run the analysis pass over a function and produce demanded bits
100 DemandedBits run(Function &F, FunctionAnalysisManager &AM);
103 /// \brief Printer pass for DemandedBits
104 class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
108 explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
109 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
112 /// Create a demanded bits analysis pass.
113 FunctionPass *createDemandedBitsWrapperPass();
115 } // End llvm namespace