-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Description
| Bugzilla Link | 15205 |
| Version | trunk |
| OS | Linux |
| Reporter | LLVM Bugzilla Contributor |
Extended Description
Using ScalarEvolution in custom pass run on the IR at the end of all standard optimization, in the following case ScalarEvolution do not recognize that the loop has an entry guard related to the initial value of its induction variable.
The C test case:
/////////////////////////////////////
extern void x(int);
int foo_int(int a, int b) {
int i;
for (i = 0; i < b/2; ++i) {
x(i);
}
return 0;
}
/////////////////////////////////////
the correspondent optimized IR is:
///////////////////////////////////////////////////////////////////////////////
define i32 @foo_int(i32 %a, i32 %b) nounwind uwtable {
entry:
%div = sdiv i32 %b, 2
%cmp3 = icmp sgt i32 %b, 1
br i1 %cmp3, label %for.body, label %for.end
for.body: ; preds = %entry, %for.body
%i.04 = phi i32 [ %inc, %for.body ], [ 0, %entry ]
tail call void @x(i32 %i.04) nounwind
%inc = add nsw i32 %i.04, 1
%cmp = icmp slt i32 %inc, %div
br i1 %cmp, label %for.body, label %for.end
for.end: ; preds = %for.body, %entry
ret i32 0
}
///////////////////////////////////////////////////////////////////////////////
From the IR is possible to see that the condition in the entry block is
%cmp3 = icmp sgt i32 %b, 1
but starting from the loop IV it should be expected something like
%cmp3 = icmp slt i32 0, %div
and this is verified looking at the instcombine debug trace:
////////////////////////////////////////////////
IC: Visiting: %cmp3 = icmp slt i32 0, %div
IC: Old = %cmp3 = icmp sgt i32 %div, 0
New = = icmp sge i32 %b, 2
IC: ADD: %cmp3 = icmp sge i32 %b, 2
IC: ERASE %0 = icmp sgt i32 %div, 0
IC: ADD: %div = sdiv i32 %b, 2
IC: Visiting: %div = sdiv i32 %b, 2
IC: Visiting: %cmp3 = icmp sge i32 %b, 2
IC: Old = %cmp3 = icmp sge i32 %b, 2
New = = icmp sgt i32 %b, 1
IC: ADD: %cmp3 = icmp sgt i32 %b, 1
IC: ERASE %0 = icmp sge i32 %b, 2
////////////////////////////////////////////////
After InstCombine pass ScalarEvolution is no more able to detect the entry guard of the loop.