blob: 2f1d5eb5287f2b83580b1d866c43e1257ff9884e [file] [log] [blame]
Dan Gohman10e730a2015-06-29 23:51:55 +00001//===-- WebAssemblyInstrInfo.cpp - WebAssembly Instruction Information ----===//
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 contains the WebAssembly implementation of the
12/// TargetInstrInfo class.
13///
14//===----------------------------------------------------------------------===//
15
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"
23using namespace llvm;
24
25#define DEBUG_TYPE "wasm-instr-info"
26
JF Bastienb9073fb2015-07-22 21:28:15 +000027#define GET_INSTRINFO_CTOR_DTOR
28#include "WebAssemblyGenInstrInfo.inc"
29
Dan Gohman10e730a2015-06-29 23:51:55 +000030WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI)
31 : RI(STI.getTargetTriple()) {}
Dan Gohman4f52e002015-09-09 00:52:47 +000032
33void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
34 MachineBasicBlock::iterator I,
35 DebugLoc DL, unsigned DestReg,
36 unsigned SrcReg, bool KillSrc) const {
Dan Gohman4ba48162015-11-18 16:12:01 +000037 const TargetRegisterClass *RC =
38 MBB.getParent()->getRegInfo().getRegClass(SrcReg);
39
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000040 unsigned CopyLocalOpcode;
Dan Gohman4ba48162015-11-18 16:12:01 +000041 if (RC == &WebAssembly::I32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000042 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I32;
Dan Gohman4ba48162015-11-18 16:12:01 +000043 else if (RC == &WebAssembly::I64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000044 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I64;
Dan Gohman4ba48162015-11-18 16:12:01 +000045 else if (RC == &WebAssembly::F32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000046 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F32;
Dan Gohman4ba48162015-11-18 16:12:01 +000047 else if (RC == &WebAssembly::F64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000048 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F64;
Dan Gohman4ba48162015-11-18 16:12:01 +000049 else
50 llvm_unreachable("Unexpected register class");
51
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000052 BuildMI(MBB, I, DL, get(CopyLocalOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000053 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
54}
Dan Gohman950a13c2015-09-16 16:51:30 +000055
56// Branch analysis.
57bool 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()) {
66 default:
67 // Unhandled instruction; bail out.
68 return true;
Dan Gohman231244c2015-11-13 00:46:31 +000069 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +000070 if (HaveCond)
71 return true;
Derek Schuff4ed47782015-11-16 21:04:51 +000072 Cond.push_back(MI.getOperand(0));
73 TBB = MI.getOperand(1).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +000074 HaveCond = true;
75 break;
76 case WebAssembly::BR:
77 if (!HaveCond)
78 TBB = MI.getOperand(0).getMBB();
79 else
80 FBB = MI.getOperand(0).getMBB();
81 break;
82 }
83 if (MI.isBarrier())
84 break;
85 }
86
87 return false;
88}
89
90unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
91 MachineBasicBlock::instr_iterator I = MBB.instr_end();
92 unsigned Count = 0;
93
94 while (I != MBB.instr_begin()) {
95 --I;
96 if (I->isDebugValue())
97 continue;
98 if (!I->isTerminator())
99 break;
100 // Remove the branch.
101 I->eraseFromParent();
102 I = MBB.instr_end();
103 ++Count;
104 }
105
106 return Count;
107}
108
109unsigned WebAssemblyInstrInfo::InsertBranch(
110 MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB,
111 ArrayRef<MachineOperand> Cond, DebugLoc DL) const {
112 assert(Cond.size() <= 1);
113
114 if (Cond.empty()) {
115 if (!TBB)
116 return 0;
117
118 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
119 return 1;
120 }
121
Dan Gohman231244c2015-11-13 00:46:31 +0000122 BuildMI(&MBB, DL, get(WebAssembly::BR_IF))
Derek Schuff4ed47782015-11-16 21:04:51 +0000123 .addOperand(Cond[0])
124 .addMBB(TBB);
Dan Gohman950a13c2015-09-16 16:51:30 +0000125 if (!FBB)
126 return 1;
127
128 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
129 return 2;
130}
131
132bool WebAssemblyInstrInfo::ReverseBranchCondition(
133 SmallVectorImpl<MachineOperand> &Cond) const {
134 assert(Cond.size() == 1);
135
136 // TODO: Add branch reversal here... And re-enable MachineBlockPlacementID
137 // when we do.
138
139 return true;
140}