-indvars issues?


Is the -indvars pass functional? I’ve done some small test to check it, but this fails to canonicalize:

int *x;
int *y;
int i;

for (i = 1; i < 100; i+=2) {
x[i] = y[i] + 3;

The IR produced after -indvars:

br label %for.cond

for.cond: ; preds = %for.inc, %entry
%indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 1, %entry ]
%0 = trunc i64 %indvars.iv to i32
%cmp = icmp slt i32 %0, 100
br i1 %cmp, label %for.body, label %for.end

for.body: ; preds = %for.cond
%arrayidx = getelementptr inbounds i32* %y, i64 %indvars.iv
%1 = load i32* %arrayidx, align 4
%add = add nsw i32 %1, 3
%arrayidx2 = getelementptr inbounds i32* %x, i64 %indvars.iv
store i32 %add, i32* %arrayidx2, align 4
br label %for.inc

for.inc: ; preds = %for.body
%indvars.iv.next = add i64 %indvars.iv, 2
br label %for.cond

for.end: ; preds = %for.cond

Which isn’t in canonical form. Is there some trick to getting this pass to work? I’ve tried adding various other passes ahead of it, like -aa-eval, -scalar-evolution, -mem2reg, -lcssa, -loop-simplify, etc but to no avail.

Thank you,

-indvars does not canonicalize as much any more, as more passes can handle non canonical loops. To get the old canonicalization add -enable-iv-rewrite on the command line. Though I would not rely on this, as this flag is about to be removed.


It seems there is no -enable-iv-rewrite now in llvm3.2, and it suggest
-enable-load-pre, but it still does not work.

So, how to active the transform?


Loops are canonicalized with -loop-rotate, which implicitly calls -loop-simplify first.

The canonical form of the induction variables is expressed in the ScalarEvolution analysis.

-indvars simplifies inductions variables, mainly by eliminating sign extension, but does not rewrite them into a canonical form.


It works, thank you, Andy.