[ConstantFold] Handle same type in ConstantFoldLoadThroughBitcast
authorNikita Popov <npopov@redhat.com>
Fri, 10 Dec 2021 15:37:49 +0000 (16:37 +0100)
committerNikita Popov <npopov@redhat.com>
Fri, 10 Dec 2021 15:39:50 +0000 (16:39 +0100)
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
llvm/test/Transforms/InstSimplify/ConstProp/loads.ll

index 9324463..fcf4be4 100644 (file)
@@ -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))
index adde181..0938078 100644 (file)
@@ -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