blob: b17155a479118774490783ff6f00fb9c67e4b5d4 [file] [log] [blame]
Diana Picus22274932016-11-11 08:27:37 +00001//===- ARMInstructionSelector.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 ARM.
11/// \todo This should be generated by TableGen.
12//===----------------------------------------------------------------------===//
13
14#include "ARMInstructionSelector.h"
15#include "ARMRegisterBankInfo.h"
16#include "ARMSubtarget.h"
17#include "ARMTargetMachine.h"
Diana Picus812caee2016-12-16 12:54:46 +000018#include "llvm/CodeGen/MachineRegisterInfo.h"
Diana Picus22274932016-11-11 08:27:37 +000019#include "llvm/Support/Debug.h"
20
21#define DEBUG_TYPE "arm-isel"
22
23using namespace llvm;
24
25#ifndef LLVM_BUILD_GLOBAL_ISEL
26#error "You shouldn't build this"
27#endif
28
Diana Picus895c6aa2016-11-15 16:42:10 +000029ARMInstructionSelector::ARMInstructionSelector(const ARMSubtarget &STI,
Diana Picus22274932016-11-11 08:27:37 +000030 const ARMRegisterBankInfo &RBI)
Diana Picus895c6aa2016-11-15 16:42:10 +000031 : InstructionSelector(), TII(*STI.getInstrInfo()),
Diana Picus812caee2016-12-16 12:54:46 +000032 TRI(*STI.getRegisterInfo()), RBI(RBI) {}
Diana Picus22274932016-11-11 08:27:37 +000033
Diana Picus812caee2016-12-16 12:54:46 +000034static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
35 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
36 const RegisterBankInfo &RBI) {
37 unsigned DstReg = I.getOperand(0).getReg();
38 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
39 return true;
40
41 const RegisterBank *RegBank = RBI.getRegBank(DstReg, MRI, TRI);
Benjamin Kramer24bf8682016-12-16 13:13:03 +000042 (void)RegBank;
Diana Picus812caee2016-12-16 12:54:46 +000043 assert(RegBank && "Can't get reg bank for virtual register");
44
Diana Picus36aa09f2016-12-19 14:07:50 +000045 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
46 unsigned SrcReg = I.getOperand(1).getReg();
47 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
48 (void)SrcSize;
49 assert((DstSize == SrcSize ||
50 // Copies are a means to setup initial types, the number of
51 // bits may not exactly match.
52 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
53 DstSize <= SrcSize)) &&
Benjamin Kramer24bf8682016-12-16 13:13:03 +000054 "Copy with different width?!");
Diana Picus812caee2016-12-16 12:54:46 +000055
56 assert(RegBank->getID() == ARM::GPRRegBankID && "Unsupported reg bank");
57 const TargetRegisterClass *RC = &ARM::GPRRegClass;
58
59 // No need to constrain SrcReg. It will get constrained when
60 // we hit another of its uses or its defs.
61 // Copies do not have constraints.
62 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
63 DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
64 << " operand\n");
65 return false;
66 }
67 return true;
68}
69
70bool ARMInstructionSelector::select(MachineInstr &I) const {
71 assert(I.getParent() && "Instruction should be in a basic block!");
72 assert(I.getParent()->getParent() && "Instruction should be in a function!");
73
74 auto &MBB = *I.getParent();
75 auto &MF = *MBB.getParent();
76 auto &MRI = MF.getRegInfo();
77
78 if (!isPreISelGenericOpcode(I.getOpcode())) {
79 if (I.isCopy())
80 return selectCopy(I, TII, MRI, TRI, RBI);
81
82 return true;
83 }
84
Diana Picus519807f2016-12-19 11:26:31 +000085 MachineInstrBuilder MIB{MF, I};
86
87 using namespace TargetOpcode;
88 switch (I.getOpcode()) {
89 case G_ADD:
Diana Picus812caee2016-12-16 12:54:46 +000090 I.setDesc(TII.get(ARM::ADDrr));
Diana Picus519807f2016-12-19 11:26:31 +000091 AddDefaultCC(AddDefaultPred(MIB));
92 break;
93 case G_FRAME_INDEX:
94 // Add 0 to the given frame index and hope it will eventually be folded into
95 // the user(s).
96 I.setDesc(TII.get(ARM::ADDri));
97 AddDefaultCC(AddDefaultPred(MIB.addImm(0)));
98 break;
99 case G_LOAD:
100 I.setDesc(TII.get(ARM::LDRi12));
101 AddDefaultPred(MIB.addImm(0));
102 break;
103 default:
104 return false;
Diana Picus812caee2016-12-16 12:54:46 +0000105 }
106
Diana Picus519807f2016-12-19 11:26:31 +0000107 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
Diana Picus22274932016-11-11 08:27:37 +0000108}