LLVM 24.0.0git
SelectionDAGAddressAnalysis.h
Go to the documentation of this file.
1//===- SelectionDAGAddressAnalysis.h - DAG Address Analysis -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CODEGEN_SELECTIONDAGADDRESSANALYSIS_H
10#define LLVM_CODEGEN_SELECTIONDAGADDRESSANALYSIS_H
11
16#include <cstdint>
17
18namespace llvm {
19
20class SelectionDAG;
21
22/// Helper struct to parse and store a memory address as base + index + offset.
23/// We ignore sign extensions when it is safe to do so.
24/// The following two expressions are not equivalent. To differentiate we need
25/// to store whether there was a sign extension involved in the index
26/// computation.
27/// (load (i64 add (i64 copyfromreg %c)
28/// (i64 signextend (add (i8 load %index)
29/// (i8 1))))
30/// vs
31///
32/// (load (i64 add (i64 copyfromreg %c)
33/// (i64 signextend (i32 add (i32 signextend (i8 load %index))
34/// (i32 1)))))
36private:
37 SDValue Base;
38 SDValue Index;
39 std::optional<StackOffset> Offset;
40 bool IsIndexSignExt = false;
41
42public:
43 BaseIndexOffset() = default;
44 BaseIndexOffset(SDValue Base, SDValue Index, bool IsIndexSignExt)
45 : Base(Base), Index(Index), IsIndexSignExt(IsIndexSignExt) {}
46 BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset,
47 bool IsIndexSignExt)
48 : BaseIndexOffset(Base, Index, StackOffset::getFixed(Offset),
49 IsIndexSignExt) {}
51 bool IsIndexSignExt)
52 : Base(Base), Index(Index), Offset(Offset),
53 IsIndexSignExt(IsIndexSignExt) {}
54
55 SDValue getBase() { return Base; }
56 SDValue getBase() const { return Base; }
57 SDValue getIndex() { return Index; }
58 SDValue getIndex() const { return Index; }
59 void addToOffset(int64_t VectorOff) {
60 Offset = Offset.value_or(StackOffset::getFixed(0)) +
61 StackOffset::getFixed(VectorOff);
62 }
63 // Whether the parsed offset has only a fixed component.
64 bool hasValidOffset() const { return Offset && Offset->getScalable() == 0; }
65 // Whether a fixed or scalable offset was parsed.
66 bool hasValidStackOffset() const { return Offset.has_value(); }
67 int64_t getOffset() const {
68 assert(hasValidOffset() && "Expected a fixed offset");
69 return Offset->getFixed();
70 }
71
72 // Returns true if `Other` and `*this` are both some offset from the same base
73 // pointer. In that case, `Off` is set to the offset between `*this` and
74 // `Other` (negative if `Other` is before `*this`).
76 const SelectionDAG &DAG, StackOffset &Off) const;
77
78 // Like the above, but also allows a known fixed offset between the pointers.
80 const SelectionDAG &DAG, int64_t &Off) const;
81
83 const SelectionDAG &DAG) const {
85 return equalBaseIndex(Other, DAG, Off);
86 }
87
88 // Returns true if `Other` (with size `OtherSize`) can be proven to be fully
89 // contained in `*this` (with size `Size`).
90 LLVM_ABI bool contains(const SelectionDAG &DAG, int64_t BitSize,
91 const BaseIndexOffset &Other, int64_t OtherBitSize,
92 int64_t &BitOffset) const;
93
94 bool contains(const SelectionDAG &DAG, int64_t BitSize,
95 const BaseIndexOffset &Other, int64_t OtherBitSize) const {
96 int64_t BitOffset;
97 return contains(DAG, BitSize, Other, OtherBitSize, BitOffset);
98 }
99
100 // Returns true `Op0` and `Op1` can be proven to alias/not alias, in
101 // which case `IsAlias` is set to true/false.
102 LLVM_ABI static bool computeAliasing(const SDNode *Op0,
103 const LocationSize NumBytes0,
104 const SDNode *Op1,
105 const LocationSize NumBytes1,
106 const SelectionDAG &DAG, bool &IsAlias);
107
108 /// Parses tree in N for base, index, and fixed/scalable offsets.
109 LLVM_ABI static BaseIndexOffset match(const SDNode *N,
110 const SelectionDAG &DAG);
111
112 LLVM_ABI void print(raw_ostream &OS) const;
113 LLVM_ABI void dump() const;
114};
115
116} // end namespace llvm
117
118#endif // LLVM_CODEGEN_SELECTIONDAGADDRESSANALYSIS_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:211
This file provides utility analysis objects describing memory locations.
Helper struct to parse and store a memory address as base + index + offset.
LLVM_ABI bool equalBaseIndex(const BaseIndexOffset &Other, const SelectionDAG &DAG, StackOffset &Off) const
LLVM_ABI bool contains(const SelectionDAG &DAG, int64_t BitSize, const BaseIndexOffset &Other, int64_t OtherBitSize, int64_t &BitOffset) const
LLVM_ABI void print(raw_ostream &OS) const
void addToOffset(int64_t VectorOff)
BaseIndexOffset(SDValue Base, SDValue Index, int64_t Offset, bool IsIndexSignExt)
bool equalBaseIndex(const BaseIndexOffset &Other, const SelectionDAG &DAG) const
static LLVM_ABI BaseIndexOffset match(const SDNode *N, const SelectionDAG &DAG)
Parses tree in N for base, index, and fixed/scalable offsets.
bool contains(const SelectionDAG &DAG, int64_t BitSize, const BaseIndexOffset &Other, int64_t OtherBitSize) const
BaseIndexOffset(SDValue Base, SDValue Index, bool IsIndexSignExt)
static LLVM_ABI bool computeAliasing(const SDNode *Op0, const LocationSize NumBytes0, const SDNode *Op1, const LocationSize NumBytes1, const SelectionDAG &DAG, bool &IsAlias)
BaseIndexOffset(SDValue Base, SDValue Index, StackOffset Offset, bool IsIndexSignExt)
Represents one node in the SelectionDAG.
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
StackOffset holds a fixed and a scalable offset in bytes.
Definition TypeSize.h:31
int64_t getFixed() const
Returns the fixed component of the stack.
Definition TypeSize.h:47
static StackOffset getFixed(int64_t Fixed)
Definition TypeSize.h:40
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
@ Other
Any other memory.
Definition ModRef.h:68
#define N