Skip to content

Commit

Permalink
[ConstantFold] Handle same type in ConstantFoldLoadThroughBitcast
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Dec 10, 2021
1 parent 5082c33 commit 65bec04
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 3 additions & 0 deletions llvm/lib/Analysis/ConstantFolding.cpp
Expand Up @@ -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))
Expand Down
3 changes: 1 addition & 2 deletions llvm/test/Transforms/InstSimplify/ConstProp/loads.ll
Expand Up @@ -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
Expand Down

0 comments on commit 65bec04

Please sign in to comment.