1 //=== MicroMipsSizeReduction.cpp - MicroMips size reduction pass --------===//
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
7 //===----------------------------------------------------------------------===//
9 /// This pass is used to reduce the size of instructions where applicable.
11 /// TODO: Implement microMIPS64 support.
12 //===----------------------------------------------------------------------===//
14 #include "MipsInstrInfo.h"
15 #include "MipsSubtarget.h"
16 #include "llvm/ADT/Statistic.h"
17 #include "llvm/CodeGen/MachineFunctionPass.h"
18 #include "llvm/Support/Debug.h"
22 #define DEBUG_TYPE "micromips-reduce-size"
23 #define MICROMIPS_SIZE_REDUCE_NAME "MicroMips instruction size reduce pass"
25 STATISTIC(NumReduced
, "Number of instructions reduced (32-bit to 16-bit ones, "
26 "or two instructions into one");
30 /// Order of operands to transfer
31 // TODO: Will be extended when additional optimizations are added
32 enum OperandTransfer
{
33 OT_NA
, ///< Not applicable
34 OT_OperandsAll
, ///< Transfer all operands
35 OT_Operands02
, ///< Transfer operands 0 and 2
36 OT_Operand2
, ///< Transfer just operand 2
37 OT_OperandsXOR
, ///< Transfer operands for XOR16
38 OT_OperandsLwp
, ///< Transfer operands for LWP
39 OT_OperandsSwp
, ///< Transfer operands for SWP
40 OT_OperandsMovep
, ///< Transfer operands for MOVEP
44 // TODO: Will be extended when additional optimizations are added
46 RT_TwoInstr
, ///< Reduce two instructions into one instruction
47 RT_OneInstr
///< Reduce one instruction into a smaller instruction
50 // Information about immediate field restrictions
52 ImmField() : ImmFieldOperand(-1), Shift(0), LBound(0), HBound(0) {}
53 ImmField(uint8_t Shift
, int16_t LBound
, int16_t HBound
,
54 int8_t ImmFieldOperand
)
55 : ImmFieldOperand(ImmFieldOperand
), Shift(Shift
), LBound(LBound
),
57 int8_t ImmFieldOperand
; // Immediate operand, -1 if it does not exist
58 uint8_t Shift
; // Shift value
59 int16_t LBound
; // Low bound of the immediate operand
60 int16_t HBound
; // High bound of the immediate operand
63 /// Information about operands
64 // TODO: Will be extended when additional optimizations are added
66 OpInfo(enum OperandTransfer TransferOperands
)
67 : TransferOperands(TransferOperands
) {}
68 OpInfo() : TransferOperands(OT_NA
) {}
71 TransferOperands
; ///< Operands to transfer to the new instruction
74 // Information about opcodes
76 OpCodes(unsigned WideOpc
, unsigned NarrowOpc
)
77 : WideOpc(WideOpc
), NarrowOpc(NarrowOpc
) {}
79 unsigned WideOpc
; ///< Wide opcode
80 unsigned NarrowOpc
; ///< Narrow opcode
83 typedef struct ReduceEntryFunArgs ReduceEntryFunArgs
;
85 /// ReduceTable - A static table with information on mapping from wide
89 enum ReduceType eRType
; ///< Reduction type
90 bool (*ReduceFunction
)(
91 ReduceEntryFunArgs
*Arguments
); ///< Pointer to reduce function
92 struct OpCodes Ops
; ///< All relevant OpCodes
93 struct OpInfo OpInf
; ///< Characteristics of operands
94 struct ImmField Imm
; ///< Characteristics of immediate field
96 ReduceEntry(enum ReduceType RType
, struct OpCodes Op
,
97 bool (*F
)(ReduceEntryFunArgs
*Arguments
), struct OpInfo OpInf
,
99 : eRType(RType
), ReduceFunction(F
), Ops(Op
), OpInf(OpInf
), Imm(Imm
) {}
101 unsigned NarrowOpc() const { return Ops
.NarrowOpc
; }
102 unsigned WideOpc() const { return Ops
.WideOpc
; }
103 int16_t LBound() const { return Imm
.LBound
; }
104 int16_t HBound() const { return Imm
.HBound
; }
105 uint8_t Shift() const { return Imm
.Shift
; }
106 int8_t ImmField() const { return Imm
.ImmFieldOperand
; }
107 enum OperandTransfer
TransferOperands() const {
108 return OpInf
.TransferOperands
;
110 enum ReduceType
RType() const { return eRType
; }
112 // operator used by std::equal_range
113 bool operator<(const unsigned int r
) const { return (WideOpc() < r
); }
115 // operator used by std::equal_range
116 friend bool operator<(const unsigned int r
, const struct ReduceEntry
&re
) {
117 return (r
< re
.WideOpc());
121 // Function arguments for ReduceFunction
122 struct ReduceEntryFunArgs
{
123 MachineInstr
*MI
; // Instruction
124 const ReduceEntry
&Entry
; // Entry field
125 MachineBasicBlock::instr_iterator
126 &NextMII
; // Iterator to next instruction in block
128 ReduceEntryFunArgs(MachineInstr
*argMI
, const ReduceEntry
&argEntry
,
129 MachineBasicBlock::instr_iterator
&argNextMII
)
130 : MI(argMI
), Entry(argEntry
), NextMII(argNextMII
) {}
133 typedef llvm::SmallVector
<ReduceEntry
, 32> ReduceEntryVector
;
135 class MicroMipsSizeReduce
: public MachineFunctionPass
{
138 MicroMipsSizeReduce();
140 static const MipsInstrInfo
*MipsII
;
141 const MipsSubtarget
*Subtarget
;
143 bool runOnMachineFunction(MachineFunction
&MF
) override
;
145 llvm::StringRef
getPassName() const override
{
146 return "microMIPS instruction size reduction pass";
150 /// Reduces width of instructions in the specified basic block.
151 bool ReduceMBB(MachineBasicBlock
&MBB
);
153 /// Attempts to reduce MI, returns true on success.
154 bool ReduceMI(const MachineBasicBlock::instr_iterator
&MII
,
155 MachineBasicBlock::instr_iterator
&NextMII
);
157 // Attempts to reduce LW/SW instruction into LWSP/SWSP,
158 // returns true on success.
159 static bool ReduceXWtoXWSP(ReduceEntryFunArgs
*Arguments
);
161 // Attempts to reduce two LW/SW instructions into LWP/SWP instruction,
162 // returns true on success.
163 static bool ReduceXWtoXWP(ReduceEntryFunArgs
*Arguments
);
165 // Attempts to reduce LBU/LHU instruction into LBU16/LHU16,
166 // returns true on success.
167 static bool ReduceLXUtoLXU16(ReduceEntryFunArgs
*Arguments
);
169 // Attempts to reduce SB/SH instruction into SB16/SH16,
170 // returns true on success.
171 static bool ReduceSXtoSX16(ReduceEntryFunArgs
*Arguments
);
173 // Attempts to reduce two MOVE instructions into MOVEP instruction,
174 // returns true on success.
175 static bool ReduceMoveToMovep(ReduceEntryFunArgs
*Arguments
);
177 // Attempts to reduce arithmetic instructions, returns true on success.
178 static bool ReduceArithmeticInstructions(ReduceEntryFunArgs
*Arguments
);
180 // Attempts to reduce ADDIU into ADDIUSP instruction,
181 // returns true on success.
182 static bool ReduceADDIUToADDIUSP(ReduceEntryFunArgs
*Arguments
);
184 // Attempts to reduce ADDIU into ADDIUR1SP instruction,
185 // returns true on success.
186 static bool ReduceADDIUToADDIUR1SP(ReduceEntryFunArgs
*Arguments
);
188 // Attempts to reduce XOR into XOR16 instruction,
189 // returns true on success.
190 static bool ReduceXORtoXOR16(ReduceEntryFunArgs
*Arguments
);
192 // Changes opcode of an instruction, replaces an instruction with a
193 // new one, or replaces two instructions with a new instruction
194 // depending on their order i.e. if these are consecutive forward
195 // or consecutive backward
196 static bool ReplaceInstruction(MachineInstr
*MI
, const ReduceEntry
&Entry
,
197 MachineInstr
*MI2
= nullptr,
198 bool ConsecutiveForward
= true);
200 // Table with transformation rules for each instruction.
201 static ReduceEntryVector ReduceTable
;
204 char MicroMipsSizeReduce::ID
= 0;
205 const MipsInstrInfo
*MicroMipsSizeReduce::MipsII
;
207 // This table must be sorted by WideOpc as a main criterion and
208 // ReduceType as a sub-criterion (when wide opcodes are the same).
209 ReduceEntryVector
MicroMipsSizeReduce::ReduceTable
= {
211 // ReduceType, OpCodes, ReduceFunction,
212 // OpInfo(TransferOperands),
213 // ImmField(Shift, LBound, HBound, ImmFieldPosition)
214 {RT_OneInstr
, OpCodes(Mips::ADDiu
, Mips::ADDIUR1SP_MM
),
215 ReduceADDIUToADDIUR1SP
, OpInfo(OT_Operands02
), ImmField(2, 0, 64, 2)},
216 {RT_OneInstr
, OpCodes(Mips::ADDiu
, Mips::ADDIUSP_MM
), ReduceADDIUToADDIUSP
,
217 OpInfo(OT_Operand2
), ImmField(0, 0, 0, 2)},
218 {RT_OneInstr
, OpCodes(Mips::ADDiu_MM
, Mips::ADDIUR1SP_MM
),
219 ReduceADDIUToADDIUR1SP
, OpInfo(OT_Operands02
), ImmField(2, 0, 64, 2)},
220 {RT_OneInstr
, OpCodes(Mips::ADDiu_MM
, Mips::ADDIUSP_MM
),
221 ReduceADDIUToADDIUSP
, OpInfo(OT_Operand2
), ImmField(0, 0, 0, 2)},
222 {RT_OneInstr
, OpCodes(Mips::ADDu
, Mips::ADDU16_MM
),
223 ReduceArithmeticInstructions
, OpInfo(OT_OperandsAll
),
224 ImmField(0, 0, 0, -1)},
225 {RT_OneInstr
, OpCodes(Mips::ADDu_MM
, Mips::ADDU16_MM
),
226 ReduceArithmeticInstructions
, OpInfo(OT_OperandsAll
),
227 ImmField(0, 0, 0, -1)},
228 {RT_OneInstr
, OpCodes(Mips::LBu
, Mips::LBU16_MM
), ReduceLXUtoLXU16
,
229 OpInfo(OT_OperandsAll
), ImmField(0, -1, 15, 2)},
230 {RT_OneInstr
, OpCodes(Mips::LBu_MM
, Mips::LBU16_MM
), ReduceLXUtoLXU16
,
231 OpInfo(OT_OperandsAll
), ImmField(0, -1, 15, 2)},
232 {RT_OneInstr
, OpCodes(Mips::LEA_ADDiu
, Mips::ADDIUR1SP_MM
),
233 ReduceADDIUToADDIUR1SP
, OpInfo(OT_Operands02
), ImmField(2, 0, 64, 2)},
234 {RT_OneInstr
, OpCodes(Mips::LEA_ADDiu_MM
, Mips::ADDIUR1SP_MM
),
235 ReduceADDIUToADDIUR1SP
, OpInfo(OT_Operands02
), ImmField(2, 0, 64, 2)},
236 {RT_OneInstr
, OpCodes(Mips::LHu
, Mips::LHU16_MM
), ReduceLXUtoLXU16
,
237 OpInfo(OT_OperandsAll
), ImmField(1, 0, 16, 2)},
238 {RT_OneInstr
, OpCodes(Mips::LHu_MM
, Mips::LHU16_MM
), ReduceLXUtoLXU16
,
239 OpInfo(OT_OperandsAll
), ImmField(1, 0, 16, 2)},
240 {RT_TwoInstr
, OpCodes(Mips::LW
, Mips::LWP_MM
), ReduceXWtoXWP
,
241 OpInfo(OT_OperandsLwp
), ImmField(0, -2048, 2048, 2)},
242 {RT_OneInstr
, OpCodes(Mips::LW
, Mips::LWSP_MM
), ReduceXWtoXWSP
,
243 OpInfo(OT_OperandsAll
), ImmField(2, 0, 32, 2)},
244 {RT_TwoInstr
, OpCodes(Mips::LW16_MM
, Mips::LWP_MM
), ReduceXWtoXWP
,
245 OpInfo(OT_OperandsLwp
), ImmField(0, -2048, 2048, 2)},
246 {RT_TwoInstr
, OpCodes(Mips::LW_MM
, Mips::LWP_MM
), ReduceXWtoXWP
,
247 OpInfo(OT_OperandsLwp
), ImmField(0, -2048, 2048, 2)},
248 {RT_OneInstr
, OpCodes(Mips::LW_MM
, Mips::LWSP_MM
), ReduceXWtoXWSP
,
249 OpInfo(OT_OperandsAll
), ImmField(2, 0, 32, 2)},
250 {RT_TwoInstr
, OpCodes(Mips::MOVE16_MM
, Mips::MOVEP_MM
), ReduceMoveToMovep
,
251 OpInfo(OT_OperandsMovep
), ImmField(0, 0, 0, -1)},
252 {RT_OneInstr
, OpCodes(Mips::SB
, Mips::SB16_MM
), ReduceSXtoSX16
,
253 OpInfo(OT_OperandsAll
), ImmField(0, 0, 16, 2)},
254 {RT_OneInstr
, OpCodes(Mips::SB_MM
, Mips::SB16_MM
), ReduceSXtoSX16
,
255 OpInfo(OT_OperandsAll
), ImmField(0, 0, 16, 2)},
256 {RT_OneInstr
, OpCodes(Mips::SH
, Mips::SH16_MM
), ReduceSXtoSX16
,
257 OpInfo(OT_OperandsAll
), ImmField(1, 0, 16, 2)},
258 {RT_OneInstr
, OpCodes(Mips::SH_MM
, Mips::SH16_MM
), ReduceSXtoSX16
,
259 OpInfo(OT_OperandsAll
), ImmField(1, 0, 16, 2)},
260 {RT_OneInstr
, OpCodes(Mips::SUBu
, Mips::SUBU16_MM
),
261 ReduceArithmeticInstructions
, OpInfo(OT_OperandsAll
),
262 ImmField(0, 0, 0, -1)},
263 {RT_OneInstr
, OpCodes(Mips::SUBu_MM
, Mips::SUBU16_MM
),
264 ReduceArithmeticInstructions
, OpInfo(OT_OperandsAll
),
265 ImmField(0, 0, 0, -1)},
266 {RT_TwoInstr
, OpCodes(Mips::SW
, Mips::SWP_MM
), ReduceXWtoXWP
,
267 OpInfo(OT_OperandsSwp
), ImmField(0, -2048, 2048, 2)},
268 {RT_OneInstr
, OpCodes(Mips::SW
, Mips::SWSP_MM
), ReduceXWtoXWSP
,
269 OpInfo(OT_OperandsAll
), ImmField(2, 0, 32, 2)},
270 {RT_TwoInstr
, OpCodes(Mips::SW16_MM
, Mips::SWP_MM
), ReduceXWtoXWP
,
271 OpInfo(OT_OperandsSwp
), ImmField(0, -2048, 2048, 2)},
272 {RT_TwoInstr
, OpCodes(Mips::SW_MM
, Mips::SWP_MM
), ReduceXWtoXWP
,
273 OpInfo(OT_OperandsSwp
), ImmField(0, -2048, 2048, 2)},
274 {RT_OneInstr
, OpCodes(Mips::SW_MM
, Mips::SWSP_MM
), ReduceXWtoXWSP
,
275 OpInfo(OT_OperandsAll
), ImmField(2, 0, 32, 2)},
276 {RT_OneInstr
, OpCodes(Mips::XOR
, Mips::XOR16_MM
), ReduceXORtoXOR16
,
277 OpInfo(OT_OperandsXOR
), ImmField(0, 0, 0, -1)},
278 {RT_OneInstr
, OpCodes(Mips::XOR_MM
, Mips::XOR16_MM
), ReduceXORtoXOR16
,
279 OpInfo(OT_OperandsXOR
), ImmField(0, 0, 0, -1)}};
280 } // end anonymous namespace
282 INITIALIZE_PASS(MicroMipsSizeReduce
, DEBUG_TYPE
, MICROMIPS_SIZE_REDUCE_NAME
,
285 // Returns true if the machine operand MO is register SP.
286 static bool IsSP(const MachineOperand
&MO
) {
287 if (MO
.isReg() && ((MO
.getReg() == Mips::SP
)))
292 // Returns true if the machine operand MO is register $16, $17, or $2-$7.
293 static bool isMMThreeBitGPRegister(const MachineOperand
&MO
) {
294 if (MO
.isReg() && Mips::GPRMM16RegClass
.contains(MO
.getReg()))
299 // Returns true if the machine operand MO is register $0, $17, or $2-$7.
300 static bool isMMSourceRegister(const MachineOperand
&MO
) {
301 if (MO
.isReg() && Mips::GPRMM16ZeroRegClass
.contains(MO
.getReg()))
306 // Returns true if the operand Op is an immediate value
307 // and writes the immediate value into variable Imm.
308 static bool GetImm(MachineInstr
*MI
, unsigned Op
, int64_t &Imm
) {
310 if (!MI
->getOperand(Op
).isImm())
312 Imm
= MI
->getOperand(Op
).getImm();
316 // Returns true if the value is a valid immediate for ADDIUSP.
317 static bool AddiuspImmValue(int64_t Value
) {
318 int64_t Value2
= Value
>> 2;
319 if (((Value
& (int64_t)maskTrailingZeros
<uint64_t>(2)) == Value
) &&
320 ((Value2
>= 2 && Value2
<= 257) || (Value2
>= -258 && Value2
<= -3)))
325 // Returns true if the variable Value has the number of least-significant zero
326 // bits equal to Shift and if the shifted value is between the bounds.
327 static bool InRange(int64_t Value
, unsigned short Shift
, int LBound
,
329 int64_t Value2
= Value
>> Shift
;
330 if (((Value
& (int64_t)maskTrailingZeros
<uint64_t>(Shift
)) == Value
) &&
331 (Value2
>= LBound
) && (Value2
< HBound
))
336 // Returns true if immediate operand is in range.
337 static bool ImmInRange(MachineInstr
*MI
, const ReduceEntry
&Entry
) {
341 if (!GetImm(MI
, Entry
.ImmField(), offset
))
344 if (!InRange(offset
, Entry
.Shift(), Entry
.LBound(), Entry
.HBound()))
350 // Returns true if MI can be reduced to lwp/swp instruction
351 static bool CheckXWPInstr(MachineInstr
*MI
, bool ReduceToLwp
,
352 const ReduceEntry
&Entry
) {
355 !(MI
->getOpcode() == Mips::LW
|| MI
->getOpcode() == Mips::LW_MM
||
356 MI
->getOpcode() == Mips::LW16_MM
))
360 !(MI
->getOpcode() == Mips::SW
|| MI
->getOpcode() == Mips::SW_MM
||
361 MI
->getOpcode() == Mips::SW16_MM
))
364 Register reg
= MI
->getOperand(0).getReg();
368 if (!ImmInRange(MI
, Entry
))
371 if (ReduceToLwp
&& (MI
->getOperand(0).getReg() == MI
->getOperand(1).getReg()))
377 // Returns true if the registers Reg1 and Reg2 are consecutive
378 static bool ConsecutiveRegisters(unsigned Reg1
, unsigned Reg2
) {
379 static SmallVector
<unsigned, 31> Registers
= {
380 Mips::AT
, Mips::V0
, Mips::V1
, Mips::A0
, Mips::A1
, Mips::A2
, Mips::A3
,
381 Mips::T0
, Mips::T1
, Mips::T2
, Mips::T3
, Mips::T4
, Mips::T5
, Mips::T6
,
382 Mips::T7
, Mips::S0
, Mips::S1
, Mips::S2
, Mips::S3
, Mips::S4
, Mips::S5
,
383 Mips::S6
, Mips::S7
, Mips::T8
, Mips::T9
, Mips::K0
, Mips::K1
, Mips::GP
,
384 Mips::SP
, Mips::FP
, Mips::RA
};
386 for (uint8_t i
= 0; i
< Registers
.size() - 1; i
++) {
387 if (Registers
[i
] == Reg1
) {
388 if (Registers
[i
+ 1] == Reg2
)
397 // Returns true if registers and offsets are consecutive
398 static bool ConsecutiveInstr(MachineInstr
*MI1
, MachineInstr
*MI2
) {
400 int64_t Offset1
, Offset2
;
401 if (!GetImm(MI1
, 2, Offset1
))
403 if (!GetImm(MI2
, 2, Offset2
))
406 Register Reg1
= MI1
->getOperand(0).getReg();
407 Register Reg2
= MI2
->getOperand(0).getReg();
409 return ((Offset1
== (Offset2
- 4)) && (ConsecutiveRegisters(Reg1
, Reg2
)));
412 MicroMipsSizeReduce::MicroMipsSizeReduce() : MachineFunctionPass(ID
) {}
414 bool MicroMipsSizeReduce::ReduceMI(const MachineBasicBlock::instr_iterator
&MII
,
415 MachineBasicBlock::instr_iterator
&NextMII
) {
417 MachineInstr
*MI
= &*MII
;
418 unsigned Opcode
= MI
->getOpcode();
421 ReduceEntryVector::const_iterator Start
= std::begin(ReduceTable
);
422 ReduceEntryVector::const_iterator End
= std::end(ReduceTable
);
424 std::pair
<ReduceEntryVector::const_iterator
,
425 ReduceEntryVector::const_iterator
>
426 Range
= std::equal_range(Start
, End
, Opcode
);
428 if (Range
.first
== Range
.second
)
431 for (ReduceEntryVector::const_iterator Entry
= Range
.first
;
432 Entry
!= Range
.second
; ++Entry
) {
433 ReduceEntryFunArgs
Arguments(&(*MII
), *Entry
, NextMII
);
434 if (((*Entry
).ReduceFunction
)(&Arguments
))
440 bool MicroMipsSizeReduce::ReduceXWtoXWSP(ReduceEntryFunArgs
*Arguments
) {
442 MachineInstr
*MI
= Arguments
->MI
;
443 const ReduceEntry
&Entry
= Arguments
->Entry
;
445 if (!ImmInRange(MI
, Entry
))
448 if (!IsSP(MI
->getOperand(1)))
451 return ReplaceInstruction(MI
, Entry
);
454 bool MicroMipsSizeReduce::ReduceXWtoXWP(ReduceEntryFunArgs
*Arguments
) {
456 const ReduceEntry
&Entry
= Arguments
->Entry
;
457 MachineBasicBlock::instr_iterator
&NextMII
= Arguments
->NextMII
;
458 const MachineBasicBlock::instr_iterator
&E
=
459 Arguments
->MI
->getParent()->instr_end();
464 MachineInstr
*MI1
= Arguments
->MI
;
465 MachineInstr
*MI2
= &*NextMII
;
467 // ReduceToLwp = true/false - reduce to LWP/SWP instruction
468 bool ReduceToLwp
= (MI1
->getOpcode() == Mips::LW
) ||
469 (MI1
->getOpcode() == Mips::LW_MM
) ||
470 (MI1
->getOpcode() == Mips::LW16_MM
);
472 if (!CheckXWPInstr(MI1
, ReduceToLwp
, Entry
))
475 if (!CheckXWPInstr(MI2
, ReduceToLwp
, Entry
))
478 Register Reg1
= MI1
->getOperand(1).getReg();
479 Register Reg2
= MI2
->getOperand(1).getReg();
484 bool ConsecutiveForward
= ConsecutiveInstr(MI1
, MI2
);
485 bool ConsecutiveBackward
= ConsecutiveInstr(MI2
, MI1
);
487 if (!(ConsecutiveForward
|| ConsecutiveBackward
))
490 NextMII
= std::next(NextMII
);
491 return ReplaceInstruction(MI1
, Entry
, MI2
, ConsecutiveForward
);
494 bool MicroMipsSizeReduce::ReduceArithmeticInstructions(
495 ReduceEntryFunArgs
*Arguments
) {
497 MachineInstr
*MI
= Arguments
->MI
;
498 const ReduceEntry
&Entry
= Arguments
->Entry
;
500 if (!isMMThreeBitGPRegister(MI
->getOperand(0)) ||
501 !isMMThreeBitGPRegister(MI
->getOperand(1)) ||
502 !isMMThreeBitGPRegister(MI
->getOperand(2)))
505 return ReplaceInstruction(MI
, Entry
);
508 bool MicroMipsSizeReduce::ReduceADDIUToADDIUR1SP(
509 ReduceEntryFunArgs
*Arguments
) {
511 MachineInstr
*MI
= Arguments
->MI
;
512 const ReduceEntry
&Entry
= Arguments
->Entry
;
514 if (!ImmInRange(MI
, Entry
))
517 if (!isMMThreeBitGPRegister(MI
->getOperand(0)) || !IsSP(MI
->getOperand(1)))
520 return ReplaceInstruction(MI
, Entry
);
523 bool MicroMipsSizeReduce::ReduceADDIUToADDIUSP(ReduceEntryFunArgs
*Arguments
) {
525 MachineInstr
*MI
= Arguments
->MI
;
526 const ReduceEntry
&Entry
= Arguments
->Entry
;
529 if (!GetImm(MI
, Entry
.ImmField(), ImmValue
))
532 if (!AddiuspImmValue(ImmValue
))
535 if (!IsSP(MI
->getOperand(0)) || !IsSP(MI
->getOperand(1)))
538 return ReplaceInstruction(MI
, Entry
);
541 bool MicroMipsSizeReduce::ReduceLXUtoLXU16(ReduceEntryFunArgs
*Arguments
) {
543 MachineInstr
*MI
= Arguments
->MI
;
544 const ReduceEntry
&Entry
= Arguments
->Entry
;
546 if (!ImmInRange(MI
, Entry
))
549 if (!isMMThreeBitGPRegister(MI
->getOperand(0)) ||
550 !isMMThreeBitGPRegister(MI
->getOperand(1)))
553 return ReplaceInstruction(MI
, Entry
);
556 bool MicroMipsSizeReduce::ReduceSXtoSX16(ReduceEntryFunArgs
*Arguments
) {
558 MachineInstr
*MI
= Arguments
->MI
;
559 const ReduceEntry
&Entry
= Arguments
->Entry
;
561 if (!ImmInRange(MI
, Entry
))
564 if (!isMMSourceRegister(MI
->getOperand(0)) ||
565 !isMMThreeBitGPRegister(MI
->getOperand(1)))
568 return ReplaceInstruction(MI
, Entry
);
571 // Returns true if Reg can be a source register
572 // of MOVEP instruction
573 static bool IsMovepSrcRegister(unsigned Reg
) {
575 if (Reg
== Mips::ZERO
|| Reg
== Mips::V0
|| Reg
== Mips::V1
||
576 Reg
== Mips::S0
|| Reg
== Mips::S1
|| Reg
== Mips::S2
||
577 Reg
== Mips::S3
|| Reg
== Mips::S4
)
583 // Returns true if Reg can be a destination register
584 // of MOVEP instruction
585 static bool IsMovepDestinationReg(unsigned Reg
) {
587 if (Reg
== Mips::A0
|| Reg
== Mips::A1
|| Reg
== Mips::A2
||
588 Reg
== Mips::A3
|| Reg
== Mips::S5
|| Reg
== Mips::S6
)
594 // Returns true if the registers can be a pair of destination
595 // registers in MOVEP instruction
596 static bool IsMovepDestinationRegPair(unsigned R0
, unsigned R1
) {
598 if ((R0
== Mips::A0
&& R1
== Mips::S5
) ||
599 (R0
== Mips::A0
&& R1
== Mips::S6
) ||
600 (R0
== Mips::A0
&& R1
== Mips::A1
) ||
601 (R0
== Mips::A0
&& R1
== Mips::A2
) ||
602 (R0
== Mips::A0
&& R1
== Mips::A3
) ||
603 (R0
== Mips::A1
&& R1
== Mips::A2
) ||
604 (R0
== Mips::A1
&& R1
== Mips::A3
) ||
605 (R0
== Mips::A2
&& R1
== Mips::A3
))
611 bool MicroMipsSizeReduce::ReduceMoveToMovep(ReduceEntryFunArgs
*Arguments
) {
613 const ReduceEntry
&Entry
= Arguments
->Entry
;
614 MachineBasicBlock::instr_iterator
&NextMII
= Arguments
->NextMII
;
615 const MachineBasicBlock::instr_iterator
&E
=
616 Arguments
->MI
->getParent()->instr_end();
621 MachineInstr
*MI1
= Arguments
->MI
;
622 MachineInstr
*MI2
= &*NextMII
;
624 Register RegDstMI1
= MI1
->getOperand(0).getReg();
625 Register RegSrcMI1
= MI1
->getOperand(1).getReg();
627 if (!IsMovepSrcRegister(RegSrcMI1
))
630 if (!IsMovepDestinationReg(RegDstMI1
))
633 if (MI2
->getOpcode() != Entry
.WideOpc())
636 Register RegDstMI2
= MI2
->getOperand(0).getReg();
637 Register RegSrcMI2
= MI2
->getOperand(1).getReg();
639 if (!IsMovepSrcRegister(RegSrcMI2
))
642 bool ConsecutiveForward
;
643 if (IsMovepDestinationRegPair(RegDstMI1
, RegDstMI2
)) {
644 ConsecutiveForward
= true;
645 } else if (IsMovepDestinationRegPair(RegDstMI2
, RegDstMI1
)) {
646 ConsecutiveForward
= false;
650 NextMII
= std::next(NextMII
);
651 return ReplaceInstruction(MI1
, Entry
, MI2
, ConsecutiveForward
);
654 bool MicroMipsSizeReduce::ReduceXORtoXOR16(ReduceEntryFunArgs
*Arguments
) {
656 MachineInstr
*MI
= Arguments
->MI
;
657 const ReduceEntry
&Entry
= Arguments
->Entry
;
659 if (!isMMThreeBitGPRegister(MI
->getOperand(0)) ||
660 !isMMThreeBitGPRegister(MI
->getOperand(1)) ||
661 !isMMThreeBitGPRegister(MI
->getOperand(2)))
664 if (!(MI
->getOperand(0).getReg() == MI
->getOperand(2).getReg()) &&
665 !(MI
->getOperand(0).getReg() == MI
->getOperand(1).getReg()))
668 return ReplaceInstruction(MI
, Entry
);
671 bool MicroMipsSizeReduce::ReduceMBB(MachineBasicBlock
&MBB
) {
672 bool Modified
= false;
673 MachineBasicBlock::instr_iterator MII
= MBB
.instr_begin(),
675 MachineBasicBlock::instr_iterator NextMII
;
677 // Iterate through the instructions in the basic block
678 for (; MII
!= E
; MII
= NextMII
) {
679 NextMII
= std::next(MII
);
680 MachineInstr
*MI
= &*MII
;
682 // Don't reduce bundled instructions or pseudo operations
683 if (MI
->isBundle() || MI
->isTransient())
686 // Try to reduce 32-bit instruction into 16-bit instruction
687 Modified
|= ReduceMI(MII
, NextMII
);
693 bool MicroMipsSizeReduce::ReplaceInstruction(MachineInstr
*MI
,
694 const ReduceEntry
&Entry
,
696 bool ConsecutiveForward
) {
698 enum OperandTransfer OpTransfer
= Entry
.TransferOperands();
700 LLVM_DEBUG(dbgs() << "Converting 32-bit: " << *MI
);
703 if (OpTransfer
== OT_OperandsAll
) {
704 MI
->setDesc(MipsII
->get(Entry
.NarrowOpc()));
705 LLVM_DEBUG(dbgs() << " to 16-bit: " << *MI
);
708 MachineBasicBlock
&MBB
= *MI
->getParent();
709 const MCInstrDesc
&NewMCID
= MipsII
->get(Entry
.NarrowOpc());
710 DebugLoc dl
= MI
->getDebugLoc();
711 MachineInstrBuilder MIB
= BuildMI(MBB
, MI
, dl
, NewMCID
);
712 switch (OpTransfer
) {
714 MIB
.add(MI
->getOperand(2));
716 case OT_Operands02
: {
717 MIB
.add(MI
->getOperand(0));
718 MIB
.add(MI
->getOperand(2));
721 case OT_OperandsXOR
: {
722 if (MI
->getOperand(0).getReg() == MI
->getOperand(2).getReg()) {
723 MIB
.add(MI
->getOperand(0));
724 MIB
.add(MI
->getOperand(1));
725 MIB
.add(MI
->getOperand(2));
727 MIB
.add(MI
->getOperand(0));
728 MIB
.add(MI
->getOperand(2));
729 MIB
.add(MI
->getOperand(1));
733 case OT_OperandsMovep
:
735 case OT_OperandsSwp
: {
736 if (ConsecutiveForward
) {
737 MIB
.add(MI
->getOperand(0));
738 MIB
.add(MI2
->getOperand(0));
739 MIB
.add(MI
->getOperand(1));
740 if (OpTransfer
== OT_OperandsMovep
)
741 MIB
.add(MI2
->getOperand(1));
743 MIB
.add(MI
->getOperand(2));
744 } else { // consecutive backward
745 MIB
.add(MI2
->getOperand(0));
746 MIB
.add(MI
->getOperand(0));
747 MIB
.add(MI2
->getOperand(1));
748 if (OpTransfer
== OT_OperandsMovep
)
749 MIB
.add(MI
->getOperand(1));
751 MIB
.add(MI2
->getOperand(2));
754 LLVM_DEBUG(dbgs() << "and converting 32-bit: " << *MI2
758 MBB
.erase_instr(MI2
);
762 llvm_unreachable("Unknown operand transfer!");
765 // Transfer MI flags.
766 MIB
.setMIFlags(MI
->getFlags());
768 LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB
);
775 bool MicroMipsSizeReduce::runOnMachineFunction(MachineFunction
&MF
) {
777 Subtarget
= &static_cast<const MipsSubtarget
&>(MF
.getSubtarget());
779 // TODO: Add support for the subtarget microMIPS32R6.
780 if (!Subtarget
->inMicroMipsMode() || !Subtarget
->hasMips32r2() ||
781 Subtarget
->hasMips32r6())
784 MipsII
= static_cast<const MipsInstrInfo
*>(Subtarget
->getInstrInfo());
786 bool Modified
= false;
787 MachineFunction::iterator I
= MF
.begin(), E
= MF
.end();
790 Modified
|= ReduceMBB(*I
);
794 /// Returns an instance of the MicroMips size reduction pass.
795 FunctionPass
*llvm::createMicroMipsSizeReducePass() {
796 return new MicroMipsSizeReduce();