357 Commits

Author SHA1 Message Date
Arthur Eubanks
e34d614e7d
[Passes] Remove -enable-infer-alignment-pass flag (#111873)
This flag has been on for a while without any complaints.
2024-10-10 12:28:46 -07:00
Kazu Hirata
4ac42afbcc
[InstCombine] Use llvm::set_is_subset (NFC) (#102778) 2024-08-10 22:46:03 -07:00
Shilei Tian
f38baad3e7
[InstCombine] Fix a crash in PointerReplacer (#98987)
A crash could happen in `PointerReplacer::replace` when constructing a
new
select instruction and there is no replacement for one of its operand.
This can
happen when the operand is a load instruction that has been replaced
earlier
such that the operand itself is already the new value. In this case, it
is not
in the replacement map and `getReplacement` simply returns nullptr.

Fix SWDEV-472192.
2024-07-16 13:17:24 -04:00
Nikita Popov
434a8a08a2 [InstCombine] Preserve all gep nowrap flags in PointerReplacer 2024-06-04 09:30:43 +02:00
Matt Arsenault
8cb19ebd21
InstCombine: Stop handling bitcast in PointerReplacer (#92937)
These should be irrelevant since opaque pointers.
2024-05-21 20:49:21 +02:00
Matt Arsenault
847c83f7cc
InstCombine: Process addrspacecast uses in PointerReplacer (#91953)
This was looking through an addrspacecast, and not finding a later
unfoldable cast to another address space. Fixes improperly deleting
a required alloca + memcpy and introducing an illegal addrspacecast.
    
This also required fixing some worklist management issues with
addrspacecast, and assuming that only memcpy sources could need
replacement.
    
Regresses one test function, but this looks like it optimized
before by accident. It never saw the pointer use by the call
to readonly_callee, which should require insertion of a new cast.
    
Fixes #68120
2024-05-15 07:02:31 +02:00
Matt Arsenault
8823abea6f InstCombine: Simplify vector initialization 2024-05-13 13:59:45 +02:00
Matt Arsenault
c5b0da9d83
InstCombine: Preserve inbounds in PointerReplacer (#91735)
This avoids spurious test changes in a future commit.
2024-05-13 13:49:09 +02:00
Jeremy Morse
2fe81edef6 [NFC][RemoveDIs] Insert instruction using iterators in Transforms/
As part of the RemoveDIs project we need LLVM to insert instructions using
iterators wherever possible, so that the iterators can carry a bit of
debug-info. This commit implements some of that by updating the contents of
llvm/lib/Transforms/Utils to always use iterator-versions of instruction
constructors.

There are two general flavours of update:
 * Almost all call-sites just call getIterator on an instruction
 * Several make use of an existing iterator (scenarios where the code is
   actually significant for debug-info)
The underlying logic is that any call to getFirstInsertionPt or similar
APIs that identify the start of a block need to have that iterator passed
directly to the insertion function, without being converted to a bare
Instruction pointer along the way.

Noteworthy changes:
 * FindInsertedValue now takes an optional iterator rather than an
   instruction pointer, as we need to always insert with iterators,
 * I've added a few iterator-taking versions of some value-tracking and
   DomTree methods -- they just unwrap the iterator. These are purely
   convenience methods to avoid extra syntax in some passes.
 * A few calls to getNextNode become std::next instead (to keep in the
   theme of using iterators for positions),
 * SeparateConstOffsetFromGEP has it's insertion-position field changed.
   Noteworthy because it's not a purely localised spelling change.

All this should be NFC.
2024-03-05 15:12:22 +00:00
Paul Walker
28fb2b33c2
[LLVM][SelectionDAG] Reduce number of ComputeValueVTs variants. (#75614)
This is another step in the direction of fixing the `Fixed(0) !=
Scalable(0)` bugbear, although whilst weird I don't believe it's causing
us any real issues.
2024-02-21 13:03:24 +00:00
Nikita Popov
89dae798cc [Loads] Use BatchAAResults for available value APIs (NFCI)
This allows caching AA queries both within and across the calls,
and enables us to use a custom AAQI configuration.
2024-01-24 14:04:21 +01:00
Nikita Popov
97efd8aa43 [InstCombine] Preserve inalloca tag when transforming alloca
This is not meaningful in any practical sense, and just makes sure
we don't cause verifier failures.
2023-12-11 14:27:00 +01:00
Nikita Popov
ae7bffd71c [InstCombine] Don't create unnecessary zero-index GEP (NFCI)
Note needed with opaque pointers.
2023-12-11 13:09:09 +01:00
Nikita Popov
6e3e21d203 [InstCombine] Remove unnecessary removeBitcastsFromLoadStoreOnMinMax() fold (NFCI)
This optimizes a very specific pointer bitcast pattern, and as
such is no longer relevant with opaque pointers.
2023-10-24 17:31:35 +02:00
Fangrui Song
2d854dd3e7 Move global namespace cl::opt inside llvm:: or internalize them 2023-10-10 19:58:03 -07:00
Dhruv Chawla
515a826326
[NFC][InferAlignment] Swap extern declaration and definition of EnableInferAlignmentPass
This prevents a linker issue when only InstCombine is linked without
PassBuilder, like in the case of bugpoint.
2023-09-20 13:07:13 +05:30
Dhruv Chawla
0104f37f16
[InstCombine] Use a cl::opt to control calls to getOrEnforceKnownAlignment in LoadInst and StoreInst
This is in preparation for the InferAlignment pass which handles
inferring alignment for instructions separately. It is better to handle
this as a separate pass as inferring alignment is quite costly, and
InstCombine running multiple times in the pass pipeline makes it even
more so.

Differential Revision: https://reviews.llvm.org/D158527
2023-09-20 12:08:14 +05:30
Paul Walker
c7d65e4466 [IR] Enable load/store/alloca for arrays of scalable vectors.
Differential Revision: https://reviews.llvm.org/D158517
2023-09-14 13:49:01 +00:00
Jeremy Morse
d529943a27 [NFC][RemoveDIs] Prefer iterators over inst-pointers in InstCombine
As per my proposal for how to eliminate debug intrinsics [0], for various
places in InstCombine prefer to insert using an instruction iterator rather
than an instruction pointer. This is so that we can eventually pass more
information in the iterator class. These call-sites where I've changed the
spelling are those that necessary to build a stage2clang to produce an
identical binary in the coming no-debug-intrinsics mode.

[0] https://discourse.llvm.org/t/rfc-instruction-api-changes-needed-to-eliminate-debug-intrinsics-from-ir/68939

Differential Revision: https://reviews.llvm.org/D152543
2023-09-11 15:04:51 +01:00
Nikita Popov
b5c2a19fed [InstCombine] Remove some typed pointer handling (NFC)
This bitcast is no longer needed.
2023-08-09 16:03:57 +02:00
Matt Arsenault
30fff9480a InstCombine: Don't bother skipping over pointer bitcasts
These don't really happen with opaque pointers.
2023-08-02 13:03:47 -04:00
Nikita Popov
c5592f7acd [InstCombine] Fix use after free when removing unreachable code (PR64235)
In degenerate cases, it is possible for unreachable code removal
to remove the current instruction. However, we still return the
instruction to report a change, resulting in a use after free.

Instead, perform the change reporting in the same way as
eraseInstFromFunction() does, by directly setting MadeIRChange
and returning nullptr.

Fixes https://github.com/llvm/llvm-project/issues/64235.
2023-08-01 10:19:27 +02:00
Matt Arsenault
f9b2cbfffa InstCombine: Drop some typed pointer handling 2023-07-31 16:55:16 -04:00
Matt Arsenault
fbeda975d2 InstCombine: Drop some typed pointer cast handling 2023-07-31 10:34:31 -04:00
Nikita Popov
70aca7b122 [InstCombine] Explicitly track dead edges
This allows us to handle dead blocks with multiple incoming edges,
where we can determine that all of those edges are dead (or cycles).

This allows InstCombine to handle certain dead code patterns that
can be produced by LoopVectorize in a single iteration.

This is in preparation for D154579.
2023-07-27 16:41:03 +02:00
Nikita Popov
7be7f23269 [llvm] Remove uses of getWithSamePointeeType() (NFC) 2023-07-18 12:07:09 +02:00
Nikita Popov
252d1c48c4 [InstCombine] Remove instructions before non-terminator unreachable
Treat non-terminator unreachable the same as unreachable, and
remove guaranteed-to-transfer instructions before it.
2023-06-22 16:33:53 +02:00
Nikita Popov
10451ded6d [InstCombine] Remove code after non-terminator unreachable
Instruction after a non-terminator unreachable are ... unreachable,
so remove them. Reuse the same logic we use for removing
instructions from dead blocks.
2023-06-22 15:59:10 +02:00
Nikita Popov
8aefa2bf39 [InstCombine] Don't remove non-terminator unreachable markers
Even if the value happens to be undef, we should preserve these so
they get turned into an unreachable terminator later.
2023-06-22 15:52:52 +02:00
Nikita Popov
c01054cc47 [InstCombine] Use CreateNonTerminatorUnreachable() helper
Create the standard non-terminator unreachable, rather than a
slight variation on it.
2023-06-22 12:30:13 +02:00
Nikita Popov
089157518a [InstCombine] Fix worklist management in replaceGEPIdxWithZero() fold (NFCI)
Make sure the old load/store operand is queued for DCE.

This should be NFC apart from worklist order effects.
2023-05-23 16:21:43 +02:00
Anshil Gandhi
5d3412a6d1 [InstCombine] Insert a bitcast to enable merging similar store insts
Given two Store instructions with equivalent pointer operands,
they could be merged into their common successor basic block if
the value operand of one is bitcasted to match the type of the
other.

Differential Revision: https://reviews.llvm.org/D150900
2023-05-22 07:36:45 -07:00
eopXD
c8eb535aed [1/11][IR] Permit load/store/alloca for struct of the same scalable vector type
This patch-set aims to simplify the existing RVV segment load/store
intrinsics to use a type that represents a tuple of vectors instead.

To achieve this, first we need to relax the current limitation for an
aggregate type to be a target of load/store/alloca when the aggregate
type contains homogeneous scalable vector types. Then to adjust the
prolog of an LLVM function during lowering to clang. Finally we
re-define the RVV segment load/store intrinsics to use the tuple types.

The pull request under the RVV intrinsic specification is
riscv-non-isa/rvv-intrinsic-doc#198

---

This is the 1st patch of the patch-set. This patch is originated from
D98169.

This patch allows aggregate type (StructType) that contains homogeneous
scalable vector types to be a target of load/store/alloca. The RFC of
this patch was posted in LLVM Discourse.

https://discourse.llvm.org/t/rfc-ir-permit-load-store-alloca-for-struct-of-the-same-scalable-vector-type/69527

The main changes in this patch are:

Extend `StructLayout::StructSize` from `uint64_t` to `TypeSize` to
accommodate an expression of scalable size.

Allow `StructType:isSized` to also return true for homogeneous
scalable vector types.

Let `Type::isScalableTy` return true when `Type` is `StructType`
and contains scalable vectors

Extra description is added in the LLVM Language Reference Manual on the
relaxation of this patch.

Authored-by: Hsiangkai Wang <kai.wang@sifive.com>
Co-Authored-by: eop Chen <eop.chen@sifive.com>

Reviewed By: craig.topper, nikic

Differential Revision: https://reviews.llvm.org/D146872
2023-05-19 09:39:36 -07:00
Nikita Popov
6170bb8959 [InstCombine] Create store using IRBuilder
This ensures it gets reprocessed in the same iteration. In
particular the alignment will be increased (which is quite
pointless, of course).
2023-05-17 15:20:21 +02:00
Michael Liao
72fc08a541 [InstCombine] Teach alloca replacement to handle addrspacecast
- As the address space cast may not be valid on a specific target,
  `addrspacecast` is not handled when an `alloca` is able to be replaced
  with the source of memcpy/memmove. This patch addresses that by
  querying a target hook on whether that address space cast is valid.
  For example, on most GPU targets, the cast from a global pointer to a
  generic pointer is valid.
- If that cast is allowedd (by querying `isValidAddrSpaceCast`), the
  replacement is enhanced to handle that `addrspacecast` as well.

Reviewed By: yaxunl

Differential Revision: https://reviews.llvm.org/D147025
2023-04-11 11:47:37 -04:00
Nikita Popov
503ef0a8e7 [InstCombine] Remove addrspacecast bitcast extraction fold (NFC)
This is not relevant for opaque pointers, and as such no longer
necessary.
2023-04-06 09:53:32 +02:00
Krzysztof Drewniak
916425b2d1 [llvm] Use pointer index type for more GEP offsets (pre-codegen)
Many uses of getIntPtrType() were using that type to calculate the
neened type for GEP offset arguments. However, some time ago,
DataLayout was extended to support pointers where the size of the
pointer is not equal to the size of the values used to index it.

Much code was already migrated to, for example, use getIndexSizeInBits
instead of getPtrSizeInBits, but some rewrites still used
getIntPtrType() to get the type for GEP offsets.

This commit changes uses of getIntPtrType() to getIndexType() where
they are involved in a GEP-related calculation.

In at least one case (bounds check insertion) this resolves a compiler
crash that the new test added here would previously trigger.

This commit does not impact
- C library-related rewriting (memcpy()), which are operating under
the assumption that intptr_t == size_t. While all the mechanisms for
breaking this assumption now exist, doing so is outside the scope of
this commit.
- Code generation and below. Note that the use of getIntPtrType() in
CodeGenPrepare will be changed in a future commit.
- Usage of getIntPtrType() in any backend

Depends on D143435

Reviewed By: arichardson

Differential Revision: https://reviews.llvm.org/D143437
2023-03-28 16:41:02 +00:00
Nikita Popov
be88b5814d [InstCombine] Call simplifyLoadInst()
InstCombine is supposed to be a superset of InstSimplify, but
failed to invoke load simplification.

Unfortunately, this causes a minor compile-time regression, which
will be mitigated in a future commit.
2023-02-20 10:49:44 +01:00
Brendon Cahoon
20cdf7c706 [InstCombine] Increase limit for max copied from constant fold
Increasing the limits fixes several performance regressions.

Differential revision: https://reviews.llvm.org/D144032
2023-02-14 16:04:14 -06:00
Anshil Gandhi
ddc484b8ea [InstCombine] Handle select inst when eliminating constant memcpy
Allow iterating through SelectInst use of the alloca when
checking if it is only ever overwritten from constant memory.
Recursively determine if the SelectInst is replacable and insert
it into the Worklist if so. Finally, define a new SelectInst to
replace the old one, with both of it's values replaced according
to the WorkMap.

Differential Revision: https://reviews.llvm.org/D136524
2023-01-23 12:02:57 -07:00
Nikita Popov
ed9806363b [InstCombine] Make worklist check in memcpy from constant fold more precise
The phi operands need to be either in the worklist or be the
alloca itself, because that one does not require replacement.
2023-01-23 14:15:41 +01:00
Anshil Gandhi
2449cbabdd [InstCombine] Handle PHI nodes in PtrReplacer
This patch adds on to the functionality implemented
in rG42ab5dc5a5dd6c79476104bdc921afa2a18559cf,
where PHI nodes are supported in the use-def traversal
algorithm to determine if an alloca ever overwritten
in addition to a memmove/memcpy. This patch implements
the support needed by the PointerReplacer to collect
all (indirect) users of the alloca in cases where a PHI
is involved. Finally, a new PHI is defined in the replace
method which takes in replaced incoming values and
updates the WorkMap accordingly.

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D136201
2023-01-17 10:56:03 -07:00
Guillaume Chatelet
8fd5558b29 [NFC] Use TypeSize::geFixedValue() instead of TypeSize::getFixedSize()
This change is one of a series to implement the discussion from
https://reviews.llvm.org/D141134.
2023-01-11 16:49:38 +00:00
Guillaume Chatelet
48f5d77eee [NFC] Use TypeSize::getKnownMinValue() instead of TypeSize::getKnownMinSize()
This change is one of a series to implement the discussion from
https://reviews.llvm.org/D141134.
2023-01-11 16:36:39 +00:00
Anshil Gandhi
42ab5dc5a5 [InstCombine] Handle PHI nodes in isOnlyCopiedFromConstantMemory()
As long as the memcpy occurs on a phi input (rather than the phi
output), we can look through phi nodes in
isOnlyCopiedFromConstantMemory().

This is split out of D136201, to only handle the case where the
address spaces are the same, and no pointer rewrite is necessary.
2023-01-11 17:24:41 +01:00
Nikita Popov
89996621de [InstCombine] Limit use walk in copied from constant fold
This fold currently performs an unbounded recursive use walk.
Make sure that we don't visit too many instructions (the limit is
chosen arbitrarily).

This is with an eye on also handling phi nodes, which will further
extend the considered use graph.
2023-01-11 17:17:46 +01:00
Nikita Popov
0e6deaa2a0 [InstCombine] Add Visited set to isOnlyCopiedFromConstantMemory()
I don't think this matters right now (because InstCombine cleans
up unreachable code early), but this will help to make sure that
we don't infinite loop once we handle phi nodes. The added test
is an example where this would happen.
2023-01-11 17:12:26 +01:00
serge-sans-paille
38818b60c5
Move from llvm::makeArrayRef to ArrayRef deduction guides - llvm/ part
Use deduction guides instead of helper functions.

The only non-automatic changes have been:

1. ArrayRef(some_uint8_pointer, 0) needs to be changed into ArrayRef(some_uint8_pointer, (size_t)0) to avoid an ambiguous call with ArrayRef((uint8_t*), (uint8_t*))
2. CVSymbol sym(makeArrayRef(symStorage)); needed to be rewritten as CVSymbol sym{ArrayRef(symStorage)}; otherwise the compiler is confused and thinks we have a (bad) function prototype. There was a few similar situation across the codebase.
3. ADL doesn't seem to work the same for deduction-guides and functions, so at some point the llvm namespace must be explicitly stated.
4. The "reference mode" of makeArrayRef(ArrayRef<T> &) that acts as no-op is not supported (a constructor cannot achieve that).

Per reviewers' comment, some useless makeArrayRef have been removed in the process.

This is a follow-up to https://reviews.llvm.org/D140896 that introduced
the deduction guides.

Differential Revision: https://reviews.llvm.org/D140955
2023-01-05 14:11:08 +01:00
Benjamin Kramer
856f7937c7 Compress a few pairs using PointerIntPairs
Use the uniform structured bindings interface where possible. NFCI.
2022-12-04 16:55:16 +01:00
Sanjay Patel
a00936484b [InstCombine] improve readability of combineLoadToOperationType(); NFC 2022-11-28 16:00:06 -05:00