LLVM  4.0.0
WebAssemblySetP2AlignOperands.cpp
Go to the documentation of this file.
1 //=- WebAssemblySetP2AlignOperands.cpp - Set alignments on loads and stores -=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief This file sets the p2align operands on load and store instructions.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "WebAssembly.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 
50  return new WebAssemblySetP2AlignOperands();
51 }
52 
53 static void RewriteP2Align(MachineInstr &MI, unsigned OperandNo) {
54  assert(MI.getOperand(OperandNo).getImm() == 0 &&
55  "ISel should set p2align operands to 0");
56  assert(MI.hasOneMemOperand() &&
57  "Load and store instructions have exactly one mem operand");
58  assert((*MI.memoperands_begin())->getSize() ==
59  (UINT64_C(1)
61  "Default p2align value should be natural");
62  assert(MI.getDesc().OpInfo[OperandNo].OperandType ==
64  "Load and store instructions should have a p2align operand");
65  uint64_t P2Align = Log2_64((*MI.memoperands_begin())->getAlignment());
66 
67  // WebAssembly does not currently support supernatural alignment.
68  P2Align = std::min(
69  P2Align, uint64_t(WebAssembly::GetDefaultP2Align(MI.getOpcode())));
70 
71  MI.getOperand(OperandNo).setImm(P2Align);
72 }
73 
74 bool WebAssemblySetP2AlignOperands::runOnMachineFunction(MachineFunction &MF) {
75  DEBUG({
76  dbgs() << "********** Set p2align Operands **********\n"
77  << "********** Function: " << MF.getName() << '\n';
78  });
79 
80  bool Changed = false;
81 
82  for (auto &MBB : MF) {
83  for (auto &MI : MBB) {
84  switch (MI.getOpcode()) {
85  case WebAssembly::LOAD_I32:
86  case WebAssembly::LOAD_I64:
87  case WebAssembly::LOAD_F32:
88  case WebAssembly::LOAD_F64:
89  case WebAssembly::LOAD8_S_I32:
90  case WebAssembly::LOAD8_U_I32:
91  case WebAssembly::LOAD16_S_I32:
92  case WebAssembly::LOAD16_U_I32:
93  case WebAssembly::LOAD8_S_I64:
94  case WebAssembly::LOAD8_U_I64:
95  case WebAssembly::LOAD16_S_I64:
96  case WebAssembly::LOAD16_U_I64:
97  case WebAssembly::LOAD32_S_I64:
98  case WebAssembly::LOAD32_U_I64:
100  break;
101  case WebAssembly::STORE_I32:
102  case WebAssembly::STORE_I64:
103  case WebAssembly::STORE_F32:
104  case WebAssembly::STORE_F64:
105  case WebAssembly::STORE8_I32:
106  case WebAssembly::STORE16_I32:
107  case WebAssembly::STORE8_I64:
108  case WebAssembly::STORE16_I64:
109  case WebAssembly::STORE32_I64:
111  break;
112  default:
113  break;
114  }
115  }
116  }
117 
118  return Changed;
119 }
static const unsigned LoadP2AlignOperandNo
The operand number of the load or store p2align in load/store instructions.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
char & MachineDominatorsID
MachineDominators - This pass is a machine dominators analysis pass.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
const MCInstrDesc & getDesc() const
Returns the target instruction descriptor of this MachineInstr.
Definition: MachineInstr.h:270
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end...
static const unsigned StoreP2AlignOperandNo
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
uint8_t OperandType
Information about the type of the operand.
Definition: MCInstrDesc.h:82
static unsigned getAlignment(GlobalVariable *GV)
MachineBasicBlock * MBB
AnalysisUsage & addPreservedID(const void *ID)
int64_t getImm() const
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with etc Experimental Use value profile to guide fuzzing Number of simultaneous worker processes to run the jobs If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:273
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:279
This file provides WebAssembly-specific target descriptions.
FunctionPass * createWebAssemblySetP2AlignOperands()
Represent the analysis usage information of a pass.
void setImm(int64_t immVal)
bool hasOneMemOperand() const
Return true if this instruction has exactly one MachineMemOperand.
Definition: MachineInstr.h:373
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
unsigned GetDefaultP2Align(unsigned Opcode)
Return the default p2align value for a load or store with the given opcode.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:276
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:52
p2align immediate for load and store address alignment.
This file declares WebAssembly-specific per-machine-function information.
static void RewriteP2Align(MachineInstr &MI, unsigned OperandNo)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCOperandInfo * OpInfo
Definition: MCInstrDesc.h:174
#define DEBUG(X)
Definition: Debug.h:100
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
unsigned Log2_64(uint64_t Value)
Log2_64 - This function returns the floor log base 2 of the specified value, -1 if the value is zero...
Definition: MathExtras.h:519
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.
Definition: MachineInstr.h:358