Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interprocedural slicing failed #140

Closed
Justme0 opened this issue Feb 19, 2017 · 4 comments
Closed

Interprocedural slicing failed #140

Justme0 opened this issue Feb 19, 2017 · 4 comments

Comments

@Justme0
Copy link
Contributor

Justme0 commented Feb 19, 2017

I use llvm-slicer to slice the following example. The criterion is assert function, and statements about sum should be sliced but failed.

// a.c
#include <assert.h>
#include <stdio.h>

int add(int x, int y) {
    return x + y;
}

int main() {
    int sum = 0;
    int i = 1;
    while (i < 11) {
        sum = add(sum, i);
        i = add(i, 1);
    }
    assert(11 == i);

    return 0;
}

Firstly, compile it to LLVM IR using clang -g -c -emit-llvm a.c;
Secondly, run slicer using ./llvm-slicer -c=__assert_fail a.bc;
Thirdly, get readable IR using llvm-dis a.sliced.
The slice about two funtions is

; Function Attrs: nounwind uwtable
define i32 @add(i32 %x, i32 %y) #0 !dbg !6 {
entry:
  %x.addr = alloca i32, align 4
  %y.addr = alloca i32, align 4
  store i32 %x, i32* %x.addr, align 4
  store i32 %y, i32* %y.addr, align 4
  %0 = load i32, i32* %x.addr, align 4, !dbg !10
  %1 = load i32, i32* %y.addr, align 4, !dbg !11
  %add = add nsw i32 %0, %1, !dbg !12
  ret i32 %add, !dbg !13
}

; Function Attrs: nounwind uwtable
define i32 @main() #0 !dbg !14 {
entry:
  %sum = alloca i32, align 4
  %i = alloca i32, align 4
  store i32 0, i32* %sum, align 4, !dbg !17
  store i32 1, i32* %i, align 4, !dbg !18
  br label %while.cond

while.cond:                                       ; preds = %while.body, %entry
  %0 = load i32, i32* %i, align 4, !dbg !19
  %cmp = icmp slt i32 %0, 11, !dbg !21
  br i1 %cmp, label %while.body, label %while.end, !dbg !22

while.body:                                       ; preds = %while.cond
  %1 = load i32, i32* %sum, align 4, !dbg !23
  %2 = load i32, i32* %i, align 4, !dbg !25
  %call = call i32 @add(i32 %1, i32 %2), !dbg !26
  store i32 %call, i32* %sum, align 4, !dbg !27
  %3 = load i32, i32* %i, align 4, !dbg !28
  %call1 = call i32 @add(i32 %3, i32 1), !dbg !29
  store i32 %call1, i32* %i, align 4, !dbg !30
  br label %while.cond

while.end:                                        ; preds = %while.cond
  %4 = load i32, i32* %i, align 4, !dbg !31
  %cmp2 = icmp eq i32 11, %4, !dbg !31
  br i1 %cmp2, label %safe_return, label %cond.false, !dbg !31

cond.false:                                       ; preds = %while.end
  call void @__assert_fail(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i32 0, i32 0), i32 15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @__PRETTY_FUNCTION__.main, i32 0, i32 0)) #2, !dbg !32
  unreachable, !dbg !32

safe_return:                                      ; preds = %while.end
  ret i32 0
}

The statements about variable sum should be deleted. It seems that dg use SDG(System Dependence Graph) to do interprocedural slicing.

@mchalupa
Copy link
Owner

dg does not use exactly SDG -- it does not use the summary edges. That is also the reason why the sum is not deleted, because without summary edges we can not perform the two-pass algorithm. We simply find dependent statements in one backward traverse through the graph. In other words, dg is atm completely context-insensitive (if you manually try to duplicate the add function, then it will work as you expect). However, it is one of planned future extensions.

@mchalupa
Copy link
Owner

You can find some more info here: #107

@Justme0
Copy link
Contributor Author

Justme0 commented Feb 21, 2017

OK. Thanks a lot!

@mchalupa
Copy link
Owner

As this question is superseded by #107, I'm closing it in the favor of the later one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants