[clang] 86bc458 - Use std::clamp (NFC)

Kazu Hirata via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 27 09:53:36 PDT 2022


Author: Kazu Hirata
Date: 2022-08-27T09:53:13-07:00
New Revision: 86bc4587e1fdb7b1b90eadc138619f5e3f2dd6fd

URL: https://github.com/llvm/llvm-project/commit/86bc4587e1fdb7b1b90eadc138619f5e3f2dd6fd
DIFF: https://github.com/llvm/llvm-project/commit/86bc4587e1fdb7b1b90eadc138619f5e3f2dd6fd.diff

LOG: Use std::clamp (NFC)

This patch replaces clamp idioms with std::clamp where the range is
obviously valid from the source code (that is, low <= high) to avoid
introducing undefined behavior.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGBuiltin.cpp
    clang/lib/CodeGen/TargetInfo.cpp
    llvm/tools/llvm-xray/xray-color-helper.cpp
    llvm/tools/llvm-xray/xray-graph-diff.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 63e293f26376..f2d0b20c00c4 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -60,11 +60,6 @@ using namespace clang;
 using namespace CodeGen;
 using namespace llvm;
 
-static
-int64_t clamp(int64_t Value, int64_t Low, int64_t High) {
-  return std::min(High, std::max(Low, Value));
-}
-
 static void initializeAlloca(CodeGenFunction &CGF, AllocaInst *AI, Value *Size,
                              Align AlignmentInBytes) {
   ConstantInt *Byte;
@@ -16024,7 +16019,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
     assert(ArgCI &&
            "Third arg to xxinsertw intrinsic must be constant integer");
     const int64_t MaxIndex = 12;
-    int64_t Index = clamp(ArgCI->getSExtValue(), 0, MaxIndex);
+    int64_t Index = std::clamp(ArgCI->getSExtValue(), (int64_t)0, MaxIndex);
 
     // The builtin semantics don't exactly match the xxinsertw instructions
     // semantics (which ppc_vsx_xxinsertw follows). The builtin extracts the
@@ -16066,7 +16061,7 @@ Value *CodeGenFunction::EmitPPCBuiltinExpr(unsigned BuiltinID,
     assert(ArgCI &&
            "Second Arg to xxextractuw intrinsic must be a constant integer!");
     const int64_t MaxIndex = 12;
-    int64_t Index = clamp(ArgCI->getSExtValue(), 0, MaxIndex);
+    int64_t Index = std::clamp(ArgCI->getSExtValue(), (int64_t)0, MaxIndex);
 
     if (getTarget().isLittleEndian()) {
       // Reverse the index.

diff  --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp
index 42d5a856edf3..370614f65e5b 100644
--- a/clang/lib/CodeGen/TargetInfo.cpp
+++ b/clang/lib/CodeGen/TargetInfo.cpp
@@ -6674,7 +6674,7 @@ ABIArgInfo ARMABIInfo::classifyArgumentType(QualType Ty, bool isVariadic,
   if (getABIKind() == ARMABIInfo::AAPCS_VFP ||
       getABIKind() == ARMABIInfo::AAPCS) {
     TyAlign = getContext().getTypeUnadjustedAlignInChars(Ty).getQuantity();
-    ABIAlign = std::min(std::max(TyAlign, (uint64_t)4), (uint64_t)8);
+    ABIAlign = std::clamp(TyAlign, (uint64_t)4, (uint64_t)8);
   } else {
     TyAlign = getContext().getTypeAlignInChars(Ty).getQuantity();
   }

diff  --git a/llvm/tools/llvm-xray/xray-color-helper.cpp b/llvm/tools/llvm-xray/xray-color-helper.cpp
index b2ed63881bdc..3dd5143ca8d0 100644
--- a/llvm/tools/llvm-xray/xray-color-helper.cpp
+++ b/llvm/tools/llvm-xray/xray-color-helper.cpp
@@ -111,7 +111,7 @@ convertToHSV(const std::tuple<uint8_t, uint8_t, uint8_t> &Color) {
 // Takes a double precision number, clips it between 0 and 1 and then converts
 // that to an integer between 0x00 and 0xFF with proxpper rounding.
 static uint8_t unitIntervalTo8BitChar(double B) {
-  double n = std::max(std::min(B, 1.0), 0.0);
+  double n = std::clamp(B, 0.0, 1.0);
   return static_cast<uint8_t>(255 * n + 0.5);
 }
 

diff  --git a/llvm/tools/llvm-xray/xray-graph-
diff .cpp b/llvm/tools/llvm-xray/xray-graph-
diff .cpp
index bcadade86bb5..8ccfd304ffe5 100644
--- a/llvm/tools/llvm-xray/xray-graph-
diff .cpp
+++ b/llvm/tools/llvm-xray/xray-graph-
diff .cpp
@@ -264,7 +264,7 @@ static std::string getColor(const GraphDiffRenderer::GraphT::EdgeValueType &E,
   const auto &RightStat = EdgeAttr.CorrEdgePtr[1]->second.S;
 
   double RelDiff = statRelDiff(LeftStat, RightStat, T);
-  double CappedRelDiff = std::min(1.0, std::max(-1.0, RelDiff));
+  double CappedRelDiff = std::clamp(RelDiff, -1.0, 1.0);
 
   return H.getColorString(CappedRelDiff);
 }
@@ -285,7 +285,7 @@ static std::string getColor(const GraphDiffRenderer::GraphT::VertexValueType &V,
   const auto &RightStat = VertexAttr.CorrVertexPtr[1]->second.S;
 
   double RelDiff = statRelDiff(LeftStat, RightStat, T);
-  double CappedRelDiff = std::min(1.0, std::max(-1.0, RelDiff));
+  double CappedRelDiff = std::clamp(RelDiff, -1.0, 1.0);
 
   return H.getColorString(CappedRelDiff);
 }


        


More information about the cfe-commits mailing list