1bb372243SDan Gohman //=- WebAssemblySetP2AlignOperands.cpp - Set alignments on loads and stores -=//
2bb372243SDan Gohman //
3bb372243SDan Gohman //                     The LLVM Compiler Infrastructure
4bb372243SDan Gohman //
5bb372243SDan Gohman // This file is distributed under the University of Illinois Open Source
6bb372243SDan Gohman // License. See LICENSE.TXT for details.
7bb372243SDan Gohman //
8bb372243SDan Gohman //===----------------------------------------------------------------------===//
9bb372243SDan Gohman ///
10bb372243SDan Gohman /// \file
115f8f34e4SAdrian Prantl /// This file sets the p2align operands on load and store instructions.
12bb372243SDan Gohman ///
13bb372243SDan Gohman //===----------------------------------------------------------------------===//
14bb372243SDan Gohman 
15bb372243SDan Gohman #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
166bda14b3SChandler Carruth #include "WebAssembly.h"
17bb372243SDan Gohman #include "WebAssemblyMachineFunctionInfo.h"
18bb372243SDan Gohman #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
19bb372243SDan Gohman #include "llvm/CodeGen/MachineMemOperand.h"
20bb372243SDan Gohman #include "llvm/CodeGen/Passes.h"
21bb372243SDan Gohman #include "llvm/Support/Debug.h"
22bb372243SDan Gohman #include "llvm/Support/raw_ostream.h"
23bb372243SDan Gohman using namespace llvm;
24bb372243SDan Gohman 
25bb372243SDan Gohman #define DEBUG_TYPE "wasm-set-p2align-operands"
26bb372243SDan Gohman 
27bb372243SDan Gohman namespace {
28bb372243SDan Gohman class WebAssemblySetP2AlignOperands final : public MachineFunctionPass {
29bb372243SDan Gohman public:
30bb372243SDan Gohman   static char ID; // Pass identification, replacement for typeid
31bb372243SDan Gohman   WebAssemblySetP2AlignOperands() : MachineFunctionPass(ID) {}
32bb372243SDan Gohman 
33117296c0SMehdi Amini   StringRef getPassName() const override {
34bb372243SDan Gohman     return "WebAssembly Set p2align Operands";
35bb372243SDan Gohman   }
36bb372243SDan Gohman 
37bb372243SDan Gohman   void getAnalysisUsage(AnalysisUsage &AU) const override {
38bb372243SDan Gohman     AU.setPreservesCFG();
39bb372243SDan Gohman     AU.addPreserved<MachineBlockFrequencyInfo>();
40bb372243SDan Gohman     AU.addPreservedID(MachineDominatorsID);
41bb372243SDan Gohman     MachineFunctionPass::getAnalysisUsage(AU);
42bb372243SDan Gohman   }
43bb372243SDan Gohman 
44bb372243SDan Gohman   bool runOnMachineFunction(MachineFunction &MF) override;
45bb372243SDan Gohman };
46bb372243SDan Gohman } // end anonymous namespace
47bb372243SDan Gohman 
48bb372243SDan Gohman char WebAssemblySetP2AlignOperands::ID = 0;
4940926451SJacob Gravelle INITIALIZE_PASS(WebAssemblySetP2AlignOperands, DEBUG_TYPE,
5040926451SJacob Gravelle                 "Set the p2align operands for WebAssembly loads and stores",
5140926451SJacob Gravelle                 false, false)
5240926451SJacob Gravelle 
53bb372243SDan Gohman FunctionPass *llvm::createWebAssemblySetP2AlignOperands() {
54bb372243SDan Gohman   return new WebAssemblySetP2AlignOperands();
55bb372243SDan Gohman }
56bb372243SDan Gohman 
577f1bdb2eSDan Gohman static void RewriteP2Align(MachineInstr &MI, unsigned OperandNo) {
587f1bdb2eSDan Gohman   assert(MI.getOperand(OperandNo).getImm() == 0 &&
597f1bdb2eSDan Gohman          "ISel should set p2align operands to 0");
607f1bdb2eSDan Gohman   assert(MI.hasOneMemOperand() &&
617f1bdb2eSDan Gohman          "Load and store instructions have exactly one mem operand");
627f1bdb2eSDan Gohman   assert((*MI.memoperands_begin())->getSize() ==
637f1bdb2eSDan Gohman              (UINT64_C(1)
647f1bdb2eSDan Gohman               << WebAssembly::GetDefaultP2Align(MI.getOpcode())) &&
657f1bdb2eSDan Gohman          "Default p2align value should be natural");
667f1bdb2eSDan Gohman   assert(MI.getDesc().OpInfo[OperandNo].OperandType ==
677f1bdb2eSDan Gohman              WebAssembly::OPERAND_P2ALIGN &&
687f1bdb2eSDan Gohman          "Load and store instructions should have a p2align operand");
697f1bdb2eSDan Gohman   uint64_t P2Align = Log2_64((*MI.memoperands_begin())->getAlignment());
707f1bdb2eSDan Gohman 
717f1bdb2eSDan Gohman   // WebAssembly does not currently support supernatural alignment.
727f1bdb2eSDan Gohman   P2Align = std::min(
737f1bdb2eSDan Gohman       P2Align, uint64_t(WebAssembly::GetDefaultP2Align(MI.getOpcode())));
747f1bdb2eSDan Gohman 
757f1bdb2eSDan Gohman   MI.getOperand(OperandNo).setImm(P2Align);
767f1bdb2eSDan Gohman }
777f1bdb2eSDan Gohman 
78bb372243SDan Gohman bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) {
79d34e60caSNicola Zaghen   LLVM_DEBUG({
80bb372243SDan Gohman     dbgs() << "********** Set p2align Operands **********\n"
81bb372243SDan Gohman            << "********** Function: " << MF.getName() << '\n';
82bb372243SDan Gohman   });
83bb372243SDan Gohman 
84bb372243SDan Gohman   bool Changed = false;
85bb372243SDan Gohman 
86bb372243SDan Gohman   for (auto &MBB : MF) {
87bb372243SDan Gohman     for (auto &MI : MBB) {
88bb372243SDan Gohman       switch (MI.getOpcode()) {
89bb372243SDan Gohman       case WebAssembly::LOAD_I32:
90bb372243SDan Gohman       case WebAssembly::LOAD_I64:
91bb372243SDan Gohman       case WebAssembly::LOAD_F32:
92bb372243SDan Gohman       case WebAssembly::LOAD_F64:
93bb372243SDan Gohman       case WebAssembly::LOAD8_S_I32:
94bb372243SDan Gohman       case WebAssembly::LOAD8_U_I32:
95bb372243SDan Gohman       case WebAssembly::LOAD16_S_I32:
96bb372243SDan Gohman       case WebAssembly::LOAD16_U_I32:
97bb372243SDan Gohman       case WebAssembly::LOAD8_S_I64:
98bb372243SDan Gohman       case WebAssembly::LOAD8_U_I64:
99bb372243SDan Gohman       case WebAssembly::LOAD16_S_I64:
100bb372243SDan Gohman       case WebAssembly::LOAD16_U_I64:
101bb372243SDan Gohman       case WebAssembly::LOAD32_S_I64:
102bb372243SDan Gohman       case WebAssembly::LOAD32_U_I64:
10318ba1928SDerek Schuff       case WebAssembly::ATOMIC_LOAD_I32:
104885dc592SDerek Schuff       case WebAssembly::ATOMIC_LOAD8_U_I32:
105885dc592SDerek Schuff       case WebAssembly::ATOMIC_LOAD16_U_I32:
106885dc592SDerek Schuff       case WebAssembly::ATOMIC_LOAD_I64:
107885dc592SDerek Schuff       case WebAssembly::ATOMIC_LOAD8_U_I64:
108885dc592SDerek Schuff       case WebAssembly::ATOMIC_LOAD16_U_I64:
109885dc592SDerek Schuff       case WebAssembly::ATOMIC_LOAD32_U_I64:
110*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_ADD_I32:
111*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_ADD_I64:
112*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_SUB_I32:
113*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_SUB_I64:
114*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_AND_I32:
115*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_AND_I64:
116*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_OR_I32:
117*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_OR_I64:
118*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_XOR_I32:
119*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_XOR_I64:
120*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_XCHG_I32:
121*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW8_U_XCHG_I64:
122*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_ADD_I32:
123*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_ADD_I64:
124*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_SUB_I32:
125*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_SUB_I64:
126*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_AND_I32:
127*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_AND_I64:
128*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_OR_I32:
129*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_OR_I64:
130*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_XOR_I32:
131*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_XOR_I64:
132*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_XCHG_I32:
133*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW16_U_XCHG_I64:
134*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_ADD_I32:
135*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW32_U_ADD_I64:
136*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_SUB_I32:
137*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW32_U_SUB_I64:
138*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_AND_I32:
139*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW32_U_AND_I64:
140*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_OR_I32:
141*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW32_U_OR_I64:
142*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_XOR_I32:
143*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW32_U_XOR_I64:
144*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_XCHG_I32:
145*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW32_U_XCHG_I64:
146*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_ADD_I64:
147*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_SUB_I64:
148*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_AND_I64:
149*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_OR_I64:
150*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_XOR_I64:
151*fed7382eSHeejin Ahn       case WebAssembly::ATOMIC_RMW_XCHG_I64:
1527f1bdb2eSDan Gohman         RewriteP2Align(MI, WebAssembly::LoadP2AlignOperandNo);
1537f1bdb2eSDan Gohman         break;
154bb372243SDan Gohman       case WebAssembly::STORE_I32:
155bb372243SDan Gohman       case WebAssembly::STORE_I64:
156bb372243SDan Gohman       case WebAssembly::STORE_F32:
157bb372243SDan Gohman       case WebAssembly::STORE_F64:
158bb372243SDan Gohman       case WebAssembly::STORE8_I32:
159bb372243SDan Gohman       case WebAssembly::STORE16_I32:
160bb372243SDan Gohman       case WebAssembly::STORE8_I64:
161bb372243SDan Gohman       case WebAssembly::STORE16_I64:
1627f1bdb2eSDan Gohman       case WebAssembly::STORE32_I64:
163402b4908SHeejin Ahn       case WebAssembly::ATOMIC_STORE_I32:
164402b4908SHeejin Ahn       case WebAssembly::ATOMIC_STORE8_I32:
165402b4908SHeejin Ahn       case WebAssembly::ATOMIC_STORE16_I32:
166402b4908SHeejin Ahn       case WebAssembly::ATOMIC_STORE_I64:
167402b4908SHeejin Ahn       case WebAssembly::ATOMIC_STORE8_I64:
168402b4908SHeejin Ahn       case WebAssembly::ATOMIC_STORE16_I64:
169402b4908SHeejin Ahn       case WebAssembly::ATOMIC_STORE32_I64:
1707f1bdb2eSDan Gohman         RewriteP2Align(MI, WebAssembly::StoreP2AlignOperandNo);
171bb372243SDan Gohman         break;
172bb372243SDan Gohman       default:
173bb372243SDan Gohman         break;
174bb372243SDan Gohman       }
175bb372243SDan Gohman     }
176bb372243SDan Gohman   }
177bb372243SDan Gohman 
178bb372243SDan Gohman   return Changed;
179bb372243SDan Gohman }
180