[SelectionDAG] Use the pointer memory type for byte `bitcast`, `bitinsert` and `bitextract` (#242624)
Lower pointer byte casts through the pointer's memory type.
On arm64_32, a pointer is 32 bits in memory but lives in a 64-bit
register. `bitcast`, `bitinsert` and `bitextract` used the 64-bit
register type as the pointer's size. Most of these crashed on a size
mismatch. `bitextract ptr, b64 %b, i32 0` miscompiled, keeping the high
32 bits of `%b` in the pointer.
Like `ptrtoint` and `inttoptr`, a pointer operand is now truncated to
its memory type, and a pointer result is zero-extended from it.
Fixes #241635.
[TTI] Cost `bitinsert` and `bitextract` by their lowering (#242516)
Add TTI costs for `bitinsert` and `bitextract`.
Previously, neither had a cost, so they fell back to `TCC_Basic`. That
made a widened `bitinsert` look as cheap as a scalar one.
This adds `getBitInsertCost()` and `getBitExtractCost()`, which take
types, so the loop vectorizer can cost vector forms. The default costs
follow the SelectionDAG lowering:
- `bitinsert` costs an `and`, a `shl` and an `or`, plus a `zext` of a
narrower value.
- With a variable offset, `bitinsert` also shifts and inverts the mask.
- `bitextract` costs an `lshr` and a `trunc` to the result.
- With a varying vector offset, both also cost a `zext` or a `trunc` of
the `i32` offset to the shift type.
[HashRecognize] Prevent values other than `ComputedValue` from exiting loop (#213080)
A check exists to prevent recurrences from having stray uses, but this
is only applied when a simple recurrence exists, and does not apply to
other values such as the induction variable. Since the loop is in LCSSA
form, check the incoming value from the loop on each of the exit block
PHIs and bail if any of them are not `ComputedValue`.
This replaces the existing check requiring `ComputedValue` to be used in
the exit block. Note that this no longer covers the case where the exit
block has no PHIs, and therefore does not use `ComputedValue`. In this
case, the loop is dead and will be cleaned up by DCE anyway.
(cherry picked from commit 34a34f1dac1d40e2ba49f39e9ebce8eafdf44ecc)
[InstCombine] Drop poison-generating annotations from call in select-of-zero fold (#245874)
Fixes #245656.
`foldSelectZeroOrFixedOp` folds `select (x == 0), 0, op(x, y)` into
`op(x, freeze(y))`, which evaluates `op` unconditionally. If `op` is a
call (`umin`, `fshl`, `fshr`) with a `range` attribute that excludes
`0`, e.g. `range(i64 1, 0)` on the return value or an argument, the call
now returns a value outside its declared range at `x == 0`. The result
is poison where the original select produced `0`.
Drop the poison-generating annotations on call instructions. Non-call
ops (`mul`, `idiv`, etc.) keep their `nuw`/`nsw`/`exact` flags, since at
`x == 0`, `0 * y` cannot overflow and `0 sdiv/udiv y` stays exact.
Alive2 proofs:
- Return range dropped: https://alive2.llvm.org/ce/z/ZhgWuj
- Arg 0 range dropped: https://alive2.llvm.org/ce/z/dAKtZF
- Arg 1 range dropped: https://alive2.llvm.org/ce/z/D5URrH
[5 lines not shown]
[alpha.webkit.UncountedCallArgsChecker] Crash in printArgument (#210411)
The crash was caused by missing nullptr check for Decl used to get
printing policy. Fixed the bug by replacing the use of Decl with
BugManager to get the policy.
(cherry picked from commit 3fc4fcade6164fc6b9dcf0d6d0d6fa9cf6e47dfc)
[X86] Fix miscompile of srl(xor(y, vecreduce_umax(x)), bw-1) (#244835)
In the signbit reduction fold added in #210281, `X` is initialized to
`N0` as the fallback for a failed `m_Not` match, but `sd_match`
overwrites `X` while trying the `xor` operands, even when the match
fails. If the last operand tried is `vecreduce_umax(x)`, the `xor` is
dropped and the shift is lowered to a `MOVMSK` test of `x`.
Set the fallback only after the match fails.
Fixes #244613
Fixes #244753
Assisted-by: Claude Opus 5.5
(cherry picked from commit 4d07c01b3ce0767348925a744566a9cd1beb3072)
[InlineSpiller] Use a new value number for the COPY range of a fold (#243055)
When folding a memory operand makes the target insert a COPY (e.g. a
reload folded into an X86 NDD instruction), foldMemoryOperand() extends
the original live interval from the COPY to the folded instruction
(#199182). That segment reused the folded instruction's value number,
though the register holds the COPY's source there.
HoistSpillHelper groups spills by (stack slot, original value number),
so rmRedundantSpills treated the store the COPY was folded into and the
later spill of the folded result as duplicates, and deleted the latter.
In memfold-origVNI-crash.ll this dropped an `or`, returning %p2 instead
of %p2 | %p3.
Give the segment its own value number at the COPY. Replace the
getVNInfoAt() guard with an assert: the sub-interval invariant already
requires OrigLI to be live at FoldMI's def.
Assisted-by: Claude Opus 5.5 <noreply at anthropic.com>
(cherry picked from commit 7016ba10ad070658b9c62f47b8fde16be7f2ac4c)
[InlineSpiller] Do not merge or hoist a spill with no original value (#237148)
HoistSpillHelper keeps a copy of the original live interval, taken at
the first spill to each stack slot and never updated, and keys
MergeableSpills by the original VNInfo of each spill. Since #199182,
foldMemoryOperand can extend the original interval backwards over the
COPY that an X86 NDD fold inserts (#191368). A spill later folded into
that COPY lies outside the saved interval, so getVNInfoAt returns null.
The null VNInfo* is stored as a MergeableSpills key and dereferenced in
runHoistSpills (SIGSEGV).
Leave such a spill out of merging and hoisting; it stays where it was
inserted. Both are optimizations, so skipping them is safe, and inputs
that did not crash before get the same code.
Assisted-by: Claude Opus 5.5 <noreply at anthropic.com>
(cherry picked from commit 1d80abf1662cdbf76bd479176f5a04e40d94f909)
[libcxx] fix missing `__get_locale_encoding()` for OpenBSD (#228306)
Fixes errors like this:
/home/alexrp/Sources/ziglang/zig-llvm23/lib/libcxx/src/text_encoding.cpp:34:43:
error: no member named '__get_locale_encoding' in namespace
'std::__locale'
34 | return
__make_text_encoding(__locale::__get_locale_encoding(static_cast<__locale::__locale_t>(0)));
| ^~~~~~~~~~~~~~~~~~~~~
/home/alexrp/Sources/ziglang/zig-llvm23/lib/libcxx/src/text_encoding.cpp:48:41:
error: no member named '__get_locale_encoding' in namespace
'std::__locale'
48 | return __make_text_encoding(__locale::__get_locale_encoding(__l));
| ^~~~~~~~~~~~~~~~~~~~~
2 errors generated.
(cherry picked from commit 19f123540bfc1dc55f93d4dc22a3276b434f6cc4)
[SLP][modularisation][NFC] Move BoUpSLP class definition to SLPTree.h
Move the BoUpSLP class definition and its nested types out of
SLPVectorizer.cpp into the internal header SLPVectorizer/SLPTree.h. Method
definitions outside the class body stay in SLPVectorizer.cpp.
The header is self-contained: it compiles as the only include of a
translation unit and does not depend on anything SLPVectorizer.cpp defines
before including it.
- The limits MaxMemDepDistance and MinScheduleRegionSize, used by inline
scheduler code, move with the class and become static constexpr.
- DenseMapInfo<BoUpSLP::EdgeInfo> moves after the class, since inline
BoUpSLP code instantiates maps keyed on EdgeInfo.
- The header defines DEBUG_TYPE for the LLVM_DEBUG uses in the inline
scheduler code and undefines it at the end.
- getShallowScore gets a function-local PatternMatch using-directive.
- clang-format reflows five moved lines (two accessors, one comment, one
parameter list, operator==).
[5 lines not shown]
[GVN] Preserve PRE load metadata as much as possible
The synthosis Load instruction preserve only hand picked metadata. In
here, we can actually preserce more then we have now. We use the
existing copyMetadataForLoad to fix it. Also, some UB implying
metadata cannot be preserved so that we drop it.
Also, AA related metada should be be keeped after dropping. We call it
after dropping so that we don't need a long whitelist list for the
dropping metadata.
[mlir][SPIR-V] Add spirv.DemoteToHelperInvocation op (#245365)
Adds the `spirv.DemoteToHelperInvocation` op corresponding to
`OpDemoteToHelperInvocation` (opcode 5380), introduced by
`SPV_EXT_demote_to_helper_invocation` and promoted to core in SPIR-V
1.6.
Assisted with Google Antigravity Gemini 4 Neon model
[SLP][modularisation][NFC] Prepare BoUpSLP for a header of its own
Remove the file-local names the BoUpSLP class definition depends on, so the
class can move to SLPVectorizer/SLPTree.h without the header relying on
declarations in SLPVectorizer.cpp:
- SLPReductionUtils: ReductionVectorPart.
- SLPCompatibilityAnalysis: generateKeySubkey.
- HorizontalReduction moves from the anonymous namespace into
llvm::slpvectorizer, so BoUpSLP can befriend it from a header.
Part of the SLPVectorizer.cpp modularization effort:
https://discourse.llvm.org/t/modularizing-slpvectorizer-cpp/90922
Assisted by AI
[RISCV][TTI] Set the default MaxVectorCoalesceElts to 1 (#246590)
Alternative to #246576
Set the default MaxVectorCoalesceElts to 1 so strided vector memory
operations are costed per element rather than receiving a cache-line
coalescing discount. This matches scheduling models such as SiFive7,
which model ordinary strided accesses as one element per cycle.
This is motivated by reversed_unit_stride: a reverse shuffle followed by
a unit-stride store can replace a negative-stride store. The coalescing
discount can favor the negative-stride store over this alternative.
Add a standalone SLP regression test checking that reversed unit-stride
stores use a reverse shuffle followed by an ordinary vector store.
[AMDGPU] Re-enable AAInstanceInfo in AMDGPUAttributor
This was enabled and then disabled because of its compile time impact. This
recent changes, the compile time issue should have been resolved.
[Attributor] Use call site values when the callee returns an argument
AAPotentialValuesCallSiteReturned takes the interprocedural values of a call
from the interprocedural return values of the callee. If the callee returns one
of its arguments, these values contain the arguments of all call sites, so each
call result also gets the values passed at all other call sites. This is not
necessary. If the callee has many call sites and the call results are passed to
other calls, the value sets also become very large.
If the callee returns only its arguments and constants, we just need to
translate these values to the call site for the interprocedural scope.
This PR was assisted by AI but I reviewed all the changes.
[Attributor] Do not copy large value sets in `AAPotentialValues`
`AAPotentialValuesImpl::recurseForValue` copies all values of another position
into the state. For example, if a callee returns one of its arguments, the call
result takes the values of the call site operand. In a chain of calls where each
call gets the result of the previous call, each call result then has one more
value than the previous one. A call result also sees the new values of the
previous call only in the next fixpoint iteration, so a chain of N calls needs N
iterations, and a long chain hits the iteration limit.
In this PR, we just limit the number of values to copy to 7, and use the value
of the position itself otherwise. 7 is a magic number here, but it is sufficient
for most cases (telling from the fact that no existing test is affected by this
change).
This PR was assisted by AI but I reviewed all the changes.
[RISCV][Docs] Fix the return type in vector add call examples (#247025)
Both the masked and unmasked `llvm.riscv.vadd` examples omit the return
type after `call`. When either example is placed in a function,
`llvm-as` reports `expected type` at the intrinsic name. Add `<vscale x
4 x i32>` to both calls.
With LLVM 24, both examples assemble and pass the IR verifier. On RV64,
`llc` emits `vadd.vv` for both and uses `v0.t` for the masked form. The
existing RV32 and RV64 `rvv/vadd.ll` tests passed, and the LLVM HTML
documentation built with warnings treated as errors.
Assisted-by: OpenAI Codex
[GVN] Process Foldable Terminator beforehead
GVN generates phi node to replace a load call. However, addDeadBlock
modify such phi so that any deadblock sucessor in a phi becomes
poison.
This causes disagreement on SSAUpdater as [%a, %b1] [poison, %b2] is
considered as different with [%a, %b1] [0, %b2] even if poison is
replace with 0.
As a result, we make processFoldableTerminator (which is the only
caller of addDeadBlock now) runs before processInstruction. This is
before what we have on phi node generation so that non of the mismatch
happens.
Asissted-by: Claude # Test