Skip to content

Commit

Permalink
Recommit: [NFC][IR] Make Module::getAliasList() private
Browse files Browse the repository at this point in the history
This reverts commit 6d4a674.
  • Loading branch information
vporpo committed Feb 14, 2023
1 parent df77dec commit afad153
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lldb/source/Expression/IRExecutionUnit.cpp
Expand Up @@ -410,7 +410,7 @@ void IRExecutionUnit::GetRunnableInfo(Status &error, lldb::addr_t &func_addr,
RegisterOneValue(global_var);
}

for (llvm::GlobalAlias &global_alias : m_module->getAliasList()) {
for (llvm::GlobalAlias &global_alias : m_module->aliases()) {
RegisterOneValue(global_alias);
}

Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm/IR/Module.h
Expand Up @@ -563,6 +563,16 @@ class LLVM_EXTERNAL_VISIBILITY Module {
return &Module::FunctionList;
}

/// Detach \p Alias from the list but don't delete it.
void removeAlias(GlobalAlias *Alias) { AliasList.remove(Alias); }
/// Remove \p Alias from the list and delete it.
void eraseAlias(GlobalAlias *Alias) { AliasList.erase(Alias); }
/// Insert \p Alias at the end of the alias list and take ownership.
void insertAlias(GlobalAlias *Alias) { AliasList.insert(AliasList.end(), Alias); }
// Use alias_size() to get the size of AliasList.
// Use aliases() to get a range of all Alias objects in AliasList.

private: // Please use functions like insertAlias(), removeAlias() etc.
/// Get the Module's list of aliases (constant).
const AliasListType &getAliasList() const { return AliasList; }
/// Get the Module's list of aliases.
Expand All @@ -571,7 +581,9 @@ class LLVM_EXTERNAL_VISIBILITY Module {
static AliasListType Module::*getSublistAccess(GlobalAlias*) {
return &Module::AliasList;
}
friend class llvm::SymbolTableListTraits<llvm::GlobalAlias>;

public:
/// Get the Module's list of ifuncs (constant).
const IFuncListType &getIFuncList() const { return IFuncList; }
/// Get the Module's list of ifuncs.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/AsmParser/LLParser.cpp
Expand Up @@ -1141,7 +1141,7 @@ bool LLParser::parseAliasOrIFunc(const std::string &Name, LocTy NameLoc,

// Insert into the module, we know its name won't collide now.
if (IsAlias)
M->getAliasList().push_back(GA.release());
M->insertAlias(GA.release());
else
M->getIFuncList().push_back(GI.release());
assert(GV->getName() == Name && "Should not be a name conflict!");
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/IR/Globals.cpp
Expand Up @@ -514,7 +514,7 @@ GlobalAlias::GlobalAlias(Type *Ty, unsigned AddressSpace, LinkageTypes Link,
AddressSpace) {
setAliasee(Aliasee);
if (ParentModule)
ParentModule->getAliasList().push_back(this);
ParentModule->insertAlias(this);
}

GlobalAlias *GlobalAlias::create(Type *Ty, unsigned AddressSpace,
Expand Down Expand Up @@ -546,11 +546,11 @@ GlobalAlias *GlobalAlias::create(const Twine &Name, GlobalValue *Aliasee) {
}

void GlobalAlias::removeFromParent() {
getParent()->getAliasList().remove(getIterator());
getParent()->removeAlias(this);
}

void GlobalAlias::eraseFromParent() {
getParent()->getAliasList().erase(getIterator());
getParent()->eraseAlias(this);
}

void GlobalAlias::setAliasee(Constant *Aliasee) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/IPO/GlobalOpt.cpp
Expand Up @@ -2360,7 +2360,7 @@ OptimizeGlobalAliases(Module &M,
continue;

// Delete the alias.
M.getAliasList().erase(&J);
M.eraseAlias(&J);
++NumAliasesRemoved;
Changed = true;
}
Expand Down
40 changes: 40 additions & 0 deletions llvm/unittests/IR/ModuleTest.cpp
Expand Up @@ -159,4 +159,44 @@ TEST(ModuleTest, setPartialSampleProfileRatio) {
EXPECT_EQ(Ratio, ProfileSummary->getPartialProfileRatio());
}

TEST(ModuleTest, AliasList) {
// This tests all Module's functions that interact with Module::AliasList.
LLVMContext C;
SMDiagnostic Err;
LLVMContext Context;
std::unique_ptr<Module> M = parseAssemblyString(R"(
declare void @Foo()
@GA = alias void (), ptr @Foo
)",
Err, Context);
Function *Foo = M->getFunction("Foo");
auto *GA = M->getNamedAlias("GA");
EXPECT_EQ(M->alias_size(), 1u);
auto *NewGA =
GlobalAlias::create(Foo->getType(), 0, GlobalValue::ExternalLinkage,
"NewGA", Foo, /*Parent=*/nullptr);
EXPECT_EQ(M->alias_size(), 1u);

M->insertAlias(NewGA);
EXPECT_EQ(&*std::prev(M->aliases().end()), NewGA);

M->removeAlias(NewGA);
EXPECT_EQ(M->alias_size(), 1u);
M->insertAlias(NewGA);
EXPECT_EQ(M->alias_size(), 2u);
EXPECT_EQ(&*std::prev(M->aliases().end()), NewGA);

auto Range = M->aliases();
EXPECT_EQ(&*Range.begin(), GA);
EXPECT_EQ(&*std::next(Range.begin()), NewGA);
EXPECT_EQ(std::next(Range.begin(), 2), Range.end());

M->removeAlias(NewGA);
EXPECT_EQ(M->alias_size(), 1u);

M->insertAlias(NewGA);
M->eraseAlias(NewGA);
EXPECT_EQ(M->alias_size(), 1u);
}

} // end namespace

0 comments on commit afad153

Please sign in to comment.