Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions llvm/include/llvm/IR/IRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -1722,16 +1722,19 @@ class IRBuilderBase {
return Insert(BinOp, Name);
}

Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") {
Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "",
Instruction *MDFrom = nullptr) {
assert(Cond2->getType()->isIntOrIntVectorTy(1));
return CreateSelect(Cond1, Cond2,
ConstantInt::getNullValue(Cond2->getType()), Name);
ConstantInt::getNullValue(Cond2->getType()), Name,
MDFrom);
}

Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") {
Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "",
Instruction *MDFrom = nullptr) {
assert(Cond2->getType()->isIntOrIntVectorTy(1));
return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()),
Cond2, Name);
Cond2, Name, MDFrom);
}

Value *CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2,
Expand Down
10 changes: 8 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3356,7 +3356,10 @@ Instruction *InstCombinerImpl::foldSelectOfBools(SelectInst &SI) {
impliesPoisonOrCond(FalseVal, B, /*Expected=*/false)) {
// (A || B) || C --> A || (B | C)
return replaceInstUsesWith(
SI, Builder.CreateLogicalOr(A, Builder.CreateOr(B, FalseVal)));
SI, Builder.CreateLogicalOr(A, Builder.CreateOr(B, FalseVal), "",
ProfcheckDisableMetadataFixes
? nullptr
: cast<SelectInst>(CondVal)));
}

// (A && B) || (C && B) --> (A || C) && B
Expand Down Expand Up @@ -3398,7 +3401,10 @@ Instruction *InstCombinerImpl::foldSelectOfBools(SelectInst &SI) {
impliesPoisonOrCond(TrueVal, B, /*Expected=*/true)) {
// (A && B) && C --> A && (B & C)
return replaceInstUsesWith(
SI, Builder.CreateLogicalAnd(A, Builder.CreateAnd(B, TrueVal)));
SI, Builder.CreateLogicalAnd(A, Builder.CreateAnd(B, TrueVal), "",
ProfcheckDisableMetadataFixes
? nullptr
: cast<SelectInst>(CondVal)));
}

// (A || B) && (C || B) --> (A && C) || B
Expand Down
27 changes: 18 additions & 9 deletions llvm/test/Transforms/InstCombine/select-safe-bool-transforms.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals
; RUN: opt < %s -passes=instcombine -S | FileCheck %s

; TODO: All of these should be optimized to less than or equal to a single
Expand All @@ -7,13 +7,13 @@
; --- (A op B) op' A / (B op A) op' A ---

; (A land B) land A
define i1 @land_land_left1(i1 %A, i1 %B) {
define i1 @land_land_left1(i1 %A, i1 %B) !prof !0 {
; CHECK-LABEL: @land_land_left1(
; CHECK-NEXT: [[C:%.*]] = select i1 [[A:%.*]], i1 [[B:%.*]], i1 false
; CHECK-NEXT: [[C:%.*]] = select i1 [[A:%.*]], i1 [[B:%.*]], i1 false, !prof [[PROF1:![0-9]+]]
; CHECK-NEXT: ret i1 [[C]]
;
%c = select i1 %A, i1 %B, i1 false
%res = select i1 %c, i1 %A, i1 false
%c = select i1 %A, i1 %B, i1 false, !prof !1
%res = select i1 %c, i1 %A, i1 false, !prof !2
ret i1 %res
}
define i1 @land_land_left2(i1 %A, i1 %B) {
Expand Down Expand Up @@ -157,13 +157,13 @@ define i1 @lor_band_left2(i1 %A, i1 %B) {
}

; (A lor B) lor A
define i1 @lor_lor_left1(i1 %A, i1 %B) {
define i1 @lor_lor_left1(i1 %A, i1 %B) !prof !0 {
; CHECK-LABEL: @lor_lor_left1(
; CHECK-NEXT: [[C:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[B:%.*]]
; CHECK-NEXT: [[C:%.*]] = select i1 [[A:%.*]], i1 true, i1 [[B:%.*]], !prof [[PROF1]]
; CHECK-NEXT: ret i1 [[C]]
;
%c = select i1 %A, i1 true, i1 %B
%res = select i1 %c, i1 true, i1 %A
%c = select i1 %A, i1 true, i1 %B, !prof !1
%res = select i1 %c, i1 true, i1 %A, !prof !2
ret i1 %res
}
define i1 @lor_lor_left2(i1 %A, i1 %B) {
Expand Down Expand Up @@ -506,3 +506,12 @@ define <2 x i1> @PR50500_falseval(<2 x i1> %a, <2 x i1> %b) {
%r = select <2 x i1> %a, <2 x i1> %b, <2 x i1> %s
ret <2 x i1> %r
}

!0 = !{!"function_entry_count", i64 1000}
!1 = !{!"branch_weights", i32 2, i32 3}
!2 = !{!"branch_weights", i32 5, i32 7}

;.
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i64 1000}
; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3}
;.