LLVM 22.0.0git
DXILFinalizeLinkage.cpp
Go to the documentation of this file.
1//===- DXILFinalizeLinkage.cpp - Finalize linkage of functions ------------===//
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
10#include "DirectX.h"
11#include "llvm/IR/Function.h"
12#include "llvm/IR/GlobalValue.h"
13#include "llvm/IR/Metadata.h"
14#include "llvm/IR/Module.h"
15
16#define DEBUG_TYPE "dxil-finalize-linkage"
17
18using namespace llvm;
19
20static bool finalizeLinkage(Module &M) {
21 bool MadeChange = false;
22
23 // Convert private globals and external globals with no usage to internal
24 // linkage.
25 for (GlobalVariable &GV : M.globals()) {
26 GV.removeDeadConstantUsers();
27 if (GV.hasPrivateLinkage() || (GV.hasExternalLinkage() && GV.use_empty())) {
28 GV.setLinkage(GlobalValue::InternalLinkage);
29 MadeChange = true;
30 }
31 }
32
34
35 // Collect non-entry and non-exported functions to set to internal linkage.
36 for (Function &EF : M.functions()) {
37 if (EF.isIntrinsic())
38 continue;
39 if (EF.hasExternalLinkage() && EF.hasDefaultVisibility())
40 continue;
41 if (EF.hasFnAttribute("hlsl.shader"))
42 continue;
43 Funcs.push_back(&EF);
44 }
45
46 for (Function *F : Funcs) {
47 if (F->getLinkage() == GlobalValue::ExternalLinkage) {
49 MadeChange = true;
50 }
51 if (F->isDefTriviallyDead()) {
52 M.getFunctionList().erase(F);
53 MadeChange = true;
54 }
55 }
56
57 return MadeChange;
58}
59
66
70
72
74 "DXIL Finalize Linkage", false, false)
76 "DXIL Finalize Linkage", false, false)
77
static bool finalizeLinkage(Module &M)
#define DEBUG_TYPE
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:55
This file contains the declarations for metadata subclasses.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
PreservedAnalyses run(Module &M, ModuleAnalysisManager &)
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is an optimization pass for GlobalISel generic memory operations.
ModulePass * createDXILFinalizeLinkageLegacyPass()
Pass to finalize linkage of functions.
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39