634 changes: 340 additions & 294 deletions llvm/lib/IR/DIBuilder.cpp

Large diffs are not rendered by default.

91 changes: 35 additions & 56 deletions llvm/lib/IR/DebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ bool DIDescriptor::Verify() const {
DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
}

static Value *getField(const MDNode *DbgNode, unsigned Elt) {
static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
if (!DbgNode || Elt >= DbgNode->getNumOperands())
return nullptr;
return DbgNode->getOperand(Elt);
Expand All @@ -73,25 +73,17 @@ StringRef DIDescriptor::getStringField(unsigned Elt) const {
}

uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
if (!DbgNode)
return 0;

if (Elt < DbgNode->getNumOperands())
if (ConstantInt *CI =
dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
if (auto *C = getConstantField(Elt))
if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
return CI->getZExtValue();

return 0;
}

int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
if (!DbgNode)
return 0;

if (Elt < DbgNode->getNumOperands())
if (ConstantInt *CI =
dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
return CI->getSExtValue();
if (auto *C = getConstantField(Elt))
if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
return CI->getZExtValue();

return 0;
}
Expand All @@ -102,30 +94,22 @@ DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
}

GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
if (!DbgNode)
return nullptr;

if (Elt < DbgNode->getNumOperands())
return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
return nullptr;
return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
}

Constant *DIDescriptor::getConstantField(unsigned Elt) const {
if (!DbgNode)
return nullptr;

if (Elt < DbgNode->getNumOperands())
return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
if (auto *C =
dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
return C->getValue();
return nullptr;
}

Function *DIDescriptor::getFunctionField(unsigned Elt) const {
if (!DbgNode)
return nullptr;

if (Elt < DbgNode->getNumOperands())
return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
return nullptr;
return dyn_cast_or_null<Function>(getConstantField(Elt));
}

void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
Expand All @@ -134,7 +118,7 @@ void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {

if (Elt < DbgNode->getNumOperands()) {
MDNode *Node = const_cast<MDNode *>(DbgNode);
Node->replaceOperandWith(Elt, F);
Node->replaceOperandWith(Elt, F ? ConstantAsMetadata::get(F) : nullptr);
}
}

Expand Down Expand Up @@ -347,27 +331,23 @@ void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
// itself.
const MDNode *DN = D;
if (DbgNode == DN) {
SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
SmallVector<Metadata *, 10> Ops(DbgNode->getNumOperands());
for (size_t i = 0; i != Ops.size(); ++i)
Ops[i] = DbgNode->getOperand(i);
DN = MDNode::get(VMContext, Ops);
}

MDNode *Node = const_cast<MDNode *>(DbgNode);
const Value *V = cast_or_null<Value>(DN);
Node->replaceAllUsesWith(const_cast<Value *>(V));
auto *Node = cast<MDNodeFwdDecl>(const_cast<MDNode *>(DbgNode));
Node->replaceAllUsesWith(const_cast<MDNode *>(DN));
MDNode::deleteTemporary(Node);
DbgNode = DN;
}

void DIDescriptor::replaceAllUsesWith(MDNode *D) {

assert(DbgNode && "Trying to replace an unverified type!");
assert(DbgNode != D && "This replacement should always happen");
MDNode *Node = const_cast<MDNode *>(DbgNode);
const MDNode *DN = D;
const Value *V = cast_or_null<Value>(DN);
Node->replaceAllUsesWith(const_cast<Value *>(V));
auto *Node = cast<MDNodeFwdDecl>(const_cast<MDNode *>(DbgNode));
Node->replaceAllUsesWith(D);
MDNode::deleteTemporary(Node);
}

Expand Down Expand Up @@ -398,15 +378,15 @@ bool DIObjCProperty::Verify() const {
static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
// FIXME: This function should return true, if the field is null or the field
// is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
Value *Fld = getField(DbgNode, Elt);
Metadata *Fld = getField(DbgNode, Elt);
if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
return false;
return true;
}

/// \brief Check if a field at position Elt of a MDNode is a MDString.
static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
Value *Fld = getField(DbgNode, Elt);
Metadata *Fld = getField(DbgNode, Elt);
return !Fld || isa<MDString>(Fld);
}

Expand Down Expand Up @@ -533,7 +513,6 @@ bool DISubprogram::Verify() const {
// If a DISubprogram has an llvm::Function*, then scope chains from all
// instructions within the function should lead to this DISubprogram.
if (auto *F = getFunction()) {
LLVMContext &Ctxt = F->getContext();
for (auto &BB : *F) {
for (auto &I : BB) {
DebugLoc DL = I.getDebugLoc();
Expand All @@ -543,15 +522,15 @@ bool DISubprogram::Verify() const {
MDNode *Scope = nullptr;
MDNode *IA = nullptr;
// walk the inlined-at scopes
while (DL.getScopeAndInlinedAt(Scope, IA, F->getContext()), IA)
while ((IA = DL.getInlinedAt()))
DL = DebugLoc::getFromDILocation(IA);
DL.getScopeAndInlinedAt(Scope, IA, Ctxt);
DL.getScopeAndInlinedAt(Scope, IA);
assert(!IA);
while (!DIDescriptor(Scope).isSubprogram()) {
DILexicalBlockFile D(Scope);
Scope = D.isLexicalBlockFile()
? D.getScope()
: DebugLoc::getFromDILexicalBlock(Scope).getScope(Ctxt);
: DebugLoc::getFromDILexicalBlock(Scope).getScope();
}
if (!DISubprogram(Scope).describes(F))
return false;
Expand Down Expand Up @@ -678,7 +657,7 @@ MDString *DICompositeType::getIdentifier() const {
static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
// Skip the 'empty' list (that's a single i32 0, rather than truly empty).
if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
if (i == 0 && mdconst::hasa<ConstantInt>(LHS->getOperand(i)))
continue;
const MDNode *E = cast<MDNode>(LHS->getOperand(i));
bool found = false;
Expand All @@ -690,7 +669,7 @@ static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
#endif

void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
TrackingVH<MDNode> N(*this);
TrackingMDNodeRef N(*this);
if (Elements) {
#ifndef NDEBUG
// Check that the new list of members contains all the old members as well.
Expand All @@ -714,7 +693,7 @@ DIScopeRef DIScope::getRef() const {
}

void DICompositeType::setContainingType(DICompositeType ContainingType) {
TrackingVH<MDNode> N(*this);
TrackingMDNodeRef N(*this);
N->replaceOperandWith(5, ContainingType.getRef());
DbgNode = N;
}
Expand Down Expand Up @@ -748,8 +727,8 @@ DIArray DISubprogram::getVariables() const {
return DIArray(getNodeField(DbgNode, 8));
}

Value *DITemplateValueParameter::getValue() const {
return getField(DbgNode, 3);
Metadata *DITemplateValueParameter::getValue() const {
return DbgNode->getOperand(3);
}

DIScopeRef DIScope::getContext() const {
Expand Down Expand Up @@ -851,7 +830,7 @@ void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {

DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
DILexicalBlockFile NewScope) {
SmallVector<Value *, 10> Elts;
SmallVector<Metadata *, 10> Elts;
assert(Verify());
for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
if (I != 2)
Expand All @@ -875,7 +854,7 @@ DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
return cleanseInlinedVariable(DV, VMContext);

// Insert inlined scope.
SmallVector<Value *, 8> Elts;
SmallVector<Metadata *, 8> Elts;
for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
Elts.push_back(DV->getOperand(I));
Elts.push_back(InlinedScope);
Expand All @@ -891,7 +870,7 @@ DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
return DIVariable(DV);

// Remove inlined scope.
SmallVector<Value *, 8> Elts;
SmallVector<Metadata *, 8> Elts;
for (unsigned I = 0, E = DIVariableInlinedAtIndex; I != E; ++I)
Elts.push_back(DV->getOperand(I));

Expand Down Expand Up @@ -923,7 +902,7 @@ DISubprogram llvm::getDISubprogram(const Function *F) {
if (Inst == BB.end())
continue;
DebugLoc DLoc = Inst->getDebugLoc();
const MDNode *Scope = DLoc.getScopeNode(F->getParent()->getContext());
const MDNode *Scope = DLoc.getScopeNode();
DISubprogram Subprogram = getDISubprogram(Scope);
return Subprogram.describes(F) ? Subprogram : DISubprogram();
}
Expand Down Expand Up @@ -1533,10 +1512,10 @@ bool llvm::StripDebugInfo(Module &M) {
}

unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Value *Val = M.getModuleFlag("Debug Info Version");
if (!Val)
return 0;
return cast<ConstantInt>(Val)->getZExtValue();
if (auto *Val = mdconst::extract_or_null<ConstantInt>(
M.getModuleFlag("Debug Info Version")))
return Val->getZExtValue();
return 0;
}

llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
Expand Down
297 changes: 35 additions & 262 deletions llvm/lib/IR/DebugLoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,67 +17,29 @@ using namespace llvm;
// DebugLoc Implementation
//===----------------------------------------------------------------------===//

MDNode *DebugLoc::getScope(const LLVMContext &Ctx) const {
if (ScopeIdx == 0) return nullptr;

if (ScopeIdx > 0) {
// Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
// position specified.
assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
"Invalid ScopeIdx!");
return Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
}

// Otherwise, the index is in the ScopeInlinedAtRecords array.
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
}
unsigned DebugLoc::getLine() const { return DILocation(Loc).getLineNumber(); }
unsigned DebugLoc::getCol() const { return DILocation(Loc).getColumnNumber(); }

MDNode *DebugLoc::getScope() const { return DILocation(Loc).getScope(); }

MDNode *DebugLoc::getInlinedAt(const LLVMContext &Ctx) const {
// Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
// position specified. Zero is invalid.
if (ScopeIdx >= 0) return nullptr;

// Otherwise, the index is in the ScopeInlinedAtRecords array.
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
return Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
MDNode *DebugLoc::getInlinedAt() const {
return DILocation(Loc).getOrigLocation();
}

/// Return both the Scope and the InlinedAt values.
void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA,
const LLVMContext &Ctx) const {
if (ScopeIdx == 0) {
Scope = IA = nullptr;
return;
}

if (ScopeIdx > 0) {
// Positive ScopeIdx is an index into ScopeRecords, which has no inlined-at
// position specified.
assert(unsigned(ScopeIdx) <= Ctx.pImpl->ScopeRecords.size() &&
"Invalid ScopeIdx!");
Scope = Ctx.pImpl->ScopeRecords[ScopeIdx-1].get();
IA = nullptr;
return;
}

// Otherwise, the index is in the ScopeInlinedAtRecords array.
assert(unsigned(-ScopeIdx) <= Ctx.pImpl->ScopeInlinedAtRecords.size() &&
"Invalid ScopeIdx");
Scope = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].first.get();
IA = Ctx.pImpl->ScopeInlinedAtRecords[-ScopeIdx-1].second.get();
void DebugLoc::getScopeAndInlinedAt(MDNode *&Scope, MDNode *&IA) const {
Scope = getScope();
IA = getInlinedAt();
}

MDNode *DebugLoc::getScopeNode(const LLVMContext &Ctx) const {
if (MDNode *InlinedAt = getInlinedAt(Ctx))
return DebugLoc::getFromDILocation(InlinedAt).getScopeNode(Ctx);
return getScope(Ctx);
MDNode *DebugLoc::getScopeNode() const {
if (MDNode *InlinedAt = getInlinedAt())
return DebugLoc::getFromDILocation(InlinedAt).getScopeNode();
return getScope();
}

DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const {
const MDNode *Scope = getScopeNode(Ctx);
DebugLoc DebugLoc::getFnDebugLoc() const {
const MDNode *Scope = getScopeNode();
DISubprogram SP = getDISubprogram(Scope);
if (SP.isSubprogram())
return DebugLoc::get(SP.getScopeLineNumber(), 0, SP);
Expand All @@ -87,53 +49,32 @@ DebugLoc DebugLoc::getFnDebugLoc(const LLVMContext &Ctx) const {

DebugLoc DebugLoc::get(unsigned Line, unsigned Col,
MDNode *Scope, MDNode *InlinedAt) {
DebugLoc Result;

// If no scope is available, this is an unknown location.
if (!Scope) return Result;
if (!Scope)
return DebugLoc();

// Saturate line and col to "unknown".
// FIXME: Allow 16-bits for columns.
if (Col > 255) Col = 0;
if (Line >= (1 << 24)) Line = 0;
Result.LineCol = Line | (Col << 24);

LLVMContext &Ctx = Scope->getContext();

// If there is no inlined-at location, use the ScopeRecords array.
if (!InlinedAt)
Result.ScopeIdx = Ctx.pImpl->getOrAddScopeRecordIdxEntry(Scope, 0);
else
Result.ScopeIdx = Ctx.pImpl->getOrAddScopeInlinedAtIdxEntry(Scope,
InlinedAt, 0);

return Result;
LLVMContext &Context = Scope->getContext();
Type *Int32 = Type::getInt32Ty(Context);
Metadata *Elts[] = {ConstantAsMetadata::get(ConstantInt::get(Int32, Line)),
ConstantAsMetadata::get(ConstantInt::get(Int32, Col)),
Scope, InlinedAt};
return getFromDILocation(MDNode::get(Context, Elts));
}

/// getAsMDNode - This method converts the compressed DebugLoc node into a
/// DILocation-compatible MDNode.
MDNode *DebugLoc::getAsMDNode(const LLVMContext &Ctx) const {
if (isUnknown()) return nullptr;

MDNode *Scope, *IA;
getScopeAndInlinedAt(Scope, IA, Ctx);
assert(Scope && "If scope is null, this should be isUnknown()");

LLVMContext &Ctx2 = Scope->getContext();
Type *Int32 = Type::getInt32Ty(Ctx2);
Value *Elts[] = {
ConstantInt::get(Int32, getLine()), ConstantInt::get(Int32, getCol()),
Scope, IA
};
return MDNode::get(Ctx2, Elts);
}
MDNode *DebugLoc::getAsMDNode() const { return Loc; }

/// getFromDILocation - Translate the DILocation quad into a DebugLoc.
DebugLoc DebugLoc::getFromDILocation(MDNode *N) {
DILocation Loc(N);
MDNode *Scope = Loc.getScope();
if (!Scope) return DebugLoc();
return get(Loc.getLineNumber(), Loc.getColumnNumber(), Scope,
Loc.getOrigLocation());
DebugLoc Loc;
Loc.Loc.reset(N);
return Loc;
}

/// getFromDILexicalBlock - Translate the DILexicalBlock into a DebugLoc.
Expand All @@ -145,26 +86,26 @@ DebugLoc DebugLoc::getFromDILexicalBlock(MDNode *N) {
nullptr);
}

void DebugLoc::dump(const LLVMContext &Ctx) const {
void DebugLoc::dump() const {
#ifndef NDEBUG
if (!isUnknown()) {
dbgs() << getLine();
if (getCol() != 0)
dbgs() << ',' << getCol();
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
if (!InlinedAtDL.isUnknown()) {
dbgs() << " @ ";
InlinedAtDL.dump(Ctx);
InlinedAtDL.dump();
} else
dbgs() << "\n";
}
#endif
}

void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const {
void DebugLoc::print(raw_ostream &OS) const {
if (!isUnknown()) {
// Print source line info.
DIScope Scope(getScope(Ctx));
DIScope Scope(getScope());
assert((!Scope || Scope.isScope()) &&
"Scope of a DebugLoc should be null or a DIScope.");
if (Scope)
Expand All @@ -174,179 +115,11 @@ void DebugLoc::print(const LLVMContext &Ctx, raw_ostream &OS) const {
OS << ':' << getLine();
if (getCol() != 0)
OS << ':' << getCol();
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt(Ctx));
DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(getInlinedAt());
if (!InlinedAtDL.isUnknown()) {
OS << " @[ ";
InlinedAtDL.print(Ctx, OS);
InlinedAtDL.print(OS);
OS << " ]";
}
}
}

//===----------------------------------------------------------------------===//
// DenseMap specialization
//===----------------------------------------------------------------------===//

unsigned DenseMapInfo<DebugLoc>::getHashValue(const DebugLoc &Key) {
return static_cast<unsigned>(hash_combine(Key.LineCol, Key.ScopeIdx));
}

//===----------------------------------------------------------------------===//
// LLVMContextImpl Implementation
//===----------------------------------------------------------------------===//

int LLVMContextImpl::getOrAddScopeRecordIdxEntry(MDNode *Scope,
int ExistingIdx) {
// If we already have an entry for this scope, return it.
int &Idx = ScopeRecordIdx[Scope];
if (Idx) return Idx;

// If we don't have an entry, but ExistingIdx is specified, use it.
if (ExistingIdx)
return Idx = ExistingIdx;

// Otherwise add a new entry.

// Start out ScopeRecords with a minimal reasonable size to avoid
// excessive reallocation starting out.
if (ScopeRecords.empty())
ScopeRecords.reserve(128);

// Index is biased by 1 for index.
Idx = ScopeRecords.size()+1;
ScopeRecords.push_back(DebugRecVH(Scope, this, Idx));
return Idx;
}

int LLVMContextImpl::getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,
int ExistingIdx) {
// If we already have an entry, return it.
int &Idx = ScopeInlinedAtIdx[std::make_pair(Scope, IA)];
if (Idx) return Idx;

// If we don't have an entry, but ExistingIdx is specified, use it.
if (ExistingIdx)
return Idx = ExistingIdx;

// Start out ScopeInlinedAtRecords with a minimal reasonable size to avoid
// excessive reallocation starting out.
if (ScopeInlinedAtRecords.empty())
ScopeInlinedAtRecords.reserve(128);

// Index is biased by 1 and negated.
Idx = -ScopeInlinedAtRecords.size()-1;
ScopeInlinedAtRecords.push_back(std::make_pair(DebugRecVH(Scope, this, Idx),
DebugRecVH(IA, this, Idx)));
return Idx;
}


//===----------------------------------------------------------------------===//
// DebugRecVH Implementation
//===----------------------------------------------------------------------===//

/// deleted - The MDNode this is pointing to got deleted, so this pointer needs
/// to drop to null and we need remove our entry from the DenseMap.
void DebugRecVH::deleted() {
// If this is a non-canonical reference, just drop the value to null, we know
// it doesn't have a map entry.
if (Idx == 0) {
setValPtr(nullptr);
return;
}

MDNode *Cur = get();

// If the index is positive, it is an entry in ScopeRecords.
if (Idx > 0) {
assert(Ctx->ScopeRecordIdx[Cur] == Idx && "Mapping out of date!");
Ctx->ScopeRecordIdx.erase(Cur);
// Reset this VH to null and we're done.
setValPtr(nullptr);
Idx = 0;
return;
}

// Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
// is the scope or the inlined-at record entry.
assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
assert((this == &Entry.first || this == &Entry.second) &&
"Mapping out of date!");

MDNode *OldScope = Entry.first.get();
MDNode *OldInlinedAt = Entry.second.get();
assert(OldScope && OldInlinedAt &&
"Entry should be non-canonical if either val dropped to null");

// Otherwise, we do have an entry in it, nuke it and we're done.
assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
"Mapping out of date");
Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));

// Reset this VH to null. Drop both 'Idx' values to null to indicate that
// we're in non-canonical form now.
setValPtr(nullptr);
Entry.first.Idx = Entry.second.Idx = 0;
}

void DebugRecVH::allUsesReplacedWith(Value *NewVa) {
// If being replaced with a non-mdnode value (e.g. undef) handle this as if
// the mdnode got deleted.
MDNode *NewVal = dyn_cast<MDNode>(NewVa);
if (!NewVal) return deleted();

// If this is a non-canonical reference, just change it, we know it already
// doesn't have a map entry.
if (Idx == 0) {
setValPtr(NewVa);
return;
}

MDNode *OldVal = get();
assert(OldVal != NewVa && "Node replaced with self?");

// If the index is positive, it is an entry in ScopeRecords.
if (Idx > 0) {
assert(Ctx->ScopeRecordIdx[OldVal] == Idx && "Mapping out of date!");
Ctx->ScopeRecordIdx.erase(OldVal);
setValPtr(NewVal);

int NewEntry = Ctx->getOrAddScopeRecordIdxEntry(NewVal, Idx);

// If NewVal already has an entry, this becomes a non-canonical reference,
// just drop Idx to 0 to signify this.
if (NewEntry != Idx)
Idx = 0;
return;
}

// Otherwise, it is an entry in ScopeInlinedAtRecords, we don't know if it
// is the scope or the inlined-at record entry.
assert(unsigned(-Idx-1) < Ctx->ScopeInlinedAtRecords.size());
std::pair<DebugRecVH, DebugRecVH> &Entry = Ctx->ScopeInlinedAtRecords[-Idx-1];
assert((this == &Entry.first || this == &Entry.second) &&
"Mapping out of date!");

MDNode *OldScope = Entry.first.get();
MDNode *OldInlinedAt = Entry.second.get();
assert(OldScope && OldInlinedAt &&
"Entry should be non-canonical if either val dropped to null");

// Otherwise, we do have an entry in it, nuke it and we're done.
assert(Ctx->ScopeInlinedAtIdx[std::make_pair(OldScope, OldInlinedAt)] == Idx&&
"Mapping out of date");
Ctx->ScopeInlinedAtIdx.erase(std::make_pair(OldScope, OldInlinedAt));

// Reset this VH to the new value.
setValPtr(NewVal);

int NewIdx = Ctx->getOrAddScopeInlinedAtIdxEntry(Entry.first.get(),
Entry.second.get(), Idx);
// If NewVal already has an entry, this becomes a non-canonical reference,
// just drop Idx to 0 to signify this.
if (NewIdx != Idx) {
std::pair<DebugRecVH, DebugRecVH> &Entry=Ctx->ScopeInlinedAtRecords[-Idx-1];
Entry.first.Idx = Entry.second.Idx = 0;
}
}
3 changes: 2 additions & 1 deletion llvm/lib/IR/DiagnosticInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ DiagnosticInfoInlineAsm::DiagnosticInfoInlineAsm(const Instruction &I,
Instr(&I) {
if (const MDNode *SrcLoc = I.getMetadata("srcloc")) {
if (SrcLoc->getNumOperands() != 0)
if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
if (const auto *CI =
mdconst::dyn_extract<ConstantInt>(SrcLoc->getOperand(0)))
LocCookie = CI->getZExtValue();
}
}
Expand Down
9 changes: 3 additions & 6 deletions llvm/lib/IR/Instructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -796,11 +796,8 @@ void BranchInst::swapSuccessors() {
return;

// The first operand is the name. Fetch them backwards and build a new one.
Value *Ops[] = {
ProfileData->getOperand(0),
ProfileData->getOperand(2),
ProfileData->getOperand(1)
};
Metadata *Ops[] = {ProfileData->getOperand(0), ProfileData->getOperand(2),
ProfileData->getOperand(1)};
setMetadata(LLVMContext::MD_prof,
MDNode::get(ProfileData->getContext(), Ops));
}
Expand Down Expand Up @@ -2076,7 +2073,7 @@ float FPMathOperator::getFPAccuracy() const {
cast<Instruction>(this)->getMetadata(LLVMContext::MD_fpmath);
if (!MD)
return 0.0;
ConstantFP *Accuracy = cast<ConstantFP>(MD->getOperand(0));
ConstantFP *Accuracy = mdconst::extract<ConstantFP>(MD->getOperand(0));
return Accuracy->getValueAPF().convertToFloat();
}

Expand Down
22 changes: 15 additions & 7 deletions llvm/lib/IR/IntrinsicInst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,33 @@ Value *DbgInfoIntrinsic::StripCast(Value *C) {
return dyn_cast<GlobalVariable>(C);
}

static Value *getValueImpl(Value *Op) {
auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
if (auto *V = dyn_cast<ValueAsMetadata>(MD))
return V->getValue();

// When the value goes to null, it gets replaced by an empty MDNode.
assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
return nullptr;
}

//===----------------------------------------------------------------------===//
/// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
///

Value *DbgDeclareInst::getAddress() const {
if (MDNode* MD = cast_or_null<MDNode>(getArgOperand(0)))
return MD->getOperand(0);
else
if (!getArgOperand(0))
return nullptr;

return getValueImpl(getArgOperand(0));
}

//===----------------------------------------------------------------------===//
/// DbgValueInst - This represents the llvm.dbg.value instruction.
///

const Value *DbgValueInst::getValue() const {
return cast<MDNode>(getArgOperand(0))->getOperand(0);
return const_cast<DbgValueInst *>(this)->getValue();
}

Value *DbgValueInst::getValue() {
return cast<MDNode>(getArgOperand(0))->getOperand(0);
}
Value *DbgValueInst::getValue() { return getValueImpl(getArgOperand(0)); }
15 changes: 15 additions & 0 deletions llvm/lib/IR/LLVMContextImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ LLVMContextImpl::~LLVMContextImpl() {
delete &*Elem;
}

// Destroy MetadataAsValues.
{
SmallVector<MetadataAsValue *, 8> MDVs;
MDVs.reserve(MetadataAsValues.size());
for (auto &Pair : MetadataAsValues)
MDVs.push_back(Pair.second);
MetadataAsValues.clear();
for (auto *V : MDVs)
delete V;
}

// Destroy ValuesAsMetadata.
for (auto &Pair : ValuesAsMetadata)
delete Pair.second;

// Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
// and the NonUniquedMDNodes sets, so copy the values out first.
SmallVector<GenericMDNode *, 8> MDNodes;
Expand Down
80 changes: 21 additions & 59 deletions llvm/lib/IR/LLVMContextImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,29 +172,29 @@ struct FunctionTypeKeyInfo {
/// the operands.
struct GenericMDNodeInfo {
struct KeyTy {
ArrayRef<Value *> Ops;
ArrayRef<Metadata *> RawOps;
ArrayRef<MDOperand> Ops;
unsigned Hash;

KeyTy(ArrayRef<Value *> Ops)
: Ops(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}
KeyTy(ArrayRef<Metadata *> Ops)
: RawOps(Ops), Hash(hash_combine_range(Ops.begin(), Ops.end())) {}

KeyTy(GenericMDNode *N, SmallVectorImpl<Value *> &Storage) {
Storage.resize(N->getNumOperands());
for (unsigned I = 0, E = N->getNumOperands(); I != E; ++I)
Storage[I] = N->getOperand(I);
Ops = Storage;
Hash = hash_combine_range(Ops.begin(), Ops.end());
}
KeyTy(GenericMDNode *N)
: Ops(N->op_begin(), N->op_end()), Hash(N->getHash()) {}

bool operator==(const GenericMDNode *RHS) const {
if (RHS == getEmptyKey() || RHS == getTombstoneKey())
return false;
if (Hash != RHS->getHash() || Ops.size() != RHS->getNumOperands())
if (Hash != RHS->getHash())
return false;
for (unsigned I = 0, E = Ops.size(); I != E; ++I)
if (Ops[I] != RHS->getOperand(I))
return false;
return true;
assert((RawOps.empty() || Ops.empty()) && "Two sets of operands?");
return RawOps.empty() ? compareOps(Ops, RHS) : compareOps(RawOps, RHS);
}
template <class T>
static bool compareOps(ArrayRef<T> Ops, const GenericMDNode *RHS) {
if (Ops.size() != RHS->getNumOperands())
return false;
return std::equal(Ops.begin(), Ops.end(), RHS->op_begin());
}
};
static inline GenericMDNode *getEmptyKey() {
Expand All @@ -215,29 +215,6 @@ struct GenericMDNodeInfo {
}
};

/// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps
/// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp.
class DebugRecVH : public CallbackVH {
/// Ctx - This is the LLVM Context being referenced.
LLVMContextImpl *Ctx;

/// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that
/// this reference lives in. If this is zero, then it represents a
/// non-canonical entry that has no DenseMap value. This can happen due to
/// RAUW.
int Idx;
public:
DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx)
: CallbackVH(n), Ctx(ctx), Idx(idx) {}

MDNode *get() const {
return cast_or_null<MDNode>(getValPtr());
}

void deleted() override;
void allUsesReplacedWith(Value *VNew) override;
};

class LLVMContextImpl {
public:
/// OwnedModules - The set of modules instantiated in this context, and which
Expand Down Expand Up @@ -265,6 +242,8 @@ class LLVMContextImpl {
FoldingSet<AttributeSetNode> AttrsSetNodes;

StringMap<MDString> MDStringCache;
DenseMap<Value *, ValueAsMetadata *> ValuesAsMetadata;
DenseMap<Metadata *, MetadataAsValue *> MetadataAsValues;

DenseSet<GenericMDNode *, GenericMDNodeInfo> MDNodeSet;

Expand Down Expand Up @@ -301,7 +280,8 @@ class LLVMContextImpl {
ConstantInt *TheFalseVal;

LeakDetectorImpl<Value> LLVMObjects;

LeakDetectorImpl<Metadata> LLVMMDObjects;

// Basic type instances.
Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy;
Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy;
Expand Down Expand Up @@ -335,32 +315,14 @@ class LLVMContextImpl {

/// CustomMDKindNames - Map to hold the metadata string to ID mapping.
StringMap<unsigned> CustomMDKindNames;
typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy;

typedef std::pair<unsigned, TrackingMDNodeRef> MDPairTy;
typedef SmallVector<MDPairTy, 2> MDMapTy;

/// MetadataStore - Collection of per-instruction metadata used in this
/// context.
DenseMap<const Instruction *, MDMapTy> MetadataStore;

/// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope
/// entry with no "inlined at" element.
DenseMap<MDNode*, int> ScopeRecordIdx;

/// ScopeRecords - These are the actual mdnodes (in a value handle) for an
/// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if
/// the MDNode is RAUW'd.
std::vector<DebugRecVH> ScopeRecords;

/// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an
/// scope/inlined-at pair.
DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx;

/// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles)
/// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up
/// to date.
std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords;

/// DiscriminatorTable - This table maps file:line locations to an
/// integer representing the next DWARF path discriminator to assign to
/// instructions in different blocks at the same location.
Expand Down
37 changes: 22 additions & 15 deletions llvm/lib/IR/MDBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ MDString *MDBuilder::createString(StringRef Str) {
return MDString::get(Context, Str);
}

ConstantAsMetadata *MDBuilder::createConstant(Constant *C) {
return ConstantAsMetadata::get(C);
}

MDNode *MDBuilder::createFPMath(float Accuracy) {
if (Accuracy == 0.0)
return nullptr;
assert(Accuracy > 0.0 && "Invalid fpmath accuracy!");
Value *Op = ConstantFP::get(Type::getFloatTy(Context), Accuracy);
auto *Op =
createConstant(ConstantFP::get(Type::getFloatTy(Context), Accuracy));
return MDNode::get(Context, Op);
}

Expand All @@ -38,12 +43,12 @@ MDNode *MDBuilder::createBranchWeights(uint32_t TrueWeight,
MDNode *MDBuilder::createBranchWeights(ArrayRef<uint32_t> Weights) {
assert(Weights.size() >= 2 && "Need at least two branch weights!");

SmallVector<Value *, 4> Vals(Weights.size() + 1);
SmallVector<Metadata *, 4> Vals(Weights.size() + 1);
Vals[0] = createString("branch_weights");

Type *Int32Ty = Type::getInt32Ty(Context);
for (unsigned i = 0, e = Weights.size(); i != e; ++i)
Vals[i + 1] = ConstantInt::get(Int32Ty, Weights[i]);
Vals[i + 1] = createConstant(ConstantInt::get(Int32Ty, Weights[i]));

return MDNode::get(Context, Vals);
}
Expand All @@ -56,15 +61,16 @@ MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {

// Return the range [Lo, Hi).
Type *Ty = IntegerType::get(Context, Lo.getBitWidth());
Value *Range[2] = {ConstantInt::get(Ty, Lo), ConstantInt::get(Ty, Hi)};
Metadata *Range[2] = {createConstant(ConstantInt::get(Ty, Lo)),
createConstant(ConstantInt::get(Ty, Hi))};
return MDNode::get(Context, Range);
}

MDNode *MDBuilder::createAnonymousAARoot(StringRef Name, MDNode *Extra) {
// To ensure uniqueness the root node is self-referential.
MDNode *Dummy = MDNode::getTemporary(Context, None);

SmallVector<Value *, 3> Args(1, Dummy);
SmallVector<Metadata *, 3> Args(1, Dummy);
if (Extra)
Args.push_back(Extra);
if (!Name.empty())
Expand Down Expand Up @@ -92,10 +98,10 @@ MDNode *MDBuilder::createTBAANode(StringRef Name, MDNode *Parent,
bool isConstant) {
if (isConstant) {
Constant *Flags = ConstantInt::get(Type::getInt64Ty(Context), 1);
Value *Ops[3] = {createString(Name), Parent, Flags};
Metadata *Ops[3] = {createString(Name), Parent, createConstant(Flags)};
return MDNode::get(Context, Ops);
} else {
Value *Ops[2] = {createString(Name), Parent};
Metadata *Ops[2] = {createString(Name), Parent};
return MDNode::get(Context, Ops);
}
}
Expand All @@ -105,18 +111,18 @@ MDNode *MDBuilder::createAliasScopeDomain(StringRef Name) {
}

MDNode *MDBuilder::createAliasScope(StringRef Name, MDNode *Domain) {
Value *Ops[2] = { createString(Name), Domain };
Metadata *Ops[2] = {createString(Name), Domain};
return MDNode::get(Context, Ops);
}

/// \brief Return metadata for a tbaa.struct node with the given
/// struct field descriptions.
MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
SmallVector<Value *, 4> Vals(Fields.size() * 3);
SmallVector<Metadata *, 4> Vals(Fields.size() * 3);
Type *Int64 = Type::getInt64Ty(Context);
for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Vals[i * 3 + 0] = ConstantInt::get(Int64, Fields[i].Offset);
Vals[i * 3 + 1] = ConstantInt::get(Int64, Fields[i].Size);
Vals[i * 3 + 0] = createConstant(ConstantInt::get(Int64, Fields[i].Offset));
Vals[i * 3 + 1] = createConstant(ConstantInt::get(Int64, Fields[i].Size));
Vals[i * 3 + 2] = Fields[i].TBAA;
}
return MDNode::get(Context, Vals);
Expand All @@ -126,12 +132,12 @@ MDNode *MDBuilder::createTBAAStructNode(ArrayRef<TBAAStructField> Fields) {
/// with the given name, a list of pairs (offset, field type in the type DAG).
MDNode *MDBuilder::createTBAAStructTypeNode(
StringRef Name, ArrayRef<std::pair<MDNode *, uint64_t>> Fields) {
SmallVector<Value *, 4> Ops(Fields.size() * 2 + 1);
SmallVector<Metadata *, 4> Ops(Fields.size() * 2 + 1);
Type *Int64 = Type::getInt64Ty(Context);
Ops[0] = createString(Name);
for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Ops[i * 2 + 1] = Fields[i].first;
Ops[i * 2 + 2] = ConstantInt::get(Int64, Fields[i].second);
Ops[i * 2 + 2] = createConstant(ConstantInt::get(Int64, Fields[i].second));
}
return MDNode::get(Context, Ops);
}
Expand All @@ -141,7 +147,7 @@ MDNode *MDBuilder::createTBAAStructTypeNode(
MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
uint64_t Offset) {
ConstantInt *Off = ConstantInt::get(Type::getInt64Ty(Context), Offset);
Value *Ops[3] = {createString(Name), Parent, Off};
Metadata *Ops[3] = {createString(Name), Parent, createConstant(Off)};
return MDNode::get(Context, Ops);
}

Expand All @@ -150,6 +156,7 @@ MDNode *MDBuilder::createTBAAScalarTypeNode(StringRef Name, MDNode *Parent,
MDNode *MDBuilder::createTBAAStructTagNode(MDNode *BaseType, MDNode *AccessType,
uint64_t Offset) {
Type *Int64 = Type::getInt64Ty(Context);
Value *Ops[3] = {BaseType, AccessType, ConstantInt::get(Int64, Offset)};
Metadata *Ops[3] = {BaseType, AccessType,
createConstant(ConstantInt::get(Int64, Offset))};
return MDNode::get(Context, Ops);
}
863 changes: 550 additions & 313 deletions llvm/lib/IR/Metadata.cpp

Large diffs are not rendered by default.

58 changes: 58 additions & 0 deletions llvm/lib/IR/MetadataTracking.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//===- MetadataTracking.cpp - Implement metadata tracking -----------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements Metadata tracking.
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/MetadataTracking.h"
#include "llvm/IR/Metadata.h"

using namespace llvm;

ReplaceableMetadataImpl *ReplaceableMetadataImpl::get(Metadata &MD) {
if (auto *N = dyn_cast<MDNode>(&MD)) {
if (auto *G = dyn_cast<GenericMDNode>(N))
return G->ReplaceableUses.get();
return cast<MDNodeFwdDecl>(N);
}
return dyn_cast<ValueAsMetadata>(&MD);
}

bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
assert(Ref && "Expected live reference");
assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
"Reference without owner must be direct");
if (auto *R = ReplaceableMetadataImpl::get(MD)) {
R->addRef(Ref, Owner);
return true;
}
return false;
}

void MetadataTracking::untrack(void *Ref, Metadata &MD) {
assert(Ref && "Expected live reference");
if (auto *R = ReplaceableMetadataImpl::get(MD))
R->dropRef(Ref);
}

bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
assert(Ref && "Expected live reference");
assert(New && "Expected live reference");
assert(Ref != New && "Expected change");
if (auto *R = ReplaceableMetadataImpl::get(MD)) {
R->moveRef(Ref, New, MD);
return true;
}
return false;
}

bool MetadataTracking::isReplaceable(const Metadata &MD) {
return ReplaceableMetadataImpl::get(const_cast<Metadata &>(MD));
}
31 changes: 18 additions & 13 deletions llvm/lib/IR/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ void Module::eraseNamedMetadata(NamedMDNode *NMD) {
NamedMDList.erase(NMD);
}

bool Module::isValidModFlagBehavior(Value *V, ModFlagBehavior &MFB) {
if (ConstantInt *Behavior = dyn_cast<ConstantInt>(V)) {
bool Module::isValidModFlagBehavior(Metadata *MD, ModFlagBehavior &MFB) {
if (ConstantInt *Behavior = mdconst::dyn_extract<ConstantInt>(MD)) {
uint64_t Val = Behavior->getLimitedValue();
if (Val >= ModFlagBehaviorFirstVal && Val <= ModFlagBehaviorLastVal) {
MFB = static_cast<ModFlagBehavior>(Val);
Expand All @@ -285,15 +285,15 @@ getModuleFlagsMetadata(SmallVectorImpl<ModuleFlagEntry> &Flags) const {
// Check the operands of the MDNode before accessing the operands.
// The verifier will actually catch these failures.
MDString *Key = cast<MDString>(Flag->getOperand(1));
Value *Val = Flag->getOperand(2);
Metadata *Val = Flag->getOperand(2);
Flags.push_back(ModuleFlagEntry(MFB, Key, Val));
}
}
}

/// Return the corresponding value if Key appears in module flags, otherwise
/// return null.
Value *Module::getModuleFlag(StringRef Key) const {
Metadata *Module::getModuleFlag(StringRef Key) const {
SmallVector<Module::ModuleFlagEntry, 8> ModuleFlags;
getModuleFlagsMetadata(ModuleFlags);
for (const ModuleFlagEntry &MFE : ModuleFlags) {
Expand Down Expand Up @@ -321,13 +321,17 @@ NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
/// metadata. It will create the module-level flags named metadata if it doesn't
/// already exist.
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Value *Val) {
Metadata *Val) {
Type *Int32Ty = Type::getInt32Ty(Context);
Value *Ops[3] = {
ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
};
Metadata *Ops[3] = {
ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Behavior)),
MDString::get(Context, Key), Val};
getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
}
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
Constant *Val) {
addModuleFlag(Behavior, Key, ConstantAsMetadata::get(Val));
}
void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
uint32_t Val) {
Type *Int32Ty = Type::getInt32Ty(Context);
Expand All @@ -336,7 +340,7 @@ void Module::addModuleFlag(ModFlagBehavior Behavior, StringRef Key,
void Module::addModuleFlag(MDNode *Node) {
assert(Node->getNumOperands() == 3 &&
"Invalid number of operands for module flag!");
assert(isa<ConstantInt>(Node->getOperand(0)) &&
assert(mdconst::hasa<ConstantInt>(Node->getOperand(0)) &&
isa<MDString>(Node->getOperand(1)) &&
"Invalid operand types for module flag!");
getOrInsertModuleFlagsMetadata()->addOperand(Node);
Expand Down Expand Up @@ -459,10 +463,10 @@ void Module::dropAllReferences() {
}

unsigned Module::getDwarfVersion() const {
Value *Val = getModuleFlag("Dwarf Version");
auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("Dwarf Version"));
if (!Val)
return dwarf::DWARF_VERSION;
return cast<ConstantInt>(Val)->getZExtValue();
return cast<ConstantInt>(Val->getValue())->getZExtValue();
}

Comdat *Module::getOrInsertComdat(StringRef Name) {
Expand All @@ -472,12 +476,13 @@ Comdat *Module::getOrInsertComdat(StringRef Name) {
}

PICLevel::Level Module::getPICLevel() const {
Value *Val = getModuleFlag("PIC Level");
auto *Val = cast_or_null<ConstantAsMetadata>(getModuleFlag("PIC Level"));

if (Val == NULL)
return PICLevel::Default;

return static_cast<PICLevel::Level>(cast<ConstantInt>(Val)->getZExtValue());
return static_cast<PICLevel::Level>(
cast<ConstantInt>(Val->getValue())->getZExtValue());
}

void Module::setPICLevel(PICLevel::Level PL) {
Expand Down
27 changes: 21 additions & 6 deletions llvm/lib/IR/TypeFinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,13 @@ void TypeFinder::incorporateType(Type *Ty) {
/// other ways. GlobalValues, basic blocks, instructions, and inst operands are
/// all explicitly enumerated.
void TypeFinder::incorporateValue(const Value *V) {
if (const MDNode *M = dyn_cast<MDNode>(V))
return incorporateMDNode(M);
if (const auto *M = dyn_cast<MetadataAsValue>(V)) {
if (const auto *N = dyn_cast<MDNode>(M->getMetadata()))
return incorporateMDNode(N);
if (const auto *MDV = dyn_cast<ValueAsMetadata>(M->getMetadata()))
return incorporateValue(MDV->getValue());
return;
}

if (!isa<Constant>(V) || isa<GlobalValue>(V)) return;

Expand All @@ -152,11 +157,21 @@ void TypeFinder::incorporateValue(const Value *V) {
/// find types hiding within.
void TypeFinder::incorporateMDNode(const MDNode *V) {
// Already visited?
if (!VisitedConstants.insert(V).second)
if (!VisitedMetadata.insert(V).second)
return;

// Look in operands for types.
for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i)
if (Value *Op = V->getOperand(i))
incorporateValue(Op);
for (unsigned i = 0, e = V->getNumOperands(); i != e; ++i) {
Metadata *Op = V->getOperand(i);
if (!Op)
continue;
if (auto *N = dyn_cast<MDNode>(Op)) {
incorporateMDNode(N);
continue;
}
if (auto *C = dyn_cast<ConstantAsMetadata>(Op)) {
incorporateValue(C->getValue());
continue;
}
}
}
70 changes: 34 additions & 36 deletions llvm/lib/IR/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ static inline Type *checkType(Type *Ty) {
}

Value::Value(Type *ty, unsigned scid)
: VTy(checkType(ty)), UseList(nullptr), Name(nullptr), SubclassID(scid),
HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
NumOperands(0) {
: VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0),
SubclassOptionalData(0), SubclassData(0), NumOperands(0) {
// FIXME: Why isn't this in the subclass gunk??
// Note, we cannot call isa<CallInst> before the CallInst has been
// constructed.
Expand All @@ -63,6 +62,8 @@ Value::~Value() {
// Notify all ValueHandles (if present) that this value is going away.
if (HasValueHandle)
ValueHandleBase::ValueIsDeleted(this);
if (isUsedByMetadata())
ValueAsMetadata::handleDeletion(this);

#ifndef NDEBUG // Only in -g mode...
// Check to make sure that there are no uses of this value that are still
Expand All @@ -82,13 +83,19 @@ Value::~Value() {

// If this value is named, destroy the name. This should not be in a symtab
// at this point.
if (Name && SubclassID != MDStringVal)
Name->Destroy();
destroyValueName();

// There should be no uses of this object anymore, remove it.
LeakDetector::removeGarbageObject(this);
}

void Value::destroyValueName() {
ValueName *Name = getValueName();
if (Name)
Name->Destroy();
setValueName(nullptr);
}

bool Value::hasNUses(unsigned N) const {
const_use_iterator UI = use_begin(), E = use_end();

Expand Down Expand Up @@ -146,9 +153,7 @@ static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
} else if (Argument *A = dyn_cast<Argument>(V)) {
if (Function *P = A->getParent())
ST = &P->getValueSymbolTable();
} else if (isa<MDString>(V))
return true;
else {
} else {
assert(isa<Constant>(V) && "Unknown value type!");
return true; // no name is setable for this.
}
Expand All @@ -159,14 +164,12 @@ StringRef Value::getName() const {
// Make sure the empty string is still a C string. For historical reasons,
// some clients want to call .data() on the result and expect it to be null
// terminated.
if (!Name) return StringRef("", 0);
return Name->getKey();
if (!getValueName())
return StringRef("", 0);
return getValueName()->getKey();
}

void Value::setName(const Twine &NewName) {
assert(SubclassID != MDStringVal &&
"Cannot set the name of MDString with this method!");

// Fast path for common IRBuilder case of setName("") when there is no name.
if (NewName.isTriviallyEmpty() && !hasName())
return;
Expand All @@ -193,42 +196,36 @@ void Value::setName(const Twine &NewName) {
if (!ST) { // No symbol table to update? Just do the change.
if (NameRef.empty()) {
// Free the name for this value.
Name->Destroy();
Name = nullptr;
destroyValueName();
return;
}

if (Name)
Name->Destroy();

// NOTE: Could optimize for the case the name is shrinking to not deallocate
// then reallocated.
destroyValueName();

// Create the new name.
Name = ValueName::Create(NameRef);
Name->setValue(this);
setValueName(ValueName::Create(NameRef));
getValueName()->setValue(this);
return;
}

// NOTE: Could optimize for the case the name is shrinking to not deallocate
// then reallocated.
if (hasName()) {
// Remove old name.
ST->removeValueName(Name);
Name->Destroy();
Name = nullptr;
ST->removeValueName(getValueName());
destroyValueName();

if (NameRef.empty())
return;
}

// Name is changing to something new.
Name = ST->createValueName(NameRef, this);
setValueName(ST->createValueName(NameRef, this));
}

void Value::takeName(Value *V) {
assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!");

ValueSymbolTable *ST = nullptr;
// If this value has a name, drop it.
if (hasName()) {
Expand All @@ -242,9 +239,8 @@ void Value::takeName(Value *V) {

// Remove old name.
if (ST)
ST->removeValueName(Name);
Name->Destroy();
Name = nullptr;
ST->removeValueName(getValueName());
destroyValueName();
}

// Now we know that this has no name.
Expand All @@ -270,20 +266,20 @@ void Value::takeName(Value *V) {
// This works even if both values have no symtab yet.
if (ST == VST) {
// Take the name!
Name = V->Name;
V->Name = nullptr;
Name->setValue(this);
setValueName(V->getValueName());
V->setValueName(nullptr);
getValueName()->setValue(this);
return;
}

// Otherwise, things are slightly more complex. Remove V's name from VST and
// then reinsert it into ST.

if (VST)
VST->removeValueName(V->Name);
Name = V->Name;
V->Name = nullptr;
Name->setValue(this);
VST->removeValueName(V->getValueName());
setValueName(V->getValueName());
V->setValueName(nullptr);
getValueName()->setValue(this);

if (ST)
ST->reinsertValue(this);
Expand Down Expand Up @@ -334,6 +330,8 @@ void Value::replaceAllUsesWith(Value *New) {
// Notify all ValueHandles (if present) that this value is going away.
if (HasValueHandle)
ValueHandleBase::ValueIsRAUWd(this, New);
if (isUsedByMetadata())
ValueAsMetadata::handleRAUW(this, New);

while (!use_empty()) {
Use &U = *UseList;
Expand Down
10 changes: 5 additions & 5 deletions llvm/lib/IR/ValueSymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ void ValueSymbolTable::reinsertValue(Value* V) {
assert(V->hasName() && "Can't insert nameless Value into symbol table");

// Try inserting the name, assuming it won't conflict.
if (vmap.insert(V->Name)) {
//DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n");
if (vmap.insert(V->getValueName())) {
//DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
return;
}

// Otherwise, there is a naming conflict. Rename this value.
SmallString<256> UniqueName(V->getName().begin(), V->getName().end());

// The name is too already used, just free it so we can allocate a new name.
V->Name->Destroy();
V->getValueName()->Destroy();

unsigned BaseSize = UniqueName.size();
while (1) {
// Trim any suffix off and append the next number.
Expand All @@ -59,7 +59,7 @@ void ValueSymbolTable::reinsertValue(Value* V) {
auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
if (IterBool.second) {
// Newly inserted name. Success!
V->Name = &*IterBool.first;
V->setValueName(&*IterBool.first);
//DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
return;
}
Expand Down
141 changes: 99 additions & 42 deletions llvm/lib/IR/Verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ struct VerifierSupport {
}
}

void WriteMetadata(const Metadata *MD) {
if (!MD)
return;
MD->printAsOperand(OS, true, M);
OS << '\n';
}

void WriteType(Type *T) {
if (!T)
return;
Expand All @@ -128,6 +135,24 @@ struct VerifierSupport {
Broken = true;
}

void CheckFailed(const Twine &Message, const Metadata *V1, const Metadata *V2,
const Metadata *V3 = nullptr, const Metadata *V4 = nullptr) {
OS << Message.str() << "\n";
WriteMetadata(V1);
WriteMetadata(V2);
WriteMetadata(V3);
WriteMetadata(V4);
Broken = true;
}

void CheckFailed(const Twine &Message, const Metadata *V1,
const Value *V2 = nullptr) {
OS << Message.str() << "\n";
WriteMetadata(V1);
WriteValue(V2);
Broken = true;
}

void CheckFailed(const Twine &Message, const Value *V1, Type *T2,
const Value *V3 = nullptr) {
OS << Message.str() << "\n";
Expand Down Expand Up @@ -167,7 +192,7 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
SmallPtrSet<Instruction *, 16> InstsInThisBlock;

/// \brief Keep track of the metadata nodes that have been checked already.
SmallPtrSet<MDNode *, 32> MDNodes;
SmallPtrSet<Metadata *, 32> MDNodes;

/// \brief The personality function referenced by the LandingPadInsts.
/// All LandingPadInsts within the same function must use the same
Expand Down Expand Up @@ -261,7 +286,9 @@ class Verifier : public InstVisitor<Verifier>, VerifierSupport {
void visitAliaseeSubExpr(SmallPtrSetImpl<const GlobalAlias *> &Visited,
const GlobalAlias &A, const Constant &C);
void visitNamedMDNode(const NamedMDNode &NMD);
void visitMDNode(MDNode &MD, Function *F);
void visitMDNode(MDNode &MD);
void visitMetadataAsValue(MetadataAsValue &MD, Function *F);
void visitValueAsMetadata(ValueAsMetadata &MD, Function *F);
void visitComdat(const Comdat &C);
void visitModuleIdents(const Module &M);
void visitModuleFlags(const Module &M);
Expand Down Expand Up @@ -560,46 +587,77 @@ void Verifier::visitNamedMDNode(const NamedMDNode &NMD) {
if (!MD)
continue;

Assert1(!MD->isFunctionLocal(),
"Named metadata operand cannot be function local!", MD);
visitMDNode(*MD, nullptr);
visitMDNode(*MD);
}
}

void Verifier::visitMDNode(MDNode &MD, Function *F) {
void Verifier::visitMDNode(MDNode &MD) {
// Only visit each node once. Metadata can be mutually recursive, so this
// avoids infinite recursion here, as well as being an optimization.
if (!MDNodes.insert(&MD).second)
return;

for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {
Value *Op = MD.getOperand(i);
Metadata *Op = MD.getOperand(i);
if (!Op)
continue;
if (isa<Constant>(Op) || isa<MDString>(Op))
Assert2(!isa<LocalAsMetadata>(Op), "Invalid operand for global metadata!",
&MD, Op);
if (auto *N = dyn_cast<MDNode>(Op)) {
visitMDNode(*N);
continue;
if (MDNode *N = dyn_cast<MDNode>(Op)) {
Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(),
"Global metadata operand cannot be function local!", &MD, N);
visitMDNode(*N, F);
}
if (auto *V = dyn_cast<ValueAsMetadata>(Op)) {
visitValueAsMetadata(*V, nullptr);
continue;
}
Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op);

// If this was an instruction, bb, or argument, verify that it is in the
// function that we expect.
Function *ActualF = nullptr;
if (Instruction *I = dyn_cast<Instruction>(Op))
ActualF = I->getParent()->getParent();
else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op))
ActualF = BB->getParent();
else if (Argument *A = dyn_cast<Argument>(Op))
ActualF = A->getParent();
assert(ActualF && "Unimplemented function local metadata case!");

Assert2(ActualF == F, "function-local metadata used in wrong function",
&MD, Op);
}

// Check these last, so we diagnose problems in operands first.
Assert1(!isa<MDNodeFwdDecl>(MD), "Expected no forward declarations!", &MD);
Assert1(MD.isResolved(), "All nodes should be resolved!", &MD);
}

void Verifier::visitValueAsMetadata(ValueAsMetadata &MD, Function *F) {
Assert1(MD.getValue(), "Expected valid value", &MD);
Assert2(!MD.getValue()->getType()->isMetadataTy(),
"Unexpected metadata round-trip through values", &MD, MD.getValue());

auto *L = dyn_cast<LocalAsMetadata>(&MD);
if (!L)
return;

Assert1(F, "function-local metadata used outside a function", L);

// If this was an instruction, bb, or argument, verify that it is in the
// function that we expect.
Function *ActualF = nullptr;
if (Instruction *I = dyn_cast<Instruction>(L->getValue())) {
Assert2(I->getParent(), "function-local metadata not in basic block", L, I);
ActualF = I->getParent()->getParent();
} else if (BasicBlock *BB = dyn_cast<BasicBlock>(L->getValue()))
ActualF = BB->getParent();
else if (Argument *A = dyn_cast<Argument>(L->getValue()))
ActualF = A->getParent();
assert(ActualF && "Unimplemented function local metadata case!");

Assert1(ActualF == F, "function-local metadata used in wrong function", L);
}

void Verifier::visitMetadataAsValue(MetadataAsValue &MDV, Function *F) {
Metadata *MD = MDV.getMetadata();
if (auto *N = dyn_cast<MDNode>(MD)) {
visitMDNode(*N);
return;
}

// Only visit each node once. Metadata can be mutually recursive, so this
// avoids infinite recursion here, as well as being an optimization.
if (!MDNodes.insert(MD).second)
return;

if (auto *V = dyn_cast<ValueAsMetadata>(MD))
visitValueAsMetadata(*V, F);
}

void Verifier::visitComdat(const Comdat &C) {
Expand Down Expand Up @@ -650,7 +708,7 @@ void Verifier::visitModuleFlags(const Module &M) {
for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
const MDNode *Requirement = Requirements[I];
const MDString *Flag = cast<MDString>(Requirement->getOperand(0));
const Value *ReqValue = Requirement->getOperand(1);
const Metadata *ReqValue = Requirement->getOperand(1);

const MDNode *Op = SeenIDs.lookup(Flag);
if (!Op) {
Expand Down Expand Up @@ -679,7 +737,7 @@ Verifier::visitModuleFlag(const MDNode *Op,
Module::ModFlagBehavior MFB;
if (!Module::isValidModFlagBehavior(Op->getOperand(0), MFB)) {
Assert1(
dyn_cast<ConstantInt>(Op->getOperand(0)),
mdconst::dyn_extract<ConstantInt>(Op->getOperand(0)),
"invalid behavior operand in module flag (expected constant integer)",
Op->getOperand(0));
Assert1(false,
Expand Down Expand Up @@ -1907,9 +1965,11 @@ void Verifier::visitRangeMetadata(Instruction& I,

ConstantRange LastRange(1); // Dummy initial value
for (unsigned i = 0; i < NumRanges; ++i) {
ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i));
ConstantInt *Low =
mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i));
Assert1(Low, "The lower limit must be an integer!", Low);
ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1));
ConstantInt *High =
mdconst::dyn_extract<ConstantInt>(Range->getOperand(2 * i + 1));
Assert1(High, "The upper limit must be an integer!", High);
Assert1(High->getType() == Low->getType() &&
High->getType() == Ty, "Range types must match instruction type!",
Expand All @@ -1932,9 +1992,9 @@ void Verifier::visitRangeMetadata(Instruction& I,
}
if (NumRanges > 2) {
APInt FirstLow =
dyn_cast<ConstantInt>(Range->getOperand(0))->getValue();
mdconst::dyn_extract<ConstantInt>(Range->getOperand(0))->getValue();
APInt FirstHigh =
dyn_cast<ConstantInt>(Range->getOperand(1))->getValue();
mdconst::dyn_extract<ConstantInt>(Range->getOperand(1))->getValue();
ConstantRange FirstRange(FirstLow, FirstHigh);
Assert1(FirstRange.intersectWith(LastRange).isEmptySet(),
"Intervals are overlapping", Range);
Expand Down Expand Up @@ -2278,8 +2338,8 @@ void Verifier::visitInstruction(Instruction &I) {
Assert1(I.getType()->isFPOrFPVectorTy(),
"fpmath requires a floating point result!", &I);
Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
Value *Op0 = MD->getOperand(0);
if (ConstantFP *CFP0 = dyn_cast_or_null<ConstantFP>(Op0)) {
if (ConstantFP *CFP0 =
mdconst::dyn_extract_or_null<ConstantFP>(MD->getOperand(0))) {
APFloat Accuracy = CFP0->getValueAPF();
Assert1(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
"fpmath accuracy not a positive number!", &I);
Expand Down Expand Up @@ -2496,8 +2556,8 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
// If the intrinsic takes MDNode arguments, verify that they are either global
// or are local to *this* function.
for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i)
if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i)))
visitMDNode(*MD, CI.getParent()->getParent());
if (auto *MD = dyn_cast<MetadataAsValue>(CI.getArgOperand(i)))
visitMetadataAsValue(*MD, CI.getParent()->getParent());

switch (ID) {
default:
Expand All @@ -2509,11 +2569,8 @@ void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
"constant int", &CI);
break;
case Intrinsic::dbg_declare: { // llvm.dbg.declare
Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)),
"invalid llvm.dbg.declare intrinsic call 1", &CI);
MDNode *MD = cast<MDNode>(CI.getArgOperand(0));
Assert1(MD->getNumOperands() == 1,
"invalid llvm.dbg.declare intrinsic call 2", &CI);
Assert1(CI.getArgOperand(0) && isa<MetadataAsValue>(CI.getArgOperand(0)),
"invalid llvm.dbg.declare intrinsic call 1", &CI);
} break;
case Intrinsic::memcpy:
case Intrinsic::memmove:
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/LTO/LTOModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ bool LTOModule::parseSymbols(std::string &errMsg) {
/// parseMetadata - Parse metadata from the module
void LTOModule::parseMetadata() {
// Linker Options
if (Value *Val = getModule().getModuleFlag("Linker Options")) {
if (Metadata *Val = getModule().getModuleFlag("Linker Options")) {
MDNode *LinkerOptions = cast<MDNode>(Val);
for (unsigned i = 0, e = LinkerOptions->getNumOperands(); i != e; ++i) {
MDNode *MDOptions = cast<MDNode>(LinkerOptions->getOperand(i));
Expand Down
37 changes: 18 additions & 19 deletions llvm/lib/Linker/LinkModules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1265,7 +1265,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
SmallSetVector<MDNode*, 16> Requirements;
for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
MDNode *Op = DstModFlags->getOperand(I);
ConstantInt *Behavior = cast<ConstantInt>(Op->getOperand(0));
ConstantInt *Behavior = mdconst::extract<ConstantInt>(Op->getOperand(0));
MDString *ID = cast<MDString>(Op->getOperand(1));

if (Behavior->getZExtValue() == Module::Require) {
Expand All @@ -1280,7 +1280,8 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
bool HasErr = false;
for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
MDNode *SrcOp = SrcModFlags->getOperand(I);
ConstantInt *SrcBehavior = cast<ConstantInt>(SrcOp->getOperand(0));
ConstantInt *SrcBehavior =
mdconst::extract<ConstantInt>(SrcOp->getOperand(0));
MDString *ID = cast<MDString>(SrcOp->getOperand(1));
MDNode *DstOp = Flags.lookup(ID);
unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
Expand All @@ -1303,7 +1304,8 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
}

// Otherwise, perform a merge.
ConstantInt *DstBehavior = cast<ConstantInt>(DstOp->getOperand(0));
ConstantInt *DstBehavior =
mdconst::extract<ConstantInt>(DstOp->getOperand(0));
unsigned DstBehaviorValue = DstBehavior->getZExtValue();

// If either flag has override behavior, handle it first.
Expand All @@ -1317,7 +1319,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
continue;
} else if (SrcBehaviorValue == Module::Override) {
// Update the destination flag to that of the source.
DstOp->replaceOperandWith(0, SrcBehavior);
DstOp->replaceOperandWith(0, ConstantAsMetadata::get(SrcBehavior));
DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
continue;
}
Expand Down Expand Up @@ -1352,29 +1354,26 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
case Module::Append: {
MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands();
Value **VP, **Values = VP = new Value*[NumOps];
for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP)
*VP = DstValue->getOperand(i);
for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP)
*VP = SrcValue->getOperand(i);
DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
ArrayRef<Value*>(Values,
NumOps)));
delete[] Values;
SmallVector<Metadata *, 8> MDs;
MDs.reserve(DstValue->getNumOperands() + SrcValue->getNumOperands());
for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
MDs.push_back(DstValue->getOperand(i));
for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
MDs.push_back(SrcValue->getOperand(i));
DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(), MDs));
break;
}
case Module::AppendUnique: {
SmallSetVector<Value*, 16> Elts;
SmallSetVector<Metadata *, 16> Elts;
MDNode *DstValue = cast<MDNode>(DstOp->getOperand(2));
MDNode *SrcValue = cast<MDNode>(SrcOp->getOperand(2));
for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
Elts.insert(DstValue->getOperand(i));
for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
Elts.insert(SrcValue->getOperand(i));
DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
ArrayRef<Value*>(Elts.begin(),
Elts.end())));
DstOp->replaceOperandWith(
2, MDNode::get(DstM->getContext(),
makeArrayRef(Elts.begin(), Elts.end())));
break;
}
}
Expand All @@ -1384,7 +1383,7 @@ bool ModuleLinker::linkModuleFlagsMetadata() {
for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
MDNode *Requirement = Requirements[I];
MDString *Flag = cast<MDString>(Requirement->getOperand(0));
Value *ReqValue = Requirement->getOperand(1);
Metadata *ReqValue = Requirement->getOperand(1);

MDNode *Op = Flags[Flag];
if (!Op || Op->getOperand(2) != ReqValue) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/ARM/ARMAsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ void ARMAsmPrinter::emitAttributes() {
if (const Module *SourceModule = MMI->getModule()) {
// ABI_PCS_wchar_t to indicate wchar_t width
// FIXME: There is no way to emit value 0 (wchar_t prohibited).
if (auto WCharWidthValue = cast_or_null<ConstantInt>(
if (auto WCharWidthValue = mdconst::extract_or_null<ConstantInt>(
SourceModule->getModuleFlag("wchar_size"))) {
int WCharWidth = WCharWidthValue->getZExtValue();
assert((WCharWidth == 2 || WCharWidth == 4) &&
Expand All @@ -809,7 +809,7 @@ void ARMAsmPrinter::emitAttributes() {
// ABI_enum_size to indicate enum width
// FIXME: There is no way to emit value 0 (enums prohibited) or value 3
// (all enums contain a value needing 32 bits to encode).
if (auto EnumWidthValue = cast_or_null<ConstantInt>(
if (auto EnumWidthValue = mdconst::extract_or_null<ConstantInt>(
SourceModule->getModuleFlag("min_enum_size"))) {
int EnumWidth = EnumWidthValue->getZExtValue();
assert((EnumWidth == 1 || EnumWidth == 4) &&
Expand Down
37 changes: 20 additions & 17 deletions llvm/lib/Target/NVPTX/NVPTXGenericToNVVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,31 +394,34 @@ void GenericToNVVM::remapNamedMDNode(Module *M, NamedMDNode *N) {
MDNode *GenericToNVVM::remapMDNode(Module *M, MDNode *N) {

bool OperandChanged = false;
SmallVector<Value *, 8> NewOperands;
SmallVector<Metadata *, 8> NewOperands;
unsigned NumOperands = N->getNumOperands();

// Check if any operand is or contains a global variable in GVMap, and thus
// converted to another value.
for (unsigned i = 0; i < NumOperands; ++i) {
Value *Operand = N->getOperand(i);
Value *NewOperand = Operand;
Metadata *Operand = N->getOperand(i);
Metadata *NewOperand = Operand;
if (Operand) {
if (isa<GlobalVariable>(Operand)) {
GVMapTy::iterator I = GVMap.find(cast<GlobalVariable>(Operand));
if (I != GVMap.end()) {
NewOperand = I->second;
if (++i < NumOperands) {
NewOperands.push_back(NewOperand);
// Address space of the global variable follows the global variable
// in the global variable debug info (see createGlobalVariable in
// lib/Analysis/DIBuilder.cpp).
NewOperand =
ConstantInt::get(Type::getInt32Ty(M->getContext()),
I->second->getType()->getAddressSpace());
if (auto *N = dyn_cast<MDNode>(Operand)) {
NewOperand = remapMDNode(M, N);
} else if (auto *C = dyn_cast<ConstantAsMetadata>(Operand)) {
if (auto *G = dyn_cast<GlobalVariable>(C->getValue())) {
GVMapTy::iterator I = GVMap.find(G);
if (I != GVMap.end()) {
NewOperand = ConstantAsMetadata::get(I->second);
if (++i < NumOperands) {
NewOperands.push_back(NewOperand);
// Address space of the global variable follows the global
// variable
// in the global variable debug info (see createGlobalVariable in
// lib/Analysis/DIBuilder.cpp).
NewOperand = ConstantAsMetadata::get(
ConstantInt::get(Type::getInt32Ty(M->getContext()),
I->second->getType()->getAddressSpace()));
}
}
}
} else if (isa<MDNode>(Operand)) {
NewOperand = remapMDNode(M, cast<MDNode>(Operand));
}
}
OperandChanged |= Operand != NewOperand;
Expand Down
7 changes: 4 additions & 3 deletions llvm/lib/Target/NVPTX/NVPTXUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static void cacheAnnotationFromMD(const MDNode *md, key_val_pair_t &retval) {
assert(prop && "Annotation property not a string");

// value
ConstantInt *Val = dyn_cast<ConstantInt>(md->getOperand(i + 1));
ConstantInt *Val = mdconst::dyn_extract<ConstantInt>(md->getOperand(i + 1));
assert(Val && "Value operand not a constant int");

std::string keyname = prop->getString().str();
Expand All @@ -75,7 +75,8 @@ static void cacheAnnotationFromMD(const Module *m, const GlobalValue *gv) {
for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
const MDNode *elem = NMD->getOperand(i);

Value *entity = elem->getOperand(0);
GlobalValue *entity =
mdconst::dyn_extract_or_null<GlobalValue>(elem->getOperand(0));
// entity may be null due to DCE
if (!entity)
continue;
Expand Down Expand Up @@ -322,7 +323,7 @@ bool llvm::getAlign(const CallInst &I, unsigned index, unsigned &align) {
if (MDNode *alignNode = I.getMetadata("callalign")) {
for (int i = 0, n = alignNode->getNumOperands(); i < n; i++) {
if (const ConstantInt *CI =
dyn_cast<ConstantInt>(alignNode->getOperand(i))) {
mdconst::dyn_extract<ConstantInt>(alignNode->getOperand(i))) {
unsigned v = CI->getZExtValue();
if ((v >> 16) == index) {
align = v & 0xFFFF;
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/IPO/StripSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ bool StripDeadDebugInfo::runOnModule(Module &M) {
// For each compile unit, find the live set of global variables/functions and
// replace the current list of potentially dead global variables/functions
// with the live list.
SmallVector<Value *, 64> LiveGlobalVariables;
SmallVector<Value *, 64> LiveSubprograms;
SmallVector<Metadata *, 64> LiveGlobalVariables;
SmallVector<Metadata *, 64> LiveSubprograms;
DenseSet<const MDNode *> VisitedSet;

for (DICompileUnit DIC : F.compile_units()) {
Expand Down
17 changes: 8 additions & 9 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,14 @@ Instruction *InstCombiner::SimplifyMemTransfer(MemIntrinsic *MI) {
// If the memcpy has metadata describing the members, see if we can
// get the TBAA tag describing our copy.
if (MDNode *M = MI->getMetadata(LLVMContext::MD_tbaa_struct)) {
if (M->getNumOperands() == 3 &&
M->getOperand(0) &&
isa<ConstantInt>(M->getOperand(0)) &&
cast<ConstantInt>(M->getOperand(0))->isNullValue() &&
if (M->getNumOperands() == 3 && M->getOperand(0) &&
mdconst::hasa<ConstantInt>(M->getOperand(0)) &&
mdconst::extract<ConstantInt>(M->getOperand(0))->isNullValue() &&
M->getOperand(1) &&
isa<ConstantInt>(M->getOperand(1)) &&
cast<ConstantInt>(M->getOperand(1))->getValue() == Size &&
M->getOperand(2) &&
isa<MDNode>(M->getOperand(2)))
mdconst::hasa<ConstantInt>(M->getOperand(1)) &&
mdconst::extract<ConstantInt>(M->getOperand(1))->getValue() ==
Size &&
M->getOperand(2) && isa<MDNode>(M->getOperand(2)))
CopyMD = cast<MDNode>(M->getOperand(2));
}
}
Expand Down Expand Up @@ -1129,7 +1128,7 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
cast<Constant>(RHS)->isNullValue()) {
LoadInst* LI = cast<LoadInst>(LHS);
if (isValidAssumeForContext(II, LI, DL, DT)) {
MDNode* MD = MDNode::get(II->getContext(), ArrayRef<Value*>());
MDNode *MD = MDNode::get(II->getContext(), None);
LI->setMetadata(LLVMContext::MD_nonnull, MD);
return EraseInstFromFunction(*II);
}
Expand Down
33 changes: 17 additions & 16 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,10 @@ struct LocationMetadata {
assert(MDN->getNumOperands() == 3);
MDString *MDFilename = cast<MDString>(MDN->getOperand(0));
Filename = MDFilename->getString();
LineNo = cast<ConstantInt>(MDN->getOperand(1))->getLimitedValue();
ColumnNo = cast<ConstantInt>(MDN->getOperand(2))->getLimitedValue();
LineNo =
mdconst::extract<ConstantInt>(MDN->getOperand(1))->getLimitedValue();
ColumnNo =
mdconst::extract<ConstantInt>(MDN->getOperand(2))->getLimitedValue();
}
};

Expand Down Expand Up @@ -249,23 +251,22 @@ class GlobalsMetadata {
for (auto MDN : Globals->operands()) {
// Metadata node contains the global and the fields of "Entry".
assert(MDN->getNumOperands() == 5);
Value *V = MDN->getOperand(0);
auto *GV = mdconst::extract_or_null<GlobalVariable>(MDN->getOperand(0));
// The optimizer may optimize away a global entirely.
if (!V)
if (!GV)
continue;
GlobalVariable *GV = cast<GlobalVariable>(V);
// We can already have an entry for GV if it was merged with another
// global.
Entry &E = Entries[GV];
if (Value *Loc = MDN->getOperand(1))
E.SourceLoc.parse(cast<MDNode>(Loc));
if (Value *Name = MDN->getOperand(2)) {
MDString *MDName = cast<MDString>(Name);
E.Name = MDName->getString();
}
ConstantInt *IsDynInit = cast<ConstantInt>(MDN->getOperand(3));
if (auto *Loc = cast_or_null<MDNode>(MDN->getOperand(1)))
E.SourceLoc.parse(Loc);
if (auto *Name = cast_or_null<MDString>(MDN->getOperand(2)))
E.Name = Name->getString();
ConstantInt *IsDynInit =
mdconst::extract<ConstantInt>(MDN->getOperand(3));
E.IsDynInit |= IsDynInit->isOne();
ConstantInt *IsBlacklisted = cast<ConstantInt>(MDN->getOperand(4));
ConstantInt *IsBlacklisted =
mdconst::extract<ConstantInt>(MDN->getOperand(4));
E.IsBlacklisted |= IsBlacklisted->isOne();
}
}
Expand Down Expand Up @@ -496,9 +497,9 @@ struct FunctionStackPoisoner : public InstVisitor<FunctionStackPoisoner> {
AllocaForValueMapTy AllocaForValue;

FunctionStackPoisoner(Function &F, AddressSanitizer &ASan)
: F(F), ASan(ASan), DIB(*F.getParent()), C(ASan.C),
IntptrTy(ASan.IntptrTy), IntptrPtrTy(PointerType::get(IntptrTy, 0)),
Mapping(ASan.Mapping),
: F(F), ASan(ASan), DIB(*F.getParent(), /*AllowUnresolved*/ false),
C(ASan.C), IntptrTy(ASan.IntptrTy),
IntptrPtrTy(PointerType::get(IntptrTy, 0)), Mapping(ASan.Mapping),
StackAlignment(1 << Mapping.Scale) {}

bool runOnFunction() {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Instrumentation/SanitizerCoverage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ void SanitizerCoverageModule::InjectCoverageAtBlock(Function &F,
Load->setAtomic(Monotonic);
Load->setAlignment(1);
Load->setMetadata(F.getParent()->getMDKindID("nosanitize"),
MDNode::get(*C, ArrayRef<llvm::Value *>()));
MDNode::get(*C, None));
Value *Cmp = IRB.CreateICmpEQ(Constant::getNullValue(Int8Ty), Load);
Instruction *Ins = SplitBlockAndInsertIfThen(
Cmp, IP, false, MDBuilder(*C).createBranchWeights(1, 100000));
Expand Down
8 changes: 3 additions & 5 deletions llvm/lib/Transforms/ObjCARC/ObjCARCOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,11 +880,9 @@ static void AppendMDNodeToInstForPtr(unsigned NodeId,
Sequence OldSeq,
Sequence NewSeq) {
MDNode *Node = nullptr;
Value *tmp[3] = {PtrSourceMDNodeID,
SequenceToMDString(Inst->getContext(),
OldSeq),
SequenceToMDString(Inst->getContext(),
NewSeq)};
Metadata *tmp[3] = {PtrSourceMDNodeID,
SequenceToMDString(Inst->getContext(), OldSeq),
SequenceToMDString(Inst->getContext(), NewSeq)};
Node = MDNode::get(Inst->getContext(), tmp);

Inst->setMetadata(NodeId, Node);
Expand Down
16 changes: 9 additions & 7 deletions llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ static unsigned UnrollCountPragmaValue(const Loop *L) {
if (MD) {
assert(MD->getNumOperands() == 2 &&
"Unroll count hint metadata should have two operands.");
unsigned Count = cast<ConstantInt>(MD->getOperand(1))->getZExtValue();
unsigned Count =
mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue();
assert(Count >= 1 && "Unroll count must be positive.");
return Count;
}
Expand All @@ -288,27 +289,28 @@ static void SetLoopAlreadyUnrolled(Loop *L) {
if (!LoopID) return;

// First remove any existing loop unrolling metadata.
SmallVector<Value *, 4> Vals;
SmallVector<Metadata *, 4> MDs;
// Reserve first location for self reference to the LoopID metadata node.
Vals.push_back(nullptr);
MDs.push_back(nullptr);
for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) {
bool IsUnrollMetadata = false;
MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
if (MD) {
const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
}
if (!IsUnrollMetadata) Vals.push_back(LoopID->getOperand(i));
if (!IsUnrollMetadata)
MDs.push_back(LoopID->getOperand(i));
}

// Add unroll(disable) metadata to disable future unrolling.
LLVMContext &Context = L->getHeader()->getContext();
SmallVector<Value *, 1> DisableOperands;
SmallVector<Metadata *, 1> DisableOperands;
DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
MDNode *DisableNode = MDNode::get(Context, DisableOperands);
Vals.push_back(DisableNode);
MDs.push_back(DisableNode);

MDNode *NewLoopID = MDNode::get(Context, Vals);
MDNode *NewLoopID = MDNode::get(Context, MDs);
// Set operand 0 to refer to the loop id itself.
NewLoopID->replaceOperandWith(0, NewLoopID);
L->setLoopID(NewLoopID);
Expand Down
16 changes: 9 additions & 7 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,12 +807,14 @@ class AllocaPromoter : public LoadAndStorePromoter {
void run(const SmallVectorImpl<Instruction*> &Insts) {
// Retain the debug information attached to the alloca for use when
// rewriting loads and stores.
if (MDNode *DebugNode = MDNode::getIfExists(AI.getContext(), &AI)) {
for (User *U : DebugNode->users())
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
DDIs.push_back(DDI);
else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U))
DVIs.push_back(DVI);
if (auto *L = LocalAsMetadata::getIfExists(&AI)) {
if (auto *DebugNode = MetadataAsValue::getIfExists(AI.getContext(), L)) {
for (User *U : DebugNode->users())
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
DDIs.push_back(DDI);
else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U))
DVIs.push_back(DVI);
}
}

LoadAndStorePromoter::run(Insts);
Expand Down Expand Up @@ -3614,7 +3616,7 @@ bool SROA::promoteAllocas(Function &F) {

DEBUG(dbgs() << "Promoting allocas with SSAUpdater...\n");
SSAUpdater SSA;
DIBuilder DIB(*F.getParent());
DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
SmallVector<Instruction *, 64> Insts;

// We need a worklist to walk the uses of each alloca.
Expand Down
16 changes: 9 additions & 7 deletions llvm/lib/Transforms/Scalar/ScalarReplAggregates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,12 +1068,14 @@ class AllocaPromoter : public LoadAndStorePromoter {
void run(AllocaInst *AI, const SmallVectorImpl<Instruction*> &Insts) {
// Remember which alloca we're promoting (for isInstInList).
this->AI = AI;
if (MDNode *DebugNode = MDNode::getIfExists(AI->getContext(), AI)) {
for (User *U : DebugNode->users())
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
DDIs.push_back(DDI);
else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U))
DVIs.push_back(DVI);
if (auto *L = LocalAsMetadata::getIfExists(AI)) {
if (auto *DebugNode = MetadataAsValue::getIfExists(AI->getContext(), L)) {
for (User *U : DebugNode->users())
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
DDIs.push_back(DDI);
else if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(U))
DVIs.push_back(DVI);
}
}

LoadAndStorePromoter::run(Insts);
Expand Down Expand Up @@ -1420,7 +1422,7 @@ bool SROA::performPromotion(Function &F) {
AssumptionTracker *AT = &getAnalysis<AssumptionTracker>();

BasicBlock &BB = F.getEntryBlock(); // Get the entry node for the function
DIBuilder DIB(*F.getParent());
DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
bool Changed = false;
SmallVector<Instruction*, 64> Insts;
while (1) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Transforms/Utils/AddDiscriminators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ bool AddDiscriminators::runOnFunction(Function &F) {
bool Changed = false;
Module *M = F.getParent();
LLVMContext &Ctx = M->getContext();
DIBuilder Builder(*M);
DIBuilder Builder(*M, /*AllowUnresolved*/ false);

// Traverse all the blocks looking for instructions in different
// blocks that are at the same file:line location.
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Utils/CloneFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ static MDNode* FindSubprogram(const Function *F, DebugInfoFinder &Finder) {

// Add an operand to an existing MDNode. The new operand will be added at the
// back of the operand list.
static void AddOperand(DICompileUnit CU, DIArray SPs, Value *NewSP) {
SmallVector<Value *, 16> NewSPs;
static void AddOperand(DICompileUnit CU, DIArray SPs, Metadata *NewSP) {
SmallVector<Metadata *, 16> NewSPs;
NewSPs.reserve(SPs->getNumOperands() + 1);
for (unsigned I = 0, E = SPs->getNumOperands(); I != E; ++I)
NewSPs.push_back(SPs->getOperand(I));
Expand Down
23 changes: 12 additions & 11 deletions llvm/lib/Transforms/Utils/InlineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ static void CloneAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap) {

// Walk the existing metadata, adding the complete (perhaps cyclic) chain to
// the set.
SmallVector<const Value *, 16> Queue(MD.begin(), MD.end());
SmallVector<const Metadata *, 16> Queue(MD.begin(), MD.end());
while (!Queue.empty()) {
const MDNode *M = cast<MDNode>(Queue.pop_back_val());
for (unsigned i = 0, ie = M->getNumOperands(); i != ie; ++i)
Expand All @@ -320,30 +320,30 @@ static void CloneAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap) {
// Now we have a complete set of all metadata in the chains used to specify
// the noalias scopes and the lists of those scopes.
SmallVector<MDNode *, 16> DummyNodes;
DenseMap<const MDNode *, TrackingVH<MDNode> > MDMap;
DenseMap<const MDNode *, TrackingMDNodeRef> MDMap;
for (SetVector<const MDNode *>::iterator I = MD.begin(), IE = MD.end();
I != IE; ++I) {
MDNode *Dummy = MDNode::getTemporary(CalledFunc->getContext(), None);
DummyNodes.push_back(Dummy);
MDMap[*I] = Dummy;
MDMap[*I].reset(Dummy);
}

// Create new metadata nodes to replace the dummy nodes, replacing old
// metadata references with either a dummy node or an already-created new
// node.
for (SetVector<const MDNode *>::iterator I = MD.begin(), IE = MD.end();
I != IE; ++I) {
SmallVector<Value *, 4> NewOps;
SmallVector<Metadata *, 4> NewOps;
for (unsigned i = 0, ie = (*I)->getNumOperands(); i != ie; ++i) {
const Value *V = (*I)->getOperand(i);
const Metadata *V = (*I)->getOperand(i);
if (const MDNode *M = dyn_cast<MDNode>(V))
NewOps.push_back(MDMap[M]);
else
NewOps.push_back(const_cast<Value *>(V));
NewOps.push_back(const_cast<Metadata *>(V));
}

MDNode *NewM = MDNode::get(CalledFunc->getContext(), NewOps),
*TempM = MDMap[*I];
MDNode *NewM = MDNode::get(CalledFunc->getContext(), NewOps);
MDNodeFwdDecl *TempM = cast<MDNodeFwdDecl>(MDMap[*I]);

TempM->replaceAllUsesWith(NewM);
}
Expand Down Expand Up @@ -516,7 +516,7 @@ static void AddAliasScopeMetadata(CallSite CS, ValueToValueMapTy &VMap,
// need to go through several PHIs to see it, and thus could be
// repeated in the Objects list.
SmallPtrSet<const Value *, 4> ObjSet;
SmallVector<Value *, 4> Scopes, NoAliases;
SmallVector<Metadata *, 4> Scopes, NoAliases;

SmallSetVector<const Argument *, 4> NAPtrArgs;
for (unsigned i = 0, ie = PtrArgs.size(); i != ie; ++i) {
Expand Down Expand Up @@ -869,8 +869,9 @@ static void fixupLineNumbers(Function *Fn, Function::iterator FI,
if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(BI)) {
LLVMContext &Ctx = BI->getContext();
MDNode *InlinedAt = BI->getDebugLoc().getInlinedAt(Ctx);
DVI->setOperand(2, createInlinedVariable(DVI->getVariable(),
InlinedAt, Ctx));
DVI->setOperand(2, MetadataAsValue::get(
Ctx, createInlinedVariable(DVI->getVariable(),
InlinedAt, Ctx)));
}
}
}
Expand Down
20 changes: 12 additions & 8 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
SmallVector<uint32_t, 8> Weights;
for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
++MD_i) {
ConstantInt* CI = dyn_cast<ConstantInt>(MD->getOperand(MD_i));
ConstantInt *CI =
mdconst::dyn_extract<ConstantInt>(MD->getOperand(MD_i));
assert(CI);
Weights.push_back(CI->getValue().getZExtValue());
}
Expand Down Expand Up @@ -208,8 +209,10 @@ bool llvm::ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions,
SI->getDefaultDest());
MDNode *MD = SI->getMetadata(LLVMContext::MD_prof);
if (MD && MD->getNumOperands() == 3) {
ConstantInt *SICase = dyn_cast<ConstantInt>(MD->getOperand(2));
ConstantInt *SIDef = dyn_cast<ConstantInt>(MD->getOperand(1));
ConstantInt *SICase =
mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
ConstantInt *SIDef =
mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
assert(SICase && SIDef);
// The TrueWeight should be the weight for the single case of SI.
NewBr->setMetadata(LLVMContext::MD_prof,
Expand Down Expand Up @@ -1048,7 +1051,7 @@ static bool isArray(AllocaInst *AI) {
/// LowerDbgDeclare - Lowers llvm.dbg.declare intrinsics into appropriate set
/// of llvm.dbg.value intrinsics.
bool llvm::LowerDbgDeclare(Function &F) {
DIBuilder DIB(*F.getParent());
DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
SmallVector<DbgDeclareInst *, 4> Dbgs;
for (auto &FI : F)
for (BasicBlock::iterator BI : FI)
Expand Down Expand Up @@ -1091,10 +1094,11 @@ bool llvm::LowerDbgDeclare(Function &F) {
/// FindAllocaDbgDeclare - Finds the llvm.dbg.declare intrinsic describing the
/// alloca 'V', if any.
DbgDeclareInst *llvm::FindAllocaDbgDeclare(Value *V) {
if (MDNode *DebugNode = MDNode::getIfExists(V->getContext(), V))
for (User *U : DebugNode->users())
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
return DDI;
if (auto *L = LocalAsMetadata::getIfExists(V))
if (auto *MDV = MetadataAsValue::getIfExists(V->getContext(), L))
for (User *U : MDV->users())
if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(U))
return DDI;

return nullptr;
}
Expand Down
13 changes: 7 additions & 6 deletions llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ static void CloneLoopBlocks(Loop *L, Value *NewIter, const bool UnrollProlog,
}
if (NewLoop) {
// Add unroll disable metadata to disable future unrolling for this loop.
SmallVector<Value *, 4> Vals;
SmallVector<Metadata *, 4> MDs;
// Reserve first location for self reference to the LoopID metadata node.
Vals.push_back(nullptr);
MDs.push_back(nullptr);
MDNode *LoopID = NewLoop->getLoopID();
if (LoopID) {
// First remove any existing loop unrolling metadata.
Expand All @@ -230,17 +230,18 @@ static void CloneLoopBlocks(Loop *L, Value *NewIter, const bool UnrollProlog,
const MDString *S = dyn_cast<MDString>(MD->getOperand(0));
IsUnrollMetadata = S && S->getString().startswith("llvm.loop.unroll.");
}
if (!IsUnrollMetadata) Vals.push_back(LoopID->getOperand(i));
if (!IsUnrollMetadata)
MDs.push_back(LoopID->getOperand(i));
}
}

LLVMContext &Context = NewLoop->getHeader()->getContext();
SmallVector<Value *, 1> DisableOperands;
SmallVector<Metadata *, 1> DisableOperands;
DisableOperands.push_back(MDString::get(Context, "llvm.loop.unroll.disable"));
MDNode *DisableNode = MDNode::get(Context, DisableOperands);
Vals.push_back(DisableNode);
MDs.push_back(DisableNode);

MDNode *NewLoopID = MDNode::get(Context, Vals);
MDNode *NewLoopID = MDNode::get(Context, MDs);
// Set operand 0 to refer to the loop id itself.
NewLoopID->replaceOperandWith(0, NewLoopID);
NewLoop->setLoopID(NewLoopID);
Expand Down
9 changes: 6 additions & 3 deletions llvm/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ struct PromoteMem2Reg {
PromoteMem2Reg(ArrayRef<AllocaInst *> Allocas, DominatorTree &DT,
AliasSetTracker *AST, AssumptionTracker *AT)
: Allocas(Allocas.begin(), Allocas.end()), DT(DT),
DIB(*DT.getRoot()->getParent()->getParent()), AST(AST), AT(AT) {}
DIB(*DT.getRoot()->getParent()->getParent(), /*AllowUnresolved*/ false),
AST(AST), AT(AT) {}

void run();

Expand Down Expand Up @@ -415,7 +416,8 @@ static bool rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
// Record debuginfo for the store and remove the declaration's
// debuginfo.
if (DbgDeclareInst *DDI = Info.DbgDeclare) {
DIBuilder DIB(*AI->getParent()->getParent()->getParent());
DIBuilder DIB(*AI->getParent()->getParent()->getParent(),
/*AllowUnresolved*/ false);
ConvertDebugDeclareToDebugValue(DDI, Info.OnlyStore, DIB);
DDI->eraseFromParent();
LBI.deleteValue(DDI);
Expand Down Expand Up @@ -498,7 +500,8 @@ static void promoteSingleBlockAlloca(AllocaInst *AI, const AllocaInfo &Info,
StoreInst *SI = cast<StoreInst>(AI->user_back());
// Record debuginfo for the store before removing it.
if (DbgDeclareInst *DDI = Info.DbgDeclare) {
DIBuilder DIB(*AI->getParent()->getParent()->getParent());
DIBuilder DIB(*AI->getParent()->getParent()->getParent(),
/*AllowUnresolved*/ false);
ConvertDebugDeclareToDebugValue(DDI, SI, DIB);
}
SI->eraseFromParent();
Expand Down
11 changes: 6 additions & 5 deletions llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,7 @@ SimplifyEqualityComparisonWithOnlyPredecessor(TerminatorInst *TI,
if (HasWeight)
for (unsigned MD_i = 1, MD_e = MD->getNumOperands(); MD_i < MD_e;
++MD_i) {
ConstantInt* CI = dyn_cast<ConstantInt>(MD->getOperand(MD_i));
assert(CI);
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(MD_i));
Weights.push_back(CI->getValue().getZExtValue());
}
for (SwitchInst::CaseIt i = SI->case_end(), e = SI->case_begin(); i != e;) {
Expand Down Expand Up @@ -819,7 +818,7 @@ static void GetBranchWeights(TerminatorInst *TI,
MDNode *MD = TI->getMetadata(LLVMContext::MD_prof);
assert(MD);
for (unsigned i = 1, e = MD->getNumOperands(); i < e; ++i) {
ConstantInt *CI = cast<ConstantInt>(MD->getOperand(i));
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(i));
Weights.push_back(CI->getValue().getZExtValue());
}

Expand Down Expand Up @@ -2037,8 +2036,10 @@ static bool ExtractBranchMetadata(BranchInst *BI,
"Looking for probabilities on unconditional branch?");
MDNode *ProfileData = BI->getMetadata(LLVMContext::MD_prof);
if (!ProfileData || ProfileData->getNumOperands() != 3) return false;
ConstantInt *CITrue = dyn_cast<ConstantInt>(ProfileData->getOperand(1));
ConstantInt *CIFalse = dyn_cast<ConstantInt>(ProfileData->getOperand(2));
ConstantInt *CITrue =
mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
ConstantInt *CIFalse =
mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
if (!CITrue || !CIFalse) return false;
ProbTrue = CITrue->getValue().getZExtValue();
ProbFalse = CIFalse->getValue().getZExtValue();
Expand Down
175 changes: 128 additions & 47 deletions llvm/lib/Transforms/Utils/ValueMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,

// Global values do not need to be seeded into the VM if they
// are using the identity mapping.
if (isa<GlobalValue>(V) || isa<MDString>(V))
if (isa<GlobalValue>(V))
return VM[V] = const_cast<Value*>(V);

if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
Expand All @@ -56,57 +56,24 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,

return VM[V] = const_cast<Value*>(V);
}


if (const MDNode *MD = dyn_cast<MDNode>(V)) {
if (const auto *MDV = dyn_cast<MetadataAsValue>(V)) {
const Metadata *MD = MDV->getMetadata();
// If this is a module-level metadata and we know that nothing at the module
// level is changing, then use an identity mapping.
if (!MD->isFunctionLocal() && (Flags & RF_NoModuleLevelChanges))
return VM[V] = const_cast<Value*>(V);

// Create a dummy node in case we have a metadata cycle.
MDNode *Dummy = MDNode::getTemporary(V->getContext(), None);
VM[V] = Dummy;

// Check all operands to see if any need to be remapped.
for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i) {
Value *OP = MD->getOperand(i);
if (!OP) continue;
Value *Mapped_OP = MapValue(OP, VM, Flags, TypeMapper, Materializer);
// Use identity map if Mapped_Op is null and we can ignore missing
// entries.
if (Mapped_OP == OP ||
(Mapped_OP == nullptr && (Flags & RF_IgnoreMissingEntries)))
continue;

// Ok, at least one operand needs remapping.
SmallVector<Value*, 4> Elts;
Elts.reserve(MD->getNumOperands());
for (i = 0; i != e; ++i) {
Value *Op = MD->getOperand(i);
if (!Op)
Elts.push_back(nullptr);
else {
Value *Mapped_Op = MapValue(Op, VM, Flags, TypeMapper, Materializer);
// Use identity map if Mapped_Op is null and we can ignore missing
// entries.
if (Mapped_Op == nullptr && (Flags & RF_IgnoreMissingEntries))
Mapped_Op = Op;
Elts.push_back(Mapped_Op);
}
}
MDNode *NewMD = MDNode::get(V->getContext(), Elts);
Dummy->replaceAllUsesWith(NewMD);
VM[V] = NewMD;
MDNode::deleteTemporary(Dummy);
return NewMD;
}
if (!isa<LocalAsMetadata>(MD) && (Flags & RF_NoModuleLevelChanges))
return VM[V] = const_cast<Value *>(V);

VM[V] = const_cast<Value*>(V);
MDNode::deleteTemporary(Dummy);
auto *MappedMD = MapValue(MD, VM, Flags, TypeMapper, Materializer);
if (MD == MappedMD || (!MappedMD && (Flags & RF_IgnoreMissingEntries)))
return VM[V] = const_cast<Value *>(V);

// No operands needed remapping. Use an identity mapping.
return const_cast<Value*>(V);
// FIXME: This assert crashes during bootstrap, but I think it should be
// correct. For now, just match behaviour from before the metadata/value
// split.
//
// assert(MappedMD && "Referenced metadata value not in value map");
return VM[V] = MetadataAsValue::get(V->getContext(), MappedMD);
}

// Okay, this either must be a constant (which may or may not be mappable) or
Expand Down Expand Up @@ -177,6 +144,120 @@ Value *llvm::MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags,
return VM[V] = ConstantPointerNull::get(cast<PointerType>(NewTy));
}

static Metadata *map(ValueToValueMapTy &VM, const Metadata *Key,
Metadata *Val) {
VM.MD()[Key].reset(Val);
return Val;
}

static Metadata *mapToSelf(ValueToValueMapTy &VM, const Metadata *MD) {
return map(VM, MD, const_cast<Metadata *>(MD));
}

static Metadata *MapValueImpl(const Metadata *MD, ValueToValueMapTy &VM,
RemapFlags Flags,
ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
// If the value already exists in the map, use it.
if (Metadata *NewMD = VM.MD().lookup(MD).get())
return NewMD;

if (isa<MDString>(MD))
return mapToSelf(VM, MD);

if (isa<ConstantAsMetadata>(MD))
if ((Flags & RF_NoModuleLevelChanges))
return mapToSelf(VM, MD);

if (const auto *VMD = dyn_cast<ValueAsMetadata>(MD)) {
Value *MappedV =
MapValue(VMD->getValue(), VM, Flags, TypeMapper, Materializer);
if (VMD->getValue() == MappedV ||
(!MappedV && (Flags & RF_IgnoreMissingEntries)))
return mapToSelf(VM, MD);

// FIXME: This assert crashes during bootstrap, but I think it should be
// correct. For now, just match behaviour from before the metadata/value
// split.
//
// assert(MappedV && "Referenced metadata not in value map!");
if (MappedV)
return map(VM, MD, ValueAsMetadata::get(MappedV));
return nullptr;
}

const MDNode *Node = cast<MDNode>(MD);
assert(Node->isResolved() && "Unexpected unresolved node");

auto getMappedOp = [&](Metadata *Op) -> Metadata *{
if (!Op)
return nullptr;
if (Metadata *MappedOp =
MapValueImpl(Op, VM, Flags, TypeMapper, Materializer))
return MappedOp;
// Use identity map if MappedOp is null and we can ignore missing entries.
if (Flags & RF_IgnoreMissingEntries)
return Op;

// FIXME: This assert crashes during bootstrap, but I think it should be
// correct. For now, just match behaviour from before the metadata/value
// split.
//
// llvm_unreachable("Referenced metadata not in value map!");
return nullptr;
};

// If this is a module-level metadata and we know that nothing at the
// module level is changing, then use an identity mapping.
if (Flags & RF_NoModuleLevelChanges)
return mapToSelf(VM, MD);

// Create a dummy node in case we have a metadata cycle.
MDNodeFwdDecl *Dummy = MDNode::getTemporary(Node->getContext(), None);
map(VM, Node, Dummy);

// Check all operands to see if any need to be remapped.
for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I) {
Metadata *Op = Node->getOperand(I);
Metadata *MappedOp = getMappedOp(Op);
if (Op == MappedOp)
continue;

// Ok, at least one operand needs remapping.
SmallVector<Metadata *, 4> Elts;
Elts.reserve(Node->getNumOperands());
for (I = 0; I != E; ++I)
Elts.push_back(getMappedOp(Node->getOperand(I)));

MDNode *NewMD = MDNode::get(Node->getContext(), Elts);
Dummy->replaceAllUsesWith(NewMD);
MDNode::deleteTemporary(Dummy);
return map(VM, Node, NewMD);
}

// No operands needed remapping. Use an identity mapping.
mapToSelf(VM, MD);
MDNode::deleteTemporary(Dummy);
return const_cast<Metadata *>(MD);
}

Metadata *llvm::MapValue(const Metadata *MD, ValueToValueMapTy &VM,
RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
Metadata *NewMD = MapValueImpl(MD, VM, Flags, TypeMapper, Materializer);
if (NewMD && NewMD != MD)
if (auto *G = dyn_cast<GenericMDNode>(NewMD))
G->resolveCycles();
return NewMD;
}

MDNode *llvm::MapValue(const MDNode *MD, ValueToValueMapTy &VM,
RemapFlags Flags, ValueMapTypeRemapper *TypeMapper,
ValueMaterializer *Materializer) {
return cast<MDNode>(MapValue(static_cast<const Metadata *>(MD), VM, Flags,
TypeMapper, Materializer));
}

/// RemapInstruction - Convert the instruction operands from referencing the
/// current values into those specified by VMap.
///
Expand Down
Loading