gpt4 book ai didi

c - 如何将函数下的多个相同类型循环映射到LLVM IR中生成的基本 block ?

转载 作者:太空宇宙 更新时间:2023-11-04 02:28:06 25 4
gpt4 key购买 nike

如果循环是不同类型的,那么我可以很容易地用名称识别它们,但是如果有多个相同类型的循环(比如 5 个 while 循环),我如何识别其中的基本 block LLVM IR对应源码中的哪个循环?

当我们按顺序访问代码和 LLVM IR 时,手动识别很容易,但我正在研究如何以编程方式识别它们。

例如,我在 C 中有以下源代码:

int main()
{
int count=1;
while (count <= 4)
{
count++;
}
while (count > 4)
{
count--;
}
return 0;
}

当我执行命令时 clang -S -emit-llvm fileName.c 我得到了 fileName.ll create with the below content:

; ModuleID = 'abc.c'
source_filename = "abc.c"
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.0.23026"

; Function Attrs: noinline nounwind uwtable
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%count = alloca i32, align 4
store i32 0, i32* %retval, align 4
store i32 1, i32* %count, align 4
br label %while.cond

while.cond: ; preds = %while.body, %entry
%0 = load i32, i32* %count, align 4
%cmp = icmp sle i32 %0, 4
br i1 %cmp, label %while.body, label %while.end

while.body: ; preds = %while.cond
%1 = load i32, i32* %count, align 4
%inc = add nsw i32 %1, 1
store i32 %inc, i32* %count, align 4
br label %while.cond

while.end: ; preds = %while.cond
br label %while.cond1

while.cond1: ; preds = %while.body3, %while.end
%2 = load i32, i32* %count, align 4
%cmp2 = icmp sgt i32 %2, 4
br i1 %cmp2, label %while.body3, label %while.end4

while.body3: ; preds = %while.cond1
%3 = load i32, i32* %count, align 4
%dec = add nsw i32 %3, -1
store i32 %dec, i32* %count, align 4
br label %while.cond1

while.end4: ; preds = %while.cond1
ret i32 0
}

attributes #0 = { noinline nounwind uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

!llvm.module.flags = !{!0}
!llvm.ident = !{!1}

!0 = !{i32 1, !"PIC Level", i32 2}
!1 = !{!"clang version 4.0.0 (tags/RELEASE_400/final)"}

现在为给定的源文件创建了两个基本 block ,分别是 while.condwhile.cond1,我如何确定哪个基本 block 用于哪个 while 循环在源代码中?

最佳答案

在我尝试回答之前,我只想注意,根据所选的优化级别或使用 opt 手动选择的传递,信息可能不存在或可能不准确(例如,因为内联、克隆等)。

现在,将低级表示和源代码关联起来的方法是使用调试信息(例如 DWARF 格式)。要生成调试信息,您需要在编译期间使用 -g 命令行标志。

对于 LLVM IR,如果你看一下 Loop API有相关调用,如getStartLoc .所以你可以做这样的事情(例如在 llvm::Function pass 的 runOn 方法中):

llvm::SmallVector<llvm::Loop *> workList;
auto &LI = getAnalysis<llvm::LoopInfoWrapperPass>(CurFunc).getLoopInfo();

std::for_each(LI.begin(), LI.end(), [&workList](llvm::Loop *e) { workList.push_back(e); });

for(auto *e : workList) {
auto line = e->getStartLoc().getLine();
auto *scope = llvm::dyn_cast<llvm::DIScope>(e->getStartLoc().getScope());
auto filename = scope->getFilename();

// do stuff here
}

此外,对于 BasicBlock ,你也可以使用Instruction中的调试相关方法(例如 getDebugLoc)并将其与对其他 Loop 的调用相结合getHeader等方法

另外,请注意有一个 getLoopID 方法,它为每个循环使用一个内部唯一 ID,但它并不总是存在,并且它受制于我在开始时提到的潜在省略。无论如何,如果您需要操作它,请查看 LLVM 源代码中遵循 setLoopID 方法的示例(例如,在 lib/Transforms/Scalar/LoopRotation.cpp 中)。

关于c - 如何将函数下的多个相同类型循环映射到LLVM IR中生成的基本 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47936016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com