Index: lib/Transforms/InstCombine/InstCombineCompares.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCompares.cpp +++ lib/Transforms/InstCombine/InstCombineCompares.cpp @@ -4498,8 +4498,14 @@ if (SelectInst *SI = dyn_cast(I.user_back())) { Value *A, *B; SelectPatternResult SPR = matchSelectPattern(SI, A, B); - if (SPR.Flavor != SPF_UNKNOWN) + if (SPR.Flavor != SPF_UNKNOWN) { + // This may be expensive in compile-time, and transforms based on known + // bits can make further analysis more difficult, so we use it as the + // last resort if we cannot do anything better. + if (Instruction *Res = foldICmpUsingKnownBits(I)) + return Res; return nullptr; + } } // Do this after checking for min/max to prevent infinite looping. Index: test/Transforms/InstCombine/max_known_bits.ll =================================================================== --- test/Transforms/InstCombine/max_known_bits.ll +++ test/Transforms/InstCombine/max_known_bits.ll @@ -1,17 +1,12 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py ; RUN: opt -S -instcombine < %s | FileCheck %s -; TODO: The entire thing should be folded to and i16 %0, 255. +; The entire thing should be folded to and i16 %0, 255. define i16 @foo(i16 ) { ; CHECK-LABEL: @foo( ; CHECK-NEXT: entry: ; CHECK-NEXT: [[TMP1:%.*]] = and i16 [[TMP0:%.*]], 255 -; CHECK-NEXT: [[TMP2:%.*]] = zext i16 [[TMP1]] to i32 -; CHECK-NEXT: [[TMP3:%.*]] = icmp ult i32 [[TMP2]], 255 -; CHECK-NEXT: [[TMP4:%.*]] = select i1 [[TMP3]], i32 [[TMP2]], i32 255 -; CHECK-NEXT: [[TMP5:%.*]] = trunc i32 [[TMP4]] to i16 -; CHECK-NEXT: [[TMP6:%.*]] = and i16 [[TMP5]], 255 -; CHECK-NEXT: ret i16 [[TMP6]] +; CHECK-NEXT: ret i16 [[TMP1]] ; entry: %1 = and i16 %0, 255