24 #define DEBUG_TYPE "micromips-reduce-size"
26 STATISTIC(NumReduced,
"Number of 32-bit instructions reduced to 16-bit ones");
45 ImmField() : ImmFieldOperand(-1), Shift(0), LBound(0), HBound(0) {}
46 ImmField(uint8_t Shift, int16_t LBound, int16_t HBound,
47 int8_t ImmFieldOperand)
48 : ImmFieldOperand(ImmFieldOperand), Shift(Shift), LBound(LBound),
50 int8_t ImmFieldOperand;
60 : TransferOperands(TransferOperands) {}
61 OpInfo() : TransferOperands(OT_NA) {}
69 OpCodes(
unsigned WideOpc,
unsigned NarrowOpc)
70 : WideOpc(WideOpc), NarrowOpc(NarrowOpc) {}
81 bool (*ReduceFunction)(
83 const ReduceEntry &Entry);
90 struct OpInfo OpInf,
struct ImmField Imm)
91 : eRType(RType), ReduceFunction(
F), Ops(Op), OpInf(OpInf), Imm(Imm) {}
93 unsigned NarrowOpc()
const {
return Ops.NarrowOpc; }
94 unsigned WideOpc()
const {
return Ops.WideOpc; }
95 int16_t LBound()
const {
return Imm.LBound; }
96 int16_t HBound()
const {
return Imm.HBound; }
97 uint8_t Shift()
const {
return Imm.Shift; }
98 int8_t ImmField()
const {
return Imm.ImmFieldOperand; }
100 return OpInf.TransferOperands;
102 enum ReduceType RType()
const {
return eRType; }
105 bool operator<(
const unsigned int r)
const {
return (WideOpc() < r); }
108 friend bool operator<(
const unsigned int r,
const struct ReduceEntry &re) {
109 return (r < re.WideOpc());
116 MicroMipsSizeReduce();
124 return "microMIPS instruction size reduction pass";
136 static bool ReduceXWtoXWSP(
MachineInstr *
MI,
const ReduceEntry &Entry);
140 static bool ReduceLXUtoLXU16(
MachineInstr *
MI,
const ReduceEntry &Entry);
144 static bool ReduceSXtoSX16(
MachineInstr *
MI,
const ReduceEntry &Entry);
148 const ReduceEntry &Entry);
151 static bool ReplaceInstruction(
MachineInstr *
MI,
const ReduceEntry &Entry);
167 {RT_OneInstr, OpCodes(Mips::ADDu, Mips::ADDU16_MM),
168 ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
169 ImmField(0, 0, 0, -1)},
170 {RT_OneInstr, OpCodes(Mips::ADDu_MM, Mips::ADDU16_MM),
171 ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
172 ImmField(0, 0, 0, -1)},
173 {RT_OneInstr, OpCodes(Mips::LBu, Mips::LBU16_MM), ReduceLXUtoLXU16,
174 OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},
175 {RT_OneInstr, OpCodes(Mips::LBu_MM, Mips::LBU16_MM), ReduceLXUtoLXU16,
176 OpInfo(OT_OperandsAll), ImmField(0, -1, 15, 2)},
177 {RT_OneInstr, OpCodes(Mips::LHu, Mips::LHU16_MM), ReduceLXUtoLXU16,
178 OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
179 {RT_OneInstr, OpCodes(Mips::LHu_MM, Mips::LHU16_MM), ReduceLXUtoLXU16,
180 OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
181 {RT_OneInstr, OpCodes(Mips::LW, Mips::LWSP_MM), ReduceXWtoXWSP,
182 OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
183 {RT_OneInstr, OpCodes(Mips::LW_MM, Mips::LWSP_MM), ReduceXWtoXWSP,
184 OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
185 {RT_OneInstr, OpCodes(
Mips::SB, Mips::SB16_MM), ReduceSXtoSX16,
186 OpInfo(OT_OperandsAll), ImmField(0, 0, 16, 2)},
187 {RT_OneInstr, OpCodes(Mips::SB_MM, Mips::SB16_MM), ReduceSXtoSX16,
188 OpInfo(OT_OperandsAll), ImmField(0, 0, 16, 2)},
189 {RT_OneInstr, OpCodes(Mips::SH, Mips::SH16_MM), ReduceSXtoSX16,
190 OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
191 {RT_OneInstr, OpCodes(Mips::SH_MM, Mips::SH16_MM), ReduceSXtoSX16,
192 OpInfo(OT_OperandsAll), ImmField(1, 0, 16, 2)},
193 {RT_OneInstr, OpCodes(Mips::SUBu, Mips::SUBU16_MM),
194 ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
195 ImmField(0, 0, 0, -1)},
196 {RT_OneInstr, OpCodes(Mips::SUBu_MM, Mips::SUBU16_MM),
197 ReduceArithmeticInstructions, OpInfo(OT_OperandsAll),
198 ImmField(0, 0, 0, -1)},
199 {RT_OneInstr, OpCodes(Mips::SW, Mips::SWSP_MM), ReduceXWtoXWSP,
200 OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
201 {RT_OneInstr, OpCodes(Mips::SW_MM, Mips::SWSP_MM), ReduceXWtoXWSP,
202 OpInfo(OT_OperandsAll), ImmField(2, 0, 32, 2)},
215 if (MO.
isReg() && Mips::GPRMM16RegClass.contains(MO.
getReg()))
222 if (MO.
isReg() && Mips::GPRMM16ZeroRegClass.contains(MO.
getReg()))
241 int64_t Value2 = Value >> Shift;
242 if ((Value2 << Shift) == Value && (Value2 >= LBound) && (Value2 < HBound))
252 if (!
GetImm(MI, Entry.ImmField(), offset))
255 if (!
InRange(offset, Entry.Shift(), Entry.LBound(), Entry.HBound()))
263 bool MicroMipsSizeReduce::ReduceMI(
275 std::pair<llvm::SmallVector<ReduceEntry, 16>::const_iterator,
277 Range = std::equal_range(Start, End, Opcode);
279 if (Range.first == Range.second)
283 Entry != Range.second; ++Entry)
284 if (((*Entry).ReduceFunction)(&(*MII), *Entry))
290 bool MicroMipsSizeReduce::ReduceXWtoXWSP(
MachineInstr *MI,
291 const ReduceEntry &Entry) {
299 return ReplaceInstruction(MI, Entry);
302 bool MicroMipsSizeReduce::ReduceArithmeticInstructions(
310 return ReplaceInstruction(MI, Entry);
313 bool MicroMipsSizeReduce::ReduceLXUtoLXU16(
MachineInstr *MI,
314 const ReduceEntry &Entry) {
323 return ReplaceInstruction(MI, Entry);
326 bool MicroMipsSizeReduce::ReduceSXtoSX16(
MachineInstr *MI,
327 const ReduceEntry &Entry) {
336 return ReplaceInstruction(MI, Entry);
340 bool Modified =
false;
346 for (; MII !=
E; MII = NextMII) {
347 NextMII = std::next(MII);
355 Modified |= ReduceMI(MII);
361 bool MicroMipsSizeReduce::ReplaceInstruction(
MachineInstr *MI,
362 const ReduceEntry &Entry) {
364 MI->
setDesc(MipsII->get(Entry.NarrowOpc()));
365 DEBUG(
dbgs() <<
"Converted into 16-bit: " << *MI);
376 if (!Subtarget->inMicroMipsMode() || !Subtarget->hasMips32r2())
379 MipsII =
static_cast<const MipsInstrInfo *
>(Subtarget->getInstrInfo());
381 bool Modified =
false;
385 Modified |= ReduceMBB(*I);
391 return new MicroMipsSizeReduce();
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()
typename SuperClass::const_iterator const_iterator
STATISTIC(NumFunctions,"Total number of functions")
OperandTransfer
Order of operands to transfer.
FunctionPass * createMicroMipsSizeReductionPass()
Returns an instance of the MicroMips size reduction pass.
static bool ImmInRange(MachineInstr *MI, const ReduceEntry &Entry)
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
static bool GetImm(MachineInstr *MI, unsigned Op, int64_t &Imm)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
INITIALIZE_PASS(HexagonEarlyIfConversion,"hexagon-eif","Hexagon early if conversion", false, false) bool HexagonEarlyIfConversion MachineBasicBlock * SB
const MachineOperand & getOperand(unsigned i) const
static const unsigned End
FunctionPass class - This class is used to implement most global optimizations.
static bool IsSP(const MachineOperand &MO)
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...
bool isTransient() const
Return true if this is a transient instruction that is either very likely to be eliminated during reg...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Representation of each machine instruction.
ReduceType
Reduction type.
static bool isMMThreeBitGPRegister(const MachineOperand &MO)
Instructions::iterator instr_iterator
unsigned getReg() const
getReg - Returns the register number.
bool operator<(int64_t V1, const APSInt &V2)
LLVM Value Representation.
StringRef - Represent a constant reference to a string, i.e.