Compiler-Inlining.st 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. acceptDagVisitor: 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. acceptDagVisitor: 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. acceptDagVisitor: 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. acceptDagVisitor: 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. acceptDagVisitor: 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. acceptDagVisitor: 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. acceptDagVisitor: 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. acceptDagVisitor: 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. acceptDagVisitor: 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 right isSend and: [
  147. self shouldInlineSend: anIRAssignment right ]]
  148. !
  149. shouldInlineReturn: anIRReturn
  150. ^ anIRReturn isInlined not and: [
  151. anIRReturn expression isSend and: [
  152. self shouldInlineSend: anIRReturn expression ]]
  153. !
  154. shouldInlineSend: anIRSend
  155. ^ anIRSend isInlined not and: [
  156. IRSendInliner shouldInline: anIRSend ]
  157. ! !
  158. !IRInliner methodsFor: 'visiting'!
  159. visitIRAssignment: anIRAssignment
  160. ^ (self shouldInlineAssignment: anIRAssignment)
  161. ifTrue: [ self assignmentInliner inlineAssignment: anIRAssignment ]
  162. ifFalse: [ super visitIRAssignment: anIRAssignment ]
  163. !
  164. visitIRNonLocalReturn: anIRNonLocalReturn
  165. | localReturn |
  166. anIRNonLocalReturn scope canInlineNonLocalReturns ifTrue: [
  167. anIRNonLocalReturn scope methodScope removeNonLocalReturn: anIRNonLocalReturn scope.
  168. localReturn := IRReturn new
  169. scope: anIRNonLocalReturn scope;
  170. yourself.
  171. anIRNonLocalReturn dagChildren do: [ :each |
  172. localReturn add: each ].
  173. anIRNonLocalReturn replaceWith: localReturn.
  174. ^ localReturn ].
  175. ^ super visitIRNonLocalReturn: anIRNonLocalReturn
  176. !
  177. visitIRReturn: anIRReturn
  178. ^ (self shouldInlineReturn: anIRReturn)
  179. ifTrue: [ self returnInliner inlineReturn: anIRReturn ]
  180. ifFalse: [ super visitIRReturn: anIRReturn ]
  181. !
  182. visitIRSend: anIRSend
  183. ^ (self shouldInlineSend: anIRSend)
  184. ifTrue: [ self sendInliner inlineSend: anIRSend ]
  185. ifFalse: [ super visitIRSend: anIRSend ]
  186. ! !
  187. IRJSTranslator subclass: #IRInliningJSTranslator
  188. instanceVariableNames: ''
  189. package: 'Compiler-Inlining'!
  190. !IRInliningJSTranslator commentStamp!
  191. I am a specialized JavaScript translator able to write inlined IR instructions to JavaScript stream (`JSStream` instance).!
  192. !IRInliningJSTranslator methodsFor: 'visiting'!
  193. visitIRInlinedAssignment: anIRInlinedAssignment
  194. self visit: anIRInlinedAssignment right
  195. !
  196. visitIRInlinedClosure: anIRInlinedClosure
  197. self stream nextPutVars: (anIRInlinedClosure tempDeclarations collect: [ :each |
  198. each name asVariableName ]).
  199. self visitAll: anIRInlinedClosure dagChildren
  200. !
  201. visitIRInlinedIfFalse: anIRInlinedIfFalse
  202. self stream nextPutIf: [
  203. self stream nextPutAll: '!!$core.assert('.
  204. self visit: anIRInlinedIfFalse dagChildren first.
  205. self stream nextPutAll: ')' ]
  206. then: [ self visit: anIRInlinedIfFalse dagChildren last ]
  207. !
  208. visitIRInlinedIfNilIfNotNil: anIRInlinedIfNilIfNotNil
  209. self stream
  210. nextPutIf: [
  211. | recvVarName |
  212. recvVarName := anIRInlinedIfNilIfNotNil receiverInternalVariableName.
  213. self stream nextPutAll: '(', recvVarName, ' = '.
  214. self visit: anIRInlinedIfNilIfNotNil dagChildren first.
  215. self stream nextPutAll: ') == null || ', recvVarName, '.isNil' ]
  216. then: [ self visit: anIRInlinedIfNilIfNotNil dagChildren second ]
  217. else: [ self visit: anIRInlinedIfNilIfNotNil dagChildren third ]
  218. !
  219. visitIRInlinedIfTrue: anIRInlinedIfTrue
  220. self stream nextPutIf: [
  221. self stream nextPutAll: '$core.assert('.
  222. self visit: anIRInlinedIfTrue dagChildren first.
  223. self stream nextPutAll: ')' ]
  224. then: [ self visit: anIRInlinedIfTrue dagChildren last ]
  225. !
  226. visitIRInlinedIfTrueIfFalse: anIRInlinedIfTrueIfFalse
  227. self stream
  228. nextPutIf: [
  229. self stream nextPutAll: '$core.assert('.
  230. self visit: anIRInlinedIfTrueIfFalse dagChildren first.
  231. self stream nextPutAll: ')' ]
  232. then: [ self visit: anIRInlinedIfTrueIfFalse dagChildren second ]
  233. else: [ self visit: anIRInlinedIfTrueIfFalse dagChildren third ]
  234. !
  235. visitIRInlinedReturn: anIRInlinedReturn
  236. self visit: anIRInlinedReturn expression
  237. !
  238. visitIRInlinedSequence: anIRInlinedSequence
  239. anIRInlinedSequence dagChildren do: [ :each |
  240. self stream nextPutStatementWith: [ self visit: each ]]
  241. ! !
  242. Object subclass: #IRSendInliner
  243. instanceVariableNames: 'send translator'
  244. package: 'Compiler-Inlining'!
  245. !IRSendInliner commentStamp!
  246. I inline some message sends and block closure arguments. I heavily rely on #perform: to dispatch inlining methods.!
  247. !IRSendInliner methodsFor: 'accessing'!
  248. send
  249. ^ send
  250. !
  251. send: anIRSend
  252. send := anIRSend
  253. !
  254. translator
  255. ^ translator
  256. !
  257. translator: anASTTranslator
  258. translator := anASTTranslator
  259. ! !
  260. !IRSendInliner methodsFor: 'error handling'!
  261. inliningError: aString
  262. InliningError signal: aString
  263. ! !
  264. !IRSendInliner methodsFor: 'factory'!
  265. inlinedClosure
  266. ^ IRInlinedClosure new
  267. !
  268. inlinedSequence
  269. ^ IRInlinedSequence new
  270. ! !
  271. !IRSendInliner methodsFor: 'inlining'!
  272. ifFalse: anIRInstruction
  273. ^ self inlinedSend: IRInlinedIfFalse new withBlock: anIRInstruction
  274. !
  275. ifFalse: anIRInstruction ifTrue: anotherIRInstruction
  276. ^ self perform: #ifTrue:ifFalse: withArguments: { anotherIRInstruction. anIRInstruction }
  277. !
  278. ifNil: anIRInstruction
  279. ^ self
  280. inlinedSend: IRInlinedIfNilIfNotNil new
  281. withBlock: anIRInstruction
  282. withBlock: (IRClosure new
  283. scope: anIRInstruction scope copy;
  284. add: (IRBlockSequence new
  285. add: self send receiver;
  286. yourself);
  287. yourself)
  288. !
  289. ifNil: anIRInstruction ifNotNil: anotherIRInstruction
  290. ^ self inlinedSend: IRInlinedIfNilIfNotNil new withBlock: anIRInstruction withBlock: anotherIRInstruction
  291. !
  292. ifNotNil: anIRInstruction
  293. ^ self
  294. inlinedSend: IRInlinedIfNilIfNotNil new
  295. withBlock: (IRClosure new
  296. scope: anIRInstruction scope copy;
  297. add: (IRBlockSequence new
  298. add: self send receiver;
  299. yourself);
  300. yourself)
  301. withBlock: anIRInstruction
  302. !
  303. ifNotNil: anIRInstruction ifNil: anotherIRInstruction
  304. ^ self inlinedSend: IRInlinedIfNilIfNotNil new withBlock: anotherIRInstruction withBlock: anIRInstruction
  305. !
  306. ifTrue: anIRInstruction
  307. ^ self inlinedSend: IRInlinedIfTrue new withBlock: anIRInstruction
  308. !
  309. ifTrue: anIRInstruction ifFalse: anotherIRInstruction
  310. ^ self inlinedSend: IRInlinedIfTrueIfFalse new withBlock: anIRInstruction withBlock: anotherIRInstruction
  311. !
  312. inlineClosure: anIRClosure
  313. | inlinedClosure sequence statements |
  314. inlinedClosure := self inlinedClosure.
  315. inlinedClosure
  316. scope: anIRClosure scope;
  317. parent: anIRClosure parent.
  318. "Add the possible temp declarations"
  319. anIRClosure tempDeclarations do: [ :each |
  320. inlinedClosure add: each ].
  321. "Add a block sequence"
  322. sequence := self inlinedSequence.
  323. "Map the closure arguments to the receiver of the message send"
  324. anIRClosure arguments do: [ :each |
  325. inlinedClosure add: (IRTempDeclaration new name: each; yourself).
  326. sequence add: (IRAssignment new
  327. add: (IRVariable new variable: (AliasVar new scope: inlinedClosure scope; name: each; yourself));
  328. add: (IRVariable new variable: (AliasVar new scope: inlinedClosure scope; name: '$receiver'; yourself));
  329. yourself) ].
  330. "To ensure the correct order of the closure instructions: first the temps then the sequence"
  331. inlinedClosure add: sequence.
  332. "Get all the statements"
  333. statements := anIRClosure sequence dagChildren.
  334. statements ifNotEmpty: [
  335. statements allButLast do: [ :each | sequence add: each ].
  336. "Inlined closures change local returns into result value itself"
  337. sequence add: statements last asInlinedBlockResult ].
  338. ^ inlinedClosure
  339. !
  340. inlineSend: anIRSend
  341. self send: anIRSend.
  342. ^ self
  343. perform: self send selector
  344. withArguments: self send arguments
  345. !
  346. inlinedSend: inlinedSend withBlock: anIRInstruction
  347. | inlinedClosure |
  348. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  349. anIRInstruction arguments size = 0 ifFalse: [ self inliningError: 'Inlined block should have zero argument' ].
  350. inlinedClosure := self translator visit: (self inlineClosure: anIRInstruction).
  351. inlinedSend
  352. add: self send receiver;
  353. add: inlinedClosure.
  354. self send replaceWith: inlinedSend.
  355. inlinedSend method internalVariables
  356. addAll: inlinedSend internalVariables.
  357. ^ inlinedSend
  358. !
  359. inlinedSend: inlinedSend withBlock: anIRInstruction withBlock: anotherIRInstruction
  360. | inlinedClosure1 inlinedClosure2 |
  361. anIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  362. anotherIRInstruction isClosure ifFalse: [ self inliningError: 'Message argument should be a block' ].
  363. inlinedClosure1 := self translator visit: (self inlineClosure: anIRInstruction).
  364. inlinedClosure2 := self translator visit: (self inlineClosure: anotherIRInstruction).
  365. inlinedSend
  366. add: self send receiver;
  367. add: inlinedClosure1;
  368. add: inlinedClosure2.
  369. self send replaceWith: inlinedSend.
  370. inlinedSend method internalVariables
  371. addAll: inlinedSend internalVariables.
  372. ^ inlinedSend
  373. ! !
  374. !IRSendInliner class methodsFor: 'accessing'!
  375. inlinedSelectors
  376. ^ #('ifTrue:' 'ifFalse:' 'ifTrue:ifFalse:' 'ifFalse:ifTrue:' 'ifNil:' 'ifNotNil:' 'ifNil:ifNotNil:' 'ifNotNil:ifNil:')
  377. !
  378. shouldInline: anIRSend
  379. (self inlinedSelectors includes: anIRSend selector) ifFalse: [ ^ false ].
  380. ^ anIRSend arguments allSatisfy: [ :each | each isClosure ]
  381. ! !
  382. IRSendInliner subclass: #IRAssignmentInliner
  383. instanceVariableNames: 'assignment'
  384. package: 'Compiler-Inlining'!
  385. !IRAssignmentInliner commentStamp!
  386. I inline message sends together with assignments by moving them around into the inline closure instructions.
  387. ##Example
  388. foo
  389. | a |
  390. a := true ifTrue: [ 1 ]
  391. Will produce:
  392. if($core.assert(true) {
  393. a = 1;
  394. };!
  395. !IRAssignmentInliner methodsFor: 'accessing'!
  396. assignment
  397. ^ assignment
  398. !
  399. assignment: aNode
  400. assignment := aNode
  401. ! !
  402. !IRAssignmentInliner methodsFor: 'inlining'!
  403. inlineAssignment: anIRAssignment
  404. | inlinedAssignment |
  405. self assignment: anIRAssignment.
  406. inlinedAssignment := IRInlinedAssignment new.
  407. anIRAssignment dagChildren do: [ :each |
  408. inlinedAssignment add: each ].
  409. anIRAssignment replaceWith: inlinedAssignment.
  410. self inlineSend: inlinedAssignment right.
  411. ^ inlinedAssignment
  412. !
  413. inlineClosure: anIRClosure
  414. | closure statements |
  415. closure := super inlineClosure: anIRClosure.
  416. statements := closure sequence dagChildren.
  417. statements ifNotEmpty: [
  418. statements last yieldsValue ifTrue: [
  419. statements last replaceWith: (IRAssignment new
  420. add: self assignment left;
  421. add: statements last copy;
  422. yourself) ] ].
  423. ^ closure
  424. ! !
  425. IRSendInliner subclass: #IRReturnInliner
  426. instanceVariableNames: ''
  427. package: 'Compiler-Inlining'!
  428. !IRReturnInliner commentStamp!
  429. I inline message sends with inlined closure together with a return instruction.!
  430. !IRReturnInliner methodsFor: 'factory'!
  431. inlinedReturn
  432. ^ IRInlinedReturn new
  433. ! !
  434. !IRReturnInliner methodsFor: 'inlining'!
  435. inlineClosure: anIRClosure
  436. | closure statements |
  437. closure := super inlineClosure: anIRClosure.
  438. statements := closure sequence dagChildren.
  439. statements ifNotEmpty: [
  440. statements last yieldsValue ifTrue: [
  441. statements last replaceWith: (IRReturn new
  442. add: statements last copy;
  443. yourself)] ].
  444. ^ closure
  445. !
  446. inlineReturn: anIRReturn
  447. | return |
  448. return := self inlinedReturn.
  449. anIRReturn dagChildren do: [ :each |
  450. return add: each ].
  451. anIRReturn replaceWith: return.
  452. self inlineSend: return expression.
  453. ^ return
  454. ! !
  455. CodeGenerator subclass: #InliningCodeGenerator
  456. instanceVariableNames: ''
  457. package: 'Compiler-Inlining'!
  458. !InliningCodeGenerator commentStamp!
  459. I am a specialized code generator that uses inlining to produce more optimized JavaScript output!
  460. !InliningCodeGenerator methodsFor: 'compiling'!
  461. compileNode: aNode
  462. | ir stream |
  463. self semanticAnalyzer visit: aNode.
  464. ir := self translator visit: aNode.
  465. self inliner visit: ir.
  466. ^ self irTranslator
  467. currentClass: self currentClass;
  468. visit: ir;
  469. contents
  470. !
  471. inliner
  472. ^ IRInliner new
  473. !
  474. irTranslator
  475. ^ IRInliningJSTranslator new
  476. ! !
  477. SemanticError subclass: #InliningError
  478. instanceVariableNames: ''
  479. package: 'Compiler-Inlining'!
  480. !InliningError commentStamp!
  481. Instances of InliningError are signaled when using an `InliningCodeGenerator`in a `Compiler`.!
  482. !IRBlockReturn methodsFor: '*Compiler-Inlining'!
  483. asInlinedBlockResult
  484. ^ self expression
  485. ! !
  486. !IRInstruction methodsFor: '*Compiler-Inlining'!
  487. asInlinedBlockResult
  488. ^ self
  489. ! !