From 65bec04295f1f678977151454ce852ba61149ac8 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Fri, 10 Dec 2021 16:37:49 +0100 Subject: [PATCH] [ConstantFold] Handle same type in ConstantFoldLoadThroughBitcast Usually the case where the types are the same ends up being handled fine because it's legal to do a trivial bitcast to the same type. However, this is not true for aggregate types. Short-circuit the whole code if the types match exactly to account for this. --- llvm/lib/Analysis/ConstantFolding.cpp | 3 +++ llvm/test/Transforms/InstSimplify/ConstProp/loads.ll | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 9324463..fcf4be4 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -352,6 +352,9 @@ Constant *llvm::ConstantFoldLoadThroughBitcast(Constant *C, Type *DestTy, const DataLayout &DL) { do { Type *SrcTy = C->getType(); + if (SrcTy == DestTy) + return C; + TypeSize DestSize = DL.getTypeSizeInBits(DestTy); TypeSize SrcSize = DL.getTypeSizeInBits(SrcTy); if (!TypeSize::isKnownGE(SrcSize, DestSize)) diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/loads.ll b/llvm/test/Transforms/InstSimplify/ConstProp/loads.ll index adde181..0938078 100644 --- a/llvm/test/Transforms/InstSimplify/ConstProp/loads.ll +++ b/llvm/test/Transforms/InstSimplify/ConstProp/loads.ll @@ -275,8 +275,7 @@ define {}* @test_trailing_zero_gep_index() { define { i64, i64 } @test_load_struct() { ; CHECK-LABEL: @test_load_struct( -; CHECK-NEXT: [[V:%.*]] = load { i64, i64 }, { i64, i64 }* @g3, align 8 -; CHECK-NEXT: ret { i64, i64 } [[V]] +; CHECK-NEXT: ret { i64, i64 } { i64 123, i64 112312312 } ; %v = load { i64, i64 }, { i64, i64 }* @g3 ret { i64, i64 } %v -- 2.7.4