Transform simple tagged integer comparisons in a conditional context. Return True if the operation is supported (and was transformed). Otherwise, do nothing and return False. Args: self: IR form Builder e: Arbitrary expression true: Branch target if comparison i
(
self: IRBuilder, e: Expression, true: BasicBlock, false: BasicBlock
)
| 54 | |
| 55 | |
| 56 | def maybe_process_conditional_comparison( |
| 57 | self: IRBuilder, e: Expression, true: BasicBlock, false: BasicBlock |
| 58 | ) -> bool: |
| 59 | """Transform simple tagged integer comparisons in a conditional context. |
| 60 | |
| 61 | Return True if the operation is supported (and was transformed). Otherwise, |
| 62 | do nothing and return False. |
| 63 | |
| 64 | Args: |
| 65 | self: IR form Builder |
| 66 | e: Arbitrary expression |
| 67 | true: Branch target if comparison is true |
| 68 | false: Branch target if comparison is false |
| 69 | """ |
| 70 | if not isinstance(e, ComparisonExpr) or len(e.operands) != 2: |
| 71 | return False |
| 72 | ltype = self.node_type(e.operands[0]) |
| 73 | rtype = self.node_type(e.operands[1]) |
| 74 | if not ( |
| 75 | (is_tagged(ltype) or is_fixed_width_rtype(ltype)) |
| 76 | and (is_tagged(rtype) or is_fixed_width_rtype(rtype)) |
| 77 | ): |
| 78 | return False |
| 79 | op = e.operators[0] |
| 80 | if op not in ("==", "!=", "<", "<=", ">", ">="): |
| 81 | return False |
| 82 | left_expr = e.operands[0] |
| 83 | right_expr = e.operands[1] |
| 84 | borrow_left = is_borrow_friendly_expr(self, right_expr) |
| 85 | left = self.accept(left_expr, can_borrow=borrow_left) |
| 86 | right = self.accept(right_expr, can_borrow=True) |
| 87 | if is_fixed_width_rtype(ltype) or is_fixed_width_rtype(rtype): |
| 88 | if not is_fixed_width_rtype(ltype): |
| 89 | left = self.coerce(left, rtype, e.line) |
| 90 | elif not is_fixed_width_rtype(rtype): |
| 91 | right = self.coerce(right, ltype, e.line) |
| 92 | reg = self.binary_op(left, right, op, e.line) |
| 93 | self.builder.flush_keep_alives(e.line) |
| 94 | self.add_bool_branch(reg, true, false) |
| 95 | else: |
| 96 | # "left op right" for two tagged integers |
| 97 | reg = self.builder.binary_op(left, right, op, e.line) |
| 98 | self.flush_keep_alives(e.line) |
| 99 | self.add_bool_branch(reg, true, false) |
| 100 | return True |
| 101 | |
| 102 | |
| 103 | def is_borrow_friendly_expr(self: IRBuilder, expr: Expression) -> bool: |
no test coverage detected
searching dependent graphs…