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/Pass.h"
26 #include "llvm/ADT/APInt.h"
27 #include "llvm/ADT/DenseMap.h"
28 #include "llvm/ADT/SmallPtrSet.h"
36 class AssumptionCache;
38 struct DemandedBits : public FunctionPass {
39 static char ID; // Pass identification, replacement for typeid
42 bool runOnFunction(Function& F) override;
43 void getAnalysisUsage(AnalysisUsage& AU) const override;
44 void print(raw_ostream &OS, const Module *M) const override;
46 /// Return the bits demanded from instruction I.
47 APInt getDemandedBits(Instruction *I);
49 /// Return true if, during analysis, I could not be reached.
50 bool isInstructionDead(Instruction *I);
53 void performAnalysis();
54 void determineLiveOperandBits(const Instruction *UserI,
55 const Instruction *I, unsigned OperandNo,
56 const APInt &AOut, APInt &AB,
57 APInt &KnownZero, APInt &KnownOne,
58 APInt &KnownZero2, APInt &KnownOne2);
65 // The set of visited instructions (non-integer-typed only).
66 SmallPtrSet<Instruction*, 128> Visited;
67 DenseMap<Instruction *, APInt> AliveBits;
70 /// Create a demanded bits analysis pass.
71 FunctionPass *createDemandedBitsPass();
73 } // End llvm namespace