Skip to content

Commit 2c2de4b

Browse files
authored
[ValueTracking] Remove SPF support from computeKnownBitsFromOperator (#76630)
This patch removes redundant SPF support (5350e1b) from `computeKnownBitsFromOperator` as we always canonicalize a SPF into an intrinsic call. Compile-time improvement: http://llvm-compile-time-tracker.com/compare.php?from=3dc0638cfc19e140daff7bf1281648daca8212fa&to=8771ef0749fb2ba4304dc68d418c88ec5769346f&stat=instructions:u |stage1-O3|stage1-ReleaseThinLTO|stage1-ReleaseLTO-g|stage1-O0-g|stage2-O3|stage2-O0-g|stage2-clang| |--|--|--|--|--|--|--| -0.01%|-0.01%|+0.01%|+0.00%|+0.01%|+0.04%|-0.01%|
1 parent e213af7 commit 2c2de4b

File tree

4 files changed

+30
-88
lines changed

4 files changed

+30
-88
lines changed

llvm/lib/Analysis/ValueTracking.cpp

-34
Original file line numberDiff line numberDiff line change
@@ -983,45 +983,11 @@ static void computeKnownBitsFromOperator(const Operator *I,
983983
break;
984984
}
985985
case Instruction::Select: {
986-
const Value *LHS = nullptr, *RHS = nullptr;
987-
SelectPatternFlavor SPF = matchSelectPattern(I, LHS, RHS).Flavor;
988-
if (SelectPatternResult::isMinOrMax(SPF)) {
989-
computeKnownBits(RHS, Known, Depth + 1, Q);
990-
computeKnownBits(LHS, Known2, Depth + 1, Q);
991-
switch (SPF) {
992-
default:
993-
llvm_unreachable("Unhandled select pattern flavor!");
994-
case SPF_SMAX:
995-
Known = KnownBits::smax(Known, Known2);
996-
break;
997-
case SPF_SMIN:
998-
Known = KnownBits::smin(Known, Known2);
999-
break;
1000-
case SPF_UMAX:
1001-
Known = KnownBits::umax(Known, Known2);
1002-
break;
1003-
case SPF_UMIN:
1004-
Known = KnownBits::umin(Known, Known2);
1005-
break;
1006-
}
1007-
break;
1008-
}
1009-
1010986
computeKnownBits(I->getOperand(2), Known, Depth + 1, Q);
1011987
computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1012988

1013989
// Only known if known in both the LHS and RHS.
1014990
Known = Known.intersectWith(Known2);
1015-
1016-
if (SPF == SPF_ABS) {
1017-
// RHS from matchSelectPattern returns the negation part of abs pattern.
1018-
// If the negate has an NSW flag we can assume the sign bit of the result
1019-
// will be 0 because that makes abs(INT_MIN) undefined.
1020-
if (match(RHS, m_Neg(m_Specific(LHS))) &&
1021-
Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(RHS)))
1022-
Known.Zero.setSignBit();
1023-
}
1024-
1025991
break;
1026992
}
1027993
case Instruction::FPTrunc:

llvm/test/Analysis/ScalarEvolution/max-expr-cache.ll

+6-6
Original file line numberDiff line numberDiff line change
@@ -229,21 +229,21 @@ define void @umax(i32 %tmp3) {
229229
; CHECK-NEXT: %tmp48 = select i1 %tmp47, i32 %tmp44, i32 %tmp46
230230
; CHECK-NEXT: --> ((7 + (256 umin {%tmp3,+,-256}<%bb4>))<nuw><nsw> umax (256 umin (1 + (256 umin (1 + (256 umin (1 + (256 umin (1 + (256 umin (1 + (256 umin (1 + (256 umin (1 + (256 umin {%tmp3,+,-256}<%bb4>))<nuw><nsw> umin {%tmp3,+,-256}<%bb4>))<nuw><nsw> umin {%tmp3,+,-256}<%bb4>))<nuw><nsw> umin {%tmp3,+,-256}<%bb4>))<nuw><nsw> umin {%tmp3,+,-256}<%bb4>))<nuw><nsw> umin {%tmp3,+,-256}<%bb4>))<nuw><nsw> umin {%tmp3,+,-256}<%bb4>))<nuw><nsw> umin {%tmp3,+,-256}<%bb4>)) U: [7,264) S: [7,264) Exits: <<Unknown>> LoopDispositions: { %bb4: Computable, %bb53: Invariant }
231231
; CHECK-NEXT: %tmp49 = ashr i32 %tmp48, 3
232-
; CHECK-NEXT: --> %tmp49 U: [0,128) S: [0,128) Exits: <<Unknown>> LoopDispositions: { %bb4: Variant, %bb53: Invariant }
232+
; CHECK-NEXT: --> %tmp49 U: [-268435456,268435456) S: [-268435456,268435456) Exits: <<Unknown>> LoopDispositions: { %bb4: Variant, %bb53: Invariant }
233233
; CHECK-NEXT: %tmp51 = select i1 %tmp50, i32 %tmp49, i32 0
234-
; CHECK-NEXT: --> %tmp49 U: [0,128) S: [0,128) Exits: <<Unknown>> LoopDispositions: { %bb4: Variant, %bb53: Invariant }
234+
; CHECK-NEXT: --> %tmp49 U: [-268435456,268435456) S: [-268435456,268435456) Exits: <<Unknown>> LoopDispositions: { %bb4: Variant, %bb53: Invariant }
235235
; CHECK-NEXT: %tmp52 = zext i32 %tmp51 to i64
236-
; CHECK-NEXT: --> (zext i32 %tmp49 to i64) U: [0,128) S: [0,128) Exits: <<Unknown>> LoopDispositions: { %bb4: Variant, %bb53: Invariant }
236+
; CHECK-NEXT: --> (zext i32 %tmp49 to i64) U: [0,4294967296) S: [0,4294967296) Exits: <<Unknown>> LoopDispositions: { %bb4: Variant, %bb53: Invariant }
237237
; CHECK-NEXT: %tmp54 = phi i64 [ undef, %bb4 ], [ %tmp59, %bb53 ]
238238
; CHECK-NEXT: --> {undef,+,1}<nsw><%bb53> U: full-set S: full-set Exits: (-1 + (zext i32 %tmp49 to i64))<nsw> LoopDispositions: { %bb53: Computable, %bb4: Variant }
239239
; CHECK-NEXT: %tmp55 = trunc i64 %tmp54 to i32
240240
; CHECK-NEXT: --> {(trunc i64 undef to i32),+,1}<%bb53> U: full-set S: full-set Exits: (-1 + %tmp49)<nsw> LoopDispositions: { %bb53: Computable, %bb4: Variant }
241241
; CHECK-NEXT: %tmp56 = shl nsw i32 %tmp55, 3
242-
; CHECK-NEXT: --> {(8 * (trunc i64 undef to i32)),+,8}<%bb53> U: [0,-7) S: [-2147483648,2147483641) Exits: (-8 + (8 * %tmp49)<nuw><nsw>)<nsw> LoopDispositions: { %bb53: Computable, %bb4: Variant }
242+
; CHECK-NEXT: --> {(8 * (trunc i64 undef to i32)),+,8}<%bb53> U: [0,-7) S: [-2147483648,2147483641) Exits: (-8 + (8 * %tmp49)<nsw>) LoopDispositions: { %bb53: Computable, %bb4: Variant }
243243
; CHECK-NEXT: %tmp57 = sext i32 %tmp56 to i64
244-
; CHECK-NEXT: --> (sext i32 {(8 * (trunc i64 undef to i32)),+,8}<%bb53> to i64) U: [0,-7) S: [-2147483648,2147483641) Exits: (-8 + (8 * (zext i32 %tmp49 to i64))<nuw><nsw>)<nsw> LoopDispositions: { %bb53: Computable, %bb4: Variant }
244+
; CHECK-NEXT: --> (sext i32 {(8 * (trunc i64 undef to i32)),+,8}<%bb53> to i64) U: [0,-7) S: [-2147483648,2147483641) Exits: (sext i32 (-8 + (8 * %tmp49)<nsw>) to i64) LoopDispositions: { %bb53: Computable, %bb4: Variant }
245245
; CHECK-NEXT: %tmp58 = getelementptr inbounds i8, ptr null, i64 %tmp57
246-
; CHECK-NEXT: --> ((sext i32 {(8 * (trunc i64 undef to i32)),+,8}<%bb53> to i64) + null) U: [0,-7) S: [-2147483648,2147483641) Exits: (-8 + (8 * (zext i32 %tmp49 to i64))<nuw><nsw> + null) LoopDispositions: { %bb53: Computable, %bb4: Variant }
246+
; CHECK-NEXT: --> ((sext i32 {(8 * (trunc i64 undef to i32)),+,8}<%bb53> to i64) + null) U: [0,-7) S: [-2147483648,2147483641) Exits: ((sext i32 (-8 + (8 * %tmp49)<nsw>) to i64) + null) LoopDispositions: { %bb53: Computable, %bb4: Variant }
247247
; CHECK-NEXT: %tmp59 = add nsw i64 %tmp54, 1
248248
; CHECK-NEXT: --> {(1 + undef),+,1}<nsw><%bb53> U: full-set S: full-set Exits: (zext i32 %tmp49 to i64) LoopDispositions: { %bb53: Computable, %bb4: Variant }
249249
; CHECK-NEXT: %tmp62 = add nuw nsw i64 %tmp5, 1

llvm/test/Transforms/LoopIdiom/ARM/ctlz.ll

+9-16
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@
3131
; Function Attrs: norecurse nounwind uwtable
3232
define i32 @ctlz_and_other(i32 %n, ptr nocapture %a) {
3333
entry:
34-
%c = icmp sgt i32 %n, 0
35-
%negn = sub nsw i32 0, %n
36-
%abs_n = select i1 %c, i32 %n, i32 %negn
34+
%abs_n = call i32 @llvm.abs.i32(i32 %n, i1 true)
3735
%shr8 = lshr i32 %abs_n, 1
3836
%tobool9 = icmp eq i32 %shr8, 0
3937
br i1 %tobool9, label %while.end, label %while.body.preheader
@@ -90,9 +88,7 @@ while.end: ; preds = %while.end.loopexit,
9088
; Function Attrs: norecurse nounwind readnone uwtable
9189
define i32 @ctlz_zero_check(i32 %n) {
9290
entry:
93-
%c = icmp sgt i32 %n, 0
94-
%negn = sub nsw i32 0, %n
95-
%abs_n = select i1 %c, i32 %n, i32 %negn
91+
%abs_n = call i32 @llvm.abs.i32(i32 %n, i1 true)
9692
%tobool4 = icmp eq i32 %abs_n, 0
9793
br i1 %tobool4, label %while.end, label %while.body.preheader
9894

@@ -140,9 +136,7 @@ while.end: ; preds = %while.end.loopexit,
140136
; Function Attrs: norecurse nounwind readnone uwtable
141137
define i32 @ctlz(i32 %n) {
142138
entry:
143-
%c = icmp sgt i32 %n, 0
144-
%negn = sub nsw i32 0, %n
145-
%abs_n = select i1 %c, i32 %n, i32 %negn
139+
%abs_n = call i32 @llvm.abs.i32(i32 %n, i1 true)
146140
br label %while.cond
147141

148142
while.cond: ; preds = %while.cond, %entry
@@ -183,9 +177,7 @@ while.end: ; preds = %while.cond
183177
; Function Attrs: norecurse nounwind readnone uwtable
184178
define i32 @ctlz_add(i32 %n, i32 %i0) {
185179
entry:
186-
%c = icmp sgt i32 %n, 0
187-
%negn = sub nsw i32 0, %n
188-
%abs_n = select i1 %c, i32 %n, i32 %negn
180+
%abs_n = call i32 @llvm.abs.i32(i32 %n, i1 true)
189181
br label %while.cond
190182

191183
while.cond: ; preds = %while.cond, %entry
@@ -227,10 +219,8 @@ while.end: ; preds = %while.cond
227219
; Function Attrs: norecurse nounwind readnone uwtable
228220
define i32 @ctlz_sext(i16 %in) {
229221
entry:
230-
%n = sext i16 %in to i32
231-
%c = icmp sgt i16 %in, 0
232-
%negn = sub nsw i32 0, %n
233-
%abs_n = select i1 %c, i32 %n, i32 %negn
222+
%abs = call i16 @llvm.abs.i16(i16 %in, i1 false)
223+
%abs_n = zext i16 %abs to i32
234224
br label %while.cond
235225

236226
while.cond: ; preds = %while.cond, %entry
@@ -244,3 +234,6 @@ while.cond: ; preds = %while.cond, %entry
244234
while.end: ; preds = %while.cond
245235
ret i32 %i.0
246236
}
237+
238+
declare i32 @llvm.abs.i32(i32, i1)
239+
declare i16 @llvm.abs.i16(i16, i1)

llvm/test/Transforms/LoopIdiom/X86/ctlz.ll

+15-32
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020
define i32 @ctlz_and_other(i32 %n, ptr nocapture %a) {
2121
; ALL-LABEL: @ctlz_and_other(
2222
; ALL-NEXT: entry:
23-
; ALL-NEXT: [[C:%.*]] = icmp sgt i32 [[N:%.*]], 0
24-
; ALL-NEXT: [[NEGN:%.*]] = sub nsw i32 0, [[N]]
25-
; ALL-NEXT: [[ABS_N:%.*]] = select i1 [[C]], i32 [[N]], i32 [[NEGN]]
23+
; ALL-NEXT: [[ABS_N:%.*]] = call i32 @llvm.abs.i32(i32 [[N:%.*]], i1 true)
2624
; ALL-NEXT: [[SHR8:%.*]] = lshr i32 [[ABS_N]], 1
2725
; ALL-NEXT: [[TOBOOL9:%.*]] = icmp eq i32 [[SHR8]], 0
2826
; ALL-NEXT: br i1 [[TOBOOL9]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
@@ -56,9 +54,7 @@ define i32 @ctlz_and_other(i32 %n, ptr nocapture %a) {
5654
; ALL-NEXT: ret i32 [[I_0_LCSSA]]
5755
;
5856
entry:
59-
%c = icmp sgt i32 %n, 0
60-
%negn = sub nsw i32 0, %n
61-
%abs_n = select i1 %c, i32 %n, i32 %negn
57+
%abs_n = call i32 @llvm.abs.i32(i32 %n, i1 true)
6258
%shr8 = lshr i32 %abs_n, 1
6359
%tobool9 = icmp eq i32 %shr8, 0
6460
br i1 %tobool9, label %while.end, label %while.body.preheader
@@ -108,9 +104,7 @@ while.end: ; preds = %while.end.loopexit,
108104
define i32 @ctlz_zero_check(i32 %n) {
109105
; ALL-LABEL: @ctlz_zero_check(
110106
; ALL-NEXT: entry:
111-
; ALL-NEXT: [[C:%.*]] = icmp sgt i32 [[N:%.*]], 0
112-
; ALL-NEXT: [[NEGN:%.*]] = sub nsw i32 0, [[N]]
113-
; ALL-NEXT: [[ABS_N:%.*]] = select i1 [[C]], i32 [[N]], i32 [[NEGN]]
107+
; ALL-NEXT: [[ABS_N:%.*]] = call i32 @llvm.abs.i32(i32 [[N:%.*]], i1 true)
114108
; ALL-NEXT: [[TOBOOL4:%.*]] = icmp eq i32 [[ABS_N]], 0
115109
; ALL-NEXT: br i1 [[TOBOOL4]], label [[WHILE_END:%.*]], label [[WHILE_BODY_PREHEADER:%.*]]
116110
; ALL: while.body.preheader:
@@ -134,9 +128,7 @@ define i32 @ctlz_zero_check(i32 %n) {
134128
; ALL-NEXT: ret i32 [[I_0_LCSSA]]
135129
;
136130
entry:
137-
%c = icmp sgt i32 %n, 0
138-
%negn = sub nsw i32 0, %n
139-
%abs_n = select i1 %c, i32 %n, i32 %negn
131+
%abs_n = call i32 @llvm.abs.i32(i32 %n, i1 true)
140132
%tobool4 = icmp eq i32 %abs_n, 0
141133
br i1 %tobool4, label %while.end, label %while.body.preheader
142134

@@ -238,9 +230,7 @@ while.end: ; preds = %while.end.loopexit,
238230
define i32 @ctlz(i32 %n) {
239231
; ALL-LABEL: @ctlz(
240232
; ALL-NEXT: entry:
241-
; ALL-NEXT: [[C:%.*]] = icmp sgt i32 [[N:%.*]], 0
242-
; ALL-NEXT: [[NEGN:%.*]] = sub nsw i32 0, [[N]]
243-
; ALL-NEXT: [[ABS_N:%.*]] = select i1 [[C]], i32 [[N]], i32 [[NEGN]]
233+
; ALL-NEXT: [[ABS_N:%.*]] = call i32 @llvm.abs.i32(i32 [[N:%.*]], i1 true)
244234
; ALL-NEXT: [[TMP0:%.*]] = ashr i32 [[ABS_N]], 1
245235
; ALL-NEXT: [[TMP1:%.*]] = call i32 @llvm.ctlz.i32(i32 [[TMP0]], i1 false)
246236
; ALL-NEXT: [[TMP2:%.*]] = sub i32 32, [[TMP1]]
@@ -260,9 +250,7 @@ define i32 @ctlz(i32 %n) {
260250
; ALL-NEXT: ret i32 [[I_0_LCSSA]]
261251
;
262252
entry:
263-
%c = icmp sgt i32 %n, 0
264-
%negn = sub nsw i32 0, %n
265-
%abs_n = select i1 %c, i32 %n, i32 %negn
253+
%abs_n = call i32 @llvm.abs.i32(i32 %n, i1 true)
266254
br label %while.cond
267255

268256
while.cond: ; preds = %while.cond, %entry
@@ -343,9 +331,7 @@ while.end: ; preds = %while.cond
343331
define i32 @ctlz_add(i32 %n, i32 %i0) {
344332
; ALL-LABEL: @ctlz_add(
345333
; ALL-NEXT: entry:
346-
; ALL-NEXT: [[C:%.*]] = icmp sgt i32 [[N:%.*]], 0
347-
; ALL-NEXT: [[NEGN:%.*]] = sub nsw i32 0, [[N]]
348-
; ALL-NEXT: [[ABS_N:%.*]] = select i1 [[C]], i32 [[N]], i32 [[NEGN]]
334+
; ALL-NEXT: [[ABS_N:%.*]] = call i32 @llvm.abs.i32(i32 [[N:%.*]], i1 true)
349335
; ALL-NEXT: [[TMP0:%.*]] = ashr i32 [[ABS_N]], 1
350336
; ALL-NEXT: [[TMP1:%.*]] = call i32 @llvm.ctlz.i32(i32 [[TMP0]], i1 false)
351337
; ALL-NEXT: [[TMP2:%.*]] = sub i32 32, [[TMP1]]
@@ -366,9 +352,7 @@ define i32 @ctlz_add(i32 %n, i32 %i0) {
366352
; ALL-NEXT: ret i32 [[I_0_LCSSA]]
367353
;
368354
entry:
369-
%c = icmp sgt i32 %n, 0
370-
%negn = sub nsw i32 0, %n
371-
%abs_n = select i1 %c, i32 %n, i32 %negn
355+
%abs_n = call i32 @llvm.abs.i32(i32 %n, i1 true)
372356
br label %while.cond
373357

374358
while.cond: ; preds = %while.cond, %entry
@@ -452,10 +436,8 @@ while.end: ; preds = %while.cond
452436
define i32 @ctlz_sext(i16 %in) {
453437
; ALL-LABEL: @ctlz_sext(
454438
; ALL-NEXT: entry:
455-
; ALL-NEXT: [[N:%.*]] = sext i16 [[IN:%.*]] to i32
456-
; ALL-NEXT: [[C:%.*]] = icmp sgt i16 [[IN]], 0
457-
; ALL-NEXT: [[NEGN:%.*]] = sub nsw i32 0, [[N]]
458-
; ALL-NEXT: [[ABS_N:%.*]] = select i1 [[C]], i32 [[N]], i32 [[NEGN]]
439+
; ALL-NEXT: [[ABS:%.*]] = call i16 @llvm.abs.i16(i16 [[IN:%.*]], i1 false)
440+
; ALL-NEXT: [[ABS_N:%.*]] = zext i16 [[ABS]] to i32
459441
; ALL-NEXT: [[TMP0:%.*]] = ashr i32 [[ABS_N]], 1
460442
; ALL-NEXT: [[TMP1:%.*]] = call i32 @llvm.ctlz.i32(i32 [[TMP0]], i1 false)
461443
; ALL-NEXT: [[TMP2:%.*]] = sub i32 32, [[TMP1]]
@@ -475,10 +457,8 @@ define i32 @ctlz_sext(i16 %in) {
475457
; ALL-NEXT: ret i32 [[I_0_LCSSA]]
476458
;
477459
entry:
478-
%n = sext i16 %in to i32
479-
%c = icmp sgt i16 %in, 0
480-
%negn = sub nsw i32 0, %n
481-
%abs_n = select i1 %c, i32 %n, i32 %negn
460+
%abs = call i16 @llvm.abs.i16(i16 %in, i1 false)
461+
%abs_n = zext i16 %abs to i32
482462
br label %while.cond
483463

484464
while.cond: ; preds = %while.cond, %entry
@@ -753,3 +733,6 @@ while.cond: ; preds = %while.cond, %entry
753733
while.end: ; preds = %while.cond
754734
ret i32 %i.0
755735
}
736+
737+
declare i32 @llvm.abs.i32(i32, i1)
738+
declare i16 @llvm.abs.i16(i16, i1)

0 commit comments

Comments
 (0)