LLVM 20.0.0git
WebAssemblySetP2AlignOperands.cpp
Go to the documentation of this file.
1//=- WebAssemblySetP2AlignOperands.cpp - Set alignments on loads and stores -=//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file sets the p2align operands on load and store instructions.
11///
12//===----------------------------------------------------------------------===//
13
15#include "WebAssembly.h"
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/Support/Debug.h"
22using namespace llvm;
23
24#define DEBUG_TYPE "wasm-set-p2align-operands"
25
26namespace {
27class WebAssemblySetP2AlignOperands final : public MachineFunctionPass {
28public:
29 static char ID; // Pass identification, replacement for typeid
30 WebAssemblySetP2AlignOperands() : MachineFunctionPass(ID) {}
31
32 StringRef getPassName() const override {
33 return "WebAssembly Set p2align Operands";
34 }
35
36 void getAnalysisUsage(AnalysisUsage &AU) const override {
37 AU.setPreservesCFG();
41 }
42
43 bool runOnMachineFunction(MachineFunction &MF) override;
44};
45} // end anonymous namespace
46
47char WebAssemblySetP2AlignOperands::ID = 0;
48INITIALIZE_PASS(WebAssemblySetP2AlignOperands, DEBUG_TYPE,
49 "Set the p2align operands for WebAssembly loads and stores",
50 false, false)
51
53 return new WebAssemblySetP2AlignOperands();
54}
55
56static void rewriteP2Align(MachineInstr &MI, unsigned OperandNo) {
57 assert(MI.getOperand(OperandNo).getImm() == 0 &&
58 "ISel should set p2align operands to 0");
59 assert(MI.hasOneMemOperand() &&
60 "Load and store instructions have exactly one mem operand");
61 assert((*MI.memoperands_begin())->getSize() ==
62 (UINT64_C(1) << WebAssembly::GetDefaultP2Align(MI.getOpcode())) &&
63 "Default p2align value should be natural");
64 assert(MI.getDesc().operands()[OperandNo].OperandType ==
66 "Load and store instructions should have a p2align operand");
67 uint64_t P2Align = Log2((*MI.memoperands_begin())->getAlign());
68
69 // WebAssembly does not currently support supernatural alignment.
70 P2Align = std::min(P2Align,
72
73 MI.getOperand(OperandNo).setImm(P2Align);
74}
75
76bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) {
78 dbgs() << "********** Set p2align Operands **********\n"
79 << "********** Function: " << MF.getName() << '\n';
80 });
81
82 bool Changed = false;
83
84 for (auto &MBB : MF) {
85 for (auto &MI : MBB) {
86 int16_t P2AlignOpNum = WebAssembly::getNamedOperandIdx(
87 MI.getOpcode(), WebAssembly::OpName::p2align);
88 if (P2AlignOpNum != -1) {
89 rewriteP2Align(MI, P2AlignOpNum);
90 Changed = true;
91 }
92 }
93 }
94
95 return Changed;
96}
MachineBasicBlock & MBB
#define LLVM_DEBUG(...)
Definition: Debug.h:106
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains the WebAssembly implementation of the TargetInstrInfo class.
This file provides WebAssembly-specific target descriptions.
static void rewriteP2Align(MachineInstr &MI, unsigned OperandNo)
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
AnalysisUsage & addPreservedID(const void *ID)
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Representation of each machine instruction.
Definition: MachineInstr.h:69
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
unsigned GetDefaultP2Align(unsigned Opc)
@ OPERAND_P2ALIGN
p2align immediate for load and store address alignment.
int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIndex)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
FunctionPass * createWebAssemblySetP2AlignOperands()
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208