FLANG
AliasAnalysis.h
1//===-- AliasAnalysis.h - Alias Analysis in FIR -----------------*- 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 FORTRAN_OPTIMIZER_ANALYSIS_ALIASANALYSIS_H
10#define FORTRAN_OPTIMIZER_ANALYSIS_ALIASANALYSIS_H
11
12#include "flang/Common/enum-class.h"
13#include "flang/Common/enum-set.h"
14#include "mlir/Analysis/AliasAnalysis.h"
15#include "mlir/IR/BuiltinAttributes.h"
16#include "mlir/IR/Dominance.h"
17#include "mlir/IR/SymbolTable.h"
18#include "mlir/IR/Value.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/PointerUnion.h"
21#include "llvm/ADT/SmallVector.h"
22#include <memory>
23
24namespace fir {
25
26//===----------------------------------------------------------------------===//
27// AliasAnalysis
28//===----------------------------------------------------------------------===//
30 // Structures to describe the memory source of a value.
31
33 ENUM_CLASS(SourceKind,
36 Allocate,
38 Global,
41 Argument,
44 HostAssoc,
48 Indirect,
51 Unknown);
52
54 ENUM_CLASS(Attribute, Target, Pointer, IntentIn, CrayPointer, CrayPointee);
55
56 // See
57 // https://discourse.llvm.org/t/rfc-distinguish-between-data-and-non-data-in-fir-alias-analysis/78759/1
58 //
59 // It is possible, while following the source of a memory reference through
60 // the use-def chain, to arrive at the same origin, even though the starting
61 // points were known to not alias.
62 //
63 // clang-format off
64 // Example:
65 // ------------------- test.f90 --------------------
66 // module top
67 // real, pointer :: a(:)
68 // end module
69 //
70 // subroutine test()
71 // use top
72 // a(1) = 1
73 // end subroutine
74 // -------------------------------------------------
75 //
76 // flang -fc1 -emit-fir test.f90 -o test.fir
77 //
78 // ------------------- test.fir --------------------
79 // fir.global @_QMtopEa : !fir.box<!fir.ptr<!fir.array<?xf32>>>
80 //
81 // func.func @_QPtest() {
82 // %c1 = arith.constant 1 : index
83 // %cst = arith.constant 1.000000e+00 : f32
84 // %0 = fir.address_of(@_QMtopEa) : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
85 // %1 = fir.declare %0 {fortran_attrs = #fir.var_attrs<pointer>, uniq_name = "_QMtopEa"} : (!fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>) -> !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
86 // %2 = fir.load %1 : !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>>
87 // ...
88 // %5 = fir.array_coor %2 %c1 : (!fir.box<!fir.ptr<!fir.array<?xf32>>>, !fir.shift<1>, index) -> !fir.ref<f32>
89 // fir.store %cst to %5 : !fir.ref<f32>
90 // return
91 // }
92 // -------------------------------------------------
93 //
94 // With high level operations, such as fir.array_coor, it is possible to
95 // reach into the data wrapped by the box (the descriptor). Therefore when
96 // asking about the memory source of %5, we are really asking about the
97 // source of the data of box %2.
98 //
99 // When asking about the source of %0 which is the address of the box, we
100 // reach the same source as in the first case: the global @_QMtopEa. Yet one
101 // source refers to the data while the other refers to the address of the box
102 // itself.
103 //
104 // To distinguish between the two, the isData flag has been added, whereby
105 // data is defined as any memory reference that is not a box reference.
106 // Additionally, because it is relied on in HLFIR lowering, we allow querying
107 // on a box SSA value, which is interpreted as querying on its data.
108 //
109 // So in the above example, !fir.ref<f32> and !fir.box<!fir.ptr<!fir.array<?xf32>>> is data,
110 // while !fir.ref<!fir.box<!fir.ptr<!fir.array<?xf32>>>> is not data.
111
112 // This also applies to function arguments. In the example below, %arg0
113 // is data, %arg1 is not data but a load of %arg1 is.
114 //
115 // func.func @_QFPtest2(%arg0: !fir.ref<f32>, %arg1: !fir.ref<!fir.box<!fir.ptr<f32>>> ) {
116 // %0 = fir.load %arg1 : !fir.ref<!fir.box<!fir.ptr<f32>>>
117 // ... }
118 //
119 // clang-format on
120
121 struct Source {
122 using SourceUnion = llvm::PointerUnion<mlir::SymbolRefAttr, mlir::Value>;
124
127 SourceUnion u;
128
138 mlir::Operation *instantiationPoint;
139
141 bool isData{false};
142 };
143
147 struct PathStep {
157 Kind kind;
161 mlir::StringAttr component;
162
163 bool operator==(const PathStep &o) const {
164 return kind == o.kind && component == o.component;
165 }
166 bool operator!=(const PathStep &o) const { return !(*this == o); }
167 };
168
180 struct AccessPath {
182
186 bool isApproximate{false};
187
189 bool hasPointerDeref() const {
190 return llvm::any_of(steps, [](const PathStep &s) {
191 return s.kind == PathStep::Kind::PointerDeref;
192 });
193 }
194
195 bool operator==(const AccessPath &o) const {
196 return isApproximate == o.isApproximate && steps == o.steps;
197 }
198 bool operator!=(const AccessPath &o) const { return !(*this == o); }
199
200 void print(llvm::raw_ostream &os) const;
201 };
202
238 mlir::Value scope;
240 mlir::Value declValue;
247 Attributes attributes;
249 bool approximateSource{false};
252 bool isData{false};
253 };
254
255 SourceOrigin origin;
256
258 SourceKind kind;
260 mlir::Type valueType;
262 Attributes attributes;
275
277 void print(llvm::raw_ostream &os) const;
278
280 bool isTargetOrPointer() const;
281
283 bool isTarget() const;
284
286 bool isPointer() const;
287
289 bool isCrayPointer() const;
290
292 bool isCrayPointee() const;
293
295 bool isCrayPointerOrPointee() const;
296
297 bool isDummyArgument() const;
298 bool isData() const;
299 bool isBoxData() const;
300
302 bool isFortranUserVariable() const;
303
310
314 bool mayBeDummyArgOrHostAssoc() const;
316 bool mayBePtrDummyArgOrHostAssoc() const;
318 bool mayBeActualArg() const;
321 bool mayBeActualArgWithPtr(const mlir::Value *val) const;
323
324 mlir::Type getType() const;
325 };
326
327 friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
328 const AliasAnalysis::Source &op);
329
331 mlir::AliasResult alias(Source lhsSrc, Source rhsSrc, mlir::Value lhs,
332 mlir::Value rhs);
333
335 mlir::AliasResult alias(mlir::Value lhs, mlir::Value rhs);
336
338 mlir::ModRefResult getModRef(mlir::Operation *op, mlir::Value location);
339
344 mlir::ModRefResult getModRef(mlir::Region &region, mlir::Value location);
345
355 bool getLastInstantiationPoint = false,
356 bool collectScopedOrigins = true);
357
360 static bool isPointerReference(mlir::Type ty);
361
362private:
369 Source buildSourceAtDeclare(const Source::ScopedOrigin &so);
370
376 mlir::Value getDeclarationScope(mlir::Operation *declareOp);
377
380 static bool isRecordWithPointerComponent(mlir::Type ty);
381
385 const mlir::SymbolTable *getNearestSymbolTable(mlir::Operation *from);
386
390 bool symbolMayHaveTargetAttr(mlir::SymbolRefAttr symbol,
391 mlir::Operation *from);
392
395 bool isCallToFortranUserProcedure(mlir::Operation *op);
396
400 mlir::ModRefResult getCallModRef(mlir::Operation *op, mlir::Value var);
401
415 llvm::DenseMap<mlir::Operation *, mlir::SymbolTable> symTabMap;
416
430 llvm::DenseMap<mlir::Operation *, std::unique_ptr<mlir::DominanceInfo>>
431 domInfoCache;
432 llvm::DenseMap<mlir::Operation *, llvm::SmallVector<mlir::Operation *, 16>>
433 sortedScopeCache;
434};
435
436inline bool operator==(const AliasAnalysis::Source::SourceOrigin &lhs,
438 return lhs.u == rhs.u && lhs.isData == rhs.isData;
439}
440inline bool operator!=(const AliasAnalysis::Source::SourceOrigin &lhs,
441 const AliasAnalysis::Source::SourceOrigin &rhs) {
442 return !(lhs == rhs);
443}
444
445inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
446 const AliasAnalysis::Source &op) {
447 op.print(os);
448 return os;
449}
450
451} // namespace fir
452
453#endif // FORTRAN_OPTIMIZER_ANALYSIS_ALIASANALYSIS_H
Definition enum-set.h:28
Definition OpenACC.h:20
Definition AbstractConverter.h:37
Definition AliasAnalysis.h:180
bool hasPointerDeref() const
Return true if any step is a PointerDeref.
Definition AliasAnalysis.h:189
bool isApproximate
Definition AliasAnalysis.h:186
Definition AliasAnalysis.h:147
mlir::StringAttr component
Definition AliasAnalysis.h:161
Kind
Definition AliasAnalysis.h:148
@ Component
Named component access, e.g. xfield.
Definition AliasAnalysis.h:150
@ AllocDeref
Loading an ALLOCATABLE box (fir.load of !fir.box<!fir.heap<...>>).
Definition AliasAnalysis.h:155
@ PointerDeref
Definition AliasAnalysis.h:153
Definition AliasAnalysis.h:234
mlir::Value scope
Definition AliasAnalysis.h:238
AccessPath accessPath
Definition AliasAnalysis.h:243
bool isData
Definition AliasAnalysis.h:252
mlir::Value declValue
Result SSA value of the [hl]fir.declare op.
Definition AliasAnalysis.h:240
bool approximateSource
Whether the path is approximate at the moment of the snapshot.
Definition AliasAnalysis.h:249
Attributes attributes
Definition AliasAnalysis.h:247
Definition AliasAnalysis.h:125
mlir::Operation * instantiationPoint
Definition AliasAnalysis.h:138
SourceUnion u
Source definition of a value.
Definition AliasAnalysis.h:127
bool isData
Whether the source was reached following data or box reference.
Definition AliasAnalysis.h:141
Definition AliasAnalysis.h:121
bool isCrayPointer() const
Return true, if CrayPointer attribute is set.
Definition AliasAnalysis.cpp:434
bool isCapturedInInternalProcedure
Source object is used in an internal procedure via host association.
Definition AliasAnalysis.h:269
bool isFortranUserVariable() const
Is this source a variable from the Fortran source?
Definition AliasAnalysis.cpp:455
Attributes attributes
Attributes of the memory source object, e.g. Target.
Definition AliasAnalysis.h:262
bool isPointer() const
Return true, if Pointer attribute is set.
Definition AliasAnalysis.cpp:426
llvm::SmallVector< ScopedOrigin, 4 > scopedOrigins
Definition AliasAnalysis.h:274
SourceKind kind
Kind of the memory source.
Definition AliasAnalysis.h:258
bool isCrayPointee() const
Return true, if CrayPointee attribute is set.
Definition AliasAnalysis.cpp:430
void print(llvm::raw_ostream &os) const
Print information about the memory source to os.
Definition AliasAnalysis.cpp:383
bool mayBePtrDummyArgOrHostAssoc() const
mayBeDummyArgOrHostAssoc and the address of a pointer?
Definition AliasAnalysis.cpp:470
bool isTargetOrPointer() const
Return true, if Target or Pointer attribute is set.
Definition AliasAnalysis.cpp:417
bool mayBeDummyArgOrHostAssoc() const
Definition AliasAnalysis.cpp:466
AccessPath accessPath
The structured access path from the root variable.
Definition AliasAnalysis.h:267
bool approximateSource
Definition AliasAnalysis.h:265
bool mayBeActualArgWithPtr(const mlir::Value *val) const
Definition AliasAnalysis.cpp:485
mlir::Type valueType
Value type of the source definition.
Definition AliasAnalysis.h:260
bool isCrayPointerOrPointee() const
Return true, if CrayPointer or CrayPointee attribute is set.
Definition AliasAnalysis.cpp:438
bool isTarget() const
Return true, if Target attribute is set.
Definition AliasAnalysis.cpp:422
bool mayBeActualArg() const
The address of an actual argument of the current function?
Definition AliasAnalysis.cpp:481
Definition AliasAnalysis.h:29
fir::AliasAnalysis::Source getSource(mlir::Value, bool getLastInstantiationPoint=false, bool collectScopedOrigins=true)
Definition AliasAnalysis.cpp:1098
mlir::AliasResult alias(Source lhsSrc, Source rhsSrc, mlir::Value lhs, mlir::Value rhs)
Given the values and their sources, return their aliasing behavior.
Definition AliasAnalysis.cpp:646
static bool isPointerReference(mlir::Type ty)
Definition AliasAnalysis.cpp:409
ENUM_CLASS(Attribute, Target, Pointer, IntentIn, CrayPointer, CrayPointee)
Attributes of the memory source object.
ENUM_CLASS(SourceKind, Allocate, Global, Argument, HostAssoc, Indirect, Unknown)
Kind of the memory source referenced by a value.
mlir::ModRefResult getModRef(mlir::Operation *op, mlir::Value location)
Return the modify-reference behavior of op on location.