36 return transitionEdge;
47 machine = dyn_cast<MachineOp>(op);
48 assert(machine &&
"Expected a fsm::MachineOp");
51 for (
auto stateOp : machine.getOps<
StateOp>()) {
53 auto *currentStateNode = getOrAddState(stateOp);
55 for (
auto transitionOp : stateOp.getTransitions().getOps<
TransitionOp>()) {
56 auto *nextStateNode = getOrAddState(transitionOp.getNextStateOp());
57 currentStateNode->addTransitionEdge(nextStateNode, transitionOp);
70 return lookup(state.getNameAttr());
76 auto *&node =
nodeMap[state.getNameAttr()];
79 nodes.push_back(node);
86 OpBuilder::InsertionGuard g(builder);
87 builder.setInsertionPointToEnd(&
getMachine().getBody().front());
96 OpBuilder::InsertionGuard g(builder);
98 builder.setInsertionPointToEnd(&from.getTransitions().getBlocks().front());
100 return currentStateNode->addTransitionEdge(nextStateNode, transition);
106 for (
auto *incomingTransition : llvm::make_early_inc_range(stateNode->uses()))
107 incomingTransition->erase();
109 for (
auto *outgoingTransitions : llvm::make_early_inc_range(*stateNode))
110 outgoingTransitions->erase();
111 nodeMap.erase(state.getNameAttr());
112 nodes.erase(stateNode);
117 auto nameStrAttr = StringAttr::get(state.getContext(), name);
119 state.setName(nameStrAttr);
122 auto updateTransitions = [&](
auto &&transitionRange) {
123 for (
auto *transition : transitionRange) {
124 auto transitionOp = transition->getTransition();
125 transitionOp->setAttr(transitionOp.getNextStateAttrName(), nameStrAttr);
129 updateTransitions(stateNode->uses());
130 updateTransitions(*stateNode);
133 nodeMap.erase(state.getNameAttr());
134 nodeMap[nameStrAttr] = stateNode;
assert(baseType &&"element must be base type")
FSMStateNode * createState(OpBuilder &builder, Location loc, StringRef name)
Creates a new StateOp operation in this machine and updates the graph.
FSMTransitionEdge * createTransition(OpBuilder &builder, Location loc, StateOp from, StateOp to)
Creates a new transition operation between two states and updates the graph.
NodeList nodes
The storage for graph nodes, with deterministic iteration.
FSMStateNode * lookup(StateOp op)
Look up a FSMStateNode for a state.
MachineOp getMachine() const
Return the FSM machine operation which this graph tracks.
void eraseState(StateOp state)
Removes this state from the graph.
llvm::DenseMap< Attribute, FSMStateNode * > nodeMap
This maps each StateOp to its graph node.
void renameState(StateOp state, StringRef name)
Renames the 'state' to the provided 'name', and updates all referencing transitions.
FSMGraph(Operation *operation)
Create a new graph of an FSM machine operation.
FSMStateNode * getOrAddState(StateOp state)
Retrieves the state node for a 'state'.
This is a Node in the FSMGraph. Each node represents a state in the FSM.
friend class FSMTransitionEdge
FSMTransitionList transitions
List of outgoing transitions from this state.
FSMTransitionEdge * firstUse
List of transitions which reference this state.
void recordUse(FSMTransitionEdge *transition)
Record that a tramsition referenced this state.
void eraseTransitionEdge(FSMTransitionEdge *edge)
Erases a transition edge from this state.
FSMTransitionEdge * addTransitionEdge(FSMStateNode *nextState, TransitionOp transition)
Adds a new transition edge from this state to 'nextState'.
This is an edge in the FSMGraph.
FSMStateNode * currentState
The state where this transition originates from.
TransitionOp getTransition() const
void erase()
Erase this transition, removing it from the source state and the target state's use-list.
FSMTransitionEdge * prevUse
FSMStateNode * nextState
The next state of this transition.
FSMTransitionEdge * nextUse
Intrusive linked list for other uses.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.