@samitouri / QOS-React / commits / 69cce3d51f

[hir][be] Move collectInputs to HIRBuilder

This seems like a better place to put helper methods.

Sathya Gunasekaran committed Nov 7, 2022 at 18:45 UTC 69cce3d51f067d6bf1b4a539ca130eafd3c52517
3 files changed +75 -74
compiler/forget/src/HIR/EliminateRedundantPhi.ts
+2 -2
@@ -8,7 +8,7 @@
8 import invariant from "invariant";
9 import { assertExhaustive } from "../Common/utils";
10 import { BlockId, HIRFunction, Identifier, Place } from "./HIR";
11 -import { collectInputs } from "./InferMutableLifetimes";
11 +import { eachInstructionOperand } from "./HIRBuilder";
12
13 /**
14 * Pass to eliminate redundant phi nodes:
@@ -80,7 +80,7 @@ export function eliminateRedundantPhi(fn: HIRFunction) {
80
81 // Rewrite all instruction lvalues and operands
82 for (const instr of block.instructions) {
83 - for (const place of collectInputs(instr)) {
83 + for (const place of eachInstructionOperand(instr)) {
84 rewritePlace(place, rewrites);
85 }
86 const { lvalue } = instr;
compiler/forget/src/HIR/HIRBuilder.ts
+71
@@ -619,3 +619,74 @@ export function mapTerminalSuccessors(
619 }
620 }
621 }
622 +
623 +export function* eachInstructionOperand(instr: Instruction) {
624 + const instrValue = instr.value;
625 + switch (instrValue.kind) {
626 + case "NewExpression":
627 + case "CallExpression": {
628 + yield instrValue.callee;
629 + for (const arg of instrValue.args) {
630 + yield arg;
631 + }
632 + break;
633 + }
634 + case "BinaryExpression": {
635 + yield instrValue.left;
636 + yield instrValue.right;
637 + break;
638 + }
639 + case "Identifier": {
640 + yield instrValue;
641 + break;
642 + }
643 + case "UnaryExpression": {
644 + yield instrValue.value;
645 + break;
646 + }
647 + case "JsxExpression": {
648 + yield instrValue.tag;
649 + for (const place of instrValue.props.values()) {
650 + yield place;
651 + }
652 + if (instrValue.children) {
653 + for (const c of instrValue.children) {
654 + yield c;
655 + }
656 + }
657 + break;
658 + }
659 + case "JsxFragment": {
660 + for (const c of instrValue.children) {
661 + yield c;
662 + }
663 + break;
664 + }
665 + case "ObjectExpression": {
666 + if (instrValue.properties !== null) {
667 + const props = instrValue.properties;
668 + for (const place of props.values()) {
669 + yield place;
670 + }
671 + }
672 + break;
673 + }
674 + case "ArrayExpression": {
675 + for (const e of instrValue.elements) {
676 + yield e;
677 + }
678 + break;
679 + }
680 + case "OtherStatement":
681 + case "Primitive":
682 + case "JSXText": {
683 + break;
684 + }
685 + default: {
686 + assertExhaustive(
687 + instrValue,
688 + `Unexpected instruction kind '${(instrValue as any).kind}'`
689 + );
690 + }
691 + }
692 +}
compiler/forget/src/HIR/InferMutableLifetimes.ts
+2 -72
@@ -7,6 +7,7 @@
7
8 import { assertExhaustive } from "../Common/utils";
9 import { Effect, HIRFunction, Instruction, Place } from "./HIR";
10 +import { eachInstructionOperand } from "./HIRBuilder";
11 import { printInstruction, printPlace } from "./PrintHIR";
12
13 /**
@@ -83,7 +84,7 @@ export function inferMutableRanges(func: HIRFunction) {
84 }
85
86 for (const instr of block.instructions) {
86 - for (const input of collectInputs(instr)) {
87 + for (const input of eachInstructionOperand(instr)) {
88 inferPlace(input, instr);
89 }
90
@@ -105,74 +106,3 @@ export function inferMutableRanges(func: HIRFunction) {
106 }
107 }
108 }
108 -
109 -export function* collectInputs(instr: Instruction) {
110 - const instrValue = instr.value;
111 - switch (instrValue.kind) {
112 - case "NewExpression":
113 - case "CallExpression": {
114 - yield instrValue.callee;
115 - for (const arg of instrValue.args) {
116 - yield arg;
117 - }
118 - break;
119 - }
120 - case "BinaryExpression": {
121 - yield instrValue.left;
122 - yield instrValue.right;
123 - break;
124 - }
125 - case "Identifier": {
126 - yield instrValue;
127 - break;
128 - }
129 - case "UnaryExpression": {
130 - yield instrValue.value;
131 - break;
132 - }
133 - case "JsxExpression": {
134 - yield instrValue.tag;
135 - for (const place of instrValue.props.values()) {
136 - yield place;
137 - }
138 - if (instrValue.children) {
139 - for (const c of instrValue.children) {
140 - yield c;
141 - }
142 - }
143 - break;
144 - }
145 - case "JsxFragment": {
146 - for (const c of instrValue.children) {
147 - yield c;
148 - }
149 - break;
150 - }
151 - case "ObjectExpression": {
152 - if (instrValue.properties !== null) {
153 - const props = instrValue.properties;
154 - for (const place of props.values()) {
155 - yield place;
156 - }
157 - }
158 - break;
159 - }
160 - case "ArrayExpression": {
161 - for (const e of instrValue.elements) {
162 - yield e;
163 - }
164 - break;
165 - }
166 - case "OtherStatement":
167 - case "Primitive":
168 - case "JSXText": {
169 - break;
170 - }
171 - default: {
172 - assertExhaustive(
173 - instrValue,
174 - `Unexpected instruction kind '${(instrValue as any).kind}'`
175 - );
176 - }
177 - }
178 -}