xref: /llvm-project/llvm/tools/llvm-reduce/deltas/ReduceGlobalValues.cpp (revision f2ab8f43800ff14051213bff40d35406691f29b0)
1 //===- ReduceGlobalValues.cpp - Specialized Delta Pass --------------------===//
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 file implements a function which calls the Generic Delta pass to reduce
10 // global value attributes/specifiers.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ReduceGlobalValues.h"
15 #include "llvm/IR/GlobalValue.h"
16 
17 using namespace llvm;
18 
shouldReduceDSOLocal(GlobalValue & GV)19 static bool shouldReduceDSOLocal(GlobalValue &GV) {
20   return GV.isDSOLocal() && !GV.isImplicitDSOLocal();
21 }
22 
shouldReduceVisibility(GlobalValue & GV)23 static bool shouldReduceVisibility(GlobalValue &GV) {
24   return GV.getVisibility() != GlobalValue::VisibilityTypes::DefaultVisibility;
25 }
26 
shouldReduceUnnamedAddress(GlobalValue & GV)27 static bool shouldReduceUnnamedAddress(GlobalValue &GV) {
28   return GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None;
29 }
30 
shouldReduceDLLStorageClass(GlobalValue & GV)31 static bool shouldReduceDLLStorageClass(GlobalValue &GV) {
32   return GV.getDLLStorageClass() !=
33          GlobalValue::DLLStorageClassTypes::DefaultStorageClass;
34 }
35 
shouldReduceThreadLocal(GlobalValue & GV)36 static bool shouldReduceThreadLocal(GlobalValue &GV) {
37   return GV.isThreadLocal();
38 }
39 
shouldReduceLinkage(GlobalValue & GV)40 static bool shouldReduceLinkage(GlobalValue &GV) {
41   return !GV.hasExternalLinkage();
42 }
43 
reduceGVs(Oracle & O,ReducerWorkItem & Program)44 static void reduceGVs(Oracle &O, ReducerWorkItem &Program) {
45   for (auto &GV : Program.getModule().global_values()) {
46     if (shouldReduceDSOLocal(GV) && !O.shouldKeep())
47       GV.setDSOLocal(false);
48     if (shouldReduceVisibility(GV) && !O.shouldKeep()) {
49       bool IsImplicitDSOLocal = GV.isImplicitDSOLocal();
50       GV.setVisibility(GlobalValue::VisibilityTypes::DefaultVisibility);
51       if (IsImplicitDSOLocal)
52         GV.setDSOLocal(false);
53     }
54     if (shouldReduceUnnamedAddress(GV) && !O.shouldKeep())
55       GV.setUnnamedAddr(GlobalValue::UnnamedAddr::None);
56     if (shouldReduceDLLStorageClass(GV) && !O.shouldKeep())
57       GV.setDLLStorageClass(
58           GlobalValue::DLLStorageClassTypes::DefaultStorageClass);
59     if (shouldReduceThreadLocal(GV) && !O.shouldKeep())
60       GV.setThreadLocal(false);
61     if (shouldReduceLinkage(GV) && !O.shouldKeep()) {
62       bool IsImplicitDSOLocal = GV.isImplicitDSOLocal();
63       GV.setLinkage(GlobalValue::ExternalLinkage);
64       if (IsImplicitDSOLocal)
65         GV.setDSOLocal(false);
66     }
67   }
68 }
69 
reduceGlobalValuesDeltaPass(TestRunner & Test)70 void llvm::reduceGlobalValuesDeltaPass(TestRunner &Test) {
71   runDeltaPass(Test, reduceGVs, "Reducing GlobalValues");
72 }
73