diff --git a/lib/Target/PowerPC/PPCISelLowering.cpp b/lib/Target/PowerPC/PPCISelLowering.cpp index 6b24c21c8bc..e93c7819b1a 100644 --- a/lib/Target/PowerPC/PPCISelLowering.cpp +++ b/lib/Target/PowerPC/PPCISelLowering.cpp @@ -9780,6 +9780,26 @@ bool PPCTargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { return NumBits1 == 64 && NumBits2 == 32; } +bool PPCTargetLowering::isZExtFree(SDValue Val, EVT VT2) const { + // Generally speaking, zexts are not free, but they are free when they can be + // folded with other operations. + if (LoadSDNode *LD = dyn_cast(Val)) { + EVT MemVT = LD->getMemoryVT(); + if ((MemVT == MVT::i1 || MemVT == MVT::i8 || MemVT == MVT::i16 || + (Subtarget.isPPC64() && MemVT == MVT::i32)) && + (LD->getExtensionType() == ISD::NON_EXTLOAD || + LD->getExtensionType() == ISD::ZEXTLOAD)) + return true; + } + + // FIXME: Add other cases... + // - 32-bit shifts with a zext to i64 + // - zext after ctlz, bswap, etc. + // - zext after and by a constant mask + + return TargetLowering::isZExtFree(Val, VT2); +} + bool PPCTargetLowering::isLegalICmpImmediate(int64_t Imm) const { return isInt<16>(Imm) || isUInt<16>(Imm); } diff --git a/lib/Target/PowerPC/PPCISelLowering.h b/lib/Target/PowerPC/PPCISelLowering.h index 6149a21f443..db5a3e42d52 100644 --- a/lib/Target/PowerPC/PPCISelLowering.h +++ b/lib/Target/PowerPC/PPCISelLowering.h @@ -526,6 +526,8 @@ namespace llvm { bool isTruncateFree(Type *Ty1, Type *Ty2) const override; bool isTruncateFree(EVT VT1, EVT VT2) const override; + bool isZExtFree(SDValue Val, EVT VT2) const override; + /// \brief Returns true if it is beneficial to convert a load of a constant /// to just the constant itself. bool shouldConvertConstantLoadToIntImm(const APInt &Imm, diff --git a/test/CodeGen/PowerPC/zext-free.ll b/test/CodeGen/PowerPC/zext-free.ll new file mode 100644 index 00000000000..080dbaa58da --- /dev/null +++ b/test/CodeGen/PowerPC/zext-free.ll @@ -0,0 +1,37 @@ +; RUN: llc -mcpu=ppc64 < %s | FileCheck %s +target datalayout = "E-m:e-i64:64-n32:64" +target triple = "powerpc64-unknown-linux-gnu" + +; Function Attrs: noreturn nounwind +define signext i32 @_Z1fRPc(i8** nocapture dereferenceable(8) %p) #0 { +entry: + %.pre = load i8** %p, align 8 + br label %loop + +loop: ; preds = %loop.backedge, %entry + %0 = phi i8* [ %.pre, %entry ], [ %.be, %loop.backedge ] + %1 = load i8* %0, align 1 + %tobool = icmp eq i8 %1, 0 + %incdec.ptr = getelementptr inbounds i8* %0, i64 1 + store i8* %incdec.ptr, i8** %p, align 8 + %2 = load i8* %incdec.ptr, align 1 + %tobool2 = icmp ne i8 %2, 0 + %or.cond = and i1 %tobool, %tobool2 + br i1 %or.cond, label %if.then3, label %loop.backedge + +if.then3: ; preds = %loop + %incdec.ptr4 = getelementptr inbounds i8* %0, i64 2 + store i8* %incdec.ptr4, i8** %p, align 8 + br label %loop.backedge + +loop.backedge: ; preds = %if.then3, %loop + %.be = phi i8* [ %incdec.ptr4, %if.then3 ], [ %incdec.ptr, %loop ] + br label %loop + +; CHECK-LABEL: @_Z1fRPc +; CHECK-NOT: rlwinm {{[0-9]+}}, {{[0-9]+}}, 0, 24, 31 +; CHECK-NOT: clrlwi {{[0-9]+}}, {{[0-9]+}}, 24 +} + +attributes #0 = { noreturn nounwind } +