This is the first part of fixing bug 24848 https://llvm.org/bugs/show_bug.cgi?id=24848.
When range metadata is provided, it should be used to constant fold comparisons with constant values.
Differential D12988
[Bug 24848] Use range metadata to constant fold comparisons with constant values Authored by • chenli on Sep 18 2015, 3:44 PM.
Details This is the first part of fixing bug 24848 https://llvm.org/bugs/show_bug.cgi?id=24848. When range metadata is provided, it should be used to constant fold comparisons with constant values.
Diff Detail Event Timeline
Comment Actions I think this looks ready to go in. @hfinkel WDYT?
Comment Actions LGTM too. There is obviously more than can be done here (where we propagate ranges through expressions), but that likely belongs in LVI. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
I'm still not very happy with the interface here -- I'd rather have this function do *just* the metadata parsing; and have the dyn_cast<Instruction>(..) logic in its caller. I'd also rename this GetConstantRangeFromMetadata (and not repeat "Range" in the CR bit and RangeMetadata):
static ConstantRange GetConstantRangeFromMetadata(MDNode *RangeMD, unsigned BitWidth) { ... }and in its caller
if (auto *I = dyn_cast<Instruction>(Val)) if (auto *Ranges = I->getMetadata(LLVMContext::MD_range)) LHS_CR.intersectWith(GetConstantRangeFromMetadata(Ranges, LHS_BitWidth));