为什么j最后的return值为10,LLVM是怎么知道的

Why is the final return value of j 10, how does LLVM know it

我有这个代码:

int main() {
   int i = 0;
   int &j = i;
   j = 10;
   return i;
}

通过-mem2reg后,得到ir如下:

define dso_local i32 @main() #0 !dbg !7 {
entry:
  call void @llvm.dbg.value(metadata i32 0, metadata !11, metadata !DIExpression()), !dbg !12
  call void @llvm.dbg.value(metadata i32* undef, metadata !13, metadata !DIExpression()), !dbg !12
  call void @llvm.dbg.value(metadata i32 10, metadata !11, metadata !DIExpression()), !dbg !12
  ret i32 10, !dbg !15
}

我感到困惑的是 LLVM 使用什么分析来使 i 和 j 相等。 我得到了这个 pass runtime 的详细信息:

  [2021-12-02 11:53:05.295018000] 0x5626d58d2a20   Executing Pass 'Function Pass Manager' on Module '/usr/local/LLVM/test/e0.ll'...
[2021-12-02 11:53:05.295137300] 0x5626d58bf890     Executing Pass 'Dominator Tree Construction' on Function 'main'...
[2021-12-02 11:53:05.295169500] 0x5626d58bf890     Executing Pass 'Promote Memory to Register' on Function 'main'...
0x5626d58bf330       Required Analyses: Assumption Cache Tracker, Dominator Tree Construction
[2021-12-02 11:53:05.295364900] 0x5626d58bf890     Made Modification 'Promote Memory to Register' on Function 'main'...
0x5626d58bf330       Preserved Analyses: Natural Loop Information, Lazy Branch Probability Analysis, Lazy Block Frequency Analysis, Interval Partition Construction, Post-Dominator Tree Construction, Machine Dominance Frontier Construction, MachineDominator Tree Construction, WebAssembly Exception Information, Spill Code Placement Analysis, Bundle Machine CFG Edges, Machine Natural Loop Construction, Detect single entry single exit regions, Dominance Frontier Construction, View regions of function, Print regions of function to 'dot' file, View regions of function (with no function bodies), MachinePostDominator Tree Construction, Delinearization, Print regions of function to 'dot' file (with no function bodies), Detect single entry single exit regions, Dependence Analysis, Dominator Tree Construction, Dominator Info Printer, Print a call graph, Lazy Machine Block Frequency Analysis, Analysis if a function is memory bound, Strip gc.relocates inserted through RewriteStatepointsForGC, Machine Block Frequency Analysis, Block Frequency Analysis, Basic Alias Analysis (stateless AA impl)
 -*- 'Promote Memory to Register' is the last user of following pass instances. Free these instances
[2021-12-02 11:53:05.295628600] 0x5626d58bf890      Freeing Pass 'Dominator Tree Construction' on Function 'main'...
[2021-12-02 11:53:05.295648400] 0x5626d58bf890      Freeing Pass 'Promote Memory to Register' on Function 'main'...
[2021-12-02 11:53:05.295663100] 0x5626d58bf890     Executing Pass 'Module Verifier' on Function 'main'...
 -*- 'Module Verifier' is the last user of following pass instances. Free these instances
[2021-12-02 11:53:05.295725200] 0x5626d58bf890      Freeing Pass 'Module Verifier' on Function 'main'...
[2021-12-02 11:53:05.295740400] 0x5626d58d2a20   Made Modification 'Function Pass Manager' on Module '/usr/local/LLVM/test/e0.ll'...
 -*- 'Function Pass Manager' is the last user of following pass instances. Free these instances
[2021-12-02 11:53:05.295828100] 0x5626d58d2a20    Freeing Pass 'Assumption Cache Tracker' on Module '/usr/local/LLVM/test/e0.ll'...
[2021-12-02 11:53:05.295850400] 0x5626d58d2a20   Executing Pass 'Print Module IR' on Module '/usr/local/LLVM/test/e0.ll'...
 -*- 'Print Module IR' is the last user of following pass instances. Free these instances
[2021-12-02 11:53:05.299496500] 0x5626d58d2a20    Freeing Pass 'Print Module IR' on Module '/usr/local/LLVM/test/e0.ll'...

谁能帮我看看是哪个分析或者优化让LLVM知道i和j是等价的,万分感谢!

使用 clang 生成未优化的 IR

clang -S -emit-llvm -Xclang -disable-O0-optnone xx.cpp

使用 opt

优化生成的 IR

opt --print-after-all -O3 xx.ll -S -o yy.ll

如果您在优化后查看 IR,您会发现它在 SimplifyCFGPass 中得到了简化。