Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 1 | //===-- MipsastISel.cpp - Mips FastISel implementation |
| 2 | //---------------------===// |
| 3 | |
| 4 | #include "llvm/CodeGen/FunctionLoweringInfo.h" |
| 5 | #include "llvm/CodeGen/FastISel.h" |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 6 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 7 | #include "llvm/IR/GlobalAlias.h" |
| 8 | #include "llvm/IR/GlobalVariable.h" |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 9 | #include "llvm/Target/TargetInstrInfo.h" |
Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 10 | #include "llvm/Target/TargetLibraryInfo.h" |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 11 | #include "MipsRegisterInfo.h" |
Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 12 | #include "MipsISelLowering.h" |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 13 | #include "MipsMachineFunction.h" |
| 14 | #include "MipsSubtarget.h" |
Reed Kotler | 9fe25f3 | 2014-06-08 02:08:43 +0000 | [diff] [blame^] | 15 | #include "MipsTargetMachine.h" |
Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 21 | // All possible address modes. |
| 22 | typedef struct Address { |
| 23 | enum { RegBase, FrameIndexBase } BaseType; |
| 24 | |
| 25 | union { |
| 26 | unsigned Reg; |
| 27 | int FI; |
| 28 | } Base; |
| 29 | |
| 30 | int64_t Offset; |
| 31 | |
| 32 | // Innocuous defaults for our address. |
| 33 | Address() : BaseType(RegBase), Offset(0) { Base.Reg = 0; } |
| 34 | } Address; |
| 35 | |
Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 36 | class MipsFastISel final : public FastISel { |
| 37 | |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 38 | /// Subtarget - Keep a pointer to the MipsSubtarget around so that we can |
| 39 | /// make the right decision when generating code for different targets. |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 40 | Module &M; |
| 41 | const TargetMachine &TM; |
| 42 | const TargetInstrInfo &TII; |
| 43 | const TargetLowering &TLI; |
Reed Kotler | 9fe25f3 | 2014-06-08 02:08:43 +0000 | [diff] [blame^] | 44 | const MipsSubtarget &Subtarget; |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 45 | MipsFunctionInfo *MFI; |
| 46 | |
| 47 | // Convenience variables to avoid some queries. |
| 48 | LLVMContext *Context; |
| 49 | |
| 50 | bool TargetSupported; |
| 51 | |
Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 52 | public: |
| 53 | explicit MipsFastISel(FunctionLoweringInfo &funcInfo, |
| 54 | const TargetLibraryInfo *libInfo) |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 55 | : FastISel(funcInfo, libInfo), |
| 56 | M(const_cast<Module &>(*funcInfo.Fn->getParent())), |
| 57 | TM(funcInfo.MF->getTarget()), TII(*TM.getInstrInfo()), |
Reed Kotler | 9fe25f3 | 2014-06-08 02:08:43 +0000 | [diff] [blame^] | 58 | TLI(*TM.getTargetLowering()), |
| 59 | Subtarget(TM.getSubtarget<MipsSubtarget>()) { |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 60 | MFI = funcInfo.MF->getInfo<MipsFunctionInfo>(); |
| 61 | Context = &funcInfo.Fn->getContext(); |
Reed Kotler | 9fe25f3 | 2014-06-08 02:08:43 +0000 | [diff] [blame^] | 62 | TargetSupported = ((Subtarget.getRelocationModel() == Reloc::PIC_) && |
| 63 | (Subtarget.hasMips32r2() && (Subtarget.isABI_O32()))); |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | bool TargetSelectInstruction(const Instruction *I) override; |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 67 | unsigned TargetMaterializeConstant(const Constant *C) override; |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 68 | |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 69 | bool ComputeAddress(const Value *Obj, Address &Addr); |
| 70 | |
| 71 | private: |
| 72 | bool EmitStore(MVT VT, unsigned SrcReg, Address &Addr, |
| 73 | unsigned Alignment = 0); |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 74 | bool SelectRet(const Instruction *I); |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 75 | bool SelectStore(const Instruction *I); |
| 76 | |
| 77 | bool isTypeLegal(Type *Ty, MVT &VT); |
| 78 | bool isLoadTypeLegal(Type *Ty, MVT &VT); |
| 79 | |
| 80 | unsigned MaterializeFP(const ConstantFP *CFP, MVT VT); |
| 81 | unsigned MaterializeGV(const GlobalValue *GV, MVT VT); |
| 82 | unsigned MaterializeInt(const Constant *C, MVT VT); |
Reed Kotler | 6280d97 | 2014-05-15 21:54:15 +0000 | [diff] [blame] | 83 | unsigned Materialize32BitInt(int64_t Imm, const TargetRegisterClass *RC); |
Reed Kotler | 9fe25f3 | 2014-06-08 02:08:43 +0000 | [diff] [blame^] | 84 | |
| 85 | // for some reason, this default is not generated by tablegen |
| 86 | // so we explicitly generate it here. |
| 87 | // |
| 88 | unsigned FastEmitInst_riir(uint64_t inst, const TargetRegisterClass *RC, |
| 89 | unsigned Op0, bool Op0IsKill, uint64_t imm1, |
| 90 | uint64_t imm2, unsigned Op3, bool Op3IsKill) { |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | #include "MipsGenFastISel.inc" |
Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 95 | }; |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 96 | |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 97 | bool MipsFastISel::isTypeLegal(Type *Ty, MVT &VT) { |
| 98 | EVT evt = TLI.getValueType(Ty, true); |
| 99 | // Only handle simple types. |
| 100 | if (evt == MVT::Other || !evt.isSimple()) |
| 101 | return false; |
| 102 | VT = evt.getSimpleVT(); |
| 103 | |
| 104 | // Handle all legal types, i.e. a register that will directly hold this |
| 105 | // value. |
| 106 | return TLI.isTypeLegal(VT); |
| 107 | } |
| 108 | |
| 109 | bool MipsFastISel::isLoadTypeLegal(Type *Ty, MVT &VT) { |
| 110 | if (isTypeLegal(Ty, VT)) |
| 111 | return true; |
| 112 | // We will extend this in a later patch: |
| 113 | // If this is a type than can be sign or zero-extended to a basic operation |
| 114 | // go ahead and accept it now. |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | bool MipsFastISel::ComputeAddress(const Value *Obj, Address &Addr) { |
| 119 | // This construct looks a big awkward but it is how other ports handle this |
| 120 | // and as this function is more fully completed, these cases which |
| 121 | // return false will have additional code in them. |
| 122 | // |
| 123 | if (isa<Instruction>(Obj)) |
| 124 | return false; |
| 125 | else if (isa<ConstantExpr>(Obj)) |
| 126 | return false; |
| 127 | Addr.Base.Reg = getRegForValue(Obj); |
| 128 | return Addr.Base.Reg != 0; |
| 129 | } |
| 130 | |
| 131 | // Materialize a constant into a register, and return the register |
| 132 | // number (or zero if we failed to handle it). |
| 133 | unsigned MipsFastISel::TargetMaterializeConstant(const Constant *C) { |
| 134 | EVT CEVT = TLI.getValueType(C->getType(), true); |
| 135 | |
| 136 | // Only handle simple types. |
| 137 | if (!CEVT.isSimple()) |
| 138 | return 0; |
| 139 | MVT VT = CEVT.getSimpleVT(); |
| 140 | |
| 141 | if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C)) |
| 142 | return MaterializeFP(CFP, VT); |
| 143 | else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C)) |
| 144 | return MaterializeGV(GV, VT); |
| 145 | else if (isa<ConstantInt>(C)) |
| 146 | return MaterializeInt(C, VT); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | bool MipsFastISel::EmitStore(MVT VT, unsigned SrcReg, Address &Addr, |
| 152 | unsigned Alignment) { |
| 153 | // |
| 154 | // more cases will be handled here in following patches. |
| 155 | // |
| 156 | if (VT != MVT::i32) |
| 157 | return false; |
| 158 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::SW)) |
| 159 | .addReg(SrcReg) |
| 160 | .addReg(Addr.Base.Reg) |
| 161 | .addImm(Addr.Offset); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | bool MipsFastISel::SelectStore(const Instruction *I) { |
| 166 | Value *Op0 = I->getOperand(0); |
| 167 | unsigned SrcReg = 0; |
| 168 | |
| 169 | // Atomic stores need special handling. |
| 170 | if (cast<StoreInst>(I)->isAtomic()) |
| 171 | return false; |
| 172 | |
| 173 | // Verify we have a legal type before going any further. |
| 174 | MVT VT; |
| 175 | if (!isLoadTypeLegal(I->getOperand(0)->getType(), VT)) |
| 176 | return false; |
| 177 | |
| 178 | // Get the value to be stored into a register. |
| 179 | SrcReg = getRegForValue(Op0); |
| 180 | if (SrcReg == 0) |
| 181 | return false; |
| 182 | |
| 183 | // See if we can handle this address. |
| 184 | Address Addr; |
| 185 | if (!ComputeAddress(I->getOperand(1), Addr)) |
| 186 | return false; |
| 187 | |
| 188 | if (!EmitStore(VT, SrcReg, Addr, cast<StoreInst>(I)->getAlignment())) |
| 189 | return false; |
| 190 | return true; |
| 191 | } |
| 192 | |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 193 | bool MipsFastISel::SelectRet(const Instruction *I) { |
| 194 | const ReturnInst *Ret = cast<ReturnInst>(I); |
| 195 | |
| 196 | if (!FuncInfo.CanLowerReturn) |
| 197 | return false; |
| 198 | if (Ret->getNumOperands() > 0) { |
| 199 | return false; |
| 200 | } |
| 201 | unsigned RetOpc = Mips::RetRA; |
| 202 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(RetOpc)); |
| 203 | return true; |
| 204 | } |
| 205 | |
| 206 | bool MipsFastISel::TargetSelectInstruction(const Instruction *I) { |
| 207 | if (!TargetSupported) |
| 208 | return false; |
| 209 | switch (I->getOpcode()) { |
| 210 | default: |
| 211 | break; |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 212 | case Instruction::Store: |
| 213 | return SelectStore(I); |
Reed Kotler | 67077b3 | 2014-04-29 17:57:50 +0000 | [diff] [blame] | 214 | case Instruction::Ret: |
| 215 | return SelectRet(I); |
| 216 | } |
| 217 | return false; |
| 218 | } |
Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 221 | unsigned MipsFastISel::MaterializeFP(const ConstantFP *CFP, MVT VT) { |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | unsigned MipsFastISel::MaterializeGV(const GlobalValue *GV, MVT VT) { |
| 226 | // For now 32-bit only. |
| 227 | if (VT != MVT::i32) |
| 228 | return 0; |
| 229 | const TargetRegisterClass *RC = &Mips::GPR32RegClass; |
| 230 | unsigned DestReg = createResultReg(RC); |
| 231 | const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV); |
| 232 | bool IsThreadLocal = GVar && GVar->isThreadLocal(); |
| 233 | // TLS not supported at this time. |
| 234 | if (IsThreadLocal) |
| 235 | return 0; |
| 236 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LW), DestReg) |
| 237 | .addReg(MFI->getGlobalBaseReg()) |
| 238 | .addGlobalAddress(GV, 0, MipsII::MO_GOT); |
| 239 | return DestReg; |
| 240 | } |
| 241 | unsigned MipsFastISel::MaterializeInt(const Constant *C, MVT VT) { |
Reed Kotler | 6280d97 | 2014-05-15 21:54:15 +0000 | [diff] [blame] | 242 | if (VT != MVT::i32 && VT != MVT::i16 && VT != MVT::i8 && VT != MVT::i1) |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 243 | return 0; |
| 244 | const TargetRegisterClass *RC = &Mips::GPR32RegClass; |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 245 | const ConstantInt *CI = cast<ConstantInt>(C); |
Reed Kotler | 6280d97 | 2014-05-15 21:54:15 +0000 | [diff] [blame] | 246 | int64_t Imm; |
| 247 | if (CI->isNegative()) |
| 248 | Imm = CI->getSExtValue(); |
| 249 | else |
| 250 | Imm = CI->getZExtValue(); |
| 251 | return Materialize32BitInt(Imm, RC); |
| 252 | } |
| 253 | |
| 254 | unsigned MipsFastISel::Materialize32BitInt(int64_t Imm, |
| 255 | const TargetRegisterClass *RC) { |
| 256 | unsigned ResultReg = createResultReg(RC); |
| 257 | |
| 258 | if (isInt<16>(Imm)) { |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 259 | unsigned Opc = Mips::ADDiu; |
Reed Kotler | 6280d97 | 2014-05-15 21:54:15 +0000 | [diff] [blame] | 260 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 261 | .addReg(Mips::ZERO) |
Reed Kotler | 6280d97 | 2014-05-15 21:54:15 +0000 | [diff] [blame] | 262 | .addImm(Imm); |
| 263 | return ResultReg; |
| 264 | } else if (isUInt<16>(Imm)) { |
| 265 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::ORi), |
| 266 | ResultReg) |
| 267 | .addReg(Mips::ZERO) |
| 268 | .addImm(Imm); |
| 269 | return ResultReg; |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 270 | } |
Reed Kotler | 6280d97 | 2014-05-15 21:54:15 +0000 | [diff] [blame] | 271 | unsigned Lo = Imm & 0xFFFF; |
| 272 | unsigned Hi = (Imm >> 16) & 0xFFFF; |
| 273 | if (Lo) { |
| 274 | // Both Lo and Hi have nonzero bits. |
| 275 | unsigned TmpReg = createResultReg(RC); |
| 276 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LUi), |
| 277 | TmpReg).addImm(Hi); |
| 278 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::ORi), |
| 279 | ResultReg) |
| 280 | .addReg(TmpReg) |
| 281 | .addImm(Lo); |
| 282 | |
| 283 | } else { |
| 284 | BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Mips::LUi), |
| 285 | ResultReg).addImm(Hi); |
| 286 | } |
| 287 | return ResultReg; |
Reed Kotler | bab3f23 | 2014-05-01 20:39:21 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Reed Kotler | 720c5ca | 2014-04-17 22:15:34 +0000 | [diff] [blame] | 290 | namespace llvm { |
| 291 | FastISel *Mips::createFastISel(FunctionLoweringInfo &funcInfo, |
| 292 | const TargetLibraryInfo *libInfo) { |
| 293 | return new MipsFastISel(funcInfo, libInfo); |
| 294 | } |
| 295 | } |