Skip to content

Commit 84a7cdf

Browse files
committed
Revert "[X86] Respect code models more when determining if a global reference can fit in 32 bits (llvm#75386)"
This reverts commit 5e38ba2.
1 parent af0cab9 commit 84a7cdf

File tree

7 files changed

+142
-208
lines changed

7 files changed

+142
-208
lines changed

llvm/include/llvm/Target/TargetMachine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class TargetMachine {
239239
void setCodeModel(CodeModel::Model CM) { CMModel = CM; }
240240

241241
void setLargeDataThreshold(uint64_t LDT) { LargeDataThreshold = LDT; }
242-
bool isLargeGlobalValue(const GlobalValue *GV) const;
242+
bool isLargeGlobalObject(const GlobalObject *GO) const;
243243

244244
bool isPositionIndependent() const;
245245

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
649649
Name = ".rodata.cst";
650650
Name += utostr(EntrySize);
651651
} else {
652-
Name = getSectionPrefixForGlobal(Kind, TM.isLargeGlobalValue(GO));
652+
Name = getSectionPrefixForGlobal(Kind, TM.isLargeGlobalObject(GO));
653653
}
654654

655655
bool HasPrefix = false;
@@ -769,7 +769,7 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
769769
Group = C->getName();
770770
IsComdat = C->getSelectionKind() == Comdat::Any;
771771
}
772-
if (TM.isLargeGlobalValue(GO))
772+
if (TM.isLargeGlobalObject(GO))
773773
Flags |= ELF::SHF_X86_64_LARGE;
774774
return {Group, IsComdat, Flags};
775775
}

llvm/lib/Target/TargetMachine.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,10 @@ TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,
3939

4040
TargetMachine::~TargetMachine() = default;
4141

42-
bool TargetMachine::isLargeGlobalValue(const GlobalValue *GVal) const {
42+
bool TargetMachine::isLargeGlobalObject(const GlobalObject *GO) const {
4343
if (getTargetTriple().getArch() != Triple::x86_64)
4444
return false;
4545

46-
auto *GO = GVal->getAliaseeObject();
47-
48-
// Be conservative if we can't find an underlying GlobalObject.
49-
if (!GO)
50-
return true;
51-
5246
auto *GV = dyn_cast<GlobalVariable>(GO);
5347

5448
// Functions/GlobalIFuncs are only large under the large code model.

llvm/lib/Target/X86/X86FastISel.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -716,8 +716,10 @@ bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
716716
return false;
717717

718718
// Can't handle large objects yet.
719-
if (TM.isLargeGlobalValue(GV))
720-
return false;
719+
if (auto *GO = dyn_cast<GlobalObject>(GV)) {
720+
if (TM.isLargeGlobalObject(GO))
721+
return false;
722+
}
721723

722724
// Can't handle TLS yet.
723725
if (GV->isThreadLocal())

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2923,10 +2923,6 @@ bool X86DAGToDAGISel::selectAddr(SDNode *Parent, SDValue N, SDValue &Base,
29232923
}
29242924

29252925
bool X86DAGToDAGISel::selectMOV64Imm32(SDValue N, SDValue &Imm) {
2926-
// Cannot use 32 bit constants to reference objects in kernel code model.
2927-
if (TM.getCodeModel() == CodeModel::Kernel)
2928-
return false;
2929-
29302926
// In static codegen with small code model, we can get the address of a label
29312927
// into a register with 'movl'
29322928
if (N->getOpcode() != X86ISD::Wrapper)
@@ -2940,18 +2936,15 @@ bool X86DAGToDAGISel::selectMOV64Imm32(SDValue N, SDValue &Imm) {
29402936
return false;
29412937

29422938
Imm = N;
2943-
// Small/medium code model can reference non-TargetGlobalAddress objects with
2944-
// 32 bit constants.
2945-
if (N->getOpcode() != ISD::TargetGlobalAddress) {
2946-
return TM.getCodeModel() == CodeModel::Small ||
2947-
TM.getCodeModel() == CodeModel::Medium;
2948-
}
2939+
if (N->getOpcode() != ISD::TargetGlobalAddress)
2940+
return TM.getCodeModel() == CodeModel::Small;
29492941

2950-
const GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
2951-
if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2952-
return CR->getUnsignedMax().ult(1ull << 32);
2942+
std::optional<ConstantRange> CR =
2943+
cast<GlobalAddressSDNode>(N)->getGlobal()->getAbsoluteSymbolRange();
2944+
if (!CR)
2945+
return TM.getCodeModel() == CodeModel::Small;
29532946

2954-
return !TM.isLargeGlobalValue(GV);
2947+
return CR->getUnsignedMax().ult(1ull << 32);
29552948
}
29562949

29572950
bool X86DAGToDAGISel::selectLEA64_32Addr(SDValue N, SDValue &Base,

llvm/lib/Target/X86/X86Subtarget.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
8585
if (isTargetELF()) {
8686
assert(CM != CodeModel::Tiny &&
8787
"Tiny codesize model not supported on X86");
88-
// In the large code model, all text is far from any global data, so we
89-
// use GOTOFF.
88+
// In the large code model, even referencing a global under the large data
89+
// threshold which is considered "small", we need to use GOTOFF.
9090
if (CM == CodeModel::Large)
9191
return X86II::MO_GOTOFF;
92-
// Large GlobalValues use GOTOFF, otherwise use RIP-rel access.
93-
if (GV)
94-
return TM.isLargeGlobalValue(GV) ? X86II::MO_GOTOFF : X86II::MO_NO_FLAG;
95-
// GV == nullptr is for all other non-GlobalValue global data like the
96-
// constant pool, jump tables, labels, etc. The small and medium code
97-
// models treat these as accessible with a RIP-rel access.
92+
// Large objects use GOTOFF, otherwise use RIP-rel access.
93+
if (auto *GO = dyn_cast_or_null<GlobalObject>(GV))
94+
return TM.isLargeGlobalObject(GO) ? X86II::MO_GOTOFF
95+
: X86II::MO_NO_FLAG;
96+
// For non-GlobalObjects, the small and medium code models treat them as
97+
// accessible with a RIP-rel access.
9898
return X86II::MO_NO_FLAG;
9999
}
100100

0 commit comments

Comments
 (0)