| Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 1 | //===- ARMLegalizerInfo.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 Machinelegalizer class for ARM. |
| 11 | /// \todo This should be generated by TableGen. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "ARMLegalizerInfo.h" |
| Diana Picus | 7cab078 | 2017-02-17 11:25:17 +0000 | [diff] [blame] | 15 | #include "ARMSubtarget.h" |
| Diana Picus | f53865d | 2017-04-24 09:12:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h" |
| 17 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
| Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/ValueTypes.h" |
| 19 | #include "llvm/IR/DerivedTypes.h" |
| 20 | #include "llvm/IR/Type.h" |
| 21 | #include "llvm/Target/TargetOpcodes.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | #ifndef LLVM_BUILD_GLOBAL_ISEL |
| 26 | #error "You shouldn't build this" |
| 27 | #endif |
| 28 | |
| Diana Picus | 7cab078 | 2017-02-17 11:25:17 +0000 | [diff] [blame] | 29 | ARMLegalizerInfo::ARMLegalizerInfo(const ARMSubtarget &ST) { |
| Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 30 | using namespace TargetOpcode; |
| Diana Picus | 5a72445 | 2016-12-19 14:07:56 +0000 | [diff] [blame] | 31 | |
| Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 32 | const LLT p0 = LLT::pointer(0, 32); |
| Diana Picus | 5a72445 | 2016-12-19 14:07:56 +0000 | [diff] [blame] | 33 | |
| Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 34 | const LLT s1 = LLT::scalar(1); |
| Diana Picus | 5a72445 | 2016-12-19 14:07:56 +0000 | [diff] [blame] | 35 | const LLT s8 = LLT::scalar(8); |
| 36 | const LLT s16 = LLT::scalar(16); |
| Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 37 | const LLT s32 = LLT::scalar(32); |
| Diana Picus | 21c3d8e | 2017-02-16 09:09:49 +0000 | [diff] [blame] | 38 | const LLT s64 = LLT::scalar(64); |
| Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 39 | |
| Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 40 | setAction({G_FRAME_INDEX, p0}, Legal); |
| 41 | |
| Diana Picus | a2b632a | 2017-02-24 11:28:24 +0000 | [diff] [blame] | 42 | for (unsigned Op : {G_LOAD, G_STORE}) { |
| 43 | for (auto Ty : {s1, s8, s16, s32, p0}) |
| 44 | setAction({Op, Ty}, Legal); |
| 45 | setAction({Op, 1, p0}, Legal); |
| 46 | } |
| Diana Picus | 519807f | 2016-12-19 11:26:31 +0000 | [diff] [blame] | 47 | |
| Diana Picus | 0196427 | 2017-06-07 11:57:30 +0000 | [diff] [blame^] | 48 | for (unsigned Op : {G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR}) { |
| Diana Picus | 9cfbc6d | 2017-05-11 09:45:57 +0000 | [diff] [blame] | 49 | for (auto Ty : {s1, s8, s16}) |
| 50 | setAction({Op, Ty}, WidenScalar); |
| 51 | setAction({Op, s32}, Legal); |
| 52 | } |
| Diana Picus | 812caee | 2016-12-16 12:54:46 +0000 | [diff] [blame] | 53 | |
| Diana Picus | b70e88b | 2017-04-24 08:20:05 +0000 | [diff] [blame] | 54 | for (unsigned Op : {G_SDIV, G_UDIV}) { |
| Diana Picus | f53865d | 2017-04-24 09:12:19 +0000 | [diff] [blame] | 55 | for (auto Ty : {s8, s16}) |
| 56 | // FIXME: We need WidenScalar here, but in the case of targets with |
| 57 | // software division we'll also need Libcall afterwards. Treat as Custom |
| 58 | // until we have better support for chaining legalization actions. |
| 59 | setAction({Op, Ty}, Custom); |
| Diana Picus | b70e88b | 2017-04-24 08:20:05 +0000 | [diff] [blame] | 60 | if (ST.hasDivideInARMMode()) |
| 61 | setAction({Op, s32}, Legal); |
| 62 | else |
| 63 | setAction({Op, s32}, Libcall); |
| 64 | } |
| 65 | |
| Quentin Colombet | 89dbea0 | 2017-01-27 01:30:46 +0000 | [diff] [blame] | 66 | for (unsigned Op : {G_SEXT, G_ZEXT}) { |
| Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 67 | setAction({Op, s32}, Legal); |
| Diana Picus | d83df5d | 2017-01-25 08:47:40 +0000 | [diff] [blame] | 68 | for (auto Ty : {s1, s8, s16}) |
| Diana Picus | 8b6c6be | 2017-01-25 08:10:40 +0000 | [diff] [blame] | 69 | setAction({Op, 1, Ty}, Legal); |
| 70 | } |
| 71 | |
| Diana Picus | 8598b17 | 2017-02-28 09:02:42 +0000 | [diff] [blame] | 72 | setAction({G_GEP, p0}, Legal); |
| 73 | setAction({G_GEP, 1, s32}, Legal); |
| 74 | |
| Diana Picus | e6beac6 | 2017-02-28 11:33:46 +0000 | [diff] [blame] | 75 | setAction({G_CONSTANT, s32}, Legal); |
| 76 | |
| Diana Picus | a5bab61 | 2017-04-07 09:41:39 +0000 | [diff] [blame] | 77 | if (!ST.useSoftFloat() && ST.hasVFP2()) { |
| Diana Picus | 7cab078 | 2017-02-17 11:25:17 +0000 | [diff] [blame] | 78 | setAction({G_FADD, s32}, Legal); |
| 79 | setAction({G_FADD, s64}, Legal); |
| 80 | |
| 81 | setAction({G_LOAD, s64}, Legal); |
| Diana Picus | a2b632a | 2017-02-24 11:28:24 +0000 | [diff] [blame] | 82 | setAction({G_STORE, s64}, Legal); |
| Diana Picus | 1314a28 | 2017-04-11 10:52:34 +0000 | [diff] [blame] | 83 | } else { |
| 84 | for (auto Ty : {s32, s64}) |
| 85 | setAction({G_FADD, Ty}, Libcall); |
| Diana Picus | 7cab078 | 2017-02-17 11:25:17 +0000 | [diff] [blame] | 86 | } |
| Diana Picus | 4fa83c0 | 2017-02-08 13:23:04 +0000 | [diff] [blame] | 87 | |
| Diana Picus | 3ff82c8 | 2017-04-10 09:27:39 +0000 | [diff] [blame] | 88 | for (unsigned Op : {G_FREM, G_FPOW}) |
| 89 | for (auto Ty : {s32, s64}) |
| 90 | setAction({Op, Ty}, Libcall); |
| Diana Picus | a5bab61 | 2017-04-07 09:41:39 +0000 | [diff] [blame] | 91 | |
| Diana Picus | 2227493 | 2016-11-11 08:27:37 +0000 | [diff] [blame] | 92 | computeTables(); |
| 93 | } |
| Diana Picus | f53865d | 2017-04-24 09:12:19 +0000 | [diff] [blame] | 94 | |
| 95 | bool ARMLegalizerInfo::legalizeCustom(MachineInstr &MI, |
| 96 | MachineRegisterInfo &MRI, |
| 97 | MachineIRBuilder &MIRBuilder) const { |
| 98 | using namespace TargetOpcode; |
| 99 | |
| 100 | switch (MI.getOpcode()) { |
| 101 | default: |
| 102 | return false; |
| 103 | case G_SDIV: |
| 104 | case G_UDIV: { |
| 105 | LLT Ty = MRI.getType(MI.getOperand(0).getReg()); |
| 106 | if (Ty != LLT::scalar(16) && Ty != LLT::scalar(8)) |
| 107 | return false; |
| 108 | |
| 109 | // We need to widen to 32 bits and then maybe, if the target requires, |
| 110 | // transform into a libcall. |
| 111 | LegalizerHelper Helper(MIRBuilder.getMF()); |
| 112 | |
| 113 | MachineInstr *NewMI = nullptr; |
| 114 | Helper.MIRBuilder.recordInsertions([&](MachineInstr *MI) { |
| 115 | // Store the new, 32-bit div instruction. |
| 116 | if (MI->getOpcode() == G_SDIV || MI->getOpcode() == G_UDIV) |
| 117 | NewMI = MI; |
| 118 | }); |
| 119 | |
| 120 | auto Result = Helper.widenScalar(MI, 0, LLT::scalar(32)); |
| 121 | Helper.MIRBuilder.stopRecordingInsertions(); |
| 122 | if (Result == LegalizerHelper::UnableToLegalize) { |
| 123 | return false; |
| 124 | } |
| 125 | assert(NewMI && "Couldn't find widened instruction"); |
| 126 | assert((NewMI->getOpcode() == G_SDIV || NewMI->getOpcode() == G_UDIV) && |
| 127 | "Unexpected widened instruction"); |
| 128 | assert(MRI.getType(NewMI->getOperand(0).getReg()).getSizeInBits() == 32 && |
| 129 | "Unexpected type for the widened instruction"); |
| 130 | |
| 131 | Result = Helper.legalizeInstrStep(*NewMI); |
| 132 | if (Result == LegalizerHelper::UnableToLegalize) { |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | } |
| 138 | } |