Skip to content

Commit e437ba2

Browse files
committed
[InstSimplify] Add additional checks when substituting pointers
1 parent d09b521 commit e437ba2

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 11 additions & 6 deletions
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"
@@ -4731,12 +4732,16 @@ static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal,
47314732
// the arms of the select. See if substituting this value into the arm and
47324733
// simplifying the result yields the same value as the other arm.
47334734
if (Pred == ICmpInst::ICMP_EQ) {
4734-
if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
4735-
FalseVal, Q, MaxRecurse))
4736-
return V;
4737-
if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
4738-
FalseVal, Q, MaxRecurse))
4739-
return V;
4735+
if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4736+
canReplacePointersIfEqual(CmpLHS, CmpRHS, Q.DL))
4737+
if (Value *V = simplifySelectWithEquivalence({{CmpLHS, CmpRHS}}, TrueVal,
4738+
FalseVal, Q, MaxRecurse))
4739+
return V;
4740+
if (CmpLHS->getType()->isIntOrIntVectorTy() ||
4741+
canReplacePointersIfEqual(CmpRHS, CmpLHS, Q.DL))
4742+
if (Value *V = simplifySelectWithEquivalence({{CmpRHS, CmpLHS}}, TrueVal,
4743+
FalseVal, Q, MaxRecurse))
4744+
return V;
47404745

47414746
Value *X;
47424747
Value *Y;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ cond.end: ; preds = %entry, %cond.true
247247

248248
define ptr @icmp_ptr_eq_replace(ptr %a, ptr %b) {
249249
; CHECK-LABEL: @icmp_ptr_eq_replace(
250-
; CHECK-NEXT: ret ptr [[B:%.*]]
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]]
251253
;
252254
%cmp = icmp eq ptr %a, %b
253255
%sel = select i1 %cmp, ptr %a, ptr %b

0 commit comments

Comments
 (0)