2022-10-21 18:33:43 +01:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#include "Luau/Def.h"
|
2023-11-17 18:15:31 +00:00
|
|
|
|
2023-10-20 21:36:26 +01:00
|
|
|
#include "Luau/Common.h"
|
2023-11-10 18:05:48 +00:00
|
|
|
|
2023-11-17 18:15:31 +00:00
|
|
|
#include <algorithm>
|
2022-10-21 18:33:43 +01:00
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
|
2023-10-20 21:36:26 +01:00
|
|
|
bool containsSubscriptedDefinition(DefId def)
|
2022-10-21 18:33:43 +01:00
|
|
|
{
|
2023-10-20 21:36:26 +01:00
|
|
|
if (auto cell = get<Cell>(def))
|
|
|
|
return cell->subscripted;
|
2023-11-10 18:05:48 +00:00
|
|
|
else if (auto phi = get<Phi>(def))
|
2023-11-17 18:15:31 +00:00
|
|
|
return std::any_of(phi->operands.begin(), phi->operands.end(), containsSubscriptedDefinition);
|
|
|
|
else
|
|
|
|
return false;
|
2023-10-20 21:36:26 +01:00
|
|
|
}
|
|
|
|
|
2023-12-08 15:42:54 +00:00
|
|
|
void collectOperands(DefId def, std::vector<DefId>* operands)
|
2023-11-17 18:15:31 +00:00
|
|
|
{
|
2023-12-08 15:42:54 +00:00
|
|
|
LUAU_ASSERT(operands);
|
|
|
|
if (std::find(operands->begin(), operands->end(), def) != operands->end())
|
2023-11-17 18:15:31 +00:00
|
|
|
return;
|
|
|
|
else if (get<Cell>(def))
|
2023-12-08 15:42:54 +00:00
|
|
|
operands->push_back(def);
|
2023-11-17 18:15:31 +00:00
|
|
|
else if (auto phi = get<Phi>(def))
|
|
|
|
{
|
|
|
|
for (const Def* operand : phi->operands)
|
|
|
|
collectOperands(NotNull{operand}, operands);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-08 15:42:54 +00:00
|
|
|
DefId DefArena::freshCell(bool subscripted)
|
|
|
|
{
|
|
|
|
return NotNull{allocator.allocate(Def{Cell{subscripted}})};
|
|
|
|
}
|
|
|
|
|
2023-11-10 18:05:48 +00:00
|
|
|
DefId DefArena::phi(DefId a, DefId b)
|
|
|
|
{
|
2023-11-17 18:15:31 +00:00
|
|
|
return phi({a, b});
|
|
|
|
}
|
|
|
|
|
|
|
|
DefId DefArena::phi(const std::vector<DefId>& defs)
|
|
|
|
{
|
|
|
|
std::vector<DefId> operands;
|
|
|
|
for (DefId operand : defs)
|
2023-12-08 15:42:54 +00:00
|
|
|
collectOperands(operand, &operands);
|
2023-11-17 18:15:31 +00:00
|
|
|
|
|
|
|
// There's no need to allocate a Phi node for a singleton set.
|
|
|
|
if (operands.size() == 1)
|
|
|
|
return operands[0];
|
2023-11-10 18:05:48 +00:00
|
|
|
else
|
2023-11-17 18:15:31 +00:00
|
|
|
return NotNull{allocator.allocate(Def{Phi{std::move(operands)}})};
|
2023-11-10 18:05:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:33:43 +01:00
|
|
|
} // namespace Luau
|