diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h --- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h +++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h @@ -22,8 +22,11 @@ #include "llvm/IR/GetElementPtrTypeIterator.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Operator.h" +#include "llvm/IR/PatternMatch.h" #include "llvm/IR/Type.h" +using namespace llvm::PatternMatch; + namespace llvm { /// Base class for use as a mix-in that aids implementing @@ -990,6 +993,18 @@ CostKind, I); } case Instruction::Select: { + const Value *Op0, *Op1; + if (match(U, m_LogicalAnd(m_Value(Op0), m_Value(Op1))) || + match(U, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) { + TTI::OperandValueProperties Op1VP = TTI::OP_None; + TTI::OperandValueProperties Op2VP = TTI::OP_None; + TTI::OperandValueKind Op1VK = TTI::getOperandInfo(Op0, Op1VP); + TTI::OperandValueKind Op2VK = TTI::getOperandInfo(Op1, Op2VP); + SmallVector Operands{Op0, Op1}; + return TargetTTI->getArithmeticInstrCost( + match(U, m_LogicalOr()) ? Instruction::Or : Instruction::And, + Ty, CostKind, Op1VK, Op2VK, Op1VP, Op2VP, Operands, I); + } Type *CondTy = U->getOperand(0)->getType(); return TargetTTI->getCmpSelInstrCost(Opcode, U->getType(), CondTy, CmpInst::BAD_ICMP_PREDICATE, diff --git a/llvm/test/Analysis/CostModel/AArch64/logicalop.ll b/llvm/test/Analysis/CostModel/AArch64/logicalop.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/CostModel/AArch64/logicalop.ll @@ -0,0 +1,28 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt < %s -cost-model -analyze -mtriple=arm64-apple-ios -mcpu=cyclone | FileCheck %s --check-prefix=CHECK-THROUGHPUT +; RUN: opt < %s -cost-model -analyze -cost-kind=code-size -mtriple=aarch64-- | FileCheck %s --check-prefix=CHECK-SIZE +target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:32:64-v128:32:128-a0:0:32-n32-S32" + +define void @op() { + ; Logical and/or - select's cost must be equivalent to that of binop +; CHECK-THROUGHPUT-LABEL: 'op' +; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-THROUGHPUT-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void +; +; CHECK-SIZE-LABEL: 'op' +; CHECK-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; + %sand = select i1 undef, i1 undef, i1 false + %band = and i1 undef, undef + %sor = select i1 undef, i1 true, i1 undef + %bor = or i1 undef, undef + + ret void +} diff --git a/llvm/test/Analysis/CostModel/AMDGPU/logicalop.ll b/llvm/test/Analysis/CostModel/AMDGPU/logicalop.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/CostModel/AMDGPU/logicalop.ll @@ -0,0 +1,19 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt -cost-model -analyze -mtriple=amdgcn-unknown-amdhsa < %s | FileCheck %s + +define amdgpu_kernel void @op() { + ; Logical and/or - select's cost must be equivalent to that of binop +; CHECK-LABEL: 'op' +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 10 for instruction: ret void +; + %sand = select i1 undef, i1 undef, i1 false + %band = and i1 undef, undef + %sor = select i1 undef, i1 true, i1 undef + %bor = or i1 undef, undef + + ret void +} diff --git a/llvm/test/Analysis/CostModel/ARM/logicalop.ll b/llvm/test/Analysis/CostModel/ARM/logicalop.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/CostModel/ARM/logicalop.ll @@ -0,0 +1,75 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt < %s -cost-model -analyze -mtriple=thumbv8.1m.main-none-eabi -mattr=+mve.fp | FileCheck %s --check-prefix=CHECK-MVE-RECIP +; RUN: opt < %s -cost-model -analyze -mtriple=thumbv7-apple-ios6.0.0 -mcpu=swift | FileCheck %s --check-prefix=CHECK-NEON-RECIP +; RUN: opt < %s -cost-model -analyze -mtriple=thumbv8m.base | FileCheck %s --check-prefix=CHECK-THUMB1-RECIP +; RUN: opt < %s -cost-model -analyze -mtriple=thumbv8m.main | FileCheck %s --check-prefix=CHECK-THUMB2-RECIP +; RUN: opt < %s -cost-model -analyze -cost-kind=code-size -mtriple=thumbv8.1m.main-none-eabi -mattr=+mve.fp | FileCheck %s --check-prefix=CHECK-MVE-SIZE +; RUN: opt < %s -cost-model -analyze -cost-kind=code-size -mtriple=thumbv7-apple-ios6.0.0 -mcpu=swift | FileCheck %s --check-prefix=CHECK-NEON-SIZE +; RUN: opt < %s -cost-model -analyze -cost-kind=code-size -mtriple=thumbv8m.base | FileCheck %s --check-prefix=CHECK-THUMB1-SIZE +; RUN: opt < %s -cost-model -analyze -cost-kind=code-size -mtriple=thumbv8m.main | FileCheck %s --check-prefix=CHECK-THUMB2-SIZE + +define void @op() { + ; Logical and/or - select's cost must be equivalent to that of binop +; CHECK-MVE-RECIP-LABEL: 'op' +; CHECK-MVE-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-MVE-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-MVE-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-MVE-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-MVE-RECIP-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void +; +; CHECK-NEON-RECIP-LABEL: 'op' +; CHECK-NEON-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-NEON-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-NEON-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-NEON-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-NEON-RECIP-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void +; +; CHECK-THUMB1-RECIP-LABEL: 'op' +; CHECK-THUMB1-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-THUMB1-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-THUMB1-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-THUMB1-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-THUMB1-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; +; CHECK-THUMB2-RECIP-LABEL: 'op' +; CHECK-THUMB2-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-THUMB2-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-THUMB2-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-THUMB2-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-THUMB2-RECIP-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; +; CHECK-MVE-SIZE-LABEL: 'op' +; CHECK-MVE-SIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-MVE-SIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %band = and i1 undef, undef +; CHECK-MVE-SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-MVE-SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %bor = or i1 undef, undef +; CHECK-MVE-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; +; CHECK-NEON-SIZE-LABEL: 'op' +; CHECK-NEON-SIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-NEON-SIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %band = and i1 undef, undef +; CHECK-NEON-SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-NEON-SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %bor = or i1 undef, undef +; CHECK-NEON-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; +; CHECK-THUMB1-SIZE-LABEL: 'op' +; CHECK-THUMB1-SIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-THUMB1-SIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %band = and i1 undef, undef +; CHECK-THUMB1-SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-THUMB1-SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %bor = or i1 undef, undef +; CHECK-THUMB1-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; +; CHECK-THUMB2-SIZE-LABEL: 'op' +; CHECK-THUMB2-SIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-THUMB2-SIZE-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %band = and i1 undef, undef +; CHECK-THUMB2-SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-THUMB2-SIZE-NEXT: Cost Model: Found an estimated cost of 3 for instruction: %bor = or i1 undef, undef +; CHECK-THUMB2-SIZE-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; + %sand = select i1 undef, i1 undef, i1 false + %band = and i1 undef, undef + %sor = select i1 undef, i1 true, i1 undef + %bor = or i1 undef, undef + + ret void +} diff --git a/llvm/test/Analysis/CostModel/PowerPC/logicalop.ll b/llvm/test/Analysis/CostModel/PowerPC/logicalop.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/CostModel/PowerPC/logicalop.ll @@ -0,0 +1,27 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt < %s -cost-model -analyze -mtriple=powerpc64-unknown-linux-gnu -mcpu=pwr7 -mattr=+vsx | FileCheck %s +; RUN: opt < %s -cost-model -analyze -mtriple=powerpc64-unknown-linux-gnu -mcpu=pwr9 -mattr=+vsx | FileCheck --check-prefix=CHECK-P9 %s + +define void @op() { + ; Logical and/or - select's cost must be equivalent to that of binop +; CHECK-LABEL: 'op' +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void +; +; CHECK-P9-LABEL: 'op' +; CHECK-P9-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-P9-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-P9-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-P9-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-P9-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void +; + %sand = select i1 undef, i1 undef, i1 false + %band = and i1 undef, undef + %sor = select i1 undef, i1 true, i1 undef + %bor = or i1 undef, undef + + ret void +} diff --git a/llvm/test/Analysis/CostModel/RISCV/logicalop.ll b/llvm/test/Analysis/CostModel/RISCV/logicalop.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/CostModel/RISCV/logicalop.ll @@ -0,0 +1,22 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt -mtriple=riscv32 -cost-model -analyze < %s \ +; RUN: | FileCheck %s -check-prefix=RVI +; RUN: opt -mtriple=riscv64 -cost-model -analyze < %s \ +; RUN: | FileCheck %s -check-prefix=RVI + +define void @op() { + ; Logical and/or - select's cost must be equivalent to that of binop +; RVI-LABEL: 'op' +; RVI-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; RVI-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; RVI-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; RVI-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; RVI-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; + %sand = select i1 undef, i1 undef, i1 false + %band = and i1 undef, undef + %sor = select i1 undef, i1 true, i1 undef + %bor = or i1 undef, undef + + ret void +} diff --git a/llvm/test/Analysis/CostModel/SystemZ/logicalop.ll b/llvm/test/Analysis/CostModel/SystemZ/logicalop.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/CostModel/SystemZ/logicalop.ll @@ -0,0 +1,19 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt < %s -cost-model -analyze -mtriple=systemz-unknown -mcpu=z13 | FileCheck %s + +define void @op() { + ; Logical and/or - select's cost must be equivalent to that of binop +; CHECK-LABEL: 'op' +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: ret void +; + %sand = select i1 undef, i1 undef, i1 false + %band = and i1 undef, undef + %sor = select i1 undef, i1 true, i1 undef + %bor = or i1 undef, undef + + ret void +} diff --git a/llvm/test/Analysis/CostModel/X86/logicalop.ll b/llvm/test/Analysis/CostModel/X86/logicalop.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/CostModel/X86/logicalop.ll @@ -0,0 +1,38 @@ +; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py +; RUN: opt < %s -cost-model -analyze -mtriple=x86_64-unknown-linux-gnu -mattr=+sse2 | FileCheck %s -check-prefixes=SSE2 +; RUN: opt < %s -cost-model -analyze -mtriple=x86_64-unknown-linux-gnu -mattr=+sse4.1 | FileCheck %s -check-prefixes=SSE41 +; RUN: opt < %s -cost-model -analyze -mtriple=x86_64-unknown-linux-gnu -mattr=+avx | FileCheck %s -check-prefixes=AVX,AVX1 +; RUN: opt < %s -cost-model -analyze -mtriple=x86_64-unknown-linux-gnu -mattr=+avx2 | FileCheck %s -check-prefixes=AVX,AVX2 +; RUN: opt < %s -cost-model -analyze -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512f,+avx512vl | FileCheck %s -check-prefixes=AVX,AVX512,AVX512F +; RUN: opt < %s -cost-model -analyze -mtriple=x86_64-unknown-linux-gnu -mattr=+avx512bw,+avx512vl | FileCheck %s -check-prefixes=AVX,AVX512,AVX512BW + +define void @op() { + ; Logical and/or - select's cost must be equivalent to that of binop +; SSE2-LABEL: 'op' +; SSE2-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; SSE2-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; SSE2-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; SSE2-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; SSE2-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void +; +; SSE41-LABEL: 'op' +; SSE41-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; SSE41-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; SSE41-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; SSE41-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; SSE41-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void +; +; AVX-LABEL: 'op' +; AVX-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sand = select i1 undef, i1 undef, i1 false +; AVX-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %band = and i1 undef, undef +; AVX-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %sor = select i1 undef, i1 true, i1 undef +; AVX-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %bor = or i1 undef, undef +; AVX-NEXT: Cost Model: Found an estimated cost of 0 for instruction: ret void +; + %sand = select i1 undef, i1 undef, i1 false + %band = and i1 undef, undef + %sor = select i1 undef, i1 true, i1 undef + %bor = or i1 undef, undef + + ret void +}