Skip to content

Commit 46db432

Browse files
committed
[llvm-diff] Explicitly check ConstantArrays
Global initializers may be ConstantArrays. They need to be checked explicitly, because different-yet-still-equivalent type names may be used for each, and/or a GEP instruction may appear in one.
1 parent ab60028 commit 46db432

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; RUN: llvm-diff %s %s
2+
3+
; An initializer that has a GEP instruction in it won't match itself in
4+
; llvm-diff unless the a deep comparison is done on the initializer.
5+
6+
@gv1 = external dso_local global [28 x i16], align 16
7+
@gv2 = private unnamed_addr constant [2 x i16*] [i16* getelementptr inbounds ([28 x i16], [28 x i16]* @gv1, i32 0, i32 0), i16* poison], align 16
8+
9+
define void @foo() {
10+
%1 = getelementptr [2 x i16*], [2 x i16*]* @gv2, i64 0, i64 undef
11+
ret void
12+
}

llvm/tools/llvm-diff/DifferenceEngine.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ class FunctionDifferenceEngine {
404404
return false;
405405
}
406406

407+
public:
407408
bool equivalentAsOperands(const Constant *L, const Constant *R) {
408409
// Use equality as a preliminary filter.
409410
if (L == R)
@@ -446,6 +447,24 @@ class FunctionDifferenceEngine {
446447
return true;
447448
}
448449

450+
// If L and R are ConstantArrays, compare the element count and types.
451+
if (isa<ConstantArray>(L)) {
452+
const ConstantArray *CAL = cast<ConstantArray>(L);
453+
const ConstantArray *CAR = cast<ConstantArray>(R);
454+
// Sometimes a type may be equivalent, but not uniquified---e.g. it may
455+
// contain a GEP instruction. Do a deeper comparison of the types.
456+
if (CAL->getType()->getNumElements() != CAR->getType()->getNumElements())
457+
return false;
458+
459+
for (unsigned I = 0; I < CAL->getType()->getNumElements(); ++I) {
460+
if (!equivalentAsOperands(CAL->getAggregateElement(I),
461+
CAR->getAggregateElement(I)))
462+
return false;
463+
}
464+
465+
return true;
466+
}
467+
449468
return false;
450469
}
451470

@@ -766,7 +785,8 @@ bool DifferenceEngine::equivalentAsOperands(const GlobalValue *L,
766785
const GlobalVariable *GVR = cast<GlobalVariable>(R);
767786
if (GVL->hasLocalLinkage() && GVL->hasUniqueInitializer() &&
768787
GVR->hasLocalLinkage() && GVR->hasUniqueInitializer())
769-
return GVL->getInitializer() == GVR->getInitializer();
788+
return FunctionDifferenceEngine(*this).equivalentAsOperands(
789+
GVL->getInitializer(), GVR->getInitializer());
770790
}
771791

772792
return L->getName() == R->getName();

0 commit comments

Comments
 (0)