【问题标题】:LLVM IR position of predecessor block labels in phi node instructionphi节点指令中前驱块标签的LLVM IR位置
【发布时间】:2015-10-07 21:52:28
【问题描述】:

当在基本块中使用 phi 节点时,如果前驱是某个块的可能性更高,是否有建议的顺序放置标签。例如下面列出的简单阶乘函数。

define private i64 @fact(i64 %start) {
entry:
  %0 = icmp sle i64 1, %start
  br i1 %0, label %loop, label %endcond

loop:                                              ; preds = %loop, %entry
  %1 = phi i64 [ %res, %loop ], [ 1, %entry ]      ; if %start > 2 predecessor
  %2 = phi i64 [ %3, %loop ], [ %start, %entry ]   ; is likely %loop
  %res = mul i64 %1, %2
  %3 = sub i64 %2, 1
  %cond = icmp sle i64 1, %3
  br i1 %cond, label %loop, label %endcond

endcond:                                           ; preds = %loop, %entry
  %fin = phi i64 [ %res, %loop ], [ 1, %entry ]    ; highly unlikely
  ret i64 %fin                                     ; predecessor is %entry
}

虽然用户可能会输入@fact(1),但这不太可能,所以我希望在大多数情况下,endcond 中 phi 节点的前驱块是post.loop。我的假设是在这种情况下

%fin = phi i64 [ %res, %post.loop ], [ 1, %entry ]

优于

%fin = phi i64 [ 1, %entry ], [ %res, %post.loop ]

正确吗?如果是这样,为什么或为什么不呢?

【问题讨论】:

    标签: llvm llvm-ir ssa


    【解决方案1】:

    没有区别。 LLVM 将对您的代码进行分析以估计分支概率,并使用它来对生成的块进行排序。

    您可以通过使用分支权重元数据来影响这一点:http://llvm.org/docs/BlockFrequencyTerminology.html

    【讨论】:

    • 谢谢!很棒的答案!我在您提供的链接上找到的Branch Weight Metadata 上的以下链接也非常有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2014-12-28
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多