LLVM  14.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"
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "wasm-optimize-returned"
22 
23 namespace {
24 class 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 
41 public:
42  static char ID;
43  OptimizeReturned() : FunctionPass(ID) {}
44 
45  void visitCallBase(CallBase &CB);
46 };
47 } // End anonymous namespace
48 
49 char OptimizeReturned::ID = 0;
50 INITIALIZE_PASS(OptimizeReturned, DEBUG_TYPE,
51  "Optimize calls with \"returned\" attributes for WebAssembly",
52  false, false)
53 
55  return new OptimizeReturned();
56 }
57 
58 void 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,
67  [&](Use &U) { return DT->dominates(&CB, U); });
68  }
69 }
70 
72  LLVM_DEBUG(dbgs() << "********** Optimize returned Attributes **********\n"
73  "********** Function: "
74  << F.getName() << '\n');
75 
76  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
77  visit(F);
78  return true;
79 }
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
WebAssembly.h
llvm::Function
Definition: Function.h:62
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:151
LLVM_DEBUG
#define LLVM_DEBUG(X)
Definition: Debug.h:101
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
INITIALIZE_PASS
INITIALIZE_PASS(OptimizeReturned, DEBUG_TYPE, "Optimize calls with \"returned\" attributes for WebAssembly", false, false) FunctionPass *llvm
Definition: WebAssemblyOptimizeReturned.cpp:50
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:287
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::createWebAssemblyOptimizeReturned
FunctionPass * createWebAssemblyOptimizeReturned()
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
InstVisitor.h
llvm::AnalysisUsage::addPreserved
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Definition: PassAnalysisSupport.h:98
llvm::InstVisitor
Base class for instruction visitors.
Definition: InstVisitor.h:79
llvm::CallBase::paramHasAttr
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
Definition: Instructions.cpp:341
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::CallBase::arg_size
unsigned arg_size() const
Definition: InstrTypes.h:1326
Dominators.h
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1328
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1161
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:93
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
raw_ostream.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
Debug.h
DEBUG_TYPE
#define DEBUG_TYPE
Definition: WebAssemblyOptimizeReturned.cpp:21
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition: Use.h:44
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37