@@ -1365,16 +1365,27 @@ removeEntryFromStack(const StackEntry &E, ConstraintInfo &Info,
1365
1365
ReproducerCondStack.pop_back ();
1366
1366
}
1367
1367
1368
- // / Check if the first condition for an AND implies the second.
1369
- static bool checkAndSecondOpImpliedByFirst (
1370
- FactOrCheck &CB, ConstraintInfo &Info, Module *ReproducerModule,
1371
- SmallVectorImpl<ReproducerEntry> &ReproducerCondStack,
1372
- SmallVectorImpl<StackEntry> &DFSInStack) {
1368
+ // / Check if either the first condition of an AND is implied by the second or
1369
+ // / vice versa.
1370
+ static bool
1371
+ checkAndOpImpliedByOther (FactOrCheck &CB, ConstraintInfo &Info,
1372
+ Module *ReproducerModule,
1373
+ SmallVectorImpl<ReproducerEntry> &ReproducerCondStack,
1374
+ SmallVectorImpl<StackEntry> &DFSInStack) {
1373
1375
1374
1376
CmpInst::Predicate Pred;
1375
1377
Value *A, *B;
1376
1378
Instruction *And = CB.getContextInst ();
1377
- if (!match (And->getOperand (0 ), m_ICmp (Pred, m_Value (A), m_Value (B))))
1379
+ CmpInst *CmpToCheck = cast<CmpInst>(CB.getInstructionToSimplify ());
1380
+ unsigned OtherOpIdx = And->getOperand (0 ) == CmpToCheck ? 1 : 0 ;
1381
+
1382
+ // Don't try to simplify the first condition of a select by the second, as
1383
+ // this may make the select more poisonous than the original one.
1384
+ // TODO: check if the first operand may be poison.
1385
+ if (OtherOpIdx != 0 && isa<SelectInst>(And))
1386
+ return false ;
1387
+
1388
+ if (!match (And->getOperand (OtherOpIdx), m_ICmp (Pred, m_Value (A), m_Value (B))))
1378
1389
return false ;
1379
1390
1380
1391
// Optimistically add fact from first condition.
@@ -1385,11 +1396,12 @@ static bool checkAndSecondOpImpliedByFirst(
1385
1396
1386
1397
bool Changed = false ;
1387
1398
// Check if the second condition can be simplified now.
1388
- ICmpInst *Cmp = cast<ICmpInst>(And->getOperand (1 ));
1389
- if (auto ImpliedCondition = checkCondition (
1390
- Cmp->getPredicate (), Cmp->getOperand (0 ), Cmp->getOperand (1 ), Cmp,
1391
- Info, CB.NumIn , CB.NumOut , CB.getContextInst ())) {
1392
- And->setOperand (1 , ConstantInt::getBool (And->getType (), *ImpliedCondition));
1399
+ if (auto ImpliedCondition =
1400
+ checkCondition (CmpToCheck->getPredicate (), CmpToCheck->getOperand (0 ),
1401
+ CmpToCheck->getOperand (1 ), CmpToCheck, Info, CB.NumIn ,
1402
+ CB.NumOut , CB.getContextInst ())) {
1403
+ And->setOperand (1 - OtherOpIdx,
1404
+ ConstantInt::getBool (And->getType (), *ImpliedCondition));
1393
1405
Changed = true ;
1394
1406
}
1395
1407
@@ -1609,11 +1621,11 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT, LoopInfo &LI,
1609
1621
bool Simplified = checkAndReplaceCondition (
1610
1622
Cmp, Info, CB.NumIn , CB.NumOut , CB.getContextInst (),
1611
1623
ReproducerModule.get (), ReproducerCondStack, S.DT , ToRemove);
1612
- if (!Simplified && match (CB. getContextInst (),
1613
- m_LogicalAnd (m_Value (), m_Specific (Inst )))) {
1624
+ if (!Simplified &&
1625
+ match (CB. getContextInst (), m_LogicalAnd (m_Value (), m_Value ( )))) {
1614
1626
Simplified =
1615
- checkAndSecondOpImpliedByFirst (CB, Info, ReproducerModule.get (),
1616
- ReproducerCondStack, DFSInStack);
1627
+ checkAndOpImpliedByOther (CB, Info, ReproducerModule.get (),
1628
+ ReproducerCondStack, DFSInStack);
1617
1629
}
1618
1630
Changed |= Simplified;
1619
1631
}
0 commit comments