[llvm-dev] Loop optimised into div (?)

Joan Lluch via llvm-dev llvm-dev at lists.llvm.org
Wed Oct 23 12:08:32 PDT 2019


Hi all,

For the following code 

int countHundred( int num )
{
  int count = 0;
  while ( num >= 100) { count++ ; num = num - 100; }
  return count;
}

the loop shown above is optimised away and replaced by ‘udiv’ instruction like this


define i16 @countHundred(i16 %num) {
entry:
  %cmp3 = icmp sgt i16 %num, 99
  br i1 %cmp3, label %while.body.preheader, label %while.end

while.body.preheader:                             ; preds = %entry
  %0 = add nsw i16 %num, -100
  %1 = udiv i16 %0, 100
  %2 = add nuw nsw i16 %1, 1
  br label %while.end

while.end:                                        ; preds = %while.body.preheader, %entry
  %count.0.lcssa = phi i16 [ 0, %entry ], [ %2, %while.body.preheader ]
  ret i16 %count.0.lcssa
}

The above was compiled for the MSP430 target with the -Os flag but similar results are obtained with the other options except -O0

Any pointers about where does that transform happen would be appreciated (I posted a similar question in cfe-dev).

Thanks in advance

John.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20191023/e09ac22c/attachment.html>


More information about the llvm-dev mailing list