Skip to content

Commit

Permalink
Properly handle the mftb instruction.
Browse files Browse the repository at this point in the history
The mftb instruction was incorrectly marked as deprecated in the PPC
Backend. Instead, it should not be treated as deprecated, but rather be
implemented using the mfspr instruction. A similar patch was put into GCC last
year. Details can be found at:

https://sourceware.org/ml/binutils/2014-11/msg00383.html.
This change will replace instances of the mftb instruction with the mfspr
instruction for all CPUs except 601 and pwr3. This will also be the default
behaviour.

Additional details can be found in:

https://llvm.org/bugs/show_bug.cgi?id=23680

Phabricator review: http://reviews.llvm.org/D10419


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239827 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
kitbarton committed Jun 16, 2015
1 parent 69266c3 commit 1b26bfb
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 38 deletions.
7 changes: 7 additions & 0 deletions lib/Target/PowerPC/AsmParser/PPCAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,13 @@ void PPCAsmParser::ProcessInstruction(MCInst &Inst,
Inst = TmpInst;
break;
}
case PPC::MFTB: {
if (STI.getFeatureBits()[PPC::FeatureMFTB]) {
assert(Inst.getNumOperands() == 2 && "Expecting two operands");
Inst.setOpcode(PPC::MFSPR);
}
break;
}
}
}

Expand Down
79 changes: 47 additions & 32 deletions lib/Target/PowerPC/PPC.td
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ def FeatureInvariantFunctionDescriptors :
"Assume function descriptors are invariant">;
def FeatureHTM : SubtargetFeature<"htm", "HasHTM", "true",
"Enable Hardware Transactional Memory instructions">;
def FeatureMFTB : SubtargetFeature<"", "FeatureMFTB", "true",
"Implement mftb using the mfspr instruction">;

def DeprecatedMFTB : SubtargetFeature<"", "DeprecatedMFTB", "true",
"Treat mftb as deprecated">;
def DeprecatedDST : SubtargetFeature<"", "DeprecatedDST", "true",
"Treat vector data stream cache control instructions as deprecated">;

Expand Down Expand Up @@ -165,7 +165,7 @@ def ProcessorFeatures {
FeaturePOPCNTD, FeatureCMPB, FeatureLDBRX,
Feature64Bit /*, Feature64BitRegs */,
FeatureBPERMD, FeatureExtDiv,
DeprecatedMFTB, DeprecatedDST];
FeatureMFTB, DeprecatedDST];
list<SubtargetFeature> Power8SpecificFeatures =
[DirectivePwr8, FeatureP8Altivec, FeatureP8Vector, FeatureP8Crypto,
FeatureHTM, FeatureDirectMove, FeatureICBT, FeaturePartwordAtomic];
Expand Down Expand Up @@ -247,119 +247,134 @@ include "PPCInstrInfo.td"
// PowerPC processors supported.
//

def : Processor<"generic", G3Itineraries, [Directive32]>;
def : Processor<"generic", G3Itineraries, [Directive32, FeatureMFTB]>;
def : ProcessorModel<"440", PPC440Model, [Directive440, FeatureISEL,
FeatureFRES, FeatureFRSQRTE,
FeatureICBT, FeatureBookE,
FeatureMSYNC, DeprecatedMFTB]>;
FeatureMSYNC, FeatureMFTB]>;
def : ProcessorModel<"450", PPC440Model, [Directive440, FeatureISEL,
FeatureFRES, FeatureFRSQRTE,
FeatureICBT, FeatureBookE,
FeatureMSYNC, DeprecatedMFTB]>;
FeatureMSYNC, FeatureMFTB]>;
def : Processor<"601", G3Itineraries, [Directive601]>;
def : Processor<"602", G3Itineraries, [Directive602]>;
def : Processor<"602", G3Itineraries, [Directive602,
FeatureMFTB]>;
def : Processor<"603", G3Itineraries, [Directive603,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"603e", G3Itineraries, [Directive603,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"603ev", G3Itineraries, [Directive603,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"604", G3Itineraries, [Directive604,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"604e", G3Itineraries, [Directive604,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"620", G3Itineraries, [Directive620,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"750", G4Itineraries, [Directive750,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"g3", G3Itineraries, [Directive750,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"7400", G4Itineraries, [Directive7400, FeatureAltivec,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"g4", G4Itineraries, [Directive7400, FeatureAltivec,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"7450", G4PlusItineraries, [Directive7400, FeatureAltivec,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;
def : Processor<"g4+", G4PlusItineraries, [Directive7400, FeatureAltivec,
FeatureFRES, FeatureFRSQRTE]>;
FeatureFRES, FeatureFRSQRTE,
FeatureMFTB]>;

def : ProcessorModel<"970", G5Model,
[Directive970, FeatureAltivec,
FeatureMFOCRF, FeatureFSqrt,
FeatureFRES, FeatureFRSQRTE, FeatureSTFIWX,
Feature64Bit /*, Feature64BitRegs */]>;
Feature64Bit /*, Feature64BitRegs */,
FeatureMFTB]>;
def : ProcessorModel<"g5", G5Model,
[Directive970, FeatureAltivec,
FeatureMFOCRF, FeatureFSqrt, FeatureSTFIWX,
FeatureFRES, FeatureFRSQRTE,
Feature64Bit /*, Feature64BitRegs */,
DeprecatedMFTB, DeprecatedDST]>;
FeatureMFTB, DeprecatedDST]>;
def : ProcessorModel<"e500mc", PPCE500mcModel,
[DirectiveE500mc, FeatureMFOCRF,
FeatureSTFIWX, FeatureICBT, FeatureBookE,
FeatureISEL, DeprecatedMFTB]>;
FeatureISEL, FeatureMFTB]>;
def : ProcessorModel<"e5500", PPCE5500Model,
[DirectiveE5500, FeatureMFOCRF, Feature64Bit,
FeatureSTFIWX, FeatureICBT, FeatureBookE,
FeatureISEL, DeprecatedMFTB]>;
FeatureISEL, FeatureMFTB]>;
def : ProcessorModel<"a2", PPCA2Model,
[DirectiveA2, FeatureICBT, FeatureBookE, FeatureMFOCRF,
FeatureFCPSGN, FeatureFSqrt, FeatureFRE, FeatureFRES,
FeatureFRSQRTE, FeatureFRSQRTES, FeatureRecipPrec,
FeatureSTFIWX, FeatureLFIWAX,
FeatureFPRND, FeatureFPCVT, FeatureISEL,
FeaturePOPCNTD, FeatureCMPB, FeatureLDBRX, Feature64Bit
/*, Feature64BitRegs */, DeprecatedMFTB]>;
/*, Feature64BitRegs */, FeatureMFTB]>;
def : ProcessorModel<"a2q", PPCA2Model,
[DirectiveA2, FeatureICBT, FeatureBookE, FeatureMFOCRF,
FeatureFCPSGN, FeatureFSqrt, FeatureFRE, FeatureFRES,
FeatureFRSQRTE, FeatureFRSQRTES, FeatureRecipPrec,
FeatureSTFIWX, FeatureLFIWAX,
FeatureFPRND, FeatureFPCVT, FeatureISEL,
FeaturePOPCNTD, FeatureCMPB, FeatureLDBRX, Feature64Bit
/*, Feature64BitRegs */, FeatureQPX, DeprecatedMFTB]>;
/*, Feature64BitRegs */, FeatureQPX, FeatureMFTB]>;
def : ProcessorModel<"pwr3", G5Model,
[DirectivePwr3, FeatureAltivec,
FeatureFRES, FeatureFRSQRTE, FeatureMFOCRF,
FeatureSTFIWX, Feature64Bit]>;
def : ProcessorModel<"pwr4", G5Model,
[DirectivePwr4, FeatureAltivec, FeatureMFOCRF,
FeatureFSqrt, FeatureFRES, FeatureFRSQRTE,
FeatureSTFIWX, Feature64Bit]>;
FeatureSTFIWX, Feature64Bit, FeatureMFTB]>;
def : ProcessorModel<"pwr5", G5Model,
[DirectivePwr5, FeatureAltivec, FeatureMFOCRF,
FeatureFSqrt, FeatureFRE, FeatureFRES,
FeatureFRSQRTE, FeatureFRSQRTES,
FeatureSTFIWX, Feature64Bit,
DeprecatedMFTB, DeprecatedDST]>;
FeatureMFTB, DeprecatedDST]>;
def : ProcessorModel<"pwr5x", G5Model,
[DirectivePwr5x, FeatureAltivec, FeatureMFOCRF,
FeatureFSqrt, FeatureFRE, FeatureFRES,
FeatureFRSQRTE, FeatureFRSQRTES,
FeatureSTFIWX, FeatureFPRND, Feature64Bit,
DeprecatedMFTB, DeprecatedDST]>;
FeatureMFTB, DeprecatedDST]>;
def : ProcessorModel<"pwr6", G5Model,
[DirectivePwr6, FeatureAltivec,
FeatureMFOCRF, FeatureFCPSGN, FeatureFSqrt, FeatureFRE,
FeatureFRES, FeatureFRSQRTE, FeatureFRSQRTES,
FeatureRecipPrec, FeatureSTFIWX, FeatureLFIWAX, FeatureCMPB,
FeatureFPRND, Feature64Bit /*, Feature64BitRegs */,
DeprecatedMFTB, DeprecatedDST]>;
FeatureMFTB, DeprecatedDST]>;
def : ProcessorModel<"pwr6x", G5Model,
[DirectivePwr5x, FeatureAltivec, FeatureMFOCRF,
FeatureFCPSGN, FeatureFSqrt, FeatureFRE, FeatureFRES,
FeatureFRSQRTE, FeatureFRSQRTES, FeatureRecipPrec,
FeatureSTFIWX, FeatureLFIWAX, FeatureCMPB,
FeatureFPRND, Feature64Bit,
DeprecatedMFTB, DeprecatedDST]>;
FeatureMFTB, DeprecatedDST]>;
def : ProcessorModel<"pwr7", P7Model, ProcessorFeatures.Power7FeatureList>;
def : ProcessorModel<"pwr8", P8Model, ProcessorFeatures.Power8FeatureList>;
def : Processor<"ppc", G3Itineraries, [Directive32]>;
def : Processor<"ppc", G3Itineraries, [Directive32, FeatureMFTB]>;
def : ProcessorModel<"ppc64", G5Model,
[Directive64, FeatureAltivec,
FeatureMFOCRF, FeatureFSqrt, FeatureFRES,
FeatureFRSQRTE, FeatureSTFIWX,
Feature64Bit /*, Feature64BitRegs */]>;
Feature64Bit /*, Feature64BitRegs */,
FeatureMFTB]>;
def : ProcessorModel<"ppc64le", P8Model, ProcessorFeatures.Power8FeatureList>;

//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion lib/Target/PowerPC/PPCInstrInfo.td
Original file line number Diff line number Diff line change
Expand Up @@ -2225,7 +2225,7 @@ def MTSPR : XFXForm_1<31, 467, (outs), (ins i32imm:$SPR, gprc:$RT),
"mtspr $SPR, $RT", IIC_SprMTSPR>;

def MFTB : XFXForm_1<31, 371, (outs gprc:$RT), (ins i32imm:$SPR),
"mftb $RT, $SPR", IIC_SprMFTB>, Deprecated<DeprecatedMFTB>;
"mftb $RT, $SPR", IIC_SprMFTB>;

// A pseudo-instruction used to implement the read of the 64-bit cycle counter
// on a 32-bit target.
Expand Down
2 changes: 1 addition & 1 deletion lib/Target/PowerPC/PPCSubtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void PPCSubtarget::initializeEnvironment() {
IsPPC4xx = false;
IsPPC6xx = false;
IsE500 = false;
DeprecatedMFTB = false;
FeatureMFTB = false;
DeprecatedDST = false;
HasLazyResolverStubs = false;
HasICBT = false;
Expand Down
4 changes: 2 additions & 2 deletions lib/Target/PowerPC/PPCSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class PPCSubtarget : public PPCGenSubtargetInfo {
bool IsE500;
bool IsPPC4xx;
bool IsPPC6xx;
bool DeprecatedMFTB;
bool FeatureMFTB;
bool DeprecatedDST;
bool HasLazyResolverStubs;
bool IsLittleEndian;
Expand Down Expand Up @@ -237,7 +237,7 @@ class PPCSubtarget : public PPCGenSubtargetInfo {
bool isPPC4xx() const { return IsPPC4xx; }
bool isPPC6xx() const { return IsPPC6xx; }
bool isE500() const { return IsE500; }
bool isDeprecatedMFTB() const { return DeprecatedMFTB; }
bool isFeatureMFTB() const { return FeatureMFTB; }
bool isDeprecatedDST() const { return DeprecatedDST; }
bool hasICBT() const { return HasICBT; }
bool hasInvariantFunctionDescriptors() const {
Expand Down
72 changes: 72 additions & 0 deletions test/CodeGen/PowerPC/mftb.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
; Check handling of the mftb instruction.
; For CPUs 601 and pwr3, the mftb instruction should be emitted.
; On all other CPUs (including generic, ppc, ppc64), the mfspr instruction
; should be used instead. There should no longer be a deprecated warning
; message emittedfor this instruction for any CPU.

; RUN: llc -mtriple=powerpc64-unknown-linux-gnu -mcpu=pwr7 < %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-MFSPR
; RUN: llc -mtriple=powerpc64-unknown-linux-gnu -mcpu=pwr8 < %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-MFSPR
; RUN: llc -mtriple=powerpc64le-unknown-linux-gnu < %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-MFSPR
; RUN: llc -mtriple=powerpc-unknown-linux-gnu < %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-MFSPR
; RUN: llc -mtriple=powerpc-unknown-linux-gnu -mcpu=ppc < %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-MFSPR
; RUN: llc -mtriple=powerpc-unknown-linux-gnu -mcpu=601 < %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-MFTB
; RUN: llc -mtriple=powerpc-unknown-linux-gnu -mcpu=pwr3 < %s 2>&1 \
; RUN: | FileCheck %s --check-prefix=CHECK-MFTB

; CHECK-MFSPR-NOT: warning: deprecated
; CHECK-MFTB-NOT: warning: deprecated

define i32 @get_time() {
%time = call i32 asm "mftb $0, 268", "=r"()
ret i32 %time
; CHECK-MFSPR-LABEL: @get_time
; CHECK-MFSPR: mfspr 3, 268
; CHECK-MFSPR: blr

; CHECK-MFTB-LABEL: @get_time
; CHECK-MFTB: mftb 3, 268
; CHECK-MFTB: blr
}

define i32 @get_timeu() {
%time = call i32 asm "mftb $0, 269", "=r"()
ret i32 %time
; CHECK-MFSPR-LABEL: @get_timeu
; CHECK-MFSPR: mfspr 3, 269
; CHECK-MFSPR: blr

; CHECK-MFTB-LABEL: @get_timeu
; CHECK-MFTB: mftbu 3
; CHECK-MFTB: blr
}

define i32 @get_time_e() {
%time = call i32 asm "mftb $0", "=r"()
ret i32 %time
; CHECK-MFSPR-LABEL: @get_time_e
; CHECK-MFSPR: mfspr 3, 268
; CHECK-MFSPR: blr

; CHECK-MFTB-LABEL: @get_time_e
; CHECK-MFTB: mftb 3, 268
; CHECK-MFTB: blr
}

define i32 @get_timeu_e() {
%time = call i32 asm "mftbu $0", "=r"()
ret i32 %time
; CHECK-MFSPR-LABEL: @get_timeu_e
; CHECK-MFSPR: mfspr 3, 269
; CHECK-MFSPR: blr

; CHECK-MFTB-LABEL: @get_timeu_e
; CHECK-MFTB: mftbu 3
; CHECK-MFTB: blr
}

4 changes: 2 additions & 2 deletions test/MC/PowerPC/deprecated-p7.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# RUN: llvm-mc -triple powerpc-unknown-linux-gnu -mcpu=601 -show-encoding < %s 2>&1 | FileCheck -check-prefix=CHECK-OLD %s

mftb 3
# CHECK: warning: deprecated
# CHECK: mftb 3
# CHECK-NOT: warning: deprecated
# CHECK: mfspr 3, 268

# CHECK-OLD-NOT: warning: deprecated
# CHECK-OLD: mftb 3
Expand Down

0 comments on commit 1b26bfb

Please sign in to comment.