xref: /llvm-project/llvm/lib/Target/AArch64/SMEABIPass.cpp (revision a41922ad7530ef5e311afbff2721e69cbf520890)
1f0f474dfSDavid Sherwood //===--------- SMEABI - SME  ABI-------------------------------------------===//
2f0f474dfSDavid Sherwood //
3f0f474dfSDavid Sherwood // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4f0f474dfSDavid Sherwood // See https://llvm.org/LICENSE.txt for license information.
5f0f474dfSDavid Sherwood // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f0f474dfSDavid Sherwood //
7f0f474dfSDavid Sherwood //===----------------------------------------------------------------------===//
8f0f474dfSDavid Sherwood //
9f0f474dfSDavid Sherwood // This pass implements parts of the the SME ABI, such as:
10f0f474dfSDavid Sherwood // * Using the lazy-save mechanism before enabling the use of ZA.
11f0f474dfSDavid Sherwood // * Setting up the lazy-save mechanism around invokes.
12f0f474dfSDavid Sherwood //
13f0f474dfSDavid Sherwood //===----------------------------------------------------------------------===//
14f0f474dfSDavid Sherwood 
15f0f474dfSDavid Sherwood #include "AArch64.h"
16f0f474dfSDavid Sherwood #include "Utils/AArch64SMEAttributes.h"
17f0f474dfSDavid Sherwood #include "llvm/ADT/StringRef.h"
18f0f474dfSDavid Sherwood #include "llvm/IR/IRBuilder.h"
19f0f474dfSDavid Sherwood #include "llvm/IR/Instructions.h"
20f0f474dfSDavid Sherwood #include "llvm/IR/IntrinsicsAArch64.h"
21f0f474dfSDavid Sherwood #include "llvm/IR/LLVMContext.h"
2274deadf1SNikita Popov #include "llvm/IR/Module.h"
23f0f474dfSDavid Sherwood #include "llvm/InitializePasses.h"
24f0f474dfSDavid Sherwood #include "llvm/Transforms/Utils/Cloning.h"
25f0f474dfSDavid Sherwood 
26f0f474dfSDavid Sherwood using namespace llvm;
27f0f474dfSDavid Sherwood 
28f0f474dfSDavid Sherwood #define DEBUG_TYPE "aarch64-sme-abi"
29f0f474dfSDavid Sherwood 
30f0f474dfSDavid Sherwood namespace {
31f0f474dfSDavid Sherwood struct SMEABI : public FunctionPass {
32f0f474dfSDavid Sherwood   static char ID; // Pass identification, replacement for typeid
33f0f474dfSDavid Sherwood   SMEABI() : FunctionPass(ID) {
34f0f474dfSDavid Sherwood     initializeSMEABIPass(*PassRegistry::getPassRegistry());
35f0f474dfSDavid Sherwood   }
36f0f474dfSDavid Sherwood 
37f0f474dfSDavid Sherwood   bool runOnFunction(Function &F) override;
38f0f474dfSDavid Sherwood 
39f0f474dfSDavid Sherwood private:
40d4d81acbSKerry McLaughlin   bool updateNewStateFunctions(Module *M, Function *F, IRBuilder<> &Builder,
41d4d81acbSKerry McLaughlin                                SMEAttrs FnAttrs);
42f0f474dfSDavid Sherwood };
43f0f474dfSDavid Sherwood } // end anonymous namespace
44f0f474dfSDavid Sherwood 
45f0f474dfSDavid Sherwood char SMEABI::ID = 0;
46f0f474dfSDavid Sherwood static const char *name = "SME ABI Pass";
47f0f474dfSDavid Sherwood INITIALIZE_PASS_BEGIN(SMEABI, DEBUG_TYPE, name, false, false)
48f0f474dfSDavid Sherwood INITIALIZE_PASS_END(SMEABI, DEBUG_TYPE, name, false, false)
49f0f474dfSDavid Sherwood 
50f0f474dfSDavid Sherwood FunctionPass *llvm::createSMEABIPass() { return new SMEABI(); }
51f0f474dfSDavid Sherwood 
52f0f474dfSDavid Sherwood //===----------------------------------------------------------------------===//
53f0f474dfSDavid Sherwood // Utility functions
54f0f474dfSDavid Sherwood //===----------------------------------------------------------------------===//
55f0f474dfSDavid Sherwood 
56f0f474dfSDavid Sherwood // Utility function to emit a call to __arm_tpidr2_save and clear TPIDR2_EL0.
57f0f474dfSDavid Sherwood void emitTPIDR2Save(Module *M, IRBuilder<> &Builder) {
58f0f474dfSDavid Sherwood   auto *TPIDR2SaveTy =
59f0f474dfSDavid Sherwood       FunctionType::get(Builder.getVoidTy(), {}, /*IsVarArgs=*/false);
60d313614bSSander de Smalen   auto Attrs = AttributeList().addFnAttribute(M->getContext(),
61d313614bSSander de Smalen                                               "aarch64_pstate_sm_compatible");
62f0f474dfSDavid Sherwood   FunctionCallee Callee =
63f0f474dfSDavid Sherwood       M->getOrInsertFunction("__arm_tpidr2_save", TPIDR2SaveTy, Attrs);
64e82cace1SSander de Smalen   CallInst *Call = Builder.CreateCall(Callee);
65e82cace1SSander de Smalen   Call->setCallingConv(
66e82cace1SSander de Smalen       CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0);
67f0f474dfSDavid Sherwood 
68f0f474dfSDavid Sherwood   // A save to TPIDR2 should be followed by clearing TPIDR2_EL0.
69f0f474dfSDavid Sherwood   Function *WriteIntr =
70*fa789dffSRahul Joshi       Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_set_tpidr2);
71f0f474dfSDavid Sherwood   Builder.CreateCall(WriteIntr->getFunctionType(), WriteIntr,
72f0f474dfSDavid Sherwood                      Builder.getInt64(0));
73f0f474dfSDavid Sherwood }
74f0f474dfSDavid Sherwood 
75d4d81acbSKerry McLaughlin /// This function generates code at the beginning and end of a function marked
76d313614bSSander de Smalen /// with either `aarch64_new_za` or `aarch64_new_zt0`.
77d4d81acbSKerry McLaughlin /// At the beginning of the function, the following code is generated:
78d4d81acbSKerry McLaughlin ///  - Commit lazy-save if active   [Private-ZA Interface*]
79d4d81acbSKerry McLaughlin ///  - Enable PSTATE.ZA             [Private-ZA Interface]
80d4d81acbSKerry McLaughlin ///  - Zero ZA                      [Has New ZA State]
81d4d81acbSKerry McLaughlin ///  - Zero ZT0                     [Has New ZT0 State]
82d4d81acbSKerry McLaughlin ///
83d4d81acbSKerry McLaughlin /// * A function with new ZT0 state will not change ZA, so committing the
84d4d81acbSKerry McLaughlin /// lazy-save is not strictly necessary. However, the lazy-save mechanism
85d4d81acbSKerry McLaughlin /// may be active on entry to the function, with PSTATE.ZA set to 1. If
86d4d81acbSKerry McLaughlin /// the new ZT0 function calls a function that does not share ZT0, we will
87d4d81acbSKerry McLaughlin /// need to conditionally SMSTOP ZA before the call, setting PSTATE.ZA to 0.
88d4d81acbSKerry McLaughlin /// For this reason, it's easier to always commit the lazy-save at the
89d4d81acbSKerry McLaughlin /// beginning of the function regardless of whether it has ZA state.
90d4d81acbSKerry McLaughlin ///
91d4d81acbSKerry McLaughlin /// At the end of the function, PSTATE.ZA is disabled if the function has a
92d4d81acbSKerry McLaughlin /// Private-ZA Interface. A function is considered to have a Private-ZA
93d4d81acbSKerry McLaughlin /// interface if it does not share ZA or ZT0.
94d4d81acbSKerry McLaughlin ///
95d4d81acbSKerry McLaughlin bool SMEABI::updateNewStateFunctions(Module *M, Function *F,
96d4d81acbSKerry McLaughlin                                      IRBuilder<> &Builder, SMEAttrs FnAttrs) {
97f0f474dfSDavid Sherwood   LLVMContext &Context = F->getContext();
98f0f474dfSDavid Sherwood   BasicBlock *OrigBB = &F->getEntryBlock();
99d4d81acbSKerry McLaughlin   Builder.SetInsertPoint(&OrigBB->front());
100f0f474dfSDavid Sherwood 
101d4d81acbSKerry McLaughlin   // Commit any active lazy-saves if this is a Private-ZA function. If the
102d4d81acbSKerry McLaughlin   // value read from TPIDR2_EL0 is not null on entry to the function then
103d4d81acbSKerry McLaughlin   // the lazy-saving scheme is active and we should call __arm_tpidr2_save
104d4d81acbSKerry McLaughlin   // to commit the lazy save.
105d4d81acbSKerry McLaughlin   if (FnAttrs.hasPrivateZAInterface()) {
106f0f474dfSDavid Sherwood     // Create the new blocks for reading TPIDR2_EL0 & enabling ZA state.
107f0f474dfSDavid Sherwood     auto *SaveBB = OrigBB->splitBasicBlock(OrigBB->begin(), "save.za", true);
108f0f474dfSDavid Sherwood     auto *PreludeBB = BasicBlock::Create(Context, "prelude", F, SaveBB);
109f0f474dfSDavid Sherwood 
110f0f474dfSDavid Sherwood     // Read TPIDR2_EL0 in PreludeBB & branch to SaveBB if not 0.
111f0f474dfSDavid Sherwood     Builder.SetInsertPoint(PreludeBB);
112f0f474dfSDavid Sherwood     Function *TPIDR2Intr =
113*fa789dffSRahul Joshi         Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_get_tpidr2);
114f0f474dfSDavid Sherwood     auto *TPIDR2 = Builder.CreateCall(TPIDR2Intr->getFunctionType(), TPIDR2Intr,
115f0f474dfSDavid Sherwood                                       {}, "tpidr2");
116d4d81acbSKerry McLaughlin     auto *Cmp = Builder.CreateCmp(ICmpInst::ICMP_NE, TPIDR2,
117d4d81acbSKerry McLaughlin                                   Builder.getInt64(0), "cmp");
118f0f474dfSDavid Sherwood     Builder.CreateCondBr(Cmp, SaveBB, OrigBB);
119f0f474dfSDavid Sherwood 
120f0f474dfSDavid Sherwood     // Create a call __arm_tpidr2_save, which commits the lazy save.
121f0f474dfSDavid Sherwood     Builder.SetInsertPoint(&SaveBB->back());
122f0f474dfSDavid Sherwood     emitTPIDR2Save(M, Builder);
123f0f474dfSDavid Sherwood 
124f0f474dfSDavid Sherwood     // Enable pstate.za at the start of the function.
125f0f474dfSDavid Sherwood     Builder.SetInsertPoint(&OrigBB->front());
126f0f474dfSDavid Sherwood     Function *EnableZAIntr =
127*fa789dffSRahul Joshi         Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_za_enable);
128f0f474dfSDavid Sherwood     Builder.CreateCall(EnableZAIntr->getFunctionType(), EnableZAIntr);
129d4d81acbSKerry McLaughlin   }
130f0f474dfSDavid Sherwood 
131d313614bSSander de Smalen   if (FnAttrs.isNewZA()) {
1329bbbfbc7SMatthew Devereau     Function *ZeroIntr =
133*fa789dffSRahul Joshi         Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_zero);
1349bbbfbc7SMatthew Devereau     Builder.CreateCall(ZeroIntr->getFunctionType(), ZeroIntr,
1359bbbfbc7SMatthew Devereau                        Builder.getInt32(0xff));
136d4d81acbSKerry McLaughlin   }
1379bbbfbc7SMatthew Devereau 
138d4d81acbSKerry McLaughlin   if (FnAttrs.isNewZT0()) {
139d4d81acbSKerry McLaughlin     Function *ClearZT0Intr =
140*fa789dffSRahul Joshi         Intrinsic::getOrInsertDeclaration(M, Intrinsic::aarch64_sme_zero_zt);
141d4d81acbSKerry McLaughlin     Builder.CreateCall(ClearZT0Intr->getFunctionType(), ClearZT0Intr,
142d4d81acbSKerry McLaughlin                        {Builder.getInt32(0)});
143d4d81acbSKerry McLaughlin   }
144d4d81acbSKerry McLaughlin 
145d4d81acbSKerry McLaughlin   if (FnAttrs.hasPrivateZAInterface()) {
146f0f474dfSDavid Sherwood     // Before returning, disable pstate.za
147adfb23c6SVasileios Porpodas     for (BasicBlock &BB : *F) {
148f0f474dfSDavid Sherwood       Instruction *T = BB.getTerminator();
149f0f474dfSDavid Sherwood       if (!T || !isa<ReturnInst>(T))
150f0f474dfSDavid Sherwood         continue;
151f0f474dfSDavid Sherwood       Builder.SetInsertPoint(T);
152*fa789dffSRahul Joshi       Function *DisableZAIntr = Intrinsic::getOrInsertDeclaration(
153*fa789dffSRahul Joshi           M, Intrinsic::aarch64_sme_za_disable);
154f0f474dfSDavid Sherwood       Builder.CreateCall(DisableZAIntr->getFunctionType(), DisableZAIntr);
155f0f474dfSDavid Sherwood     }
156d4d81acbSKerry McLaughlin   }
157f0f474dfSDavid Sherwood 
158f0f474dfSDavid Sherwood   F->addFnAttr("aarch64_expanded_pstate_za");
159f0f474dfSDavid Sherwood   return true;
160f0f474dfSDavid Sherwood }
161f0f474dfSDavid Sherwood 
162f0f474dfSDavid Sherwood bool SMEABI::runOnFunction(Function &F) {
163f0f474dfSDavid Sherwood   Module *M = F.getParent();
164f0f474dfSDavid Sherwood   LLVMContext &Context = F.getContext();
165f0f474dfSDavid Sherwood   IRBuilder<> Builder(Context);
166f0f474dfSDavid Sherwood 
167f0f474dfSDavid Sherwood   if (F.isDeclaration() || F.hasFnAttribute("aarch64_expanded_pstate_za"))
168f0f474dfSDavid Sherwood     return false;
169f0f474dfSDavid Sherwood 
170f0f474dfSDavid Sherwood   bool Changed = false;
171f0f474dfSDavid Sherwood   SMEAttrs FnAttrs(F);
172d313614bSSander de Smalen   if (FnAttrs.isNewZA() || FnAttrs.isNewZT0())
173d4d81acbSKerry McLaughlin     Changed |= updateNewStateFunctions(M, &F, Builder, FnAttrs);
174f0f474dfSDavid Sherwood 
175f0f474dfSDavid Sherwood   return Changed;
176f0f474dfSDavid Sherwood }
177