LLVM  14.0.0git
WebAssemblyInstrInfo.cpp
Go to the documentation of this file.
1 //===-- WebAssemblyInstrInfo.cpp - WebAssembly Instruction Information ----===//
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 contains the WebAssembly implementation of the
11 /// TargetInstrInfo class.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "WebAssemblyInstrInfo.h"
18 #include "WebAssembly.h"
20 #include "WebAssemblySubtarget.h"
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "wasm-instr-info"
28 
29 #define GET_INSTRINFO_CTOR_DTOR
30 #include "WebAssemblyGenInstrInfo.inc"
31 
32 // defines WebAssembly::getNamedOperandIdx
33 #define GET_INSTRINFO_NAMED_OPS
34 #include "WebAssemblyGenInstrInfo.inc"
35 
37  : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN,
38  WebAssembly::ADJCALLSTACKUP,
39  WebAssembly::CATCHRET),
40  RI(STI.getTargetTriple()) {}
41 
43  const MachineInstr &MI, AAResults *AA) const {
44  switch (MI.getOpcode()) {
45  case WebAssembly::CONST_I32:
46  case WebAssembly::CONST_I64:
47  case WebAssembly::CONST_F32:
48  case WebAssembly::CONST_F64:
49  // isReallyTriviallyReMaterializableGeneric misses these because of the
50  // ARGUMENTS implicit def, so we manualy override it here.
51  return true;
52  default:
53  return false;
54  }
55 }
56 
59  const DebugLoc &DL, MCRegister DestReg,
60  MCRegister SrcReg, bool KillSrc) const {
61  // This method is called by post-RA expansion, which expects only pregs to
62  // exist. However we need to handle both here.
63  auto &MRI = MBB.getParent()->getRegInfo();
64  const TargetRegisterClass *RC =
66  ? MRI.getRegClass(DestReg)
68 
69  unsigned CopyOpcode;
70  if (RC == &WebAssembly::I32RegClass)
71  CopyOpcode = WebAssembly::COPY_I32;
72  else if (RC == &WebAssembly::I64RegClass)
73  CopyOpcode = WebAssembly::COPY_I64;
74  else if (RC == &WebAssembly::F32RegClass)
75  CopyOpcode = WebAssembly::COPY_F32;
76  else if (RC == &WebAssembly::F64RegClass)
77  CopyOpcode = WebAssembly::COPY_F64;
78  else if (RC == &WebAssembly::V128RegClass)
79  CopyOpcode = WebAssembly::COPY_V128;
80  else if (RC == &WebAssembly::FUNCREFRegClass)
81  CopyOpcode = WebAssembly::COPY_FUNCREF;
82  else if (RC == &WebAssembly::EXTERNREFRegClass)
83  CopyOpcode = WebAssembly::COPY_EXTERNREF;
84  else
85  llvm_unreachable("Unexpected register class");
86 
87  BuildMI(MBB, I, DL, get(CopyOpcode), DestReg)
88  .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
89 }
90 
92  MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const {
93  // If the operands are stackified, we can't reorder them.
95  *MI.getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>();
96  if (MFI.isVRegStackified(MI.getOperand(OpIdx1).getReg()) ||
97  MFI.isVRegStackified(MI.getOperand(OpIdx2).getReg()))
98  return nullptr;
99 
100  // Otherwise use the default implementation.
101  return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
102 }
103 
104 // Branch analysis.
106  MachineBasicBlock *&TBB,
107  MachineBasicBlock *&FBB,
109  bool /*AllowModify*/) const {
110  const auto &MFI = *MBB.getParent()->getInfo<WebAssemblyFunctionInfo>();
111  // WebAssembly has control flow that doesn't have explicit branches or direct
112  // fallthrough (e.g. try/catch), which can't be modeled by analyzeBranch. It
113  // is created after CFGStackify.
114  if (MFI.isCFGStackified())
115  return true;
116 
117  bool HaveCond = false;
118  for (MachineInstr &MI : MBB.terminators()) {
119  switch (MI.getOpcode()) {
120  default:
121  // Unhandled instruction; bail out.
122  return true;
123  case WebAssembly::BR_IF:
124  if (HaveCond)
125  return true;
126  Cond.push_back(MachineOperand::CreateImm(true));
127  Cond.push_back(MI.getOperand(1));
128  TBB = MI.getOperand(0).getMBB();
129  HaveCond = true;
130  break;
131  case WebAssembly::BR_UNLESS:
132  if (HaveCond)
133  return true;
134  Cond.push_back(MachineOperand::CreateImm(false));
135  Cond.push_back(MI.getOperand(1));
136  TBB = MI.getOperand(0).getMBB();
137  HaveCond = true;
138  break;
139  case WebAssembly::BR:
140  if (!HaveCond)
141  TBB = MI.getOperand(0).getMBB();
142  else
143  FBB = MI.getOperand(0).getMBB();
144  break;
145  }
146  if (MI.isBarrier())
147  break;
148  }
149 
150  return false;
151 }
152 
154  int *BytesRemoved) const {
155  assert(!BytesRemoved && "code size not handled");
156 
158  unsigned Count = 0;
159 
160  while (I != MBB.instr_begin()) {
161  --I;
162  if (I->isDebugInstr())
163  continue;
164  if (!I->isTerminator())
165  break;
166  // Remove the branch.
167  I->eraseFromParent();
168  I = MBB.instr_end();
169  ++Count;
170  }
171 
172  return Count;
173 }
174 
177  ArrayRef<MachineOperand> Cond, const DebugLoc &DL, int *BytesAdded) const {
178  assert(!BytesAdded && "code size not handled");
179 
180  if (Cond.empty()) {
181  if (!TBB)
182  return 0;
183 
185  return 1;
186  }
187 
188  assert(Cond.size() == 2 && "Expected a flag and a successor block");
189 
190  if (Cond[0].getImm())
191  BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addMBB(TBB).add(Cond[1]);
192  else
193  BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)).addMBB(TBB).add(Cond[1]);
194  if (!FBB)
195  return 1;
196 
198  return 2;
199 }
200 
203  assert(Cond.size() == 2 && "Expected a flag and a condition expression");
204  Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm());
205  return false;
206 }
207 
210  static const std::pair<int, const char *> TargetIndices[] = {
211  {WebAssembly::TI_LOCAL, "wasm-local"},
212  {WebAssembly::TI_GLOBAL_FIXED, "wasm-global-fixed"},
213  {WebAssembly::TI_OPERAND_STACK, "wasm-operand-stack"},
214  {WebAssembly::TI_GLOBAL_RELOC, "wasm-global-reloc"},
215  {WebAssembly::TI_LOCAL_INDIRECT, "wasm-local-indirect"}};
216  return makeArrayRef(TargetIndices);
217 }
218 
219 const MachineOperand &
222 }
llvm::WebAssemblyInstrInfo::WebAssemblyInstrInfo
WebAssemblyInstrInfo(const WebAssemblySubtarget &STI)
Definition: WebAssemblyInstrInfo.cpp:36
llvm::WebAssembly::getCalleeOp
const MachineOperand & getCalleeOp(const MachineInstr &MI)
Returns the operand number of a callee, assuming the argument is a call instruction.
Definition: WebAssemblyUtilities.cpp:83
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm::WebAssemblyInstrInfo::insertBranch
unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const override
Definition: WebAssemblyInstrInfo.cpp:175
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
WebAssembly.h
llvm::MachineInstrBuilder::add
const MachineInstrBuilder & add(const MachineOperand &MO) const
Definition: MachineInstrBuilder.h:224
llvm::WebAssemblyInstrInfo::commuteInstructionImpl
MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const override
Definition: WebAssemblyInstrInfo.cpp:91
llvm::WebAssemblyInstrInfo::copyPhysReg
void copyPhysReg(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI, const DebugLoc &DL, MCRegister DestReg, MCRegister SrcReg, bool KillSrc) const override
Definition: WebAssemblyInstrInfo.cpp:57
llvm::MachineRegisterInfo::getTargetRegisterInfo
const TargetRegisterInfo * getTargetRegisterInfo() const
Definition: MachineRegisterInfo.h:153
llvm::MachineBasicBlock::terminators
iterator_range< iterator > terminators()
Definition: MachineBasicBlock.h:288
MachineRegisterInfo.h
llvm::MachineFunction::getRegInfo
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Definition: MachineFunction.h:636
llvm::TargetInstrInfo::commuteInstructionImpl
virtual MachineInstr * commuteInstructionImpl(MachineInstr &MI, bool NewMI, unsigned OpIdx1, unsigned OpIdx2) const
This method commutes the operands of the given machine instruction MI.
Definition: TargetInstrInfo.cpp:167
llvm::MachineInstrBuilder::addMBB
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
Definition: MachineInstrBuilder.h:146
llvm::AAResults
Definition: AliasAnalysis.h:456
llvm::MachineOperand::CreateImm
static MachineOperand CreateImm(int64_t Val)
Definition: MachineOperand.h:773
llvm::MachineFunction::getInfo
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
Definition: MachineFunction.h:724
llvm::ISD::CATCHRET
@ CATCHRET
CATCHRET - Represents a return from a catch block funclet.
Definition: ISDOpcodes.h:999
llvm::TargetRegisterClass
Definition: TargetRegisterInfo.h:46
WebAssemblyInstrInfo.h
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::WebAssemblyInstrInfo::isReallyTriviallyReMaterializable
bool isReallyTriviallyReMaterializable(const MachineInstr &MI, AAResults *AA) const override
Definition: WebAssemblyInstrInfo.cpp:42
llvm::WebAssembly::TI_OPERAND_STACK
@ TI_OPERAND_STACK
Definition: WebAssembly.h:97
llvm::WebAssembly::TI_GLOBAL_RELOC
@ TI_GLOBAL_RELOC
Definition: WebAssembly.h:100
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
WebAssemblyUtilities.h
WebAssemblyMCTargetDesc.h
llvm::MachineRegisterInfo::getRegClass
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
Definition: MachineRegisterInfo.h:634
WebAssemblyGenInstrInfo
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::WebAssemblyInstrInfo::reverseBranchCondition
bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const override
Definition: WebAssemblyInstrInfo.cpp:201
llvm::Register::isVirtualRegister
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:71
llvm::WebAssemblyFunctionInfo::isVRegStackified
bool isVRegStackified(unsigned VReg) const
Definition: WebAssemblyMachineFunctionInfo.h:136
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:225
llvm::WebAssemblyFunctionInfo
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
Definition: WebAssemblyMachineFunctionInfo.h:33
llvm::MachineInstrBuilder::addReg
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Definition: MachineInstrBuilder.h:97
llvm::WebAssemblyInstrInfo::getCalleeOperand
const MachineOperand & getCalleeOperand(const MachineInstr &MI) const override
Definition: WebAssemblyInstrInfo.cpp:220
llvm::MachineBasicBlock::instr_begin
instr_iterator instr_begin()
Definition: MachineBasicBlock.h:252
WebAssemblyMachineFunctionInfo.h
llvm::MachineBasicBlock::instr_end
instr_iterator instr_end()
Definition: MachineBasicBlock.h:254
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
Cond
SmallVector< MachineOperand, 4 > Cond
Definition: BasicBlockSections.cpp:179
llvm::WebAssemblyInstrInfo::analyzeBranch
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify=false) const override
Definition: WebAssemblyInstrInfo.cpp:105
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:136
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::WebAssemblySubtarget
Definition: WebAssemblySubtarget.h:35
llvm::WebAssemblyInstrInfo::getSerializableTargetIndices
ArrayRef< std::pair< int, const char * > > getSerializableTargetIndices() const override
Definition: WebAssemblyInstrInfo.cpp:209
llvm::WebAssembly::TI_GLOBAL_FIXED
@ TI_GLOBAL_FIXED
Definition: WebAssembly.h:95
MRI
unsigned const MachineRegisterInfo * MRI
Definition: AArch64AdvSIMDScalarPass.cpp:105
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::WebAssembly::TI_LOCAL
@ TI_LOCAL
Definition: WebAssembly.h:93
get
Should compile to something r4 addze r3 instead we get
Definition: README.txt:24
llvm::ISD::BR
@ BR
Control flow instructions. These all have token chains.
Definition: ISDOpcodes.h:937
llvm::ilist_iterator
Iterator for intrusive lists based on ilist_node.
Definition: ilist_iterator.h:57
MachineFrameInfo.h
llvm::WebAssembly::TI_LOCAL_INDIRECT
@ TI_LOCAL_INDIRECT
Definition: WebAssembly.h:103
WebAssemblySubtarget.h
llvm::makeArrayRef
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:476
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
llvm::RegState::Kill
@ Kill
The last use of a register.
Definition: MachineInstrBuilder.h:48
MachineMemOperand.h
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::TargetRegisterInfo::getMinimalPhysRegClass
const TargetRegisterClass * getMinimalPhysRegClass(MCRegister Reg, MVT VT=MVT::Other) const
Returns the Register Class of a physical register of the given type, picking the most sub register cl...
Definition: TargetRegisterInfo.cpp:211
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::WebAssemblyInstrInfo::removeBranch
unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const override
Definition: WebAssemblyInstrInfo.cpp:153
llvm::MCRegister
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:23