Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/WebAssembly/WebAssemblyOptimizeReturned.cpp
Line
Count
Source
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"
18
#include "llvm/Support/raw_ostream.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
4.36k
  StringRef getPassName() const override {
27
4.36k
    return "WebAssembly Optimize Returned";
28
4.36k
  }
29
30
413
  void getAnalysisUsage(AnalysisUsage &AU) const override {
31
413
    AU.setPreservesCFG();
32
413
    AU.addRequired<DominatorTreeWrapperPass>();
33
413
    AU.addPreserved<DominatorTreeWrapperPass>();
34
413
    FunctionPass::getAnalysisUsage(AU);
35
413
  }
36
37
  bool runOnFunction(Function &F) override;
38
39
  DominatorTree *DT = nullptr;
40
41
public:
42
  static char ID;
43
413
  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
54
413
FunctionPass *llvm::createWebAssemblyOptimizeReturned() {
55
413
  return new OptimizeReturned();
56
413
}
57
58
1.28k
void OptimizeReturned::visitCallSite(CallSite CS) {
59
2.70k
  for (unsigned I = 0, E = CS.getNumArgOperands(); I < E; 
++I1.41k
)
60
1.41k
    if (CS.paramHasAttr(I, Attribute::Returned)) {
61
12
      Instruction *Inst = CS.getInstruction();
62
12
      Value *Arg = CS.getArgOperand(I);
63
12
      // Ignore constants, globals, undef, etc.
64
12
      if (isa<Constant>(Arg))
65
1
        continue;
66
11
      // Like replaceDominatedUsesWith but using Instruction/Use dominance.
67
31
      
for (auto UI = Arg->use_begin(), UE = Arg->use_end(); 11
UI != UE;) {
68
20
        Use &U = *UI++;
69
20
        if (DT->dominates(Inst, U))
70
3
          U.set(Inst);
71
20
      }
72
11
    }
73
1.28k
}
74
75
4.36k
bool OptimizeReturned::runOnFunction(Function &F) {
76
4.36k
  LLVM_DEBUG(dbgs() << "********** Optimize returned Attributes **********\n"
77
4.36k
                       "********** Function: "
78
4.36k
                    << F.getName() << '\n');
79
4.36k
80
4.36k
  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
81
4.36k
  visit(F);
82
4.36k
  return true;
83
4.36k
}