Skip to content

Commit 3114bd3

Browse files
authored
[StackColoring] Do not drop AA metadata when not doing remappings (#71958)
In the StackColoring pass we first scan for possible stack slot merges. A SlotRemap map is setup with the remappings that should be performed. Then the main work is done by calling remapInstructions and providing that map. Most of the work in remapInstructions would just be a waste of time in situations when the SlotRemap map is empty, but it turns out that the part that adjusts Alias Analysis information could end up dropping AA metadata even when there are no stack slot merges being done. This happens since all instruction's machine memory operands are considered, and if we can't determine the underlying object that is accessed (using getUnderlyingObjectsForCodeGen) then we conservatively drop AA metadata. This patch simply avoids calling remapInstructions if we don't intend to do any remappings (i.e. if SlotRemap is empty). That avoids touching AA metadata when all we do is to remove lifetime markers. That seems like a safe thing to do, as it is the same thing as happens when we bail out early due to other reasons (e.g. when only having one lifetime marker). For targets that do not care about Alias Analysis information after the StackColoring pass this shouldn't have any impact, except that it might improve compile time slightly as we now skip spending time in remapInstructions when not doing any stack merges.
1 parent e097582 commit 3114bd3

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

llvm/lib/CodeGen/StackColoring.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,8 +1338,10 @@ bool StackColoring::runOnMachineFunction(MachineFunction &Func) {
13381338

13391339
// Scan the entire function and update all machine operands that use frame
13401340
// indices to use the remapped frame index.
1341-
expungeSlotMap(SlotRemap, NumSlots);
1342-
remapInstructions(SlotRemap);
1341+
if (!SlotRemap.empty()) {
1342+
expungeSlotMap(SlotRemap, NumSlots);
1343+
remapInstructions(SlotRemap);
1344+
}
13431345

13441346
return removeAllMarkers();
13451347
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 3
2+
# RUN: llc -run-pass=stack-coloring %s -o - | FileCheck %s
3+
4+
# We do not expect any stack coloring remappings in this test case.
5+
# And then there should be no reason to drop the tbaa metadata on the
6+
# MOV8rm instruction, so we check that the tbaa info is kept.
7+
8+
--- |
9+
source_filename = "test_case.cc"
10+
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
11+
target triple = "x86_64-unknown-linux-gnu"
12+
13+
%struct.Agg = type { [3 x i8], [3 x i8] }
14+
15+
define i8 @main() {
16+
%padding = alloca %struct.Agg, align 8
17+
%agg = alloca %struct.Agg, align 8
18+
%a2 = getelementptr inbounds %struct.Agg, ptr %agg, i64 0, i32 1
19+
%a22 = getelementptr inbounds [3 x i8], ptr %a2, i64 0, i32 1
20+
%t0 = load i8, ptr %a22, align 1, !tbaa !2
21+
%tobool = icmp slt i8 %t0, 0
22+
%t1 = load ptr, ptr %a2, align 8
23+
%cond = select i1 %tobool, ptr %t1, ptr %a2
24+
%add.ptr.i = getelementptr inbounds i8, ptr %cond, i64 16
25+
%t2 = load i8, ptr %add.ptr.i, align 1, !tbaa !2
26+
ret i8 %t2
27+
}
28+
29+
!llvm.module.flags = !{!0}
30+
!llvm.ident = !{!1}
31+
32+
!0 = !{i32 1, !"wchar_size", i32 4}
33+
!1 = !{!"clang version 9.0.0"}
34+
!2 = !{!3, !3, i64 0}
35+
!3 = !{!"omnipotent char", !4, i64 0}
36+
!4 = !{!"Simple C++ TBAA"}
37+
38+
...
39+
---
40+
name: main
41+
alignment: 16
42+
tracksRegLiveness: true
43+
stack:
44+
- { id: 0, name: padding, type: default, offset: 0, size: 24, alignment: 16,
45+
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
46+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
47+
- { id: 1, name: agg, type: default, offset: 0, size: 48, alignment: 16,
48+
stack-id: default, callee-saved-register: '', callee-saved-restored: true,
49+
debug-info-variable: '', debug-info-expression: '', debug-info-location: '' }
50+
body: |
51+
bb.0:
52+
; CHECK-LABEL: name: main
53+
; CHECK: [[LEA64r:%[0-9]+]]:gr64 = nuw LEA64r %stack.1.agg, 1, $noreg, 24, $noreg
54+
; CHECK-NEXT: CMP8mi %stack.1.agg, 1, $noreg, 47, $noreg, 0, implicit-def $eflags :: (dereferenceable load (s8) from %ir.a22, !tbaa !2)
55+
; CHECK-NEXT: [[CMOV64rm:%[0-9]+]]:gr64 = CMOV64rm [[LEA64r]], %stack.1.agg, 1, $noreg, 24, $noreg, 8, implicit $eflags :: (dereferenceable load (s64) from %ir.a2)
56+
; CHECK-NEXT: [[MOV8rm:%[0-9]+]]:gr8 = MOV8rm killed [[CMOV64rm]], 1, $noreg, 16, $noreg :: (load (s8) from %ir.add.ptr.i, !tbaa !2)
57+
; CHECK-NEXT: $al = COPY [[MOV8rm]]
58+
; CHECK-NEXT: RET 0, $al
59+
LIFETIME_START %stack.0.padding
60+
LIFETIME_START %stack.1.agg
61+
%8:gr64 = nuw LEA64r %stack.1.agg, 1, $noreg, 24, $noreg
62+
CMP8mi %stack.1.agg, 1, $noreg, 47, $noreg, 0, implicit-def $eflags :: (dereferenceable load (s8) from %ir.a22, !tbaa !2)
63+
%13:gr64 = CMOV64rm %8, %stack.1.agg, 1, $noreg, 24, $noreg, 8, implicit $eflags :: (dereferenceable load (s64) from %ir.a2)
64+
%14:gr8 = MOV8rm killed %13, 1, $noreg, 16, $noreg :: (load (s8) from %ir.add.ptr.i, !tbaa !2)
65+
$al = COPY %14
66+
RET 0, $al
67+
68+
...

0 commit comments

Comments
 (0)