LLVM  7.0.0
AggressiveInstCombineInternal.h
Go to the documentation of this file.
1 //===- AggressiveInstCombineInternal.h --------------------------*- 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 file implements the instruction pattern combiner classes.
11 // Currently, it handles pattern expressions for:
12 // * Truncate instruction
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/ADT/MapVector.h"
17 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/Pass.h"
25 using namespace llvm;
26 
27 //===----------------------------------------------------------------------===//
28 // TruncInstCombine - looks for expression dags dominated by trunc instructions
29 // and for each eligible dag, it will create a reduced bit-width expression and
30 // replace the old expression with this new one and remove the old one.
31 // Eligible expression dag is such that:
32 // 1. Contains only supported instructions.
33 // 2. Supported leaves: ZExtInst, SExtInst, TruncInst and Constant value.
34 // 3. Can be evaluated into type with reduced legal bit-width (or Trunc type).
35 // 4. All instructions in the dag must not have users outside the dag.
36 // Only exception is for {ZExt, SExt}Inst with operand type equal to the
37 // new reduced type chosen in (3).
38 //
39 // The motivation for this optimization is that evaluating and expression using
40 // smaller bit-width is preferable, especially for vectorization where we can
41 // fit more values in one vectorized instruction. In addition, this optimization
42 // may decrease the number of cast instructions, but will not increase it.
43 //===----------------------------------------------------------------------===//
44 
45 namespace llvm {
46  class DataLayout;
47  class DominatorTree;
48  class TargetLibraryInfo;
49 
51  TargetLibraryInfo &TLI;
52  const DataLayout &DL;
53  const DominatorTree &DT;
54 
55  /// List of all TruncInst instructions to be processed.
57 
58  /// Current processed TruncInst instruction.
59  TruncInst *CurrentTruncInst;
60 
61  /// Information per each instruction in the expression dag.
62  struct Info {
63  /// Number of LSBs that are needed to generate a valid expression.
64  unsigned ValidBitWidth = 0;
65  /// Minimum number of LSBs needed to generate the ValidBitWidth.
66  unsigned MinBitWidth = 0;
67  /// The reduced value generated to replace the old instruction.
68  Value *NewValue = nullptr;
69  };
70  /// An ordered map representing expression dag post-dominated by current
71  /// processed TruncInst. It maps each instruction in the dag to its Info
72  /// structure. The map is ordered such that each instruction appears before
73  /// all other instructions in the dag that uses it.
75 
76 public:
78  const DominatorTree &DT)
79  : TLI(TLI), DL(DL), DT(DT), CurrentTruncInst(nullptr) {}
80 
81  /// Perform TruncInst pattern optimization on given function.
82  bool run(Function &F);
83 
84 private:
85  /// Build expression dag dominated by the /p CurrentTruncInst and append it to
86  /// the InstInfoMap container.
87  ///
88  /// \return true only if succeed to generate an eligible sub expression dag.
89  bool buildTruncExpressionDag();
90 
91  /// Calculate the minimal allowed bit-width of the chain ending with the
92  /// currently visited truncate's operand.
93  ///
94  /// \return minimum number of bits to which the chain ending with the
95  /// truncate's operand can be shrunk to.
96  unsigned getMinBitWidth();
97 
98  /// Build an expression dag dominated by the current processed TruncInst and
99  /// Check if it is eligible to be reduced to a smaller type.
100  ///
101  /// \return the scalar version of the new type to be used for the reduced
102  /// expression dag, or nullptr if the expression dag is not eligible
103  /// to be reduced.
104  Type *getBestTruncatedType();
105 
106  /// Given a \p V value and a \p SclTy scalar type return the generated reduced
107  /// value of \p V based on the type \p SclTy.
108  ///
109  /// \param V value to be reduced.
110  /// \param SclTy scalar version of new type to reduce to.
111  /// \return the new reduced value.
112  Value *getReducedOperand(Value *V, Type *SclTy);
113 
114  /// Create a new expression dag using the reduced /p SclTy type and replace
115  /// the old expression dag with it. Also erase all instructions in the old
116  /// dag, except those that are still needed outside the dag.
117  ///
118  /// \param SclTy scalar version of new type to reduce expression dag into.
119  void ReduceExpressionDag(Type *SclTy);
120 };
121 } // end namespace llvm.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
This is the interface for a simple mod/ref and alias analysis over globals.
This class implements a map that also provides access to all stored values in a deterministic order...
Definition: MapVector.h:38
F(f)
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:142
This class represents a truncation of integer types.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
bool run(Function &F)
Perform TruncInst pattern optimization on given function.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Provides information about what library functions are available for the current target.
LLVM Value Representation.
Definition: Value.h:73
This is the interface for LLVM's primary stateless and local alias analysis.
TruncInstCombine(TargetLibraryInfo &TLI, const DataLayout &DL, const DominatorTree &DT)