Developer knowledge network · moderated exchange

UnreliableCode コミュニティ

開発者リサーチ、リバース エンジニアリング、コーディング コミュニティ

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.