diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index 03d6d227b470..559fd3d9b702 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -837,6 +837,12 @@ class CIRBaseClassAddrOpLowering adaptor.getOffset().getZExtValue()}; mlir::Type byteType = mlir::IntegerType::get(resultType.getContext(), 8, mlir::IntegerType::Signless); + if (adaptor.getOffset().getZExtValue() == 0) { + rewriter.replaceOpWithNewOp( + baseClassOp, resultType, adaptor.getDerivedAddr()); + return mlir::success(); + } + if (baseClassOp.getAssumeNotNull()) { rewriter.replaceOpWithNewOp( baseClassOp, resultType, byteType, derivedAddr, offset); diff --git a/clang/test/CIR/CodeGen/derived-cast.cpp b/clang/test/CIR/CodeGen/derived-cast.cpp index c65bbe2dcc38..28109f553a5e 100644 --- a/clang/test/CIR/CodeGen/derived-cast.cpp +++ b/clang/test/CIR/CodeGen/derived-cast.cpp @@ -36,6 +36,5 @@ A *B::getAsA() { // LLVM: store ptr %[[VAL_2:.*]], ptr %[[VAL_0:.*]], align 8 // LLVM: %[[VAL_3:.*]] = load ptr, ptr %[[VAL_0]], align 8 // LLVM: %[[VAL_4:.*]] = getelementptr i8, ptr %[[VAL_3]], i32 -4 -// LLVM: %[[VAL_5:.*]] = icmp eq ptr %[[VAL_4]], null, -// LLVM: %[[VAL_6:.*]] = getelementptr i8, ptr %[[VAL_4]], i32 0, -// LLVM: %[[VAL_7:.*]] = select i1 %[[VAL_5]], ptr %[[VAL_4]], ptr %[[VAL_6]], \ No newline at end of file +// LLVM-NOT: select i1 +// LLVM: ret ptr \ No newline at end of file diff --git a/clang/test/CIR/CodeGen/vtt.cpp b/clang/test/CIR/CodeGen/vtt.cpp index 797e94475ede..16203276c544 100644 --- a/clang/test/CIR/CodeGen/vtt.cpp +++ b/clang/test/CIR/CodeGen/vtt.cpp @@ -143,8 +143,7 @@ int f() { // LLVM: %[[THIS:.*]] = load ptr, ptr %2, align 8 // LLVM: %[[BASE_A:.*]] = getelementptr i8, ptr %[[THIS]], i32 40 // LLVM: call void @_ZN1AC2Ev(ptr %[[BASE_A]]) -// LLVM: %[[BASE_B:.*]] = getelementptr i8, ptr %[[THIS]], i32 0 -// LLVM: call void @_ZN1BC2Ev(ptr %[[BASE_B]], ptr getelementptr inbounds ([7 x ptr], ptr @_ZTT1D, i32 0, i32 1)) +// LLVM: call void @_ZN1BC2Ev(ptr %[[THIS]], ptr getelementptr inbounds ([7 x ptr], ptr @_ZTT1D, i32 0, i32 1)) // LLVM: %[[BASE_C:.*]] = getelementptr i8, ptr %[[THIS]], i32 16 // LLVM: call void @_ZN1CC2Ev(ptr %[[BASE_C]], ptr getelementptr inbounds ([7 x ptr], ptr @_ZTT1D, i32 0, i32 3)) // LLVM: ret void diff --git a/clang/test/CIR/Lowering/derived-to-base.cpp b/clang/test/CIR/Lowering/derived-to-base.cpp index ef02ed0639b0..6e29c16c6608 100644 --- a/clang/test/CIR/Lowering/derived-to-base.cpp +++ b/clang/test/CIR/Lowering/derived-to-base.cpp @@ -8,21 +8,20 @@ void test_multi_base() { Derived d; Base2& bref = d; // no null check needed - // LLVM: %7 = getelementptr i8, ptr %1, i32 4 + // LLVM: getelementptr i8, ptr %[[D:.*]], i32 4 Base2* bptr = &d; // has null pointer check - // LLVM: %8 = icmp eq ptr %1, null - // LLVM: %9 = getelementptr i8, ptr %1, i32 4 - // LLVM: %10 = select i1 %8, ptr %1, ptr %9 + // LLVM: %[[CHECK:.*]] = icmp eq ptr %[[D]], null + // LLVM: %[[BPTR:.*]] = getelementptr i8, ptr %[[D]], i32 4 + // LLVM: select i1 %[[CHECK]], ptr %[[D]], ptr %[[BPTR]] int a = d.a; - // LLVM: %11 = getelementptr i8, ptr %1, i32 0 - // LLVM: %12 = getelementptr %struct.Base1, ptr %11, i32 0, i32 0 + // LLVM: getelementptr %struct.Base1, ptr %[[D]], i32 0, i32 0 int b = d.b; - // LLVM: %14 = getelementptr i8, ptr %1, i32 4 - // LLVM: %15 = getelementptr %struct.Base2, ptr %14, i32 0, i32 0 + // LLVM: %[[BASE2_OFFSET:.*]] = getelementptr i8, ptr %[[D]], i32 4 + // LLVM: %[[BASE2:.*]] = getelementptr %struct.Base2, ptr %[[BASE2_OFFSET]], i32 0, i32 0 int c = d.c; - // LLVM: %17 = getelementptr %struct.Derived, ptr %1, i32 0, i32 2 + // LLVM: getelementptr %struct.Derived, ptr %[[D]], i32 0, i32 2 }