107 changes: 107 additions & 0 deletions llvm/test/Transforms/IROutliner/outlining-commutative-fp.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -verify -iroutliner < %s | FileCheck %s

; This test checks that floating point commutative instructions are not treated
; as commutative. Even though an ffadd is technically commutative, the order
; of operands still needs to be enforced since the process of fadding floating
; point values requires the order to be the same.

; We make sure that we outline the identical regions from the first two
; functions, but not the third. this is because the operands are in a different
; order in a floating point instruction in this section.

define void @outline_from_fadd1() {
; CHECK-LABEL: @outline_from_fadd1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca double, align 4
; CHECK-NEXT: [[B:%.*]] = alloca double, align 4
; CHECK-NEXT: [[C:%.*]] = alloca double, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(double* [[A]], double* [[B]], double* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca double, align 4
%b = alloca double, align 4
%c = alloca double, align 4
store double 2.0, double* %a, align 4
store double 3.0, double* %b, align 4
store double 4.0, double* %c, align 4
%al = load double, double* %a
%bl = load double, double* %b
%cl = load double, double* %c
%0 = fadd double %al, %bl
%1 = fadd double %al, %cl
%2 = fadd double %bl, %cl
ret void
}

define void @outline_from_fadd2.0() {
; CHECK-LABEL: @outline_from_fadd2.0(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca double, align 4
; CHECK-NEXT: [[B:%.*]] = alloca double, align 4
; CHECK-NEXT: [[C:%.*]] = alloca double, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(double* [[A]], double* [[B]], double* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca double, align 4
%b = alloca double, align 4
%c = alloca double, align 4
store double 2.0, double* %a, align 4
store double 3.0, double* %b, align 4
store double 4.0, double* %c, align 4
%al = load double, double* %a
%bl = load double, double* %b
%cl = load double, double* %c
%0 = fadd double %al, %bl
%1 = fadd double %al, %cl
%2 = fadd double %bl, %cl
ret void
}

define void @outline_from_flipped_fadd3.0() {
; CHECK-LABEL: @outline_from_flipped_fadd3.0(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca double, align 4
; CHECK-NEXT: [[B:%.*]] = alloca double, align 4
; CHECK-NEXT: [[C:%.*]] = alloca double, align 4
; CHECK-NEXT: store double 2.000000e+00, double* [[A]], align 4
; CHECK-NEXT: store double 3.000000e+00, double* [[B]], align 4
; CHECK-NEXT: store double 4.000000e+00, double* [[C]], align 4
; CHECK-NEXT: [[AL:%.*]] = load double, double* [[A]], align 8
; CHECK-NEXT: [[BL:%.*]] = load double, double* [[B]], align 8
; CHECK-NEXT: [[CL:%.*]] = load double, double* [[C]], align 8
; CHECK-NEXT: [[TMP0:%.*]] = fadd double [[BL]], [[AL]]
; CHECK-NEXT: [[TMP1:%.*]] = fadd double [[CL]], [[AL]]
; CHECK-NEXT: [[TMP2:%.*]] = fadd double [[CL]], [[BL]]
; CHECK-NEXT: ret void
;
entry:
%a = alloca double, align 4
%b = alloca double, align 4
%c = alloca double, align 4
store double 2.0, double* %a, align 4
store double 3.0, double* %b, align 4
store double 4.0, double* %c, align 4
%al = load double, double* %a
%bl = load double, double* %b
%cl = load double, double* %c
%0 = fadd double %bl, %al
%1 = fadd double %cl, %al
%2 = fadd double %cl, %bl
ret void
}

; CHECK: define internal void @outlined_ir_func_0(double* [[ARG0:%.*]], double* [[ARG1:%.*]], double* [[ARG2:%.*]]) #0 {
; CHECK: entry_to_outline:
; CHECK-NEXT: store double 2.000000e+00, double* [[ARG0]], align 4
; CHECK-NEXT: store double 3.000000e+00, double* [[ARG1]], align 4
; CHECK-NEXT: store double 4.000000e+00, double* [[ARG2]], align 4
; CHECK-NEXT: [[AL:%.*]] = load double, double* [[ARG0]], align 8
; CHECK-NEXT: [[BL:%.*]] = load double, double* [[ARG1]], align 8
; CHECK-NEXT: [[CL:%.*]] = load double, double* [[ARG2]], align 8
; CHECK-NEXT: [[TMP0:%.*]] = fadd double [[AL]], [[BL]]
; CHECK-NEXT: [[TMP1:%.*]] = fadd double [[AL]], [[CL]]
; CHECK-NEXT: [[TMP2:%.*]] = fadd double [[BL]], [[CL]]

254 changes: 254 additions & 0 deletions llvm/test/Transforms/IROutliner/outlining-commutative.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -verify -iroutliner < %s | FileCheck %s

; This test checks that commutative instructions where the operands are
; swapped are outlined as the same function.

; It also checks that non-commutative instructions outlined as different
; functions when the operands are swapped;

; These are identical functions, except that in the flipped functions,
; the operands in the adds are commuted. However, since add instructions
; are commutative, we should still outline from all four as the same
; instruction.

define void @outline_from_add1() {
; CHECK-LABEL: @outline_from_add1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[C:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(i32* [[A]], i32* [[B]], i32* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
store i32 4, i32* %c, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%cl = load i32, i32* %c
%0 = add i32 %al, %bl
%1 = add i32 %al, %cl
%2 = add i32 %bl, %cl
ret void
}

define void @outline_from_add2() {
; CHECK-LABEL: @outline_from_add2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[C:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(i32* [[A]], i32* [[B]], i32* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
store i32 4, i32* %c, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%cl = load i32, i32* %c
%0 = add i32 %al, %bl
%1 = add i32 %al, %cl
%2 = add i32 %bl, %cl
ret void
}

define void @outline_from_flipped_add3() {
; CHECK-LABEL: @outline_from_flipped_add3(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[C:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(i32* [[A]], i32* [[B]], i32* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
store i32 4, i32* %c, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%cl = load i32, i32* %c
%0 = add i32 %bl, %al
%1 = add i32 %cl, %al
%2 = add i32 %cl, %bl
ret void
}

define void @outline_from_flipped_add4() {
; CHECK-LABEL: @outline_from_flipped_add4(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[C:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(i32* [[A]], i32* [[B]], i32* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
store i32 4, i32* %c, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%cl = load i32, i32* %c
%0 = add i32 %bl, %al
%1 = add i32 %cl, %al
%2 = add i32 %cl, %bl
ret void
}

; These are identical functions, except that in the flipped functions,
; the operands in the subtractions are commuted. Since subtraction
; instructions are not commutative, we should outline the first two functions
; differently than the second two functions.

define void @outline_from_sub1() {
; CHECK-LABEL: @outline_from_sub1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[C:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_2(i32* [[A]], i32* [[B]], i32* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
store i32 4, i32* %c, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%cl = load i32, i32* %c
%0 = sub i32 %al, %bl
%1 = sub i32 %al, %cl
%2 = sub i32 %bl, %cl
ret void
}

define void @outline_from_sub2() {
; CHECK-LABEL: @outline_from_sub2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[C:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_2(i32* [[A]], i32* [[B]], i32* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
store i32 4, i32* %c, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%cl = load i32, i32* %c
%0 = sub i32 %al, %bl
%1 = sub i32 %al, %cl
%2 = sub i32 %bl, %cl
ret void
}

define void @dontoutline_from_flipped_sub3() {
; CHECK-LABEL: @dontoutline_from_flipped_sub3(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[C:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_1(i32* [[A]], i32* [[B]], i32* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
store i32 4, i32* %c, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%cl = load i32, i32* %c
%0 = sub i32 %bl, %al
%1 = sub i32 %cl, %al
%2 = sub i32 %cl, %bl
ret void
}

define void @dontoutline_from_flipped_sub4() {
; CHECK-LABEL: @dontoutline_from_flipped_sub4(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[C:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_1(i32* [[A]], i32* [[B]], i32* [[C]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
%c = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
store i32 4, i32* %c, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%cl = load i32, i32* %c
%0 = sub i32 %bl, %al
%1 = sub i32 %cl, %al
%2 = sub i32 %cl, %bl
ret void
}

; CHECK: define internal void @outlined_ir_func_0(i32* [[ARG0:%.*]], i32* [[ARG1:%.*]], i32* [[ARG2:%.*]]) #0 {
; CHECK: entry_to_outline:
; CHECK-NEXT: store i32 2, i32* [[ARG0]], align 4
; CHECK-NEXT: store i32 3, i32* [[ARG1]], align 4
; CHECK-NEXT: store i32 4, i32* [[ARG2]], align 4
; CHECK-NEXT: [[AL:%.*]] = load i32, i32* [[ARG0]], align 4
; CHECK-NEXT: [[BL:%.*]] = load i32, i32* [[ARG1]], align 4
; CHECK-NEXT: [[CL:%.*]] = load i32, i32* [[ARG2]], align 4
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[AL]], [[BL]]
; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[AL]], [[CL]]
; CHECK-NEXT: [[TMP2:%.*]] = add i32 [[BL]], [[CL]]

; CHECK: define internal void @outlined_ir_func_1(i32* [[ARG0:%.*]], i32* [[ARG1:%.*]], i32* [[ARG2:%.*]]) #0 {
; CHECK: entry_to_outline:
; CHECK-NEXT: store i32 2, i32* [[ARG0]], align 4
; CHECK-NEXT: store i32 3, i32* [[ARG1]], align 4
; CHECK-NEXT: store i32 4, i32* [[ARG2]], align 4
; CHECK-NEXT: [[AL:%.*]] = load i32, i32* [[ARG0]], align 4
; CHECK-NEXT: [[BL:%.*]] = load i32, i32* [[ARG1]], align 4
; CHECK-NEXT: [[CL:%.*]] = load i32, i32* [[ARG2]], align 4
; CHECK-NEXT: [[TMP0:%.*]] = sub i32 [[BL]], [[AL]]
; CHECK-NEXT: [[TMP1:%.*]] = sub i32 [[CL]], [[AL]]
; CHECK-NEXT: [[TMP2:%.*]] = sub i32 [[CL]], [[BL]]

; CHECK: define internal void @outlined_ir_func_2(i32* [[ARG0:%.*]], i32* [[ARG1:%.*]], i32* [[ARG2:%.*]]) #0 {
; CHECK: entry_to_outline:
; CHECK-NEXT: store i32 2, i32* [[ARG0]], align 4
; CHECK-NEXT: store i32 3, i32* [[ARG1]], align 4
; CHECK-NEXT: store i32 4, i32* [[ARG2]], align 4
; CHECK-NEXT: [[AL:%.*]] = load i32, i32* [[ARG0]], align 4
; CHECK-NEXT: [[BL:%.*]] = load i32, i32* [[ARG1]], align 4
; CHECK-NEXT: [[CL:%.*]] = load i32, i32* [[ARG2]], align 4
; CHECK-NEXT: [[TMP0:%.*]] = sub i32 [[AL]], [[BL]]
; CHECK-NEXT: [[TMP1:%.*]] = sub i32 [[AL]], [[CL]]
; CHECK-NEXT: [[TMP2:%.*]] = sub i32 [[BL]], [[CL]]
170 changes: 170 additions & 0 deletions llvm/test/Transforms/IROutliner/outlining-isomorphic-predicates.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -verify -iroutliner < %s | FileCheck %s

; This test checks the isomorphic comparisons can be outlined together into one
; function.

; The following three function are identical, except that in the third, the
; operand order, and predicate are swapped, meaning it is structurally the same
; and should be outlined together.

define void @outline_slt1() {
; CHECK-LABEL: @outline_slt1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_1(i32* [[A]], i32* [[B]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%0 = icmp slt i32 %al, %bl
ret void
}

define void @outline_slt2() {
; CHECK-LABEL: @outline_slt2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_1(i32* [[A]], i32* [[B]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%0 = icmp slt i32 %al, %bl
ret void
}

define void @outline_sgt() {
; CHECK-LABEL: @outline_sgt(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_1(i32* [[A]], i32* [[B]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%0 = icmp sgt i32 %bl, %al
ret void
}

; This has a swapped predicate, but not swapped operands, so it cannot use
; the same outlined function as the ones above.

define void @dontoutline_sgt() {
; CHECK-LABEL: @dontoutline_sgt(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: store i32 2, i32* [[A]], align 4
; CHECK-NEXT: store i32 3, i32* [[B]], align 4
; CHECK-NEXT: [[AL:%.*]] = load i32, i32* [[A]], align 4
; CHECK-NEXT: [[BL:%.*]] = load i32, i32* [[B]], align 4
; CHECK-NEXT: [[TMP0:%.*]] = icmp sgt i32 [[AL]], [[BL]]
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%0 = icmp sgt i32 %al, %bl
ret void
}

; The below functions use a different kind of predicate that is not compatible
; with the ones above, and should use a different outlined function.
; The other difference here is that the predicate with swapped operands comes
; first this time.

define void @outline_ugt1() {
; CHECK-LABEL: @outline_ugt1(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(i32* [[A]], i32* [[B]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%0 = icmp ugt i32 %al, %bl
ret void
}

define void @outline_ugt2() {
; CHECK-LABEL: @outline_ugt2(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(i32* [[A]], i32* [[B]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%0 = icmp ugt i32 %al, %bl
ret void
}

define void @outline_ult() {
; CHECK-LABEL: @outline_ult(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
; CHECK-NEXT: [[B:%.*]] = alloca i32, align 4
; CHECK-NEXT: call void @outlined_ir_func_0(i32* [[A]], i32* [[B]])
; CHECK-NEXT: ret void
;
entry:
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
store i32 3, i32* %b, align 4
%al = load i32, i32* %a
%bl = load i32, i32* %b
%0 = icmp ult i32 %bl, %al
ret void
}

; CHECK: define internal void @outlined_ir_func_0(i32* [[ARG0:%.*]], i32* [[ARG1:%.*]]) #0 {
; CHECK: entry_to_outline:
; CHECK-NEXT: store i32 2, i32* [[ARG0]], align 4
; CHECK-NEXT: store i32 3, i32* [[ARG1]], align 4
; CHECK-NEXT: [[AL:%.*]] = load i32, i32* [[ARG0]], align 4
; CHECK-NEXT: [[BL:%.*]] = load i32, i32* [[ARG1]], align 4
; CHECK-NEXT: [[TMP0:%.*]] = icmp ugt i32 [[AL]], [[BL]]

; CHECK: define internal void @outlined_ir_func_1(i32* [[ARG0:%.*]], i32* [[ARG1:%.*]]) #0 {
; CHECK: entry_to_outline:
; CHECK-NEXT: store i32 2, i32* [[ARG0]], align 4
; CHECK-NEXT: store i32 3, i32* [[ARG1]], align 4
; CHECK-NEXT: [[AL:%.*]] = load i32, i32* [[ARG0]], align 4
; CHECK-NEXT: [[BL:%.*]] = load i32, i32* [[ARG1]], align 4
; CHECK-NEXT: [[TMP0:%.*]] = icmp slt i32 [[AL]], [[BL]]
182 changes: 173 additions & 9 deletions llvm/unittests/Analysis/IRSimilarityIdentifierTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ TEST(IRInstructionMapper, PredicateDifferentiation) {
ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
}

// Checks that predicates with the same swapped predicate map to different
// values.
// Checks that predicates where that can be considered the same when the
// operands are swapped, i.e. greater than to less than are mapped to the same
// unsigned integer.
TEST(IRInstructionMapper, PredicateIsomorphism) {
StringRef ModuleString = R"(
define i32 @f(i32 %a, i32 %b) {
Expand All @@ -177,7 +178,7 @@ TEST(IRInstructionMapper, PredicateIsomorphism) {

ASSERT_TRUE(InstrList.size() == UnsignedVec.size());
ASSERT_TRUE(UnsignedVec.size() == 3);
ASSERT_TRUE(UnsignedVec[0] != UnsignedVec[1]);
ASSERT_TRUE(UnsignedVec[0] == UnsignedVec[1]);
}

// Checks that the same predicate maps to the same value.
Expand Down Expand Up @@ -1375,6 +1376,49 @@ TEST(IRSimilarityCandidate, CheckIdenticalInstructions) {
ASSERT_TRUE(IRSimilarityCandidate::isSimilar(Cand1, Cand2));
}

// Checks that comparison instructions are found to be similar instructions
// when the operands are flipped and the predicate is also swapped.
TEST(IRSimilarityCandidate, PredicateIsomorphism) {
StringRef ModuleString = R"(
define i32 @f(i32 %a, i32 %b) {
bb0:
%0 = icmp sgt i32 %a, %b
%1 = add i32 %b, %a
br label %bb1
bb1:
%2 = icmp slt i32 %a, %b
%3 = add i32 %a, %b
ret i32 0
})";
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);

std::vector<IRInstructionData *> InstrList;
std::vector<unsigned> UnsignedVec;

getVectors(*M, InstrList, UnsignedVec);


ASSERT_TRUE(InstrList.size() > 5);
ASSERT_TRUE(InstrList.size() == UnsignedVec.size());

std::vector<IRInstructionData *>::iterator Start, End;
Start = InstrList.begin();
End = InstrList.begin();

std::advance(End, 1);
IRSimilarityCandidate Cand1(0, 2, *Start, *End);

Start = InstrList.begin();
End = InstrList.begin();

std::advance(Start, 3);
std::advance(End, 4);
IRSimilarityCandidate Cand2(3, 2, *Start, *End);

ASSERT_TRUE(IRSimilarityCandidate::isSimilar(Cand1, Cand2));
}

// Checks that IRSimilarityCandidates wrapping these two regions of instructions
// are able to differentiate between instructions that have different opcodes.
TEST(IRSimilarityCandidate, CheckRegionsDifferentInstruction) {
Expand Down Expand Up @@ -1567,6 +1611,61 @@ TEST(IRSimilarityCandidate, DifferentStructure) {
ASSERT_FALSE(longSimCandCompare(InstrList, true));
}

// Checks that comparison instructions are found to have the same structure
// when the operands are flipped and the predicate is also swapped.
TEST(IRSimilarityCandidate, PredicateIsomorphismStructure) {
StringRef ModuleString = R"(
define i32 @f(i32 %a, i32 %b) {
bb0:
%0 = icmp sgt i32 %a, %b
%1 = add i32 %a, %b
br label %bb1
bb1:
%2 = icmp slt i32 %b, %a
%3 = add i32 %a, %b
ret i32 0
})";
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);

std::vector<IRInstructionData *> InstrList;
std::vector<unsigned> UnsignedVec;

getVectors(*M, InstrList, UnsignedVec);

ASSERT_TRUE(InstrList.size() > 5);
ASSERT_TRUE(InstrList.size() == UnsignedVec.size());

ASSERT_TRUE(longSimCandCompare(InstrList, true));
}

// Checks that different predicates are counted as diferent.
TEST(IRSimilarityCandidate, PredicateDifference) {
StringRef ModuleString = R"(
define i32 @f(i32 %a, i32 %b) {
bb0:
%0 = icmp sge i32 %a, %b
%1 = add i32 %b, %a
br label %bb1
bb1:
%2 = icmp slt i32 %b, %a
%3 = add i32 %a, %b
ret i32 0
})";
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);

std::vector<IRInstructionData *> InstrList;
std::vector<unsigned> UnsignedVec;

getVectors(*M, InstrList, UnsignedVec);

ASSERT_TRUE(InstrList.size() > 5);
ASSERT_TRUE(InstrList.size() == UnsignedVec.size());

ASSERT_FALSE(longSimCandCompare(InstrList));
}

// Checks that the same structure is recognized between two candidates. The
// items %a and %b are used in the same way in both sets of instructions.
TEST(IRSimilarityCandidate, SameStructure) {
Expand Down Expand Up @@ -1688,8 +1787,8 @@ TEST(IRSimilarityIdentifier, InstructionDifference) {
ASSERT_TRUE(SimilarityCandidates.empty());
}

// This test checks to see whether we can detect similarity for commutativen
// instructions where the operands have been reversed. Right now, we cannot.
// This test checks to see whether we can detect similarity for commutative
// instructions where the operands have been reversed.
TEST(IRSimilarityIdentifier, CommutativeSimilarity) {
StringRef ModuleString = R"(
define i32 @f(i32 %a, i32 %b) {
Expand All @@ -1708,13 +1807,45 @@ TEST(IRSimilarityIdentifier, CommutativeSimilarity) {
std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
getSimilarities(*M, SimilarityCandidates);

ASSERT_TRUE(SimilarityCandidates.empty());
ASSERT_TRUE(SimilarityCandidates.size() == 1);
for (std::vector<IRSimilarityCandidate> &Cands : SimilarityCandidates) {
ASSERT_TRUE(Cands.size() == 2);
unsigned InstIdx = 0;
for (IRSimilarityCandidate &Cand : Cands) {
ASSERT_TRUE(Cand.getStartIdx() == InstIdx);
InstIdx += 3;
}
}
}

// This test checks to see whether we can detect different structure in
// commutative instructions. In this case, the second operand in the second
// add is different.
TEST(IRSimilarityIdentifier, NoCommutativeSimilarity) {
StringRef ModuleString = R"(
define i32 @f(i32 %a, i32 %b) {
bb0:
%0 = add i32 %a, %b
%1 = add i32 %1, %b
br label %bb1
bb1:
%2 = add i32 %a, %b
%3 = add i32 %2, %a
ret i32 0
})";
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);

std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
getSimilarities(*M, SimilarityCandidates);

ASSERT_TRUE(SimilarityCandidates.size() == 0);
}

// Check that we are not finding commutative similarity in non commutative
// Check that we are not finding similarity in non commutative
// instructions. That is, while the instruction and operands used are the same
// in the two subtraction sequences, they cannot be counted as the same since
// a subtraction is not commutative.
// in the two subtraction sequences, they are in a different order, and cannot
// be counted as the same since a subtraction is not commutative.
TEST(IRSimilarityIdentifier, NonCommutativeDifference) {
StringRef ModuleString = R"(
define i32 @f(i32 %a, i32 %b) {
Expand Down Expand Up @@ -1766,6 +1897,39 @@ TEST(IRSimilarityIdentifier, MappingSimilarity) {
}
}

// Check that we find instances of swapped predicate isomorphism. That is,
// for predicates that can be flipped, e.g. greater than to less than,
// we can identify that instances of these different literal predicates, but are
// the same within a single swap can be found.
TEST(IRSimilarityIdentifier, PredicateIsomorphism) {
StringRef ModuleString = R"(
define i32 @f(i32 %a, i32 %b) {
bb0:
%0 = add i32 %a, %b
%1 = icmp sgt i32 %b, %a
br label %bb1
bb1:
%2 = add i32 %a, %b
%3 = icmp slt i32 %a, %b
ret i32 0
})";
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleString);

std::vector<std::vector<IRSimilarityCandidate>> SimilarityCandidates;
getSimilarities(*M, SimilarityCandidates);

ASSERT_TRUE(SimilarityCandidates.size() == 1);
for (std::vector<IRSimilarityCandidate> &Cands : SimilarityCandidates) {
ASSERT_TRUE(Cands.size() == 2);
unsigned InstIdx = 0;
for (IRSimilarityCandidate &Cand : Cands) {
ASSERT_TRUE(Cand.getStartIdx() == InstIdx);
InstIdx += 3;
}
}
}

// Checks that constants are detected as the same operand in each use in the
// sequences of instructions. Also checks that we can find structural
// equivalence using constants. In this case the 1 has the same use pattern as
Expand Down