From c37ae28d939b603d176688e2bc256bde1bb1790c Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Mon, 21 Apr 2014 22:35:11 +0000 Subject: Store State objects by value in TableGen's DFAPacketizerEmitter Removes some extra manual dynamic memory allocation/management. It does get a bit quirky having to make State's members mutable and pointers/references to const rather than non-const, but that's a necessary workaround to dealing with the std::set elements. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206807 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/TableGen/DFAPacketizerEmitter.cpp | 64 ++++++++++++++++----------------- 1 file changed, 30 insertions(+), 34 deletions(-) (limited to 'utils') diff --git a/utils/TableGen/DFAPacketizerEmitter.cpp b/utils/TableGen/DFAPacketizerEmitter.cpp index b9e34af90e7..e5a6bb6f0dc 100644 --- a/utils/TableGen/DFAPacketizerEmitter.cpp +++ b/utils/TableGen/DFAPacketizerEmitter.cpp @@ -82,11 +82,13 @@ namespace { class State { public: static int currentStateNum; - int stateNum; - bool isInitial; - std::set stateInfo; - typedef std::map TransitionMap; - TransitionMap Transitions; + // stateNum is the only member used for equality/ordering, all other members + // can be mutated even in const State objects. + const int stateNum; + mutable bool isInitial; + mutable std::set stateInfo; + typedef std::map TransitionMap; + mutable TransitionMap Transitions; State(); State(const State &S); @@ -108,16 +110,16 @@ class State { // AddInsnClass - Return all combinations of resource reservation // which are possible from this state (PossibleStates). // - void AddInsnClass(unsigned InsnClass, std::set &PossibleStates); + void AddInsnClass(unsigned InsnClass, std::set &PossibleStates) const; // // addTransition - Add a transition from this state given the input InsnClass // - void addTransition(unsigned InsnClass, State *To); + void addTransition(unsigned InsnClass, const State *To) const; // // hasTransition - Returns true if there is a transition from this state // given the input InsnClass // - bool hasTransition(unsigned InsnClass); + bool hasTransition(unsigned InsnClass) const; }; } // End anonymous namespace. @@ -128,10 +130,9 @@ namespace { class DFA { public: DFA(); - ~DFA(); // Set of states. Need to keep this sorted to emit the transition table. - typedef std::set > StateSet; + typedef std::set StateSet; StateSet states; State *currentState; @@ -139,7 +140,7 @@ public: // // Modify the DFA. // - void addState(State *); + const State &newState(); // // writeTable: Print out a table representing the DFA. @@ -162,14 +163,10 @@ State::State(const State &S) : DFA::DFA(): currentState(nullptr) {} -DFA::~DFA() { - DeleteContainerPointers(states); -} - // // addTransition - Add a transition from this state given the input InsnClass // -void State::addTransition(unsigned InsnClass, State *To) { +void State::addTransition(unsigned InsnClass, const State *To) const { assert(!Transitions.count(InsnClass) && "Cannot have multiple transitions for the same input"); Transitions[InsnClass] = To; @@ -179,7 +176,7 @@ void State::addTransition(unsigned InsnClass, State *To) { // hasTransition - Returns true if there is a transition from this state // given the input InsnClass // -bool State::hasTransition(unsigned InsnClass) { +bool State::hasTransition(unsigned InsnClass) const { return Transitions.count(InsnClass) > 0; } @@ -188,7 +185,7 @@ bool State::hasTransition(unsigned InsnClass) { // which are possible from this state (PossibleStates). // void State::AddInsnClass(unsigned InsnClass, - std::set &PossibleStates) { + std::set &PossibleStates) const { // // Iterate over all resource states in currentState. // @@ -247,9 +244,10 @@ bool State::canAddInsnClass(unsigned InsnClass) const { } -void DFA::addState(State *S) { - assert(!states.count(S) && "State already exists"); - states.insert(S); +const State &DFA::newState() { + auto IterPair = states.emplace(); + assert(IterPair.second && "State already exists"); + return *IterPair.first; } @@ -285,16 +283,16 @@ void DFA::writeTableAndAPI(raw_ostream &OS, const std::string &TargetName) { // to construct the StateEntry table. int ValidTransitions = 0; for (unsigned i = 0; i < states.size(); ++i, ++SI) { - assert (((*SI)->stateNum == (int) i) && "Mismatch in state numbers"); + assert ((SI->stateNum == (int) i) && "Mismatch in state numbers"); StateEntry[i] = ValidTransitions; for (State::TransitionMap::iterator - II = (*SI)->Transitions.begin(), IE = (*SI)->Transitions.end(); + II = SI->Transitions.begin(), IE = SI->Transitions.end(); II != IE; ++II) { OS << "{" << II->first << ", " << II->second->stateNum << "}, "; } - ValidTransitions += (*SI)->Transitions.size(); + ValidTransitions += SI->Transitions.size(); // If there are no valid transitions from this stage, we need a sentinel // transition. @@ -440,12 +438,11 @@ void DFAPacketizerEmitter::run(raw_ostream &OS) { // Run a worklist algorithm to generate the DFA. // DFA D; - State *Initial = new State; + const State *Initial = &D.newState(); Initial->isInitial = true; Initial->stateInfo.insert(0x0); - D.addState(Initial); - SmallVector WorkList; - std::map, State*> Visited; + SmallVector WorkList; + std::map, const State*> Visited; WorkList.push_back(Initial); @@ -467,7 +464,7 @@ void DFAPacketizerEmitter::run(raw_ostream &OS) { // Add S' to Visited // while (!WorkList.empty()) { - State *current = WorkList.pop_back_val(); + const State *current = WorkList.pop_back_val(); for (DenseSet::iterator CI = allInsnClasses.begin(), CE = allInsnClasses.end(); CI != CE; ++CI) { unsigned InsnClass = *CI; @@ -479,7 +476,7 @@ void DFAPacketizerEmitter::run(raw_ostream &OS) { // if (!current->hasTransition(InsnClass) && current->canAddInsnClass(InsnClass)) { - State *NewState = nullptr; + const State *NewState; current->AddInsnClass(InsnClass, NewStateResources); assert(NewStateResources.size() && "New states must be generated"); @@ -487,13 +484,12 @@ void DFAPacketizerEmitter::run(raw_ostream &OS) { // If we have seen this state before, then do not create a new state. // // - std::map, State*>::iterator VI; - if ((VI = Visited.find(NewStateResources)) != Visited.end()) + auto VI = Visited.find(NewStateResources); + if (VI != Visited.end()) NewState = VI->second; else { - NewState = new State; + NewState = &D.newState(); NewState->stateInfo = NewStateResources; - D.addState(NewState); Visited[NewStateResources] = NewState; WorkList.push_back(NewState); } -- cgit v1.2.3