LSR must sometimes sign-extend before generating double constants.
authorAndrew Trick <atrick@apple.com>
Thu, 21 Jul 2011 01:05:01 +0000 (01:05 +0000)
committerAndrew Trick <atrick@apple.com>
Thu, 21 Jul 2011 01:05:01 +0000 (01:05 +0000)
rdar://9786536

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@135650 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/LoopStrengthReduce.cpp
test/Transforms/LoopStrengthReduce/2011-07-20-DoubleIV.ll

index 4598df0c428bfe953acf9954e73989b300ada874..92864817aee28d8ba42dbaf3f2af9c5489e355db 100644 (file)
@@ -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<UIToFPInst>(CandidateUI->getUser()))
+    if (UIToFPInst *UCast = dyn_cast<UIToFPInst>(CandidateUI->getUser())) {
+      IsSigned = false;
       DestTy = UCast->getDestTy();
-    else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser()))
+    }
+    else if (SIToFPInst *SCast = dyn_cast<SIToFPInst>(CandidateUI->getUser())) {
+      IsSigned = true;
       DestTy = SCast->getDestTy();
+    }
     if (!DestTy) continue;
 
     if (TLI) {
@@ -1474,7 +1479,9 @@ void LSRInstance::OptimizeShadowIV() {
 
     ConstantInt *Init = dyn_cast<ConstantInt>(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<BinaryOperator>(PH->getIncomingValue(Latch));
index 3ceeeb658d1410439b0d431710feda7b9d3a6443..aecb06cfb87de4dbd3986a345b6da600ebe902fa 100644 (file)
@@ -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