Skip to content

Commit 8ac68f9

Browse files
committed
[mips] Put conditions when we need to expand memory operand into a separate function. NFC
`expandMemInst` expects instruction with 3 or 4 operands and the last operand requires expanding. It's redundant to scan all operands in a loop. We can check the last operands.
1 parent 452d0b2 commit 8ac68f9

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

llvm/lib/Target/Mips/AsmParser/MipsAsmParser.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,37 @@ static bool isEvaluated(const MCExpr *Expr) {
18151815
return false;
18161816
}
18171817

1818+
static bool needsExpandMemInst(MCInst &Inst) {
1819+
const MCInstrDesc &MCID = getInstDesc(Inst.getOpcode());
1820+
1821+
unsigned NumOp = MCID.getNumOperands();
1822+
if (NumOp != 3 && NumOp != 4)
1823+
return false;
1824+
1825+
const MCOperandInfo &OpInfo = MCID.OpInfo[NumOp - 1];
1826+
if (OpInfo.OperandType != MCOI::OPERAND_MEMORY &&
1827+
OpInfo.OperandType != MCOI::OPERAND_UNKNOWN)
1828+
return false;
1829+
1830+
MCOperand &Op = Inst.getOperand(NumOp - 1);
1831+
if (Op.isImm()) {
1832+
// Offset can't exceed 16bit value.
1833+
return !isInt<16>(Op.getImm());
1834+
}
1835+
1836+
if (Op.isExpr()) {
1837+
const MCExpr *Expr = Op.getExpr();
1838+
if (Expr->getKind() != MCExpr::SymbolRef)
1839+
return !isEvaluated(Expr);
1840+
1841+
// Expand symbol.
1842+
const MCSymbolRefExpr *SR = static_cast<const MCSymbolRefExpr *>(Expr);
1843+
return SR->getKind() == MCSymbolRefExpr::VK_None;
1844+
}
1845+
1846+
return false;
1847+
}
1848+
18181849
bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
18191850
MCStreamer &Out,
18201851
const MCSubtargetInfo *STI) {
@@ -2102,35 +2133,11 @@ bool MipsAsmParser::processInstruction(MCInst &Inst, SMLoc IDLoc,
21022133
if ((MCID.mayLoad() || MCID.mayStore()) && !IsPCRelativeLoad) {
21032134
// Check the offset of memory operand, if it is a symbol
21042135
// reference or immediate we may have to expand instructions.
2105-
for (unsigned i = 0; i < MCID.getNumOperands(); i++) {
2106-
const MCOperandInfo &OpInfo = MCID.OpInfo[i];
2107-
if ((OpInfo.OperandType == MCOI::OPERAND_MEMORY) ||
2108-
(OpInfo.OperandType == MCOI::OPERAND_UNKNOWN)) {
2109-
MCOperand &Op = Inst.getOperand(i);
2110-
if (Op.isImm()) {
2111-
if (!isInt<16>(Op.getImm())) {
2112-
// Offset can't exceed 16bit value.
2113-
expandMemInst(Inst, IDLoc, Out, STI, MCID.mayLoad());
2114-
return getParser().hasPendingError();
2115-
}
2116-
} else if (Op.isExpr()) {
2117-
const MCExpr *Expr = Op.getExpr();
2118-
if (Expr->getKind() == MCExpr::SymbolRef) {
2119-
const MCSymbolRefExpr *SR =
2120-
static_cast<const MCSymbolRefExpr *>(Expr);
2121-
if (SR->getKind() == MCSymbolRefExpr::VK_None) {
2122-
// Expand symbol.
2123-
expandMemInst(Inst, IDLoc, Out, STI, MCID.mayLoad());
2124-
return getParser().hasPendingError();
2125-
}
2126-
} else if (!isEvaluated(Expr)) {
2127-
expandMemInst(Inst, IDLoc, Out, STI, MCID.mayLoad());
2128-
return getParser().hasPendingError();
2129-
}
2130-
}
2131-
}
2132-
} // for
2133-
} // if load/store
2136+
if (needsExpandMemInst(Inst)) {
2137+
expandMemInst(Inst, IDLoc, Out, STI, MCID.mayLoad());
2138+
return getParser().hasPendingError();
2139+
}
2140+
}
21342141

21352142
if (inMicroMipsMode()) {
21362143
if (MCID.mayLoad() && Opcode != Mips::LWP_MM) {

0 commit comments

Comments
 (0)