LLVM  10.0.0svn
Go to the documentation of this file.
1 //===- InstSimplifyPass.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
11 #include "llvm/ADT/SmallPtrSet.h"
12 #include "llvm/ADT/Statistic.h"
17 #include "llvm/IR/DataLayout.h"
18 #include "llvm/IR/Dominators.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Transforms/Utils.h"
24 using namespace llvm;
26 #define DEBUG_TYPE "instsimplify"
28 STATISTIC(NumSimplified, "Number of redundant instructions removed");
30 static bool runImpl(Function &F, const SimplifyQuery &SQ,
32  SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
33  bool Changed = false;
35  do {
36  for (BasicBlock &BB : F) {
37  // Unreachable code can take on strange forms that we are not prepared to
38  // handle. For example, an instruction may have itself as an operand.
39  if (!SQ.DT->isReachableFromEntry(&BB))
40  continue;
42  SmallVector<Instruction *, 8> DeadInstsInBB;
43  for (Instruction &I : BB) {
44  // The first time through the loop, ToSimplify is empty and we try to
45  // simplify all instructions. On later iterations, ToSimplify is not
46  // empty and we only bother simplifying instructions that are in it.
47  if (!ToSimplify->empty() && !ToSimplify->count(&I))
48  continue;
50  // Don't waste time simplifying dead/unused instructions.
52  DeadInstsInBB.push_back(&I);
53  Changed = true;
54  } else if (!I.use_empty()) {
55  if (Value *V = SimplifyInstruction(&I, SQ, ORE)) {
56  // Mark all uses for resimplification next time round the loop.
57  for (User *U : I.users())
58  Next->insert(cast<Instruction>(U));
59  I.replaceAllUsesWith(V);
60  ++NumSimplified;
61  Changed = true;
62  // A call can get simplified, but it may not be trivially dead.
64  DeadInstsInBB.push_back(&I);
65  }
66  }
67  }
69  }
71  // Place the list of instructions to simplify on the next loop iteration
72  // into ToSimplify.
73  std::swap(ToSimplify, Next);
74  Next->clear();
75  } while (!ToSimplify->empty());
77  return Changed;
78 }
80 namespace {
81 struct InstSimplifyLegacyPass : public FunctionPass {
82  static char ID; // Pass identification, replacement for typeid
83  InstSimplifyLegacyPass() : FunctionPass(ID) {
85  }
87  void getAnalysisUsage(AnalysisUsage &AU) const override {
88  AU.setPreservesCFG();
93  }
95  /// Remove instructions that simplify.
96  bool runOnFunction(Function &F) override {
97  if (skipFunction(F))
98  return false;
100  const DominatorTree *DT =
101  &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
102  const TargetLibraryInfo *TLI =
103  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
104  AssumptionCache *AC =
105  &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
107  &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
108  const DataLayout &DL = F.getParent()->getDataLayout();
109  const SimplifyQuery SQ(DL, TLI, DT, AC);
110  return runImpl(F, SQ, ORE);
111  }
112 };
113 } // namespace
116 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
117  "Remove redundant instructions", false, false)
122 INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
123  "Remove redundant instructions", false, false)
125 // Public interface to the simplify instructions pass.
127  return new InstSimplifyLegacyPass();
128 }
132  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
133  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
134  auto &AC = AM.getResult<AssumptionAnalysis>(F);
136  const DataLayout &DL = F.getParent()->getDataLayout();
137  const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
138  bool Changed = runImpl(F, SQ, &ORE);
139  if (!Changed)
140  return PreservedAnalyses::all();
143  PA.preserveSet<CFGAnalyses>();
144  return PA;
145 }
Defines passes for running instruction simplification across chunks of IR.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:777
This class represents lattice values for constants.
Definition: AllocatorList.h:23
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
STATISTIC(NumFunctions, "Total number of functions")
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:230
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
const TargetLibraryInfo * TLI
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:299
AnalysisUsage & addRequired()
Definition: PassSupport.h:50
const DataLayout & getDataLayout() const
Get the data layout for the module&#39;s target platform.
Definition: Module.cpp:369
static bool runImpl(Function &F, const SimplifyQuery &SQ, OptimizationRemarkEmitter *ORE)
FunctionPass * createInstSimplifyLegacyPass()
Create a legacy pass that does instruction simplification on each instruction in a function...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:154
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
LLVM_NODISCARD bool empty() const
Definition: SmallPtrSet.h:91
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:381
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr)
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:440
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:160
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
A function analysis which provides an AssumptionCache.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:417
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:837
Provides information about what library functions are available for the current target.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
Represents analyses that only rely on functions&#39; control flow.
Definition: PassManager.h:115
void initializeInstSimplifyLegacyPassPass(PassRegistry &)
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:190
#define I(x, y, z)
Definition: MD5.cpp:58
Analysis pass providing the TargetLibraryInfo.
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:575
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
Definition: Local.cpp:359
LLVM Value Representation.
Definition: Value.h:74
OptimizationRemarkEmitter legacy analysis pass.
const DominatorTree * DT
inst_range instructions(Function *F)
Definition: InstIterator.h:133
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
Value * SimplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
The optimization diagnostic interface.
INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify", "Remove redundant instructions", false, false) INITIALIZE_PASS_END(InstSimplifyLegacyPass