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 --- a/llvm/test/CodeGen/LoongArch/lsr-legaladdimm.ll +++ b/llvm/test/CodeGen/LoongArch/lsr-legaladdimm.ll @@ -4,8 +4,7 @@ define i32 @lsr_addimm(i32 %x, i32 %y) { ; CHECK-LABEL: lsr_addimm: ; CHECK: # %bb.0: -; CHECK-NEXT: ori $a2, $zero, 4095 -; CHECK-NEXT: add.d $a0, $a0, $a2 +; CHECK-NEXT: addi.d $a0, $a0, -1 ; CHECK-NEXT: bstrpick.d $a1, $a1, 31, 20 ; CHECK-NEXT: and $a0, $a1, $a0 ; CHECK-NEXT: ret