Skip to content

Commit f4d61cd

Browse files
committed
[CSKY] Lower ISD::ConstantPool node to support getting the address of ConstantPool entry
When there is not GRS or MOVIH/ORI instruction, we can not get the address of ConstantPool entry directly. So we need put the address into ConstantPool to leverage CSKY::LRW instruction.
1 parent c2ec455 commit f4d61cd

File tree

9 files changed

+114
-11
lines changed

9 files changed

+114
-11
lines changed

llvm/lib/Target/CSKY/CSKYAsmPrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ void CSKYAsmPrinter::emitMachineConstantPoolValue(
208208
} else if (CCPV->isJT()) {
209209
signed JTI = cast<CSKYConstantPoolJT>(CCPV)->getJTI();
210210
MCSym = GetJTISymbol(JTI);
211+
} else if (CCPV->isConstPool()) {
212+
const Constant *C = cast<CSKYConstantPoolConstant>(CCPV)->getConstantPool();
213+
MCSym = GetCPISymbol(MCP->getConstantPoolIndex(C, Align(4)));
211214
} else {
212215
assert(CCPV->isExtSymbol() && "unrecognized constant pool value");
213216
StringRef Sym = cast<CSKYConstantPoolSymbol>(CCPV)->getSymbol();

llvm/lib/Target/CSKY/CSKYConstantIslandPass.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,6 @@ void CSKYConstantIslands::initializeFunctionInfo(
573573
CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
574574
assert(CPE && "Cannot find a corresponding CPEntry!");
575575
CPE->RefCount++;
576-
577-
// Instructions can only use one CP entry, don't bother scanning the
578-
// rest of the operands.
579-
break;
580576
}
581577
}
582578
}

llvm/lib/Target/CSKY/CSKYConstantPoolValue.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,23 @@ void CSKYConstantPoolValue::print(raw_ostream &O) const {
7777
//===----------------------------------------------------------------------===//
7878

7979
CSKYConstantPoolConstant::CSKYConstantPoolConstant(
80-
const Constant *C, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
80+
const Constant *C, Type *Ty, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
8181
CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress, unsigned ID)
82-
: CSKYConstantPoolValue(C->getType(), Kind, PCAdjust, Modifier,
83-
AddCurrentAddress, ID),
82+
: CSKYConstantPoolValue(Ty, Kind, PCAdjust, Modifier, AddCurrentAddress,
83+
ID),
8484
CVal(C) {}
8585

8686
CSKYConstantPoolConstant *CSKYConstantPoolConstant::Create(
8787
const Constant *C, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
8888
CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress, unsigned ID) {
89-
return new CSKYConstantPoolConstant(C, Kind, PCAdjust, Modifier,
89+
return new CSKYConstantPoolConstant(C, C->getType(), Kind, PCAdjust, Modifier,
90+
AddCurrentAddress, ID);
91+
}
92+
93+
CSKYConstantPoolConstant *CSKYConstantPoolConstant::Create(
94+
const Constant *C, Type *Ty, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
95+
CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress, unsigned ID) {
96+
return new CSKYConstantPoolConstant(C, Ty, Kind, PCAdjust, Modifier,
9097
AddCurrentAddress, ID);
9198
}
9299

@@ -100,6 +107,10 @@ const BlockAddress *CSKYConstantPoolConstant::getBlockAddress() const {
100107
return cast<BlockAddress>(CVal);
101108
}
102109

110+
const Constant *CSKYConstantPoolConstant::getConstantPool() const {
111+
return CVal;
112+
}
113+
103114
int CSKYConstantPoolConstant::getExistingMachineCPValue(MachineConstantPool *CP,
104115
Align Alignment) {
105116
return getExistingMachineCPValueImpl<CSKYConstantPoolConstant>(CP, Alignment);

llvm/lib/Target/CSKY/CSKYConstantPoolValue.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ enum CSKYCPKind {
3333
CPExtSymbol,
3434
CPBlockAddress,
3535
CPMachineBasicBlock,
36-
CPJT
36+
CPJT,
37+
CPConstPool
3738
};
3839

3940
enum CSKYCPModifier { NO_MOD, ADDR, GOT, GOTOFF, PLT, TLSLE, TLSIE, TLSGD };
@@ -69,6 +70,7 @@ class CSKYConstantPoolValue : public MachineConstantPoolValue {
6970
return Kind == CSKYCP::CPMachineBasicBlock;
7071
}
7172
bool isJT() const { return Kind == CSKYCP::CPJT; }
73+
bool isConstPool() const { return Kind == CSKYCP::CPConstPool; }
7274

7375
int getExistingMachineCPValue(MachineConstantPool *CP,
7476
Align Alignment) override;
@@ -105,7 +107,7 @@ class CSKYConstantPoolValue : public MachineConstantPoolValue {
105107
class CSKYConstantPoolConstant : public CSKYConstantPoolValue {
106108
const Constant *CVal; // Constant being loaded.
107109

108-
CSKYConstantPoolConstant(const Constant *C, CSKYCP::CSKYCPKind Kind,
110+
CSKYConstantPoolConstant(const Constant *C, Type *Ty, CSKYCP::CSKYCPKind Kind,
109111
unsigned PCAdjust, CSKYCP::CSKYCPModifier Modifier,
110112
bool AddCurrentAddress, unsigned ID);
111113

@@ -114,8 +116,13 @@ class CSKYConstantPoolConstant : public CSKYConstantPoolValue {
114116
Create(const Constant *C, CSKYCP::CSKYCPKind Kind, unsigned PCAdjust,
115117
CSKYCP::CSKYCPModifier Modifier, bool AddCurrentAddress,
116118
unsigned ID = 0);
119+
static CSKYConstantPoolConstant *
120+
Create(const Constant *C, Type *Ty, CSKYCP::CSKYCPKind Kind,
121+
unsigned PCAdjust, CSKYCP::CSKYCPModifier Modifier,
122+
bool AddCurrentAddress, unsigned ID = 0);
117123
const GlobalValue *getGV() const;
118124
const BlockAddress *getBlockAddress() const;
125+
const Constant *getConstantPool() const;
119126

120127
int getExistingMachineCPValue(MachineConstantPool *CP,
121128
Align Alignment) override;
@@ -127,7 +134,7 @@ class CSKYConstantPoolConstant : public CSKYConstantPoolValue {
127134
}
128135

129136
static bool classof(const CSKYConstantPoolValue *APV) {
130-
return APV->isGlobalValue() || APV->isBlockAddress();
137+
return APV->isGlobalValue() || APV->isBlockAddress() || APV->isConstPool();
131138
}
132139
};
133140

llvm/lib/Target/CSKY/CSKYISelLowering.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ CSKYTargetLowering::CSKYTargetLowering(const TargetMachine &TM,
8787
setOperationAction(ISD::ExternalSymbol, MVT::i32, Custom);
8888
setOperationAction(ISD::GlobalTLSAddress, MVT::i32, Custom);
8989
setOperationAction(ISD::BlockAddress, MVT::i32, Custom);
90+
if (!Subtarget.hasE2()) {
91+
setOperationAction(ISD::ConstantPool, MVT::i32, Custom);
92+
}
9093
setOperationAction(ISD::JumpTable, MVT::i32, Custom);
9194
setOperationAction(ISD::VASTART, MVT::Other, Custom);
9295

@@ -170,6 +173,8 @@ SDValue CSKYTargetLowering::LowerOperation(SDValue Op,
170173
return LowerJumpTable(Op, DAG);
171174
case ISD::BlockAddress:
172175
return LowerBlockAddress(Op, DAG);
176+
case ISD::ConstantPool:
177+
return LowerConstantPool(Op, DAG);
173178
case ISD::VASTART:
174179
return LowerVASTART(Op, DAG);
175180
case ISD::FRAMEADDR:
@@ -1058,12 +1063,24 @@ SDValue CSKYTargetLowering::getTargetConstantPoolValue(BlockAddressSDNode *N,
10581063
EVT Ty,
10591064
SelectionDAG &DAG,
10601065
unsigned Flags) const {
1066+
assert(N->getOffset() == 0);
10611067
CSKYConstantPoolValue *CPV = CSKYConstantPoolConstant::Create(
10621068
N->getBlockAddress(), CSKYCP::CPBlockAddress, 0, getModifier(Flags),
10631069
false);
10641070
return DAG.getTargetConstantPool(CPV, Ty);
10651071
}
10661072

1073+
SDValue CSKYTargetLowering::getTargetConstantPoolValue(ConstantPoolSDNode *N,
1074+
EVT Ty,
1075+
SelectionDAG &DAG,
1076+
unsigned Flags) const {
1077+
assert(N->getOffset() == 0);
1078+
CSKYConstantPoolValue *CPV = CSKYConstantPoolConstant::Create(
1079+
N->getConstVal(), Type::getInt32Ty(*DAG.getContext()),
1080+
CSKYCP::CPConstPool, 0, getModifier(Flags), false);
1081+
return DAG.getTargetConstantPool(CPV, Ty);
1082+
}
1083+
10671084
SDValue CSKYTargetLowering::getTargetNode(GlobalAddressSDNode *N, SDLoc DL,
10681085
EVT Ty, SelectionDAG &DAG,
10691086
unsigned Flags) const {
@@ -1089,6 +1106,14 @@ SDValue CSKYTargetLowering::getTargetNode(BlockAddressSDNode *N, SDLoc DL,
10891106
Flags);
10901107
}
10911108

1109+
SDValue CSKYTargetLowering::getTargetNode(ConstantPoolSDNode *N, SDLoc DL,
1110+
EVT Ty, SelectionDAG &DAG,
1111+
unsigned Flags) const {
1112+
1113+
return DAG.getTargetConstantPool(N->getConstVal(), Ty, N->getAlign(),
1114+
N->getOffset(), Flags);
1115+
}
1116+
10921117
const char *CSKYTargetLowering::getTargetNodeName(unsigned Opcode) const {
10931118
switch (Opcode) {
10941119
default:
@@ -1158,6 +1183,14 @@ SDValue CSKYTargetLowering::LowerBlockAddress(SDValue Op,
11581183
return getAddr(N, DAG);
11591184
}
11601185

1186+
SDValue CSKYTargetLowering::LowerConstantPool(SDValue Op,
1187+
SelectionDAG &DAG) const {
1188+
assert(!Subtarget.hasE2());
1189+
ConstantPoolSDNode *N = cast<ConstantPoolSDNode>(Op);
1190+
1191+
return getAddr(N, DAG);
1192+
}
1193+
11611194
SDValue CSKYTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
11621195
MachineFunction &MF = DAG.getMachineFunction();
11631196
CSKYMachineFunctionInfo *FuncInfo = MF.getInfo<CSKYMachineFunctionInfo>();

llvm/lib/Target/CSKY/CSKYISelLowering.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ class CSKYTargetLowering : public TargetLowering {
110110
SDValue getTargetNode(BlockAddressSDNode *N, SDLoc DL, EVT Ty,
111111
SelectionDAG &DAG, unsigned Flags) const;
112112

113+
SDValue getTargetNode(ConstantPoolSDNode *N, SDLoc DL, EVT Ty,
114+
SelectionDAG &DAG, unsigned Flags) const;
115+
113116
SDValue getTargetConstantPoolValue(GlobalAddressSDNode *N, EVT Ty,
114117
SelectionDAG &DAG, unsigned Flags) const;
115118

@@ -122,6 +125,9 @@ class CSKYTargetLowering : public TargetLowering {
122125
SDValue getTargetConstantPoolValue(BlockAddressSDNode *N, EVT Ty,
123126
SelectionDAG &DAG, unsigned Flags) const;
124127

128+
SDValue getTargetConstantPoolValue(ConstantPoolSDNode *N, EVT Ty,
129+
SelectionDAG &DAG, unsigned Flags) const;
130+
125131
template <class NodeTy, bool IsCall = false>
126132
SDValue getAddr(NodeTy *N, SelectionDAG &DAG, bool IsLocal = true) const {
127133
SDLoc DL(N);
@@ -155,6 +161,7 @@ class CSKYTargetLowering : public TargetLowering {
155161
SDValue LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const;
156162
SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
157163
SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;
164+
SDValue LowerConstantPool(SDValue Op, SelectionDAG &DAG) const;
158165
SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
159166
SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG) const;
160167
SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;

llvm/lib/Target/CSKY/CSKYInstrInfo.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,7 @@ def : Pat<(CSKY_LOAD_ADDR tglobaladdr, tconstpool:$src2), (LRW32 tconstpool:$src
11171117
def : Pat<(CSKY_LOAD_ADDR tblockaddress, tconstpool:$src2), (LRW32 tconstpool:$src2)>;
11181118
def : Pat<(CSKY_LOAD_ADDR tjumptable:$src1, tconstpool:$src2), (LRW32_Gen tjumptable:$src1, tconstpool:$src2)>;
11191119
def : Pat<(CSKY_LOAD_ADDR texternalsym, tconstpool:$src2), (LRW32 tconstpool:$src2)>;
1120+
def : Pat<(CSKY_LOAD_ADDR tconstpool:$src1, tconstpool:$src2), (LRW32_Gen tconstpool:$src1, tconstpool:$src2)>;
11201121

11211122
let Predicates = [iHas2E3] in
11221123
def : Pat<(i32 constpool:$src), (GRS32 (to_tconstpool tconstpool:$src))>;

llvm/lib/Target/CSKY/CSKYInstrInfo16Instr.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ def : Pat<(CSKY_LOAD_ADDR tglobaladdr, tconstpool:$src2), (LRW16 tconstpool:$src
472472
def : Pat<(CSKY_LOAD_ADDR tblockaddress, tconstpool:$src2), (LRW16 tconstpool:$src2)>;
473473
def : Pat<(CSKY_LOAD_ADDR tjumptable:$src1, tconstpool:$src2), (LRW16_Gen tjumptable:$src1, tconstpool:$src2)>;
474474
def : Pat<(CSKY_LOAD_ADDR texternalsym, tconstpool:$src2), (LRW16 tconstpool:$src2)>;
475+
def : Pat<(CSKY_LOAD_ADDR tconstpool:$src1, tconstpool:$src2), (LRW16_Gen tconstpool:$src1, tconstpool:$src2)>;
475476

476477
def : Pat<(i32 (load constpool:$src)), (LRW16 (to_tconstpool tconstpool:$src))>;
477478

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2+
; Test get the address of constant pool without grs instruction
3+
; RUN: llc -verify-machineinstrs -csky-no-aliases < %s -mtriple=csky | FileCheck %s --check-prefix=GENERIC
4+
5+
declare i32 @llvm.cttz.i32(i32, i1)
6+
7+
define void @cttztest(i32 %C, i32* %CP) {
8+
; GENERIC-LABEL: cttztest:
9+
; GENERIC: # %bb.0:
10+
; GENERIC-NEXT: .cfi_def_cfa_offset 0
11+
; GENERIC-NEXT: subi16 sp, sp, 4
12+
; GENERIC-NEXT: .cfi_def_cfa_offset 4
13+
; GENERIC-NEXT: movi16 a2, 0
14+
; GENERIC-NEXT: subu16 a2, a2, a0
15+
; GENERIC-NEXT: and16 a2, a0
16+
; GENERIC-NEXT: movi16 a0, 7
17+
; GENERIC-NEXT: lsli16 a0, a0, 24
18+
; GENERIC-NEXT: movi16 a3, 124
19+
; GENERIC-NEXT: lsli16 a3, a3, 16
20+
; GENERIC-NEXT: or16 a3, a0
21+
; GENERIC-NEXT: movi16 a0, 181
22+
; GENERIC-NEXT: lsli16 a0, a0, 8
23+
; GENERIC-NEXT: or16 a0, a3
24+
; GENERIC-NEXT: movi16 a3, 49
25+
; GENERIC-NEXT: or16 a3, a0
26+
; GENERIC-NEXT: mult16 a3, a2
27+
; GENERIC-NEXT: lsri16 a0, a3, 27
28+
; GENERIC-NEXT: lrw32 a2, [.LCPI0_1]
29+
; GENERIC-NEXT: addu16 a0, a2, a0
30+
; GENERIC-NEXT: ld16.b a0, (a0, 0)
31+
; GENERIC-NEXT: st16.w a0, (a1, 0)
32+
; GENERIC-NEXT: addi16 sp, sp, 4
33+
; GENERIC-NEXT: rts16
34+
; GENERIC-NEXT: .p2align 1
35+
; GENERIC-NEXT: # %bb.1:
36+
; GENERIC-NEXT: .p2align 2, 0x0
37+
; GENERIC-NEXT: .LCPI0_1:
38+
; GENERIC-NEXT: .long .LCPI0_0
39+
; GENERIC-NEXT: .LCPI0_0:
40+
; GENERIC-NEXT: .ascii "\000\001\034\002\035\016\030\003\036\026\024\017\031\021\004\b\037\033\r\027\025\023\020\007\032\f\022\006\013\005\n\t"
41+
%c = call i32 @llvm.cttz.i32( i32 %C, i1 true )
42+
store i32 %c, i32* %CP
43+
ret void
44+
}

0 commit comments

Comments
 (0)