From a801172e504b45b2266486ec68adb64f7fcf8e17 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 11 May 2005 20:02:14 +0000 Subject: [PATCH] Fix lowering of cttz to work with signed values git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21874 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/IntrinsicLowering.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/CodeGen/IntrinsicLowering.cpp b/lib/CodeGen/IntrinsicLowering.cpp index 1817ade1cdb..634f959f3c4 100644 --- a/lib/CodeGen/IntrinsicLowering.cpp +++ b/lib/CodeGen/IntrinsicLowering.cpp @@ -251,13 +251,12 @@ void DefaultIntrinsicLowering::LowerIntrinsicCall(CallInst *CI) { break; } case Intrinsic::cttz: { + // cttz(x) -> ctpop(~X & (X-1)) Value *Src = CI->getOperand(1); Value *NotSrc = BinaryOperator::createNot(Src, Src->getName()+".not", CI); - Src = BinaryOperator::createAnd(NotSrc, - BinaryOperator::createSub(Src, - ConstantUInt::get(CI->getOperand(0)->getType(), 1), "", CI)); - - Src = LowerCTPOP(Src, CI); + Value *SrcM1 = ConstantInt::get(Src->getType(), 1); + SrcM1 = BinaryOperator::createSub(Src, SrcM1, "", CI); + Src = LowerCTPOP(BinaryOperator::createAnd(NotSrc, SrcM1, "", CI), CI); CI->replaceAllUsesWith(Src); break; }