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/SelectionDAGNodes.h"
18 #include "llvm/Pass.h"
19 #include "llvm/PassRegistry.h"
20 #include "llvm/Target/TargetMachine.h"
21 
22 namespace llvm {
23 
24 class AVRTargetMachine;
25 class FunctionPass;
26 
27 Pass *createAVRShiftExpandPass();
28 FunctionPass *createAVRISelDag(AVRTargetMachine &TM,
29                                CodeGenOpt::Level OptLevel);
30 FunctionPass *createAVRExpandPseudoPass();
31 FunctionPass *createAVRFrameAnalyzerPass();
32 FunctionPass *createAVRBranchSelectionPass();
33 
34 void initializeAVRShiftExpandPass(PassRegistry &);
35 void initializeAVRExpandPseudoPass(PassRegistry &);
36 
37 /// Contains the AVR backend.
38 namespace AVR {
39 
40 /// An integer that identifies all of the supported AVR address spaces.
41 enum AddressSpace {
42   DataMemory,
43   ProgramMemory,
44   ProgramMemory1,
45   ProgramMemory2,
46   ProgramMemory3,
47   ProgramMemory4,
48   ProgramMemory5,
49   NumAddrSpaces,
50 };
51 
52 /// Checks if a given type is a pointer to program memory.
53 template <typename T> bool isProgramMemoryAddress(T *V) {
54   auto *PT = cast<PointerType>(V->getType());
55   assert(PT != nullptr && "unexpected MemSDNode");
56   return PT->getAddressSpace() == ProgramMemory ||
57          PT->getAddressSpace() == ProgramMemory1 ||
58          PT->getAddressSpace() == ProgramMemory2 ||
59          PT->getAddressSpace() == ProgramMemory3 ||
60          PT->getAddressSpace() == ProgramMemory4 ||
61          PT->getAddressSpace() == ProgramMemory5;
62 }
63 
64 template <typename T> AddressSpace getAddressSpace(T *V) {
65   auto *PT = cast<PointerType>(V->getType());
66   assert(PT != nullptr && "unexpected MemSDNode");
67   unsigned AS = PT->getAddressSpace();
68   if (AS < NumAddrSpaces)
69     return static_cast<AddressSpace>(AS);
70   return NumAddrSpaces;
71 }
72 
73 inline bool isProgramMemoryAccess(MemSDNode const *N) {
74   auto *V = N->getMemOperand()->getValue();
75   if (V != nullptr && isProgramMemoryAddress(V))
76     return true;
77   return false;
78 }
79 
80 // Get the index of the program memory bank.
81 //  -1: not program memory
82 //   0: ordinary program memory
83 // 1~5: extended program memory
84 inline int getProgramMemoryBank(MemSDNode const *N) {
85   auto *V = N->getMemOperand()->getValue();
86   if (V == nullptr || !isProgramMemoryAddress(V))
87     return -1;
88   AddressSpace AS = getAddressSpace(V);
89   assert(ProgramMemory <= AS && AS <= ProgramMemory5);
90   return static_cast<int>(AS - ProgramMemory);
91 }
92 
93 } // end of namespace AVR
94 
95 } // end namespace llvm
96 
97 #endif // LLVM_AVR_H
98