Give MDNode printing has access to the current Module in more

cases. This will be needed when function-local metadata can
appear in places that aren't intrinsic function arguments.

llvm-svn: 108971
This commit is contained in:
Dan Gohman 2010-07-20 23:55:01 +00:00
parent b3706e6dd2
commit a2489d1bb6
1 changed files with 62 additions and 39 deletions

View File

@ -581,8 +581,12 @@ static SlotTracker *createSlotTracker(const Value *V) {
if (const Function *Func = dyn_cast<Function>(V)) if (const Function *Func = dyn_cast<Function>(V))
return new SlotTracker(Func); return new SlotTracker(Func);
if (isa<MDNode>(V)) if (const MDNode *MD = dyn_cast<MDNode>(V)) {
if (!MD->isFunctionLocal())
return new SlotTracker(MD->getFunction());
return new SlotTracker((Function *)0); return new SlotTracker((Function *)0);
}
return 0; return 0;
} }
@ -778,15 +782,14 @@ void SlotTracker::CreateMetadataSlot(const MDNode *N) {
// Don't insert if N is a function-local metadata, these are always printed // Don't insert if N is a function-local metadata, these are always printed
// inline. // inline.
if (N->isFunctionLocal()) if (!N->isFunctionLocal()) {
return; mdn_iterator I = mdnMap.find(N);
if (I != mdnMap.end())
return;
mdn_iterator I = mdnMap.find(N); unsigned DestSlot = mdnNext++;
if (I != mdnMap.end()) mdnMap[N] = DestSlot;
return; }
unsigned DestSlot = mdnNext++;
mdnMap[N] = DestSlot;
// Recursively add any MDNodes referenced by operands. // Recursively add any MDNodes referenced by operands.
for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
@ -800,7 +803,8 @@ void SlotTracker::CreateMetadataSlot(const MDNode *N) {
static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
TypePrinting *TypePrinter, TypePrinting *TypePrinter,
SlotTracker *Machine); SlotTracker *Machine,
const Module *Context);
@ -856,7 +860,8 @@ static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
TypePrinting &TypePrinter, TypePrinting &TypePrinter,
SlotTracker *Machine) { SlotTracker *Machine,
const Module *Context) {
if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
if (CI->getType()->isIntegerTy(1)) { if (CI->getType()->isIntegerTy(1)) {
Out << (CI->getZExtValue() ? "true" : "false"); Out << (CI->getZExtValue() ? "true" : "false");
@ -972,9 +977,11 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) { if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
Out << "blockaddress("; Out << "blockaddress(";
WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine); WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
Context);
Out << ", "; Out << ", ";
WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine); WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
Context);
Out << ")"; Out << ")";
return; return;
} }
@ -994,12 +1001,14 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
TypePrinter.print(ETy, Out); TypePrinter.print(ETy, Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CA->getOperand(0), WriteAsOperandInternal(Out, CA->getOperand(0),
&TypePrinter, Machine); &TypePrinter, Machine,
Context);
for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
Out << ", "; Out << ", ";
TypePrinter.print(ETy, Out); TypePrinter.print(ETy, Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine); WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
Context);
} }
} }
Out << ']'; Out << ']';
@ -1017,14 +1026,16 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
TypePrinter.print(CS->getOperand(0)->getType(), Out); TypePrinter.print(CS->getOperand(0)->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine); WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
Context);
for (unsigned i = 1; i < N; i++) { for (unsigned i = 1; i < N; i++) {
Out << ", "; Out << ", ";
TypePrinter.print(CS->getOperand(i)->getType(), Out); TypePrinter.print(CS->getOperand(i)->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine); WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
Context);
} }
Out << ' '; Out << ' ';
} }
@ -1039,7 +1050,8 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
Out << "{ "; Out << "{ ";
TypePrinter.print(CU->getOperand(0)->getType(), Out); TypePrinter.print(CU->getOperand(0)->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CU->getOperand(0), &TypePrinter, Machine); WriteAsOperandInternal(Out, CU->getOperand(0), &TypePrinter, Machine,
Context);
Out << " }"; Out << " }";
return; return;
} }
@ -1051,12 +1063,14 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
Out << '<'; Out << '<';
TypePrinter.print(ETy, Out); TypePrinter.print(ETy, Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine); WriteAsOperandInternal(Out, CP->getOperand(0), &TypePrinter, Machine,
Context);
for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) { for (unsigned i = 1, e = CP->getNumOperands(); i != e; ++i) {
Out << ", "; Out << ", ";
TypePrinter.print(ETy, Out); TypePrinter.print(ETy, Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine); WriteAsOperandInternal(Out, CP->getOperand(i), &TypePrinter, Machine,
Context);
} }
Out << '>'; Out << '>';
return; return;
@ -1087,7 +1101,7 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
TypePrinter.print((*OI)->getType(), Out); TypePrinter.print((*OI)->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine); WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
if (OI+1 != CE->op_end()) if (OI+1 != CE->op_end())
Out << ", "; Out << ", ";
} }
@ -1112,7 +1126,8 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
TypePrinting *TypePrinter, TypePrinting *TypePrinter,
SlotTracker *Machine) { SlotTracker *Machine,
const Module *Context) {
Out << "!{"; Out << "!{";
for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
const Value *V = Node->getOperand(mi); const Value *V = Node->getOperand(mi);
@ -1122,7 +1137,7 @@ static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
TypePrinter->print(V->getType(), Out); TypePrinter->print(V->getType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, Node->getOperand(mi), WriteAsOperandInternal(Out, Node->getOperand(mi),
TypePrinter, Machine); TypePrinter, Machine, Context);
} }
if (mi + 1 != me) if (mi + 1 != me)
Out << ", "; Out << ", ";
@ -1138,7 +1153,8 @@ static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
/// ///
static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
TypePrinting *TypePrinter, TypePrinting *TypePrinter,
SlotTracker *Machine) { SlotTracker *Machine,
const Module *Context) {
if (V->hasName()) { if (V->hasName()) {
PrintLLVMName(Out, V); PrintLLVMName(Out, V);
return; return;
@ -1147,7 +1163,7 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
const Constant *CV = dyn_cast<Constant>(V); const Constant *CV = dyn_cast<Constant>(V);
if (CV && !isa<GlobalValue>(CV)) { if (CV && !isa<GlobalValue>(CV)) {
assert(TypePrinter && "Constants require TypePrinting!"); assert(TypePrinter && "Constants require TypePrinting!");
WriteConstantInternal(Out, CV, *TypePrinter, Machine); WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
return; return;
} }
@ -1168,12 +1184,16 @@ static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
if (const MDNode *N = dyn_cast<MDNode>(V)) { if (const MDNode *N = dyn_cast<MDNode>(V)) {
if (N->isFunctionLocal()) { if (N->isFunctionLocal()) {
// Print metadata inline, not via slot reference number. // Print metadata inline, not via slot reference number.
WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine); WriteMDNodeBodyInternal(Out, N, TypePrinter, Machine, Context);
return; return;
} }
if (!Machine) if (!Machine) {
Machine = createSlotTracker(V); if (N->isFunctionLocal())
Machine = new SlotTracker(N->getFunction());
else
Machine = new SlotTracker(Context);
}
Out << '!' << Machine->getMetadataSlot(N); Out << '!' << Machine->getMetadataSlot(N);
return; return;
} }
@ -1227,8 +1247,9 @@ void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
// Fast path: Don't construct and populate a TypePrinting object if we // Fast path: Don't construct and populate a TypePrinting object if we
// won't be needing any types printed. // won't be needing any types printed.
if (!PrintType && if (!PrintType &&
(!isa<Constant>(V) || V->hasName() || isa<GlobalValue>(V))) { ((!isa<Constant>(V) && !isa<MDNode>(V)) ||
WriteAsOperandInternal(Out, V, 0, 0); V->hasName() || isa<GlobalValue>(V))) {
WriteAsOperandInternal(Out, V, 0, 0, Context);
return; return;
} }
@ -1242,7 +1263,7 @@ void llvm::WriteAsOperand(raw_ostream &Out, const Value *V,
Out << ' '; Out << ' ';
} }
WriteAsOperandInternal(Out, V, &TypePrinter, 0); WriteAsOperandInternal(Out, V, &TypePrinter, 0, Context);
} }
namespace { namespace {
@ -1297,7 +1318,7 @@ void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
TypePrinter.print(Operand->getType(), Out); TypePrinter.print(Operand->getType(), Out);
Out << ' '; Out << ' ';
} }
WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine); WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
} }
void AssemblyWriter::writeParamOperand(const Value *Operand, void AssemblyWriter::writeParamOperand(const Value *Operand,
@ -1314,7 +1335,7 @@ void AssemblyWriter::writeParamOperand(const Value *Operand,
Out << ' ' << Attribute::getAsString(Attrs); Out << ' ' << Attribute::getAsString(Attrs);
Out << ' '; Out << ' ';
// Print the operand // Print the operand
WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine); WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
} }
void AssemblyWriter::printModule(const Module *M) { void AssemblyWriter::printModule(const Module *M) {
@ -1451,7 +1472,7 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
if (GV->isMaterializable()) if (GV->isMaterializable())
Out << "; Materializable\n"; Out << "; Materializable\n";
WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine); WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
Out << " = "; Out << " = ";
if (!GV->hasInitializer() && GV->hasExternalLinkage()) if (!GV->hasInitializer() && GV->hasExternalLinkage())
@ -1510,7 +1531,7 @@ void AssemblyWriter::printAlias(const GlobalAlias *GA) {
TypePrinter.print(F->getFunctionType(), Out); TypePrinter.print(F->getFunctionType(), Out);
Out << "* "; Out << "* ";
WriteAsOperandInternal(Out, F, &TypePrinter, &Machine); WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
} else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) { } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Aliasee)) {
TypePrinter.print(GA->getType(), Out); TypePrinter.print(GA->getType(), Out);
Out << ' '; Out << ' ';
@ -1593,7 +1614,7 @@ void AssemblyWriter::printFunction(const Function *F) {
Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' '; Out << Attribute::getAsString(Attrs.getRetAttributes()) << ' ';
TypePrinter.print(F->getReturnType(), Out); TypePrinter.print(F->getReturnType(), Out);
Out << ' '; Out << ' ';
WriteAsOperandInternal(Out, F, &TypePrinter, &Machine); WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
Out << '('; Out << '(';
Machine.incorporateFunction(F); Machine.incorporateFunction(F);
@ -2029,7 +2050,9 @@ void AssemblyWriter::printInstruction(const Instruction &I) {
} else { } else {
Out << ", !<unknown kind #" << Kind << ">"; Out << ", !<unknown kind #" << Kind << ">";
} }
Out << " !" << Machine.getMetadataSlot(InstMD[i].second); Out << ' ';
WriteAsOperandInternal(Out, InstMD[i].second, &TypePrinter, &Machine,
TheModule);
} }
} }
printInfoComment(I); printInfoComment(I);
@ -2077,7 +2100,7 @@ void AssemblyWriter::writeAllMDNodes() {
} }
void AssemblyWriter::printMDNodeBody(const MDNode *Node) { void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine); WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
WriteMDNodeComment(Node, Out); WriteMDNodeComment(Node, Out);
Out << "\n"; Out << "\n";
} }
@ -2138,7 +2161,7 @@ void Value::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW) const {
TypePrinting TypePrinter; TypePrinting TypePrinter;
TypePrinter.print(C->getType(), OS); TypePrinter.print(C->getType(), OS);
OS << ' '; OS << ' ';
WriteConstantInternal(OS, C, TypePrinter, 0); WriteConstantInternal(OS, C, TypePrinter, 0, 0);
} else if (isa<InlineAsm>(this) || isa<MDString>(this) || } else if (isa<InlineAsm>(this) || isa<MDString>(this) ||
isa<Argument>(this)) { isa<Argument>(this)) {
WriteAsOperand(OS, this, true, 0); WriteAsOperand(OS, this, true, 0);