2023-01-13 20:36:28 +00:00
|
|
|
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Luau/Bytecode.h"
|
|
|
|
#include "Luau/Common.h"
|
2023-02-03 12:34:12 +00:00
|
|
|
#include "Luau/IrData.h"
|
2023-01-13 20:36:28 +00:00
|
|
|
|
|
|
|
namespace Luau
|
|
|
|
{
|
|
|
|
namespace CodeGen
|
|
|
|
{
|
|
|
|
|
2023-02-17 14:53:37 +00:00
|
|
|
struct IrBuilder;
|
|
|
|
|
2023-01-13 20:36:28 +00:00
|
|
|
inline bool isJumpD(LuauOpcode op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case LOP_JUMP:
|
|
|
|
case LOP_JUMPIF:
|
|
|
|
case LOP_JUMPIFNOT:
|
|
|
|
case LOP_JUMPIFEQ:
|
|
|
|
case LOP_JUMPIFLE:
|
|
|
|
case LOP_JUMPIFLT:
|
|
|
|
case LOP_JUMPIFNOTEQ:
|
|
|
|
case LOP_JUMPIFNOTLE:
|
|
|
|
case LOP_JUMPIFNOTLT:
|
|
|
|
case LOP_FORNPREP:
|
|
|
|
case LOP_FORNLOOP:
|
|
|
|
case LOP_FORGPREP:
|
|
|
|
case LOP_FORGLOOP:
|
|
|
|
case LOP_FORGPREP_INEXT:
|
|
|
|
case LOP_FORGPREP_NEXT:
|
|
|
|
case LOP_JUMPBACK:
|
|
|
|
case LOP_JUMPXEQKNIL:
|
|
|
|
case LOP_JUMPXEQKB:
|
|
|
|
case LOP_JUMPXEQKN:
|
|
|
|
case LOP_JUMPXEQKS:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isSkipC(LuauOpcode op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case LOP_LOADB:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isFastCall(LuauOpcode op)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case LOP_FASTCALL:
|
|
|
|
case LOP_FASTCALL1:
|
|
|
|
case LOP_FASTCALL2:
|
|
|
|
case LOP_FASTCALL2K:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int getJumpTarget(uint32_t insn, uint32_t pc)
|
|
|
|
{
|
|
|
|
LuauOpcode op = LuauOpcode(LUAU_INSN_OP(insn));
|
|
|
|
|
|
|
|
if (isJumpD(op))
|
|
|
|
return int(pc + LUAU_INSN_D(insn) + 1);
|
|
|
|
else if (isFastCall(op))
|
|
|
|
return int(pc + LUAU_INSN_C(insn) + 2);
|
|
|
|
else if (isSkipC(op) && LUAU_INSN_C(insn))
|
|
|
|
return int(pc + LUAU_INSN_C(insn) + 1);
|
|
|
|
else if (op == LOP_JUMPX)
|
|
|
|
return int(pc + LUAU_INSN_E(insn) + 1);
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool isBlockTerminator(IrCmd cmd)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case IrCmd::JUMP:
|
|
|
|
case IrCmd::JUMP_IF_TRUTHY:
|
|
|
|
case IrCmd::JUMP_IF_FALSY:
|
|
|
|
case IrCmd::JUMP_EQ_TAG:
|
2023-02-10 18:50:54 +00:00
|
|
|
case IrCmd::JUMP_EQ_INT:
|
2023-04-21 22:41:03 +01:00
|
|
|
case IrCmd::JUMP_LT_INT:
|
|
|
|
case IrCmd::JUMP_GE_UINT:
|
2023-01-13 20:36:28 +00:00
|
|
|
case IrCmd::JUMP_EQ_POINTER:
|
|
|
|
case IrCmd::JUMP_CMP_NUM:
|
|
|
|
case IrCmd::JUMP_CMP_ANY:
|
2023-03-17 14:59:30 +00:00
|
|
|
case IrCmd::JUMP_SLOT_MATCH:
|
2023-03-31 13:21:14 +01:00
|
|
|
case IrCmd::RETURN:
|
|
|
|
case IrCmd::FORGLOOP:
|
|
|
|
case IrCmd::FORGLOOP_FALLBACK:
|
|
|
|
case IrCmd::FORGPREP_XNEXT_FALLBACK:
|
2023-01-13 20:36:28 +00:00
|
|
|
case IrCmd::FALLBACK_FORGPREP:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool hasResult(IrCmd cmd)
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case IrCmd::LOAD_TAG:
|
|
|
|
case IrCmd::LOAD_POINTER:
|
|
|
|
case IrCmd::LOAD_DOUBLE:
|
|
|
|
case IrCmd::LOAD_INT:
|
|
|
|
case IrCmd::LOAD_TVALUE:
|
|
|
|
case IrCmd::LOAD_NODE_VALUE_TV:
|
|
|
|
case IrCmd::LOAD_ENV:
|
|
|
|
case IrCmd::GET_ARR_ADDR:
|
|
|
|
case IrCmd::GET_SLOT_NODE_ADDR:
|
2023-03-17 14:59:30 +00:00
|
|
|
case IrCmd::GET_HASH_NODE_ADDR:
|
2023-01-13 20:36:28 +00:00
|
|
|
case IrCmd::ADD_INT:
|
|
|
|
case IrCmd::SUB_INT:
|
|
|
|
case IrCmd::ADD_NUM:
|
|
|
|
case IrCmd::SUB_NUM:
|
|
|
|
case IrCmd::MUL_NUM:
|
|
|
|
case IrCmd::DIV_NUM:
|
|
|
|
case IrCmd::MOD_NUM:
|
2023-03-03 13:45:38 +00:00
|
|
|
case IrCmd::MIN_NUM:
|
|
|
|
case IrCmd::MAX_NUM:
|
2023-01-13 20:36:28 +00:00
|
|
|
case IrCmd::UNM_NUM:
|
2023-03-31 13:21:14 +01:00
|
|
|
case IrCmd::FLOOR_NUM:
|
|
|
|
case IrCmd::CEIL_NUM:
|
|
|
|
case IrCmd::ROUND_NUM:
|
|
|
|
case IrCmd::SQRT_NUM:
|
|
|
|
case IrCmd::ABS_NUM:
|
2023-01-13 20:36:28 +00:00
|
|
|
case IrCmd::NOT_ANY:
|
|
|
|
case IrCmd::TABLE_LEN:
|
|
|
|
case IrCmd::NEW_TABLE:
|
|
|
|
case IrCmd::DUP_TABLE:
|
2023-03-17 14:59:30 +00:00
|
|
|
case IrCmd::TRY_NUM_TO_INDEX:
|
|
|
|
case IrCmd::TRY_CALL_FASTGETTM:
|
2023-02-10 18:50:54 +00:00
|
|
|
case IrCmd::INT_TO_NUM:
|
2023-04-21 22:41:03 +01:00
|
|
|
case IrCmd::UINT_TO_NUM:
|
|
|
|
case IrCmd::NUM_TO_INT:
|
|
|
|
case IrCmd::NUM_TO_UINT:
|
2023-02-17 14:53:37 +00:00
|
|
|
case IrCmd::SUBSTITUTE:
|
2023-03-03 13:45:38 +00:00
|
|
|
case IrCmd::INVOKE_FASTCALL:
|
2023-04-21 22:41:03 +01:00
|
|
|
case IrCmd::BITAND_UINT:
|
|
|
|
case IrCmd::BITXOR_UINT:
|
|
|
|
case IrCmd::BITOR_UINT:
|
|
|
|
case IrCmd::BITNOT_UINT:
|
|
|
|
case IrCmd::BITLSHIFT_UINT:
|
|
|
|
case IrCmd::BITRSHIFT_UINT:
|
|
|
|
case IrCmd::BITARSHIFT_UINT:
|
|
|
|
case IrCmd::BITLROTATE_UINT:
|
|
|
|
case IrCmd::BITRROTATE_UINT:
|
|
|
|
case IrCmd::BITCOUNTLZ_UINT:
|
|
|
|
case IrCmd::BITCOUNTRZ_UINT:
|
|
|
|
case IrCmd::INVOKE_LIBM:
|
2023-01-13 20:36:28 +00:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool hasSideEffects(IrCmd cmd)
|
|
|
|
{
|
2023-03-03 13:45:38 +00:00
|
|
|
if (cmd == IrCmd::INVOKE_FASTCALL)
|
|
|
|
return true;
|
|
|
|
|
2023-01-13 20:36:28 +00:00
|
|
|
// Instructions that don't produce a result most likely have other side-effects to make them useful
|
|
|
|
// Right now, a full switch would mirror the 'hasResult' function, so we use this simple condition
|
|
|
|
return !hasResult(cmd);
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:53:37 +00:00
|
|
|
inline bool isPseudo(IrCmd cmd)
|
|
|
|
{
|
|
|
|
// Instructions that are used for internal needs and are not a part of final lowering
|
|
|
|
return cmd == IrCmd::NOP || cmd == IrCmd::SUBSTITUTE;
|
|
|
|
}
|
|
|
|
|
2023-04-07 20:56:27 +01:00
|
|
|
IrValueKind getCmdValueKind(IrCmd cmd);
|
|
|
|
|
2023-02-24 18:24:22 +00:00
|
|
|
bool isGCO(uint8_t tag);
|
|
|
|
|
2023-03-03 13:45:38 +00:00
|
|
|
// Manually add or remove use of an operand
|
|
|
|
void addUse(IrFunction& function, IrOp op);
|
|
|
|
void removeUse(IrFunction& function, IrOp op);
|
|
|
|
|
2023-02-10 18:50:54 +00:00
|
|
|
// Remove a single instruction
|
|
|
|
void kill(IrFunction& function, IrInst& inst);
|
|
|
|
|
|
|
|
// Remove a range of instructions
|
|
|
|
void kill(IrFunction& function, uint32_t start, uint32_t end);
|
|
|
|
|
|
|
|
// Remove a block, including all instructions inside
|
|
|
|
void kill(IrFunction& function, IrBlock& block);
|
|
|
|
|
|
|
|
// Replace a single operand and update use counts (can cause chain removal of dead code)
|
|
|
|
void replace(IrFunction& function, IrOp& original, IrOp replacement);
|
|
|
|
|
|
|
|
// Replace a single instruction
|
|
|
|
// Target instruction index instead of reference is used to handle introduction of a new block terminator
|
2023-02-24 18:24:22 +00:00
|
|
|
void replace(IrFunction& function, IrBlock& block, uint32_t instIdx, IrInst replacement);
|
2023-02-10 18:50:54 +00:00
|
|
|
|
2023-02-17 14:53:37 +00:00
|
|
|
// Replace instruction with a different value (using IrCmd::SUBSTITUTE)
|
2023-04-21 22:41:03 +01:00
|
|
|
void substitute(IrFunction& function, IrInst& inst, IrOp replacement);
|
2023-02-17 14:53:37 +00:00
|
|
|
|
|
|
|
// Replace instruction arguments that point to substitutions with target values
|
|
|
|
void applySubstitutions(IrFunction& function, IrOp& op);
|
|
|
|
void applySubstitutions(IrFunction& function, IrInst& inst);
|
|
|
|
|
2023-02-24 18:24:22 +00:00
|
|
|
// Compare numbers using IR condition value
|
|
|
|
bool compare(double a, double b, IrCondition cond);
|
|
|
|
|
2023-02-17 14:53:37 +00:00
|
|
|
// Perform constant folding on instruction at index
|
|
|
|
// For most instructions, successful folding results in a IrCmd::SUBSTITUTE
|
|
|
|
// But it can also be successful on conditional control-flow, replacing it with an unconditional IrCmd::JUMP
|
2023-02-24 18:24:22 +00:00
|
|
|
void foldConstants(IrBuilder& build, IrFunction& function, IrBlock& block, uint32_t instIdx);
|
2023-02-17 14:53:37 +00:00
|
|
|
|
2023-04-28 12:55:55 +01:00
|
|
|
uint32_t getNativeContextOffset(int bfid);
|
2023-04-21 22:41:03 +01:00
|
|
|
|
2023-01-13 20:36:28 +00:00
|
|
|
} // namespace CodeGen
|
|
|
|
} // namespace Luau
|