[PATCH] D12988: [Bug 24848] Use range metadata to constant fold comparisons with constant values

Sanjoy Das via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 18 18:30:01 PDT 2015


sanjoy requested changes to this revision.
This revision now requires changes to proceed.

================
Comment at: lib/Analysis/InstructionSimplify.cpp:2392
@@ +2391,3 @@
+          ConstantInt *LowerBound =
+              mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
+          ConstantInt *UpperBound =
----------------
I don't think this is correct.  When an instruction has a `!range` like `[a,b) [c,d)`, the set of possible values is the union of all of these ranges.

For instance, I think you'll constant fold `x slt 10` to `true` here if the range of `x` is `[0,9) [100,200)`, even though `x` can be `120`.

I also don't think `FirstRangeContainsSecond` is a good abstraction, and its name is misleading.

I'd structure this bit of code as:

instead of

```
    if (Lower != Upper) {
      ConstantRange LHS_CR = ConstantRange(Lower, Upper);

      if (Value *RetVal =
          FirstRangeContainsSecond(RHS_CR, LHS_CR, RHS->getContext())) {
        return RetVal;
      }
    }
```

```
ConstantRange LHS_CR = Lower == Upper ? ConstantRange::getFullSet() : ConstantRange(Lower, Upper);
if (Instruction has !range MD) {
  ConstantRange CR = NullSet;
  for (auto Range : Inst->Ranges) {
    CR = CR.union(RangeMD)
  }
  LHS_CR = LHS_CR.intersect(CR);
}

if (!LHS_CR.isFullSet()) {
  if (RHS_CR.contains(LHS_CR))
  ... // the code as it is today
}
```


http://reviews.llvm.org/D12988





More information about the llvm-commits mailing list