[llvm] r222265 - InstCombine: Fix another infinite loop caused by visitFPTrunc

David Majnemer david.majnemer at gmail.com
Thu Dec 11 18:43:45 PST 2014


On Thu, Dec 11, 2014 at 2:13 PM, Steven Wu <stevenwu at apple.com> wrote:

> Hi David
>
> Here is another infinite loop fall out from this patch. Here is a test
> case:
>  define float @test(float %arg) {
>  entry:
>    %0 = fpext float %arg to double
>    %1 = frem double %0, 0xBAD
>    %2 = fptrunc double %1 to float
>    ret float %2
>  }
>
> The test case has exactly the same pattern as the instruction generated
> from instcombine which suggests probably it shouldn’t generate a truncation
> of same type? Maybe even a smaller type?
>

I think that makes sense.


> Here is a patch. Let me know what you think.
>

LGTM if you include a test case.


>
> diff --git a/lib/Transforms/InstCombine/InstCombineCasts.cpp
> b/lib/Transforms/InstCombine/InstCombineCasts.cpp
> index aba77bb..357bf24 100644
> --- a/lib/Transforms/InstCombine/InstCombineCasts.cpp
> +++ b/lib/Transforms/InstCombine/InstCombineCasts.cpp
> @@ -1269,16 +1269,19 @@ Instruction
> *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
>          // type of OpI doesn't enter into things at all.  We simply
> evaluate
>          // in whichever source type is larger, then convert to the
>          // destination type.
> -        if (LHSWidth < SrcWidth)
> -          LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->getType());
> -        else if (RHSWidth <= SrcWidth)
> -          RHSOrig = Builder->CreateFPExt(RHSOrig, LHSOrig->getType());
> -        if (LHSOrig != OpI->getOperand(0) || RHSOrig !=
> OpI->getOperand(1)) {
> -          Value *ExactResult = Builder->CreateFRem(LHSOrig, RHSOrig);
> -          if (Instruction *RI = dyn_cast<Instruction>(ExactResult))
> -            RI->copyFastMathFlags(OpI);
> -          return CastInst::CreateFPCast(ExactResult, CI.getType());
> +        if (SrcWidth != OpWidth) {
> +          if (LHSWidth < SrcWidth)
> +            LHSOrig = Builder->CreateFPExt(LHSOrig, RHSOrig->getType());
> +          else if (RHSWidth <= SrcWidth)
> +            RHSOrig = Builder->CreateFPExt(RHSOrig, LHSOrig->getType());
> +          if (LHSOrig != OpI->getOperand(0) || RHSOrig !=
> OpI->getOperand(1)) {
> +            Value *ExactResult = Builder->CreateFRem(LHSOrig, RHSOrig);
> +            if (Instruction *RI = dyn_cast<Instruction>(ExactResult))
> +              RI->copyFastMathFlags(OpI);
> +            return CastInst::CreateFPCast(ExactResult, CI.getType());
> +          }
>          }
> +        break;
>      }
>
> Thanks
>
> Steven
>
> On Nov 18, 2014, at 2:06 PM, David Majnemer <david.majnemer at gmail.com>
> wrote:
>
> majnemer
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141211/0d3312fd/attachment.html>


More information about the llvm-commits mailing list