This patch canonicalizes getelementptr instructions with constant indices to use the `i8` source element type. This makes it easier for optimizations to recognize that two GEPs are identical, because they don't need to see past many different ways to express the same offset. This is a first step towards https://discourse.llvm.org/t/rfc-replacing-getelementptr-with-ptradd/68699. This is limited to constant GEPs only for now, as they have a clear canonical form, while we're not yet sure how exactly to deal with variable indices. The test llvm/test/Transforms/PhaseOrdering/switch_with_geps.ll gives two representative examples of the kind of optimization improvement we expect from this change. In the first test SimplifyCFG can now realize that all switch branches are actually the same. In the second test it can convert it into simple arithmetic. These are representative of common optimization failures we see in Rust. Fixes https://github.com/llvm/llvm-project/issues/69841.
103 lines
2.8 KiB
LLVM
103 lines
2.8 KiB
LLVM
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
|
|
; RUN: opt -S -passes='default<O1>' < %s | FileCheck %s
|
|
; RUN: opt -S -passes='default<O2>' < %s | FileCheck %s
|
|
; RUN: opt -S -passes='default<O3>' < %s | FileCheck %s
|
|
|
|
target datalayout = "n64"
|
|
|
|
%"OpKind::Zero" = type { [1 x i32], i32 }
|
|
%"OpKind::One" = type { [1 x i32], i32, i16, [1 x i16] }
|
|
%"OpKind::Two" = type { [1 x i32], i32, i16, i16 }
|
|
%"OpKind::Three" = type { [1 x i32], i32, i16, i16, i16, [1 x i16] }
|
|
|
|
define i32 @test(ptr %ptr) {
|
|
; CHECK-LABEL: define i32 @test(
|
|
; CHECK-SAME: ptr nocapture readonly [[PTR:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
|
|
; CHECK-NEXT: start:
|
|
; CHECK-NEXT: [[PHI:%.*]] = getelementptr inbounds i8, ptr [[PTR]], i64 4
|
|
; CHECK-NEXT: [[RET:%.*]] = load i32, ptr [[PHI]], align 4
|
|
; CHECK-NEXT: ret i32 [[RET]]
|
|
;
|
|
start:
|
|
%t = load i32, ptr %ptr, align 4
|
|
switch i32 %t, label %default [
|
|
i32 0, label %bb4
|
|
i32 1, label %bb5
|
|
i32 2, label %bb6
|
|
i32 3, label %bb7
|
|
]
|
|
|
|
default:
|
|
unreachable
|
|
|
|
bb4:
|
|
%gep0 = getelementptr inbounds %"OpKind::Zero", ptr %ptr, i64 0, i32 1
|
|
br label %exit
|
|
|
|
bb5:
|
|
%gep1 = getelementptr inbounds %"OpKind::One", ptr %ptr, i64 0, i32 1
|
|
br label %exit
|
|
|
|
bb6:
|
|
%gep2 = getelementptr inbounds %"OpKind::Two", ptr %ptr, i64 0, i32 1
|
|
br label %exit
|
|
|
|
bb7:
|
|
%gep3 = getelementptr inbounds %"OpKind::Three", ptr %ptr, i64 0, i32 1
|
|
br label %exit
|
|
|
|
exit:
|
|
%phi = phi ptr [ %gep3, %bb7 ], [ %gep2, %bb6 ], [ %gep1, %bb5 ], [ %gep0, %bb4 ]
|
|
%ret = load i32, ptr %phi, align 4
|
|
ret i32 %ret
|
|
}
|
|
|
|
%X = type { i64, i64, i64, i64, i64, i64 }
|
|
|
|
define void @test2(ptr %self, i64 %v, i64 %ix) {
|
|
; CHECK-LABEL: define void @test2(
|
|
; CHECK-SAME: ptr nocapture writeonly [[SELF:%.*]], i64 [[V:%.*]], i64 [[IX:%.*]]) local_unnamed_addr #[[ATTR1:[0-9]+]] {
|
|
; CHECK-NEXT: start:
|
|
; CHECK-NEXT: [[SWITCH_TABLEIDX:%.*]] = shl i64 [[IX]], 3
|
|
; CHECK-NEXT: [[GEP5:%.*]] = getelementptr inbounds i8, ptr [[SELF]], i64 [[SWITCH_TABLEIDX]]
|
|
; CHECK-NEXT: store i64 [[V]], ptr [[GEP5]], align 8
|
|
; CHECK-NEXT: ret void
|
|
;
|
|
start:
|
|
switch i64 %ix, label %default [
|
|
i64 1, label %bb3
|
|
i64 2, label %bb4
|
|
i64 3, label %bb5
|
|
i64 4, label %bb6
|
|
i64 5, label %bb7
|
|
]
|
|
|
|
default:
|
|
unreachable
|
|
|
|
bb3:
|
|
%gep1 = getelementptr inbounds %X, ptr %self, i64 0, i32 1
|
|
br label %bb8
|
|
|
|
bb4:
|
|
%gep2 = getelementptr inbounds %X, ptr %self, i64 0, i32 2
|
|
br label %bb8
|
|
|
|
bb5:
|
|
%gep3 = getelementptr inbounds %X, ptr %self, i64 0, i32 3
|
|
br label %bb8
|
|
|
|
bb6:
|
|
%gep4 = getelementptr inbounds %X, ptr %self, i64 0, i32 4
|
|
br label %bb8
|
|
|
|
bb7:
|
|
%gep5 = getelementptr inbounds %X, ptr %self, i64 0, i32 5
|
|
br label %bb8
|
|
|
|
bb8:
|
|
%ptr = phi ptr [ %gep5, %bb7 ], [ %gep4, %bb6 ], [ %gep3, %bb5 ], [ %gep2, %bb4 ], [ %gep1, %bb3 ]
|
|
store i64 %v, ptr %ptr, align 8
|
|
ret void
|
|
}
|