| 1 | //===- HexagonLowerHLXPairAllocas.cpp - Lower HLX pair allocas ------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // HLX pair registers are interleaved, so a plain load or store of an HLX |
| 10 | // VectorPair is not two HLX vector memory accesses. Clang only creates pair |
| 11 | // memory for compiler-generated allocas (e.g. -O0 locals); rewrite the |
| 12 | // accesses to those allocas that survive optimization into the LL16b.tmp and |
| 13 | // SS16b.new intrinsics, which define the in-memory layout of a pair. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "Hexagon.h" |
| 18 | #include "HexagonSubtarget.h" |
| 19 | #include "HexagonTargetMachine.h" |
| 20 | #include "llvm/CodeGen/TargetPassConfig.h" |
| 21 | #include "llvm/IR/IRBuilder.h" |
| 22 | #include "llvm/IR/InstIterator.h" |
| 23 | #include "llvm/IR/Instructions.h" |
| 24 | #include "llvm/IR/IntrinsicsHexagon.h" |
| 25 | #include "llvm/InitializePasses.h" |
| 26 | #include "llvm/Pass.h" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | #define DEBUG_TYPE "hexagon-lower-hlx-pair-allocas" |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | class HexagonLowerHLXPairAllocas : public FunctionPass { |
| 35 | public: |
| 36 | static char ID; |
| 37 | |
| 38 | HexagonLowerHLXPairAllocas() : FunctionPass(ID) {} |
| 39 | |
| 40 | StringRef getPassName() const override { |
| 41 | return "Hexagon Lower HLX Pair Allocas" ; |
| 42 | } |
| 43 | |
| 44 | bool runOnFunction(Function &F) override; |
| 45 | |
| 46 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 47 | AU.setPreservesCFG(); |
| 48 | AU.addRequired<TargetPassConfig>(); |
| 49 | FunctionPass::getAnalysisUsage(AU); |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | } // end anonymous namespace |
| 54 | |
| 55 | char HexagonLowerHLXPairAllocas::ID = 0; |
| 56 | |
| 57 | INITIALIZE_PASS_BEGIN(HexagonLowerHLXPairAllocas, DEBUG_TYPE, |
| 58 | "Hexagon Lower HLX Pair Allocas" , false, false) |
| 59 | INITIALIZE_PASS_DEPENDENCY(TargetPassConfig) |
| 60 | INITIALIZE_PASS_END(HexagonLowerHLXPairAllocas, DEBUG_TYPE, |
| 61 | "Hexagon Lower HLX Pair Allocas" , false, false) |
| 62 | |
| 63 | // Return the alloca accessed by I if I is a simple, aligned, temporal load or |
| 64 | // store of PairTy directly from a PairTy alloca. |
| 65 | static AllocaInst *getPairAlloca(Instruction &I, Type *PairTy, |
| 66 | unsigned VectorLength) { |
| 67 | if (!isa<LoadInst, StoreInst>(Val: I) || getLoadStoreType(I: &I) != PairTy || |
| 68 | I.isVolatile() || I.isAtomic() || |
| 69 | I.getMetadata(KindID: LLVMContext::MD_nontemporal) || |
| 70 | getLoadStoreAlignment(I: &I).value() < VectorLength) |
| 71 | return nullptr; |
| 72 | auto *AI = dyn_cast<AllocaInst>( |
| 73 | Val: getLoadStorePointerOperand(V: &I)->stripPointerCastsSameRepresentation()); |
| 74 | if (!AI || AI->getAllocatedType() != PairTy || |
| 75 | AI->getAlign().value() < VectorLength) |
| 76 | return nullptr; |
| 77 | return AI; |
| 78 | } |
| 79 | |
| 80 | bool HexagonLowerHLXPairAllocas::runOnFunction(Function &F) { |
| 81 | auto &TM = getAnalysis<TargetPassConfig>().getTM<HexagonTargetMachine>(); |
| 82 | const HexagonSubtarget *HST = TM.getSubtargetImpl(F); |
| 83 | if (!HST->useHLXOps()) |
| 84 | return false; |
| 85 | |
| 86 | unsigned VectorLength = HST->getHLXVectorLength(); |
| 87 | // Only the i32 VectorPair type is lowered; other HLX types are rejected or |
| 88 | // handled by ISel. |
| 89 | Type *PairTy = |
| 90 | FixedVectorType::get(ElementType: Type::getInt32Ty(C&: F.getContext()), NumElts: VectorLength / 2); |
| 91 | bool Changed = false; |
| 92 | for (Instruction &I : make_early_inc_range(Range: instructions(F))) { |
| 93 | AllocaInst *AI = getPairAlloca(I, PairTy, VectorLength); |
| 94 | if (!AI) |
| 95 | continue; |
| 96 | |
| 97 | IRBuilder<> Builder(&I); |
| 98 | CallInst *Call; |
| 99 | if (auto *SI = dyn_cast<StoreInst>(Val: &I)) { |
| 100 | Call = Builder.CreateCall( |
| 101 | Callee: Intrinsic::getOrInsertDeclaration(M: F.getParent(), |
| 102 | id: Intrinsic::hexagon_LX_SS16b_new), |
| 103 | Args: {AI, SI->getValueOperand()}); |
| 104 | } else { |
| 105 | Call = Builder.CreateCall( |
| 106 | Callee: Intrinsic::getOrInsertDeclaration(M: F.getParent(), |
| 107 | id: Intrinsic::hexagon_LX_LL16b_tmp), |
| 108 | Args: AI); |
| 109 | I.replaceAllUsesWith(V: Call); |
| 110 | } |
| 111 | Call->setAAMetadata(I.getAAMetadata()); |
| 112 | Call->copyMetadata(SrcInst: I, WL: {LLVMContext::MD_access_group}); |
| 113 | I.eraseFromParent(); |
| 114 | Changed = true; |
| 115 | } |
| 116 | return Changed; |
| 117 | } |
| 118 | |
| 119 | FunctionPass *llvm::createHexagonLowerHLXPairAllocas() { |
| 120 | return new HexagonLowerHLXPairAllocas(); |
| 121 | } |
| 122 | |