xref: /llvm-project/llvm/test/Transforms/Reassociate/2002-05-15-SubReassociate.ll (revision 0676acb6fd8d5e22fde16e6ec977ceac08427ab0)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt < %s -passes=reassociate,instcombine,dce -S | FileCheck %s
3
4; With sub reassociation, constant folding can eliminate all of the constants.
5define i32 @test1(i32 %A, i32 %B) {
6; CHECK-LABEL: @test1(
7; CHECK-NEXT:    [[Z:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
8; CHECK-NEXT:    ret i32 [[Z]]
9;
10  %W = add i32 5, %B
11  %X = add i32 -7, %A
12  %Y = sub i32 %X, %W
13  %Z = add i32 %Y, 12
14  ret i32 %Z
15}
16
17; With sub reassociation, constant folding can eliminate the two 12 constants.
18define i32 @test2(i32 %A, i32 %B, i32 %C, i32 %D) {
19; CHECK-LABEL: @test2(
20; CHECK-NEXT:    [[TMP1:%.*]] = add i32 [[B:%.*]], [[A:%.*]]
21; CHECK-NEXT:    [[TMP2:%.*]] = add i32 [[TMP1]], [[C:%.*]]
22; CHECK-NEXT:    [[Q:%.*]] = sub i32 [[D:%.*]], [[TMP2]]
23; CHECK-NEXT:    ret i32 [[Q]]
24;
25  %M = add i32 %A, 12
26  %N = add i32 %M, %B
27  %O = add i32 %N, %C
28  %P = sub i32 %D, %O
29  %Q = add i32 %P, 12
30  ret i32 %Q
31}
32
33