xref: /freebsd/contrib/llvm-project/llvm/lib/Target/AVR/AVR.h (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===-- AVR.h - Top-level interface for AVR representation ------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains the entry points for global functions defined in the LLVM
100b57cec5SDimitry Andric // AVR back-end.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric 
140b57cec5SDimitry Andric #ifndef LLVM_AVR_H
150b57cec5SDimitry Andric #define LLVM_AVR_H
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h"
18*81ad6265SDimitry Andric #include "llvm/Pass.h"
19*81ad6265SDimitry Andric #include "llvm/PassRegistry.h"
200b57cec5SDimitry Andric #include "llvm/Target/TargetMachine.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric namespace llvm {
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric class AVRTargetMachine;
250b57cec5SDimitry Andric class FunctionPass;
260b57cec5SDimitry Andric 
27fe6060f1SDimitry Andric Pass *createAVRShiftExpandPass();
280b57cec5SDimitry Andric FunctionPass *createAVRISelDag(AVRTargetMachine &TM,
290b57cec5SDimitry Andric                                CodeGenOpt::Level OptLevel);
300b57cec5SDimitry Andric FunctionPass *createAVRExpandPseudoPass();
310b57cec5SDimitry Andric FunctionPass *createAVRFrameAnalyzerPass();
320b57cec5SDimitry Andric FunctionPass *createAVRBranchSelectionPass();
330b57cec5SDimitry Andric 
34fe6060f1SDimitry Andric void initializeAVRShiftExpandPass(PassRegistry &);
350b57cec5SDimitry Andric void initializeAVRExpandPseudoPass(PassRegistry &);
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric /// Contains the AVR backend.
380b57cec5SDimitry Andric namespace AVR {
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric /// An integer that identifies all of the supported AVR address spaces.
4104eeddc0SDimitry Andric enum AddressSpace {
4204eeddc0SDimitry Andric   DataMemory,
4304eeddc0SDimitry Andric   ProgramMemory,
4404eeddc0SDimitry Andric   ProgramMemory1,
4504eeddc0SDimitry Andric   ProgramMemory2,
4604eeddc0SDimitry Andric   ProgramMemory3,
4704eeddc0SDimitry Andric   ProgramMemory4,
4804eeddc0SDimitry Andric   ProgramMemory5,
4904eeddc0SDimitry Andric   NumAddrSpaces,
5004eeddc0SDimitry Andric };
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric /// Checks if a given type is a pointer to program memory.
530b57cec5SDimitry Andric template <typename T> bool isProgramMemoryAddress(T *V) {
5404eeddc0SDimitry Andric   auto *PT = cast<PointerType>(V->getType());
5504eeddc0SDimitry Andric   assert(PT != nullptr && "unexpected MemSDNode");
5604eeddc0SDimitry Andric   return PT->getAddressSpace() == ProgramMemory ||
5704eeddc0SDimitry Andric          PT->getAddressSpace() == ProgramMemory1 ||
5804eeddc0SDimitry Andric          PT->getAddressSpace() == ProgramMemory2 ||
5904eeddc0SDimitry Andric          PT->getAddressSpace() == ProgramMemory3 ||
6004eeddc0SDimitry Andric          PT->getAddressSpace() == ProgramMemory4 ||
6104eeddc0SDimitry Andric          PT->getAddressSpace() == ProgramMemory5;
6204eeddc0SDimitry Andric }
6304eeddc0SDimitry Andric 
6404eeddc0SDimitry Andric template <typename T> AddressSpace getAddressSpace(T *V) {
6504eeddc0SDimitry Andric   auto *PT = cast<PointerType>(V->getType());
6604eeddc0SDimitry Andric   assert(PT != nullptr && "unexpected MemSDNode");
6704eeddc0SDimitry Andric   unsigned AS = PT->getAddressSpace();
6804eeddc0SDimitry Andric   if (AS < NumAddrSpaces)
6904eeddc0SDimitry Andric     return static_cast<AddressSpace>(AS);
7004eeddc0SDimitry Andric   return NumAddrSpaces;
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric inline bool isProgramMemoryAccess(MemSDNode const *N) {
7404eeddc0SDimitry Andric   auto *V = N->getMemOperand()->getValue();
7504eeddc0SDimitry Andric   if (V != nullptr && isProgramMemoryAddress(V))
7604eeddc0SDimitry Andric     return true;
7704eeddc0SDimitry Andric   return false;
7804eeddc0SDimitry Andric }
790b57cec5SDimitry Andric 
8004eeddc0SDimitry Andric // Get the index of the program memory bank.
8104eeddc0SDimitry Andric //  -1: not program memory
8204eeddc0SDimitry Andric //   0: ordinary program memory
8304eeddc0SDimitry Andric // 1~5: extended program memory
8404eeddc0SDimitry Andric inline int getProgramMemoryBank(MemSDNode const *N) {
8504eeddc0SDimitry Andric   auto *V = N->getMemOperand()->getValue();
8604eeddc0SDimitry Andric   if (V == nullptr || !isProgramMemoryAddress(V))
8704eeddc0SDimitry Andric     return -1;
8804eeddc0SDimitry Andric   AddressSpace AS = getAddressSpace(V);
8904eeddc0SDimitry Andric   assert(ProgramMemory <= AS && AS <= ProgramMemory5);
9004eeddc0SDimitry Andric   return static_cast<int>(AS - ProgramMemory);
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric } // end of namespace AVR
940b57cec5SDimitry Andric 
950b57cec5SDimitry Andric } // end namespace llvm
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric #endif // LLVM_AVR_H
98