LLVM 24.0.0git
WebAssemblyLowerBrUnless.cpp
Go to the documentation of this file.
1//===-- WebAssemblyLowerBrUnless.cpp - Lower br_unless --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file lowers br_unless into br_if with an inverted condition.
11///
12/// br_unless is not currently in the spec, but it's very convenient for LLVM
13/// to use. This pass allows LLVM to use it, for now.
14///
15//===----------------------------------------------------------------------===//
16
18#include "WebAssembly.h"
25#include "llvm/IR/Analysis.h"
26#include "llvm/Support/Debug.h"
28using namespace llvm;
29
30#define DEBUG_TYPE "wasm-lower-br_unless"
31
32namespace {
33class WebAssemblyLowerBrUnlessLegacy final : public MachineFunctionPass {
34 StringRef getPassName() const override {
35 return "WebAssembly Lower br_unless";
36 }
37
38 void getAnalysisUsage(AnalysisUsage &AU) const override {
39 AU.setPreservesCFG();
41 }
42
43 bool runOnMachineFunction(MachineFunction &MF) override;
44
45public:
46 static char ID; // Pass identification, replacement for typeid
47 WebAssemblyLowerBrUnlessLegacy() : MachineFunctionPass(ID) {}
48};
49} // end anonymous namespace
50
51char WebAssemblyLowerBrUnlessLegacy::ID = 0;
52INITIALIZE_PASS(WebAssemblyLowerBrUnlessLegacy, DEBUG_TYPE,
53 "Lowers br_unless into inverted br_if", false, false)
54
56 return new WebAssemblyLowerBrUnlessLegacy();
57}
58
60 LLVM_DEBUG(dbgs() << "********** Lowering br_unless **********\n"
61 "********** Function: "
62 << MF.getName() << '\n');
63
64 auto &MFI = *MF.getInfo<WebAssemblyFunctionInfo>();
65 const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
66 auto &MRI = MF.getRegInfo();
67
68 for (auto &MBB : MF) {
70 if (MI.getOpcode() != WebAssembly::BR_UNLESS)
71 continue;
72
73 Register Cond = MI.getOperand(1).getReg();
74 bool Inverted = false;
75
76 // Attempt to invert the condition in place.
77 if (MFI.isVRegStackified(Cond)) {
78 assert(MRI.hasOneDef(Cond));
79 MachineInstr *Def = MRI.getVRegDef(Cond);
80 switch (Def->getOpcode()) {
81 using namespace WebAssembly;
82 case EQ_I32:
83 Def->setDesc(TII.get(NE_I32));
84 Inverted = true;
85 break;
86 case NE_I32:
87 Def->setDesc(TII.get(EQ_I32));
88 Inverted = true;
89 break;
90 case GT_S_I32:
91 Def->setDesc(TII.get(LE_S_I32));
92 Inverted = true;
93 break;
94 case GE_S_I32:
95 Def->setDesc(TII.get(LT_S_I32));
96 Inverted = true;
97 break;
98 case LT_S_I32:
99 Def->setDesc(TII.get(GE_S_I32));
100 Inverted = true;
101 break;
102 case LE_S_I32:
103 Def->setDesc(TII.get(GT_S_I32));
104 Inverted = true;
105 break;
106 case GT_U_I32:
107 Def->setDesc(TII.get(LE_U_I32));
108 Inverted = true;
109 break;
110 case GE_U_I32:
111 Def->setDesc(TII.get(LT_U_I32));
112 Inverted = true;
113 break;
114 case LT_U_I32:
115 Def->setDesc(TII.get(GE_U_I32));
116 Inverted = true;
117 break;
118 case LE_U_I32:
119 Def->setDesc(TII.get(GT_U_I32));
120 Inverted = true;
121 break;
122 case EQ_I64:
123 Def->setDesc(TII.get(NE_I64));
124 Inverted = true;
125 break;
126 case NE_I64:
127 Def->setDesc(TII.get(EQ_I64));
128 Inverted = true;
129 break;
130 case GT_S_I64:
131 Def->setDesc(TII.get(LE_S_I64));
132 Inverted = true;
133 break;
134 case GE_S_I64:
135 Def->setDesc(TII.get(LT_S_I64));
136 Inverted = true;
137 break;
138 case LT_S_I64:
139 Def->setDesc(TII.get(GE_S_I64));
140 Inverted = true;
141 break;
142 case LE_S_I64:
143 Def->setDesc(TII.get(GT_S_I64));
144 Inverted = true;
145 break;
146 case GT_U_I64:
147 Def->setDesc(TII.get(LE_U_I64));
148 Inverted = true;
149 break;
150 case GE_U_I64:
151 Def->setDesc(TII.get(LT_U_I64));
152 Inverted = true;
153 break;
154 case LT_U_I64:
155 Def->setDesc(TII.get(GE_U_I64));
156 Inverted = true;
157 break;
158 case LE_U_I64:
159 Def->setDesc(TII.get(GT_U_I64));
160 Inverted = true;
161 break;
162 case EQ_F32:
163 Def->setDesc(TII.get(NE_F32));
164 Inverted = true;
165 break;
166 case NE_F32:
167 Def->setDesc(TII.get(EQ_F32));
168 Inverted = true;
169 break;
170 case EQ_F64:
171 Def->setDesc(TII.get(NE_F64));
172 Inverted = true;
173 break;
174 case NE_F64:
175 Def->setDesc(TII.get(EQ_F64));
176 Inverted = true;
177 break;
178 case EQZ_I32: {
179 // Invert an eqz by replacing it with its operand.
180 Cond = Def->getOperand(1).getReg();
181 Def->eraseFromParent();
182 Inverted = true;
183 break;
184 }
185 default:
186 break;
187 }
188 }
189
190 // If we weren't able to invert the condition in place. Insert an
191 // instruction to invert it.
192 if (!Inverted) {
193 Register Tmp = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
194 BuildMI(MBB, &MI, MI.getDebugLoc(), TII.get(WebAssembly::EQZ_I32), Tmp)
195 .addReg(Cond);
196 MFI.stackifyVReg(MRI, Tmp);
197 Cond = Tmp;
198 Inverted = true;
199 }
200
201 // The br_unless condition has now been inverted. Insert a br_if and
202 // delete the br_unless.
203 assert(Inverted);
204 BuildMI(MBB, &MI, MI.getDebugLoc(), TII.get(WebAssembly::BR_IF))
205 .add(MI.getOperand(0))
206 .addReg(Cond);
207 MBB.erase(&MI);
208 }
209 }
210
211 return true;
212}
213
214bool WebAssemblyLowerBrUnlessLegacy::runOnMachineFunction(MachineFunction &MF) {
215 return lowerBrUnless(MF);
216}
217
218PreservedAnalyses
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
const SmallVectorImpl< MachineOperand > & Cond
#define LLVM_DEBUG(...)
Definition Debug.h:119
static bool lowerBrUnless(MachineFunction &MF)
This file provides WebAssembly-specific target descriptions.
This file declares WebAssembly-specific per-machine-function information.
This file declares the WebAssembly-specific subclass of TargetSubtarget.
This file contains the entry points for global functions defined in the LLVM WebAssembly back-end.
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class is derived from MachineFunctionInfo and contains private WebAssembly-specific information ...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Pass manager infrastructure for declaring and invalidating analyses.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
FunctionPass * createWebAssemblyLowerBrUnlessLegacyPass()