InstCombine: Propagate profile metadata in floating point clamp and swap canonicalization
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 39fcb70..16ae4ec 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -4430,6 +4430,9 @@
           FMF.setNoInfs(true);
         Value *NewSel =
             Builder.CreateSelectFMF(NewCond, FalseVal, TrueVal, FMF);
+        if (!ProfcheckDisableMetadataFixes)
+          if (auto *NewSelI = dyn_cast<Instruction>(NewSel))
+            NewSelI->copyMetadata(SI);
         return replaceInstUsesWith(SI, NewSel);
       }
     }
@@ -4634,10 +4637,22 @@
         if (CmpInst::isIntPredicate(MinMaxPred))
           Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS);
         else
-          Cmp = Builder.CreateFCmpFMF(MinMaxPred, LHS, RHS,
+        Cmp = Builder.CreateFCmpFMF(MinMaxPred, LHS, RHS,
                                       cast<Instruction>(SI.getCondition()));
 
-        Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
+        Value *NewSI;
+        if (!ProfcheckDisableMetadataFixes) {
+          NewSI =
+              createSelectInstWithUnknownProfile(Cmp, LHS, RHS, SI.getName());
+          auto *NewInst = cast<Instruction>(NewSI);
+          NewInst->copyMetadata(SI);
+          if (auto *SIFPOp = dyn_cast<FPMathOperator>(&SI))
+            if (isa<FPMathOperator>(NewInst))
+              NewInst->setFastMathFlags(SIFPOp->getFastMathFlags());
+          Builder.Insert(NewInst);
+        } else {
+          NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
+        }
         if (!IsCastNeeded)
           return replaceInstUsesWith(SI, NewSI);
 
diff --git a/llvm/test/Transforms/InstCombine/clamp-to-minmax.ll b/llvm/test/Transforms/InstCombine/clamp-to-minmax.ll
index 0ccaa9c..3a1e460 100644
--- a/llvm/test/Transforms/InstCombine/clamp-to-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/clamp-to-minmax.ll
@@ -1,20 +1,20 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals smart
 ; RUN: opt < %s -passes=instcombine -S | FileCheck %s
 ; RUN: opt < %s -passes=instcombine -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S | FileCheck %s
 
 ; (X < C1) ? C1 : MIN(X, C2)
-define float @clamp_float_fast_ordered_strict_maxmin(float %x) {
+define float @clamp_float_fast_ordered_strict_maxmin(float %x) !prof !0 {
 ; CHECK-LABEL: @clamp_float_fast_ordered_strict_maxmin(
 ; CHECK-NEXT:    [[CMP2:%.*]] = fcmp fast olt float [[X:%.*]], 2.550000e+02
-; CHECK-NEXT:    [[MIN:%.*]] = select i1 [[CMP2]], float [[X]], float 2.550000e+02
+; CHECK-NEXT:    [[MIN:%.*]] = select i1 [[CMP2]], float [[X]], float 2.550000e+02, !prof [[PROF1:![0-9]+]]
 ; CHECK-NEXT:    [[DOTINV:%.*]] = fcmp fast oge float [[MIN]], 1.000000e+00
-; CHECK-NEXT:    [[R1:%.*]] = select nnan ninf i1 [[DOTINV]], float [[MIN]], float 1.000000e+00
+; CHECK-NEXT:    [[R1:%.*]] = select nnan ninf i1 [[DOTINV]], float [[MIN]], float 1.000000e+00, !prof [[PROF1]]
 ; CHECK-NEXT:    ret float [[R1]]
 ;
   %cmp2 = fcmp fast olt float %x, 255.0
-  %min = select i1 %cmp2, float %x, float 255.0
+  %min = select i1 %cmp2, float %x, float 255.0, !prof !1
   %cmp1 = fcmp fast olt float %x, 1.0
-  %r = select i1 %cmp1, float 1.0, float %min
+  %r = select i1 %cmp1, float 1.0, float %min, !prof !1
   ret float %r
 }
 
@@ -590,3 +590,8 @@
   %r = select <2 x i1> %lo_cmp, <2 x float> <float 1.0, float 1.0>, <2 x float> %f_min
   ret <2 x float> %r
 }
+!0 = !{!"function_entry_count", i64 1000}
+!1 = !{!"branch_weights", i32 10, i32 20}
+;.
+; CHECK: [[PROF1]] = !{!"branch_weights", i32 10, i32 20}
+;.
diff --git a/llvm/utils/profcheck-xfail.txt b/llvm/utils/profcheck-xfail.txt
index 45dee8a..8cf70ff 100644
--- a/llvm/utils/profcheck-xfail.txt
+++ b/llvm/utils/profcheck-xfail.txt
@@ -215,7 +215,9 @@
 Transforms/InstCombine/binop-select-cast-of-select-cond.ll
 Transforms/InstCombine/bit-checks.ll
 Transforms/InstCombine/canonicalize-clamp-like-pattern-between-zero-and-positive-threshold.ll
-Transforms/InstCombine/clamp-to-minmax.ll
+Transforms/InstCombine/cttz.ll
+Transforms/InstCombine/div.ll
+Transforms/InstCombine/div-shift.ll
 Transforms/InstCombine/fabs.ll
 Transforms/InstCombine/fcmp-select.ll
 Transforms/InstCombine/ffs-1.ll