Home | History | Annotate | Download | only in InstCombine
      1 ; Test that the abs library call simplifier works correctly.
      2 ;
      3 ; RUN: opt < %s -instcombine -S | FileCheck %s
      4 
      5 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
      6 
      7 declare i32 @abs(i32)
      8 declare i64 @labs(i64)
      9 declare i64 @llabs(i64)
     10 
     11 ; Check abs(x) -> x >s -1 ? x : -x.
     12 
     13 define i32 @test_simplify1(i32 %x) {
     14 ; CHECK-LABEL: @test_simplify1(
     15   %ret = call i32 @abs(i32 %x)
     16 ; CHECK-NEXT: [[ISPOS:%[a-z0-9]+]] = icmp sgt i32 %x, -1
     17 ; CHECK-NEXT: [[NEG:%[a-z0-9]+]] = sub i32 0, %x
     18 ; CHECK-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[ISPOS]], i32 %x, i32 [[NEG]]
     19   ret i32 %ret
     20 ; CHECK-NEXT: ret i32 [[RET]]
     21 }
     22 
     23 define i64 @test_simplify2(i64 %x) {
     24 ; CHECK-LABEL: @test_simplify2(
     25   %ret = call i64 @labs(i64 %x)
     26 ; CHECK-NEXT: [[ISPOS:%[a-z0-9]+]] = icmp sgt i64 %x, -1
     27 ; CHECK-NEXT: [[NEG:%[a-z0-9]+]] = sub i64 0, %x
     28 ; CHECK-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[ISPOS]], i64 %x, i64 [[NEG]]
     29   ret i64 %ret
     30 ; CHECK-NEXT: ret i64 [[RET]]
     31 }
     32 
     33 define i64 @test_simplify3(i64 %x) {
     34 ; CHECK-LABEL: @test_simplify3(
     35   %ret = call i64 @llabs(i64 %x)
     36 ; CHECK-NEXT: [[ISPOS:%[a-z0-9]+]] = icmp sgt i64 %x, -1
     37 ; CHECK-NEXT: [[NEG:%[a-z0-9]+]] = sub i64 0, %x
     38 ; CHECK-NEXT: [[RET:%[a-z0-9]+]] = select i1 [[ISPOS]], i64 %x, i64 [[NEG]]
     39   ret i64 %ret
     40 ; CHECK-NEXT: ret i64 [[RET]]
     41 }
     42