Skip to content

Commit

Permalink
[X86] Add intrinsic support for the RDPID instruction
Browse files Browse the repository at this point in the history
This adds a new instrinsic to support the rdpid instruction. The implementation is a bit weird because the intrinsic is defined as always returning 32-bits, but the assembler support thinks the instruction produces a 64-bit register in 64-bit mode. But really it zeros the upper 32 bits. So I had to add separate patterns where 64-bit mode uses an extract_subreg.

Differential Revision: https://reviews.llvm.org/D42205

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322910 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
topperc committed Jan 18, 2018
1 parent 8073692 commit 8677133
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
6 changes: 6 additions & 0 deletions include/llvm/IR/IntrinsicsX86.td
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ let TargetPrefix = "x86" in {
Intrinsic<[llvm_i64_ty], [llvm_i32_ty], []>;
}

// Read processor ID.
let TargetPrefix = "x86" in {
def int_x86_rdpid : GCCBuiltin<"__builtin_ia32_rdpid">,
Intrinsic<[llvm_i32_ty], [], []>;
}

//===----------------------------------------------------------------------===//
// CET SS
let TargetPrefix = "x86" in {
Expand Down
4 changes: 3 additions & 1 deletion lib/Support/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,9 @@ bool sys::getHostCPUFeatures(StringMap<bool> &Features) {
Features["avx512vnni"] = HasLeaf7 && ((ECX >> 11) & 1) && HasAVX512Save;
Features["avx512bitalg"] = HasLeaf7 && ((ECX >> 12) & 1) && HasAVX512Save;
Features["avx512vpopcntdq"] = HasLeaf7 && ((ECX >> 14) & 1) && HasAVX512Save;
Features["ibt"] = HasLeaf7 && ((EDX >> 20) & 1);
Features["rdpid"] = HasLeaf7 && ((ECX >> 22) & 1);

Features["ibt"] = HasLeaf7 && ((EDX >> 20) & 1);

bool HasLeafD = MaxLevel >= 0xd &&
!getX86CpuIDAndInfoEx(0xd, 0x1, &EAX, &EBX, &ECX, &EDX);
Expand Down
5 changes: 4 additions & 1 deletion lib/Target/X86/X86.td
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ def FeatureCLFLUSHOPT : SubtargetFeature<"clflushopt", "HasCLFLUSHOPT", "true",
"Flush A Cache Line Optimized">;
def FeatureCLWB : SubtargetFeature<"clwb", "HasCLWB", "true",
"Cache Line Write Back">;
def FeatureRDPID : SubtargetFeature<"rdpid", "HasRDPID", "true",
"Support RDPID instructions">;
// On some processors, instructions that implicitly take two memory operands are
// slow. In practice, this means that CALL, PUSH, and POP with memory operands
// should be avoided in favor of a MOV + register CALL/PUSH/POP.
Expand Down Expand Up @@ -752,7 +754,8 @@ def ICLFeatures : ProcessorFeatures<CNLFeatures.Value, [
FeatureVPCLMULQDQ,
FeatureVPOPCNTDQ,
FeatureGFNI,
FeatureCLWB
FeatureCLWB,
FeatureRDPID
]>;

class IcelakeProc<string Name> : ProcModel<Name, SkylakeServerModel,
Expand Down
1 change: 1 addition & 0 deletions lib/Target/X86/X86InstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ def HasSHSTK : Predicate<"Subtarget->hasSHSTK()">;
def HasIBT : Predicate<"Subtarget->hasIBT()">;
def HasCLFLUSHOPT : Predicate<"Subtarget->hasCLFLUSHOPT()">;
def HasCLWB : Predicate<"Subtarget->hasCLWB()">;
def HasRDPID : Predicate<"Subtarget->hasRDPID()">;
def HasCmpxchg16b: Predicate<"Subtarget->hasCmpxchg16b()">;
def Not64BitMode : Predicate<"!Subtarget->is64Bit()">,
AssemblerPredicate<"!Mode64Bit", "Not 64-bit mode">;
Expand Down
20 changes: 14 additions & 6 deletions lib/Target/X86/X86InstrSystem.td
Original file line number Diff line number Diff line change
Expand Up @@ -700,14 +700,22 @@ let Uses = [RAX, RBX, RCX, RDX], Defs = [RAX, RBX, RCX] in {
//===----------------------------------------------------------------------===//
// RDPID Instruction
let SchedRW = [WriteSystem] in {
def RDPID32 : I<0xC7, MRM7r, (outs GR32:$src), (ins),
"rdpid\t$src", [], IIC_RDPID>, XS,
Requires<[Not64BitMode]>;
def RDPID64 : I<0xC7, MRM7r, (outs GR64:$src), (ins),
"rdpid\t$src", [], IIC_RDPID>, XS,
Requires<[In64BitMode]>;
def RDPID32 : I<0xC7, MRM7r, (outs GR32:$dst), (ins),
"rdpid\t$dst", [(set GR32:$dst, (int_x86_rdpid))], IIC_RDPID>, XS,
Requires<[Not64BitMode, HasRDPID]>;
def RDPID64 : I<0xC7, MRM7r, (outs GR64:$dst), (ins),
"rdpid\t$dst", [], IIC_RDPID>, XS,
Requires<[In64BitMode, HasRDPID]>;
} // SchedRW

let Predicates = [In64BitMode, HasRDPID] in {
// Due to silly instruction definition, we have to compensate for the
// instruction outputing a 64-bit register.
def : Pat<(int_x86_rdpid),
(EXTRACT_SUBREG (RDPID64), sub_32bit)>;
}


//===----------------------------------------------------------------------===//
// PTWRITE Instruction
let SchedRW = [WriteSystem] in {
Expand Down
1 change: 1 addition & 0 deletions lib/Target/X86/X86Subtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ void X86Subtarget::initializeEnvironment() {
HasSGX = false;
HasCLFLUSHOPT = false;
HasCLWB = false;
HasRDPID = false;
IsPMULLDSlow = false;
IsSHLDSlow = false;
IsUAMem16Slow = false;
Expand Down
4 changes: 4 additions & 0 deletions lib/Target/X86/X86Subtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@ class X86Subtarget final : public X86GenSubtargetInfo {
/// Processor supports Cache Line Write Back instruction
bool HasCLWB;

/// Processor support RDPID instruction
bool HasRDPID;

/// Use software floating point for code generation.
bool UseSoftFloat;

Expand Down Expand Up @@ -579,6 +582,7 @@ class X86Subtarget final : public X86GenSubtargetInfo {
bool hasIBT() const { return HasIBT; }
bool hasCLFLUSHOPT() const { return HasCLFLUSHOPT; }
bool hasCLWB() const { return HasCLWB; }
bool hasRDPID() const { return HasRDPID; }

bool isXRaySupported() const override { return is64Bit(); }

Expand Down
21 changes: 21 additions & 0 deletions test/CodeGen/X86/rdpid.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
; RUN: llc < %s -mtriple=x86_64-- -mattr=rdpid | FileCheck %s --check-prefix=CHECK --check-prefix=X86-64
; RUN: llc < %s -mtriple=i686-- -mattr=rdpid | FileCheck %s --check-prefix=CHECK --check-prefix=X86

define i32 @test_builtin_rdpid() {
; X86-64-LABEL: test_builtin_rdpid:
; X86-64: # %bb.0:
; X86-64-NEXT: rdpid %rax
; X86-64-NEXT: # kill: def %eax killed %eax killed %rax
; X86-64-NEXT: retq
;
; X86-LABEL: test_builtin_rdpid:
; X86: # %bb.0:
; X86-NEXT: rdpid %eax
; X86-NEXT: retl
%1 = tail call i32 @llvm.x86.rdpid()
ret i32 %1
}

declare i32 @llvm.x86.rdpid()

0 comments on commit 8677133

Please sign in to comment.