LLVM 24.0.0git
WebAssemblyOptimizeReturned.cpp
Go to the documentation of this file.
1//===-- WebAssemblyOptimizeReturned.cpp - Optimize "returned" attributes --===//
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/// \file
10/// Optimize calls with "returned" attributes for WebAssembly.
11///
12//===----------------------------------------------------------------------===//
13
14#include "WebAssembly.h"
15#include "llvm/IR/Analysis.h"
16#include "llvm/IR/Dominators.h"
17#include "llvm/IR/InstVisitor.h"
18#include "llvm/IR/PassManager.h"
19#include "llvm/Support/Debug.h"
21using namespace llvm;
22
23#define DEBUG_TYPE "wasm-optimize-returned"
24
25namespace {
26class WebAssemblyOptimizeReturnedImpl
27 : public InstVisitor<WebAssemblyOptimizeReturnedImpl> {
28 DominatorTree *DT = nullptr;
29
30public:
31 WebAssemblyOptimizeReturnedImpl(DominatorTree *DT) : DT(DT) {}
33 void visitCallBase(CallBase &CB);
34};
35
36class WebAssemblyOptimizeReturnedLegacy final : public FunctionPass {
37 StringRef getPassName() const override {
38 return "WebAssembly Optimize Returned";
39 }
40
41 void getAnalysisUsage(AnalysisUsage &AU) const override {
42 AU.setPreservesCFG();
46 }
47
48 bool runOnFunction(Function &F) override;
49
50public:
51 static char ID;
52 WebAssemblyOptimizeReturnedLegacy() : FunctionPass(ID) {}
53
54 void visitCallBase(CallBase &CB);
55};
56} // End anonymous namespace
57
58char WebAssemblyOptimizeReturnedLegacy::ID = 0;
59INITIALIZE_PASS(WebAssemblyOptimizeReturnedLegacy, DEBUG_TYPE,
60 "Optimize calls with \"returned\" attributes for WebAssembly",
61 false, false)
62
64 return new WebAssemblyOptimizeReturnedLegacy();
65}
66
67void WebAssemblyOptimizeReturnedImpl::visitCallBase(CallBase &CB) {
68 for (unsigned I = 0, E = CB.arg_size(); I < E; ++I)
69 if (CB.paramHasAttr(I, Attribute::Returned)) {
70 Value *Arg = CB.getArgOperand(I);
71 // Ignore constants, globals, undef, etc.
72 if (isa<Constant>(Arg))
73 continue;
74 // Like replaceDominatedUsesWith but using Instruction/Use dominance.
75 Arg->replaceUsesWithIf(&CB, [&](Use &U) {
76 auto *I = cast<Instruction>(U.getUser());
77 return !I->isLifetimeStartOrEnd() && DT->dominates(&CB, U);
78 });
79 }
80}
81
82bool WebAssemblyOptimizeReturnedImpl::runOnFunction(Function &F) {
83 LLVM_DEBUG(dbgs() << "********** Optimize returned Attributes **********\n"
84 "********** Function: "
85 << F.getName() << '\n');
86
87 visit(F);
88 return true;
89}
90
91bool WebAssemblyOptimizeReturnedLegacy::runOnFunction(Function &F) {
92 DominatorTree *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
93 WebAssemblyOptimizeReturnedImpl Impl(DT);
94 return Impl.runOnFunction(F);
95}
96
97PreservedAnalyses
100 DominatorTree *DT = &FAM.getResult<DominatorTreeAnalysis>(F);
101 WebAssemblyOptimizeReturnedImpl Impl(DT);
102 return Impl.runOnFunction(F)
105}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
This header defines various interfaces for pass management in LLVM.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
FunctionAnalysisManager FAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
#define LLVM_DEBUG(...)
Definition Debug.h:119
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
Analysis pass which computes a DominatorTree.
Definition Dominators.h:270
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:306
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:151
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
Base class for instruction visitors.
Definition InstVisitor.h:78
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition Pass.cpp:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI bool replaceUsesWithIf(Value *New, llvm::function_ref< bool(Use &U)> ShouldReplace)
Go through the uses list for this definition and make each use point to "V" if the callback ShouldRep...
Definition Value.cpp:561
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
Pass manager infrastructure for declaring and invalidating analyses.
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.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
FunctionPass * createWebAssemblyOptimizeReturnedLegacyPass()
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.