编译器优化揭秘:C语言二叉查找树的性能提升比较

一.应对在线测试题目的C语言代码(红黑树,AVL树)

这里筛选了一个简单的AVl(二叉搜索树)实现的代码例子:

点击查看代码
#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* left, * right;
    int height;
} Node;

// 创建一个新节点
Node* newNode(int data) {
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->left = node->right = NULL;
    node->height = 1;
    return node;
}
int max(int a, int b) {
    return (a > b) ? a : b;
}

// 获取节点的高度
int height(Node* N) {
    if (N == NULL)
        return 0;
    return N->height;
}

// 获取平衡因子
int getBalance(Node* N) {
    if (N == NULL)
        return 0;
    return height(N->left) - height(N->right);
}

// 右旋
Node* rightRotate(Node* y) {
    Node* x = y->left;
    Node* T2 = x->right;

    x->right = y;
    y->left = T2;

    y->height = max(height(y->left), height(y->right)) + 1;
    x->height = max(height(x->left), height(x->right)) + 1;

    return x;
}

// 左旋
Node* leftRotate(Node* x) {
    Node* y = x->right;
    Node* T2 = y->left;

    y->left = x;
    x->right = T2;

    x->height = max(height(x->left), height(x->right)) + 1;
    y->height = max(height(y->left), height(y->right)) + 1;

    return y;
}

// 插入节点
Node* insert(Node* node, int data) {
    if (node == NULL)
        return newNode(data);

    if (data < node->data)
        node->left = insert(node->left, data);
    else if (data > node->data)
        node->right = insert(node->right, data);
    else
        return node;

    node->height = 1 + max(height(node->left), height(node->right));

    int balance = getBalance(node);

    if (balance > 1 && data < node->left->data)
        return rightRotate(node);

    if (balance < -1 && data > node->right->data)
        return leftRotate(node);

    if (balance > 1 && data > node->left->data) {
        node->left = leftRotate(node->left);
        return rightRotate(node);
    }

    if (balance < -1 && data < node->right->data) {
        node->right = rightRotate(node->right);
        return leftRotate(node);
    }

    return node;
}

// 前序遍历
void preOrder(Node* root) {
    if (root != NULL) {
        printf("%d ", root->data);
        preOrder(root->left);
        preOrder(root->right);
    }
}

// 主函数
int main() {
    Node* root = NULL;
    root = insert(root, 10);
    insert(root, 20);
    insert(root, 30);
    insert(root, 40);
    insert(root, 50);
    insert(root, 25);

    printf("Preorder traversal of the constructed AVL tree is \n");
    preOrder(root);
    return 0;
}

二.gcc编译比较

我们使用gcc指令将这个C代码翻译成使用三种优化的汇编代码文件:


哈哈,汇编代码十分的长,所以我不打算将01,02,03优化后的代码全部放入,这里我就浅放个01,
后面我将通过文件属性直观比较:

点击查看代码
	.file	"avl.c"
	.text
	.globl	newNode
	.type	newNode, @function
newNode:
.LFB39:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	%edi, %ebx
	movl	$32, %edi
	call	malloc@PLT
	movl	%ebx, (%rax)
	movq	$0, 16(%rax)
	movq	$0, 8(%rax)
	movl	$1, 24(%rax)
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE39:
	.size	newNode, .-newNode
	.globl	max
	.type	max, @function
max:
.LFB40:
	.cfi_startproc
	endbr64
	cmpl	%edi, %esi
	movl	%edi, %eax
	cmovge	%esi, %eax
	ret
	.cfi_endproc
.LFE40:
	.size	max, .-max
	.globl	height
	.type	height, @function
height:
.LFB41:
	.cfi_startproc
	endbr64
	testq	%rdi, %rdi
	je	.L6
	movl	24(%rdi), %eax
	ret
.L6:
	movl	$0, %eax
	ret
	.cfi_endproc
.LFE41:
	.size	height, .-height
	.globl	getBalance
	.type	getBalance, @function
getBalance:
.LFB42:
	.cfi_startproc
	endbr64
	testq	%rdi, %rdi
	je	.L11
	movq	8(%rdi), %rax
	testq	%rax, %rax
	je	.L12
	movl	24(%rax), %eax
.L9:
	movq	16(%rdi), %rdx
	testq	%rdx, %rdx
	je	.L13
	movl	24(%rdx), %edx
.L10:
	subl	%edx, %eax
	ret
.L12:
	movl	$0, %eax
	jmp	.L9
.L13:
	movl	$0, %edx
	jmp	.L10
.L11:
	movl	$0, %eax
	ret
	.cfi_endproc
.LFE42:
	.size	getBalance, .-getBalance
	.globl	rightRotate
	.type	rightRotate, @function
rightRotate:
.LFB43:
	.cfi_startproc
	endbr64
	movq	8(%rdi), %rax
	movq	16(%rax), %rdx
	movq	%rdi, 16(%rax)
	movq	%rdx, 8(%rdi)
	movq	16(%rdi), %rcx
	testq	%rcx, %rcx
	je	.L19
	movl	24(%rcx), %ecx
.L15:
	testq	%rdx, %rdx
	je	.L20
	movl	24(%rdx), %edx
.L16:
	cmpl	%ecx, %edx
	cmovl	%ecx, %edx
	addl	$1, %edx
	movl	%edx, 24(%rdi)
	movq	16(%rax), %rdx
	testq	%rdx, %rdx
	je	.L21
	movl	24(%rdx), %ecx
.L17:
	movq	8(%rax), %rdx
	testq	%rdx, %rdx
	je	.L22
	movl	24(%rdx), %edx
.L18:
	cmpl	%ecx, %edx
	cmovl	%ecx, %edx
	addl	$1, %edx
	movl	%edx, 24(%rax)
	ret
.L19:
	movl	$0, %ecx
	jmp	.L15
.L20:
	movl	$0, %edx
	jmp	.L16
.L21:
	movl	$0, %ecx
	jmp	.L17
.L22:
	movl	$0, %edx
	jmp	.L18
	.cfi_endproc
.LFE43:
	.size	rightRotate, .-rightRotate
	.globl	leftRotate
	.type	leftRotate, @function
leftRotate:
.LFB44:
	.cfi_startproc
	endbr64
	movq	16(%rdi), %rax
	movq	8(%rax), %rdx
	movq	%rdi, 8(%rax)
	movq	%rdx, 16(%rdi)
	testq	%rdx, %rdx
	je	.L28
	movl	24(%rdx), %ecx
.L24:
	movq	8(%rdi), %rdx
	testq	%rdx, %rdx
	je	.L29
	movl	24(%rdx), %edx
.L25:
	cmpl	%ecx, %edx
	cmovl	%ecx, %edx
	addl	$1, %edx
	movl	%edx, 24(%rdi)
	movq	16(%rax), %rdx
	testq	%rdx, %rdx
	je	.L30
	movl	24(%rdx), %ecx
.L26:
	movq	8(%rax), %rdx
	testq	%rdx, %rdx
	je	.L31
	movl	24(%rdx), %edx
.L27:
	cmpl	%ecx, %edx
	cmovl	%ecx, %edx
	addl	$1, %edx
	movl	%edx, 24(%rax)
	ret
.L28:
	movl	$0, %ecx
	jmp	.L24
.L29:
	movl	$0, %edx
	jmp	.L25
.L30:
	movl	$0, %ecx
	jmp	.L26
.L31:
	movl	$0, %edx
	jmp	.L27
	.cfi_endproc
.LFE44:
	.size	leftRotate, .-leftRotate
	.globl	insert
	.type	insert, @function
insert:
.LFB45:
	.cfi_startproc
	endbr64
	pushq	%r13
	.cfi_def_cfa_offset 16
	.cfi_offset 13, -16
	pushq	%r12
	.cfi_def_cfa_offset 24
	.cfi_offset 12, -24
	pushq	%rbp
	.cfi_def_cfa_offset 32
	.cfi_offset 6, -32
	pushq	%rbx
	.cfi_def_cfa_offset 40
	.cfi_offset 3, -40
	subq	$8, %rsp
	.cfi_def_cfa_offset 48
	movl	%esi, %ebp
	testq	%rdi, %rdi
	je	.L45
	movq	%rdi, %rbx
	movl	(%rdi), %eax
	cmpl	%esi, %eax
	jg	.L46
	jl	.L47
.L34:
	movq	%rbx, %rax
	addq	$8, %rsp
	.cfi_remember_state
	.cfi_def_cfa_offset 40
	popq	%rbx
	.cfi_def_cfa_offset 32
	popq	%rbp
	.cfi_def_cfa_offset 24
	popq	%r12
	.cfi_def_cfa_offset 16
	popq	%r13
	.cfi_def_cfa_offset 8
	ret
.L45:
	.cfi_restore_state
	movl	%esi, %edi
	call	newNode
	movq	%rax, %rbx
	jmp	.L34
.L46:
	movq	8(%rdi), %rdi
	call	insert
	movq	%rax, 8(%rbx)
.L36:
	movq	16(%rbx), %r13
	testq	%r13, %r13
	je	.L42
	movl	24(%r13), %edx
.L37:
	movq	8(%rbx), %r12
	testq	%r12, %r12
	je	.L43
	movl	24(%r12), %eax
.L38:
	cmpl	%edx, %eax
	cmovl	%edx, %eax
	addl	$1, %eax
	movl	%eax, 24(%rbx)
	movq	%rbx, %rdi
	call	getBalance
	cmpl	$1, %eax
	jle	.L39
	cmpl	%ebp, (%r12)
	jg	.L48
	cmpl	%ebp, (%r12)
	jge	.L34
	movq	%r12, %rdi
	call	leftRotate
	movq	%rax, 8(%rbx)
	movq	%rbx, %rdi
	call	rightRotate
	movq	%rax, %rbx
	jmp	.L34
.L47:
	movq	16(%rdi), %rdi
	call	insert
	movq	%rax, 16(%rbx)
	jmp	.L36
.L42:
	movl	$0, %edx
	jmp	.L37
.L43:
	movl	$0, %eax
	jmp	.L38
.L48:
	movq	%rbx, %rdi
	call	rightRotate
	movq	%rax, %rbx
	jmp	.L34
.L39:
	cmpl	$-1, %eax
	jge	.L34
	movl	0(%r13), %eax
	cmpl	%ebp, %eax
	jl	.L49
	jle	.L34
	movq	%r13, %rdi
	call	rightRotate
	movq	%rax, 16(%rbx)
	movq	%rbx, %rdi
	call	leftRotate
	movq	%rax, %rbx
	jmp	.L34
.L49:
	movq	%rbx, %rdi
	call	leftRotate
	movq	%rax, %rbx
	jmp	.L34
	.cfi_endproc
.LFE45:
	.size	insert, .-insert
	.section	.rodata.str1.1,"aMS",@progbits,1
.LC0:
	.string	"%d "
	.text
	.globl	preOrder
	.type	preOrder, @function
preOrder:
.LFB46:
	.cfi_startproc
	endbr64
	testq	%rdi, %rdi
	je	.L53
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movq	%rdi, %rbx
	movl	(%rdi), %edx
	leaq	.LC0(%rip), %rsi
	movl	$2, %edi
	movl	$0, %eax
	call	__printf_chk@PLT
	movq	8(%rbx), %rdi
	call	preOrder
	movq	16(%rbx), %rdi
	call	preOrder
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
.L53:
	.cfi_restore 3
	ret
	.cfi_endproc
.LFE46:
	.size	preOrder, .-preOrder
	.section	.rodata.str1.8,"aMS",@progbits,1
	.align 8
.LC1:
	.string	"Preorder traversal of the constructed AVL tree is "
	.text
	.globl	main
	.type	main, @function
main:
.LFB47:
	.cfi_startproc
	endbr64
	pushq	%rbx
	.cfi_def_cfa_offset 16
	.cfi_offset 3, -16
	movl	$10, %esi
	movl	$0, %edi
	call	insert
	movq	%rax, %rbx
	movl	$20, %esi
	movq	%rax, %rdi
	call	insert
	movl	$30, %esi
	movq	%rbx, %rdi
	call	insert
	movl	$40, %esi
	movq	%rbx, %rdi
	call	insert
	movl	$50, %esi
	movq	%rbx, %rdi
	call	insert
	movl	$25, %esi
	movq	%rbx, %rdi
	call	insert
	leaq	.LC1(%rip), %rdi
	call	puts@PLT
	movq	%rbx, %rdi
	call	preOrder
	movl	$0, %eax
	popq	%rbx
	.cfi_def_cfa_offset 8
	ret
	.cfi_endproc
.LFE47:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5
0:
	.string	"GNU"
1:
	.align 8
	.long	0xc0000002
	.long	3f - 2f
2:
	.long	0x3
3:
	.align 8
4:

我在比较三个文件的属性后,直观地发现代码量是依次递增的:

通过拷打大模型我了解到这三种优化的效果应该是03>02>01,那么由此就可以得到一个比较明显的结论:

  1. 优化可能会引来更多的代码
  2. 随着代码量的增加,那么编译时间必然也会增加
    那么不明显的结论呢?就针对二叉搜索树的遍历来说,在每个结点插入后都要进行一系列的函数调用,我发现对比01优化,
    02优化似乎将一些分支展开,预测分支,03因为水平有限,我参考了大模型,其告诉我是优化了循环,并采用了更激进的
    分支预测,最终这边建议要优化还是使用02.

三.Clang-LLVM比较


原本报了一个大错,那就是输错了指令差点搞成一个可执行文件啦,我们需要的是生成 LLVM 的中间表示(IR)文件。
一样的,放入01作为参考:

点击查看代码
; ModuleID = 'avl.c'
source_filename = "avl.c"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"

%struct.AVLTreeNode = type { ptr, ptr, %struct.KeyValue, i32 }
%struct.KeyValue = type { i32, i32 }
%struct.AVLTree = type { ptr, i32 }

@.str = private unnamed_addr constant [8 x i8] c"%d: %d\0A\00", align 1
@str = private unnamed_addr constant [10 x i8] c"AVL Tree:\00", align 1
@str.3 = private unnamed_addr constant [19 x i8] c"After deleting 30:\00", align 1

; Function Attrs: mustprogress nofree nounwind willreturn memory(write, argmem: none, inaccessiblemem: readwrite) uwtable
define dso_local noalias noundef ptr @createNode(i32 noundef %0, i32 noundef %1) local_unnamed_addr #0 {
  %3 = tail call noalias dereferenceable_or_null(32) ptr @malloc(i64 noundef 32) #15
  %4 = icmp eq ptr %3, null
  br i1 %4, label %9, label %5

5:                                                ; preds = %2
  %6 = getelementptr inbounds %struct.AVLTreeNode, ptr %3, i64 0, i32 2
  store i32 %0, ptr %6, align 8, !tbaa !5
  %7 = getelementptr inbounds %struct.AVLTreeNode, ptr %3, i64 0, i32 2, i32 1
  store i32 %1, ptr %7, align 4, !tbaa !12
  %8 = getelementptr inbounds %struct.AVLTreeNode, ptr %3, i64 0, i32 3
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %3, i8 0, i64 16, i1 false)
  store i32 1, ptr %8, align 8, !tbaa !13
  br label %9

9:                                                ; preds = %2, %5
  %10 = phi ptr [ %3, %5 ], [ null, %2 ]
  ret ptr %10
}

; Function Attrs: mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite)
declare noalias noundef ptr @malloc(i64 noundef) local_unnamed_addr #1

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable
define dso_local i32 @getHeight(ptr noundef readonly %0) local_unnamed_addr #2 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %6, label %3

3:                                                ; preds = %1
  %4 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  %5 = load i32, ptr %4, align 8, !tbaa !13
  br label %6

6:                                                ; preds = %1, %3
  %7 = phi i32 [ %5, %3 ], [ 0, %1 ]
  ret i32 %7
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable
define dso_local i32 @getBalanceFactor(ptr noundef readonly %0) local_unnamed_addr #3 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %20, label %3

3:                                                ; preds = %1
  %4 = load ptr, ptr %0, align 8, !tbaa !14
  %5 = icmp eq ptr %4, null
  br i1 %5, label %9, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds %struct.AVLTreeNode, ptr %4, i64 0, i32 3
  %8 = load i32, ptr %7, align 8, !tbaa !13
  br label %9

9:                                                ; preds = %3, %6
  %10 = phi i32 [ %8, %6 ], [ 0, %3 ]
  %11 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !15
  %13 = icmp eq ptr %12, null
  br i1 %13, label %17, label %14

14:                                               ; preds = %9
  %15 = getelementptr inbounds %struct.AVLTreeNode, ptr %12, i64 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !13
  br label %17

17:                                               ; preds = %9, %14
  %18 = phi i32 [ %16, %14 ], [ 0, %9 ]
  %19 = sub nsw i32 %10, %18
  br label %20

20:                                               ; preds = %1, %17
  %21 = phi i32 [ %19, %17 ], [ 0, %1 ]
  ret i32 %21
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable
define dso_local void @updateHeight(ptr noundef %0) local_unnamed_addr #4 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %22, label %3

3:                                                ; preds = %1
  %4 = load ptr, ptr %0, align 8, !tbaa !14
  %5 = icmp eq ptr %4, null
  br i1 %5, label %9, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds %struct.AVLTreeNode, ptr %4, i64 0, i32 3
  %8 = load i32, ptr %7, align 8, !tbaa !13
  br label %9

9:                                                ; preds = %3, %6
  %10 = phi i32 [ %8, %6 ], [ 0, %3 ]
  %11 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !15
  %13 = icmp eq ptr %12, null
  br i1 %13, label %17, label %14

14:                                               ; preds = %9
  %15 = getelementptr inbounds %struct.AVLTreeNode, ptr %12, i64 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !13
  br label %17

17:                                               ; preds = %9, %14
  %18 = phi i32 [ %16, %14 ], [ 0, %9 ]
  %19 = tail call i32 @llvm.smax.i32(i32 %10, i32 %18)
  %20 = add nsw i32 %19, 1
  %21 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  store i32 %20, ptr %21, align 8, !tbaa !13
  br label %22

22:                                               ; preds = %17, %1
  ret void
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local ptr @rightRotate(ptr noundef %0) local_unnamed_addr #5 {
  %2 = load ptr, ptr %0, align 8, !tbaa !14
  %3 = getelementptr inbounds %struct.AVLTreeNode, ptr %2, i64 0, i32 1
  %4 = load ptr, ptr %3, align 8, !tbaa !15
  store ptr %0, ptr %3, align 8, !tbaa !15
  store ptr %4, ptr %0, align 8, !tbaa !14
  %5 = icmp eq ptr %4, null
  br i1 %5, label %9, label %6

6:                                                ; preds = %1
  %7 = getelementptr inbounds %struct.AVLTreeNode, ptr %4, i64 0, i32 3
  %8 = load i32, ptr %7, align 8, !tbaa !13
  br label %9

9:                                                ; preds = %6, %1
  %10 = phi i32 [ %8, %6 ], [ 0, %1 ]
  %11 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !15
  %13 = icmp eq ptr %12, null
  br i1 %13, label %17, label %14

14:                                               ; preds = %9
  %15 = getelementptr inbounds %struct.AVLTreeNode, ptr %12, i64 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !13
  br label %17

17:                                               ; preds = %14, %9
  %18 = phi i32 [ %16, %14 ], [ 0, %9 ]
  %19 = tail call i32 @llvm.smax.i32(i32 %10, i32 %18)
  %20 = add nsw i32 %19, 1
  %21 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  store i32 %20, ptr %21, align 8, !tbaa !13
  %22 = icmp eq ptr %2, null
  br i1 %22, label %36, label %23

23:                                               ; preds = %17
  %24 = load ptr, ptr %2, align 8, !tbaa !14
  %25 = icmp eq ptr %24, null
  br i1 %25, label %29, label %26

26:                                               ; preds = %23
  %27 = getelementptr inbounds %struct.AVLTreeNode, ptr %24, i64 0, i32 3
  %28 = load i32, ptr %27, align 8, !tbaa !13
  br label %29

29:                                               ; preds = %23, %26
  %30 = phi i32 [ %28, %26 ], [ 0, %23 ]
  %31 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  %32 = load i32, ptr %31, align 8, !tbaa !13
  %33 = tail call i32 @llvm.smax.i32(i32 %30, i32 %32)
  %34 = add nsw i32 %33, 1
  %35 = getelementptr inbounds %struct.AVLTreeNode, ptr %2, i64 0, i32 3
  store i32 %34, ptr %35, align 8, !tbaa !13
  br label %36

36:                                               ; preds = %17, %29
  ret ptr %2
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local noundef ptr @leftRotate(ptr noundef %0) local_unnamed_addr #5 {
  %2 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %3 = load ptr, ptr %2, align 8, !tbaa !15
  %4 = load ptr, ptr %3, align 8, !tbaa !14
  store ptr %0, ptr %3, align 8, !tbaa !14
  store ptr %4, ptr %2, align 8, !tbaa !15
  %5 = icmp eq ptr %0, null
  br i1 %5, label %26, label %6

6:                                                ; preds = %1
  %7 = load ptr, ptr %0, align 8, !tbaa !14
  %8 = icmp eq ptr %7, null
  br i1 %8, label %12, label %9

9:                                                ; preds = %6
  %10 = getelementptr inbounds %struct.AVLTreeNode, ptr %7, i64 0, i32 3
  %11 = load i32, ptr %10, align 8, !tbaa !13
  br label %12

12:                                               ; preds = %9, %6
  %13 = phi i32 [ %11, %9 ], [ 0, %6 ]
  %14 = icmp eq ptr %4, null
  br i1 %14, label %18, label %15

15:                                               ; preds = %12
  %16 = getelementptr inbounds %struct.AVLTreeNode, ptr %4, i64 0, i32 3
  %17 = load i32, ptr %16, align 8, !tbaa !13
  br label %18

18:                                               ; preds = %15, %12
  %19 = phi i32 [ %17, %15 ], [ 0, %12 ]
  %20 = tail call i32 @llvm.smax.i32(i32 %13, i32 %19)
  %21 = add nsw i32 %20, 1
  %22 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  store i32 %21, ptr %22, align 8, !tbaa !13
  br i1 %5, label %26, label %23

23:                                               ; preds = %18
  %24 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  %25 = load i32, ptr %24, align 8, !tbaa !13
  br label %26

26:                                               ; preds = %1, %23, %18
  %27 = phi i32 [ %25, %23 ], [ 0, %18 ], [ 0, %1 ]
  %28 = getelementptr inbounds %struct.AVLTreeNode, ptr %3, i64 0, i32 1
  %29 = load ptr, ptr %28, align 8, !tbaa !15
  %30 = icmp eq ptr %29, null
  br i1 %30, label %34, label %31

31:                                               ; preds = %26
  %32 = getelementptr inbounds %struct.AVLTreeNode, ptr %29, i64 0, i32 3
  %33 = load i32, ptr %32, align 8, !tbaa !13
  br label %34

34:                                               ; preds = %31, %26
  %35 = phi i32 [ %33, %31 ], [ 0, %26 ]
  %36 = tail call i32 @llvm.smax.i32(i32 %27, i32 %35)
  %37 = add nsw i32 %36, 1
  %38 = getelementptr inbounds %struct.AVLTreeNode, ptr %3, i64 0, i32 3
  store i32 %37, ptr %38, align 8, !tbaa !13
  ret ptr %3
}

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable
define dso_local ptr @balance(ptr noundef %0) local_unnamed_addr #5 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %262, label %3

3:                                                ; preds = %1
  %4 = load ptr, ptr %0, align 8, !tbaa !14
  %5 = icmp eq ptr %4, null
  br i1 %5, label %9, label %6

6:                                                ; preds = %3
  %7 = getelementptr inbounds %struct.AVLTreeNode, ptr %4, i64 0, i32 3
  %8 = load i32, ptr %7, align 8, !tbaa !13
  br label %9

9:                                                ; preds = %6, %3
  %10 = phi i32 [ %8, %6 ], [ 0, %3 ]
  %11 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %12 = load ptr, ptr %11, align 8, !tbaa !15
  %13 = icmp eq ptr %12, null
  br i1 %13, label %17, label %14

14:                                               ; preds = %9
  %15 = getelementptr inbounds %struct.AVLTreeNode, ptr %12, i64 0, i32 3
  %16 = load i32, ptr %15, align 8, !tbaa !13
  br label %17

17:                                               ; preds = %14, %9
  %18 = phi i32 [ %16, %14 ], [ 0, %9 ]
  %19 = tail call i32 @llvm.smax.i32(i32 %10, i32 %18)
  %20 = add nsw i32 %19, 1
  %21 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  store i32 %20, ptr %21, align 8, !tbaa !13
  %22 = load ptr, ptr %0, align 8, !tbaa !14
  %23 = icmp eq ptr %22, null
  br i1 %23, label %27, label %24

24:                                               ; preds = %17
  %25 = getelementptr inbounds %struct.AVLTreeNode, ptr %22, i64 0, i32 3
  %26 = load i32, ptr %25, align 8, !tbaa !13
  br label %27

27:                                               ; preds = %24, %17
  %28 = phi i32 [ %26, %24 ], [ 0, %17 ]
  %29 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %30 = load ptr, ptr %29, align 8, !tbaa !15
  %31 = icmp eq ptr %30, null
  br i1 %31, label %35, label %32

32:                                               ; preds = %27
  %33 = getelementptr inbounds %struct.AVLTreeNode, ptr %30, i64 0, i32 3
  %34 = load i32, ptr %33, align 8, !tbaa !13
  br label %35

35:                                               ; preds = %32, %27
  %36 = phi i32 [ %34, %32 ], [ 0, %27 ]
  %37 = sub nsw i32 %28, %36
  %38 = icmp sgt i32 %37, 1
  br i1 %38, label %39, label %149

39:                                               ; preds = %35
  %40 = load ptr, ptr %0, align 8, !tbaa !14
  %41 = icmp eq ptr %40, null
  br i1 %41, label %59, label %42

42:                                               ; preds = %39
  %43 = load ptr, ptr %40, align 8, !tbaa !14
  %44 = icmp eq ptr %43, null
  br i1 %44, label %48, label %45

45:                                               ; preds = %42
  %46 = getelementptr inbounds %struct.AVLTreeNode, ptr %43, i64 0, i32 3
  %47 = load i32, ptr %46, align 8, !tbaa !13
  br label %48

48:                                               ; preds = %45, %42
  %49 = phi i32 [ %47, %45 ], [ 0, %42 ]
  %50 = getelementptr inbounds %struct.AVLTreeNode, ptr %40, i64 0, i32 1
  %51 = load ptr, ptr %50, align 8, !tbaa !15
  %52 = icmp eq ptr %51, null
  br i1 %52, label %56, label %53

53:                                               ; preds = %48
  %54 = getelementptr inbounds %struct.AVLTreeNode, ptr %51, i64 0, i32 3
  %55 = load i32, ptr %54, align 8, !tbaa !13
  br label %56

56:                                               ; preds = %53, %48
  %57 = phi i32 [ %55, %53 ], [ 0, %48 ]
  %58 = icmp slt i32 %49, %57
  br i1 %58, label %90, label %59

59:                                               ; preds = %39, %56
  %60 = getelementptr inbounds %struct.AVLTreeNode, ptr %40, i64 0, i32 1
  %61 = load ptr, ptr %60, align 8, !tbaa !15
  store ptr %0, ptr %60, align 8, !tbaa !15
  store ptr %61, ptr %0, align 8, !tbaa !14
  %62 = icmp eq ptr %61, null
  br i1 %62, label %66, label %63

63:                                               ; preds = %59
  %64 = getelementptr inbounds %struct.AVLTreeNode, ptr %61, i64 0, i32 3
  %65 = load i32, ptr %64, align 8, !tbaa !13
  br label %66

66:                                               ; preds = %63, %59
  %67 = phi i32 [ %65, %63 ], [ 0, %59 ]
  %68 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %69 = load ptr, ptr %68, align 8, !tbaa !15
  %70 = icmp eq ptr %69, null
  br i1 %70, label %74, label %71

71:                                               ; preds = %66
  %72 = getelementptr inbounds %struct.AVLTreeNode, ptr %69, i64 0, i32 3
  %73 = load i32, ptr %72, align 8, !tbaa !13
  br label %74

74:                                               ; preds = %71, %66
  %75 = phi i32 [ %73, %71 ], [ 0, %66 ]
  %76 = tail call i32 @llvm.smax.i32(i32 %67, i32 %75)
  %77 = add nsw i32 %76, 1
  %78 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  store i32 %77, ptr %78, align 8, !tbaa !13
  br i1 %41, label %262, label %79

79:                                               ; preds = %74
  %80 = load ptr, ptr %40, align 8, !tbaa !14
  %81 = icmp eq ptr %80, null
  br i1 %81, label %85, label %82

82:                                               ; preds = %79
  %83 = getelementptr inbounds %struct.AVLTreeNode, ptr %80, i64 0, i32 3
  %84 = load i32, ptr %83, align 8, !tbaa !13
  br label %85

85:                                               ; preds = %82, %79
  %86 = phi i32 [ %84, %82 ], [ 0, %79 ]
  %87 = tail call i32 @llvm.smax.i32(i32 %86, i32 %77)
  %88 = add nsw i32 %87, 1
  %89 = getelementptr inbounds %struct.AVLTreeNode, ptr %40, i64 0, i32 3
  store i32 %88, ptr %89, align 8, !tbaa !13
  br label %262

90:                                               ; preds = %56
  %91 = getelementptr inbounds %struct.AVLTreeNode, ptr %40, i64 0, i32 1
  %92 = load ptr, ptr %91, align 8, !tbaa !15
  %93 = load ptr, ptr %92, align 8, !tbaa !14
  store ptr %40, ptr %92, align 8, !tbaa !14
  store ptr %93, ptr %91, align 8, !tbaa !15
  br i1 %41, label %111, label %94

94:                                               ; preds = %90
  %95 = load ptr, ptr %40, align 8, !tbaa !14
  %96 = icmp eq ptr %95, null
  br i1 %96, label %100, label %97

97:                                               ; preds = %94
  %98 = getelementptr inbounds %struct.AVLTreeNode, ptr %95, i64 0, i32 3
  %99 = load i32, ptr %98, align 8, !tbaa !13
  br label %100

100:                                              ; preds = %97, %94
  %101 = phi i32 [ %99, %97 ], [ 0, %94 ]
  %102 = icmp eq ptr %93, null
  br i1 %102, label %106, label %103

103:                                              ; preds = %100
  %104 = getelementptr inbounds %struct.AVLTreeNode, ptr %93, i64 0, i32 3
  %105 = load i32, ptr %104, align 8, !tbaa !13
  br label %106

106:                                              ; preds = %103, %100
  %107 = phi i32 [ %105, %103 ], [ 0, %100 ]
  %108 = tail call i32 @llvm.smax.i32(i32 %101, i32 %107)
  %109 = add nsw i32 %108, 1
  %110 = getelementptr inbounds %struct.AVLTreeNode, ptr %40, i64 0, i32 3
  store i32 %109, ptr %110, align 8, !tbaa !13
  br label %111

111:                                              ; preds = %106, %90
  %112 = phi i32 [ %109, %106 ], [ 0, %90 ]
  %113 = getelementptr inbounds %struct.AVLTreeNode, ptr %92, i64 0, i32 1
  %114 = load ptr, ptr %113, align 8, !tbaa !15
  %115 = icmp eq ptr %114, null
  br i1 %115, label %119, label %116

116:                                              ; preds = %111
  %117 = getelementptr inbounds %struct.AVLTreeNode, ptr %114, i64 0, i32 3
  %118 = load i32, ptr %117, align 8, !tbaa !13
  br label %119

119:                                              ; preds = %111, %116
  %120 = phi i32 [ %118, %116 ], [ 0, %111 ]
  %121 = tail call i32 @llvm.smax.i32(i32 %112, i32 %120)
  %122 = add nsw i32 %121, 1
  %123 = getelementptr inbounds %struct.AVLTreeNode, ptr %92, i64 0, i32 3
  store i32 %122, ptr %123, align 8, !tbaa !13
  store ptr %92, ptr %0, align 8, !tbaa !14
  store ptr %0, ptr %113, align 8, !tbaa !15
  store ptr %114, ptr %0, align 8, !tbaa !14
  br i1 %115, label %127, label %124

124:                                              ; preds = %119
  %125 = getelementptr inbounds %struct.AVLTreeNode, ptr %114, i64 0, i32 3
  %126 = load i32, ptr %125, align 8, !tbaa !13
  br label %127

127:                                              ; preds = %124, %119
  %128 = phi i32 [ %126, %124 ], [ 0, %119 ]
  %129 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %130 = load ptr, ptr %129, align 8, !tbaa !15
  %131 = icmp eq ptr %130, null
  br i1 %131, label %135, label %132

132:                                              ; preds = %127
  %133 = getelementptr inbounds %struct.AVLTreeNode, ptr %130, i64 0, i32 3
  %134 = load i32, ptr %133, align 8, !tbaa !13
  br label %135

135:                                              ; preds = %132, %127
  %136 = phi i32 [ %134, %132 ], [ 0, %127 ]
  %137 = tail call i32 @llvm.smax.i32(i32 %128, i32 %136)
  %138 = add nsw i32 %137, 1
  %139 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  store i32 %138, ptr %139, align 8, !tbaa !13
  %140 = load ptr, ptr %92, align 8, !tbaa !14
  %141 = icmp eq ptr %140, null
  br i1 %141, label %145, label %142

142:                                              ; preds = %135
  %143 = getelementptr inbounds %struct.AVLTreeNode, ptr %140, i64 0, i32 3
  %144 = load i32, ptr %143, align 8, !tbaa !13
  br label %145

145:                                              ; preds = %142, %135
  %146 = phi i32 [ %144, %142 ], [ 0, %135 ]
  %147 = tail call i32 @llvm.smax.i32(i32 %146, i32 %138)
  %148 = add nsw i32 %147, 1
  store i32 %148, ptr %123, align 8, !tbaa !13
  br label %262

149:                                              ; preds = %35
  %150 = icmp slt i32 %37, -1
  br i1 %150, label %151, label %262

151:                                              ; preds = %149
  %152 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %153 = load ptr, ptr %152, align 8, !tbaa !15
  %154 = icmp eq ptr %153, null
  br i1 %154, label %172, label %155

155:                                              ; preds = %151
  %156 = load ptr, ptr %153, align 8, !tbaa !14
  %157 = icmp eq ptr %156, null
  br i1 %157, label %161, label %158

158:                                              ; preds = %155
  %159 = getelementptr inbounds %struct.AVLTreeNode, ptr %156, i64 0, i32 3
  %160 = load i32, ptr %159, align 8, !tbaa !13
  br label %161

161:                                              ; preds = %158, %155
  %162 = phi i32 [ %160, %158 ], [ 0, %155 ]
  %163 = getelementptr inbounds %struct.AVLTreeNode, ptr %153, i64 0, i32 1
  %164 = load ptr, ptr %163, align 8, !tbaa !15
  %165 = icmp eq ptr %164, null
  br i1 %165, label %169, label %166

166:                                              ; preds = %161
  %167 = getelementptr inbounds %struct.AVLTreeNode, ptr %164, i64 0, i32 3
  %168 = load i32, ptr %167, align 8, !tbaa !13
  br label %169

169:                                              ; preds = %166, %161
  %170 = phi i32 [ %168, %166 ], [ 0, %161 ]
  %171 = icmp sgt i32 %162, %170
  br i1 %171, label %201, label %172

172:                                              ; preds = %151, %169
  %173 = load ptr, ptr %153, align 8, !tbaa !14
  store ptr %0, ptr %153, align 8, !tbaa !14
  store ptr %173, ptr %152, align 8, !tbaa !15
  %174 = load ptr, ptr %0, align 8, !tbaa !14
  %175 = icmp eq ptr %174, null
  br i1 %175, label %179, label %176

176:                                              ; preds = %172
  %177 = getelementptr inbounds %struct.AVLTreeNode, ptr %174, i64 0, i32 3
  %178 = load i32, ptr %177, align 8, !tbaa !13
  br label %179

179:                                              ; preds = %176, %172
  %180 = phi i32 [ %178, %176 ], [ 0, %172 ]
  %181 = icmp eq ptr %173, null
  br i1 %181, label %185, label %182

182:                                              ; preds = %179
  %183 = getelementptr inbounds %struct.AVLTreeNode, ptr %173, i64 0, i32 3
  %184 = load i32, ptr %183, align 8, !tbaa !13
  br label %185

185:                                              ; preds = %182, %179
  %186 = phi i32 [ %184, %182 ], [ 0, %179 ]
  %187 = tail call i32 @llvm.smax.i32(i32 %180, i32 %186)
  %188 = add nsw i32 %187, 1
  %189 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  store i32 %188, ptr %189, align 8, !tbaa !13
  %190 = getelementptr inbounds %struct.AVLTreeNode, ptr %153, i64 0, i32 1
  %191 = load ptr, ptr %190, align 8, !tbaa !15
  %192 = icmp eq ptr %191, null
  br i1 %192, label %196, label %193

193:                                              ; preds = %185
  %194 = getelementptr inbounds %struct.AVLTreeNode, ptr %191, i64 0, i32 3
  %195 = load i32, ptr %194, align 8, !tbaa !13
  br label %196

196:                                              ; preds = %185, %193
  %197 = phi i32 [ %195, %193 ], [ 0, %185 ]
  %198 = tail call i32 @llvm.smax.i32(i32 %188, i32 %197)
  %199 = add nsw i32 %198, 1
  %200 = getelementptr inbounds %struct.AVLTreeNode, ptr %153, i64 0, i32 3
  store i32 %199, ptr %200, align 8, !tbaa !13
  br label %262

201:                                              ; preds = %169
  %202 = load ptr, ptr %153, align 8, !tbaa !14
  %203 = getelementptr inbounds %struct.AVLTreeNode, ptr %202, i64 0, i32 1
  %204 = load ptr, ptr %203, align 8, !tbaa !15
  store ptr %153, ptr %203, align 8, !tbaa !15
  store ptr %204, ptr %153, align 8, !tbaa !14
  %205 = icmp eq ptr %204, null
  br i1 %205, label %209, label %206

206:                                              ; preds = %201
  %207 = getelementptr inbounds %struct.AVLTreeNode, ptr %204, i64 0, i32 3
  %208 = load i32, ptr %207, align 8, !tbaa !13
  br label %209

209:                                              ; preds = %206, %201
  %210 = phi i32 [ %208, %206 ], [ 0, %201 ]
  %211 = getelementptr inbounds %struct.AVLTreeNode, ptr %153, i64 0, i32 1
  %212 = load ptr, ptr %211, align 8, !tbaa !15
  %213 = icmp eq ptr %212, null
  br i1 %213, label %217, label %214

214:                                              ; preds = %209
  %215 = getelementptr inbounds %struct.AVLTreeNode, ptr %212, i64 0, i32 3
  %216 = load i32, ptr %215, align 8, !tbaa !13
  br label %217

217:                                              ; preds = %214, %209
  %218 = phi i32 [ %216, %214 ], [ 0, %209 ]
  %219 = tail call i32 @llvm.smax.i32(i32 %210, i32 %218)
  %220 = add nsw i32 %219, 1
  %221 = getelementptr inbounds %struct.AVLTreeNode, ptr %153, i64 0, i32 3
  store i32 %220, ptr %221, align 8, !tbaa !13
  %222 = icmp eq ptr %202, null
  br i1 %222, label %234, label %223

223:                                              ; preds = %217
  %224 = load ptr, ptr %202, align 8, !tbaa !14
  %225 = icmp eq ptr %224, null
  br i1 %225, label %229, label %226

226:                                              ; preds = %223
  %227 = getelementptr inbounds %struct.AVLTreeNode, ptr %224, i64 0, i32 3
  %228 = load i32, ptr %227, align 8, !tbaa !13
  br label %229

229:                                              ; preds = %226, %223
  %230 = phi i32 [ %228, %226 ], [ 0, %223 ]
  %231 = tail call i32 @llvm.smax.i32(i32 %230, i32 %220)
  %232 = add nsw i32 %231, 1
  %233 = getelementptr inbounds %struct.AVLTreeNode, ptr %202, i64 0, i32 3
  store i32 %232, ptr %233, align 8, !tbaa !13
  br label %234

234:                                              ; preds = %217, %229
  store ptr %202, ptr %152, align 8, !tbaa !15
  %235 = load ptr, ptr %202, align 8, !tbaa !14
  store ptr %0, ptr %202, align 8, !tbaa !14
  store ptr %235, ptr %152, align 8, !tbaa !15
  %236 = load ptr, ptr %0, align 8, !tbaa !14
  %237 = icmp eq ptr %236, null
  br i1 %237, label %241, label %238

238:                                              ; preds = %234
  %239 = getelementptr inbounds %struct.AVLTreeNode, ptr %236, i64 0, i32 3
  %240 = load i32, ptr %239, align 8, !tbaa !13
  br label %241

241:                                              ; preds = %238, %234
  %242 = phi i32 [ %240, %238 ], [ 0, %234 ]
  %243 = icmp eq ptr %235, null
  br i1 %243, label %247, label %244

244:                                              ; preds = %241
  %245 = getelementptr inbounds %struct.AVLTreeNode, ptr %235, i64 0, i32 3
  %246 = load i32, ptr %245, align 8, !tbaa !13
  br label %247

247:                                              ; preds = %244, %241
  %248 = phi i32 [ %246, %244 ], [ 0, %241 ]
  %249 = tail call i32 @llvm.smax.i32(i32 %242, i32 %248)
  %250 = add nsw i32 %249, 1
  %251 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 3
  store i32 %250, ptr %251, align 8, !tbaa !13
  %252 = load ptr, ptr %203, align 8, !tbaa !15
  %253 = icmp eq ptr %252, null
  br i1 %253, label %257, label %254

254:                                              ; preds = %247
  %255 = getelementptr inbounds %struct.AVLTreeNode, ptr %252, i64 0, i32 3
  %256 = load i32, ptr %255, align 8, !tbaa !13
  br label %257

257:                                              ; preds = %247, %254
  %258 = phi i32 [ %256, %254 ], [ 0, %247 ]
  %259 = tail call i32 @llvm.smax.i32(i32 %250, i32 %258)
  %260 = add nsw i32 %259, 1
  %261 = getelementptr inbounds %struct.AVLTreeNode, ptr %202, i64 0, i32 3
  store i32 %260, ptr %261, align 8, !tbaa !13
  br label %262

262:                                              ; preds = %85, %74, %145, %196, %257, %149, %1
  %263 = phi ptr [ null, %1 ], [ %92, %145 ], [ %153, %196 ], [ %202, %257 ], [ %0, %149 ], [ %40, %74 ], [ %40, %85 ]
  ret ptr %263
}

; Function Attrs: nofree nounwind uwtable
define dso_local ptr @insertNode(ptr noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #6 {
  %4 = icmp eq ptr %0, null
  br i1 %4, label %5, label %12

5:                                                ; preds = %3
  %6 = tail call noalias dereferenceable_or_null(32) ptr @malloc(i64 noundef 32) #15
  %7 = icmp eq ptr %6, null
  br i1 %7, label %20, label %8

8:                                                ; preds = %5
  %9 = getelementptr inbounds %struct.AVLTreeNode, ptr %6, i64 0, i32 2
  store i32 %1, ptr %9, align 8, !tbaa !5
  %10 = getelementptr inbounds %struct.AVLTreeNode, ptr %6, i64 0, i32 2, i32 1
  store i32 %2, ptr %10, align 4, !tbaa !12
  %11 = getelementptr inbounds %struct.AVLTreeNode, ptr %6, i64 0, i32 3
  tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(16) %6, i8 0, i64 16, i1 false)
  store i32 1, ptr %11, align 8, !tbaa !13
  br label %20

12:                                               ; preds = %3
  %13 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 2
  %14 = load i32, ptr %13, align 8, !tbaa !5
  %15 = icmp sgt i32 %14, %1
  br i1 %15, label %22, label %16

16:                                               ; preds = %12
  %17 = icmp slt i32 %14, %1
  br i1 %17, label %18, label %20

18:                                               ; preds = %16
  %19 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  br label %22

20:                                               ; preds = %16, %5, %8, %22
  %21 = phi ptr [ %26, %22 ], [ %0, %16 ], [ %6, %8 ], [ null, %5 ]
  ret ptr %21

22:                                               ; preds = %12, %18
  %23 = phi ptr [ %19, %18 ], [ %0, %12 ]
  %24 = load ptr, ptr %23, align 8, !tbaa !16
  %25 = tail call ptr @insertNode(ptr noundef %24, i32 noundef %1, i32 noundef %2)
  store ptr %25, ptr %23, align 8, !tbaa !16
  %26 = tail call ptr @balance(ptr noundef nonnull %0)
  br label %20
}

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local noundef ptr @findMin(ptr noundef readonly %0) local_unnamed_addr #7 {
  br label %2

2:                                                ; preds = %2, %1
  %3 = phi ptr [ %0, %1 ], [ %4, %2 ]
  %4 = load ptr, ptr %3, align 8, !tbaa !14
  %5 = icmp eq ptr %4, null
  br i1 %5, label %6, label %2, !llvm.loop !17

6:                                                ; preds = %2
  ret ptr %3
}

; Function Attrs: nounwind uwtable
define dso_local ptr @deleteNode(ptr noundef %0, i32 noundef %1) local_unnamed_addr #8 {
  %3 = icmp eq ptr %0, null
  br i1 %3, label %41, label %4

4:                                                ; preds = %2
  %5 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 2
  %6 = load i32, ptr %5, align 8, !tbaa !5
  %7 = icmp sgt i32 %6, %1
  br i1 %7, label %8, label %11

8:                                                ; preds = %4
  %9 = load ptr, ptr %0, align 8, !tbaa !14
  %10 = tail call ptr @deleteNode(ptr noundef %9, i32 noundef %1)
  store ptr %10, ptr %0, align 8, !tbaa !14
  br label %39

11:                                               ; preds = %4
  %12 = icmp slt i32 %6, %1
  br i1 %12, label %13, label %17

13:                                               ; preds = %11
  %14 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %15 = load ptr, ptr %14, align 8, !tbaa !15
  %16 = tail call ptr @deleteNode(ptr noundef %15, i32 noundef %1)
  store ptr %16, ptr %14, align 8, !tbaa !15
  br label %39

17:                                               ; preds = %11
  %18 = load ptr, ptr %0, align 8, !tbaa !14
  %19 = icmp eq ptr %18, null
  br i1 %19, label %25, label %20

20:                                               ; preds = %17
  %21 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %22 = load ptr, ptr %21, align 8, !tbaa !15
  %23 = icmp eq ptr %22, null
  br i1 %23, label %24, label %30

24:                                               ; preds = %20
  br i1 %19, label %25, label %28

25:                                               ; preds = %17, %24
  %26 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %27 = load ptr, ptr %26, align 8, !tbaa !15
  br label %28

28:                                               ; preds = %24, %25
  %29 = phi ptr [ %27, %25 ], [ %18, %24 ]
  tail call void @free(ptr noundef %0) #16
  br label %41

30:                                               ; preds = %20, %30
  %31 = phi ptr [ %32, %30 ], [ %22, %20 ]
  %32 = load ptr, ptr %31, align 8, !tbaa !14
  %33 = icmp eq ptr %32, null
  br i1 %33, label %34, label %30, !llvm.loop !17

34:                                               ; preds = %30
  %35 = getelementptr inbounds %struct.AVLTreeNode, ptr %31, i64 0, i32 2
  %36 = load i64, ptr %35, align 8
  store i64 %36, ptr %5, align 8
  %37 = load i32, ptr %35, align 8, !tbaa !5
  %38 = tail call ptr @deleteNode(ptr noundef nonnull %22, i32 noundef %37)
  store ptr %38, ptr %21, align 8, !tbaa !15
  br label %39

39:                                               ; preds = %13, %34, %8
  %40 = tail call ptr @balance(ptr noundef nonnull %0)
  br label %41

41:                                               ; preds = %2, %39, %28
  %42 = phi ptr [ %40, %39 ], [ %29, %28 ], [ null, %2 ]
  ret ptr %42
}

; Function Attrs: mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite)
declare void @free(ptr allocptr nocapture noundef) local_unnamed_addr #9

; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) uwtable
define dso_local void @initAVLTree(ptr nocapture noundef writeonly %0) local_unnamed_addr #10 {
  store ptr null, ptr %0, align 8, !tbaa !20
  %2 = getelementptr inbounds %struct.AVLTree, ptr %0, i64 0, i32 1
  store i32 0, ptr %2, align 8, !tbaa !22
  ret void
}

; Function Attrs: nofree nounwind uwtable
define dso_local void @insertAVL(ptr nocapture noundef %0, i32 noundef %1, i32 noundef %2) local_unnamed_addr #6 {
  %4 = load ptr, ptr %0, align 8, !tbaa !20
  %5 = tail call ptr @insertNode(ptr noundef %4, i32 noundef %1, i32 noundef %2)
  store ptr %5, ptr %0, align 8, !tbaa !20
  %6 = getelementptr inbounds %struct.AVLTree, ptr %0, i64 0, i32 1
  %7 = load i32, ptr %6, align 8, !tbaa !22
  %8 = add nsw i32 %7, 1
  store i32 %8, ptr %6, align 8, !tbaa !22
  ret void
}

; Function Attrs: nounwind uwtable
define dso_local void @eraseAVL(ptr nocapture noundef %0, i32 noundef %1) local_unnamed_addr #8 {
  %3 = load ptr, ptr %0, align 8, !tbaa !20
  %4 = tail call ptr @deleteNode(ptr noundef %3, i32 noundef %1)
  store ptr %4, ptr %0, align 8, !tbaa !20
  %5 = getelementptr inbounds %struct.AVLTree, ptr %0, i64 0, i32 1
  %6 = load i32, ptr %5, align 8, !tbaa !22
  %7 = add nsw i32 %6, -1
  store i32 %7, ptr %5, align 8, !tbaa !22
  ret void
}

; Function Attrs: nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable
define dso_local noundef i32 @containsKey(ptr nocapture noundef readonly %0, i32 noundef %1) local_unnamed_addr #7 {
  %3 = load ptr, ptr %0, align 8, !tbaa !16
  %4 = icmp eq ptr %3, null
  br i1 %4, label %18, label %5

5:                                                ; preds = %2, %14
  %6 = phi ptr [ %16, %14 ], [ %3, %2 ]
  %7 = getelementptr inbounds %struct.AVLTreeNode, ptr %6, i64 0, i32 2
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %9 = icmp sgt i32 %8, %1
  br i1 %9, label %14, label %10

10:                                               ; preds = %5
  %11 = icmp slt i32 %8, %1
  br i1 %11, label %12, label %18

12:                                               ; preds = %10
  %13 = getelementptr inbounds %struct.AVLTreeNode, ptr %6, i64 0, i32 1
  br label %14

14:                                               ; preds = %5, %12
  %15 = phi ptr [ %13, %12 ], [ %6, %5 ]
  %16 = load ptr, ptr %15, align 8, !tbaa !16
  %17 = icmp eq ptr %16, null
  br i1 %17, label %18, label %5, !llvm.loop !23

18:                                               ; preds = %10, %14, %2
  %19 = phi i32 [ 0, %2 ], [ 1, %10 ], [ 0, %14 ]
  ret i32 %19
}

; Function Attrs: nofree nounwind uwtable
define dso_local void @inorderTraversal(ptr noundef readonly %0) local_unnamed_addr #6 {
  br label %2

2:                                                ; preds = %5, %1
  %3 = phi ptr [ %0, %1 ], [ %13, %5 ]
  %4 = icmp eq ptr %3, null
  br i1 %4, label %14, label %5

5:                                                ; preds = %2
  %6 = load ptr, ptr %3, align 8, !tbaa !14
  tail call void @inorderTraversal(ptr noundef %6)
  %7 = getelementptr inbounds %struct.AVLTreeNode, ptr %3, i64 0, i32 2
  %8 = load i32, ptr %7, align 8, !tbaa !5
  %9 = getelementptr inbounds %struct.AVLTreeNode, ptr %3, i64 0, i32 2, i32 1
  %10 = load i32, ptr %9, align 4, !tbaa !12
  %11 = tail call i32 (ptr, ...) @printf(ptr noundef nonnull dereferenceable(1) @.str, i32 noundef %8, i32 noundef %10)
  %12 = getelementptr inbounds %struct.AVLTreeNode, ptr %3, i64 0, i32 1
  %13 = load ptr, ptr %12, align 8, !tbaa !15
  br label %2

14:                                               ; preds = %2
  ret void
}

; Function Attrs: nofree nounwind
declare noundef i32 @printf(ptr nocapture noundef readonly, ...) local_unnamed_addr #11

; Function Attrs: nofree nounwind uwtable
define dso_local void @printAVL(ptr nocapture noundef readonly %0) local_unnamed_addr #6 {
  %2 = load ptr, ptr %0, align 8, !tbaa !20
  tail call void @inorderTraversal(ptr noundef %2)
  ret void
}

; Function Attrs: nounwind uwtable
define dso_local void @freeAVL(ptr noundef %0) local_unnamed_addr #8 {
  %2 = icmp eq ptr %0, null
  br i1 %2, label %3, label %4

3:                                                ; preds = %1, %4
  ret void

4:                                                ; preds = %1
  %5 = load ptr, ptr %0, align 8, !tbaa !14
  tail call void @freeAVL(ptr noundef %5)
  %6 = getelementptr inbounds %struct.AVLTreeNode, ptr %0, i64 0, i32 1
  %7 = load ptr, ptr %6, align 8, !tbaa !15
  tail call void @freeAVL(ptr noundef %7)
  tail call void @free(ptr noundef %0) #16
  br label %3
}

; Function Attrs: nounwind uwtable
define dso_local noundef i32 @main() local_unnamed_addr #8 {
  %1 = tail call ptr @insertNode(ptr noundef null, i32 noundef 10, i32 noundef 100)
  %2 = tail call ptr @insertNode(ptr noundef %1, i32 noundef 20, i32 noundef 200)
  %3 = tail call ptr @insertNode(ptr noundef %2, i32 noundef 30, i32 noundef 300)
  %4 = tail call ptr @insertNode(ptr noundef %3, i32 noundef 40, i32 noundef 400)
  %5 = tail call ptr @insertNode(ptr noundef %4, i32 noundef 50, i32 noundef 500)
  %6 = tail call i32 @puts(ptr nonnull dereferenceable(1) @str)
  tail call void @inorderTraversal(ptr noundef %5)
  %7 = tail call ptr @deleteNode(ptr noundef %5, i32 noundef 30)
  %8 = tail call i32 @puts(ptr nonnull dereferenceable(1) @str.3)
  tail call void @inorderTraversal(ptr noundef %7)
  tail call void @freeAVL(ptr noundef %7)
  ret i32 0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare i32 @llvm.smax.i32(i32, i32) #12

; Function Attrs: nofree nounwind
declare noundef i32 @puts(ptr nocapture noundef readonly) local_unnamed_addr #13

; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: write)
declare void @llvm.memset.p0.i64(ptr nocapture writeonly, i8, i64, i1 immarg) #14

attributes #0 = { mustprogress nofree nounwind willreturn memory(write, argmem: none, inaccessiblemem: readwrite) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { mustprogress nofree nounwind willreturn allockind("alloc,uninitialized") allocsize(0) memory(inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #2 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: read) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #3 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #4 = { mustprogress nofree norecurse nosync nounwind willreturn memory(read, argmem: readwrite, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #5 = { mustprogress nofree norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #6 = { nofree nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #7 = { nofree norecurse nosync nounwind memory(read, inaccessiblemem: none) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #8 = { nounwind uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #9 = { mustprogress nounwind willreturn allockind("free") memory(argmem: readwrite, inaccessiblemem: readwrite) "alloc-family"="malloc" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #10 = { mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: write) uwtable "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #11 = { nofree nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #12 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
attributes #13 = { nofree nounwind }
attributes #14 = { nocallback nofree nounwind willreturn memory(argmem: write) }
attributes #15 = { nounwind allocsize(0) }
attributes #16 = { nounwind }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 8, !"PIC Level", i32 2}
!2 = !{i32 7, !"PIE Level", i32 2}
!3 = !{i32 7, !"uwtable", i32 2}
!4 = !{!"Ubuntu clang version 18.1.3 (1ubuntu1)"}
!5 = !{!6, !11, i64 16}
!6 = !{!"AVLTreeNode", !7, i64 0, !7, i64 8, !10, i64 16, !11, i64 24}
!7 = !{!"any pointer", !8, i64 0}
!8 = !{!"omnipotent char", !9, i64 0}
!9 = !{!"Simple C/C++ TBAA"}
!10 = !{!"KeyValue", !11, i64 0, !11, i64 4}
!11 = !{!"int", !8, i64 0}
!12 = !{!6, !11, i64 20}
!13 = !{!6, !11, i64 24}
!14 = !{!6, !7, i64 0}
!15 = !{!6, !7, i64 8}
!16 = !{!7, !7, i64 0}
!17 = distinct !{!17, !18, !19}
!18 = !{!"llvm.loop.mustprogress"}
!19 = !{!"llvm.loop.unroll.disable"}
!20 = !{!21, !7, i64 0}
!21 = !{!"", !7, i64 0, !11, i64 8}
!22 = !{!21, !11, i64 8}
!23 = distinct !{!23, !18, !19}

这个LLVM IR文本我是只可远观而不可亵玩。拿属性来浅看一下吧:

直观地看到01地文本量大于02和03,02和03竟然出奇地一致,这是巧合嘛,查阅资料才发现说01地调试信息会更完善一些,而02和03
在这个层面都是对01进行有关循环展开,函数内敛的优化之类的。

四.大模型附录

老实交代,使用的是kimi。其实原本想测试一下mini_cursor现在怎么样了,但不管了,简单的问题往往只需要简单地大模型来处理:
gcc:
直观差别总结
性能:从 -O1 到 -O3,性能通常会逐渐提升,但提升的幅度会逐渐减小,因为优化的空间越来越小。
代码大小:从 -O1 到 -O3,生成的代码大小可能会逐渐增加,因为优化可能会引入更多的代码来提高性能。
编译时间:从 -O1 到 -O3,编译时间会逐渐增加,因为编译器需要进行更多的分析和优化。
稳定性:从 -O1 到 -O3,程序的稳定性可能会逐渐降低,因为更高级的优化可能会引入难以发现的bug。
clang:
分析生成的 LLVM IR 文件:比较不同优化级别下的 LLVM IR 文件,观察编译器在不同级别下应用的优化策略。例如,-O1 会包括一些基本优化,如常量折叠、死代码消除等;-O2 在 -O1 的基础上增加了更多的优化,包括循环展开、函数内联等;-O3 包含了所有中级优化,并在此基础上增加了更复杂的优化技术,如向量化、超标量优化等。
观察性能差异:通过运行生成的可执行文件或分析 LLVM IR 文件,比较不同优化级别对程序性能的影响。通常情况下,-O2 和 -O3 会显著提高程序的执行速度,而 -O0 则运行较慢但调试信息更完整。

posted @ 2025-03-05 15:11  哎哎呦呦喂喂0211  阅读(45)  评论(0)    收藏  举报