From c2c988e5e0b15408f790c96fd7ad2d86a6a93a08 Mon Sep 17 00:00:00 2001 From: Andrew Trick Date: Thu, 21 Jul 2011 01:05:01 +0000 Subject: [PATCH] LSR must sometimes sign-extend before generating double constants. rdar://9786536 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135650 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/LoopStrengthReduce.cpp | 13 +++++++++--- .../LoopStrengthReduce/2011-07-20-DoubleIV.ll | 21 ++++++++++++++++++- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp index 4598df0c428..92864817aee 100644 --- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -1427,6 +1427,7 @@ void LSRInstance::OptimizeShadowIV() { ++UI; Instruction *ShadowUse = CandidateUI->getUser(); Type *DestTy = NULL; + bool IsSigned = false; /* If shadow use is a int->float cast then insert a second IV to eliminate this cast. @@ -1440,10 +1441,14 @@ void LSRInstance::OptimizeShadowIV() { for (unsigned i = 0; i < n; ++i, ++d) foo(d); */ - if (UIToFPInst *UCast = dyn_cast(CandidateUI->getUser())) + if (UIToFPInst *UCast = dyn_cast(CandidateUI->getUser())) { + IsSigned = false; DestTy = UCast->getDestTy(); - else if (SIToFPInst *SCast = dyn_cast(CandidateUI->getUser())) + } + else if (SIToFPInst *SCast = dyn_cast(CandidateUI->getUser())) { + IsSigned = true; DestTy = SCast->getDestTy(); + } if (!DestTy) continue; if (TLI) { @@ -1474,7 +1479,9 @@ void LSRInstance::OptimizeShadowIV() { ConstantInt *Init = dyn_cast(PH->getIncomingValue(Entry)); if (!Init) continue; - Constant *NewInit = ConstantFP::get(DestTy, Init->getZExtValue()); + Constant *NewInit = ConstantFP::get(DestTy, IsSigned ? + Init->getSExtValue() : + Init->getZExtValue()); BinaryOperator *Incr = dyn_cast(PH->getIncomingValue(Latch)); diff --git a/test/Transforms/LoopStrengthReduce/2011-07-20-DoubleIV.ll b/test/Transforms/LoopStrengthReduce/2011-07-20-DoubleIV.ll index 3ceeeb658d1..aecb06cfb87 100644 --- a/test/Transforms/LoopStrengthReduce/2011-07-20-DoubleIV.ll +++ b/test/Transforms/LoopStrengthReduce/2011-07-20-DoubleIV.ll @@ -15,7 +15,26 @@ entry: loop: %i.01 = phi i32 [ -39, %entry ], [ %inc, %loop ] - %conv7 = sitofp i32 %i.01 to double + %conv = sitofp i32 %i.01 to double + %inc = add nsw i32 %i.01, 1 + br i1 undef, label %loop, label %for.end + +for.end: + unreachable +} + +; Now check that the computed double constant is correct. +; CHECK: @doubleIV +; CHECK: phi double [ 0x43F0000000000000, %entry ] +; CHECK: br +define void @doubleIV() nounwind { +entry: + br label %loop + +loop: + %i.01 = phi i32 [ -39, %entry ], [ %inc, %loop ] + %conv = sitofp i32 %i.01 to double + %div = fdiv double %conv, 4.000000e+01 %inc = add nsw i32 %i.01, 1 br i1 undef, label %loop, label %for.end -- 2.34.1