wpobase.pas 19 KB

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