Project Stage 2 (Part 3): Implementing the Clone-Pruning Logic
Building the Heart of the Pass
Once I had GCC built and the demo pass working, it was time to dive into the real challenge: implementing the clone-pruning logic in tree-ctyler.cc
.
How I Tackled It
1. Understanding Gimple IR
GCC represents programs using intermediate representations, and my pass needed to analyze the Gimple IR. At first, Gimple seemed overwhelming, but once I found examples of basic block and statement iteration, it started making sense.
2. Comparing Functions
The goal was to check if two cloned functions were equivalent. I wrote a function to compare Gimple statements:
That I added the following logic to tree-ctyler.cc:
Then, I added a loop to iterate through the basic blocks and statements of two functions:
And also the pass was registered in passes.def;
NEXT_PASS(pass_ctyler);
3. Diagnostic Messages
Finally, the pass emitted messages based on whether the functions were equivalent:
Challenges
- Statement Matching: Even small differences (e.g., variable names) can cause false negatives, so I kept the comparison logic simple for now.
- Debugging: Adding print statements to the Gimple IR helped me understand what was happening under the hood.
Reflections
This stage was the most fun for me. Gimple IR is like looking into the soul of a program, and writing logic to analyze it felt empowering. The PRUNE
and NOPRUNE
messages were satisfying proof that the pass was working.
Nhận xét
Đăng nhận xét