Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
llvm source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
clang (assertions trunk)
clang (trunk)
clang 10.0.0
clang 10.0.1
clang 11.0.0
clang 11.0.1
clang 12.0.0
clang 12.0.1
clang 13.0.0
clang 14.0.0
clang 15.0.0
clang 16.0.0
clang 17.0.1
clang 18.1.0
clang 19.1.0
clang 4.0.1
clang 5.0.0
clang 6.0.0
clang 7.0.0
clang 8.0.0
clang 9.0.0
hexagon-clang 16.0.5
llc (assertions trunk)
llc (trunk)
llc 10.0.0
llc 10.0.1
llc 11.0.0
llc 11.0.1
llc 12.0.0
llc 12.0.1
llc 13.0.0
llc 14.0.0
llc 15.0.0
llc 16.0.0
llc 17.0.1
llc 18.1.0
llc 19.1.0
llc 3.2
llc 3.3
llc 3.9.1
llc 4.0.0
llc 4.0.1
llc 5.0.0
llc 6.0.0
llc 7.0.0
llc 8.0.0
llc 9.0.0
opt (assertions trunk)
opt (trunk)
opt 10.0.0
opt 10.0.1
opt 11.0.0
opt 11.0.1
opt 12.0.0
opt 12.0.1
opt 13.0.0
opt 14.0.0
opt 15.0.0
opt 16.0.0
opt 17.0.1
opt 18.1.0
opt 19.1.0
opt 3.2
opt 3.3
opt 3.9.1
opt 4.0.0
opt 4.0.1
opt 5.0.0
opt 6.0.0
opt 7.0.0
opt 8.0.0
opt 9.0.0
Options
Source code
define i64 @and_shiftedreg_from_and(i64 %a, i64 %b) { ; CHECK-LABEL: and_shiftedreg_from_and: ; CHECK: // %bb.0: ; CHECK-NEXT: and x8, x1, x0, asr #23 ; CHECK-NEXT: and x0, x8, #0xffffffffff000000 ; CHECK-NEXT: ret %ashr = ashr i64 %a, 23 %and = and i64 %ashr, -16777216 %r = and i64 %b, %and ret i64 %r } ; TODO: logic shift reg pattern: bic define i64 @bic_shiftedreg_from_and(i64 %a, i64 %b) { ; CHECK-LABEL: bic_shiftedreg_from_and: ; CHECK: // %bb.0: ; CHECK-NEXT: mov w8, #16777215 ; CHECK-NEXT: orn x8, x8, x0, asr #23 ; CHECK-NEXT: and x0, x1, x8 ; CHECK-NEXT: ret %ashr = ashr i64 %a, 23 %and = and i64 %ashr, -16777216 %not = xor i64 %and, -1 %r = and i64 %b, %not ret i64 %r } ; logic shift reg pattern: eon define i64 @eon_shiftedreg_from_and(i64 %a, i64 %b) { ; CHECK-LABEL: eon_shiftedreg_from_and: ; CHECK: // %bb.0: ; CHECK-NEXT: lsr x8, x0, #17 ; CHECK-NEXT: eon x0, x1, x8, lsl #53 ; CHECK-NEXT: ret %shl = shl i64 %a, 36 %and = and i64 %shl, -9007199254740992 %xor = xor i64 %and, -1 %r = xor i64 %b, %xor ret i64 %r } ; logic shift reg pattern: eor define i64 @eor_shiftedreg_from_and(i64 %a, i64 %b) { ; CHECK-LABEL: eor_shiftedreg_from_and: ; CHECK: // %bb.0: ; CHECK-NEXT: lsr x8, x0, #47 ; CHECK-NEXT: eor x0, x1, x8, lsl #24 ; CHECK-NEXT: ret %lshr = lshr i64 %a, 23 %and = and i64 %lshr, 2199006478336 %or = xor i64 %and, %b ret i64 %or } ; logic shift reg pattern: mvn ; already optimized by another pattern define i64 @mvn_shiftedreg_from_and(i64 %a) { ; CHECK-LABEL: mvn_shiftedreg_from_and: ; CHECK: // %bb.0: ; CHECK-NEXT: mov x8, #9007199254740991 ; CHECK-NEXT: orn x0, x8, x0, lsl #36 ; CHECK-NEXT: ret %shl = shl i64 %a, 36 %and = and i64 %shl, -9007199254740992 %xor = xor i64 %and, -1 ret i64 %xor } ; logic shift reg pattern: orn ; already optimized by another pattern define i64 @orn_shiftedreg_from_and(i64 %a, i64 %b) { ; CHECK-LABEL: orn_shiftedreg_from_and: ; CHECK: // %bb.0: ; CHECK-NEXT: orn x8, x1, x0, lsr #23 ; CHECK-NEXT: orr x0, x8, #0xfffffe0000ffffff ; CHECK-NEXT: ret %lshr = lshr i64 %a, 23 %and = and i64 %lshr, 2199006478336 %not = xor i64 %and, -1 %or = or i64 %not, %b ret i64 %or } ; logic shift reg pattern: orr define i64 @orr_shiftedreg_from_and(i64 %a, i64 %b) { ; CHECK-LABEL: orr_shiftedreg_from_and: ; CHECK: // %bb.0: ; CHECK-NEXT: lsr x8, x0, #47 ; CHECK-NEXT: orr x0, x1, x8, lsl #24 ; CHECK-NEXT: ret %lshr = lshr i64 %a, 23 %and = and i64 %lshr, 2199006478336 %or = or i64 %and, %b ret i64 %or } ; arithmetic shift reg pattern: add define i32 @add_shiftedreg_from_and(i32 %a, i32 %b) { ; CHECK-LABEL: add_shiftedreg_from_and: ; CHECK: // %bb.0: ; CHECK-NEXT: asr w8, w0, #47 ; CHECK-NEXT: add w0, w1, w8, lsl #24 ; CHECK-NEXT: ret %ashr = ashr i32 %a, 23 %and = and i32 %ashr, -16777216 %add = add i32 %and, %b ret i32 %add } ; arithmetic shift reg pattern: sub ; optimized; sub with shifted operand has higher latency, using M pipeline. ; also verify has one use. define i64 @sub_shiftedreg_from_and_shl(i64 %a, i64 %b) { ; CHECK-LABEL: sub_shiftedreg_from_and_shl: ; CHECK: // %bb.0: ; CHECK-NEXT: lsr x8, x0, #17 ; CHECK-NEXT: sub x0, x1, x8, lsl #53 ; CHECK-NEXT: ret %shl = shl i64 %a, 36 %and = and i64 %shl, -9007199254740992 %sub = sub i64 %b, %and ret i64 %sub } ; negative test: type is not i32 or i64 define i16 @shiftedreg_from_and_negative_type(i16 %a, i16 %b) { ; CHECK-LABEL: shiftedreg_from_and_negative_type: ; CHECK: // %bb.0: ; CHECK-NEXT: lsr w8, w0, #3 ; CHECK-NEXT: sub w0, w1, w8, lsl #5 ; CHECK-NEXT: ret %shl = shl i16 %a, 2 %and = and i16 %shl, -32 %sub = sub i16 %b, %and ret i16 %sub } ; negative test: shift one-use define i32 @shiftedreg_from_and_negative_oneuse1(i32 %a, i32 %b) { ; CHECK-LABEL: shiftedreg_from_and_negative_oneuse1: ; CHECK: // %bb.0: ; CHECK-NEXT: asr w8, w0, #23 ; CHECK-NEXT: and w9, w8, #0xff000000 ; CHECK-NEXT: add w9, w9, w1 ; CHECK-NEXT: mul w0, w8, w9 ; CHECK-NEXT: ret %ashr = ashr i32 %a, 23 %and = and i32 %ashr, -16777216 %add = add i32 %and, %b %r = mul i32 %ashr, %add ret i32 %r } ; negative test: and one-use define i32 @shiftedreg_from_and_negative_oneuse2(i32 %a, i32 %b) { ; CHECK-LABEL: shiftedreg_from_and_negative_oneuse2: ; CHECK: // %bb.0: ; CHECK-NEXT: asr w8, w0, #23 ; CHECK-NEXT: and w8, w8, #0xff000000 ; CHECK-NEXT: add w9, w8, w1 ; CHECK-NEXT: mul w0, w8, w9 ; CHECK-NEXT: ret %ashr = ashr i32 %a, 23 %and = and i32 %ashr, -16777216 %add = add i32 %and, %b %r = mul i32 %and, %add ret i32 %r } ; negative test: and c is not mask define i32 @shiftedreg_from_and_negative_andc1(i32 %a, i32 %b) { ; CHECK-LABEL: shiftedreg_from_and_negative_andc1: ; CHECK: // %bb.0: ; CHECK-NEXT: mov w8, #26215 ; CHECK-NEXT: movk w8, #65510, lsl #16 ; CHECK-NEXT: and w8, w8, w0, asr #23 ; CHECK-NEXT: add w0, w8, w1 ; CHECK-NEXT: ret %ashr = ashr i32 %a, 23 %and = and i32 %ashr, -1677721 %add = add i32 %and, %b ret i32 %add } ; negative test: sra with and c is not legal mask define i32 @shiftedreg_from_and_negative_andc2(i32 %a, i32 %b) { ; CHECK-LABEL: shiftedreg_from_and_negative_andc2: ; CHECK: // %bb.0: ; CHECK-NEXT: mov w8, #-285212672 ; CHECK-NEXT: and w8, w8, w0, asr #23 ; CHECK-NEXT: add w0, w8, w1 ; CHECK-NEXT: ret %ashr = ashr i32 %a, 23 %and = and i32 %ashr, 4009754624 ; 0xef000000 %add = add i32 %and, %b ret i32 %add } ; negative test: shl with and c is not legal mask define i64 @shiftedreg_from_and_negative_andc3(i64 %a, i64 %b) { ; CHECK-LABEL: shiftedreg_from_and_negative_andc3: ; CHECK: // %bb.0: ; CHECK-NEXT: eor x0, x1, x0, lsl #36 ; CHECK-NEXT: ret %shl = shl i64 %a, 36 %and = and i64 %shl, -4294967296 %xor = xor i64 %and, %b ret i64 %xor } ; negative test: shl with and c is not legal mask define i64 @shiftedreg_from_and_negative_andc4(i64 %a, i64 %b) { ; CHECK-LABEL: shiftedreg_from_and_negative_andc4: ; CHECK: // %bb.0: ; CHECK-NEXT: lsl x8, x0, #36 ; CHECK-NEXT: and x8, x8, #0x7fe0000000000000 ; CHECK-NEXT: eor x0, x8, x1 ; CHECK-NEXT: ret %shl = shl i64 %a, 36 %and = and i64 %shl, 9214364837600034816 %xor = xor i64 %and, %b ret i64 %xor }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
About the author
Statistics
Changelog
Version tree