Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 1 | //===-- 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 |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame^] | 11 | /// This file contains the WebAssembly implementation of the |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 12 | /// TargetInstrInfo class. |
| 13 | /// |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #include "WebAssemblyInstrInfo.h" |
| 17 | #include "MCTargetDesc/WebAssemblyMCTargetDesc.h" |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 18 | #include "WebAssemblyMachineFunctionInfo.h" |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 19 | #include "WebAssemblySubtarget.h" |
| 20 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 21 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 22 | #include "llvm/CodeGen/MachineMemOperand.h" |
| 23 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| 24 | using namespace llvm; |
| 25 | |
| 26 | #define DEBUG_TYPE "wasm-instr-info" |
| 27 | |
JF Bastien | b9073fb | 2015-07-22 21:28:15 +0000 | [diff] [blame] | 28 | #define GET_INSTRINFO_CTOR_DTOR |
| 29 | #include "WebAssemblyGenInstrInfo.inc" |
| 30 | |
Dan Gohman | 10e730a | 2015-06-29 23:51:55 +0000 | [diff] [blame] | 31 | WebAssemblyInstrInfo::WebAssemblyInstrInfo(const WebAssemblySubtarget &STI) |
Dan Gohman | 35bfb24 | 2015-12-04 23:22:35 +0000 | [diff] [blame] | 32 | : WebAssemblyGenInstrInfo(WebAssembly::ADJCALLSTACKDOWN, |
| 33 | WebAssembly::ADJCALLSTACKUP), |
| 34 | RI(STI.getTargetTriple()) {} |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 35 | |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 36 | bool WebAssemblyInstrInfo::isReallyTriviallyReMaterializable( |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 37 | const MachineInstr &MI, AliasAnalysis *AA) const { |
| 38 | switch (MI.getOpcode()) { |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 39 | case WebAssembly::CONST_I32: |
| 40 | case WebAssembly::CONST_I64: |
| 41 | case WebAssembly::CONST_F32: |
| 42 | case WebAssembly::CONST_F64: |
| 43 | // isReallyTriviallyReMaterializableGeneric misses these because of the |
| 44 | // ARGUMENTS implicit def, so we manualy override it here. |
| 45 | return true; |
| 46 | default: |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 51 | void WebAssemblyInstrInfo::copyPhysReg(MachineBasicBlock &MBB, |
| 52 | MachineBasicBlock::iterator I, |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 53 | const DebugLoc &DL, unsigned DestReg, |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 54 | unsigned SrcReg, bool KillSrc) const { |
Derek Schuff | 8bb5f29 | 2015-12-16 23:21:30 +0000 | [diff] [blame] | 55 | // This method is called by post-RA expansion, which expects only pregs to |
| 56 | // exist. However we need to handle both here. |
| 57 | auto &MRI = MBB.getParent()->getRegInfo(); |
Dan Gohman | b6fd39a | 2016-01-19 16:59:23 +0000 | [diff] [blame] | 58 | const TargetRegisterClass *RC = |
| 59 | TargetRegisterInfo::isVirtualRegister(DestReg) |
| 60 | ? MRI.getRegClass(DestReg) |
Derek Schuff | 6ea637a | 2016-01-29 18:37:49 +0000 | [diff] [blame] | 61 | : MRI.getTargetRegisterInfo()->getMinimalPhysRegClass(DestReg); |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 62 | |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 63 | unsigned CopyOpcode; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 64 | if (RC == &WebAssembly::I32RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 65 | CopyOpcode = WebAssembly::COPY_I32; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 66 | else if (RC == &WebAssembly::I64RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 67 | CopyOpcode = WebAssembly::COPY_I64; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 68 | else if (RC == &WebAssembly::F32RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 69 | CopyOpcode = WebAssembly::COPY_F32; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 70 | else if (RC == &WebAssembly::F64RegClass) |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 71 | CopyOpcode = WebAssembly::COPY_F64; |
Dan Gohman | 4ba4816 | 2015-11-18 16:12:01 +0000 | [diff] [blame] | 72 | else |
| 73 | llvm_unreachable("Unexpected register class"); |
| 74 | |
Dan Gohman | 4fc4e42 | 2016-10-24 19:49:43 +0000 | [diff] [blame] | 75 | BuildMI(MBB, I, DL, get(CopyOpcode), DestReg) |
Dan Gohman | 4f52e00 | 2015-09-09 00:52:47 +0000 | [diff] [blame] | 76 | .addReg(SrcReg, KillSrc ? RegState::Kill : 0); |
| 77 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 78 | |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 79 | MachineInstr * |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 80 | WebAssemblyInstrInfo::commuteInstructionImpl(MachineInstr &MI, bool NewMI, |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 81 | unsigned OpIdx1, |
| 82 | unsigned OpIdx2) const { |
| 83 | // If the operands are stackified, we can't reorder them. |
| 84 | WebAssemblyFunctionInfo &MFI = |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 85 | *MI.getParent()->getParent()->getInfo<WebAssemblyFunctionInfo>(); |
| 86 | if (MFI.isVRegStackified(MI.getOperand(OpIdx1).getReg()) || |
| 87 | MFI.isVRegStackified(MI.getOperand(OpIdx2).getReg())) |
Dan Gohman | adf2817 | 2016-01-28 01:22:44 +0000 | [diff] [blame] | 88 | return nullptr; |
| 89 | |
| 90 | // Otherwise use the default implementation. |
| 91 | return TargetInstrInfo::commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2); |
| 92 | } |
| 93 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 94 | // Branch analysis. |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 95 | bool WebAssemblyInstrInfo::analyzeBranch(MachineBasicBlock &MBB, |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 96 | MachineBasicBlock *&TBB, |
| 97 | MachineBasicBlock *&FBB, |
| 98 | SmallVectorImpl<MachineOperand> &Cond, |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 99 | bool /*AllowModify*/) const { |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 100 | bool HaveCond = false; |
Dan Gohman | d544e0c | 2015-12-21 17:22:02 +0000 | [diff] [blame] | 101 | for (MachineInstr &MI : MBB.terminators()) { |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 102 | switch (MI.getOpcode()) { |
| 103 | default: |
| 104 | // Unhandled instruction; bail out. |
| 105 | return true; |
Dan Gohman | 231244c | 2015-11-13 00:46:31 +0000 | [diff] [blame] | 106 | case WebAssembly::BR_IF: |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 107 | if (HaveCond) |
| 108 | return true; |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 109 | // If we're running after CFGStackify, we can't optimize further. |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 110 | if (!MI.getOperand(0).isMBB()) |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 111 | return true; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 112 | Cond.push_back(MachineOperand::CreateImm(true)); |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 113 | Cond.push_back(MI.getOperand(1)); |
| 114 | TBB = MI.getOperand(0).getMBB(); |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 115 | HaveCond = true; |
| 116 | break; |
| 117 | case WebAssembly::BR_UNLESS: |
| 118 | if (HaveCond) |
| 119 | return true; |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 120 | // If we're running after CFGStackify, we can't optimize further. |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 121 | if (!MI.getOperand(0).isMBB()) |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 122 | return true; |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 123 | Cond.push_back(MachineOperand::CreateImm(false)); |
Dan Gohman | 06b4958 | 2016-02-08 21:50:13 +0000 | [diff] [blame] | 124 | Cond.push_back(MI.getOperand(1)); |
| 125 | TBB = MI.getOperand(0).getMBB(); |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 126 | HaveCond = true; |
| 127 | break; |
| 128 | case WebAssembly::BR: |
Dan Gohman | 1d68e80f | 2016-01-12 19:14:46 +0000 | [diff] [blame] | 129 | // If we're running after CFGStackify, we can't optimize further. |
| 130 | if (!MI.getOperand(0).isMBB()) |
| 131 | return true; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 132 | if (!HaveCond) |
| 133 | TBB = MI.getOperand(0).getMBB(); |
| 134 | else |
| 135 | FBB = MI.getOperand(0).getMBB(); |
| 136 | break; |
| 137 | } |
| 138 | if (MI.isBarrier()) |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | return false; |
| 143 | } |
| 144 | |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 145 | unsigned WebAssemblyInstrInfo::removeBranch(MachineBasicBlock &MBB, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 146 | int *BytesRemoved) const { |
| 147 | assert(!BytesRemoved && "code size not handled"); |
| 148 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 149 | MachineBasicBlock::instr_iterator I = MBB.instr_end(); |
| 150 | unsigned Count = 0; |
| 151 | |
| 152 | while (I != MBB.instr_begin()) { |
| 153 | --I; |
| 154 | if (I->isDebugValue()) |
| 155 | continue; |
| 156 | if (!I->isTerminator()) |
| 157 | break; |
| 158 | // Remove the branch. |
| 159 | I->eraseFromParent(); |
| 160 | I = MBB.instr_end(); |
| 161 | ++Count; |
| 162 | } |
| 163 | |
| 164 | return Count; |
| 165 | } |
| 166 | |
Matt Arsenault | e8e0f5c | 2016-09-14 17:24:15 +0000 | [diff] [blame] | 167 | unsigned WebAssemblyInstrInfo::insertBranch(MachineBasicBlock &MBB, |
Dan Gohman | 7a6b982 | 2015-11-29 22:32:02 +0000 | [diff] [blame] | 168 | MachineBasicBlock *TBB, |
| 169 | MachineBasicBlock *FBB, |
| 170 | ArrayRef<MachineOperand> Cond, |
Matt Arsenault | a2b036e | 2016-09-14 17:23:48 +0000 | [diff] [blame] | 171 | const DebugLoc &DL, |
| 172 | int *BytesAdded) const { |
| 173 | assert(!BytesAdded && "code size not handled"); |
| 174 | |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 175 | if (Cond.empty()) { |
| 176 | if (!TBB) |
| 177 | return 0; |
| 178 | |
| 179 | BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(TBB); |
| 180 | return 1; |
| 181 | } |
| 182 | |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 183 | assert(Cond.size() == 2 && "Expected a flag and a successor block"); |
| 184 | |
| 185 | if (Cond[0].getImm()) { |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 186 | BuildMI(&MBB, DL, get(WebAssembly::BR_IF)).addMBB(TBB).add(Cond[1]); |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 187 | } else { |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 188 | BuildMI(&MBB, DL, get(WebAssembly::BR_UNLESS)).addMBB(TBB).add(Cond[1]); |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 189 | } |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 190 | if (!FBB) |
| 191 | return 1; |
| 192 | |
| 193 | BuildMI(&MBB, DL, get(WebAssembly::BR)).addMBB(FBB); |
| 194 | return 2; |
| 195 | } |
| 196 | |
Matt Arsenault | 1b9fc8e | 2016-09-14 20:43:16 +0000 | [diff] [blame] | 197 | bool WebAssemblyInstrInfo::reverseBranchCondition( |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 198 | SmallVectorImpl<MachineOperand> &Cond) const { |
Dan Gohman | f0b165a | 2015-12-05 03:03:35 +0000 | [diff] [blame] | 199 | assert(Cond.size() == 2 && "Expected a flag and a successor block"); |
| 200 | Cond.front() = MachineOperand::CreateImm(!Cond.front().getImm()); |
| 201 | return false; |
Dan Gohman | 950a13c | 2015-09-16 16:51:30 +0000 | [diff] [blame] | 202 | } |