Compiler-Inlining.st 16 KB

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