Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ProfDataUtils.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Value.h"
#include "llvm/Support/AlignOf.h"
Expand Down Expand Up @@ -878,13 +879,18 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
return BinaryOperator::CreateAdd(Builder.CreateNot(Y), X);

// zext(bool) + C -> bool ? C + 1 : C
SelectInst *SI = nullptr;
if (match(Op0, m_ZExt(m_Value(X))) &&
X->getType()->getScalarSizeInBits() == 1)
return SelectInst::Create(X, InstCombiner::AddOne(Op1C), Op1);
SI = SelectInst::Create(X, InstCombiner::AddOne(Op1C), Op1);
// sext(bool) + C -> bool ? C - 1 : C
if (match(Op0, m_SExt(m_Value(X))) &&
X->getType()->getScalarSizeInBits() == 1)
return SelectInst::Create(X, InstCombiner::SubOne(Op1C), Op1);
SI = SelectInst::Create(X, InstCombiner::SubOne(Op1C), Op1);
if (SI) {
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
return SI;
}

// ~X + C --> (C-1) - X
if (match(Op0, m_Not(m_Value(X)))) {
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ProfDataUtils.h"
#include "llvm/Transforms/InstCombine/InstCombiner.h"
using namespace llvm;
using namespace PatternMatch;
Expand Down Expand Up @@ -1253,7 +1254,9 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
// shl (zext i1 X), C1 --> select (X, 1 << C1, 0)
if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1)) {
auto *NewC = Builder.CreateShl(ConstantInt::get(Ty, 1), C1);
return SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
auto *SI = SelectInst::Create(X, NewC, ConstantInt::getNullValue(Ty));
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
return SI;
}
}

Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
#include "llvm/IR/Operator.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/ProfDataUtils.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Use.h"
#include "llvm/IR/User.h"
Expand Down Expand Up @@ -1735,7 +1736,9 @@ Instruction *InstCombinerImpl::foldBinopOfSextBoolToSelect(BinaryOperator &BO) {
Constant *Zero = ConstantInt::getNullValue(BO.getType());
Value *TVal = Builder.CreateBinOp(BO.getOpcode(), Ones, C);
Value *FVal = Builder.CreateBinOp(BO.getOpcode(), Zero, C);
return SelectInst::Create(X, TVal, FVal);
SelectInst *SI = SelectInst::Create(X, TVal, FVal);
setExplicitlyUnknownBranchWeights(*SI, DEBUG_TYPE);
return SI;
}

static Value *simplifyOperationIntoSelectOperand(Instruction &I, SelectInst *SI,
Expand Down
39 changes: 39 additions & 0 deletions llvm/test/Transforms/InstCombine/preserve-profile.ll
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,48 @@ define i32 @NegBin(i1 %C) !prof !0 {
ret i32 %V
}

define i32 @select_C_minus_1_or_C_from_bool(i1 %x) {
; CHECK-LABEL: define i32 @select_C_minus_1_or_C_from_bool(
; CHECK-SAME: i1 [[X:%.*]]) {
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[X]], i32 41, i32 42, !prof [[PROF2:![0-9]+]]
; CHECK-NEXT: ret i32 [[ADD]]
;
%ext = sext i1 %x to i32
%add = add i32 %ext, 42
ret i32 %add
}

define i5 @and_add(i1 %x, i1 %y) {
; CHECK-LABEL: define i5 @and_add(
; CHECK-SAME: i1 [[X:%.*]], i1 [[Y:%.*]]) {
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[X]], true
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[Y]], [[TMP1]]
; CHECK-NEXT: [[R:%.*]] = select i1 [[TMP2]], i5 -2, i5 0, !prof [[PROF2]]
; CHECK-NEXT: ret i5 [[R]]
;
%xz = zext i1 %x to i5
%ys = sext i1 %y to i5
%sub = add i5 %xz, %ys
%r = and i5 %sub, 30
ret i5 %r
}

define i32 @add_zext_zext_i1(i1 %a) {
; CHECK-LABEL: define i32 @add_zext_zext_i1(
; CHECK-SAME: i1 [[A:%.*]]) {
; CHECK-NEXT: [[ADD:%.*]] = select i1 [[A]], i32 2, i32 0, !prof [[PROF2]]
; CHECK-NEXT: ret i32 [[ADD]]
;
%zext = zext i1 %a to i32
%add = add i32 %zext, %zext
ret i32 %add
}


!0 = !{!"function_entry_count", i64 1000}
!1 = !{!"branch_weights", i32 2, i32 3}
;.
; CHECK: [[PROF0]] = !{!"function_entry_count", i64 1000}
; CHECK: [[PROF1]] = !{!"branch_weights", i32 2, i32 3}
; CHECK: [[PROF2]] = !{!"unknown", !"instcombine"}
;.
2 changes: 0 additions & 2 deletions llvm/utils/profcheck-xfail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -836,8 +836,6 @@ Transforms/InstCombine/2011-02-14-InfLoop.ll
Transforms/InstCombine/AArch64/sve-intrinsic-sel.ll
Transforms/InstCombine/AArch64/sve-intrinsic-simplify-binop.ll
Transforms/InstCombine/AArch64/sve-intrinsic-simplify-shift.ll
Transforms/InstCombine/add2.ll
Transforms/InstCombine/add.ll
Transforms/InstCombine/add-mask.ll
Transforms/InstCombine/add-shl-mul-umax.ll
Transforms/InstCombine/add-shl-sdiv-to-srem.ll
Expand Down