# Changeset View

Changeset View

# Standalone View

Standalone View

# llvm/trunk/lib/Analysis/InstructionSimplify.cpp

Show First 20 Lines • Show All 911 Lines • ▼ Show 20 Line(s) | 911 | static bool isICmpTrue(ICmpInst::Predicate Pred, Value *LHS, Value *RHS, | |||
---|---|---|---|---|---|

912 | const SimplifyQuery &Q, unsigned MaxRecurse) { | 912 | const SimplifyQuery &Q, unsigned MaxRecurse) { | ||

913 | Value *V = SimplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse); | 913 | Value *V = SimplifyICmpInst(Pred, LHS, RHS, Q, MaxRecurse); | ||

914 | Constant *C = dyn_cast_or_null<Constant>(V); | 914 | Constant *C = dyn_cast_or_null<Constant>(V); | ||

915 | return (C && C->isAllOnesValue()); | 915 | return (C && C->isAllOnesValue()); | ||

916 | } | 916 | } | ||

917 | 917 | | |||

918 | /// Return true if we can simplify X / Y to 0. Remainder can adapt that answer | 918 | /// Return true if we can simplify X / Y to 0. Remainder can adapt that answer | ||

919 | /// to simplify X % Y to X. | 919 | /// to simplify X % Y to X. | ||

920 | static bool isDivZero(Value *Op0, Value *Op1, const SimplifyQuery &Q, | 920 | static bool isDivZero(Value *X, Value *Y, const SimplifyQuery &Q, | ||

921 | unsigned MaxRecurse, bool IsSigned) { | 921 | unsigned MaxRecurse, bool IsSigned) { | ||

922 | // Recursion is always used, so bail out at once if we already hit the limit. | 922 | // Recursion is always used, so bail out at once if we already hit the limit. | ||

923 | if (!MaxRecurse--) | 923 | if (!MaxRecurse--) | ||

924 | return false; | 924 | return false; | ||

925 | 925 | | |||

926 | if (IsSigned) { | 926 | if (IsSigned) { | ||

927 | // TODO: Handle signed. | 927 | // |X| / |Y| --> 0 | ||

928 | // | ||||

929 | // We require that 1 operand is a simple constant. That could be extended to | ||||

930 | // 2 variables if we computed the sign bit for each. | ||||

931 | // | ||||

932 | // Make sure that a constant is not the minimum signed value because taking | ||||

933 | // the abs() of that is undefined. | ||||

934 | Type *Ty = X->getType(); | ||||

935 | const APInt *C; | ||||

936 | if (match(X, m_APInt(C)) && !C->isMinSignedValue()) { | ||||

937 | // Is the variable divisor magnitude always greater than the constant | ||||

938 | // dividend magnitude? | ||||

939 | // |Y| > |C| --> Y < -abs(C) or Y > abs(C) | ||||

940 | Constant *PosDividendC = ConstantInt::get(Ty, C->abs()); | ||||

941 | Constant *NegDividendC = ConstantInt::get(Ty, -C->abs()); | ||||

942 | if (isICmpTrue(CmpInst::ICMP_SLT, Y, NegDividendC, Q, MaxRecurse) || | ||||

943 | isICmpTrue(CmpInst::ICMP_SGT, Y, PosDividendC, Q, MaxRecurse)) | ||||

944 | return true; | ||||

945 | } | ||||

946 | if (match(Y, m_APInt(C))) { | ||||

947 | // Special-case: we can't take the abs() of a minimum signed value. If | ||||

948 | // that's the divisor, then all we have to do is prove that the dividend | ||||

949 | // is also not the minimum signed value. | ||||

950 | if (C->isMinSignedValue()) | ||||

951 | return isICmpTrue(CmpInst::ICMP_NE, X, Y, Q, MaxRecurse); | ||||

952 | | ||||

953 | // Is the variable dividend magnitude always less than the constant | ||||

954 | // divisor magnitude? | ||||

955 | // |X| < |C| --> X > -abs(C) and X < abs(C) | ||||

956 | Constant *PosDivisorC = ConstantInt::get(Ty, C->abs()); | ||||

957 | Constant *NegDivisorC = ConstantInt::get(Ty, -C->abs()); | ||||

958 | if (isICmpTrue(CmpInst::ICMP_SGT, X, NegDivisorC, Q, MaxRecurse) && | ||||

959 | isICmpTrue(CmpInst::ICMP_SLT, X, PosDivisorC, Q, MaxRecurse)) | ||||

960 | return true; | ||||

961 | } | ||||

928 | return false; | 962 | return false; | ||

929 | } | 963 | } | ||

930 | 964 | | |||

931 | // IsSigned == false. | 965 | // IsSigned == false. | ||

932 | // Is the quotient unsigned less than the divisor? | 966 | // Is the dividend unsigned less than the divisor? | ||

933 | return isICmpTrue(ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse); | 967 | return isICmpTrue(ICmpInst::ICMP_ULT, X, Y, Q, MaxRecurse); | ||

934 | } | 968 | } | ||

935 | 969 | | |||

936 | /// These are simplifications common to SDiv and UDiv. | 970 | /// These are simplifications common to SDiv and UDiv. | ||

937 | static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, | 971 | static Value *simplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1, | ||

938 | const SimplifyQuery &Q, unsigned MaxRecurse) { | 972 | const SimplifyQuery &Q, unsigned MaxRecurse) { | ||

939 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) | 973 | if (Constant *C = foldOrCommuteConstant(Opcode, Op0, Op1, Q)) | ||

940 | return C; | 974 | return C; | ||

941 | 975 | | |||

▲ Show 20 Lines • Show All 3824 Lines • Show Last 20 Lines |