LLVM 20.0.0git
SSAContext.cpp
Go to the documentation of this file.
1//===- SSAContext.cpp -------------------------------------------*- 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 a specialization of the GenericSSAContext<X>
11/// template class for LLVM IR.
12///
13//===----------------------------------------------------------------------===//
14
15#include "llvm/IR/SSAContext.h"
16#include "llvm/IR/Argument.h"
17#include "llvm/IR/BasicBlock.h"
18#include "llvm/IR/Function.h"
20#include "llvm/IR/Intrinsics.h"
23
24using namespace llvm;
25
26template <>
29 for (auto &instr : block) {
30 if (instr.isTerminator())
31 break;
32 defs.push_back(&instr);
33 }
34}
35
36template <>
38 const BasicBlock &block) {
39 for (auto &instr : block) {
40 if (instr.isTerminator())
41 break;
42 defs.push_back(&instr);
43 }
44}
45
46template <>
49 terms.push_back(block.getTerminator());
50}
51
52template <>
54 const BasicBlock &block) {
55 terms.push_back(block.getTerminator());
56}
57
58template <>
59const BasicBlock *SSAContext::getDefBlock(const Value *value) const {
60 if (const auto *instruction = dyn_cast<Instruction>(value))
61 return instruction->getParent();
62 return nullptr;
63}
64
65template <>
67 if (auto *Phi = dyn_cast<PHINode>(&Instr))
68 return Phi->hasConstantOrUndefValue();
69 return false;
70}
71
73 if (auto *CB = dyn_cast<CallBase>(&I))
74 return CB->getIntrinsicID();
76}
77
78template <> Printable SSAContext::print(const Value *V) const {
79 return Printable([V](raw_ostream &Out) { V->print(Out); });
80}
81
82template <> Printable SSAContext::print(const Instruction *Inst) const {
83 return print(cast<Value>(Inst));
84}
85
86template <> Printable SSAContext::print(const BasicBlock *BB) const {
87 if (!BB)
88 return Printable([](raw_ostream &Out) { Out << "<nullptr>"; });
89 if (BB->hasName())
90 return Printable([BB](raw_ostream &Out) { Out << BB->getName(); });
91
92 return Printable([BB](raw_ostream &Out) {
93 ModuleSlotTracker MST{BB->getParent()->getParent(), false};
94 MST.incorporateFunction(*BB->getParent());
95 Out << MST.getLocalSlot(BB);
96 });
97}
98
99template <> Printable SSAContext::printAsOperand(const BasicBlock *BB) const {
100 return Printable([BB](raw_ostream &Out) { BB->printAsOperand(Out); });
101}
static const Function * getParent(const Value *V)
Given that RA is a live value
#define I(x, y, z)
Definition: MD5.cpp:58
This file declares a specialization of the GenericSSAContext<X> class template for LLVM IR.
unify loop Fixup each natural loop to have a single exit block
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:219
static void appendBlockDefs(SmallVectorImpl< ValueRefT > &defs, BlockT &block)
static bool isConstantOrUndefValuePhi(const InstructionT &Instr)
static void appendBlockTerms(SmallVectorImpl< InstructionT * > &terms, BlockT &block)
static Intrinsic::ID getIntrinsicID(const InstructionT &I)
Printable print(const BlockT *block) const
const BlockT * getDefBlock(ConstValueRefT value) const
Printable printAsOperand(const BlockT *BB) const
Manage lifetime of a slot tracker for printing IR.
Simple wrapper around std::function<void(raw_ostream&)>.
Definition: Printable.h:38
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void push_back(const T &Elt)
Definition: SmallVector.h:426
LLVM Value Representation.
Definition: Value.h:74
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition: AsmWriter.cpp:5106
bool hasName() const
Definition: Value.h:261
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
MCInst const & instruction(MCInst const &MCB, size_t Index)
NodeAddr< PhiNode * > Phi
Definition: RDFGraph.h:390
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18