#!/bin/bash : <<'````````bash' # Programming Exercise 4: Writing LLVM Passes This homework does not depend on the previous homework, but you can of course include your optimization pass in your compiler. ## Transformation Write an LLVM plugin which provides two passes, which can be executed via LLVM's `opt` command line tool: 1. The module pass `cginline`, which repeatedly inlines leaf functions (i.e., functions without non-intrinsic function calls) if they contain less than IR 20 instructions. (Hint: use `llvm::InlineFunction` for the actual inlining.) 2. The function pass `cgopt`, which (until convergence) (a) transforms conditional branches with a constant condition into unconditional branches; (b) eliminates unreachable blocks; and (c) eliminates trivial PHI nodes (i.e., PHIs that can be replaced by one of their operands). Note: you may use LLVM's analyses and utilities, but not rely on existing transformation passes. ## Submission - Submission deadline: 2025-11-26 23:59 - Submit using `curl --data-binary "@" 'https://db.in.tum.de/teaching/ws2526/codegen/submit.py?hw=4&matrno='` ````````bash set -euo pipefail FAILED=0 testcase_ec() { if ./bc1 -l "$3" | lli; s="${PIPESTATUS[@]}"; [ "$s" != "0 $2" ]; \ then echo "FAILED: $1 (got [$s] expected [0 $2])"; FAILED=1; fi; } testcase_chk() { ./bc1 -l "$2" | "${@:3}" || { echo "FAILED: $1"; FAILED=1; }; } testcase_run_chk() { ./bc1 -l "$2" | lli | "${@:3}" || { echo "FAILED: $1"; FAILED=1; }; } clang++ -O2 -Wall -Wextra -Wno-unused-parameter $(llvm-config --cppflags) -shared -fPIC -o cgopt.so cgopt.cc split-file "$0" hw4-tests opt -S --load-pass-plugin=$PWD/cgopt.so -passes=cginline --verify-each