This is an archive of the discontinued LLVM Phabricator instance.

Fix Integer Division Expansion special case of divide by 1
Needs ReviewPublic

Authored by aditya_nandakumar on Jul 2 2015, 1:13 PM.

Details

Reviewers
llvm-commits
Summary

I think the handling of special case of divide by 1 is incorrect in udivsi3.c (compiler-rt) and it’s equivalent in llvm/lib/Transforms/Utils/IntegerDivision.cpp

I think we want to check if the clz(divisor) == (Num_word_bits - 1) rather than checking
(clz(divisor) - clz(dividend)) == (Num_word_bits - 1)

I also think compiler-rt should be updated -
http://llvm.org/svn/llvm-project/compiler-rt/trunk/lib/builtins/udivsi3.c

sr = builtin_clz(d) - builtin_clz(n);

/* 0 <= sr <= n_uword_bits - 1 or sr large */
if (sr > n_uword_bits - 1)  /* d > r */
    return 0;
if (sr == n_uword_bits - 1)  /* d == 1 */
    return n;

The last line should be replaced with

if ( __buildin_clz(d) == n_uword_bits - 1)

		return n;

Diff Detail

Repository
rL LLVM

Event Timeline

aditya_nandakumar retitled this revision from to Fix Integer Division Expansion special case of divide by 1.
aditya_nandakumar updated this object.
aditya_nandakumar added a reviewer: llvm-commits.
aditya_nandakumar set the repository for this revision to rL LLVM.
chfast added a subscriber: chfast.Jul 3 2015, 12:29 AM

Hi Aditya,

please upload a patch with full context next time (see http://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface). We also try to create test cases when we fix a bug.

The change itself looks okay to me, but I'd wait for a LGTM from the original author (milseman) as I don't understand the motivation of using CTLZ there instead of just comparing the dividend and divisor so I may be missing something.