1 //=== MicroMipsSizeReduction.cpp - MicroMips size reduction pass --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This pass is used to reduce the size of instructions where applicable.
12 /// TODO: Implement microMIPS64 support.
13 //===----------------------------------------------------------------------===//
15 #include "MipsInstrInfo.h"
16 #include "MipsSubtarget.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/Support/Debug.h"
23 #define DEBUG_TYPE "micromips-reduce-size"
24 #define MICROMIPS_SIZE_REDUCE_NAME "MicroMips instruction size reduce pass"
26 STATISTIC(NumReduced
, "Number of instructions reduced (32-bit to 16-bit ones, "
27 "or two instructions into one");
31 /// Order of operands to transfer
32 // TODO: Will be extended when additional optimizations are added
33 enum OperandTransfer
{
34 OT_NA
, ///< Not applicable
35 OT_OperandsAll
, ///< Transfer all operands
36 OT_Operands02
, ///< Transfer operands 0 and 2
37 OT_Operand2
, ///< Transfer just operand 2
38 OT_OperandsXOR
, ///< Transfer operands for XOR16
39 OT_OperandsLwp
, ///< Transfer operands for LWP
40 OT_OperandsSwp
, ///< Transfer operands for SWP
41 OT_OperandsMovep
, ///< Transfer operands for MOVEP
45 // TODO: Will be extended when additional optimizations are added
47 RT_TwoInstr
, ///< Reduce two instructions into one instruction
48 RT_OneInstr
///< Reduce one instruction into a smaller instruction
51 // Information about immediate field restrictions
53 ImmField() : ImmFieldOperand(-1), Shift(0), LBound(0), HBound(0) {}
54 ImmField(uint8_t Shift
, int16_t LBound
, int16_t HBound
,
55 int8_t ImmFieldOperand
)
56 : ImmFieldOperand(ImmFieldOperand
), Shift(Shift
), LBound(LBound
),
58 int8_t ImmFieldOperand
; // Immediate operand, -1 if it does not exist
59 uint8_t Shift
; // Shift value
60 int16_t LBound
; // Low bound of the immediate operand
61 int16_t HBound
; // High bound of the immediate operand
64 /// Information about operands
65 // TODO: Will be extended when additional optimizations are added
67 OpInfo(enum OperandTransfer TransferOperands
)
68 : TransferOperands(TransferOperands
) {}
69 OpInfo() : TransferOperands(OT_NA
) {}
72 TransferOperands
; ///< Operands to transfer to the new instruction
75 // Information about opcodes
77 OpCodes(unsigned WideOpc
, unsigned NarrowOpc
)
78 : WideOpc(WideOpc
), NarrowOpc(NarrowOpc
) {}
80 unsigned WideOpc
; ///< Wide opcode
81 unsigned NarrowOpc
; ///< Narrow opcode
84 typedef struct ReduceEntryFunArgs ReduceEntryFunArgs
;
86 /// ReduceTable - A static table with information on mapping from wide
90 enum ReduceType eRType
; ///< Reduction type
91 bool (*ReduceFunction
)(
92 ReduceEntryFunArgs
*Arguments
); ///< Pointer to reduce function
93 struct OpCodes Ops
; ///< All relevant OpCodes
94 struct OpInfo OpInf
; ///< Characteristics of operands
95 struct ImmField Imm
; ///< Characteristics of immediate field
97 ReduceEntry(enum ReduceType RType
, struct OpCodes Op
,
98 bool (*F
)(ReduceEntryFunArgs
*Arguments
), struct OpInfo OpInf
,
100 : eRType(RType
), ReduceFunction(F
), Ops(Op
), OpInf(OpInf
), Imm(Imm
) {}
102 unsigned NarrowOpc() const { return Ops
.NarrowOpc
; }
103 unsigned WideOpc() const { return Ops
.WideOpc
; }
104 int16_t LBound() const { return Imm
.LBound
; }
105 int16_t HBound() const { return Imm
.HBound
; }
106 uint8_t Shift() const { return Imm
.Shift
; }
107 int8_t ImmField() const { return Imm
.ImmFieldOperand
; }
108 enum OperandTransfer
TransferOperands() const {
109 return OpInf
.TransferOperands
;
111 enum ReduceType
RType() const { return eRType
; }
113 // operator used by std::equal_range
114 bool operator<(const unsigned int r
) const { return (WideOpc() < r
); }
116 // operator used by std::equal_range
117 friend bool operator<(const unsigned int r
, const struct ReduceEntry
&re
) {
118 return (r
< re
.WideOpc());
122 // Function arguments for ReduceFunction
123 struct ReduceEntryFunArgs
{
124 MachineInstr
*MI
; // Instruction
125 const ReduceEntry
&Entry
; // Entry field
126 MachineBasicBlock::instr_iterator
127 &NextMII
; // Iterator to next instruction in block
129 ReduceEntryFunArgs(MachineInstr
*argMI
, const ReduceEntry
&argEntry
,
130 MachineBasicBlock::instr_iterator
&argNextMII
)
131 : MI(argMI
), Entry(argEntry
), NextMII(argNextMII
) {}
134 typedef llvm::SmallVector
<ReduceEntry
, 32> ReduceEntryVector
;
136 class MicroMipsSizeReduce
: public MachineFunctionPass
{
139 MicroMipsSizeReduce();
141 static const MipsInstrInfo
*MipsII
;
142 const MipsSubtarget
*Subtarget
;
144 bool runOnMachineFunction(MachineFunction
&MF
) override
;
146 llvm::StringRef
getPassName() const override
{
147 return "microMIPS instruction size reduction pass";
151 /// Reduces width of instructions in the specified basic block.
152 bool ReduceMBB(MachineBasicBlock
&MBB
);
154 /// Attempts to reduce MI, returns true on success.
155 bool ReduceMI(const MachineBasicBlock::instr_iterator
&MII
,
156 MachineBasicBlock::instr_iterator
&NextMII
);
158 // Attempts to reduce LW/SW instruction into LWSP/SWSP,
159 // returns true on success.
160 static bool ReduceXWtoXWSP(ReduceEntryFunArgs
*Arguments
);
162 // Attempts to reduce two LW/SW instructions into LWP/SWP instruction,
163 // returns true on success.
164 static bool ReduceXWtoXWP(ReduceEntryFunArgs
*Arguments
);
166 // Attempts to reduce LBU/LHU instruction into LBU16/LHU16,
167 // returns true on success.
168 static bool ReduceLXUtoLXU16(ReduceEntryFunArgs
*Arguments
);
170 // Attempts to reduce SB/SH instruction into SB16/SH16,
171 // returns true on success.
172 static bool ReduceSXtoSX16(ReduceEntryFunArgs
*Arguments
);
174 // Attempts to reduce two MOVE instructions into MOVEP instruction,
175 // returns true on success.
176 static bool ReduceMoveToMovep(ReduceEntryFunArgs
*Arguments
);
178 // Attempts to reduce arithmetic instructions, returns true on success.
179 static bool ReduceArithmeticInstructions(ReduceEntryFunArgs
*Arguments
);
181 // Attempts to reduce ADDIU into ADDIUSP instruction,
182 // returns true on success.
183 static bool ReduceADDIUToADDIUSP(ReduceEntryFunArgs
*Arguments
);
185 // Attempts to reduce ADDIU into ADDIUR1SP instruction,
186 // returns true on success.
187 static bool ReduceADDIUToADDIUR1SP(ReduceEntryFunArgs
*Arguments
);
189 // Attempts to reduce XOR into XOR16 instruction,
190 // returns true on success.
191 static bool ReduceXORtoXOR16(ReduceEntryFunArgs
*Arguments
);
193 // Changes opcode of an instruction, replaces an instruction with a
194 // new one, or replaces two instructions with a new instruction
195 // depending on their order i.e. if these are consecutive forward
196 // or consecutive backward
197 static bool ReplaceInstruction(MachineInstr
*MI
, const ReduceEntry
&Entry
,
198 MachineInstr
*MI2
= nullptr,
199 bool ConsecutiveForward
= true);
201 // Table with transformation rules for each instruction.
202 static ReduceEntryVector ReduceTable
;
205 char MicroMipsSizeReduce::ID
= 0;
206 const MipsInstrInfo
*MicroMipsSizeReduce::MipsII
;
208 // This table must be sorted by WideOpc as a main criterion and
209 // ReduceType as a sub-criterion (when wide opcodes are the same).
210 ReduceEntryVector
MicroMipsSizeReduce::ReduceTable
= {
212 // ReduceType, OpCodes, ReduceFunction,
213 // OpInfo(TransferOperands),
214 // ImmField(Shift, LBound, HBound, ImmFieldPosition)
215 {RT_OneInstr
, OpCodes(Mips::ADDiu
, Mips::ADDIUR1SP_MM
),
216 ReduceADDIUToADDIUR1SP
, OpInfo(OT_Operands02
), ImmField(2, 0, 64, 2)},
217 {RT_OneInstr
, OpCodes(Mips::ADDiu
, Mips::ADDIUSP_MM
), ReduceADDIUToADDIUSP
,
218 OpInfo(OT_Operand2
), ImmField(0, 0, 0, 2)},
219 {RT_OneInstr
, OpCodes(Mips::ADDiu_MM
, Mips::ADDIUR1SP_MM
),
220 ReduceADDIUToADDIUR1SP
, OpInfo(OT_Operands02
), ImmField(2, 0, 64, 2)},
221 {RT_OneInstr
, OpCodes(Mips::ADDiu_MM
, Mips::ADDIUSP_MM
),
222 ReduceADDIUToADDIUSP
, OpInfo(OT_Operand2
), ImmField(0, 0, 0, 2)},
223 {RT_OneInstr
, OpCodes(Mips::ADDu
, Mips::ADDU16_MM
),
224 ReduceArithmeticInstructions
, OpInfo(OT_OperandsAll
),
225 ImmField(0, 0, 0, -1)},
226 {RT_OneInstr
, OpCodes(Mips::ADDu_MM
, Mips::ADDU16_MM
),
227 ReduceArithmeticInstructions
, OpInfo(OT_OperandsAll
),
228 ImmField(0, 0, 0, -1)},
229 {RT_OneInstr
, OpCodes(Mips::LBu
, Mips::LBU16_MM
), ReduceLXUtoLXU16
,
230 OpInfo(OT_OperandsAll
), ImmField(0, -1, 15, 2)},
231 {RT_OneInstr
, OpCodes(Mips::LBu_MM
, Mips::LBU16_MM
), ReduceLXUtoLXU16
,
232 OpInfo(OT_OperandsAll
), ImmField(0, -1, 15, 2)},
233 {RT_OneInstr
, OpCodes(Mips::LEA_ADDiu
, Mips::ADDIUR1SP_MM
),
234 ReduceADDIUToADDIUR1SP
, OpInfo(OT_Operands02
), ImmField(2, 0, 64, 2)},
235 {RT_OneInstr
, OpCodes(Mips::LEA_ADDiu_MM
, Mips::ADDIUR1SP_MM
),
236 ReduceADDIUToADDIUR1SP
, OpInfo(OT_Operands02
), ImmField(2, 0, 64, 2)},
237 {RT_OneInstr
, OpCodes(Mips::LHu
, Mips::LHU16_MM
), ReduceLXUtoLXU16
,
238 OpInfo(OT_OperandsAll
), ImmField(1, 0, 16, 2)},
239 {RT_OneInstr
, OpCodes(Mips::LHu_MM
, Mips::LHU16_MM
), ReduceLXUtoLXU16
,
240 OpInfo(OT_OperandsAll
), ImmField(1, 0, 16, 2)},
241 {RT_TwoInstr
, OpCodes(Mips::LW
, Mips::LWP_MM
), ReduceXWtoXWP
,
242 OpInfo(OT_OperandsLwp
), ImmField(0, -2048, 2048, 2)},
243 {RT_OneInstr
, OpCodes(Mips::LW
, Mips::LWSP_MM
), ReduceXWtoXWSP
,
244 OpInfo(OT_OperandsAll
), ImmField(2, 0, 32, 2)},
245 {RT_TwoInstr
, OpCodes(Mips::LW16_MM
, Mips::LWP_MM
), ReduceXWtoXWP
,
246 OpInfo(OT_OperandsLwp
), ImmField(0, -2048, 2048, 2)},
247 {RT_TwoInstr
, OpCodes(Mips::LW_MM
, Mips::LWP_MM
), ReduceXWtoXWP
,
248 OpInfo(OT_OperandsLwp
), ImmField(0, -2048, 2048, 2)},
249 {RT_OneInstr
, OpCodes(Mips::LW_MM
, Mips::LWSP_MM
), ReduceXWtoXWSP
,
250 OpInfo(OT_OperandsAll
), ImmField(2, 0, 32, 2)},
251 {RT_TwoInstr
, OpCodes(Mips::MOVE16_MM
, Mips::MOVEP_MM
), ReduceMoveToMovep
,
252 OpInfo(OT_OperandsMovep
), ImmField(0, 0, 0, -1)},
253 {RT_OneInstr
, OpCodes(Mips::SB
, Mips::SB16_MM
), ReduceSXtoSX16
,
254 OpInfo(OT_OperandsAll
), ImmField(0, 0, 16, 2)},
255 {RT_OneInstr
, OpCodes(Mips::SB_MM
, Mips::SB16_MM
), ReduceSXtoSX16
,
256 OpInfo(OT_OperandsAll
), ImmField(0, 0, 16, 2)},
257 {RT_OneInstr
, OpCodes(Mips::SH
, Mips::SH16_MM
), ReduceSXtoSX16
,
258 OpInfo(OT_OperandsAll
), ImmField(1, 0, 16, 2)},
259 {RT_OneInstr
, OpCodes(Mips::SH_MM
, Mips::SH16_MM
), ReduceSXtoSX16
,
260 OpInfo(OT_OperandsAll
), ImmField(1, 0, 16, 2)},
261 {RT_OneInstr
, OpCodes(Mips::SUBu
, Mips::SUBU16_MM
),
262 ReduceArithmeticInstructions
, OpInfo(OT_OperandsAll
),
263 ImmField(0, 0, 0, -1)},
264 {RT_OneInstr
, OpCodes(Mips::SUBu_MM
, Mips::SUBU16_MM
),
265 ReduceArithmeticInstructions
, OpInfo(OT_OperandsAll
),
266 ImmField(0, 0, 0, -1)},
267 {RT_TwoInstr
, OpCodes(Mips::SW
, Mips::SWP_MM
), ReduceXWtoXWP
,
268 OpInfo(OT_OperandsSwp
), ImmField(0, -2048, 2048, 2)},
269 {RT_OneInstr
, OpCodes(Mips::SW
, Mips::SWSP_MM
), ReduceXWtoXWSP
,
270 OpInfo(OT_OperandsAll
), ImmField(2, 0, 32, 2)},
271 {RT_TwoInstr
, OpCodes(Mips::SW16_MM
, Mips::SWP_MM
), ReduceXWtoXWP
,
272 OpInfo(OT_OperandsSwp
), ImmField(0, -2048, 2048, 2)},
273 {RT_TwoInstr
, OpCodes(Mips::SW_MM
, Mips::SWP_MM
), ReduceXWtoXWP
,
274 OpInfo(OT_OperandsSwp
), ImmField(0, -2048, 2048, 2)},
275 {RT_OneInstr
, OpCodes(Mips::SW_MM
, Mips::SWSP_MM
), ReduceXWtoXWSP
,
276 OpInfo(OT_OperandsAll
), ImmField(2, 0, 32, 2)},
277 {RT_OneInstr
, OpCodes(Mips::XOR
, Mips::XOR16_MM
), ReduceXORtoXOR16
,
278 OpInfo(OT_OperandsXOR
), ImmField(0, 0, 0, -1)},
279 {RT_OneInstr
, OpCodes(Mips::XOR_MM
, Mips::XOR16_MM
), ReduceXORtoXOR16
,
280 OpInfo(OT_OperandsXOR
), ImmField(0, 0, 0, -1)}};
281 } // end anonymous namespace
283 INITIALIZE_PASS(MicroMipsSizeReduce
, DEBUG_TYPE
, MICROMIPS_SIZE_REDUCE_NAME
,
286 // Returns true if the machine operand MO is register SP.
287 static bool IsSP(const MachineOperand
&MO
) {
288 if (MO
.isReg() && ((MO
.getReg() == Mips::SP
)))
293 // Returns true if the machine operand MO is register $16, $17, or $2-$7.
294 static bool isMMThreeBitGPRegister(const MachineOperand
&MO
) {
295 if (MO
.isReg() && Mips::GPRMM16RegClass
.contains(MO
.getReg()))
300 // Returns true if the machine operand MO is register $0, $17, or $2-$7.
301 static bool isMMSourceRegister(const MachineOperand
&MO
) {
302 if (MO
.isReg() && Mips::GPRMM16ZeroRegClass
.contains(MO
.getReg()))
307 // Returns true if the operand Op is an immediate value
308 // and writes the immediate value into variable Imm.
309 static bool GetImm(MachineInstr
*MI
, unsigned Op
, int64_t &Imm
) {
311 if (!MI
->getOperand(Op
).isImm())
313 Imm
= MI
->getOperand(Op
).getImm();
317 // Returns true if the value is a valid immediate for ADDIUSP.
318 static bool AddiuspImmValue(int64_t Value
) {
319 int64_t Value2
= Value
>> 2;
320 if (((Value
& (int64_t)maskTrailingZeros
<uint64_t>(2)) == Value
) &&
321 ((Value2
>= 2 && Value2
<= 257) || (Value2
>= -258 && Value2
<= -3)))
326 // Returns true if the variable Value has the number of least-significant zero
327 // bits equal to Shift and if the shifted value is between the bounds.
328 static bool InRange(int64_t Value
, unsigned short Shift
, int LBound
,
330 int64_t Value2
= Value
>> Shift
;
331 if (((Value
& (int64_t)maskTrailingZeros
<uint64_t>(Shift
)) == Value
) &&
332 (Value2
>= LBound
) && (Value2
< HBound
))
337 // Returns true if immediate operand is in range.
338 static bool ImmInRange(MachineInstr
*MI
, const ReduceEntry
&Entry
) {
342 if (!GetImm(MI
, Entry
.ImmField(), offset
))
345 if (!InRange(offset
, Entry
.Shift(), Entry
.LBound(), Entry
.HBound()))
351 // Returns true if MI can be reduced to lwp/swp instruction
352 static bool CheckXWPInstr(MachineInstr
*MI
, bool ReduceToLwp
,
353 const ReduceEntry
&Entry
) {
356 !(MI
->getOpcode() == Mips::LW
|| MI
->getOpcode() == Mips::LW_MM
||
357 MI
->getOpcode() == Mips::LW16_MM
))
361 !(MI
->getOpcode() == Mips::SW
|| MI
->getOpcode() == Mips::SW_MM
||
362 MI
->getOpcode() == Mips::SW16_MM
))
365 unsigned reg
= MI
->getOperand(0).getReg();
369 if (!ImmInRange(MI
, Entry
))
372 if (ReduceToLwp
&& (MI
->getOperand(0).getReg() == MI
->getOperand(1).getReg()))
378 // Returns true if the registers Reg1 and Reg2 are consecutive
379 static bool ConsecutiveRegisters(unsigned Reg1
, unsigned Reg2
) {
380 static SmallVector
<unsigned, 31> Registers
= {
381 Mips::AT
, Mips::V0
, Mips::V1
, Mips::A0
, Mips::A1
, Mips::A2
, Mips::A3
,
382 Mips::T0
, Mips::T1
, Mips::T2
, Mips::T3
, Mips::T4
, Mips::T5
, Mips::T6
,
383 Mips::T7
, Mips::S0
, Mips::S1
, Mips::S2
, Mips::S3
, Mips::S4
, Mips::S5
,
384 Mips::S6
, Mips::S7
, Mips::T8
, Mips::T9
, Mips::K0
, Mips::K1
, Mips::GP
,
385 Mips::SP
, Mips::FP
, Mips::RA
};
387 for (uint8_t i
= 0; i
< Registers
.size() - 1; i
++) {
388 if (Registers
[i
] == Reg1
) {
389 if (Registers
[i
+ 1] == Reg2
)
398 // Returns true if registers and offsets are consecutive
399 static bool ConsecutiveInstr(MachineInstr
*MI1
, MachineInstr
*MI2
) {
401 int64_t Offset1
, Offset2
;
402 if (!GetImm(MI1
, 2, Offset1
))
404 if (!GetImm(MI2
, 2, Offset2
))
407 unsigned Reg1
= MI1
->getOperand(0).getReg();
408 unsigned Reg2
= MI2
->getOperand(0).getReg();
410 return ((Offset1
== (Offset2
- 4)) && (ConsecutiveRegisters(Reg1
, Reg2
)));
413 MicroMipsSizeReduce::MicroMipsSizeReduce() : MachineFunctionPass(ID
) {}
415 bool MicroMipsSizeReduce::ReduceMI(const MachineBasicBlock::instr_iterator
&MII
,
416 MachineBasicBlock::instr_iterator
&NextMII
) {
418 MachineInstr
*MI
= &*MII
;
419 unsigned Opcode
= MI
->getOpcode();
422 ReduceEntryVector::const_iterator Start
= std::begin(ReduceTable
);
423 ReduceEntryVector::const_iterator End
= std::end(ReduceTable
);
425 std::pair
<ReduceEntryVector::const_iterator
,
426 ReduceEntryVector::const_iterator
>
427 Range
= std::equal_range(Start
, End
, Opcode
);
429 if (Range
.first
== Range
.second
)
432 for (ReduceEntryVector::const_iterator Entry
= Range
.first
;
433 Entry
!= Range
.second
; ++Entry
) {
434 ReduceEntryFunArgs
Arguments(&(*MII
), *Entry
, NextMII
);
435 if (((*Entry
).ReduceFunction
)(&Arguments
))
441 bool MicroMipsSizeReduce::ReduceXWtoXWSP(ReduceEntryFunArgs
*Arguments
) {
443 MachineInstr
*MI
= Arguments
->MI
;
444 const ReduceEntry
&Entry
= Arguments
->Entry
;
446 if (!ImmInRange(MI
, Entry
))
449 if (!IsSP(MI
->getOperand(1)))
452 return ReplaceInstruction(MI
, Entry
);
455 bool MicroMipsSizeReduce::ReduceXWtoXWP(ReduceEntryFunArgs
*Arguments
) {
457 const ReduceEntry
&Entry
= Arguments
->Entry
;
458 MachineBasicBlock::instr_iterator
&NextMII
= Arguments
->NextMII
;
459 const MachineBasicBlock::instr_iterator
&E
=
460 Arguments
->MI
->getParent()->instr_end();
465 MachineInstr
*MI1
= Arguments
->MI
;
466 MachineInstr
*MI2
= &*NextMII
;
468 // ReduceToLwp = true/false - reduce to LWP/SWP instruction
469 bool ReduceToLwp
= (MI1
->getOpcode() == Mips::LW
) ||
470 (MI1
->getOpcode() == Mips::LW_MM
) ||
471 (MI1
->getOpcode() == Mips::LW16_MM
);
473 if (!CheckXWPInstr(MI1
, ReduceToLwp
, Entry
))
476 if (!CheckXWPInstr(MI2
, ReduceToLwp
, Entry
))
479 unsigned Reg1
= MI1
->getOperand(1).getReg();
480 unsigned Reg2
= MI2
->getOperand(1).getReg();
485 bool ConsecutiveForward
= ConsecutiveInstr(MI1
, MI2
);
486 bool ConsecutiveBackward
= ConsecutiveInstr(MI2
, MI1
);
488 if (!(ConsecutiveForward
|| ConsecutiveBackward
))
491 NextMII
= std::next(NextMII
);
492 return ReplaceInstruction(MI1
, Entry
, MI2
, ConsecutiveForward
);
495 bool MicroMipsSizeReduce::ReduceArithmeticInstructions(
496 ReduceEntryFunArgs
*Arguments
) {
498 MachineInstr
*MI
= Arguments
->MI
;
499 const ReduceEntry
&Entry
= Arguments
->Entry
;
501 if (!isMMThreeBitGPRegister(MI
->getOperand(0)) ||
502 !isMMThreeBitGPRegister(MI
->getOperand(1)) ||
503 !isMMThreeBitGPRegister(MI
->getOperand(2)))
506 return ReplaceInstruction(MI
, Entry
);
509 bool MicroMipsSizeReduce::ReduceADDIUToADDIUR1SP(
510 ReduceEntryFunArgs
*Arguments
) {
512 MachineInstr
*MI
= Arguments
->MI
;
513 const ReduceEntry
&Entry
= Arguments
->Entry
;
515 if (!ImmInRange(MI
, Entry
))
518 if (!isMMThreeBitGPRegister(MI
->getOperand(0)) || !IsSP(MI
->getOperand(1)))
521 return ReplaceInstruction(MI
, Entry
);
524 bool MicroMipsSizeReduce::ReduceADDIUToADDIUSP(ReduceEntryFunArgs
*Arguments
) {
526 MachineInstr
*MI
= Arguments
->MI
;
527 const ReduceEntry
&Entry
= Arguments
->Entry
;
530 if (!GetImm(MI
, Entry
.ImmField(), ImmValue
))
533 if (!AddiuspImmValue(ImmValue
))
536 if (!IsSP(MI
->getOperand(0)) || !IsSP(MI
->getOperand(1)))
539 return ReplaceInstruction(MI
, Entry
);
542 bool MicroMipsSizeReduce::ReduceLXUtoLXU16(ReduceEntryFunArgs
*Arguments
) {
544 MachineInstr
*MI
= Arguments
->MI
;
545 const ReduceEntry
&Entry
= Arguments
->Entry
;
547 if (!ImmInRange(MI
, Entry
))
550 if (!isMMThreeBitGPRegister(MI
->getOperand(0)) ||
551 !isMMThreeBitGPRegister(MI
->getOperand(1)))
554 return ReplaceInstruction(MI
, Entry
);
557 bool MicroMipsSizeReduce::ReduceSXtoSX16(ReduceEntryFunArgs
*Arguments
) {
559 MachineInstr
*MI
= Arguments
->MI
;
560 const ReduceEntry
&Entry
= Arguments
->Entry
;
562 if (!ImmInRange(MI
, Entry
))
565 if (!isMMSourceRegister(MI
->getOperand(0)) ||
566 !isMMThreeBitGPRegister(MI
->getOperand(1)))
569 return ReplaceInstruction(MI
, Entry
);
572 // Returns true if Reg can be a source register
573 // of MOVEP instruction
574 static bool IsMovepSrcRegister(unsigned Reg
) {
576 if (Reg
== Mips::ZERO
|| Reg
== Mips::V0
|| Reg
== Mips::V1
||
577 Reg
== Mips::S0
|| Reg
== Mips::S1
|| Reg
== Mips::S2
||
578 Reg
== Mips::S3
|| Reg
== Mips::S4
)
584 // Returns true if Reg can be a destination register
585 // of MOVEP instruction
586 static bool IsMovepDestinationReg(unsigned Reg
) {
588 if (Reg
== Mips::A0
|| Reg
== Mips::A1
|| Reg
== Mips::A2
||
589 Reg
== Mips::A3
|| Reg
== Mips::S5
|| Reg
== Mips::S6
)
595 // Returns true if the registers can be a pair of destination
596 // registers in MOVEP instruction
597 static bool IsMovepDestinationRegPair(unsigned R0
, unsigned R1
) {
599 if ((R0
== Mips::A0
&& R1
== Mips::S5
) ||
600 (R0
== Mips::A0
&& R1
== Mips::S6
) ||
601 (R0
== Mips::A0
&& R1
== Mips::A1
) ||
602 (R0
== Mips::A0
&& R1
== Mips::A2
) ||
603 (R0
== Mips::A0
&& R1
== Mips::A3
) ||
604 (R0
== Mips::A1
&& R1
== Mips::A2
) ||
605 (R0
== Mips::A1
&& R1
== Mips::A3
) ||
606 (R0
== Mips::A2
&& R1
== Mips::A3
))
612 bool MicroMipsSizeReduce::ReduceMoveToMovep(ReduceEntryFunArgs
*Arguments
) {
614 const ReduceEntry
&Entry
= Arguments
->Entry
;
615 MachineBasicBlock::instr_iterator
&NextMII
= Arguments
->NextMII
;
616 const MachineBasicBlock::instr_iterator
&E
=
617 Arguments
->MI
->getParent()->instr_end();
622 MachineInstr
*MI1
= Arguments
->MI
;
623 MachineInstr
*MI2
= &*NextMII
;
625 unsigned RegDstMI1
= MI1
->getOperand(0).getReg();
626 unsigned RegSrcMI1
= MI1
->getOperand(1).getReg();
628 if (!IsMovepSrcRegister(RegSrcMI1
))
631 if (!IsMovepDestinationReg(RegDstMI1
))
634 if (MI2
->getOpcode() != Entry
.WideOpc())
637 unsigned RegDstMI2
= MI2
->getOperand(0).getReg();
638 unsigned RegSrcMI2
= MI2
->getOperand(1).getReg();
640 if (!IsMovepSrcRegister(RegSrcMI2
))
643 bool ConsecutiveForward
;
644 if (IsMovepDestinationRegPair(RegDstMI1
, RegDstMI2
)) {
645 ConsecutiveForward
= true;
646 } else if (IsMovepDestinationRegPair(RegDstMI2
, RegDstMI1
)) {
647 ConsecutiveForward
= false;
651 NextMII
= std::next(NextMII
);
652 return ReplaceInstruction(MI1
, Entry
, MI2
, ConsecutiveForward
);
655 bool MicroMipsSizeReduce::ReduceXORtoXOR16(ReduceEntryFunArgs
*Arguments
) {
657 MachineInstr
*MI
= Arguments
->MI
;
658 const ReduceEntry
&Entry
= Arguments
->Entry
;
660 if (!isMMThreeBitGPRegister(MI
->getOperand(0)) ||
661 !isMMThreeBitGPRegister(MI
->getOperand(1)) ||
662 !isMMThreeBitGPRegister(MI
->getOperand(2)))
665 if (!(MI
->getOperand(0).getReg() == MI
->getOperand(2).getReg()) &&
666 !(MI
->getOperand(0).getReg() == MI
->getOperand(1).getReg()))
669 return ReplaceInstruction(MI
, Entry
);
672 bool MicroMipsSizeReduce::ReduceMBB(MachineBasicBlock
&MBB
) {
673 bool Modified
= false;
674 MachineBasicBlock::instr_iterator MII
= MBB
.instr_begin(),
676 MachineBasicBlock::instr_iterator NextMII
;
678 // Iterate through the instructions in the basic block
679 for (; MII
!= E
; MII
= NextMII
) {
680 NextMII
= std::next(MII
);
681 MachineInstr
*MI
= &*MII
;
683 // Don't reduce bundled instructions or pseudo operations
684 if (MI
->isBundle() || MI
->isTransient())
687 // Try to reduce 32-bit instruction into 16-bit instruction
688 Modified
|= ReduceMI(MII
, NextMII
);
694 bool MicroMipsSizeReduce::ReplaceInstruction(MachineInstr
*MI
,
695 const ReduceEntry
&Entry
,
697 bool ConsecutiveForward
) {
699 enum OperandTransfer OpTransfer
= Entry
.TransferOperands();
701 LLVM_DEBUG(dbgs() << "Converting 32-bit: " << *MI
);
704 if (OpTransfer
== OT_OperandsAll
) {
705 MI
->setDesc(MipsII
->get(Entry
.NarrowOpc()));
706 LLVM_DEBUG(dbgs() << " to 16-bit: " << *MI
);
709 MachineBasicBlock
&MBB
= *MI
->getParent();
710 const MCInstrDesc
&NewMCID
= MipsII
->get(Entry
.NarrowOpc());
711 DebugLoc dl
= MI
->getDebugLoc();
712 MachineInstrBuilder MIB
= BuildMI(MBB
, MI
, dl
, NewMCID
);
713 switch (OpTransfer
) {
715 MIB
.add(MI
->getOperand(2));
717 case OT_Operands02
: {
718 MIB
.add(MI
->getOperand(0));
719 MIB
.add(MI
->getOperand(2));
722 case OT_OperandsXOR
: {
723 if (MI
->getOperand(0).getReg() == MI
->getOperand(2).getReg()) {
724 MIB
.add(MI
->getOperand(0));
725 MIB
.add(MI
->getOperand(1));
726 MIB
.add(MI
->getOperand(2));
728 MIB
.add(MI
->getOperand(0));
729 MIB
.add(MI
->getOperand(2));
730 MIB
.add(MI
->getOperand(1));
734 case OT_OperandsMovep
:
736 case OT_OperandsSwp
: {
737 if (ConsecutiveForward
) {
738 MIB
.add(MI
->getOperand(0));
739 MIB
.add(MI2
->getOperand(0));
740 MIB
.add(MI
->getOperand(1));
741 if (OpTransfer
== OT_OperandsMovep
)
742 MIB
.add(MI2
->getOperand(1));
744 MIB
.add(MI
->getOperand(2));
745 } else { // consecutive backward
746 MIB
.add(MI2
->getOperand(0));
747 MIB
.add(MI
->getOperand(0));
748 MIB
.add(MI2
->getOperand(1));
749 if (OpTransfer
== OT_OperandsMovep
)
750 MIB
.add(MI
->getOperand(1));
752 MIB
.add(MI2
->getOperand(2));
755 LLVM_DEBUG(dbgs() << "and converting 32-bit: " << *MI2
759 MBB
.erase_instr(MI2
);
763 llvm_unreachable("Unknown operand transfer!");
766 // Transfer MI flags.
767 MIB
.setMIFlags(MI
->getFlags());
769 LLVM_DEBUG(dbgs() << " to 16-bit: " << *MIB
);
776 bool MicroMipsSizeReduce::runOnMachineFunction(MachineFunction
&MF
) {
778 Subtarget
= &static_cast<const MipsSubtarget
&>(MF
.getSubtarget());
780 // TODO: Add support for the subtarget microMIPS32R6.
781 if (!Subtarget
->inMicroMipsMode() || !Subtarget
->hasMips32r2() ||
782 Subtarget
->hasMips32r6())
785 MipsII
= static_cast<const MipsInstrInfo
*>(Subtarget
->getInstrInfo());
787 bool Modified
= false;
788 MachineFunction::iterator I
= MF
.begin(), E
= MF
.end();
791 Modified
|= ReduceMBB(*I
);
795 /// Returns an instance of the MicroMips size reduction pass.
796 FunctionPass
*llvm::createMicroMipsSizeReducePass() {
797 return new MicroMipsSizeReduce();