From 1931b01a6456182fe27b0fc840c689a38279ae59 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Thu, 31 Mar 2016 14:44:50 +0000 Subject: [PATCH] [PowerPC] Remove incorrect use of COPY_TO_REGCLASS in fast isel The fast isel pass currently emits a COPY_TO_REGCLASS node to convert from a F4RC to a F8RC register class during conversion of a floating-point number to integer. There is actually no support in the common code instruction printers to emit COPY_TO_REGCLASS nodes, so the PowerPC back-end has special code there to simply ignore COPY_TO_REGCLASS. This is correct *if and only if* the source and destination registers of COPY_TO_REGCLASS are the same (except for the different register class). But nothing guarantees this to be the case, and if the register allocator does end up allocating source and destination to different registers after all, the back-end simply generates incorrect code. I've included a test case that shows such incorrect code generation. However, it seems that COPY_TO_REGCLASS is actually not intended to be used at the MI layer at all. It is used during SelectionDAG, but always lowered to a plain COPY before emitting MI. Other back-end's fast isel passes never emit COPY_TO_REGCLASS at all. I suspect it is simply wrong for the PowerPC back-end to emit it here. This patch changes the PowerPC back-end to directly emit COPY instead of COPY_TO_REGCLASS and removes the special handling in the instruction printers. Differential Revision: http://reviews.llvm.org/D18605 llvm-svn: 265020 --- .../PowerPC/InstPrinter/PPCInstPrinter.cpp | 11 ------- .../PowerPC/MCTargetDesc/PPCMCCodeEmitter.cpp | 5 --- llvm/lib/Target/PowerPC/PPCFastISel.cpp | 7 ++-- llvm/test/CodeGen/PowerPC/fast-isel-fpconv.ll | 33 +++++++++++++++++++ 4 files changed, 36 insertions(+), 20 deletions(-) create mode 100644 llvm/test/CodeGen/PowerPC/fast-isel-fpconv.ll diff --git a/llvm/lib/Target/PowerPC/InstPrinter/PPCInstPrinter.cpp b/llvm/lib/Target/PowerPC/InstPrinter/PPCInstPrinter.cpp index 5214303ec82a1..d9d9b4f180f71 100644 --- a/llvm/lib/Target/PowerPC/InstPrinter/PPCInstPrinter.cpp +++ b/llvm/lib/Target/PowerPC/InstPrinter/PPCInstPrinter.cpp @@ -136,17 +136,6 @@ void PPCInstPrinter::printInst(const MCInst *MI, raw_ostream &O, return; } - // For fast-isel, a COPY_TO_REGCLASS may survive this long. This is - // used when converting a 32-bit float to a 64-bit float as part of - // conversion to an integer (see PPCFastISel.cpp:SelectFPToI()), - // as otherwise we have problems with incorrect register classes - // in machine instruction verification. For now, just avoid trying - // to print it as such an instruction has no effect (a 32-bit float - // in a register is already in 64-bit form, just with lower - // precision). FIXME: Is there a better solution? - if (MI->getOpcode() == TargetOpcode::COPY_TO_REGCLASS) - return; - if (!printAliasInstr(MI, O)) printInstruction(MI, O); printAnnotation(O, Annot); diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.cpp index 9fa5beb50c0ce..0f6385e0a674f 100644 --- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.cpp +++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCMCCodeEmitter.cpp @@ -105,13 +105,8 @@ class PPCMCCodeEmitter : public MCCodeEmitter { void encodeInstruction(const MCInst &MI, raw_ostream &OS, SmallVectorImpl &Fixups, const MCSubtargetInfo &STI) const override { - // For fast-isel, a float COPY_TO_REGCLASS can survive this long. - // It's just a nop to keep the register classes happy, so don't - // generate anything. unsigned Opcode = MI.getOpcode(); const MCInstrDesc &Desc = MCII.get(Opcode); - if (Opcode == TargetOpcode::COPY_TO_REGCLASS) - return; uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI); diff --git a/llvm/lib/Target/PowerPC/PPCFastISel.cpp b/llvm/lib/Target/PowerPC/PPCFastISel.cpp index 360fbb61bc5e5..901a7502cb2c4 100644 --- a/llvm/lib/Target/PowerPC/PPCFastISel.cpp +++ b/llvm/lib/Target/PowerPC/PPCFastISel.cpp @@ -1131,14 +1131,13 @@ bool PPCFastISel::SelectFPToI(const Instruction *I, bool IsSigned) { return false; // Convert f32 to f64 if necessary. This is just a meaningless copy - // to get the register class right. COPY_TO_REGCLASS is needed since - // a COPY from F4RC to F8RC is converted to a F4RC-F4RC copy downstream. + // to get the register class right. const TargetRegisterClass *InRC = MRI.getRegClass(SrcReg); if (InRC == &PPC::F4RCRegClass) { unsigned TmpReg = createResultReg(&PPC::F8RCRegClass); BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, - TII.get(TargetOpcode::COPY_TO_REGCLASS), TmpReg) - .addReg(SrcReg).addImm(PPC::F8RCRegClassID); + TII.get(TargetOpcode::COPY), TmpReg) + .addReg(SrcReg); SrcReg = TmpReg; } diff --git a/llvm/test/CodeGen/PowerPC/fast-isel-fpconv.ll b/llvm/test/CodeGen/PowerPC/fast-isel-fpconv.ll new file mode 100644 index 0000000000000..eb14cf2aa769d --- /dev/null +++ b/llvm/test/CodeGen/PowerPC/fast-isel-fpconv.ll @@ -0,0 +1,33 @@ +; RUN: llc -mtriple powerpc64-unknown-linux-gnu -fast-isel -O0 < %s | FileCheck %s + +; The second fctiwz would use an incorrect input register due to wrong handling +; of COPY_TO_REGCLASS in the FastISel pass. Verify that this is fixed. + +declare void @func(i32, i32) + +define void @test() { +; CHECK-LABEL: test: +; CHECK: bl func +; CHECK-NEXT: nop +; CHECK: lfs [[REG:[0-9]+]], +; CHECK: fctiwz {{[0-9]+}}, [[REG]] +; CHECK: bl func +; CHECK-NEXT: nop + + %memPos = alloca float, align 4 + store float 1.500000e+01, float* %memPos + %valPos = load float, float* %memPos + + %memNeg = alloca float, align 4 + store float -1.500000e+01, float* %memNeg + %valNeg = load float, float* %memNeg + + %FloatToIntPos = fptosi float %valPos to i32 + call void @func(i32 15, i32 %FloatToIntPos) + + %FloatToIntNeg = fptosi float %valNeg to i32 + call void @func(i32 -15, i32 %FloatToIntNeg) + + ret void +} +