Skip to content

Commit b18ed55

Browse files
committed
Move isSymbolUsedInExpression to MCExpr, use for recursion detection and add MCTargetExpr specific subexpr considerations for isSymbolUsedInExpression
1 parent f99b859 commit b18ed55

File tree

6 files changed

+50
-88
lines changed

6 files changed

+50
-88
lines changed

llvm/include/llvm/MC/MCExpr.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ class MCExpr {
8686
bool InParens = false) const;
8787
void dump() const;
8888

89+
/// Returns whether the given symbol is used anywhere in the expression or
90+
/// subexpressions.
91+
bool isSymbolUsedInExpression(const MCSymbol *Sym) const;
92+
8993
/// @}
9094
/// \name Expression Evaluation
9195
/// @{
@@ -663,6 +667,9 @@ class MCTargetExpr : public MCExpr {
663667
const MCFixup *Fixup) const = 0;
664668
// allow Target Expressions to be checked for equality
665669
virtual bool isEqualTo(const MCExpr *x) const { return false; }
670+
virtual bool isSymbolUsedInExpression(const MCSymbol *Sym) const {
671+
return false;
672+
}
666673
// This should be set when assigned expressions are not valid ".set"
667674
// expressions, e.g. registers, and must be inlined.
668675
virtual bool inlineAssignedExpr() const { return false; }

llvm/lib/MC/MCExpr.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,35 @@ LLVM_DUMP_METHOD void MCExpr::dump() const {
177177
}
178178
#endif
179179

180+
bool MCExpr::isSymbolUsedInExpression(const MCSymbol *Sym) const {
181+
switch (getKind()) {
182+
case MCExpr::Binary: {
183+
const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(this);
184+
return BE->getLHS()->isSymbolUsedInExpression(Sym) ||
185+
BE->getRHS()->isSymbolUsedInExpression(Sym);
186+
}
187+
case MCExpr::Target: {
188+
const MCTargetExpr *TE = static_cast<const MCTargetExpr *>(this);
189+
return TE->isSymbolUsedInExpression(Sym);
190+
}
191+
case MCExpr::Constant:
192+
return false;
193+
case MCExpr::SymbolRef: {
194+
const MCSymbol &S = static_cast<const MCSymbolRefExpr *>(this)->getSymbol();
195+
if (S.isVariable() && !S.isWeakExternal())
196+
return S.getVariableValue()->isSymbolUsedInExpression(Sym);
197+
return &S == Sym;
198+
}
199+
case MCExpr::Unary: {
200+
const MCExpr *SubExpr =
201+
static_cast<const MCUnaryExpr *>(this)->getSubExpr();
202+
return SubExpr->isSymbolUsedInExpression(Sym);
203+
}
204+
}
205+
206+
llvm_unreachable("Unknown expr kind!");
207+
}
208+
180209
/* *** */
181210

182211
const MCBinaryExpr *MCBinaryExpr::create(Opcode Opc, const MCExpr *LHS,

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6394,33 +6394,6 @@ bool HLASMAsmParser::parseStatement(ParseStatementInfo &Info,
63946394
namespace llvm {
63956395
namespace MCParserUtils {
63966396

6397-
/// Returns whether the given symbol is used anywhere in the given expression,
6398-
/// or subexpressions.
6399-
static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) {
6400-
switch (Value->getKind()) {
6401-
case MCExpr::Binary: {
6402-
const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value);
6403-
return isSymbolUsedInExpression(Sym, BE->getLHS()) ||
6404-
isSymbolUsedInExpression(Sym, BE->getRHS());
6405-
}
6406-
case MCExpr::Target:
6407-
case MCExpr::Constant:
6408-
return false;
6409-
case MCExpr::SymbolRef: {
6410-
const MCSymbol &S =
6411-
static_cast<const MCSymbolRefExpr *>(Value)->getSymbol();
6412-
if (S.isVariable() && !S.isWeakExternal())
6413-
return isSymbolUsedInExpression(Sym, S.getVariableValue());
6414-
return &S == Sym;
6415-
}
6416-
case MCExpr::Unary:
6417-
return isSymbolUsedInExpression(
6418-
Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr());
6419-
}
6420-
6421-
llvm_unreachable("Unknown expr kind!");
6422-
}
6423-
64246397
bool parseAssignmentExpression(StringRef Name, bool allow_redef,
64256398
MCAsmParser &Parser, MCSymbol *&Sym,
64266399
const MCExpr *&Value) {
@@ -6445,7 +6418,7 @@ bool parseAssignmentExpression(StringRef Name, bool allow_redef,
64456418
//
64466419
// FIXME: Diagnostics. Note the location of the definition as a label.
64476420
// FIXME: Diagnose assignment to protected identifier (e.g., register name).
6448-
if (isSymbolUsedInExpression(Sym, Value))
6421+
if (Value->isSymbolUsedInExpression(Sym))
64496422
return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'");
64506423
else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() &&
64516424
!Sym->isVariable())

llvm/lib/Target/AMDGPU/AMDGPUMCResourceInfo.cpp

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -91,58 +91,6 @@ MCSymbol *MCResourceInfo::getMaxSGPRSymbol(MCContext &OutContext) {
9191
return OutContext.getOrCreateSymbol("amdgpu.max_num_sgpr");
9292
}
9393

94-
// The expression should have no recursion in it. Test a (sub-)expression to see
95-
// if it needs to be further visited, or if a recursion has been found. Returns
96-
// true if Sym is found within Expr (i.e., has a recurrance of Sym found), false
97-
// otherwise.
98-
static bool findSymbolInExpr(MCSymbol *Sym, const MCExpr *Expr,
99-
SmallPtrSetImpl<const MCExpr *> &Visited) {
100-
101-
if (Expr->getKind() == MCExpr::ExprKind::SymbolRef) {
102-
const MCSymbolRefExpr *SymRefExpr = cast<MCSymbolRefExpr>(Expr);
103-
const MCSymbol &SymRef = SymRefExpr->getSymbol();
104-
if (Sym == &SymRef)
105-
return true;
106-
}
107-
108-
if (!Visited.insert(Expr).second)
109-
return false;
110-
111-
switch (Expr->getKind()) {
112-
default:
113-
return false;
114-
case MCExpr::ExprKind::SymbolRef: {
115-
const MCSymbolRefExpr *SymRefExpr = cast<MCSymbolRefExpr>(Expr);
116-
const MCSymbol &SymRef = SymRefExpr->getSymbol();
117-
if (SymRef.isVariable()) {
118-
return findSymbolInExpr(Sym, SymRef.getVariableValue(/*isUsed=*/false),
119-
Visited);
120-
}
121-
return false;
122-
}
123-
case MCExpr::ExprKind::Binary: {
124-
const MCBinaryExpr *BExpr = cast<MCBinaryExpr>(Expr);
125-
if (findSymbolInExpr(Sym, BExpr->getLHS(), Visited) ||
126-
findSymbolInExpr(Sym, BExpr->getRHS(), Visited)) {
127-
return true;
128-
}
129-
return false;
130-
}
131-
case MCExpr::ExprKind::Unary: {
132-
const MCUnaryExpr *UExpr = cast<MCUnaryExpr>(Expr);
133-
return findSymbolInExpr(Sym, UExpr->getSubExpr(), Visited);
134-
}
135-
case MCExpr::ExprKind::Target: {
136-
const AMDGPUMCExpr *AGVK = cast<AMDGPUMCExpr>(Expr);
137-
for (const MCExpr *E : AGVK->getArgs()) {
138-
if (findSymbolInExpr(Sym, E, Visited))
139-
return true;
140-
}
141-
return false;
142-
}
143-
}
144-
}
145-
14694
void MCResourceInfo::assignResourceInfoExpr(
14795
int64_t LocalValue, ResourceInfoKind RIK, AMDGPUMCExpr::VariantKind Kind,
14896
const MachineFunction &MF, const SmallVectorImpl<const Function *> &Callees,
@@ -160,12 +108,10 @@ void MCResourceInfo::assignResourceInfoExpr(
160108
if (!Seen.insert(Callee).second)
161109
continue;
162110

163-
SmallPtrSet<const MCExpr *, 8> Visited;
164111
MCSymbol *CalleeValSym = getSymbol(Callee->getName(), RIK, OutContext);
165-
166112
if (!CalleeValSym->isVariable() ||
167-
!findSymbolInExpr(
168-
Sym, CalleeValSym->getVariableValue(/*IsUsed=*/false), Visited)) {
113+
!CalleeValSym->getVariableValue(/*isUsed=*/false)
114+
->isSymbolUsedInExpression(Sym)) {
169115
ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext));
170116
}
171117
}
@@ -223,14 +169,12 @@ void MCResourceInfo::gatherResourceInfo(
223169
if (!Seen.insert(Callee).second)
224170
continue;
225171
if (!Callee->isDeclaration()) {
226-
SmallPtrSet<const MCExpr *, 8> Visited;
227172
MCSymbol *CalleeValSym =
228173
getSymbol(Callee->getName(), RIK_PrivateSegSize, OutContext);
229174

230175
if (!CalleeValSym->isVariable() ||
231-
!findSymbolInExpr(Sym,
232-
CalleeValSym->getVariableValue(/*IsUsed=*/false),
233-
Visited)) {
176+
!CalleeValSym->getVariableValue(/*isUsed=*/false)
177+
->isSymbolUsedInExpression(Sym)) {
234178
ArgExprs.push_back(MCSymbolRefExpr::create(CalleeValSym, OutContext));
235179
}
236180
}

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCExpr.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,14 @@ const AMDGPUMCExpr *AMDGPUMCExpr::createOccupancy(unsigned InitOcc,
306306
Ctx);
307307
}
308308

309+
bool AMDGPUMCExpr::isSymbolUsedInExpression(const MCSymbol *Sym) const {
310+
for (const MCExpr *E : getArgs()) {
311+
if (E->isSymbolUsedInExpression(Sym))
312+
return true;
313+
}
314+
return false;
315+
}
316+
309317
static KnownBits fromOptionalToKnownBits(std::optional<bool> CompareResult) {
310318
static constexpr unsigned BitWidth = 64;
311319
const APInt True(BitWidth, 1);

llvm/lib/Target/AMDGPU/MCTargetDesc/AMDGPUMCExpr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class AMDGPUMCExpr : public MCTargetExpr {
9797
void printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const override;
9898
bool evaluateAsRelocatableImpl(MCValue &Res, const MCAssembler *Asm,
9999
const MCFixup *Fixup) const override;
100+
bool isSymbolUsedInExpression(const MCSymbol *Sym) const override;
100101
void visitUsedExpr(MCStreamer &Streamer) const override;
101102
MCFragment *findAssociatedFragment() const override;
102103
void fixELFSymbolsInTLSFixups(MCAssembler &) const override{};

0 commit comments

Comments
 (0)