gdb a.out (gdb) b main Breakpoint 1 at 0x1140 (gdb) r Starting program: /tmp/a.out [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 1, 0x0000555555555140 in unreachable() () (gdb) disassemble $pc Dump of assembler code for function _Z11unreachablev: => 0x0000555555555140 <+0>: push %rax 0x0000555555555141 <+1>: lea 0xebc(%rip),%rdi # 0x555555556004 0x0000555555555148 <+8>: call 0x555555555030 <puts@plt> 0x000055555555514d <+13>: pop %rax 0x000055555555514e <+14>: ret End of assembler dump. (gdb)
readelf -s a.out Symbol table '.symtab' contains 37 entries: Num: Value Size Type Bind Vis Ndx Name 22: 0000000000001140 0 FUNC GLOBAL DEFAULT 15 main 23: 0000000000001140 15 FUNC GLOBAL DEFAULT 15 _Z11unreachablev
objdump -t a.out 0000000000001140 g F .text 0000000000000000 main 0000000000001140 g F .text 000000000000000f _Z11unreachablev
.globl main # -- Begin function main .p2align 4, 0x90 .type main,@function main: # @main .cfi_startproc # %bb.0: .Lfunc_end0: .size main, .Lfunc_end0-main .cfi_endproc # -- End function
// This file implements the Dead Loop Deletion Pass. This pass is responsible // for eliminating loops with non-infinite computable trip counts that have no // side effects or volatile instructions, and do not contribute to the // computation of the function's return value.
#include<iostream> boolfermat() { constint max_value = 1000; // Endless loop with no side effects is UB for (int a = 1, b = 1, c = 1; true; ) { if (((a * a * a) == ((b * b * b) + (c * c * c)))) returntrue; // disproved :) a++; if (a > max_value) { a = 1; b++; } if (b > max_value) { b = 1; c++; } if (c > max_value) c = 1; } returnfalse; // not disproved } intmain() { std::cout << "Fermat's Last Theorem "; fermat() ? std::cout << "has been disproved!\n" : std::cout << "has not been disproved.\n"; }
使用 opt 将 LoopDeletionPass 作用于 C 的 IR 和 CPP 的 IR。(因为需要编译llvm,因为要看LLVM_DEBUG的日志,需要开启 DLLVM_ENABLE_ASSERTIONS=TRUE)
二者执行起来日志分别为:
opt作用于C语言
1 2 3 4
opt -debug-only=loop-delete -passes=loop-deletion test.c.ll -S -o /dev/null Analyzing Loop for deletion: Loop at depth 1 containing: %2<header><latch> Could not compute SCEV MaxBackedgeTakenCount and was not required to make progress. Loop is not invariant, cannot delete.
opt作用于C++语言
1 2 3
opt -debug-only=loop-delete -passes=loop-deletion test.cpp.ll -S -o /dev/null Analyzing Loop for deletion: Parallel Loop at depth 1 containing: %2<header><latch> Loop is invariant, delete it!