@@ -1402,40 +1402,6 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
14021402 return nullptr ;
14031403 }
14041404
1405- std::optional<bool > CanReplacePointersIfEqualCache[2 ] = {std::nullopt ,
1406- std::nullopt };
1407- std::optional<bool > ShareSameUnderlyingObject = std::nullopt ;
1408- auto CanReplacePointersIfEqual = [&](Value *From, Value *To,
1409- std::optional<bool > &Cache) -> bool {
1410- if (Cache.has_value ())
1411- return *Cache;
1412-
1413- assert (From->getType () == To->getType () &&
1414- " values must have matching types" );
1415- // Not a pointer, just return true.
1416- if (!From->getType ()->isPointerTy ()) {
1417- Cache = true ;
1418- return true ;
1419- }
1420-
1421- if (isa<ConstantPointerNull>(To)) {
1422- Cache = true ;
1423- return true ;
1424- }
1425- if (isa<Constant>(To) &&
1426- isDereferenceablePointer (To, Type::getInt8Ty (To->getContext ()), DL)) {
1427- Cache = true ;
1428- return true ;
1429- }
1430-
1431- if (!ShareSameUnderlyingObject.has_value ())
1432- ShareSameUnderlyingObject = getUnderlyingObjectAggressive (From) ==
1433- getUnderlyingObjectAggressive (To);
1434-
1435- Cache = *ShareSameUnderlyingObject;
1436- return *ShareSameUnderlyingObject;
1437- };
1438-
14391405 Value *CmpLHS = Cmp.getOperand (0 ), *CmpRHS = Cmp.getOperand (1 );
14401406 auto ReplaceOldOpWithNewOp = [&](Value *OldOp, Value *NewOp,
14411407 uint32_t Direction) -> Instruction * {
@@ -1447,9 +1413,6 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
14471413 // in the cmp and in f(Y).
14481414 if (TrueVal == OldOp && (isa<Constant>(OldOp) || !isa<Constant>(NewOp)))
14491415 return nullptr ;
1450- if (!CanReplacePointersIfEqual (OldOp, NewOp,
1451- CanReplacePointersIfEqualCache[Direction]))
1452- return nullptr ;
14531416
14541417 if (Value *V = simplifyWithOpReplaced (TrueVal, OldOp, NewOp, SQ,
14551418 /* AllowRefinement=*/ true )) {
@@ -1487,10 +1450,16 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
14871450 return nullptr ;
14881451 };
14891452
1490- if (Instruction *R = ReplaceOldOpWithNewOp (CmpLHS, CmpRHS, 0 ))
1491- return R;
1492- if (Instruction *R = ReplaceOldOpWithNewOp (CmpRHS, CmpLHS, 1 ))
1493- return R;
1453+ bool CanReplaceCmpLHSWithRHS = canReplacePointersIfEqual (CmpLHS, CmpRHS, DL);
1454+ if (CanReplaceCmpLHSWithRHS) {
1455+ if (Instruction *R = ReplaceOldOpWithNewOp (CmpLHS, CmpRHS, 0 ))
1456+ return R;
1457+ }
1458+ bool CanReplaceCmpRHSWithLHS = canReplacePointersIfEqual (CmpRHS, CmpLHS, DL);
1459+ if (CanReplaceCmpRHSWithLHS) {
1460+ if (Instruction *R = ReplaceOldOpWithNewOp (CmpRHS, CmpLHS, 1 ))
1461+ return R;
1462+ }
14941463
14951464 auto *FalseInst = dyn_cast<Instruction>(FalseVal);
14961465 if (!FalseInst)
@@ -1505,13 +1474,11 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
15051474 // Example:
15061475 // (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1
15071476 SmallVector<Instruction *> DropFlags;
1508- if ((CanReplacePointersIfEqual (CmpLHS, CmpRHS,
1509- CanReplacePointersIfEqualCache[0 ]) &&
1477+ if ((CanReplaceCmpLHSWithRHS &&
15101478 simplifyWithOpReplaced (FalseVal, CmpLHS, CmpRHS, SQ,
15111479 /* AllowRefinement */ false ,
15121480 &DropFlags) == TrueVal) ||
1513- (CanReplacePointersIfEqual (CmpRHS, CmpLHS,
1514- CanReplacePointersIfEqualCache[1 ]) &&
1481+ (CanReplaceCmpRHSWithLHS &&
15151482 simplifyWithOpReplaced (FalseVal, CmpRHS, CmpLHS, SQ,
15161483 /* AllowRefinement */ false ,
15171484 &DropFlags) == TrueVal)) {
0 commit comments