Skip to content

Commit 8ddc816

Browse files
committed
[CSKY] Lower leaf DAG node such as global symbol, frame address and jumptable, etc.
Lower global symbols such as call/external symbol. Lower other leaf DAG node such as frame address/block address/jumptable/vastart. Normally some leaf symbols need reside in constant pool as ABI prefers, and are addressed by lrw or jsri instructions. Every symbol in constant pool is lowered with one entry in target constant pool. The entry has different type corresponding to different leaf node such as blockaddress, jumptable, or global value.
1 parent cd4deef commit 8ddc816

15 files changed

+1831
-4
lines changed

llvm/lib/Target/CSKY/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_public_tablegen_target(CSKYCommonTableGen)
1717

1818
add_llvm_target(CSKYCodeGen
1919
CSKYAsmPrinter.cpp
20+
CSKYConstantPoolValue.cpp
2021
CSKYFrameLowering.cpp
2122
CSKYInstrInfo.cpp
2223
CSKYISelDAGToDAG.cpp

llvm/lib/Target/CSKY/CSKYAsmPrinter.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313
#include "CSKYAsmPrinter.h"
1414
#include "CSKY.h"
15+
#include "CSKYConstantPoolValue.h"
1516
#include "CSKYTargetMachine.h"
1617
#include "MCTargetDesc/CSKYInstPrinter.h"
1718
#include "MCTargetDesc/CSKYMCExpr.h"
@@ -66,6 +67,84 @@ void CSKYAsmPrinter::emitInstruction(const MachineInstr *MI) {
6667
EmitToStreamer(*OutStreamer, TmpInst);
6768
}
6869

70+
// Convert a CSKY-specific constant pool modifier into the associated
71+
// MCSymbolRefExpr variant kind.
72+
static CSKYMCExpr::VariantKind
73+
getModifierVariantKind(CSKYCP::CSKYCPModifier Modifier) {
74+
switch (Modifier) {
75+
case CSKYCP::NO_MOD:
76+
return CSKYMCExpr::VK_CSKY_None;
77+
case CSKYCP::ADDR:
78+
return CSKYMCExpr::VK_CSKY_ADDR;
79+
case CSKYCP::GOT:
80+
return CSKYMCExpr::VK_CSKY_GOT;
81+
case CSKYCP::GOTOFF:
82+
return CSKYMCExpr::VK_CSKY_GOTOFF;
83+
case CSKYCP::PLT:
84+
return CSKYMCExpr::VK_CSKY_PLT;
85+
case CSKYCP::TLSGD:
86+
return CSKYMCExpr::VK_CSKY_TLSGD;
87+
case CSKYCP::TLSLE:
88+
return CSKYMCExpr::VK_CSKY_TLSLE;
89+
case CSKYCP::TLSIE:
90+
return CSKYMCExpr::VK_CSKY_TLSIE;
91+
}
92+
llvm_unreachable("Invalid CSKYCPModifier!");
93+
}
94+
95+
void CSKYAsmPrinter::emitMachineConstantPoolValue(
96+
MachineConstantPoolValue *MCPV) {
97+
int Size = getDataLayout().getTypeAllocSize(MCPV->getType());
98+
CSKYConstantPoolValue *CCPV = static_cast<CSKYConstantPoolValue *>(MCPV);
99+
MCSymbol *MCSym;
100+
101+
if (CCPV->isBlockAddress()) {
102+
const BlockAddress *BA =
103+
cast<CSKYConstantPoolConstant>(CCPV)->getBlockAddress();
104+
MCSym = GetBlockAddressSymbol(BA);
105+
} else if (CCPV->isGlobalValue()) {
106+
const GlobalValue *GV = cast<CSKYConstantPoolConstant>(CCPV)->getGV();
107+
MCSym = getSymbol(GV);
108+
} else if (CCPV->isMachineBasicBlock()) {
109+
const MachineBasicBlock *MBB = cast<CSKYConstantPoolMBB>(CCPV)->getMBB();
110+
MCSym = MBB->getSymbol();
111+
} else if (CCPV->isJT()) {
112+
signed JTI = cast<CSKYConstantPoolJT>(CCPV)->getJTI();
113+
MCSym = GetJTISymbol(JTI);
114+
} else {
115+
assert(CCPV->isExtSymbol() && "unrecognized constant pool value");
116+
StringRef Sym = cast<CSKYConstantPoolSymbol>(CCPV)->getSymbol();
117+
MCSym = GetExternalSymbolSymbol(Sym);
118+
}
119+
// Create an MCSymbol for the reference.
120+
const MCExpr *Expr =
121+
MCSymbolRefExpr::create(MCSym, MCSymbolRefExpr::VK_None, OutContext);
122+
123+
if (CCPV->getPCAdjustment()) {
124+
125+
MCSymbol *PCLabel = OutContext.getOrCreateSymbol(
126+
Twine(MAI->getPrivateGlobalPrefix()) + "PC" +
127+
Twine(getFunctionNumber()) + "_" + Twine(CCPV->getLabelID()));
128+
129+
const MCExpr *PCRelExpr = MCSymbolRefExpr::create(PCLabel, OutContext);
130+
if (CCPV->mustAddCurrentAddress()) {
131+
// We want "(<expr> - .)", but MC doesn't have a concept of the '.'
132+
// label, so just emit a local label end reference that instead.
133+
MCSymbol *DotSym = OutContext.createTempSymbol();
134+
OutStreamer->emitLabel(DotSym);
135+
const MCExpr *DotExpr = MCSymbolRefExpr::create(DotSym, OutContext);
136+
PCRelExpr = MCBinaryExpr::createSub(PCRelExpr, DotExpr, OutContext);
137+
}
138+
Expr = MCBinaryExpr::createSub(Expr, PCRelExpr, OutContext);
139+
}
140+
141+
// Create an MCSymbol for the reference.
142+
Expr = CSKYMCExpr::create(Expr, getModifierVariantKind(CCPV->getModifier()),
143+
OutContext);
144+
145+
OutStreamer->emitValue(Expr, Size);
146+
}
147+
69148
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYAsmPrinter() {
70149
RegisterAsmPrinter<CSKYAsmPrinter> X(getTheCSKYTarget());
71150
}

llvm/lib/Target/CSKY/CSKYAsmPrinter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class LLVM_LIBRARY_VISIBILITY CSKYAsmPrinter : public AsmPrinter {
3333
bool emitPseudoExpansionLowering(MCStreamer &OutStreamer,
3434
const MachineInstr *MI);
3535

36+
void emitMachineConstantPoolValue(MachineConstantPoolValue *MCPV) override;
37+
3638
void emitInstruction(const MachineInstr *MI) override;
3739

3840
bool runOnMachineFunction(MachineFunction &MF) override;
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
//===-- CSKYConstantPoolValue.cpp - CSKY constantpool value ---------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file implements the CSKY specific constantpool value class.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "CSKYConstantPoolValue.h"
14+
#include "llvm/ADT/FoldingSet.h"
15+
#include "llvm/CodeGen/MachineBasicBlock.h"
16+
#include "llvm/IR/Constant.h"
17+
#include "llvm/IR/Constants.h"
18+
#include "llvm/IR/GlobalValue.h"
19+
#include "llvm/IR/Type.h"
20+
#include "llvm/Support/raw_ostream.h"
21+
using namespace llvm;
22+
23+
//===----------------------------------------------------------------------===//
24+
// CSKYConstantPoolValue
25+
//===----------------------------------------------------------------------===//
26+
27+
CSKYConstantPoolValue::CSKYConstantPoolValue(Type *Ty, CSKYCP::CSKYCPKind Kind,
28+
unsigned PCAdjust,
29+
CSKYCP::CSKYCPModifier Modifier,
30+
bool AddCurrentAddress,
31+
unsigned ID)
32+
: MachineConstantPoolValue(Ty), Kind(Kind), PCAdjust(PCAdjust),
33+
Modifier(Modifier), AddCurrentAddress(AddCurrentAddress), LabelId(ID) {}
34+
35+
const char *CSKYConstantPoolValue::getModifierText() const {
36+
switch (Modifier) {
37+
case CSKYCP::ADDR:
38+
return "ADDR";
39+
case CSKYCP::GOT:
40+
return "GOT";
41+
case CSKYCP::GOTOFF:
42+
return "GOTOFF";
43+
case CSKYCP::PLT:
44+
return "PLT";
45+
case CSKYCP::TLSIE:
46+
return "TLSIE";
47+
case CSKYCP::TLSLE:
48+
return "TLSLE";
49+
case CSKYCP::TLSGD:
50+
return "TLSGD";
51+
case CSKYCP::NO_MOD:
52+
return "";
53+
}
54+
llvm_unreachable("Unknown modifier!");
55+
}
56+
57+
int CSKYConstantPoolValue::getExistingMachineCPValue(MachineConstantPool *CP,
58+
Align Alignment) {
59+
llvm_unreachable("Shouldn't be calling this directly!");
60+
}
61+
62+
void CSKYConstantPoolValue::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
63+
ID.AddInteger(LabelId);
64+
ID.AddInteger(PCAdjust);
65+
ID.AddInteger(Modifier);
66+
}
67+
68+
void CSKYConstantPoolValue::print(raw_ostream &O) const {
69+
if (Modifier)
70+
O << "(" << getModifierText() << ")";
71+
if (PCAdjust)
72+
O << " + " << PCAdjust;
73+
}
74+
75+
//===----------------------------------------------------------------------===//
76+
// CSKYConstantPoolConstant
77+
//===----------------------------------------------------------------------===//
78+
79+
CSKYConstantPoolConstant::CSKYConstantPoolConstant(
80+
const Constant *C, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
81+
CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress, unsigned ID)
82+
: CSKYConstantPoolValue(C->getType(), Kind, PCAdjust, Modifier,
83+
AddCurrentAddress, ID),
84+
CVal(C) {}
85+
86+
CSKYConstantPoolConstant *CSKYConstantPoolConstant::Create(
87+
const Constant *C, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
88+
CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress, unsigned ID) {
89+
return new CSKYConstantPoolConstant(C, Kind, PCAdjust, Modifier,
90+
AddCurrentAddress, ID);
91+
}
92+
93+
const GlobalValue *CSKYConstantPoolConstant::getGV() const {
94+
assert(isa<GlobalValue>(CVal) && "CVal should be GlobalValue");
95+
return cast<GlobalValue>(CVal);
96+
}
97+
98+
const BlockAddress *CSKYConstantPoolConstant::getBlockAddress() const {
99+
assert(isa<BlockAddress>(CVal) && "CVal should be BlockAddress");
100+
return cast<BlockAddress>(CVal);
101+
}
102+
103+
int CSKYConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
104+
Align Alignment) {
105+
return getExistingMachineCPValueImpl<CSKYConstantPoolConstant>(CP, Alignment);
106+
}
107+
108+
void CSKYConstantPoolConstant::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
109+
ID.AddPointer(CVal);
110+
111+
CSKYConstantPoolValue::addSelectionDAGCSEId(ID);
112+
}
113+
114+
void CSKYConstantPoolConstant::print(raw_ostream &O) const {
115+
O << CVal->getName();
116+
CSKYConstantPoolValue::print(O);
117+
}
118+
119+
//===----------------------------------------------------------------------===//
120+
// CSKYConstantPoolSymbol
121+
//===----------------------------------------------------------------------===//
122+
123+
CSKYConstantPoolSymbol::CSKYConstantPoolSymbol(Type *Ty, const char *S,
124+
unsigned PCAdjust,
125+
CSKYCP::CSKYCPModifier Modifier,
126+
bool AddCurrentAddress)
127+
: CSKYConstantPoolValue(Ty, CSKYCP::CPExtSymbol, PCAdjust, Modifier,
128+
AddCurrentAddress),
129+
S(strdup(S)) {}
130+
131+
CSKYConstantPoolSymbol *
132+
CSKYConstantPoolSymbol::Create(Type *Ty, const char *S, unsigned PCAdjust,
133+
CSKYCP::CSKYCPModifier Modifier) {
134+
return new CSKYConstantPoolSymbol(Ty, S, PCAdjust, Modifier, false);
135+
}
136+
137+
int CSKYConstantPoolSymbol::getExistingMachineCPValue(MachineConstantPool *CP,
138+
Align Alignment) {
139+
140+
return getExistingMachineCPValueImpl<CSKYConstantPoolSymbol>(CP, Alignment);
141+
}
142+
143+
void CSKYConstantPoolSymbol::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
144+
ID.AddString(S);
145+
CSKYConstantPoolValue::addSelectionDAGCSEId(ID);
146+
}
147+
148+
void CSKYConstantPoolSymbol::print(raw_ostream &O) const {
149+
O << S;
150+
CSKYConstantPoolValue::print(O);
151+
}
152+
153+
//===----------------------------------------------------------------------===//
154+
// CSKYConstantPoolMBB
155+
//===----------------------------------------------------------------------===//
156+
157+
CSKYConstantPoolMBB::CSKYConstantPoolMBB(Type *Ty, const MachineBasicBlock *Mbb,
158+
unsigned PCAdjust,
159+
CSKYCP::CSKYCPModifier Modifier,
160+
bool AddCurrentAddress)
161+
: CSKYConstantPoolValue(Ty, CSKYCP::CPMachineBasicBlock, PCAdjust, Modifier,
162+
AddCurrentAddress),
163+
MBB(Mbb) {}
164+
165+
CSKYConstantPoolMBB *CSKYConstantPoolMBB::Create(Type *Ty,
166+
const MachineBasicBlock *Mbb,
167+
unsigned PCAdjust) {
168+
return new CSKYConstantPoolMBB(Ty, Mbb, PCAdjust, CSKYCP::ADDR, false);
169+
}
170+
171+
int CSKYConstantPoolMBB::getExistingMachineCPValue(MachineConstantPool *CP,
172+
Align Alignment) {
173+
return getExistingMachineCPValueImpl<CSKYConstantPoolMBB>(CP, Alignment);
174+
}
175+
176+
void CSKYConstantPoolMBB::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
177+
ID.AddPointer(MBB);
178+
CSKYConstantPoolValue::addSelectionDAGCSEId(ID);
179+
}
180+
181+
void CSKYConstantPoolMBB::print(raw_ostream &O) const {
182+
O << "BB#" << MBB->getNumber();
183+
CSKYConstantPoolValue::print(O);
184+
}
185+
186+
//===----------------------------------------------------------------------===//
187+
// CSKYConstantPoolJT
188+
//===----------------------------------------------------------------------===//
189+
190+
CSKYConstantPoolJT::CSKYConstantPoolJT(Type *Ty, int JTIndex, unsigned PCAdj,
191+
CSKYCP::CSKYCPModifier Modifier,
192+
bool AddCurrentAddress)
193+
: CSKYConstantPoolValue(Ty, CSKYCP::CPJT, PCAdj, Modifier,
194+
AddCurrentAddress),
195+
JTI(JTIndex) {}
196+
197+
CSKYConstantPoolJT *
198+
CSKYConstantPoolJT::Create(Type *Ty, int JTI, unsigned PCAdj,
199+
CSKYCP::CSKYCPModifier Modifier) {
200+
return new CSKYConstantPoolJT(Ty, JTI, PCAdj, Modifier, false);
201+
}
202+
203+
int CSKYConstantPoolJT::getExistingMachineCPValue(MachineConstantPool *CP,
204+
Align Alignment) {
205+
return getExistingMachineCPValueImpl<CSKYConstantPoolJT>(CP, Alignment);
206+
}
207+
208+
void CSKYConstantPoolJT::addSelectionDAGCSEId(FoldingSetNodeID &ID) {
209+
ID.AddInteger(JTI);
210+
CSKYConstantPoolValue::addSelectionDAGCSEId(ID);
211+
}
212+
213+
void CSKYConstantPoolJT::print(raw_ostream &O) const {
214+
O << "JTI#" << JTI;
215+
CSKYConstantPoolValue::print(O);
216+
}

0 commit comments

Comments
 (0)