Skip to content

Commit 56b4c11

Browse files
dtcxzywtstellar
authored andcommitted
[InstSimplify] Add additional checks when substituting pointers (llvm#125385)
Compile-time impact: https://llvm-compile-time-tracker.com/compare.php?from=d09b521624f263b5f1296f8d4771836b97e600cb&to=e437ba2cb83bb965e13ef00727671896f03ff84f&stat=instructions:u IR diff looks acceptable. Closes llvm#115574 (cherry picked from commit 1af627b)
1 parent 7fda02c commit 56b4c11

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

+11-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Analysis/CmpInstAnalysis.h"
2828
#include "llvm/Analysis/ConstantFolding.h"
2929
#include "llvm/Analysis/InstSimplifyFolder.h"
30+
#include "llvm/Analysis/Loads.h"
3031
#include "llvm/Analysis/LoopAnalysisManager.h"
3132
#include "llvm/Analysis/MemoryBuiltins.h"
3233
#include "llvm/Analysis/OverflowInstAnalysis.h"
@@ -4737,12 +4738,16 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
47374738
// the arms of the select. See if substituting this value into the arm and
47384739
// simplifying the result yields the same value as the other arm.
47394740
if (Pred == ICmpInst::ICMP_EQ) {
4740-
if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
4741-
FalseVal, Q, MaxRecurse))
4742-
return V;
4743-
if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
4744-
FalseVal, Q, MaxRecurse))
4745-
return V;
4741+
if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4742+
canReplacePointersIfEqual(CmpLHS, CmpRHS, Q.DL))
4743+
if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
4744+
FalseVal, Q, MaxRecurse))
4745+
return V;
4746+
if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4747+
canReplacePointersIfEqual(CmpRHS, CmpLHS, Q.DL))
4748+
if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
4749+
FalseVal, Q, MaxRecurse))
4750+
return V;
47464751

47474752
Value *X;
47484753
Value *Y;

llvm/test/Transforms/InstSimplify/select-icmp.ll

+32
Original file line numberDiff line numberDiff line change
@@ -244,3 +244,35 @@ cond.true: ; preds = %entry
244244
cond.end: ; preds = %entry, %cond.true
245245
ret i8 0
246246
}
247+
248+
define ptr @icmp_ptr_eq_replace(ptr %a, ptr %b) {
249+
; CHECK-LABEL: @icmp_ptr_eq_replace(
250+
; CHECK-NEXT: [[CMP:%.*]] = icmp eq ptr [[A:%.*]], [[B1:%.*]]
251+
; CHECK-NEXT: [[B:%.*]] = select i1 [[CMP]], ptr [[A]], ptr [[B1]]
252+
; CHECK-NEXT: ret ptr [[B]]
253+
;
254+
%cmp = icmp eq ptr %a, %b
255+
%sel = select i1 %cmp, ptr %a, ptr %b
256+
ret ptr %sel
257+
}
258+
259+
define ptr @icmp_ptr_eq_replace_null(ptr %a) {
260+
; CHECK-LABEL: @icmp_ptr_eq_replace_null(
261+
; CHECK-NEXT: ret ptr [[A:%.*]]
262+
;
263+
%cmp = icmp eq ptr %a, null
264+
%sel = select i1 %cmp, ptr null, ptr %a
265+
ret ptr %sel
266+
}
267+
268+
define ptr @ptr_eq_replace_same_underlying_object(ptr %st, i64 %i, i64 %j) {
269+
; CHECK-LABEL: @ptr_eq_replace_same_underlying_object(
270+
; CHECK-NEXT: [[B:%.*]] = getelementptr inbounds i8, ptr [[ST:%.*]], i64 [[J:%.*]]
271+
; CHECK-NEXT: ret ptr [[B]]
272+
;
273+
%a = getelementptr inbounds i8, ptr %st, i64 %i
274+
%b = getelementptr inbounds i8, ptr %st, i64 %j
275+
%cmp = icmp eq ptr %a, %b
276+
%sel = select i1 %cmp, ptr %a, ptr %b
277+
ret ptr %sel
278+
}

0 commit comments

Comments
 (0)