[llvm] r224669 - InstSimplify: Optimize away pointless comparisons
David Majnemer
david.majnemer at gmail.com
Fri Dec 19 19:04:38 PST 2014
Author: majnemer
Date: Fri Dec 19 21:04:38 2014
New Revision: 224669
URL: http://llvm.org/viewvc/llvm-project?rev=224669&view=rev
Log:
InstSimplify: Optimize away pointless comparisons
(X & INT_MIN) ? X & INT_MAX : X into X & INT_MAX
(X & INT_MIN) ? X : X & INT_MAX into X
(X & INT_MIN) ? X | INT_MIN : X into X
(X & INT_MIN) ? X : X | INT_MIN into X | INT_MIN
Modified:
llvm/trunk/include/llvm/IR/PatternMatch.h
llvm/trunk/lib/Analysis/InstructionSimplify.cpp
llvm/trunk/test/Transforms/InstSimplify/select.ll
Modified: llvm/trunk/include/llvm/IR/PatternMatch.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/PatternMatch.h?rev=224669&r1=224668&r2=224669&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/PatternMatch.h (original)
+++ llvm/trunk/include/llvm/IR/PatternMatch.h Fri Dec 19 21:04:38 2014
@@ -272,6 +272,13 @@ struct is_power2 {
inline cst_pred_ty<is_power2> m_Power2() { return cst_pred_ty<is_power2>(); }
inline api_pred_ty<is_power2> m_Power2(const APInt *&V) { return V; }
+struct is_maxsignedvalue {
+ bool isValue(const APInt &C) { return C.isMaxSignedValue(); }
+};
+
+inline cst_pred_ty<is_maxsignedvalue> m_MaxSignedValue() { return cst_pred_ty<is_maxsignedvalue>(); }
+inline api_pred_ty<is_maxsignedvalue> m_MaxSignedValue(const APInt *&V) { return V; }
+
template <typename Class> struct bind_ty {
Class *&VR;
bind_ty(Class *&V) : VR(V) {}
Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=224669&r1=224668&r2=224669&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Fri Dec 19 21:04:38 2014
@@ -3155,10 +3155,10 @@ static Value *SimplifySelectInst(Value *
if (const auto *ICI = dyn_cast<ICmpInst>(CondVal)) {
Value *X;
const APInt *Y;
- if (ICI->isEquality() &&
+ ICmpInst::Predicate Pred = ICI->getPredicate();
+ if (ICmpInst::isEquality(Pred) &&
match(ICI->getOperand(0), m_And(m_Value(X), m_APInt(Y))) &&
match(ICI->getOperand(1), m_Zero())) {
- ICmpInst::Predicate Pred = ICI->getPredicate();
const APInt *C;
// (X & Y) == 0 ? X & ~Y : X --> X
// (X & Y) != 0 ? X & ~Y : X --> X & ~Y
@@ -3184,6 +3184,42 @@ static Value *SimplifySelectInst(Value *
return Pred == ICmpInst::ICMP_EQ ? TrueVal : FalseVal;
}
}
+ if (Pred == ICmpInst::ICMP_SLT && match(ICI->getOperand(1), m_Zero())) {
+ // (X < 0) ? X : X | SignBit --> X | SignBit
+ if (TrueVal == ICI->getOperand(0) &&
+ match(FalseVal, m_Or(m_Specific(TrueVal), m_SignBit())))
+ return FalseVal;
+ // (X < 0) ? X | SignBit : X --> X
+ if (FalseVal == ICI->getOperand(0) &&
+ match(TrueVal, m_Or(m_Specific(FalseVal), m_SignBit())))
+ return FalseVal;
+ // (X < 0) ? X & INT_MAX : X --> X & INT_MAX
+ if (FalseVal == ICI->getOperand(0) &&
+ match(TrueVal, m_And(m_Specific(FalseVal), m_MaxSignedValue())))
+ return TrueVal;
+ // (X < 0) ? X : X & INT_MAX --> X
+ if (TrueVal == ICI->getOperand(0) &&
+ match(FalseVal, m_And(m_Specific(TrueVal), m_MaxSignedValue())))
+ return TrueVal;
+ }
+ if (Pred == ICmpInst::ICMP_SGT && match(ICI->getOperand(1), m_AllOnes())) {
+ // (X > -1) ? X : X | SignBit --> X
+ if (TrueVal == ICI->getOperand(0) &&
+ match(FalseVal, m_Or(m_Specific(TrueVal), m_SignBit())))
+ return TrueVal;
+ // (X > -1) ? X | SignBit : X --> X | SignBit
+ if (FalseVal == ICI->getOperand(0) &&
+ match(TrueVal, m_Or(m_Specific(FalseVal), m_SignBit())))
+ return TrueVal;
+ // (X > -1) ? X & INT_MAX : X --> X
+ if (FalseVal == ICI->getOperand(0) &&
+ match(TrueVal, m_And(m_Specific(FalseVal), m_MaxSignedValue())))
+ return FalseVal;
+ // (X > -1) ? X : X & INT_MAX --> X & INT_MAX
+ if (TrueVal == ICI->getOperand(0) &&
+ match(FalseVal, m_And(m_Specific(TrueVal), m_MaxSignedValue())))
+ return FalseVal;
+ }
}
return nullptr;
Modified: llvm/trunk/test/Transforms/InstSimplify/select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/select.ll?rev=224669&r1=224668&r2=224669&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/select.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/select.ll Fri Dec 19 21:04:38 2014
@@ -31,6 +31,82 @@ define i32 @test3(i32 %x) {
; CHECK: ret i32 %[[and]]
}
+define i32 @test4(i32 %X) {
+ %cmp = icmp slt i32 %X, 0
+ %or = or i32 %X, -2147483648
+ %cond = select i1 %cmp, i32 %X, i32 %or
+ ret i32 %cond
+; CHECK-LABEL: @test4
+; CHECK: %[[or:.*]] = or i32 %X, -2147483648
+; CHECK: ret i32 %[[or]]
+}
+
+define i32 @test5(i32 %X) {
+ %cmp = icmp slt i32 %X, 0
+ %or = or i32 %X, -2147483648
+ %cond = select i1 %cmp, i32 %or, i32 %X
+ ret i32 %cond
+; CHECK-LABEL: @test5
+; CHECK: ret i32 %X
+}
+
+define i32 @test6(i32 %X) {
+ %cmp = icmp slt i32 %X, 0
+ %and = and i32 %X, 2147483647
+ %cond = select i1 %cmp, i32 %and, i32 %X
+ ret i32 %cond
+; CHECK-LABEL: @test6
+; CHECK: %[[and:.*]] = and i32 %X, 2147483647
+; CHECK: ret i32 %[[and]]
+}
+
+define i32 @test7(i32 %X) {
+ %cmp = icmp slt i32 %X, 0
+ %and = and i32 %X, 2147483647
+ %cond = select i1 %cmp, i32 %X, i32 %and
+ ret i32 %cond
+; CHECK-LABEL: @test7
+; CHECK: ret i32 %X
+}
+
+define i32 @test8(i32 %X) {
+ %cmp = icmp sgt i32 %X, -1
+ %or = or i32 %X, -2147483648
+ %cond = select i1 %cmp, i32 %X, i32 %or
+ ret i32 %cond
+; CHECK-LABEL: @test8
+; CHECK: ret i32 %X
+}
+
+define i32 @test9(i32 %X) {
+ %cmp = icmp sgt i32 %X, -1
+ %or = or i32 %X, -2147483648
+ %cond = select i1 %cmp, i32 %or, i32 %X
+ ret i32 %cond
+; CHECK-LABEL: @test9
+; CHECK: %[[or:.*]] = or i32 %X, -2147483648
+; CHECK: ret i32 %[[or]]
+}
+
+define i32 @test10(i32 %X) {
+ %cmp = icmp sgt i32 %X, -1
+ %and = and i32 %X, 2147483647
+ %cond = select i1 %cmp, i32 %and, i32 %X
+ ret i32 %cond
+; CHECK-LABEL: @test10
+; CHECK: ret i32 %X
+}
+
+define i32 @test11(i32 %X) {
+ %cmp = icmp sgt i32 %X, -1
+ %and = and i32 %X, 2147483647
+ %cond = select i1 %cmp, i32 %X, i32 %and
+ ret i32 %cond
+; CHECK-LABEL: @test11
+; CHECK: %[[and:.*]] = and i32 %X, 2147483647
+; CHECK: ret i32 %[[and]]
+}
+
; CHECK-LABEL: @select_icmp_and_8_eq_0_or_8(
; CHECK-NEXT: [[OR:%[a-z0-9]+]] = or i32 %x, 8
; CHECK-NEXT: ret i32 [[OR]]
More information about the llvm-commits
mailing list