wpobase.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 staticnameforcallingvirtualmethod(objdef, procdef: tdef; out staticname: string): boolean; virtual; abstract;
  160. { checks whether procdef (a procdef for a virtual method) can be replaced with
  161. a different procname in the vmt of objdef, and if so returns the new
  162. mangledname in staticname
  163. }
  164. function staticnameforvmtentry(objdef, procdef: tdef; out staticname: string): boolean; virtual; abstract;
  165. end;
  166. twpodeadcodehandler = class(twpocomponentbase)
  167. { checks whether a mangledname was removed as dead code from the final
  168. binary (WARNING: must *not* be called for functions marked as inline,
  169. since if all call sites are inlined, it won't appear in the final
  170. binary but nevertheless is still necessary!)
  171. }
  172. function symbolinfinalbinary(const s: shortstring): boolean; virtual; abstract;
  173. end;
  174. { ************************************************************************* }
  175. { ************ Collection of all instances of wpo components ************** }
  176. { ************************************************************************* }
  177. { class doing all the bookkeeping for everything }
  178. twpoinfomanagerbase = class
  179. private
  180. { array of classrefs of handler classes for the various kinds of whole
  181. program optimizations that we support
  182. }
  183. fwpocomponents: tfphashlist;
  184. freader: twpofilereader;
  185. fwriter: twpofilewriter;
  186. public
  187. { instances of the various optimizers/information collectors (for
  188. information used during this compilation)
  189. }
  190. wpoinfouse: array[twpotype] of twpocomponentbase;
  191. { register a whole program optimization class type }
  192. procedure registerwpocomponentclass(wpocomponent: twpocomponentbaseclass);
  193. { get the program optimization class type that can parse the contents
  194. of the section with name "secname" in the wpo feedback file
  195. }
  196. function gethandlerforsection(const secname: string): twpocomponentbaseclass;
  197. { tell all instantiated wpo component classes to collect the information
  198. from the global compiler state that they need (done at the very end of
  199. the compilation process)
  200. }
  201. procedure extractwpoinfofromprogram;
  202. { set the name of the feedback file from which all whole-program information
  203. to be used during the current compilation will be read
  204. }
  205. procedure setwpoinputfile(const fn: tcmdstr);
  206. { set the name of the feedback file to which all whole-program information
  207. collected during the current compilation will be written
  208. }
  209. procedure setwpooutputfile(const fn: tcmdstr);
  210. { check whether the specified wpo options (-FW/-Fw/-OW/-Ow) are complete
  211. and sensical, and parse the wpo feedback file specified with
  212. setwpoinputfile
  213. }
  214. procedure parseandcheckwpoinfo;
  215. { routines accessing the optimizer information }
  216. { 1) devirtualization at the symbol name level }
  217. function can_be_devirtualized(objdef, procdef: tdef; out name: shortstring): boolean; virtual; abstract;
  218. { 2) optimal replacement method name in vmt }
  219. function optimized_name_for_vmt(objdef, procdef: tdef; out name: shortstring): boolean; virtual; abstract;
  220. { 3) does a symbol appear in the final binary (i.e., not removed by dead code stripping/smart linking).
  221. WARNING: do *not* call for inline functions/procedures/methods/...
  222. }
  223. function symbol_live(const name: shortstring): boolean; virtual; abstract;
  224. constructor create; reintroduce;
  225. destructor destroy; override;
  226. end;
  227. var
  228. wpoinfomanager: twpoinfomanagerbase;
  229. implementation
  230. uses
  231. globals,
  232. cutils,
  233. sysutils,
  234. symdef,
  235. verbose;
  236. { tcreatedwpoinfobase }
  237. constructor tunitwpoinfobase.create;
  238. begin
  239. fcreatedobjtypes:=tfpobjectlist.create(false);
  240. fcreatedclassrefobjtypes:=tfpobjectlist.create(false);
  241. end;
  242. destructor tunitwpoinfobase.destroy;
  243. begin
  244. fcreatedobjtypes.free;
  245. fcreatedobjtypes:=nil;
  246. fcreatedclassrefobjtypes.free;
  247. fcreatedclassrefobjtypes:=nil;
  248. inherited destroy;
  249. end;
  250. procedure tunitwpoinfobase.addcreatedobjtype(def: tdef);
  251. begin
  252. fcreatedobjtypes.add(def);
  253. end;
  254. procedure tunitwpoinfobase.addcreatedobjtypeforclassref(def: tdef);
  255. begin
  256. fcreatedclassrefobjtypes.add(def);
  257. end;
  258. { twpofilereader }
  259. function twpofilereader.getnextnoncommentline(out s: string):
  260. boolean;
  261. begin
  262. if (fusecurline) then
  263. begin
  264. s:=fcurline;
  265. fusecurline:=false;
  266. result:=true;
  267. exit;
  268. end;
  269. repeat
  270. readln(finputfile,s);
  271. if (s='') and
  272. eof(finputfile) then
  273. begin
  274. result:=false;
  275. exit;
  276. end;
  277. inc(flinenr);
  278. until (s='') or
  279. (s[1]<>'#');
  280. result:=true;
  281. end;
  282. constructor twpofilereader.create(const fn: tcmdstr; dest: twpoinfomanagerbase);
  283. begin
  284. if not FileExists(fn) then
  285. begin
  286. cgmessage1(wpo_cant_find_file,fn);
  287. exit;
  288. end;
  289. assign(finputfile,fn);
  290. ffilename:=fn;
  291. fdest:=dest;
  292. end;
  293. destructor twpofilereader.destroy;
  294. begin
  295. inherited destroy;
  296. end;
  297. procedure twpofilereader.processfile;
  298. var
  299. sectionhandler: twpocomponentbaseclass;
  300. i: longint;
  301. wpotype: twpotype;
  302. s,
  303. sectionname: string;
  304. begin
  305. cgmessage1(wpo_begin_processing,ffilename);
  306. reset(finputfile);
  307. flinenr:=0;
  308. while getnextnoncommentline(s) do
  309. begin
  310. if (s='') then
  311. continue;
  312. { format: "% sectionname" }
  313. if (s[1]<>'%') then
  314. begin
  315. cgmessage2(wpo_expected_section,tostr(flinenr),s);
  316. break;
  317. end;
  318. for i:=2 to length(s) do
  319. if (s[i]<>' ') then
  320. break;
  321. sectionname:=copy(s,i,255);
  322. { find handler for section and process }
  323. sectionhandler:=fdest.gethandlerforsection(sectionname);
  324. if assigned(sectionhandler) then
  325. begin
  326. wpotype:=sectionhandler.getwpotype;
  327. cgmessage2(wpo_found_section,sectionname,wpo2str[wpotype]);
  328. { do we need this information? }
  329. if ((sectionhandler.performswpoforswitches * init_settings.dowpoptimizerswitches) <> []) then
  330. begin
  331. { did some other section already generate this type of information? }
  332. if assigned(fdest.wpoinfouse[wpotype]) then
  333. begin
  334. cgmessage2(wpo_duplicate_wpotype,wpo2str[wpotype],sectionname);
  335. fdest.wpoinfouse[wpotype].free;
  336. end;
  337. { process the section }
  338. fdest.wpoinfouse[wpotype]:=sectionhandler.create;
  339. twpocomponentbase(fdest.wpoinfouse[wpotype]).loadfromwpofilesection(self);
  340. end
  341. else
  342. begin
  343. cgmessage1(wpo_skipping_unnecessary_section,sectionname);
  344. { skip the current section }
  345. while sectiongetnextline(s) do
  346. ;
  347. end;
  348. end
  349. else
  350. begin
  351. cgmessage1(wpo_no_section_handler,sectionname);
  352. { skip the current section }
  353. while sectiongetnextline(s) do
  354. ;
  355. end;
  356. end;
  357. close(finputfile);
  358. cgmessage1(wpo_end_processing,ffilename);
  359. end;
  360. function twpofilereader.sectiongetnextline(out s: string): boolean;
  361. begin
  362. result:=getnextnoncommentline(s);
  363. if not result then
  364. exit;
  365. { start of new section? }
  366. if (s<>'') and
  367. (s[1]='%') then
  368. begin
  369. { keep read line for next call to getnextnoncommentline() }
  370. fcurline:=s;
  371. fusecurline:=true;
  372. result:=false;
  373. end;
  374. end;
  375. { twpocomponentbase }
  376. constructor twpocomponentbase.create;
  377. begin
  378. { do nothing }
  379. end;
  380. class procedure twpocomponentbase.checkoptions;
  381. begin
  382. { do nothing }
  383. end;
  384. { twpofilewriter }
  385. constructor twpofilewriter.create(const fn: tcmdstr);
  386. begin
  387. assign(foutputfile,fn);
  388. ffilename:=fn;
  389. fsectioncontents:=tfpobjectlist.create(true);
  390. end;
  391. destructor twpofilewriter.destroy;
  392. begin
  393. fsectioncontents.free;
  394. inherited destroy;
  395. end;
  396. procedure twpofilewriter.writefile;
  397. var
  398. i: longint;
  399. begin
  400. rewrite(foutputfile);
  401. for i:=0 to fsectioncontents.count-1 do
  402. twpocomponentbase(fsectioncontents[i]).storewpofilesection(self);
  403. close(foutputfile);
  404. end;
  405. procedure twpofilewriter.startsection(const name: string);
  406. begin
  407. writeln(foutputfile,'% ',name);
  408. end;
  409. procedure twpofilewriter.sectionputline(const s: string);
  410. begin
  411. writeln(foutputfile,s);
  412. end;
  413. procedure twpofilewriter.registerwpocomponent(
  414. component: twpocomponentbase);
  415. begin
  416. fsectioncontents.add(component);
  417. end;
  418. { twpoinfomanagerbase }
  419. procedure twpoinfomanagerbase.registerwpocomponentclass(wpocomponent: twpocomponentbaseclass);
  420. begin
  421. fwpocomponents.add(wpocomponent.sectionname,wpocomponent);
  422. end;
  423. function twpoinfomanagerbase.gethandlerforsection(const secname: string
  424. ): twpocomponentbaseclass;
  425. begin
  426. result:=twpocomponentbaseclass(fwpocomponents.find(secname));
  427. end;
  428. procedure twpoinfomanagerbase.setwpoinputfile(const fn: tcmdstr);
  429. begin
  430. freader:=twpofilereader.create(fn,self);
  431. end;
  432. procedure twpoinfomanagerbase.setwpooutputfile(const fn: tcmdstr);
  433. begin
  434. fwriter:=twpofilewriter.create(fn);
  435. end;
  436. procedure twpoinfomanagerbase.parseandcheckwpoinfo;
  437. var
  438. i: longint;
  439. begin
  440. { error if we don't have to optimize yet have an input feedback file }
  441. if (init_settings.dowpoptimizerswitches=[]) and
  442. assigned(freader) then
  443. begin
  444. cgmessage(wpo_input_without_info_use);
  445. exit;
  446. end;
  447. { error if we have to optimize yet don't have an input feedback file }
  448. if (init_settings.dowpoptimizerswitches<>[]) and
  449. not assigned(freader) then
  450. begin
  451. cgmessage(wpo_no_input_specified);
  452. exit;
  453. end;
  454. { if we have to generate wpo information, check that a file has been
  455. specified and that we have something to write to it
  456. }
  457. if (init_settings.genwpoptimizerswitches<>[]) and
  458. not assigned(fwriter) then
  459. begin
  460. cgmessage(wpo_no_output_specified);
  461. exit;
  462. end;
  463. if (init_settings.genwpoptimizerswitches=[]) and
  464. assigned(fwriter) then
  465. begin
  466. cgmessage(wpo_output_without_info_gen);
  467. exit;
  468. end;
  469. { now read the input feedback file }
  470. if assigned(freader) then
  471. begin
  472. freader.processfile;
  473. freader.free;
  474. freader:=nil;
  475. end;
  476. { and for each specified optimization check whether the input feedback
  477. file contained the necessary information
  478. }
  479. if (([cs_wpo_devirtualize_calls,cs_wpo_optimize_vmts] * init_settings.dowpoptimizerswitches) <> []) and
  480. not assigned(wpoinfouse[wpo_devirtualization_context_insensitive]) then
  481. begin
  482. cgmessage1(wpo_not_enough_info,wpo2str[wpo_devirtualization_context_insensitive]);
  483. exit;
  484. end;
  485. if (cs_wpo_symbol_liveness in init_settings.dowpoptimizerswitches) and
  486. not assigned(wpoinfouse[wpo_live_symbol_information]) then
  487. begin
  488. cgmessage1(wpo_not_enough_info,wpo2str[wpo_live_symbol_information]);
  489. exit;
  490. end;
  491. { perform pre-checking to ensure there are no known incompatibilities between
  492. the selected optimizations and other switches
  493. }
  494. for i:=0 to fwpocomponents.count-1 do
  495. if (twpocomponentbaseclass(fwpocomponents[i]).generatesinfoforwposwitches*init_settings.genwpoptimizerswitches)<>[] then
  496. twpocomponentbaseclass(fwpocomponents[i]).checkoptions
  497. end;
  498. procedure twpoinfomanagerbase.extractwpoinfofromprogram;
  499. var
  500. i: longint;
  501. info: twpocomponentbase;
  502. begin
  503. { if don't have to write anything, fwriter has not been created }
  504. if not assigned(fwriter) then
  505. exit;
  506. { let all wpo components gather the necessary info from the compiler state }
  507. for i:=0 to fwpocomponents.count-1 do
  508. if (twpocomponentbaseclass(fwpocomponents[i]).generatesinfoforwposwitches*current_settings.genwpoptimizerswitches)<>[] then
  509. begin
  510. info:=twpocomponentbaseclass(fwpocomponents[i]).create;
  511. info.constructfromcompilerstate;
  512. fwriter.registerwpocomponent(info);
  513. end;
  514. { and write their info to disk }
  515. fwriter.writefile;
  516. fwriter.free;
  517. fwriter:=nil;
  518. end;
  519. constructor twpoinfomanagerbase.create;
  520. begin
  521. inherited create;
  522. fwpocomponents:=tfphashlist.create;
  523. end;
  524. destructor twpoinfomanagerbase.destroy;
  525. var
  526. i: twpotype;
  527. begin
  528. freader.free;
  529. freader:=nil;
  530. fwriter.free;
  531. fwriter:=nil;
  532. fwpocomponents.free;
  533. fwpocomponents:=nil;
  534. for i:=low(wpoinfouse) to high(wpoinfouse) do
  535. if assigned(wpoinfouse[i]) then
  536. wpoinfouse[i].free;
  537. inherited destroy;
  538. end;
  539. end.