// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details1#pragma once23#include "Luau/CodeGenCommon.h"45#include <bitset>6#include <queue>7#include <utility>8#include <vector>910#include <stdint.h>1112namespace Luau13{14namespace CodeGen15{1617struct IrBlock;18struct IrFunction;1920void updateUseCounts(IrFunction& function);2122void updateLastUseLocations(IrFunction& function, const std::vector<uint32_t>& sortedBlocks);2324uint32_t getNextInstUse(IrFunction& function, uint32_t targetInstIdx, uint32_t startInstIdx);2526// Returns how many values are coming into the block (live in) and how many are coming out of the block (live out)27std::pair<uint32_t, uint32_t> getLiveInOutValueCount(IrFunction& function, IrBlock& start, bool visitChain);28uint32_t getLiveInValueCount(IrFunction& function, IrBlock& block);29uint32_t getLiveOutValueCount(IrFunction& function, IrBlock& block);3031struct RegisterSet32{33std::bitset<256> regs;3435// If variadic sequence is active, we track register from which it starts36bool varargSeq = false;37uint8_t varargStart = 0;38};3940void requireVariadicSequence(RegisterSet& sourceRs, const RegisterSet& defRs, uint8_t varargStart);4142struct BlockOrdering43{44uint32_t depth = 0;4546uint32_t preOrder = ~0u;47uint32_t postOrder = ~0u;4849bool visited = false;50};5152struct CfgInfo53{54std::vector<uint32_t> predecessors;55std::vector<uint32_t> predecessorsOffsets;5657std::vector<uint32_t> successors;58std::vector<uint32_t> successorsOffsets;5960// Immediate dominators (unique parent in the dominator tree)61std::vector<uint32_t> idoms;6263// Children in the dominator tree64std::vector<uint32_t> domChildren;65std::vector<uint32_t> domChildrenOffsets;6667std::vector<BlockOrdering> domOrdering;6869// VM registers that are live when the block is entered70// Additionally, an active variadic sequence can exist at the entry of the block71std::vector<RegisterSet> in;7273// VM registers that are defined inside the block74// It can also contain a variadic sequence definition if that hasn't been consumed inside the block75// Note that this means that checking 'def' set might not be enough to say that register has not been written to76std::vector<RegisterSet> def;7778// VM registers that are coming out from the block79// These might be registers that are defined inside the block or have been defined at the entry of the block80// Additionally, an active variadic sequence can exist at the exit of the block81std::vector<RegisterSet> out;8283// VM registers captured by nested closures84// This set can never have an active variadic sequence85RegisterSet captured;8687// VM registers defined in any block88// Lowest variadic sequence start is stored to mark variadic writes89RegisterSet written;90};9192// Calculate lists of block predecessors and successors93void computeCfgBlockEdges(IrFunction& function);9495// A quick refresher on dominance and dominator trees:96// * If A is a dominator of B (A dom B), you can never execute B without executing A first97// * A is a strict dominator of B (A sdom B) is similar to previous one but A != B98// * Immediate dominator node N (idom N) is a unique node T so that T sdom N,99// but T does not strictly dominate any other node that dominates N.100// * Dominance frontier is a set of nodes where dominance of a node X ends.101// In practice this is where values established by node X might no longer hold because of join edges from other nodes coming in.102// This is also where PHI instructions in SSA are placed.103void computeCfgImmediateDominators(IrFunction& function);104void computeCfgDominanceTreeChildren(IrFunction& function);105106struct IdfContext107{108struct BlockAndOrdering109{110uint32_t blockIdx;111BlockOrdering ordering;112113bool operator<(const BlockAndOrdering& rhs) const114{115if (ordering.depth != rhs.ordering.depth)116return ordering.depth < rhs.ordering.depth;117118return ordering.preOrder < rhs.ordering.preOrder;119}120};121122// Using priority queue to work on nodes in the order from the bottom of the dominator tree to the top123// If the depth of keys is equal, DFS order is used to provide strong ordering124std::priority_queue<BlockAndOrdering> queue;125std::vector<uint32_t> worklist;126127struct IdfVisitMarks128{129bool seenInQueue = false;130bool seenInWorklist = false;131};132133std::vector<IdfVisitMarks> visits;134135std::vector<uint32_t> idf;136};137138// Compute iterated dominance frontier (IDF or DF+) for a variable, given the set of blocks where that variable is defined139// Providing a set of blocks where the variable is a live-in at the entry helps produce a pruned SSA form (inserted phi nodes will not be dead)140//141// 'Iterated' comes from the definition where we recompute the IDFn+1 = DF(S) while adding IDFn to S until a fixed point is reached142// Iterated dominance frontier has been shown to be equal to the set of nodes where phi instructions have to be inserted143void computeIteratedDominanceFrontierForDefs(144IdfContext& ctx,145const IrFunction& function,146const std::vector<uint32_t>& defBlocks,147const std::vector<uint32_t>& liveInBlocks148);149150// Function used to update all CFG data151void computeCfgInfo(IrFunction& function);152153struct BlockIteratorWrapper154{155const uint32_t* itBegin = nullptr;156const uint32_t* itEnd = nullptr;157158bool empty() const159{160return itBegin == itEnd;161}162163size_t size() const164{165return size_t(itEnd - itBegin);166}167168const uint32_t* begin() const169{170return itBegin;171}172173const uint32_t* end() const174{175return itEnd;176}177178uint32_t operator[](size_t pos) const179{180CODEGEN_ASSERT(pos < size_t(itEnd - itBegin));181return itBegin[pos];182}183};184185BlockIteratorWrapper predecessors(const CfgInfo& cfg, uint32_t blockIdx);186BlockIteratorWrapper successors(const CfgInfo& cfg, uint32_t blockIdx);187BlockIteratorWrapper domChildren(const CfgInfo& cfg, uint32_t blockIdx);188189} // namespace CodeGen190} // namespace Luau191192193