Analysis of Control Flow and Data Flow_Chap 4
Analysis of Control Flow and Data Flow_Chap 4
• These building blocks are the operations of the C program and the relations between
them.
Dashed components, entry, exit and body, are other CFGs of the C
program which have single-entry and single-exit points
The do‐while loop and the while‐do loop are similar iterative structures
Data and Control Edges of a C Program
Construction of the Control Flow Graph
Consider the CFG for the GCD algorithm
• A control path is defined as a sequence of control edges that traverse the CFG
• For example, each non-terminating iteration of the while loop will follow the path 2->3->4->2 or else
2->3->5->2
• Note that the CFG and the DFG will contain the same set of nodes ‐‐ only the edges will
be different
• While it is possible to derive the DFG directly from a C program, it is easier to create the
CFG first and use it to derive the DFG
• The method involves tracing control paths in the CFG while simultaneously identifying
corresponding read and write operations of the variables
• Our analysis focuses on C programs that do NOT have arrays or pointers
Data and Control Edges of a C Program
Construction of the Data Flow Graph
• The procedure to recover the data edges related to assignment statements is as follows:
1. In the CFG, select a node where a variable is used as an operand in an expression.
• Mark that node as a read‐node.
2. Find the CFG nodes that assign that variable.
• Mark those nodes as write‐nodes.
3. If there exists a direct control path from a write‐node into a read‐node that does not pass
through another write‐node, then you have identified a data edge.
• The data edge originates at the write‐node and ends at the read‐node.
4. Repeat the previous steps for all variable‐read nodes.
• This procedure identifies all data edges related to assignment statements, but not those
originating from conditional expressions in control flow statements.
Data and Control Edges of a C Program
Construction of the Data Flow Graph
• Let’s derive the DFG of the GCD program
• We first pick a node where a variable is read
Consider statement 5:
• There are two variable-reads in this statement, one for a and one for b Consider b first
• Find all nodes that reference b by tracing backwards through predecessors of node 5 in the CFG -- this
produces the ordered sequence 3, 2, 1, 4, and 5
• Both nodes 1 and 5 write b and there is a direct path from 1 to 5 (e.g. 1, 2, 3,5), and from 5 to 5 (e.g. 5, 2, 3, 5)
• Therefore, we need to add data edges for b from 1 to 5 and from 5 to 5
Data and Control Edges of a C Program
Construction of the Data Flow Graph
Incoming data edges for
• A similar process can be carried out for variable‐read of a in node 5 in the GCD program
node 5
• Nodes 1 and 4 write into a and there is a direct control path
from 1 to 5 and from 4 to 5
• Hence, data edges are added for a from 1 to 5 and from 4 to
5
• To complete the set of data edges into node 5, we also need
to identify all conditional expressions that affect the
outcome of node 5
• From the CFG, node 5 depends on the condition evaluated
in node 3 (a > b) AND the condition evaluated in node 2 (a
!= b)
Data and Control Edges of a C Program
Construction of the Data Flow Graph
The final DFG for all nodes and all variable-reads for GCD is shown below
Note: this DFG leaves out the data edges originating from conditional expressions
Translating C to Hardware
• As mentioned, control and data flow analysis can be helpful in translating C into
hardware
• Translating data structures and pointers from C to hardware can get tricky
• For the purpose of this course, we restrict our analysis as follows
• Only scalar C code is used (no pointers, arrays or other data structures)
• We assume each C statement executes in a single clock cycle
• We first create the CFG and DFG for the C program
• The control edges translate to signals that control datapath operations
• The data edges define the interconnection of the datapath components
Translating C to Hardware
Data Path Design:
With the CFG and DFG available, the following rules will define the implementation of the hardware
datapath.
1. Find the C expression embedded in each node of the CFG, and create an equivalent
combinational circuit to implement the expression.
• For example, if a node in the CFG corresponds to the C statement a = b ‐ a;, then the C expression embedded in
that statement is b ‐ a.
• The combinational circuit required to implement this expression is a subtractor.
• Conditional expressions generate datapath elements, too.
• The outputs of these expressions become the flags used by the hardware controller of this datapath.
2. Each variable in the C program is translated into a register with a multiplexer in front of it.
• The multiplexer is needed when multiple sources may update the register.
• By default, the register will update itself.
• The selection signals of the multiplexer will be driven by the controller.
3. The datapath circuit and the register variables are connected based on the nodes and data
edges in the DFG.
• Each assignment operation connects a combinational circuit with a register.
• Each data edge connects a register with the input of a combinational circuit.
• Finally, we also connect the system‐inputs and system outputs to inputs of datapath circuits and register outputs,
respectively.
Translating C to Hardware
Data Path Design:
• The datapath and the registers are connected consistent with the DFG
• Assignments connect combinational circuit outputs to register inputs, while the
• data edges connect register outputs to combinational circuit inputs.
• System I/O is connected to datapath inputs and register outputs resp.
• Let’s convert the GCD program to a hardware implementation
• The variables a and b are assigned to registers
• The conditional expressions for the if and while stmts require an equality‐ and greater‐than
comparator circuit
• The subtractions b ‐ a and a ‐ b are implemented using subtractors
• The connectivity of the components is defined by the data edges of the DFG
• The resulting datapath has two data inputs (in_a and in_b), and one data output (out_a)
• The circuit needs two control variables, upd_a and upd_b (outputs of controller)
• and it produces two flags, flag_while and flag_if (inputs to controller)
Translating C to Hardware
Translating C to Hardware
• The directed edges in the DFG correspond to the connections in the schematic
• Schematic representations of a circuit are low‐level representations with lots of detail
(similar to assembly code in programming languages)
• We will learn how to create HLS and behavioral VHDL descriptions that synthesize to
schematics similar to the one shown here
Translating C to Hardware: Control Path Design
• Controller Design: The design of the controller can
be derived directly from the CFG and translated
into a finite state machines (FSM)
• How do we create the controller for this datapath
such that it implements the GCD algorithm?
• This control information is present in the C
program and is captured in the CFG.
• In fact, we can translate the CFG almost straight
into hardware by considering it to be a finite state
machine (FSM) specification.
• Each of the transitions in this FSM takes 1 clock
cycle to complete.
• The activities of the FSM are expressed as
condition/command tuples.
Translating C to Hardware: Control Path Design
• For example, _ /run1 means that during this clock cycle, the value of the condition flags is a don’t‐
care, while the command for the datapath is the symbol run1.
• Similarly, flag_while/_ means that this transition is conditional on flag while being true, and that the
command for the datapath is a hold operation.
• A hold operation is one which does not change the state of the datapath, including registers.
• The command set for this FSM includes ( _, run1, run4,run5).
• Each of these symbols represents the execution of a particular node of the CFG.
• The datapath control signals can be created by additional decoding of these command signals.
Translating C to Hardware: Control Path Design
• Here, a2 and b2 are mapped into registers while the other variables are replaced with wires
• This type of manipulation allows multiple C statements to be executed per clock