Skip to content

Commit 5374a3d

Browse files
nikicpaulhuggett
authored andcommitted
[InstSimplify] Simplify both operands of select before comparing (llvm#121753)
In the simplifySelectWithEquivalence fold, simplify both operands before comparing them, instead of comparing one simplified operand with a non-simplified operand. This is slightly more powerful.
1 parent 632e6c8 commit 5374a3d

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,13 +4599,21 @@ static Value *simplifySelectWithEquivalence(Value *CmpLHS, Value *CmpRHS,
45994599
Value *TrueVal, Value *FalseVal,
46004600
const SimplifyQuery &Q,
46014601
unsigned MaxRecurse) {
4602-
if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q.getWithoutUndef(),
4602+
Value *SimplifiedFalseVal =
4603+
simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q.getWithoutUndef(),
46034604
/* AllowRefinement */ false,
4604-
/* DropFlags */ nullptr, MaxRecurse) == TrueVal)
4605-
return FalseVal;
4606-
if (simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q,
4605+
/* DropFlags */ nullptr, MaxRecurse);
4606+
if (!SimplifiedFalseVal)
4607+
SimplifiedFalseVal = FalseVal;
4608+
4609+
Value *SimplifiedTrueVal =
4610+
simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q,
46074611
/* AllowRefinement */ true,
4608-
/* DropFlags */ nullptr, MaxRecurse) == FalseVal)
4612+
/* DropFlags */ nullptr, MaxRecurse);
4613+
if (!SimplifiedTrueVal)
4614+
SimplifiedTrueVal = TrueVal;
4615+
4616+
if (SimplifiedFalseVal == SimplifiedTrueVal)
46094617
return FalseVal;
46104618

46114619
return nullptr;

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4453,11 +4453,8 @@ define i32 @src_no_trans_select_or_eq0_or_and(i32 %x, i32 %y) {
44534453

44544454
define i32 @src_no_trans_select_or_eq0_or_xor(i32 %x, i32 %y) {
44554455
; CHECK-LABEL: @src_no_trans_select_or_eq0_or_xor(
4456-
; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
4457-
; CHECK-NEXT: [[OR0:%.*]] = icmp eq i32 [[OR]], 0
4458-
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[X]], [[Y]]
4459-
; CHECK-NEXT: [[COND:%.*]] = select i1 [[OR0]], i32 0, i32 [[XOR]]
4460-
; CHECK-NEXT: ret i32 [[COND]]
4456+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
4457+
; CHECK-NEXT: ret i32 [[XOR]]
44614458
;
44624459
%or = or i32 %x, %y
44634460
%or0 = icmp eq i32 %or, 0
@@ -4492,11 +4489,8 @@ define i32 @src_no_trans_select_or_eq0_xor_or(i32 %x, i32 %y) {
44924489

44934490
define i32 @src_no_trans_select_and_ne0_xor_or(i32 %x, i32 %y) {
44944491
; CHECK-LABEL: @src_no_trans_select_and_ne0_xor_or(
4495-
; CHECK-NEXT: [[OR:%.*]] = or i32 [[X:%.*]], [[Y:%.*]]
4496-
; CHECK-NEXT: [[OR0_NOT:%.*]] = icmp eq i32 [[OR]], 0
4497-
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[X]], [[Y]]
4498-
; CHECK-NEXT: [[COND:%.*]] = select i1 [[OR0_NOT]], i32 0, i32 [[XOR]]
4499-
; CHECK-NEXT: ret i32 [[COND]]
4492+
; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[X:%.*]], [[Y:%.*]]
4493+
; CHECK-NEXT: ret i32 [[XOR]]
45004494
;
45014495
%or = or i32 %x, %y
45024496
%or0 = icmp ne i32 %or, 0

0 commit comments

Comments
 (0)