| 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 | |
| 19 | static bool shouldReduceDSOLocal(GlobalValue &GV) { |
| 20 | return GV.isDSOLocal() && !GV.isImplicitDSOLocal(); |
| 21 | } |
| 22 | |
| 23 | static bool shouldReduceVisibility(GlobalValue &GV) { |
| 24 | return GV.getVisibility() != GlobalValue::VisibilityTypes::DefaultVisibility; |
| 25 | } |
| 26 | |
| 27 | static bool shouldReduceUnnamedAddress(GlobalValue &GV) { |
| 28 | return GV.getUnnamedAddr() != GlobalValue::UnnamedAddr::None; |
| 29 | } |
| 30 | |
| 31 | static bool shouldReduceDLLStorageClass(GlobalValue &GV) { |
| 32 | return GV.getDLLStorageClass() != |
| 33 | GlobalValue::DLLStorageClassTypes::DefaultStorageClass; |
| 34 | } |
| 35 | |
| 36 | static bool shouldReduceThreadLocal(GlobalValue &GV) { |
| 37 | return GV.isThreadLocal(); |
| 38 | } |
| 39 | |
| 40 | static bool shouldReduceLinkage(GlobalValue &GV) { |
| 41 | return !GV.hasExternalLinkage() && !GV.hasAppendingLinkage(); |
| 42 | } |
| 43 | |
| 44 | void llvm::reduceGlobalValuesDeltaPass(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 | // TODO: Should this go in a separate reduction? |
| 69 | if (auto *GVar = dyn_cast<GlobalVariable>(Val: &GV)) { |
| 70 | if (GVar->isExternallyInitialized() && !O.shouldKeep()) |
| 71 | GVar->setExternallyInitialized(false); |
| 72 | |
| 73 | if (GVar->getCodeModel() && !O.shouldKeep()) |
| 74 | GVar->clearCodeModel(); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 |