Skip to content

Commit

Permalink
[CIR][LowerToLLVM] Fold base address computation if offset is zero
Browse files Browse the repository at this point in the history
  • Loading branch information
bcardosolopes committed Nov 2, 2024
1 parent 6038a43 commit 723e78a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
6 changes: 6 additions & 0 deletions clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<mlir::LLVM::BitcastOp>(
baseClassOp, resultType, adaptor.getDerivedAddr());
return mlir::success();
}

if (baseClassOp.getAssumeNotNull()) {
rewriter.replaceOpWithNewOp<mlir::LLVM::GEPOp>(
baseClassOp, resultType, byteType, derivedAddr, offset);
Expand Down
5 changes: 2 additions & 3 deletions clang/test/CIR/CodeGen/derived-cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
// LLVM-NOT: select i1
// LLVM: ret ptr
3 changes: 1 addition & 2 deletions clang/test/CIR/CodeGen/vtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 8 additions & 9 deletions clang/test/CIR/Lowering/derived-to-base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 723e78a

Please sign in to comment.