-static ConstantRange GetConstantRangeFromMetadata(MDNode *Ranges, uint32_t BitWidth) {
- const unsigned NumRanges = Ranges->getNumOperands() / 2;
- assert(NumRanges >= 1);
-
- ConstantRange CR(BitWidth, false);
- for (unsigned i = 0; i < NumRanges; ++i) {
- auto *Low =
- mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
- auto *High =
- mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
-
- // Union will merge two ranges to one and potentially introduce a range
- // not covered by the original two ranges. For example, [1, 5) and [8, 10)
- // will become [1, 10). In this case, we can not fold comparison between
- // constant 6 and a value of the above ranges. In practice, most values
- // have only one range, so it might not be worth handling this by
- // introducing additional complexity.
- CR = CR.unionWith(ConstantRange(Low->getValue(), High->getValue()));
- }
-
- return CR;
-}
-