Skip to content

Commit 9688a79

Browse files
committed
Merge remote-tracking branch 'origin/main' into vpbundlerecipe
2 parents e8dc289 + 74ec1c2 commit 9688a79

File tree

15 files changed

+668
-46
lines changed

15 files changed

+668
-46
lines changed

compiler-rt/lib/lsan/lsan_interceptors.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ INTERCEPTOR(void*, valloc, uptr size) {
146146
GET_STACK_TRACE_MALLOC;
147147
return lsan_valloc(size, stack);
148148
}
149+
#else
150+
# define LSAN_MAYBE_INTERCEPT_FREE_SIZED
151+
# define LSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED
149152
#endif // !SANITIZER_APPLE
150153

151154
#if SANITIZER_INTERCEPT_MEMALIGN

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,14 @@ m_NSWAdd(const LHS &L, const RHS &R) {
13231323
R);
13241324
}
13251325
template <typename LHS, typename RHS>
1326+
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1327+
OverflowingBinaryOperator::NoSignedWrap, true>
1328+
m_c_NSWAdd(const LHS &L, const RHS &R) {
1329+
return OverflowingBinaryOp_match<LHS, RHS, Instruction::Add,
1330+
OverflowingBinaryOperator::NoSignedWrap,
1331+
true>(L, R);
1332+
}
1333+
template <typename LHS, typename RHS>
13261334
inline OverflowingBinaryOp_match<LHS, RHS, Instruction::Sub,
13271335
OverflowingBinaryOperator::NoSignedWrap>
13281336
m_NSWSub(const LHS &L, const RHS &R) {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,8 @@ static void computeKnownBitsFromOperator(const Operator *I,
13461346
isa<ScalableVectorType>(I->getType()))
13471347
break;
13481348

1349+
unsigned NumElts = DemandedElts.getBitWidth();
1350+
bool IsLE = Q.DL.isLittleEndian();
13491351
// Look through a cast from narrow vector elements to wider type.
13501352
// Examples: v4i32 -> v2i64, v3i8 -> v24
13511353
unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
@@ -1364,7 +1366,6 @@ static void computeKnownBitsFromOperator(const Operator *I,
13641366
//
13651367
// The known bits of each sub-element are then inserted into place
13661368
// (dependent on endian) to form the full result of known bits.
1367-
unsigned NumElts = DemandedElts.getBitWidth();
13681369
unsigned SubScale = BitWidth / SubBitWidth;
13691370
APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
13701371
for (unsigned i = 0; i != NumElts; ++i) {
@@ -1376,10 +1377,32 @@ static void computeKnownBitsFromOperator(const Operator *I,
13761377
for (unsigned i = 0; i != SubScale; ++i) {
13771378
computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
13781379
Depth + 1);
1379-
unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
1380+
unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
13801381
Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
13811382
}
13821383
}
1384+
// Look through a cast from wider vector elements to narrow type.
1385+
// Examples: v2i64 -> v4i32
1386+
if (SubBitWidth % BitWidth == 0) {
1387+
unsigned SubScale = SubBitWidth / BitWidth;
1388+
KnownBits KnownSrc(SubBitWidth);
1389+
APInt SubDemandedElts =
1390+
APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1391+
computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1392+
Depth + 1);
1393+
1394+
Known.Zero.setAllBits();
1395+
Known.One.setAllBits();
1396+
for (unsigned i = 0; i != SubScale; ++i) {
1397+
if (DemandedElts[i]) {
1398+
unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1399+
unsigned Offset = (Shifts % SubScale) * BitWidth;
1400+
Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1401+
if (Known.isUnknown())
1402+
break;
1403+
}
1404+
}
1405+
}
13831406
break;
13841407
}
13851408
case Instruction::SExt: {

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ Instruction *InstCombinerImpl::visitAdd(BinaryOperator &I) {
18961896
{Sub, Builder.getFalse()});
18971897
Value *Ret = Builder.CreateSub(
18981898
ConstantInt::get(A->getType(), A->getType()->getScalarSizeInBits()),
1899-
Ctlz, "", /*HasNUW*/ true, /*HasNSW*/ true);
1899+
Ctlz, "", /*HasNUW=*/true, /*HasNSW=*/true);
19001900
return replaceInstUsesWith(I, Builder.CreateZExtOrTrunc(Ret, I.getType()));
19011901
}
19021902

@@ -2363,8 +2363,8 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
23632363
OverflowingBinaryOperator *LHSSub = cast<OverflowingBinaryOperator>(Op0);
23642364
bool HasNUW = I.hasNoUnsignedWrap() && LHSSub->hasNoUnsignedWrap();
23652365
bool HasNSW = HasNUW && I.hasNoSignedWrap() && LHSSub->hasNoSignedWrap();
2366-
Value *Add = Builder.CreateAdd(Y, Op1, "", /* HasNUW */ HasNUW,
2367-
/* HasNSW */ HasNSW);
2366+
Value *Add = Builder.CreateAdd(Y, Op1, "", /*HasNUW=*/HasNUW,
2367+
/*HasNSW=*/HasNSW);
23682368
BinaryOperator *Sub = BinaryOperator::CreateSub(X, Add);
23692369
Sub->setHasNoUnsignedWrap(HasNUW);
23702370
Sub->setHasNoSignedWrap(HasNSW);
@@ -2835,6 +2835,51 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
28352835
if (Instruction *Res = foldBinOpOfSelectAndCastOfSelectCondition(I))
28362836
return Res;
28372837

2838+
// (sub (sext (add nsw (X, Y)), sext (X))) --> (sext (Y))
2839+
if (match(Op1, m_SExtLike(m_Value(X))) &&
2840+
match(Op0, m_SExtLike(m_c_NSWAdd(m_Specific(X), m_Value(Y))))) {
2841+
Value *SExtY = Builder.CreateSExt(Y, I.getType());
2842+
return replaceInstUsesWith(I, SExtY);
2843+
}
2844+
2845+
// (sub[ nsw] (sext (add nsw (X, Y)), sext (add nsw (X, Z)))) -->
2846+
// --> (sub[ nsw] (sext (Y), sext (Z)))
2847+
{
2848+
Value *Z, *Add0, *Add1;
2849+
if (match(Op0, m_SExtLike(m_Value(Add0))) &&
2850+
match(Op1, m_SExtLike(m_Value(Add1))) &&
2851+
((match(Add0, m_NSWAdd(m_Value(X), m_Value(Y))) &&
2852+
match(Add1, m_c_NSWAdd(m_Specific(X), m_Value(Z)))) ||
2853+
(match(Add0, m_NSWAdd(m_Value(Y), m_Value(X))) &&
2854+
match(Add1, m_c_NSWAdd(m_Specific(X), m_Value(Z)))))) {
2855+
unsigned NumOfNewInstrs = 0;
2856+
// Non-constant Y, Z require new SExt.
2857+
NumOfNewInstrs += !isa<Constant>(Y) ? 1 : 0;
2858+
NumOfNewInstrs += !isa<Constant>(Z) ? 1 : 0;
2859+
// Check if we can trade some of the old instructions for the new ones.
2860+
unsigned NumOfDeadInstrs = 0;
2861+
if (Op0->hasOneUse()) {
2862+
// If Op0 (sext) has multiple uses, then we keep it
2863+
// and the add that it uses, otherwise, we can remove
2864+
// the sext and probably the add (depending on the number of its uses).
2865+
++NumOfDeadInstrs;
2866+
NumOfDeadInstrs += Add0->hasOneUse() ? 1 : 0;
2867+
}
2868+
if (Op1->hasOneUse()) {
2869+
++NumOfDeadInstrs;
2870+
NumOfDeadInstrs += Add1->hasOneUse() ? 1 : 0;
2871+
}
2872+
if (NumOfDeadInstrs >= NumOfNewInstrs) {
2873+
Value *SExtY = Builder.CreateSExt(Y, I.getType());
2874+
Value *SExtZ = Builder.CreateSExt(Z, I.getType());
2875+
Value *Sub = Builder.CreateSub(SExtY, SExtZ, "",
2876+
/*HasNUW=*/false,
2877+
/*HasNSW=*/I.hasNoSignedWrap());
2878+
return replaceInstUsesWith(I, Sub);
2879+
}
2880+
}
2881+
}
2882+
28382883
return TryToNarrowDeduceFlags();
28392884
}
28402885

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
290290
auto *Op1C = cast<Constant>(Op1);
291291
return replaceInstUsesWith(
292292
I, Builder.CreateMul(NegOp0, ConstantExpr::getNeg(Op1C), "",
293-
/* HasNUW */ false,
293+
/*HasNUW=*/false,
294294
HasNSW && Op1C->isNotMinSignedValue()));
295295
}
296296

@@ -1255,8 +1255,8 @@ static Value *foldIDivShl(BinaryOperator &I, InstCombiner::BuilderTy &Builder) {
12551255
// or divisor has nsw and operator is sdiv.
12561256
Value *Dividend = Builder.CreateShl(
12571257
One, Y, "shl.dividend",
1258-
/*HasNUW*/ true,
1259-
/*HasNSW*/
1258+
/*HasNUW=*/true,
1259+
/*HasNSW=*/
12601260
IsSigned ? (Shl0->hasNoUnsignedWrap() || Shl1->hasNoUnsignedWrap())
12611261
: Shl0->hasNoSignedWrap());
12621262
return Builder.CreateLShr(Dividend, Z, "", I.isExact());

llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
233233
// However, only do this either if the old `sub` doesn't stick around, or
234234
// it was subtracting from a constant. Otherwise, this isn't profitable.
235235
return Builder.CreateSub(I->getOperand(1), I->getOperand(0),
236-
I->getName() + ".neg", /* HasNUW */ false,
236+
I->getName() + ".neg", /*HasNUW=*/false,
237237
IsNSW && I->hasNoSignedWrap());
238238
}
239239

@@ -404,15 +404,15 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
404404
IsNSW &= I->hasNoSignedWrap();
405405
if (Value *NegOp0 = negate(I->getOperand(0), IsNSW, Depth + 1))
406406
return Builder.CreateShl(NegOp0, I->getOperand(1), I->getName() + ".neg",
407-
/* HasNUW */ false, IsNSW);
407+
/*HasNUW=*/false, IsNSW);
408408
// Otherwise, `shl %x, C` can be interpreted as `mul %x, 1<<C`.
409409
Constant *Op1C;
410410
if (!match(I->getOperand(1), m_ImmConstant(Op1C)) || !IsTrulyNegation)
411411
return nullptr;
412412
return Builder.CreateMul(
413413
I->getOperand(0),
414414
Builder.CreateShl(Constant::getAllOnesValue(Op1C->getType()), Op1C),
415-
I->getName() + ".neg", /* HasNUW */ false, IsNSW);
415+
I->getName() + ".neg", /*HasNUW=*/false, IsNSW);
416416
}
417417
case Instruction::Or: {
418418
if (!cast<PossiblyDisjointInst>(I)->isDisjoint())
@@ -483,7 +483,7 @@ std::array<Value *, 2> Negator::getSortedOperandsOfBinOp(Instruction *I) {
483483
// Can't negate either of them.
484484
return nullptr;
485485
return Builder.CreateMul(NegatedOp, OtherOp, I->getName() + ".neg",
486-
/* HasNUW */ false, IsNSW && I->hasNoSignedWrap());
486+
/*HasNUW=*/false, IsNSW && I->hasNoSignedWrap());
487487
}
488488
default:
489489
return nullptr; // Don't know, likely not negatible for free.

llvm/test/Transforms/InstCombine/X86/x86-vector-shifts.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3732,7 +3732,6 @@ define <4 x i64> @test_avx2_psrl_0() {
37323732
ret <4 x i64> %16
37333733
}
37343734

3735-
; FIXME: Failure to peek through bitcasts to ensure psllq shift amount is within bounds.
37363735
define <2 x i64> @PR125228(<2 x i64> %v, <2 x i64> %s) {
37373736
; CHECK-LABEL: @PR125228(
37383737
; CHECK-NEXT: [[MASK:%.*]] = and <2 x i64> [[S:%.*]], splat (i64 63)
@@ -3741,7 +3740,8 @@ define <2 x i64> @PR125228(<2 x i64> %v, <2 x i64> %s) {
37413740
; CHECK-NEXT: [[CAST:%.*]] = bitcast <2 x i64> [[MASK]] to <16 x i8>
37423741
; CHECK-NEXT: [[PSRLDQ:%.*]] = shufflevector <16 x i8> [[CAST]], <16 x i8> poison, <16 x i32> <i32 8, i32 9, i32 10, i32 11, i32 12, i32 13, i32 14, i32 15, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison, i32 poison>
37433742
; CHECK-NEXT: [[CAST3:%.*]] = bitcast <16 x i8> [[PSRLDQ]] to <2 x i64>
3744-
; CHECK-NEXT: [[SLL1:%.*]] = call <2 x i64> @llvm.x86.sse2.psll.q(<2 x i64> [[V]], <2 x i64> [[CAST3]])
3743+
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <2 x i64> [[CAST3]], <2 x i64> poison, <2 x i32> zeroinitializer
3744+
; CHECK-NEXT: [[SLL1:%.*]] = shl <2 x i64> [[V]], [[TMP2]]
37453745
; CHECK-NEXT: [[SHUFP_UNCASTED:%.*]] = shufflevector <2 x i64> [[SLL0]], <2 x i64> [[SLL1]], <2 x i32> <i32 0, i32 3>
37463746
; CHECK-NEXT: ret <2 x i64> [[SHUFP_UNCASTED]]
37473747
;

llvm/test/Transforms/InstCombine/bitcast-known-bits.ll

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ define <16 x i8> @knownbits_bitcast_masked_shift(<16 x i8> %arg1, <16 x i8> %arg
1212
; CHECK-NEXT: [[BITCAST4:%.*]] = bitcast <16 x i8> [[OR]] to <8 x i16>
1313
; CHECK-NEXT: [[SHL5:%.*]] = shl nuw <8 x i16> [[BITCAST4]], splat (i16 2)
1414
; CHECK-NEXT: [[BITCAST6:%.*]] = bitcast <8 x i16> [[SHL5]] to <16 x i8>
15-
; CHECK-NEXT: [[AND7:%.*]] = and <16 x i8> [[BITCAST6]], splat (i8 -52)
16-
; CHECK-NEXT: ret <16 x i8> [[AND7]]
15+
; CHECK-NEXT: ret <16 x i8> [[BITCAST6]]
1716
;
1817
%and = and <16 x i8> %arg1, splat (i8 3)
1918
%and3 = and <16 x i8> %arg2, splat (i8 48)
@@ -33,8 +32,7 @@ define <16 x i8> @knownbits_shuffle_masked_nibble_shift(<16 x i8> %arg) {
3332
; CHECK-NEXT: [[BITCAST1:%.*]] = bitcast <16 x i8> [[SHUFFLEVECTOR]] to <8 x i16>
3433
; CHECK-NEXT: [[SHL:%.*]] = shl nuw <8 x i16> [[BITCAST1]], splat (i16 4)
3534
; CHECK-NEXT: [[BITCAST2:%.*]] = bitcast <8 x i16> [[SHL]] to <16 x i8>
36-
; CHECK-NEXT: [[AND3:%.*]] = and <16 x i8> [[BITCAST2]], splat (i8 -16)
37-
; CHECK-NEXT: ret <16 x i8> [[AND3]]
35+
; CHECK-NEXT: ret <16 x i8> [[BITCAST2]]
3836
;
3937
%and = and <16 x i8> %arg, splat (i8 15)
4038
%shufflevector = shufflevector <16 x i8> %and, <16 x i8> poison, <16 x i32> <i32 1, i32 0, i32 3, i32 2, i32 5, i32 4, i32 7, i32 6, i32 9, i32 8, i32 11, i32 10, i32 13, i32 12, i32 15, i32 14>
@@ -53,8 +51,7 @@ define <16 x i8> @knownbits_reverse_shuffle_masked_shift(<16 x i8> %arg) {
5351
; CHECK-NEXT: [[BITCAST1:%.*]] = bitcast <16 x i8> [[SHUFFLEVECTOR]] to <8 x i16>
5452
; CHECK-NEXT: [[SHL:%.*]] = shl nuw <8 x i16> [[BITCAST1]], splat (i16 4)
5553
; CHECK-NEXT: [[BITCAST2:%.*]] = bitcast <8 x i16> [[SHL]] to <16 x i8>
56-
; CHECK-NEXT: [[AND3:%.*]] = and <16 x i8> [[BITCAST2]], splat (i8 -16)
57-
; CHECK-NEXT: ret <16 x i8> [[AND3]]
54+
; CHECK-NEXT: ret <16 x i8> [[BITCAST2]]
5855
;
5956
%and = and <16 x i8> %arg, splat (i8 15)
6057
%shufflevector = shufflevector <16 x i8> %and, <16 x i8> poison, <16 x i32> <i32 3, i32 2, i32 1, i32 0, i32 7, i32 6, i32 5, i32 4, i32 11, i32 10, i32 9, i32 8, i32 15, i32 14, i32 13, i32 12>
@@ -70,8 +67,7 @@ define <16 x i8> @knownbits_extract_bit(<8 x i16> %arg) {
7067
; CHECK-SAME: <8 x i16> [[ARG:%.*]]) {
7168
; CHECK-NEXT: [[LSHR:%.*]] = lshr <8 x i16> [[ARG]], splat (i16 15)
7269
; CHECK-NEXT: [[BITCAST1:%.*]] = bitcast <8 x i16> [[LSHR]] to <16 x i8>
73-
; CHECK-NEXT: [[AND:%.*]] = and <16 x i8> [[BITCAST1]], splat (i8 1)
74-
; CHECK-NEXT: ret <16 x i8> [[AND]]
70+
; CHECK-NEXT: ret <16 x i8> [[BITCAST1]]
7571
;
7672
%lshr = lshr <8 x i16> %arg, splat (i16 15)
7773
%bitcast1 = bitcast <8 x i16> %lshr to <16 x i8>
@@ -88,7 +84,8 @@ define { i32, i1 } @knownbits_popcount_add_with_overflow(<2 x i64> %arg1, <2 x i
8884
; CHECK-NEXT: [[CALL9:%.*]] = tail call range(i64 0, 65) <2 x i64> @llvm.ctpop.v2i64(<2 x i64> [[ARG2]])
8985
; CHECK-NEXT: [[BITCAST10:%.*]] = bitcast <2 x i64> [[CALL9]] to <4 x i32>
9086
; CHECK-NEXT: [[EXTRACTELEMENT11:%.*]] = extractelement <4 x i32> [[BITCAST10]], i64 0
91-
; CHECK-NEXT: [[TMP1:%.*]] = tail call { i32, i1 } @llvm.uadd.with.overflow.i32(i32 [[EXTRACTELEMENT]], i32 [[EXTRACTELEMENT11]])
87+
; CHECK-NEXT: [[CALL12:%.*]] = add nuw nsw i32 [[EXTRACTELEMENT]], [[EXTRACTELEMENT11]]
88+
; CHECK-NEXT: [[TMP1:%.*]] = insertvalue { i32, i1 } { i32 poison, i1 false }, i32 [[CALL12]], 0
9289
; CHECK-NEXT: ret { i32, i1 } [[TMP1]]
9390
;
9491
%call = tail call <2 x i64> @llvm.ctpop.v2i64(<2 x i64> %arg1)
@@ -110,11 +107,7 @@ define <16 x i8> @knownbits_shuffle_add_shift_v32i8(<16 x i8> %arg1, <8 x i16> %
110107
; CHECK-NEXT: [[BITCAST11:%.*]] = bitcast <8 x i16> [[SHL10]] to <16 x i8>
111108
; CHECK-NEXT: [[ADD12:%.*]] = add <16 x i8> [[BITCAST11]], [[BITCAST7]]
112109
; CHECK-NEXT: [[ADD14:%.*]] = add <16 x i8> [[ADD12]], [[ARG1]]
113-
; CHECK-NEXT: [[BITCAST14:%.*]] = bitcast <16 x i8> [[ADD12]] to <8 x i16>
114-
; CHECK-NEXT: [[SHL15:%.*]] = shl <8 x i16> [[BITCAST14]], splat (i16 8)
115-
; CHECK-NEXT: [[BITCAST16:%.*]] = bitcast <8 x i16> [[SHL15]] to <16 x i8>
116-
; CHECK-NEXT: [[ADD13:%.*]] = add <16 x i8> [[ADD14]], [[BITCAST16]]
117-
; CHECK-NEXT: ret <16 x i8> [[ADD13]]
110+
; CHECK-NEXT: ret <16 x i8> [[ADD14]]
118111
;
119112
%shl6 = shl <8 x i16> %arg2, splat (i16 8)
120113
%bitcast7 = bitcast <8 x i16> %shl6 to <16 x i8>

0 commit comments

Comments
 (0)