1 //===-- WebAssemblyOptimizeReturned.cpp - Optimize "returned" attributes --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Optimize calls with "returned" attributes for WebAssembly.
13 //===----------------------------------------------------------------------===//
15 #include "WebAssembly.h"
16 #include "llvm/IR/Dominators.h"
17 #include "llvm/IR/InstVisitor.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
22 #define DEBUG_TYPE "wasm-optimize-returned"
25 class OptimizeReturned final : public FunctionPass,
26 public InstVisitor<OptimizeReturned> {
27 const char *getPassName() const override {
28 return "WebAssembly Optimize Returned";
31 void getAnalysisUsage(AnalysisUsage &AU) const override {
33 AU.addRequired<DominatorTreeWrapperPass>();
34 AU.addPreserved<DominatorTreeWrapperPass>();
35 FunctionPass::getAnalysisUsage(AU);
38 bool runOnFunction(Function &F) override;
44 OptimizeReturned() : FunctionPass(ID), DT(nullptr) {}
46 void visitCallSite(CallSite CS);
48 } // End anonymous namespace
50 char OptimizeReturned::ID = 0;
51 FunctionPass *llvm::createWebAssemblyOptimizeReturned() {
52 return new OptimizeReturned();
55 void OptimizeReturned::visitCallSite(CallSite CS) {
56 for (unsigned i = 0, e = CS.getNumArgOperands(); i < e; ++i)
57 if (CS.paramHasAttr(1 + i, Attribute::Returned)) {
58 Instruction *Inst = CS.getInstruction();
59 Value *Arg = CS.getArgOperand(i);
60 // Ignore constants, globals, undef, etc.
61 if (isa<Constant>(Arg))
63 // Like replaceDominatedUsesWith but using Instruction/Use dominance.
64 for (auto UI = Arg->use_begin(), UE = Arg->use_end(); UI != UE;) {
66 if (DT->dominates(Inst, U))
72 bool OptimizeReturned::runOnFunction(Function &F) {
73 DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();