aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/AST/ExprConstant.cpp
AgeCommit message (Collapse)AuthorFilesLines
4 hours[X86] Allow PSHUFD/PSHUFLW/PSHUFW intrinsics in constexpr. (#161210)Nagraj Gaonkar1-1/+82
The i16/i32 shuffle intrinsics (`pshufw`, `pshuflw`, `pshufhw`, `pshufd`) currently cannot be used in constant expressions. This patch adds support in both bytecode interpreter (InterpBuiltin.cpp) and constant evaluator (ExprConstant.cpp) for pshuf intrinsics, enabling their use in constant expressions. ## Intrinsics covered - `_mm_shuffle_pi16` (MMX `pshufw`) - `_mm_shufflelo_epi16` / `_mm_shufflehi_epi16` - `_mm_shuffle_epi32` - Their AVX2/AVX512 vector-width variants - Masked and maskz forms (handled indirectly via `__builtin_ia32_select*`) Fixes #156611
6 hours[X86][Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - allow ↵Aditya Chaudhari1-0/+65
element extraction/insertion intrinsics to be used in constexpr #159753 (#161302) FIXES: #159753 Enable constexpr evaluation for X86 vector element extract/insert builtins. and adds corresponding tests Index is masked with `(Idx & (NumElts - 1))`, matching existing CodeGen.
9 days[clang][bytecode] Use in `Expr::tryEvaluateString` (#160118)Timm Baeder1-1/+7
Fixes #157492
9 days[Headers][X86] VectorExprEvaluator::VisitCallExpr - allow SSE/AVX2/AVX512 ↵woruyu1-1/+63
pack intrinsics to be used in constexpr (#156003) Fixes #154283
10 days[Headers][X86] Allow basic AVX512 predicate ops to be used in constexpr ↵fennecJ1-0/+73
(#159998) Fixes #158646
2025-09-19[Clang] Rename elementwise builtins to `clzg` and `ctzg` (#157128)Joseph Huber1-11/+11
Summary: The added bit counting builtins for vectors used `cttz` and `ctlz`, which is consistent with the LLVM naming convention. However, these are clang builtins and implement exactly the `__builtin_ctzg` and `__builtin_clzg` behavior. It is confusing to people familiar with other other builtins that these are the only bit counting intrinsics named differently. This includes the additional operation for the undefined zero case, which was added as a `clzg` extension.
2025-09-18[Clang] VectorExprEvaluator::VisitCallExpr / InterpretBuiltin - allow ↵Aditya Chaudhari1-0/+45
AVX/AVX512 subvector insertion intrinsics to be used in constexpr #157709 (#158778) AVX/AVX512 vector insert intrinsics now support constexpr evaluation in both the AST evaluator and bytecode interpreter paths. FIXES: #157709
2025-09-18[clang] Remove Diag parameter from Expr::EvaluateKnownConstInt (#159512)Timm Baeder1-3/+1
If it's truly a known const int, it won't emit any diagnostics anyway. And if it did, we wouldn't notice because no call site passed something non-null.
2025-09-17[clang] Add unreachable after fully covered switches, avoid GCC warnings. ↵Martin Storsjö1-0/+1
NFC. (#159330) This avoids the following warnings: ../../clang/lib/AST/ExprConstant.cpp: In member function ‘bool {anonymous}::IntExprEvaluator::VisitBuiltinCallExpr(const clang::CallExpr*, unsigned int)’: ../../clang/lib/AST/ExprConstant.cpp:14104:3: warning: this statement may fall through [-Wimplicit-fallthrough=] 14104 | } | ^ ../../clang/lib/AST/ExprConstant.cpp:14105:3: note: here 14105 | case Builtin::BIstrlen: | ^~~~ ../../clang/lib/Driver/ToolChains/CommonArgs.cpp: In function ‘std::string clang::driver::tools::complexRangeKindToStr(clang::LangOptionsBase::ComplexRangeKind ’: ../../clang/lib/Driver/ToolChains/CommonArgs.cpp:3523:1: warning: control reaches end of non-void function [-Wreturn-type] 3523 | } | ^
2025-09-17[clang][ExprConst] Move getLangOpts() to interp::State subclasses (#159280)Timm Baeder1-0/+1
Instead of having `State::getLangOpts()`, which does a virtual call to `getASTContext()` to call `getLangOpts()` on that, just move `getLangOpts()` to the subclasses so we can do that without the virtual call. We never call `getLangOpts()` in `State.cpp`, so it's not needed in the base class.
2025-09-17[clang][ExprConst] Assert that EvaluateAsInitializer has non-null VD (#159274)Timm Baeder1-0/+1
Both the expression (the initializer) as well as the VarDecl can't be null here. Assert that.
2025-09-16[clang][ExprConst] Reject unary vector shuffles (#158589)Timm Baeder1-0/+3
This is not implemented at compile time and asserts in assertion builds, so reject it here. Fixed the coding style in `BuiltinShuffleVector` at the same time. Fixes #158471
2025-09-15[X86][bytecode] Allow SSE/AVX BLEND imm intrinsics to be used in constexpr ↵Brandon1-0/+27
(#157776) This marks the following builtins as constexpr, which allows their corresponding intrinsics to be used in constexprs. | Intrinsics | X86 Builtins | CPUID Flags | Header | | -------------------- | --------------------------- | ----------- | ----------- | | `_mm_blend_pd` | `__builtin_ia32_blendpd` | SSE4.1 | smmintrin.h | | `_mm256_blend_pd` | `__builtin_ia32_blendpd256` | AVX | immintrin.h | | `_mm_blend_ps` | `__builtin_ia32_blendps` | SSE4.1 | smmintrin.h | | `_mm256_blend_ps` | `__builtin_ia32_blendps256` | AVX | immintrin.h | | `_mm_blend_epi16` | `__builtin_ia32_pblendw128` | SSE4.1 | smmintrin.h | | `_mm256_blend_epi16` | `__builtin_ia32_pblendw256` | AVX2 | immintrin.h | | `_mm_blend_epi32` | `__builtin_ia32_pblendd128` | AVX2 | immintrin.h | | `_mm256_blend_epi32` | `__builtin_ia32_pblendd256` | AVX2 | immintrin.h | Fixes #157065 --------- Co-authored-by: Timm Baeder <tbaeder@redhat.com> Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-09-15[clang][bytecode] Pass initializer along in `evaluateAsInitializer()` (#158056)Timm Baeder1-1/+1
We just called `getInit()`, which isn't always correct and used the wrong initializer in the module test case.
2025-09-11[clang][bytecode] Use bytecode interpreter in EvaluateAsLValue (#158038)Timm Baeder1-0/+12
Set the EvalMode on InterpState and abort when initalizing a global temporary, like the current interpreter does. The rest is just plumbing in EvaluateAsLValue. Fixes #157497
2025-09-11[clang][ExprConst][NFC] Move EvalMode enum to State (#157988)Timm Baeder1-70/+52
Make it an enum class and move the enum to State.h as well as the `EvalMode` member to `State`. This is in preparation of using the evaluation mode from `InterpState` as well.
2025-09-11[clang][ExprConst][NFC] Take a const ASTContext in a few places (#157985)Timm Baeder1-3/+3
We don't need a mutable `ASTContext` here.
2025-09-09[Headers][X86] Enable constexpr handling for MMX/SSE/AVX/AVX512 avg ↵Bhasawut Singhaphan1-0/+8
intrinsics (#157464) This PR updates the avg builtins to support constant expression handling, by extending the VectorExprEvaluator::VisitCallExpr that handles elementwise integer binop builtins. Closes #155390 --------- Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-09-09[clang][bytecode][X86] Allow AVX512 funnel shift by scalar immediate ↵Simon Pilgrim1-0/+63
intrinsics to be used in constexpr (#157681) Extends interp__builtin_elementwise_triop to handle (vector, vector, scalar) trinary op intrinsics Fixes #153152
2025-09-09[X86] Allow remaining AVX512 per-element shift intrinsics to be used in ↵Simon Pilgrim1-0/+14
constexpr (#157696) Handle missing AVX512F epi64 and AVX512BW epi16 cases with existing code paths Fixes #154287
2025-09-06[clang] VectorExprEvaluator::VisitCallExpr - use APSInt callback instead of ↵Simon Pilgrim1-195/+86
repeated switch statement (#157137) Create a EvaluateBinOpExpr helper that each related group of elementwise binop builtins can use with their own custom callback, to help reduce the amount of duplication and avoid too much code bloat as more builtins are added. This also handles builtins which have a elementwise LHS operand and a scalar RHS operand. Similar to #155891 which did the same thing for the new ByteCode eval.
2025-09-06[clang] Move two flags from EvalInfo to State (#157046)Timm Baeder1-26/+0
Instead of relaying from InterpState to the parent state (which is an EvalInfo), just save the variables in State instead, so both subclasses have access to it.
2025-09-05[X86][bytecode] Allow SSE/AVX BLENDVPD/PD intrinsics to be used in constexpr ↵Simon Pilgrim1-2/+7
(#157126) BLENDV intrinsics use the signbit of the condition mask to select between the LHS (false) and RHS (true) operands Fixes #157066
2025-09-05[X86][bytecode] Allow SSE/AVX PBLENDVB intrinsics to be used in constexpr ↵Simon Pilgrim1-0/+22
(#157100) BLENDV intrinsics use the signbit of the condition mask to select between the LHS (false) and RHS (true) operands First part of #157066 - the BLENDVPS/D requires floatbits hacking which I need to do some prep work for
2025-09-04[Clang] Enable constexpr handling for builtin elementwise fshl/fshr (#153572)Chaitanya Koparkar1-0/+53
Fixes #153151.
2025-09-04[clang][initlist] handle incomplete array type in Constant Expr Calculation ↵Congcong Cai1-5/+8
(#155080) fix: #151716 In #65918, support of incomplete array type is added in TryReferenceListInitialization. It causes the crash in Constant Expr Calculation since it only considers the case where it is ConstantArrayType. This patch wants to add support for incomplete array type also.
2025-09-03[X86] Allow AVX512 512-bit variants of AVX2 per-element i32 shift intrinsics ↵Simon Pilgrim1-0/+6
to be used in constexpr (#156480) Followup to #154780
2025-09-02[clang] Followup for constexpr-unknown potential constant expressions. (#151053)Eli Friedman1-60/+81
6a60f18997d62b0e2842a921fcb6beb3e52ed823 fixed the primary issue of dereferences, but there are some expressions that depend on the identity of the pointed-to object without actually accessing it. Handle those cases. Also, while I'm here, fix a crash in interpreter mode comparing typeid to nullptr.
2025-09-02[Clang] [C2y] Implement N3355 ‘Named Loops’ (#152870)Sirraide1-17/+65
This implements support for [named loops](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3355.htm) for C2y. When parsing a `LabelStmt`, we create the `LabeDecl` early before we parse the substatement; this label is then passed down to `ParseWhileStatement()` and friends, which then store it in the loop’s (or switch statement’s) `Scope`; when we encounter a `break/continue` statement, we perform a lookup for the label (and error if it doesn’t exist), and then walk the scope stack and check if there is a scope whose preceding label is the target label, which identifies the jump target. The feature is only supported in C2y mode, though a cc1-only option exists for testing (`-fnamed-loops`), which is mostly intended to try and make sure that we don’t have to refactor this entire implementation when/if we start supporting it in C++. --------- Co-authored-by: Balazs Benics <benicsbalazs@gmail.com>
2025-08-30[X86] Add constexpr handling for XOP/AVX512 rotate by immediate intrinsics ↵Simon Pilgrim1-0/+56
(#156047)
2025-08-29[Headers][X86] VisitCallExpr constexpr immediate shifts (#154293) (#155542)Justin Riddell1-1/+90
Fixes #154293 Implement VectorExprEvaluator::VisitCallExpr constexpr support for left, right, arithmetic shift for MMX/SSE/AVX2/AVX512 intrinsics _mm*_slli_epi* _mm*_srli_epi* _mm*_srai_epi* _mm*_mask_slli_epi* _mm*_maskz_slli_epi* NOTE: not all intrinsics have all widths i.e. _mm_srli_pi32 doesn't have pi64 etc.
2025-08-27[clang] NFC: reintroduce clang/include/clang/AST/Type.h (#155050)Matheus Izvekov1-1/+1
This reintroduces `Type.h`, having earlier been renamed to `TypeBase.h`, as a redirection to `TypeBase.h`, and redirects most users to include the former instead. This is a preparatory patch for being able to provide inline definitions for `Type` methods which would otherwise cause a circular dependency with `Decl{,CXX}.h`. Doing these operations into their own NFC patch helps the git rename detection logic work, preserving the history. This patch makes clang just a little slower to build (~0.17%), just because it makes more code indirectly include `DeclCXX.h`.
2025-08-27[clang] NFC: rename clang/include/clang/AST/Type.h to TypeBase.h (#155049)Matheus Izvekov1-1/+1
This is a preparatory patch, to be able to provide inline definitions for `Type` functions which depend on `Decl{,CXX}.h`. As the latter also depends on `Type.h`, this would not be possible without some reorganizing. Splitting this rename into its own patch allows git to track this as a rename, and preserve all git history, and not force any code reformatting. A later NFC patch will reintroduce `Type.h` as redirection to `TypeBase.h`, rewriting most places back to directly including `Type.h` instead of `TypeBase.h`, leaving only a handful of places where this is necessary. Then yet a later patch will exploit this by making more stuff inline.
2025-08-27[clang] Make sure EvalInfo pointer isn't null (#155563)Timm Baeder1-4/+6
It can be null, when called via CheckICE(). Accidentally introduced via #119366 Fixes #155507
2025-08-27[clang] AST: fix getAs canonicalization of leaf types (#155028)Matheus Izvekov1-9/+8
2025-08-27[clang] Post-commit review for #150028 (#155351)Timm Baeder1-1/+2
1) Return `std::nullopt` instead of `{}`. 2) Rename the new function to evaluate*, it's not a simple getter.
2025-08-26[clang] NFC: introduce Type::getAsEnumDecl, and cast variants for all ↵Matheus Izvekov1-18/+6
TagDecls (#155463) And make use of those. These changes are split from prior PR #155028, in order to decrease the size of that PR and facilitate review.
2025-08-26[Clang] Support generic bit counting builtins on fixed boolean vectors (#154203)Joseph Huber1-3/+39
Summary: Boolean vectors as implemented in clang can be bit-casted to an integer that is rounded up to the next primitive sized integer. Users can do this themselves, but since the counting bits are very likely to be used with bitmasks like this and the generic forms are expected to be generic it seems reasonable that we handle this case directly.
2025-08-25[clang] NFC: change more places to use Type::getAsTagDecl and friends (#155313)Matheus Izvekov1-10/+7
This changes a bunch of places which use getAs<TagType>, including derived types, just to obtain the tag definition. This is preparation for #155028, offloading all the changes that PR used to introduce which don't depend on any new helpers.
2025-08-25[Clang] Fix Variable Length Array `_Countof` Crash (#154627)Vincent1-1/+11
Check for missing VLA size expressions (e.g. in int a[*][10]) before evaluation to avoid crashes in _Countof and constant expression checks. Fixes #152826
2025-08-25[clang] Implement -Walloc-size diagnostic option (#150028)Vladimir Vuksanovic1-63/+8
Warns about calls to functions decorated with attribute `alloc_size` that specify insufficient size for the type they are cast to. Matches the behavior of the GCC option of the same name. Closes #138973 --------- Co-authored-by: Vladimir Vuksanovic <vvuksano@cisco.com>
2025-08-23[clang] Remove hasValue() check in ↵Timm Baeder1-4/+0
`RecordExprEvaluator::VisitCXXConstructExpr()` (#154610) When initializing an anonymous struct via an `IndirectFieldDecl`, we create an `APValue` for the struct, but we leave the fields uninitialized. This would later cause the `CXXConstructExpr` that initializes the anonymous struct member to not do anything since its `APValue` already had a value (but the member didn't). Just remove the check for an `APValue` that already has a value from `RecordExprEvaluator::VisitCXXConstructExpr()`. Fixes #154567
2025-08-22[X86] Allow AVX2 per-element shift intrinsics to be used in constexpr (#154780)Simon Pilgrim1-3/+48
This handles constant folding for the AVX2 per-element shift intrinsics, which handle out of bounds shift amounts (logical result = 0, arithmetic result = signbit splat) AVX512 intrinsics will follow in follow up patches First stage of #154287
2025-08-20[clang] Enable constexpr handling for __builtin_elementwise_fma (#152919)Chaitanya Koparkar1-0/+37
Fixes https://github.com/llvm/llvm-project/issues/152455.
2025-08-20[clang] Introduce elementwise ctlz/cttz builtins (#131995)Fraser Cormack1-3/+65
These builtins are modeled on the clzg/ctzg builtins, which accept an optional second argument. This second argument is returned if the first argument is 0. These builtins unconditionally exhibit zero-is-undef behaviour, regardless of target preference for the other ctz/clz builtins. The builtins have constexpr support. Fixes #154113
2025-08-18Remove unused variable introduced in #152705Simon Pilgrim1-1/+0
2025-08-18[Clang][X86] Add avx512 __builtin_ia32_select* constexpr handling (#152705)Simon Pilgrim1-0/+45
This should allow us to constexpr many avx512 predicated intrinsics where they wrap basic intrinsics that are already constexpr Fixes #152321
2025-08-18[Headers][X86] Allow pmuludq/pmuldq to be used in constexpr (#153293)Pedro Lobo1-0/+37
Adds `constexpr` support for `pmuludq` and `pmuldq` intrinsics. Closes #153002. Part of #30794.
2025-08-14[clang] constexpr `__builtin_elementwise_abs` support (#152497)Vincent1-0/+32
Added constant evaluation support for `__builtin_elementwise_abs` on integer, float and vector type. fixes #152276 --------- Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk>
2025-08-12[clang] constexpr integer `__builtin_elementwise_{max,min}` (#152294)Iris Shi1-0/+52
- Closes #152278 - Part of #51787 This PR adds support for the bulitin elementwise max/min functions for integer types.