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 BuildMI(MBB, I, DL, get(WebAssembly::COPY), DestReg)
38 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
42 bool WebAssemblyInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
43 MachineBasicBlock *&TBB,
44 MachineBasicBlock *&FBB,
45 SmallVectorImpl<MachineOperand> &Cond,
46 bool AllowModify) const {
47 bool HaveCond = false;
48 for (MachineInstr &MI : iterator_range<MachineBasicBlock::instr_iterator>(
49 MBB.getFirstInstrTerminator(), MBB.instr_end())) {
50 switch (MI.getOpcode()) {
52 // Unhandled instruction; bail out.
54 case WebAssembly::BR_IF_:
57 Cond.push_back(MI.getOperand(1));
58 TBB = MI.getOperand(0).getMBB();
63 TBB = MI.getOperand(0).getMBB();
65 FBB = MI.getOperand(0).getMBB();
75 unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
76 MachineBasicBlock::instr_iterator I = MBB.instr_end();
79 while (I != MBB.instr_begin()) {
81 if (I->isDebugValue())
83 if (!I->isTerminator())
94 unsigned WebAssemblyInstrInfo::InsertBranch(
95 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
96 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
97 assert(Cond.size() <= 1);
103 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
107 BuildMI(&MBB, DL, get(WebAssembly::BR_IF_))
109 .addOperand(Cond[0]);
113 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
117 bool WebAssemblyInstrInfo::ReverseBranchCondition(
118 SmallVectorImpl<MachineOperand> &Cond) const {
119 assert(Cond.size() == 1);
121 // TODO: Add branch reversal here... And re-enable MachineBlockPlacementID