LLVM 19.0.0git
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/Statistic.h"
15#include "llvm/IR/Dominators.h"
16#include "llvm/IR/Function.h"
18#include "llvm/Pass.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "instsimplify"
25
26STATISTIC(NumSimplified, "Number of redundant instructions removed");
27
28static bool runImpl(Function &F, const SimplifyQuery &SQ) {
30 bool Changed = false;
31
32 do {
33 for (BasicBlock &BB : F) {
34 // Unreachable code can take on strange forms that we are not prepared to
35 // handle. For example, an instruction may have itself as an operand.
36 if (!SQ.DT->isReachableFromEntry(&BB))
37 continue;
38
40 for (Instruction &I : BB) {
41 // The first time through the loop, ToSimplify is empty and we try to
42 // simplify all instructions. On later iterations, ToSimplify is not
43 // empty and we only bother simplifying instructions that are in it.
44 if (!ToSimplify->empty() && !ToSimplify->count(&I))
45 continue;
46
47 // Don't waste time simplifying dead/unused instructions.
49 DeadInstsInBB.push_back(&I);
50 Changed = true;
51 } else if (!I.use_empty()) {
52 if (Value *V = simplifyInstruction(&I, SQ)) {
53 // Mark all uses for resimplification next time round the loop.
54 for (User *U : I.users())
55 Next->insert(cast<Instruction>(U));
56 I.replaceAllUsesWith(V);
57 ++NumSimplified;
58 Changed = true;
59 // A call can get simplified, but it may not be trivially dead.
61 DeadInstsInBB.push_back(&I);
62 }
63 }
64 }
66 }
67
68 // Place the list of instructions to simplify on the next loop iteration
69 // into ToSimplify.
70 std::swap(ToSimplify, Next);
71 Next->clear();
72 } while (!ToSimplify->empty());
73
74 return Changed;
75}
76
77namespace {
78struct InstSimplifyLegacyPass : public FunctionPass {
79 static char ID; // Pass identification, replacement for typeid
80 InstSimplifyLegacyPass() : FunctionPass(ID) {
82 }
83
84 void getAnalysisUsage(AnalysisUsage &AU) const override {
85 AU.setPreservesCFG();
89 }
90
91 /// Remove instructions that simplify.
92 bool runOnFunction(Function &F) override {
93 if (skipFunction(F))
94 return false;
95
96 const DominatorTree *DT =
97 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
98 const TargetLibraryInfo *TLI =
99 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
100 AssumptionCache *AC =
101 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
102 const DataLayout &DL = F.getParent()->getDataLayout();
103 const SimplifyQuery SQ(DL, TLI, DT, AC);
104 return runImpl(F, SQ);
105 }
106};
107} // namespace
108
109char InstSimplifyLegacyPass::ID = 0;
110INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass, "instsimplify",
111 "Remove redundant instructions", false, false)
115INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
116 "Remove redundant instructions", false, false)
117
118// Public interface to the simplify instructions pass.
120 return new InstSimplifyLegacyPass();
121}
122
125 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
126 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
127 auto &AC = AM.getResult<AssumptionAnalysis>(F);
128 const DataLayout &DL = F.getParent()->getDataLayout();
129 const SimplifyQuery SQ(DL, &TLI, &DT, &AC);
130 bool Changed = runImpl(F, SQ);
131 if (!Changed)
132 return PreservedAnalyses::all();
133
136 return PA;
137}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const LLT S1
static bool runImpl(Function &F, const TargetLowering &TLI)
instsimplify
static bool runImpl(Function &F, const SimplifyQuery &SQ)
Remove redundant instructions
Defines passes for running instruction simplification across chunks of IR.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
SmallVector< Instruction *, 4 > ToSimplify
Definition: NVVMReflect.cpp:94
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
This file defines the SmallPtrSet class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:500
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:269
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:70
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:317
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:178
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:144
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
LLVM Value Representation.
Definition: Value.h:74
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:533
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:399
void initializeInstSimplifyLegacyPassPass(PassRegistry &)
FunctionPass * createInstSimplifyLegacyPass()
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
const DominatorTree * DT
Definition: SimplifyQuery.h:63
const TargetLibraryInfo * TLI
Definition: SimplifyQuery.h:62