wpobase.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. {
  2. Copyright (c) 2008 by Jonas Maebe
  3. Whole program optimisation information collection base class
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit wpobase;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. globtype,
  22. cclasses,
  23. symtype;
  24. type
  25. { the types of available whole program optimization }
  26. twpotype = (wpo_devirtualization_context_insensitive,wpo_live_symbol_information);
  27. const
  28. wpo2str: array[twpotype] of string[16] = ('devirtualization','symbol liveness');
  29. type
  30. { ************************************************************************* }
  31. { ******************** General base classes/interfaces ******************** }
  32. { ************************************************************************* }
  33. { interface to reading a section from a file with wpo info }
  34. twposectionreaderintf = interface
  35. ['{51BE3F89-C9C5-4965-9C83-AE7490C92E3E}']
  36. function sectiongetnextline(out s: string): boolean;
  37. end;
  38. { interface to writing sections to a file with wpoinfo }
  39. twposectionwriterintf = interface
  40. ['{C056F0DD-62B1-4612-86C7-2D39944C4437}']
  41. procedure startsection(const name: string);
  42. procedure sectionputline(const s: string);
  43. end;
  44. { base class for wpo information stores }
  45. { twpocomponentbase }
  46. twpocomponentbase = class
  47. public
  48. constructor create; reintroduce; virtual;
  49. { type of whole program optimization information collected/provided by
  50. this class
  51. }
  52. class function getwpotype: twpotype; virtual; abstract;
  53. { whole program optimizations for which this class generates information }
  54. class function generatesinfoforwposwitches: twpoptimizerswitches; virtual; abstract;
  55. { whole program optimizations performed by this class }
  56. class function performswpoforswitches: twpoptimizerswitches; virtual; abstract;
  57. { returns the name of the section parsed by this class }
  58. class function sectionname: shortstring; virtual; abstract;
  59. { checks whether the compiler options are compatible with this
  60. optimization (default: don't check anything)
  61. }
  62. class procedure checkoptions; virtual;
  63. { loads the information pertinent to this whole program optimization from
  64. the current section being processed by reader
  65. }
  66. procedure loadfromwpofilesection(reader: twposectionreaderintf); virtual; abstract;
  67. { stores the information of this component to a file in a format that can
  68. be loaded again using loadfromwpofilesection()
  69. }
  70. procedure storewpofilesection(writer: twposectionwriterintf); virtual; abstract;
  71. { extracts the information pertinent to this whole program optimization
  72. from the current compiler state (loaded units, ...)
  73. }
  74. procedure constructfromcompilerstate; virtual; abstract;
  75. end;
  76. twpocomponentbaseclass = class of twpocomponentbase;
  77. { forward declaration of overall wpo info manager class }
  78. twpoinfomanagerbase = class;
  79. { ************************************************************************* }
  80. { ** Information created per unit for use during subsequent compilation *** }
  81. { ************************************************************************* }
  82. { base class of information collected per unit. Still needs to be
  83. generalised for different kinds of wpo information, currently specific
  84. to devirtualization.
  85. }
  86. tunitwpoinfobase = class
  87. protected
  88. { created object types }
  89. fcreatedobjtypes: tfpobjectlist;
  90. { objectdefs pointed to by created classrefdefs }
  91. fcreatedclassrefobjtypes: tfpobjectlist;
  92. public
  93. constructor create; reintroduce; virtual;
  94. destructor destroy; override;
  95. property createdobjtypes: tfpobjectlist read fcreatedobjtypes;
  96. property createdclassrefobjtypes: tfpobjectlist read fcreatedclassrefobjtypes;
  97. procedure addcreatedobjtype(def: tdef);
  98. procedure addcreatedobjtypeforclassref(def: tdef);
  99. end;
  100. { ************************************************************************* }
  101. { **** Total information created for use during subsequent compilation **** }
  102. { ************************************************************************* }
  103. { class to create a file with wpo information }
  104. { tavailablewpofilewriter }
  105. twpofilewriter = class(tobject,twposectionwriterintf)
  106. private
  107. { array of class *instances* that wish to be written out to the
  108. whole program optimization feedback file
  109. }
  110. fsectioncontents: tfpobjectlist;
  111. ffilename: tcmdstr;
  112. foutputfile: text;
  113. public
  114. constructor create(const fn: tcmdstr);
  115. destructor destroy; override;
  116. procedure writefile;
  117. { starts a new section with name "name" }
  118. procedure startsection(const name: string);
  119. { writes s to the wpo file }
  120. procedure sectionputline(const s: string);
  121. { register a component instance that needs to be written
  122. to the wpo feedback file
  123. }
  124. procedure registerwpocomponent(component: twpocomponentbase);
  125. end;
  126. { ************************************************************************* }
  127. { ************ Information for use during current compilation ************* }
  128. { ************************************************************************* }
  129. { class to read a file with wpo information }
  130. twpofilereader = class(tobject,twposectionreaderintf)
  131. private
  132. ffilename: tcmdstr;
  133. flinenr: longint;
  134. finputfile: text;
  135. fcurline: string;
  136. fusecurline: boolean;
  137. { destination for the read information }
  138. fdest: twpoinfomanagerbase;
  139. function getnextnoncommentline(out s: string): boolean;
  140. public
  141. constructor create(const fn: tcmdstr; dest: twpoinfomanagerbase);
  142. destructor destroy; override;
  143. { processes the wpo info in the file }
  144. procedure processfile;
  145. { returns next line of the current section in s, and false if no more
  146. lines in the current section
  147. }
  148. function sectiongetnextline(out s: string): boolean;
  149. end;
  150. { ************************************************************************* }
  151. { ******* Specific kinds of whole program optimization components ********* }
  152. { ************************************************************************* }
  153. { method devirtualisation }
  154. twpodevirtualisationhandler = class(twpocomponentbase)
  155. { checks whether procdef (a procdef for a virtual method) can be replaced with
  156. a static call when it's called as objdef.procdef, and if so returns the
  157. mangled name in staticname.
  158. }
  159. function staticnameforvirtualmethod(objdef, procdef: tdef; out staticname: string): boolean; virtual; abstract;
  160. end;
  161. twpodeadcodehandler = class(twpocomponentbase)
  162. { checks whether a mangledname was removed as dead code from the final
  163. binary (WARNING: must *not* be called for functions marked as inline,
  164. since if all call sites are inlined, it won't appear in the final
  165. binary but nevertheless is still necessary!)
  166. }
  167. function symbolinfinalbinary(const s: shortstring): boolean; virtual; abstract;
  168. end;
  169. { ************************************************************************* }
  170. { ************ Collection of all instances of wpo components ************** }
  171. { ************************************************************************* }
  172. { class doing all the bookkeeping for everything }
  173. twpoinfomanagerbase = class
  174. private
  175. { array of classrefs of handler classes for the various kinds of whole
  176. program optimizations that we support
  177. }
  178. fwpocomponents: tfphashlist;
  179. freader: twpofilereader;
  180. fwriter: twpofilewriter;
  181. public
  182. { instances of the various optimizers/information collectors (for
  183. information used during this compilation)
  184. }
  185. wpoinfouse: array[twpotype] of twpocomponentbase;
  186. { register a whole program optimization class type }
  187. procedure registerwpocomponentclass(wpocomponent: twpocomponentbaseclass);
  188. { get the program optimization class type that can parse the contents
  189. of the section with name "secname" in the wpo feedback file
  190. }
  191. function gethandlerforsection(const secname: string): twpocomponentbaseclass;
  192. { tell all instantiated wpo component classes to collect the information
  193. from the global compiler state that they need (done at the very end of
  194. the compilation process)
  195. }
  196. procedure extractwpoinfofromprogram;
  197. { set the name of the feedback file from which all whole-program information
  198. to be used during the current compilation will be read
  199. }
  200. procedure setwpoinputfile(const fn: tcmdstr);
  201. { set the name of the feedback file to which all whole-program information
  202. collected during the current compilation will be written
  203. }
  204. procedure setwpooutputfile(const fn: tcmdstr);
  205. { check whether the specified wpo options (-FW/-Fw/-OW/-Ow) are complete
  206. and sensical, and parse the wpo feedback file specified with
  207. setwpoinputfile
  208. }
  209. procedure parseandcheckwpoinfo;
  210. { routines accessing the optimizer information }
  211. { 1) devirtualization at the symbol name level }
  212. function can_be_devirtualized(objdef, procdef: tdef; out name: shortstring): boolean; virtual; abstract;
  213. { 2) optimal replacement method name in vmt }
  214. function optimized_name_for_vmt(objdef, procdef: tdef; out name: shortstring): boolean; virtual; abstract;
  215. { 3) does a symbol appear in the final binary (i.e., not removed by dead code stripping/smart linking).
  216. WARNING: do *not* call for inline functions/procedures/methods/...
  217. }
  218. function symbol_live(const name: shortstring): boolean; virtual; abstract;
  219. constructor create; reintroduce;
  220. destructor destroy; override;
  221. end;
  222. var
  223. wpoinfomanager: twpoinfomanagerbase;
  224. implementation
  225. uses
  226. globals,
  227. cutils,
  228. sysutils,
  229. symdef,
  230. verbose;
  231. { tcreatedwpoinfobase }
  232. constructor tunitwpoinfobase.create;
  233. begin
  234. fcreatedobjtypes:=tfpobjectlist.create(false);
  235. fcreatedclassrefobjtypes:=tfpobjectlist.create(false);
  236. end;
  237. destructor tunitwpoinfobase.destroy;
  238. begin
  239. fcreatedobjtypes.free;
  240. fcreatedobjtypes:=nil;
  241. fcreatedclassrefobjtypes.free;
  242. fcreatedclassrefobjtypes:=nil;
  243. inherited destroy;
  244. end;
  245. procedure tunitwpoinfobase.addcreatedobjtype(def: tdef);
  246. begin
  247. fcreatedobjtypes.add(def);
  248. end;
  249. procedure tunitwpoinfobase.addcreatedobjtypeforclassref(def: tdef);
  250. begin
  251. fcreatedclassrefobjtypes.add(def);
  252. end;
  253. { twpofilereader }
  254. function twpofilereader.getnextnoncommentline(out s: string):
  255. boolean;
  256. begin
  257. if (fusecurline) then
  258. begin
  259. s:=fcurline;
  260. fusecurline:=false;
  261. result:=true;
  262. exit;
  263. end;
  264. repeat
  265. readln(finputfile,s);
  266. if (s='') and
  267. eof(finputfile) then
  268. begin
  269. result:=false;
  270. exit;
  271. end;
  272. inc(flinenr);
  273. until (s='') or
  274. (s[1]<>'#');
  275. result:=true;
  276. end;
  277. constructor twpofilereader.create(const fn: tcmdstr; dest: twpoinfomanagerbase);
  278. begin
  279. if not FileExists(fn) then
  280. begin
  281. cgmessage1(wpo_cant_find_file,fn);
  282. exit;
  283. end;
  284. assign(finputfile,fn);
  285. ffilename:=fn;
  286. fdest:=dest;
  287. end;
  288. destructor twpofilereader.destroy;
  289. begin
  290. inherited destroy;
  291. end;
  292. procedure twpofilereader.processfile;
  293. var
  294. sectionhandler: twpocomponentbaseclass;
  295. i: longint;
  296. wpotype: twpotype;
  297. s,
  298. sectionname: string;
  299. begin
  300. cgmessage1(wpo_begin_processing,ffilename);
  301. reset(finputfile);
  302. flinenr:=0;
  303. while getnextnoncommentline(s) do
  304. begin
  305. if (s='') then
  306. continue;
  307. { format: "% sectionname" }
  308. if (s[1]<>'%') then
  309. begin
  310. cgmessage2(wpo_expected_section,tostr(flinenr),s);
  311. break;
  312. end;
  313. for i:=2 to length(s) do
  314. if (s[i]<>' ') then
  315. break;
  316. sectionname:=copy(s,i,255);
  317. { find handler for section and process }
  318. sectionhandler:=fdest.gethandlerforsection(sectionname);
  319. if assigned(sectionhandler) then
  320. begin
  321. wpotype:=sectionhandler.getwpotype;
  322. cgmessage2(wpo_found_section,sectionname,wpo2str[wpotype]);
  323. { do we need this information? }
  324. if ((sectionhandler.performswpoforswitches * init_settings.dowpoptimizerswitches) <> []) then
  325. begin
  326. { did some other section already generate this type of information? }
  327. if assigned(fdest.wpoinfouse[wpotype]) then
  328. begin
  329. cgmessage2(wpo_duplicate_wpotype,wpo2str[wpotype],sectionname);
  330. fdest.wpoinfouse[wpotype].free;
  331. end;
  332. { process the section }
  333. fdest.wpoinfouse[wpotype]:=sectionhandler.create;
  334. twpocomponentbase(fdest.wpoinfouse[wpotype]).loadfromwpofilesection(self);
  335. end
  336. else
  337. begin
  338. cgmessage1(wpo_skipping_unnecessary_section,sectionname);
  339. { skip the current section }
  340. while sectiongetnextline(s) do
  341. ;
  342. end;
  343. end
  344. else
  345. begin
  346. cgmessage1(wpo_no_section_handler,sectionname);
  347. { skip the current section }
  348. while sectiongetnextline(s) do
  349. ;
  350. end;
  351. end;
  352. close(finputfile);
  353. cgmessage1(wpo_end_processing,ffilename);
  354. end;
  355. function twpofilereader.sectiongetnextline(out s: string): boolean;
  356. begin
  357. result:=getnextnoncommentline(s);
  358. if not result then
  359. exit;
  360. { start of new section? }
  361. if (s<>'') and
  362. (s[1]='%') then
  363. begin
  364. { keep read line for next call to getnextnoncommentline() }
  365. fcurline:=s;
  366. fusecurline:=true;
  367. result:=false;
  368. end;
  369. end;
  370. { twpocomponentbase }
  371. constructor twpocomponentbase.create;
  372. begin
  373. { do nothing }
  374. end;
  375. class procedure twpocomponentbase.checkoptions;
  376. begin
  377. { do nothing }
  378. end;
  379. { twpofilewriter }
  380. constructor twpofilewriter.create(const fn: tcmdstr);
  381. begin
  382. assign(foutputfile,fn);
  383. ffilename:=fn;
  384. fsectioncontents:=tfpobjectlist.create(true);
  385. end;
  386. destructor twpofilewriter.destroy;
  387. begin
  388. fsectioncontents.free;
  389. inherited destroy;
  390. end;
  391. procedure twpofilewriter.writefile;
  392. var
  393. i: longint;
  394. begin
  395. rewrite(foutputfile);
  396. for i:=0 to fsectioncontents.count-1 do
  397. twpocomponentbase(fsectioncontents[i]).storewpofilesection(self);
  398. close(foutputfile);
  399. end;
  400. procedure twpofilewriter.startsection(const name: string);
  401. begin
  402. writeln(foutputfile,'% ',name);
  403. end;
  404. procedure twpofilewriter.sectionputline(const s: string);
  405. begin
  406. writeln(foutputfile,s);
  407. end;
  408. procedure twpofilewriter.registerwpocomponent(
  409. component: twpocomponentbase);
  410. begin
  411. fsectioncontents.add(component);
  412. end;
  413. { twpoinfomanagerbase }
  414. procedure twpoinfomanagerbase.registerwpocomponentclass(wpocomponent: twpocomponentbaseclass);
  415. begin
  416. fwpocomponents.add(wpocomponent.sectionname,wpocomponent);
  417. end;
  418. function twpoinfomanagerbase.gethandlerforsection(const secname: string
  419. ): twpocomponentbaseclass;
  420. begin
  421. result:=twpocomponentbaseclass(fwpocomponents.find(secname));
  422. end;
  423. procedure twpoinfomanagerbase.setwpoinputfile(const fn: tcmdstr);
  424. begin
  425. freader:=twpofilereader.create(fn,self);
  426. end;
  427. procedure twpoinfomanagerbase.setwpooutputfile(const fn: tcmdstr);
  428. begin
  429. fwriter:=twpofilewriter.create(fn);
  430. end;
  431. procedure twpoinfomanagerbase.parseandcheckwpoinfo;
  432. var
  433. i: longint;
  434. begin
  435. { error if we don't have to optimize yet have an input feedback file }
  436. if (init_settings.dowpoptimizerswitches=[]) and
  437. assigned(freader) then
  438. begin
  439. cgmessage(wpo_input_without_info_use);
  440. exit;
  441. end;
  442. { error if we have to optimize yet don't have an input feedback file }
  443. if (init_settings.dowpoptimizerswitches<>[]) and
  444. not assigned(freader) then
  445. begin
  446. cgmessage(wpo_no_input_specified);
  447. exit;
  448. end;
  449. { if we have to generate wpo information, check that a file has been
  450. specified and that we have something to write to it
  451. }
  452. if (init_settings.genwpoptimizerswitches<>[]) and
  453. not assigned(fwriter) then
  454. begin
  455. cgmessage(wpo_no_output_specified);
  456. exit;
  457. end;
  458. if (init_settings.genwpoptimizerswitches=[]) and
  459. assigned(fwriter) then
  460. begin
  461. cgmessage(wpo_output_without_info_gen);
  462. exit;
  463. end;
  464. { now read the input feedback file }
  465. if assigned(freader) then
  466. begin
  467. freader.processfile;
  468. freader.free;
  469. freader:=nil;
  470. end;
  471. { and for each specified optimization check whether the input feedback
  472. file contained the necessary information
  473. }
  474. if (([cs_wpo_devirtualize_calls,cs_wpo_optimize_vmts] * init_settings.dowpoptimizerswitches) <> []) and
  475. not assigned(wpoinfouse[wpo_devirtualization_context_insensitive]) then
  476. begin
  477. cgmessage1(wpo_not_enough_info,wpo2str[wpo_devirtualization_context_insensitive]);
  478. exit;
  479. end;
  480. if (cs_wpo_symbol_liveness in init_settings.dowpoptimizerswitches) and
  481. not assigned(wpoinfouse[wpo_live_symbol_information]) then
  482. begin
  483. cgmessage1(wpo_not_enough_info,wpo2str[wpo_live_symbol_information]);
  484. exit;
  485. end;
  486. { perform pre-checking to ensure there are no known incompatibilities between
  487. the selected optimizations and other switches
  488. }
  489. for i:=0 to fwpocomponents.count-1 do
  490. if (twpocomponentbaseclass(fwpocomponents[i]).generatesinfoforwposwitches*init_settings.genwpoptimizerswitches)<>[] then
  491. twpocomponentbaseclass(fwpocomponents[i]).checkoptions
  492. end;
  493. procedure twpoinfomanagerbase.extractwpoinfofromprogram;
  494. var
  495. i: longint;
  496. info: twpocomponentbase;
  497. begin
  498. { if don't have to write anything, fwriter has not been created }
  499. if not assigned(fwriter) then
  500. exit;
  501. { let all wpo components gather the necessary info from the compiler state }
  502. for i:=0 to fwpocomponents.count-1 do
  503. if (twpocomponentbaseclass(fwpocomponents[i]).generatesinfoforwposwitches*current_settings.genwpoptimizerswitches)<>[] then
  504. begin
  505. info:=twpocomponentbaseclass(fwpocomponents[i]).create;
  506. info.constructfromcompilerstate;
  507. fwriter.registerwpocomponent(info);
  508. end;
  509. { and write their info to disk }
  510. fwriter.writefile;
  511. fwriter.free;
  512. fwriter:=nil;
  513. end;
  514. constructor twpoinfomanagerbase.create;
  515. begin
  516. inherited create;
  517. fwpocomponents:=tfphashlist.create;
  518. end;
  519. destructor twpoinfomanagerbase.destroy;
  520. var
  521. i: twpotype;
  522. begin
  523. freader.free;
  524. freader:=nil;
  525. fwriter.free;
  526. fwriter:=nil;
  527. fwpocomponents.free;
  528. fwpocomponents:=nil;
  529. for i:=low(wpoinfouse) to high(wpoinfouse) do
  530. if assigned(wpoinfouse[i]) then
  531. wpoinfouse[i].free;
  532. inherited destroy;
  533. end;
  534. end.