[cfe-users] How to get llvm Location of Used Variables

Bella V via cfe-users cfe-users at lists.llvm.org
Tue Feb 23 18:17:18 PST 2021


Hello Experts,


I'm trying to get the location of used variables inside the function pass.

For example the output should be: variable name = [line numbers where
they are used]

Output:

a = [5,7]

b= [6,8]

C code

void bar() {

    int a = 10;   //line 2

    int b = 20;   //line 3

    a = a + 5;    //line 5

    b = b + 1;   //line 6

    a++;           //line 7

    b--;            //line 8

}

LLVM IR

define dso_local void @bar() #0 !dbg !7 {

  %1 = alloca i32, align 4

  %2 = alloca i32, align 4

  call void @llvm.dbg.declare(metadata i32* %1, metadata !11, metadata
!DIExpression()), !dbg !13

  store i32 10, i32* %1, align 4, !dbg !13

  call void @llvm.dbg.declare(metadata i32* %2, metadata !14, metadata
!DIExpression()), !dbg !15

  store i32 20, i32* %2, align 4, !dbg !15

  %3 = load i32, i32* %1, align 4, !dbg !16

  %4 = add nsw i32 %3, 5, !dbg !17

  store i32 %4, i32* %1, align 4, !dbg !18

  %5 = load i32, i32* %2, align 4, !dbg !19

  %6 = add nsw i32 %5, 1, !dbg !20

  store i32 %6, i32* %2, align 4, !dbg !21

  %7 = load i32, i32* %1, align 4, !dbg !22

  %8 = add nsw i32 %7, 1, !dbg !22

  store i32 %8, i32* %1, align 4, !dbg !22

  %9 = load i32, i32* %2, align 4, !dbg !23

  %10 = add nsw i32 %9, -1, !dbg !23

  store i32 %10, i32* %2, align 4, !dbg !23

  ret void, !dbg !24

}


!11 = !DILocalVariable(name: "a", scope: !7, file: !8, line: 2, type: !12)

!12 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)

!13 = !DILocation(line: 2, column: 9, scope: !7)

!14 = !DILocalVariable(name: "b", scope: !7, file: !8, line: 3, type: !12)

!15 = !DILocation(line: 3, column: 9, scope: !7)

!16 = !DILocation(line: 5, column: 9, scope: !7)

!17 = !DILocation(line: 5, column: 11, scope: !7)

!18 = !DILocation(line: 5, column: 7, scope: !7)

!19 = !DILocation(line: 6, column: 9, scope: !7)

!20 = !DILocation(line: 6, column: 11, scope: !7)

!21 = !DILocation(line: 6, column: 7, scope: !7)

!22 = !DILocation(line: 7, column: 6, scope: !7)

!23 = !DILocation(line: 8, column: 6, scope: !7)

!24 = !DILocation(line: 9, column: 1, scope: !7)



Thanks.


















Hello
Team,



I'm trying to get the location of used variables inside the function
pass. For example the output should be: variable name = [line numbers
where they are used]

a = [5,7]

b= [6,8]

C code

void
bar() {

    int a = 10;
 //line 2

    int b = 20;
 //line 3



    a = a + 5;
  //line 5

    b = b + 1;   //line
6

    a++;
   //line 7

    b--;
    //line 8

}

LLVM IR

define
dso_local void @bar() #0 !dbg !7 {

  %1 = alloca i32, align 4

  %2 = alloca i32, align 4

  call void @llvm.dbg.declare(metadata i32* %1,
metadata !11, metadata !DIExpression()), !dbg !13

  store i32 10, i32* %1, align 4, !dbg !13

  call void @llvm.dbg.declare(metadata i32* %2,
metadata !14, metadata !DIExpression()), !dbg !15

  store i32 20, i32* %2, align 4, !dbg !15

  %3 = load i32, i32* %1, align 4, !dbg !16

  %4 = add nsw i32 %3, 5, !dbg !17

  store i32 %4, i32* %1, align 4, !dbg !18

  %5 = load i32, i32* %2, align 4, !dbg !19

  %6 = add nsw i32 %5, 1, !dbg !20

  store i32 %6, i32* %2, align 4, !dbg !21

  %7 = load i32, i32* %1, align 4, !dbg !22

  %8 = add nsw i32 %7, 1, !dbg !22

  store i32 %8, i32* %1, align 4, !dbg !22

  %9 = load i32, i32* %2, align 4, !dbg !23

  %10 = add nsw i32 %9, -1, !dbg !23

  store i32 %10, i32* %2, align 4, !dbg !23

  ret void, !dbg !24

}





!11
= !DILocalVariable(name: "a", scope: !7, file: !8, line: 2, type:
!12)

!12
= !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)

!13
= !DILocation(line: 2, column: 9, scope: !7)

!14
= !DILocalVariable(name: "b", scope: !7, file: !8, line: 3, type:
!12)

!15
= !DILocation(line: 3, column: 9, scope: !7)

!16
= !DILocation(line: 5, column: 9, scope: !7)

!17
= !DILocation(line: 5, column: 11, scope: !7)

!18
= !DILocation(line: 5, column: 7, scope: !7)

!19
= !DILocation(line: 6, column: 9, scope: !7)

!20
= !DILocation(line: 6, column: 11, scope: !7)

!21
= !DILocation(line: 6, column: 7, scope: !7)

!22
= !DILocation(line: 7, column: 6, scope: !7)

!23
= !DILocation(line: 8, column: 6, scope: !7)

!24
= !DILocation(line: 9, column: 1, scope: !7)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20210223/e0b55ccc/attachment-0001.html>


More information about the cfe-users mailing list