1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
//===-- ARMExpandPseudoInsts.cpp - Expand pseudo instructions -----*- C++ -*-=//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file contains a pass that expand pseudo instructions into target
// instructions to allow proper scheduling, if-conversion, and other late
// optimizations. This pass should be run after register allocation but before
// post- regalloc scheduling pass.
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "arm-pseudo"
#include "ARM.h"
#include "ARMBaseInstrInfo.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/Target/TargetRegisterInfo.h"
using namespace llvm;
namespace {
class ARMExpandPseudo : public MachineFunctionPass {
// Constants for register spacing in NEON load/store instructions.
enum NEONRegSpacing {
SingleSpc,
EvenDblSpc,
OddDblSpc
};
public:
static char ID;
ARMExpandPseudo() : MachineFunctionPass(ID) {}
const TargetInstrInfo *TII;
const TargetRegisterInfo *TRI;
virtual bool runOnMachineFunction(MachineFunction &Fn);
virtual const char *getPassName() const {
return "ARM pseudo instruction expansion pass";
}
private:
void TransferImpOps(MachineInstr &OldMI,
MachineInstrBuilder &UseMI, MachineInstrBuilder &DefMI);
bool ExpandMBB(MachineBasicBlock &MBB);
void ExpandVST4(MachineBasicBlock::iterator &MBBI, unsigned Opc,
bool hasWriteBack, NEONRegSpacing RegSpc);
};
char ARMExpandPseudo::ID = 0;
}
/// TransferImpOps - Transfer implicit operands on the pseudo instruction to
/// the instructions created from the expansion.
void ARMExpandPseudo::TransferImpOps(MachineInstr &OldMI,
MachineInstrBuilder &UseMI,
MachineInstrBuilder &DefMI) {
const TargetInstrDesc &Desc = OldMI.getDesc();
for (unsigned i = Desc.getNumOperands(), e = OldMI.getNumOperands();
i != e; ++i) {
const MachineOperand &MO = OldMI.getOperand(i);
assert(MO.isReg() && MO.getReg());
if (MO.isUse())
UseMI.addReg(MO.getReg(), getKillRegState(MO.isKill()));
else
DefMI.addReg(MO.getReg(),
getDefRegState(true) | getDeadRegState(MO.isDead()));
}
}
/// ExpandVST4 - Translate VST4 pseudo instructions with QQ or QQQQ register
/// operands to real VST4 instructions with 4 D register operands.
void ARMExpandPseudo::ExpandVST4(MachineBasicBlock::iterator &MBBI,
unsigned Opc, bool hasWriteBack,
NEONRegSpacing RegSpc) {
MachineInstr &MI = *MBBI;
MachineBasicBlock &MBB = *MI.getParent();
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(Opc));
unsigned OpIdx = 0;
if (hasWriteBack) {
bool DstIsDead = MI.getOperand(OpIdx).isDead();
unsigned DstReg = MI.getOperand(OpIdx++).getReg();
MIB.addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead));
}
// Copy the addrmode6 operands.
bool AddrIsKill = MI.getOperand(OpIdx).isKill();
MIB.addReg(MI.getOperand(OpIdx++).getReg(), getKillRegState(AddrIsKill));
MIB.addImm(MI.getOperand(OpIdx++).getImm());
if (hasWriteBack) {
// Copy the am6offset operand.
bool OffsetIsKill = MI.getOperand(OpIdx).isKill();
MIB.addReg(MI.getOperand(OpIdx++).getReg(), getKillRegState(OffsetIsKill));
}
bool SrcIsKill = MI.getOperand(OpIdx).isKill();
unsigned SrcReg = MI.getOperand(OpIdx).getReg();
unsigned D0, D1, D2, D3;
if (RegSpc == SingleSpc) {
D0 = TRI->getSubReg(SrcReg, ARM::dsub_0);
D1 = TRI->getSubReg(SrcReg, ARM::dsub_1);
D2 = TRI->getSubReg(SrcReg, ARM::dsub_2);
D3 = TRI->getSubReg(SrcReg, ARM::dsub_3);
} else if (RegSpc == EvenDblSpc) {
D0 = TRI->getSubReg(SrcReg, ARM::dsub_0);
D1 = TRI->getSubReg(SrcReg, ARM::dsub_2);
D2 = TRI->getSubReg(SrcReg, ARM::dsub_4);
D3 = TRI->getSubReg(SrcReg, ARM::dsub_6);
} else {
assert(RegSpc == OddDblSpc && "unknown register spacing for VST4");
D0 = TRI->getSubReg(SrcReg, ARM::dsub_1);
D1 = TRI->getSubReg(SrcReg, ARM::dsub_3);
D2 = TRI->getSubReg(SrcReg, ARM::dsub_5);
D3 = TRI->getSubReg(SrcReg, ARM::dsub_7);
}
MIB.addReg(D0, getKillRegState(SrcIsKill))
.addReg(D1, getKillRegState(SrcIsKill))
.addReg(D2, getKillRegState(SrcIsKill))
.addReg(D3, getKillRegState(SrcIsKill));
MIB = AddDefaultPred(MIB);
TransferImpOps(MI, MIB, MIB);
MI.eraseFromParent();
}
bool ARMExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) {
bool Modified = false;
MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
while (MBBI != E) {
MachineInstr &MI = *MBBI;
MachineBasicBlock::iterator NMBBI = llvm::next(MBBI);
bool ModifiedOp = true;
unsigned Opcode = MI.getOpcode();
switch (Opcode) {
default:
ModifiedOp = false;
break;
case ARM::tLDRpci_pic:
case ARM::t2LDRpci_pic: {
unsigned NewLdOpc = (Opcode == ARM::tLDRpci_pic)
? ARM::tLDRpci : ARM::t2LDRpci;
unsigned DstReg = MI.getOperand(0).getReg();
bool DstIsDead = MI.getOperand(0).isDead();
MachineInstrBuilder MIB1 =
AddDefaultPred(BuildMI(MBB, MBBI, MI.getDebugLoc(),
TII->get(NewLdOpc), DstReg)
.addOperand(MI.getOperand(1)));
(*MIB1).setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
MachineInstrBuilder MIB2 = BuildMI(MBB, MBBI, MI.getDebugLoc(),
TII->get(ARM::tPICADD))
.addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
.addReg(DstReg)
.addOperand(MI.getOperand(2));
TransferImpOps(MI, MIB1, MIB2);
MI.eraseFromParent();
break;
}
case ARM::t2MOVi32imm: {
unsigned PredReg = 0;
ARMCC::CondCodes Pred = llvm::getInstrPredicate(&MI, PredReg);
unsigned DstReg = MI.getOperand(0).getReg();
bool DstIsDead = MI.getOperand(0).isDead();
const MachineOperand &MO = MI.getOperand(1);
MachineInstrBuilder LO16, HI16;
LO16 = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::t2MOVi16),
DstReg);
HI16 = BuildMI(MBB, MBBI, MI.getDebugLoc(), TII->get(ARM::t2MOVTi16))
.addReg(DstReg, getDefRegState(true) | getDeadRegState(DstIsDead))
.addReg(DstReg);
if (MO.isImm()) {
unsigned Imm = MO.getImm();
unsigned Lo16 = Imm & 0xffff;
unsigned Hi16 = (Imm >> 16) & 0xffff;
LO16 = LO16.addImm(Lo16);
HI16 = HI16.addImm(Hi16);
} else {
const GlobalValue *GV = MO.getGlobal();
unsigned TF = MO.getTargetFlags();
LO16 = LO16.addGlobalAddress(GV, MO.getOffset(), TF | ARMII::MO_LO16);
HI16 = HI16.addGlobalAddress(GV, MO.getOffset(), TF | ARMII::MO_HI16);
}
(*LO16).setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
(*HI16).setMemRefs(MI.memoperands_begin(), MI.memoperands_end());
LO16.addImm(Pred).addReg(PredReg);
HI16.addImm(Pred).addReg(PredReg);
TransferImpOps(MI, LO16, HI16);
MI.eraseFromParent();
break;
}
case ARM::VMOVQQ: {
unsigned DstReg = MI.getOperand(0).getReg();
bool DstIsDead = MI.getOperand(0).isDead();
unsigned EvenDst = TRI->getSubReg(DstReg, ARM::qsub_0);
unsigned OddDst = TRI->getSubReg(DstReg, ARM::qsub_1);
unsigned SrcReg = MI.getOperand(1).getReg();
bool SrcIsKill = MI.getOperand(1).isKill();
unsigned EvenSrc = TRI->getSubReg(SrcReg, ARM::qsub_0);
unsigned OddSrc = TRI->getSubReg(SrcReg, ARM::qsub_1);
MachineInstrBuilder Even =
AddDefaultPred(BuildMI(MBB, MBBI, MI.getDebugLoc(),
TII->get(ARM::VMOVQ))
.addReg(EvenDst,
getDefRegState(true) | getDeadRegState(DstIsDead))
.addReg(EvenSrc, getKillRegState(SrcIsKill)));
MachineInstrBuilder Odd =
AddDefaultPred(BuildMI(MBB, MBBI, MI.getDebugLoc(),
TII->get(ARM::VMOVQ))
.addReg(OddDst,
getDefRegState(true) | getDeadRegState(DstIsDead))
.addReg(OddSrc, getKillRegState(SrcIsKill)));
TransferImpOps(MI, Even, Odd);
MI.eraseFromParent();
}
case ARM::VST4d8Pseudo:
ExpandVST4(MBBI, ARM::VST4d8, false, SingleSpc); break;
case ARM::VST4d16Pseudo:
ExpandVST4(MBBI, ARM::VST4d16, false, SingleSpc); break;
case ARM::VST4d32Pseudo:
ExpandVST4(MBBI, ARM::VST4d32, false, SingleSpc); break;
case ARM::VST1d64QPseudo:
ExpandVST4(MBBI, ARM::VST1d64Q, false, SingleSpc); break;
case ARM::VST4d8Pseudo_UPD:
ExpandVST4(MBBI, ARM::VST4d8_UPD, true, SingleSpc); break;
case ARM::VST4d16Pseudo_UPD:
ExpandVST4(MBBI, ARM::VST4d16_UPD, true, SingleSpc); break;
case ARM::VST4d32Pseudo_UPD:
ExpandVST4(MBBI, ARM::VST4d32_UPD, true, SingleSpc); break;
case ARM::VST1d64QPseudo_UPD:
ExpandVST4(MBBI, ARM::VST1d64Q_UPD, true, SingleSpc); break;
case ARM::VST4q8Pseudo_UPD:
ExpandVST4(MBBI, ARM::VST4q8_UPD, true, EvenDblSpc); break;
case ARM::VST4q16Pseudo_UPD:
ExpandVST4(MBBI, ARM::VST4q16_UPD, true, EvenDblSpc); break;
case ARM::VST4q32Pseudo_UPD:
ExpandVST4(MBBI, ARM::VST4q32_UPD, true, EvenDblSpc); break;
case ARM::VST4q8oddPseudo_UPD:
ExpandVST4(MBBI, ARM::VST4q8_UPD, true, OddDblSpc); break;
case ARM::VST4q16oddPseudo_UPD:
ExpandVST4(MBBI, ARM::VST4q16_UPD, true, OddDblSpc); break;
case ARM::VST4q32oddPseudo_UPD:
ExpandVST4(MBBI, ARM::VST4q32_UPD, true, OddDblSpc); break;
break;
}
if (ModifiedOp)
Modified = true;
MBBI = NMBBI;
}
return Modified;
}
bool ARMExpandPseudo::runOnMachineFunction(MachineFunction &MF) {
TII = MF.getTarget().getInstrInfo();
TRI = MF.getTarget().getRegisterInfo();
bool Modified = false;
for (MachineFunction::iterator MFI = MF.begin(), E = MF.end(); MFI != E;
++MFI)
Modified |= ExpandMBB(*MFI);
return Modified;
}
/// createARMExpandPseudoPass - returns an instance of the pseudo instruction
/// expansion pass.
FunctionPass *llvm::createARMExpandPseudoPass() {
return new ARMExpandPseudo();
}
|