[llvm] r310870 - [InstCombine][InstSimplify] 'git add' two files that moved in r310869.
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 14 12:01:32 PDT 2017
Author: ctopper
Date: Mon Aug 14 12:01:32 2017
New Revision: 310870
URL: http://llvm.org/viewvc/llvm-project?rev=310870&view=rev
Log:
[InstCombine][InstSimplify] 'git add' two files that moved in r310869.
Added:
llvm/trunk/include/llvm/Analysis/CmpInstAnalysis.h (with props)
llvm/trunk/lib/Analysis/CmpInstAnalysis.cpp (with props)
Added: llvm/trunk/include/llvm/Analysis/CmpInstAnalysis.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Analysis/CmpInstAnalysis.h?rev=310870&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Analysis/CmpInstAnalysis.h (added)
+++ llvm/trunk/include/llvm/Analysis/CmpInstAnalysis.h Mon Aug 14 12:01:32 2017
@@ -0,0 +1,71 @@
+//===-- CmpInstAnalysis.h - Utils to help fold compare insts ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file holds routines to help analyse compare instructions
+// and fold them into constants or other compare instructions
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_ANALYSIS_CMPINSTANALYSIS_H
+#define LLVM_ANALYSIS_CMPINSTANALYSIS_H
+
+#include "llvm/IR/InstrTypes.h"
+
+namespace llvm {
+ class ICmpInst;
+ class Value;
+
+ /// Encode a icmp predicate into a three bit mask. These bits are carefully
+ /// arranged to allow folding of expressions such as:
+ ///
+ /// (A < B) | (A > B) --> (A != B)
+ ///
+ /// Note that this is only valid if the first and second predicates have the
+ /// same sign. It is illegal to do: (A u< B) | (A s> B)
+ ///
+ /// Three bits are used to represent the condition, as follows:
+ /// 0 A > B
+ /// 1 A == B
+ /// 2 A < B
+ ///
+ /// <=> Value Definition
+ /// 000 0 Always false
+ /// 001 1 A > B
+ /// 010 2 A == B
+ /// 011 3 A >= B
+ /// 100 4 A < B
+ /// 101 5 A != B
+ /// 110 6 A <= B
+ /// 111 7 Always true
+ ///
+ unsigned getICmpCode(const ICmpInst *ICI, bool InvertPred = false);
+
+ /// This is the complement of getICmpCode, which turns an opcode and two
+ /// operands into either a constant true or false, or the predicate for a new
+ /// ICmp instruction. The sign is passed in to determine which kind of
+ /// predicate to use in the new icmp instruction.
+ /// Non-NULL return value will be a true or false constant.
+ /// NULL return means a new ICmp is needed. The predicate for which is output
+ /// in NewICmpPred.
+ Value *getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
+ CmpInst::Predicate &NewICmpPred);
+
+ /// Return true if both predicates match sign or if at least one of them is an
+ /// equality comparison (which is signless).
+ bool PredicatesFoldable(CmpInst::Predicate p1, CmpInst::Predicate p2);
+
+ /// Decompose an icmp into the form ((X & Mask) pred 0) if possible. The
+ /// returned predicate is either == or !=. Returns false if decomposition
+ /// fails.
+ bool decomposeBitTestICmp(Value *LHS, Value *RHS, CmpInst::Predicate &Pred,
+ Value *&X, APInt &Mask);
+
+} // end namespace llvm
+
+#endif
Propchange: llvm/trunk/include/llvm/Analysis/CmpInstAnalysis.h
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: llvm/trunk/include/llvm/Analysis/CmpInstAnalysis.h
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Rev URL"
Propchange: llvm/trunk/include/llvm/Analysis/CmpInstAnalysis.h
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: llvm/trunk/lib/Analysis/CmpInstAnalysis.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/CmpInstAnalysis.cpp?rev=310870&view=auto
==============================================================================
--- llvm/trunk/lib/Analysis/CmpInstAnalysis.cpp (added)
+++ llvm/trunk/lib/Analysis/CmpInstAnalysis.cpp Mon Aug 14 12:01:32 2017
@@ -0,0 +1,137 @@
+//===- CmpInstAnalysis.cpp - Utils to help fold compares ---------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file holds routines to help analyse compare instructions
+// and fold them into constants or other compare instructions
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/CmpInstAnalysis.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/PatternMatch.h"
+
+using namespace llvm;
+
+unsigned llvm::getICmpCode(const ICmpInst *ICI, bool InvertPred) {
+ ICmpInst::Predicate Pred = InvertPred ? ICI->getInversePredicate()
+ : ICI->getPredicate();
+ switch (Pred) {
+ // False -> 0
+ case ICmpInst::ICMP_UGT: return 1; // 001
+ case ICmpInst::ICMP_SGT: return 1; // 001
+ case ICmpInst::ICMP_EQ: return 2; // 010
+ case ICmpInst::ICMP_UGE: return 3; // 011
+ case ICmpInst::ICMP_SGE: return 3; // 011
+ case ICmpInst::ICMP_ULT: return 4; // 100
+ case ICmpInst::ICMP_SLT: return 4; // 100
+ case ICmpInst::ICMP_NE: return 5; // 101
+ case ICmpInst::ICMP_ULE: return 6; // 110
+ case ICmpInst::ICMP_SLE: return 6; // 110
+ // True -> 7
+ default:
+ llvm_unreachable("Invalid ICmp predicate!");
+ }
+}
+
+Value *llvm::getICmpValue(bool Sign, unsigned Code, Value *LHS, Value *RHS,
+ CmpInst::Predicate &NewICmpPred) {
+ switch (Code) {
+ default: llvm_unreachable("Illegal ICmp code!");
+ case 0: // False.
+ return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 0);
+ case 1: NewICmpPred = Sign ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT; break;
+ case 2: NewICmpPred = ICmpInst::ICMP_EQ; break;
+ case 3: NewICmpPred = Sign ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE; break;
+ case 4: NewICmpPred = Sign ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT; break;
+ case 5: NewICmpPred = ICmpInst::ICMP_NE; break;
+ case 6: NewICmpPred = Sign ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE; break;
+ case 7: // True.
+ return ConstantInt::get(CmpInst::makeCmpResultType(LHS->getType()), 1);
+ }
+ return nullptr;
+}
+
+bool llvm::PredicatesFoldable(ICmpInst::Predicate p1, ICmpInst::Predicate p2) {
+ return (CmpInst::isSigned(p1) == CmpInst::isSigned(p2)) ||
+ (CmpInst::isSigned(p1) && ICmpInst::isEquality(p2)) ||
+ (CmpInst::isSigned(p2) && ICmpInst::isEquality(p1));
+}
+
+bool llvm::decomposeBitTestICmp(Value *LHS, Value *RHS,
+ CmpInst::Predicate &Pred,
+ Value *&X, APInt &Mask) {
+ const APInt *C;
+ if (!match(RHS, PatternMatch::m_APInt(C)))
+ return false;
+
+ switch (Pred) {
+ default:
+ return false;
+ case ICmpInst::ICMP_SLT:
+ // X < 0 is equivalent to (X & SignMask) != 0.
+ if (!C->isNullValue())
+ return false;
+ Mask = APInt::getSignMask(C->getBitWidth());
+ Pred = ICmpInst::ICMP_NE;
+ break;
+ case ICmpInst::ICMP_SLE:
+ // X <= -1 is equivalent to (X & SignMask) != 0.
+ if (!C->isAllOnesValue())
+ return false;
+ Mask = APInt::getSignMask(C->getBitWidth());
+ Pred = ICmpInst::ICMP_NE;
+ break;
+ case ICmpInst::ICMP_SGT:
+ // X > -1 is equivalent to (X & SignMask) == 0.
+ if (!C->isAllOnesValue())
+ return false;
+ Mask = APInt::getSignMask(C->getBitWidth());
+ Pred = ICmpInst::ICMP_EQ;
+ break;
+ case ICmpInst::ICMP_SGE:
+ // X >= 0 is equivalent to (X & SignMask) == 0.
+ if (!C->isNullValue())
+ return false;
+ Mask = APInt::getSignMask(C->getBitWidth());
+ Pred = ICmpInst::ICMP_EQ;
+ break;
+ case ICmpInst::ICMP_ULT:
+ // X <u 2^n is equivalent to (X & ~(2^n-1)) == 0.
+ if (!C->isPowerOf2())
+ return false;
+ Mask = -*C;
+ Pred = ICmpInst::ICMP_EQ;
+ break;
+ case ICmpInst::ICMP_ULE:
+ // X <=u 2^n-1 is equivalent to (X & ~(2^n-1)) == 0.
+ if (!(*C + 1).isPowerOf2())
+ return false;
+ Mask = ~*C;
+ Pred = ICmpInst::ICMP_EQ;
+ break;
+ case ICmpInst::ICMP_UGT:
+ // X >u 2^n-1 is equivalent to (X & ~(2^n-1)) != 0.
+ if (!(*C + 1).isPowerOf2())
+ return false;
+ Mask = ~*C;
+ Pred = ICmpInst::ICMP_NE;
+ break;
+ case ICmpInst::ICMP_UGE:
+ // X >=u 2^n is equivalent to (X & ~(2^n-1)) != 0.
+ if (!C->isPowerOf2())
+ return false;
+ Mask = -*C;
+ Pred = ICmpInst::ICMP_NE;
+ break;
+ }
+
+ X = LHS;
+ return true;
+}
Propchange: llvm/trunk/lib/Analysis/CmpInstAnalysis.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: llvm/trunk/lib/Analysis/CmpInstAnalysis.cpp
------------------------------------------------------------------------------
svn:keywords = "Author Date Id Rev URL"
Propchange: llvm/trunk/lib/Analysis/CmpInstAnalysis.cpp
------------------------------------------------------------------------------
svn:mime-type = text/plain
More information about the llvm-commits
mailing list