blob: 5fb97e38939ad52578da07701665acb1a2a20011 [file] [log] [blame]
Dan Gohmanf0b165a2015-12-05 03:03:35 +00001//===-- WebAssemblyLowerBrUnless.cpp - Lower br_unless --------------------===//
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 Prantl5f8f34e42018-05-01 15:54:18 +000011/// This file lowers br_unless into br_if with an inverted condition.
Dan Gohmanf0b165a2015-12-05 03:03:35 +000012///
13/// br_unless is not currently in the spec, but it's very convenient for LLVM
14/// to use. This pass allows LLVM to use it, for now.
15///
16//===----------------------------------------------------------------------===//
17
Dan Gohman83947562016-01-20 05:54:22 +000018#include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000019#include "WebAssembly.h"
Dan Gohmanf0b165a2015-12-05 03:03:35 +000020#include "WebAssemblyMachineFunctionInfo.h"
21#include "WebAssemblySubtarget.h"
Dan Gohmanf0b165a2015-12-05 03:03:35 +000022#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
26using namespace llvm;
27
28#define DEBUG_TYPE "wasm-lower-br_unless"
29
30namespace {
31class WebAssemblyLowerBrUnless final : public MachineFunctionPass {
Mehdi Amini117296c2016-10-01 02:56:57 +000032 StringRef getPassName() const override {
Dan Gohmanf0b165a2015-12-05 03:03:35 +000033 return "WebAssembly Lower br_unless";
34 }
35
36 void getAnalysisUsage(AnalysisUsage &AU) const override {
37 AU.setPreservesCFG();
38 MachineFunctionPass::getAnalysisUsage(AU);
39 }
40
41 bool runOnMachineFunction(MachineFunction &MF) override;
42
43public:
44 static char ID; // Pass identification, replacement for typeid
45 WebAssemblyLowerBrUnless() : MachineFunctionPass(ID) {}
46};
47} // end anonymous namespace
48
49char WebAssemblyLowerBrUnless::ID = 0;
Jacob Gravelle40926452018-03-30 20:36:58 +000050INITIALIZE_PASS(WebAssemblyLowerBrUnless, DEBUG_TYPE,
51 "Lowers br_unless into inverted br_if", false, false)
52
Dan Gohmanf0b165a2015-12-05 03:03:35 +000053FunctionPass *llvm::createWebAssemblyLowerBrUnless() {
54 return new WebAssemblyLowerBrUnless();
55}
56
57bool WebAssemblyLowerBrUnless::runOnMachineFunction(MachineFunction &MF) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000058 LLVM_DEBUG(dbgs() << "********** Lowering br_unless **********\n"
59 "********** Function: "
60 << MF.getName() << '\n');
Dan Gohmanf0b165a2015-12-05 03:03:35 +000061
62 auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
63 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
64 auto &MRI = MF.getRegInfo();
65
66 for (auto &MBB : MF) {
Dan Gohman83947562016-01-20 05:54:22 +000067 for (auto MII = MBB.begin(); MII != MBB.end();) {
Dan Gohmanf0b165a2015-12-05 03:03:35 +000068 MachineInstr *MI = &*MII++;
69 if (MI->getOpcode() != WebAssembly::BR_UNLESS)
70 continue;
71
Dan Gohman06b49582016-02-08 21:50:13 +000072 unsigned Cond = MI->getOperand(1).getReg();
Dan Gohmanf0b165a2015-12-05 03:03:35 +000073 bool Inverted = false;
74
75 // Attempt to invert the condition in place.
76 if (MFI.isVRegStackified(Cond)) {
77 assert(MRI.hasOneDef(Cond));
78 MachineInstr *Def = MRI.getVRegDef(Cond);
79 switch (Def->getOpcode()) {
Dan Gohman83947562016-01-20 05:54:22 +000080 using namespace WebAssembly;
Dan Gohmanf0b165a2015-12-05 03:03:35 +000081 case EQ_I32: Def->setDesc(TII.get(NE_I32)); Inverted = true; break;
82 case NE_I32: Def->setDesc(TII.get(EQ_I32)); Inverted = true; break;
83 case GT_S_I32: Def->setDesc(TII.get(LE_S_I32)); Inverted = true; break;
84 case GE_S_I32: Def->setDesc(TII.get(LT_S_I32)); Inverted = true; break;
85 case LT_S_I32: Def->setDesc(TII.get(GE_S_I32)); Inverted = true; break;
86 case LE_S_I32: Def->setDesc(TII.get(GT_S_I32)); Inverted = true; break;
87 case GT_U_I32: Def->setDesc(TII.get(LE_U_I32)); Inverted = true; break;
88 case GE_U_I32: Def->setDesc(TII.get(LT_U_I32)); Inverted = true; break;
89 case LT_U_I32: Def->setDesc(TII.get(GE_U_I32)); Inverted = true; break;
90 case LE_U_I32: Def->setDesc(TII.get(GT_U_I32)); Inverted = true; break;
91 case EQ_I64: Def->setDesc(TII.get(NE_I64)); Inverted = true; break;
92 case NE_I64: Def->setDesc(TII.get(EQ_I64)); Inverted = true; break;
93 case GT_S_I64: Def->setDesc(TII.get(LE_S_I64)); Inverted = true; break;
94 case GE_S_I64: Def->setDesc(TII.get(LT_S_I64)); Inverted = true; break;
95 case LT_S_I64: Def->setDesc(TII.get(GE_S_I64)); Inverted = true; break;
96 case LE_S_I64: Def->setDesc(TII.get(GT_S_I64)); Inverted = true; break;
97 case GT_U_I64: Def->setDesc(TII.get(LE_U_I64)); Inverted = true; break;
98 case GE_U_I64: Def->setDesc(TII.get(LT_U_I64)); Inverted = true; break;
99 case LT_U_I64: Def->setDesc(TII.get(GE_U_I64)); Inverted = true; break;
100 case LE_U_I64: Def->setDesc(TII.get(GT_U_I64)); Inverted = true; break;
101 case EQ_F32: Def->setDesc(TII.get(NE_F32)); Inverted = true; break;
102 case NE_F32: Def->setDesc(TII.get(EQ_F32)); Inverted = true; break;
103 case EQ_F64: Def->setDesc(TII.get(NE_F64)); Inverted = true; break;
104 case NE_F64: Def->setDesc(TII.get(EQ_F64)); Inverted = true; break;
Dan Gohman580c1022017-11-29 20:20:11 +0000105 case EQZ_I32: {
106 // Invert an eqz by replacing it with its operand.
107 Cond = Def->getOperand(1).getReg();
108 Def->eraseFromParent();
109 Inverted = true;
110 break;
111 }
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000112 default: break;
113 }
114 }
115
116 // If we weren't able to invert the condition in place. Insert an
Dan Gohmane0405332016-10-03 22:43:53 +0000117 // instruction to invert it.
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000118 if (!Inverted) {
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000119 unsigned Tmp = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
Dan Gohman804749c2016-05-16 18:59:34 +0000120 BuildMI(MBB, MI, MI->getDebugLoc(), TII.get(WebAssembly::EQZ_I32), Tmp)
121 .addReg(Cond);
Dan Gohman4fc4e422016-10-24 19:49:43 +0000122 MFI.stackifyVReg(Tmp);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000123 Cond = Tmp;
124 Inverted = true;
125 }
126
127 // The br_unless condition has now been inverted. Insert a br_if and
128 // delete the br_unless.
129 assert(Inverted);
130 BuildMI(MBB, MI, MI->getDebugLoc(), TII.get(WebAssembly::BR_IF))
Diana Picus116bbab2017-01-13 09:58:52 +0000131 .add(MI->getOperand(0))
Dan Gohman06b49582016-02-08 21:50:13 +0000132 .addReg(Cond);
Dan Gohmanf0b165a2015-12-05 03:03:35 +0000133 MBB.erase(MI);
134 }
135 }
136
137 return true;
138}