LLVM  4.0.0
JumpThreading.h
Go to the documentation of this file.
1 //===- JumpThreading.h - thread control through conditional BBs -*- 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 /// \file
10 /// See the comments on JumpThreadingPass.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_TRANSFORMS_SCALAR_JUMPTHREADING_H
15 #define LLVM_TRANSFORMS_SCALAR_JUMPTHREADING_H
16 
17 #include "llvm/ADT/DenseSet.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/ValueHandle.h"
27 
28 namespace llvm {
29 
30 /// A private "module" namespace for types and utilities used by
31 /// JumpThreading.
32 /// These are implementation details and should not be used by clients.
33 namespace jumpthreading {
34 // These are at global scope so static functions can use them too.
37 
38 // This is used to keep track of what kind of constant we're currently hoping
39 // to find.
41 }
42 
43 /// This pass performs 'jump threading', which looks at blocks that have
44 /// multiple predecessors and multiple successors. If one or more of the
45 /// predecessors of the block can be proven to always jump to one of the
46 /// successors, we forward the edge from the predecessor to the successor by
47 /// duplicating the contents of this block.
48 ///
49 /// An example of when this can occur is code like this:
50 ///
51 /// if () { ...
52 /// X = 4;
53 /// }
54 /// if (X < 3) {
55 ///
56 /// In this case, the unconditional branch at the end of the first if can be
57 /// revectored to the false side of the second if.
58 ///
59 class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
60  TargetLibraryInfo *TLI;
61  LazyValueInfo *LVI;
62  std::unique_ptr<BlockFrequencyInfo> BFI;
63  std::unique_ptr<BranchProbabilityInfo> BPI;
64  bool HasProfileData = false;
65 #ifdef NDEBUG
67 #else
69 #endif
71 
72  unsigned BBDupThreshold;
73 
74  // RAII helper for updating the recursion stack.
75  struct RecursionSetRemover {
77  std::pair<Value *, BasicBlock *> ThePair;
78 
79  RecursionSetRemover(DenseSet<std::pair<Value *, BasicBlock *>> &S,
80  std::pair<Value *, BasicBlock *> P)
81  : TheSet(S), ThePair(P) {}
82 
83  ~RecursionSetRemover() { TheSet.erase(ThePair); }
84  };
85 
86 public:
87  JumpThreadingPass(int T = -1);
88 
89  // Glue for old PM.
90  bool runImpl(Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_,
91  bool HasProfileData_, std::unique_ptr<BlockFrequencyInfo> BFI_,
92  std::unique_ptr<BranchProbabilityInfo> BPI_);
93 
95 
96  void releaseMemory() {
97  BFI.reset();
98  BPI.reset();
99  }
100 
101  void FindLoopHeaders(Function &F);
102  bool ProcessBlock(BasicBlock *BB);
103  bool ThreadEdge(BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs,
104  BasicBlock *SuccBB);
106  BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs);
107 
108  bool
112  Instruction *CxtI = nullptr);
113  bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
115  Instruction *CxtI = nullptr);
116 
117  bool ProcessBranchOnPHI(PHINode *PN);
120 
122  bool TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB);
124 
125 private:
126  BasicBlock *SplitBlockPreds(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
127  const char *Suffix);
128  void UpdateBlockFreqAndEdgeWeight(BasicBlock *PredBB, BasicBlock *BB,
129  BasicBlock *NewBB, BasicBlock *SuccBB);
130  /// Check if the block has profile metadata for its outgoing edges.
131  bool doesBlockHaveProfileData(BasicBlock *BB);
132 };
133 
134 } // end namespace llvm
135 
136 #endif
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:870
bool ThreadEdge(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs, BasicBlock *SuccBB)
ThreadEdge - We have decided that it is safe and profitable to factor the blocks in PredBBs to one pr...
Implements a dense probed hash-table based set.
Definition: DenseSet.h:202
An instruction for reading from memory.
Definition: Instructions.h:164
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:32
bool TryToUnfoldSelectInCurrBB(BasicBlock *BB)
TryToUnfoldSelectInCurrBB - Look for PHI/Select in the same BB of the form bb: p = phi [false...
#define F(x, y, z)
Definition: MD5.cpp:51
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:311
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
#define P(N)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:107
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:36
bool DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs)
DuplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch to BB which contains an i1...
void FindLoopHeaders(Function &F)
FindLoopHeaders - We do not want jump threading to turn proper loop structures into irreducible loops...
SmallVectorImpl< std::pair< Constant *, BasicBlock * > > PredValueInfo
Definition: JumpThreading.h:35
bool ProcessBranchOnPHI(PHINode *PN)
ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node in the curren...
bool ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see if we can infer that the ...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:425
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:843
Provides information about what library functions are available for the current target.
bool runImpl(Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_, bool HasProfileData_, std::unique_ptr< BlockFrequencyInfo > BFI_, std::unique_ptr< BranchProbabilityInfo > BPI_)
This pass performs 'jump threading', which looks at blocks that have multiple predecessors and multip...
Definition: JumpThreading.h:59
bool SimplifyPartiallyRedundantLoad(LoadInst *LI)
SimplifyPartiallyRedundantLoad - If LI is an obviously partially redundant load instruction, eliminate it by replacing it with a PHI node.
bool ProcessBranchOnXOR(BinaryOperator *BO)
ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on a xor instruction in the...
bool ProcessImpliedCondition(BasicBlock *BB)
SmallVector< std::pair< Constant *, BasicBlock * >, 8 > PredValueInfoTy
Definition: JumpThreading.h:36
bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
This pass computes, caches, and vends lazy value constraint information.
Definition: LazyValueInfo.h:32
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM Value Representation.
Definition: Value.h:71
bool ProcessBlock(BasicBlock *BB)
ProcessBlock - If there are any predecessors whose control can be threaded through to a successor...
A container for analyses that lazily runs them and caches their results.
bool TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB)
TryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb.