@@ -1045,13 +1045,15 @@ struct Score {
10451045 friend Score operator -(const Score &x, const Score &y) {
10461046 Score result;
10471047 for (unsigned i = 0 ; i != NumScoreKinds; ++i) {
1048+ ASSERT (x.Data [i] >= y.Data [i]);
10481049 result.Data [i] = x.Data [i] - y.Data [i];
10491050 }
10501051 return result;
10511052 }
10521053
10531054 friend Score &operator -=(Score &x, const Score &y) {
10541055 for (unsigned i = 0 ; i != NumScoreKinds; ++i) {
1056+ ASSERT (x.Data [i] >= y.Data [i]);
10551057 x.Data [i] -= y.Data [i];
10561058 }
10571059 return x;
@@ -2531,98 +2533,47 @@ class ConstraintSystem {
25312533 // / The number of the solution attempts we're looking at.
25322534 unsigned SolutionAttempt;
25332535
2534- // / Refers to the innermost partial solution scope.
2535- SolverScope *PartialSolutionScope = nullptr ;
2536+ // / The number of fixes in the innermost partial solution scope.
2537+ unsigned numPartialSolutionFixes = 0 ;
25362538
25372539 // Statistics
25382540 #define CS_STATISTIC (Name, Description ) unsigned Name = 0 ;
25392541 #include " ConstraintSolverStats.def"
25402542
2541- // / Check whether there are any retired constraints present.
2542- bool hasRetiredConstraints () const {
2543- return !retiredConstraints.empty ();
2544- }
2545-
25462543 // / Mark given constraint as retired along current solver path.
25472544 // /
25482545 // / \param constraint The constraint to retire temporarily.
25492546 void retireConstraint (Constraint *constraint) {
2550- retiredConstraints.push_front (constraint);
2551- }
2552-
2553- // / Iterate over all of the retired constraints registered with
2554- // / current solver state.
2555- // /
2556- // / \param processor The processor function to be applied to each of
2557- // / the constraints retrieved.
2558- void forEachRetired (llvm::function_ref<void (Constraint &)> processor) {
2559- for (auto &constraint : retiredConstraints)
2560- processor (constraint);
2547+ Trail.recordChange (SolverTrail::Change::RetiredConstraint (constraint));
25612548 }
25622549
25632550 // / Add new "generated" constraint along the current solver path.
25642551 // /
25652552 // / \param constraint The newly generated constraint.
25662553 void addGeneratedConstraint (Constraint *constraint) {
2567- assert (constraint && " Null generated constraint?" );
2568- generatedConstraints.push_back (constraint);
2554+ Trail.recordChange (SolverTrail::Change::GeneratedConstraint (constraint));
25692555 }
25702556
2571- // / Register given scope to be tracked by the current solver state,
2572- // / this helps to make sure that all of the retired/generated constraints
2573- // / are dealt with correctly when the life time of the scope ends.
2557+ // / Update statistics when a scope begins.
25742558 // /
25752559 // / \param scope The scope to associate with current solver state.
2576- void registerScope (SolverScope *scope) {
2560+ void beginScope (SolverScope *scope) {
25772561 ++depth;
25782562 maxDepth = std::max (maxDepth, depth);
25792563 scope->scopeNumber = NumStatesExplored++;
25802564
25812565 CS.incrementScopeCounter ();
2582- auto scopeInfo =
2583- std::make_tuple (scope, retiredConstraints.begin (),
2584- generatedConstraints.size ());
2585- scopes.push_back (scopeInfo);
25862566 }
25872567
2588- // / Restore all of the retired/generated constraints to the state
2589- // / before given scope. This is required because retired constraints have
2590- // / to be re-introduced to the system in order of arrival (LIFO) and list
2591- // / of the generated constraints has to be truncated back to the
2592- // / original size.
2568+ // / Update statistics when a scope ends.
25932569 // /
25942570 // / \param scope The solver scope to rollback.
2595- void rollback (SolverScope *scope) {
2571+ void endScope (SolverScope *scope) {
25962572 --depth;
25972573
25982574 unsigned countScopesExplored = NumStatesExplored - scope->scopeNumber ;
25992575 if (countScopesExplored == 1 )
26002576 CS.incrementLeafScopes ();
2601-
2602- SolverScope *savedScope;
2603- // The position of last retired constraint before given scope.
2604- ConstraintList::iterator lastRetiredPos;
2605- // The original number of generated constraints before given scope.
2606- unsigned numGenerated;
2607-
2608- std::tie (savedScope, lastRetiredPos, numGenerated) =
2609- scopes.pop_back_val ();
2610-
2611- assert (savedScope == scope && " Scope rollback not in LIFO order!" );
2612-
2613- // Restore all of the retired constraints.
2614- CS.InactiveConstraints .splice (CS.InactiveConstraints .end (),
2615- retiredConstraints,
2616- retiredConstraints.begin (), lastRetiredPos);
2617-
2618- // And remove all of the generated constraints.
2619- auto genStart = generatedConstraints.begin () + numGenerated,
2620- genEnd = generatedConstraints.end ();
2621- for (auto genI = genStart; genI != genEnd; ++genI) {
2622- CS.InactiveConstraints .erase (ConstraintList::iterator (*genI));
2623- }
2624-
2625- generatedConstraints.erase (genStart, genEnd);
26262577 }
26272578
26282579 // / Check whether constraint system is allowed to form solutions
@@ -2649,29 +2600,12 @@ class ConstraintSystem {
26492600 }
26502601
26512602 private:
2652- // / The list of constraints that have been retired along the
2653- // / current path, this list is used in LIFO fashion when
2654- // / constraints are added back to the circulation.
2655- ConstraintList retiredConstraints;
2603+ // / Depth of the solution stack.
2604+ unsigned depth = 0 ;
26562605
26572606 // / The set of constraints which were active at the time of this state
26582607 // / creating, it's used to re-activate them on destruction.
26592608 SmallVector<Constraint *, 4 > activeConstraints;
2660-
2661- // / The current set of generated constraints.
2662- SmallVector<Constraint *, 4 > generatedConstraints;
2663-
2664- // / The collection which holds association between solver scope
2665- // / and position of the last retired constraint and number of
2666- // / constraints generated before registration of given scope,
2667- // / this helps to rollback all of the constraints retired/generated
2668- // / each of the registered scopes correct (LIFO) order.
2669- llvm::SmallVector<
2670- std::tuple<SolverScope *, ConstraintList::iterator, unsigned >, 4 > scopes;
2671-
2672-
2673- // / Depth of the solution stack.
2674- unsigned depth = 0 ;
26752609 };
26762610
26772611 class CacheExprTypes : public ASTWalker {
@@ -2850,14 +2784,6 @@ class ConstraintSystem {
28502784 // / The length of \c Trail.
28512785 unsigned numTrailChanges;
28522786
2853- // / The length of \c Fixes.
2854- // /
2855- // / FIXME: Remove this.
2856- unsigned numFixes;
2857-
2858- // / The previous score.
2859- Score PreviousScore;
2860-
28612787 // / The scope number of this scope. Set when the scope is registered.
28622788 unsigned scopeNumber = 0 ;
28632789
@@ -2903,7 +2829,8 @@ class ConstraintSystem {
29032829 // / This operation is used to take a solution computed based on some
29042830 // / subset of the constraints and then apply it back to the
29052831 // / constraint system for further exploration.
2906- void applySolution (const Solution &solution);
2832+ void replaySolution (const Solution &solution,
2833+ bool shouldIncreaseScore=true );
29072834
29082835 // FIXME: Perhaps these belong on ConstraintSystem itself.
29092836 friend std::optional<BraceStmt *>
@@ -5531,10 +5458,13 @@ class ConstraintSystem {
55315458
55325459public:
55335460 // / Increase the score of the given kind for the current (partial) solution
5534- // / along the.
5461+ // / along the current solver path .
55355462 void increaseScore (ScoreKind kind, ConstraintLocatorBuilder Locator,
55365463 unsigned value = 1 );
55375464
5465+ // / Primitive form of the above. Records a change in the trail.
5466+ void increaseScore (ScoreKind kind, unsigned value);
5467+
55385468 // / Determine whether this solution is guaranteed to be worse than the best
55395469 // / solution found so far.
55405470 bool worseThanBestSolution () const ;
0 commit comments