about summary refs log tree commit diff
path: root/compiler/rustc_codegen_cranelift/src/optimize
diff options
context:
space:
mode:
authorbjorn3 <bjorn3@users.noreply.github.com>2020-12-27 10:30:38 +0100
committerbjorn3 <bjorn3@users.noreply.github.com>2020-12-27 10:30:38 +0100
commit52cf01c81579fa1add25e935d26de8cd3e9ea1d7 (patch)
treecf1a6b47c9f7ea51d544db738c33eea673a828fc /compiler/rustc_codegen_cranelift/src/optimize
parent0f42d47bd5352028fe297f5e759b7e3763d55cbb (diff)
parentdbee13661efa269cb4cd57bb4c6b99a19732b484 (diff)
downloadrust-52cf01c81579fa1add25e935d26de8cd3e9ea1d7.tar.gz
rust-52cf01c81579fa1add25e935d26de8cd3e9ea1d7.zip
Merge commit 'dbee13661efa269cb4cd57bb4c6b99a19732b484' into sync_cg_clif-2020-12-27
Diffstat (limited to 'compiler/rustc_codegen_cranelift/src/optimize')
-rw-r--r--compiler/rustc_codegen_cranelift/src/optimize/peephole.rs39
1 files changed, 38 insertions, 1 deletions
diff --git a/compiler/rustc_codegen_cranelift/src/optimize/peephole.rs b/compiler/rustc_codegen_cranelift/src/optimize/peephole.rs
index f8e0f3af3d0..a575ed8dc35 100644
--- a/compiler/rustc_codegen_cranelift/src/optimize/peephole.rs
+++ b/compiler/rustc_codegen_cranelift/src/optimize/peephole.rs
@@ -73,7 +73,7 @@ pub(crate) fn make_branchable_value(bcx: &mut FunctionBuilder<'_>, arg: Value) -
     })()
     .unwrap_or_else(|| {
         match bcx.func.dfg.value_type(arg) {
-            types::I8 | types::I32 => {
+            types::I8 | types::I16 => {
                 // WORKAROUND for brz.i8 and brnz.i8 not yet being implemented
                 bcx.ins().uextend(types::I32, arg)
             }
@@ -81,3 +81,40 @@ pub(crate) fn make_branchable_value(bcx: &mut FunctionBuilder<'_>, arg: Value) -
         }
     })
 }
+
+/// Returns whether the branch is statically known to be taken or `None` if it isn't statically known.
+pub(crate) fn maybe_known_branch_taken(
+    bcx: &FunctionBuilder<'_>,
+    arg: Value,
+    test_zero: bool,
+) -> Option<bool> {
+    let arg_inst = if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) {
+        arg_inst
+    } else {
+        return None;
+    };
+
+    match bcx.func.dfg[arg_inst] {
+        InstructionData::UnaryBool {
+            opcode: Opcode::Bconst,
+            imm,
+        } => {
+            if test_zero {
+                Some(!imm)
+            } else {
+                Some(imm)
+            }
+        }
+        InstructionData::UnaryImm {
+            opcode: Opcode::Iconst,
+            imm,
+        } => {
+            if test_zero {
+                Some(imm.bits() == 0)
+            } else {
+                Some(imm.bits() != 0)
+            }
+        }
+        _ => None,
+    }
+}