Skip to content

Commit 567eea9

Browse files
committed
Cherry-pick: Use memcmp for more array comparisons again
dlang/dmd#21647
1 parent 85d8c47 commit 567eea9

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

dmd/expressionsem.d

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3870,9 +3870,28 @@ private extern(C++) final class IsMemcmpableVisitor : Visitor
38703870
public:
38713871
bool result = false;
38723872

3873+
static Type loweredBaseElemOf(Type t)
3874+
{
3875+
t = t.baseElemOf(); // skip over static-array parents
3876+
switch (t.ty)
3877+
{
3878+
case Tvoid:
3879+
return Type.tuns8;
3880+
case Tpointer:
3881+
return Type.tsize_t;
3882+
default:
3883+
return t;
3884+
}
3885+
}
3886+
3887+
static bool isTriviallyMemcmpable(Type t)
3888+
{
3889+
return loweredBaseElemOf(t).isIntegral();
3890+
}
3891+
38733892
override void visit(Type t)
38743893
{
3875-
result = t.ty == Tvoid || (t.isScalar() && !t.isFloating());
3894+
result = isTriviallyMemcmpable(t);
38763895
}
38773896

38783897
override void visit(TypeStruct ts)
@@ -13510,15 +13529,20 @@ version (IN_LLVM)
1351013529
{
1351113530
Type t1n = t1.nextOf().toBasetype();
1351213531
Type t2n = t2.nextOf().toBasetype();
13513-
const t1nsz = t1n.size();
13514-
const t2nsz = t2n.size();
1351513532

13516-
if ((t1n.ty == Tvoid || (t1n.isScalar() && !t1n.isFloating())) &&
13517-
(t2n.ty == Tvoid || (t2n.isScalar() && !t1n.isFloating())) &&
13518-
t1nsz == t2nsz && t1n.isUnsigned() == t2n.isUnsigned())
13533+
if (t1n.size() != t2n.size())
13534+
return false;
13535+
13536+
if (IsMemcmpableVisitor.isTriviallyMemcmpable(t1n) &&
13537+
IsMemcmpableVisitor.isTriviallyMemcmpable(t2n))
1351913538
{
13520-
return true;
13539+
// due to int promotion, disallow small integers of diverging signed-ness
13540+
Type e1 = IsMemcmpableVisitor.loweredBaseElemOf(t1n);
13541+
Type e2 = IsMemcmpableVisitor.loweredBaseElemOf(t2n);
13542+
if ((e1.size() >= 4 && e2.size() >= 4) || e1.isUnsigned() == e2.isUnsigned())
13543+
return true;
1352113544
}
13545+
1352213546
if (t1n.constOf() != t2n.constOf())
1352313547
{
1352413548
return false;
@@ -13533,7 +13557,7 @@ version (IN_LLVM)
1353313557
if (global.params.useTypeInfo && Type.dtypeinfo)
1353413558
semanticTypeInfo(sc, ts);
1353513559

13536-
auto v = new IsMemcmpableVisitor();
13560+
scope v = new IsMemcmpableVisitor();
1353713561
ts.accept(v);
1353813562
return v.result;
1353913563
}

0 commit comments

Comments
 (0)