COL729 Lab 3 : Studying Compiler Passes

Due date - 23:55, 4 October 2026

Weightage: 30 Marks (Actual weightage will be decided later)

Part A: A transformation pass (10 marks)

Implement a function-pass plugin for LLVM's new pass manager, named strength-reduce. It must replace multiplication by a positive power-of-two integer constant with an equivalent shl. Handle the constant on either side, preserve the integer type and applicable nuw/nsw flags (you may need to read about the meanings of these flags), avoid changing vector multiplications, and return the correct PreservedAnalyses value. Run it with:

opt -load-pass-plugin ./libStrengthReduce.so \
  -passes=strength-reduce -S input.ll -o output.ll
opt -passes=verify -disable-output output.ll

Provide positive tests for at least two integer widths and both operand orders, and negative tests for zero, one, a non-power-of-two, a negative constant, and a vector value. Compare the original and transformed programs' outputs.

You may use an online search or AI conversation bots to learn about how to write an LLVM pass, but please do not try to get an AI to solve the whole assignment for you.

Part B : Examples of Dataflow Analyses for C++ in Clang Analyzer (5 marks)

Read about Clang's analysis at https://clang.llvm.org/docs/DataFlowAnalysisIntro.html. Notice the difference in terminology: they use \bottom for \top and vice-versa; also they use the term join instead of "meet" (as discussed in class). Other than these cosmetic differences, the dataflow analysis fundamentals are identical to those discussed in class. Answer the following questions:

Do not use AI for this part

Part C : Understand Clang's CFG (10 marks)

Download and extract the files of LLVM 23.1.0. Clang's CFG format is available in llvm-project/clang/include/clang/Analysis/CFG.h. This consists of: Build the clang executable using the following commands: Once you build the clang repo, you can dump a CFG for real code using clang -Xclang -analyze -Xclang -analyzer-checker=debug.DumpCFG -fsyntax-only foo.c, or to render it as a graph (needs Graphviz) using clang -Xclang -analyze -Xclang -analyzer-checker=debug.ViewCFG -fsyntax-only foo.c. You can also add debug statements in the clang code, e.g., using llvm::dbgs() << "Debug statement: ...", build and run again. Answer the following question:

Do not use AI for this part

Part D : Study Clang's Single Variable Constant Propagation Dataflow Analysis (5 marks)

Study the code of llvm-project/clang/unittests/Analysis/FlowSensitive/SingleVarConstantPropagationTest.cpp. You may also look at the code that implements the generic fixed-point algorithm for a dataflow analysis in runTypeErasedDataflowAnalysis() in clang/lib/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.cpp (headers defined in include/clang/Analysis/FlowSensitive/DataflowAnalysis.h, ...). You may need to execute the code on some example programs to be able to answer the following questions.

Do not use AI for this part