17
17
#include " llvm/Analysis/AssumptionCache.h"
18
18
#include " llvm/Analysis/CmpInstAnalysis.h"
19
19
#include " llvm/Analysis/InstructionSimplify.h"
20
+ #include " llvm/Analysis/Loads.h"
20
21
#include " llvm/Analysis/OverflowInstAnalysis.h"
21
22
#include " llvm/Analysis/ValueTracking.h"
22
23
#include " llvm/Analysis/VectorUtils.h"
42
43
#include " llvm/Support/KnownBits.h"
43
44
#include " llvm/Transforms/InstCombine/InstCombiner.h"
44
45
#include < cassert>
46
+ #include < optional>
45
47
#include < utility>
46
48
47
49
#define DEBUG_TYPE " instcombine"
@@ -1451,10 +1453,16 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
1451
1453
return nullptr ;
1452
1454
};
1453
1455
1454
- if (Instruction *R = ReplaceOldOpWithNewOp (CmpLHS, CmpRHS))
1455
- return R;
1456
- if (Instruction *R = ReplaceOldOpWithNewOp (CmpRHS, CmpLHS))
1457
- return R;
1456
+ bool CanReplaceCmpLHSWithRHS = canReplacePointersIfEqual (CmpLHS, CmpRHS, DL);
1457
+ if (CanReplaceCmpLHSWithRHS) {
1458
+ if (Instruction *R = ReplaceOldOpWithNewOp (CmpLHS, CmpRHS))
1459
+ return R;
1460
+ }
1461
+ bool CanReplaceCmpRHSWithLHS = canReplacePointersIfEqual (CmpRHS, CmpLHS, DL);
1462
+ if (CanReplaceCmpRHSWithLHS) {
1463
+ if (Instruction *R = ReplaceOldOpWithNewOp (CmpRHS, CmpLHS))
1464
+ return R;
1465
+ }
1458
1466
1459
1467
auto *FalseInst = dyn_cast<Instruction>(FalseVal);
1460
1468
if (!FalseInst)
@@ -1469,12 +1477,14 @@ Instruction *InstCombinerImpl::foldSelectValueEquivalence(SelectInst &Sel,
1469
1477
// Example:
1470
1478
// (X == 42) ? 43 : (X + 1) --> (X == 42) ? (X + 1) : (X + 1) --> X + 1
1471
1479
SmallVector<Instruction *> DropFlags;
1472
- if (simplifyWithOpReplaced (FalseVal, CmpLHS, CmpRHS, SQ,
1473
- /* AllowRefinement */ false ,
1474
- &DropFlags) == TrueVal ||
1475
- simplifyWithOpReplaced (FalseVal, CmpRHS, CmpLHS, SQ,
1476
- /* AllowRefinement */ false ,
1477
- &DropFlags) == TrueVal) {
1480
+ if ((CanReplaceCmpLHSWithRHS &&
1481
+ simplifyWithOpReplaced (FalseVal, CmpLHS, CmpRHS, SQ,
1482
+ /* AllowRefinement */ false ,
1483
+ &DropFlags) == TrueVal) ||
1484
+ (CanReplaceCmpRHSWithLHS &&
1485
+ simplifyWithOpReplaced (FalseVal, CmpRHS, CmpLHS, SQ,
1486
+ /* AllowRefinement */ false ,
1487
+ &DropFlags) == TrueVal)) {
1478
1488
for (Instruction *I : DropFlags) {
1479
1489
I->dropPoisonGeneratingAnnotations ();
1480
1490
Worklist.add (I);
0 commit comments