diff --git a/llvm/lib/Target/CSKY/CSKYISelLowering.cpp b/llvm/lib/Target/CSKY/CSKYISelLowering.cpp --- a/llvm/lib/Target/CSKY/CSKYISelLowering.cpp +++ b/llvm/lib/Target/CSKY/CSKYISelLowering.cpp @@ -59,7 +59,6 @@ setOperationAction(ISD::UREM, MVT::i32, Expand); setOperationAction(ISD::UDIVREM, MVT::i32, Expand); setOperationAction(ISD::SDIVREM, MVT::i32, Expand); - setOperationAction(ISD::CTTZ, MVT::i32, Expand); setOperationAction(ISD::CTPOP, MVT::i32, Expand); setOperationAction(ISD::ROTR, MVT::i32, Expand); setOperationAction(ISD::SHL_PARTS, MVT::i32, Expand); @@ -103,6 +102,7 @@ if (!Subtarget.has2E3()) { setOperationAction(ISD::ABS, MVT::i32, Expand); setOperationAction(ISD::BITREVERSE, MVT::i32, Expand); + setOperationAction(ISD::CTTZ, MVT::i32, Expand); setOperationAction(ISD::SDIV, MVT::i32, Expand); setOperationAction(ISD::UDIV, MVT::i32, Expand); } diff --git a/llvm/lib/Target/CSKY/CSKYInstrInfo.td b/llvm/lib/Target/CSKY/CSKYInstrInfo.td --- a/llvm/lib/Target/CSKY/CSKYInstrInfo.td +++ b/llvm/lib/Target/CSKY/CSKYInstrInfo.td @@ -1429,6 +1429,7 @@ let Predicates = [iHas2E3] in { def : Pat<(bitreverse GPR:$rx), (BREV32 GPR:$rx)>; def : Pat<(bswap GPR:$rx), (REVB32 GPR:$rx)>; + def : Pat<(i32 (cttz GPR:$rx)), (FF1 (BREV32 GPR:$rx))>; } def : Pat<(i32 (ctlz GPR:$rx)), (FF1 GPR:$rx)>; } diff --git a/llvm/test/CodeGen/CSKY/intrinsic.ll b/llvm/test/CodeGen/CSKY/intrinsic.ll --- a/llvm/test/CodeGen/CSKY/intrinsic.ll +++ b/llvm/test/CodeGen/CSKY/intrinsic.ll @@ -11,6 +11,33 @@ ret i32 %nlz } +define i32 @cttz_0(i32 %x) { +; CHECK-LABEL: cttz_0: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: bez32 a0, .LBB1_2 +; CHECK-NEXT: # %bb.1: # %cond.false +; CHECK-NEXT: brev32 a0, a0 +; CHECK-NEXT: ff1.32 a0, a0 +; CHECK-NEXT: rts16 +; CHECK-NEXT: .LBB1_2: +; CHECK-NEXT: movi16 a0, 32 +; CHECK-NEXT: rts16 +entry: + %ntz = call i32 @llvm.cttz.i32(i32 %x, i1 0) + ret i32 %ntz +} + +define i32 @cttz_1(i32 %x) { +; CHECK-LABEL: cttz_1: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: brev32 a0, a0 +; CHECK-NEXT: ff1.32 a0, a0 +; CHECK-NEXT: rts16 +entry: + %ntz = call i32 @llvm.cttz.i32(i32 %x, i1 1) + ret i32 %ntz +} + define i32 @bswap(i32 %x) { ; CHECK-LABEL: bswap: ; CHECK: # %bb.0: # %entry @@ -33,4 +60,5 @@ declare i32 @llvm.bswap.i32(i32) declare i32 @llvm.ctlz.i32 (i32, i1) +declare i32 @llvm.cttz.i32 (i32, i1) declare i32 @llvm.bitreverse.i32(i32)