@@ -2404,20 +2404,25 @@ RISCVTTIImpl::getPreferredAddressingMode(const Loop *L,
24042404
24052405bool RISCVTTIImpl::isLegalBaseRegForLSR (const SCEV *S) const {
24062406 if (ST->hasVendorXCheriot ()) {
2407- // Disallow any add-recurrence SCEV where the base offset is negative.
2407+ // Disallow any SCEV where the base offset is negative.
24082408 // This is needed because CHERIoT can't represent pointers before the
24092409 // beginning of an array.
2410+ auto IsAddOfNegativeCst = [](const SCEVAddExpr *A) {
2411+ if (!A) return false ;
2412+ const auto *Offset = dyn_cast<SCEVConstant>(StartAdd->getOperand (0 ));
2413+ if (Offset && Offset->getValue ()->isNegative ()) return true ;
2414+ Offset = dyn_cast<SCEVConstant>(StartAdd->getOperand (1 ));
2415+ if (Offset && Offset->getValue ()->isNegative ()) return true ;
2416+ return false ;
2417+ };
2418+
24102419 if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
2411- const auto *StartAdd = dyn_cast<SCEVAddExpr>(AddRec->getStart ());
2412- if (StartAdd) {
2413- const auto *Offset = dyn_cast<SCEVConstant>(StartAdd->getOperand (0 ));
2414- if (Offset && Offset->getValue ()->isNegative ())
2415- return false ;
2416- Offset = dyn_cast<SCEVConstant>(StartAdd->getOperand (1 ));
2417- if (Offset && Offset->getValue ()->isNegative ())
2418- return false ;
2419- }
2420+ if (const auto *Cst = dyn_cast<SCEVConstant>(AddRec->getStart ()))
2421+ return !Cst->getValue ()->isNegative ();
2422+ return !IsAddOfNegative (dyn_cast<SCEVAddExpr>(AddRec->getStart ()));
24202423 }
2424+
2425+ return !IsAddOfNegativeCst (dyn_cast < SCEVAddExpr (S));
24212426 }
24222427 return BasicTTIImplBase::isLegalBaseRegForLSR (S);
24232428}
0 commit comments