[llvm] [Hexagon] Prevent optimizing out counter reads (PR #182338)

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 19 10:46:09 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-hexagon

Author: Alexey Karyakin (quic-akaryaki)

<details>
<summary>Changes</summary>

On Hexagon, ISD::READCYCLECOUNTER and ISD::READSTEADYCOUNTER are lowered
to the A2_tfrcpp instruction which reads the UPCYCLE (c15:14) and
UTIMER (c31:30) registers. Since these register are volatile, compiler
should not optimize out or move these calls. Usually, this is achieved
by declaring the instruction as having side effects (e.g. MRS in
AArch64). However, as the same instruction is used to read other control
registers, and some of them have normal read/write semantics, it is not
desirable to inhibit such optimization for all of registers.
Additionally, pattern-based lowering of cycle counters is not allowed
by tablegen because of the mismatch in the hasSideEffects attribute.
Previously, this was worked around with custom lowering and
a Hexagon-specific opcode for each operation.

Therefore, reading UPCYCLE and UTIMER is now done through a
pseudoinstruction, which has the hasSideEffects attribute and is lowered
by a pattern. This allows us to easily separate control registers with
and without side effects. Two pseudoinsructions are defined:
PS_TfrCRR (32 bits) and PS_TfrCRR64 (64-bits). I also remove the custom
lowering and Hexagon-specific opcodes.

In case the compiler needs to emit code reading other counters, this
method can be used by simply adding adding patterns that use PS_TfrCRR
or PS_TfrCRR64.

---
Full diff: https://github.com/llvm/llvm-project/pull/182338.diff


2 Files Affected:

- (modified) llvm/test/CodeGen/Hexagon/readcyclecounter.ll (+16) 
- (modified) llvm/test/CodeGen/Hexagon/readsteadycounter.ll (+16) 


``````````diff
diff --git a/llvm/test/CodeGen/Hexagon/readcyclecounter.ll b/llvm/test/CodeGen/Hexagon/readcyclecounter.ll
index b1b4696641dee..be5edc7348d9e 100644
--- a/llvm/test/CodeGen/Hexagon/readcyclecounter.ll
+++ b/llvm/test/CodeGen/Hexagon/readcyclecounter.ll
@@ -7,4 +7,20 @@ define i64 @test_readcyclecounter() nounwind {
   ret i64 %t0
 }
 
+;; Make sure readcyclecounter calls are not optmized out and
+;; not moved across function calls.
+
+; CHECK-LABEL: test_readcyclecounter2
+; CHECK: [[R1:r[0-9:]+]] = c31:30
+; CHECK: call fun
+; CHECK: [[R2:r[0-9:]+]] = c31:30
+; CHECK: [[R3:r[0-9:]+]] = sub([[R2]],[[R1]])
+define i64 @test_readcyclecounter2() {
+  %1 = tail call i64 @llvm.readcyclecounter()
+  tail call void @fun()
+  %2 = tail call i64 @llvm.readcyclecounter()
+  %sub = sub i64 %2, %1
+  ret i64 %sub
+}
+
 declare i64 @llvm.readcyclecounter()
diff --git a/llvm/test/CodeGen/Hexagon/readsteadycounter.ll b/llvm/test/CodeGen/Hexagon/readsteadycounter.ll
index 377097a99132b..b123d7fe79043 100644
--- a/llvm/test/CodeGen/Hexagon/readsteadycounter.ll
+++ b/llvm/test/CodeGen/Hexagon/readsteadycounter.ll
@@ -8,4 +8,20 @@ define i64 @test_readsteadycounter() nounwind {
   ret i64 %t0
 }
 
+;; Make sure readsteadycounter calls are not optmized out and
+;; not moved across function calls.
+
+; CHECK-LABEL: test_readsteadycounter2
+; CHECK: [[R1:r[0-9:]+]] = c15:14
+; CHECK: call fun
+; CHECK: [[R2:r[0-9:]+]] = c15:14
+; CHECK: [[R3:r[0-9:]+]] = sub([[R2]],[[R1]])
+define i64 @test_readsteadycounter2() {
+  %1 = tail call i64 @llvm.readsteadycounter()
+  tail call void @fun()
+  %2 = tail call i64 @llvm.readsteadycounter()
+  %sub = sub i64 %2, %1
+  ret i64 %sub
+}
+
 declare i64 @llvm.readsteadycounter()

``````````

</details>


https://github.com/llvm/llvm-project/pull/182338


More information about the llvm-commits mailing list