Compiler-Inlining.st 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. Smalltalk createPackage: 'Compiler-Inlining'!
  2. IRAssignment subclass: #IRInlinedAssignment
  3. instanceVariableNames: ''
  4. package: 'Compiler-Inlining'!
  5. !IRInlinedAssignment commentStamp!
  6. I represent an inlined assignment instruction.!
  7. !IRInlinedAssignment methodsFor: 'testing'!
  8. isInlined
  9. ^ true
  10. ! !
  11. !IRInlinedAssignment methodsFor: 'visiting'!
  12. accept: aVisitor
  13. ^ aVisitor visitIRInlinedAssignment: self
  14. ! !
  15. IRClosure subclass: #IRInlinedClosure
  16. instanceVariableNames: ''
  17. package: 'Compiler-Inlining'!
  18. !IRInlinedClosure commentStamp!
  19. I represent an inlined closure instruction.!
  20. !IRInlinedClosure methodsFor: 'testing'!
  21. isInlined
  22. ^ true
  23. ! !
  24. !IRInlinedClosure methodsFor: 'visiting'!
  25. accept: aVisitor
  26. aVisitor visitIRInlinedClosure: self
  27. ! !
  28. IRReturn subclass: #IRInlinedReturn
  29. instanceVariableNames: ''
  30. package: 'Compiler-Inlining'!
  31. !IRInlinedReturn commentStamp!
  32. I represent an inlined local return instruction.!
  33. !IRInlinedReturn methodsFor: 'testing'!
  34. isInlined
  35. ^ true
  36. ! !
  37. !IRInlinedReturn methodsFor: 'visiting'!
  38. accept: aVisitor
  39. ^ aVisitor visitIRInlinedReturn: self
  40. ! !
  41. IRSend subclass: #IRInlinedSend
  42. instanceVariableNames: ''
  43. package: 'Compiler-Inlining'!
  44. !IRInlinedSend commentStamp!
  45. I am the abstract super class of inlined message send instructions.!
  46. !IRInlinedSend methodsFor: 'accessing'!
  47. internalVariables
  48. "Answer a collection of internal variables required
  49. to perform the inlining"
  50. ^ #()
  51. ! !
  52. !IRInlinedSend methodsFor: 'testing'!
  53. isInlined
  54. ^ true
  55. ! !
  56. !IRInlinedSend methodsFor: 'visiting'!
  57. accept: aVisitor
  58. aVisitor visitInlinedSend: self
  59. ! !
  60. IRInlinedSend subclass: #IRInlinedIfFalse
  61. instanceVariableNames: ''
  62. package: 'Compiler-Inlining'!
  63. !IRInlinedIfFalse commentStamp!
  64. I represent an inlined `#ifFalse:` message send instruction.!
  65. !IRInlinedIfFalse methodsFor: 'visiting'!
  66. accept: aVisitor
  67. aVisitor visitIRInlinedIfFalse: self
  68. ! !
  69. IRInlinedSend subclass: #IRInlinedIfNilIfNotNil
  70. instanceVariableNames: ''
  71. package: 'Compiler-Inlining'!
  72. !IRInlinedIfNilIfNotNil commentStamp!
  73. I represent an inlined `#ifNil:ifNotNil:` message send instruction.!
  74. !IRInlinedIfNilIfNotNil methodsFor: 'accessing'!
  75. internalVariables
  76. ^ Array with: self receiverInternalVariable
  77. !
  78. receiverInternalVariable
  79. ^ IRVariable new
  80. variable: (AliasVar new name: self receiverInternalVariableName);
  81. yourself.
  82. !
  83. receiverInternalVariableName
  84. ^ '$receiver'
  85. ! !
  86. !IRInlinedIfNilIfNotNil methodsFor: 'visiting'!
  87. accept: aVisitor
  88. aVisitor visitIRInlinedIfNilIfNotNil: self
  89. ! !
  90. IRInlinedSend subclass: #IRInlinedIfTrue
  91. instanceVariableNames: ''
  92. package: 'Compiler-Inlining'!
  93. !IRInlinedIfTrue commentStamp!
  94. I represent an inlined `#ifTrue:` message send instruction.!
  95. !IRInlinedIfTrue methodsFor: 'visiting'!
  96. accept: aVisitor
  97. aVisitor visitIRInlinedIfTrue: self
  98. ! !
  99. IRInlinedSend subclass: #IRInlinedIfTrueIfFalse
  100. instanceVariableNames: ''
  101. package: 'Compiler-Inlining'!
  102. !IRInlinedIfTrueIfFalse commentStamp!
  103. I represent an inlined `#ifTrue:ifFalse:` message send instruction.!
  104. !IRInlinedIfTrueIfFalse methodsFor: 'visiting'!
  105. accept: aVisitor
  106. aVisitor visitIRInlinedIfTrueIfFalse: self
  107. ! !
  108. IRBlockSequence subclass: #IRInlinedSequence
  109. instanceVariableNames: ''
  110. package: 'Compiler-Inlining'!
  111. !IRInlinedSequence commentStamp!
  112. I represent a (block) sequence inside an inlined closure instruction (instance of `IRInlinedClosure`).!
  113. !IRInlinedSequence methodsFor: 'testing'!
  114. isInlined
  115. ^ true
  116. ! !
  117. !IRInlinedSequence methodsFor: 'visiting'!
  118. accept: aVisitor
  119. aVisitor visitIRInlinedSequence: self
  120. ! !
  121. IRVisitor subclass: #IRInliner
  122. instanceVariableNames: ''
  123. package: 'Compiler-Inlining'!
  124. !IRInliner commentStamp!
  125. I visit an IR tree, inlining message sends and block closures.
  126. Message selectors that can be inlined are answered by `IRSendInliner >> #inlinedSelectors`!
  127. !IRInliner methodsFor: 'factory'!
  128. assignmentInliner
  129. ^ IRAssignmentInliner new
  130. translator: self;
  131. yourself
  132. !
  133. returnInliner
  134. ^ IRReturnInliner new
  135. translator: self;
  136. yourself
  137. !
  138. sendInliner
  139. ^ IRSendInliner new
  140. translator: self;
  141. yourself
  142. ! !
  143. !IRInliner methodsFor: 'testing'!
  144. shouldInlineAssignment: anIRAssignment
  145. ^ anIRAssignment isInlined not and: [
  146. anIRAssignment instructions last isSend and: [
  147. self shouldInlineSend: (anIRAssignment instructions last) ]]
  148. !
  149. shouldInlineReturn: anIRReturn
  150. ^ anIRReturn isInlined not and: [
  151. anIRReturn instructions first isSend and: [
  152. self shouldInlineSend: (anIRReturn instructions first) ]]
  153. !
  154. shouldInlineSend: anIRSend
  155. ^ anIRSend isInlined not and: [
  156. IRSendInliner shouldInline: anIRSend ]
  157. ! !
  158. !IRInliner methodsFor: 'visiting'!
  159. transformNonLocalReturn: anIRNonLocalReturn
  160. "Replace a non local return into a local return"
  161. | localReturn |
  162. anIRNonLocalReturn scope canInlineNonLocalReturns ifTrue: [
  163. anIRNonLocalReturn scope methodScope removeNonLocalReturn: anIRNonLocalReturn scope.
  164. localReturn := IRReturn new
  165. scope: anIRNonLocalReturn scope;
  166. yourself.
  167. anIRNonLocalReturn instructions do: [ :each |
  168. localReturn add: each ].
  169. anIRNonLocalReturn replaceWith: localReturn.
  170. ^ localReturn ].
  171. ^ super visitIRNonLocalReturn: anIRNonLocalReturn
  172. !
  173. visitIRAssignment: anIRAssignment
  174. ^ (self shouldInlineAssignment: anIRAssignment)
  175. ifTrue: [ self assignmentInliner inlineAssignment: anIRAssignment ]
  176. ifFalse: [ super visitIRAssignment: anIRAssignment ]
  177. !
  178. visitIRNonLocalReturn: anIRNonLocalReturn
  179. ^ self transformNonLocalReturn: anIRNonLocalReturn
  180. !
  181. visitIRReturn: anIRReturn
  182. ^ (self shouldInlineReturn: anIRReturn)
  183. ifTrue: [ self returnInliner inlineReturn: anIRReturn ]
  184. ifFalse: [ super visitIRReturn: anIRReturn ]
  185. !
  186. visitIRSend: anIRSend
  187. ^ (self shouldInlineSend: anIRSend)
  188. ifTrue: [ self sendInliner inlineSend: anIRSend ]
  189. ifFalse: [ super visitIRSend: anIRSend ]
  190. ! !
  191. IRJSTranslator subclass: #IRInliningJSTranslator
  192. instanceVariableNames: ''
  193. package: 'Compiler-Inlining'!
  194. !IRInliningJSTranslator commentStamp!
  195. I am a specialized JavaScript translator able to write inlined IR instructions to JavaScript stream (`JSStream` instance).!
  196. !IRInliningJSTranslator methodsFor: 'visiting'!
  197. visitIRInlinedAssignment: anIRInlinedAssignment
  198. self visit: anIRInlinedAssignment instructions last
  199. !
  200. visitIRInlinedClosure: anIRInlinedClosure
  201. self stream nextPutVars: (anIRInlinedClosure tempDeclarations collect: [ :each |
  202. each name asVariableName ]).
  203. anIRInlinedClosure instructions do: [ :each |
  204. self visit: each ]
  205. !
  206. visitIRInlinedIfFalse: anIRInlinedIfFalse
  207. self stream nextPutIf: [
  208. self stream nextPutAll: '!!$core.assert('.
  209. self visit: anIRInlinedIfFalse instructions first.
  210. self stream nextPutAll: ')' ]
  211. with: [ self visit: anIRInlinedIfFalse instructions last ]
  212. !
  213. visitIRInlinedIfNilIfNotNil: anIRInlinedIfNilIfNotNil
  214. self stream
  215. nextPutIfElse: [
  216. self stream nextPutAll: '(', anIRInlinedIfNilIfNotNil receiverInternalVariableName, ' = '.
  217. self visit: anIRInlinedIfNilIfNotNil instructions first.
  218. self stream nextPutAll: ') == null || $receiver.isNil' ]
  219. with: [ self visit: anIRInlinedIfNilIfNotNil instructions second ]
  220. with: [ self visit: anIRInlinedIfNilIfNotNil instructions third ]
  221. !
  222. visitIRInlinedIfTrue: anIRInlinedIfTrue
  223. self stream nextPutIf: [
  224. self stream nextPutAll: '$core.assert('.
  225. self visit: anIRInlinedIfTrue instructions first.
  226. self stream nextPutAll: ')' ]
  227. with: [ self visit: anIRInlinedIfTrue instructions last ]
  228. !
  229. visitIRInlinedIfTrueIfFalse: anIRInlinedIfTrueIfFalse
  230. self stream
  231. nextPutIfElse: [
  232. self stream nextPutAll: '$core.assert('.
  233. self visit: anIRInlinedIfTrueIfFalse instructions first.
  234. self stream nextPutAll: ')' ]
  235. with: [ self visit: anIRInlinedIfTrueIfFalse instructions second ]
  236. with: [ self visit: anIRInlinedIfTrueIfFalse instructions third ]
  237. !
  238. visitIRInlinedNonLocalReturn: anIRInlinedReturn
  239. self stream nextPutStatementWith: [
  240. self visit: anIRInlinedReturn instructions last ].
  241. self stream nextPutNonLocalReturnWith: [ ]
  242. !
  243. visitIRInlinedReturn: anIRInlinedReturn
  244. self visit: anIRInlinedReturn instructions last
  245. !
  246. visitIRInlinedSequence: anIRInlinedSequence
  247. anIRInlinedSequence instructions do: [ :each |
  248. self stream nextPutStatementWith: [ self visit: each ]]
  249. ! !
  250. Object subclass: #IRSendInliner
  251. instanceVariableNames: 'send translator'
  252. package: 'Compiler-Inlining'!
  253. !IRSendInliner commentStamp!
  254. I inline some message sends and block closure arguments. I heavily rely on #perform: to dispatch inlining methods.!
  255. !IRSendInliner methodsFor: 'accessing'!
  256. send
  257. ^ send
  258. !
  259. send: anIRSend
  260. send := anIRSend
  261. !
  262. translator
  263. ^ translator
  264. !
  265. translator: anASTTranslator
  266. translator := anASTTranslator
  267. ! !
  268. !IRSendInliner methodsFor: 'error handling'!
  269. inliningError: aString
  270. InliningError signal: aString
  271. ! !
  272. !IRSendInliner methodsFor: 'factory'!
  273. inlinedClosure
  274. ^ IRInlinedClosure new
  275. !
  276. inlinedSequence
  277. ^ IRInlinedSequence new
  278. ! !
  279. !IRSendInliner methodsFor: 'inlining'!
  280. ifFalse: anIRInstruction
  281. ^ self inlinedSend: IRInlinedIfFalse new with: anIRInstruction
  282. !
  283. ifFalse: anIRInstruction ifTrue: anotherIRInstruction
  284. ^ self perform: #ifTrue:ifFalse: withArguments: { anotherIRInstruction. anIRInstruction }
  285. !
  286. ifNil: anIRInstruction
  287. ^ self
  288. inlinedSend: IRInlinedIfNilIfNotNil new
  289. with: anIRInstruction
  290. with: (IRClosure new
  291. scope: anIRInstruction scope copy;
  292. add: (IRBlockSequence new
  293. add: self send instructions first;
  294. yourself);
  295. yourself)
  296. !
  297. ifNil: anIRInstruction ifNotNil: anotherIRInstruction
  298. ^ self inlinedSend: IRInlinedIfNilIfNotNil new with: anIRInstruction with: anotherIRInstruction
  299. !
  300. ifNotNil: anIRInstruction
  301. ^ self
  302. inlinedSend: IRInlinedIfNilIfNotNil new
  303. with: (IRClosure new
  304. scope: anIRInstruction scope copy;
  305. add: (IRBlockSequence new
  306. add: self send instructions first;
  307. yourself);
  308. yourself)
  309. with: anIRInstruction
  310. !
  311. ifNotNil: anIRInstruction ifNil: anotherIRInstruction
  312. ^ self inlinedSend: IRInlinedIfNilIfNotNil new with: anotherIRInstruction with: anIRInstruction
  313. !
  314. ifTrue: anIRInstruction
  315. ^ self inlinedSend: IRInlinedIfTrue new with: anIRInstruction
  316. !
  317. ifTrue: anIRInstruction ifFalse: anotherIRInstruction
  318. ^ self inlinedSend: IRInlinedIfTrueIfFalse new with: anIRInstruction with: anotherIRInstruction
  319. !
  320. inlineClosure: anIRClosure
  321. | inlinedClosure sequence statements |
  322. inlinedClosure := self inlinedClosure.
  323. inlinedClosure
  324. scope: anIRClosure scope;
  325. parent: anIRClosure parent.
  326. "Add the possible temp declarations"
  327. anIRClosure tempDeclarations do: [ :each |
  328. inlinedClosure add: each ].
  329. "Add a block sequence"
  330. sequence := self inlinedSequence.
  331. "Map the closure arguments to the receiver of the message send"
  332. anIRClosure arguments do: [ :each |
  333. inlinedClosure add: (IRTempDeclaration new name: each; yourself).
  334. sequence add: (IRAssignment new
  335. add: (IRVariable new variable: (AliasVar new scope: inlinedClosure scope; name: each; yourself));
  336. add: (IRVariable new variable: (AliasVar new scope: inlinedClosure scope; name: '$receiver'; yourself));
  337. yourself) ].
  338. "To ensure the correct order of the closure instructions: first the temps then the sequence"
  339. inlinedClosure add: sequence.
  340. "Get all the statements"
  341. statements := anIRClosure instructions last instructions.
  342. statements ifNotEmpty: [
  343. statements allButLast do: [ :each | sequence add: each ].
  344. "Inlined closures don't have implicit local returns"
  345. (statements last isReturn and: [ statements last isBlockReturn ])
  346. ifTrue: [ sequence add: statements last instructions first ]
  347. ifFalse: [ sequence add: statements last ] ].
  348. ^ inlinedClosure
  349. !
  350. inlineSend: anIRSend
  351. self send: anIRSend.
  352. ^ self
  353. perform: self send selector
  354. withArguments: self send instructions allButFirst
  355. !
  356. inlinedSend: inlinedSend with: anIRInstruction
  357. | inlinedClosure |
  358. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  359. anIRInstruction arguments size = 0 ifFalse: [ self inliningError: 'Inlined block should have zero argument' ].
  360. inlinedClosure := self translator visit: (self inlineClosure: anIRInstruction).
  361. inlinedSend
  362. add: self send instructions first;
  363. add: inlinedClosure.
  364. self send replaceWith: inlinedSend.
  365. inlinedSend method internalVariables
  366. addAll: inlinedSend internalVariables.
  367. ^ inlinedSend
  368. !
  369. inlinedSend: inlinedSend with: anIRInstruction with: anotherIRInstruction
  370. | inlinedClosure1 inlinedClosure2 |
  371. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  372. anotherIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  373. inlinedClosure1 := self translator visit: (self inlineClosure: anIRInstruction).
  374. inlinedClosure2 := self translator visit: (self inlineClosure: anotherIRInstruction).
  375. inlinedSend
  376. add: self send instructions first;
  377. add: inlinedClosure1;
  378. add: inlinedClosure2.
  379. self send replaceWith: inlinedSend.
  380. inlinedSend method internalVariables
  381. addAll: inlinedSend internalVariables.
  382. ^ inlinedSend
  383. ! !
  384. !IRSendInliner class methodsFor: 'accessing'!
  385. inlinedSelectors
  386. ^ #('ifTrue:' 'ifFalse:' 'ifTrue:ifFalse:' 'ifFalse:ifTrue:' 'ifNil:' 'ifNotNil:' 'ifNil:ifNotNil:' 'ifNotNil:ifNil:')
  387. !
  388. shouldInline: anIRInstruction
  389. (self inlinedSelectors includes: anIRInstruction selector) ifFalse: [ ^ false ].
  390. ^ anIRInstruction instructions allButFirst allSatisfy: [ :each | each isClosure]
  391. ! !
  392. IRSendInliner subclass: #IRAssignmentInliner
  393. instanceVariableNames: 'assignment'
  394. package: 'Compiler-Inlining'!
  395. !IRAssignmentInliner commentStamp!
  396. I inline message sends together with assignments by moving them around into the inline closure instructions.
  397. ##Example
  398. foo
  399. | a |
  400. a := true ifTrue: [ 1 ]
  401. Will produce:
  402. if($core.assert(true) {
  403. a = 1;
  404. };!
  405. !IRAssignmentInliner methodsFor: 'accessing'!
  406. assignment
  407. ^ assignment
  408. !
  409. assignment: aNode
  410. assignment := aNode
  411. ! !
  412. !IRAssignmentInliner methodsFor: 'inlining'!
  413. inlineAssignment: anIRAssignment
  414. | inlinedAssignment |
  415. self assignment: anIRAssignment.
  416. inlinedAssignment := IRInlinedAssignment new.
  417. anIRAssignment instructions do: [ :each |
  418. inlinedAssignment add: each ].
  419. anIRAssignment replaceWith: inlinedAssignment.
  420. self inlineSend: inlinedAssignment instructions last.
  421. ^ inlinedAssignment
  422. !
  423. inlineClosure: anIRClosure
  424. | inlinedClosure statements |
  425. inlinedClosure := super inlineClosure: anIRClosure.
  426. statements := inlinedClosure instructions last instructions.
  427. statements ifNotEmpty: [
  428. statements last canBeAssigned ifTrue: [
  429. statements last replaceWith: (IRAssignment new
  430. add: self assignment instructions first;
  431. add: statements last copy;
  432. yourself) ] ].
  433. ^ inlinedClosure
  434. ! !
  435. IRSendInliner subclass: #IRReturnInliner
  436. instanceVariableNames: ''
  437. package: 'Compiler-Inlining'!
  438. !IRReturnInliner commentStamp!
  439. I inline message sends with inlined closure together with a return instruction.!
  440. !IRReturnInliner methodsFor: 'factory'!
  441. inlinedReturn
  442. ^ IRInlinedReturn new
  443. ! !
  444. !IRReturnInliner methodsFor: 'inlining'!
  445. inlineClosure: anIRClosure
  446. | closure statements |
  447. closure := super inlineClosure: anIRClosure.
  448. statements := closure instructions last instructions.
  449. statements ifNotEmpty: [
  450. statements last isReturn
  451. ifFalse: [ statements last replaceWith: (IRReturn new
  452. add: statements last copy;
  453. yourself)] ].
  454. ^ closure
  455. !
  456. inlineReturn: anIRReturn
  457. | return |
  458. return := self inlinedReturn.
  459. anIRReturn instructions do: [ :each |
  460. return add: each ].
  461. anIRReturn replaceWith: return.
  462. self inlineSend: return instructions last.
  463. ^ return
  464. ! !
  465. CodeGenerator subclass: #InliningCodeGenerator
  466. instanceVariableNames: ''
  467. package: 'Compiler-Inlining'!
  468. !InliningCodeGenerator commentStamp!
  469. I am a specialized code generator that uses inlining to produce more optimized JavaScript output!
  470. !InliningCodeGenerator methodsFor: 'compiling'!
  471. compileNode: aNode
  472. | ir stream |
  473. self semanticAnalyzer visit: aNode.
  474. ir := self translator visit: aNode.
  475. self inliner visit: ir.
  476. ^ self irTranslator
  477. currentClass: self currentClass;
  478. visit: ir;
  479. contents
  480. !
  481. inliner
  482. ^ IRInliner new
  483. !
  484. irTranslator
  485. ^ IRInliningJSTranslator new
  486. ! !
  487. SemanticError subclass: #InliningError
  488. instanceVariableNames: ''
  489. package: 'Compiler-Inlining'!
  490. !InliningError commentStamp!
  491. Instances of InliningError are signaled when using an `InliningCodeGenerator`in a `Compiler`.!