Skip to content

[NFC][MC] Use StringRef for Modifier in Inst/Asm Printers #135403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Apr 11, 2025

  • Change various Inst/Asm Printer functions to use a StringRef for the Modifier parameter (instead of a const char *).
  • This simplifies various string comparisons used within these functions.
  • Remove these params for print functions that do not use them.

@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-backend-msp430

Author: Rahul Joshi (jurahul)

Changes
  • Change various Inst/Asm Printer functions to use a StringRef for the Modifier parameter (instead of a const char *).
  • This simplifies various string comparisons used within these functions.

Patch is 29.33 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/135403.diff

21 Files Affected:

  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.cpp (+2-3)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.h (+1-1)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp (+4-5)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp (+8-8)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+16-19)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.h (+4-4)
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
index 792aa47e77a0a..70ece31c63985 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
@@ -51,8 +51,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                  raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                  raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -65,7 +65,7 @@ void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                                     const char *Modifier) {
+                                     StringRef Modifier) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
 
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
index 41835bb2d10d8..e2b948bb79b25 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
@@ -25,9 +25,9 @@ class BPFInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
index a4b0d8488cf53..0f25f2e78a048 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
@@ -99,8 +99,8 @@ void CSKYInstPrinter::printFPRRegName(raw_ostream &O, unsigned RegNo) const {
 
 void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                    const MCSubtargetInfo &STI, raw_ostream &O,
-                                   const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                   StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
index 6640add076b4b..f5dfa9d0fa971 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
@@ -34,7 +34,7 @@ class CSKYInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
 
   void printFPRRegName(raw_ostream &O, unsigned RegNo) const;
 
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
index 2cad5cf6cafbd..4b8d0afe2007c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
@@ -144,8 +144,8 @@ void LanaiInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 }
 
 void LanaiInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &OS, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &OS, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg())
     OS << "%" << getRegisterName(Op.getReg());
@@ -233,7 +233,7 @@ static void printMemoryImmediateOffset(const MCAsmInfo &MAI,
 
 void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -248,7 +248,7 @@ void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -269,7 +269,7 @@ void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemSplsOperand(const MCInst *MI, int OpNo,
                                            raw_ostream &OS,
-                                           const char * /*Modifier*/) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
index de3cb8c178d8d..d7babb1ecb26c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
@@ -27,14 +27,14 @@ class LanaiInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printPredicateOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O);
   void printMemRiOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemRrOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemSplsOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                           const char *Modifier = nullptr);
+                           StringRef Modifier = {});
   void printCCOperand(const MCInst *MI, int OpNo, raw_ostream &O);
   void printHi16ImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printHi16AndImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
index 57e23d2472042..5017d40d3d77a 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
@@ -53,8 +53,8 @@ void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                     raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                     raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -69,7 +69,7 @@ void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 
 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
                                            raw_ostream &O,
-                                           const char *Modifier) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &Base = MI->getOperand(OpNo);
   const MCOperand &Disp = MI->getOperand(OpNo+1);
 
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
index e1785c98bd5c7..5b933eaf24864 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
@@ -39,10 +39,10 @@ namespace llvm {
 
   private:
     void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                      const char *Modifier = nullptr);
+                      StringRef Modifier = {});
     void printPCRelImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printSrcMemOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                            const char *Modifier = nullptr);
+                            StringRef Modifier = {});
     void printIndRegOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
                                 raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
index 247054343a85a..05a13f3541a6f 100644
--- a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
@@ -43,8 +43,8 @@ namespace {
     bool runOnMachineFunction(MachineFunction &MF) override;
 
     void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
-    void printOperand(const MachineInstr *MI, int OpNum,
-                      raw_ostream &O, const char* Modifier = nullptr);
+    void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
+                      StringRef Modifier = {});
     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
                             raw_ostream &O);
     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
@@ -70,7 +70,7 @@ void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
 }
 
 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
-                                    raw_ostream &O, const char *Modifier) {
+                                    raw_ostream &O, StringRef Modifier) {
   const MachineOperand &MO = MI->getOperand(OpNum);
   switch (MO.getType()) {
   default: llvm_unreachable("Not implemented yet!");
@@ -78,7 +78,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     O << MSP430InstPrinter::getRegisterName(MO.getReg());
     return;
   case MachineOperand::MO_Immediate:
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     O << MO.getImm();
     return;
@@ -90,7 +90,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     // register base, we should not emit any prefix symbol here, e.g.
     //   mov.w glb(r1), r2
     // Otherwise (!) msp430-as will silently miscompile the output :(
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     PrintSymbolOperand(MO, O);
     return;
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
index b411056b332f7..4c4488fca15d1 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
@@ -714,9 +714,8 @@ printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
   printOperand(MI, opNum+1, O);
 }
 
-void MipsAsmPrinter::
-printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                const char *Modifier) {
+void MipsAsmPrinter::printFCCOperand(const MachineInstr *MI, int opNum,
+                                     raw_ostream &O, StringRef /*Modifier*/) {
   const MachineOperand &MO = MI->getOperand(opNum);
   O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
 }
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.h b/llvm/lib/Target/Mips/MipsAsmPrinter.h
index 060bba6ef65e0..2b12291cbdb5a 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.h
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.h
@@ -153,7 +153,7 @@ class LLVM_LIBRARY_VISIBILITY MipsAsmPrinter : public AsmPrinter {
   void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O);
   void emitStartOfAsmFile(Module &M) override;
   void emitEndOfAsmFile(Module &M) override;
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
index ee4a7009535c7..0e1b28af691d5 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
@@ -218,11 +218,10 @@ void PPCInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 
 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
                                            const MCSubtargetInfo &STI,
-                                           raw_ostream &O,
-                                           const char *Modifier) {
+                                           raw_ostream &O, StringRef Modifier) {
   unsigned Code = MI->getOperand(OpNo).getImm();
 
-  if (StringRef(Modifier) == "cc") {
+  if (Modifier == "cc") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT_MINUS:
     case PPC::PRED_LT_PLUS:
@@ -271,7 +270,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  if (StringRef(Modifier) == "pm") {
+  if (Modifier == "pm") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT:
     case PPC::PRED_LE:
@@ -309,7 +308,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  assert(StringRef(Modifier) == "reg" &&
+  assert(Modifier == "reg" &&
          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
   printOperand(MI, OpNo + 1, STI, O);
 }
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
index 2286484c8a2dd..48f66ca26958e 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
@@ -52,7 +52,7 @@ class PPCInstPrinter : public MCInstPrinter {
                     raw_ostream &O);
   void printPredicateOperand(const MCInst *MI, unsigned OpNo,
                              const MCSubtargetInfo &STI, raw_ostream &O,
-                             const char *Modifier = nullptr);
+                             StringRef Modifier = {});
   void printATBitsAsHint(const MCInst *MI, unsigned OpNo,
                          const MCSubtargetInfo &STI, raw_ostream &O);
 
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
index 83ecf805489c1..df412097279f3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
@@ -79,8 +79,8 @@ void RISCVInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) {
 
 void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                     const MCSubtargetInfo &STI, raw_ostream &O,
-                                    const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
index 7463088d1bebf..f83a88861a3a3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
@@ -31,7 +31,7 @@ class RISCVInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
   void printBranchOperand(const MCInst *MI, uint64_t Address, unsigned OpNo,
                           const MCSubtargetInfo &STI, raw_ostream &O);
   void printCSRSystemRegister(const MCInst *MI, unsigned OpNo,
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
index e559aa2483f26..f1b1721538392 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
@@ -359,8 +359,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void SPIRVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &O, const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   if (OpNo < MI->getNumOperands()) {
     const MCOperand &Op = MI->getOperand(OpNo);
     if (Op.isReg())
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
index a7b38a6951c51..a7d939d59593c 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
@@ -29,7 +29,7 @@ class SPIRVInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &OS) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
 
   void printStringImm(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
index 47455a9a0274c..51d604a23bdb5 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
@@ -66,9 +66,9 @@ void VEInstPrinter::printOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
                                        const MCSubtargetInfo &STI,
-                                       raw_ostream &O, const char *Modifier) {
+                                       raw_ostream &O, StringRef Modifier) {
   // If this is an ADD operand, emit it like normal operands.
-  if (Modifier && !strcmp(Modifier, "arith")) {
+  if (Modifier == "arith") {
     printOperand(MI, OpNum, STI, O);
     O << ", ";
     printOperand(MI, OpNum + 1, STI, O);
@@ -110,9 +110,9 @@ void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASOperandASX(const MCInst *MI, int OpNum,
                                          const MCSubtargetInfo &STI,
-                                         raw_ostream &O, cons...
[truncated]

@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-backend-risc-v

Author: Rahul Joshi (jurahul)

Changes
  • Change various Inst/Asm Printer functions to use a StringRef for the Modifier parameter (instead of a const char *).
  • This simplifies various string comparisons used within these functions.

Patch is 29.33 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/135403.diff

21 Files Affected:

  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.cpp (+2-3)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.h (+1-1)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp (+4-5)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp (+8-8)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+16-19)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.h (+4-4)
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
index 792aa47e77a0a..70ece31c63985 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
@@ -51,8 +51,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                  raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                  raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -65,7 +65,7 @@ void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                                     const char *Modifier) {
+                                     StringRef Modifier) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
 
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
index 41835bb2d10d8..e2b948bb79b25 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
@@ -25,9 +25,9 @@ class BPFInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
index a4b0d8488cf53..0f25f2e78a048 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
@@ -99,8 +99,8 @@ void CSKYInstPrinter::printFPRRegName(raw_ostream &O, unsigned RegNo) const {
 
 void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                    const MCSubtargetInfo &STI, raw_ostream &O,
-                                   const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                   StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
index 6640add076b4b..f5dfa9d0fa971 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
@@ -34,7 +34,7 @@ class CSKYInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
 
   void printFPRRegName(raw_ostream &O, unsigned RegNo) const;
 
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
index 2cad5cf6cafbd..4b8d0afe2007c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
@@ -144,8 +144,8 @@ void LanaiInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 }
 
 void LanaiInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &OS, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &OS, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg())
     OS << "%" << getRegisterName(Op.getReg());
@@ -233,7 +233,7 @@ static void printMemoryImmediateOffset(const MCAsmInfo &MAI,
 
 void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -248,7 +248,7 @@ void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -269,7 +269,7 @@ void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemSplsOperand(const MCInst *MI, int OpNo,
                                            raw_ostream &OS,
-                                           const char * /*Modifier*/) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
index de3cb8c178d8d..d7babb1ecb26c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
@@ -27,14 +27,14 @@ class LanaiInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printPredicateOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O);
   void printMemRiOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemRrOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemSplsOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                           const char *Modifier = nullptr);
+                           StringRef Modifier = {});
   void printCCOperand(const MCInst *MI, int OpNo, raw_ostream &O);
   void printHi16ImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printHi16AndImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
index 57e23d2472042..5017d40d3d77a 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
@@ -53,8 +53,8 @@ void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                     raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                     raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -69,7 +69,7 @@ void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 
 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
                                            raw_ostream &O,
-                                           const char *Modifier) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &Base = MI->getOperand(OpNo);
   const MCOperand &Disp = MI->getOperand(OpNo+1);
 
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
index e1785c98bd5c7..5b933eaf24864 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
@@ -39,10 +39,10 @@ namespace llvm {
 
   private:
     void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                      const char *Modifier = nullptr);
+                      StringRef Modifier = {});
     void printPCRelImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printSrcMemOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                            const char *Modifier = nullptr);
+                            StringRef Modifier = {});
     void printIndRegOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
                                 raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
index 247054343a85a..05a13f3541a6f 100644
--- a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
@@ -43,8 +43,8 @@ namespace {
     bool runOnMachineFunction(MachineFunction &MF) override;
 
     void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
-    void printOperand(const MachineInstr *MI, int OpNum,
-                      raw_ostream &O, const char* Modifier = nullptr);
+    void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
+                      StringRef Modifier = {});
     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
                             raw_ostream &O);
     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
@@ -70,7 +70,7 @@ void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
 }
 
 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
-                                    raw_ostream &O, const char *Modifier) {
+                                    raw_ostream &O, StringRef Modifier) {
   const MachineOperand &MO = MI->getOperand(OpNum);
   switch (MO.getType()) {
   default: llvm_unreachable("Not implemented yet!");
@@ -78,7 +78,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     O << MSP430InstPrinter::getRegisterName(MO.getReg());
     return;
   case MachineOperand::MO_Immediate:
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     O << MO.getImm();
     return;
@@ -90,7 +90,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     // register base, we should not emit any prefix symbol here, e.g.
     //   mov.w glb(r1), r2
     // Otherwise (!) msp430-as will silently miscompile the output :(
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     PrintSymbolOperand(MO, O);
     return;
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
index b411056b332f7..4c4488fca15d1 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
@@ -714,9 +714,8 @@ printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
   printOperand(MI, opNum+1, O);
 }
 
-void MipsAsmPrinter::
-printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                const char *Modifier) {
+void MipsAsmPrinter::printFCCOperand(const MachineInstr *MI, int opNum,
+                                     raw_ostream &O, StringRef /*Modifier*/) {
   const MachineOperand &MO = MI->getOperand(opNum);
   O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
 }
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.h b/llvm/lib/Target/Mips/MipsAsmPrinter.h
index 060bba6ef65e0..2b12291cbdb5a 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.h
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.h
@@ -153,7 +153,7 @@ class LLVM_LIBRARY_VISIBILITY MipsAsmPrinter : public AsmPrinter {
   void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O);
   void emitStartOfAsmFile(Module &M) override;
   void emitEndOfAsmFile(Module &M) override;
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
index ee4a7009535c7..0e1b28af691d5 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
@@ -218,11 +218,10 @@ void PPCInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 
 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
                                            const MCSubtargetInfo &STI,
-                                           raw_ostream &O,
-                                           const char *Modifier) {
+                                           raw_ostream &O, StringRef Modifier) {
   unsigned Code = MI->getOperand(OpNo).getImm();
 
-  if (StringRef(Modifier) == "cc") {
+  if (Modifier == "cc") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT_MINUS:
     case PPC::PRED_LT_PLUS:
@@ -271,7 +270,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  if (StringRef(Modifier) == "pm") {
+  if (Modifier == "pm") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT:
     case PPC::PRED_LE:
@@ -309,7 +308,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  assert(StringRef(Modifier) == "reg" &&
+  assert(Modifier == "reg" &&
          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
   printOperand(MI, OpNo + 1, STI, O);
 }
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
index 2286484c8a2dd..48f66ca26958e 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
@@ -52,7 +52,7 @@ class PPCInstPrinter : public MCInstPrinter {
                     raw_ostream &O);
   void printPredicateOperand(const MCInst *MI, unsigned OpNo,
                              const MCSubtargetInfo &STI, raw_ostream &O,
-                             const char *Modifier = nullptr);
+                             StringRef Modifier = {});
   void printATBitsAsHint(const MCInst *MI, unsigned OpNo,
                          const MCSubtargetInfo &STI, raw_ostream &O);
 
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
index 83ecf805489c1..df412097279f3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
@@ -79,8 +79,8 @@ void RISCVInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) {
 
 void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                     const MCSubtargetInfo &STI, raw_ostream &O,
-                                    const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
index 7463088d1bebf..f83a88861a3a3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
@@ -31,7 +31,7 @@ class RISCVInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
   void printBranchOperand(const MCInst *MI, uint64_t Address, unsigned OpNo,
                           const MCSubtargetInfo &STI, raw_ostream &O);
   void printCSRSystemRegister(const MCInst *MI, unsigned OpNo,
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
index e559aa2483f26..f1b1721538392 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
@@ -359,8 +359,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void SPIRVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &O, const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   if (OpNo < MI->getNumOperands()) {
     const MCOperand &Op = MI->getOperand(OpNo);
     if (Op.isReg())
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
index a7b38a6951c51..a7d939d59593c 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
@@ -29,7 +29,7 @@ class SPIRVInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &OS) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
 
   void printStringImm(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
index 47455a9a0274c..51d604a23bdb5 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
@@ -66,9 +66,9 @@ void VEInstPrinter::printOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
                                        const MCSubtargetInfo &STI,
-                                       raw_ostream &O, const char *Modifier) {
+                                       raw_ostream &O, StringRef Modifier) {
   // If this is an ADD operand, emit it like normal operands.
-  if (Modifier && !strcmp(Modifier, "arith")) {
+  if (Modifier == "arith") {
     printOperand(MI, OpNum, STI, O);
     O << ", ";
     printOperand(MI, OpNum + 1, STI, O);
@@ -110,9 +110,9 @@ void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASOperandASX(const MCInst *MI, int OpNum,
                                          const MCSubtargetInfo &STI,
-                                         raw_ostream &O, cons...
[truncated]

@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-backend-x86

Author: Rahul Joshi (jurahul)

Changes
  • Change various Inst/Asm Printer functions to use a StringRef for the Modifier parameter (instead of a const char *).
  • This simplifies various string comparisons used within these functions.

Patch is 29.33 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/135403.diff

21 Files Affected:

  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.cpp (+2-3)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.h (+1-1)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp (+4-5)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp (+8-8)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+16-19)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.h (+4-4)
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
index 792aa47e77a0a..70ece31c63985 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
@@ -51,8 +51,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                  raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                  raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -65,7 +65,7 @@ void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                                     const char *Modifier) {
+                                     StringRef Modifier) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
 
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
index 41835bb2d10d8..e2b948bb79b25 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
@@ -25,9 +25,9 @@ class BPFInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
index a4b0d8488cf53..0f25f2e78a048 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
@@ -99,8 +99,8 @@ void CSKYInstPrinter::printFPRRegName(raw_ostream &O, unsigned RegNo) const {
 
 void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                    const MCSubtargetInfo &STI, raw_ostream &O,
-                                   const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                   StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
index 6640add076b4b..f5dfa9d0fa971 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
@@ -34,7 +34,7 @@ class CSKYInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
 
   void printFPRRegName(raw_ostream &O, unsigned RegNo) const;
 
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
index 2cad5cf6cafbd..4b8d0afe2007c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
@@ -144,8 +144,8 @@ void LanaiInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 }
 
 void LanaiInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &OS, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &OS, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg())
     OS << "%" << getRegisterName(Op.getReg());
@@ -233,7 +233,7 @@ static void printMemoryImmediateOffset(const MCAsmInfo &MAI,
 
 void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -248,7 +248,7 @@ void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -269,7 +269,7 @@ void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemSplsOperand(const MCInst *MI, int OpNo,
                                            raw_ostream &OS,
-                                           const char * /*Modifier*/) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
index de3cb8c178d8d..d7babb1ecb26c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
@@ -27,14 +27,14 @@ class LanaiInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printPredicateOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O);
   void printMemRiOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemRrOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemSplsOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                           const char *Modifier = nullptr);
+                           StringRef Modifier = {});
   void printCCOperand(const MCInst *MI, int OpNo, raw_ostream &O);
   void printHi16ImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printHi16AndImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
index 57e23d2472042..5017d40d3d77a 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
@@ -53,8 +53,8 @@ void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                     raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                     raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -69,7 +69,7 @@ void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 
 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
                                            raw_ostream &O,
-                                           const char *Modifier) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &Base = MI->getOperand(OpNo);
   const MCOperand &Disp = MI->getOperand(OpNo+1);
 
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
index e1785c98bd5c7..5b933eaf24864 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
@@ -39,10 +39,10 @@ namespace llvm {
 
   private:
     void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                      const char *Modifier = nullptr);
+                      StringRef Modifier = {});
     void printPCRelImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printSrcMemOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                            const char *Modifier = nullptr);
+                            StringRef Modifier = {});
     void printIndRegOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
                                 raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
index 247054343a85a..05a13f3541a6f 100644
--- a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
@@ -43,8 +43,8 @@ namespace {
     bool runOnMachineFunction(MachineFunction &MF) override;
 
     void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
-    void printOperand(const MachineInstr *MI, int OpNum,
-                      raw_ostream &O, const char* Modifier = nullptr);
+    void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
+                      StringRef Modifier = {});
     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
                             raw_ostream &O);
     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
@@ -70,7 +70,7 @@ void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
 }
 
 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
-                                    raw_ostream &O, const char *Modifier) {
+                                    raw_ostream &O, StringRef Modifier) {
   const MachineOperand &MO = MI->getOperand(OpNum);
   switch (MO.getType()) {
   default: llvm_unreachable("Not implemented yet!");
@@ -78,7 +78,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     O << MSP430InstPrinter::getRegisterName(MO.getReg());
     return;
   case MachineOperand::MO_Immediate:
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     O << MO.getImm();
     return;
@@ -90,7 +90,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     // register base, we should not emit any prefix symbol here, e.g.
     //   mov.w glb(r1), r2
     // Otherwise (!) msp430-as will silently miscompile the output :(
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     PrintSymbolOperand(MO, O);
     return;
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
index b411056b332f7..4c4488fca15d1 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
@@ -714,9 +714,8 @@ printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
   printOperand(MI, opNum+1, O);
 }
 
-void MipsAsmPrinter::
-printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                const char *Modifier) {
+void MipsAsmPrinter::printFCCOperand(const MachineInstr *MI, int opNum,
+                                     raw_ostream &O, StringRef /*Modifier*/) {
   const MachineOperand &MO = MI->getOperand(opNum);
   O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
 }
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.h b/llvm/lib/Target/Mips/MipsAsmPrinter.h
index 060bba6ef65e0..2b12291cbdb5a 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.h
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.h
@@ -153,7 +153,7 @@ class LLVM_LIBRARY_VISIBILITY MipsAsmPrinter : public AsmPrinter {
   void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O);
   void emitStartOfAsmFile(Module &M) override;
   void emitEndOfAsmFile(Module &M) override;
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
index ee4a7009535c7..0e1b28af691d5 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
@@ -218,11 +218,10 @@ void PPCInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 
 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
                                            const MCSubtargetInfo &STI,
-                                           raw_ostream &O,
-                                           const char *Modifier) {
+                                           raw_ostream &O, StringRef Modifier) {
   unsigned Code = MI->getOperand(OpNo).getImm();
 
-  if (StringRef(Modifier) == "cc") {
+  if (Modifier == "cc") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT_MINUS:
     case PPC::PRED_LT_PLUS:
@@ -271,7 +270,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  if (StringRef(Modifier) == "pm") {
+  if (Modifier == "pm") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT:
     case PPC::PRED_LE:
@@ -309,7 +308,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  assert(StringRef(Modifier) == "reg" &&
+  assert(Modifier == "reg" &&
          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
   printOperand(MI, OpNo + 1, STI, O);
 }
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
index 2286484c8a2dd..48f66ca26958e 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
@@ -52,7 +52,7 @@ class PPCInstPrinter : public MCInstPrinter {
                     raw_ostream &O);
   void printPredicateOperand(const MCInst *MI, unsigned OpNo,
                              const MCSubtargetInfo &STI, raw_ostream &O,
-                             const char *Modifier = nullptr);
+                             StringRef Modifier = {});
   void printATBitsAsHint(const MCInst *MI, unsigned OpNo,
                          const MCSubtargetInfo &STI, raw_ostream &O);
 
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
index 83ecf805489c1..df412097279f3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
@@ -79,8 +79,8 @@ void RISCVInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) {
 
 void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                     const MCSubtargetInfo &STI, raw_ostream &O,
-                                    const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
index 7463088d1bebf..f83a88861a3a3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
@@ -31,7 +31,7 @@ class RISCVInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
   void printBranchOperand(const MCInst *MI, uint64_t Address, unsigned OpNo,
                           const MCSubtargetInfo &STI, raw_ostream &O);
   void printCSRSystemRegister(const MCInst *MI, unsigned OpNo,
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
index e559aa2483f26..f1b1721538392 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
@@ -359,8 +359,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void SPIRVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &O, const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   if (OpNo < MI->getNumOperands()) {
     const MCOperand &Op = MI->getOperand(OpNo);
     if (Op.isReg())
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
index a7b38a6951c51..a7d939d59593c 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
@@ -29,7 +29,7 @@ class SPIRVInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &OS) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
 
   void printStringImm(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
index 47455a9a0274c..51d604a23bdb5 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
@@ -66,9 +66,9 @@ void VEInstPrinter::printOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
                                        const MCSubtargetInfo &STI,
-                                       raw_ostream &O, const char *Modifier) {
+                                       raw_ostream &O, StringRef Modifier) {
   // If this is an ADD operand, emit it like normal operands.
-  if (Modifier && !strcmp(Modifier, "arith")) {
+  if (Modifier == "arith") {
     printOperand(MI, OpNum, STI, O);
     O << ", ";
     printOperand(MI, OpNum + 1, STI, O);
@@ -110,9 +110,9 @@ void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASOperandASX(const MCInst *MI, int OpNum,
                                          const MCSubtargetInfo &STI,
-                                         raw_ostream &O, cons...
[truncated]

@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-backend-powerpc

Author: Rahul Joshi (jurahul)

Changes
  • Change various Inst/Asm Printer functions to use a StringRef for the Modifier parameter (instead of a const char *).
  • This simplifies various string comparisons used within these functions.

Patch is 29.33 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/135403.diff

21 Files Affected:

  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.cpp (+2-3)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.h (+1-1)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp (+4-5)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp (+8-8)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+16-19)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.h (+4-4)
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
index 792aa47e77a0a..70ece31c63985 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
@@ -51,8 +51,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                  raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                  raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -65,7 +65,7 @@ void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                                     const char *Modifier) {
+                                     StringRef Modifier) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
 
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
index 41835bb2d10d8..e2b948bb79b25 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
@@ -25,9 +25,9 @@ class BPFInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
index a4b0d8488cf53..0f25f2e78a048 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
@@ -99,8 +99,8 @@ void CSKYInstPrinter::printFPRRegName(raw_ostream &O, unsigned RegNo) const {
 
 void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                    const MCSubtargetInfo &STI, raw_ostream &O,
-                                   const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                   StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
index 6640add076b4b..f5dfa9d0fa971 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
@@ -34,7 +34,7 @@ class CSKYInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
 
   void printFPRRegName(raw_ostream &O, unsigned RegNo) const;
 
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
index 2cad5cf6cafbd..4b8d0afe2007c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
@@ -144,8 +144,8 @@ void LanaiInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 }
 
 void LanaiInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &OS, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &OS, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg())
     OS << "%" << getRegisterName(Op.getReg());
@@ -233,7 +233,7 @@ static void printMemoryImmediateOffset(const MCAsmInfo &MAI,
 
 void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -248,7 +248,7 @@ void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -269,7 +269,7 @@ void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemSplsOperand(const MCInst *MI, int OpNo,
                                            raw_ostream &OS,
-                                           const char * /*Modifier*/) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
index de3cb8c178d8d..d7babb1ecb26c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
@@ -27,14 +27,14 @@ class LanaiInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printPredicateOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O);
   void printMemRiOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemRrOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemSplsOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                           const char *Modifier = nullptr);
+                           StringRef Modifier = {});
   void printCCOperand(const MCInst *MI, int OpNo, raw_ostream &O);
   void printHi16ImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printHi16AndImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
index 57e23d2472042..5017d40d3d77a 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
@@ -53,8 +53,8 @@ void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                     raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                     raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -69,7 +69,7 @@ void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 
 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
                                            raw_ostream &O,
-                                           const char *Modifier) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &Base = MI->getOperand(OpNo);
   const MCOperand &Disp = MI->getOperand(OpNo+1);
 
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
index e1785c98bd5c7..5b933eaf24864 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
@@ -39,10 +39,10 @@ namespace llvm {
 
   private:
     void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                      const char *Modifier = nullptr);
+                      StringRef Modifier = {});
     void printPCRelImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printSrcMemOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                            const char *Modifier = nullptr);
+                            StringRef Modifier = {});
     void printIndRegOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
                                 raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
index 247054343a85a..05a13f3541a6f 100644
--- a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
@@ -43,8 +43,8 @@ namespace {
     bool runOnMachineFunction(MachineFunction &MF) override;
 
     void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
-    void printOperand(const MachineInstr *MI, int OpNum,
-                      raw_ostream &O, const char* Modifier = nullptr);
+    void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
+                      StringRef Modifier = {});
     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
                             raw_ostream &O);
     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
@@ -70,7 +70,7 @@ void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
 }
 
 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
-                                    raw_ostream &O, const char *Modifier) {
+                                    raw_ostream &O, StringRef Modifier) {
   const MachineOperand &MO = MI->getOperand(OpNum);
   switch (MO.getType()) {
   default: llvm_unreachable("Not implemented yet!");
@@ -78,7 +78,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     O << MSP430InstPrinter::getRegisterName(MO.getReg());
     return;
   case MachineOperand::MO_Immediate:
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     O << MO.getImm();
     return;
@@ -90,7 +90,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     // register base, we should not emit any prefix symbol here, e.g.
     //   mov.w glb(r1), r2
     // Otherwise (!) msp430-as will silently miscompile the output :(
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     PrintSymbolOperand(MO, O);
     return;
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
index b411056b332f7..4c4488fca15d1 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
@@ -714,9 +714,8 @@ printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
   printOperand(MI, opNum+1, O);
 }
 
-void MipsAsmPrinter::
-printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                const char *Modifier) {
+void MipsAsmPrinter::printFCCOperand(const MachineInstr *MI, int opNum,
+                                     raw_ostream &O, StringRef /*Modifier*/) {
   const MachineOperand &MO = MI->getOperand(opNum);
   O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
 }
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.h b/llvm/lib/Target/Mips/MipsAsmPrinter.h
index 060bba6ef65e0..2b12291cbdb5a 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.h
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.h
@@ -153,7 +153,7 @@ class LLVM_LIBRARY_VISIBILITY MipsAsmPrinter : public AsmPrinter {
   void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O);
   void emitStartOfAsmFile(Module &M) override;
   void emitEndOfAsmFile(Module &M) override;
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
index ee4a7009535c7..0e1b28af691d5 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
@@ -218,11 +218,10 @@ void PPCInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 
 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
                                            const MCSubtargetInfo &STI,
-                                           raw_ostream &O,
-                                           const char *Modifier) {
+                                           raw_ostream &O, StringRef Modifier) {
   unsigned Code = MI->getOperand(OpNo).getImm();
 
-  if (StringRef(Modifier) == "cc") {
+  if (Modifier == "cc") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT_MINUS:
     case PPC::PRED_LT_PLUS:
@@ -271,7 +270,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  if (StringRef(Modifier) == "pm") {
+  if (Modifier == "pm") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT:
     case PPC::PRED_LE:
@@ -309,7 +308,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  assert(StringRef(Modifier) == "reg" &&
+  assert(Modifier == "reg" &&
          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
   printOperand(MI, OpNo + 1, STI, O);
 }
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
index 2286484c8a2dd..48f66ca26958e 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
@@ -52,7 +52,7 @@ class PPCInstPrinter : public MCInstPrinter {
                     raw_ostream &O);
   void printPredicateOperand(const MCInst *MI, unsigned OpNo,
                              const MCSubtargetInfo &STI, raw_ostream &O,
-                             const char *Modifier = nullptr);
+                             StringRef Modifier = {});
   void printATBitsAsHint(const MCInst *MI, unsigned OpNo,
                          const MCSubtargetInfo &STI, raw_ostream &O);
 
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
index 83ecf805489c1..df412097279f3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
@@ -79,8 +79,8 @@ void RISCVInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) {
 
 void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                     const MCSubtargetInfo &STI, raw_ostream &O,
-                                    const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
index 7463088d1bebf..f83a88861a3a3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
@@ -31,7 +31,7 @@ class RISCVInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
   void printBranchOperand(const MCInst *MI, uint64_t Address, unsigned OpNo,
                           const MCSubtargetInfo &STI, raw_ostream &O);
   void printCSRSystemRegister(const MCInst *MI, unsigned OpNo,
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
index e559aa2483f26..f1b1721538392 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
@@ -359,8 +359,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void SPIRVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &O, const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   if (OpNo < MI->getNumOperands()) {
     const MCOperand &Op = MI->getOperand(OpNo);
     if (Op.isReg())
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
index a7b38a6951c51..a7d939d59593c 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
@@ -29,7 +29,7 @@ class SPIRVInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &OS) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
 
   void printStringImm(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
index 47455a9a0274c..51d604a23bdb5 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
@@ -66,9 +66,9 @@ void VEInstPrinter::printOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
                                        const MCSubtargetInfo &STI,
-                                       raw_ostream &O, const char *Modifier) {
+                                       raw_ostream &O, StringRef Modifier) {
   // If this is an ADD operand, emit it like normal operands.
-  if (Modifier && !strcmp(Modifier, "arith")) {
+  if (Modifier == "arith") {
     printOperand(MI, OpNum, STI, O);
     O << ", ";
     printOperand(MI, OpNum + 1, STI, O);
@@ -110,9 +110,9 @@ void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASOperandASX(const MCInst *MI, int OpNum,
                                          const MCSubtargetInfo &STI,
-                                         raw_ostream &O, cons...
[truncated]

@jurahul jurahul changed the title [NFC][LLVM] Use StringRef for Modifier in Inst/ASM Printers [NFC][LLVM] Use StringRef for Modifier in Inst/Asm Printers Apr 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-backend-spir-v

Author: Rahul Joshi (jurahul)

Changes
  • Change various Inst/Asm Printer functions to use a StringRef for the Modifier parameter (instead of a const char *).
  • This simplifies various string comparisons used within these functions.

Patch is 29.33 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/135403.diff

21 Files Affected:

  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp (+3-3)
  • (modified) llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h (+2-2)
  • (modified) llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp (+5-5)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.cpp (+2-3)
  • (modified) llvm/lib/Target/Mips/MipsAsmPrinter.h (+1-1)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp (+4-5)
  • (modified) llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp (+2-2)
  • (modified) llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h (+1-1)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp (+8-8)
  • (modified) llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.h (+4-4)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+16-19)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.h (+4-4)
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
index 792aa47e77a0a..70ece31c63985 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.cpp
@@ -51,8 +51,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                  raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                  raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -65,7 +65,7 @@ void BPFInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void BPFInstPrinter::printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                                     const char *Modifier) {
+                                     StringRef Modifier) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
 
diff --git a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
index 41835bb2d10d8..e2b948bb79b25 100644
--- a/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
+++ b/llvm/lib/Target/BPF/MCTargetDesc/BPFInstPrinter.h
@@ -25,9 +25,9 @@ class BPFInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printMemOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printImm64Operand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printBrTargetOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
index a4b0d8488cf53..0f25f2e78a048 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp
@@ -99,8 +99,8 @@ void CSKYInstPrinter::printFPRRegName(raw_ostream &O, unsigned RegNo) const {
 
 void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                    const MCSubtargetInfo &STI, raw_ostream &O,
-                                   const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                   StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
index 6640add076b4b..f5dfa9d0fa971 100644
--- a/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
+++ b/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.h
@@ -34,7 +34,7 @@ class CSKYInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
 
   void printFPRRegName(raw_ostream &O, unsigned RegNo) const;
 
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
index 2cad5cf6cafbd..4b8d0afe2007c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.cpp
@@ -144,8 +144,8 @@ void LanaiInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 }
 
 void LanaiInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &OS, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &OS, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg())
     OS << "%" << getRegisterName(Op.getReg());
@@ -233,7 +233,7 @@ static void printMemoryImmediateOffset(const MCAsmInfo &MAI,
 
 void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -248,7 +248,7 @@ void LanaiInstPrinter::printMemRiOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
                                          raw_ostream &OS,
-                                         const char * /*Modifier*/) {
+                                         StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
@@ -269,7 +269,7 @@ void LanaiInstPrinter::printMemRrOperand(const MCInst *MI, int OpNo,
 
 void LanaiInstPrinter::printMemSplsOperand(const MCInst *MI, int OpNo,
                                            raw_ostream &OS,
-                                           const char * /*Modifier*/) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &RegOp = MI->getOperand(OpNo);
   const MCOperand &OffsetOp = MI->getOperand(OpNo + 1);
   const MCOperand &AluOp = MI->getOperand(OpNo + 2);
diff --git a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
index de3cb8c178d8d..d7babb1ecb26c 100644
--- a/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
+++ b/llvm/lib/Target/Lanai/MCTargetDesc/LanaiInstPrinter.h
@@ -27,14 +27,14 @@ class LanaiInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &O) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
   void printPredicateOperand(const MCInst *MI, unsigned OpNum, raw_ostream &O);
   void printMemRiOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemRrOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                         const char *Modifier = nullptr);
+                         StringRef Modifier = {});
   void printMemSplsOperand(const MCInst *MI, int OpNo, raw_ostream &O,
-                           const char *Modifier = nullptr);
+                           StringRef Modifier = {});
   void printCCOperand(const MCInst *MI, int OpNo, raw_ostream &O);
   void printHi16ImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
   void printHi16AndImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
index 57e23d2472042..5017d40d3d77a 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.cpp
@@ -53,8 +53,8 @@ void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
 }
 
 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                     raw_ostream &O, const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                     raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &Op = MI->getOperand(OpNo);
   if (Op.isReg()) {
     O << getRegisterName(Op.getReg());
@@ -69,7 +69,7 @@ void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
 
 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
                                            raw_ostream &O,
-                                           const char *Modifier) {
+                                           StringRef /*Modifier*/) {
   const MCOperand &Base = MI->getOperand(OpNo);
   const MCOperand &Disp = MI->getOperand(OpNo+1);
 
diff --git a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
index e1785c98bd5c7..5b933eaf24864 100644
--- a/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
+++ b/llvm/lib/Target/MSP430/MCTargetDesc/MSP430InstPrinter.h
@@ -39,10 +39,10 @@ namespace llvm {
 
   private:
     void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                      const char *Modifier = nullptr);
+                      StringRef Modifier = {});
     void printPCRelImmOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printSrcMemOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                            const char *Modifier = nullptr);
+                            StringRef Modifier = {});
     void printIndRegOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O);
     void printPostIndRegOperand(const MCInst *MI, unsigned OpNo,
                                 raw_ostream &O);
diff --git a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
index 247054343a85a..05a13f3541a6f 100644
--- a/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
+++ b/llvm/lib/Target/MSP430/MSP430AsmPrinter.cpp
@@ -43,8 +43,8 @@ namespace {
     bool runOnMachineFunction(MachineFunction &MF) override;
 
     void PrintSymbolOperand(const MachineOperand &MO, raw_ostream &O) override;
-    void printOperand(const MachineInstr *MI, int OpNum,
-                      raw_ostream &O, const char* Modifier = nullptr);
+    void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O,
+                      StringRef Modifier = {});
     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
                             raw_ostream &O);
     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
@@ -70,7 +70,7 @@ void MSP430AsmPrinter::PrintSymbolOperand(const MachineOperand &MO,
 }
 
 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
-                                    raw_ostream &O, const char *Modifier) {
+                                    raw_ostream &O, StringRef Modifier) {
   const MachineOperand &MO = MI->getOperand(OpNum);
   switch (MO.getType()) {
   default: llvm_unreachable("Not implemented yet!");
@@ -78,7 +78,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     O << MSP430InstPrinter::getRegisterName(MO.getReg());
     return;
   case MachineOperand::MO_Immediate:
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     O << MO.getImm();
     return;
@@ -90,7 +90,7 @@ void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
     // register base, we should not emit any prefix symbol here, e.g.
     //   mov.w glb(r1), r2
     // Otherwise (!) msp430-as will silently miscompile the output :(
-    if (!Modifier || strcmp(Modifier, "nohash"))
+    if (Modifier != "nohash")
       O << '#';
     PrintSymbolOperand(MO, O);
     return;
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
index b411056b332f7..4c4488fca15d1 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.cpp
@@ -714,9 +714,8 @@ printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O) {
   printOperand(MI, opNum+1, O);
 }
 
-void MipsAsmPrinter::
-printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                const char *Modifier) {
+void MipsAsmPrinter::printFCCOperand(const MachineInstr *MI, int opNum,
+                                     raw_ostream &O, StringRef /*Modifier*/) {
   const MachineOperand &MO = MI->getOperand(opNum);
   O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm());
 }
diff --git a/llvm/lib/Target/Mips/MipsAsmPrinter.h b/llvm/lib/Target/Mips/MipsAsmPrinter.h
index 060bba6ef65e0..2b12291cbdb5a 100644
--- a/llvm/lib/Target/Mips/MipsAsmPrinter.h
+++ b/llvm/lib/Target/Mips/MipsAsmPrinter.h
@@ -153,7 +153,7 @@ class LLVM_LIBRARY_VISIBILITY MipsAsmPrinter : public AsmPrinter {
   void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printMemOperandEA(const MachineInstr *MI, int opNum, raw_ostream &O);
   void printFCCOperand(const MachineInstr *MI, int opNum, raw_ostream &O,
-                       const char *Modifier = nullptr);
+                       StringRef Modifier = {});
   void printRegisterList(const MachineInstr *MI, int opNum, raw_ostream &O);
   void emitStartOfAsmFile(Module &M) override;
   void emitEndOfAsmFile(Module &M) override;
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
index ee4a7009535c7..0e1b28af691d5 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.cpp
@@ -218,11 +218,10 @@ void PPCInstPrinter::printInst(const MCInst *MI, uint64_t Address,
 
 void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
                                            const MCSubtargetInfo &STI,
-                                           raw_ostream &O,
-                                           const char *Modifier) {
+                                           raw_ostream &O, StringRef Modifier) {
   unsigned Code = MI->getOperand(OpNo).getImm();
 
-  if (StringRef(Modifier) == "cc") {
+  if (Modifier == "cc") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT_MINUS:
     case PPC::PRED_LT_PLUS:
@@ -271,7 +270,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  if (StringRef(Modifier) == "pm") {
+  if (Modifier == "pm") {
     switch ((PPC::Predicate)Code) {
     case PPC::PRED_LT:
     case PPC::PRED_LE:
@@ -309,7 +308,7 @@ void PPCInstPrinter::printPredicateOperand(const MCInst *MI, unsigned OpNo,
     llvm_unreachable("Invalid predicate code");
   }
 
-  assert(StringRef(Modifier) == "reg" &&
+  assert(Modifier == "reg" &&
          "Need to specify 'cc', 'pm' or 'reg' as predicate op modifier!");
   printOperand(MI, OpNo + 1, STI, O);
 }
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
index 2286484c8a2dd..48f66ca26958e 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCInstPrinter.h
@@ -52,7 +52,7 @@ class PPCInstPrinter : public MCInstPrinter {
                     raw_ostream &O);
   void printPredicateOperand(const MCInst *MI, unsigned OpNo,
                              const MCSubtargetInfo &STI, raw_ostream &O,
-                             const char *Modifier = nullptr);
+                             StringRef Modifier = {});
   void printATBitsAsHint(const MCInst *MI, unsigned OpNo,
                          const MCSubtargetInfo &STI, raw_ostream &O);
 
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
index 83ecf805489c1..df412097279f3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
@@ -79,8 +79,8 @@ void RISCVInstPrinter::printRegName(raw_ostream &O, MCRegister Reg) {
 
 void RISCVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
                                     const MCSubtargetInfo &STI, raw_ostream &O,
-                                    const char *Modifier) {
-  assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported");
+                                    StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   const MCOperand &MO = MI->getOperand(OpNo);
 
   if (MO.isReg()) {
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
index 7463088d1bebf..f83a88861a3a3 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.h
@@ -31,7 +31,7 @@ class RISCVInstPrinter : public MCInstPrinter {
   void printRegName(raw_ostream &O, MCRegister Reg) override;
 
   void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI,
-                    raw_ostream &O, const char *Modifier = nullptr);
+                    raw_ostream &O, StringRef Modifier = {});
   void printBranchOperand(const MCInst *MI, uint64_t Address, unsigned OpNo,
                           const MCSubtargetInfo &STI, raw_ostream &O);
   void printCSRSystemRegister(const MCInst *MI, unsigned OpNo,
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
index e559aa2483f26..f1b1721538392 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.cpp
@@ -359,8 +359,8 @@ static void printExpr(const MCExpr *Expr, raw_ostream &O) {
 }
 
 void SPIRVInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
-                                    raw_ostream &O, const char *Modifier) {
-  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
+                                    raw_ostream &O, StringRef Modifier) {
+  assert(Modifier.empty() && "No modifiers supported");
   if (OpNo < MI->getNumOperands()) {
     const MCOperand &Op = MI->getOperand(OpNo);
     if (Op.isReg())
diff --git a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
index a7b38a6951c51..a7d939d59593c 100644
--- a/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
+++ b/llvm/lib/Target/SPIRV/MCTargetDesc/SPIRVInstPrinter.h
@@ -29,7 +29,7 @@ class SPIRVInstPrinter : public MCInstPrinter {
   void printInst(const MCInst *MI, uint64_t Address, StringRef Annot,
                  const MCSubtargetInfo &STI, raw_ostream &OS) override;
   void printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O,
-                    const char *Modifier = nullptr);
+                    StringRef Modifier = {});
 
   void printStringImm(const MCInst *MI, unsigned OpNo, raw_ostream &O);
 
diff --git a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
index 47455a9a0274c..51d604a23bdb5 100644
--- a/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
+++ b/llvm/lib/Target/VE/MCTargetDesc/VEInstPrinter.cpp
@@ -66,9 +66,9 @@ void VEInstPrinter::printOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
                                        const MCSubtargetInfo &STI,
-                                       raw_ostream &O, const char *Modifier) {
+                                       raw_ostream &O, StringRef Modifier) {
   // If this is an ADD operand, emit it like normal operands.
-  if (Modifier && !strcmp(Modifier, "arith")) {
+  if (Modifier == "arith") {
     printOperand(MI, OpNum, STI, O);
     O << ", ";
     printOperand(MI, OpNum + 1, STI, O);
@@ -110,9 +110,9 @@ void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
 
 void VEInstPrinter::printMemASOperandASX(const MCInst *MI, int OpNum,
                                          const MCSubtargetInfo &STI,
-                                         raw_ostream &O, cons...
[truncated]

@jurahul jurahul requested review from topperc and s-barannikov April 11, 2025 19:42
@s-barannikov
Copy link
Contributor

This PR should be tagged [MC] rather than [LLVM].

@jurahul jurahul force-pushed the inst_printer_stringref branch from 71043d3 to e684c40 Compare April 11, 2025 21:05
@jurahul jurahul changed the title [NFC][LLVM] Use StringRef for Modifier in Inst/Asm Printers [NFC][MC] Use StringRef for Modifier in Inst/Asm Printers Apr 11, 2025
@jurahul jurahul force-pushed the inst_printer_stringref branch 2 times, most recently from d23f96c to 7c5464c Compare April 12, 2025 00:39
Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks,
LGTM with MSP430 fixed (failing pre-commit checks)

@jurahul jurahul force-pushed the inst_printer_stringref branch from 7c5464c to ce7daf6 Compare April 12, 2025 01:31
- Change various Inst/Asm Printer functions to use a StringRef
  for the Modifier parameter (instead of a const char *).
- This simplifies various string comparisons used within these
  functions.
@jurahul jurahul force-pushed the inst_printer_stringref branch from ce7daf6 to 1b7a0ac Compare April 12, 2025 18:19
@jurahul jurahul merged commit 8ede3dd into llvm:main Apr 14, 2025
12 checks passed
@jurahul jurahul deleted the inst_printer_stringref branch April 14, 2025 16:02
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
)

- Change various Inst/Asm Printer functions to use a StringRef for the
Modifier parameter (instead of a const char *).
- This simplifies various string comparisons used within these
functions.
- Remove these params for print functions that do not use them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants