Developer knowledge network · moderated exchange

UnreliableCode 커뮤니티

개발자 연구, 리버스 엔지니어링 및 코딩 커뮤니티

Knowledge index살다
4Categories
919Threads
2.8K게시물
Analysis

Writing a Custom LLVM Intermediate Representation (IR) Pass for Constant Folding & Dead Code Elimination [StackOverflow Architecture Guide]

llvm_compiler_dev
LLVM & Clang Hacker
MEMBER
대표: 139
가입 날짜: Jul 2018
게시물: 21
감사해요: 15
4주 전 · Jul 26, 2026 3:58 PM
#1

How LLVM passes transform Three-Address Code SSA form:

CPP
#include "llvm/IR/PassManager.h"
#include "llvm/Passes/PassPlugin.h"

struct DeadBranchEliminator : llvm::PassInfoMixin<DeadBranchEliminator> {
    llvm::PreservedAnalyses run(llvm::Function& F, llvm::FunctionAnalysisManager& FAM) {
        for (auto& BB : F) {
            for (auto& Inst : BB) {
                if (auto* branch = llvm::dyn_cast<llvm::BranchInst>(&Inst)) {
                    if (branch->isConditional()) {
                        if (auto* c = llvm::dyn_cast<llvm::ConstantInt>(branch->getCondition())) {
                            // Constant condition found: simplify conditional branch to direct jump!
                        }
                    }
                }
            }
        }
        return llvm::PreservedAnalyses::all();
    }
};
modern_cpp_artisan
C++ Template Wizard
MEMBER
대표: 124
가입 날짜: Jun 2019
게시물: 29
감사해요: 72
4주 전 · Jul 26, 2026 5:48 PM
#2

LLVM's New Pass Manager (llvm::PassInfoMixin) makes writing custom optimization and instrumentation passes so clean.

vtable_slayer
Senior Reverser
MEMBER
대표: 215
가입 날짜: Mar 2018
게시물: 86
감사해요: 61
3주 전 · Jul 27, 2026 11:10 AM
#3

Static Single Assignment (SSA) form with phi nodes makes dataflow analysis linear time.