blob: a249ccf1763865e648a0ab28fce020cf4987b9b9 [file] [log] [blame]
Dan Gohmanbb372242016-01-26 03:39:31 +00001//=- WebAssemblySetP2AlignOperands.cpp - Set alignments on loads and stores -=//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dan Gohmanbb372242016-01-26 03:39:31 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000010/// This file sets the p2align operands on load and store instructions.
Dan Gohmanbb372242016-01-26 03:39:31 +000011///
12//===----------------------------------------------------------------------===//
13
Dan Gohmanbb372242016-01-26 03:39:31 +000014#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000015#include "WebAssembly.h"
Thomas Lively972d7d52019-03-09 04:31:37 +000016#include "WebAssemblyInstrInfo.h"
Dan Gohmanbb372242016-01-26 03:39:31 +000017#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"
23using namespace llvm;
24
25#define DEBUG_TYPE "wasm-set-p2align-operands"
26
27namespace {
28class WebAssemblySetP2AlignOperands final : public MachineFunctionPass {
29public:
30 static char ID; // Pass identification, replacement for typeid
31 WebAssemblySetP2AlignOperands() : MachineFunctionPass(ID) {}
32
Mehdi Amini117296c2016-10-01 02:56:57 +000033 StringRef getPassName() const override {
Dan Gohmanbb372242016-01-26 03:39:31 +000034 return "WebAssembly Set p2align Operands";
35 }
36
37 void getAnalysisUsage(AnalysisUsage &AU) const override {
38 AU.setPreservesCFG();
39 AU.addPreserved<MachineBlockFrequencyInfo>();
40 AU.addPreservedID(MachineDominatorsID);
41 MachineFunctionPass::getAnalysisUsage(AU);
42 }
43
44 bool runOnMachineFunction(MachineFunction &MF) override;
45};
46} // end anonymous namespace
47
48char WebAssemblySetP2AlignOperands::ID = 0;
Jacob Gravelle40926452018-03-30 20:36:58 +000049INITIALIZE_PASS(WebAssemblySetP2AlignOperands, DEBUG_TYPE,
50 "Set the p2align operands for WebAssembly loads and stores",
51 false, false)
52
Dan Gohmanbb372242016-01-26 03:39:31 +000053FunctionPass *llvm::createWebAssemblySetP2AlignOperands() {
54 return new WebAssemblySetP2AlignOperands();
55}
56
Heejin Ahn18c56a02019-02-04 19:13:39 +000057static void rewriteP2Align(MachineInstr &MI, unsigned OperandNo) {
Dan Gohman7f1bdb22016-10-06 22:08:28 +000058 assert(MI.getOperand(OperandNo).getImm() == 0 &&
59 "ISel should set p2align operands to 0");
60 assert(MI.hasOneMemOperand() &&
61 "Load and store instructions have exactly one mem operand");
62 assert((*MI.memoperands_begin())->getSize() ==
Heejin Ahnf208f632018-09-05 01:27:38 +000063 (UINT64_C(1) << WebAssembly::GetDefaultP2Align(MI.getOpcode())) &&
Dan Gohman7f1bdb22016-10-06 22:08:28 +000064 "Default p2align value should be natural");
65 assert(MI.getDesc().OpInfo[OperandNo].OperandType ==
66 WebAssembly::OPERAND_P2ALIGN &&
67 "Load and store instructions should have a p2align operand");
68 uint64_t P2Align = Log2_64((*MI.memoperands_begin())->getAlignment());
69
70 // WebAssembly does not currently support supernatural alignment.
Heejin Ahnf208f632018-09-05 01:27:38 +000071 P2Align = std::min(P2Align,
72 uint64_t(WebAssembly::GetDefaultP2Align(MI.getOpcode())));
Dan Gohman7f1bdb22016-10-06 22:08:28 +000073
74 MI.getOperand(OperandNo).setImm(P2Align);
75}
76
Dan Gohmanbb372242016-01-26 03:39:31 +000077bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000078 LLVM_DEBUG({
Dan Gohmanbb372242016-01-26 03:39:31 +000079 dbgs() << "********** Set p2align Operands **********\n"
80 << "********** Function: " << MF.getName() << '\n';
81 });
82
83 bool Changed = false;
84
85 for (auto &MBB : MF) {
86 for (auto &MI : MBB) {
Thomas Lively972d7d52019-03-09 04:31:37 +000087 int16_t P2AlignOpNum = WebAssembly::getNamedOperandIdx(
88 MI.getOpcode(), WebAssembly::OpName::p2align);
89 if (P2AlignOpNum != -1) {
90 rewriteP2Align(MI, P2AlignOpNum);
91 Changed = true;
Dan Gohmanbb372242016-01-26 03:39:31 +000092 }
93 }
94 }
95
96 return Changed;
97}