Compiler-Inlining.st 15 KB

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