This is an archive of the discontinued LLVM Phabricator instance.

Track whether a unary operation can overflow
ClosedPublic

Authored by aaron.ballman on May 25 2017, 1:23 PM.

Details

Summary

We do not explicitly model integer promotions on unary operators even though those promotions happen in practice. This patch tracks the most important piece of information about the promotion on the AST node: whether the operator can overflow or not. It then pulls this logic out from various places and instead uses what's calculated on the AST node.

This is effectively a NFC patch, however, it does help out-of-tree builds that need to know about the integral promotions.

Diff Detail

Event Timeline

aaron.ballman created this revision.May 25 2017, 1:23 PM
ahatanak added inline comments.
lib/Sema/SemaDeclCXX.cpp
11119

Can we pass false here if we know the array size is not too large and the pre-increment won't cause overflow?

Addressing review comments.

aaron.ballman marked an inline comment as done.May 30 2017, 9:57 AM
aaron.ballman added inline comments.
lib/Sema/SemaDeclCXX.cpp
11119

Good idea!

aaron.ballman marked an inline comment as done.Jun 16 2017, 6:27 AM

Ping

Rebased against ToT, ping.

Post-holiday ping.

efriedma added inline comments.
include/clang/AST/Expr.h
1728

Is the default argument necessary here? Better to avoid when possible.

lib/Sema/SemaExpr.cpp
12212

UO_Plus can't overflow.

test/Misc/ast-dump-stmt.c
66

What does it mean for bitwise complement to "overflow"?

aaron.ballman marked 3 inline comments as done.

Updated based on review feedback.

Thanks for the review!

include/clang/AST/Expr.h
1728

It's not required, but there are quite a few places where we gin up a UnaryOperator for things like address of or dereference where there is no overflow possible. However, I agree that less default arguments are better, so I've made it a required formal argument.

test/Misc/ast-dump-stmt.c
66

When the sign bit flips on a signed value, e.g., ~0.

efriedma added inline comments.Jan 8 2018, 11:54 AM
test/Misc/ast-dump-stmt.c
66

Bitwise complement always flips the sign bit; calling that an "overflow" doesn't seem useful.

aaron.ballman marked 2 inline comments as done.

Updated based on review feedback.

test/Misc/ast-dump-stmt.c
66

For reference, I am upstreaming some changes we needed for an out-of-tree project, so "useful" kinda depends on context. However, I looked at our uses of this functionality and I'm not convinced we need this to report overflows, so I've made the change to not report overflow on a one's complement operand.

rsmith added a comment.Jan 8 2018, 3:14 PM

Looks fine to me, wait to see if Eli has more comments.

efriedma accepted this revision.Jan 8 2018, 3:56 PM

LGTM

This revision is now accepted and ready to land.Jan 8 2018, 3:56 PM
aaron.ballman closed this revision.Jan 9 2018, 5:08 AM

Thanks! Committed in r322074.