英文:
What is the difference between my LLVM IR and Clang's
问题
目前我正在开发一种需要使用LLVM的编程语言。我正在使用clang作为参考,因为它也使用了LLVM。
// 我正在使用的C代码示例
int add(int num1, int num2){
int result = num1 + num2;
return result;
}
; 编译C示例代码的Clang输出
define i32 @add(i32 noundef %0, i32 noundef %1) #0 {
%3 = alloca i32, align 4
%4 = alloca i32, align 4
%5 = alloca i32, align 4
store i32 %0, i32* %3, align 4
store i32 %1, i32* %4, align 4
%6 = load i32, i32* %3, align 4
%7 = load i32, i32* %4, align 4
%8 = add nsw i32 %6, %7
store i32 %8, i32* %5, align 4
%9 = load i32, i32* %5, align 4
ret i32 %9
}
; 我自己编译的C示例代码版本
define i32 @add(i32 %0, i32 %1) #0 {
%2 = add nsw i32 %0, %1
ret i32 %2
}
这是我生成的LLVM IR版本。
这是我第一次使用LLVM,我不确定这两个代码片段在速度和安全性方面有什么区别。
英文:
Currently I am working on a programming language that requires me to use LLVM. I am using clang as a reference because it uses LLVM too.
// The C code example I am using
int add(int num1, int num2){
int result = num1 + num2;
return result;
}
; The Clang output from compiling the C example code
define i32 @add(i32 noundef %0, i32 noundef %1) #0 {
%3 = alloca i32, align 4
%4 = alloca i32, align 4
%5 = alloca i32, align 4
store i32 %0, i32* %3, align 4
store i32 %1, i32* %4, align 4
%6 = load i32, i32* %3, align 4
%7 = load i32, i32* %4, align 4
%8 = add nsw i32 %6, %7
store i32 %8, i32* %5, align 4
%9 = load i32, i32* %5, align 4
ret i32 %9
}
; My own version of the compiled C example code
define i32 @add(i32 %0, i32 %1) #0 {
%2 = add nsw i32 %0, %1
ret i32 %2
}
This is my version of the llvm ir generation
This is my first time using llvm and I am not sure what are the differences between those two code snippets in terms of speed and safety.
答案1
得分: 0
LLVM IR中没有"address of"操作,但在C中有。
当clang生成IR时,它不会扫描变量以检查是否需要为它们分配栈空间,而是始终在栈上分配空间(使用alloca
指令),并使用加载和存储来访问值。如果需要地址,alloca
指令就是指针。如果地址永远不需要,LLVM会在优化阶段清理它。
英文:
There is no "address of" operation in LLVM IR, but there is in C.
When clang emits IR, it doesn't scan through variables to check whether it needs to allocate space on the stack for them, instead it always allocates space on the stack (the alloca
instructions) and uses loads and stores to access the value. If the address is needed, the alloca instruction is the pointer. If the address is never needed, LLVM will clean it up in the optimization phase.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论