[llvm-commits] [llvm] r85678 - in /llvm/trunk: docs/LangRef.html include/llvm/Constants.h lib/AsmParser/LLParser.cpp lib/Bitcode/Reader/BitcodeReader.cpp lib/Bitcode/Writer/BitcodeWriter.cpp lib/Bitcode/Writer/ValueEnumerator.cpp lib/VMCore/AsmWriter.cpp lib/VMCore/BasicBlock.cpp lib/VMCore/Constants.cpp
Chris Lattner
sabre at nondot.org
Sat Oct 31 13:08:38 PDT 2009
Author: lattner
Date: Sat Oct 31 15:08:37 2009
New Revision: 85678
URL: http://llvm.org/viewvc/llvm-project?rev=85678&view=rev
Log:
Make blockaddress(@func, null) be valid, and make 'deleting a basic
block with a blockaddress still referring to it' replace the invalid
blockaddress with a new blockaddress(@func, null) instead of a
inttoptr(1).
This changes the bitcode encoding format, and still needs codegen
support (this should produce a non-zero value, referring to the entry
block of the function would also be quite reasonable).
Modified:
llvm/trunk/docs/LangRef.html
llvm/trunk/include/llvm/Constants.h
llvm/trunk/lib/AsmParser/LLParser.cpp
llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
llvm/trunk/lib/VMCore/AsmWriter.cpp
llvm/trunk/lib/VMCore/BasicBlock.cpp
llvm/trunk/lib/VMCore/Constants.cpp
Modified: llvm/trunk/docs/LangRef.html
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/docs/LangRef.html?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/docs/LangRef.html (original)
+++ llvm/trunk/docs/LangRef.html Sat Oct 31 15:08:37 2009
@@ -2176,11 +2176,13 @@
Blocks</a></div>
<div class="doc_text">
-<p><b><tt>blockaddress(@function, %block)</tt></b></p>
+<p><b><tt>blockaddress(@function, %block)</tt></b><br>
+ <b><tt>blockaddress(@function, null)</tt></b></p>
<p>The '<tt>blockaddress</tt>' constant computes the address of the specified
basic block in the specified function, and always has an i8* type. Taking
- the address of the entry block is illegal.</p>
+ the address of the entry block is illegal. The BasicBlock operand may also
+ be null.</p>
<p>This value only has defined behavior when used as an operand to the
'<a href="#i_indirectbr"><tt>indirectbr</tt></a>' instruction or for comparisons
Modified: llvm/trunk/include/llvm/Constants.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Constants.h?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Constants.h (original)
+++ llvm/trunk/include/llvm/Constants.h Sat Oct 31 15:08:37 2009
@@ -567,6 +567,10 @@
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
Function *getFunction() const { return (Function*)Op<0>().get(); }
+
+ /// getBasicBlock - This returns the block associated with this BlockAddress.
+ /// Note that this can return null if the block this originally referred to
+ /// was deleted.
BasicBlock *getBasicBlock() const { return (BasicBlock*)Op<1>().get(); }
/// isNullValue - Return true if this is the value that would be returned by
Modified: llvm/trunk/lib/AsmParser/LLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLParser.cpp?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLParser.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLParser.cpp Sat Oct 31 15:08:37 2009
@@ -123,20 +123,25 @@
// Loop over all the references, resolving them.
for (unsigned i = 0, e = Refs.size(); i != e; ++i) {
BasicBlock *Res;
- if (PFS) {
+ if (Refs[i].first.Kind == ValID::t_Null)
+ Res = 0;
+ else if (PFS) {
if (Refs[i].first.Kind == ValID::t_LocalName)
Res = PFS->GetBB(Refs[i].first.StrVal, Refs[i].first.Loc);
- else
+ else {
+ assert(Refs[i].first.Kind == ValID::t_LocalID);
Res = PFS->GetBB(Refs[i].first.UIntVal, Refs[i].first.Loc);
+ }
} else if (Refs[i].first.Kind == ValID::t_LocalID) {
return Error(Refs[i].first.Loc,
"cannot take address of numeric label after it the function is defined");
} else {
+ assert(Refs[i].first.Kind == ValID::t_LocalName);
Res = dyn_cast_or_null<BasicBlock>(
TheFn->getValueSymbolTable().lookup(Refs[i].first.StrVal));
}
- if (Res == 0)
+ if (Res == 0 && Refs[i].first.Kind != ValID::t_Null)
return Error(Refs[i].first.Loc,
"referenced value is not a basic block");
@@ -2055,10 +2060,11 @@
ParseValID(Label) ||
ParseToken(lltok::rparen, "expected ')' in block address expression"))
return true;
-
+
if (Fn.Kind != ValID::t_GlobalID && Fn.Kind != ValID::t_GlobalName)
return Error(Fn.Loc, "expected function name in blockaddress");
- if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName)
+ if (Label.Kind != ValID::t_LocalID && Label.Kind != ValID::t_LocalName &&
+ Label.Kind != ValID::t_Null)
return Error(Label.Loc, "expected basic block name in blockaddress");
// Make a global variable as a placeholder for this reference.
Modified: llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/trunk/lib/Bitcode/Reader/BitcodeReader.cpp Sat Oct 31 15:08:37 2009
@@ -2274,11 +2274,12 @@
std::vector<BlockAddrRefTy> &RefList = BAFRI->second;
for (unsigned i = 0, e = RefList.size(); i != e; ++i) {
unsigned BlockIdx = RefList[i].first;
- if (BlockIdx >= FunctionBBs.size())
+ if (BlockIdx > FunctionBBs.size())
return Error("Invalid blockaddress block #");
GlobalVariable *FwdRef = RefList[i].second;
- FwdRef->replaceAllUsesWith(BlockAddress::get(F, FunctionBBs[BlockIdx]));
+ BasicBlock *BB = BlockIdx == 0 ? 0 : FunctionBBs[BlockIdx-1];
+ FwdRef->replaceAllUsesWith(BlockAddress::get(F, BB));
FwdRef->eraseFromParent();
}
Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Sat Oct 31 15:08:37 2009
@@ -854,7 +854,8 @@
break;
}
} else if (const BlockAddress *BA = dyn_cast<BlockAddress>(C)) {
- assert(BA->getFunction() == BA->getBasicBlock()->getParent() &&
+ assert((!BA->getBasicBlock() ||
+ BA->getFunction() == BA->getBasicBlock()->getParent()) &&
"Malformed blockaddress");
Code = bitc::CST_CODE_BLOCKADDRESS;
Record.push_back(VE.getTypeID(BA->getFunction()->getType()));
Modified: llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/ValueEnumerator.cpp Sat Oct 31 15:08:37 2009
@@ -265,6 +265,8 @@
// Do not enumerate the initializers for an array of simple characters.
// The initializers just polute the value table, and we emit the strings
// specially.
+ } else if (isa<BlockAddress>(C)) {
+ // Don't enumerate function or block.
} else if (C->getNumOperands()) {
// If a constant has operands, enumerate them. This makes sure that if a
// constant has uses (for example an array of const ints), that they are
@@ -276,8 +278,7 @@
// graph that don't go through a global variable.
for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
I != E; ++I)
- if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
- EnumerateValue(*I);
+ EnumerateValue(*I);
// Finally, add the value. Doing this could make the ValueID reference be
// dangling, don't reuse it.
@@ -417,9 +418,10 @@
/// specified basic block. This is relatively expensive information, so it
/// should only be used by rare constructs such as address-of-label.
unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
+ if (BB == 0) return 0;
unsigned &Idx = GlobalBasicBlockIDs[BB];
if (Idx != 0)
- return Idx-1;
+ return Idx;
IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
return getGlobalBasicBlockID(BB);
Modified: llvm/trunk/lib/VMCore/AsmWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/AsmWriter.cpp?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/AsmWriter.cpp (original)
+++ llvm/trunk/lib/VMCore/AsmWriter.cpp Sat Oct 31 15:08:37 2009
@@ -1065,7 +1065,10 @@
Out << "blockaddress(";
WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine);
Out << ", ";
- WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine);
+ if (BA->getBasicBlock())
+ WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine);
+ else
+ Out << "null";
Out << ")";
return;
}
Modified: llvm/trunk/lib/VMCore/BasicBlock.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/BasicBlock.cpp?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/BasicBlock.cpp (original)
+++ llvm/trunk/lib/VMCore/BasicBlock.cpp Sat Oct 31 15:08:37 2009
@@ -63,15 +63,13 @@
// hanging off the block, or an undefined use of the block (source code
// expecting the address of a label to keep the block alive even though there
// is no indirect branch). Handle these cases by zapping the BlockAddress
- // nodes. There are no other possible uses at this point.
+ // nodes, replacing them with BlockAddress(F, NULL). There are no other
+ // possible uses at this point.
if (hasAddressTaken()) {
assert(!use_empty() && "There should be at least one blockaddress!");
- Constant *Replacement =
- ConstantInt::get(llvm::Type::getInt32Ty(getContext()), 1);
while (!use_empty()) {
BlockAddress *BA = cast<BlockAddress>(use_back());
- BA->replaceAllUsesWith(ConstantExpr::getIntToPtr(Replacement,
- BA->getType()));
+ BA->replaceAllUsesWith(BlockAddress::get(BA->getFunction(), 0));
BA->destroyConstant();
}
}
Modified: llvm/trunk/lib/VMCore/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Constants.cpp?rev=85678&r1=85677&r2=85678&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Constants.cpp (original)
+++ llvm/trunk/lib/VMCore/Constants.cpp Sat Oct 31 15:08:37 2009
@@ -1045,7 +1045,7 @@
&Op<0>(), 2) {
Op<0>() = F;
Op<1>() = BB;
- BB->AdjustBlockAddressRefCount(1);
+ if (BB) BB->AdjustBlockAddressRefCount(1);
}
@@ -1054,7 +1054,8 @@
void BlockAddress::destroyConstant() {
getFunction()->getType()->getContext().pImpl
->BlockAddresses.erase(std::make_pair(getFunction(), getBasicBlock()));
- getBasicBlock()->AdjustBlockAddressRefCount(-1);
+ if (BasicBlock *BB = getBasicBlock())
+ BB->AdjustBlockAddressRefCount(-1);
destroyConstantImpl();
}
More information about the llvm-commits
mailing list