aboutsummaryrefslogtreecommitdiff
path: root/src/ir.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-03-26 14:41:17 -0400
committerAndrew Kelley <superjoe30@gmail.com>2017-03-26 14:41:17 -0400
commit310335580536777055d9bc2cb8b91b40203ae292 (patch)
treec2fe3770c224c364310d20a4b9a9f1a5b97ec33c /src/ir.cpp
parent5bc9feb5cb98fc13db62d01b2b9fec15677310a7 (diff)
downloadzig-310335580536777055d9bc2cb8b91b40203ae292.tar.gz
zig-310335580536777055d9bc2cb8b91b40203ae292.zip
add comptime eval for some uint comparisons with 0
closes #55
Diffstat (limited to 'src/ir.cpp')
-rw-r--r--src/ir.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index 9619ffd8ba..94d3fed315 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -7349,6 +7349,39 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
return ira->codegen->builtin_types.entry_bool;
}
+ // some comparisons with unsigned numbers can be evaluated
+ if (resolved_type->id == TypeTableEntryIdInt && !resolved_type->data.integral.is_signed) {
+ ConstExprValue *known_left_val;
+ IrBinOp flipped_op_id;
+ if (value_is_comptime(op1_val)) {
+ known_left_val = op1_val;
+ flipped_op_id = op_id;
+ } else if (value_is_comptime(op2_val)) {
+ known_left_val = op2_val;
+ if (op_id == IrBinOpCmpLessThan) {
+ flipped_op_id = IrBinOpCmpGreaterThan;
+ } else if (op_id == IrBinOpCmpGreaterThan) {
+ flipped_op_id = IrBinOpCmpLessThan;
+ } else if (op_id == IrBinOpCmpLessOrEq) {
+ flipped_op_id = IrBinOpCmpGreaterOrEq;
+ } else if (op_id == IrBinOpCmpGreaterOrEq) {
+ flipped_op_id = IrBinOpCmpLessOrEq;
+ } else {
+ flipped_op_id = op_id;
+ }
+ } else {
+ known_left_val = nullptr;
+ }
+ if (known_left_val != nullptr && known_left_val->data.x_bignum.data.x_uint == 0 &&
+ (flipped_op_id == IrBinOpCmpLessOrEq || flipped_op_id == IrBinOpCmpGreaterThan))
+ {
+ bool answer = (flipped_op_id == IrBinOpCmpLessOrEq);
+ ConstExprValue *out_val = ir_build_const_from(ira, &bin_op_instruction->base);
+ out_val->data.x_bool = answer;
+ return ira->codegen->builtin_types.entry_bool;
+ }
+ }
+
ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, op_id,
casted_op1, casted_op2, bin_op_instruction->safety_check_on);