Skip to content

Commit

Permalink
[ConstantFold][GlobalOpt] Don't create x86_mmx null value
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Dec 21, 2021
1 parent 9be6728 commit 2926d6d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,8 @@ Constant *llvm::ConstantFoldLoadFromConstPtr(Constant *C, Type *Ty,
// is all undef or zero, we know what it loads.
if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(C))) {
if (GV->isConstant() && GV->hasDefinitiveInitializer()) {
if (GV->getInitializer()->isNullValue())
if (GV->getInitializer()->isNullValue() && !Ty->isX86_MMXTy() &&
!Ty->isX86_AMXTy())
return Constant::getNullValue(Ty);
if (isa<UndefValue>(GV->getInitializer()))
return UndefValue::get(Ty);
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Transforms/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,9 @@ static bool CleanupConstantGlobalUsers(GlobalVariable *GV,
else if (auto *LI = dyn_cast<LoadInst>(U)) {
// A load from zeroinitializer is always zeroinitializer, regardless of
// any applied offset.
if (Init->isNullValue()) {
LI->replaceAllUsesWith(Constant::getNullValue(LI->getType()));
Type *Ty = LI->getType();
if (Init->isNullValue() && !Ty->isX86_MMXTy() && !Ty->isX86_AMXTy()) {
LI->replaceAllUsesWith(Constant::getNullValue(Ty));
EraseFromParent(LI);
continue;
}
Expand All @@ -316,8 +317,7 @@ static bool CleanupConstantGlobalUsers(GlobalVariable *GV,
PtrOp = PtrOp->stripAndAccumulateConstantOffsets(
DL, Offset, /* AllowNonInbounds */ true);
if (PtrOp == GV) {
if (auto *Value = ConstantFoldLoadFromConst(Init, LI->getType(),
Offset, DL)) {
if (auto *Value = ConstantFoldLoadFromConst(Init, Ty, Offset, DL)) {
LI->replaceAllUsesWith(Value);
EraseFromParent(LI);
}
Expand Down
12 changes: 12 additions & 0 deletions llvm/test/Transforms/GlobalOpt/x86_mmx_load.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -globalopt < %s | FileCheck %s

@m64 = internal global <1 x i64> zeroinitializer

define i32 @load_mmx() {
; CHECK-LABEL: @load_mmx(
; CHECK-NEXT: ret i32 0
;
%temp = load x86_mmx, x86_mmx* bitcast (<1 x i64>* @m64 to x86_mmx*)
ret i32 0
}
13 changes: 13 additions & 0 deletions llvm/test/Transforms/InstSimplify/ConstProp/loads.ll
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,16 @@ define { i64, i64 } @test_load_struct() {
%v = load { i64, i64 }, { i64, i64 }* @g3
ret { i64, i64 } %v
}

@m64 = internal constant [2 x i64] zeroinitializer
@idx = external global i32

; This should not try to create an x86_mmx null value.
define x86_mmx @load_mmx() {
; CHECK-LABEL: @load_mmx(
; 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
; CHECK-NEXT: ret x86_mmx [[TEMP]]
;
%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*)
ret x86_mmx %temp
}

0 comments on commit 2926d6d

Please sign in to comment.