summaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm18/include/llvm/ADT/GenericSSAContext.h
blob: 4566e94fb0e73501d8994c23c360892ff3130dac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif

//===- GenericSSAContext.h --------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file defines the little GenericSSAContext<X> template class
/// that can be used to implement IR analyses as templates.
/// Specializing these templates allows the analyses to be used over
/// both LLVM IR and Machine IR.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_ADT_GENERICSSACONTEXT_H
#define LLVM_ADT_GENERICSSACONTEXT_H

#include "llvm/Support/Printable.h"

namespace llvm {

template <typename, bool> class DominatorTreeBase;
template <typename> class SmallVectorImpl;

namespace Intrinsic {
typedef unsigned ID;
}

// Specializations of this template should provide the types used by the
// template GenericSSAContext below.
template <typename _FunctionT> struct GenericSSATraits;

// Ideally this should have been a stateless traits class. But the print methods
// for Machine IR need access to the owning function. So we track that state in
// the template itself.
//
// We use FunctionT as a template argument and not GenericSSATraits to allow
// forward declarations using well-known typenames.
template <typename _FunctionT> class GenericSSAContext {
  using SSATraits = GenericSSATraits<_FunctionT>;
  const typename SSATraits::FunctionT *F;

public:
  // The smallest unit of the IR is a ValueT. The SSA context uses a ValueRefT,
  // which is a pointer to a ValueT, since Machine IR does not have the
  // equivalent of a ValueT.
  using ValueRefT = typename SSATraits::ValueRefT;

  // The ConstValueRefT is needed to work with "const Value *", where const
  // needs to bind to the pointee and not the pointer.
  using ConstValueRefT = typename SSATraits::ConstValueRefT;

  // The null value for ValueRefT. For LLVM IR and MIR, this is simply the
  // default constructed value.
  static constexpr ValueRefT *ValueRefNull = {};

  // An InstructionT usually defines one or more ValueT objects.
  using InstructionT = typename SSATraits::InstructionT;

  // A UseT represents a data-edge from the defining instruction to the using
  // instruction.
  using UseT = typename SSATraits::UseT;

  // A BlockT is a sequence of InstructionT, and forms a node of the CFG. It
  // has global methods predecessors() and successors() that return
  // the list of incoming CFG edges and outgoing CFG edges
  // respectively.
  using BlockT = typename SSATraits::BlockT;

  // A FunctionT represents a CFG along with arguments and return values. It is
  // the smallest complete unit of code in a Module.
  using FunctionT = typename SSATraits::FunctionT;

  // A dominator tree provides the dominance relation between basic blocks in
  // a given funciton.
  using DominatorTreeT = DominatorTreeBase<BlockT, false>;

  GenericSSAContext() = default;
  GenericSSAContext(const FunctionT *F) : F(F) {}

  const FunctionT *getFunction() const { return F; }

  static Intrinsic::ID getIntrinsicID(const InstructionT &I);

  static void appendBlockDefs(SmallVectorImpl<ValueRefT> &defs, BlockT &block);
  static void appendBlockDefs(SmallVectorImpl<ConstValueRefT> &defs,
                              const BlockT &block);

  static void appendBlockTerms(SmallVectorImpl<InstructionT *> &terms,
                               BlockT &block);
  static void appendBlockTerms(SmallVectorImpl<const InstructionT *> &terms,
                               const BlockT &block);

  static bool isConstantOrUndefValuePhi(const InstructionT &Instr);
  const BlockT *getDefBlock(ConstValueRefT value) const;

  Printable print(const BlockT *block) const;
  Printable printAsOperand(const BlockT *BB) const;
  Printable print(const InstructionT *inst) const;
  Printable print(ConstValueRefT value) const;
};
} // namespace llvm

#endif // LLVM_ADT_GENERICSSACONTEXT_H

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif