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");
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;
67 : TransferOperands(TransferOperands) {}
68 OpInfo() : TransferOperands(OT_NA) {}
76 OpCodes(
unsigned WideOpc,
unsigned NarrowOpc)
77 : WideOpc(WideOpc), NarrowOpc(NarrowOpc) {}
83 typedef struct ReduceEntryFunArgs ReduceEntryFunArgs;
90 bool (*ReduceFunction)(
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; }
108 return OpInf.TransferOperands;
110 enum ReduceType RType()
const {
return eRType; }
113 bool operator<(
const unsigned int r)
const {
return (WideOpc() < r); }
116 friend bool operator<(
const unsigned int r,
const struct ReduceEntry &re) {
117 return (r < re.WideOpc());
122 struct ReduceEntryFunArgs {
124 const ReduceEntry &Entry;
128 ReduceEntryFunArgs(
MachineInstr *argMI,
const ReduceEntry &argEntry,
130 :
MI(argMI), Entry(argEntry), NextMII(argNextMII) {}
138 MicroMipsSizeReduce();
146 return "microMIPS instruction size reduction pass";
159 static bool ReduceXWtoXWSP(ReduceEntryFunArgs *
Arguments);
163 static bool ReduceXWtoXWP(ReduceEntryFunArgs *Arguments);
167 static bool ReduceLXUtoLXU16(ReduceEntryFunArgs *Arguments);
171 static bool ReduceSXtoSX16(ReduceEntryFunArgs *Arguments);
174 static bool ReduceArithmeticInstructions(ReduceEntryFunArgs *Arguments);
178 static bool ReduceADDIUToADDIUSP(ReduceEntryFunArgs *Arguments);
182 static bool ReduceADDIUToADDIUR1SP(ReduceEntryFunArgs *Arguments);
186 static bool ReduceXORtoXOR16(ReduceEntryFunArgs *Arguments);
192 static bool ReplaceInstruction(
MachineInstr *
MI,
const ReduceEntry &Entry,
194 bool ConsecutiveForward =
true);
197 static ReduceEntryVector ReduceTable;
205 ReduceEntryVector MicroMipsSizeReduce::ReduceTable = {
210 {RT_OneInstr, OpCodes(Mips::ADDiu, Mips::ADDIUR1SP_MM),
211 ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
212 {RT_OneInstr, OpCodes(Mips::ADDiu, Mips::ADDIUSP_MM), ReduceADDIUToADDIUSP,
213 OpInfo(OT_Operand2), ImmField(0, 0, 0, 2)},
214 {RT_OneInstr, OpCodes(Mips::ADDiu_MM, Mips::ADDIUR1SP_MM),
215 ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
216 {RT_OneInstr, OpCodes(Mips::ADDiu_MM, Mips::ADDIUSP_MM),
217 ReduceADDIUToADDIUSP, OpInfo(OT_Operand2), ImmField(0, 0, 0, 2)},
218 {RT_OneInstr, OpCodes(Mips::ADDu, Mips::ADDU16_MM),
219 ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
220 ImmField(0, 0, 0, -1)},
221 {RT_OneInstr, OpCodes(Mips::ADDu_MM, Mips::ADDU16_MM),
222 ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
223 ImmField(0, 0, 0, -1)},
224 {RT_OneInstr, OpCodes(Mips::LBu, Mips::LBU16_MM), ReduceLXUtoLXU16,
225 OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},
226 {RT_OneInstr, OpCodes(Mips::LBu_MM, Mips::LBU16_MM), ReduceLXUtoLXU16,
227 OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},
228 {RT_OneInstr, OpCodes(Mips::LEA_ADDiu, Mips::ADDIUR1SP_MM),
229 ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
230 {RT_OneInstr, OpCodes(Mips::LEA_ADDiu_MM, Mips::ADDIUR1SP_MM),
231 ReduceADDIUToADDIUR1SP, OpInfo(OT_Operands02), ImmField(2, 0, 64, 2)},
232 {RT_OneInstr, OpCodes(Mips::LHu, Mips::LHU16_MM), ReduceLXUtoLXU16,
233 OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
234 {RT_OneInstr, OpCodes(Mips::LHu_MM, Mips::LHU16_MM), ReduceLXUtoLXU16,
235 OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
236 {RT_TwoInstr, OpCodes(Mips::LW, Mips::LWP_MM), ReduceXWtoXWP,
237 OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},
238 {RT_OneInstr, OpCodes(Mips::LW, Mips::LWSP_MM), ReduceXWtoXWSP,
239 OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
240 {RT_TwoInstr, OpCodes(Mips::LW16_MM, Mips::LWP_MM), ReduceXWtoXWP,
241 OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},
242 {RT_TwoInstr, OpCodes(Mips::LW_MM, Mips::LWP_MM), ReduceXWtoXWP,
243 OpInfo(OT_OperandsLwp), ImmField(0, -2048, 2048, 2)},
244 {RT_OneInstr, OpCodes(Mips::LW_MM, Mips::LWSP_MM), ReduceXWtoXWSP,
245 OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
246 {RT_OneInstr, OpCodes(Mips::SB, Mips::SB16_MM), ReduceSXtoSX16,
247 OpInfo(OT_OperandsAll), ImmField(0, 0, 16, 2)},
248 {RT_OneInstr, OpCodes(Mips::SB_MM, Mips::SB16_MM), ReduceSXtoSX16,
249 OpInfo(OT_OperandsAll), ImmField(0, 0, 16, 2)},
250 {RT_OneInstr, OpCodes(Mips::SH, Mips::SH16_MM), ReduceSXtoSX16,
251 OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
252 {RT_OneInstr, OpCodes(Mips::SH_MM, Mips::SH16_MM), ReduceSXtoSX16,
253 OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
254 {RT_OneInstr, OpCodes(Mips::SUBu, Mips::SUBU16_MM),
255 ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
256 ImmField(0, 0, 0, -1)},
257 {RT_OneInstr, OpCodes(Mips::SUBu_MM, Mips::SUBU16_MM),
258 ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
259 ImmField(0, 0, 0, -1)},
260 {RT_TwoInstr, OpCodes(Mips::SW, Mips::SWP_MM), ReduceXWtoXWP,
261 OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},
262 {RT_OneInstr, OpCodes(Mips::SW, Mips::SWSP_MM), ReduceXWtoXWSP,
263 OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
264 {RT_TwoInstr, OpCodes(Mips::SW16_MM, Mips::SWP_MM), ReduceXWtoXWP,
265 OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},
266 {RT_TwoInstr, OpCodes(Mips::SW_MM, Mips::SWP_MM), ReduceXWtoXWP,
267 OpInfo(OT_OperandsSwp), ImmField(0, -2048, 2048, 2)},
268 {RT_OneInstr, OpCodes(Mips::SW_MM, Mips::SWSP_MM), ReduceXWtoXWSP,
269 OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
270 {RT_OneInstr, OpCodes(
Mips::XOR, Mips::XOR16_MM), ReduceXORtoXOR16,
271 OpInfo(OT_OperandsXOR), ImmField(0, 0, 0, -1)},
272 {RT_OneInstr, OpCodes(Mips::XOR_MM, Mips::XOR16_MM), ReduceXORtoXOR16,
273 OpInfo(OT_OperandsXOR), ImmField(0, 0, 0, -1)}};
281 if (MO.isReg() && ((MO.getReg() == Mips::SP)))
288 if (MO.
isReg() && Mips::GPRMM16RegClass.contains(MO.
getReg()))
295 if (MO.
isReg() && Mips::GPRMM16ZeroRegClass.contains(MO.
getReg()))
312 int64_t Value2 = Value >> 2;
313 if (((Value & (int64_t)maskTrailingZeros<uint64_t>(2)) == Value) &&
314 ((Value2 >= 2 && Value2 <= 257) || (Value2 >= -258 && Value2 <= -3)))
323 int64_t Value2 = Value >> Shift;
324 if (((Value & (int64_t)maskTrailingZeros<uint64_t>(Shift)) == Value) &&
325 (Value2 >= LBound) && (Value2 < HBound))
335 if (!
GetImm(MI, Entry.ImmField(), offset))
338 if (!
InRange(offset, Entry.Shift(), Entry.LBound(), Entry.HBound()))
346 const ReduceEntry &Entry) {
374 Mips::AT, Mips::V0, Mips::V1, Mips::A0, Mips::A1, Mips::A2, Mips::A3,
375 Mips::T0,
Mips::T1, Mips::T2, Mips::T3, Mips::T4, Mips::T5, Mips::T6,
376 Mips::T7, Mips::S0, Mips::S1, Mips::S2, Mips::S3, Mips::S4, Mips::S5,
377 Mips::S6, Mips::S7,
Mips::T8, Mips::T9, Mips::K0, Mips::K1, Mips::GP,
380 for (uint8_t i = 0; i < Registers.
size() - 1; i++) {
381 if (Registers[i] == Reg1) {
382 if (Registers[i + 1] == Reg2)
394 int64_t Offset1, Offset2;
395 if (!
GetImm(MI1, 2, Offset1))
397 if (!
GetImm(MI2, 2, Offset2))
415 ReduceEntryVector::const_iterator Start =
std::begin(ReduceTable);
416 ReduceEntryVector::const_iterator End =
std::end(ReduceTable);
418 std::pair<ReduceEntryVector::const_iterator,
419 ReduceEntryVector::const_iterator>
420 Range = std::equal_range(Start, End, Opcode);
422 if (Range.first == Range.second)
425 for (ReduceEntryVector::const_iterator Entry = Range.first;
426 Entry != Range.second; ++Entry) {
427 ReduceEntryFunArgs
Arguments(&(*MII), *Entry, NextMII);
428 if (((*Entry).ReduceFunction)(&Arguments))
434 bool MicroMipsSizeReduce::ReduceXWtoXWSP(ReduceEntryFunArgs *
Arguments) {
437 const ReduceEntry &Entry = Arguments->Entry;
445 return ReplaceInstruction(MI, Entry);
448 bool MicroMipsSizeReduce::ReduceXWtoXWP(ReduceEntryFunArgs *Arguments) {
450 const ReduceEntry &Entry = Arguments->Entry;
453 Arguments->MI->getParent()->instr_end();
462 bool ReduceToLwp = (MI1->
getOpcode() == Mips::LW) ||
481 if (!(ConsecutiveForward || ConsecutiveBackward))
484 NextMII = std::next(NextMII);
485 return ReplaceInstruction(MI1, Entry, MI2, ConsecutiveForward);
488 bool MicroMipsSizeReduce::ReduceArithmeticInstructions(
489 ReduceEntryFunArgs *Arguments) {
492 const ReduceEntry &Entry = Arguments->Entry;
499 return ReplaceInstruction(MI, Entry);
502 bool MicroMipsSizeReduce::ReduceADDIUToADDIUR1SP(
503 ReduceEntryFunArgs *Arguments) {
506 const ReduceEntry &Entry = Arguments->Entry;
514 return ReplaceInstruction(MI, Entry);
517 bool MicroMipsSizeReduce::ReduceADDIUToADDIUSP(ReduceEntryFunArgs *Arguments) {
520 const ReduceEntry &Entry = Arguments->Entry;
523 if (!
GetImm(MI, Entry.ImmField(), ImmValue))
532 return ReplaceInstruction(MI, Entry);
535 bool MicroMipsSizeReduce::ReduceLXUtoLXU16(ReduceEntryFunArgs *Arguments) {
538 const ReduceEntry &Entry = Arguments->Entry;
547 return ReplaceInstruction(MI, Entry);
550 bool MicroMipsSizeReduce::ReduceSXtoSX16(ReduceEntryFunArgs *Arguments) {
553 const ReduceEntry &Entry = Arguments->Entry;
562 return ReplaceInstruction(MI, Entry);
565 bool MicroMipsSizeReduce::ReduceXORtoXOR16(ReduceEntryFunArgs *Arguments) {
568 const ReduceEntry &Entry = Arguments->Entry;
579 return ReplaceInstruction(MI, Entry);
589 for (; MII !=
E; MII = NextMII) {
590 NextMII = std::next(MII);
598 Modified |= ReduceMI(MII, NextMII);
605 const ReduceEntry &Entry,
607 bool ConsecutiveForward) {
614 if (OpTransfer == OT_OperandsAll) {
615 MI->
setDesc(MipsII->get(Entry.NarrowOpc()));
620 const MCInstrDesc &NewMCID = MipsII->get(Entry.NarrowOpc());
623 switch (OpTransfer) {
627 case OT_Operands02: {
632 case OT_OperandsXOR: {
645 case OT_OperandsSwp: {
646 if (ConsecutiveForward) {
684 if (!Subtarget->inMicroMipsMode() || !Subtarget->hasMips32r2() ||
685 Subtarget->hasMips32r6())
688 MipsII =
static_cast<const MipsInstrInfo *
>(Subtarget->getInstrInfo());
694 Modified |= ReduceMBB(*I);
700 return new MicroMipsSizeReduce();
const MachineInstrBuilder & add(const MachineOperand &MO) const
const_iterator end(StringRef path)
Get end iterator over path.
instr_iterator instr_begin()
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
instr_iterator instr_end()
OperandTransfer
Order of operands to transfer.
Compute iterated dominance frontiers using a linear time algorithm.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Describe properties that are true of each instruction in the target description file.
unsigned getReg() const
getReg - Returns the register number.
bool isTransient() const
Return true if this is a transient instruction that is either very likely to be eliminated during reg...
STATISTIC(NumFunctions, "Total number of functions")
INITIALIZE_PASS(MicroMipsSizeReduce, DEBUG_TYPE, MICROMIPS_SIZE_REDUCE_NAME, false, false) static bool IsSP(const MachineOperand &MO)
static bool ImmInRange(MachineInstr *MI, const ReduceEntry &Entry)
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
static bool ConsecutiveInstr(MachineInstr *MI1, MachineInstr *MI2)
SI optimize exec mask operations pre RA
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
static bool GetImm(MachineInstr *MI, unsigned Op, int64_t &Imm)
static bool AddiuspImmValue(int64_t Value)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
static bool ConsecutiveRegisters(unsigned Reg1, unsigned Reg2)
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
instr_iterator erase_instr(MachineInstr *I)
Remove an instruction from the instruction list and delete it.
#define MICROMIPS_SIZE_REDUCE_NAME
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
AMDGPU Lower Kernel Arguments
FunctionPass class - This class is used to implement most global optimizations.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static bool isMMSourceRegister(const MachineOperand &MO)
Iterator for intrusive lists based on ilist_node.
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
FunctionPass * createMicroMipsSizeReducePass()
Returns an instance of the MicroMips size reduction pass.
const MachineBasicBlock * getParent() const
Representation of each machine instruction.
ReduceType
Reduction type.
const MachineInstrBuilder & setMIFlags(unsigned Flags) const
static bool isMMThreeBitGPRegister(const MachineOperand &MO)
static bool CheckXWPInstr(MachineInstr *MI, bool ReduceToLwp, const ReduceEntry &Entry)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
uint16_t getFlags() const
Return the MI flags bitvector.
bool operator<(int64_t V1, const APSInt &V2)
LLVM Value Representation.
StringRef - Represent a constant reference to a string, i.e.
const MachineOperand & getOperand(unsigned i) const