[llvm] 33ac23e - [Float2Int] Avoid unnecessary lamdbas (NFC)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 31 07:13:23 PDT 2022


Author: Nikita Popov
Date: 2022-03-31T16:13:13+02:00
New Revision: 33ac23e7cf8fa8422550f83ff1734f941f19dc6c

URL: https://github.com/llvm/llvm-project/commit/33ac23e7cf8fa8422550f83ff1734f941f19dc6c
DIFF: https://github.com/llvm/llvm-project/commit/33ac23e7cf8fa8422550f83ff1734f941f19dc6c.diff

LOG: [Float2Int] Avoid unnecessary lamdbas (NFC)

Instead of first creating a lambda for calculating the range,
then collecting the ranges for the operands, and then calling the
lambda on those ranges, we can first calculate the operand ranges
and then calculate the result directly in the switch.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/Float2Int.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/Float2Int.cpp b/llvm/lib/Transforms/Scalar/Float2Int.cpp
index bf87311143289..6b04d2ce07324 100644
--- a/llvm/lib/Transforms/Scalar/Float2Int.cpp
+++ b/llvm/lib/Transforms/Scalar/Float2Int.cpp
@@ -237,56 +237,6 @@ void Float2IntPass::walkBackwards() {
 
 // Calculate result range from operand ranges
 ConstantRange Float2IntPass::calcRange(Instruction *I) {
-  std::function<ConstantRange(ArrayRef<ConstantRange>)> Op;
-  switch (I->getOpcode()) {
-    // FIXME: Handle select and phi nodes.
-  default:
-  case Instruction::UIToFP:
-  case Instruction::SIToFP:
-    llvm_unreachable("Should have been handled in walkForwards!");
-
-  case Instruction::FNeg:
-    Op = [](ArrayRef<ConstantRange> Ops) {
-      assert(Ops.size() == 1 && "FNeg is a unary operator!");
-      unsigned Size = Ops[0].getBitWidth();
-      auto Zero = ConstantRange(APInt::getZero(Size));
-      return Zero.sub(Ops[0]);
-    };
-    break;
-
-  case Instruction::FAdd:
-  case Instruction::FSub:
-  case Instruction::FMul:
-    Op = [I](ArrayRef<ConstantRange> Ops) {
-      assert(Ops.size() == 2 && "its a binary operator!");
-      auto BinOp = (Instruction::BinaryOps) I->getOpcode();
-      return Ops[0].binaryOp(BinOp, Ops[1]);
-    };
-    break;
-
-  //
-  // Root-only instructions - we'll only see these if they're the
-  //                          first node in a walk.
-  //
-  case Instruction::FPToUI:
-  case Instruction::FPToSI:
-    Op = [I](ArrayRef<ConstantRange> Ops) {
-      assert(Ops.size() == 1 && "FPTo[US]I is a unary operator!");
-      // Note: We're ignoring the casts output size here as that's what the
-      // caller expects.
-      auto CastOp = (Instruction::CastOps)I->getOpcode();
-      return Ops[0].castOp(CastOp, MaxIntegerBW+1);
-    };
-    break;
-
-  case Instruction::FCmp:
-    Op = [](ArrayRef<ConstantRange> Ops) {
-      assert(Ops.size() == 2 && "FCmp is a binary operator!");
-      return Ops[0].unionWith(Ops[1]);
-    };
-    break;
-  }
-
   SmallVector<ConstantRange, 4> OpRanges;
   for (Value *O : I->operands()) {
     if (Instruction *OI = dyn_cast<Instruction>(O)) {
@@ -330,8 +280,45 @@ ConstantRange Float2IntPass::calcRange(Instruction *I) {
     }
   }
 
-  // Reduce the operands' ranges to a single range.
-  return Op(OpRanges);
+  switch (I->getOpcode()) {
+  // FIXME: Handle select and phi nodes.
+  default:
+  case Instruction::UIToFP:
+  case Instruction::SIToFP:
+    llvm_unreachable("Should have been handled in walkForwards!");
+
+  case Instruction::FNeg: {
+    assert(OpRanges.size() == 1 && "FNeg is a unary operator!");
+    unsigned Size = OpRanges[0].getBitWidth();
+    auto Zero = ConstantRange(APInt::getZero(Size));
+    return Zero.sub(OpRanges[0]);
+  }
+
+  case Instruction::FAdd:
+  case Instruction::FSub:
+  case Instruction::FMul: {
+    assert(OpRanges.size() == 2 && "its a binary operator!");
+    auto BinOp = (Instruction::BinaryOps) I->getOpcode();
+    return OpRanges[0].binaryOp(BinOp, OpRanges[1]);
+  }
+
+  //
+  // Root-only instructions - we'll only see these if they're the
+  //                          first node in a walk.
+  //
+  case Instruction::FPToUI:
+  case Instruction::FPToSI: {
+    assert(OpRanges.size() == 1 && "FPTo[US]I is a unary operator!");
+    // Note: We're ignoring the casts output size here as that's what the
+    // caller expects.
+    auto CastOp = (Instruction::CastOps)I->getOpcode();
+    return OpRanges[0].castOp(CastOp, MaxIntegerBW+1);
+  }
+
+  case Instruction::FCmp:
+    assert(OpRanges.size() == 2 && "FCmp is a binary operator!");
+    return OpRanges[0].unionWith(OpRanges[1]);
+  }
 }
 
 // Walk forwards down the list of seen instructions, so we visit defs before


        


More information about the llvm-commits mailing list