xref: /freebsd/contrib/llvm-project/llvm/lib/IR/SSAContext.cpp (revision 643ac419fafba89f5adda0e0ea75b538727453fb)
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/BasicBlock.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Instruction.h"
19 #include "llvm/IR/ModuleSlotTracker.h"
20 #include "llvm/IR/Value.h"
21 #include "llvm/Support/raw_ostream.h"
22 
23 using namespace llvm;
24 
25 BasicBlock *SSAContext::getEntryBlock(Function &F) {
26   return &F.getEntryBlock();
27 }
28 
29 void SSAContext::setFunction(Function &Fn) { F = &Fn; }
30 
31 Printable SSAContext::print(Value *V) const {
32   return Printable([V](raw_ostream &Out) { V->print(Out); });
33 }
34 
35 Printable SSAContext::print(Instruction *Inst) const {
36   return print(cast<Value>(Inst));
37 }
38 
39 Printable SSAContext::print(BasicBlock *BB) const {
40   if (BB->hasName())
41     return Printable([BB](raw_ostream &Out) { Out << BB->getName(); });
42 
43   return Printable([BB](raw_ostream &Out) {
44     ModuleSlotTracker MST{BB->getParent()->getParent(), false};
45     MST.incorporateFunction(*BB->getParent());
46     Out << MST.getLocalSlot(BB);
47   });
48 }
49