[PATCH] Added llvm.is.constant intrinsic

Sobczak, Janusz janusz.sobczak at mobica.com
Mon Jun 30 01:45:24 PDT 2014


On 28 June 2014 09:37, Nick Lewycky <nlewycky at google.com> wrote:

> On 27 June 2014 08:28, Sobczak, Janusz <janusz.sobczak at mobica.com> wrote:
>
>> On 27 June 2014 09:18, Nick Lewycky <nicholas at mxc.ca> wrote:
>>
>>> Rafael Avila de Espindola wrote:
>>>
>>>>
>>>> Ccing nick since it seems related to the enable_if attribute.
>>>>
>>>
>>> Surely not? attribute enable_if is defined entirely in language terms.
>>>
>>> I don't see it in LangRef but I thought we already had @llvm.is.constant
>>> to implement __builtin_constant_p in the middle-end? If I'm remembering
>>> right then what we should do is try to match gcc's behaviour.
>>>
>>
>> Currently, __builtin_constant_p() is lowered to true/false in clang.
>> Adding this intrinsic (and using it in clang) will allow the compiler to
>> optimise the following code:
>> int inline __attribute__((always_inline)) fun(int x) {
>>     return __builtin_constant_p(x);
>> }
>>
>> int main() {
>>     return fun(0);
>> }
>>
>> This example will return 1 if compiled with gcc but it will return 0 if
>> compiled with clang.
>>
>
> I wish I'd written down how builtin_constant_p is supposed to behave from
> last time I tangoed with it. I think the rule is that it decides whether a
> pointer is constant early, and whether an integer is constant after later
> optimization. I may have those two reversed. +cc Richard Smith who may
> recall.
>
> I'm attaching the test I wrote for it. Note that gcc gives different
> answers at different optimization levels, and again if you move the globals
> into being function-local variables.
>
>
I'm thinking about modyfing __builtin_constant_p() implementation in clang
to do this (for integer arguments):
if (argument is known to be constant)
    replace  __builtin_constant_p() with 1 (True)
else
   replace __builtin_constant_p() with @llvm.is_constant() IR

-- 
Janusz Sobczak
Mobica Ltd
www.mobica.com <http://www.mobica.com>


*Mobica is a provider of software engineering, testing and consultancy
services based in the UK, Poland and the USA, with a worldwide customer
base. We have a proven track record in delivering innovative solutions to
some of the world’s best-known companies in a range of sectors including
Automotive, Mobile, Semiconductor, Finance, TV, Marine and Aviation.*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140630/ade702f1/attachment.html>


More information about the llvm-commits mailing list