blob: 3d7a7c979961e862fa140dcd85b7b8f8559b828e [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===-- AArch64AdvSIMDScalar.cpp - Replace dead defs w/ zero reg --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// When profitable, replace GPR targeting i64 instructions with their
10// AdvSIMD scalar equivalents. Generally speaking, "profitable" is defined
11// as minimizing the number of cross-class register copies.
12//===----------------------------------------------------------------------===//
13
14//===----------------------------------------------------------------------===//
15// TODO: Graph based predicate heuristics.
16// Walking the instruction list linearly will get many, perhaps most, of
17// the cases, but to do a truly thorough job of this, we need a more
18// wholistic approach.
19//
20// This optimization is very similar in spirit to the register allocator's
21// spill placement, only here we're determining where to place cross-class
22// register copies rather than spills. As such, a similar approach is
23// called for.
24//
25// We want to build up a set of graphs of all instructions which are candidates
26// for transformation along with instructions which generate their inputs and
27// consume their outputs. For each edge in the graph, we assign a weight
28// based on whether there is a copy required there (weight zero if not) and
29// the block frequency of the block containing the defining or using
30// instruction, whichever is less. Our optimization is then a graph problem
31// to minimize the total weight of all the graphs, then transform instructions
32// and add or remove copy instructions as called for to implement the
33// solution.
34//===----------------------------------------------------------------------===//
35
36#include "AArch64.h"
37#include "AArch64InstrInfo.h"
38#include "AArch64RegisterInfo.h"
Eric Christopherd9134482014-08-04 21:25:23 +000039#include "AArch64Subtarget.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000040#include "llvm/ADT/Statistic.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000041#include "llvm/CodeGen/MachineFunction.h"
Benjamin Kramer1f8930e2014-07-25 11:42:14 +000042#include "llvm/CodeGen/MachineFunctionPass.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000043#include "llvm/CodeGen/MachineInstr.h"
44#include "llvm/CodeGen/MachineInstrBuilder.h"
45#include "llvm/CodeGen/MachineRegisterInfo.h"
46#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/Debug.h"
48#include "llvm/Support/raw_ostream.h"
49using namespace llvm;
50
51#define DEBUG_TYPE "aarch64-simd-scalar"
52
53// Allow forcing all i64 operations with equivalent SIMD instructions to use
54// them. For stress-testing the transformation function.
55static cl::opt<bool>
56TransformAll("aarch64-simd-scalar-force-all",
57 cl::desc("Force use of AdvSIMD scalar instructions everywhere"),
58 cl::init(false), cl::Hidden);
59
60STATISTIC(NumScalarInsnsUsed, "Number of scalar instructions used");
61STATISTIC(NumCopiesDeleted, "Number of cross-class copies deleted");
62STATISTIC(NumCopiesInserted, "Number of cross-class copies inserted");
63
Chad Rosier794b9b22015-08-05 15:18:58 +000064namespace llvm {
65void initializeAArch64AdvSIMDScalarPass(PassRegistry &);
66}
67
68#define AARCH64_ADVSIMD_NAME "AdvSIMD Scalar Operation Optimization"
69
Tim Northover3b0846e2014-05-24 12:50:23 +000070namespace {
71class AArch64AdvSIMDScalar : public MachineFunctionPass {
72 MachineRegisterInfo *MRI;
Eric Christopherf761d902015-01-30 01:10:18 +000073 const TargetInstrInfo *TII;
Tim Northover3b0846e2014-05-24 12:50:23 +000074
75private:
76 // isProfitableToTransform - Predicate function to determine whether an
77 // instruction should be transformed to its equivalent AdvSIMD scalar
78 // instruction. "add Xd, Xn, Xm" ==> "add Dd, Da, Db", for example.
79 bool isProfitableToTransform(const MachineInstr *MI) const;
80
81 // transformInstruction - Perform the transformation of an instruction
82 // to its equivalant AdvSIMD scalar instruction. Update inputs and outputs
83 // to be the correct register class, minimizing cross-class copies.
84 void transformInstruction(MachineInstr *MI);
85
86 // processMachineBasicBlock - Main optimzation loop.
87 bool processMachineBasicBlock(MachineBasicBlock *MBB);
88
89public:
90 static char ID; // Pass identification, replacement for typeid.
Chad Rosier794b9b22015-08-05 15:18:58 +000091 explicit AArch64AdvSIMDScalar() : MachineFunctionPass(ID) {
92 initializeAArch64AdvSIMDScalarPass(*PassRegistry::getPassRegistry());
93 }
Tim Northover3b0846e2014-05-24 12:50:23 +000094
95 bool runOnMachineFunction(MachineFunction &F) override;
96
97 const char *getPassName() const override {
Chad Rosier794b9b22015-08-05 15:18:58 +000098 return AARCH64_ADVSIMD_NAME;
Tim Northover3b0846e2014-05-24 12:50:23 +000099 }
100
101 void getAnalysisUsage(AnalysisUsage &AU) const override {
102 AU.setPreservesCFG();
103 MachineFunctionPass::getAnalysisUsage(AU);
104 }
105};
106char AArch64AdvSIMDScalar::ID = 0;
107} // end anonymous namespace
108
Chad Rosier794b9b22015-08-05 15:18:58 +0000109INITIALIZE_PASS(AArch64AdvSIMDScalar, "aarch64-simd-scalar",
110 AARCH64_ADVSIMD_NAME, false, false)
111
Tim Northover3b0846e2014-05-24 12:50:23 +0000112static bool isGPR64(unsigned Reg, unsigned SubReg,
113 const MachineRegisterInfo *MRI) {
114 if (SubReg)
115 return false;
116 if (TargetRegisterInfo::isVirtualRegister(Reg))
117 return MRI->getRegClass(Reg)->hasSuperClassEq(&AArch64::GPR64RegClass);
118 return AArch64::GPR64RegClass.contains(Reg);
119}
120
121static bool isFPR64(unsigned Reg, unsigned SubReg,
122 const MachineRegisterInfo *MRI) {
123 if (TargetRegisterInfo::isVirtualRegister(Reg))
124 return (MRI->getRegClass(Reg)->hasSuperClassEq(&AArch64::FPR64RegClass) &&
125 SubReg == 0) ||
126 (MRI->getRegClass(Reg)->hasSuperClassEq(&AArch64::FPR128RegClass) &&
127 SubReg == AArch64::dsub);
128 // Physical register references just check the register class directly.
129 return (AArch64::FPR64RegClass.contains(Reg) && SubReg == 0) ||
130 (AArch64::FPR128RegClass.contains(Reg) && SubReg == AArch64::dsub);
131}
132
133// getSrcFromCopy - Get the original source register for a GPR64 <--> FPR64
134// copy instruction. Return zero_reg if the instruction is not a copy.
Quentin Colombet9598f102016-04-22 18:09:14 +0000135static MachineOperand *getSrcFromCopy(MachineInstr *MI,
136 const MachineRegisterInfo *MRI,
137 unsigned &SubReg) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000138 SubReg = 0;
139 // The "FMOV Xd, Dn" instruction is the typical form.
140 if (MI->getOpcode() == AArch64::FMOVDXr ||
141 MI->getOpcode() == AArch64::FMOVXDr)
Quentin Colombet9598f102016-04-22 18:09:14 +0000142 return &MI->getOperand(1);
Tim Northover3b0846e2014-05-24 12:50:23 +0000143 // A lane zero extract "UMOV.d Xd, Vn[0]" is equivalent. We shouldn't see
144 // these at this stage, but it's easy to check for.
145 if (MI->getOpcode() == AArch64::UMOVvi64 && MI->getOperand(2).getImm() == 0) {
146 SubReg = AArch64::dsub;
Quentin Colombet9598f102016-04-22 18:09:14 +0000147 return &MI->getOperand(1);
Tim Northover3b0846e2014-05-24 12:50:23 +0000148 }
149 // Or just a plain COPY instruction. This can be directly to/from FPR64,
150 // or it can be a dsub subreg reference to an FPR128.
151 if (MI->getOpcode() == AArch64::COPY) {
152 if (isFPR64(MI->getOperand(0).getReg(), MI->getOperand(0).getSubReg(),
153 MRI) &&
154 isGPR64(MI->getOperand(1).getReg(), MI->getOperand(1).getSubReg(), MRI))
Quentin Colombet9598f102016-04-22 18:09:14 +0000155 return &MI->getOperand(1);
Tim Northover3b0846e2014-05-24 12:50:23 +0000156 if (isGPR64(MI->getOperand(0).getReg(), MI->getOperand(0).getSubReg(),
157 MRI) &&
158 isFPR64(MI->getOperand(1).getReg(), MI->getOperand(1).getSubReg(),
159 MRI)) {
160 SubReg = MI->getOperand(1).getSubReg();
Quentin Colombet9598f102016-04-22 18:09:14 +0000161 return &MI->getOperand(1);
Tim Northover3b0846e2014-05-24 12:50:23 +0000162 }
163 }
164
165 // Otherwise, this is some other kind of instruction.
Quentin Colombet9598f102016-04-22 18:09:14 +0000166 return nullptr;
Tim Northover3b0846e2014-05-24 12:50:23 +0000167}
168
169// getTransformOpcode - For any opcode for which there is an AdvSIMD equivalent
170// that we're considering transforming to, return that AdvSIMD opcode. For all
171// others, return the original opcode.
Matthias Braunfa3872e2015-05-18 20:27:55 +0000172static unsigned getTransformOpcode(unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000173 switch (Opc) {
174 default:
175 break;
176 // FIXME: Lots more possibilities.
177 case AArch64::ADDXrr:
178 return AArch64::ADDv1i64;
179 case AArch64::SUBXrr:
180 return AArch64::SUBv1i64;
Chad Rosier5908ab42014-08-04 21:20:25 +0000181 case AArch64::ANDXrr:
182 return AArch64::ANDv8i8;
183 case AArch64::EORXrr:
184 return AArch64::EORv8i8;
185 case AArch64::ORRXrr:
186 return AArch64::ORRv8i8;
Tim Northover3b0846e2014-05-24 12:50:23 +0000187 }
188 // No AdvSIMD equivalent, so just return the original opcode.
189 return Opc;
190}
191
192static bool isTransformable(const MachineInstr *MI) {
Matthias Braunfa3872e2015-05-18 20:27:55 +0000193 unsigned Opc = MI->getOpcode();
Tim Northover3b0846e2014-05-24 12:50:23 +0000194 return Opc != getTransformOpcode(Opc);
195}
196
197// isProfitableToTransform - Predicate function to determine whether an
198// instruction should be transformed to its equivalent AdvSIMD scalar
199// instruction. "add Xd, Xn, Xm" ==> "add Dd, Da, Db", for example.
200bool
201AArch64AdvSIMDScalar::isProfitableToTransform(const MachineInstr *MI) const {
202 // If this instruction isn't eligible to be transformed (no SIMD equivalent),
203 // early exit since that's the common case.
204 if (!isTransformable(MI))
205 return false;
206
207 // Count the number of copies we'll need to add and approximate the number
208 // of copies that a transform will enable us to remove.
209 unsigned NumNewCopies = 3;
210 unsigned NumRemovableCopies = 0;
211
212 unsigned OrigSrc0 = MI->getOperand(1).getReg();
213 unsigned OrigSrc1 = MI->getOperand(2).getReg();
Quentin Colombet9598f102016-04-22 18:09:14 +0000214 unsigned SubReg0;
215 unsigned SubReg1;
Tim Northover3b0846e2014-05-24 12:50:23 +0000216 if (!MRI->def_empty(OrigSrc0)) {
217 MachineRegisterInfo::def_instr_iterator Def =
218 MRI->def_instr_begin(OrigSrc0);
219 assert(std::next(Def) == MRI->def_instr_end() && "Multiple def in SSA!");
Quentin Colombet9598f102016-04-22 18:09:14 +0000220 MachineOperand *MOSrc0 = getSrcFromCopy(&*Def, MRI, SubReg0);
Tim Northover3b0846e2014-05-24 12:50:23 +0000221 // If the source was from a copy, we don't need to insert a new copy.
Quentin Colombet9598f102016-04-22 18:09:14 +0000222 if (MOSrc0)
Tim Northover3b0846e2014-05-24 12:50:23 +0000223 --NumNewCopies;
224 // If there are no other users of the original source, we can delete
225 // that instruction.
Quentin Colombet9598f102016-04-22 18:09:14 +0000226 if (MOSrc0 && MRI->hasOneNonDBGUse(OrigSrc0))
Tim Northover3b0846e2014-05-24 12:50:23 +0000227 ++NumRemovableCopies;
228 }
229 if (!MRI->def_empty(OrigSrc1)) {
230 MachineRegisterInfo::def_instr_iterator Def =
231 MRI->def_instr_begin(OrigSrc1);
232 assert(std::next(Def) == MRI->def_instr_end() && "Multiple def in SSA!");
Quentin Colombet9598f102016-04-22 18:09:14 +0000233 MachineOperand *MOSrc1 = getSrcFromCopy(&*Def, MRI, SubReg1);
234 if (MOSrc1)
Tim Northover3b0846e2014-05-24 12:50:23 +0000235 --NumNewCopies;
236 // If there are no other users of the original source, we can delete
237 // that instruction.
Quentin Colombet9598f102016-04-22 18:09:14 +0000238 if (MOSrc1 && MRI->hasOneNonDBGUse(OrigSrc1))
Tim Northover3b0846e2014-05-24 12:50:23 +0000239 ++NumRemovableCopies;
240 }
241
242 // If any of the uses of the original instructions is a cross class copy,
243 // that's a copy that will be removable if we transform. Likewise, if
244 // any of the uses is a transformable instruction, it's likely the tranforms
245 // will chain, enabling us to save a copy there, too. This is an aggressive
246 // heuristic that approximates the graph based cost analysis described above.
247 unsigned Dst = MI->getOperand(0).getReg();
248 bool AllUsesAreCopies = true;
249 for (MachineRegisterInfo::use_instr_nodbg_iterator
250 Use = MRI->use_instr_nodbg_begin(Dst),
251 E = MRI->use_instr_nodbg_end();
252 Use != E; ++Use) {
253 unsigned SubReg;
254 if (getSrcFromCopy(&*Use, MRI, SubReg) || isTransformable(&*Use))
255 ++NumRemovableCopies;
256 // If the use is an INSERT_SUBREG, that's still something that can
257 // directly use the FPR64, so we don't invalidate AllUsesAreCopies. It's
258 // preferable to have it use the FPR64 in most cases, as if the source
259 // vector is an IMPLICIT_DEF, the INSERT_SUBREG just goes away entirely.
260 // Ditto for a lane insert.
261 else if (Use->getOpcode() == AArch64::INSERT_SUBREG ||
262 Use->getOpcode() == AArch64::INSvi64gpr)
263 ;
264 else
265 AllUsesAreCopies = false;
266 }
267 // If all of the uses of the original destination register are copies to
268 // FPR64, then we won't end up having a new copy back to GPR64 either.
269 if (AllUsesAreCopies)
270 --NumNewCopies;
271
272 // If a transform will not increase the number of cross-class copies required,
273 // return true.
274 if (NumNewCopies <= NumRemovableCopies)
275 return true;
276
277 // Finally, even if we otherwise wouldn't transform, check if we're forcing
278 // transformation of everything.
279 return TransformAll;
280}
281
Eric Christopherf761d902015-01-30 01:10:18 +0000282static MachineInstr *insertCopy(const TargetInstrInfo *TII, MachineInstr *MI,
Tim Northover3b0846e2014-05-24 12:50:23 +0000283 unsigned Dst, unsigned Src, bool IsKill) {
284 MachineInstrBuilder MIB =
285 BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII->get(AArch64::COPY),
286 Dst)
287 .addReg(Src, getKillRegState(IsKill));
288 DEBUG(dbgs() << " adding copy: " << *MIB);
289 ++NumCopiesInserted;
290 return MIB;
291}
292
293// transformInstruction - Perform the transformation of an instruction
294// to its equivalant AdvSIMD scalar instruction. Update inputs and outputs
295// to be the correct register class, minimizing cross-class copies.
296void AArch64AdvSIMDScalar::transformInstruction(MachineInstr *MI) {
297 DEBUG(dbgs() << "Scalar transform: " << *MI);
298
299 MachineBasicBlock *MBB = MI->getParent();
Matthias Braunfa3872e2015-05-18 20:27:55 +0000300 unsigned OldOpc = MI->getOpcode();
301 unsigned NewOpc = getTransformOpcode(OldOpc);
Tim Northover3b0846e2014-05-24 12:50:23 +0000302 assert(OldOpc != NewOpc && "transform an instruction to itself?!");
303
304 // Check if we need a copy for the source registers.
305 unsigned OrigSrc0 = MI->getOperand(1).getReg();
306 unsigned OrigSrc1 = MI->getOperand(2).getReg();
307 unsigned Src0 = 0, SubReg0;
308 unsigned Src1 = 0, SubReg1;
Quentin Colombet9598f102016-04-22 18:09:14 +0000309 bool KillSrc0 = false, KillSrc1 = false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000310 if (!MRI->def_empty(OrigSrc0)) {
311 MachineRegisterInfo::def_instr_iterator Def =
312 MRI->def_instr_begin(OrigSrc0);
313 assert(std::next(Def) == MRI->def_instr_end() && "Multiple def in SSA!");
Quentin Colombet9598f102016-04-22 18:09:14 +0000314 MachineOperand *MOSrc0 = getSrcFromCopy(&*Def, MRI, SubReg0);
Tim Northover3b0846e2014-05-24 12:50:23 +0000315 // If there are no other users of the original source, we can delete
316 // that instruction.
Quentin Colombet9598f102016-04-22 18:09:14 +0000317 if (MOSrc0) {
318 Src0 = MOSrc0->getReg();
319 KillSrc0 = MOSrc0->isKill();
320 // Src0 is going to be reused, thus, it cannot be killed anymore.
321 MOSrc0->setIsKill(false);
322 if (MRI->hasOneNonDBGUse(OrigSrc0)) {
323 assert(MOSrc0 && "Can't delete copy w/o a valid original source!");
324 Def->eraseFromParent();
325 ++NumCopiesDeleted;
326 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000327 }
328 }
329 if (!MRI->def_empty(OrigSrc1)) {
330 MachineRegisterInfo::def_instr_iterator Def =
331 MRI->def_instr_begin(OrigSrc1);
332 assert(std::next(Def) == MRI->def_instr_end() && "Multiple def in SSA!");
Quentin Colombet9598f102016-04-22 18:09:14 +0000333 MachineOperand *MOSrc1 = getSrcFromCopy(&*Def, MRI, SubReg1);
Tim Northover3b0846e2014-05-24 12:50:23 +0000334 // If there are no other users of the original source, we can delete
335 // that instruction.
Quentin Colombet9598f102016-04-22 18:09:14 +0000336 if (MOSrc1) {
337 Src1 = MOSrc1->getReg();
338 KillSrc1 = MOSrc1->isKill();
339 // Src0 is going to be reused, thus, it cannot be killed anymore.
340 MOSrc1->setIsKill(false);
341 if (MRI->hasOneNonDBGUse(OrigSrc1)) {
342 assert(MOSrc1 && "Can't delete copy w/o a valid original source!");
343 Def->eraseFromParent();
344 ++NumCopiesDeleted;
345 }
Tim Northover3b0846e2014-05-24 12:50:23 +0000346 }
347 }
348 // If we weren't able to reference the original source directly, create a
349 // copy.
350 if (!Src0) {
351 SubReg0 = 0;
352 Src0 = MRI->createVirtualRegister(&AArch64::FPR64RegClass);
Quentin Colombet9598f102016-04-22 18:09:14 +0000353 insertCopy(TII, MI, Src0, OrigSrc0, KillSrc0);
354 KillSrc0 = true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000355 }
356 if (!Src1) {
357 SubReg1 = 0;
358 Src1 = MRI->createVirtualRegister(&AArch64::FPR64RegClass);
Quentin Colombet9598f102016-04-22 18:09:14 +0000359 insertCopy(TII, MI, Src1, OrigSrc1, KillSrc1);
360 KillSrc1 = true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000361 }
362
363 // Create a vreg for the destination.
364 // FIXME: No need to do this if the ultimate user expects an FPR64.
365 // Check for that and avoid the copy if possible.
366 unsigned Dst = MRI->createVirtualRegister(&AArch64::FPR64RegClass);
367
368 // For now, all of the new instructions have the same simple three-register
369 // form, so no need to special case based on what instruction we're
370 // building.
371 BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(NewOpc), Dst)
Quentin Colombet9598f102016-04-22 18:09:14 +0000372 .addReg(Src0, getKillRegState(KillSrc0), SubReg0)
373 .addReg(Src1, getKillRegState(KillSrc1), SubReg1);
Tim Northover3b0846e2014-05-24 12:50:23 +0000374
375 // Now copy the result back out to a GPR.
376 // FIXME: Try to avoid this if all uses could actually just use the FPR64
377 // directly.
378 insertCopy(TII, MI, MI->getOperand(0).getReg(), Dst, true);
379
380 // Erase the old instruction.
381 MI->eraseFromParent();
382
383 ++NumScalarInsnsUsed;
384}
385
386// processMachineBasicBlock - Main optimzation loop.
387bool AArch64AdvSIMDScalar::processMachineBasicBlock(MachineBasicBlock *MBB) {
388 bool Changed = false;
389 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;) {
390 MachineInstr *MI = I;
391 ++I;
392 if (isProfitableToTransform(MI)) {
393 transformInstruction(MI);
394 Changed = true;
395 }
396 }
397 return Changed;
398}
399
400// runOnMachineFunction - Pass entry point from PassManager.
401bool AArch64AdvSIMDScalar::runOnMachineFunction(MachineFunction &mf) {
402 bool Changed = false;
403 DEBUG(dbgs() << "***** AArch64AdvSIMDScalar *****\n");
404
Andrew Kaylor1ac98bb2016-04-25 21:58:52 +0000405 if (skipFunction(*mf.getFunction()))
406 return false;
407
Tim Northover3b0846e2014-05-24 12:50:23 +0000408 MRI = &mf.getRegInfo();
Eric Christopherf761d902015-01-30 01:10:18 +0000409 TII = mf.getSubtarget().getInstrInfo();
Tim Northover3b0846e2014-05-24 12:50:23 +0000410
411 // Just check things on a one-block-at-a-time basis.
412 for (MachineFunction::iterator I = mf.begin(), E = mf.end(); I != E; ++I)
Duncan P. N. Exon Smithd3b9df02015-10-13 20:02:15 +0000413 if (processMachineBasicBlock(&*I))
Tim Northover3b0846e2014-05-24 12:50:23 +0000414 Changed = true;
415 return Changed;
416}
417
418// createAArch64AdvSIMDScalar - Factory function used by AArch64TargetMachine
419// to add the pass to the PassManager.
420FunctionPass *llvm::createAArch64AdvSIMDScalar() {
421 return new AArch64AdvSIMDScalar();
422}