xref: /llvm-project/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp (revision f2ab8f43800ff14051213bff40d35406691f29b0)
124339056SSamuel //===- ReduceGlobalValues.cpp - Specialized Delta Pass --------------------===//
224339056SSamuel //
324339056SSamuel // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
424339056SSamuel // See https://llvm.org/LICENSE.txt for license information.
524339056SSamuel // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
624339056SSamuel //
724339056SSamuel //===----------------------------------------------------------------------===//
824339056SSamuel //
924339056SSamuel // This file implements a function which calls the Generic Delta pass to reduce
1024339056SSamuel // global value attributes/specifiers.
1124339056SSamuel //
1224339056SSamuel //===----------------------------------------------------------------------===//
1324339056SSamuel 
1424339056SSamuel #include "ReduceGlobalValues.h"
1524339056SSamuel #include "llvm/IR/GlobalValue.h"
1624339056SSamuel 
1724339056SSamuel using namespace llvm;
1824339056SSamuel 
shouldReduceDSOLocal(GlobalValue & GV)19f54a8759SArthur Eubanks static bool shouldReduceDSOLocal(GlobalValue &GV) {
20511f2cecSArthur Eubanks   return GV.isDSOLocal() && !GV.isImplicitDSOLocal();
21511f2cecSArthur Eubanks }
22511f2cecSArthur Eubanks 
shouldReduceVisibility(GlobalValue & GV)23f54a8759SArthur Eubanks static bool shouldReduceVisibility(GlobalValue &GV) {
24f54a8759SArthur Eubanks   return GV.getVisibility() != GlobalValue::VisibilityTypes::DefaultVisibility;
25f54a8759SArthur Eubanks }
26f54a8759SArthur Eubanks 
shouldReduceUnnamedAddress(GlobalValue & GV)27f54a8759SArthur Eubanks static bool shouldReduceUnnamedAddress(GlobalValue &GV) {
28f54a8759SArthur Eubanks   return GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None;
29f54a8759SArthur Eubanks }
30f54a8759SArthur Eubanks 
shouldReduceDLLStorageClass(GlobalValue & GV)31f54a8759SArthur Eubanks static bool shouldReduceDLLStorageClass(GlobalValue &GV) {
32f54a8759SArthur Eubanks   return GV.getDLLStorageClass() !=
33f54a8759SArthur Eubanks          GlobalValue::DLLStorageClassTypes::DefaultStorageClass;
34f54a8759SArthur Eubanks }
35f54a8759SArthur Eubanks 
shouldReduceThreadLocal(GlobalValue & GV)36f54a8759SArthur Eubanks static bool shouldReduceThreadLocal(GlobalValue &GV) {
37f54a8759SArthur Eubanks   return GV.isThreadLocal();
38f54a8759SArthur Eubanks }
39f54a8759SArthur Eubanks 
shouldReduceLinkage(GlobalValue & GV)40*f2ab8f43SArthur Eubanks static bool shouldReduceLinkage(GlobalValue &GV) {
41*f2ab8f43SArthur Eubanks   return !GV.hasExternalLinkage();
42*f2ab8f43SArthur Eubanks }
43*f2ab8f43SArthur Eubanks 
reduceGVs(Oracle & O,ReducerWorkItem & Program)4423cc36e4SMatt Arsenault static void reduceGVs(Oracle &O, ReducerWorkItem &Program) {
4523cc36e4SMatt Arsenault   for (auto &GV : Program.getModule().global_values()) {
46f54a8759SArthur Eubanks     if (shouldReduceDSOLocal(GV) && !O.shouldKeep())
4724339056SSamuel       GV.setDSOLocal(false);
48f54a8759SArthur Eubanks     if (shouldReduceVisibility(GV) && !O.shouldKeep()) {
49f54a8759SArthur Eubanks       bool IsImplicitDSOLocal = GV.isImplicitDSOLocal();
50f54a8759SArthur Eubanks       GV.setVisibility(GlobalValue::VisibilityTypes::DefaultVisibility);
51f54a8759SArthur Eubanks       if (IsImplicitDSOLocal)
52f54a8759SArthur Eubanks         GV.setDSOLocal(false);
53f54a8759SArthur Eubanks     }
54f54a8759SArthur Eubanks     if (shouldReduceUnnamedAddress(GV) && !O.shouldKeep())
55f54a8759SArthur Eubanks       GV.setUnnamedAddr(GlobalValue::UnnamedAddr::None);
56f54a8759SArthur Eubanks     if (shouldReduceDLLStorageClass(GV) && !O.shouldKeep())
57f54a8759SArthur Eubanks       GV.setDLLStorageClass(
58f54a8759SArthur Eubanks           GlobalValue::DLLStorageClassTypes::DefaultStorageClass);
59f54a8759SArthur Eubanks     if (shouldReduceThreadLocal(GV) && !O.shouldKeep())
60f54a8759SArthur Eubanks       GV.setThreadLocal(false);
61*f2ab8f43SArthur Eubanks     if (shouldReduceLinkage(GV) && !O.shouldKeep()) {
62*f2ab8f43SArthur Eubanks       bool IsImplicitDSOLocal = GV.isImplicitDSOLocal();
63*f2ab8f43SArthur Eubanks       GV.setLinkage(GlobalValue::ExternalLinkage);
64*f2ab8f43SArthur Eubanks       if (IsImplicitDSOLocal)
65*f2ab8f43SArthur Eubanks         GV.setDSOLocal(false);
66*f2ab8f43SArthur Eubanks     }
6724339056SSamuel   }
6824339056SSamuel }
6924339056SSamuel 
reduceGlobalValuesDeltaPass(TestRunner & Test)7024339056SSamuel void llvm::reduceGlobalValuesDeltaPass(TestRunner &Test) {
712592ccdeSArthur Eubanks   runDeltaPass(Test, reduceGVs, "Reducing GlobalValues");
7224339056SSamuel }
73