diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp index f4b378b82daec2..0e3f0647d62846 100644 --- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp +++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp @@ -245,11 +245,46 @@ static Value *simplifyInstruction(SCCPSolver &Solver, const APInt *RHSC; // Remove masking operations. if (match(&Inst, m_And(m_Value(X), m_LowBitMask(RHSC)))) { - ConstantRange LRange = GetRange(Inst.getOperand(0)); + ConstantRange LRange = GetRange(X); if (LRange.getUnsignedMax().ule(*RHSC)) return X; } + if (auto *II = dyn_cast(&Inst)) { + Intrinsic::ID IID = II->getIntrinsicID(); + // Check if we can simplify [us]cmp(X, Y) to X - Y. + if (IID == Intrinsic::scmp || IID == Intrinsic::ucmp) { + Value *LHS = II->getOperand(0); + Value *RHS = II->getOperand(1); + unsigned BitWidth = LHS->getType()->getScalarSizeInBits(); + ConstantRange LRange = GetRange(LHS); + if (LRange.isSizeLargerThan(3)) + return nullptr; + ConstantRange RRange = GetRange(RHS); + if (RRange.isSizeLargerThan(3)) + return nullptr; + ConstantRange RHSLower = RRange.sub(APInt(BitWidth, 1)); + ConstantRange RHSUpper = RRange.add(APInt(BitWidth, 1)); + ICmpInst::Predicate Pred = + IID == Intrinsic::scmp ? CmpInst::ICMP_SLE : CmpInst::ICMP_ULE; + if (!RHSLower.icmp(Pred, LRange) || !LRange.icmp(Pred, RHSUpper)) + return nullptr; + Instruction *Sub = BinaryOperator::CreateSub(LHS, RHS, Inst.getName(), + Inst.getIterator()); + if (IID == Intrinsic::scmp) + Sub->setHasNoSignedWrap(true); + Sub->setDebugLoc(Inst.getDebugLoc()); + InsertedValues.insert(Sub); + if (Sub->getType() != Inst.getType()) { + Sub = CastInst::CreateIntegerCast(Sub, Inst.getType(), true, "", + Inst.getIterator()); + Sub->setDebugLoc(Inst.getDebugLoc()); + InsertedValues.insert(Sub); + } + return Sub; + } + } + return nullptr; } diff --git a/llvm/test/Transforms/SCCP/uscmp.ll b/llvm/test/Transforms/SCCP/uscmp.ll new file mode 100644 index 00000000000000..ea5a65556ac551 --- /dev/null +++ b/llvm/test/Transforms/SCCP/uscmp.ll @@ -0,0 +1,78 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt -passes=sccp -S < %s | FileCheck %s + +define i32 @scmp_to_sub(i32 range(i32 -1, 2) %a) { +; CHECK-LABEL: define i32 @scmp_to_sub( +; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) { +; CHECK-NEXT: [[SCMP:%.*]] = sub nsw i32 [[A]], 0 +; CHECK-NEXT: ret i32 [[SCMP]] +; + %scmp = call i32 @llvm.scmp(i32 %a, i32 0) + ret i32 %scmp +} + +define i32 @scmp_zext_to_sub(i1 %a, i1 %b) { +; CHECK-LABEL: define i32 @scmp_zext_to_sub( +; CHECK-SAME: i1 [[A:%.*]], i1 [[B:%.*]]) { +; CHECK-NEXT: [[ZEXT_A:%.*]] = zext i1 [[A]] to i32 +; CHECK-NEXT: [[ZEXT_B:%.*]] = zext i1 [[B]] to i32 +; CHECK-NEXT: [[SCMP:%.*]] = sub nsw i32 [[ZEXT_A]], [[ZEXT_B]] +; CHECK-NEXT: ret i32 [[SCMP]] +; + %zext_a = zext i1 %a to i32 + %zext_b = zext i1 %b to i32 + %scmp = call i32 @llvm.scmp(i32 %zext_a, i32 %zext_b) + ret i32 %scmp +} + +define i8 @scmp_to_sub_trunc(i32 range(i32 -1, 2) %a) { +; CHECK-LABEL: define i8 @scmp_to_sub_trunc( +; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) { +; CHECK-NEXT: [[SCMP1:%.*]] = sub nsw i32 [[A]], 0 +; CHECK-NEXT: [[SCMP:%.*]] = trunc i32 [[SCMP1]] to i8 +; CHECK-NEXT: ret i8 [[SCMP]] +; + %scmp = call i8 @llvm.scmp(i32 %a, i32 0) + ret i8 %scmp +} + +define i64 @scmp_to_sub_sext(i32 range(i32 -1, 2) %a) { +; CHECK-LABEL: define i64 @scmp_to_sub_sext( +; CHECK-SAME: i32 range(i32 -1, 2) [[A:%.*]]) { +; CHECK-NEXT: [[SCMP1:%.*]] = sub nsw i32 [[A]], 0 +; CHECK-NEXT: [[SCMP:%.*]] = sext i32 [[SCMP1]] to i64 +; CHECK-NEXT: ret i64 [[SCMP]] +; + %scmp = call i64 @llvm.scmp(i32 %a, i32 0) + ret i64 %scmp +} + +define i32 @scmp_to_sub_small_range(i32 range(i32 -1, 1) %a) { +; CHECK-LABEL: define i32 @scmp_to_sub_small_range( +; CHECK-SAME: i32 range(i32 -1, 1) [[A:%.*]]) { +; CHECK-NEXT: [[SCMP:%.*]] = sub nsw i32 [[A]], 0 +; CHECK-NEXT: ret i32 [[SCMP]] +; + %scmp = call i32 @llvm.scmp(i32 %a, i32 0) + ret i32 %scmp +} + +define i32 @ucmp_to_sub(i32 range(i32 0, 3) %a) { +; CHECK-LABEL: define i32 @ucmp_to_sub( +; CHECK-SAME: i32 range(i32 0, 3) [[A:%.*]]) { +; CHECK-NEXT: [[SCMP:%.*]] = sub nsw i32 [[A]], 1 +; CHECK-NEXT: ret i32 [[SCMP]] +; + %scmp = call i32 @llvm.scmp(i32 %a, i32 1) + ret i32 %scmp +} + +define i32 @scmp_to_sub_large_range(i32 range(i32 -1, 3) %a) { +; CHECK-LABEL: define i32 @scmp_to_sub_large_range( +; CHECK-SAME: i32 range(i32 -1, 3) [[A:%.*]]) { +; CHECK-NEXT: [[SCMP:%.*]] = call i32 @llvm.scmp.i32.i32(i32 [[A]], i32 0) +; CHECK-NEXT: ret i32 [[SCMP]] +; + %scmp = call i32 @llvm.scmp(i32 %a, i32 0) + ret i32 %scmp +}