LLVM 22.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/Dominators.h"
16#include "llvm/IR/InstVisitor.h"
17#include "llvm/Support/Debug.h"
19using namespace llvm;
20
21#define DEBUG_TYPE "wasm-optimize-returned"
22
23namespace {
24class OptimizeReturned final : public FunctionPass,
25 public InstVisitor<OptimizeReturned> {
26 StringRef getPassName() const override {
27 return "WebAssembly Optimize Returned";
28 }
29
30 void getAnalysisUsage(AnalysisUsage &AU) const override {
31 AU.setPreservesCFG();
35 }
36
37 bool runOnFunction(Function &F) override;
38
39 DominatorTree *DT = nullptr;
40
41public:
42 static char ID;
43 OptimizeReturned() : FunctionPass(ID) {}
44
45 void visitCallBase(CallBase &CB);
46};
47} // End anonymous namespace
48
49char OptimizeReturned::ID = 0;
50INITIALIZE_PASS(OptimizeReturned, DEBUG_TYPE,
51 "Optimize calls with \"returned\" attributes for WebAssembly",
52 false, false)
53
55 return new OptimizeReturned();
56}
57
58void OptimizeReturned::visitCallBase(CallBase &CB) {
59 for (unsigned I = 0, E = CB.arg_size(); I < E; ++I)
60 if (CB.paramHasAttr(I, Attribute::Returned)) {
61 Value *Arg = CB.getArgOperand(I);
62 // Ignore constants, globals, undef, etc.
63 if (isa<Constant>(Arg))
64 continue;
65 // Like replaceDominatedUsesWith but using Instruction/Use dominance.
66 Arg->replaceUsesWithIf(&CB, [&](Use &U) {
67 auto *I = cast<Instruction>(U.getUser());
68 return !I->isLifetimeStartOrEnd() && DT->dominates(&CB, U);
69 });
70 }
71}
72
73bool OptimizeReturned::runOnFunction(Function &F) {
74 LLVM_DEBUG(dbgs() << "********** Optimize returned Attributes **********\n"
75 "********** Function: "
76 << F.getName() << '\n');
77
78 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
79 visit(F);
80 return true;
81}
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
void visit(MachineFunction &MF, MachineBasicBlock &Start, std::function< void(MachineBasicBlock *)> op)
#define LLVM_DEBUG(...)
Definition: Debug.h:119
#define DEBUG_TYPE
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:270
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
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
Definition: InstrTypes.h:1292
unsigned arg_size() const
Definition: InstrTypes.h:1290
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:322
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
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.
Definition: Dominators.cpp:135
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Base class for instruction visitors.
Definition: InstVisitor.h:78
RetTy visitCallBase(CallBase &I)
Definition: InstVisitor.h:262
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:112
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:85
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
LLVM Value Representation.
Definition: Value.h:75
LLVM_ABI void 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:554
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
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
FunctionPass * createWebAssemblyOptimizeReturned()