gpt4 book ai didi

c++ - 将适当的 PHI 节点添加到 llvm-ir

转载 作者:太空狗 更新时间:2023-10-29 21:19:13 24 4
gpt4 key购买 nike

我有以下场景:

       A
/ \
B C
\ /
D
/ \
E F
\ /
...

其中A,B,C,D,E,F都是基本 block ,|是一个向下的箭头。

现在,在 B 中我会有一些 def ,比如 %mul = ...,稍后将在相应的左侧 block 中使用E ,比如 ... = %mul ... 。我插入了适当的控制流 - 要么只采用左侧分支,要么只采用右侧分支,问题是我在验证通过时遇到 Instruction does not dominate all uses 错误。

我尝试通过添加 PHINode 指令来解决这个问题,如下所示:

B:                                     ; preds = %A
%shl = shl ...
br label %D

C: ; preds = %A
...
br label %D

D: ; preds = %B , %C
...
br i1 %ctrl, label %E, label %F

E: ; preds = %D
%phi_nlcs = phi i32 [ %shl, %extra_l_diff ], [ 0, %for.cond ]
%cmp = icmp slt i32 %phi_nlcs, %1
br label ...

省略号是为了隐藏不相关的细节,我也重命名了 block ,但核心逻辑应该在那里。如您所见,我已使用适当的 replaceAllUsesWith() 调用将 %shl 替换为 PHINode

但是,现在我收到以下新错误:

PHINode should have one entry for each predecessor of its parent basic block! 
%phi_nlcs = phi i32 [ %shl, %extra_l_diff ], [ 0, %for.cond ]

我该如何解决这个问题?

最佳答案

phi节点需要在D的开头。

D: 
%phi_nlcs = phi [%shl, B] [undef, C] ; if we've come from B use %shl, if from C an undef value.
...

这是因为 phi 节点表示每个主要基本 block (B 和 C)的值需要是什么,因此需要将其放置在基本 block 组合的边缘。

关于c++ - 将适当的 PHI 节点添加到 llvm-ir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28034601/

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