[llvm] Avoid indirect branches from cold sections when BTI is enabled (PR #194727)

Nick Desaulniers via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 16:03:06 PDT 2026


================
@@ -401,19 +401,21 @@ void AArch64InstrInfo::insertIndirectBranch(MachineBasicBlock &MBB,
     return;
   }
 
-  // If there's a free register and it's worth inflating the code size,
+  // If we are in a cold block, BTI is not enabled, and there's a free register,
   // manually insert the indirect branch.
-  Register Scavenged = RS->FindUnusedReg(&AArch64::GPR64RegClass);
-  if (Scavenged != AArch64::NoRegister &&
-      MBB.getSectionID() == MBBSectionID::ColdSectionID) {
-    buildIndirectBranch(Scavenged, NewDestBB);
-    RS->setRegUsed(Scavenged);
-    return;
+  AArch64FunctionInfo *AFI = MBB.getParent()->getInfo<AArch64FunctionInfo>();
+  bool HasBTI = AFI && AFI->branchTargetEnforcement();
+  if (MBB.getSectionID() == MBBSectionID::ColdSectionID && !HasBTI) {
+    Register Scavenged = RS->FindUnusedReg(&AArch64::GPR64RegClass);
+    if (Scavenged != AArch64::NoRegister) {
+      buildIndirectBranch(Scavenged, NewDestBB);
----------------
nickdesaulniers wrote:

@dhoekwater left some feedback on the internal bug about regressing hot code size.  If that's the preference here, we should make the super explicit in the comments.

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


More information about the llvm-commits mailing list