blob: 6f0ae897b96749326c80ac5f66954972387995e0 [file] [log] [blame]
Dan Gohmanbb372242016-01-26 03:39:31 +00001//=- WebAssemblySetP2AlignOperands.cpp - Set alignments on loads and stores -=//
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
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file sets the p2align operands on load and store instructions.
Dan Gohmanbb372242016-01-26 03:39:31 +000012///
13//===----------------------------------------------------------------------===//
14
Dan Gohmanbb372242016-01-26 03:39:31 +000015#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "WebAssembly.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
Dan Gohman7f1bdb22016-10-06 22:08:28 +000057static void RewriteP2Align(MachineInstr &MI, unsigned OperandNo) {
58 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() ==
63 (UINT64_C(1)
64 << WebAssembly::GetDefaultP2Align(MI.getOpcode())) &&
65 "Default p2align value should be natural");
66 assert(MI.getDesc().OpInfo[OperandNo].OperandType ==
67 WebAssembly::OPERAND_P2ALIGN &&
68 "Load and store instructions should have a p2align operand");
69 uint64_t P2Align = Log2_64((*MI.memoperands_begin())->getAlignment());
70
71 // WebAssembly does not currently support supernatural alignment.
72 P2Align = std::min(
73 P2Align, uint64_t(WebAssembly::GetDefaultP2Align(MI.getOpcode())));
74
75 MI.getOperand(OperandNo).setImm(P2Align);
76}
77
Dan Gohmanbb372242016-01-26 03:39:31 +000078bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000079 LLVM_DEBUG({
Dan Gohmanbb372242016-01-26 03:39:31 +000080 dbgs() << "********** Set p2align Operands **********\n"
81 << "********** Function: " << MF.getName() << '\n';
82 });
83
84 bool Changed = false;
85
86 for (auto &MBB : MF) {
87 for (auto &MI : MBB) {
88 switch (MI.getOpcode()) {
89 case WebAssembly::LOAD_I32:
90 case WebAssembly::LOAD_I64:
91 case WebAssembly::LOAD_F32:
92 case WebAssembly::LOAD_F64:
93 case WebAssembly::LOAD8_S_I32:
94 case WebAssembly::LOAD8_U_I32:
95 case WebAssembly::LOAD16_S_I32:
96 case WebAssembly::LOAD16_U_I32:
97 case WebAssembly::LOAD8_S_I64:
98 case WebAssembly::LOAD8_U_I64:
99 case WebAssembly::LOAD16_S_I64:
100 case WebAssembly::LOAD16_U_I64:
101 case WebAssembly::LOAD32_S_I64:
102 case WebAssembly::LOAD32_U_I64:
Derek Schuff18ba1922017-08-30 18:07:45 +0000103 case WebAssembly::ATOMIC_LOAD_I32:
Derek Schuff885dc592017-10-05 21:18:42 +0000104 case WebAssembly::ATOMIC_LOAD8_U_I32:
105 case WebAssembly::ATOMIC_LOAD16_U_I32:
106 case WebAssembly::ATOMIC_LOAD_I64:
107 case WebAssembly::ATOMIC_LOAD8_U_I64:
108 case WebAssembly::ATOMIC_LOAD16_U_I64:
109 case WebAssembly::ATOMIC_LOAD32_U_I64:
Dan Gohman7f1bdb22016-10-06 22:08:28 +0000110 RewriteP2Align(MI, WebAssembly::LoadP2AlignOperandNo);
111 break;
Dan Gohmanbb372242016-01-26 03:39:31 +0000112 case WebAssembly::STORE_I32:
113 case WebAssembly::STORE_I64:
114 case WebAssembly::STORE_F32:
115 case WebAssembly::STORE_F64:
116 case WebAssembly::STORE8_I32:
117 case WebAssembly::STORE16_I32:
118 case WebAssembly::STORE8_I64:
119 case WebAssembly::STORE16_I64:
Dan Gohman7f1bdb22016-10-06 22:08:28 +0000120 case WebAssembly::STORE32_I64:
Heejin Ahn402b4902018-07-02 21:22:59 +0000121 case WebAssembly::ATOMIC_STORE_I32:
122 case WebAssembly::ATOMIC_STORE8_I32:
123 case WebAssembly::ATOMIC_STORE16_I32:
124 case WebAssembly::ATOMIC_STORE_I64:
125 case WebAssembly::ATOMIC_STORE8_I64:
126 case WebAssembly::ATOMIC_STORE16_I64:
127 case WebAssembly::ATOMIC_STORE32_I64:
Dan Gohman7f1bdb22016-10-06 22:08:28 +0000128 RewriteP2Align(MI, WebAssembly::StoreP2AlignOperandNo);
Dan Gohmanbb372242016-01-26 03:39:31 +0000129 break;
Dan Gohmanbb372242016-01-26 03:39:31 +0000130 default:
131 break;
132 }
133 }
134 }
135
136 return Changed;
137}