- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如果循环是不同类型的,那么我可以很容易地用名称识别它们,但是如果有多个相同类型的循环(比如 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.cond
和 while.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/
我指的是 https://llvm.org/docs/GettingStarted.html从其源代码构建 LLVM。我正在使用 Ubuntu 18.04。 $ cmake -G Ninja -DCM
使用 ModulePass,我的目标是向上遍历 SSA 图:从一个具有 0..2 个操作数的语句(大多数操作码属于该语句)开始,我想找出两件事: 操作数是元数据/常量(简单:只需尝试转换为常量类型)还
注意:我注意到 this question与这个问题有很大关系,所以如果您对我的问题感兴趣,那么您一定要阅读另一个问题及其答案。 我可以想到一些 OOP 语言前端可以做的优化,例如创建临时变量来保存来
我正在尝试使用 clang-3.2 创建自动矢量化代码 从这里的幻灯片 - http://llvm.org/devmtg/2012-04-12/Slides/Hal_Finkel.pdf 我应该能够使
我想问的是,我们可以将现有pass生成的信息转化为新pass吗? 如果是,那么如何? 例如 -loops 给出了自然循环信息,所以我们可以通过调用它来将这些信息用于新的传递。 最佳答案 您可以通过覆盖
在 LLVM IR 中,当我想从一个数组中获取一个值时,似乎有三种方法可以做到这一点:使用 extractvalue、使用 extractelement 和使用 getelementptr 然后加载。
我想逐步介绍一下我生成的LLVM IR代码。就llc和lli而言,该代码在语法上是有效的并且类型有效,但是结果不是我所期望的。 这些块足够大,以至于我无法简单地读取该错误就无法成功完成,并且我很难制作
我想弄清楚如何使用 trampoline intrinsics在 LLVM 中。该文档提到了存储蹦床所需的一些存储量,这取决于平台。我的问题是,我如何确定需要多少? 我找到了 this example
我需要使用抽象解释来使用 LLVM 进行一些分析。这可能吗?或者我需要更轻松地使用分析工具。如果我可以通过 LLVM 做到这一点,哪些类将帮助我从原始源代码中制定语句以获取变量之间的关系(以及每个变量
我正在创建一种静态编译的编程语言,并使用 LLVM 作为其后端。我希望我的语言在发生整数溢出时陷入/崩溃。 我知道类似 llvm.sadd.with.overflow 的事情,但我认为这不是最佳/有效
我正在尝试学习 LLVM tablegen。 http://llvm.org/docs/TableGen/LangRef.html表明 field关键字存在但不解释其含义。有人知道这是什么意思吗? 我
Fibonacci LLVM 示例使用 errs() getIR() ) 我一直在搜索 llvm::Module Class Reference并没有得到任何帮助。 Fibonacci.cpp 的一部
我想在 llvm IR 中找到对 llvm.pow.f64 函数的所有函数调用。请建议我一个方法来做到这一点。 最佳答案 嗯,这是一个基本的 FunctionPass找到所有对函数的调用: class
我正在尝试从 llvm 中间代码中删除未使用的 block 。 首先,我通过从入口 basicblock 开始并从它们的终止符指令添加所有 basicblock 后继者来构建可访问的 basicblo
我想获取 llvm 中每条指令的后继列表。如果我没理解错的话,对于除了branch(br)之外的所有指令,后继指令就是下一条。但是对于分支指令来说有点棘手。 例如,如果我有以下 C 代码: int m
我有文本格式的 LLVM IR 代码。我想做的是能够解析它并修改该代码。是否有可以帮助解析 LLVM IR 代码的 API?我的系统中应该有哪些库?此刻我有clang编译器也安装了 LLVM,因为我可
(1) @str = private constant [13 x i8] c"Hello World\0A\00" (2) define i32 @main(){ (3) %r2 = getelem
如果我有一组基本块和边,我需要为它们创建一个具有新入口和端点的新函数。 我可以直接在 LLVM 中创建它吗,就像 createFunction(F) 一样然后 F.insert(bb, edges)其
我通过教程使用 LLVM http://llvm.org/releases/3.1/docs/tutorial/ 编写玩具编译器 但是关于符号表处理的内容并不多。 有一个命令 llvm-nm 显示符号
我希望编写一个 LLVM 传递两个 i32 类型的参数来选择函数。我的第一次尝试(如下所示)失败了: bool MyFunctionPass::runOnFunction(Function &f) {
我是一名优秀的程序员,十分优秀!