Skip to content

Commit 26ee8af

Browse files
committed
[CodeExtractor] Don't create bitcasts when inserting lifetime markers (NFCI)
Lifetime marker intrinsics support any pointer type, so CodeExtractor does not need to bitcast to `i8*` in order to use these markers.
1 parent 2f95c50 commit 26ee8af

File tree

5 files changed

+21
-45
lines changed

5 files changed

+21
-45
lines changed

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,50 +1024,31 @@ static void insertLifetimeMarkersSurroundingCall(
10241024
Module *M, ArrayRef<Value *> LifetimesStart, ArrayRef<Value *> LifetimesEnd,
10251025
CallInst *TheCall) {
10261026
LLVMContext &Ctx = M->getContext();
1027-
auto Int8PtrTy = Type::getInt8PtrTy(Ctx);
10281027
auto NegativeOne = ConstantInt::getSigned(Type::getInt64Ty(Ctx), -1);
10291028
Instruction *Term = TheCall->getParent()->getTerminator();
10301029

1031-
// The memory argument to a lifetime marker must be a i8*. Cache any bitcasts
1032-
// needed to satisfy this requirement so they may be reused.
1033-
DenseMap<Value *, Value *> Bitcasts;
1034-
10351030
// Emit lifetime markers for the pointers given in \p Objects. Insert the
10361031
// markers before the call if \p InsertBefore, and after the call otherwise.
1037-
auto insertMarkers = [&](Function *MarkerFunc, ArrayRef<Value *> Objects,
1032+
auto insertMarkers = [&](Intrinsic::ID IID, ArrayRef<Value *> Objects,
10381033
bool InsertBefore) {
10391034
for (Value *Mem : Objects) {
10401035
assert((!isa<Instruction>(Mem) || cast<Instruction>(Mem)->getFunction() ==
10411036
TheCall->getFunction()) &&
10421037
"Input memory not defined in original function");
1043-
Value *&MemAsI8Ptr = Bitcasts[Mem];
1044-
if (!MemAsI8Ptr) {
1045-
if (Mem->getType() == Int8PtrTy)
1046-
MemAsI8Ptr = Mem;
1047-
else
1048-
MemAsI8Ptr =
1049-
CastInst::CreatePointerCast(Mem, Int8PtrTy, "lt.cast", TheCall);
1050-
}
1051-
1052-
auto Marker = CallInst::Create(MarkerFunc, {NegativeOne, MemAsI8Ptr});
1038+
assert(Mem->getType()->isPointerTy() && "Expected pointer to memory");
1039+
Function *MarkerFunc =
1040+
llvm::Intrinsic::getDeclaration(M, IID, Mem->getType());
1041+
auto Marker = CallInst::Create(MarkerFunc, {NegativeOne, Mem});
10531042
if (InsertBefore)
10541043
Marker->insertBefore(TheCall);
10551044
else
10561045
Marker->insertBefore(Term);
10571046
}
10581047
};
10591048

1060-
if (!LifetimesStart.empty()) {
1061-
auto StartFn = llvm::Intrinsic::getDeclaration(
1062-
M, llvm::Intrinsic::lifetime_start, Int8PtrTy);
1063-
insertMarkers(StartFn, LifetimesStart, /*InsertBefore=*/true);
1064-
}
1065-
1066-
if (!LifetimesEnd.empty()) {
1067-
auto EndFn = llvm::Intrinsic::getDeclaration(
1068-
M, llvm::Intrinsic::lifetime_end, Int8PtrTy);
1069-
insertMarkers(EndFn, LifetimesEnd, /*InsertBefore=*/false);
1070-
}
1049+
insertMarkers(Intrinsic::lifetime_start, LifetimesStart,
1050+
/*InsertBefore=*/true);
1051+
insertMarkers(Intrinsic::lifetime_end, LifetimesEnd, /*InsertBefore=*/false);
10711052
}
10721053

10731054
/// emitCallAndSwitchStatement - This method sets up the caller side by adding

llvm/test/Transforms/CodeExtractor/PartialInlineInvokeProducesOutVal.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ bb5: ; preds = %bb4, %bb1, %bb
2626
; CHECK-LABEL: bb:
2727
; CHECK-NEXT: [[CALL26LOC:%.*]] = alloca i8*
2828
; CHECK-LABEL: codeRepl.i:
29-
; CHECK-NEXT: %lt.cast.i = bitcast i8** [[CALL26LOC]] to i8*
30-
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* %lt.cast.i)
29+
; CHECK-NEXT: call void @llvm.lifetime.start.p0p0i8(i64 -1, i8** [[CALL26LOC]])
3130
; CHECK-NEXT: call void @bar.1.bb1(i8** [[CALL26LOC]])
3231
; CHECK-NEXT: %call26.reload.i = load i8*, i8** [[CALL26LOC]]
33-
; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 -1, i8* %lt.cast.i)
32+
; CHECK-NEXT: call void @llvm.lifetime.end.p0p0i8(i64 -1, i8** [[CALL26LOC]])
3433
define i8* @dummy_caller(i32 %arg) {
3534
bb:
3635
%tmp = tail call i8* @bar(i32 %arg)

llvm/test/Transforms/HotColdSplit/lifetime-markers-on-inputs-1.ll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ normalPath:
2929
ret void
3030

3131
; CHECK-LABEL: codeRepl:
32-
; CHECK: [[local1_cast:%.*]] = bitcast i256* %local1 to i8*
33-
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[local1_cast]])
34-
; CHECK-NEXT: [[local2_cast:%.*]] = bitcast i256* %local2 to i8*
35-
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[local2_cast]])
32+
; CHECK: call void @llvm.lifetime.start.p0i256(i64 -1, i256* %local1)
33+
; CHECK-NEXT: call void @llvm.lifetime.start.p0i256(i64 -1, i256* %local2)
3634
; CHECK-NEXT: call i1 @foo.cold.1(i8* %local1_cast, i8* %local2_cast)
3735
; CHECK-NEXT: br i1
3836

@@ -61,4 +59,4 @@ outlinedPathExit:
6159
}
6260

6361
; CHECK-LABEL: define {{.*}}@foo.cold.1(
64-
; CHECK-NOT: @llvm.lifetime
62+
; CHECK-NOT: call void @llvm.lifetime

llvm/test/Transforms/HotColdSplit/lifetime-markers-on-inputs-2.ll

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ declare void @use(i8*)
3737
define void @only_lifetime_start_is_cold() {
3838
; CHECK-LABEL: @only_lifetime_start_is_cold(
3939
; CHECK-NEXT: entry:
40-
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
40+
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
4141
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
4242
; CHECK-NEXT: br i1 undef, label [[CODEREPL:%.*]], label [[NO_EXTRACT1:%.*]]
4343
; CHECK: codeRepl:
44-
; CHECK-NEXT: [[LT_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
45-
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 -1, i8* [[LT_CAST]])
46-
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @only_lifetime_start_is_cold.cold.1(i8* [[LOCAL1_CAST]]) #3
44+
; CHECK-NEXT: call void @llvm.lifetime.start.p0i256(i64 -1, i256* [[LOCAL1]])
45+
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @only_lifetime_start_is_cold.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3:#.*]]
4746
; CHECK-NEXT: br i1 [[TARGETBLOCK]], label [[NO_EXTRACT1]], label [[EXIT:%.*]]
4847
; CHECK: no-extract1:
4948
; CHECK-NEXT: br label [[EXIT]]
@@ -98,15 +97,15 @@ exit:
9897
define void @only_lifetime_end_is_cold() {
9998
; CHECK-LABEL: @only_lifetime_end_is_cold(
10099
; CHECK-NEXT: entry:
101-
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
100+
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
102101
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
103102
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 1, i8* [[LOCAL1_CAST]])
104103
; CHECK-NEXT: br i1 undef, label [[NO_EXTRACT1:%.*]], label [[CODEREPL:%.*]]
105104
; CHECK: no-extract1:
106105
; CHECK-NEXT: call void @llvm.lifetime.end.p0i8(i64 1, i8* [[LOCAL1_CAST]])
107106
; CHECK-NEXT: br label [[EXIT:%.*]]
108107
; CHECK: codeRepl:
109-
; CHECK-NEXT: call void @only_lifetime_end_is_cold.cold.1(i8* [[LOCAL1_CAST]]) #3
108+
; CHECK-NEXT: call void @only_lifetime_end_is_cold.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3]]
110109
; CHECK-NEXT: br label [[EXIT]]
111110
; CHECK: exit:
112111
; CHECK-NEXT: ret void
@@ -138,15 +137,15 @@ exit:
138137
define void @do_not_lift_lifetime_end() {
139138
; CHECK-LABEL: @do_not_lift_lifetime_end(
140139
; CHECK-NEXT: entry:
141-
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256
140+
; CHECK-NEXT: [[LOCAL1:%.*]] = alloca i256, align 8
142141
; CHECK-NEXT: [[LOCAL1_CAST:%.*]] = bitcast i256* [[LOCAL1]] to i8*
143142
; CHECK-NEXT: call void @llvm.lifetime.start.p0i8(i64 1, i8* [[LOCAL1_CAST]])
144143
; CHECK-NEXT: br label [[HEADER:%.*]]
145144
; CHECK: header:
146145
; CHECK-NEXT: call void @use(i8* [[LOCAL1_CAST]])
147146
; CHECK-NEXT: br i1 undef, label [[EXIT:%.*]], label [[CODEREPL:%.*]]
148147
; CHECK: codeRepl:
149-
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @do_not_lift_lifetime_end.cold.1(i8* [[LOCAL1_CAST]]) #3
148+
; CHECK-NEXT: [[TARGETBLOCK:%.*]] = call i1 @do_not_lift_lifetime_end.cold.1(i8* [[LOCAL1_CAST]]) [[ATTR3]]
150149
; CHECK-NEXT: br i1 [[TARGETBLOCK]], label [[HEADER]], label [[EXIT]]
151150
; CHECK: exit:
152151
; CHECK-NEXT: ret void

llvm/test/Transforms/HotColdSplit/split-phis-in-exit-blocks.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ target triple = "x86_64-apple-macosx10.14.0"
1212
; CHECK-NEXT: ]
1313
;
1414
; CHECK: codeRepl:
15-
; CHECK-NEXT: bitcast
16-
; CHECK-NEXT: lifetime.start
15+
; CHECK: lifetime.start
1716
; CHECK-NEXT: call void @pluto.cold.1(i1* %tmp8.ce.loc)
1817
; CHECK-NEXT: %tmp8.ce.reload = load i1, i1* %tmp8.ce.loc
1918
; CHECK-NEXT: lifetime.end

0 commit comments

Comments
 (0)