Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp +++ lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -1400,7 +1400,8 @@ case Intrinsic::cttz: { // If all bits below the first known one are known zero, // this value is constant. - IntegerType *IT = dyn_cast(II->getArgOperand(0)->getType()); + auto Op0 = II->getArgOperand(0); + IntegerType *IT = dyn_cast(Op0->getType()); // FIXME: Try to simplify vectors of integers. if (!IT) break; uint32_t BitWidth = IT->getBitWidth(); @@ -1412,13 +1413,15 @@ if ((Mask & KnownZero) == Mask) return replaceInstUsesWith(CI, ConstantInt::get(IT, APInt(BitWidth, TrailingZeros))); - + if (KnownOne != 0 || isKnownNonZero(Op0, DL)) + goto MakeCttzCltzUndef; } break; case Intrinsic::ctlz: { // If all bits above the first known one are known zero, // this value is constant. - IntegerType *IT = dyn_cast(II->getArgOperand(0)->getType()); + auto Op0 = II->getArgOperand(0); + IntegerType *IT = dyn_cast(Op0->getType()); // FIXME: Try to simplify vectors of integers. if (!IT) break; uint32_t BitWidth = IT->getBitWidth(); @@ -1430,10 +1433,21 @@ if ((Mask & KnownZero) == Mask) return replaceInstUsesWith(CI, ConstantInt::get(IT, APInt(BitWidth, LeadingZeros))); - + if (KnownOne != 0 || isKnownNonZero(Op0, DL)) + goto MakeCttzCltzUndef; } break; - + MakeCttzCltzUndef: { + bool IsZeroUndef = false; + auto Op1 = II->getArgOperand(1); + if (auto Op1C = dyn_cast(Op1)) + IsZeroUndef = Op1C->getZExtValue() != 0; + if (!IsZeroUndef) { + Worklist.Add(II); + II->setOperand(1, ConstantInt::getAllOnesValue(Op1->getType())); + } + break; + } case Intrinsic::uadd_with_overflow: case Intrinsic::sadd_with_overflow: case Intrinsic::umul_with_overflow: Index: test/Transforms/InstCombine/intrinsics.ll =================================================================== --- test/Transforms/InstCombine/intrinsics.ll +++ test/Transforms/InstCombine/intrinsics.ll @@ -379,6 +379,18 @@ ; CHECK-NEXT: ret i32 undef } +define i32 @ctlz_make_undef(i32 %a) { +entry: + %or = or i32 %a, 8 + %ctlz = tail call i32 @llvm.ctlz.i32(i32 %or, i1 false) + ret i32 %ctlz +; CHECK-LABEL: @ctlz_make_undef( +; CHECK-NEXT: entry: +; CHECK-NEXT: %or = or i32 %a, 8 +; CHECK-NEXT: %ctlz = tail call i32 @llvm.ctlz.i32(i32 %or, i1 true) +; CHECK-NEXT: ret i32 %ctlz +} + define i32 @cttz_undef(i32 %Value) nounwind { %cttz = call i32 @llvm.cttz.i32(i32 0, i1 true) ret i32 %cttz @@ -387,6 +399,18 @@ ; CHECK-NEXT: ret i32 undef } +define i32 @cttz_make_undef(i32 %a) { +entry: + %or = or i32 %a, 8 + %cttz = tail call i32 @llvm.cttz.i32(i32 %or, i1 false) + ret i32 %cttz +; CHECK-LABEL: @cttz_make_undef( +; CHECK-NEXT: entry: +; CHECK-NEXT: %or = or i32 %a, 8 +; CHECK-NEXT: %cttz = tail call i32 @llvm.cttz.i32(i32 %or, i1 true) +; CHECK-NEXT: ret i32 %cttz +} + define i32 @ctlz_select(i32 %Value) nounwind { %tobool = icmp ne i32 %Value, 0 %ctlz = call i32 @llvm.ctlz.i32(i32 %Value, i1 true)