2017-12-01 07:30:03 +01:00
|
|
|
#pragma once
|
|
|
|
#include "c-opast.hh"
|
2017-12-02 12:18:31 +01:00
|
|
|
#include "c-opcomp.hh"
|
2017-12-01 07:30:03 +01:00
|
|
|
|
|
|
|
#include <ebcl/Algorithms.hh>
|
|
|
|
|
|
|
|
struct T_SyncCurves;
|
|
|
|
|
|
|
|
namespace opopt {
|
|
|
|
|
2017-12-02 10:07:14 +01:00
|
|
|
// Persistent data for the various stages of the optimizer.
|
|
|
|
struct T_OptData
|
|
|
|
{
|
2017-12-02 12:18:31 +01:00
|
|
|
// Logger
|
|
|
|
F_OPLogger logger{ []( auto , auto ) {} };
|
|
|
|
|
2017-12-02 10:07:14 +01:00
|
|
|
// List of errors generated by the optimizer
|
|
|
|
T_Array< ebcl::T_SRDError > errors;
|
|
|
|
|
|
|
|
// If the size of the ouput is fixed, this field contains it as a
|
|
|
|
// <width,height> pair.
|
|
|
|
T_Optional< std::pair< uint32_t , uint32_t > > fixedSize;
|
|
|
|
|
|
|
|
// The curves that will be bound to the inputs.
|
2017-12-02 12:18:31 +01:00
|
|
|
T_SyncCurves const* curves{ nullptr };
|
2017-12-02 10:07:14 +01:00
|
|
|
|
|
|
|
// A visitor to be used for the tree
|
|
|
|
ebcl::T_Visitor< opast::A_Node > visitor{ opast::ASTVisitorBrowser };
|
2017-12-02 10:40:47 +01:00
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2017-12-02 10:40:47 +01:00
|
|
|
// Table of input declarations; used to fold constant inputs.
|
|
|
|
struct T_InputDecl {
|
|
|
|
ebcl::T_SRDLocation location;
|
|
|
|
float value;
|
|
|
|
};
|
|
|
|
T_Optional< T_KeyValueTable< T_String , T_Array< T_InputDecl > > > inputDecls;
|
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
void findInputDecls( T_OpsParserOutput& program ) noexcept;
|
|
|
|
|
2017-12-02 10:40:47 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
// Data for instruction numbering
|
|
|
|
struct T_InstrPos {
|
|
|
|
uint32_t index;
|
|
|
|
opast::A_InstructionNode* node;
|
|
|
|
bool lastOfSequence;
|
|
|
|
uint32_t funcIndex;
|
|
|
|
};
|
|
|
|
T_HashIndex instrIndex;
|
|
|
|
T_Array< T_InstrPos > instructions;
|
|
|
|
|
|
|
|
void numberInstructions( T_OpsParserOutput& program ) noexcept;
|
|
|
|
uint32_t indexOf( opast::A_InstructionNode const& instr ) noexcept;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Basic block of consecutive instructions
|
|
|
|
struct T_BasicBlock
|
|
|
|
{
|
|
|
|
uint32_t first;
|
|
|
|
uint32_t count;
|
|
|
|
|
|
|
|
explicit T_BasicBlock( uint32_t first ) noexcept
|
|
|
|
: first( first ) , count( 1 )
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2017-12-10 11:12:45 +01:00
|
|
|
// Control flow graph edges and nodes
|
|
|
|
struct T_CtrlFlowEdge
|
|
|
|
{
|
|
|
|
enum E_Type {
|
2017-12-10 11:57:07 +01:00
|
|
|
FLOW , CALL , RET , BYPASS
|
2017-12-10 11:12:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t target;
|
|
|
|
E_Type type;
|
|
|
|
|
|
|
|
T_CtrlFlowEdge(
|
|
|
|
const uint32_t target ,
|
|
|
|
const E_Type type = FLOW ) noexcept
|
|
|
|
: target{ target } , type{ type }
|
|
|
|
{}
|
|
|
|
};
|
2017-12-03 18:35:56 +01:00
|
|
|
struct T_CtrlFlowNode
|
|
|
|
{
|
|
|
|
T_Optional< T_BasicBlock > instructions;
|
2017-12-10 11:12:45 +01:00
|
|
|
T_AutoArray< T_CtrlFlowEdge , 16 > inbound;
|
|
|
|
T_AutoArray< T_CtrlFlowEdge , 16 > outbound;
|
2017-12-03 18:35:56 +01:00
|
|
|
};
|
|
|
|
using P_CtrlFlowNode = T_OwnPtr< T_CtrlFlowNode >;
|
|
|
|
|
2017-12-10 10:14:35 +01:00
|
|
|
// Data structure for call sites
|
|
|
|
struct T_CallSite {
|
|
|
|
T_String name;
|
|
|
|
uint32_t callBlock;
|
|
|
|
uint32_t retBlock;
|
|
|
|
};
|
|
|
|
|
2017-12-03 18:35:56 +01:00
|
|
|
// Special nodes in the graph
|
|
|
|
static constexpr uint32_t CFG_ENTER = 0;
|
|
|
|
static constexpr uint32_t CFG_MAINLOOP = 1;
|
|
|
|
static constexpr uint32_t CFG_END = 2;
|
|
|
|
|
|
|
|
// Control flow graph
|
|
|
|
T_Array< P_CtrlFlowNode > ctrlFlowGraph;
|
2017-12-10 10:14:35 +01:00
|
|
|
T_Array< T_CallSite > callSites;
|
2017-12-03 18:35:56 +01:00
|
|
|
T_KeyValueTable< T_String , T_BasicBlock > cfgFunctions;
|
2017-12-03 23:15:44 +01:00
|
|
|
|
|
|
|
// Build the control flow graph. Instruction numbering must be
|
|
|
|
// up-to-date before calling this.
|
2017-12-03 18:35:56 +01:00
|
|
|
void buildControlFlowGraph(
|
|
|
|
T_OpsParserOutput& program ) noexcept;
|
|
|
|
|
2017-12-03 23:15:44 +01:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Type of variables used for use/define chains
|
|
|
|
enum class E_UDVarType {
|
2017-12-10 15:25:51 +01:00
|
|
|
GLOBAL , LOCAL , ARGUMENT
|
2017-12-03 23:15:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Variable identifier for the U/D chains
|
|
|
|
struct T_VarId
|
|
|
|
{
|
|
|
|
E_UDVarType type; // Var type
|
|
|
|
T_String name; // Var name
|
|
|
|
T_String owner; // Function name (empty for globals)
|
|
|
|
|
|
|
|
explicit T_VarId( T_String const& name ) noexcept
|
|
|
|
: type{ E_UDVarType::GLOBAL } , name{ name } ,
|
|
|
|
owner{ }
|
|
|
|
{ }
|
|
|
|
|
|
|
|
T_VarId( T_String const& name ,
|
|
|
|
T_String const& owner ,
|
|
|
|
const bool isArgument ) noexcept
|
|
|
|
: type{ isArgument ? E_UDVarType::ARGUMENT : E_UDVarType::LOCAL } ,
|
|
|
|
name{ name } , owner{ owner }
|
|
|
|
{ assert( owner ); }
|
|
|
|
|
|
|
|
bool operator ==( T_VarId const& other ) const noexcept
|
|
|
|
{
|
|
|
|
return type == other.type && name == other.name
|
|
|
|
&& owner == other.owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator !=( T_VarId const& other ) const noexcept
|
|
|
|
{
|
|
|
|
return type != other.type || name != other.name
|
|
|
|
|| owner != other.owner;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use/define chain data
|
|
|
|
struct T_VarUDRecord
|
|
|
|
{
|
|
|
|
uint32_t node; // Instruction that uses or sets it
|
|
|
|
uint32_t fnIndex; // Function in which the use/define is located
|
2017-12-10 15:25:51 +01:00
|
|
|
ebcl::T_Set< uint32_t > refs{ // Corresponding uses/defines
|
|
|
|
ebcl::UseTag< ebcl::ArrayBacked< 16 > >( ) };
|
2017-12-03 23:15:44 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Use/define chains for a variable
|
|
|
|
struct T_VarUseDefine
|
|
|
|
{
|
|
|
|
T_VarId var;
|
|
|
|
T_AutoArray< T_VarUDRecord , 16 > uses;
|
|
|
|
T_AutoArray< T_VarUDRecord , 4> defines;
|
|
|
|
|
|
|
|
explicit T_VarUseDefine( T_VarId const& id )
|
|
|
|
: var{ id }
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
// Use/define chains
|
|
|
|
T_ObjectTable< T_VarId , T_VarUseDefine > varUDChains{
|
|
|
|
[]( auto const& var ) -> T_VarId {
|
|
|
|
return var.var;
|
|
|
|
} };
|
|
|
|
|
|
|
|
// Build the use/define chains. The control flow graph must be up to
|
|
|
|
// date before calling this.
|
|
|
|
void buildUseDefineChains(
|
|
|
|
T_OpsParserOutput& program ) noexcept;
|
2017-12-02 10:07:14 +01:00
|
|
|
};
|
|
|
|
|
2017-12-03 23:15:44 +01:00
|
|
|
uint32_t ComputeHash( T_OptData::T_VarId const& id ) noexcept;
|
2017-12-10 11:12:45 +01:00
|
|
|
M_LSHIFT_OP( T_StringBuilder , T_OptData::T_CtrlFlowEdge const& ) noexcept;
|
2017-12-03 23:15:44 +01:00
|
|
|
|
2017-12-02 10:07:14 +01:00
|
|
|
|
2017-12-03 14:59:39 +01:00
|
|
|
/*= INDIVIDUAL OPTIMISATIONS =================================================*/
|
|
|
|
// All functions below return true if transformations were made, false if not.
|
|
|
|
|
|
|
|
// Attempts to fold constant expressions into single constants.
|
|
|
|
//
|
|
|
|
bool FoldConstants(
|
|
|
|
T_OpsParserOutput& program ,
|
|
|
|
T_OptData& optData ) noexcept;
|
|
|
|
|
|
|
|
// Attempts to propagate values from variables that contain constants to the
|
|
|
|
// locations at which they are used.
|
2017-12-01 07:30:03 +01:00
|
|
|
//
|
2017-12-03 14:59:39 +01:00
|
|
|
bool PropagateConstants(
|
|
|
|
T_OpsParserOutput& program ,
|
2017-12-02 10:07:14 +01:00
|
|
|
T_OptData& optData ) noexcept;
|
2017-12-01 07:30:03 +01:00
|
|
|
|
|
|
|
// Attempt to remove blocks of code that will not be executed because of
|
2017-12-03 14:59:39 +01:00
|
|
|
// constant conditions.
|
2017-12-01 07:30:03 +01:00
|
|
|
//
|
2017-12-03 14:59:39 +01:00
|
|
|
bool RemoveDeadCode(
|
|
|
|
T_OpsParserOutput& program ,
|
2017-12-02 10:07:14 +01:00
|
|
|
T_OptData& optData ) noexcept;
|
2017-12-01 07:30:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace opopt
|