1 //===- SSAContext.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 declares a specialization of the GenericSSAContext<X>
11 /// class template for LLVM IR.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_SSACONTEXT_H
16 #define LLVM_IR_SSACONTEXT_H
17 
18 #include "llvm/ADT/GenericSSAContext.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/ModuleSlotTracker.h"
21 #include "llvm/Support/Printable.h"
22 
23 #include <memory>
24 
25 namespace llvm {
26 class BasicBlock;
27 class Function;
28 class Instruction;
29 class Value;
30 template <typename> class SmallVectorImpl;
31 template <typename, bool> class DominatorTreeBase;
32 
33 inline auto instrs(const BasicBlock &BB) {
34   return llvm::make_range(BB.begin(), BB.end());
35 }
36 
37 template <> class GenericSSAContext<Function> {
38   Function *F;
39 
40 public:
41   using BlockT = BasicBlock;
42   using FunctionT = Function;
43   using InstructionT = Instruction;
44   using ValueRefT = Value *;
45   using ConstValueRefT = const Value *;
46   using UseT = Use;
47   using DominatorTreeT = DominatorTreeBase<BlockT, false>;
48 
49   static constexpr Value *ValueRefNull = nullptr;
50 
51   void setFunction(Function &Fn);
52   Function *getFunction() const { return F; }
53 
54   static BasicBlock *getEntryBlock(Function &F);
55   static const BasicBlock *getEntryBlock(const Function &F);
56 
57   static void appendBlockDefs(SmallVectorImpl<Value *> &defs,
58                               BasicBlock &block);
59   static void appendBlockDefs(SmallVectorImpl<const Value *> &defs,
60                               const BasicBlock &block);
61 
62   static void appendBlockTerms(SmallVectorImpl<Instruction *> &terms,
63                                BasicBlock &block);
64   static void appendBlockTerms(SmallVectorImpl<const Instruction *> &terms,
65                                const BasicBlock &block);
66 
67   static bool comesBefore(const Instruction *lhs, const Instruction *rhs);
68   static bool isConstantOrUndefValuePhi(const Instruction &Instr);
69   const BasicBlock *getDefBlock(const Value *value) const;
70 
71   Printable print(const BasicBlock *Block) const;
72   Printable print(const Instruction *Inst) const;
73   Printable print(const Value *Value) const;
74 };
75 
76 using SSAContext = GenericSSAContext<Function>;
77 
78 } // namespace llvm
79 
80 #endif // LLVM_IR_SSACONTEXT_H
81