1 //===-- WebAssemblyInstrInfo.cpp - WebAssembly Instruction Information ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains the WebAssembly implementation of the
12 /// TargetInstrInfo class.
14 //===----------------------------------------------------------------------===//
16 #include "WebAssemblyInstrInfo.h"
17 #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
18 #include "WebAssemblySubtarget.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineMemOperand.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #define DEBUG_TYPE "wasm-instr-info"
27 #define GET_INSTRINFO_CTOR_DTOR
28 #include "WebAssemblyGenInstrInfo.inc"
30 WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI)
31 : RI(STI.getTargetTriple()) {}
33 void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
34 MachineBasicBlock::iterator I,
35 DebugLoc DL, unsigned DestReg,
36 unsigned SrcReg, bool KillSrc) const {
37 const TargetRegisterClass *RC =
38 MBB.getParent()->getRegInfo().getRegClass(SrcReg);
40 unsigned CopyLocalOpcode;
41 if (RC == &WebAssembly::I32RegClass)
42 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I32;
43 else if (RC == &WebAssembly::I64RegClass)
44 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I64;
45 else if (RC == &WebAssembly::F32RegClass)
46 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F32;
47 else if (RC == &WebAssembly::F64RegClass)
48 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F64;
50 llvm_unreachable("Unexpected register class");
52 BuildMI(MBB, I, DL, get(CopyLocalOpcode), DestReg)
53 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
57 bool WebAssemblyInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
58 MachineBasicBlock *&TBB,
59 MachineBasicBlock *&FBB,
60 SmallVectorImpl<MachineOperand> &Cond,
61 bool AllowModify) const {
62 bool HaveCond = false;
63 for (MachineInstr &MI : iterator_range<MachineBasicBlock::instr_iterator>(
64 MBB.getFirstInstrTerminator(), MBB.instr_end())) {
65 switch (MI.getOpcode()) {
67 // Unhandled instruction; bail out.
69 case WebAssembly::BR_IF:
72 Cond.push_back(MI.getOperand(0));
73 TBB = MI.getOperand(1).getMBB();
78 TBB = MI.getOperand(0).getMBB();
80 FBB = MI.getOperand(0).getMBB();
90 unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
91 MachineBasicBlock::instr_iterator I = MBB.instr_end();
94 while (I != MBB.instr_begin()) {
96 if (I->isDebugValue())
98 if (!I->isTerminator())
100 // Remove the branch.
101 I->eraseFromParent();
109 unsigned WebAssemblyInstrInfo::InsertBranch(
110 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
111 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
112 assert(Cond.size() <= 1);
118 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
122 BuildMI(&MBB, DL, get(WebAssembly::BR_IF))
128 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
132 bool WebAssemblyInstrInfo::ReverseBranchCondition(
133 SmallVectorImpl<MachineOperand> &Cond) const {
134 assert(Cond.size() == 1);
136 // TODO: Add branch reversal here... And re-enable MachineBlockPlacementID