Index: lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp +++ lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp @@ -227,6 +227,12 @@ // Compute the KnownZero/KnownOne bits to simplify things downstream. computeKnownBits(I, KnownZero, KnownOne, Depth, CxtI); + + // If the client is only demanding bits that we know, return the known + // constant. + if ((DemandedMask & (KnownZero|KnownOne)) == DemandedMask) + return Constant::getIntegerValue(VTy, KnownOne); + return nullptr; } Index: test/Transforms/InstCombine/and2.ll =================================================================== --- test/Transforms/InstCombine/and2.ll +++ test/Transforms/InstCombine/and2.ll @@ -126,8 +126,7 @@ define i32 @test11(i32 %a, i32 %b) { ; CHECK-LABEL: @test11( ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8 -; CHECK-NEXT: [[Y:%.*]] = add i32 [[X]], [[B:%.*]] -; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y]], 128 +; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]] ; CHECK-NEXT: ret i32 [[W]] ; @@ -142,8 +141,7 @@ define i32 @test12(i32 %a, i32 %b) { ; CHECK-LABEL: @test12( ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8 -; CHECK-NEXT: [[Y:%.*]] = add i32 [[X]], [[B:%.*]] -; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y]], 128 +; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]] ; CHECK-NEXT: ret i32 [[W]] ; @@ -158,8 +156,7 @@ define i32 @test13(i32 %a, i32 %b) { ; CHECK-LABEL: @test13( ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8 -; CHECK-NEXT: [[Y:%.*]] = sub i32 [[B:%.*]], [[X]] -; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y]], 128 +; CHECK-NEXT: [[Z:%.*]] = and i32 [[B:%.*]], 128 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]] ; CHECK-NEXT: ret i32 [[W]] ; @@ -174,7 +171,7 @@ define i32 @test14(i32 %a, i32 %b) { ; CHECK-LABEL: @test14( ; CHECK-NEXT: [[X:%.*]] = shl i32 [[A:%.*]], 8 -; CHECK-NEXT: [[Y:%.*]] = sub i32 [[X]], [[B:%.*]] +; CHECK-NEXT: [[Y:%.*]] = sub i32 0, [[B:%.*]] ; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y]], 128 ; CHECK-NEXT: [[W:%.*]] = mul i32 [[Z]], [[X]] ; CHECK-NEXT: ret i32 [[W]]