[PATCH] D122817: [Float2Int] Make sure dependent ranges are calculated first (PR54669)

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 1 03:30:17 PDT 2022


nikic updated this revision to Diff 419701.
nikic added a comment.

Add test


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122817/new/

https://reviews.llvm.org/D122817

Files:
  llvm/include/llvm/Transforms/Scalar/Float2Int.h
  llvm/lib/Transforms/Scalar/Float2Int.cpp
  llvm/test/Transforms/Float2Int/pr54669.ll


Index: llvm/test/Transforms/Float2Int/pr54669.ll
===================================================================
--- llvm/test/Transforms/Float2Int/pr54669.ll
+++ llvm/test/Transforms/Float2Int/pr54669.ll
@@ -3,11 +3,9 @@
 
 declare void @use(i32)
 
-; FIXME: Currently being miscompiled.
-
 define i1 @src() {
 ; CHECK-LABEL: @src(
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 2147483647, -1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 -1, -1
 ; CHECK-NEXT:    ret i1 [[CMP]]
 ;
   %add = fadd double 2.000000e+00, -1.000000e+00
Index: llvm/lib/Transforms/Scalar/Float2Int.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/Float2Int.cpp
+++ llvm/lib/Transforms/Scalar/Float2Int.cpp
@@ -235,14 +235,17 @@
   }
 }
 
-// Calculate result range from operand ranges
-ConstantRange Float2IntPass::calcRange(Instruction *I) {
+// Calculate result range from operand ranges.
+// Return None if the range cannot be calculated yet.
+Optional<ConstantRange> Float2IntPass::calcRange(Instruction *I) {
   SmallVector<ConstantRange, 4> OpRanges;
   for (Value *O : I->operands()) {
     if (Instruction *OI = dyn_cast<Instruction>(O)) {
-      assert(SeenInsts.find(OI) != SeenInsts.end() &&
-             "def not seen before use!");
-      OpRanges.push_back(SeenInsts.find(OI)->second);
+      auto OpIt = SeenInsts.find(OI);
+      assert(OpIt != SeenInsts.end() && "def not seen before use!");
+      if (OpIt->second == unknownRange())
+        return None; // Wait until operand range has been calculated.
+      OpRanges.push_back(OpIt->second);
     } else if (ConstantFP *CF = dyn_cast<ConstantFP>(O)) {
       // Work out if the floating point number can be losslessly represented
       // as an integer.
@@ -324,12 +327,19 @@
 // Walk forwards down the list of seen instructions, so we visit defs before
 // uses.
 void Float2IntPass::walkForwards() {
-  for (auto &It : reverse(SeenInsts)) {
-    if (It.second != unknownRange())
-      continue;
+  std::deque<Instruction *> Worklist;
+  for (const auto &Pair : SeenInsts)
+    if (Pair.second == unknownRange())
+      Worklist.push_back(Pair.first);
+
+  while (!Worklist.empty()) {
+    Instruction *I = Worklist.back();
+    Worklist.pop_back();
 
-    Instruction *I = It.first;
-    seen(I, calcRange(I));
+    if (Optional<ConstantRange> Range = calcRange(I))
+      seen(I, *Range);
+    else
+      Worklist.push_front(I); // Reprocess later.
   }
 }
 
Index: llvm/include/llvm/Transforms/Scalar/Float2Int.h
===================================================================
--- llvm/include/llvm/Transforms/Scalar/Float2Int.h
+++ llvm/include/llvm/Transforms/Scalar/Float2Int.h
@@ -25,6 +25,7 @@
 class Function;
 class Instruction;
 class LLVMContext;
+template <typename T> class Optional;
 class Type;
 class Value;
 
@@ -41,7 +42,7 @@
   ConstantRange badRange();
   ConstantRange unknownRange();
   ConstantRange validateRange(ConstantRange R);
-  ConstantRange calcRange(Instruction *I);
+  Optional<ConstantRange> calcRange(Instruction *I);
   void walkBackwards();
   void walkForwards();
   bool validateAndTransform();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122817.419701.patch
Type: text/x-patch
Size: 3149 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220401/5d000fc7/attachment.bin>


More information about the llvm-commits mailing list