@@ -104,8 +104,7 @@ bool isConstant(const ValueLatticeElement &LV) {
104
104
// ValueLatticeElement::isOverdefined() and is intended to be used in the
105
105
// transition to ValueLatticeElement.
106
106
bool isOverdefined (const ValueLatticeElement &LV) {
107
- return LV.isOverdefined () ||
108
- (LV.isConstantRange () && !LV.getConstantRange ().isSingleElement ());
107
+ return !LV.isUnknownOrUndef () && !isConstant (LV);
109
108
}
110
109
111
110
// ===----------------------------------------------------------------------===//
@@ -1123,7 +1122,9 @@ static ValueLatticeElement getValueFromMetadata(const Instruction *I) {
1123
1122
if (I->getType ()->isIntegerTy ())
1124
1123
return ValueLatticeElement::getRange (
1125
1124
getConstantRangeFromMetadata (*Ranges));
1126
- // TODO: Also handle MD_nonnull.
1125
+ if (I->hasMetadata (LLVMContext::MD_nonnull))
1126
+ return ValueLatticeElement::getNot (
1127
+ ConstantPointerNull::get (cast<PointerType>(I->getType ())));
1127
1128
return ValueLatticeElement::getOverdefined ();
1128
1129
}
1129
1130
@@ -1291,6 +1292,17 @@ void SCCPSolver::handleCallResult(CallBase &CB) {
1291
1292
return ;
1292
1293
}
1293
1294
1295
+ // TODO: Actually filp MayIncludeUndef for the created range to false,
1296
+ // once most places in the optimizer respect the branches on
1297
+ // undef/poison are UB rule. The reason why the new range cannot be
1298
+ // undef is as follows below:
1299
+ // The new range is based on a branch condition. That guarantees that
1300
+ // neither of the compare operands can be undef in the branch targets,
1301
+ // unless we have conditions that are always true/false (e.g. icmp ule
1302
+ // i32, %a, i32_max). For the latter overdefined/empty range will be
1303
+ // inferred, but the branch will get folded accordingly anyways.
1304
+ bool MayIncludeUndef = !isa<PredicateAssume>(PI);
1305
+
1294
1306
ValueLatticeElement CondVal = getValueState (OtherOp);
1295
1307
ValueLatticeElement &IV = ValueState[&CB];
1296
1308
if (CondVal.isConstantRange () || CopyOfVal.isConstantRange ()) {
@@ -1316,25 +1328,23 @@ void SCCPSolver::handleCallResult(CallBase &CB) {
1316
1328
NewCR = CopyOfCR;
1317
1329
1318
1330
addAdditionalUser (OtherOp, &CB);
1319
- // TODO: Actually filp MayIncludeUndef for the created range to false,
1320
- // once most places in the optimizer respect the branches on
1321
- // undef/poison are UB rule. The reason why the new range cannot be
1322
- // undef is as follows below:
1323
- // The new range is based on a branch condition. That guarantees that
1324
- // neither of the compare operands can be undef in the branch targets,
1325
- // unless we have conditions that are always true/false (e.g. icmp ule
1326
- // i32, %a, i32_max). For the latter overdefined/empty range will be
1327
- // inferred, but the branch will get folded accordingly anyways.
1328
1331
mergeInValue (
1329
1332
IV, &CB,
1330
- ValueLatticeElement::getRange (NewCR, /* MayIncludeUndef= */ true ));
1333
+ ValueLatticeElement::getRange (NewCR, MayIncludeUndef));
1331
1334
return ;
1332
1335
} else if (Pred == CmpInst::ICMP_EQ && CondVal.isConstant ()) {
1333
1336
// For non-integer values or integer constant expressions, only
1334
1337
// propagate equal constants.
1335
1338
addAdditionalUser (OtherOp, &CB);
1336
1339
mergeInValue (IV, &CB, CondVal);
1337
1340
return ;
1341
+ } else if (Pred == CmpInst::ICMP_NE && CondVal.isConstant () &&
1342
+ !MayIncludeUndef) {
1343
+ // Propagate inequalities.
1344
+ addAdditionalUser (OtherOp, &CB);
1345
+ mergeInValue (IV, &CB,
1346
+ ValueLatticeElement::getNot (CondVal.getConstant ()));
1347
+ return ;
1338
1348
}
1339
1349
1340
1350
return (void )mergeInValue (IV, &CB, CopyOfVal);
0 commit comments