LLVM  10.0.0svn
InstSimplifyPass.cpp
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 //===----------------------------------------------------------------------===//
8 
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;
25 
26 #define DEBUG_TYPE "instsimplify"
27 
28 STATISTIC(NumSimplified, "Number of redundant instructions removed");
29 
30 static bool runImpl(Function &F, const SimplifyQuery &SQ,
32  SmallPtrSet<const Instruction *, 8> S1, S2, *ToSimplify = &S1, *Next = &S2;
33  bool Changed = false;
34 
35  do {
36  for (BasicBlock &BB : F) {
37  SmallVector<Instruction *, 8> DeadInstsInBB;
38  for (Instruction &I : BB) {
39  // The first time through the loop, ToSimplify is empty and we try to
40  // simplify all instructions. On later iterations, ToSimplify is not
41  // empty and we only bother simplifying instructions that are in it.
42  if (!ToSimplify->empty() && !ToSimplify->count(&I))
43  continue;
44 
45  // Don't waste time simplifying dead/unused instructions.
47  DeadInstsInBB.push_back(&I);
48  Changed = true;
49  } else if (!I.use_empty()) {
50  if (Value *V = SimplifyInstruction(&I, SQ, ORE)) {
51  // Mark all uses for resimplification next time round the loop.
52  for (User *U : I.users())
53  Next->insert(cast<Instruction>(U));
54  I.replaceAllUsesWith(V);
55  ++NumSimplified;
56  Changed = true;
57  // A call can get simplified, but it may not be trivially dead.
59  DeadInstsInBB.push_back(&I);
60  }
61  }
62  }
64  }
65 
66  // Place the list of instructions to simplify on the next loop iteration
67  // into ToSimplify.
68  std::swap(ToSimplify, Next);
69  Next->clear();
70  } while (!ToSimplify->empty());
71 
72  return Changed;
73 }
74 
75 namespace {
76 struct InstSimplifyLegacyPass : public FunctionPass {
77  static char ID; // Pass identification, replacement for typeid
78  InstSimplifyLegacyPass() : FunctionPass(ID) {
80  }
81 
82  void getAnalysisUsage(AnalysisUsage &AU) const override {
83  AU.setPreservesCFG();
88  }
89 
90  /// runOnFunction - Remove instructions that simplify.
91  bool runOnFunction(Function &F) override {
92  if (skipFunction(F))
93  return false;
94 
95  const DominatorTree *DT =
96  &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
97  const TargetLibraryInfo *TLI =
98  &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
99  AssumptionCache *AC =
100  &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
102  &getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
103  const DataLayout &DL = F.getParent()->getDataLayout();
104  const SimplifyQuery SQ(DL, TLI, DT, AC);
105  return runImpl(F, SQ, ORE);
106  }
107 };
108 } // namespace
109 
111 INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
112  "Remove redundant instructions", false, false)
117 INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
118  "Remove redundant instructions", false, false)
119 
120 // Public interface to the simplify instructions pass.
122  return new InstSimplifyLegacyPass();
123 }
124 
127  auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
128  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
129  auto &AC = AM.getResult<AssumptionAnalysis>(F);
131  const DataLayout &DL = F.getParent()->getDataLayout();
132  const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
133  bool Changed = runImpl(F, SQ, &ORE);
134  if (!Changed)
135  return PreservedAnalyses::all();
136 
138  PA.preserveSet<CFGAnalyses>();
139  return PA;
140 }
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:776
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
F(f)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
const TargetLibraryInfo * TLI
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
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:153
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:159
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.
instsimplify
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:114
void initializeInstSimplifyLegacyPassPass(PassRegistry &)
void preserveSet()
Mark an analysis set as preserved.
Definition: PassManager.h:189
#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:73
OptimizationRemarkEmitter legacy analysis pass.
inst_range instructions(Function *F)
Definition: InstIterator.h:133
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