[llvm-commits] [llvm] r47287 - in /llvm/trunk: lib/Transforms/Scalar/InstructionCombining.cpp test/Transforms/InstCombine/2008-02-16-SDivOverflow.ll test/Transforms/InstCombine/2008-02-16-SDivOverflow2.ll

Chris Lattner clattner at apple.com
Mon Feb 18 21:24:45 PST 2008


> URL: http://llvm.org/viewvc/llvm-project?rev=47287&view=rev
> Log:
> Correctly fold divide-by-constant, even when faced with overflow.

Nice.

> +/// MultiplyOverflows - True if the multiply can not be expressed  
> in an int
> +/// this size.
> +static bool MultiplyOverflows(ConstantInt *C1, ConstantInt *C2,  
> bool sign) {
> +  uint32_t W = C1->getBitWidth();
> +  APInt LHSExt = C1->getValue(), RHSExt = C2->getValue();
> +  if (sign) {
> +    LHSExt.sext(W * 2);
> +    RHSExt.sext(W * 2);
> +  } else {
> +    LHSExt.zext(W * 2);
> +    RHSExt.zext(W * 2);
> +  }
> +
> +  APInt MulExt = LHSExt * RHSExt;
> +
> +  if (sign) {
> +    APInt Min = APInt::getSignedMinValue(W).sext(W * 2);
> +    APInt Max = APInt::getSignedMaxValue(W).sext(W * 2);
> +    return MulExt.slt(Min) || MulExt.sgt(Max);
> +  } else
> +    return MulExt.ugt(APInt::getLowBitsSet(W * 2, W));
> +}

It sure would be nice to have an APSInt::mul(x,y, overflow) method  
that captured this :)

-Chris




More information about the llvm-commits mailing list