FastISel.cpp revision b71fea248fd7cf9ab2c5737997a3dc5682948dc4
1b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman///===-- FastISel.cpp - Implementation of the FastISel class --------------===//
2b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
3b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//                     The LLVM Compiler Infrastructure
4b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
5b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman// This file is distributed under the University of Illinois Open Source
6b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman// License. See LICENSE.TXT for details.
7b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
8b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//===----------------------------------------------------------------------===//
9b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
10b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman// This file contains the implementation of the FastISel class.
11b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//
12b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//===----------------------------------------------------------------------===//
13b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
146f2766d59744bb3d48867f3151643eac7111e773Dan Gohman#include "llvm/Instructions.h"
15b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/FastISel.h"
16b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/MachineInstrBuilder.h"
17b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/MachineRegisterInfo.h"
1883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng#include "llvm/Target/TargetData.h"
19b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/Target/TargetInstrInfo.h"
2083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng#include "llvm/Target/TargetLowering.h"
21bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman#include "llvm/Target/TargetMachine.h"
22b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanusing namespace llvm;
23b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
24bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// SelectBinaryOp - Select and emit code for a binary operator instruction,
25bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// which has an opcode which directly corresponds to the given ISD opcode.
26bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman///
27bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohmanbool FastISel::SelectBinaryOp(Instruction *I, ISD::NodeType ISDOpcode,
28bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman                              DenseMap<const Value*, unsigned> &ValueMap) {
29d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
30d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (VT == MVT::Other || !VT.isSimple())
31d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    // Unhandled type. Halt "fast" selection and bail.
32d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    return false;
33b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // We only handle legal types. For example, on x86-32 the instruction
34b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // selector contains all of the 64-bit instructions from x86-64,
35b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // under the assumption that i64 won't be used if the target doesn't
36b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // support it.
37b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  if (!TLI.isTypeLegal(VT))
38b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman    return false;
39d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
40bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  unsigned Op0 = ValueMap[I->getOperand(0)];
41d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (Op0 == 0)
42a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman    // Unhandled operand. Halt "fast" selection and bail.
43a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman    return false;
44a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman
45d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  // Check if the second operand is a constant and handle it appropriately.
46d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
47d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    unsigned ResultReg = FastEmit_ri_(VT.getSimpleVT(), ISDOpcode, Op0,
48d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                      CI->getZExtValue(), VT.getSimpleVT());
49d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    if (ResultReg == 0)
50d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman      // Target-specific code wasn't able to find a machine opcode for
51d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman      // the given ISD opcode and type. Halt "fast" selection and bail.
52d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman      return false;
53d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
54d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    // We successfully emitted code for the given LLVM Instruction.
55d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    ValueMap[I] = ResultReg;
56d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    return true;
57d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  }
58d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
59d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned Op1 = ValueMap[I->getOperand(1)];
60d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (Op1 == 0)
61d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    // Unhandled operand. Halt "fast" selection and bail.
62bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    return false;
63bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
640f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson  unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
650f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                                   ISDOpcode, Op0, Op1);
66bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  if (ResultReg == 0)
67bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // Target-specific code wasn't able to find a machine opcode for
68bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // the given ISD opcode and type. Halt "fast" selection and bail.
69bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    return false;
70bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
718014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman  // We successfully emitted code for the given LLVM Instruction.
72bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  ValueMap[I] = ResultReg;
73bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  return true;
74bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman}
75bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
76bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohmanbool FastISel::SelectGetElementPtr(Instruction *I,
77bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman                                   DenseMap<const Value*, unsigned> &ValueMap) {
7883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  unsigned N = ValueMap[I->getOperand(0)];
7983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (N == 0)
8083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    // Unhandled operand. Halt "fast" selection and bail.
8183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    return false;
8283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
8383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  const Type *Ty = I->getOperand(0)->getType();
847a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman  MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
8583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
8683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng       OI != E; ++OI) {
8783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    Value *Idx = *OI;
8883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
8983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
9083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (Field) {
9183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // N = N + Offset
9283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
9383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // FIXME: This can be optimized by combining the add with a
9483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // subsequent one.
957a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman        N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
9683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (N == 0)
9783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          // Unhandled operand. Halt "fast" selection and bail.
9883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          return false;
9983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      }
10083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      Ty = StTy->getElementType(Field);
10183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    } else {
10283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      Ty = cast<SequentialType>(Ty)->getElementType();
10383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
10483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // If this is a constant subscript, handle it quickly.
10583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
10683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (CI->getZExtValue() == 0) continue;
10783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        uint64_t Offs =
10883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
1097a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman        N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
11083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (N == 0)
11183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          // Unhandled operand. Halt "fast" selection and bail.
11283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          return false;
11383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        continue;
11483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      }
11583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
11683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // N = N + Idx * ElementSize;
11783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      uint64_t ElementSize = TD.getABITypeSize(Ty);
11883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      unsigned IdxN = ValueMap[Idx];
11983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxN == 0)
12083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
12183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
12283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
12383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // If the index is smaller or larger than intptr_t, truncate or extend
12483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // it.
1252076aa800e78a2e196eac47cc8413a074a761d8dEvan Cheng      MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
12683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxVT.bitsLT(VT))
1270f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson        IdxN = FastEmit_r(VT, VT, ISD::SIGN_EXTEND, IdxN);
12883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      else if (IdxVT.bitsGT(VT))
1290f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson        IdxN = FastEmit_r(VT, VT, ISD::TRUNCATE, IdxN);
13083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxN == 0)
13183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
13283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
13383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
134f93cf79505f07cb97597fbc5955462ad7670ca5cDan Gohman      if (ElementSize != 1)
135f93cf79505f07cb97597fbc5955462ad7670ca5cDan Gohman        IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
13683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxN == 0)
13783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
13883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
1390f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson      N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
14083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (N == 0)
14183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
14283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
14383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    }
14483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  }
14583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
14683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  // We successfully emitted code for the given LLVM Instruction.
14783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  ValueMap[I] = N;
14883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return true;
149bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman}
150bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
151b0cf29c5cfff797284b3660dc233e135feb65d9aDan GohmanBasicBlock::iterator
152b7864a9e23be526f4f8cde529a57c8943c61c0c7Dan GohmanFastISel::SelectInstructions(BasicBlock::iterator Begin,
153b7864a9e23be526f4f8cde529a57c8943c61c0c7Dan Gohman                             BasicBlock::iterator End,
154bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman                             DenseMap<const Value*, unsigned> &ValueMap,
1556ecf50908c78aae0feff1c378fbb75dcf013ed21Dan Gohman                             DenseMap<const BasicBlock*,
1563c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman                                      MachineBasicBlock *> &MBBMap,
157bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman                             MachineBasicBlock *mbb) {
158bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  MBB = mbb;
159b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  BasicBlock::iterator I = Begin;
160b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
161b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  for (; I != End; ++I) {
162b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman    switch (I->getOpcode()) {
1638014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    case Instruction::Add: {
1648014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
1658014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
1668014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    }
1678014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    case Instruction::Sub: {
1688014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
1698014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
1708014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    }
1718014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    case Instruction::Mul: {
1728014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
1738014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
1748014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    }
175bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::SDiv:
176bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
177bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::UDiv:
178bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
179bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::FDiv:
180bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
181bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::SRem:
182bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
183bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::URem:
184bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
185bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::FRem:
186bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
187bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::Shl:
188bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
189bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::LShr:
190bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
191bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::AShr:
192bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
193bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::And:
194bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
195bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::Or:
196bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
197bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::Xor:
198bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
199bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
200bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::GetElementPtr:
201bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectGetElementPtr(I, ValueMap)) return I;
202b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman      break;
203bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
2046f2766d59744bb3d48867f3151643eac7111e773Dan Gohman    case Instruction::Br: {
2056f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      BranchInst *BI = cast<BranchInst>(I);
2066f2766d59744bb3d48867f3151643eac7111e773Dan Gohman
207e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman      if (BI->isUnconditional()) {
2083c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman        MachineFunction::iterator NextMBB =
209e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman           next(MachineFunction::iterator(MBB));
2103c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman        BasicBlock *LLVMSucc = BI->getSuccessor(0);
2113c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman        MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
2123c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman
2133c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman        if (NextMBB != MF.end() && MSucc == NextMBB) {
2143c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman          // The unconditional fall-through case, which needs no instructions.
2153c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman        } else {
2163c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman          // The unconditional branch case.
2173c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman          TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
218e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman        }
2193c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman        MBB->addSuccessor(MSucc);
2203c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman        break;
2216f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      }
2226f2766d59744bb3d48867f3151643eac7111e773Dan Gohman
2233c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman      // Conditional branches are not handed yet.
2243c8f36fd03d046ee2a8700c60b5a52887d9f07b9Dan Gohman      // Halt "fast" selection and bail.
2256f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      return I;
2266f2766d59744bb3d48867f3151643eac7111e773Dan Gohman    }
2273b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman
2283b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman    case Instruction::PHI:
2293b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman      // PHI nodes are already emitted.
2303b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman      break;
2316d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson
2326d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson    case Instruction::BitCast:
2336d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson      // BitCast consists of either an immediate to register move
2346d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson      // or a register to register move.
2356d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson      if (ConstantInt* CI = dyn_cast<ConstantInt>(I->getOperand(0))) {
2366d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson        if (I->getType()->isInteger()) {
2376d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson          MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/false);
23846aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson          unsigned result = FastEmit_i(VT.getSimpleVT(), VT.getSimpleVT(),
23946aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson                                       ISD::Constant,
24046aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson                                       CI->getZExtValue());
24146aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson          if (!result)
24246aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson            return I;
24346aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson
24446aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson          ValueMap[I] = result;
2456d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson          break;
2466d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson        } else
2476d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson          // TODO: Support vector and fp constants.
2486d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson          return I;
249d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson      } else if (!isa<Constant>(I->getOperand(0))) {
250d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson        // Bitcasts of non-constant values become reg-reg copies.
251d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson        MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
25246aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        MVT DstVT = MVT::getMVT(I->getType());
253d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson
254d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson        if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
255d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson            DstVT == MVT::Other || !DstVT.isSimple() ||
256d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson            !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
257d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson          // Unhandled type. Halt "fast" selection and bail.
258d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson          return I;
259d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson
260d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson        unsigned Op0 = ValueMap[I->getOperand(0)];
261d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson        if (Op0 == 0)
262d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson          // Unhandled operand. Halt "fast" selection and bail.
263d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson          return false;
264d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson
26577a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson        // First, try to perform the bitcast by inserting a reg-reg copy.
26677a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson        unsigned ResultReg = 0;
26777a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson        if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
26877a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson          TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
26977a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson          TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
27077a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson          ResultReg = createResultReg(DstClass);
27177a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson
27277a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson          bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
27377a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson                                               Op0, DstClass, SrcClass);
27477a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson          if (!InsertedCopy)
27577a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson            ResultReg = 0;
27677a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson        }
27777a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson
27877a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson        // If the reg-reg copy failed, select a BIT_CONVERT opcode.
27977a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson        if (!ResultReg)
28077a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson          ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
28177a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson                                 ISD::BIT_CONVERT, Op0);
28277a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson
28377a218765ac85b2e0c6ce01bf8a7b07dfe80beadOwen Anderson        if (!ResultReg)
284940f83e772ca2007d62faffc83094bd7e8da6401Owen Anderson          return I;
285d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson
286940f83e772ca2007d62faffc83094bd7e8da6401Owen Anderson        ValueMap[I] = ResultReg;
287d894f1d274515db527962b6db2d9983827fdcbd6Owen Anderson        break;
2886d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson      } else
28946aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        // TODO: Casting a non-integral constant?
29046aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        return I;
29146aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson
29246aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson    case Instruction::FPToSI:
29346aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson      if (!isa<ConstantFP>(I->getOperand(0))) {
29446aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
29546aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        MVT DstVT = MVT::getMVT(I->getType());
29646aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson
29746aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
29846aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson            DstVT == MVT::Other || !DstVT.isSimple() ||
29946aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson            !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
30046aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson          // Unhandled type. Halt "fast" selection and bail.
30146aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson          return I;
30246aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson
30346aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        unsigned InputReg = ValueMap[I->getOperand(0)];
30446aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        if (!InputReg)
30546aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson          // Unhandled operand.  Halt "fast" selection and bail.
30646aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson          return I;
30746aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson
30846aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
30946aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson                                        DstVT.getSimpleVT(),
31046aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson                                        ISD::FP_TO_SINT,
31146aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson                                        InputReg);
31246aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        if (!ResultReg)
31346aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson          return I;
31446aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson
31546aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        ValueMap[I] = ResultReg;
31646aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        break;
31746aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson      } else
31846aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        // TODO: Materialize the FP constant and then convert,
31946aa2f5aab3d39e9cba840ecd8068a6531f8f8a9Owen Anderson        // or attempt constant folding.
3206d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson        return I;
3213b7753be2e899cf3a79835a8ff810e869035c87bDan Gohman
322a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson    case Instruction::SIToFP:
323a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson      if (!isa<ConstantInt>(I->getOperand(0))) {
324a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        MVT SrcVT = MVT::getMVT(I->getOperand(0)->getType());
325a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        MVT DstVT = MVT::getMVT(I->getType());
326a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson
327a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
328a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson            DstVT == MVT::Other || !DstVT.isSimple() ||
329a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson            !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
330a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson          // Unhandled type. Halt "fast" selection and bail.
331a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson          return I;
332a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson
333a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        unsigned InputReg = ValueMap[I->getOperand(0)];
334a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        if (!InputReg)
335a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson          // Unhandled operan.  Halt "fast" selection and bail.
336a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson          return I;
337a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson
338a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
339a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson                                        DstVT.getSimpleVT(),
340a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson                                        ISD::SINT_TO_FP,
341a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson                                        InputReg);
342a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        if (!ResultReg)
343a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson          return I;
344a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson
345a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        ValueMap[I] = ResultReg;
346a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        break;
347a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson      } else
348a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        // TODO: Materialize constant and convert to FP.
349a843b8d391de28ca0f532b22ab3e791e1e09c47bOwen Anderson        return I;
350b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman    default:
351b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman      // Unhandled instruction. Halt "fast" selection and bail.
352b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman      return I;
353b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman    }
354b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  }
355b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
356b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return I;
357b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
358b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
359bb466331e7e50d03497ce40ee344870236fd9c32Dan GohmanFastISel::FastISel(MachineFunction &mf)
36022bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman  : MF(mf),
36122bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    MRI(mf.getRegInfo()),
36222bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TM(mf.getTarget()),
36322bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TD(*TM.getTargetData()),
36422bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TII(*TM.getInstrInfo()),
36522bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TLI(*TM.getTargetLowering()) {
366bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman}
367bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman
368e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan GohmanFastISel::~FastISel() {}
369e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan Gohman
3700f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType, ISD::NodeType) {
371b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
372b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
373b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
3740f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
3750f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                              ISD::NodeType, unsigned /*Op0*/) {
376b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
377b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
378b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
3790f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
3800f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               ISD::NodeType, unsigned /*Op0*/,
3810f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               unsigned /*Op0*/) {
382b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
383b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
384b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
3850f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
3860f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                              ISD::NodeType, uint64_t /*Imm*/) {
38783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return 0;
38883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
38983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
3900f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
3910f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               ISD::NodeType, unsigned /*Op0*/,
3920f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               uint64_t /*Imm*/) {
393d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return 0;
394d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
395d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
3960f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
3970f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                                ISD::NodeType,
398d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                unsigned /*Op0*/, unsigned /*Op1*/,
399d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                uint64_t /*Imm*/) {
40083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return 0;
40183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
40283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
40383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
40483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// to emit an instruction with an immediate operand using FastEmit_ri.
40583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// If that fails, it materializes the immediate into a register and try
40683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_rr instead.
40783785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengunsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
408d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                unsigned Op0, uint64_t Imm,
409d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                MVT::SimpleValueType ImmType) {
41083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  unsigned ResultReg = 0;
41183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  // First check if immediate type is legal. If not, we can't use the ri form.
41283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
4130f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson    ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
41483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (ResultReg != 0)
41583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    return ResultReg;
4160f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson  unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
417d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (MaterialReg == 0)
418d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    return 0;
4190f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson  return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
420d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
421d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
422d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
423d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return MRI.createVirtualRegister(RC);
42483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
42583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
426b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
42777ad79689d755c49146f534107421cb3d9703fedDan Gohman                                 const TargetRegisterClass* RC) {
428d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
429bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
430b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
431fd903944de8ddcbe88902fa1eca9366eb9641201Dan Gohman  BuildMI(MBB, II, ResultReg);
432b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
433b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
434b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
435b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
436b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                  const TargetRegisterClass *RC,
437b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                  unsigned Op0) {
438d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
439bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
440b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
441fd903944de8ddcbe88902fa1eca9366eb9641201Dan Gohman  BuildMI(MBB, II, ResultReg).addReg(Op0);
442b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
443b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
444b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
445b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
446b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                   const TargetRegisterClass *RC,
447b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                   unsigned Op0, unsigned Op1) {
448d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
449bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
450b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
451fd903944de8ddcbe88902fa1eca9366eb9641201Dan Gohman  BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
452b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
453b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
454d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
455d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
456d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                   const TargetRegisterClass *RC,
457d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                   unsigned Op0, uint64_t Imm) {
458d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
459d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
460d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
461d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  BuildMI(MBB, II, ResultReg).addReg(Op0).addImm(Imm);
462d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return ResultReg;
463d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
464d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
465d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
466d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                    const TargetRegisterClass *RC,
467d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                    unsigned Op0, unsigned Op1, uint64_t Imm) {
468d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
469d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
470d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
471d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
472d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return ResultReg;
473d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
4746d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson
4756d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Andersonunsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
4766d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                                  const TargetRegisterClass *RC,
4776d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                                  uint64_t Imm) {
4786d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  unsigned ResultReg = createResultReg(RC);
4796d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
4806d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson
4816d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  BuildMI(MBB, II, ResultReg).addImm(Imm);
4826d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  return ResultReg;
483b41aec54767a825ac54c8822e787700bb08a3460Evan Cheng}
484