Skip to content

Commit 080286d

Browse files
authored
[IR] BlockAddress doesn't use BasicBlock (#200772)
BlockAddress is the only non-terminator user of a BasicBlock, and it occurs very rarely. To speed up predecessor iteration, change BlockAddress to no longer use its BasicBlock. This should also make uselistorder_bb obsolete.
1 parent 1433381 commit 080286d

23 files changed

Lines changed: 24 additions & 202 deletions

llvm/docs/LangRef.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4325,16 +4325,11 @@ Use-list directives may appear at function scope or global scope. They are not
43254325
instructions, and have no effect on the semantics of the IR. When they're at
43264326
function scope, they must appear after the terminator of the final basic block.
43274327

4328-
If basic blocks have their address taken via ``blockaddress()`` expressions,
4329-
``uselistorder_bb`` can be used to reorder their use-lists from outside their
4330-
function's scope.
4331-
43324328
:Syntax:
43334329

43344330
::
43354331

43364332
uselistorder <ty> <value>, { <order-indexes> }
4337-
uselistorder_bb @function, %block { <order-indexes> }
43384333

43394334
:Examples:
43404335

@@ -4355,7 +4350,6 @@ function's scope.
43554350
uselistorder ptr @global, { 1, 2, 0 }
43564351
uselistorder i32 7, { 1, 0 }
43574352
uselistorder i32 (i32) @bar, { 1, 0 }
4358-
uselistorder_bb @foo, %bb, { 5, 1, 3, 2, 0, 4 }
43594353

43604354
.. _source_filename:
43614355

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,6 @@ namespace llvm {
706706

707707
// Use-list order directives.
708708
bool parseUseListOrder(PerFunctionState *PFS = nullptr);
709-
bool parseUseListOrderBB();
710709
bool parseUseListOrderIndexes(SmallVectorImpl<unsigned> &Indexes);
711710
bool sortUseListOrder(Value *V, ArrayRef<unsigned> Indexes, SMLoc Loc);
712711
};

llvm/include/llvm/AsmParser/LLToken.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ enum Kind {
388388

389389
// Use-list order directives.
390390
kw_uselistorder,
391-
kw_uselistorder_bb,
392391

393392
// Summary index keywords
394393
kw_path,

llvm/include/llvm/IR/CFG.h

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,25 @@ class PredIterator {
5151
using Self = PredIterator<Ptr, USE_iterator>;
5252
USE_iterator It;
5353

54-
inline void advancePastNonTerminators() {
55-
// Loop to ignore non-terminator uses (for example BlockAddresses).
56-
while (!It.atEnd()) {
57-
if (auto *Inst = dyn_cast<Instruction>(*It)) {
58-
assert(Inst->isTerminator() && "BasicBlock used in non-terminator");
59-
break;
60-
}
61-
62-
++It;
63-
}
64-
}
65-
6654
public:
6755
PredIterator() = default;
68-
explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {
69-
advancePastNonTerminators();
70-
}
56+
explicit inline PredIterator(Ptr *bb) : It(bb->user_begin()) {}
7157
inline PredIterator(Ptr *bb, bool) : It(bb->user_end()) {}
7258

7359
inline bool operator==(const Self& x) const { return It == x.It; }
7460
inline bool operator!=(const Self& x) const { return !operator==(x); }
7561

7662
inline reference operator*() const {
7763
assert(!It.atEnd() && "pred_iterator out of range!");
78-
return cast<Instruction>(*It)->getParent();
64+
auto *I = cast<Instruction>(*It);
65+
assert(I->isTerminator() && "BasicBlock used in non-terminator");
66+
return I->getParent();
7967
}
8068
inline pointer *operator->() const { return &operator*(); }
8169

8270
inline Self& operator++() { // Preincrement
8371
assert(!It.atEnd() && "pred_iterator out of range!");
84-
++It; advancePastNonTerminators();
72+
++It;
8573
return *this;
8674
}
8775

llvm/include/llvm/IR/Constants.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,9 @@ class ConstantTargetNone final : public ConstantData {
10821082
class BlockAddress final : public Constant {
10831083
friend class Constant;
10841084

1085-
constexpr static IntrusiveOperandsAllocMarker AllocMarker{1};
1085+
constexpr static IntrusiveOperandsAllocMarker AllocMarker{0};
1086+
1087+
BasicBlock *Block;
10861088

10871089
BlockAddress(Type *Ty, BasicBlock *BB);
10881090

@@ -1114,7 +1116,7 @@ class BlockAddress final : public Constant {
11141116
/// Transparently provide more efficient getOperand methods.
11151117
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
11161118

1117-
BasicBlock *getBasicBlock() const { return cast<BasicBlock>(Op<0>().get()); }
1119+
BasicBlock *getBasicBlock() const { return Block; }
11181120
Function *getFunction() const { return getBasicBlock()->getParent(); }
11191121

11201122
/// Methods for support type inquiry through isa, cast, and dyn_cast:
@@ -1125,7 +1127,7 @@ class BlockAddress final : public Constant {
11251127

11261128
template <>
11271129
struct OperandTraits<BlockAddress>
1128-
: public FixedNumOperandTraits<BlockAddress, 1> {};
1130+
: public FixedNumOperandTraits<BlockAddress, 0> {};
11291131

11301132
DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BlockAddress, Value)
11311133

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,6 @@ lltok::Kind LLLexer::LexIdentifier() {
801801

802802
// Use-list order directives.
803803
KEYWORD(uselistorder);
804-
KEYWORD(uselistorder_bb);
805804

806805
KEYWORD(personality);
807806
KEYWORD(cleanup);

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,6 @@ bool LLParser::parseTopLevelEntities() {
634634
if (parseUseListOrder())
635635
return true;
636636
break;
637-
case lltok::kw_uselistorder_bb:
638-
if (parseUseListOrderBB())
639-
return true;
640-
break;
641637
}
642638
}
643639
}
@@ -9472,53 +9468,6 @@ bool LLParser::parseUseListOrder(PerFunctionState *PFS) {
94729468
return sortUseListOrder(V, Indexes, Loc);
94739469
}
94749470

9475-
/// parseUseListOrderBB
9476-
/// ::= 'uselistorder_bb' @foo ',' %bar ',' UseListOrderIndexes
9477-
bool LLParser::parseUseListOrderBB() {
9478-
assert(Lex.getKind() == lltok::kw_uselistorder_bb);
9479-
SMLoc Loc = Lex.getLoc();
9480-
Lex.Lex();
9481-
9482-
ValID Fn, Label;
9483-
SmallVector<unsigned, 16> Indexes;
9484-
if (parseValID(Fn, /*PFS=*/nullptr) ||
9485-
parseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
9486-
parseValID(Label, /*PFS=*/nullptr) ||
9487-
parseToken(lltok::comma, "expected comma in uselistorder_bb directive") ||
9488-
parseUseListOrderIndexes(Indexes))
9489-
return true;
9490-
9491-
// Check the function.
9492-
GlobalValue *GV;
9493-
if (Fn.Kind == ValID::t_GlobalName)
9494-
GV = M->getNamedValue(Fn.StrVal);
9495-
else if (Fn.Kind == ValID::t_GlobalID)
9496-
GV = NumberedVals.get(Fn.UIntVal);
9497-
else
9498-
return error(Fn.Loc, "expected function name in uselistorder_bb");
9499-
if (!GV)
9500-
return error(Fn.Loc,
9501-
"invalid function forward reference in uselistorder_bb");
9502-
auto *F = dyn_cast<Function>(GV);
9503-
if (!F)
9504-
return error(Fn.Loc, "expected function name in uselistorder_bb");
9505-
if (F->isDeclaration())
9506-
return error(Fn.Loc, "invalid declaration in uselistorder_bb");
9507-
9508-
// Check the basic block.
9509-
if (Label.Kind == ValID::t_LocalID)
9510-
return error(Label.Loc, "invalid numeric label in uselistorder_bb");
9511-
if (Label.Kind != ValID::t_LocalName)
9512-
return error(Label.Loc, "expected basic block name in uselistorder_bb");
9513-
Value *V = F->getValueSymbolTable()->lookup(Label.StrVal);
9514-
if (!V)
9515-
return error(Label.Loc, "invalid basic block in uselistorder_bb");
9516-
if (!isa<BasicBlock>(V))
9517-
return error(Label.Loc, "expected basic block in uselistorder_bb");
9518-
9519-
return sortUseListOrder(V, Indexes, Loc);
9520-
}
9521-
95229471
/// ModuleEntry
95239472
/// ::= 'module' ':' '(' 'path' ':' STRINGCONSTANT ',' 'hash' ':' Hash ')'
95249473
/// Hash ::= '(' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ',' UInt32 ')'

llvm/lib/CodeGen/IndirectBrExpandPass.cpp

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,11 @@ bool runImpl(Function &F, const TargetLowering *TLI, DomTreeUpdater *DTU) {
135135
if (!IndirectBrSuccs.count(&BB))
136136
continue;
137137

138-
auto IsBlockAddressUse = [&](const Use &U) {
139-
return isa<BlockAddress>(U.getUser());
140-
};
141-
auto BlockAddressUseIt = llvm::find_if(BB.uses(), IsBlockAddressUse);
142-
if (BlockAddressUseIt == BB.use_end())
143-
continue;
144-
145-
assert(std::none_of(std::next(BlockAddressUseIt), BB.use_end(),
146-
IsBlockAddressUse) &&
147-
"There should only ever be a single blockaddress use because it is "
148-
"a constant and should be uniqued.");
149-
150-
auto *BA = cast<BlockAddress>(BlockAddressUseIt->getUser());
138+
auto *BA = BlockAddress::lookup(&BB);
151139

152140
// Skip if the constant was formed but ended up not being used (due to DCE
153141
// or whatever).
154-
if (!BA->isConstantUsed())
142+
if (!BA || !BA->isConstantUsed())
155143
continue;
156144

157145
// Compute the index we want to use for this basic block. We can't use zero

llvm/lib/IR/AsmWriter.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5038,20 +5038,11 @@ void AssemblyWriter::writeAllAttributeGroups() {
50385038

50395039
void AssemblyWriter::printUseListOrder(const Value *V,
50405040
ArrayRef<unsigned> Shuffle) {
5041-
bool IsInFunction = Machine.getFunction();
5042-
if (IsInFunction)
5041+
if (Machine.getFunction())
50435042
Out << " ";
50445043

5045-
Out << "uselistorder";
5046-
if (const BasicBlock *BB = IsInFunction ? nullptr : dyn_cast<BasicBlock>(V)) {
5047-
Out << "_bb ";
5048-
writeOperand(BB->getParent(), false);
5049-
Out << ", ";
5050-
writeOperand(BB, false);
5051-
} else {
5052-
Out << " ";
5053-
writeOperand(V, true);
5054-
}
5044+
Out << "uselistorder ";
5045+
writeOperand(V, true);
50555046

50565047
assert(Shuffle.size() >= 2 && "Shuffle too small");
50575048
Out << ", { " << llvm::interleaved(Shuffle) << " }\n";

llvm/lib/IR/BasicBlock.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ BasicBlock::~BasicBlock() {
175175
// is no indirect branch). Handle these cases by zapping the BlockAddress
176176
// nodes. There are no other possible uses at this point.
177177
if (hasAddressTaken()) {
178-
assert(!use_empty() && "There should be at least one blockaddress!");
179-
BlockAddress *BA = cast<BlockAddress>(user_back());
178+
BlockAddress *BA = BlockAddress::lookup(this);
180179

181180
Constant *Replacement = ConstantInt::get(Type::getInt32Ty(getContext()), 1);
182181
BA->replaceAllUsesWith(

0 commit comments

Comments
 (0)