LLVM  5.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"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/IntrinsicInst.h"
28 #include "llvm/IR/ValueHandle.h"
29 
30 namespace llvm {
31 
32 /// A private "module" namespace for types and utilities used by
33 /// JumpThreading.
34 /// These are implementation details and should not be used by clients.
35 namespace jumpthreading {
36 // These are at global scope so static functions can use them too.
39 
40 // This is used to keep track of what kind of constant we're currently hoping
41 // to find.
43 }
44 
45 /// This pass performs 'jump threading', which looks at blocks that have
46 /// multiple predecessors and multiple successors. If one or more of the
47 /// predecessors of the block can be proven to always jump to one of the
48 /// successors, we forward the edge from the predecessor to the successor by
49 /// duplicating the contents of this block.
50 ///
51 /// An example of when this can occur is code like this:
52 ///
53 /// if () { ...
54 /// X = 4;
55 /// }
56 /// if (X < 3) {
57 ///
58 /// In this case, the unconditional branch at the end of the first if can be
59 /// revectored to the false side of the second if.
60 ///
61 class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
62  TargetLibraryInfo *TLI;
63  LazyValueInfo *LVI;
64  AliasAnalysis *AA;
65  std::unique_ptr<BlockFrequencyInfo> BFI;
66  std::unique_ptr<BranchProbabilityInfo> BPI;
67  bool HasProfileData = false;
68  bool HasGuards = false;
69 #ifdef NDEBUG
71 #else
73 #endif
75 
76  unsigned BBDupThreshold;
77 
78  // RAII helper for updating the recursion stack.
79  struct RecursionSetRemover {
81  std::pair<Value *, BasicBlock *> ThePair;
82 
83  RecursionSetRemover(DenseSet<std::pair<Value *, BasicBlock *>> &S,
84  std::pair<Value *, BasicBlock *> P)
85  : TheSet(S), ThePair(P) {}
86 
87  ~RecursionSetRemover() { TheSet.erase(ThePair); }
88  };
89 
90 public:
91  JumpThreadingPass(int T = -1);
92 
93  // Glue for old PM.
94  bool runImpl(Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_,
95  AliasAnalysis *AA_, bool HasProfileData_,
96  std::unique_ptr<BlockFrequencyInfo> BFI_,
97  std::unique_ptr<BranchProbabilityInfo> BPI_);
98 
100 
101  void releaseMemory() {
102  BFI.reset();
103  BPI.reset();
104  }
105 
106  void FindLoopHeaders(Function &F);
107  bool ProcessBlock(BasicBlock *BB);
108  bool ThreadEdge(BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs,
109  BasicBlock *SuccBB);
111  BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs);
112 
113  bool
117  Instruction *CxtI = nullptr);
118  bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB,
120  Instruction *CxtI = nullptr);
121 
122  bool ProcessBranchOnPHI(PHINode *PN);
125 
127  bool TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB);
129 
130  bool ProcessGuards(BasicBlock *BB);
131  bool ThreadGuard(BasicBlock *BB, IntrinsicInst *Guard, BranchInst *BI);
132 
133 private:
134  BasicBlock *SplitBlockPreds(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
135  const char *Suffix);
136  void UpdateBlockFreqAndEdgeWeight(BasicBlock *PredBB, BasicBlock *BB,
137  BasicBlock *NewBB, BasicBlock *SuccBB);
138  /// Check if the block has profile metadata for its outgoing edges.
139  bool doesBlockHaveProfileData(BasicBlock *BB);
140 };
141 
142 } // end namespace llvm
143 
144 #endif
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:850
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:221
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:42
bool TryToUnfoldSelectInCurrBB(BasicBlock *BB)
TryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the same BB in the form bb: p = ...
#define F(x, y, z)
Definition: MD5.cpp:55
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:365
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:153
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
Conditional or Unconditional Branch instruction.
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...
bool runImpl(Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_, AliasAnalysis *AA_, bool HasProfileData_, std::unique_ptr< BlockFrequencyInfo > BFI_, std::unique_ptr< BranchProbabilityInfo > BPI_)
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:37
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:423
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Provides information about what library functions are available for the current target.
This pass performs 'jump threading', which looks at blocks that have multiple predecessors and multip...
Definition: JumpThreading.h:61
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:38
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)
bool ThreadGuard(BasicBlock *BB, IntrinsicInst *Guard, BranchInst *BI)
Try to propagate the guard from BB which is the lower block of a diamond to one of its branches...
bool ProcessGuards(BasicBlock *BB)
Try to propagate a guard from the current BB into one of its predecessors in case if another branch o...
LLVM Value Representation.
Definition: Value.h:73
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.
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:44
bool TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB)
TryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb2.