Skip to content

Commit 191f148

Browse files
committed
[X86] Use PC-relative mode for the kernel code model
Summary: The Linux kernel uses PC-relative mode, so allow that when the code model is "kernel". Reviewers: craig.topper Reviewed By: craig.topper Subscribers: llvm-commits, kees, nickdesaulniers Tags: #llvm Differential Revision: https://reviews.llvm.org/D60643 llvm-svn: 358343
1 parent 040871d commit 191f148

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

llvm/lib/Target/X86/X86ISelDAGToDAG.cpp

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,15 +1264,20 @@ bool X86DAGToDAGISel::matchAddress(SDValue N, X86ISelAddressMode &AM) {
12641264
// Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
12651265
// because it has a smaller encoding.
12661266
// TODO: Which other code models can use this?
1267-
if (TM.getCodeModel() == CodeModel::Small &&
1268-
Subtarget->is64Bit() &&
1269-
AM.Scale == 1 &&
1270-
AM.BaseType == X86ISelAddressMode::RegBase &&
1271-
AM.Base_Reg.getNode() == nullptr &&
1272-
AM.IndexReg.getNode() == nullptr &&
1273-
AM.SymbolFlags == X86II::MO_NO_FLAG &&
1274-
AM.hasSymbolicDisplacement())
1275-
AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
1267+
switch (TM.getCodeModel()) {
1268+
default: break;
1269+
case CodeModel::Small:
1270+
case CodeModel::Kernel:
1271+
if (Subtarget->is64Bit() &&
1272+
AM.Scale == 1 &&
1273+
AM.BaseType == X86ISelAddressMode::RegBase &&
1274+
AM.Base_Reg.getNode() == nullptr &&
1275+
AM.IndexReg.getNode() == nullptr &&
1276+
AM.SymbolFlags == X86II::MO_NO_FLAG &&
1277+
AM.hasSymbolicDisplacement())
1278+
AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
1279+
break;
1280+
}
12761281

12771282
return false;
12781283
}

llvm/test/CodeGen/X86/codemodel.ll

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,27 @@ entry:
1313
; CHECK-SMALL-LABEL: foo:
1414
; CHECK-SMALL: movl data(%rip), %eax
1515
; CHECK-KERNEL-LABEL: foo:
16-
; CHECK-KERNEL: movl data, %eax
16+
; CHECK-KERNEL: movl data(%rip), %eax
1717
%0 = load i32, i32* getelementptr ([0 x i32], [0 x i32]* @data, i64 0, i64 0), align 4 ; <i32> [#uses=1]
1818
ret i32 %0
1919
}
2020

21+
define i32 @foo1() nounwind readonly {
22+
entry:
23+
; CHECK-SMALL-LABEL: foo1:
24+
; CHECK-SMALL: movl data+16777212(%rip), %eax
25+
; CHECK-KERNEL-LABEL: foo1:
26+
; CHECK-KERNEL: movl data+16777212(%rip), %eax
27+
%0 = load i32, i32* getelementptr ([0 x i32], [0 x i32]* @data, i32 0, i64 4194303), align 4 ; <i32> [#uses=1]
28+
ret i32 %0
29+
}
30+
2131
define i32 @foo2() nounwind readonly {
2232
entry:
2333
; CHECK-SMALL-LABEL: foo2:
2434
; CHECK-SMALL: movl data+40(%rip), %eax
2535
; CHECK-KERNEL-LABEL: foo2:
26-
; CHECK-KERNEL: movl data+40, %eax
36+
; CHECK-KERNEL: movl data+40(%rip), %eax
2737
%0 = load i32, i32* getelementptr ([0 x i32], [0 x i32]* @data, i32 0, i64 10), align 4 ; <i32> [#uses=1]
2838
ret i32 %0
2939
}
@@ -34,6 +44,7 @@ entry:
3444
; CHECK-SMALL: movl data-40(%rip), %eax
3545
; CHECK-KERNEL-LABEL: foo3:
3646
; CHECK-KERNEL: movq $-40, %rax
47+
; CHECK-KERNEL: movl data(%rax), %eax
3748
%0 = load i32, i32* getelementptr ([0 x i32], [0 x i32]* @data, i32 0, i64 -10), align 4 ; <i32> [#uses=1]
3849
ret i32 %0
3950
}
@@ -45,26 +56,18 @@ entry:
4556
; CHECK-SMALL: movl $16777216, %eax
4657
; CHECK-SMALL: movl data(%rax), %eax
4758
; CHECK-KERNEL-LABEL: foo4:
48-
; CHECK-KERNEL: movl data+16777216, %eax
59+
; CHECK-KERNEL: movl data+16777216(%rip), %eax
4960
%0 = load i32, i32* getelementptr ([0 x i32], [0 x i32]* @data, i32 0, i64 4194304), align 4 ; <i32> [#uses=1]
5061
ret i32 %0
5162
}
5263

53-
define i32 @foo1() nounwind readonly {
54-
entry:
55-
; CHECK-SMALL-LABEL: foo1:
56-
; CHECK-SMALL: movl data+16777212(%rip), %eax
57-
; CHECK-KERNEL-LABEL: foo1:
58-
; CHECK-KERNEL: movl data+16777212, %eax
59-
%0 = load i32, i32* getelementptr ([0 x i32], [0 x i32]* @data, i32 0, i64 4194303), align 4 ; <i32> [#uses=1]
60-
ret i32 %0
61-
}
6264
define i32 @foo5() nounwind readonly {
6365
entry:
6466
; CHECK-SMALL-LABEL: foo5:
6567
; CHECK-SMALL: movl data-16777216(%rip), %eax
6668
; CHECK-KERNEL-LABEL: foo5:
6769
; CHECK-KERNEL: movq $-16777216, %rax
70+
; CHECK-KERNEL: movl data(%rax), %eax
6871
%0 = load i32, i32* getelementptr ([0 x i32], [0 x i32]* @data, i32 0, i64 -4194304), align 4 ; <i32> [#uses=1]
6972
ret i32 %0
7073
}

llvm/unittests/Support/VirtualFileSystemTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,9 @@ struct ScopedFile {
401401
this->Path = "";
402402
}
403403
~ScopedFile() {
404-
if (Path != "")
404+
if (Path != "") {
405405
EXPECT_FALSE(llvm::sys::fs::remove(Path.str()));
406+
}
406407
}
407408
};
408409
} // end anonymous namespace

0 commit comments

Comments
 (0)