Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/WebAssembly/WebAssemblySetP2AlignOperands.cpp
Line
Count
Source
1
//=- WebAssemblySetP2AlignOperands.cpp - Set alignments on loads and stores -=//
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
/// This file sets the p2align operands on load and store instructions.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
15
#include "WebAssembly.h"
16
#include "WebAssemblyInstrInfo.h"
17
#include "WebAssemblyMachineFunctionInfo.h"
18
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
19
#include "llvm/CodeGen/MachineMemOperand.h"
20
#include "llvm/CodeGen/Passes.h"
21
#include "llvm/Support/Debug.h"
22
#include "llvm/Support/raw_ostream.h"
23
using namespace llvm;
24
25
#define DEBUG_TYPE "wasm-set-p2align-operands"
26
27
namespace {
28
class WebAssemblySetP2AlignOperands final : public MachineFunctionPass {
29
public:
30
  static char ID; // Pass identification, replacement for typeid
31
426
  WebAssemblySetP2AlignOperands() : MachineFunctionPass(ID) {}
32
33
4.45k
  StringRef getPassName() const override {
34
4.45k
    return "WebAssembly Set p2align Operands";
35
4.45k
  }
36
37
426
  void getAnalysisUsage(AnalysisUsage &AU) const override {
38
426
    AU.setPreservesCFG();
39
426
    AU.addPreserved<MachineBlockFrequencyInfo>();
40
426
    AU.addPreservedID(MachineDominatorsID);
41
426
    MachineFunctionPass::getAnalysisUsage(AU);
42
426
  }
43
44
  bool runOnMachineFunction(MachineFunction &MF) override;
45
};
46
} // end anonymous namespace
47
48
char WebAssemblySetP2AlignOperands::ID = 0;
49
INITIALIZE_PASS(WebAssemblySetP2AlignOperands, DEBUG_TYPE,
50
                "Set the p2align operands for WebAssembly loads and stores",
51
                false, false)
52
53
426
FunctionPass *llvm::createWebAssemblySetP2AlignOperands() {
54
426
  return new WebAssemblySetP2AlignOperands();
55
426
}
56
57
5.62k
static void rewriteP2Align(MachineInstr &MI, unsigned OperandNo) {
58
5.62k
  assert(MI.getOperand(OperandNo).getImm() == 0 &&
59
5.62k
         "ISel should set p2align operands to 0");
60
5.62k
  assert(MI.hasOneMemOperand() &&
61
5.62k
         "Load and store instructions have exactly one mem operand");
62
5.62k
  assert((*MI.memoperands_begin())->getSize() ==
63
5.62k
             (UINT64_C(1) << WebAssembly::GetDefaultP2Align(MI.getOpcode())) &&
64
5.62k
         "Default p2align value should be natural");
65
5.62k
  assert(MI.getDesc().OpInfo[OperandNo].OperandType ==
66
5.62k
             WebAssembly::OPERAND_P2ALIGN &&
67
5.62k
         "Load and store instructions should have a p2align operand");
68
5.62k
  uint64_t P2Align = Log2_64((*MI.memoperands_begin())->getAlignment());
69
5.62k
70
5.62k
  // WebAssembly does not currently support supernatural alignment.
71
5.62k
  P2Align = std::min(P2Align,
72
5.62k
                     uint64_t(WebAssembly::GetDefaultP2Align(MI.getOpcode())));
73
5.62k
74
5.62k
  MI.getOperand(OperandNo).setImm(P2Align);
75
5.62k
}
76
77
4.45k
bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) {
78
4.45k
  LLVM_DEBUG({
79
4.45k
    dbgs() << "********** Set p2align Operands **********\n"
80
4.45k
           << "********** Function: " << MF.getName() << '\n';
81
4.45k
  });
82
4.45k
83
4.45k
  bool Changed = false;
84
4.45k
85
5.10k
  for (auto &MBB : MF) {
86
44.3k
    for (auto &MI : MBB) {
87
44.3k
      int16_t P2AlignOpNum = WebAssembly::getNamedOperandIdx(
88
44.3k
          MI.getOpcode(), WebAssembly::OpName::p2align);
89
44.3k
      if (P2AlignOpNum != -1) {
90
5.62k
        rewriteP2Align(MI, P2AlignOpNum);
91
5.62k
        Changed = true;
92
5.62k
      }
93
44.3k
    }
94
5.10k
  }
95
4.45k
96
4.45k
  return Changed;
97
4.45k
}