diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.h b/llvm/lib/Target/LoongArch/LoongArchISelLowering.h --- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.h +++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.h @@ -202,6 +202,8 @@ unsigned AS, Instruction *I = nullptr) const override; + bool isLegalAddImmediate(int64_t Imm) const override; + bool hasAndNotCompare(SDValue Y) const override; bool convertSelectOfConstantsToMath(EVT VT) const override { return true; } diff --git a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp --- a/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp +++ b/llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp @@ -3243,6 +3243,10 @@ return true; } +bool LoongArchTargetLowering::isLegalAddImmediate(int64_t Imm) const { + return isInt<12>(Imm); +} + bool LoongArchTargetLowering::hasAndNotCompare(SDValue Y) const { // TODO: Support vectors. if (Y.getValueType().isVector()) diff --git a/llvm/test/CodeGen/LoongArch/lsr-legaladdimm.ll b/llvm/test/CodeGen/LoongArch/lsr-legaladdimm.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/LoongArch/lsr-legaladdimm.ll @@ -0,0 +1,15 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc --mtriple=loongarch64 < %s | FileCheck %s + +define i32 @lsr_addimm(i32 %x, i32 %y) { +; CHECK-LABEL: lsr_addimm: +; CHECK: # %bb.0: +; CHECK-NEXT: addi.d $a0, $a0, -1 +; CHECK-NEXT: bstrpick.d $a1, $a1, 31, 20 +; CHECK-NEXT: and $a0, $a1, $a0 +; CHECK-NEXT: ret + %1 = add i32 %x, 4095 + %2 = lshr i32 %y, 20 + %r = and i32 %2, %1 + ret i32 %r +}