[llvm] 46db432 - [llvm-diff] Explicitly check ConstantArrays
Bill Wendling via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 22 12:24:09 PDT 2021
Author: Bill Wendling
Date: 2021-06-22T12:23:38-07:00
New Revision: 46db43240f0f24e76ab903db8086f53a7dddbedf
URL: https://github.com/llvm/llvm-project/commit/46db43240f0f24e76ab903db8086f53a7dddbedf
DIFF: https://github.com/llvm/llvm-project/commit/46db43240f0f24e76ab903db8086f53a7dddbedf.diff
LOG: [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.
Added:
llvm/test/tools/llvm-diff/initializers.ll
Modified:
llvm/tools/llvm-diff/DifferenceEngine.cpp
Removed:
################################################################################
diff --git a/llvm/test/tools/llvm-
diff /initializers.ll b/llvm/test/tools/llvm-
diff /initializers.ll
new file mode 100644
index 0000000000000..7f59e65e8b40a
--- /dev/null
+++ b/llvm/test/tools/llvm-
diff /initializers.ll
@@ -0,0 +1,12 @@
+; RUN: llvm-
diff %s %s
+
+; An initializer that has a GEP instruction in it won't match itself in
+; llvm-
diff unless the a deep comparison is done on the initializer.
+
+ at gv1 = external dso_local global [28 x i16], align 16
+ at 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
+
+define void @foo() {
+ %1 = getelementptr [2 x i16*], [2 x i16*]* @gv2, i64 0, i64 undef
+ ret void
+}
diff --git a/llvm/tools/llvm-
diff /DifferenceEngine.cpp b/llvm/tools/llvm-
diff /DifferenceEngine.cpp
index 326d572d7288e..cc9dce04de05d 100644
--- a/llvm/tools/llvm-
diff /DifferenceEngine.cpp
+++ b/llvm/tools/llvm-
diff /DifferenceEngine.cpp
@@ -404,6 +404,7 @@ class FunctionDifferenceEngine {
return false;
}
+public:
bool equivalentAsOperands(const Constant *L, const Constant *R) {
// Use equality as a preliminary filter.
if (L == R)
@@ -446,6 +447,24 @@ class FunctionDifferenceEngine {
return true;
}
+ // If L and R are ConstantArrays, compare the element count and types.
+ if (isa<ConstantArray>(L)) {
+ const ConstantArray *CAL = cast<ConstantArray>(L);
+ const ConstantArray *CAR = cast<ConstantArray>(R);
+ // Sometimes a type may be equivalent, but not uniquified---e.g. it may
+ // contain a GEP instruction. Do a deeper comparison of the types.
+ if (CAL->getType()->getNumElements() != CAR->getType()->getNumElements())
+ return false;
+
+ for (unsigned I = 0; I < CAL->getType()->getNumElements(); ++I) {
+ if (!equivalentAsOperands(CAL->getAggregateElement(I),
+ CAR->getAggregateElement(I)))
+ return false;
+ }
+
+ return true;
+ }
+
return false;
}
@@ -766,7 +785,8 @@ bool DifferenceEngine::equivalentAsOperands(const GlobalValue *L,
const GlobalVariable *GVR = cast<GlobalVariable>(R);
if (GVL->hasLocalLinkage() && GVL->hasUniqueInitializer() &&
GVR->hasLocalLinkage() && GVR->hasUniqueInitializer())
- return GVL->getInitializer() == GVR->getInitializer();
+ return FunctionDifferenceEngine(*this).equivalentAsOperands(
+ GVL->getInitializer(), GVR->getInitializer());
}
return L->getName() == R->getName();
More information about the llvm-commits
mailing list