From b0f78769178ebb5e0691aca5143fead7f79ee55c Mon Sep 17 00:00:00 2001 From: luxufan Date: Mon, 5 Dec 2022 19:38:25 +0800 Subject: [PATCH] [SCCP] Propagate equality of a not-constant The equality state of a not-constant can be used to do constant propagation. For example, ``` define i32 @equal_not_constant(ptr noundef %p, ptr noundef %q) { entry: %cmp = icmp ne ptr %p, null br i1 %cmp, label %if.then, label %if.end if.then: ; preds = %entry %cmp.then = icmp eq ptr %p, %q br i1 %cmp.then, label %if.then1, label %if.end if.then1: ; preds = %if.then %cmp.then1 = icmp ne ptr %q, null call void @use(i1 %cmp.then1) br label %if.end if.end: ret i32 0 } ``` In this case, we can fold `%cmp.then1` as `true`. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D139289 --- llvm/lib/Transforms/Utils/SCCPSolver.cpp | 5 +- llvm/test/Transforms/SCCP/conditions-ranges.ll | 63 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/Utils/SCCPSolver.cpp b/llvm/lib/Transforms/Utils/SCCPSolver.cpp index 4b779c5..4bfec66 100644 --- a/llvm/lib/Transforms/Utils/SCCPSolver.cpp +++ b/llvm/lib/Transforms/Utils/SCCPSolver.cpp @@ -1344,9 +1344,10 @@ void SCCPInstVisitor::handleCallResult(CallBase &CB) { IV, &CB, ValueLatticeElement::getRange(NewCR, /*MayIncludeUndef*/ false)); return; - } else if (Pred == CmpInst::ICMP_EQ && CondVal.isConstant()) { + } else if (Pred == CmpInst::ICMP_EQ && + (CondVal.isConstant() || CondVal.isNotConstant())) { // For non-integer values or integer constant expressions, only - // propagate equal constants. + // propagate equal constants or not-constants. addAdditionalUser(OtherOp, &CB); mergeInValue(IV, &CB, CondVal); return; diff --git a/llvm/test/Transforms/SCCP/conditions-ranges.ll b/llvm/test/Transforms/SCCP/conditions-ranges.ll index e108d63..3de2e9c 100644 --- a/llvm/test/Transforms/SCCP/conditions-ranges.ll +++ b/llvm/test/Transforms/SCCP/conditions-ranges.ll @@ -1370,6 +1370,69 @@ bb142: ; preds = %bb139 ret void } +define i32 @equal_not_constant(ptr noundef %p, ptr noundef %q) { +; CHECK-LABEL: @equal_not_constant( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[P:%.*]], null +; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]] +; CHECK: if.then: +; CHECK-NEXT: [[CMP_THEN:%.*]] = icmp eq ptr [[P]], [[Q:%.*]] +; CHECK-NEXT: br i1 [[CMP_THEN]], label [[IF_THEN1:%.*]], label [[IF_END]] +; CHECK: if.then1: +; CHECK-NEXT: call void @use(i1 true) +; CHECK-NEXT: br label [[IF_END]] +; CHECK: if.end: +; CHECK-NEXT: ret i32 0 +; +entry: + %cmp = icmp ne ptr %p, null + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + %cmp.then = icmp eq ptr %p, %q + br i1 %cmp.then, label %if.then1, label %if.end + +if.then1: ; preds = %if.then + %cmp.then1 = icmp ne ptr %q, null + call void @use(i1 %cmp.then1) + br label %if.end + +if.end: + ret i32 0 +} + +define i32 @not_equal_not_constant(ptr noundef %p, ptr noundef %q) { +; CHECK-LABEL: @not_equal_not_constant( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[P:%.*]], null +; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]] +; CHECK: if.then: +; CHECK-NEXT: [[CMP_THEN:%.*]] = icmp ne ptr [[P]], [[Q:%.*]] +; CHECK-NEXT: br i1 [[CMP_THEN]], label [[IF_THEN1:%.*]], label [[IF_END]] +; CHECK: if.then1: +; CHECK-NEXT: [[CMP_THEN1:%.*]] = icmp ne ptr [[Q]], null +; CHECK-NEXT: call void @use(i1 [[CMP_THEN1]]) +; CHECK-NEXT: br label [[IF_END]] +; CHECK: if.end: +; CHECK-NEXT: ret i32 0 +; +entry: + %cmp = icmp ne ptr %p, null + br i1 %cmp, label %if.then, label %if.end + +if.then: ; preds = %entry + %cmp.then = icmp ne ptr %p, %q + br i1 %cmp.then, label %if.then1, label %if.end + +if.then1: ; preds = %if.then + %cmp.then1 = icmp ne ptr %q, null + call void @use(i1 %cmp.then1) + br label %if.end + +if.end: + ret i32 0 +} + define i1 @ptr_icmp_data_layout() { ; CHECK-LABEL: @ptr_icmp_data_layout( ; CHECK-NEXT: ret i1 false -- 2.7.4