llvm-6502/test/Transforms/IndVarSimplify/2011-09-10-widen-nsw.ll
Sanjoy Das 31123d4529 This patch teaches IndVarSimplify to add nuw and nsw to certain kinds
of operations that provably don't overflow. For example, we can prove
%civ.inc below does not sign-overflow. With this change,
IndVarSimplify changes %civ.inc to an add nsw.

  define i32 @foo(i32* %array, i32* %length_ptr, i32 %init) {
   entry:
    %length = load i32* %length_ptr, !range !0
    %len.sub.1 = sub i32 %length, 1
    %upper = icmp slt i32 %init, %len.sub.1
    br i1 %upper, label %loop, label %exit
  
   loop:
    %civ = phi i32 [ %init, %entry ], [ %civ.inc, %latch ]
    %civ.inc = add i32 %civ, 1
    %cmp = icmp slt i32 %civ.inc, %length
    br i1 %cmp, label %latch, label %break
  
   latch:
    store i32 0, i32* %array
    %check = icmp slt i32 %civ.inc, %len.sub.1
    br i1 %check, label %loop, label %break
  
   break:
    ret i32 %civ.inc
  
   exit:
    ret i32 42
  }

Differential Revision: http://reviews.llvm.org/D6748



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225282 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-06 19:02:56 +00:00

44 lines
1.5 KiB
LLVM

; RUN: opt < %s -indvars -S | FileCheck %s
; Test WidenIV::GetExtendedOperandRecurrence.
; %add, %sub and %mul should be extended to i64 because it is nsw, even though its
; sext cannot be hoisted outside the loop.
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
define void @test() nounwind {
entry:
br i1 undef, label %for.body11, label %for.end285
for.body11: ; preds = %entry
%shl = shl i32 1, 1
%shl132 = shl i32 %shl, 1
br label %for.body153
for.body153: ; preds = %for.body153, %for.body11
br i1 undef, label %for.body170, label %for.body153
; CHECK: add nuw nsw i64 %indvars.iv, 1
; CHECK: sub nsw i64 %indvars.iv, 2
; CHECK: sub nsw i64 4, %indvars.iv
; CHECK: mul nsw i64 %indvars.iv, 8
for.body170: ; preds = %for.body170, %for.body153
%i2.19 = phi i32 [ %add249, %for.body170 ], [ 0, %for.body153 ]
%add = add nsw i32 %i2.19, 1
%add.idxprom = sext i32 %add to i64
%sub = sub nsw i32 %i2.19, 2
%sub.idxprom = sext i32 %sub to i64
%sub.neg = sub nsw i32 4, %i2.19
%sub.neg.idxprom = sext i32 %sub.neg to i64
%mul = mul nsw i32 %i2.19, 8
%mul.idxprom = sext i32 %mul to i64
%add249 = add nsw i32 %i2.19, %shl132
br label %for.body170
for.end285: ; preds = %entry
ret void
}