Files
mercury/compiler/basic_block.m
Zoltan Somogyi 9c58f97e3b Add a new optimization, --use-local-vars, to the LLDS backend.
Estimated hours taken: 20
Branches: main

Add a new optimization, --use-local-vars, to the LLDS backend. This
optimization is intended to replace references to fake registers and stack
slots with references to temporary variables in C code, since accessing these
should be cheaper.

With this optimization and one for delaying construction unifications,
the eager code generator should generate code at least good as that produced by
the old value numbering pass. This should make it possible to get rid of value
numbering, which is much harder to maintain.

compiler/use_local_vars.m:
	New module containing the optimization.

compiler/notes/compiler_design.html:
	Mention the new module.

compiler/exprn_aux.m:
	Add new utility predicates for use by use_local_vars.

	If --debug-opt is specified, do not dump instruction sequences to
	standard output. Instead, put them in separate files, where they can be
	compared more easily.

compiler/options.m:
	Add the --use-local-vars option to control whether the use_local_vars
	pass gets run.

compiler/llds.m:
	Add liveness information to the c_code and pragma_foreign_code LLDS
	instructions, in order to allow use_local_vars to work in the presence
	of automatically-generated C code (e.g. by debugging).

compiler/livemap.m:
	Use the new liveness information to generate useful livemap information
	even in the presence of automatically generated C code.

compiler/code_gen.m:
compiler/code_info.m:
compiler/dupelim.m:
compiler/frameopt.m:
compiler/llds_common.m:
compiler/llds_out.m:
compiler/middle_rec.m:
compiler/opt_debug.m:
compiler/opt_util.m:
compiler/pragma_c_gen.m:
compiler/trace.m:
compiler/vn_block.m:
compiler/vn_cost.m:
compiler/vn_filter.m:
compiler/vn_verify.m:
	Provide and/or ignore this additional liveness information.

compiler/wrap_block.m:
	The post_value_number pass wraps LLDS instruction sequences
	using temporaries in a block instruction which actually declares
	those temporaries. It used to be used only by value numbering;
	it is now also used by use_local_vars. It has therefore been renamed
	and put in its own file.

compiler/optimize.m:
	Invoke use_local_vars if required, and call wrap_blocks instead of
	post_value_number.

compiler/value_number.m:
	Since the value numbering pass still cannot handle automatically
	generated C code, check for it explicitly now that livemap carries out
	only a weaker check.

compiler/basic_block.m:
	Add a module qualification.

library/set.m:
library/set_bbbtree.m:
library/set_ordlist.m:
library/set_unordlist.m:
	Add a new predicate, union_list, to each implementation of sets,
	for use by some of the new code above.

tests/general/array_test.m:
	Print out the result of each operation as soon as it is done, so that
	if you get a seg fault, you know which operations have completed and
	which haven't.
2001-04-24 03:59:13 +00:00

155 lines
4.8 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1997-2001 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
%
% Main author: zs.
%
% This module defines a representation for basic blocks, sequences of
% instructions with one entry and one exit, and provides predicates
% that convert a list of instructions into a list of basic blocks
% and vice versa.
%-----------------------------------------------------------------------------%
:- module basic_block.
:- interface.
:- import_module llds.
:- import_module list, map, std_util, counter.
:- type block_map == map(label, block_info).
:- type block_info
---> block_info(
label,
% The label starting the block.
instruction,
% The instruction containing the label.
list(instruction),
% The code of the block without the initial
% label.
list(label),
% The labels we can jump to
% (not falling through).
maybe(label)
% The label we fall through to
% (if there is one).
).
:- pred create_basic_blocks(list(instruction)::in, list(instruction)::out,
proc_label::in, counter::in, counter::out,
list(label)::out, block_map::out) is det.
:- pred flatten_basic_blocks(list(label)::in, block_map::in,
list(instruction)::out) is det.
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module opt_util.
:- import_module bool, int, require.
create_basic_blocks(Instrs0, Comments, ProcLabel, C0, C,
LabelSeq, BlockMap) :-
opt_util__get_prologue(Instrs0, LabelInstr, Comments,
AfterLabelInstrs),
Instrs1 = [LabelInstr | AfterLabelInstrs],
map__init(BlockMap0),
build_block_map(Instrs1, LabelSeq, BlockMap0, BlockMap,
ProcLabel, C0, C).
% Add labels to the given instruction sequence so that
% every basic block has labels around it.
%-----------------------------------------------------------------------------%
:- pred build_block_map(list(instruction)::in, list(label)::out,
block_map::in, block_map::out, proc_label::in,
counter::in, counter::out) is det.
build_block_map([], [], BlockMap, BlockMap, _, C, C).
build_block_map([OrigInstr0 | OrigInstrs0], LabelSeq, BlockMap0, BlockMap,
ProcLabel, C0, C) :-
( OrigInstr0 = label(OrigLabel) - _ ->
Label = OrigLabel,
LabelInstr = OrigInstr0,
RestInstrs = OrigInstrs0,
C1 = C0
;
counter__allocate(N, C0, C1),
Label = local(N, ProcLabel),
LabelInstr = label(Label) - "",
RestInstrs = [OrigInstr0 | OrigInstrs0]
),
(
take_until_end_of_block(RestInstrs, BlockInstrs, Instrs1),
build_block_map(Instrs1, LabelSeq0,
BlockMap0, BlockMap1, ProcLabel, C1, C),
( list__last(BlockInstrs, LastInstr) ->
LastInstr = LastUinstr - _,
opt_util__possible_targets(LastUinstr, SideLabels),
opt_util__can_instr_fall_through(LastUinstr,
CanFallThrough),
( CanFallThrough = yes ->
get_fallthrough_from_seq(LabelSeq0,
MaybeFallThrough)
;
MaybeFallThrough = no
)
;
SideLabels = [],
get_fallthrough_from_seq(LabelSeq0,
MaybeFallThrough)
),
BlockInfo = block_info(Label, LabelInstr, BlockInstrs,
SideLabels, MaybeFallThrough),
map__det_insert(BlockMap1, Label, BlockInfo, BlockMap),
LabelSeq = [Label | LabelSeq0]
).
%-----------------------------------------------------------------------------%
:- pred take_until_end_of_block(list(instruction)::in,
list(instruction)::out, list(instruction)::out) is det.
take_until_end_of_block([], [], []).
take_until_end_of_block([Instr0 | Instrs0], BlockInstrs, Rest) :-
Instr0 = Uinstr0 - _Comment,
( Uinstr0 = label(_) ->
BlockInstrs = [],
Rest = [Instr0 | Instrs0]
; opt_util__can_instr_branch_away(Uinstr0, yes) ->
BlockInstrs = [Instr0],
Rest = Instrs0
;
take_until_end_of_block(Instrs0, BlockInstrs1, Rest),
BlockInstrs = [Instr0 | BlockInstrs1]
).
%-----------------------------------------------------------------------------%
:- pred get_fallthrough_from_seq(list(label)::in, maybe(label)::out) is det.
get_fallthrough_from_seq(LabelSeq, MaybeFallThrough) :-
( LabelSeq = [NextLabel | _] ->
MaybeFallThrough = yes(NextLabel)
;
MaybeFallThrough = no
).
%-----------------------------------------------------------------------------%
flatten_basic_blocks([], _, []).
flatten_basic_blocks([Label | Labels], BlockMap, Instrs) :-
flatten_basic_blocks(Labels, BlockMap, RestInstrs),
map__lookup(BlockMap, Label, BlockInfo),
BlockInfo = block_info(_, BlockLabelInstr, BlockInstrs, _, _),
list__append([BlockLabelInstr | BlockInstrs], RestInstrs, Instrs).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%