Skip to content

[InstSimplify] Add additional checks when substituting pointers #125385

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "llvm/Analysis/CmpInstAnalysis.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/Analysis/InstSimplifyFolder.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
#include "llvm/Analysis/MemoryBuiltins.h"
#include "llvm/Analysis/OverflowInstAnalysis.h"
Expand Down Expand Up @@ -4731,12 +4732,16 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
// the arms of the select. See if substituting this value into the arm and
// simplifying the result yields the same value as the other arm.
if (Pred == ICmpInst::ICMP_EQ) {
if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
FalseVal, Q, MaxRecurse))
return V;
if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
FalseVal, Q, MaxRecurse))
return V;
if (CmpLHS->getType()->isIntOrIntVectorTy() ||
canReplacePointersIfEqual(CmpLHS, CmpRHS, Q.DL))
if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
FalseVal, Q, MaxRecurse))
return V;
if (CmpLHS->getType()->isIntOrIntVectorTy() ||
canReplacePointersIfEqual(CmpRHS, CmpLHS, Q.DL))
if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
FalseVal, Q, MaxRecurse))
return V;

Value *X;
Value *Y;
Expand Down
32 changes: 32 additions & 0 deletions llvm/test/Transforms/InstSimplify/select-icmp.ll
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,35 @@ cond.true: ; preds = %entry
cond.end: ; preds = %entry, %cond.true
ret i8 0
}

define ptr @icmp_ptr_eq_replace(ptr %a, ptr %b) {
; CHECK-LABEL: @icmp_ptr_eq_replace(
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[A:%.*]], [[B1:%.*]]
; CHECK-NEXT: [[B:%.*]] = select i1 [[CMP]], ptr [[A]], ptr [[B1]]
; CHECK-NEXT: ret ptr [[B]]
;
%cmp = icmp eq ptr %a, %b
%sel = select i1 %cmp, ptr %a, ptr %b
ret ptr %sel
}

define ptr @icmp_ptr_eq_replace_null(ptr %a) {
; CHECK-LABEL: @icmp_ptr_eq_replace_null(
; CHECK-NEXT: ret ptr [[A:%.*]]
;
%cmp = icmp eq ptr %a, null
%sel = select i1 %cmp, ptr null, ptr %a
ret ptr %sel
}

define ptr @ptr_eq_replace_same_underlying_object(ptr %st, i64 %i, i64 %j) {
; CHECK-LABEL: @ptr_eq_replace_same_underlying_object(
; CHECK-NEXT: [[B:%.*]] = getelementptr inbounds i8, ptr [[ST:%.*]], i64 [[J:%.*]]
; CHECK-NEXT: ret ptr [[B]]
;
%a = getelementptr inbounds i8, ptr %st, i64 %i
%b = getelementptr inbounds i8, ptr %st, i64 %j
%cmp = icmp eq ptr %a, %b
%sel = select i1 %cmp, ptr %a, ptr %b
ret ptr %sel
}