Index: lib/Transforms/InstCombine/InstCombineSelect.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineSelect.cpp +++ lib/Transforms/InstCombine/InstCombineSelect.cpp @@ -912,6 +912,37 @@ return nullptr; } +/// If one of the operands is a sext/zext from i1 and the other is a constant, +/// we may be able to create an i1 select which can be further folded to +/// logical ops. +static Instruction *foldSelectExtConst(InstCombiner::BuilderTy &Builder, + SelectInst &SI, Instruction *EI, + ConstantInt *C, bool isExtTrueVal, + bool isSigned) { + Value *SmallVal = EI->getOperand(0); + Type *SmallType = SmallVal->getType(); + + if (SmallType->getIntegerBitWidth() != 1) + return nullptr; + + const APInt &CInt = C->getValue(); + + if (CInt != 0 && (isSigned || CInt != 1) && + (!isSigned || !CInt.isAllOnesValue())) + return nullptr; + + Value *SmallConst = ConstantInt::get(SmallType, CInt.trunc(1)); + Value *TrueVal = isExtTrueVal ? SmallVal : SmallConst; + Value *FalseVal = isExtTrueVal ? SmallConst : SmallVal; + Value *Select = Builder.CreateSelect(SI.getOperand(0), TrueVal, FalseVal, + "fold." + SI.getName()); + + if (isSigned) + return new SExtInst(Select, SI.getType()); + + return new ZExtInst(Select, SI.getType()); +} + Instruction *InstCombiner::visitSelectInst(SelectInst &SI) { Value *CondVal = SI.getCondition(); Value *TrueVal = SI.getTrueValue(); @@ -1098,6 +1129,35 @@ if (Instruction *IV = FoldSelectOpOp(SI, TI, FI)) return IV; + // (select C, (sext X), const) -> (sext (select C, X, const')) and + // variations thereof when extending from i1, as that allows further folding + // into logic ops. When the sext is from a larger type, we prefer to have it + // as an operand. + if (TI) { + if (ConstantInt *C = dyn_cast(FalseVal)) { + if (isa(TI)) + if (Instruction *IV = + foldSelectExtConst(*Builder, SI, TI, C, true, true)) + return IV; + if (isa(TI)) + if (Instruction *IV = + foldSelectExtConst(*Builder, SI, TI, C, true, false)) + return IV; + } + } + if (FI) { + if (ConstantInt *C = dyn_cast(TrueVal)) { + if (isa(FI)) + if (Instruction *IV = + foldSelectExtConst(*Builder, SI, FI, C, false, true)) + return IV; + if (isa(FI)) + if (Instruction *IV = + foldSelectExtConst(*Builder, SI, FI, C, false, false)) + return IV; + } + } + // See if we can fold the select into one of our operands. if (SelType->isIntOrIntVectorTy() || SelType->isFPOrFPVectorTy()) { if (Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal)) Index: test/Transforms/InstCombine/select-bitext.ll =================================================================== --- test/Transforms/InstCombine/select-bitext.ll +++ test/Transforms/InstCombine/select-bitext.ll @@ -4,9 +4,9 @@ define i32 @test_sext1(i32 %a, i32 %b) { ; CHECK-LABEL: @test_sext1( ; CHECK-NEXT: [[CCA:%.*]] = icmp sgt i32 %a, 0 -; CHECK-NEXT: [[CCAX:%.*]] = sext i1 [[CCA]] to i32 ; CHECK-NEXT: [[CCB:%.*]] = icmp sgt i32 %b, 0 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CCB]], i32 [[CCAX]], i32 0 +; CHECK-NEXT: [[FOLD_R:%.*]] = and i1 [[CCB]], [[CCA]] +; CHECK-NEXT: [[R:%.*]] = sext i1 [[FOLD_R]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %cca = icmp sgt i32 %a, 0 @@ -19,9 +19,9 @@ define i32 @test_sext2(i32 %a, i32 %b) { ; CHECK-LABEL: @test_sext2( ; CHECK-NEXT: [[CCA:%.*]] = icmp sgt i32 %a, 0 -; CHECK-NEXT: [[CCAX:%.*]] = sext i1 [[CCA]] to i32 ; CHECK-NEXT: [[CCB:%.*]] = icmp sgt i32 %b, 0 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CCB]], i32 -1, i32 [[CCAX]] +; CHECK-NEXT: [[FOLD_R:%.*]] = or i1 [[CCB]], [[CCA]] +; CHECK-NEXT: [[R:%.*]] = sext i1 [[FOLD_R]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %cca = icmp sgt i32 %a, 0 @@ -34,9 +34,9 @@ define i32 @test_sext3(i32 %a, i32 %b) { ; CHECK-LABEL: @test_sext3( ; CHECK-NEXT: [[CCA:%.*]] = icmp sgt i32 %a, 0 -; CHECK-NEXT: [[CCAX:%.*]] = sext i1 [[CCA]] to i32 -; CHECK-NEXT: [[CCB:%.*]] = icmp sgt i32 %b, 0 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CCB]], i32 0, i32 [[CCAX]] +; CHECK-NEXT: [[NOT_CCB:%.*]] = icmp slt i32 %b, 1 +; CHECK-NEXT: [[FOLD_R:%.*]] = and i1 [[CCA]], [[NOT_CCB]] +; CHECK-NEXT: [[R:%.*]] = sext i1 [[FOLD_R]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %cca = icmp sgt i32 %a, 0 @@ -49,9 +49,9 @@ define i32 @test_sext4(i32 %a, i32 %b) { ; CHECK-LABEL: @test_sext4( ; CHECK-NEXT: [[CCA:%.*]] = icmp sgt i32 %a, 0 -; CHECK-NEXT: [[CCAX:%.*]] = sext i1 [[CCA]] to i32 -; CHECK-NEXT: [[CCB:%.*]] = icmp sgt i32 %b, 0 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CCB]], i32 [[CCAX]], i32 -1 +; CHECK-NEXT: [[NOT_CCB:%.*]] = icmp slt i32 %b, 1 +; CHECK-NEXT: [[FOLD_R:%.*]] = or i1 [[CCA]], [[NOT_CCB]] +; CHECK-NEXT: [[R:%.*]] = sext i1 [[FOLD_R]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %cca = icmp sgt i32 %a, 0 @@ -64,9 +64,9 @@ define i32 @test_zext1(i32 %a, i32 %b) { ; CHECK-LABEL: @test_zext1( ; CHECK-NEXT: [[CCA:%.*]] = icmp sgt i32 %a, 0 -; CHECK-NEXT: [[CCAX:%.*]] = zext i1 [[CCA]] to i32 ; CHECK-NEXT: [[CCB:%.*]] = icmp sgt i32 %b, 0 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CCB]], i32 [[CCAX]], i32 0 +; CHECK-NEXT: [[FOLD_R:%.*]] = and i1 [[CCB]], [[CCA]] +; CHECK-NEXT: [[R:%.*]] = zext i1 [[FOLD_R]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %cca = icmp sgt i32 %a, 0 @@ -79,9 +79,9 @@ define i32 @test_zext2(i32 %a, i32 %b) { ; CHECK-LABEL: @test_zext2( ; CHECK-NEXT: [[CCA:%.*]] = icmp sgt i32 %a, 0 -; CHECK-NEXT: [[CCAX:%.*]] = zext i1 [[CCA]] to i32 ; CHECK-NEXT: [[CCB:%.*]] = icmp sgt i32 %b, 0 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CCB]], i32 1, i32 [[CCAX]] +; CHECK-NEXT: [[FOLD_R:%.*]] = or i1 [[CCB]], [[CCA]] +; CHECK-NEXT: [[R:%.*]] = zext i1 [[FOLD_R]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %cca = icmp sgt i32 %a, 0 @@ -94,9 +94,9 @@ define i32 @test_zext3(i32 %a, i32 %b) { ; CHECK-LABEL: @test_zext3( ; CHECK-NEXT: [[CCA:%.*]] = icmp sgt i32 %a, 0 -; CHECK-NEXT: [[CCAX:%.*]] = zext i1 [[CCA]] to i32 -; CHECK-NEXT: [[CCB:%.*]] = icmp sgt i32 %b, 0 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CCB]], i32 0, i32 [[CCAX]] +; CHECK-NEXT: [[NOT_CCB:%.*]] = icmp slt i32 %b, 1 +; CHECK-NEXT: [[FOLD_R:%.*]] = and i1 [[CCA]], [[NOT_CCB]] +; CHECK-NEXT: [[R:%.*]] = zext i1 [[FOLD_R]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %cca = icmp sgt i32 %a, 0 @@ -109,9 +109,9 @@ define i32 @test_zext4(i32 %a, i32 %b) { ; CHECK-LABEL: @test_zext4( ; CHECK-NEXT: [[CCA:%.*]] = icmp sgt i32 %a, 0 -; CHECK-NEXT: [[CCAX:%.*]] = zext i1 [[CCA]] to i32 -; CHECK-NEXT: [[CCB:%.*]] = icmp sgt i32 %b, 0 -; CHECK-NEXT: [[R:%.*]] = select i1 [[CCB]], i32 [[CCAX]], i32 1 +; CHECK-NEXT: [[NOT_CCB:%.*]] = icmp slt i32 %b, 1 +; CHECK-NEXT: [[FOLD_R:%.*]] = or i1 [[CCA]], [[NOT_CCB]] +; CHECK-NEXT: [[R:%.*]] = zext i1 [[FOLD_R]] to i32 ; CHECK-NEXT: ret i32 [[R]] ; %cca = icmp sgt i32 %a, 0