blob: bd06bc396dcdd039a45ce16de287dc17ecbc8dfa [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)
Dan Gohman35bfb242015-12-04 23:22:35 +000031 : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN,
32 WebAssembly::ADJCALLSTACKUP),
33 RI(STI.getTargetTriple()) {}
Dan Gohman4f52e002015-09-09 00:52:47 +000034
35void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
36 MachineBasicBlock::iterator I,
37 DebugLoc DL, unsigned DestReg,
38 unsigned SrcReg, bool KillSrc) const {
Dan Gohman4ba48162015-11-18 16:12:01 +000039 const TargetRegisterClass *RC =
40 MBB.getParent()->getRegInfo().getRegClass(SrcReg);
41
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000042 unsigned CopyLocalOpcode;
Dan Gohman4ba48162015-11-18 16:12:01 +000043 if (RC == &WebAssembly::I32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000044 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I32;
Dan Gohman4ba48162015-11-18 16:12:01 +000045 else if (RC == &WebAssembly::I64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000046 CopyLocalOpcode = WebAssembly::COPY_LOCAL_I64;
Dan Gohman4ba48162015-11-18 16:12:01 +000047 else if (RC == &WebAssembly::F32RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000048 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F32;
Dan Gohman4ba48162015-11-18 16:12:01 +000049 else if (RC == &WebAssembly::F64RegClass)
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000050 CopyLocalOpcode = WebAssembly::COPY_LOCAL_F64;
Dan Gohman4ba48162015-11-18 16:12:01 +000051 else
52 llvm_unreachable("Unexpected register class");
53
Dan Gohmanaa0a4bd2015-11-23 19:30:43 +000054 BuildMI(MBB, I, DL, get(CopyLocalOpcode), DestReg)
Dan Gohman4f52e002015-09-09 00:52:47 +000055 .addReg(SrcReg, KillSrc ? RegState::Kill : 0);
56}
Dan Gohman950a13c2015-09-16 16:51:30 +000057
58// Branch analysis.
59bool WebAssemblyInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,
60 MachineBasicBlock *&TBB,
61 MachineBasicBlock *&FBB,
62 SmallVectorImpl<MachineOperand> &Cond,
Dan Gohman7a6b9822015-11-29 22:32:02 +000063 bool /*AllowModify*/) const {
Dan Gohman950a13c2015-09-16 16:51:30 +000064 bool HaveCond = false;
65 for (MachineInstr &MI : iterator_range<MachineBasicBlock::instr_iterator>(
66 MBB.getFirstInstrTerminator(), MBB.instr_end())) {
67 switch (MI.getOpcode()) {
68 default:
69 // Unhandled instruction; bail out.
70 return true;
Dan Gohman231244c2015-11-13 00:46:31 +000071 case WebAssembly::BR_IF:
Dan Gohman950a13c2015-09-16 16:51:30 +000072 if (HaveCond)
73 return true;
Derek Schuff4ed47782015-11-16 21:04:51 +000074 Cond.push_back(MI.getOperand(0));
75 TBB = MI.getOperand(1).getMBB();
Dan Gohman950a13c2015-09-16 16:51:30 +000076 HaveCond = true;
77 break;
78 case WebAssembly::BR:
79 if (!HaveCond)
80 TBB = MI.getOperand(0).getMBB();
81 else
82 FBB = MI.getOperand(0).getMBB();
83 break;
84 }
85 if (MI.isBarrier())
86 break;
87 }
88
89 return false;
90}
91
92unsigned WebAssemblyInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
93 MachineBasicBlock::instr_iterator I = MBB.instr_end();
94 unsigned Count = 0;
95
96 while (I != MBB.instr_begin()) {
97 --I;
98 if (I->isDebugValue())
99 continue;
100 if (!I->isTerminator())
101 break;
102 // Remove the branch.
103 I->eraseFromParent();
104 I = MBB.instr_end();
105 ++Count;
106 }
107
108 return Count;
109}
110
Dan Gohman7a6b9822015-11-29 22:32:02 +0000111unsigned WebAssemblyInstrInfo::InsertBranch(MachineBasicBlock &MBB,
112 MachineBasicBlock *TBB,
113 MachineBasicBlock *FBB,
114 ArrayRef<MachineOperand> Cond,
115 DebugLoc DL) const {
Dan Gohman950a13c2015-09-16 16:51:30 +0000116 assert(Cond.size() <= 1);
117
118 if (Cond.empty()) {
119 if (!TBB)
120 return 0;
121
122 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB);
123 return 1;
124 }
125
Dan Gohman7a6b9822015-11-29 22:32:02 +0000126 BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addOperand(Cond[0]).addMBB(TBB);
Dan Gohman950a13c2015-09-16 16:51:30 +0000127 if (!FBB)
128 return 1;
129
130 BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB);
131 return 2;
132}
133
134bool WebAssemblyInstrInfo::ReverseBranchCondition(
135 SmallVectorImpl<MachineOperand> &Cond) const {
136 assert(Cond.size() == 1);
137
138 // TODO: Add branch reversal here... And re-enable MachineBlockPlacementID
139 // when we do.
140
141 return true;
142}