From b8bae4478454bfdbaac042a1729b5d5dff057d7f Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Tue, 3 Dec 2024 17:54:42 +0800 Subject: [PATCH 1/2] [ConstraintElim] Add range support for Constraint Elimination --- .../Scalar/ConstraintElimination.cpp | 64 ++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index 4884c23f16e12a..b41255f1dbe7c6 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -1600,8 +1600,68 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B, DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned, std::move(ValuesToRelease)); - if (!R.IsSigned) { - for (Value *V : NewVariables) { + // Add range information from attributes and metadata. + for (auto *V : NewVariables) { + if (V->getType()->getScalarSizeInBits() >= 64) + continue; + std::optional CR; + if (const auto *Arg = dyn_cast(V)) { + CR = Arg->getRange(); + } else if (const auto *CB = dyn_cast(V)) { + CR = CB->getRange(); + } else if (const auto *I = dyn_cast(V)) { + if (auto *Range = I->getMetadata(LLVMContext::MD_range)) + CR = getConstantRangeFromMetadata(*Range); + } + if (CR) { + if (R.IsSigned) { + int64_t MaxVal = CR->getSignedMax().getSExtValue(); + int64_t MinVal = CR->getSignedMin().getSExtValue(); + if (MaxVal != MaxConstraintValue) { + ConstraintTy VarPos( + SmallVector(Value2Index.size() + 1, 0), false, + false, false); + VarPos.Coefficients[0] = MaxVal; + VarPos.Coefficients[Value2Index[V]] = 1; + CSToUse.addVariableRow(VarPos.Coefficients); + DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned, + SmallVector()); + } + if (MinVal != MinSignedConstraintValue) { + ConstraintTy VarPos( + SmallVector(Value2Index.size() + 1, 0), false, + false, false); + VarPos.Coefficients[0] = MinVal; + VarPos.Coefficients[Value2Index[V]] = -1; + CSToUse.addVariableRow(VarPos.Coefficients); + DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned, + SmallVector()); + } + } else { + uint64_t MaxVal = CR->getUnsignedMax().getZExtValue(); + uint64_t MinVal = CR->getUnsignedMin().getZExtValue(); + if (MaxVal < static_cast(MaxConstraintValue)) { + ConstraintTy VarPos( + SmallVector(Value2Index.size() + 1, 0), false, + false, false); + VarPos.Coefficients[0] = MaxVal; + VarPos.Coefficients[Value2Index[V]] = 1; + CSToUse.addVariableRow(VarPos.Coefficients); + DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned, + SmallVector()); + } + MinVal = + std::min(MinVal, static_cast(MaxConstraintValue - 1)); + ConstraintTy VarPos( + SmallVector(Value2Index.size() + 1, 0), false, false, + false); + VarPos.Coefficients[0] = MinVal; + VarPos.Coefficients[Value2Index[V]] = -1; + CSToUse.addVariableRow(VarPos.Coefficients); + DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned, + SmallVector()); + } + } else if (!R.IsSigned) { ConstraintTy VarPos(SmallVector(Value2Index.size() + 1, 0), false, false, false); VarPos.Coefficients[Value2Index[V]] = -1; From 37f36235a4e42979e47a7310ef2551906082a410 Mon Sep 17 00:00:00 2001 From: Yingwei Zheng Date: Tue, 3 Dec 2024 19:54:02 +0800 Subject: [PATCH 2/2] [ConstraintElim] Fix miscompilation --- llvm/lib/Transforms/Scalar/ConstraintElimination.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp index b41255f1dbe7c6..dd27ca468f64de 100644 --- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp +++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp @@ -1619,8 +1619,8 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B, int64_t MinVal = CR->getSignedMin().getSExtValue(); if (MaxVal != MaxConstraintValue) { ConstraintTy VarPos( - SmallVector(Value2Index.size() + 1, 0), false, - false, false); + SmallVector(Value2Index.size() + 1, 0), true, false, + false); VarPos.Coefficients[0] = MaxVal; VarPos.Coefficients[Value2Index[V]] = 1; CSToUse.addVariableRow(VarPos.Coefficients); @@ -1629,9 +1629,9 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B, } if (MinVal != MinSignedConstraintValue) { ConstraintTy VarPos( - SmallVector(Value2Index.size() + 1, 0), false, - false, false); - VarPos.Coefficients[0] = MinVal; + SmallVector(Value2Index.size() + 1, 0), true, false, + false); + VarPos.Coefficients[0] = -MinVal; VarPos.Coefficients[Value2Index[V]] = -1; CSToUse.addVariableRow(VarPos.Coefficients); DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned, @@ -1655,7 +1655,7 @@ void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B, ConstraintTy VarPos( SmallVector(Value2Index.size() + 1, 0), false, false, false); - VarPos.Coefficients[0] = MinVal; + VarPos.Coefficients[0] = -MinVal; VarPos.Coefficients[Value2Index[V]] = -1; CSToUse.addVariableRow(VarPos.Coefficients); DFSInStack.emplace_back(NumIn, NumOut, R.IsSigned,