Coverage Report

Created: 2018-12-11 00:00

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/include/llvm/Analysis/DemandedBits.h
Line
Count
Source
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:
43
  DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT) :
44
1.00M
    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.
55
  APInt getDemandedBits(Instruction *I);
56
57
  /// Return true if, during analysis, I could not be reached.
58
  bool isInstructionDead(Instruction *I);
59
60
  void print(raw_ostream &OS);
61
62
private:
63
  void performAnalysis();
64
  void determineLiveOperandBits(const Instruction *UserI,
65
    const Instruction *I, unsigned OperandNo,
66
    const APInt &AOut, APInt &AB,
67
    KnownBits &Known, KnownBits &Known2);
68
69
  Function &F;
70
  AssumptionCache ∾
71
  DominatorTree &DT;
72
73
  bool Analyzed = false;
74
75
  // The set of visited instructions (non-integer-typed only).
76
  SmallPtrSet<Instruction*, 32> Visited;
77
  DenseMap<Instruction *, APInt> AliveBits;
78
};
79
80
class DemandedBitsWrapperPass : public FunctionPass {
81
private:
82
  mutable Optional<DemandedBits> DB;
83
84
public:
85
  static char ID; // Pass identification, replacement for typeid
86
87
  DemandedBitsWrapperPass();
88
89
  bool runOnFunction(Function &F) override;
90
  void getAnalysisUsage(AnalysisUsage &AU) const override;
91
92
  /// Clean up memory in between runs
93
  void releaseMemory() override;
94
95
1.00M
  DemandedBits &getDemandedBits() { return *DB; }
96
97
  void print(raw_ostream &OS, const Module *M) const override;
98
};
99
100
/// An analysis that produces \c DemandedBits for a function.
101
class DemandedBitsAnalysis : public AnalysisInfoMixin<DemandedBitsAnalysis> {
102
  friend AnalysisInfoMixin<DemandedBitsAnalysis>;
103
104
  static AnalysisKey Key;
105
106
public:
107
  /// Provide the result type for this analysis pass.
108
  using Result = DemandedBits;
109
110
  /// Run the analysis pass over a function and produce demanded bits
111
  /// information.
112
  DemandedBits run(Function &F, FunctionAnalysisManager &AM);
113
};
114
115
/// Printer pass for DemandedBits
116
class DemandedBitsPrinterPass : public PassInfoMixin<DemandedBitsPrinterPass> {
117
  raw_ostream &OS;
118
119
public:
120
3
  explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
121
122
  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
123
};
124
125
/// Create a demanded bits analysis pass.
126
FunctionPass *createDemandedBitsWrapperPass();
127
128
} // end namespace llvm
129
130
#endif // LLVM_ANALYSIS_DEMANDED_BITS_H