blob: 6899950c263322dd27ccdb5ea0a923f1fbff7ad5 [file] [log] [blame]
Ahmed Bougacha6756a2c2016-07-27 14:31:55 +00001//===- AArch64InstructionSelector.cpp ----------------------------*- C++ -*-==//
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/// \file
10/// This file implements the targeting of the InstructionSelector class for
11/// AArch64.
12/// \todo This should be generated by TableGen.
13//===----------------------------------------------------------------------===//
14
15#include "AArch64InstructionSelector.h"
16#include "AArch64InstrInfo.h"
17#include "AArch64RegisterBankInfo.h"
18#include "AArch64RegisterInfo.h"
19#include "AArch64Subtarget.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineInstr.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/CodeGen/MachineRegisterInfo.h"
25#include "llvm/IR/Type.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/raw_ostream.h"
28
29#define DEBUG_TYPE "aarch64-isel"
30
31using namespace llvm;
32
33#ifndef LLVM_BUILD_GLOBAL_ISEL
34#error "You shouldn't build this"
35#endif
36
37AArch64InstructionSelector::AArch64InstructionSelector(
38 const AArch64Subtarget &STI, const AArch64RegisterBankInfo &RBI)
39 : InstructionSelector(), TII(*STI.getInstrInfo()),
40 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
41
42/// Select the AArch64 opcode for the basic binary operation \p GenericOpc
43/// (such as G_OR or G_ADD), appropriate for the register bank \p RegBankID
44/// and of size \p OpSize.
45/// \returns \p GenericOpc if the combination is unsupported.
46static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
47 unsigned OpSize) {
48 switch (RegBankID) {
49 case AArch64::GPRRegBankID:
50 switch (OpSize) {
51 case 32:
52 switch (GenericOpc) {
53 case TargetOpcode::G_OR:
54 return AArch64::ORRWrr;
55 case TargetOpcode::G_ADD:
56 return AArch64::ADDWrr;
57 default:
58 return GenericOpc;
59 }
60 case 64:
61 switch (GenericOpc) {
62 case TargetOpcode::G_OR:
63 return AArch64::ORRXrr;
64 case TargetOpcode::G_ADD:
65 return AArch64::ADDXrr;
66 default:
67 return GenericOpc;
68 }
69 }
70 };
71 return GenericOpc;
72}
73
74bool AArch64InstructionSelector::select(MachineInstr &I) const {
75 assert(I.getParent() && "Instruction should be in a basic block!");
76 assert(I.getParent()->getParent() && "Instruction should be in a function!");
77
78 MachineBasicBlock &MBB = *I.getParent();
79 MachineFunction &MF = *MBB.getParent();
80 MachineRegisterInfo &MRI = MF.getRegInfo();
81
82 // FIXME: Is there *really* nothing to be done here? This assumes that
83 // no upstream pass introduces things like generic vreg on copies or
84 // target-specific instructions.
85 // We should document (and verify) that assumption.
86 if (!isPreISelGenericOpcode(I.getOpcode()))
87 return true;
88
89 if (I.getNumOperands() != I.getNumExplicitOperands()) {
90 DEBUG(dbgs() << "Generic instruction has unexpected implicit operands\n");
91 return false;
92 }
93
94 LLT Ty = I.getType();
95 assert(Ty.isValid() && "Generic instruction doesn't have a type");
96
97 // FIXME: Support unsized instructions (e.g., G_BR).
98 if (!Ty.isSized()) {
99 DEBUG(dbgs() << "Unsized generic instructions are unsupported\n");
100 return false;
101 }
102
103 // The size (in bits) of the operation, or 0 for the label type.
104 const unsigned OpSize = Ty.getSizeInBits();
105
106 switch (I.getOpcode()) {
107 case TargetOpcode::G_OR:
108 case TargetOpcode::G_ADD: {
109 DEBUG(dbgs() << "AArch64: Selecting: binop\n");
110
111 // Reject the various things we don't support yet.
112 {
113 const RegisterBank *PrevOpBank = nullptr;
114 for (auto &MO : I.operands()) {
115 // FIXME: Support non-register operands.
116 if (!MO.isReg()) {
117 DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
118 return false;
119 }
120
121 // FIXME: Can generic operations have physical registers operands? If
122 // so, this will need to be taught about that, and we'll need to get the
123 // bank out of the minimal class for the register.
124 // Either way, this needs to be documented (and possibly verified).
125 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
126 DEBUG(dbgs() << "Generic inst has physical register operand\n");
127 return false;
128 }
129
130 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
131 if (!OpBank) {
132 DEBUG(dbgs() << "Generic register has no bank or class\n");
133 return false;
134 }
135
136 if (PrevOpBank && OpBank != PrevOpBank) {
137 DEBUG(dbgs() << "Generic inst operands have different banks\n");
138 return false;
139 }
140 PrevOpBank = OpBank;
141 }
142 }
143
144 const unsigned DefReg = I.getOperand(0).getReg();
145 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
146
147 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
148 if (NewOpc == I.getOpcode())
149 return false;
150
151 I.setDesc(TII.get(NewOpc));
152 // FIXME: Should the type be always reset in setDesc?
153 I.setType(LLT());
154
155 // Now that we selected an opcode, we need to constrain the register
156 // operands to use appropriate classes.
157 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
158 }
159 }
160
161 return false;
162}