[llvm-branch-commits] [llvm] 5d977f2 - [Xtensa] Implement assembler representation of the Constant Pool.

Andrei Safronov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Aug 11 15:40:29 PDT 2020


Author: Andrei Safronov
Date: 2020-07-21T13:25:50+03:00
New Revision: 5d977f2260f4f37b6da9e7727ca2106a64a27596

URL: https://github.com/llvm/llvm-project/commit/5d977f2260f4f37b6da9e7727ca2106a64a27596
DIFF: https://github.com/llvm/llvm-project/commit/5d977f2260f4f37b6da9e7727ca2106a64a27596.diff

LOG: [Xtensa] Implement assembler representation of the Constant Pool.

Added: 
    

Modified: 
    llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp
    llvm/lib/Target/Xtensa/XtensaAsmPrinter.h

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp b/llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp
index 4b5b824420bc..94085c93c4e1 100644
--- a/llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp
+++ b/llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp
@@ -14,6 +14,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "XtensaAsmPrinter.h"
+#include "XtensaConstantPoolValue.h"
 #include "XtensaMCInstLower.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/CodeGen/MachineModuleInfoImpls.h"
@@ -27,6 +28,17 @@
 
 using namespace llvm;
 
+static MCSymbolRefExpr::VariantKind
+getModifierVariantKind(XtensaCP::XtensaCPModifier Modifier) {
+  switch (Modifier) {
+  case XtensaCP::no_modifier:
+    return MCSymbolRefExpr::VK_None;
+  case XtensaCP::TPOFF:
+    return MCSymbolRefExpr::VK_TPOFF;
+  }
+  llvm_unreachable("Invalid XtensaCPModifier!");
+}
+
 void XtensaAsmPrinter::EmitInstruction(const MachineInstr *MI) {
   XtensaMCInstLower Lower(MF->getContext(), *this);
   MCInst LoweredMI;
@@ -34,6 +46,164 @@ void XtensaAsmPrinter::EmitInstruction(const MachineInstr *MI) {
   EmitToStreamer(*OutStreamer, LoweredMI);
 }
 
+/// EmitConstantPool - Print to the current output stream assembly
+/// representations of the constants in the constant pool MCP. This is
+/// used to print out constants which have been "spilled to memory" by
+/// the code generator.
+void XtensaAsmPrinter::EmitConstantPool() {
+  const Function &F = MF->getFunction();
+  const MachineConstantPool *MCP = MF->getConstantPool();
+  const std::vector<MachineConstantPoolEntry> &CP = MCP->getConstants();
+  if (CP.empty())
+    return;
+
+  for (unsigned i = 0, e = CP.size(); i != e; ++i) {
+    const MachineConstantPoolEntry &CPE = CP[i];
+
+    if (i == 0) {
+      if (OutStreamer->hasRawTextSupport()) {
+        OutStreamer->SwitchSection(
+            getObjFileLowering().SectionForGlobal(&F, TM));
+        OutStreamer->EmitRawText("\t.literal_position\n");
+      } else {
+        MCSectionELF *CS =
+            (MCSectionELF *)getObjFileLowering().SectionForGlobal(&F, TM);
+        std::string CSectionName = CS->getSectionName();
+        std::size_t Pos = CSectionName.find(".text");
+        std::string SectionName;
+        if (Pos != std::string::npos) {
+          if (Pos > 0)
+            SectionName = CSectionName.substr(0, Pos + 5);
+          else
+            SectionName = "";
+          SectionName += ".literal";
+          SectionName += CSectionName.substr(Pos + 5);
+        } else {
+          SectionName = CSectionName;
+          SectionName += ".literal";
+        }
+
+        MCSectionELF *S =
+            OutContext.getELFSection(SectionName, ELF::SHT_PROGBITS,
+                                     ELF::SHF_EXECINSTR | ELF::SHF_ALLOC);
+        S->setAlignment(Align(4));
+        OutStreamer->SwitchSection(S);
+      }
+    }
+
+    if (CPE.isMachineConstantPoolEntry()) {
+      XtensaConstantPoolValue *ACPV =
+          static_cast<XtensaConstantPoolValue *>(CPE.Val.MachineCPVal);
+      ACPV->setLabelId(i);
+      EmitMachineConstantPoolValue(CPE.Val.MachineCPVal);
+    } else {
+      MCSymbol *LblSym = GetCPISymbol(i);
+      // TODO find a better way to check whether we emit data to .s file
+      if (OutStreamer->hasRawTextSupport()) {
+        std::string str("\t.literal ");
+        str += LblSym->getName();
+        str += ", ";
+        const Constant *C = CPE.Val.ConstVal;
+
+        Type *Ty = C->getType();
+        if (const auto *CFP = dyn_cast<ConstantFP>(C)) {
+          str += CFP->getValueAPF().bitcastToAPInt().toString(10, true);
+        } else if (const auto *CI = dyn_cast<ConstantInt>(C)) {
+          str += CI->getValue().toString(10, true);
+        } else if (isa<PointerType>(Ty)) {
+          const MCExpr *ME = lowerConstant(C);
+          const MCSymbolRefExpr &SRE = cast<MCSymbolRefExpr>(*ME);
+          const MCSymbol &Sym = SRE.getSymbol();
+          str += Sym.getName();
+        } else {
+          unsigned NumElements;
+          if (isa<VectorType>(Ty))
+            NumElements = Ty->getVectorNumElements();
+          else
+            NumElements = Ty->getArrayNumElements();
+
+          for (unsigned I = 0; I < NumElements; I++) {
+            const Constant *CAE = C->getAggregateElement(I);
+            if (I > 0)
+              str += ", ";
+            if (const auto *CFP = dyn_cast<ConstantFP>(CAE)) {
+              str += CFP->getValueAPF().bitcastToAPInt().toString(10, true);
+            } else if (const auto *CI = dyn_cast<ConstantInt>(CAE)) {
+              str += CI->getValue().toString(10, true);
+            }
+          }
+        }
+
+        OutStreamer->EmitRawText(str);
+      } else {
+        OutStreamer->EmitLabel(LblSym);
+        EmitGlobalConstant(getDataLayout(), CPE.Val.ConstVal);
+      }
+    }
+  }
+}
+
+void XtensaAsmPrinter::EmitMachineConstantPoolValue(
+    MachineConstantPoolValue *MCPV) {
+  XtensaConstantPoolValue *ACPV = static_cast<XtensaConstantPoolValue *>(MCPV);
+
+  MCSymbol *MCSym;
+  if (ACPV->isBlockAddress()) {
+    const BlockAddress *BA =
+        cast<XtensaConstantPoolConstant>(ACPV)->getBlockAddress();
+    MCSym = GetBlockAddressSymbol(BA);
+  } else if (ACPV->isGlobalValue()) {
+    const GlobalValue *GV = cast<XtensaConstantPoolConstant>(ACPV)->getGV();
+    // TODO some modifiers
+    MCSym = getSymbol(GV);
+  } else if (ACPV->isMachineBasicBlock()) {
+    const MachineBasicBlock *MBB = cast<XtensaConstantPoolMBB>(ACPV)->getMBB();
+    MCSym = MBB->getSymbol();
+  } else if (ACPV->isJumpTable()) {
+    unsigned idx = cast<XtensaConstantPoolJumpTable>(ACPV)->getIndex();
+    MCSym = this->GetJTISymbol(idx, false);
+  } else {
+    assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
+    XtensaConstantPoolSymbol *XtensaSym = cast<XtensaConstantPoolSymbol>(ACPV);
+    const char *Sym = XtensaSym->getSymbol();
+    // TODO it's a trick to distinguish static references and generated rodata
+    // references Some clear method required
+    {
+      std::string SymName(Sym);
+      if (XtensaSym->isPrivateLinkage())
+        SymName = ".L" + SymName;
+      MCSym = GetExternalSymbolSymbol(StringRef(SymName));
+    }
+  }
+
+  MCSymbol *LblSym = GetCPISymbol(ACPV->getLabelId());
+  // TODO find a better way to check whether we emit data to .s file
+  if (OutStreamer->hasRawTextSupport()) {
+    std::string SymName("\t.literal ");
+    SymName += LblSym->getName();
+    SymName += ", ";
+    SymName += MCSym->getName();
+
+    StringRef Modifier = ACPV->getModifierText();
+    SymName += Modifier;
+
+    OutStreamer->EmitRawText(SymName);
+  } else {
+    MCSymbolRefExpr::VariantKind VK =
+        getModifierVariantKind(ACPV->getModifier());
+
+    if (ACPV->getModifier() != XtensaCP::no_modifier) {
+      std::string SymName(MCSym->getName());
+      MCSym = GetExternalSymbolSymbol(StringRef(SymName));
+    }
+
+    const MCExpr *Expr = MCSymbolRefExpr::create(MCSym, VK, OutContext);
+    uint64_t Size = getDataLayout().getTypeAllocSize(ACPV->getType());
+    OutStreamer->EmitLabel(LblSym);
+    OutStreamer->EmitValue(Expr, Size);
+  }
+}
+
 // Force static initialization.
 extern "C" void LLVMInitializeXtensaAsmPrinter() {
   RegisterAsmPrinter<XtensaAsmPrinter> A(TheXtensaTarget);

diff  --git a/llvm/lib/Target/Xtensa/XtensaAsmPrinter.h b/llvm/lib/Target/Xtensa/XtensaAsmPrinter.h
index 9343f1181d26..96960ac17cb3 100644
--- a/llvm/lib/Target/Xtensa/XtensaAsmPrinter.h
+++ b/llvm/lib/Target/Xtensa/XtensaAsmPrinter.h
@@ -35,6 +35,8 @@ class LLVM_LIBRARY_VISIBILITY XtensaAsmPrinter : public AsmPrinter {
   // Override AsmPrinter.
   StringRef getPassName() const override { return "Xtensa Assembly Printer"; }
   void EmitInstruction(const MachineInstr *MI) override;
+  void EmitConstantPool() override;
+  void EmitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
 };
 } // end namespace llvm
 


        


More information about the llvm-branch-commits mailing list