Skip to content

Commit 55c27af

Browse files
committed
[X86] Respect code models more when determining if a global reference can fit in 32 bits
For non-GlobalValue references, the small and medium code models can use 32 bit constants. For GlobalValue references, use TargetMachine::isLargeGlobalObject(). Look through aliases for determining if a GlobalValue is small or large.
1 parent e8f4388 commit 55c27af

File tree

2 files changed

+190
-126
lines changed

2 files changed

+190
-126
lines changed

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,10 @@ bool X86DAGToDAGISel::selectAddr(SDNode *Parent, SDValue N, SDValue &Base,
29272927
}
29282928

29292929
bool X86DAGToDAGISel::selectMOV64Imm32(SDValue N, SDValue &Imm) {
2930+
// Cannot use 32 bit constants to reference objects in kernel code model.
2931+
if (TM.getCodeModel() == CodeModel::Kernel)
2932+
return false;
2933+
29302934
// In static codegen with small code model, we can get the address of a label
29312935
// into a register with 'movl'
29322936
if (N->getOpcode() != X86ISD::Wrapper)
@@ -2940,15 +2944,20 @@ bool X86DAGToDAGISel::selectMOV64Imm32(SDValue N, SDValue &Imm) {
29402944
return false;
29412945

29422946
Imm = N;
2943-
if (N->getOpcode() != ISD::TargetGlobalAddress)
2944-
return TM.getCodeModel() == CodeModel::Small;
2947+
// Small/medium code model can reference non-TargetGlobalAddress objects with
2948+
// 32 bit constants.
2949+
if (N->getOpcode() != ISD::TargetGlobalAddress) {
2950+
return TM.getCodeModel() == CodeModel::Small ||
2951+
TM.getCodeModel() == CodeModel::Medium;
2952+
}
29452953

2946-
std::optional<ConstantRange> CR =
2947-
cast<GlobalAddressSDNode>(N)->getGlobal()->getAbsoluteSymbolRange();
2948-
if (!CR)
2949-
return TM.getCodeModel() == CodeModel::Small;
2954+
const GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
2955+
if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2956+
return CR->getUnsignedMax().ult(1ull << 32);
29502957

2951-
return CR->getUnsignedMax().ult(1ull << 32);
2958+
if (auto *GO = GV->getAliaseeObject())
2959+
return !TM.isLargeGlobalObject(GO);
2960+
return true;
29522961
}
29532962

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

0 commit comments

Comments
 (0)