fpc/compiler/wpobase.pas
Jonas Maebe 060d81b8fa Merged revisions 11878,11881-11882,11889,11891-11893,11895,11899-11902,11935,11938,12212,12304,12308-12310,12316,12330-12332,12334,12339-12340 via svnmerge from
svn+ssh://jonas@svn.freepascal.org/FPC/svn/fpc/branches/wpo

........
r11878 | jonas | 2008-10-11 02:25:18 +0200 (Sat, 11 Oct 2008) | 19 lines

  + initial implementation of whole-program optimisation framework
  + implementation of whole-program devirtualisation
  o use:
     a) generate whole-program optimisation information (no need
        to completely compile the program and all of its units
        with -OW/-FW, only the main program is sufficient)
      fpc -OWdevirtcalls -FWmyprog.wpo myprog
     b) use it to optimise the program
      fpc -B -Owdevirtcalls -Fwmyprog.wpo myprog
     (the -B is not required, but only sources recompiled during
      the second pass will actually be optimised -- if you want,
      you can even rebuild the rtl devirtualised for a particular 
      program; and these options can obviously also be used         
      together with regular optimisation switches)
  o warning:
    - there are no checks yet to ensure that you do not use 
      units optimised for a particular program with another
      program (or with a changed version of the same program)

........
r11881 | jonas | 2008-10-11 19:35:52 +0200 (Sat, 11 Oct 2008) | 13 lines

  * extracted code to detect constructed class/object types from
    tcallnode.gen_vmt_tree into its own method to avoid clutter
  * detect x.classtype.create constructs (with classtype = the
    system.tobject.classtype method), and treat them as if a
    "class of x" has been instantiated rather than a
    "class of tobject". this required storing the instantiated
    classrefs in their own array though, because at such a
    point we don't have a "class of x" tdef available (so
    now "x", and all other defs instantiated via a classref,
    are now stored as tobjectdefs in a separate array)
  + support for devirtualising class methods (including
    constructors)

........
r11882 | jonas | 2008-10-11 20:44:02 +0200 (Sat, 11 Oct 2008) | 7 lines

  + -Owoptvmts whole program optimisation which replaces vmt entries
    with method names of child classes in case the current class'
    method can never be called (e.g., because this class is never
    instantiated). As a result, such methods can then be removed
    by dead code removal/smart linking (not much effect for either
    the compiler, lazarus or a trivial lazarus app though).

........
r11889 | jonas | 2008-10-12 14:29:54 +0200 (Sun, 12 Oct 2008) | 2 lines

  * some comment fixes

........
r11891 | jonas | 2008-10-12 18:49:13 +0200 (Sun, 12 Oct 2008) | 4 lines

  * fixed twpofilereader.getnextnoncommentline() when reusing a previously
    read line
  * fixed skipping of unnecessary wpo feedback file sections

........
r11892 | jonas | 2008-10-12 23:42:43 +0200 (Sun, 12 Oct 2008) | 31 lines

  + symbol liveness wpo information extracted from smartlinked programs
    (-OW/-Owsymbolliveness)
  + use symbol liveness information to improve devirtualisation (don't
    consider classes created in code that has been dead code stripped).
    This requires at least two passes of using wpo (first uses dead code
    info to locate classes that are constructed only in dead code,
    second pass uses this info to potentially further devirtualise).
    I.e.:
     1) generate initial liveness and devirtualisation feedback
       fpc -FWtt.wpo -OWall tt.pp -Xs- -CX -XX
     2) use previously generated feedback, and regenerate new feedback
        based on this (i.e., disregard classes created in dead code)
       fpc -FWtt-1.wpo -OWall -Fwtt.wo -Owall tt.pp -Xs- -CX -XX
     3) use the newly generated feedback (in theory, it is possible
        that even more opportunities pop up afterwards; you can
        continue until the program does not get smaller anymore)
       fpc -Fwtt-1.wpo -Owall tt.pp -CX -XX
  * changed all message() to cgmessage() calls so the set codegenerror
  * changed static fsectionhandlers field to a regular field called
    fwpocomponents
  * changed registration of wpocomponents: no longer happens in the
    initialization section of their unit, but in the InitWpo routine
    (which has been moved from the woinfo to the wpo unit). This way
    you can register different classes based on the target/parameters.
  + added static method to twpocomponentbase for checking whether
    the command line parameters don't conflict with the requested
    optimisations (e.g. generating liveness info requires that
    smartlinking is turned on)
  + added static method to twpocomponentbase to request the
    section name

........
r11893 | jonas | 2008-10-12 23:53:57 +0200 (Sun, 12 Oct 2008) | 3 lines

  * fixed comment error (twpodeadcodeinfo keeps a list of live,
    not dead symbols)

........
r11895 | jonas | 2008-10-13 00:13:59 +0200 (Mon, 13 Oct 2008) | 2 lines

  + documented -OW<x>, -Ow<x>, -FW<x> and -Fw<x> wpo parameters

........
r11899 | jonas | 2008-10-14 22:14:56 +0200 (Tue, 14 Oct 2008) | 2 lines

  * replaced hardcoded string with objdumpsearchstr constant

........
r11900 | jonas | 2008-10-14 22:15:25 +0200 (Tue, 14 Oct 2008) | 2 lines

  * reset wpofeedbackinput and wpofeedbackoutput in wpodone

........
r11901 | jonas | 2008-10-14 22:16:07 +0200 (Tue, 14 Oct 2008) | 2 lines

  * various additional comments and comment fixes

........
r11902 | jonas | 2008-10-15 18:09:42 +0200 (Wed, 15 Oct 2008) | 5 lines

  * store vmt procdefs in the ppu files so we don't have to use a hack to
    regenerate them for whole-program optimisation
  * fixed crash when performing devirtualisation optimisation on programs
    that do not construct any classes/objects with optimisable vmts

........
r11935 | jonas | 2008-10-19 12:24:26 +0200 (Sun, 19 Oct 2008) | 4 lines

  * set the vmt entries of non-class virtual methods of not instantiated
    objects/classes to FPC_ABSTRACTERROR so the code they refer to can
    be thrown away if it is not referred to in any other way either

........
r11938 | jonas | 2008-10-19 20:55:02 +0200 (Sun, 19 Oct 2008) | 7 lines

  * record all classrefdefs/objdefs for which a loadvmtaddrnode is generated,
    and instead of marking all classes that derive from instantiated
    classrefdefs as instantiated, only mark those classes from the above
    collection that derive from instantiated classrefdefs as
    instantiated (since to instantiate a class, you have to load its vmt
    somehow -- this may be broken by using assembler code though)

........
r12212 | jonas | 2008-11-23 12:26:34 +0100 (Sun, 23 Nov 2008) | 3 lines

  * fixed to work with the new vmtentries that are always available and
    removed previously added code to save/load vmtentries to ppu files

........
r12304 | jonas | 2008-12-05 22:23:30 +0100 (Fri, 05 Dec 2008) | 4 lines

  * check whether the correct wpo feedback file is used in the current
    compilation when using units that were compiled using wpo information
    during a previous compilation run

........
r12308 | jonas | 2008-12-06 18:03:39 +0100 (Sat, 06 Dec 2008) | 2 lines

  * abort compilation if an error occurred during wpo initialisation

........
r12309 | jonas | 2008-12-06 18:04:28 +0100 (Sat, 06 Dec 2008) | 3 lines

  * give an error message instead of crashing with an io exception if the
    compiler is unable to create the wpo feedback file specified using -FW

........
r12310 | jonas | 2008-12-06 18:12:43 +0100 (Sat, 06 Dec 2008) | 3 lines

  * don't let the used wpo feedback file influence the interface crc (there's
    a separate check for such changes)

........
r12316 | jonas | 2008-12-08 19:08:25 +0100 (Mon, 08 Dec 2008) | 3 lines

  * document the format of the sections of the wpo feedback file inside the
    feedback file itself

........
r12330 | jonas | 2008-12-10 22:26:47 +0100 (Wed, 10 Dec 2008) | 2 lines

  * use sysutils instead of dos to avoid command line length limits

........
r12331 | jonas | 2008-12-10 22:31:11 +0100 (Wed, 10 Dec 2008) | 3 lines

  + support for testing whole program optimisation tests (multiple
    compilations using successively generated feedback files)

........
r12332 | jonas | 2008-12-10 22:31:40 +0100 (Wed, 10 Dec 2008) | 2 lines

  + whole program optimisation tests

........
r12334 | jonas | 2008-12-10 22:38:07 +0100 (Wed, 10 Dec 2008) | 2 lines

  - removed unused local variable

........
r12339 | jonas | 2008-12-11 18:06:36 +0100 (Thu, 11 Dec 2008) | 2 lines

  + comments for newly added fields to tobjectdef for devirtualisation

........
r12340 | jonas | 2008-12-11 18:10:01 +0100 (Thu, 11 Dec 2008) | 2 lines

  * increase ppu version (was no longer different from trunk due to merging)

........

git-svn-id: trunk@12341 -
2008-12-11 17:40:18 +00:00

681 lines
21 KiB
ObjectPascal
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
Copyright (c) 2008 by Jonas Maebe
Whole program optimisation information collection base class
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
****************************************************************************
}
unit wpobase;
{$i fpcdefs.inc}
interface
uses
globtype,
cclasses,
symtype;
type
{ the types of available whole program optimization }
twpotype = (wpo_devirtualization_context_insensitive,wpo_live_symbol_information);
const
wpo2str: array[twpotype] of string[16] = ('devirtualization','symbol liveness');
type
{ ************************************************************************* }
{ ******************** General base classes/interfaces ******************** }
{ ************************************************************************* }
{ interface to reading a section from a file with wpo info }
twposectionreaderintf = interface
['{51BE3F89-C9C5-4965-9C83-AE7490C92E3E}']
function sectiongetnextline(out s: string): boolean;
end;
{ interface to writing sections to a file with wpoinfo }
twposectionwriterintf = interface
['{C056F0DD-62B1-4612-86C7-2D39944C4437}']
procedure startsection(const name: string);
procedure sectionputline(const s: string);
end;
{ base class for wpo information stores }
{ twpocomponentbase }
twpocomponentbase = class
public
constructor create; reintroduce; virtual;
{ type of whole program optimization information collected/provided by
this class
}
class function getwpotype: twpotype; virtual; abstract;
{ whole program optimizations for which this class generates information }
class function generatesinfoforwposwitches: twpoptimizerswitches; virtual; abstract;
{ whole program optimizations performed by this class }
class function performswpoforswitches: twpoptimizerswitches; virtual; abstract;
{ returns the name of the section parsed by this class }
class function sectionname: shortstring; virtual; abstract;
{ checks whether the compiler options are compatible with this
optimization (default: don't check anything)
}
class procedure checkoptions; virtual;
{ loads the information pertinent to this whole program optimization from
the current section being processed by reader
}
procedure loadfromwpofilesection(reader: twposectionreaderintf); virtual; abstract;
{ stores the information of this component to a file in a format that can
be loaded again using loadfromwpofilesection()
}
procedure storewpofilesection(writer: twposectionwriterintf); virtual; abstract;
{ extracts the information pertinent to this whole program optimization
from the current compiler state (loaded units, ...)
}
procedure constructfromcompilerstate; virtual; abstract;
end;
twpocomponentbaseclass = class of twpocomponentbase;
{ forward declaration of overall wpo info manager class }
twpoinfomanagerbase = class;
{ ************************************************************************* }
{ ** Information created per unit for use during subsequent compilation *** }
{ ************************************************************************* }
{ base class of information collected per unit. Still needs to be
generalised for different kinds of wpo information, currently specific
to devirtualization.
}
tunitwpoinfobase = class
protected
{ created object types }
fcreatedobjtypes: tfpobjectlist;
{ objectdefs pointed to by created classrefdefs }
fcreatedclassrefobjtypes: tfpobjectlist;
{ objtypes potentially instantiated by fcreatedclassrefobjtypes
(objdectdefs pointed to by classrefdefs that are
passed as a regular parameter, loaded in a variable, ...
so they can end up in a classrefdef var and be instantiated)
}
fmaybecreatedbyclassrefdeftypes: tfpobjectlist;
public
constructor create; reintroduce; virtual;
destructor destroy; override;
property createdobjtypes: tfpobjectlist read fcreatedobjtypes;
property createdclassrefobjtypes: tfpobjectlist read fcreatedclassrefobjtypes;
property maybecreatedbyclassrefdeftypes: tfpobjectlist read fmaybecreatedbyclassrefdeftypes;
procedure addcreatedobjtype(def: tdef);
procedure addcreatedobjtypeforclassref(def: tdef);
procedure addmaybecreatedbyclassref(def: tdef);
end;
{ ************************************************************************* }
{ **** Total information created for use during subsequent compilation **** }
{ ************************************************************************* }
{ class to create a file with wpo information }
{ tavailablewpofilewriter }
twpofilewriter = class(tobject,twposectionwriterintf)
private
{ array of class *instances* that wish to be written out to the
whole program optimization feedback file
}
fsectioncontents: tfpobjectlist;
ffilename: tcmdstr;
foutputfile: text;
public
constructor create(const fn: tcmdstr);
destructor destroy; override;
procedure writefile;
{ starts a new section with name "name" }
procedure startsection(const name: string);
{ writes s to the wpo file }
procedure sectionputline(const s: string);
{ register a component instance that needs to be written
to the wpo feedback file
}
procedure registerwpocomponent(component: twpocomponentbase);
end;
{ ************************************************************************* }
{ ************ Information for use during current compilation ************* }
{ ************************************************************************* }
{ class to read a file with wpo information }
twpofilereader = class(tobject,twposectionreaderintf)
private
ffilename: tcmdstr;
flinenr: longint;
finputfile: text;
fcurline: string;
fusecurline: boolean;
{ destination for the read information }
fdest: twpoinfomanagerbase;
function getnextnoncommentline(out s: string): boolean;
public
constructor create(const fn: tcmdstr; dest: twpoinfomanagerbase);
destructor destroy; override;
{ processes the wpo info in the file }
procedure processfile;
{ returns next line of the current section in s, and false if no more
lines in the current section
}
function sectiongetnextline(out s: string): boolean;
end;
{ ************************************************************************* }
{ ******* Specific kinds of whole program optimization components ********* }
{ ************************************************************************* }
{ method devirtualisation }
twpodevirtualisationhandler = class(twpocomponentbase)
{ checks whether procdef (a procdef for a virtual method) can be replaced with
a static call when it's called as objdef.procdef, and if so returns the
mangled name in staticname.
}
function staticnameforcallingvirtualmethod(objdef, procdef: tdef; out staticname: string): boolean; virtual; abstract;
{ checks whether procdef (a procdef for a virtual method) can be replaced with
a different procname in the vmt of objdef, and if so returns the new
mangledname in staticname
}
function staticnameforvmtentry(objdef, procdef: tdef; out staticname: string): boolean; virtual; abstract;
end;
twpodeadcodehandler = class(twpocomponentbase)
{ checks whether a mangledname was removed as dead code from the final
binary (WARNING: must *not* be called for functions marked as inline,
since if all call sites are inlined, it won't appear in the final
binary but nevertheless is still necessary!)
}
function symbolinfinalbinary(const s: shortstring): boolean; virtual; abstract;
end;
{ ************************************************************************* }
{ ************ Collection of all instances of wpo components ************** }
{ ************************************************************************* }
{ class doing all the bookkeeping for everything }
twpoinfomanagerbase = class
private
{ array of classrefs of handler classes for the various kinds of whole
program optimizations that we support
}
fwpocomponents: tfphashlist;
freader: twpofilereader;
fwriter: twpofilewriter;
public
{ instances of the various optimizers/information collectors (for
information used during this compilation)
}
wpoinfouse: array[twpotype] of twpocomponentbase;
{ register a whole program optimization class type }
procedure registerwpocomponentclass(wpocomponent: twpocomponentbaseclass);
{ get the program optimization class type that can parse the contents
of the section with name "secname" in the wpo feedback file
}
function gethandlerforsection(const secname: string): twpocomponentbaseclass;
{ tell all instantiated wpo component classes to collect the information
from the global compiler state that they need (done at the very end of
the compilation process)
}
procedure extractwpoinfofromprogram;
{ set the name of the feedback file from which all whole-program information
to be used during the current compilation will be read
}
procedure setwpoinputfile(const fn: tcmdstr);
{ set the name of the feedback file to which all whole-program information
collected during the current compilation will be written
}
procedure setwpooutputfile(const fn: tcmdstr);
{ check whether the specified wpo options (-FW/-Fw/-OW/-Ow) are complete
and sensical, and parse the wpo feedback file specified with
setwpoinputfile
}
procedure parseandcheckwpoinfo;
{ routines accessing the optimizer information }
{ 1) devirtualization at the symbol name level }
function can_be_devirtualized(objdef, procdef: tdef; out name: shortstring): boolean; virtual; abstract;
{ 2) optimal replacement method name in vmt }
function optimized_name_for_vmt(objdef, procdef: tdef; out name: shortstring): boolean; virtual; abstract;
{ 3) does a symbol appear in the final binary (i.e., not removed by dead code stripping/smart linking).
WARNING: do *not* call for inline functions/procedures/methods/...
}
function symbol_live(const name: shortstring): boolean; virtual; abstract;
constructor create; reintroduce;
destructor destroy; override;
end;
var
wpoinfomanager: twpoinfomanagerbase;
implementation
uses
globals,
cutils,
sysutils,
symdef,
verbose;
{ tcreatedwpoinfobase }
constructor tunitwpoinfobase.create;
begin
fcreatedobjtypes:=tfpobjectlist.create(false);
fcreatedclassrefobjtypes:=tfpobjectlist.create(false);
fmaybecreatedbyclassrefdeftypes:=tfpobjectlist.create(false);
end;
destructor tunitwpoinfobase.destroy;
begin
fcreatedobjtypes.free;
fcreatedobjtypes:=nil;
fcreatedclassrefobjtypes.free;
fcreatedclassrefobjtypes:=nil;
fmaybecreatedbyclassrefdeftypes.free;
fmaybecreatedbyclassrefdeftypes:=nil;
inherited destroy;
end;
procedure tunitwpoinfobase.addcreatedobjtype(def: tdef);
begin
fcreatedobjtypes.add(def);
end;
procedure tunitwpoinfobase.addcreatedobjtypeforclassref(def: tdef);
begin
fcreatedclassrefobjtypes.add(def);
end;
procedure tunitwpoinfobase.addmaybecreatedbyclassref(def: tdef);
begin
fmaybecreatedbyclassrefdeftypes.add(def);
end;
{ twpofilereader }
function twpofilereader.getnextnoncommentline(out s: string):
boolean;
begin
if (fusecurline) then
begin
s:=fcurline;
fusecurline:=false;
result:=true;
exit;
end;
repeat
readln(finputfile,s);
if (s='') and
eof(finputfile) then
begin
result:=false;
exit;
end;
inc(flinenr);
until (s='') or
(s[1]<>'#');
result:=true;
end;
constructor twpofilereader.create(const fn: tcmdstr; dest: twpoinfomanagerbase);
begin
if not FileExists(fn) then
begin
cgmessage1(wpo_cant_find_file,fn);
exit;
end;
assign(finputfile,fn);
ffilename:=fn;
fdest:=dest;
end;
destructor twpofilereader.destroy;
begin
inherited destroy;
end;
procedure twpofilereader.processfile;
var
sectionhandler: twpocomponentbaseclass;
i: longint;
wpotype: twpotype;
s,
sectionname: string;
begin
cgmessage1(wpo_begin_processing,ffilename);
reset(finputfile);
flinenr:=0;
while getnextnoncommentline(s) do
begin
if (s='') then
continue;
{ format: "% sectionname" }
if (s[1]<>'%') then
begin
cgmessage2(wpo_expected_section,tostr(flinenr),s);
break;
end;
for i:=2 to length(s) do
if (s[i]<>' ') then
break;
sectionname:=copy(s,i,255);
{ find handler for section and process }
sectionhandler:=fdest.gethandlerforsection(sectionname);
if assigned(sectionhandler) then
begin
wpotype:=sectionhandler.getwpotype;
cgmessage2(wpo_found_section,sectionname,wpo2str[wpotype]);
{ do we need this information? }
if ((sectionhandler.performswpoforswitches * init_settings.dowpoptimizerswitches) <> []) then
begin
{ did some other section already generate this type of information? }
if assigned(fdest.wpoinfouse[wpotype]) then
begin
cgmessage2(wpo_duplicate_wpotype,wpo2str[wpotype],sectionname);
fdest.wpoinfouse[wpotype].free;
end;
{ process the section }
fdest.wpoinfouse[wpotype]:=sectionhandler.create;
twpocomponentbase(fdest.wpoinfouse[wpotype]).loadfromwpofilesection(self);
end
else
begin
cgmessage1(wpo_skipping_unnecessary_section,sectionname);
{ skip the current section }
while sectiongetnextline(s) do
;
end;
end
else
begin
cgmessage1(wpo_no_section_handler,sectionname);
{ skip the current section }
while sectiongetnextline(s) do
;
end;
end;
close(finputfile);
cgmessage1(wpo_end_processing,ffilename);
end;
function twpofilereader.sectiongetnextline(out s: string): boolean;
begin
result:=getnextnoncommentline(s);
if not result then
exit;
{ start of new section? }
if (s<>'') and
(s[1]='%') then
begin
{ keep read line for next call to getnextnoncommentline() }
fcurline:=s;
fusecurline:=true;
result:=false;
end;
end;
{ twpocomponentbase }
constructor twpocomponentbase.create;
begin
{ do nothing }
end;
class procedure twpocomponentbase.checkoptions;
begin
{ do nothing }
end;
{ twpofilewriter }
constructor twpofilewriter.create(const fn: tcmdstr);
begin
assign(foutputfile,fn);
ffilename:=fn;
fsectioncontents:=tfpobjectlist.create(true);
end;
destructor twpofilewriter.destroy;
begin
fsectioncontents.free;
inherited destroy;
end;
procedure twpofilewriter.writefile;
var
i: longint;
begin
{$i-}
rewrite(foutputfile);
{$i+}
if (ioresult <> 0) then
begin
cgmessage1(wpo_cant_create_feedback_file,ffilename);
exit;
end;
for i:=0 to fsectioncontents.count-1 do
twpocomponentbase(fsectioncontents[i]).storewpofilesection(self);
close(foutputfile);
end;
procedure twpofilewriter.startsection(const name: string);
begin
writeln(foutputfile,'% ',name);
end;
procedure twpofilewriter.sectionputline(const s: string);
begin
writeln(foutputfile,s);
end;
procedure twpofilewriter.registerwpocomponent(
component: twpocomponentbase);
begin
fsectioncontents.add(component);
end;
{ twpoinfomanagerbase }
procedure twpoinfomanagerbase.registerwpocomponentclass(wpocomponent: twpocomponentbaseclass);
begin
fwpocomponents.add(wpocomponent.sectionname,wpocomponent);
end;
function twpoinfomanagerbase.gethandlerforsection(const secname: string
): twpocomponentbaseclass;
begin
result:=twpocomponentbaseclass(fwpocomponents.find(secname));
end;
procedure twpoinfomanagerbase.setwpoinputfile(const fn: tcmdstr);
begin
freader:=twpofilereader.create(fn,self);
end;
procedure twpoinfomanagerbase.setwpooutputfile(const fn: tcmdstr);
begin
fwriter:=twpofilewriter.create(fn);
end;
procedure twpoinfomanagerbase.parseandcheckwpoinfo;
var
i: longint;
begin
{ error if we don't have to optimize yet have an input feedback file }
if (init_settings.dowpoptimizerswitches=[]) and
assigned(freader) then
begin
cgmessage(wpo_input_without_info_use);
exit;
end;
{ error if we have to optimize yet don't have an input feedback file }
if (init_settings.dowpoptimizerswitches<>[]) and
not assigned(freader) then
begin
cgmessage(wpo_no_input_specified);
exit;
end;
{ if we have to generate wpo information, check that a file has been
specified and that we have something to write to it
}
if (init_settings.genwpoptimizerswitches<>[]) and
not assigned(fwriter) then
begin
cgmessage(wpo_no_output_specified);
exit;
end;
if (init_settings.genwpoptimizerswitches=[]) and
assigned(fwriter) then
begin
cgmessage(wpo_output_without_info_gen);
exit;
end;
{ now read the input feedback file }
if assigned(freader) then
begin
freader.processfile;
freader.free;
freader:=nil;
end;
{ and for each specified optimization check whether the input feedback
file contained the necessary information
}
if (([cs_wpo_devirtualize_calls,cs_wpo_optimize_vmts] * init_settings.dowpoptimizerswitches) <> []) and
not assigned(wpoinfouse[wpo_devirtualization_context_insensitive]) then
begin
cgmessage1(wpo_not_enough_info,wpo2str[wpo_devirtualization_context_insensitive]);
exit;
end;
if (cs_wpo_symbol_liveness in init_settings.dowpoptimizerswitches) and
not assigned(wpoinfouse[wpo_live_symbol_information]) then
begin
cgmessage1(wpo_not_enough_info,wpo2str[wpo_live_symbol_information]);
exit;
end;
{ perform pre-checking to ensure there are no known incompatibilities between
the selected optimizations and other switches
}
for i:=0 to fwpocomponents.count-1 do
if (twpocomponentbaseclass(fwpocomponents[i]).generatesinfoforwposwitches*init_settings.genwpoptimizerswitches)<>[] then
twpocomponentbaseclass(fwpocomponents[i]).checkoptions
end;
procedure twpoinfomanagerbase.extractwpoinfofromprogram;
var
i: longint;
info: twpocomponentbase;
begin
{ if don't have to write anything, fwriter has not been created }
if not assigned(fwriter) then
exit;
{ let all wpo components gather the necessary info from the compiler state }
for i:=0 to fwpocomponents.count-1 do
if (twpocomponentbaseclass(fwpocomponents[i]).generatesinfoforwposwitches*current_settings.genwpoptimizerswitches)<>[] then
begin
info:=twpocomponentbaseclass(fwpocomponents[i]).create;
info.constructfromcompilerstate;
fwriter.registerwpocomponent(info);
end;
{ and write their info to disk }
fwriter.writefile;
fwriter.free;
fwriter:=nil;
end;
constructor twpoinfomanagerbase.create;
begin
inherited create;
fwpocomponents:=tfphashlist.create;
end;
destructor twpoinfomanagerbase.destroy;
var
i: twpotype;
begin
freader.free;
freader:=nil;
fwriter.free;
fwriter:=nil;
fwpocomponents.free;
fwpocomponents:=nil;
for i:=low(wpoinfouse) to high(wpoinfouse) do
if assigned(wpoinfouse[i]) then
wpoinfouse[i].free;
inherited destroy;
end;
end.