1 //=- WebAssemblySetP2AlignOperands.cpp - Set alignments on loads and stores -=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file sets the p2align operands on load and store instructions.
13 //===----------------------------------------------------------------------===//
15 #include "WebAssembly.h"
16 #include "MCTargetDesc/WebAssemblyMCTargetDesc.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"
25 #define DEBUG_TYPE "wasm-set-p2align-operands"
28 class WebAssemblySetP2AlignOperands final : public MachineFunctionPass {
30 static char ID; // Pass identification, replacement for typeid
31 WebAssemblySetP2AlignOperands() : MachineFunctionPass(ID) {}
33 const char *getPassName() const override {
34 return "WebAssembly Set p2align Operands";
37 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.addPreserved<MachineBlockFrequencyInfo>();
40 AU.addPreservedID(MachineDominatorsID);
41 MachineFunctionPass::getAnalysisUsage(AU);
44 bool runOnMachineFunction(MachineFunction &MF) override;
46 } // end anonymous namespace
48 char WebAssemblySetP2AlignOperands::ID = 0;
49 FunctionPass *llvm::createWebAssemblySetP2AlignOperands() {
50 return new WebAssemblySetP2AlignOperands();
53 bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) {
55 dbgs() << "********** Set p2align Operands **********\n"
56 << "********** Function: " << MF.getName() << '\n';
61 for (auto &MBB : MF) {
62 for (auto &MI : MBB) {
63 switch (MI.getOpcode()) {
64 case WebAssembly::LOAD_I32:
65 case WebAssembly::LOAD_I64:
66 case WebAssembly::LOAD_F32:
67 case WebAssembly::LOAD_F64:
68 case WebAssembly::LOAD8_S_I32:
69 case WebAssembly::LOAD8_U_I32:
70 case WebAssembly::LOAD16_S_I32:
71 case WebAssembly::LOAD16_U_I32:
72 case WebAssembly::LOAD8_S_I64:
73 case WebAssembly::LOAD8_U_I64:
74 case WebAssembly::LOAD16_S_I64:
75 case WebAssembly::LOAD16_U_I64:
76 case WebAssembly::LOAD32_S_I64:
77 case WebAssembly::LOAD32_U_I64:
78 case WebAssembly::STORE_I32:
79 case WebAssembly::STORE_I64:
80 case WebAssembly::STORE_F32:
81 case WebAssembly::STORE_F64:
82 case WebAssembly::STORE8_I32:
83 case WebAssembly::STORE16_I32:
84 case WebAssembly::STORE8_I64:
85 case WebAssembly::STORE16_I64:
86 case WebAssembly::STORE32_I64: {
87 assert(MI.getOperand(3).getImm() == 0 &&
88 "ISel should set p2align operands to 0");
89 assert(MI.hasOneMemOperand() &&
90 "Load and store instructions have exactly one mem operand");
91 assert((*MI.memoperands_begin())->getSize() ==
93 << WebAssembly::GetDefaultP2Align(MI.getOpcode())) &&
94 "Default p2align value should be natural");
95 assert(MI.getDesc().OpInfo[3].OperandType ==
96 WebAssembly::OPERAND_P2ALIGN &&
97 "Load and store instructions should have a p2align operand");
98 uint64_t P2Align = Log2_64((*MI.memoperands_begin())->getAlignment());
100 // WebAssembly does not currently support supernatural alignment.
102 P2Align, uint64_t(WebAssembly::GetDefaultP2Align(MI.getOpcode())));
104 MI.getOperand(3).setImm(P2Align);