From e72825927839659defde72a63ed4a8f907271a9d Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 21 Aug 2019 11:56:08 +0000 Subject: [InstCombine] narrow icmp with extended operands of different widths An intermediate extend is used to widen the narrow operand to the width of the other (wider) operand. At that point, we have the same logic as the existing transform that was restricted to folds of equal width zext/sext. This mostly solves PR42700: https://bugs.llvm.org/show_bug.cgi?id=42700 llvm-svn: 369519 --- .../Transforms/InstCombine/InstCombineCompares.cpp | 23 ++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp') diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp index b318d0b2de9..5e0a3c37979 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4026,7 +4026,8 @@ Instruction *InstCombiner::foldICmpEquality(ICmpInst &I) { return nullptr; } -static Instruction *foldICmpWithZextOrSext(ICmpInst &ICmp) { +static Instruction *foldICmpWithZextOrSext(ICmpInst &ICmp, + InstCombiner::BuilderTy &Builder) { assert(isa(ICmp.getOperand(0)) && "Expected cast for operand 0"); auto *CastOp0 = cast(ICmp.getOperand(0)); Value *X; @@ -4038,15 +4039,25 @@ static Instruction *foldICmpWithZextOrSext(ICmpInst &ICmp) { if (auto *CastOp1 = dyn_cast(ICmp.getOperand(1))) { // If the signedness of the two casts doesn't agree (i.e. one is a sext // and the other is a zext), then we can't handle this. + // TODO: This is too strict. We can handle some predicates (equality?). if (CastOp0->getOpcode() != CastOp1->getOpcode()) return nullptr; // Not an extension from the same type? - // TODO: Handle this by extending the narrower operand to the type of - // the wider operand. Value *Y = CastOp1->getOperand(0); - if (X->getType() != Y->getType()) - return nullptr; + Type *XTy = X->getType(), *YTy = Y->getType(); + if (XTy != YTy) { + // One of the casts must have one use because we are creating a new cast. + if (!CastOp0->hasOneUse() && !CastOp1->hasOneUse()) + return nullptr; + // Extend the narrower operand to the type of the wider operand. + if (XTy->getScalarSizeInBits() < YTy->getScalarSizeInBits()) + X = Builder.CreateCast(CastOp0->getOpcode(), X, YTy); + else if (YTy->getScalarSizeInBits() < XTy->getScalarSizeInBits()) + Y = Builder.CreateCast(CastOp0->getOpcode(), Y, XTy); + else + return nullptr; + } // (zext X) == (zext Y) --> X == Y // (sext X) == (sext Y) --> X == Y @@ -4148,7 +4159,7 @@ Instruction *InstCombiner::foldICmpWithCastOp(ICmpInst &ICmp) { return new ICmpInst(ICmp.getPredicate(), Op0Src, NewOp1); } - return foldICmpWithZextOrSext(ICmp); + return foldICmpWithZextOrSext(ICmp, Builder); } static bool isNeutralValue(Instruction::BinaryOps BinaryOp, Value *RHS) { -- cgit v1.2.3