annotate lib/Target/AVR/AVRRelaxMemOperations.cpp @ 147:c2174574ed3a

LLVM 10
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 14 Aug 2019 16:55:33 +0900
parents 3a76565eade5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===-- AVRRelaxMemOperations.cpp - Relax out of range loads/stores -------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
147
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
c2174574ed3a LLVM 10
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 134
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 // This file contains a pass which relaxes out of range memory operations into
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // equivalent operations which handle bigger addresses.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 #include "AVR.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #include "AVRInstrInfo.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #include "AVRTargetMachine.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "MCTargetDesc/AVRMCTargetDesc.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/CodeGen/MachineFunctionPass.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
134
3a76565eade5 update 5.0.1
mir3636
parents: 121
diff changeset
22 #include "llvm/CodeGen/TargetRegisterInfo.h"
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 #define AVR_RELAX_MEM_OPS_NAME "AVR memory operation relaxation pass"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 namespace {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 class AVRRelaxMem : public MachineFunctionPass {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 static char ID;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 AVRRelaxMem() : MachineFunctionPass(ID) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 initializeAVRRelaxMemPass(*PassRegistry::getPassRegistry());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 bool runOnMachineFunction(MachineFunction &MF) override;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 StringRef getPassName() const override { return AVR_RELAX_MEM_OPS_NAME; }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 typedef MachineBasicBlock Block;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 typedef Block::iterator BlockIt;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 const TargetInstrInfo *TII;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 template <unsigned OP> bool relax(Block &MBB, BlockIt MBBI);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 bool runOnBasicBlock(Block &MBB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 bool runOnInstruction(Block &MBB, BlockIt MBBI);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 MachineInstrBuilder buildMI(Block &MBB, BlockIt MBBI, unsigned Opcode) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 return BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(Opcode));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 char AVRRelaxMem::ID = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 bool AVRRelaxMem::runOnMachineFunction(MachineFunction &MF) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 bool Modified = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 TII = STI.getInstrInfo();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 for (Block &MBB : MF) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 bool BlockModified = runOnBasicBlock(MBB);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 Modified |= BlockModified;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 return Modified;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 bool AVRRelaxMem::runOnBasicBlock(Block &MBB) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 bool Modified = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 BlockIt MBBI = MBB.begin(), E = MBB.end();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 while (MBBI != E) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 BlockIt NMBBI = std::next(MBBI);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 Modified |= runOnInstruction(MBB, MBBI);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 MBBI = NMBBI;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 return Modified;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 template <>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 bool AVRRelaxMem::relax<AVR::STDWPtrQRr>(Block &MBB, BlockIt MBBI) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 MachineInstr &MI = *MBBI;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 MachineOperand &Ptr = MI.getOperand(0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 MachineOperand &Src = MI.getOperand(2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 int64_t Imm = MI.getOperand(1).getImm();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 // We can definitely optimise this better.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 if (Imm > 63) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 // Push the previous state of the pointer register.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 // This instruction must preserve the value.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 buildMI(MBB, MBBI, AVR::PUSHWRr)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 .addReg(Ptr.getReg());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 // Add the immediate to the pointer register.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 buildMI(MBB, MBBI, AVR::SBCIWRdK)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 .addReg(Ptr.getReg(), RegState::Define)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 .addReg(Ptr.getReg())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 .addImm(-Imm);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 // Store the value in the source register to the address
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 // pointed to by the pointer register.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 buildMI(MBB, MBBI, AVR::STWPtrRr)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 .addReg(Ptr.getReg())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 .addReg(Src.getReg(), getKillRegState(Src.isKill()));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 // Pop the original state of the pointer register.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 buildMI(MBB, MBBI, AVR::POPWRd)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 .addReg(Ptr.getReg(), getKillRegState(Ptr.isKill()));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 MI.removeFromParent();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 bool AVRRelaxMem::runOnInstruction(Block &MBB, BlockIt MBBI) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 MachineInstr &MI = *MBBI;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 int Opcode = MBBI->getOpcode();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 #define RELAX(Op) \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 case Op: \
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 return relax<Op>(MBB, MI)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 switch (Opcode) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 RELAX(AVR::STDWPtrQRr);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 #undef RELAX
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 } // end of anonymous namespace
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 INITIALIZE_PASS(AVRRelaxMem, "avr-relax-mem",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 AVR_RELAX_MEM_OPS_NAME, false, false)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 FunctionPass *createAVRRelaxMemPass() { return new AVRRelaxMem(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 } // end of namespace llvm