Skip to content

Commit e686175

Browse files
committed
[Review] Move llvm.used/llvm.compiler.used helpers from ModuleUtils to
GlobalValue; and remove dependency between BitcodeWriter & TransformUtils
1 parent a368f1c commit e686175

File tree

9 files changed

+90
-94
lines changed

9 files changed

+90
-94
lines changed

llvm/include/llvm/IR/GlobalValue.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,18 @@ class GlobalValue : public Constant {
684684
LLVM_ABI bool canBeOmittedFromSymbolTable() const;
685685
};
686686

687+
/// Adds global values to the llvm.used list.
688+
LLVM_ABI void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
689+
690+
/// Adds global values to the llvm.compiler.used list.
691+
LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
692+
693+
/// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
694+
/// ShouldRemove should return true for any initializer field that should not be
695+
/// included in the replacement global.
696+
LLVM_ABI void removeFromUsedLists(Module &M,
697+
function_ref<bool(Constant *)> ShouldRemove);
698+
687699
} // end namespace llvm
688700

689701
#endif // LLVM_IR_GLOBALVALUE_H

llvm/include/llvm/Transforms/Utils/ModuleUtils.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,6 @@ getOrCreateSanitizerCtorAndInitFunctions(
9696
/// the list of public globals in the module.
9797
LLVM_ABI bool nameUnamedGlobals(Module &M);
9898

99-
/// Adds global values to the llvm.used list.
100-
LLVM_ABI void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
101-
102-
/// Adds global values to the llvm.compiler.used list.
103-
LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
104-
105-
/// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
106-
/// ShouldRemove should return true for any initializer field that should not be
107-
/// included in the replacement global.
108-
LLVM_ABI void removeFromUsedLists(Module &M,
109-
function_ref<bool(Constant *)> ShouldRemove);
110-
11199
/// Filter out potentially dead comdat functions where other entries keep the
112100
/// entire comdat group alive.
113101
///

llvm/lib/Bitcode/Writer/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ add_llvm_component_library(LLVMBitWriter
1515
ProfileData
1616
Support
1717
TargetParser
18-
TransformUtils
1918
)

llvm/lib/CodeGen/JMCInstrumenter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include "llvm/Pass.h"
3737
#include "llvm/Support/DJB.h"
3838
#include "llvm/Support/Path.h"
39-
#include "llvm/Transforms/Utils/ModuleUtils.h"
4039

4140
using namespace llvm;
4241

llvm/lib/IR/Globals.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "LLVMContextImpl.h"
15+
#include "llvm/ADT/SetVector.h"
1516
#include "llvm/IR/ConstantRange.h"
1617
#include "llvm/IR/Constants.h"
1718
#include "llvm/IR/DerivedTypes.h"
@@ -671,3 +672,80 @@ void GlobalIFunc::applyAlongResolverPath(
671672
DenseSet<const GlobalAlias *> Aliases;
672673
findBaseObject(getResolver(), Aliases, Op);
673674
}
675+
676+
static void collectUsedGlobals(GlobalVariable *GV,
677+
SmallSetVector<Constant *, 16> &Init) {
678+
if (!GV || !GV->hasInitializer())
679+
return;
680+
681+
auto *CA = cast<ConstantArray>(GV->getInitializer());
682+
for (Use &Op : CA->operands())
683+
Init.insert(cast<Constant>(Op));
684+
}
685+
686+
static void appendToUsedList(Module &M, StringRef Name,
687+
ArrayRef<GlobalValue *> Values) {
688+
GlobalVariable *GV = M.getGlobalVariable(Name);
689+
690+
SmallSetVector<Constant *, 16> Init;
691+
collectUsedGlobals(GV, Init);
692+
Type *ArrayEltTy = GV ? GV->getValueType()->getArrayElementType()
693+
: PointerType::getUnqual(M.getContext());
694+
if (GV)
695+
GV->eraseFromParent();
696+
697+
for (auto *V : Values)
698+
Init.insert(ConstantExpr::getPointerBitCastOrAddrSpaceCast(V, ArrayEltTy));
699+
700+
if (Init.empty())
701+
return;
702+
703+
ArrayType *ATy = ArrayType::get(ArrayEltTy, Init.size());
704+
GV = new GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
705+
ConstantArray::get(ATy, Init.getArrayRef()), Name);
706+
GV->setSection("llvm.metadata");
707+
}
708+
709+
void llvm::appendToUsed(Module &M, ArrayRef<GlobalValue *> Values) {
710+
appendToUsedList(M, "llvm.used", Values);
711+
}
712+
713+
void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
714+
appendToUsedList(M, "llvm.compiler.used", Values);
715+
}
716+
717+
static void removeFromUsedList(Module &M, StringRef Name,
718+
function_ref<bool(Constant *)> ShouldRemove) {
719+
GlobalVariable *GV = M.getNamedGlobal(Name);
720+
if (!GV)
721+
return;
722+
723+
SmallSetVector<Constant *, 16> Init;
724+
collectUsedGlobals(GV, Init);
725+
726+
Type *ArrayEltTy = cast<ArrayType>(GV->getValueType())->getElementType();
727+
728+
SmallVector<Constant *, 16> NewInit;
729+
for (Constant *MaybeRemoved : Init) {
730+
if (!ShouldRemove(MaybeRemoved->stripPointerCasts()))
731+
NewInit.push_back(MaybeRemoved);
732+
}
733+
734+
if (!NewInit.empty()) {
735+
ArrayType *ATy = ArrayType::get(ArrayEltTy, NewInit.size());
736+
GlobalVariable *NewGV =
737+
new GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
738+
ConstantArray::get(ATy, NewInit), "", GV,
739+
GV->getThreadLocalMode(), GV->getAddressSpace());
740+
NewGV->setSection(GV->getSection());
741+
NewGV->takeName(GV);
742+
}
743+
744+
GV->eraseFromParent();
745+
}
746+
747+
void llvm::removeFromUsedLists(Module &M,
748+
function_ref<bool(Constant *)> ShouldRemove) {
749+
removeFromUsedList(M, "llvm.used", ShouldRemove);
750+
removeFromUsedList(M, "llvm.compiler.used", ShouldRemove);
751+
}

llvm/lib/Target/AMDGPU/AMDGPUCtorDtorLowering.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include "llvm/IR/Module.h"
2020
#include "llvm/IR/Value.h"
2121
#include "llvm/Pass.h"
22-
#include "llvm/Transforms/Utils/ModuleUtils.h"
2322

2423
using namespace llvm;
2524

llvm/lib/Target/NVPTX/NVPTXCtorDtorLowering.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "llvm/Pass.h"
2525
#include "llvm/Support/CommandLine.h"
2626
#include "llvm/Support/MD5.h"
27-
#include "llvm/Transforms/Utils/ModuleUtils.h"
2827

2928
using namespace llvm;
3029

llvm/lib/Transforms/Utils/ModuleUtils.cpp

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -123,83 +123,6 @@ void llvm::transformGlobalDtors(Module &M, const GlobalCtorTransformFn &Fn) {
123123
transformGlobalArray("llvm.global_dtors", M, Fn);
124124
}
125125

126-
static void collectUsedGlobals(GlobalVariable *GV,
127-
SmallSetVector<Constant *, 16> &Init) {
128-
if (!GV || !GV->hasInitializer())
129-
return;
130-
131-
auto *CA = cast<ConstantArray>(GV->getInitializer());
132-
for (Use &Op : CA->operands())
133-
Init.insert(cast<Constant>(Op));
134-
}
135-
136-
static void appendToUsedList(Module &M, StringRef Name, ArrayRef<GlobalValue *> Values) {
137-
GlobalVariable *GV = M.getGlobalVariable(Name);
138-
139-
SmallSetVector<Constant *, 16> Init;
140-
collectUsedGlobals(GV, Init);
141-
Type *ArrayEltTy = GV ? GV->getValueType()->getArrayElementType()
142-
: PointerType::getUnqual(M.getContext());
143-
if (GV)
144-
GV->eraseFromParent();
145-
146-
for (auto *V : Values)
147-
Init.insert(ConstantExpr::getPointerBitCastOrAddrSpaceCast(V, ArrayEltTy));
148-
149-
if (Init.empty())
150-
return;
151-
152-
ArrayType *ATy = ArrayType::get(ArrayEltTy, Init.size());
153-
GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
154-
ConstantArray::get(ATy, Init.getArrayRef()),
155-
Name);
156-
GV->setSection("llvm.metadata");
157-
}
158-
159-
void llvm::appendToUsed(Module &M, ArrayRef<GlobalValue *> Values) {
160-
appendToUsedList(M, "llvm.used", Values);
161-
}
162-
163-
void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
164-
appendToUsedList(M, "llvm.compiler.used", Values);
165-
}
166-
167-
static void removeFromUsedList(Module &M, StringRef Name,
168-
function_ref<bool(Constant *)> ShouldRemove) {
169-
GlobalVariable *GV = M.getNamedGlobal(Name);
170-
if (!GV)
171-
return;
172-
173-
SmallSetVector<Constant *, 16> Init;
174-
collectUsedGlobals(GV, Init);
175-
176-
Type *ArrayEltTy = cast<ArrayType>(GV->getValueType())->getElementType();
177-
178-
SmallVector<Constant *, 16> NewInit;
179-
for (Constant *MaybeRemoved : Init) {
180-
if (!ShouldRemove(MaybeRemoved->stripPointerCasts()))
181-
NewInit.push_back(MaybeRemoved);
182-
}
183-
184-
if (!NewInit.empty()) {
185-
ArrayType *ATy = ArrayType::get(ArrayEltTy, NewInit.size());
186-
GlobalVariable *NewGV =
187-
new GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
188-
ConstantArray::get(ATy, NewInit), "", GV,
189-
GV->getThreadLocalMode(), GV->getAddressSpace());
190-
NewGV->setSection(GV->getSection());
191-
NewGV->takeName(GV);
192-
}
193-
194-
GV->eraseFromParent();
195-
}
196-
197-
void llvm::removeFromUsedLists(Module &M,
198-
function_ref<bool(Constant *)> ShouldRemove) {
199-
removeFromUsedList(M, "llvm.used", ShouldRemove);
200-
removeFromUsedList(M, "llvm.compiler.used", ShouldRemove);
201-
}
202-
203126
void llvm::setKCFIType(Module &M, Function &F, StringRef MangledType) {
204127
if (!M.getModuleFlag("kcfi"))
205128
return;

llvm/lib/Transforms/Utils/SampleProfileLoaderBaseUtil.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "llvm/Analysis/ProfileSummaryInfo.h"
1515
#include "llvm/IR/Constants.h"
1616
#include "llvm/IR/Module.h"
17-
#include "llvm/Transforms/Utils/ModuleUtils.h"
1817

1918
namespace llvm {
2019

0 commit comments

Comments
 (0)