Skip to content

Commit b516d58

Browse files
committed
[Xtensa] Fix ConstantPool lowering.
Fix lowering FP immediate constant. Fix loading from constant pool callee, basic block, globaladdress and jumptable addresses. Also fixed potential memory leakage when several similar XtensaConstantPoolValue objects are created, implement unique ID for such objects.
1 parent 06161d4 commit b516d58

File tree

11 files changed

+254
-215
lines changed

11 files changed

+254
-215
lines changed

llvm/lib/Target/Xtensa/XtensaAsmPrinter.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,22 +62,25 @@ void XtensaAsmPrinter::emitInstruction(const MachineInstr *MI) {
6262

6363
void XtensaAsmPrinter::emitMachineConstantPoolValue(
6464
MachineConstantPoolValue *MCPV) {
65-
XtensaConstantPoolValue *ACPV = static_cast<XtensaConstantPoolValue *>(MCPV);
65+
XtensaConstantPoolValue *XtensaCPV =
66+
static_cast<XtensaConstantPoolValue *>(MCPV);
6667
MCSymbol *MCSym;
6768

68-
if (ACPV->isBlockAddress()) {
69+
if (XtensaCPV->isBlockAddress()) {
6970
const BlockAddress *BA =
70-
cast<XtensaConstantPoolConstant>(ACPV)->getBlockAddress();
71+
cast<XtensaConstantPoolConstant>(XtensaCPV)->getBlockAddress();
7172
MCSym = GetBlockAddressSymbol(BA);
72-
} else if (ACPV->isMachineBasicBlock()) {
73-
const MachineBasicBlock *MBB = cast<XtensaConstantPoolMBB>(ACPV)->getMBB();
73+
} else if (XtensaCPV->isMachineBasicBlock()) {
74+
const MachineBasicBlock *MBB =
75+
cast<XtensaConstantPoolMBB>(XtensaCPV)->getMBB();
7476
MCSym = MBB->getSymbol();
75-
} else if (ACPV->isJumpTable()) {
76-
unsigned Idx = cast<XtensaConstantPoolJumpTable>(ACPV)->getIndex();
77+
} else if (XtensaCPV->isJumpTable()) {
78+
unsigned Idx = cast<XtensaConstantPoolJumpTable>(XtensaCPV)->getIndex();
7779
MCSym = this->GetJTISymbol(Idx, false);
7880
} else {
79-
assert(ACPV->isExtSymbol() && "unrecognized constant pool value");
80-
XtensaConstantPoolSymbol *XtensaSym = cast<XtensaConstantPoolSymbol>(ACPV);
81+
assert(XtensaCPV->isExtSymbol() && "unrecognized constant pool value");
82+
XtensaConstantPoolSymbol *XtensaSym =
83+
cast<XtensaConstantPoolSymbol>(XtensaCPV);
8184
const char *SymName = XtensaSym->getSymbol();
8285

8386
if (XtensaSym->isPrivateLinkage()) {
@@ -89,14 +92,14 @@ void XtensaAsmPrinter::emitMachineConstantPoolValue(
8992
}
9093
}
9194

92-
MCSymbol *LblSym = GetCPISymbol(ACPV->getLabelId());
95+
MCSymbol *LblSym = GetCPISymbol(XtensaCPV->getLabelId());
9396
auto *TS =
9497
static_cast<XtensaTargetStreamer *>(OutStreamer->getTargetStreamer());
95-
auto Spec = getModifierSpecifier(ACPV->getModifier());
98+
auto Spec = getModifierSpecifier(XtensaCPV->getModifier());
9699

97-
if (ACPV->getModifier() != XtensaCP::no_modifier) {
100+
if (XtensaCPV->getModifier() != XtensaCP::no_modifier) {
98101
std::string SymName(MCSym->getName());
99-
StringRef Modifier = ACPV->getModifierText();
102+
StringRef Modifier = XtensaCPV->getModifierText();
100103
SymName += Modifier;
101104
MCSym = OutContext.getOrCreateSymbol(SymName);
102105
}
@@ -108,9 +111,9 @@ void XtensaAsmPrinter::emitMachineConstantPoolValue(
108111
void XtensaAsmPrinter::emitMachineConstantPoolEntry(
109112
const MachineConstantPoolEntry &CPE, int i) {
110113
if (CPE.isMachineConstantPoolEntry()) {
111-
XtensaConstantPoolValue *ACPV =
114+
XtensaConstantPoolValue *XtensaCPV =
112115
static_cast<XtensaConstantPoolValue *>(CPE.Val.MachineCPVal);
113-
ACPV->setLabelId(i);
116+
XtensaCPV->setLabelId(i);
114117
emitMachineConstantPoolValue(CPE.Val.MachineCPVal);
115118
} else {
116119
MCSymbol *LblSym = GetCPISymbol(i);

llvm/lib/Target/Xtensa/XtensaISelDAGToDAG.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class XtensaDAGToDAGISel : public SelectionDAGISel {
5050
int Scale) {
5151
EVT ValTy = Addr.getValueType();
5252

53+
if (Addr.getOpcode() == XtensaISD::PCREL_WRAPPER) {
54+
Base = Addr.getOperand(0);
55+
if (Base.getOpcode() == ISD::TargetConstantPool)
56+
return false; // We want to select L32R instead.
57+
}
58+
5359
// if Address is FI, get the TargetFrameIndex.
5460
if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
5561
Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), ValTy);

llvm/lib/Target/Xtensa/XtensaISelLowering.cpp

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ XtensaTargetLowering::XtensaTargetLowering(const TargetMachine &TM,
7575

7676
setOperationAction(ISD::Constant, MVT::i32, Custom);
7777
setOperationAction(ISD::Constant, MVT::i64, Expand);
78-
setOperationAction(ISD::ConstantFP, MVT::f32, Custom);
78+
setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
7979
setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
8080

8181
setBooleanContents(ZeroOrOneBooleanContent);
@@ -687,15 +687,19 @@ XtensaTargetLowering::LowerCall(CallLoweringInfo &CLI,
687687
if ((!name.empty()) && isLongCall(name.c_str())) {
688688
// Create a constant pool entry for the callee address
689689
XtensaCP::XtensaCPModifier Modifier = XtensaCP::no_modifier;
690+
XtensaMachineFunctionInfo *XtensaFI =
691+
MF.getInfo<XtensaMachineFunctionInfo>();
692+
unsigned LabelId = XtensaFI->createCPLabelId();
690693

691694
XtensaConstantPoolValue *CPV = XtensaConstantPoolSymbol::Create(
692-
*DAG.getContext(), name.c_str(), 0 /* XtensaCLabelIndex */, false,
693-
Modifier);
695+
*DAG.getContext(), name.c_str(), LabelId, false, Modifier);
694696

695697
// Get the address of the callee into a register
696698
SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, Align(4), 0, TF);
697699
SDValue CPWrap = getAddrPCRel(CPAddr, DAG);
698-
Callee = CPWrap;
700+
Callee = DAG.getLoad(
701+
PtrVT, DL, DAG.getEntryNode(), CPWrap,
702+
MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
699703
}
700704

701705
// The first call operand is the chain and the second is the target address.
@@ -893,22 +897,9 @@ SDValue XtensaTargetLowering::LowerImmediate(SDValue Op,
893897
Type *Ty = Type::getInt32Ty(*DAG.getContext());
894898
Constant *CV = ConstantInt::get(Ty, Value);
895899
SDValue CP = DAG.getConstantPool(CV, MVT::i32);
896-
return CP;
897-
}
898-
return Op;
899-
}
900-
901-
SDValue XtensaTargetLowering::LowerImmediateFP(SDValue Op,
902-
SelectionDAG &DAG) const {
903-
const ConstantFPSDNode *CN = cast<ConstantFPSDNode>(Op);
904-
SDLoc DL(CN);
905-
APFloat apval = CN->getValueAPF();
906-
int64_t value = llvm::bit_cast<uint32_t>(CN->getValueAPF().convertToFloat());
907-
if (Op.getValueType() == MVT::f32) {
908-
Type *Ty = Type::getInt32Ty(*DAG.getContext());
909-
Constant *CV = ConstantInt::get(Ty, value);
910-
SDValue CP = DAG.getConstantPool(CV, MVT::i32);
911-
return DAG.getNode(ISD::BITCAST, DL, MVT::f32, CP);
900+
SDValue Res =
901+
DAG.getLoad(MVT::i32, DL, DAG.getEntryNode(), CP, MachinePointerInfo());
902+
return Res;
912903
}
913904
return Op;
914905
}
@@ -922,22 +913,30 @@ SDValue XtensaTargetLowering::LowerGlobalAddress(SDValue Op,
922913

923914
SDValue CPAddr = DAG.getTargetConstantPool(GV, PtrVT, Align(4));
924915
SDValue CPWrap = getAddrPCRel(CPAddr, DAG);
925-
926-
return CPWrap;
916+
SDValue Res = DAG.getLoad(
917+
PtrVT, DL, DAG.getEntryNode(), CPWrap,
918+
MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
919+
return Res;
927920
}
928921

929922
SDValue XtensaTargetLowering::LowerBlockAddress(SDValue Op,
930923
SelectionDAG &DAG) const {
931924
BlockAddressSDNode *Node = cast<BlockAddressSDNode>(Op);
925+
SDLoc DL(Op);
932926
const BlockAddress *BA = Node->getBlockAddress();
933927
EVT PtrVT = Op.getValueType();
928+
MachineFunction &MF = DAG.getMachineFunction();
929+
XtensaMachineFunctionInfo *XtensaFI = MF.getInfo<XtensaMachineFunctionInfo>();
930+
unsigned LabelId = XtensaFI->createCPLabelId();
934931

935932
XtensaConstantPoolValue *CPV =
936-
XtensaConstantPoolConstant::Create(BA, 0, XtensaCP::CPBlockAddress);
933+
XtensaConstantPoolConstant::Create(BA, LabelId, XtensaCP::CPBlockAddress);
937934
SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, Align(4));
938935
SDValue CPWrap = getAddrPCRel(CPAddr, DAG);
939-
940-
return CPWrap;
936+
SDValue Res = DAG.getLoad(
937+
PtrVT, DL, DAG.getEntryNode(), CPWrap,
938+
MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
939+
return Res;
941940
}
942941

943942
SDValue XtensaTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
@@ -972,15 +971,19 @@ SDValue XtensaTargetLowering::LowerJumpTable(SDValue Op,
972971
SelectionDAG &DAG) const {
973972
JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
974973
EVT PtrVT = Op.getValueType();
974+
SDLoc DL(Op);
975975

976-
// Create a constant pool entry for the callee address
976+
// Create a constant pool entry for the jumptable address
977977
XtensaConstantPoolValue *CPV =
978978
XtensaConstantPoolJumpTable::Create(*DAG.getContext(), JT->getIndex());
979979

980-
// Get the address of the callee into a register
980+
// Get the address of the jumptable into a register
981981
SDValue CPAddr = DAG.getTargetConstantPool(CPV, PtrVT, Align(4));
982982

983-
return getAddrPCRel(CPAddr, DAG);
983+
SDValue Res = DAG.getLoad(
984+
PtrVT, DL, DAG.getEntryNode(), getAddrPCRel(CPAddr, DAG),
985+
MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
986+
return Res;
984987
}
985988

986989
SDValue XtensaTargetLowering::getAddrPCRel(SDValue Op,
@@ -1346,8 +1349,6 @@ SDValue XtensaTargetLowering::LowerOperation(SDValue Op,
13461349
return LowerBR_JT(Op, DAG);
13471350
case ISD::Constant:
13481351
return LowerImmediate(Op, DAG);
1349-
case ISD::ConstantFP:
1350-
return LowerImmediateFP(Op, DAG);
13511352
case ISD::RETURNADDR:
13521353
return LowerRETURNADDR(Op, DAG);
13531354
case ISD::GlobalAddress:

llvm/lib/Target/Xtensa/XtensaISelLowering.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,6 @@ class XtensaTargetLowering : public TargetLowering {
154154

155155
SDValue LowerImmediate(SDValue Op, SelectionDAG &DAG) const;
156156

157-
SDValue LowerImmediateFP(SDValue Op, SelectionDAG &DAG) const;
158-
159157
SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
160158

161159
SDValue LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const;

llvm/lib/Target/Xtensa/XtensaInstrInfo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,10 @@ void XtensaInstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
524524
JumpToMBB = &RestoreBB;
525525
}
526526

527+
unsigned LabelId = XtensaFI->createCPLabelId();
528+
527529
XtensaConstantPoolValue *C = XtensaConstantPoolMBB::Create(
528-
MF->getFunction().getContext(), JumpToMBB, 0);
530+
MF->getFunction().getContext(), JumpToMBB, LabelId);
529531
unsigned Idx = ConstantPool->getConstantPoolIndex(C, Align(4));
530532
L32R.addOperand(MachineOperand::CreateCPI(Idx, 0));
531533

llvm/lib/Target/Xtensa/XtensaInstrInfo.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,11 @@ def S16I : Store_II8<0x05, "s16i", truncstorei16, addr_ish2, mem16>;
235235
def S32I : Store_II8<0x06, "s32i", store, addr_ish4, mem32>;
236236

237237
def L32R : RI16_Inst<0x01, (outs AR:$t), (ins L32Rtarget:$label),
238-
"l32r\t$t, $label", []> {
238+
"l32r\t$t, $label", [(set AR:$t, (load (Xtensa_pcrel_wrapper tconstpool:$label)))]> {
239239
bits<16> label;
240240
let imm16 = label;
241241
}
242242

243-
// pcrel addr loading using L32R
244-
def : Pat<(Xtensa_pcrel_wrapper tconstpool : $in), (L32R tconstpool : $in)>;
245-
246243
// FrameIndexes are legalized when they are operands from load/store
247244
// instructions. The same not happens for stack address copies, so an
248245
// add op with mem ComplexPattern is used and the stack address copy
@@ -1321,6 +1318,9 @@ def WFR : RRR_Inst<0x00, 0x0A, 0x0f, (outs FPR:$r), (ins AR:$s),
13211318
let t = 0x05;
13221319
}
13231320

1321+
def : Pat<(f32 (load (Xtensa_pcrel_wrapper tconstpool:$in))),
1322+
(WFR (L32R tconstpool:$in))>;
1323+
13241324
//===----------------------------------------------------------------------===//
13251325
// Region Protection feature instructions
13261326
//===----------------------------------------------------------------------===//

llvm/lib/Target/Xtensa/XtensaMachineFunctionInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class XtensaMachineFunctionInfo : public MachineFunctionInfo {
2828
int VarArgsOnStackFrameIndex;
2929
int VarArgsInRegsFrameIndex;
3030
bool SaveFrameRegister = false;
31+
unsigned CPLabelId = 0;
3132

3233
public:
3334
explicit XtensaMachineFunctionInfo(const Function &F,
@@ -54,6 +55,8 @@ class XtensaMachineFunctionInfo : public MachineFunctionInfo {
5455

5556
bool isSaveFrameRegister() const { return SaveFrameRegister; }
5657
void setSaveFrameRegister() { SaveFrameRegister = true; }
58+
59+
unsigned createCPLabelId() { return CPLabelId++; }
5760
};
5861

5962
} // namespace llvm

llvm/test/CodeGen/Xtensa/aligned_alloc.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ define i8 @loadi8_128(i8 %a) {
1111
; XTENSA-NEXT: .cfi_def_cfa_offset 128
1212
; XTENSA-NEXT: s32i a0, a1, 124 # 4-byte Folded Spill
1313
; XTENSA-NEXT: .cfi_offset a0, -4
14+
; XTENSA-NEXT: l32r a8, .LCPI0_0
1415
; XTENSA-NEXT: addi a2, a1, 0
1516
; XTENSA-NEXT: movi a3, 0
1617
; XTENSA-NEXT: movi a4, 64
17-
; XTENSA-NEXT: l32r a8, .LCPI0_0
1818
; XTENSA-NEXT: callx0 a8
1919
; XTENSA-NEXT: l8ui a2, a1, 0
2020
; XTENSA-NEXT: l32i a0, a1, 124 # 4-byte Folded Reload

0 commit comments

Comments
 (0)