LLVM 24.0.0git
InstructionNamer.cpp
Go to the documentation of this file.
1//===- InstructionNamer.cpp - Give anonymous instructions names -----------===//
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//
9// This is a little utility pass that gives instructions names, this is mostly
10// useful when diffing the effect of an optimization because deleting an
11// unnamed instruction can change all other instruction numbering, making the
12// diff very noisy.
13//
14//===----------------------------------------------------------------------===//
15
17#include "llvm/ADT/Twine.h"
20#include "llvm/IR/Function.h"
21#include "llvm/IR/Module.h"
23#include "llvm/IR/PassManager.h"
24#include "llvm/IR/Type.h"
25#include <cstdint>
26#include <string>
27
28using namespace llvm;
29
30static void nameInstructions(Function &F, uint64_t *NextID = nullptr) {
31 auto getName = [NextID](StringRef Prefix) -> std::string {
32 if (!NextID)
33 return Prefix.str();
34 return (Twine(Prefix) + "." + Twine((*NextID)++)).str();
35 };
36
37 for (Argument &Arg : F.args()) {
38 if (!Arg.hasName())
39 Arg.setName(getName("arg"));
40 }
41
42 for (BasicBlock &BB : F) {
43 if (!BB.hasName())
44 BB.setName(getName("bb"));
45
46 for (Instruction &I : BB) {
47 if (!I.hasName() && !I.getType()->isVoidTy())
48 I.setName(getName("i"));
49 }
50 }
51}
52
58
59static void nameIRUnit(IRUnitRef IR, uint64_t &NextID) {
60 if (const auto *M = dyn_cast<Module>(IR)) {
61 for (Function &F : *const_cast<Module *>(M))
62 nameInstructions(F, &NextID);
63 } else if (const auto *F = dyn_cast<Function>(IR)) {
64 nameInstructions(*const_cast<Function *>(F), &NextID);
65 } else if (const auto *C = dyn_cast<LazyCallGraph::SCC>(IR)) {
66 for (const LazyCallGraph::Node &N : *C)
67 nameInstructions(N.getFunction(), &NextID);
68 } else if (const auto *L = dyn_cast<Loop>(IR)) {
69 nameInstructions(*L->getHeader()->getParent(), &NextID);
70 }
71}
72
74 uint64_t &NextID) {
75 // The symbol table only detects collisions with live values. Keep an ID
76 // across callbacks so deleting a value does not reuse its generated ID.
77 PIC.registerBeforeNonSkippedPassCallback(
78 [&NextID](StringRef, IRUnitRef IR) { nameIRUnit(IR, NextID); });
79 PIC.registerAfterPassCallback(
80 [&NextID](StringRef, IRUnitRef IR, const PreservedAnalyses &) {
81 nameIRUnit(IR, NextID);
82 });
83}
unsigned uint64_t
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
static void nameIRUnit(IRUnitRef IR, uint64_t &NextID)
static void nameInstructions(Function &F, uint64_t *NextID=nullptr)
Implements a lazy call graph analysis and related passes for the new pass manager.
Legalize the Machine IR a function s Machine IR
Definition Legalizer.cpp:85
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
FunctionAnalysisManager FAM
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
static StringRef getName(Value *V)
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM Basic Block Representation.
Definition BasicBlock.h:62
A type-erased reference to the IR unit a pass or analysis is running on, together with the kind of IR...
Definition IRUnitRef.h:60
A node in the call graph.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
#define N
static LLVM_ABI void registerCallbacks(PassInstrumentationCallbacks &PIC, uint64_t &NextID)
Register callbacks to name unnamed values around new-PM passes.
LLVM_ABI PreservedAnalyses run(Function &, FunctionAnalysisManager &)