1bb372243SDan Gohman //=- WebAssemblySetP2AlignOperands.cpp - Set alignments on loads and stores -=// 2bb372243SDan Gohman // 3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bb372243SDan Gohman // 7bb372243SDan Gohman //===----------------------------------------------------------------------===// 8bb372243SDan Gohman /// 9bb372243SDan Gohman /// \file 105f8f34e4SAdrian Prantl /// This file sets the p2align operands on load and store instructions. 11bb372243SDan Gohman /// 12bb372243SDan Gohman //===----------------------------------------------------------------------===// 13bb372243SDan Gohman 14bb372243SDan Gohman #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" 156bda14b3SChandler Carruth #include "WebAssembly.h" 16bb372243SDan Gohman #include "WebAssemblyMachineFunctionInfo.h" 17bb372243SDan Gohman #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 18bb372243SDan Gohman #include "llvm/CodeGen/MachineMemOperand.h" 19bb372243SDan Gohman #include "llvm/CodeGen/Passes.h" 20bb372243SDan Gohman #include "llvm/Support/Debug.h" 21bb372243SDan Gohman #include "llvm/Support/raw_ostream.h" 22bb372243SDan Gohman using namespace llvm; 23bb372243SDan Gohman 24bb372243SDan Gohman #define DEBUG_TYPE "wasm-set-p2align-operands" 25bb372243SDan Gohman 26bb372243SDan Gohman namespace { 27bb372243SDan Gohman class WebAssemblySetP2AlignOperands final : public MachineFunctionPass { 28bb372243SDan Gohman public: 29bb372243SDan Gohman static char ID; // Pass identification, replacement for typeid 30bb372243SDan Gohman WebAssemblySetP2AlignOperands() : MachineFunctionPass(ID) {} 31bb372243SDan Gohman 32117296c0SMehdi Amini StringRef getPassName() const override { 33bb372243SDan Gohman return "WebAssembly Set p2align Operands"; 34bb372243SDan Gohman } 35bb372243SDan Gohman 36bb372243SDan Gohman void getAnalysisUsage(AnalysisUsage &AU) const override { 37bb372243SDan Gohman AU.setPreservesCFG(); 38bb372243SDan Gohman AU.addPreserved<MachineBlockFrequencyInfo>(); 39bb372243SDan Gohman AU.addPreservedID(MachineDominatorsID); 40bb372243SDan Gohman MachineFunctionPass::getAnalysisUsage(AU); 41bb372243SDan Gohman } 42bb372243SDan Gohman 43bb372243SDan Gohman bool runOnMachineFunction(MachineFunction &MF) override; 44bb372243SDan Gohman }; 45bb372243SDan Gohman } // end anonymous namespace 46bb372243SDan Gohman 47bb372243SDan Gohman char WebAssemblySetP2AlignOperands::ID = 0; 4840926451SJacob Gravelle INITIALIZE_PASS(WebAssemblySetP2AlignOperands, DEBUG_TYPE, 4940926451SJacob Gravelle "Set the p2align operands for WebAssembly loads and stores", 5040926451SJacob Gravelle false, false) 5140926451SJacob Gravelle 52bb372243SDan Gohman FunctionPass *llvm::createWebAssemblySetP2AlignOperands() { 53bb372243SDan Gohman return new WebAssemblySetP2AlignOperands(); 54bb372243SDan Gohman } 55bb372243SDan Gohman 567f1bdb2eSDan Gohman static void RewriteP2Align(MachineInstr &MI, unsigned OperandNo) { 577f1bdb2eSDan Gohman assert(MI.getOperand(OperandNo).getImm() == 0 && 587f1bdb2eSDan Gohman "ISel should set p2align operands to 0"); 597f1bdb2eSDan Gohman assert(MI.hasOneMemOperand() && 607f1bdb2eSDan Gohman "Load and store instructions have exactly one mem operand"); 617f1bdb2eSDan Gohman assert((*MI.memoperands_begin())->getSize() == 62f208f631SHeejin Ahn (UINT64_C(1) << WebAssembly::GetDefaultP2Align(MI.getOpcode())) && 637f1bdb2eSDan Gohman "Default p2align value should be natural"); 647f1bdb2eSDan Gohman assert(MI.getDesc().OpInfo[OperandNo].OperandType == 657f1bdb2eSDan Gohman WebAssembly::OPERAND_P2ALIGN && 667f1bdb2eSDan Gohman "Load and store instructions should have a p2align operand"); 677f1bdb2eSDan Gohman uint64_t P2Align = Log2_64((*MI.memoperands_begin())->getAlignment()); 687f1bdb2eSDan Gohman 697f1bdb2eSDan Gohman // WebAssembly does not currently support supernatural alignment. 70f208f631SHeejin Ahn P2Align = std::min(P2Align, 71f208f631SHeejin Ahn uint64_t(WebAssembly::GetDefaultP2Align(MI.getOpcode()))); 727f1bdb2eSDan Gohman 737f1bdb2eSDan Gohman MI.getOperand(OperandNo).setImm(P2Align); 747f1bdb2eSDan Gohman } 757f1bdb2eSDan Gohman 76bb372243SDan Gohman bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) { 77d34e60caSNicola Zaghen LLVM_DEBUG({ 78bb372243SDan Gohman dbgs() << "********** Set p2align Operands **********\n" 79bb372243SDan Gohman << "********** Function: " << MF.getName() << '\n'; 80bb372243SDan Gohman }); 81bb372243SDan Gohman 82bb372243SDan Gohman bool Changed = false; 83bb372243SDan Gohman 84bb372243SDan Gohman for (auto &MBB : MF) { 85bb372243SDan Gohman for (auto &MI : MBB) { 86bb372243SDan Gohman switch (MI.getOpcode()) { 87bb372243SDan Gohman case WebAssembly::LOAD_I32: 88bb372243SDan Gohman case WebAssembly::LOAD_I64: 89bb372243SDan Gohman case WebAssembly::LOAD_F32: 90bb372243SDan Gohman case WebAssembly::LOAD_F64: 91b61232eaSThomas Lively case WebAssembly::LOAD_v16i8: 92b61232eaSThomas Lively case WebAssembly::LOAD_v8i16: 93b61232eaSThomas Lively case WebAssembly::LOAD_v4i32: 94b61232eaSThomas Lively case WebAssembly::LOAD_v2i64: 95b61232eaSThomas Lively case WebAssembly::LOAD_v4f32: 96b61232eaSThomas Lively case WebAssembly::LOAD_v2f64: 97bb372243SDan Gohman case WebAssembly::LOAD8_S_I32: 98bb372243SDan Gohman case WebAssembly::LOAD8_U_I32: 99bb372243SDan Gohman case WebAssembly::LOAD16_S_I32: 100bb372243SDan Gohman case WebAssembly::LOAD16_U_I32: 101bb372243SDan Gohman case WebAssembly::LOAD8_S_I64: 102bb372243SDan Gohman case WebAssembly::LOAD8_U_I64: 103bb372243SDan Gohman case WebAssembly::LOAD16_S_I64: 104bb372243SDan Gohman case WebAssembly::LOAD16_U_I64: 105bb372243SDan Gohman case WebAssembly::LOAD32_S_I64: 106bb372243SDan Gohman case WebAssembly::LOAD32_U_I64: 10718ba1928SDerek Schuff case WebAssembly::ATOMIC_LOAD_I32: 108885dc592SDerek Schuff case WebAssembly::ATOMIC_LOAD8_U_I32: 109885dc592SDerek Schuff case WebAssembly::ATOMIC_LOAD16_U_I32: 110885dc592SDerek Schuff case WebAssembly::ATOMIC_LOAD_I64: 111885dc592SDerek Schuff case WebAssembly::ATOMIC_LOAD8_U_I64: 112885dc592SDerek Schuff case WebAssembly::ATOMIC_LOAD16_U_I64: 113885dc592SDerek Schuff case WebAssembly::ATOMIC_LOAD32_U_I64: 114fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_ADD_I32: 115fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_ADD_I64: 116fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_SUB_I32: 117fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_SUB_I64: 118fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_AND_I32: 119fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_AND_I64: 120fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_OR_I32: 121fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_OR_I64: 122fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_XOR_I32: 123fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_XOR_I64: 124fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_XCHG_I32: 125fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_XCHG_I64: 126b3724b71SHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_CMPXCHG_I32: 127b3724b71SHeejin Ahn case WebAssembly::ATOMIC_RMW8_U_CMPXCHG_I64: 128fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_ADD_I32: 129fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_ADD_I64: 130fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_SUB_I32: 131fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_SUB_I64: 132fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_AND_I32: 133fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_AND_I64: 134fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_OR_I32: 135fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_OR_I64: 136fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_XOR_I32: 137fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_XOR_I64: 138fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_XCHG_I32: 139fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_XCHG_I64: 140b3724b71SHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_CMPXCHG_I32: 141b3724b71SHeejin Ahn case WebAssembly::ATOMIC_RMW16_U_CMPXCHG_I64: 142fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_ADD_I32: 143fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW32_U_ADD_I64: 144fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_SUB_I32: 145fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW32_U_SUB_I64: 146fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_AND_I32: 147fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW32_U_AND_I64: 148fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_OR_I32: 149fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW32_U_OR_I64: 150fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_XOR_I32: 151fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW32_U_XOR_I64: 152fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_XCHG_I32: 153fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW32_U_XCHG_I64: 154b3724b71SHeejin Ahn case WebAssembly::ATOMIC_RMW_CMPXCHG_I32: 155b3724b71SHeejin Ahn case WebAssembly::ATOMIC_RMW32_U_CMPXCHG_I64: 156fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_ADD_I64: 157fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_SUB_I64: 158fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_AND_I64: 159fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_OR_I64: 160fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_XOR_I64: 161fed7382eSHeejin Ahn case WebAssembly::ATOMIC_RMW_XCHG_I64: 162b3724b71SHeejin Ahn case WebAssembly::ATOMIC_RMW_CMPXCHG_I64: 1634128cb0bSHeejin Ahn case WebAssembly::ATOMIC_NOTIFY: 1644128cb0bSHeejin Ahn case WebAssembly::ATOMIC_WAIT_I32: 1654128cb0bSHeejin Ahn case WebAssembly::ATOMIC_WAIT_I64: 1667f1bdb2eSDan Gohman RewriteP2Align(MI, WebAssembly::LoadP2AlignOperandNo); 1677f1bdb2eSDan Gohman break; 168bb372243SDan Gohman case WebAssembly::STORE_I32: 169bb372243SDan Gohman case WebAssembly::STORE_I64: 170bb372243SDan Gohman case WebAssembly::STORE_F32: 171bb372243SDan Gohman case WebAssembly::STORE_F64: 172b61232eaSThomas Lively case WebAssembly::STORE_v16i8: 173b61232eaSThomas Lively case WebAssembly::STORE_v8i16: 174b61232eaSThomas Lively case WebAssembly::STORE_v4i32: 175b61232eaSThomas Lively case WebAssembly::STORE_v2i64: 176b61232eaSThomas Lively case WebAssembly::STORE_v4f32: 177b61232eaSThomas Lively case WebAssembly::STORE_v2f64: 178bb372243SDan Gohman case WebAssembly::STORE8_I32: 179bb372243SDan Gohman case WebAssembly::STORE16_I32: 180bb372243SDan Gohman case WebAssembly::STORE8_I64: 181bb372243SDan Gohman case WebAssembly::STORE16_I64: 1827f1bdb2eSDan Gohman case WebAssembly::STORE32_I64: 183402b4908SHeejin Ahn case WebAssembly::ATOMIC_STORE_I32: 184402b4908SHeejin Ahn case WebAssembly::ATOMIC_STORE8_I32: 185402b4908SHeejin Ahn case WebAssembly::ATOMIC_STORE16_I32: 186402b4908SHeejin Ahn case WebAssembly::ATOMIC_STORE_I64: 187402b4908SHeejin Ahn case WebAssembly::ATOMIC_STORE8_I64: 188402b4908SHeejin Ahn case WebAssembly::ATOMIC_STORE16_I64: 189402b4908SHeejin Ahn case WebAssembly::ATOMIC_STORE32_I64: 1907f1bdb2eSDan Gohman RewriteP2Align(MI, WebAssembly::StoreP2AlignOperandNo); 191bb372243SDan Gohman break; 192bb372243SDan Gohman default: 193bb372243SDan Gohman break; 194bb372243SDan Gohman } 195bb372243SDan Gohman } 196bb372243SDan Gohman } 197bb372243SDan Gohman 198bb372243SDan Gohman return Changed; 199bb372243SDan Gohman } 200