1//===- GenericSSAContext.h --------------------------------------*- 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/// \file
9///
10/// This file defines the little GenericSSAContext<X> template class
11/// that can be used to implement IR analyses as templates.
12/// Specializing these templates allows the analyses to be used over
13/// both LLVM IR and Machine IR.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_ADT_GENERICSSACONTEXT_H
18#define LLVM_ADT_GENERICSSACONTEXT_H
19
20#include "llvm/Support/Printable.h"
21
22namespace llvm {
23
24template <typename, bool> class DominatorTreeBase;
25template <typename> class SmallVectorImpl;
26
27namespace Intrinsic {
28typedef unsigned ID;
29}
30
31// Specializations of this template should provide the types used by the
32// template GenericSSAContext below.
33template <typename _FunctionT> struct GenericSSATraits;
34
35// Ideally this should have been a stateless traits class. But the print methods
36// for Machine IR need access to the owning function. So we track that state in
37// the template itself.
38//
39// We use FunctionT as a template argument and not GenericSSATraits to allow
40// forward declarations using well-known typenames.
41template <typename _FunctionT> class GenericSSAContext {
42 using SSATraits = GenericSSATraits<_FunctionT>;
43 const typename SSATraits::FunctionT *F;
44
45public:
46 // The smallest unit of the IR is a ValueT. The SSA context uses a ValueRefT,
47 // which is a pointer to a ValueT, since Machine IR does not have the
48 // equivalent of a ValueT.
49 using ValueRefT = typename SSATraits::ValueRefT;
50
51 // The ConstValueRefT is needed to work with "const Value *", where const
52 // needs to bind to the pointee and not the pointer.
53 using ConstValueRefT = typename SSATraits::ConstValueRefT;
54
55 // The null value for ValueRefT. For LLVM IR and MIR, this is simply the
56 // default constructed value.
57 static constexpr ValueRefT *ValueRefNull = {};
58
59 // An InstructionT usually defines one or more ValueT objects.
60 using InstructionT = typename SSATraits::InstructionT;
61
62 // A UseT represents a data-edge from the defining instruction to the using
63 // instruction.
64 using UseT = typename SSATraits::UseT;
65
66 // A BlockT is a sequence of InstructionT, and forms a node of the CFG. It
67 // has global methods predecessors() and successors() that return
68 // the list of incoming CFG edges and outgoing CFG edges
69 // respectively.
70 using BlockT = typename SSATraits::BlockT;
71
72 // A FunctionT represents a CFG along with arguments and return values. It is
73 // the smallest complete unit of code in a Module.
74 using FunctionT = typename SSATraits::FunctionT;
75
76 // A dominator tree provides the dominance relation between basic blocks in
77 // a given funciton.
78 using DominatorTreeT = DominatorTreeBase<BlockT, false>;
79
80 GenericSSAContext() = default;
81 GenericSSAContext(const FunctionT *F) : F(F) {}
82
83 const FunctionT *getFunction() const { return F; }
84
85 static Intrinsic::ID getIntrinsicID(const InstructionT &I);
86
87 static void appendBlockDefs(SmallVectorImpl<ValueRefT> &defs, BlockT &block);
88 static void appendBlockDefs(SmallVectorImpl<ConstValueRefT> &defs,
89 const BlockT &block);
90
91 static void appendBlockTerms(SmallVectorImpl<InstructionT *> &terms,
92 BlockT &block);
93 static void appendBlockTerms(SmallVectorImpl<const InstructionT *> &terms,
94 const BlockT &block);
95
96 static bool isConstantOrUndefValuePhi(const InstructionT &Instr);
97 const BlockT *getDefBlock(ConstValueRefT value) const;
98
99 Printable print(const BlockT *block) const;
100 Printable printAsOperand(const BlockT *BB) const;
101 Printable print(const InstructionT *inst) const;
102 Printable print(ConstValueRefT value) const;
103};
104} // namespace llvm
105
106#endif // LLVM_ADT_GENERICSSACONTEXT_H
107

source code of llvm/include/llvm/ADT/GenericSSAContext.h