| 1 | //===-- AVR.h - Top-level interface for AVR representation ------*- C++ -*-===// |
| 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 | // This file contains the entry points for global functions defined in the LLVM |
| 10 | // AVR back-end. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_AVR_H |
| 15 | #define LLVM_AVR_H |
| 16 | |
| 17 | #include "llvm/CodeGen/MachineFunctionAnalysisManager.h" |
| 18 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 19 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
| 20 | #include "llvm/IR/Analysis.h" |
| 21 | #include "llvm/IR/PassManager.h" |
| 22 | #include "llvm/Pass.h" |
| 23 | #include "llvm/PassRegistry.h" |
| 24 | #include "llvm/Support/CodeGen.h" |
| 25 | #include "llvm/Target/TargetMachine.h" |
| 26 | |
| 27 | namespace llvm { |
| 28 | |
| 29 | class AVRTargetMachine; |
| 30 | class FunctionPass; |
| 31 | class PassRegistry; |
| 32 | |
| 33 | class AVRShiftExpandPass : public RequiredPassInfoMixin<AVRShiftExpandPass> { |
| 34 | public: |
| 35 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM); |
| 36 | }; |
| 37 | |
| 38 | Pass *createAVRShiftExpandLegacyPass(); |
| 39 | |
| 40 | class AVRISelDAGToDAGPass : public SelectionDAGISelPass { |
| 41 | public: |
| 42 | AVRISelDAGToDAGPass(AVRTargetMachine &TM, CodeGenOptLevel OptLevel); |
| 43 | }; |
| 44 | |
| 45 | FunctionPass *createAVRISelDagLegacyPass(AVRTargetMachine &TM, |
| 46 | CodeGenOptLevel OptLevel); |
| 47 | |
| 48 | class AVRExpandPseudoPass : public RequiredPassInfoMixin<AVRExpandPseudoPass> { |
| 49 | public: |
| 50 | PreservedAnalyses run(MachineFunction &MF, |
| 51 | MachineFunctionAnalysisManager &MFAM); |
| 52 | }; |
| 53 | |
| 54 | FunctionPass *createAVRExpandPseudoLegacyPass(); |
| 55 | |
| 56 | class AVRFrameAnalyzerPass |
| 57 | : public RequiredPassInfoMixin<AVRFrameAnalyzerPass> { |
| 58 | public: |
| 59 | PreservedAnalyses run(MachineFunction &MF, |
| 60 | MachineFunctionAnalysisManager &MFAM); |
| 61 | }; |
| 62 | |
| 63 | FunctionPass *createAVRFrameAnalyzerLegacyPass(); |
| 64 | |
| 65 | void initializeAVRAsmPrinterPass(PassRegistry &); |
| 66 | void initializeAVRDAGToDAGISelLegacyPass(PassRegistry &); |
| 67 | void initializeAVRExpandPseudoLegacyPass(PassRegistry &); |
| 68 | void initializeAVRShiftExpandLegacyPass(PassRegistry &); |
| 69 | |
| 70 | /// Contains the AVR backend. |
| 71 | namespace AVR { |
| 72 | |
| 73 | /// An integer that identifies all of the supported AVR address spaces. |
| 74 | enum AddressSpace { |
| 75 | DataMemory, |
| 76 | ProgramMemory, |
| 77 | ProgramMemory1, |
| 78 | ProgramMemory2, |
| 79 | ProgramMemory3, |
| 80 | ProgramMemory4, |
| 81 | ProgramMemory5, |
| 82 | NumAddrSpaces, |
| 83 | }; |
| 84 | |
| 85 | /// Checks if a given type is a pointer to program memory. |
| 86 | template <typename T> bool isProgramMemoryAddress(T *V) { |
| 87 | auto *PT = cast<PointerType>(V->getType()); |
| 88 | assert(PT != nullptr && "unexpected MemSDNode" ); |
| 89 | return PT->getAddressSpace() == ProgramMemory || |
| 90 | PT->getAddressSpace() == ProgramMemory1 || |
| 91 | PT->getAddressSpace() == ProgramMemory2 || |
| 92 | PT->getAddressSpace() == ProgramMemory3 || |
| 93 | PT->getAddressSpace() == ProgramMemory4 || |
| 94 | PT->getAddressSpace() == ProgramMemory5; |
| 95 | } |
| 96 | |
| 97 | template <typename T> AddressSpace getAddressSpace(T *V) { |
| 98 | auto *PT = cast<PointerType>(V->getType()); |
| 99 | assert(PT != nullptr && "unexpected MemSDNode" ); |
| 100 | unsigned AS = PT->getAddressSpace(); |
| 101 | if (AS < NumAddrSpaces) |
| 102 | return static_cast<AddressSpace>(AS); |
| 103 | return NumAddrSpaces; |
| 104 | } |
| 105 | |
| 106 | inline bool isProgramMemoryAccess(MemSDNode const *N) { |
| 107 | auto *V = N->getMemOperand()->getValue(); |
| 108 | if (V != nullptr && isProgramMemoryAddress(V)) |
| 109 | return true; |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | // Get the index of the program memory bank. |
| 114 | // -1: not program memory |
| 115 | // 0: ordinary program memory |
| 116 | // 1~5: extended program memory |
| 117 | inline int getProgramMemoryBank(MemSDNode const *N) { |
| 118 | auto *V = N->getMemOperand()->getValue(); |
| 119 | if (V == nullptr || !isProgramMemoryAddress(V)) |
| 120 | return -1; |
| 121 | AddressSpace AS = getAddressSpace(V); |
| 122 | assert(ProgramMemory <= AS && AS <= ProgramMemory5); |
| 123 | return static_cast<int>(AS - ProgramMemory); |
| 124 | } |
| 125 | |
| 126 | } // end of namespace AVR |
| 127 | |
| 128 | } // end namespace llvm |
| 129 | |
| 130 | #endif // LLVM_AVR_H |
| 131 | |