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

2021-02-23 Thread Bella V via cfe-users
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)
___
cfe-users mailing list
cfe-users@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users


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

2021-02-23 Thread Bella V via cfe-users
Hello LLVM Experts,
Please ignore the last email. I'm trying to get the location of used
variables inside the function pass.

For example:

*Symbol a = [4, 4, 6] linesSymbol b= [5, 5, 7] lines*

*C code:*
void bar() {
int a = 10;   //line 2
int b = 20;   //line 3
a = a + 5;//line 4
b = b + 1;   //line 5
a++;   //line 6
b--;//line 7
}

*LLVM IR:*
  %a = alloca i32, align 4
  %b = alloca i32, align 4
  call void @llvm.dbg.declare(metadata i32* %a, metadata !10, metadata
!DIExpression()), !dbg !12
  store i32 10, i32* %a, align 4, !dbg !12
  call void @llvm.dbg.declare(metadata i32* %b, metadata !13, metadata
!DIExpression()), !dbg !14
  store i32 20, i32* %b, align 4, !dbg !14

  %0 = load i32, i32* *%a*, align 4, !dbg !15
  %add = add nsw i32 %0, 5, !dbg !16
  store i32 %add, i32* *%a*, align 4, !dbg !17
  %1 = load i32, i32* *%b*, align 4, !dbg !18
  %add1 = add nsw i32 %1, 1, !dbg !19
  store i32 %add1, i32* *%b*, align 4, !dbg !20
  %2 = load i32, i32* *%a*, align 4, !dbg !21
  %inc = add nsw i32 %2, 1, !dbg !21
  store i32 %inc, i32* *%a*, align 4, !dbg !21
  %3 = load i32, i32* *%b*, align 4, !dbg !22
  %dec = add nsw i32 %3, -1, !dbg !22
  store i32 %dec, i32* *%b*, align 4, !dbg !22
  ret void, !dbg !23
}
!10 = !DILocalVariable(name: "a", scope: !7, file: !1, line: 2, type: !11)
!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!12 = !DILocation(line: 2, column: 7, scope: !7)
!13 = !DILocalVariable(name: "b", scope: !7, file: !1, line: 3, type: !11)
!14 = !DILocation(line: 3, column: 7, scope: !7)
!15 = !DILocation(line: 4, column: 7, scope: !7)
!16 = !DILocation(line: 4, column: 9, scope: !7)
!17 = !DILocation(line: 4, column: 5, scope: !7)
!18 = !DILocation(line: 5, column: 7, scope: !7)
!19 = !DILocation(line: 5, column: 9, scope: !7)
!20 = !DILocation(line: 5, column: 5, scope: !7)
!21 = !DILocation(line: 6, column: 4, scope: !7)
!22 = !DILocation(line: 7, column: 4, scope: !7)
!23 = !DILocation(line: 8, column: 2, scope: !7)


Thanks and Regards.
___
cfe-users mailing list
cfe-users@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-users