1bdd1243dSDimitry Andric //===--------- SMEABI - SME ABI-------------------------------------------===// 2bdd1243dSDimitry Andric // 3bdd1243dSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4bdd1243dSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5bdd1243dSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6bdd1243dSDimitry Andric // 7bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 8bdd1243dSDimitry Andric // 9bdd1243dSDimitry Andric // This pass implements parts of the the SME ABI, such as: 10bdd1243dSDimitry Andric // * Using the lazy-save mechanism before enabling the use of ZA. 11bdd1243dSDimitry Andric // * Setting up the lazy-save mechanism around invokes. 12bdd1243dSDimitry Andric // 13bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 14bdd1243dSDimitry Andric 15bdd1243dSDimitry Andric #include "AArch64.h" 16bdd1243dSDimitry Andric #include "Utils/AArch64BaseInfo.h" 17bdd1243dSDimitry Andric #include "Utils/AArch64SMEAttributes.h" 18bdd1243dSDimitry Andric #include "llvm/ADT/StringRef.h" 19bdd1243dSDimitry Andric #include "llvm/IR/Constants.h" 20bdd1243dSDimitry Andric #include "llvm/IR/IRBuilder.h" 21bdd1243dSDimitry Andric #include "llvm/IR/Instructions.h" 22bdd1243dSDimitry Andric #include "llvm/IR/IntrinsicInst.h" 23bdd1243dSDimitry Andric #include "llvm/IR/IntrinsicsAArch64.h" 24bdd1243dSDimitry Andric #include "llvm/IR/LLVMContext.h" 25*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h" 26bdd1243dSDimitry Andric #include "llvm/InitializePasses.h" 27bdd1243dSDimitry Andric #include "llvm/Support/Debug.h" 28bdd1243dSDimitry Andric #include "llvm/Transforms/Utils/Cloning.h" 29bdd1243dSDimitry Andric 30bdd1243dSDimitry Andric using namespace llvm; 31bdd1243dSDimitry Andric 32bdd1243dSDimitry Andric #define DEBUG_TYPE "aarch64-sme-abi" 33bdd1243dSDimitry Andric 34bdd1243dSDimitry Andric namespace { 35bdd1243dSDimitry Andric struct SMEABI : public FunctionPass { 36bdd1243dSDimitry Andric static char ID; // Pass identification, replacement for typeid 37bdd1243dSDimitry Andric SMEABI() : FunctionPass(ID) { 38bdd1243dSDimitry Andric initializeSMEABIPass(*PassRegistry::getPassRegistry()); 39bdd1243dSDimitry Andric } 40bdd1243dSDimitry Andric 41bdd1243dSDimitry Andric bool runOnFunction(Function &F) override; 42bdd1243dSDimitry Andric 43bdd1243dSDimitry Andric private: 447a6dacacSDimitry Andric bool updateNewStateFunctions(Module *M, Function *F, IRBuilder<> &Builder, 457a6dacacSDimitry Andric SMEAttrs FnAttrs); 46bdd1243dSDimitry Andric }; 47bdd1243dSDimitry Andric } // end anonymous namespace 48bdd1243dSDimitry Andric 49bdd1243dSDimitry Andric char SMEABI::ID = 0; 50bdd1243dSDimitry Andric static const char *name = "SME ABI Pass"; 51bdd1243dSDimitry Andric INITIALIZE_PASS_BEGIN(SMEABI, DEBUG_TYPE, name, false, false) 52bdd1243dSDimitry Andric INITIALIZE_PASS_END(SMEABI, DEBUG_TYPE, name, false, false) 53bdd1243dSDimitry Andric 54bdd1243dSDimitry Andric FunctionPass *llvm::createSMEABIPass() { return new SMEABI(); } 55bdd1243dSDimitry Andric 56bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 57bdd1243dSDimitry Andric // Utility functions 58bdd1243dSDimitry Andric //===----------------------------------------------------------------------===// 59bdd1243dSDimitry Andric 60bdd1243dSDimitry Andric // Utility function to emit a call to __arm_tpidr2_save and clear TPIDR2_EL0. 61bdd1243dSDimitry Andric void emitTPIDR2Save(Module *M, IRBuilder<> &Builder) { 62bdd1243dSDimitry Andric auto *TPIDR2SaveTy = 63bdd1243dSDimitry Andric FunctionType::get(Builder.getVoidTy(), {}, /*IsVarArgs=*/false); 64*0fca6ea1SDimitry Andric auto Attrs = AttributeList().addFnAttribute(M->getContext(), 65*0fca6ea1SDimitry Andric "aarch64_pstate_sm_compatible"); 66bdd1243dSDimitry Andric FunctionCallee Callee = 67bdd1243dSDimitry Andric M->getOrInsertFunction("__arm_tpidr2_save", TPIDR2SaveTy, Attrs); 68bdd1243dSDimitry Andric CallInst *Call = Builder.CreateCall(Callee); 69bdd1243dSDimitry Andric Call->setCallingConv( 70bdd1243dSDimitry Andric CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0); 71bdd1243dSDimitry Andric 72bdd1243dSDimitry Andric // A save to TPIDR2 should be followed by clearing TPIDR2_EL0. 73bdd1243dSDimitry Andric Function *WriteIntr = 74bdd1243dSDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_set_tpidr2); 75bdd1243dSDimitry Andric Builder.CreateCall(WriteIntr->getFunctionType(), WriteIntr, 76bdd1243dSDimitry Andric Builder.getInt64(0)); 77bdd1243dSDimitry Andric } 78bdd1243dSDimitry Andric 797a6dacacSDimitry Andric /// This function generates code at the beginning and end of a function marked 80*0fca6ea1SDimitry Andric /// with either `aarch64_new_za` or `aarch64_new_zt0`. 817a6dacacSDimitry Andric /// At the beginning of the function, the following code is generated: 827a6dacacSDimitry Andric /// - Commit lazy-save if active [Private-ZA Interface*] 837a6dacacSDimitry Andric /// - Enable PSTATE.ZA [Private-ZA Interface] 847a6dacacSDimitry Andric /// - Zero ZA [Has New ZA State] 857a6dacacSDimitry Andric /// - Zero ZT0 [Has New ZT0 State] 867a6dacacSDimitry Andric /// 877a6dacacSDimitry Andric /// * A function with new ZT0 state will not change ZA, so committing the 887a6dacacSDimitry Andric /// lazy-save is not strictly necessary. However, the lazy-save mechanism 897a6dacacSDimitry Andric /// may be active on entry to the function, with PSTATE.ZA set to 1. If 907a6dacacSDimitry Andric /// the new ZT0 function calls a function that does not share ZT0, we will 917a6dacacSDimitry Andric /// need to conditionally SMSTOP ZA before the call, setting PSTATE.ZA to 0. 927a6dacacSDimitry Andric /// For this reason, it's easier to always commit the lazy-save at the 937a6dacacSDimitry Andric /// beginning of the function regardless of whether it has ZA state. 947a6dacacSDimitry Andric /// 957a6dacacSDimitry Andric /// At the end of the function, PSTATE.ZA is disabled if the function has a 967a6dacacSDimitry Andric /// Private-ZA Interface. A function is considered to have a Private-ZA 977a6dacacSDimitry Andric /// interface if it does not share ZA or ZT0. 987a6dacacSDimitry Andric /// 997a6dacacSDimitry Andric bool SMEABI::updateNewStateFunctions(Module *M, Function *F, 1007a6dacacSDimitry Andric IRBuilder<> &Builder, SMEAttrs FnAttrs) { 101bdd1243dSDimitry Andric LLVMContext &Context = F->getContext(); 102bdd1243dSDimitry Andric BasicBlock *OrigBB = &F->getEntryBlock(); 1037a6dacacSDimitry Andric Builder.SetInsertPoint(&OrigBB->front()); 104bdd1243dSDimitry Andric 1057a6dacacSDimitry Andric // Commit any active lazy-saves if this is a Private-ZA function. If the 1067a6dacacSDimitry Andric // value read from TPIDR2_EL0 is not null on entry to the function then 1077a6dacacSDimitry Andric // the lazy-saving scheme is active and we should call __arm_tpidr2_save 1087a6dacacSDimitry Andric // to commit the lazy save. 1097a6dacacSDimitry Andric if (FnAttrs.hasPrivateZAInterface()) { 110bdd1243dSDimitry Andric // Create the new blocks for reading TPIDR2_EL0 & enabling ZA state. 111bdd1243dSDimitry Andric auto *SaveBB = OrigBB->splitBasicBlock(OrigBB->begin(), "save.za", true); 112bdd1243dSDimitry Andric auto *PreludeBB = BasicBlock::Create(Context, "prelude", F, SaveBB); 113bdd1243dSDimitry Andric 114bdd1243dSDimitry Andric // Read TPIDR2_EL0 in PreludeBB & branch to SaveBB if not 0. 115bdd1243dSDimitry Andric Builder.SetInsertPoint(PreludeBB); 116bdd1243dSDimitry Andric Function *TPIDR2Intr = 117bdd1243dSDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_get_tpidr2); 118bdd1243dSDimitry Andric auto *TPIDR2 = Builder.CreateCall(TPIDR2Intr->getFunctionType(), TPIDR2Intr, 119bdd1243dSDimitry Andric {}, "tpidr2"); 1207a6dacacSDimitry Andric auto *Cmp = Builder.CreateCmp(ICmpInst::ICMP_NE, TPIDR2, 1217a6dacacSDimitry Andric Builder.getInt64(0), "cmp"); 122bdd1243dSDimitry Andric Builder.CreateCondBr(Cmp, SaveBB, OrigBB); 123bdd1243dSDimitry Andric 124bdd1243dSDimitry Andric // Create a call __arm_tpidr2_save, which commits the lazy save. 125bdd1243dSDimitry Andric Builder.SetInsertPoint(&SaveBB->back()); 126bdd1243dSDimitry Andric emitTPIDR2Save(M, Builder); 127bdd1243dSDimitry Andric 128bdd1243dSDimitry Andric // Enable pstate.za at the start of the function. 129bdd1243dSDimitry Andric Builder.SetInsertPoint(&OrigBB->front()); 130bdd1243dSDimitry Andric Function *EnableZAIntr = 131bdd1243dSDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_za_enable); 132bdd1243dSDimitry Andric Builder.CreateCall(EnableZAIntr->getFunctionType(), EnableZAIntr); 1337a6dacacSDimitry Andric } 134bdd1243dSDimitry Andric 135*0fca6ea1SDimitry Andric if (FnAttrs.isNewZA()) { 1365f757f3fSDimitry Andric Function *ZeroIntr = 1375f757f3fSDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_zero); 1385f757f3fSDimitry Andric Builder.CreateCall(ZeroIntr->getFunctionType(), ZeroIntr, 1395f757f3fSDimitry Andric Builder.getInt32(0xff)); 1407a6dacacSDimitry Andric } 1415f757f3fSDimitry Andric 1427a6dacacSDimitry Andric if (FnAttrs.isNewZT0()) { 1437a6dacacSDimitry Andric Function *ClearZT0Intr = 1447a6dacacSDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_zero_zt); 1457a6dacacSDimitry Andric Builder.CreateCall(ClearZT0Intr->getFunctionType(), ClearZT0Intr, 1467a6dacacSDimitry Andric {Builder.getInt32(0)}); 1477a6dacacSDimitry Andric } 1487a6dacacSDimitry Andric 1497a6dacacSDimitry Andric if (FnAttrs.hasPrivateZAInterface()) { 150bdd1243dSDimitry Andric // Before returning, disable pstate.za 151bdd1243dSDimitry Andric for (BasicBlock &BB : *F) { 152bdd1243dSDimitry Andric Instruction *T = BB.getTerminator(); 153bdd1243dSDimitry Andric if (!T || !isa<ReturnInst>(T)) 154bdd1243dSDimitry Andric continue; 155bdd1243dSDimitry Andric Builder.SetInsertPoint(T); 156bdd1243dSDimitry Andric Function *DisableZAIntr = 157bdd1243dSDimitry Andric Intrinsic::getDeclaration(M, Intrinsic::aarch64_sme_za_disable); 158bdd1243dSDimitry Andric Builder.CreateCall(DisableZAIntr->getFunctionType(), DisableZAIntr); 159bdd1243dSDimitry Andric } 1607a6dacacSDimitry Andric } 161bdd1243dSDimitry Andric 162bdd1243dSDimitry Andric F->addFnAttr("aarch64_expanded_pstate_za"); 163bdd1243dSDimitry Andric return true; 164bdd1243dSDimitry Andric } 165bdd1243dSDimitry Andric 166bdd1243dSDimitry Andric bool SMEABI::runOnFunction(Function &F) { 167bdd1243dSDimitry Andric Module *M = F.getParent(); 168bdd1243dSDimitry Andric LLVMContext &Context = F.getContext(); 169bdd1243dSDimitry Andric IRBuilder<> Builder(Context); 170bdd1243dSDimitry Andric 171bdd1243dSDimitry Andric if (F.isDeclaration() || F.hasFnAttribute("aarch64_expanded_pstate_za")) 172bdd1243dSDimitry Andric return false; 173bdd1243dSDimitry Andric 174bdd1243dSDimitry Andric bool Changed = false; 175bdd1243dSDimitry Andric SMEAttrs FnAttrs(F); 176*0fca6ea1SDimitry Andric if (FnAttrs.isNewZA() || FnAttrs.isNewZT0()) 1777a6dacacSDimitry Andric Changed |= updateNewStateFunctions(M, &F, Builder, FnAttrs); 178bdd1243dSDimitry Andric 179bdd1243dSDimitry Andric return Changed; 180bdd1243dSDimitry Andric } 181