Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 1 | //===- 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 Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | |
| 21 | #define DEBUG_TYPE "arm-isel" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | #ifndef LLVM_BUILD_GLOBAL_ISEL |
| 26 | #error "You shouldn't build this" |
| 27 | #endif |
| 28 | |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 29 | ARMInstructionSelector::ARMInstructionSelector(const ARMSubtarget &STI, |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 30 | const ARMRegisterBankInfo &RBI) |
Diana Picus | 895c6aa | 2016-11-15 16:42:10 +0000 | [diff] [blame] | 31 | : InstructionSelector(), TII(*STI.getInstrInfo()), |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 32 | TRI(*STI.getRegisterInfo()), RBI(RBI) {} |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 33 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 34 | static 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 Kramer | 24bf868 | 2016-12-16 13:13:03 +0000 | [diff] [blame] | 42 | (void)RegBank; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 43 | assert(RegBank && "Can't get reg bank for virtual register"); |
| 44 | |
Diana Picus | 36aa09f | 2016-12-19 14:07:50 +0000 | [diff] [blame] | 45 | const unsigned DstSize = MRI.getType(DstReg).getSizeInBits(); |
Daniel Jasper | 24218d5 | 2016-12-19 14:24:22 +0000 | [diff] [blame] | 46 | (void)DstSize; |
Diana Picus | 36aa09f | 2016-12-19 14:07:50 +0000 | [diff] [blame] | 47 | unsigned SrcReg = I.getOperand(1).getReg(); |
| 48 | const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI); |
| 49 | (void)SrcSize; |
| 50 | assert((DstSize == SrcSize || |
| 51 | // Copies are a means to setup initial types, the number of |
| 52 | // bits may not exactly match. |
| 53 | (TargetRegisterInfo::isPhysicalRegister(SrcReg) && |
| 54 | DstSize <= SrcSize)) && |
Benjamin Kramer | 24bf868 | 2016-12-16 13:13:03 +0000 | [diff] [blame] | 55 | "Copy with different width?!"); |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 56 | |
| 57 | assert(RegBank->getID() == ARM::GPRRegBankID && "Unsupported reg bank"); |
| 58 | const TargetRegisterClass *RC = &ARM::GPRRegClass; |
| 59 | |
| 60 | // No need to constrain SrcReg. It will get constrained when |
| 61 | // we hit another of its uses or its defs. |
| 62 | // Copies do not have constraints. |
| 63 | if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) { |
| 64 | DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode()) |
| 65 | << " operand\n"); |
| 66 | return false; |
| 67 | } |
| 68 | return true; |
| 69 | } |
| 70 | |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 71 | /// Select the opcode for simple extensions (that translate to a single SXT/UXT |
| 72 | /// instruction). Extension operations more complicated than that should not |
| 73 | /// invoke this. |
| 74 | static unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) { |
| 75 | using namespace TargetOpcode; |
| 76 | |
| 77 | assert((Size == 8 || Size == 16) && "Unsupported size"); |
| 78 | |
| 79 | if (Opc == G_SEXT) |
| 80 | return Size == 8 ? ARM::SXTB : ARM::SXTH; |
| 81 | |
| 82 | if (Opc == G_ZEXT) |
| 83 | return Size == 8 ? ARM::UXTB : ARM::UXTH; |
| 84 | |
| 85 | llvm_unreachable("Unsupported opcode"); |
| 86 | } |
| 87 | |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 88 | bool ARMInstructionSelector::select(MachineInstr &I) const { |
| 89 | assert(I.getParent() && "Instruction should be in a basic block!"); |
| 90 | assert(I.getParent()->getParent() && "Instruction should be in a function!"); |
| 91 | |
| 92 | auto &MBB = *I.getParent(); |
| 93 | auto &MF = *MBB.getParent(); |
| 94 | auto &MRI = MF.getRegInfo(); |
| 95 | |
| 96 | if (!isPreISelGenericOpcode(I.getOpcode())) { |
| 97 | if (I.isCopy()) |
| 98 | return selectCopy(I, TII, MRI, TRI, RBI); |
| 99 | |
| 100 | return true; |
| 101 | } |
| 102 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 103 | MachineInstrBuilder MIB{MF, I}; |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 104 | bool isSExt = false; |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 105 | |
| 106 | using namespace TargetOpcode; |
| 107 | switch (I.getOpcode()) { |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 108 | case G_SEXT: |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 109 | isSExt = true; |
| 110 | LLVM_FALLTHROUGH; |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 111 | case G_ZEXT: { |
| 112 | LLT DstTy = MRI.getType(I.getOperand(0).getReg()); |
| 113 | // FIXME: Smaller destination sizes coming soon! |
| 114 | if (DstTy.getSizeInBits() != 32) { |
| 115 | DEBUG(dbgs() << "Unsupported destination size for extension"); |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | LLT SrcTy = MRI.getType(I.getOperand(1).getReg()); |
| 120 | unsigned SrcSize = SrcTy.getSizeInBits(); |
| 121 | switch (SrcSize) { |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 122 | case 1: { |
| 123 | // ZExt boils down to & 0x1; for SExt we also subtract that from 0 |
| 124 | I.setDesc(TII.get(ARM::ANDri)); |
| 125 | MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp()); |
| 126 | |
| 127 | if (isSExt) { |
| 128 | unsigned SExtResult = I.getOperand(0).getReg(); |
| 129 | |
| 130 | // Use a new virtual register for the result of the AND |
| 131 | unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass); |
| 132 | I.getOperand(0).setReg(AndResult); |
| 133 | |
| 134 | auto InsertBefore = std::next(I.getIterator()); |
Martin Bohme | 8396e14 | 2017-01-25 14:28:19 +0000 | [diff] [blame^] | 135 | auto SubI = |
Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 136 | BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::RSBri)) |
| 137 | .addDef(SExtResult) |
| 138 | .addUse(AndResult) |
| 139 | .addImm(0) |
| 140 | .add(predOps(ARMCC::AL)) |
| 141 | .add(condCodeOp()); |
| 142 | if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI)) |
| 143 | return false; |
| 144 | } |
| 145 | break; |
| 146 | } |
Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 147 | case 8: |
| 148 | case 16: { |
| 149 | unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize); |
| 150 | I.setDesc(TII.get(NewOpc)); |
| 151 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
| 152 | break; |
| 153 | } |
| 154 | default: |
| 155 | DEBUG(dbgs() << "Unsupported source size for extension"); |
| 156 | return false; |
| 157 | } |
| 158 | break; |
| 159 | } |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 160 | case G_ADD: |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 161 | I.setDesc(TII.get(ARM::ADDrr)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 162 | MIB.add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 163 | break; |
| 164 | case G_FRAME_INDEX: |
| 165 | // Add 0 to the given frame index and hope it will eventually be folded into |
| 166 | // the user(s). |
| 167 | I.setDesc(TII.get(ARM::ADDri)); |
Diana Picus | 8a73f55 | 2017-01-13 10:18:01 +0000 | [diff] [blame] | 168 | MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp()); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 169 | break; |
| 170 | case G_LOAD: |
| 171 | I.setDesc(TII.get(ARM::LDRi12)); |
Diana Picus | 4f8c3e1 | 2017-01-13 09:37:56 +0000 | [diff] [blame] | 172 | MIB.addImm(0).add(predOps(ARMCC::AL)); |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 173 | break; |
| 174 | default: |
| 175 | return false; |
Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 178 | return constrainSelectedInstRegOperands(I, TII, TRI, RBI); |
Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 179 | } |