Skip to content

Commit d313dfd

Browse files
author
Chen Zheng
committed
redesign the target flags for ppc
1 parent fd1a0b0 commit d313dfd

12 files changed

+144
-107
lines changed

llvm/lib/Target/PowerPC/PPC.h

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,79 +102,99 @@ class ModulePass;
102102
// PPC Specific MachineOperand flags.
103103
MO_NO_FLAG,
104104

105+
/// On PPC, the 12 bits are not enough for all target operand flags.
106+
/// Treat all PPC target flags as direct flags. This also means we can not
107+
/// use a bitmask flag, so if one operand has two or more flags, a fake
108+
/// combination flag must be created. See example MO_GOT_TPREL_PCREL_FLAG.
109+
105110
/// On a symbol operand "FOO", this indicates that the reference is actually
106111
/// to "FOO@plt". This is used for calls and jumps to external functions
107112
/// and for PIC calls on 32-bit ELF systems.
108-
MO_PLT = 1,
113+
MO_PLT,
109114

110115
/// MO_PIC_FLAG - If this bit is set, the symbol reference is relative to
111116
/// the function's picbase, e.g. lo16(symbol-picbase).
112-
MO_PIC_FLAG = 2,
117+
MO_PIC_FLAG,
113118

114119
/// MO_PCREL_FLAG - If this bit is set, the symbol reference is relative to
115120
/// the current instruction address(pc), e.g., var@pcrel. Fixup is VK_PCREL.
116-
MO_PCREL_FLAG = 4,
121+
MO_PCREL_FLAG,
117122

118123
/// MO_GOT_FLAG - If this bit is set the symbol reference is to be computed
119124
/// via the GOT. For example when combined with the MO_PCREL_FLAG it should
120125
/// produce the relocation @got@pcrel. Fixup is VK_PPC_GOT_PCREL.
121-
MO_GOT_FLAG = 8,
126+
MO_GOT_FLAG,
122127

123-
// MO_PCREL_OPT_FLAG - If this bit is set the operand is part of a
124-
// PC Relative linker optimization.
125-
MO_PCREL_OPT_FLAG = 16,
128+
/// MO_PCREL_OPT_FLAG - If this bit is set the operand is part of a
129+
/// PC Relative linker optimization.
130+
MO_PCREL_OPT_FLAG,
126131

127132
/// MO_TLSGD_FLAG - If this bit is set the symbol reference is relative to
128133
/// TLS General Dynamic model for Linux and the variable offset of TLS
129134
/// General Dynamic model for AIX.
130-
MO_TLSGD_FLAG = 32,
135+
MO_TLSGD_FLAG,
131136

132137
/// MO_TPREL_FLAG - If this bit is set, the symbol reference is relative to
133138
/// the thread pointer and the symbol can be used for the TLS Initial Exec
134139
/// and Local Exec models.
135-
MO_TPREL_FLAG = 64,
140+
MO_TPREL_FLAG,
136141

137142
/// MO_TLSLD_FLAG - If this bit is set the symbol reference is relative to
138143
/// TLS Local Dynamic model.
139-
MO_TLSLD_FLAG = 128,
144+
MO_TLSLD_FLAG,
140145

141146
/// MO_TLSGDM_FLAG - If this bit is set the symbol reference is relative
142147
/// to the region handle of TLS General Dynamic model for AIX.
143-
MO_TLSGDM_FLAG = 256,
148+
MO_TLSGDM_FLAG,
144149

145150
/// MO_GOT_TLSGD_PCREL_FLAG - A combintaion of flags, if these bits are set
146151
/// they should produce the relocation @got@tlsgd@pcrel.
147152
/// Fix up is VK_PPC_GOT_TLSGD_PCREL
148-
MO_GOT_TLSGD_PCREL_FLAG = MO_PCREL_FLAG | MO_GOT_FLAG | MO_TLSGD_FLAG,
153+
/// MO_GOT_TLSGD_PCREL_FLAG = MO_PCREL_FLAG | MO_GOT_FLAG | MO_TLSGD_FLAG,
154+
MO_GOT_TLSGD_PCREL_FLAG,
149155

150156
/// MO_GOT_TLSLD_PCREL_FLAG - A combintaion of flags, if these bits are set
151157
/// they should produce the relocation @got@tlsld@pcrel.
152158
/// Fix up is VK_PPC_GOT_TLSLD_PCREL
153-
MO_GOT_TLSLD_PCREL_FLAG = MO_PCREL_FLAG | MO_GOT_FLAG | MO_TLSLD_FLAG,
159+
/// MO_GOT_TLSLD_PCREL_FLAG = MO_PCREL_FLAG | MO_GOT_FLAG | MO_TLSLD_FLAG,
160+
MO_GOT_TLSLD_PCREL_FLAG,
154161

155162
/// MO_GOT_TPREL_PCREL_FLAG - A combintaion of flags, if these bits are set
156163
/// they should produce the relocation @got@tprel@pcrel.
157164
/// Fix up is VK_PPC_GOT_TPREL_PCREL
158-
MO_GOT_TPREL_PCREL_FLAG = MO_GOT_FLAG | MO_TPREL_FLAG | MO_PCREL_FLAG,
159-
160-
/// The next are not flags but distinct values.
161-
MO_ACCESS_MASK = 0xf00,
165+
/// MO_GOT_TPREL_PCREL_FLAG = MO_GOT_FLAG | MO_TPREL_FLAG | MO_PCREL_FLAG,
166+
MO_GOT_TPREL_PCREL_FLAG,
162167

163168
/// MO_LO, MO_HA - lo16(symbol) and ha16(symbol)
164-
MO_LO = 1 << 8,
165-
MO_HA = 2 << 8,
169+
MO_LO,
170+
MO_HA,
166171

167-
MO_TPREL_LO = 4 << 8,
168-
MO_TPREL_HA = 3 << 8,
172+
MO_TPREL_LO,
173+
MO_TPREL_HA,
169174

170175
/// These values identify relocations on immediates folded
171176
/// into memory operations.
172-
MO_DTPREL_LO = 5 << 8,
173-
MO_TLSLD_LO = 6 << 8,
174-
MO_TOC_LO = 7 << 8,
177+
MO_DTPREL_LO,
178+
MO_TLSLD_LO,
179+
MO_TOC_LO,
180+
181+
/// Symbol for VK_PPC_TLS fixup attached to an ADD instruction
182+
MO_TLS,
183+
184+
/// MO_PIC_HA_FLAG = MO_PIC_FLAG | MO_HA
185+
MO_PIC_HA_FLAG,
186+
187+
/// MO_PIC_LO_FLAG = MO_PIC_FLAG | MO_LO
188+
MO_PIC_LO_FLAG,
189+
190+
/// MO_TPREL_PCREL_FLAG = MO_PCREL_FLAG | MO_TPREL_FLAG
191+
MO_TPREL_PCREL_FLAG,
192+
193+
/// MO_TPREL_PCREL_FLAG = MO_PCREL_FLAG | MO_TLS
194+
MO_TLS_PCREL_FLAG,
175195

176-
// Symbol for VK_PPC_TLS fixup attached to an ADD instruction
177-
MO_TLS = 8 << 8
196+
/// MO_GOT_PCREL_FLAG = MO_PCREL_FLAG | MO_GOT_FLAG
197+
MO_GOT_PCREL_FLAG,
178198
};
179199
} // end namespace PPCII
180200

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -715,25 +715,11 @@ static MCSymbol *getMCSymbolForTOCPseudoMO(const MachineOperand &MO,
715715
}
716716
}
717717

718-
static bool hasTLSFlag(const MachineOperand &MO) {
719-
unsigned Flags = MO.getTargetFlags();
720-
if (Flags & PPCII::MO_TLSGD_FLAG || Flags & PPCII::MO_TPREL_FLAG ||
721-
Flags & PPCII::MO_TLSLD_FLAG || Flags & PPCII::MO_TLSGDM_FLAG)
722-
return true;
723-
724-
if (Flags == PPCII::MO_TPREL_LO || Flags == PPCII::MO_TPREL_HA ||
725-
Flags == PPCII::MO_DTPREL_LO || Flags == PPCII::MO_TLSLD_LO ||
726-
Flags == PPCII::MO_TLS)
727-
return true;
728-
729-
return false;
730-
}
731-
732718
static PPCAsmPrinter::TOCEntryType
733719
getTOCEntryTypeForMO(const MachineOperand &MO) {
734720
// Use the target flags to determine if this MO is Thread Local.
735721
// If we don't do this it comes out as Global.
736-
if (hasTLSFlag(MO))
722+
if (PPCInstrInfo::hasTLSFlag(MO.getTargetFlags()))
737723
return PPCAsmPrinter::TOCType_ThreadLocal;
738724

739725
switch (MO.getType()) {
@@ -830,7 +816,9 @@ void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
830816
// For TLS initial-exec and local-exec accesses on AIX, we have one TOC
831817
// entry for the symbol (with the variable offset), which is differentiated
832818
// by MO_TPREL_FLAG.
833-
if (MO.getTargetFlags() & PPCII::MO_TPREL_FLAG) {
819+
if (MO.getTargetFlags() == PPCII::MO_TPREL_FLAG ||
820+
MO.getTargetFlags() == PPCII::MO_GOT_TPREL_PCREL_FLAG ||
821+
MO.getTargetFlags() == PPCII::MO_TPREL_PCREL_FLAG) {
834822
assert(MO.isGlobal() && "Only expecting a global MachineOperand here!\n");
835823
TLSModel::Model Model = TM.getTLSModel(MO.getGlobal());
836824
if (Model == TLSModel::LocalExec)
@@ -842,9 +830,10 @@ void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
842830
// For GD TLS access on AIX, we have two TOC entries for the symbol (one for
843831
// the variable offset and the other for the region handle). They are
844832
// differentiated by MO_TLSGD_FLAG and MO_TLSGDM_FLAG.
845-
if (MO.getTargetFlags() & PPCII::MO_TLSGDM_FLAG)
833+
if (MO.getTargetFlags() == PPCII::MO_TLSGDM_FLAG)
846834
return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGDM;
847-
if (MO.getTargetFlags() & PPCII::MO_TLSGD_FLAG)
835+
if (MO.getTargetFlags() == PPCII::MO_TLSGD_FLAG ||
836+
MO.getTargetFlags() == PPCII::MO_GOT_TLSGD_PCREL_FLAG)
848837
return MCSymbolRefExpr::VariantKind::VK_PPC_AIX_TLSGD;
849838
return MCSymbolRefExpr::VariantKind::VK_None;
850839
};
@@ -1539,7 +1528,9 @@ void PPCAsmPrinter::emitInstruction(const MachineInstr *MI) {
15391528
// with an immediate operand having the MO_TPREL_FLAG. Such an instruction
15401529
// does not otherwise arise.
15411530
const MachineOperand &MO = MI->getOperand(2);
1542-
if ((MO.getTargetFlags() & PPCII::MO_TPREL_FLAG) != 0) {
1531+
if (MO.getTargetFlags() == PPCII::MO_TPREL_FLAG ||
1532+
MO.getTargetFlags() == PPCII::MO_GOT_TPREL_PCREL_FLAG ||
1533+
MO.getTargetFlags() == PPCII::MO_TPREL_PCREL_FLAG) {
15431534
assert(
15441535
Subtarget->hasAIXSmallLocalExecTLS() &&
15451536
"addi with thread-pointer only expected with local-exec small TLS");

llvm/lib/Target/PowerPC/PPCISelLowering.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2971,7 +2971,7 @@ bool PPCTargetLowering::SelectAddressRegRegOnly(SDValue N, SDValue &Base,
29712971

29722972
template <typename Ty> static bool isValidPCRelNode(SDValue N) {
29732973
Ty *PCRelCand = dyn_cast<Ty>(N);
2974-
return PCRelCand && (PCRelCand->getTargetFlags() & PPCII::MO_PCREL_FLAG);
2974+
return PCRelCand && (PPCInstrInfo::hasPCRelFlag(PCRelCand->getTargetFlags()));
29752975
}
29762976

29772977
/// Returns true if this address is a PC Relative address.
@@ -3132,8 +3132,8 @@ static void getLabelAccessInfo(bool IsPIC, const PPCSubtarget &Subtarget,
31323132

31333133
// Don't use the pic base if not in PIC relocation model.
31343134
if (IsPIC) {
3135-
HiOpFlags |= PPCII::MO_PIC_FLAG;
3136-
LoOpFlags |= PPCII::MO_PIC_FLAG;
3135+
HiOpFlags = PPCII::MO_PIC_HA_FLAG;
3136+
LoOpFlags = PPCII::MO_PIC_LO_FLAG;
31373137
}
31383138
}
31393139

@@ -3452,8 +3452,8 @@ SDValue PPCTargetLowering::LowerGlobalTLSAddressLinux(SDValue Op,
34523452
if (Model == TLSModel::LocalExec) {
34533453
if (Subtarget.isUsingPCRelativeCalls()) {
34543454
SDValue TLSReg = DAG.getRegister(PPC::X13, MVT::i64);
3455-
SDValue TGA = DAG.getTargetGlobalAddress(
3456-
GV, dl, PtrVT, 0, (PPCII::MO_PCREL_FLAG | PPCII::MO_TPREL_FLAG));
3455+
SDValue TGA = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0,
3456+
PPCII::MO_TPREL_PCREL_FLAG);
34573457
SDValue MatAddr =
34583458
DAG.getNode(PPCISD::TLS_LOCAL_EXEC_MAT_ADDR, dl, PtrVT, TGA);
34593459
return DAG.getNode(PPCISD::ADD_TLS, dl, PtrVT, TLSReg, MatAddr);
@@ -3475,8 +3475,7 @@ SDValue PPCTargetLowering::LowerGlobalTLSAddressLinux(SDValue Op,
34753475
SDValue TGA = DAG.getTargetGlobalAddress(
34763476
GV, dl, PtrVT, 0, IsPCRel ? PPCII::MO_GOT_TPREL_PCREL_FLAG : 0);
34773477
SDValue TGATLS = DAG.getTargetGlobalAddress(
3478-
GV, dl, PtrVT, 0,
3479-
IsPCRel ? (PPCII::MO_TLS | PPCII::MO_PCREL_FLAG) : PPCII::MO_TLS);
3478+
GV, dl, PtrVT, 0, IsPCRel ? PPCII::MO_TLS_PCREL_FLAG : PPCII::MO_TLS);
34803479
SDValue TPOffset;
34813480
if (IsPCRel) {
34823481
SDValue MatPCRel = DAG.getNode(PPCISD::MAT_PCREL_ADDR, dl, PtrVT, TGA);
@@ -3572,8 +3571,7 @@ SDValue PPCTargetLowering::LowerGlobalAddress(SDValue Op,
35723571
EVT Ty = getPointerTy(DAG.getDataLayout());
35733572
if (isAccessedAsGotIndirect(Op)) {
35743573
SDValue GA = DAG.getTargetGlobalAddress(GV, DL, Ty, GSDN->getOffset(),
3575-
PPCII::MO_PCREL_FLAG |
3576-
PPCII::MO_GOT_FLAG);
3574+
PPCII::MO_GOT_PCREL_FLAG);
35773575
SDValue MatPCRel = DAG.getNode(PPCISD::MAT_PCREL_ADDR, DL, Ty, GA);
35783576
SDValue Load = DAG.getLoad(MVT::i64, DL, DAG.getEntryNode(), MatPCRel,
35793577
MachinePointerInfo());

llvm/lib/Target/PowerPC/PPCInstrInfo.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2973,27 +2973,12 @@ unsigned PPCInstrInfo::getInstSizeInBytes(const MachineInstr &MI) const {
29732973

29742974
std::pair<unsigned, unsigned>
29752975
PPCInstrInfo::decomposeMachineOperandsTargetFlags(unsigned TF) const {
2976-
const unsigned Mask = PPCII::MO_ACCESS_MASK;
2977-
return std::make_pair(TF & Mask, TF & ~Mask);
2976+
// PPC always uses a direct mask.
2977+
return std::make_pair(TF, 0u);
29782978
}
29792979

29802980
ArrayRef<std::pair<unsigned, const char *>>
29812981
PPCInstrInfo::getSerializableDirectMachineOperandTargetFlags() const {
2982-
using namespace PPCII;
2983-
static const std::pair<unsigned, const char *> TargetFlags[] = {
2984-
{MO_LO, "ppc-lo"},
2985-
{MO_HA, "ppc-ha"},
2986-
{MO_TPREL_LO, "ppc-tprel-lo"},
2987-
{MO_TPREL_HA, "ppc-tprel-ha"},
2988-
{MO_DTPREL_LO, "ppc-dtprel-lo"},
2989-
{MO_TLSLD_LO, "ppc-tlsld-lo"},
2990-
{MO_TOC_LO, "ppc-toc-lo"},
2991-
{MO_TLS, "ppc-tls"}};
2992-
return ArrayRef(TargetFlags);
2993-
}
2994-
2995-
ArrayRef<std::pair<unsigned, const char *>>
2996-
PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
29972982
using namespace PPCII;
29982983
static const std::pair<unsigned, const char *> TargetFlags[] = {
29992984
{MO_PLT, "ppc-plt"},
@@ -3002,12 +2987,26 @@ PPCInstrInfo::getSerializableBitmaskMachineOperandTargetFlags() const {
30022987
{MO_GOT_FLAG, "ppc-got"},
30032988
{MO_PCREL_OPT_FLAG, "ppc-opt-pcrel"},
30042989
{MO_TLSGD_FLAG, "ppc-tlsgd"},
3005-
{MO_TLSLD_FLAG, "ppc-tlsld"},
30062990
{MO_TPREL_FLAG, "ppc-tprel"},
2991+
{MO_TLSLD_FLAG, "ppc-tlsld"},
30072992
{MO_TLSGDM_FLAG, "ppc-tlsgdm"},
30082993
{MO_GOT_TLSGD_PCREL_FLAG, "ppc-got-tlsgd-pcrel"},
30092994
{MO_GOT_TLSLD_PCREL_FLAG, "ppc-got-tlsld-pcrel"},
3010-
{MO_GOT_TPREL_PCREL_FLAG, "ppc-got-tprel-pcrel"}};
2995+
{MO_GOT_TPREL_PCREL_FLAG, "ppc-got-tprel-pcrel"},
2996+
{MO_LO, "ppc-lo"},
2997+
{MO_HA, "ppc-ha"},
2998+
{MO_TPREL_LO, "ppc-tprel-lo"},
2999+
{MO_TPREL_HA, "ppc-tprel-ha"},
3000+
{MO_DTPREL_LO, "ppc-dtprel-lo"},
3001+
{MO_TLSLD_LO, "ppc-tlsld-lo"},
3002+
{MO_TOC_LO, "ppc-toc-lo"},
3003+
{MO_TLS, "ppc-tls"},
3004+
{MO_PIC_HA_FLAG, "ppc-ha-pic"},
3005+
{MO_PIC_LO_FLAG, "ppc-lo-pic"},
3006+
{MO_TPREL_PCREL_FLAG, "ppc-tprel-pcrel"},
3007+
{MO_TLS_PCREL_FLAG, "ppc-tls-pcrel"},
3008+
{MO_GOT_PCREL_FLAG, "ppc-got-pcrel"},
3009+
};
30113010
return ArrayRef(TargetFlags);
30123011
}
30133012

llvm/lib/Target/PowerPC/PPCInstrInfo.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_LIB_TARGET_POWERPC_PPCINSTRINFO_H
1515

1616
#include "MCTargetDesc/PPCMCTargetDesc.h"
17+
#include "PPC.h"
1718
#include "PPCRegisterInfo.h"
1819
#include "llvm/ADT/SmallSet.h"
1920
#include "llvm/CodeGen/TargetInstrInfo.h"
@@ -283,6 +284,32 @@ class PPCInstrInfo : public PPCGenInstrInfo {
283284
return false;
284285
}
285286

287+
static bool hasPCRelFlag(unsigned TF) {
288+
return TF == PPCII::MO_PCREL_FLAG || TF == PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
289+
TF == PPCII::MO_GOT_TLSLD_PCREL_FLAG ||
290+
TF == PPCII::MO_GOT_TPREL_PCREL_FLAG ||
291+
TF == PPCII::MO_TPREL_PCREL_FLAG || TF == PPCII::MO_TLS_PCREL_FLAG ||
292+
TF == PPCII::MO_GOT_PCREL_FLAG;
293+
}
294+
295+
static bool hasGOTFlag(unsigned TF) {
296+
return TF == PPCII::MO_GOT_FLAG || TF == PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
297+
TF == PPCII::MO_GOT_TLSLD_PCREL_FLAG ||
298+
TF == PPCII::MO_GOT_TPREL_PCREL_FLAG ||
299+
TF == PPCII::MO_GOT_PCREL_FLAG;
300+
}
301+
302+
static bool hasTLSFlag(unsigned TF) {
303+
return TF == PPCII::MO_TLSGD_FLAG || TF == PPCII::MO_TPREL_FLAG ||
304+
TF == PPCII::MO_TLSLD_FLAG || TF == PPCII::MO_TLSGDM_FLAG ||
305+
TF == PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
306+
TF == PPCII::MO_GOT_TLSLD_PCREL_FLAG ||
307+
TF == PPCII::MO_GOT_TPREL_PCREL_FLAG || TF == PPCII::MO_TPREL_LO ||
308+
TF == PPCII::MO_TPREL_HA || TF == PPCII::MO_DTPREL_LO ||
309+
TF == PPCII::MO_TLSLD_LO || TF == PPCII::MO_TLS ||
310+
TF == PPCII::MO_TPREL_PCREL_FLAG || TF == PPCII::MO_TLS_PCREL_FLAG;
311+
}
312+
286313
ScheduleHazardRecognizer *
287314
CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
288315
const ScheduleDAG *DAG) const override;
@@ -555,9 +582,6 @@ class PPCInstrInfo : public PPCGenInstrInfo {
555582
ArrayRef<std::pair<unsigned, const char *>>
556583
getSerializableDirectMachineOperandTargetFlags() const override;
557584

558-
ArrayRef<std::pair<unsigned, const char *>>
559-
getSerializableBitmaskMachineOperandTargetFlags() const override;
560-
561585
// Expand VSX Memory Pseudo instruction to either a VSX or a FP instruction.
562586
bool expandVSXMemPseudo(MachineInstr &MI) const;
563587

0 commit comments

Comments
 (0)