Skip to content

Commit

Permalink
Revert "Reland [X86] Respect code models more when determining if a g…
Browse files Browse the repository at this point in the history
…lobal reference can fit in 32 bits (llvm#75386)"

This reverts commit ec92d74.

Breaks some compiler-rt tests, e.g. https://lab.llvm.org/buildbot/#/builders/37/builds/28834
  • Loading branch information
aeubanks committed Dec 14, 2023
1 parent e75f37f commit 15617d1
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 209 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/Target/TargetMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class TargetMachine {
void setCodeModel(CodeModel::Model CM) { CMModel = CM; }

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

bool isPositionIndependent() const;

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ getELFSectionNameForGlobal(const GlobalObject *GO, SectionKind Kind,
Name = ".rodata.cst";
Name += utostr(EntrySize);
} else {
Name = getSectionPrefixForGlobal(Kind, TM.isLargeGlobalValue(GO));
Name = getSectionPrefixForGlobal(Kind, TM.isLargeGlobalObject(GO));
}

bool HasPrefix = false;
Expand Down Expand Up @@ -769,7 +769,7 @@ getGlobalObjectInfo(const GlobalObject *GO, const TargetMachine &TM) {
Group = C->getName();
IsComdat = C->getSelectionKind() == Comdat::Any;
}
if (TM.isLargeGlobalValue(GO))
if (TM.isLargeGlobalObject(GO))
Flags |= ELF::SHF_X86_64_LARGE;
return {Group, IsComdat, Flags};
}
Expand Down
8 changes: 1 addition & 7 deletions llvm/lib/Target/TargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,10 @@ TargetMachine::TargetMachine(const Target &T, StringRef DataLayoutString,

TargetMachine::~TargetMachine() = default;

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

auto *GO = GVal->getAliaseeObject();

// Be conservative if we can't find an underlying GlobalObject.
if (!GO)
return true;

auto *GV = dyn_cast<GlobalVariable>(GO);

// Functions/GlobalIFuncs are only large under the large code model.
Expand Down
8 changes: 5 additions & 3 deletions llvm/lib/Target/X86/X86FastISel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,10 @@ bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
return false;

// Can't handle large objects yet.
if (TM.isLargeGlobalValue(GV))
return false;
if (auto *GO = dyn_cast<GlobalObject>(GV)) {
if (TM.isLargeGlobalObject(GO))
return false;
}

// Can't handle TLS yet.
if (GV->isThreadLocal())
Expand Down Expand Up @@ -3847,7 +3849,7 @@ unsigned X86FastISel::X86MaterializeGV(const GlobalValue *GV, MVT VT) {
if (TM.getCodeModel() != CodeModel::Small &&
TM.getCodeModel() != CodeModel::Medium)
return 0;
if (TM.isLargeGlobalValue(GV))
if (!isa<GlobalObject>(GV) || TM.isLargeGlobalObject(cast<GlobalObject>(GV)))
return 0;

// Materialize addresses with LEA/MOV instructions.
Expand Down
21 changes: 7 additions & 14 deletions llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2927,10 +2927,6 @@ bool X86DAGToDAGISel::selectAddr(SDNode *Parent, SDValue N, SDValue &Base,
}

bool X86DAGToDAGISel::selectMOV64Imm32(SDValue N, SDValue &Imm) {
// Cannot use 32 bit constants to reference objects in kernel code model.
if (TM.getCodeModel() == CodeModel::Kernel)
return false;

// In static codegen with small code model, we can get the address of a label
// into a register with 'movl'
if (N->getOpcode() != X86ISD::Wrapper)
Expand All @@ -2944,18 +2940,15 @@ bool X86DAGToDAGISel::selectMOV64Imm32(SDValue N, SDValue &Imm) {
return false;

Imm = N;
// Small/medium code model can reference non-TargetGlobalAddress objects with
// 32 bit constants.
if (N->getOpcode() != ISD::TargetGlobalAddress) {
return TM.getCodeModel() == CodeModel::Small ||
TM.getCodeModel() == CodeModel::Medium;
}
if (N->getOpcode() != ISD::TargetGlobalAddress)
return TM.getCodeModel() == CodeModel::Small;

const GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
return CR->getUnsignedMax().ult(1ull << 32);
std::optional<ConstantRange> CR =
cast<GlobalAddressSDNode>(N)->getGlobal()->getAbsoluteSymbolRange();
if (!CR)
return TM.getCodeModel() == CodeModel::Small;

return !TM.isLargeGlobalValue(GV);
return CR->getUnsignedMax().ult(1ull << 32);
}

bool X86DAGToDAGISel::selectLEA64_32Addr(SDValue N, SDValue &Base,
Expand Down
16 changes: 8 additions & 8 deletions llvm/lib/Target/X86/X86Subtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ X86Subtarget::classifyLocalReference(const GlobalValue *GV) const {
CodeModel::Model CM = TM.getCodeModel();
assert(CM != CodeModel::Tiny &&
"Tiny codesize model not supported on X86");
// In the large code model, all text is far from any global data, so we
// use GOTOFF.
// In the large code model, even referencing a global under the large data
// threshold which is considered "small", we need to use GOTOFF.
if (CM == CodeModel::Large)
return X86II::MO_GOTOFF;
// Large GlobalValues use GOTOFF, otherwise use RIP-rel access.
if (GV)
return TM.isLargeGlobalValue(GV) ? X86II::MO_GOTOFF : X86II::MO_NO_FLAG;
// GV == nullptr is for all other non-GlobalValue global data like the
// constant pool, jump tables, labels, etc. The small and medium code
// models treat these as accessible with a RIP-rel access.
// Large objects use GOTOFF, otherwise use RIP-rel access.
if (auto *GO = dyn_cast_or_null<GlobalObject>(GV))
return TM.isLargeGlobalObject(GO) ? X86II::MO_GOTOFF
: X86II::MO_NO_FLAG;
// For non-GlobalObjects, the small and medium code models treat them as
// accessible with a RIP-rel access.
return X86II::MO_NO_FLAG;
}

Expand Down
Loading

0 comments on commit 15617d1

Please sign in to comment.