Skip to content

Commit dfb4916

Browse files
author
git apple-llvm automerger
committed
Merge commit '57384aeb3743' from llvm.org/main into next
2 parents 31c9c4e + 57384ae commit dfb4916

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

llvm/lib/IR/ConstantFold.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ static Constant *ExtractConstantBytes(Constant *C, unsigned ByteStart,
295295
}
296296
}
297297

298+
static Constant *foldMaybeUndesirableCast(unsigned opc, Constant *V,
299+
Type *DestTy) {
300+
return ConstantExpr::isDesirableCastOp(opc)
301+
? ConstantExpr::getCast(opc, V, DestTy)
302+
: ConstantFoldCastInstruction(opc, V, DestTy);
303+
}
304+
298305
Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
299306
Type *DestTy) {
300307
if (isa<PoisonValue>(V))
@@ -320,7 +327,7 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
320327
if (CE->isCast()) {
321328
// Try hard to fold cast of cast because they are often eliminable.
322329
if (unsigned newOpc = foldConstantCastPair(opc, CE, DestTy))
323-
return ConstantExpr::getCast(newOpc, CE->getOperand(0), DestTy);
330+
return foldMaybeUndesirableCast(newOpc, CE->getOperand(0), DestTy);
324331
} else if (CE->getOpcode() == Instruction::GetElementPtr &&
325332
// Do not fold addrspacecast (gep 0, .., 0). It might make the
326333
// addrspacecast uncanonicalized.
@@ -357,18 +364,22 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V,
357364
Type *DstEltTy = DestVecTy->getElementType();
358365
// Fast path for splatted constants.
359366
if (Constant *Splat = V->getSplatValue()) {
367+
Constant *Res = foldMaybeUndesirableCast(opc, Splat, DstEltTy);
368+
if (!Res)
369+
return nullptr;
360370
return ConstantVector::getSplat(
361-
cast<VectorType>(DestTy)->getElementCount(),
362-
ConstantExpr::getCast(opc, Splat, DstEltTy));
371+
cast<VectorType>(DestTy)->getElementCount(), Res);
363372
}
364373
SmallVector<Constant *, 16> res;
365374
Type *Ty = IntegerType::get(V->getContext(), 32);
366375
for (unsigned i = 0,
367376
e = cast<FixedVectorType>(V->getType())->getNumElements();
368377
i != e; ++i) {
369-
Constant *C =
370-
ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
371-
res.push_back(ConstantExpr::getCast(opc, C, DstEltTy));
378+
Constant *C = ConstantExpr::getExtractElement(V, ConstantInt::get(Ty, i));
379+
Constant *Casted = foldMaybeUndesirableCast(opc, C, DstEltTy);
380+
if (!Casted)
381+
return nullptr;
382+
res.push_back(Casted);
372383
}
373384
return ConstantVector::get(res);
374385
}

llvm/test/CodeGen/AArch64/arm64-codegen-prepare-extload.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,8 @@ entry:
338338
; OPTALL: [[LD:%[a-zA-Z_0-9-]+]] = load i16, ptr %addr
339339
;
340340
; OPT-NEXT: [[SEXT:%[a-zA-Z_0-9-]+]] = sext i16 [[LD]] to i32
341-
; OPT-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nuw nsw i32 [[SEXT]], zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i32], ptr @c, i64 0, i64 1), ptr @a) to i32)
341+
; OPT-NEXT: [[SEXT2:%[a-zA-Z_0-9-]+]] = sext i16 zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i32], ptr @c, i64 0, i64 1), ptr @a) to i16) to i32
342+
; OPT-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = add nuw nsw i32 [[SEXT]], [[SEXT2]]
342343
;
343344
; DISABLE-NEXT: [[ADD:%[a-zA-Z_0-9-]+]] = add nuw nsw i16 [[LD]], zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i32], ptr @c, i64 0, i64 1), ptr @a) to i16)
344345
; DISABLE-NEXT: [[RES:%[a-zA-Z_0-9-]+]] = sext i16 [[ADD]] to i32

llvm/test/CodeGen/X86/codegen-prepare-extload.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,8 @@ define i32 @promotionOfArgEndsUpInValue(ptr %addr) {
546546
; OPT-NEXT: entry:
547547
; OPT-NEXT: [[VAL:%.*]] = load i16, ptr [[ADDR]], align 2
548548
; OPT-NEXT: [[CONV3:%.*]] = sext i16 [[VAL]] to i32
549-
; OPT-NEXT: [[ADD:%.*]] = add nuw nsw i32 [[CONV3]], zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i32], ptr @c, i64 0, i64 1), ptr @a) to i32)
549+
; OPT-NEXT: [[PROMOTED:%.*]] = sext i16 zext (i1 icmp ne (ptr getelementptr inbounds ([2 x i32], ptr @c, i64 0, i64 1), ptr @a) to i16) to i32
550+
; OPT-NEXT: [[ADD:%.*]] = add nuw nsw i32 [[CONV3]], [[PROMOTED]]
550551
; OPT-NEXT: ret i32 [[ADD]]
551552
;
552553
; DISABLE-LABEL: define i32 @promotionOfArgEndsUpInValue(

0 commit comments

Comments
 (0)