summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorJames Y Knight <jyknight@google.com>2019-03-22 18:27:13 +0000
committerJames Y Knight <jyknight@google.com>2019-03-22 18:27:13 +0000
commitc0e6b8ac3ad442e08d7a16382ff50969ba3f2b2d (patch)
tree51c36938481eb486b2efb0ba04711803b46bbaeb /llvm/lib
parent5e381fb11a01e81a0084338d4a7b35b5180d6f0c (diff)
downloadbcm5719-llvm-c0e6b8ac3ad442e08d7a16382ff50969ba3f2b2d.tar.gz
bcm5719-llvm-c0e6b8ac3ad442e08d7a16382ff50969ba3f2b2d.zip
IR: Support parsing numeric block ids, and emit them in textual output.
Just as as llvm IR supports explicitly specifying numeric value ids for instructions, and emits them by default in textual output, now do the same for blocks. This is a slightly incompatible change in the textual IR format. Previously, llvm would parse numeric labels as string names. E.g. define void @f() { br label %"55" 55: ret void } defined a label *named* "55", even without needing to be quoted, while the reference required quoting. Now, if you intend a block label which looks like a value number to be a name, you must quote it in the definition too (e.g. `"55":`). Previously, llvm would print nameless blocks only as a comment, and would omit it if there was no predecessor. This could cause confusion for readers of the IR, just as unnamed instructions did prior to the addition of "%5 = " syntax, back in 2008 (PR2480). Now, it will always print a label for an unnamed block, with the exception of the entry block. (IMO it may be better to print it for the entry-block as well. However, that requires updating many more tests.) Thus, the following is supported, and is the canonical printing: define i32 @f(i32, i32) { %3 = add i32 %0, %1 br label %4 4: ret i32 %3 } New test cases covering this behavior are added, and other tests updated as required. Differential Revision: https://reviews.llvm.org/D58548 llvm-svn: 356789
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/AsmParser/LLLexer.cpp12
-rw-r--r--llvm/lib/AsmParser/LLParser.cpp33
-rw-r--r--llvm/lib/AsmParser/LLParser.h2
-rw-r--r--llvm/lib/AsmParser/LLToken.h1
-rw-r--r--llvm/lib/IR/AsmWriter.cpp9
5 files changed, 43 insertions, 14 deletions
diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index 9e5bfb1f72b..bc3776da966 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -1048,7 +1048,17 @@ lltok::Kind LLLexer::LexDigitOrNegative() {
for (; isdigit(static_cast<unsigned char>(CurPtr[0])); ++CurPtr)
/*empty*/;
- // Check to see if this really is a label afterall, e.g. "-1:".
+ // Check if this is a fully-numeric label:
+ if (isdigit(TokStart[0]) && CurPtr[0] == ':') {
+ uint64_t Val = atoull(TokStart, CurPtr);
+ ++CurPtr; // Skip the colon.
+ if ((unsigned)Val != Val)
+ Error("invalid value number (too large)!");
+ UIntVal = unsigned(Val);
+ return lltok::LabelID;
+ }
+
+ // Check to see if this really is a string label, e.g. "-1:".
if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
if (const char *End = isLabelTail(CurPtr)) {
StrVal.assign(TokStart, End-1);
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 71315f3c0fb..1ab2702941d 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -2926,13 +2926,27 @@ BasicBlock *LLParser::PerFunctionState::GetBB(unsigned ID, LocTy Loc) {
/// unnamed. If there is an error, this returns null otherwise it returns
/// the block being defined.
BasicBlock *LLParser::PerFunctionState::DefineBB(const std::string &Name,
- LocTy Loc) {
+ int NameID, LocTy Loc) {
BasicBlock *BB;
- if (Name.empty())
+ if (Name.empty()) {
+ if (NameID != -1 && unsigned(NameID) != NumberedVals.size()) {
+ P.Error(Loc, "label expected to be numbered '" +
+ Twine(NumberedVals.size()) + "'");
+ return nullptr;
+ }
BB = GetBB(NumberedVals.size(), Loc);
- else
+ if (!BB) {
+ P.Error(Loc, "unable to create block numbered '" +
+ Twine(NumberedVals.size()) + "'");
+ return nullptr;
+ }
+ } else {
BB = GetBB(Name, Loc);
- if (!BB) return nullptr; // Already diagnosed error.
+ if (!BB) {
+ P.Error(Loc, "unable to create block named '" + Name + "'");
+ return nullptr;
+ }
+ }
// Move the block to the end of the function. Forward ref'd blocks are
// inserted wherever they happen to be referenced.
@@ -5489,20 +5503,23 @@ bool LLParser::ParseFunctionBody(Function &Fn) {
}
/// ParseBasicBlock
-/// ::= LabelStr? Instruction*
+/// ::= (LabelStr|LabelID)? Instruction*
bool LLParser::ParseBasicBlock(PerFunctionState &PFS) {
// If this basic block starts out with a name, remember it.
std::string Name;
+ int NameID = -1;
LocTy NameLoc = Lex.getLoc();
if (Lex.getKind() == lltok::LabelStr) {
Name = Lex.getStrVal();
Lex.Lex();
+ } else if (Lex.getKind() == lltok::LabelID) {
+ NameID = Lex.getUIntVal();
+ Lex.Lex();
}
- BasicBlock *BB = PFS.DefineBB(Name, NameLoc);
+ BasicBlock *BB = PFS.DefineBB(Name, NameID, NameLoc);
if (!BB)
- return Error(NameLoc,
- "unable to create block named '" + Name + "'");
+ return true;
std::string NameStr;
diff --git a/llvm/lib/AsmParser/LLParser.h b/llvm/lib/AsmParser/LLParser.h
index 95aea0c775a..452492b862a 100644
--- a/llvm/lib/AsmParser/LLParser.h
+++ b/llvm/lib/AsmParser/LLParser.h
@@ -445,7 +445,7 @@ namespace llvm {
/// DefineBB - Define the specified basic block, which is either named or
/// unnamed. If there is an error, this returns null otherwise it returns
/// the block being defined.
- BasicBlock *DefineBB(const std::string &Name, LocTy Loc);
+ BasicBlock *DefineBB(const std::string &Name, int NameID, LocTy Loc);
bool resolveForwardRefBlockAddresses();
};
diff --git a/llvm/lib/AsmParser/LLToken.h b/llvm/lib/AsmParser/LLToken.h
index 50bdf05092a..80a1eb99e35 100644
--- a/llvm/lib/AsmParser/LLToken.h
+++ b/llvm/lib/AsmParser/LLToken.h
@@ -422,6 +422,7 @@ enum Kind {
kw_varFlags,
// Unsigned Valued tokens (UIntVal).
+ LabelID, // 42:
GlobalID, // @42
LocalVarID, // %42
AttrGrpID, // #42
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 07dea403d56..ad6c2c8e883 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -3482,23 +3482,24 @@ void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
/// printBasicBlock - This member is called for each basic block in a method.
void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
+ bool IsEntryBlock = BB == &BB->getParent()->getEntryBlock();
if (BB->hasName()) { // Print out the label if it exists...
Out << "\n";
PrintLLVMName(Out, BB->getName(), LabelPrefix);
Out << ':';
- } else if (!BB->use_empty()) { // Don't print block # of no uses...
- Out << "\n; <label>:";
+ } else if (!IsEntryBlock) {
+ Out << "\n";
int Slot = Machine.getLocalSlot(BB);
if (Slot != -1)
Out << Slot << ":";
else
- Out << "<badref>";
+ Out << "<badref>:";
}
if (!BB->getParent()) {
Out.PadToColumn(50);
Out << "; Error: Block without parent!";
- } else if (BB != &BB->getParent()->getEntryBlock()) { // Not the entry block?
+ } else if (!IsEntryBlock) {
// Output predecessors for the block.
Out.PadToColumn(50);
Out << ";";
OpenPOWER on IntegriCloud