diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -48,10 +48,11 @@ enum { RecursionLimit = 3 }; -STATISTIC(NumExpand, "Number of expansions"); +STATISTIC(NumExpand, "Number of expansions"); STATISTIC(NumReassoc, "Number of reassociations"); -static Value *SimplifyAndInst(Value *, Value *, const SimplifyQuery &, unsigned); +static Value *SimplifyAndInst(Value *, Value *, const SimplifyQuery &, + unsigned); static Value *simplifyUnOp(unsigned, Value *, const SimplifyQuery &, unsigned); static Value *simplifyFPUnOp(unsigned, Value *, const FastMathFlags &, const SimplifyQuery &, unsigned); @@ -64,9 +65,10 @@ static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q, unsigned MaxRecurse); static Value *SimplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned); -static Value *SimplifyXorInst(Value *, Value *, const SimplifyQuery &, unsigned); -static Value *SimplifyCastInst(unsigned, Value *, Type *, - const SimplifyQuery &, unsigned); +static Value *SimplifyXorInst(Value *, Value *, const SimplifyQuery &, + unsigned); +static Value *SimplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &, + unsigned); static Value *SimplifyGEPInst(Type *, Value *, ArrayRef, bool, const SimplifyQuery &, unsigned); static Value *SimplifySelectInst(Value *, Value *, Value *, @@ -116,15 +118,11 @@ /// For a boolean type or a vector of boolean type, return false or a vector /// with every element false. -static Constant *getFalse(Type *Ty) { - return ConstantInt::getFalse(Ty); -} +static Constant *getFalse(Type *Ty) { return ConstantInt::getFalse(Ty); } /// For a boolean type or a vector of boolean type, return true or a vector /// with every element true. -static Constant *getTrue(Type *Ty) { - return ConstantInt::getTrue(Ty); -} +static Constant *getTrue(Type *Ty) { return ConstantInt::getTrue(Ty); } /// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"? static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS, @@ -137,7 +135,7 @@ if (CPred == Pred && CLHS == LHS && CRHS == RHS) return true; return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS && - CRHS == LHS; + CRHS == LHS; } /// Simplify comparison with true or false branch of select: @@ -244,12 +242,12 @@ if (!B || B->getOpcode() != OpcodeToExpand) return nullptr; Value *B0 = B->getOperand(0), *B1 = B->getOperand(1); - Value *L = SimplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(), - MaxRecurse); + Value *L = + SimplifyBinOp(Opcode, B0, OtherOp, Q.getWithoutUndef(), MaxRecurse); if (!L) return nullptr; - Value *R = SimplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(), - MaxRecurse); + Value *R = + SimplifyBinOp(Opcode, B1, OtherOp, Q.getWithoutUndef(), MaxRecurse); if (!R) return nullptr; @@ -271,8 +269,8 @@ /// Try to simplify binops of form "A op (B op' C)" or the commuted variant by /// distributing op over op'. -static Value *expandCommutativeBinOp(Instruction::BinaryOps Opcode, - Value *L, Value *R, +static Value *expandCommutativeBinOp(Instruction::BinaryOps Opcode, Value *L, + Value *R, Instruction::BinaryOps OpcodeToExpand, const SimplifyQuery &Q, unsigned MaxRecurse) { @@ -312,7 +310,8 @@ if (Value *V = SimplifyBinOp(Opcode, B, C, Q, MaxRecurse)) { // It does! Return "A op V" if it simplifies or is already available. // If V equals B then "A op V" is just the LHS. - if (V == B) return LHS; + if (V == B) + return LHS; // Otherwise return "A op V" if it simplifies. if (Value *W = SimplifyBinOp(Opcode, A, V, Q, MaxRecurse)) { ++NumReassoc; @@ -331,7 +330,8 @@ if (Value *V = SimplifyBinOp(Opcode, A, B, Q, MaxRecurse)) { // It does! Return "V op C" if it simplifies or is already available. // If V equals B then "V op C" is just the RHS. - if (V == B) return RHS; + if (V == B) + return RHS; // Otherwise return "V op C" if it simplifies. if (Value *W = SimplifyBinOp(Opcode, V, C, Q, MaxRecurse)) { ++NumReassoc; @@ -354,7 +354,8 @@ if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { // It does! Return "V op B" if it simplifies or is already available. // If V equals A then "V op B" is just the LHS. - if (V == A) return LHS; + if (V == A) + return LHS; // Otherwise return "V op B" if it simplifies. if (Value *W = SimplifyBinOp(Opcode, V, B, Q, MaxRecurse)) { ++NumReassoc; @@ -373,7 +374,8 @@ if (Value *V = SimplifyBinOp(Opcode, C, A, Q, MaxRecurse)) { // It does! Return "B op V" if it simplifies or is already available. // If V equals C then "B op V" is just the RHS. - if (V == C) return RHS; + if (V == C) + return RHS; // Otherwise return "B op V" if it simplifies. if (Value *W = SimplifyBinOp(Opcode, B, V, Q, MaxRecurse)) { ++NumReassoc; @@ -538,10 +540,10 @@ Value *CommonValue = nullptr; for (Value *Incoming : PI->incoming_values()) { // If the incoming value is the phi node itself, it can safely be skipped. - if (Incoming == PI) continue; - Value *V = PI == LHS ? - SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) : - SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); + if (Incoming == PI) + continue; + Value *V = PI == LHS ? SimplifyBinOp(Opcode, Incoming, RHS, Q, MaxRecurse) + : SimplifyBinOp(Opcode, LHS, Incoming, Q, MaxRecurse); // If the operation failed to simplify, or simplified to a different value // to previously, then give up. if (!V || (CommonValue && V != CommonValue)) @@ -580,7 +582,8 @@ Value *Incoming = PI->getIncomingValue(u); Instruction *InTI = PI->getIncomingBlock(u)->getTerminator(); // If the incoming value is the phi node itself, it can safely be skipped. - if (Incoming == PI) continue; + if (Incoming == PI) + continue; // Change the context instruction to the "edge" that flows into the phi. // This is important because that is where incoming is actually "evaluated" // even though it is used later somewhere else. @@ -643,8 +646,7 @@ // X + ~X -> -1 since ~X = -X-1 Type *Ty = Op0->getType(); - if (match(Op0, m_Not(m_Specific(Op1))) || - match(Op1, m_Not(m_Specific(Op0)))) + if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0)))) return Constant::getAllOnesValue(Ty); // add nsw/nuw (xor Y, signmask), signmask --> Y @@ -660,12 +662,12 @@ /// i1 add -> xor. if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1)) - if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) + if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse - 1)) return V; // Try some generic simplifications for associative operations. - if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, Q, MaxRecurse)) return V; // Threading Add over selects and phi nodes is pointless, so don't bother. @@ -775,17 +777,17 @@ Value *X = nullptr, *Y = nullptr, *Z = Op1; if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z // See if "V === Y - Z" simplifies. - if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse-1)) + if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, Q, MaxRecurse - 1)) // It does! Now see if "X + V" simplifies. - if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse-1)) { + if (Value *W = SimplifyBinOp(Instruction::Add, X, V, Q, MaxRecurse - 1)) { // It does, we successfully reassociated! ++NumReassoc; return W; } // See if "V === X - Z" simplifies. - if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) + if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1)) // It does! Now see if "Y + V" simplifies. - if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse-1)) { + if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, Q, MaxRecurse - 1)) { // It does, we successfully reassociated! ++NumReassoc; return W; @@ -797,17 +799,17 @@ X = Op0; if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) // See if "V === X - Y" simplifies. - if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) + if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1)) // It does! Now see if "V - Z" simplifies. - if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse-1)) { + if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, Q, MaxRecurse - 1)) { // It does, we successfully reassociated! ++NumReassoc; return W; } // See if "V === X - Z" simplifies. - if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse-1)) + if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, Q, MaxRecurse - 1)) // It does! Now see if "V - Y" simplifies. - if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse-1)) { + if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, Q, MaxRecurse - 1)) { // It does, we successfully reassociated! ++NumReassoc; return W; @@ -819,9 +821,9 @@ Z = Op0; if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) // See if "V === Z - X" simplifies. - if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse-1)) + if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, Q, MaxRecurse - 1)) // It does! Now see if "V + Y" simplifies. - if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse-1)) { + if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, Q, MaxRecurse - 1)) { // It does, we successfully reassociated! ++NumReassoc; return W; @@ -832,7 +834,7 @@ match(Op1, m_Trunc(m_Value(Y)))) if (X->getType() == Y->getType()) // See if "V === X - Y" simplifies. - if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse-1)) + if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, Q, MaxRecurse - 1)) // It does! Now see if "trunc V" simplifies. if (Value *W = SimplifyCastInst(Instruction::Trunc, V, Op0->getType(), Q, MaxRecurse - 1)) @@ -840,14 +842,13 @@ return W; // Variations on GEP(base, I, ...) - GEP(base, i, ...) -> GEP(null, I-i, ...). - if (match(Op0, m_PtrToInt(m_Value(X))) && - match(Op1, m_PtrToInt(m_Value(Y)))) + if (match(Op0, m_PtrToInt(m_Value(X))) && match(Op1, m_PtrToInt(m_Value(Y)))) if (Constant *Result = computePointerDifference(Q.DL, X, Y)) return ConstantExpr::getIntegerCast(Result, Op0->getType(), true); // i1 sub -> xor. if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1)) - if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse-1)) + if (Value *V = SimplifyXorInst(Op0, Op1, Q, MaxRecurse - 1)) return V; // Threading Sub over selects and phi nodes is pointless, so don't bother. @@ -897,12 +898,12 @@ // i1 mul -> and. if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1)) - if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse-1)) + if (Value *V = SimplifyAndInst(Op0, Op1, Q, MaxRecurse - 1)) return V; // Try some generic simplifications for associative operations. - if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, Q, MaxRecurse)) return V; // Mul distributes over Add. Try some generic simplifications based on this. @@ -913,15 +914,15 @@ // If the operation is with the result of a select instruction, check whether // operating on either branch of the select always yields the same value. if (isa(Op0) || isa(Op1)) - if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, Q, MaxRecurse)) return V; // If the operation is with the result of a phi instruction, check whether // operating on all incoming values of the phi always yields the same value. if (isa(Op0) || isa(Op1)) - if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, Q, MaxRecurse)) return V; return nullptr; @@ -1330,8 +1331,8 @@ /// Given operands for an Shl, LShr or AShr, see if we can /// fold the result. If not, this returns null. static Value *SimplifyRightShift(Instruction::BinaryOps Opcode, Value *Op0, - Value *Op1, bool isExact, const SimplifyQuery &Q, - unsigned MaxRecurse) { + Value *Op1, bool isExact, + const SimplifyQuery &Q, unsigned MaxRecurse) { if (Value *V = SimplifyShift(Opcode, Op0, Op1, /*IsNSW*/ false, Q, MaxRecurse)) return V; @@ -1347,7 +1348,8 @@ // The low bit cannot be shifted out of an exact shift if it is set. if (isExact) { - KnownBits Op0Known = computeKnownBits(Op0, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT); + KnownBits Op0Known = + computeKnownBits(Op0, Q.DL, /*Depth=*/0, Q.AC, Q.CxtI, Q.DT); if (Op0Known.One[0]) return Op0; } @@ -1394,7 +1396,7 @@ const SimplifyQuery &Q, unsigned MaxRecurse) { if (Value *V = SimplifyRightShift(Instruction::LShr, Op0, Op1, isExact, Q, MaxRecurse)) - return V; + return V; // (X << A) >> A -> X Value *X; @@ -1572,7 +1574,7 @@ /// with the parameters swapped. static Value *simplifyAndOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { ICmpInst::Predicate Pred0, Pred1; - Value *A ,*B; + Value *A, *B; if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) return nullptr; @@ -1597,7 +1599,7 @@ /// with the parameters swapped. static Value *simplifyOrOfICmpsWithSameOperands(ICmpInst *Op0, ICmpInst *Op1) { ICmpInst::Predicate Pred0, Pred1; - Value *A ,*B; + Value *A, *B; if (!match(Op0, m_ICmp(Pred0, m_Value(A), m_Value(B))) || !match(Op1, m_ICmp(Pred1, m_Specific(A), m_Specific(B)))) return nullptr; @@ -1939,8 +1941,8 @@ return nullptr; } -static Value *simplifyAndOrOfFCmps(const TargetLibraryInfo *TLI, - FCmpInst *LHS, FCmpInst *RHS, bool IsAnd) { +static Value *simplifyAndOrOfFCmps(const TargetLibraryInfo *TLI, FCmpInst *LHS, + FCmpInst *RHS, bool IsAnd) { Value *LHS0 = LHS->getOperand(0), *LHS1 = LHS->getOperand(1); Value *RHS0 = RHS->getOperand(0), *RHS1 = RHS->getOperand(1); if (LHS0->getType() != RHS0->getType()) @@ -1977,8 +1979,8 @@ return nullptr; } -static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q, - Value *Op0, Value *Op1, bool IsAnd) { +static Value *simplifyAndOrOfCmps(const SimplifyQuery &Q, Value *Op0, + Value *Op1, bool IsAnd) { // Look through casts of the 'and' operands to find compares. auto *Cast0 = dyn_cast(Op0); auto *Cast1 = dyn_cast(Op1); @@ -2065,8 +2067,7 @@ return Op0; // A & ~A = ~A & A = 0 - if (match(Op0, m_Not(m_Specific(Op1))) || - match(Op1, m_Not(m_Specific(Op0)))) + if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0)))) return Constant::getNullValue(Op0->getType()); // (A | ?) & A = A @@ -2139,8 +2140,8 @@ return V; // Try some generic simplifications for associative operations. - if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, Q, MaxRecurse)) return V; // And distributes over Or. Try some generic simplifications based on this. @@ -2164,16 +2165,16 @@ // If the operation is with the result of a select instruction, check // whether operating on either branch of the select always yields the same // value. - if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + ThreadBinOpOverSelect(Instruction::And, Op0, Op1, Q, MaxRecurse)) return V; } // If the operation is with the result of a phi instruction, check whether // operating on all incoming values of the phi always yields the same value. if (isa(Op0) || isa(Op1)) - if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + ThreadBinOpOverPHI(Instruction::And, Op0, Op1, Q, MaxRecurse)) return V; // Assuming the effective width of Y is not larger than A, i.e. all bits @@ -2196,8 +2197,7 @@ const KnownBits YKnown = computeKnownBits(Y, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); const unsigned EffWidthY = YKnown.countMaxActiveBits(); if (EffWidthY <= ShftCnt) { - const KnownBits XKnown = computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI, - Q.DT); + const KnownBits XKnown = computeKnownBits(X, Q.DL, 0, Q.AC, Q.CxtI, Q.DT); const unsigned EffWidthX = XKnown.countMaxActiveBits(); const APInt EffBitsY = APInt::getLowBitsSet(Width, EffWidthY); const APInt EffBitsX = APInt::getLowBitsSet(Width, EffWidthX) << ShftCnt; @@ -2402,8 +2402,8 @@ return Op0; // Try some generic simplifications for associative operations. - if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, Q, MaxRecurse)) return V; // Or distributes over And. Try some generic simplifications based on this. @@ -2422,8 +2422,8 @@ // If the operation is with the result of a select instruction, check // whether operating on either branch of the select always yields the same // value. - if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, Q, MaxRecurse)) return V; } @@ -2445,8 +2445,7 @@ return A; } // Or commutes, try both ways. - if (C1->isMask() && - match(B, m_c_Add(m_Specific(A), m_Value(N)))) { + if (C1->isMask() && match(B, m_c_Add(m_Specific(A), m_Value(N)))) { // Add commutes, try both ways. if (MaskedValueIsZero(N, *C1, Q.DL, 0, Q.AC, Q.CxtI, Q.DT)) return B; @@ -2500,8 +2499,7 @@ return Constant::getNullValue(Op0->getType()); // A ^ ~A = ~A ^ A = -1 - if (match(Op0, m_Not(m_Specific(Op1))) || - match(Op1, m_Not(m_Specific(Op0)))) + if (match(Op0, m_Not(m_Specific(Op1))) || match(Op1, m_Not(m_Specific(Op0)))) return Constant::getAllOnesValue(Op0->getType()); auto foldAndOrNot = [](Value *X, Value *Y) -> Value * { @@ -2532,8 +2530,8 @@ return V; // Try some generic simplifications for associative operations. - if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, - MaxRecurse)) + if (Value *V = + SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, Q, MaxRecurse)) return V; // Threading Xor over selects and phi nodes is pointless, so don't bother. @@ -2552,7 +2550,6 @@ return ::SimplifyXorInst(Op0, Op1, Q, RecursionLimit); } - static Type *GetCompareTy(Value *Op) { return CmpInst::makeCmpResultType(Op->getType()); } @@ -2590,7 +2587,7 @@ if (const GlobalValue *GV = dyn_cast(V)) return (GV->hasLocalLinkage() || GV->hasHiddenVisibility() || GV->hasProtectedVisibility() || GV->hasGlobalUnnamedAddr()) && - !GV->isThreadLocal(); + !GV->isThreadLocal(); if (const Argument *A = dyn_cast(V)) return A->hasByValAttr(); return false; @@ -2637,8 +2634,8 @@ if (isByValArg(V2)) return isa(V1) || isa(V1) || isByValArg(V1); - return isa(V1) && - (isa(V2) || isa(V2)); + return isa(V1) && + (isa(V2) || isa(V2)); } // A significant optimization not implemented here is assuming that alloca @@ -2669,9 +2666,8 @@ // If the C and C++ standards are ever made sufficiently restrictive in this // area, it may be possible to update LLVM's semantics accordingly and reinstate // this optimization. -static Constant * -computePointerICmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, - const SimplifyQuery &Q) { +static Constant *computePointerICmp(CmpInst::Predicate Pred, Value *LHS, + Value *RHS, const SimplifyQuery &Q) { const DataLayout &DL = Q.DL; const TargetLibraryInfo *TLI = Q.TLI; const DominatorTree *DT = Q.DT; @@ -2686,8 +2682,7 @@ if (isa(RHS) && ICmpInst::isEquality(Pred) && llvm::isKnownNonZero(LHS, DL, 0, nullptr, nullptr, nullptr, IIQ.UseInstrInfo)) - return ConstantInt::get(GetCompareTy(LHS), - !CmpInst::isTrueWhenEqual(Pred)); + return ConstantInt::get(GetCompareTy(LHS), !CmpInst::isTrueWhenEqual(Pred)); // We can only fold certain predicates on pointer comparisons. switch (Pred) { @@ -2727,8 +2722,8 @@ // If LHS and RHS are related via constant offsets to the same base // value, we can replace it with an icmp which just compares the offsets. if (LHS == RHS) - return ConstantInt::get( - GetCompareTy(LHS), ICmpInst::compare(LHSOffset, RHSOffset, Pred)); + return ConstantInt::get(GetCompareTy(LHS), + ICmpInst::compare(LHSOffset, RHSOffset, Pred)); // Various optimizations for (in)equality comparisons. if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) { @@ -2781,8 +2776,8 @@ if ((IsNAC(LHSUObjs) && IsAllocDisjoint(RHSUObjs)) || (IsNAC(RHSUObjs) && IsAllocDisjoint(LHSUObjs))) - return ConstantInt::get(GetCompareTy(LHS), - !CmpInst::isTrueWhenEqual(Pred)); + return ConstantInt::get(GetCompareTy(LHS), + !CmpInst::isTrueWhenEqual(Pred)); // Fold comparisons for non-escaping pointer even if the allocation call // cannot be elided. We cannot fold malloc comparison to null. Also, the @@ -2848,7 +2843,8 @@ case CmpInst::ICMP_SLE: // X <=s 0 -> true return getTrue(ITy); - default: break; + default: + break; } } else if (match(RHS, m_One())) { switch (Pred) { @@ -2872,7 +2868,8 @@ case CmpInst::ICMP_SGE: // X >=s -1 -> true return getTrue(ITy); - default: break; + default: + break; } } @@ -3015,9 +3012,10 @@ return nullptr; } -static Value *simplifyICmpWithBinOpOnLHS( - CmpInst::Predicate Pred, BinaryOperator *LBO, Value *RHS, - const SimplifyQuery &Q, unsigned MaxRecurse) { +static Value *simplifyICmpWithBinOpOnLHS(CmpInst::Predicate Pred, + BinaryOperator *LBO, Value *RHS, + const SimplifyQuery &Q, + unsigned MaxRecurse) { Type *ITy = GetCompareTy(RHS); // The return type. Value *Y = nullptr; @@ -3153,7 +3151,6 @@ return nullptr; } - // If only one of the icmp's operands has NSW flags, try to prove that: // // icmp slt (x + C1), (x +nsw C2) @@ -3188,7 +3185,6 @@ (C2->slt(*C1) && C1->isNonPositive()); } - /// TODO: A large part of this logic is duplicated in InstCombine's /// foldICmpBinOp(). We should be able to share that and avoid the code /// duplication. @@ -3340,7 +3336,7 @@ break; if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0), RBO->getOperand(0), Q, MaxRecurse - 1)) - return V; + return V; break; case Instruction::SDiv: if (!ICmpInst::isEquality(Pred) || !Q.IIQ.isExact(LBO) || @@ -3574,9 +3570,8 @@ continue; CallInst *Assume = cast(AssumeVH); - if (Optional Imp = - isImpliedCondition(Assume->getArgOperand(0), Predicate, LHS, RHS, - Q.DL)) + if (Optional Imp = isImpliedCondition(Assume->getArgOperand(0), + Predicate, LHS, RHS, Q.DL)) if (isValidAssumeForContext(Assume, Q.CxtI, Q.DT)) return ConstantInt::get(GetCompareTy(LHS), *Imp); } @@ -3666,13 +3661,13 @@ // Transfer the cast to the constant. if (Value *V = SimplifyICmpInst(Pred, SrcOp, ConstantExpr::getIntToPtr(RHSC, SrcTy), - Q, MaxRecurse-1)) + Q, MaxRecurse - 1)) return V; } else if (PtrToIntInst *RI = dyn_cast(RHS)) { if (RI->getOperand(0)->getType() == SrcTy) // Compare without the cast. - if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), - Q, MaxRecurse-1)) + if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q, + MaxRecurse - 1)) return V; } } @@ -3683,9 +3678,9 @@ if (ZExtInst *RI = dyn_cast(RHS)) { if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) // Compare X and Y. Note that signed predicates become unsigned. - if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), - SrcOp, RI->getOperand(0), Q, - MaxRecurse-1)) + if (Value *V = + SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), SrcOp, + RI->getOperand(0), Q, MaxRecurse - 1)) return V; } // Fold (zext X) ule (sext X), (zext X) sge (sext X) to true. @@ -3709,14 +3704,15 @@ // also a case of comparing two zero-extended values. if (RExt == CI && MaxRecurse) if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), - SrcOp, Trunc, Q, MaxRecurse-1)) + SrcOp, Trunc, Q, MaxRecurse - 1)) return V; // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit // there. Use this to work out the result of the comparison. if (RExt != CI) { switch (Pred) { - default: llvm_unreachable("Unknown ICmp predicate!"); + default: + llvm_unreachable("Unknown ICmp predicate!"); // LHS getValue().isNegative() ? - ConstantInt::getTrue(CI->getContext()) : - ConstantInt::getFalse(CI->getContext()); + return CI->getValue().isNegative() + ? ConstantInt::getTrue(CI->getContext()) + : ConstantInt::getFalse(CI->getContext()); case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: - return CI->getValue().isNegative() ? - ConstantInt::getFalse(CI->getContext()) : - ConstantInt::getTrue(CI->getContext()); + return CI->getValue().isNegative() + ? ConstantInt::getFalse(CI->getContext()) + : ConstantInt::getTrue(CI->getContext()); } } } @@ -3752,8 +3748,8 @@ if (SExtInst *RI = dyn_cast(RHS)) { if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) // Compare X and Y. Note that the predicate does not change. - if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), - Q, MaxRecurse-1)) + if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), Q, + MaxRecurse - 1)) return V; } // Fold (sext X) uge (zext X), (sext X) sle (zext X) to true. @@ -3776,14 +3772,16 @@ // If the re-extended constant didn't change then this is effectively // also a case of comparing two sign-extended values. if (RExt == CI && MaxRecurse) - if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse-1)) + if (Value *V = + SimplifyICmpInst(Pred, SrcOp, Trunc, Q, MaxRecurse - 1)) return V; // Otherwise the upper bits of LHS are all equal, while RHS has varying // bits there. Use this to work out the result of the comparison. if (RExt != CI) { switch (Pred) { - default: llvm_unreachable("Unknown ICmp predicate!"); + default: + llvm_unreachable("Unknown ICmp predicate!"); case ICmpInst::ICMP_EQ: return ConstantInt::getFalse(CI->getContext()); case ICmpInst::ICMP_NE: @@ -3793,14 +3791,14 @@ // LHS >s RHS. case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGE: - return CI->getValue().isNegative() ? - ConstantInt::getTrue(CI->getContext()) : - ConstantInt::getFalse(CI->getContext()); + return CI->getValue().isNegative() + ? ConstantInt::getTrue(CI->getContext()) + : ConstantInt::getFalse(CI->getContext()); case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: - return CI->getValue().isNegative() ? - ConstantInt::getFalse(CI->getContext()) : - ConstantInt::getTrue(CI->getContext()); + return CI->getValue().isNegative() + ? ConstantInt::getFalse(CI->getContext()) + : ConstantInt::getTrue(CI->getContext()); // If LHS is non-negative then LHS u RHS. @@ -3809,8 +3807,8 @@ // Comparison is true iff the LHS =s 0. if (MaxRecurse) if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, - Constant::getNullValue(SrcTy), - Q, MaxRecurse-1)) + Constant::getNullValue(SrcTy), Q, + MaxRecurse - 1)) return V; break; } @@ -4018,23 +4016,29 @@ // The ordered relationship and minnum/maxnum guarantee that we do not // have NaN constants, so ordered/unordered preds are handled the same. switch (Pred) { - case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_UEQ: + case FCmpInst::FCMP_OEQ: + case FCmpInst::FCMP_UEQ: // minnum(X, LesserC) == C --> false // maxnum(X, GreaterC) == C --> false return getFalse(RetTy); - case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_UNE: + case FCmpInst::FCMP_ONE: + case FCmpInst::FCMP_UNE: // minnum(X, LesserC) != C --> true // maxnum(X, GreaterC) != C --> true return getTrue(RetTy); - case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_UGE: - case FCmpInst::FCMP_OGT: case FCmpInst::FCMP_UGT: + case FCmpInst::FCMP_OGE: + case FCmpInst::FCMP_UGE: + case FCmpInst::FCMP_OGT: + case FCmpInst::FCMP_UGT: // minnum(X, LesserC) >= C --> false // minnum(X, LesserC) > C --> false // maxnum(X, GreaterC) >= C --> true // maxnum(X, GreaterC) > C --> true return ConstantInt::get(RetTy, IsMaxNum); - case FCmpInst::FCMP_OLE: case FCmpInst::FCMP_ULE: - case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_ULT: + case FCmpInst::FCMP_OLE: + case FCmpInst::FCMP_ULE: + case FCmpInst::FCMP_OLT: + case FCmpInst::FCMP_ULT: // minnum(X, LesserC) <= C --> true // minnum(X, LesserC) < C --> true // maxnum(X, GreaterC) <= C --> false @@ -4166,9 +4170,8 @@ GEP->isInBounds(), Q, MaxRecurse - 1)); if (isa(I)) - return PreventSelfSimplify( - SimplifySelectInst(NewOps[0], NewOps[1], NewOps[2], Q, - MaxRecurse - 1)); + return PreventSelfSimplify(SimplifySelectInst( + NewOps[0], NewOps[1], NewOps[2], Q, MaxRecurse - 1)); // TODO: We could hand off more cases to instsimplify here. } @@ -4264,7 +4267,8 @@ /// Try to simplify a select instruction when its condition operand is an /// integer comparison. static Value *simplifySelectWithICmpCond(Value *CondVal, Value *TrueVal, - Value *FalseVal, const SimplifyQuery &Q, + Value *FalseVal, + const SimplifyQuery &Q, unsigned MaxRecurse) { ICmpInst::Predicate Pred; Value *CmpLHS, *CmpRHS; @@ -4284,7 +4288,8 @@ Value *X, *Y; SelectPatternFlavor SPF = matchDecomposedSelectPattern(cast(CondVal), TrueVal, FalseVal, - X, Y).Flavor; + X, Y) + .Flavor; if (SelectPatternResult::isMinOrMax(SPF) && Pred == getMinMaxPred(SPF)) { APInt LimitC = getMinMaxLimit(getInverseMinMaxFlavor(SPF), X->getType()->getScalarSizeInBits()); @@ -4336,8 +4341,8 @@ } // Check for other compares that behave like bit test. - if (Value *V = simplifySelectWithFakeICmpEq(CmpLHS, CmpRHS, Pred, - TrueVal, FalseVal)) + if (Value *V = + simplifySelectWithFakeICmpEq(CmpLHS, CmpRHS, Pred, TrueVal, FalseVal)) return V; // If we have a scalar equality comparison, then we know the value in one of @@ -4347,18 +4352,18 @@ // because each element of a vector select is chosen independently. if (Pred == ICmpInst::ICMP_EQ && !CondVal->getType()->isVectorTy()) { if (simplifyWithOpReplaced(FalseVal, CmpLHS, CmpRHS, Q, - /* AllowRefinement */ false, MaxRecurse) == - TrueVal || + /* AllowRefinement */ false, + MaxRecurse) == TrueVal || simplifyWithOpReplaced(FalseVal, CmpRHS, CmpLHS, Q, - /* AllowRefinement */ false, MaxRecurse) == - TrueVal) + /* AllowRefinement */ false, + MaxRecurse) == TrueVal) return FalseVal; if (simplifyWithOpReplaced(TrueVal, CmpLHS, CmpRHS, Q, - /* AllowRefinement */ true, MaxRecurse) == - FalseVal || + /* AllowRefinement */ true, + MaxRecurse) == FalseVal || simplifyWithOpReplaced(TrueVal, CmpRHS, CmpLHS, Q, - /* AllowRefinement */ true, MaxRecurse) == - FalseVal) + /* AllowRefinement */ true, + MaxRecurse) == FalseVal) return FalseVal; } @@ -4377,11 +4382,11 @@ // This transform is safe if we do not have (do not care about) -0.0 or if // at least one operand is known to not be -0.0. Otherwise, the select can // change the sign of a zero operand. - bool HasNoSignedZeros = Q.CxtI && isa(Q.CxtI) && - Q.CxtI->hasNoSignedZeros(); + bool HasNoSignedZeros = + Q.CxtI && isa(Q.CxtI) && Q.CxtI->hasNoSignedZeros(); const APFloat *C; if (HasNoSignedZeros || (match(T, m_APFloat(C)) && C->isNonZero()) || - (match(F, m_APFloat(C)) && C->isNonZero())) { + (match(F, m_APFloat(C)) && C->isNonZero())) { // (T == F) ? T : F --> F // (F == T) ? T : F --> F if (Pred == FCmpInst::FCMP_OEQ) @@ -4667,8 +4672,8 @@ /// Given operands for an InsertValueInst, see if we can fold the result. /// If not, this returns null. static Value *SimplifyInsertValueInst(Value *Agg, Value *Val, - ArrayRef Idxs, const SimplifyQuery &Q, - unsigned) { + ArrayRef Idxs, + const SimplifyQuery &Q, unsigned) { if (Constant *CAgg = dyn_cast(Agg)) if (Constant *CVal = dyn_cast(Val)) return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs); @@ -4821,14 +4826,15 @@ bool HasUndefInput = false; for (Value *Incoming : IncomingValues) { // If the incoming value is the phi node itself, it can safely be skipped. - if (Incoming == PN) continue; + if (Incoming == PN) + continue; if (Q.isUndefValue(Incoming)) { // Remember that we saw an undef value, but otherwise ignore them. HasUndefInput = true; continue; } if (CommonValue && Incoming != CommonValue) - return nullptr; // Not the same, bail out. + return nullptr; // Not the same, bail out. CommonValue = Incoming; } @@ -4853,8 +4859,8 @@ return CommonValue; } -static Value *SimplifyCastInst(unsigned CastOpc, Value *Op, - Type *Ty, const SimplifyQuery &Q, unsigned MaxRecurse) { +static Value *SimplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty, + const SimplifyQuery &Q, unsigned MaxRecurse) { if (auto *C = dyn_cast(Op)) return ConstantFoldCastOperand(CastOpc, C, Ty, Q.DL); @@ -5065,8 +5071,8 @@ return ::SimplifyShuffleVectorInst(Op0, Op1, Mask, RetTy, Q, RecursionLimit); } -static Constant *foldConstant(Instruction::UnaryOps Opcode, - Value *&Op, const SimplifyQuery &Q) { +static Constant *foldConstant(Instruction::UnaryOps Opcode, Value *&Op, + const SimplifyQuery &Q) { if (auto *C = dyn_cast(Op)) return ConstantFoldUnaryOpOperand(Opcode, C, Q.DL); return nullptr; @@ -5231,8 +5237,7 @@ // fsub -0.0, (fneg X) ==> X Value *X; if (canIgnoreSNaN(ExBehavior, FMF)) - if (match(Op0, m_NegZeroFP()) && - match(Op1, m_FNeg(m_Value(X)))) + if (match(Op0, m_NegZeroFP()) && match(Op1, m_FNeg(m_Value(X)))) return X; if (!isDefaultFPEnvironment(ExBehavior, Rounding)) @@ -5454,8 +5459,8 @@ /// If not, this returns null. /// Try to use FastMathFlags when folding the result. static Value *simplifyFPUnOp(unsigned Opcode, Value *Op, - const FastMathFlags &FMF, - const SimplifyQuery &Q, unsigned MaxRecurse) { + const FastMathFlags &FMF, const SimplifyQuery &Q, + unsigned MaxRecurse) { switch (Opcode) { case Instruction::FNeg: return simplifyFNegInst(Op, FMF, Q, MaxRecurse); @@ -5564,7 +5569,8 @@ static bool IsIdempotent(Intrinsic::ID ID) { switch (ID) { - default: return false; + default: + return false; // Unary idempotent: f(f(x)) = f(x) case Intrinsic::fabs: @@ -5648,15 +5654,18 @@ Value *X; switch (IID) { case Intrinsic::fabs: - if (SignBitMustBeZero(Op0, Q.TLI)) return Op0; + if (SignBitMustBeZero(Op0, Q.TLI)) + return Op0; break; case Intrinsic::bswap: // bswap(bswap(x)) -> x - if (match(Op0, m_BSwap(m_Value(X)))) return X; + if (match(Op0, m_BSwap(m_Value(X)))) + return X; break; case Intrinsic::bitreverse: // bitreverse(bitreverse(x)) -> x - if (match(Op0, m_BitReverse(m_Value(X)))) return X; + if (match(Op0, m_BitReverse(m_Value(X)))) + return X; break; case Intrinsic::ctpop: { // If everything but the lowest bit is zero, that bit is the pop-count. Ex: @@ -5670,30 +5679,34 @@ case Intrinsic::exp: // exp(log(x)) -> x if (Q.CxtI->hasAllowReassoc() && - match(Op0, m_Intrinsic(m_Value(X)))) return X; + match(Op0, m_Intrinsic(m_Value(X)))) + return X; break; case Intrinsic::exp2: // exp2(log2(x)) -> x if (Q.CxtI->hasAllowReassoc() && - match(Op0, m_Intrinsic(m_Value(X)))) return X; + match(Op0, m_Intrinsic(m_Value(X)))) + return X; break; case Intrinsic::log: // log(exp(x)) -> x if (Q.CxtI->hasAllowReassoc() && - match(Op0, m_Intrinsic(m_Value(X)))) return X; + match(Op0, m_Intrinsic(m_Value(X)))) + return X; break; case Intrinsic::log2: // log2(exp2(x)) -> x if (Q.CxtI->hasAllowReassoc() && (match(Op0, m_Intrinsic(m_Value(X))) || - match(Op0, m_Intrinsic(m_SpecificFP(2.0), - m_Value(X))))) return X; + match(Op0, + m_Intrinsic(m_SpecificFP(2.0), m_Value(X))))) + return X; break; case Intrinsic::log10: // log10(pow(10.0, x)) -> x if (Q.CxtI->hasAllowReassoc() && - match(Op0, m_Intrinsic(m_SpecificFP(10.0), - m_Value(X)))) return X; + match(Op0, m_Intrinsic(m_SpecificFP(10.0), m_Value(X)))) + return X; break; case Intrinsic::floor: case Intrinsic::trunc: @@ -5942,7 +5955,8 @@ case Intrinsic::maximum: case Intrinsic::minimum: { // If the arguments are the same, this is a no-op. - if (Op0 == Op1) return Op0; + if (Op0 == Op1) + return Op0; // Canonicalize constant operand as Op1. if (isa(Op0)) @@ -6571,6 +6585,6 @@ } template const SimplifyQuery getBestSimplifyQuery(AnalysisManager &, Function &); -} +} // namespace llvm void InstSimplifyFolder::anchor() {}