-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[RegAlloc] Fix register's live range for early-clobber #152895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cc8e0f3 to
447fec6
Compare
668b5aa to
4446a07
Compare
4446a07 to
7baf189
Compare
|
@llvm/pr-subscribers-backend-x86 @llvm/pr-subscribers-llvm-regalloc Author: Luo, Yuanke (LuoYuanke) ChangesWhen rematerialize a virtual register the register may be early clobbered. Full diff: https://github.com/llvm/llvm-project/pull/152895.diff 2 Files Affected:
diff --git a/llvm/lib/CodeGen/LiveRangeEdit.cpp b/llvm/lib/CodeGen/LiveRangeEdit.cpp
index a3858efbdc5e1..0bd9b0dad740d 100644
--- a/llvm/lib/CodeGen/LiveRangeEdit.cpp
+++ b/llvm/lib/CodeGen/LiveRangeEdit.cpp
@@ -190,9 +190,12 @@ SlotIndex LiveRangeEdit::rematerializeAt(MachineBasicBlock &MBB,
Rematted.insert(RM.ParentVNI);
++NumReMaterialization;
+ bool EarlyClobber = MI->getOperand(0).isEarlyClobber();
if (ReplaceIndexMI)
- return LIS.ReplaceMachineInstrInMaps(*ReplaceIndexMI, *MI).getRegSlot();
- return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot();
+ return LIS.ReplaceMachineInstrInMaps(*ReplaceIndexMI, *MI)
+ .getRegSlot(EearlyClobber);
+ return LIS.getSlotIndexes()->insertMachineInstrInMaps(*MI, Late).getRegSlot(
+ EarlyClobber);
}
void LiveRangeEdit::eraseVirtReg(Register Reg) {
diff --git a/llvm/test/CodeGen/X86/early-clobber.mir b/llvm/test/CodeGen/X86/early-clobber.mir
new file mode 100644
index 0000000000000..27c83d2a68c95
--- /dev/null
+++ b/llvm/test/CodeGen/X86/early-clobber.mir
@@ -0,0 +1,75 @@
+# RUN: llc -mtriple=i386-unknown-linux-gnu -start-before=twoaddressinstruction -verify-machineinstrs -o - %s
+
+# Test register live range that is split from rematerializing. The live range should
+# start with Slot_EarlyClobber instead of Slot_Register. Machineverifer can check it.
+
+--- |
+ target datalayout = "e-m:e-p:32:32-p270:32:32-p271:32:32-p272:64:64-f64:32:64-f80:32-n8:16:32-S128"
+ target triple = "i386-unknown-linux-gnu"
+
+ @g = external constant [2 x i32], align 4
+
+ ; Function Attrs: nounwind
+ define i32 @test(i32 noundef %a) #0 {
+ entry:
+ %0 = xor i32 %a, %a
+ tail call void asm sideeffect "", "~{ebp},~{eax},~{ebx},~{ecx},~{edx},~{esi},~{edi},~{dirflag},~{fpsr},~{flags}"()
+ br i1 undef, label %if, label %else
+
+ if: ; preds = %entry
+ %add = add i32 %0, 1
+ br label %exit
+
+ else: ; preds = %entry
+ %shl = shl i32 %0, 1
+ br label %exit
+
+ exit: ; preds = %else, %if
+ %phi = phi i32 [ %add, %if ], [ %shl, %else ]
+ ret i32 %phi
+ }
+
+ attributes #0 = { nounwind }
+
+...
+---
+name: test
+alignment: 16
+tracksRegLiveness: true
+registers:
+ - { id: 0, class: gr32_abcd }
+ - { id: 1, class: gr32 }
+ - { id: 2, class: gr32 }
+ - { id: 3, class: gr32 }
+ - { id: 4, class: gr8 }
+ - { id: 5, class: gr32 }
+frameInfo:
+ maxAlignment: 4
+fixedStack:
+ - { id: 0, size: 4, alignment: 16, isImmutable: true }
+machineFunctionInfo: {}
+body: |
+ bb.0.entry:
+ successors: %bb.1, %bb.2
+
+ early-clobber %0:gr32_abcd = MOV32r0 implicit-def dead $eflags
+ INLINEASM &"", 1 /* sideeffect attdialect */, 12 /* clobber */, implicit-def dead early-clobber $ebp, 12 /* clobber */, implicit-def dead early-clobber $eax, 12 /* clobber */, implicit-def dead early-clobber $ebx, 12 /* clobber */, implicit-def dead early-clobber $ecx, 12 /* clobber */, implicit-def dead early-clobber $edx, 12 /* clobber */, implicit-def dead early-clobber $esi, 12 /* clobber */, implicit-def dead early-clobber $edi, 12 /* clobber */, implicit-def dead early-clobber $df, 12 /* clobber */, implicit-def early-clobber $fpsw, 12 /* clobber */, implicit-def dead early-clobber $eflags
+ %4:gr8 = COPY %0.sub_8bit
+ TEST8rr killed %4, %4, implicit-def $eflags
+ JCC_1 %bb.2, 5, implicit killed $eflags
+ JMP_1 %bb.1
+
+ bb.1.if:
+ %1:gr32 = MOV32ri 1
+ %5:gr32 = COPY killed %1
+ JMP_1 %bb.3
+
+ bb.2.else:
+ %5:gr32 = COPY killed %0
+
+ bb.3.exit:
+ %3:gr32 = COPY killed %5
+ $eax = COPY killed %3
+ RET 0, killed $eax
+
+...
|
ac2e20c to
7baf189
Compare
phoebewang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
When rematerialize a virtual register the register may be early clobbered. However rematerializeAt(...) just return the slot index of Slot_Register which cause mis-calculating live range for the register
e0ec4c8 to
fbf0bbe
Compare
|
All the comments have been addressed. If there is no new comment, I'd like to land the patch. |
KanRobert
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
When rematerialize a virtual register the register may be early clobbered.
However rematerializeAt(...) just return the slot index of Slot_Register
which cause mis-calculating live range for the register