Skip to content

Commit 2926d6d

Browse files
committed
[ConstantFold][GlobalOpt] Don't create x86_mmx null value
This fixes the assertion failure reported at https://reviews.llvm.org/D114889#3198921 with a straightforward check, until the cleaner fix in D115924 can be reapplied.
1 parent 9be6728 commit 2926d6d

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
708708
// is all undef or zero, we know what it loads.
709709
if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(C))) {
710710
if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
711-
if (GV->getInitializer()->isNullValue())
711+
if (GV->getInitializer()->isNullValue() && !Ty->isX86_MMXTy() &&
712+
!Ty->isX86_AMXTy())
712713
return Constant::getNullValue(Ty);
713714
if (isa<UndefValue>(GV->getInitializer()))
714715
return UndefValue::get(Ty);

llvm/lib/Transforms/IPO/GlobalOpt.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,9 @@ static bool CleanupConstantGlobalUsers(GlobalVariable *GV,
305305
else if (auto *LI = dyn_cast<LoadInst>(U)) {
306306
// A load from zeroinitializer is always zeroinitializer, regardless of
307307
// any applied offset.
308-
if (Init->isNullValue()) {
309-
LI->replaceAllUsesWith(Constant::getNullValue(LI->getType()));
308+
Type *Ty = LI->getType();
309+
if (Init->isNullValue() && !Ty->isX86_MMXTy() && !Ty->isX86_AMXTy()) {
310+
LI->replaceAllUsesWith(Constant::getNullValue(Ty));
310311
EraseFromParent(LI);
311312
continue;
312313
}
@@ -316,8 +317,7 @@ static bool CleanupConstantGlobalUsers(GlobalVariable *GV,
316317
PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
317318
DL, Offset, /* AllowNonInbounds */ true);
318319
if (PtrOp == GV) {
319-
if (auto *Value = ConstantFoldLoadFromConst(Init, LI->getType(),
320-
Offset, DL)) {
320+
if (auto *Value = ConstantFoldLoadFromConst(Init, Ty, Offset, DL)) {
321321
LI->replaceAllUsesWith(Value);
322322
EraseFromParent(LI);
323323
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -S -globalopt < %s | FileCheck %s
3+
4+
@m64 = internal global <1 x i64> zeroinitializer
5+
6+
define i32 @load_mmx() {
7+
; CHECK-LABEL: @load_mmx(
8+
; CHECK-NEXT: ret i32 0
9+
;
10+
%temp = load x86_mmx, x86_mmx* bitcast (<1 x i64>* @m64 to x86_mmx*)
11+
ret i32 0
12+
}

llvm/test/Transforms/InstSimplify/ConstProp/loads.ll

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,16 @@ define { i64, i64 } @test_load_struct() {
280280
%v = load { i64, i64 }, { i64, i64 }* @g3
281281
ret { i64, i64 } %v
282282
}
283+
284+
@m64 = internal constant [2 x i64] zeroinitializer
285+
@idx = external global i32
286+
287+
; This should not try to create an x86_mmx null value.
288+
define x86_mmx @load_mmx() {
289+
; CHECK-LABEL: @load_mmx(
290+
; CHECK-NEXT: [[TEMP:%.*]] = load x86_mmx, x86_mmx* bitcast (i64* getelementptr ([2 x i64], [2 x i64]* @m64, i64 0, i64 ptrtoint (i32* @idx to i64)) to x86_mmx*), align 8
291+
; CHECK-NEXT: ret x86_mmx [[TEMP]]
292+
;
293+
%temp = load x86_mmx, x86_mmx* bitcast (i64* getelementptr ([2 x i64], [2 x i64]* @m64, i64 0, i64 ptrtoint (i32* @idx to i64)) to x86_mmx*)
294+
ret x86_mmx %temp
295+
}

0 commit comments

Comments
 (0)