[PATCH] D93931: [X86] Don't fold negative offset into 32-bit absolute address (e.g. movl $foo-1, %eax)
Fangrui Song via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 29 21:42:59 PST 2020
MaskRay created this revision.
MaskRay added reviewers: craig.topper, pengfei, RKSimon.
Herald added a subscriber: hiraditya.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
When building abseil-cpp `bin/absl_hash_test` with Clang in -fno-pic
mode, an instruction like `movl $foo-2147483648, $eax` may be produced
(subtracting a number from a static variable address). This is invalid
if foo's address is smaller than 2147483648 because ELF R_X86_64_32 does
not allow a negative value (errored by GNU ld/gold/LLD).
Actually any negative offset is not allowed because the symbol address
can be zero (e.g. set by `-Wl,--defsym=foo=0`). So disallow such folding
in isOffsetSuitableForCodeModel.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D93931
Files:
llvm/lib/Target/X86/X86ISelLowering.cpp
llvm/lib/Target/X86/X86ISelLowering.h
llvm/test/CodeGen/X86/fold-add.ll
Index: llvm/test/CodeGen/X86/fold-add.ll
===================================================================
--- llvm/test/CodeGen/X86/fold-add.ll
+++ llvm/test/CodeGen/X86/fold-add.ll
@@ -54,10 +54,12 @@
ret i64 add (i64 ptrtoint (i32* @foo to i64), i64 1701208431)
}
+;; Test we don't emit movl foo-1, %eax. ELF R_X86_64_32 does not allow
+;; a negative value.
define dso_local i64 @neg_1() #0 {
; CHECK-LABEL: neg_1:
; CHECK: # %bb.0:
-; STATIC-NEXT: movl $foo-1, %eax
+; STATIC-NEXT: leaq foo-1(%rip), %rax
; PIC-NEXT: leaq foo-1(%rip), %rax
; MSTATIC-NEXT: movabsq $foo, %rax
; MSTATIC-NEXT: decq %rax
@@ -71,7 +73,7 @@
define dso_local i64 @neg_0x80000000() #0 {
; CHECK-LABEL: neg_0x80000000:
; CHECK: # %bb.0:
-; STATIC-NEXT: movl $foo-2147483648, %eax
+; STATIC-NEXT: leaq foo-2147483648(%rip), %rax
; PIC-NEXT: leaq foo-2147483648(%rip), %rax
; MSTATIC-NEXT: movabsq $foo, %rax
; MSTATIC-NEXT: addq $-2147483648, %rax
Index: llvm/lib/Target/X86/X86ISelLowering.h
===================================================================
--- llvm/lib/Target/X86/X86ISelLowering.h
+++ llvm/lib/Target/X86/X86ISelLowering.h
@@ -855,7 +855,8 @@
/// Returns true of the given offset can be
/// fit into displacement field of the instruction.
bool isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
- bool hasSymbolicDisplacement = true);
+ bool hasSymbolicDisplacement,
+ bool formsAbsoluteAddress = false);
/// Determines whether the callee is required to pop its
/// own arguments. Callee pop is necessary to support tail calls.
Index: llvm/lib/Target/X86/X86ISelLowering.cpp
===================================================================
--- llvm/lib/Target/X86/X86ISelLowering.cpp
+++ llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -4872,7 +4872,8 @@
}
bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
- bool hasSymbolicDisplacement) {
+ bool hasSymbolicDisplacement,
+ bool formsAbsoluteAddress) {
// Offset should fit into 32 bit immediate field.
if (!isInt<32>(Offset))
return false;
@@ -4888,8 +4889,11 @@
// For small code model we assume that latest object is 16MB before end of 31
// bits boundary. We may also accept pretty large negative constants knowing
- // that all objects are in the positive half of address space.
- if (M == CodeModel::Small && Offset < 16*1024*1024)
+ // that all objects are in the positive half of address space, if Offset is
+ // not used to form an absolute address. movl foo-1, %eax is not allowed
+ // because if the address of foo is 0, this fixup is not representable.
+ if (M == CodeModel::Small && Offset < 16 * 1024 * 1024 &&
+ (Offset >= 0 || !formsAbsoluteAddress))
return true;
// For kernel code model we know that all object resist in the negative half
@@ -19109,7 +19113,7 @@
// offset into the global address reference. Otherwise, ADD it on later.
int64_t GlobalOffset = 0;
if (OpFlags == X86II::MO_NO_FLAG &&
- X86::isOffsetSuitableForCodeModel(Offset, M)) {
+ X86::isOffsetSuitableForCodeModel(Offset, M, true, true)) {
std::swap(GlobalOffset, Offset);
}
Result = DAG.getTargetGlobalAddress(GV, dl, PtrVT, GlobalOffset, OpFlags);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D93931.314065.patch
Type: text/x-patch
Size: 3522 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201230/dbcfa425/attachment.bin>
More information about the llvm-commits
mailing list