19 using namespace PatternMatch;
86 case Instruction::Add:
87 case Instruction::Mul:
92 case Instruction::Sub:
93 case Instruction::Shl:
94 case Instruction::LShr:
95 case Instruction::AShr:
107 case Instruction::Add:
108 case Instruction::Sub:
111 case Instruction::Shl:
112 case Instruction::LShr:
113 case Instruction::AShr:
117 case Instruction::Mul:
151 if (!isa<BinaryOperator>(TI))
155 Value *MatchOp, *OtherOpT, *OtherOpF;
161 MatchIsOpZero =
true;
166 MatchIsOpZero =
false;
173 MatchIsOpZero =
true;
178 MatchIsOpZero =
true;
215 if (
Instruction *TVI = dyn_cast<Instruction>(TrueVal)) {
216 if (TVI->hasOneUse() && TVI->getNumOperands() == 2 &&
217 !isa<Constant>(FalseVal)) {
219 unsigned OpToFold = 0;
220 if ((SFO & 1) && FalseVal == TVI->getOperand(0)) {
222 }
else if ((SFO & 2) && FalseVal == TVI->getOperand(1)) {
228 Value *OOp = TVI->getOperand(2-OpToFold);
231 if (!isa<Constant>(OOp) ||
isSelect01(C, cast<Constant>(OOp))) {
237 if (isa<PossiblyExactOperator>(BO))
239 if (isa<OverflowingBinaryOperator>(BO)) {
250 if (
Instruction *FVI = dyn_cast<Instruction>(FalseVal)) {
251 if (FVI->hasOneUse() && FVI->getNumOperands() == 2 &&
252 !isa<Constant>(TrueVal)) {
254 unsigned OpToFold = 0;
255 if ((SFO & 1) && TrueVal == FVI->getOperand(0)) {
257 }
else if ((SFO & 2) && TrueVal == FVI->getOperand(1)) {
263 Value *OOp = FVI->getOperand(2-OpToFold);
266 if (!isa<Constant>(OOp) ||
isSelect01(C, cast<Constant>(OOp))) {
272 if (isa<PossiblyExactOperator>(BO))
274 if (isa<OverflowingBinaryOperator>(BO)) {
303 if (B->getOperand(0) == Op)
304 return SimplifyBinOp(B->getOpcode(), RepOp, B->getOperand(1),
TD, TLI);
305 if (B->getOperand(1) == Op)
306 return SimplifyBinOp(B->getOpcode(), B->getOperand(0), RepOp,
TD, TLI);
310 if (
CmpInst *
C = dyn_cast<CmpInst>(I)) {
311 if (
C->getOperand(0) == Op)
314 if (
C->getOperand(1) == Op)
323 if (
Constant *CRepOp = dyn_cast<Constant>(RepOp)) {
337 if (
CmpInst *
C = dyn_cast<CmpInst>(I))
339 ConstOps[1],
TD, TLI);
342 if (!
LI->isVolatile())
385 bool OrOnTrueVal =
false;
390 if (!OrOnFalseVal && !OrOnTrueVal)
394 Value *
Y = OrOnFalseVal ? TrueVal : FalseVal;
400 V = Builder->
CreateShl(V, C2Log - C1Log);
401 }
else if (C1Log > C2Log) {
420 bool Changed =
false;
431 if (
ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) {
435 return ReplaceInstUsesWith(SI, FalseVal);
439 return ReplaceInstUsesWith(SI, FalseVal);
459 if ((CmpLHS == TrueVal && AdjustedRHS == FalseVal) ||
460 (CmpLHS == FalseVal && AdjustedRHS == TrueVal))
475 sextRHS == FalseVal) {
477 AdjustedRHS = sextRHS;
479 sextRHS == TrueVal) {
481 AdjustedRHS = sextRHS;
489 zextRHS == FalseVal) {
491 AdjustedRHS = zextRHS;
493 zextRHS == TrueVal) {
495 AdjustedRHS = zextRHS;
504 CmpRHS = AdjustedRHS;
528 if (TrueVal->
getType() == Ty) {
529 if (
ConstantInt *Cmp = dyn_cast<ConstantInt>(CmpRHS)) {
540 Value *AShr = Builder->CreateAShr(CmpLHS, Ty->getBitWidth()-1);
543 if (C2->isAllOnesValue())
544 return ReplaceInstUsesWith(SI, Builder->CreateOr(AShr, C1));
547 return ReplaceInstUsesWith(SI, Builder->CreateAdd(And, C1));
559 return ReplaceInstUsesWith(SI, FalseVal);
562 return ReplaceInstUsesWith(SI, FalseVal);
566 return ReplaceInstUsesWith(SI, TrueVal);
569 return ReplaceInstUsesWith(SI, TrueVal);
574 if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS)) {
587 return ReplaceInstUsesWith(SI, V);
589 return Changed ? &SI : 0;
609 if (I == 0)
return true;
615 if (
const PHINode *VP = dyn_cast<PHINode>(I))
616 if (VP->getParent() == CondPHI->
getParent())
637 if (C == A || C == B) {
641 return ReplaceInstUsesWith(Outer, Inner);
649 return ReplaceInstUsesWith(Outer, C);
692 FalseVal->getValue() - Offset->
getValue());
714 if (ValZeros > AndZeros)
715 V = Builder->
CreateShl(V, ValZeros - AndZeros);
716 else if (ValZeros < AndZeros)
717 V = Builder->
CreateLShr(V, AndZeros - ValZeros);
721 bool ShouldNotVal = !TrueVal->
isZero();
738 return ReplaceInstUsesWith(SI, V);
742 if (
C->getZExtValue()) {
744 return BinaryOperator::CreateOr(CondVal, FalseVal);
747 Value *NotCond = Builder->CreateNot(CondVal,
"not."+CondVal->
getName());
748 return BinaryOperator::CreateAnd(NotCond, FalseVal);
751 if (
C->getZExtValue() ==
false) {
753 return BinaryOperator::CreateAnd(CondVal, TrueVal);
756 Value *NotCond = Builder->CreateNot(CondVal,
"not."+CondVal->
getName());
757 return BinaryOperator::CreateOr(NotCond, TrueVal);
762 if (CondVal == TrueVal)
763 return BinaryOperator::CreateOr(CondVal, FalseVal);
764 if (CondVal == FalseVal)
765 return BinaryOperator::CreateAnd(CondVal, TrueVal);
770 return BinaryOperator::CreateAnd(TrueVal, FalseVal);
772 return BinaryOperator::CreateOr(TrueVal, FalseVal);
776 if (
ConstantInt *TrueValC = dyn_cast<ConstantInt>(TrueVal))
777 if (
ConstantInt *FalseValC = dyn_cast<ConstantInt>(FalseVal)) {
779 if (FalseValC->isZero() && TrueValC->getValue() == 1)
783 if (FalseValC->isZero() && TrueValC->isAllOnesValue())
787 if (TrueValC->isZero() && FalseValC->getValue() == 1) {
788 Value *NotCond = Builder->CreateNot(CondVal,
"not."+CondVal->
getName());
793 if (TrueValC->isZero() && FalseValC->isAllOnesValue()) {
794 Value *NotCond = Builder->CreateNot(CondVal,
"not."+CondVal->
getName());
799 return ReplaceInstUsesWith(SI, V);
803 if (
FCmpInst *FCI = dyn_cast<FCmpInst>(CondVal)) {
804 if (FCI->getOperand(0) == TrueVal && FCI->getOperand(1) == FalseVal) {
811 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
813 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
815 return ReplaceInstUsesWith(SI, FalseVal);
823 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
825 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
827 return ReplaceInstUsesWith(SI, TrueVal);
831 }
else if (FCI->getOperand(0) == FalseVal && FCI->getOperand(1) == TrueVal){
838 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
840 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
842 return ReplaceInstUsesWith(SI, FalseVal);
850 if (((CFPt = dyn_cast<ConstantFP>(TrueVal)) &&
852 ((CFPf = dyn_cast<ConstantFP>(FalseVal)) &&
854 return ReplaceInstUsesWith(SI, TrueVal);
862 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
863 if (
Instruction *Result = visitSelectInstWithICmp(SI, ICI))
866 if (
Instruction *TI = dyn_cast<Instruction>(TrueVal))
867 if (
Instruction *FI = dyn_cast<Instruction>(FalseVal))
868 if (TI->hasOneUse() && FI->hasOneUse()) {
872 if (TI->getOpcode() == FI->getOpcode())
878 if ((TI->getOpcode() == Instruction::Sub &&
879 FI->getOpcode() == Instruction::Add) ||
880 (TI->getOpcode() == Instruction::FSub &&
881 FI->getOpcode() == Instruction::FAdd)) {
882 AddOp = FI; SubOp = TI;
883 }
else if ((FI->getOpcode() == Instruction::Sub &&
884 TI->getOpcode() == Instruction::Add) ||
885 (FI->getOpcode() == Instruction::FSub &&
886 TI->getOpcode() == Instruction::FAdd)) {
887 AddOp = TI; SubOp = FI;
891 Value *OtherAddOp = 0;
892 if (SubOp->getOperand(0) == AddOp->
getOperand(0)) {
894 }
else if (SubOp->getOperand(0) == AddOp->
getOperand(1)) {
903 NegVal = Builder->CreateFNeg(SubOp->getOperand(1));
905 NegVal = Builder->CreateNeg(SubOp->getOperand(1));
908 Value *NewTrueOp = OtherAddOp;
909 Value *NewFalseOp = NegVal;
913 Builder->CreateSelect(CondVal, NewTrueOp,
914 NewFalseOp, SI.
getName() +
".p");
917 return BinaryOperator::CreateFAdd(SubOp->getOperand(0), NewSel);
919 return BinaryOperator::CreateAdd(SubOp->getOperand(0), NewSel);
926 if (
Instruction *FoldI = FoldSelectIntoOp(SI, TrueVal, FalseVal))
933 Value *LHS, *RHS, *LHS2, *RHS2;
936 if (
Instruction *R = FoldSPFofSPF(cast<Instruction>(LHS),SPF2,LHS2,RHS2,
940 if (
Instruction *R = FoldSPFofSPF(cast<Instruction>(RHS),SPF2,LHS2,RHS2,
958 if (
SelectInst *TrueSI = dyn_cast<SelectInst>(TrueVal)) {
959 if (TrueSI->getCondition() == CondVal) {
966 if (
SelectInst *FalseSI = dyn_cast<SelectInst>(FalseVal)) {
967 if (FalseSI->getCondition() == CondVal) {
983 unsigned VWidth = VecTy->getNumElements();
984 APInt UndefElts(VWidth, 0);
986 if (
Value *V = SimplifyDemandedVectorElts(&SI, AllOnesEltMask, UndefElts)) {
988 return ReplaceInstUsesWith(SI, V);
992 if (isa<ConstantAggregateZero>(CondVal)) {
993 return ReplaceInstUsesWith(SI, FalseVal);
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
void push_back(const T &Elt)
class_match< Value > m_Value()
m_Value() - Match an arbitrary value and ignore it.
Abstract base class of comparison instructions.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
unsigned getScalarSizeInBits()
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
This class represents zero extension of integer types.
unsigned getNumOperands() const
static Constant * GetSelectFoldableConstant(Instruction *I)
1 1 1 0 True if unordered or not equal
static bool isEquality(Predicate P)
This class represents a sign extension of integer types.
unsigned getBitWidth() const
Get the number of bits in this IntegerType.
LoopInfoBase< BlockT, LoopT > * LI
static Constant * getNullValue(Type *Ty)
StringRef getName() const
bool match(Val *V, const Pattern &P)
Value * CreateXor(Value *LHS, Value *RHS, const Twine &Name="")
Instruction * visitSelectInst(SelectInst &SI)
const APInt & getValue() const
Return the constant's value.
#define llvm_unreachable(msg)
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
static Value * foldSelectICmpAnd(const SelectInst &SI, ConstantInt *TrueVal, ConstantInt *FalseVal, InstCombiner::BuilderTy *Builder)
not_match< LHS > m_Not(const LHS &L)
static Value * SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, const DataLayout *TD, const TargetLibraryInfo *TLI)
Value * CreateOr(Value *LHS, Value *RHS, const Twine &Name="")
CastClass_match< OpTy, Instruction::ZExt > m_ZExt(const OpTy &Op)
m_ZExt
Represents a floating point comparison operator.
class_match< ConstantInt > m_ConstantInt()
m_ConstantInt() - Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_power2 > m_Power2()
m_Power2() - Match an integer or vector power of 2.
static const Value * getNotArgument(const Value *BinOp)
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=0)
Construct any of the CastInst subclasses.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
LLVM Constant Representation.
const Value * getCondition() const
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
specificval_ty m_Specific(const Value *V)
m_Specific - Match if we have a specific specified value.
Represent an integer comparison operator.
Instruction * FoldSelectIntoOp(SelectInst &SI, Value *, Value *)
Value * getOperand(unsigned i) const
bool isCommutative() const
static bool isNot(const Value *V)
static Value * foldSelectICmpAndOr(const SelectInst &SI, Value *TrueVal, Value *FalseVal, InstCombiner::BuilderTy *Builder)
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
Integer representation type.
Predicate getPredicate() const
Return the predicate for this instruction.
Instruction * FoldSelectOpOp(SelectInst &SI, Instruction *TI, Instruction *FI)
Constant * ConstantFoldLoadFromConstPtr(Constant *C, const DataLayout *TD=0)
static Constant * getAllOnesValue(Type *Ty)
Get the all ones value.
LLVMContext & getContext() const
bool isFPOrFPVectorTy() const
bool hasNoSignedWrap() const
hasNoSignedWrap - Determine whether the no signed wrap flag is set.
const Value * getTrueValue() const
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Value * SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
void setIsExact(bool b=true)
CastClass_match< OpTy, Instruction::SExt > m_SExt(const OpTy &Op)
m_SExt
BinaryOps getOpcode() const
static SelectPatternFlavor MatchSelectPattern(Value *V, Value *&LHS, Value *&RHS)
Class for constant integers.
unsigned getVectorNumElements() const
unsigned logBase2() const
Predicate getSwappedPredicate() const
Return the predicate as if the operands were swapped.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Value * SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool isExact() const
isExact - Determine whether the exact flag is set.
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
void setOperand(unsigned i, Value *Val)
bool isAllOnesValue() const
Instruction * FoldSPFofSPF(Instruction *Inner, SelectPatternFlavor SPF1, Value *A, Value *B, Instruction &Outer, SelectPatternFlavor SPF2, Value *C)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Class for arbitrary precision integers.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
Value * CreateShl(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateZExtOrTrunc(Value *V, Type *DestTy, const Twine &Name="")
Create a ZExt or Trunc from the integer value V to DestTy. Return the value untouched if the type of ...
Value * SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
static Constant * getSExt(Constant *C, Type *Ty)
static Constant * getZExt(Constant *C, Type *Ty)
unsigned greater or equal
static bool CanSelectOperandBeMappingIntoPredBlock(const Value *V, const SelectInst &SI)
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=0)
const APFloat & getValueAPF() const
bool isUnsigned() const
Determine if this instruction is using an unsigned comparison.
0 0 0 1 True if ordered and equal
LLVM Value Representation.
bool hasNoUnsignedWrap() const
hasNoUnsignedWrap - Determine whether the no unsigned wrap flag is set.
unsigned getOpcode() const
getOpcode() returns a member of one of the enums like Instruction::Add.
bool isZero() const
Returns true if and only if the float is plus or minus zero.
void moveBefore(Instruction *MovePos)
Constant * ConstantFoldInstOperands(unsigned Opcode, Type *DestTy, ArrayRef< Constant * > Ops, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0)
static bool isSelect01(Constant *C1, Constant *C2)
static unsigned GetSelectFoldableOperands(Instruction *I)
const Value * getFalseValue() const
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml","ocaml 3.10-compatible collector")
Instruction * visitSelectInstWithICmp(SelectInst &SI, ICmpInst *ICI)
static RegisterPass< NVPTXAllocaHoisting > X("alloca-hoisting","Hoisting alloca instructions in non-entry ""blocks to the entry block")
const BasicBlock * getParent() const
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
bool isOne() const
Determine if the value is one.