LLVM 24.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"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/IR/Analysis.h"
23#include "llvm/Support/Debug.h"
25using namespace llvm;
26
27#define DEBUG_TYPE "wasm-set-p2align-operands"
28
29namespace {
30class WebAssemblySetP2AlignOperandsLegacy final : public MachineFunctionPass {
31public:
32 static char ID; // Pass identification, replacement for typeid
33 WebAssemblySetP2AlignOperandsLegacy() : MachineFunctionPass(ID) {}
34
35 StringRef getPassName() const override {
36 return "WebAssembly Set p2align Operands";
37 }
38
39 void getAnalysisUsage(AnalysisUsage &AU) const override {
40 AU.setPreservesCFG();
44 }
45
46 bool runOnMachineFunction(MachineFunction &MF) override;
47};
48} // end anonymous namespace
49
50char WebAssemblySetP2AlignOperandsLegacy::ID = 0;
51INITIALIZE_PASS(WebAssemblySetP2AlignOperandsLegacy, DEBUG_TYPE,
52 "Set the p2align operands for WebAssembly loads and stores",
53 false, false)
54
56 return new WebAssemblySetP2AlignOperandsLegacy();
57}
58
59static void rewriteP2Align(MachineInstr &MI, unsigned OperandNo) {
60 assert(MI.getOperand(OperandNo).getImm() == 0 &&
61 "ISel should set p2align operands to 0");
62 assert(MI.hasOneMemOperand() &&
63 "Load and store instructions have exactly one mem operand");
64 assert((*MI.memoperands_begin())->getSize() ==
65 (UINT64_C(1) << WebAssembly::GetDefaultP2Align(MI.getOpcode())) &&
66 "Default p2align value should be natural");
67 assert(MI.getDesc().operands()[OperandNo].OperandType ==
69 "Load and store instructions should have a p2align operand");
70 uint64_t P2Align = Log2((*MI.memoperands_begin())->getAlign());
71
72 // WebAssembly does not currently support supernatural alignment.
73 P2Align = std::min(P2Align,
75
76 MI.getOperand(OperandNo).setImm(P2Align);
77}
78
81 dbgs() << "********** Set p2align Operands **********\n"
82 << "********** Function: " << MF.getName() << '\n';
83 });
84
85 bool Changed = false;
86
87 for (auto &MBB : MF) {
88 for (auto &MI : MBB) {
89 int16_t P2AlignOpNum = WebAssembly::getNamedOperandIdx(
90 MI.getOpcode(), WebAssembly::OpName::p2align);
91 if (P2AlignOpNum != -1) {
92 rewriteP2Align(MI, P2AlignOpNum);
93 Changed = true;
94 }
95 }
96 }
97
98 return Changed;
99}
100
101bool WebAssemblySetP2AlignOperandsLegacy::runOnMachineFunction(
102 MachineFunction &MF) {
103 return setP2AlignOperands(MF);
104}
105
106PreservedAnalyses
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
#define LLVM_DEBUG(...)
Definition Debug.h:119
This file contains the WebAssembly implementation of the TargetInstrInfo class.
This file provides WebAssembly-specific target descriptions.
static void rewriteP2Align(MachineInstr &MI, unsigned OperandNo)
static bool setP2AlignOperands(MachineFunction &MF)
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.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
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.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Representation of each machine instruction.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Changed
Pass manager infrastructure for declaring and invalidating analyses.
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.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
FunctionPass * createWebAssemblySetP2AlignOperandsLegacyPass()
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197