LLVM  10.0.0svn
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 visitCallSite(CallSite CS);
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::visitCallSite(CallSite CS) {
59  for (unsigned I = 0, E = CS.getNumArgOperands(); I < E; ++I)
60  if (CS.paramHasAttr(I, Attribute::Returned)) {
61  Instruction *Inst = CS.getInstruction();
62  Value *Arg = CS.getArgOperand(I);
63  // Ignore constants, globals, undef, etc.
64  if (isa<Constant>(Arg))
65  continue;
66  // Like replaceDominatedUsesWith but using Instruction/Use dominance.
67  Arg->replaceUsesWithIf(Inst,
68  [&](Use &U) { return DT->dominates(Inst, U); });
69  }
70 }
71 
73  LLVM_DEBUG(dbgs() << "********** Optimize returned Attributes **********\n"
74  "********** Function: "
75  << F.getName() << '\n');
76 
77  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
78  visit(F);
79  return true;
80 }
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Base class for instruction visitors.
Definition: InstVisitor.h:80
This class represents lattice values for constants.
Definition: AllocatorList.h:23
#define DEBUG_TYPE
F(f)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
AnalysisUsage & addRequired()
INITIALIZE_PASS(OptimizeReturned, DEBUG_TYPE, "Optimize calls with \eturned\attributes for WebAssembly", false, false) FunctionPass *llvm
FunctionPass * createWebAssemblyOptimizeReturned()
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
InstrTy * getInstruction() const
Definition: CallSite.h:96
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:96
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the call or the callee has the given attribute.
Definition: CallSite.h:385
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:144
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.h:300
static bool runOnFunction(Function &F, bool PostInlining)
unsigned getNumArgOperands() const
Definition: CallSite.h:303
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM Value Representation.
Definition: Value.h:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:259
#define LLVM_DEBUG(X)
Definition: Debug.h:122
ValTy * getArgOperand(unsigned i) const
Definition: CallSite.h:307