Skip to content

[X86] Don't convert local function foo in the same section to foo(%rip) when the offset is near INT32_MIN #98438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions llvm/lib/Target/X86/X86ISelDAGToDAG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,16 @@ bool X86DAGToDAGISel::matchAddress(SDValue N, X86ISelAddressMode &AM) {
AM.Scale == 1 && AM.BaseType == X86ISelAddressMode::RegBase &&
AM.Base_Reg.getNode() == nullptr && AM.IndexReg.getNode() == nullptr &&
AM.SymbolFlags == X86II::MO_NO_FLAG && AM.hasSymbolicDisplacement()) {
// However, when GV is a local function symbol and in the same section as
// the current instruction, and AM.Disp is negative and near INT32_MIN,
// referencing GV+Disp generates a relocation referencing the section symbol
// with an even smaller offset, which might underflow. We should bail out if
// the negative offset is too close to INT32_MIN. Actually, we are more
// conservative here, using a smaller magic number also used by
// isOffsetSuitableForCodeModel.
if (isa_and_nonnull<Function>(AM.GV) && AM.Disp < -16 * 1024 * 1024)
return true;

AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
}

Expand Down
67 changes: 67 additions & 0 deletions llvm/test/CodeGen/X86/fold-add.ll
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,71 @@ entry:
ret i64 add (i64 ptrtoint (ptr @foo to i64), i64 -2147483649)
}

define internal void @bar() #0 {
; STATIC-LABEL: bar:
; STATIC: # %bb.0:
; STATIC-NEXT: retq
;
; PIC-LABEL: bar:
; PIC: # %bb.0:
; PIC-NEXT: retq
;
; MSTATIC-LABEL: bar:
; MSTATIC: # %bb.0:
; MSTATIC-NEXT: retq
;
; MPIC-LABEL: bar:
; MPIC: # %bb.0:
; MPIC-NEXT: retq
ret void
}

define dso_local i64 @fun_neg_0xfeffffff() #0 {
; STATIC-LABEL: fun_neg_0xfeffffff:
; STATIC: # %bb.0:
; STATIC-NEXT: movl $bar, %eax
; STATIC-NEXT: addq $-16777217, %rax # imm = 0xFEFFFFFF
; STATIC-NEXT: retq
;
; PIC-LABEL: fun_neg_0xfeffffff:
; PIC: # %bb.0:
; PIC-NEXT: leaq bar-16777217(%rip), %rax
; PIC-NEXT: retq
;
; MSTATIC-LABEL: fun_neg_0xfeffffff:
; MSTATIC: # %bb.0:
; MSTATIC-NEXT: movl $bar, %eax
; MSTATIC-NEXT: addq $-16777217, %rax # imm = 0xFEFFFFFF
; MSTATIC-NEXT: retq
;
; MPIC-LABEL: fun_neg_0xfeffffff:
; MPIC: # %bb.0:
; MPIC-NEXT: leaq bar-16777217(%rip), %rax
; MPIC-NEXT: retq
ret i64 add (i64 ptrtoint (ptr @bar to i64), i64 -16777217)
}

define dso_local i64 @fun_neg_ff000000() #0 {
; STATIC-LABEL: fun_neg_ff000000:
; STATIC: # %bb.0:
; STATIC-NEXT: leaq bar-16777216(%rip), %rax
; STATIC-NEXT: retq
;
; PIC-LABEL: fun_neg_ff000000:
; PIC: # %bb.0:
; PIC-NEXT: leaq bar-16777216(%rip), %rax
; PIC-NEXT: retq
;
; MSTATIC-LABEL: fun_neg_ff000000:
; MSTATIC: # %bb.0:
; MSTATIC-NEXT: leaq bar-16777216(%rip), %rax
; MSTATIC-NEXT: retq
;
; MPIC-LABEL: fun_neg_ff000000:
; MPIC: # %bb.0:
; MPIC-NEXT: leaq bar-16777216(%rip), %rax
; MPIC-NEXT: retq
ret i64 add (i64 ptrtoint (ptr @bar to i64), i64 -16777216)
}

attributes #0 = { nounwind }
Loading