LLVM  5.0.0
Macros | Enumerations | Functions
InstCombineAndOrXor.cpp File Reference
#include "InstCombineInternal.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/IR/ConstantRange.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Transforms/Utils/CmpInstAnalysis.h"
#include "llvm/Transforms/Utils/Local.h"
Include dependency graph for InstCombineAndOrXor.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "instcombine"
 

Enumerations

enum  MaskedICmpType {
  AMask_AllOnes = 1, AMask_NotAllOnes = 2, BMask_AllOnes = 4, BMask_NotAllOnes = 8,
  Mask_AllZeros = 16, Mask_NotAllZeros = 32, AMask_Mixed = 64, AMask_NotMixed = 128,
  BMask_Mixed = 256, BMask_NotMixed = 512
}
 Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns that can be simplified. More...
 

Functions

static unsigned getFCmpCode (FCmpInst::Predicate CC)
 Similar to getICmpCode but for FCmpInst. More...
 
static Value * getNewICmpValue (bool Sign, unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy &Builder)
 This is the complement of getICmpCode, which turns an opcode and two operands into either a constant true or false, or a brand new ICmp instruction. More...
 
static Value * getFCmpValue (unsigned Code, Value *LHS, Value *RHS, InstCombiner::BuilderTy &Builder)
 This is the complement of getFCmpCode, which turns an opcode and two operands into either a FCmp instruction, or a true/false constant. More...
 
static Value * SimplifyBSwap (BinaryOperator &I, InstCombiner::BuilderTy &Builder)
 Transform BITWISE_OP(BSWAP(A),BSWAP(B)) or BITWISE_OP(BSWAP(A), Constant) to BSWAP(BITWISE_OP(A, B)) More...
 
static unsigned getMaskedICmpType (Value *A, Value *B, Value *C, ICmpInst::Predicate Pred)
 Return the set of patterns (from MaskedICmpType) that (icmp SCC (A & B), C) satisfies. More...
 
static unsigned conjugateICmpMask (unsigned Mask)
 Convert an analysis of a masked ICmp into its equivalent if all boolean operations had the opposite sense. More...
 
static unsigned getMaskedTypeForICmpPair (Value *&A, Value *&B, Value *&C, Value *&D, Value *&E, ICmpInst *LHS, ICmpInst *RHS, ICmpInst::Predicate &PredL, ICmpInst::Predicate &PredR)
 Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E). More...
 
static Value * foldLogOpOfMaskedICmps (ICmpInst *LHS, ICmpInst *RHS, bool IsAnd, llvm::InstCombiner::BuilderTy &Builder)
 Try to fold (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E) into a single (icmp(A & X) ==/!= Y). More...
 
static Value * foldAndOrOfEqualityCmpsWithConstants (ICmpInst *LHS, ICmpInst *RHS, bool JoinedByAnd, InstCombiner::BuilderTy &Builder)
 
static Instruction * matchDeMorgansLaws (BinaryOperator &I, InstCombiner::BuilderTy &Builder)
 Match De Morgan's Laws: (~A & ~B) == (~(A | B)) (~A | ~B) == (~(A & B)) More...
 
static Instruction * foldLogicCastConstant (BinaryOperator &Logic, CastInst *Cast, InstCombiner::BuilderTy &Builder)
 Fold {and,or,xor} (cast X), C. More...
 
static Instruction * foldBoolSextMaskToSelect (BinaryOperator &I)
 
static Instruction * foldAndToXor (BinaryOperator &I, InstCombiner::BuilderTy &Builder)
 
static Instruction * foldOrToXor (BinaryOperator &I, InstCombiner::BuilderTy &Builder)
 
static bool areInverseVectorBitmasks (Constant *C1, Constant *C2)
 If all elements of two constant vectors are 0/-1 and inverses, return true. More...
 
static Value * getSelectCondition (Value *A, Value *B, InstCombiner::BuilderTy &Builder)
 We have an expression of the form (A & C) | (B & D). More...
 
static Value * matchSelectFromAndOr (Value *A, Value *C, Value *B, Value *D, InstCombiner::BuilderTy &Builder)
 We have an expression of the form (A & C) | (B & D). More...
 
static Instruction * FoldOrWithConstants (BinaryOperator &I, Value *Op, Value *A, Value *B, Value *C, InstCombiner::BuilderTy &Builder)
 This helper function folds: More...
 
static Instruction * FoldXorWithConstants (BinaryOperator &I, Value *Op, Value *A, Value *B, Value *C, InstCombiner::BuilderTy &Builder)
 This helper function folds: More...
 
static Instruction * foldXorToXor (BinaryOperator &I, InstCombiner::BuilderTy &Builder)
 A ^ B can be specified using other logic ops in a variety of patterns. More...
 

Macro Definition Documentation

#define DEBUG_TYPE   "instcombine"

Definition at line 24 of file InstCombineAndOrXor.cpp.

Enumeration Type Documentation

Classify (icmp eq (A & B), C) and (icmp ne (A & B), C) as matching patterns that can be simplified.

One of A and B is considered the mask. The other is the value. This is described as the "AMask" or "BMask" part of the enum. If the enum contains only "Mask", then both A and B can be considered masks. If A is the mask, then it was proven that (A & C) == C. This is trivial if C == A or C == 0. If both A and C are constants, this proof is also easy. For the following explanations, we assume that A is the mask.

"AllOnes" declares that the comparison is true only if (A & B) == A or all bits of A are set in B. Example: (icmp eq (A & 3), 3) -> AMask_AllOnes

"AllZeros" declares that the comparison is true only if (A & B) == 0 or all bits of A are cleared in B. Example: (icmp eq (A & 3), 0) -> Mask_AllZeroes

"Mixed" declares that (A & B) == C and C might or might not contain any number of one bits and zero bits. Example: (icmp eq (A & 3), 1) -> AMask_Mixed

"Not" means that in above descriptions "==" should be replaced by "!=". Example: (icmp ne (A & 3), 3) -> AMask_NotAllOnes

If the mask A contains a single bit, then the following is equivalent: (icmp eq (A & B), A) equals (icmp ne (A & B), 0) (icmp ne (A & B), A) equals (icmp eq (A & B), 0)

Enumerator
AMask_AllOnes 
AMask_NotAllOnes 
BMask_AllOnes 
BMask_NotAllOnes 
Mask_AllZeros 
Mask_NotAllZeros 
AMask_Mixed 
AMask_NotMixed 
BMask_Mixed 
BMask_NotMixed 

Definition at line 302 of file InstCombineAndOrXor.cpp.

Function Documentation

static bool areInverseVectorBitmasks ( Constant *  C1,
Constant *  C2 
)
static
static unsigned conjugateICmpMask ( unsigned  Mask)
static

Convert an analysis of a masked ICmp into its equivalent if all boolean operations had the opposite sense.

Since each "NotXXX" flag (recording !=) is adjacent to the corresponding normal flag (recording ==), this just involves swapping those bits over.

Definition at line 366 of file InstCombineAndOrXor.cpp.

References AMask_AllOnes, AMask_Mixed, AMask_NotAllOnes, AMask_NotMixed, BMask_AllOnes, BMask_Mixed, BMask_NotAllOnes, BMask_NotMixed, Mask_AllZeros, and Mask_NotAllZeros.

Referenced by foldLogOpOfMaskedICmps().

static Value* foldAndOrOfEqualityCmpsWithConstants ( ICmpInst *  LHS,
ICmpInst *  RHS,
bool  JoinedByAnd,
InstCombiner::BuilderTy &  Builder 
)
static
static Instruction* foldAndToXor ( BinaryOperator &  I,
InstCombiner::BuilderTy &  Builder 
)
static
static Instruction* foldBoolSextMaskToSelect ( BinaryOperator &  I)
static
static Instruction* foldLogicCastConstant ( BinaryOperator &  Logic,
CastInst *  Cast,
InstCombiner::BuilderTy &  Builder 
)
static
static Value* foldLogOpOfMaskedICmps ( ICmpInst *  LHS,
ICmpInst *  RHS,
bool  IsAnd,
llvm::InstCombiner::BuilderTy &  Builder 
)
static
static Instruction* foldOrToXor ( BinaryOperator &  I,
InstCombiner::BuilderTy &  Builder 
)
static
static Instruction* FoldOrWithConstants ( BinaryOperator &  I,
Value *  Op,
Value *  A,
Value *  B,
Value *  C,
InstCombiner::BuilderTy &  Builder 
)
static

This helper function folds:

((A | B) & C1) | (B & C2)

into:

(A & C1) | B

when the XOR of the two constants is "all ones" (-1).

Definition at line 1895 of file InstCombineAndOrXor.cpp.

References C, llvm::IRBuilder< T, Inserter >::CreateAnd(), llvm::dyn_cast(), llvm::ConstantInt::getValue(), llvm::APInt::isAllOnesValue(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().

Referenced by llvm::InstCombiner::visitOr().

static Instruction* foldXorToXor ( BinaryOperator &  I,
InstCombiner::BuilderTy &  Builder 
)
static
static Instruction* FoldXorWithConstants ( BinaryOperator &  I,
Value *  Op,
Value *  A,
Value *  B,
Value *  C,
InstCombiner::BuilderTy &  Builder 
)
static

This helper function folds:

((A ^ B) & C1) | (B & C2)

into:

(A & C1) ^ B

when the XOR of the two constants is "all ones" (-1).

Definition at line 1925 of file InstCombineAndOrXor.cpp.

References C, llvm::IRBuilder< T, Inserter >::CreateAnd(), llvm::dyn_cast(), llvm::ConstantInt::getValue(), llvm::APInt::isAllOnesValue(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_ConstantInt(), llvm::PatternMatch::m_Value(), and llvm::PatternMatch::match().

Referenced by llvm::InstCombiner::visitOr().

static unsigned getFCmpCode ( FCmpInst::Predicate  CC)
static
static Value* getFCmpValue ( unsigned  Code,
Value *  LHS,
Value *  RHS,
InstCombiner::BuilderTy &  Builder 
)
static

This is the complement of getFCmpCode, which turns an opcode and two operands into either a FCmp instruction, or a true/false constant.

Definition at line 66 of file InstCombineAndOrXor.cpp.

References assert(), llvm::IRBuilder< T, Inserter >::CreateFCmp(), llvm::CmpInst::FCMP_FALSE, llvm::CmpInst::FCMP_TRUE, llvm::ConstantInt::get(), llvm::Value::getType(), and llvm::CmpInst::makeCmpResultType().

static unsigned getMaskedICmpType ( Value *  A,
Value *  B,
Value *  C,
ICmpInst::Predicate  Pred 
)
static
static unsigned getMaskedTypeForICmpPair ( Value *&  A,
Value *&  B,
Value *&  C,
Value *&  D,
Value *&  E,
ICmpInst *  LHS,
ICmpInst *  RHS,
ICmpInst::Predicate &  PredL,
ICmpInst::Predicate &  PredR 
)
static

Handle (icmp(A & B) ==/!= C) &/| (icmp(A & D) ==/!= E).

Return the set of pattern classes (from MaskedICmpType) that both LHS and RHS satisfy.

Definition at line 382 of file InstCombineAndOrXor.cpp.

References llvm::decomposeBitTestICmp(), llvm::Constant::getAllOnesValue(), getMaskedICmpType(), llvm::User::getOperand(), llvm::Value::getType(), llvm::ICmpInst::isEquality(), llvm::Type::isIntegerTy(), llvm::Type::isVectorTy(), llvm::PatternMatch::m_And(), llvm::PatternMatch::m_Value(), llvm::PatternMatch::match(), and R2.

Referenced by foldLogOpOfMaskedICmps().

static Value* getNewICmpValue ( bool  Sign,
unsigned  Code,
Value *  LHS,
Value *  RHS,
InstCombiner::BuilderTy &  Builder 
)
static

This is the complement of getICmpCode, which turns an opcode and two operands into either a constant true or false, or a brand new ICmp instruction.

The sign is passed in to determine which kind of predicate to use in the new icmp instruction.

Definition at line 56 of file InstCombineAndOrXor.cpp.

References llvm::IRBuilder< T, Inserter >::CreateICmp(), and llvm::getICmpValue().

static Value* getSelectCondition ( Value *  A,
Value *  B,
InstCombiner::BuilderTy &  Builder 
)
static
static Instruction* matchDeMorgansLaws ( BinaryOperator &  I,
InstCombiner::BuilderTy &  Builder 
)
static
static Value* matchSelectFromAndOr ( Value *  A,
Value *  C,
Value *  B,
Value *  D,
InstCombiner::BuilderTy &  Builder 
)
static

We have an expression of the form (A & C) | (B & D).

Try to simplify this to "A' ? C : D", where A' is a boolean or vector of booleans.

Definition at line 1587 of file InstCombineAndOrXor.cpp.

References llvm::IRBuilder< T, Inserter >::CreateBitCast(), llvm::IRBuilder< T, Inserter >::CreateSelect(), getSelectCondition(), llvm::Value::getType(), llvm::peekThroughBitcast(), and llvm::MCID::Select.

Referenced by llvm::InstCombiner::visitOr().

static Value* SimplifyBSwap ( BinaryOperator &  I,
InstCombiner::BuilderTy &  Builder 
)
static