I have the following non trivial loop that does 10 iterations and stops (computes n
over k
)
#include <stdio.h>#include <stdlib.h>int main(int argc,char **argv){ int d=1; bool f = true; unsigned int j = 1; unsigned int n = 3; unsigned int k = 2; unsigned int m0 = 0; unsigned int I[2]; I[0]=m0; while (j != -1) { if (j == k) { f=false; j--; } else if (f) { f=true ; I[j]=I[j-1]+1;j++; } else if (I[j] >= n-k+j) { f=false; j--; } else { f=true ; I[j]=I[j ]+1;j++; d++; } } printf("num subsets = %d\n",d); return 0;}
I'm trying to force llvm/clang
to unroll the loop, but fail to do so:
$ clang++ -c -O3 -emit-llvm main.cpp -o main.bc$ llvm-dis main.bc -o main.ll$ sed -n '23,34p;35q' main.llwhile.cond: ; preds = %while.cond.backedge, %while.cond.outer %j.0 = phi i32 [ %j.0.ph, %while.cond.outer ], [ %j.0.be, %while.cond.backedge ] %f.0 = phi i1 [ true, %while.cond.outer ], [ %f.0.be, %while.cond.backedge ] switch i32 %j.0, label %if.else [ i32 -1, label %while.end i32 2, label %while.cond.backedge ]while.cond.backedge: ; preds = %while.cond, %if.then2, %if.then12 %j.0.be = phi i32 [ %inc, %if.then2 ], [ %dec13, %if.then12 ], [ 1, %while.cond ] %f.0.be = phi i1 [ true, %if.then2 ], [ false, %if.then12 ], [ false, %while.cond ] br label %while.cond
So it looks like the loop is still there. Actually, even I use n=2
, and the loop does only 4 iterations, unrolling fails. Any way to force llvm/clang
to unroll this? gcc
?