JumpThreading.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- JumpThreading.h - thread control through conditional BBs -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. /// \file
  15. /// See the comments on JumpThreadingPass.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_TRANSFORMS_SCALAR_JUMPTHREADING_H
  19. #define LLVM_TRANSFORMS_SCALAR_JUMPTHREADING_H
  20. #include "llvm/ADT/ArrayRef.h"
  21. #include "llvm/ADT/DenseSet.h"
  22. #include "llvm/ADT/SmallSet.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/Analysis/BlockFrequencyInfo.h"
  25. #include "llvm/Analysis/BranchProbabilityInfo.h"
  26. #include "llvm/IR/ValueHandle.h"
  27. #include <utility>
  28. namespace llvm {
  29. class AAResults;
  30. class BasicBlock;
  31. class BinaryOperator;
  32. class BranchInst;
  33. class CmpInst;
  34. class Constant;
  35. class DomTreeUpdater;
  36. class Function;
  37. class Instruction;
  38. class IntrinsicInst;
  39. class LazyValueInfo;
  40. class LoadInst;
  41. class PHINode;
  42. class SelectInst;
  43. class SwitchInst;
  44. class TargetLibraryInfo;
  45. class TargetTransformInfo;
  46. class Value;
  47. /// A private "module" namespace for types and utilities used by
  48. /// JumpThreading.
  49. /// These are implementation details and should not be used by clients.
  50. namespace jumpthreading {
  51. // These are at global scope so static functions can use them too.
  52. using PredValueInfo = SmallVectorImpl<std::pair<Constant *, BasicBlock *>>;
  53. using PredValueInfoTy = SmallVector<std::pair<Constant *, BasicBlock *>, 8>;
  54. // This is used to keep track of what kind of constant we're currently hoping
  55. // to find.
  56. enum ConstantPreference { WantInteger, WantBlockAddress };
  57. } // end namespace jumpthreading
  58. /// This pass performs 'jump threading', which looks at blocks that have
  59. /// multiple predecessors and multiple successors. If one or more of the
  60. /// predecessors of the block can be proven to always jump to one of the
  61. /// successors, we forward the edge from the predecessor to the successor by
  62. /// duplicating the contents of this block.
  63. ///
  64. /// An example of when this can occur is code like this:
  65. ///
  66. /// if () { ...
  67. /// X = 4;
  68. /// }
  69. /// if (X < 3) {
  70. ///
  71. /// In this case, the unconditional branch at the end of the first if can be
  72. /// revectored to the false side of the second if.
  73. class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
  74. TargetLibraryInfo *TLI;
  75. TargetTransformInfo *TTI;
  76. LazyValueInfo *LVI;
  77. AAResults *AA;
  78. DomTreeUpdater *DTU;
  79. std::unique_ptr<BlockFrequencyInfo> BFI;
  80. std::unique_ptr<BranchProbabilityInfo> BPI;
  81. bool HasProfileData = false;
  82. bool HasGuards = false;
  83. #ifndef LLVM_ENABLE_ABI_BREAKING_CHECKS
  84. SmallPtrSet<const BasicBlock *, 16> LoopHeaders;
  85. #else
  86. SmallSet<AssertingVH<const BasicBlock>, 16> LoopHeaders;
  87. #endif
  88. unsigned BBDupThreshold;
  89. unsigned DefaultBBDupThreshold;
  90. public:
  91. JumpThreadingPass(int T = -1);
  92. // Glue for old PM.
  93. bool runImpl(Function &F, TargetLibraryInfo *TLI, TargetTransformInfo *TTI,
  94. LazyValueInfo *LVI, AAResults *AA, DomTreeUpdater *DTU,
  95. bool HasProfileData, std::unique_ptr<BlockFrequencyInfo> BFI,
  96. std::unique_ptr<BranchProbabilityInfo> BPI);
  97. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  98. void releaseMemory() {
  99. BFI.reset();
  100. BPI.reset();
  101. }
  102. void findLoopHeaders(Function &F);
  103. bool processBlock(BasicBlock *BB);
  104. bool maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB);
  105. void updateSSA(BasicBlock *BB, BasicBlock *NewBB,
  106. DenseMap<Instruction *, Value *> &ValueMapping);
  107. DenseMap<Instruction *, Value *> cloneInstructions(BasicBlock::iterator BI,
  108. BasicBlock::iterator BE,
  109. BasicBlock *NewBB,
  110. BasicBlock *PredBB);
  111. bool tryThreadEdge(BasicBlock *BB,
  112. const SmallVectorImpl<BasicBlock *> &PredBBs,
  113. BasicBlock *SuccBB);
  114. void threadEdge(BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs,
  115. BasicBlock *SuccBB);
  116. bool duplicateCondBranchOnPHIIntoPred(
  117. BasicBlock *BB, const SmallVectorImpl<BasicBlock *> &PredBBs);
  118. bool computeValueKnownInPredecessorsImpl(
  119. Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result,
  120. jumpthreading::ConstantPreference Preference,
  121. DenseSet<Value *> &RecursionSet, Instruction *CxtI = nullptr);
  122. bool
  123. computeValueKnownInPredecessors(Value *V, BasicBlock *BB,
  124. jumpthreading::PredValueInfo &Result,
  125. jumpthreading::ConstantPreference Preference,
  126. Instruction *CxtI = nullptr) {
  127. DenseSet<Value *> RecursionSet;
  128. return computeValueKnownInPredecessorsImpl(V, BB, Result, Preference,
  129. RecursionSet, CxtI);
  130. }
  131. Constant *evaluateOnPredecessorEdge(BasicBlock *BB, BasicBlock *PredPredBB,
  132. Value *cond);
  133. bool maybethreadThroughTwoBasicBlocks(BasicBlock *BB, Value *Cond);
  134. void threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, BasicBlock *PredBB,
  135. BasicBlock *BB, BasicBlock *SuccBB);
  136. bool processThreadableEdges(Value *Cond, BasicBlock *BB,
  137. jumpthreading::ConstantPreference Preference,
  138. Instruction *CxtI = nullptr);
  139. bool processBranchOnPHI(PHINode *PN);
  140. bool processBranchOnXOR(BinaryOperator *BO);
  141. bool processImpliedCondition(BasicBlock *BB);
  142. bool simplifyPartiallyRedundantLoad(LoadInst *LI);
  143. void unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB, SelectInst *SI,
  144. PHINode *SIUse, unsigned Idx);
  145. bool tryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB);
  146. bool tryToUnfoldSelect(SwitchInst *SI, BasicBlock *BB);
  147. bool tryToUnfoldSelectInCurrBB(BasicBlock *BB);
  148. bool processGuards(BasicBlock *BB);
  149. bool threadGuard(BasicBlock *BB, IntrinsicInst *Guard, BranchInst *BI);
  150. private:
  151. BasicBlock *splitBlockPreds(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
  152. const char *Suffix);
  153. void updateBlockFreqAndEdgeWeight(BasicBlock *PredBB, BasicBlock *BB,
  154. BasicBlock *NewBB, BasicBlock *SuccBB);
  155. /// Check if the block has profile metadata for its outgoing edges.
  156. bool doesBlockHaveProfileData(BasicBlock *BB);
  157. };
  158. } // end namespace llvm
  159. #endif // LLVM_TRANSFORMS_SCALAR_JUMPTHREADING_H
  160. #ifdef __GNUC__
  161. #pragma GCC diagnostic pop
  162. #endif