1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 10:42:39 +01:00

AArch64+ARM: make LLVM consider system registers volatile.

Some of the system registers readable on AArch64 and ARM platforms
return different values with each read (for example a timer counter),
these shouldn't be hoisted outside loops or otherwise interfered with,
but the normal @llvm.read_register intrinsic is only considered to read
memory.

This introduces a separate @llvm.read_volatile_register intrinsic and
maps all system-registers on ARM platforms to use it for the
__builtin_arm_rsr calls. Registers declared with asm("r9") or similar
are unaffected.
This commit is contained in:
Tim Northover 2020-07-15 09:11:36 +01:00
parent 97e701dc7a
commit 80a21440b3
5 changed files with 51 additions and 8 deletions

View File

@ -11643,9 +11643,11 @@ the escaped allocas are allocated, which would break attempts to use
'``llvm.localrecover``'.
.. _int_read_register:
.. _int_read_volatile_register:
.. _int_write_register:
'``llvm.read_register``' and '``llvm.write_register``' Intrinsics
'``llvm.read_register``', '``llvm.read_volatile_register``', and
'``llvm.write_register``' Intrinsics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Syntax:
@ -11655,6 +11657,8 @@ Syntax:
declare i32 @llvm.read_register.i32(metadata)
declare i64 @llvm.read_register.i64(metadata)
declare i32 @llvm.read_volatile_register.i32(metadata)
declare i64 @llvm.read_volatile_register.i64(metadata)
declare void @llvm.write_register.i32(metadata, i32 @value)
declare void @llvm.write_register.i64(metadata, i64 @value)
!0 = !{!"sp\00"}
@ -11662,17 +11666,21 @@ Syntax:
Overview:
"""""""""
The '``llvm.read_register``' and '``llvm.write_register``' intrinsics
provides access to the named register. The register must be valid on
the architecture being compiled to. The type needs to be compatible
with the register being read.
The '``llvm.read_register``', '``llvm.read_volatile_register``', and
'``llvm.write_register``' intrinsics provide access to the named register.
The register must be valid on the architecture being compiled to. The type
needs to be compatible with the register being read.
Semantics:
""""""""""
The '``llvm.read_register``' intrinsic returns the current value of the
register, where possible. The '``llvm.write_register``' intrinsic sets
the current value of the register, where possible.
The '``llvm.read_register``' and '``llvm.read_volatile_register``' intrinsics
return the current value of the register, where possible. The
'``llvm.write_register``' intrinsic sets the current value of the register,
where possible.
A call to '``llvm.read_volatile_register``' is assumed to have side-effects
and possibly return a different value each time (e.g. for a timer register).
This is useful to implement named register global variables that need
to always be mapped to a specific register, as is common practice on

View File

@ -458,6 +458,9 @@ def int_read_register : Intrinsic<[llvm_anyint_ty], [llvm_metadata_ty],
[IntrReadMem], "llvm.read_register">;
def int_write_register : Intrinsic<[], [llvm_metadata_ty, llvm_anyint_ty],
[], "llvm.write_register">;
def int_read_volatile_register : Intrinsic<[llvm_anyint_ty], [llvm_metadata_ty],
[IntrHasSideEffects],
"llvm.read_volatile_register">;
// Gets the address of the local variable area. This is typically a copy of the
// stack, frame, or base pointer depending on the type of prologue.

View File

@ -1598,6 +1598,7 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
case Intrinsic::sideeffect:
// Discard annotate attributes, assumptions, and artificial side-effects.
return true;
case Intrinsic::read_volatile_register:
case Intrinsic::read_register: {
Value *Arg = CI.getArgOperand(0);
MIRBuilder

View File

@ -5698,6 +5698,7 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
TLI.getFrameIndexTy(DAG.getDataLayout()),
getValue(I.getArgOperand(0))));
return;
case Intrinsic::read_volatile_register:
case Intrinsic::read_register: {
Value *Reg = I.getArgOperand(0);
SDValue Chain = getRoot();

View File

@ -0,0 +1,30 @@
; RUN: opt -S -licm %s | FileCheck %s
; Volatile register shouldn't be hoisted ourside loops.
define i32 @test_read() {
; CHECK-LABEL: define i32 @test_read()
; CHECK: br label %loop
; CHECK: loop:
; CHECK: %counter = tail call i64 @llvm.read_volatile_register
entry:
br label %loop
loop:
%i = phi i32 [ 0, %entry ], [ %i.next, %inc ]
%counter = tail call i64 @llvm.read_volatile_register.i64(metadata !1)
%tst = icmp ult i64 %counter, 1000
br i1 %tst, label %inc, label %done
inc:
%i.next = add nuw nsw i32 %i, 1
br label %loop
done:
ret i32 %i
}
declare i64 @llvm.read_register.i64(metadata)
declare i64 @llvm.read_volatile_register.i64(metadata)
!1 = !{!"cntpct_el0"}