[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
[X86][APX] Fold loads into NDD CMOVs that are converted to branches (#245602)
Without prefer-ndd-mem, ISel folds only RIP-relative loads into NDD
CMOVs, so select(c, load, x) becomes MOVrm + CMOVrr_ND instead of
CMOVrm. The force-mem-operand conversion in X86CmovConversion no longer
sees the load, and the CMOV is kept where non-APX code gets a branch.
Fold any load into an NDD CMOV when X86CmovConversion converts memory
operand CMOVs to branches, so the existing CMOVrm conversion applies.
Where it keeps such a CMOV (e.g. an unpredictable select), unfold the
non-RIP-relative load again.
X86::foldNDDCmovLoads() holds the condition, so ISel and
X86CmovConversion agree on when a load is folded and unfolded.
-x86-cmov-converter-ndd-load=false disables the fold.
Assisted-by: Claude Opus 5.5 <noreply at anthropic.com>
[lld][MachO] Stream FunctionStartsSection from laid-out code sections
Currently, `FunctionStartsSection::finalizeContents()` scans every
`ObjFile`'s full symbol table, resolves `defined->getVA()` for each live
code symbol, sorts all addresses with `llvm::sort`, and encodes ULEB128
deltas through `raw_svector_ostream`.
By the time `FunctionStartsSection::finalizeContents()` runs, output
segments, their sections, and each `ConcatOutputSection`'s inputs are
already laid out in ascending virtual address order, and each input
section's `symbols` list is kept sorted by offset. This PR walks those
laid-out code sections directly and streams ULEB128 deltas straight into
`contents` via `llvm::appendLEB128`. If a malformed input produces an
out-of-order symbol address, it reports an `error()` and skips the
symbol.
To avoid calling `isCodeSection()` on every subsection, we add a
`SectionView` wrapper so `isCodeSection()` can accept either a `Section`
or an `InputSection`, and cache its result across consecutive inputs
[10 lines not shown]
[VPlan] Remove header masks whose tail lanes aren't used
Replace a use of header-mask && x with x if the user, transitively
through freeze, logical-and, not and mask.beforefirst(freeze ...), only
reaches masks that are and'ed with the header mask again. The header
mask is a prefix and lane i of mask.beforefirst only depends on lanes
0..i, so the lanes outside of the header mask are never used.
This avoids the i1 vp.merge that EVL tail folding needs for the inner
header mask, e.g. in the early exit mask.beforefirst(freeze(header-mask
&& c)) and in switch default edges.
[VPlan] Allow tail folding early exits with side effects
Continues the work in #179595.
This allows us to tail fold e.g. the following loop:
```c
extern char a[1024], b[1024], c[1024], y[1024];
void two_guards(void) {
for (int i = 0; i < 1000; i++) {
if (a[i] == 0)
break;
if (b[i] > 3) {
if (c[i] < 9)
y[i] = 1;
}
}
}
```
[30 lines not shown]
[VPlan] Support tail folding uncountable early exits
Enables the first step towards #179595. After #237254, the MaskedHandleExitInVectorLoop early exit style uses the CFG to handle masking for early exit loops. This allows it to compose with tail folding which is also handled via the CFG in #176143.
This allows us to tail fold the following loop:
```c
extern char x[256];
int f(char * restrict y) {
for (int i = 0; i < 256; i++) {
if (x[i] == 0)
return i;
}
return -1;
}
```
Into this on RISC-V:
[18 lines not shown]
[VPlan] Use vputils::findCanonicalIVIncrement in more places. NFC
In a future patch we want to enable more scenarios with early exits and tail folding. In these scenarios the terminator will no longer be a BranchOnCount/BranchOnCond but a BranchOnTwoConds, so a bunch of canonical IV matchers break. Use vputils::findCanonicalIVIncrement instead since it searches the users of the canonical IV instead of looking at the terminator.
[VPlan] Allow early exit loops with side effects by masking in the vector loop.
When all blocks with side effects are dominated by every early exit, we can vectorize the loop if we mask off any lanes past the exit. We can do this with control flow:
```mermaid
flowchart TD
header --> exiting
exiting -->| exit mask | sideeffects --> latch --> latch.split
exiting -->| !exit mask | latch.split
```
For every early exiting block we add a branch to the latch on `!mask.beforefirst(exitcond)`. This is implemented in UncountableExitStyle::ReadOnly which is now renamed to MaskedHandleExitInVectorLoop.
This is a complementary exit style to MaskedHandleExitInScalarLoop. If any of the side effects aren't dominated by the early exit it still falls back to the scalar loop style, which is why none of the existing early exit side effect tests changed. Masking in loop is profitable for RISC-V and I'm assuming it's also profitable for AArch64 too, but if not then we can add a TTI hook for a preferred exit style.
[VPlan] Preserve SSA in early exit loops
MaskedCond is used to logically-and the condition to early exit with the current block in-mask. However because we use it in the latch block where the condition isn't defined on all paths, we have to workaround it not being in SSA in the verifier.
This fixes this by using reconstructSSA added in #212209 to preserve SSA in both the exit conditions and live outs in the exit blocks. It also means we get the right exit condition without MaskedCond and allows us to remove the MaskedCond exceptions in the verifier. MaskedCond is still kept since a followup PR will still use it.
One subtle point is that we need to freeze any branch conditions. Any lanes after and including the early exiting lane might contain poison and we can have branches on those poison lanes that didn't we didn't previously branch on in the scalar loop.
There's no miscompile on main today because we coincidentally don't keep SSA so exit conditions are read directly without being polluted by poison stemming from a branch past the early exit. But in this PR the exit conditions are polluted and we can end up with poison in the condition operand of a select, which causes an undefined result for AnyOf even if it freezes its operand. The poison in the branch will also cause problems in future if we decide to preserve uniform control flow and emit branches instead of blends. The exit_cond_merged_after_poison_branch test illustrates this.
[VPlan] Simplify BLEND X/M, false, ..., false -> M && X
If an i1 blend has false for all but one incoming value, then we can simplify this to the one non-false value. Blends by construction never have any overlapping mask values, so we don't need to worry about the order as the masks can't "shortcut" each other.
[VPlan] Remove header masks whose tail lanes aren't used
Replace a use of header-mask && x with x if the user, transitively
through freeze, logical-and, not and mask.beforefirst(freeze ...), only
reaches masks that are and'ed with the header mask again. The header
mask is a prefix and lane i of mask.beforefirst only depends on lanes
0..i, so the lanes outside of the header mask are never used.
This avoids the i1 vp.merge that EVL tail folding needs for the inner
header mask, e.g. in the early exit mask.beforefirst(freeze(header-mask
&& c)) and in switch default edges.