LLVM  10.0.0svn
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"
16 #include "WebAssemblyInstrInfo.h"
20 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/Support/Debug.h"
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "wasm-set-p2align-operands"
26 
27 namespace {
28 class WebAssemblySetP2AlignOperands final : public MachineFunctionPass {
29 public:
30  static char ID; // Pass identification, replacement for typeid
31  WebAssemblySetP2AlignOperands() : MachineFunctionPass(ID) {}
32 
33  StringRef getPassName() const override {
34  return "WebAssembly Set p2align Operands";
35  }
36 
37  void getAnalysisUsage(AnalysisUsage &AU) const override {
38  AU.setPreservesCFG();
42  }
43 
44  bool runOnMachineFunction(MachineFunction &MF) override;
45 };
46 } // end anonymous namespace
47 
49 INITIALIZE_PASS(WebAssemblySetP2AlignOperands, DEBUG_TYPE,
50  "Set the p2align operands for WebAssembly loads and stores",
51  false, false)
52 
54  return new WebAssemblySetP2AlignOperands();
55 }
56 
57 static void rewriteP2Align(MachineInstr &MI, unsigned OperandNo) {
58  assert(MI.getOperand(OperandNo).getImm() == 0 &&
59  "ISel should set p2align operands to 0");
60  assert(MI.hasOneMemOperand() &&
61  "Load and store instructions have exactly one mem operand");
62  assert((*MI.memoperands_begin())->getSize() ==
63  (UINT64_C(1) << WebAssembly::GetDefaultP2Align(MI.getOpcode())) &&
64  "Default p2align value should be natural");
65  assert(MI.getDesc().OpInfo[OperandNo].OperandType ==
67  "Load and store instructions should have a p2align operand");
68  uint64_t P2Align = Log2_64((*MI.memoperands_begin())->getAlignment());
69 
70  // WebAssembly does not currently support supernatural alignment.
71  P2Align = std::min(P2Align,
73 
74  MI.getOperand(OperandNo).setImm(P2Align);
75 }
76 
77 bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) {
78  LLVM_DEBUG({
79  dbgs() << "********** Set p2align Operands **********\n"
80  << "********** Function: " << MF.getName() << '\n';
81  });
82 
83  bool Changed = false;
84 
85  for (auto &MBB : MF) {
86  for (auto &MI : MBB) {
87  int16_t P2AlignOpNum = WebAssembly::getNamedOperandIdx(
88  MI.getOpcode(), WebAssembly::OpName::p2align);
89  if (P2AlignOpNum != -1) {
90  rewriteP2Align(MI, P2AlignOpNum);
91  Changed = true;
92  }
93  }
94  }
95 
96  return Changed;
97 }
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
unsigned GetDefaultP2Align(unsigned Opc)
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
static uint32_t getAlignment(const MCSectionCOFF &Sec)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:411
uint8_t OperandType
Information about the type of the operand.
Definition: MCInstrDesc.h:78
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:408
AnalysisUsage & addPreservedID(const void *ID)
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
This file provides WebAssembly-specific target descriptions.
Represent the analysis usage information of a pass.
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
Definition: MachineInstr.h:551
void setImm(int64_t immVal)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:284
FunctionPass * createWebAssemblySetP2AlignOperands()
INITIALIZE_PASS(WebAssemblySetP2AlignOperands, DEBUG_TYPE, "Set the p2align operands for WebAssembly loads and stores", false, false) FunctionPass *llvm
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:536
This file contains the WebAssembly implementation of the TargetInstrInfo class.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:301
int64_t getImm() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
Representation of each machine instruction.
Definition: MachineInstr.h:64
p2align immediate for load and store address alignment.
This file declares WebAssembly-specific per-machine-function information.
int16_t getNamedOperandIdx(uint16_t Opcode, uint16_t NamedIndex)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCOperandInfo * OpInfo
Definition: MCInstrDesc.h:175
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static void rewriteP2Align(MachineInstr &MI, unsigned OperandNo)
#define LLVM_DEBUG(X)
Definition: Debug.h:122
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:416
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:544