}
void Value::setNameImpl(const Twine &NewName) {
+ bool NeedNewName =
+ !getContext().shouldDiscardValueNames() || isa<GlobalValue>(this);
+
// Fast-path: LLVMContext can be set to strip out non-GlobalValue names
- if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this))
+ // and there is no need to delete the old name.
+ if (!NeedNewName && !hasName())
return;
// Fast path for common IRBuilder case of setName("") when there is no name.
return;
SmallString<256> NameData;
- StringRef NameRef = NewName.toStringRef(NameData);
+ StringRef NameRef = NeedNewName ? NewName.toStringRef(NameData) : "";
assert(NameRef.find_first_of(0) == StringRef::npos &&
"Null bytes are not allowed in names");
return; // Cannot set a name on this value (e.g. constant).
if (!ST) { // No symbol table to update? Just do the change.
- if (NameRef.empty()) {
- // Free the name for this value.
- destroyValueName();
- return;
- }
-
// NOTE: Could optimize for the case the name is shrinking to not deallocate
// then reallocated.
destroyValueName();
- // Create the new name.
- MallocAllocator Allocator;
- setValueName(ValueName::create(NameRef, Allocator));
- getValueName()->setValue(this);
+ if (!NameRef.empty()) {
+ // Create the new name.
+ assert(NeedNewName);
+ MallocAllocator Allocator;
+ setValueName(ValueName::create(NameRef, Allocator));
+ getValueName()->setValue(this);
+ }
return;
}
}
// Name is changing to something new.
+ assert(NeedNewName);
setValueName(ST->createValueName(NameRef, this));
}
BB0->erase(BB0->begin(), BB0->end());
EXPECT_TRUE(BB0->empty());
}
+
+TEST(BasicBlockTest, DiscardValueNames) {
+ const char *ModuleString = "declare void @f(i32 %dangling)";
+ SMDiagnostic Err;
+ LLVMContext Ctx;
+ { // Scope of M.
+ auto M = parseAssemblyString(ModuleString, Err, Ctx);
+ ASSERT_TRUE(M.get());
+ EXPECT_FALSE(Ctx.shouldDiscardValueNames());
+ }
+ { // Scope of M.
+ auto M = parseAssemblyString(ModuleString, Err, Ctx);
+ ASSERT_TRUE(M.get());
+ Ctx.setDiscardValueNames(true);
+ }
+}
+
+TEST(BasicBlockTest, DiscardValueNames2) {
+ SMDiagnostic Err;
+ LLVMContext Ctx;
+ Module M("Mod", Ctx);
+ auto FTy = FunctionType::get(Type::getVoidTy(M.getContext()),
+ {Type::getInt32Ty(Ctx)}, /*isVarArg=*/false);
+ { // Scope of F.
+ Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", &M);
+ F->getArg(0)->setName("dangling");
+ F->removeFromParent();
+ EXPECT_FALSE(Ctx.shouldDiscardValueNames());
+ delete F;
+ }
+ { // Scope of F.
+ Function *F = Function::Create(FTy, Function::ExternalLinkage, "f", &M);
+ F->getArg(0)->setName("dangling");
+ F->removeFromParent();
+ Ctx.setDiscardValueNames(true);
+ delete F;
+ }
+}
+
} // End anonymous namespace.
} // End llvm namespace.