Skip to content

adi-2254/cfg_optimizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Static Code Analyzer & CFG Optimizer

A compiler middle-end optimization framework that parses C programs into Abstract Syntax Trees (AST), constructs Control Flow Graphs (CFG), performs classical compiler optimizations, and visualizes the optimized execution flow through an interactive web dashboard.


πŸ“Œ Overview

This project implements a simplified compiler middle-end in Python, inspired by modern compiler optimization pipelines.

Starting from raw C source code, it:

  • Parses the code into an Abstract Syntax Tree (AST) using pycparser
  • Constructs a mathematical Control Flow Graph (CFG)
  • Performs multiple static analysis passes
  • Applies classical compiler optimizations
  • Visualizes the optimized CFG using Graphviz
  • Benchmarks optimization performance on large datasets such as CodeNet and SV-COMP

The project also includes an interactive Streamlit dashboard for visual exploration of CFG transformations.


πŸ“Έ Demo

CFG Optimizer Dashboard


✨ Features

πŸ”Ή AST Generation

  • Parses ANSI C programs using pycparser
  • Converts source code into an Abstract Syntax Tree

πŸ”Ή Control Flow Graph (CFG) Construction

  • Builds CFG nodes and directed edges
  • Models execution paths using graph theory

πŸ”Ή Compiler Optimizations

βœ… Constant Folding

Evaluates constant expressions during compile time.

Example:

int x = 5 * 4;

↓

int x = 20;

βœ… Constant Propagation

Propagates known constant values through subsequent statements.


βœ… Live Variable Analysis

Computes:

  • live_in
  • live_out

sets for every CFG node.


βœ… Dead Code Elimination

Removes assignments whose values never contribute to future computation.

Example:

int x = 10;
x = 20;
return x;

↓

x = 20;
return x;

βœ… Unreachable Code Removal

Uses CFG traversal to identify and eliminate execution blocks that cannot be reached from the function entry point.


πŸ“Š Interactive Dashboard

The Streamlit application allows users to:

  • Paste custom C programs
  • Generate AST
  • Visualize the CFG
  • Observe optimization passes
  • Inspect optimized execution flow

πŸ“ Project Structure

CFG_OPTIMIZER/
β”‚
β”œβ”€β”€ datasets/                  # CodeNet & SV-COMP benchmark datasets
β”‚
β”œβ”€β”€ app.py                     # Streamlit dashboard
β”œβ”€β”€ batch_runner.py            # Batch benchmarking framework
β”œβ”€β”€ main.py                    # CFG builder & optimization engine
β”‚
β”œβ”€β”€ codenet_report.json        # Generated benchmark report
β”œβ”€β”€ sv_comp_report.json        # Generated benchmark report
β”‚
β”œβ”€β”€ cfg_demo.png               # Dashboard preview
└── README.md

πŸ› οΈ Tech Stack

Programming Language

  • Python

Parsing

  • pycparser

Graph Modeling

  • NetworkX

Graph Visualization

  • Graphviz
  • pydot

Web Interface

  • Streamlit

βš™οΈ Installation

1. Clone the Repository

git clone https://github.com/adi-2254/cfg_optimizer.git
cd cfg_optimizer

2. Create a Virtual Environment

Linux / macOS

python -m venv env
source env/bin/activate

Windows

python -m venv env
.\env\Scripts\activate

3. Install Dependencies

pip install streamlit networkx pycparser pydot

4. Install Graphviz

Graphviz is required for CFG rendering.

Windows

Download from:

https://graphviz.org/download/

During installation, enable "Add Graphviz to PATH".

macOS

brew install graphviz

Ubuntu / Debian

sudo apt-get install graphviz

πŸš€ Usage

Launch Interactive Dashboard

streamlit run app.py

Open your browser and paste any C program to visualize:

  • AST
  • CFG
  • Optimized CFG

Run Batch Benchmarking

Evaluate the optimizer on large benchmark datasets.

python batch_runner.py

The generated reports include:

  • Optimization statistics
  • Number of optimized CFG nodes
  • Removed dead code
  • Reachability metrics

Reports are stored as:

codenet_report.json
sv_comp_report.json

πŸ“ˆ Supported Optimizations

Optimization Implemented
AST Generation βœ…
CFG Construction βœ…
Constant Folding βœ…
Constant Propagation βœ…
Live Variable Analysis βœ…
Dead Code Elimination βœ…
Unreachable Code Removal βœ…
CFG Visualization βœ…
Dataset Benchmarking βœ…

πŸ‘¨β€πŸ’» Authors

  • Anurag Raj (2024CSB1101)
  • Aditya Raj (2024CSB1003)
  • Aakash Jaisinghani (2024CSB1092)

If you found this project useful, consider ⭐ starring the repository.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors