Corresponding register of stack variables after mem2reg pass


mem2reg promotes scalar stack variables to registers. Is it possible to find the name of

int f(int a, int b) {
int x = a - b;

After mem2reg pass eliminating alloca, load and stores for stack variables, the IR:

define i32 @f(i32 %a, i32 %b) #0 {

call void @llvm.dbg.value(metadata i32 %a, i64 0, metadata !27, metadata !28), !dbg !29
call void @llvm.dbg.value(metadata i32 %b, i64 0, metadata !30, metadata !28), !dbg !31
%sub = sub nsw i32 %a, %b, !dbg !32

Here %sub is the corresponding register for variable “x”. My question is after “mem2reg” pass is it possible to know these corresponding registers?


Only really as a best-effort via debug info, I think. In this case,
the sub is followed by an @llvm.dbg.value call which specifies which
source variable it came from:

  call void @llvm.dbg.value(metadata i32 %sub, i64 0, metadata !18,
metadata !13), !dbg !19

on my build, where the metadata !18 refers to "!DILocalVariable(name:
"x", scope: !4, file: !1, line: 2, type: !7)"

Of course, that information is likely to get fuzzier and fuzzier as
more optimisations happen.