Skip to content

Commit ff0e4fb

Browse files
authored
[SCEV] Use or disjoint flag (llvm#74467)
Use the disjoint flag to convert or to add instead of calling the haveNoCommonBitsSet() ValueTracking query. This ensures that we can reliably undo add -> or canonicalization, even in cases where the necessary information has been lost or is too complex to reinfer in SCEV. I have updated the bulk of the test coverage to add the necessary disjoint flags in advance.
1 parent 64a9b35 commit ff0e4fb

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5223,11 +5223,8 @@ static std::optional<BinaryOp> MatchBinaryOp(Value *V, const DataLayout &DL,
52235223
return BinaryOp(Op);
52245224

52255225
case Instruction::Or: {
5226-
// LLVM loves to convert `add` of operands with no common bits
5227-
// into an `or`. But SCEV really doesn't deal with `or` that well,
5228-
// so try extra hard to recognize this `or` as an `add`.
5229-
if (haveNoCommonBitsSet(Op->getOperand(0), Op->getOperand(1),
5230-
SimplifyQuery(DL, &DT, &AC, CxtI)))
5226+
// Convert or disjoint into add nuw nsw.
5227+
if (cast<PossiblyDisjointInst>(Op)->isDisjoint())
52315228
return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1),
52325229
/*IsNSW=*/true, /*IsNUW=*/true);
52335230
return BinaryOp(Op);

llvm/test/Analysis/ScalarEvolution/add-like-or.ll

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,49 @@ define i8 @or-of-constant-with-no-common-bits-set(i8 %x, i8 %y) {
66
; CHECK-NEXT: Classifying expressions for: @or-of-constant-with-no-common-bits-set
77
; CHECK-NEXT: %t0 = shl i8 %x, 2
88
; CHECK-NEXT: --> (4 * %x) U: [0,-3) S: [-128,125)
9-
; CHECK-NEXT: %r = or i8 %t0, 3
9+
; CHECK-NEXT: %r = or disjoint i8 %t0, 3
1010
; CHECK-NEXT: --> (3 + (4 * %x))<nuw><nsw> U: [3,0) S: [-125,-128)
1111
; CHECK-NEXT: Determining loop execution counts for: @or-of-constant-with-no-common-bits-set
1212
;
1313
%t0 = shl i8 %x, 2
14-
%r = or i8 %t0, 3
14+
%r = or disjoint i8 %t0, 3
1515
ret i8 %r
1616
}
1717

18+
define i8 @or-disjoint(i8 %x, i8 %y) {
19+
; CHECK-LABEL: 'or-disjoint'
20+
; CHECK-NEXT: Classifying expressions for: @or-disjoint
21+
; CHECK-NEXT: %or = or disjoint i8 %x, %y
22+
; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
23+
; CHECK-NEXT: Determining loop execution counts for: @or-disjoint
24+
;
25+
%or = or disjoint i8 %x, %y
26+
ret i8 %or
27+
}
28+
29+
define i8 @or-no-disjoint(i8 %x, i8 %y) {
30+
; CHECK-LABEL: 'or-no-disjoint'
31+
; CHECK-NEXT: Classifying expressions for: @or-no-disjoint
32+
; CHECK-NEXT: %or = or i8 %x, %y
33+
; CHECK-NEXT: --> %or U: full-set S: full-set
34+
; CHECK-NEXT: Determining loop execution counts for: @or-no-disjoint
35+
;
36+
%or = or i8 %x, %y
37+
ret i8 %or
38+
}
39+
40+
; FIXME: We could add nuw nsw flags here.
41+
define noundef i8 @or-disjoint-transfer-flags(i8 %x, i8 %y) {
42+
; CHECK-LABEL: 'or-disjoint-transfer-flags'
43+
; CHECK-NEXT: Classifying expressions for: @or-disjoint-transfer-flags
44+
; CHECK-NEXT: %or = or disjoint i8 %x, %y
45+
; CHECK-NEXT: --> (%x + %y) U: full-set S: full-set
46+
; CHECK-NEXT: Determining loop execution counts for: @or-disjoint-transfer-flags
47+
;
48+
%or = or disjoint i8 %x, %y
49+
ret i8 %or
50+
}
51+
1852
define void @mask-high(i64 %arg, ptr dereferenceable(4) %arg1) {
1953
; CHECK-LABEL: 'mask-high'
2054
; CHECK-NEXT: Classifying expressions for: @mask-high
@@ -24,7 +58,7 @@ define void @mask-high(i64 %arg, ptr dereferenceable(4) %arg1) {
2458
; CHECK-NEXT: --> (sext i32 %i to i64) U: [-2147483648,2147483648) S: [-2147483648,2147483648)
2559
; CHECK-NEXT: %i3 = and i64 %arg, -16
2660
; CHECK-NEXT: --> (16 * (%arg /u 16))<nuw> U: [0,-15) S: [-9223372036854775808,9223372036854775793)
27-
; CHECK-NEXT: %i4 = or i64 1, %i3
61+
; CHECK-NEXT: %i4 = or disjoint i64 1, %i3
2862
; CHECK-NEXT: --> (1 + (16 * (%arg /u 16))<nuw>)<nuw><nsw> U: [1,-14) S: [-9223372036854775807,9223372036854775794)
2963
; CHECK-NEXT: %i7 = phi i64 [ %i4, %bb ], [ %i8, %bb6 ]
3064
; CHECK-NEXT: --> {(1 + (16 * (%arg /u 16))<nuw>)<nuw><nsw>,+,1}<%bb6> U: full-set S: full-set Exits: ((sext i32 %i to i64) smax (1 + (16 * (%arg /u 16))<nuw>)<nuw><nsw>) LoopDispositions: { %bb6: Computable }
@@ -42,7 +76,7 @@ bb:
4276
%i = load i32, ptr %arg1, align 4
4377
%i2 = sext i32 %i to i64
4478
%i3 = and i64 %arg, -16
45-
%i4 = or i64 1, %i3
79+
%i4 = or disjoint i64 1, %i3
4680
%i5 = icmp sgt i64 %i4, %i2
4781
br i1 %i5, label %bb10, label %bb6
4882

0 commit comments

Comments
 (0)