Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -2134,15 +2134,11 @@ if (!C) continue; - // Ignore negative constants, as the code below doesn't handle them - // correctly. TODO: Remove this restriction. - if (!C->getValue().isStrictlyPositive()) continue; - /* Add new PHINode. */ PHINode *NewPH = PHINode::Create(DestTy, 2, "IV.S.", PH); /* create new increment. '++d' in above example. */ - Constant *CFP = ConstantFP::get(DestTy, C->getZExtValue()); + Constant *CFP = ConstantFP::get(DestTy, C->getSExtValue()); BinaryOperator *NewIncr = BinaryOperator::Create(Incr->getOpcode() == Instruction::Add ? Instruction::FAdd : Instruction::FSub, Index: llvm/test/CodeGen/X86/negative-stride-fptosi-user.ll =================================================================== --- llvm/test/CodeGen/X86/negative-stride-fptosi-user.ll +++ llvm/test/CodeGen/X86/negative-stride-fptosi-user.ll @@ -9,27 +9,28 @@ define void @foo(i32 %N) nounwind { ; CHECK-LABEL: foo: ; CHECK: # %bb.0: # %entry -; CHECK-NEXT: pushq %rbp ; CHECK-NEXT: pushq %rbx -; CHECK-NEXT: pushq %rax +; CHECK-NEXT: subq $16, %rsp ; CHECK-NEXT: testl %edi, %edi ; CHECK-NEXT: jns .LBB0_3 ; CHECK-NEXT: # %bb.1: # %bb.preheader ; CHECK-NEXT: movl %edi, %ebx -; CHECK-NEXT: xorl %ebp, %ebp +; CHECK-NEXT: xorpd %xmm0, %xmm0 ; CHECK-NEXT: .p2align 4, 0x90 ; CHECK-NEXT: .LBB0_2: # %bb ; CHECK-NEXT: # =>This Inner Loop Header: Depth=1 -; CHECK-NEXT: xorps %xmm0, %xmm0 -; CHECK-NEXT: cvtsi2sd %ebp, %xmm0 +; CHECK-NEXT: movsd %xmm0, 8(%rsp) # 8-byte Spill +; CHECK-NEXT: movsd 8(%rsp), %xmm0 # 8-byte Reload +; CHECK-NEXT: # xmm0 = mem[0],zero ; CHECK-NEXT: callq bar -; CHECK-NEXT: decl %ebp -; CHECK-NEXT: cmpl %ebp, %ebx +; CHECK-NEXT: movsd 8(%rsp), %xmm0 # 8-byte Reload +; CHECK-NEXT: # xmm0 = mem[0],zero +; CHECK-NEXT: addsd .LCPI0_0(%rip), %xmm0 +; CHECK-NEXT: incl %ebx ; CHECK-NEXT: jne .LBB0_2 ; CHECK-NEXT: .LBB0_3: # %return -; CHECK-NEXT: addq $8, %rsp +; CHECK-NEXT: addq $16, %rsp ; CHECK-NEXT: popq %rbx -; CHECK-NEXT: popq %rbp ; CHECK-NEXT: retq entry: %0 = icmp slt i32 %N, 0 ; [#uses=1] Index: llvm/test/Transforms/LoopStrengthReduce/X86/2008-08-14-ShadowIV.ll =================================================================== --- llvm/test/Transforms/LoopStrengthReduce/X86/2008-08-14-ShadowIV.ll +++ llvm/test/Transforms/LoopStrengthReduce/X86/2008-08-14-ShadowIV.ll @@ -208,6 +208,42 @@ ret i32 %accum.next } +; void negative_step(int n) { +; for (int i = 10; i > n; i += -1) +; foo((double)i); +; } +; +; Transformed to: +; +; void negative_step(int n) { +; for (int i = 10, double d = 10.0; i > n; i += -1, d += -1.0) +; foo(d); +; } + +define void @negative_step(i32 %n) { +; CHECK-LABEL: negative_step( +; CHECK-NOT: sitofp +entry: + br label %while.header + +while.header: + %i = phi i32 [ 10, %entry ], [ %i.next, %latch ] + %cond = icmp sgt i32 %i, %n + br i1 %cond, label %body, label %exit + +body: + %cast = sitofp i32 %i to double + call void @foo(double %cast) + br label %latch + +latch: + %i.next = add nsw i32 %i, -1 + br label %while.header + +exit: + ret void +} + declare void @bar(i32) declare void @foo(double)