LLVM  8.0.1
WebAssemblyOptimizeReturned.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyOptimizeReturned.cpp - Optimize "returned" attributes --===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// Optimize calls with "returned" attributes for WebAssembly.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "WebAssembly.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/IR/InstVisitor.h"
18 #include "llvm/Support/Debug.h"
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "wasm-optimize-returned"
23 
24 namespace {
25 class OptimizeReturned final : public FunctionPass,
26  public InstVisitor<OptimizeReturned> {
27  StringRef getPassName() const override {
28  return "WebAssembly Optimize Returned";
29  }
30 
31  void getAnalysisUsage(AnalysisUsage &AU) const override {
32  AU.setPreservesCFG();
36  }
37 
38  bool runOnFunction(Function &F) override;
39 
40  DominatorTree *DT;
41 
42 public:
43  static char ID;
44  OptimizeReturned() : FunctionPass(ID), DT(nullptr) {}
45 
46  void visitCallSite(CallSite CS);
47 };
48 } // End anonymous namespace
49 
50 char OptimizeReturned::ID = 0;
51 INITIALIZE_PASS(OptimizeReturned, DEBUG_TYPE,
52  "Optimize calls with \"returned\" attributes for WebAssembly",
53  false, false)
54 
56  return new OptimizeReturned();
57 }
58 
59 void OptimizeReturned::visitCallSite(CallSite CS) {
60  for (unsigned i = 0, e = CS.getNumArgOperands(); i < e; ++i)
61  if (CS.paramHasAttr(i, Attribute::Returned)) {
62  Instruction *Inst = CS.getInstruction();
63  Value *Arg = CS.getArgOperand(i);
64  // Ignore constants, globals, undef, etc.
65  if (isa<Constant>(Arg))
66  continue;
67  // Like replaceDominatedUsesWith but using Instruction/Use dominance.
68  for (auto UI = Arg->use_begin(), UE = Arg->use_end(); UI != UE;) {
69  Use &U = *UI++;
70  if (DT->dominates(Inst, U))
71  U.set(Inst);
72  }
73  }
74 }
75 
77  LLVM_DEBUG(dbgs() << "********** Optimize returned Attributes **********\n"
78  "********** Function: "
79  << F.getName() << '\n');
80 
81  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
82  visit(F);
83  return true;
84 }
use_iterator use_end()
Definition: Value.h:347
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Base class for instruction visitors.
Definition: InstVisitor.h:81
This class represents lattice values for constants.
Definition: AllocatorList.h:24
#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:56
InstrTy * getInstruction() const
Definition: CallSite.h:92
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:92
ValTy * getArgOperand(unsigned i) const
Definition: CallSite.h:297
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the call or the callee has the given attribute.
Definition: CallSite.h:377
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:145
static bool runOnFunction(Function &F, bool PostInlining)
void set(Value *Val)
Definition: Value.h:671
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
unsigned getNumArgOperands() const
Definition: CallSite.h:293
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:286
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
amdgpu Simplify well known AMD library false Value Value * Arg
use_iterator use_begin()
Definition: Value.h:339
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
LLVM Value Representation.
Definition: Value.h:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:260
#define LLVM_DEBUG(X)
Definition: Debug.h:123