Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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
9#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
10#include "llvm/ADT/SmallPtrSet.h"
11#include "llvm/ADT/Statistic.h"
12#include "llvm/Analysis/AssumptionCache.h"
13#include "llvm/Analysis/InstructionSimplify.h"
14#include "llvm/Analysis/TargetLibraryInfo.h"
15#include "llvm/IR/Dominators.h"
16#include "llvm/IR/Function.h"
17#include "llvm/InitializePasses.h"
18#include "llvm/Pass.h"
19#include "llvm/Transforms/Scalar.h"
20#include "llvm/Transforms/Utils/Local.h"
21
22using namespacellvm;
23
24#define DEBUG_TYPE "instsimplify"
25
26STATISTIC(NumSimplified,"Number of redundant instructions removed");
27
28staticboolrunImpl(Function &F,constSimplifyQuery &SQ) {
29SmallPtrSet<const Instruction *, 8>S1, S2, *ToSimplify = &S1, *Next = &S2;
30bool Changed =false;
31
32do {
33for (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.
36if (!SQ.DT->isReachableFromEntry(&BB))
37continue;
38
39SmallVector<WeakTrackingVH, 8> DeadInstsInBB;
40for (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.
44if (!ToSimplify->empty() && !ToSimplify->count(&I))
45continue;
46
47// Don't waste time simplifying dead/unused instructions.
48if (isInstructionTriviallyDead(&I)) {
49 DeadInstsInBB.push_back(&I);
50 Changed =true;
51 }elseif (!I.use_empty()) {
52if (Value *V =simplifyInstruction(&I, SQ)) {
53// Mark all uses for resimplification next time round the loop.
54for (User *U :I.users())
55 Next->insert(cast<Instruction>(U));
56I.replaceAllUsesWith(V);
57 ++NumSimplified;
58 Changed =true;
59// A call can get simplified, but it may not be trivially dead.
60if (isInstructionTriviallyDead(&I))
61 DeadInstsInBB.push_back(&I);
62 }
63 }
64 }
65RecursivelyDeleteTriviallyDeadInstructions(DeadInstsInBB, SQ.TLI);
66 }
67
68// Place the list of instructions to simplify on the next loop iteration
69// into ToSimplify.
70std::swap(ToSimplify, Next);
71 Next->clear();
72 }while (!ToSimplify->empty());
73
74return Changed;
75}
76
77namespace{
78structInstSimplifyLegacyPass :publicFunctionPass {
79staticcharID;// Pass identification, replacement for typeid
80 InstSimplifyLegacyPass() :FunctionPass(ID) {
81initializeInstSimplifyLegacyPassPass(*PassRegistry::getPassRegistry());
82 }
83
84voidgetAnalysisUsage(AnalysisUsage &AU) const override{
85 AU.setPreservesCFG();
86 AU.addRequired<DominatorTreeWrapperPass>();
87 AU.addRequired<AssumptionCacheTracker>();
88 AU.addRequired<TargetLibraryInfoWrapperPass>();
89 }
90
91 /// Remove instructions that simplify.
92boolrunOnFunction(Function &F) override{
93if (skipFunction(F))
94returnfalse;
95
96constDominatorTree *DT =
97 &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
98constTargetLibraryInfo *TLI =
99 &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
100AssumptionCache *AC =
101 &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
102constDataLayout &DL =F.getDataLayout();
103constSimplifyQuery SQ(DL, TLI, DT, AC);
104returnrunImpl(F, SQ);
105 }
106};
107}// namespace
108
109char InstSimplifyLegacyPass::ID = 0;
110INITIALIZE_PASS_BEGIN(InstSimplifyLegacyPass,"instsimplify",
111"Remove redundant instructions",false,false)
112INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
113INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
114INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
115INITIALIZE_PASS_END(InstSimplifyLegacyPass, "instsimplify",
116 "Remove redundantinstructions",false,false)
117
118// Public interface to the simplify instructions pass.
119FunctionPass *llvm::createInstSimplifyLegacyPass() {
120returnnew InstSimplifyLegacyPass();
121}
122
123PreservedAnalysesInstSimplifyPass::run(Function &F,
124FunctionAnalysisManager &AM) {
125auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
126auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
127auto &AC = AM.getResult<AssumptionAnalysis>(F);
128constDataLayout &DL =F.getDataLayout();
129constSimplifyQuery SQ(DL, &TLI, &DT, &AC);
130bool Changed =runImpl(F, SQ);
131if (!Changed)
132returnPreservedAnalyses::all();
133
134PreservedAnalyses PA;
135 PA.preserveSet<CFGAnalyses>();
136return PA;
137}
S1
static const LLT S1
Definition:AMDGPULegalizerInfo.cpp:282
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition:ARMSLSHardening.cpp:73
AssumptionCache.h
Dominators.h
runImpl
static bool runImpl(Function &F, const TargetLowering &TLI)
Definition:ExpandLargeDivRem.cpp:79
Function.h
InitializePasses.h
instsimplify
instsimplify
Definition:InstSimplifyPass.cpp:115
runImpl
static bool runImpl(Function &F, const SimplifyQuery &SQ)
Definition:InstSimplifyPass.cpp:28
instructions
Remove redundant instructions
Definition:InstSimplifyPass.cpp:116
InstSimplifyPass.h
Defines passes for running instruction simplification across chunks of IR.
InstructionSimplify.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
I
#define I(x, y, z)
Definition:MD5.cpp:58
ToSimplify
SmallVector< Instruction *, 4 > ToSimplify
Definition:NVVMReflect.cpp:94
INITIALIZE_PASS_DEPENDENCY
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition:PassSupport.h:55
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:57
INITIALIZE_PASS_BEGIN
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:52
Pass.h
Scalar.h
SmallPtrSet.h
This file defines the SmallPtrSet class.
Statistic.h
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
STATISTIC
#define STATISTIC(VARNAME, DESC)
Definition:Statistic.h:166
TargetLibraryInfo.h
Local.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition:PassManager.h:253
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition:PassManager.h:410
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition:PassAnalysisSupport.h:75
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition:Pass.cpp:256
llvm::AssumptionAnalysis
A function analysis which provides an AssumptionCache.
Definition:AssumptionCache.h:173
llvm::AssumptionCacheTracker
An immutable pass that tracks lazily created AssumptionCache objects.
Definition:AssumptionCache.h:204
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition:AssumptionCache.h:42
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::CFGAnalyses
Represents analyses that only rely on functions' control flow.
Definition:Analysis.h:72
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition:DataLayout.h:63
llvm::DominatorTreeAnalysis
Analysis pass which computes a DominatorTree.
Definition:Dominators.h:279
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition:Dominators.h:317
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition:Dominators.h:162
llvm::DominatorTree::isReachableFromEntry
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition:Dominators.cpp:321
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition:Pass.h:310
llvm::FunctionPass::runOnFunction
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
llvm::FunctionPass::skipFunction
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition:Pass.cpp:178
llvm::Function
Definition:Function.h:63
llvm::InstSimplifyPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition:InstSimplifyPass.cpp:123
llvm::Instruction
Definition:Instruction.h:68
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition:PassRegistry.cpp:24
llvm::Pass::getAnalysisUsage
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition:Pass.cpp:98
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition:Analysis.h:111
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition:Analysis.h:117
llvm::PreservedAnalyses::preserveSet
void preserveSet()
Mark an analysis set as preserved.
Definition:Analysis.h:146
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition:SmallPtrSet.h:519
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition:TargetLibraryInfo.h:614
llvm::TargetLibraryInfoWrapperPass
Definition:TargetLibraryInfo.h:639
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition:TargetLibraryInfo.h:280
llvm::User
Definition:User.h:44
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
unsigned
false
Definition:StackSlotColoring.cpp:193
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition:CallingConv.h:24
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::RecursivelyDeleteTriviallyDeadInstructions
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:546
llvm::simplifyInstruction
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
Definition:InstructionSimplify.cpp:7234
llvm::isInstructionTriviallyDead
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:406
llvm::initializeInstSimplifyLegacyPassPass
void initializeInstSimplifyLegacyPassPass(PassRegistry &)
llvm::createInstSimplifyLegacyPass
FunctionPass * createInstSimplifyLegacyPass()
Definition:InstSimplifyPass.cpp:119
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition:BitVector.h:860
llvm::SimplifyQuery
Definition:SimplifyQuery.h:70
llvm::SimplifyQuery::DT
const DominatorTree * DT
Definition:SimplifyQuery.h:73
llvm::SimplifyQuery::TLI
const TargetLibraryInfo * TLI
Definition:SimplifyQuery.h:72

Generated on Thu Jul 17 2025 17:01:47 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp