Handling of builtin_constant_p

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Handling of builtin_constant_p

Eric Fiselier via cfe-dev
Hi,

I'm trying to understand where __builtin_constant_p is folded in the
front-end. It appears to be handled differently when placed in an if
condition or in a conditional expression.

In the example below, I see that it is folded in
CodeGenFunction::EmitBuiltinExpr() in CGBuiltin.cpp
int x = __builtin_constant_p(100);

But for cases like the ones below, it appears to be folded much
earlier and it doesn't reach EmitBuiltinExpr().

int x = __builtin_constant_p(100)? 10: 20;

if (__builtin_constant_p(100)) { ... }

Any pointers on what functions to look up for the above case would be
very helpful.

Thanks,
Bharathi
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Handling of builtin_constant_p

Eric Fiselier via cfe-dev
`grep -r "builtin_constant_p"` under tool/clang shows some hints. For example, check VisitConditionalOperator if you want.

HTH,
chenwj

2017-10-27 11:22 GMT+08:00 Bharathi Seshadri via cfe-dev <[hidden email]>:
Hi,

I'm trying to understand where __builtin_constant_p is folded in the
front-end. It appears to be handled differently when placed in an if
condition or in a conditional expression.

In the example below, I see that it is folded in
CodeGenFunction::EmitBuiltinExpr() in CGBuiltin.cpp
int x = __builtin_constant_p(100);

But for cases like the ones below, it appears to be folded much
earlier and it doesn't reach EmitBuiltinExpr().

int x = __builtin_constant_p(100)? 10: 20;

if (__builtin_constant_p(100)) { ... }

Any pointers on what functions to look up for the above case would be
very helpful.

Thanks,
Bharathi
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev



--
Wei-Ren Chen (陳韋任)
Homepage: https://people.cs.nctu.edu.tw/~chenwj

_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Handling of builtin_constant_p

Eric Fiselier via cfe-dev
In reply to this post by Eric Fiselier via cfe-dev
On 26 October 2017 at 20:22, Bharathi Seshadri via cfe-dev <[hidden email]> wrote:
Hi,

I'm trying to understand where __builtin_constant_p is folded in the
front-end. It appears to be handled differently when placed in an if
condition or in a conditional expression.

In the example below, I see that it is folded in
CodeGenFunction::EmitBuiltinExpr() in CGBuiltin.cpp
int x = __builtin_constant_p(100);

But for cases like the ones below, it appears to be folded much
earlier and it doesn't reach EmitBuiltinExpr().

int x = __builtin_constant_p(100)? 10: 20;

if (__builtin_constant_p(100)) { ... }

Any pointers on what functions to look up for the above case would be
very helpful.

In some cases, Clang tries to constant-evaluate expressions rather than emitting them directly. (C and C++ require this to be done in some cases for variable initializers, particularly for globals, and Clang also tries it when it emits some conditional branches, to avoid even emitting trivially-unreachable code when the branch condition is constant). The constant expression evaluation lives in lib/AST/ExprConstant.cpp, and knows how to evaluate many of Clang's builtins (but not all of them).
 
Thanks,
Bharathi
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev