FastISel.cpp revision 83785c80968165b30fcdd111ceb2c28d38bcff86
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) {
29bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  unsigned Op0 = ValueMap[I->getOperand(0)];
30bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  unsigned Op1 = ValueMap[I->getOperand(1)];
31a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman  if (Op0 == 0 || Op1 == 0)
32a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman    // Unhandled operand. Halt "fast" selection and bail.
33a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman    return false;
34a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman
35bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
36bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  if (VT == MVT::Other || !VT.isSimple())
37bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // Unhandled type. Halt "fast" selection and bail.
38bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    return false;
39bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
40bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), ISDOpcode, Op0, Op1);
41bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  if (ResultReg == 0)
42bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // Target-specific code wasn't able to find a machine opcode for
43bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // the given ISD opcode and type. Halt "fast" selection and bail.
44bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    return false;
45bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
468014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman  // We successfully emitted code for the given LLVM Instruction.
47bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  ValueMap[I] = ResultReg;
48bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  return true;
49bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman}
50bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
51bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohmanbool FastISel::SelectGetElementPtr(Instruction *I,
52bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman                                   DenseMap<const Value*, unsigned> &ValueMap) {
5383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  unsigned N = ValueMap[I->getOperand(0)];
5483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (N == 0)
5583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    // Unhandled operand. Halt "fast" selection and bail.
5683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    return false;
5783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
5883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  const Type *Ty = I->getOperand(0)->getType();
5983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  MVT VT = MVT::getMVT(Ty, /*HandleUnknown=*/true);
6083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  MVT::SimpleValueType PtrVT = TLI.getPointerTy().getSimpleVT();
6183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
6283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
6383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng       OI != E; ++OI) {
6483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    Value *Idx = *OI;
6583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
6683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
6783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (Field) {
6883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // N = N + Offset
6983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
7083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // FIXME: This can be optimized by combining the add with a
7183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // subsequent one.
7283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        N = FastEmit_ri(VT.getSimpleVT(), ISD::ADD, N, Offs, PtrVT);
7383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (N == 0)
7483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          // Unhandled operand. Halt "fast" selection and bail.
7583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          return false;
7683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      }
7783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      Ty = StTy->getElementType(Field);
7883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    } else {
7983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      Ty = cast<SequentialType>(Ty)->getElementType();
8083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
8183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // If this is a constant subscript, handle it quickly.
8283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
8383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (CI->getZExtValue() == 0) continue;
8483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        uint64_t Offs =
8583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          TD.getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
8683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        N = FastEmit_ri(VT.getSimpleVT(), ISD::ADD, N, Offs, PtrVT);
8783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (N == 0)
8883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          // Unhandled operand. Halt "fast" selection and bail.
8983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          return false;
9083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        continue;
9183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      }
9283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
9383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // N = N + Idx * ElementSize;
9483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      uint64_t ElementSize = TD.getABITypeSize(Ty);
9583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      unsigned IdxN = ValueMap[Idx];
9683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxN == 0)
9783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
9883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
9983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
10083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // If the index is smaller or larger than intptr_t, truncate or extend
10183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // it.
10283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/true);
10383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxVT.bitsLT(VT))
10483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        IdxN = FastEmit_r(VT.getSimpleVT(), ISD::SIGN_EXTEND, IdxN);
10583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      else if (IdxVT.bitsGT(VT))
10683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        IdxN = FastEmit_r(VT.getSimpleVT(), ISD::TRUNCATE, IdxN);
10783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxN == 0)
10883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
10983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
11083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
11183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // FIXME: If multiple is power of two, turn it into a shift. The
11283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // optimization should be in FastEmit_ri?
11383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      IdxN = FastEmit_ri(VT.getSimpleVT(), ISD::MUL, IdxN,
11483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng                         ElementSize, PtrVT);
11583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxN == 0)
11683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
11783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
11883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      N = FastEmit_rr(VT.getSimpleVT(), ISD::ADD, N, IdxN);
11983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (N == 0)
12083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
12183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
12283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    }
12383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  }
12483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
12583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  // We successfully emitted code for the given LLVM Instruction.
12683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  ValueMap[I] = N;
12783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return true;
128bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman}
129bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
130b0cf29c5cfff797284b3660dc233e135feb65d9aDan GohmanBasicBlock::iterator
131b7864a9e23be526f4f8cde529a57c8943c61c0c7Dan GohmanFastISel::SelectInstructions(BasicBlock::iterator Begin,
132b7864a9e23be526f4f8cde529a57c8943c61c0c7Dan Gohman                             BasicBlock::iterator End,
133bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman                             DenseMap<const Value*, unsigned> &ValueMap,
134bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman                             MachineBasicBlock *mbb) {
135bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  MBB = mbb;
136b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  BasicBlock::iterator I = Begin;
137b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
138b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  for (; I != End; ++I) {
139b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman    switch (I->getOpcode()) {
1408014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    case Instruction::Add: {
1418014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
1428014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
1438014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    }
1448014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    case Instruction::Sub: {
1458014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
1468014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
1478014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    }
1488014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    case Instruction::Mul: {
1498014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
1508014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman      if (!SelectBinaryOp(I, Opc, ValueMap))  return I; break;
1518014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman    }
152bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::SDiv:
153bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SDIV, ValueMap)) return I; break;
154bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::UDiv:
155bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::UDIV, ValueMap)) return I; break;
156bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::FDiv:
157bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::FDIV, ValueMap)) return I; break;
158bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::SRem:
159bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SREM, ValueMap)) return I; break;
160bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::URem:
161bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::UREM, ValueMap)) return I; break;
162bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::FRem:
163bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::FREM, ValueMap)) return I; break;
164bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::Shl:
165bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SHL, ValueMap)) return I; break;
166bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::LShr:
167bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SRL, ValueMap)) return I; break;
168bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::AShr:
169bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::SRA, ValueMap)) return I; break;
170bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::And:
171bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::AND, ValueMap)) return I; break;
172bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::Or:
173bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::OR, ValueMap)) return I; break;
174bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::Xor:
175bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectBinaryOp(I, ISD::XOR, ValueMap)) return I; break;
176bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
177bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    case Instruction::GetElementPtr:
178bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman      if (!SelectGetElementPtr(I, ValueMap)) return I;
179b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman      break;
180bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
1816f2766d59744bb3d48867f3151643eac7111e773Dan Gohman    case Instruction::Br: {
1826f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      BranchInst *BI = cast<BranchInst>(I);
1836f2766d59744bb3d48867f3151643eac7111e773Dan Gohman
1846f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      // For now, check for and handle just the most trivial case: an
1856f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      // unconditional fall-through branch.
186e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman      if (BI->isUnconditional()) {
187e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman         MachineFunction::iterator NextMBB =
188e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman           next(MachineFunction::iterator(MBB));
189bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman         if (NextMBB != MF.end() &&
190e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman             NextMBB->getBasicBlock() == BI->getSuccessor(0)) {
191e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman          MBB->addSuccessor(NextMBB);
192e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman          break;
193e6798b757acb3a2077c2498e9913fff2f5e4325cDan Gohman        }
1946f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      }
1956f2766d59744bb3d48867f3151643eac7111e773Dan Gohman
1966f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      // Something more complicated. Halt "fast" selection and bail.
1976f2766d59744bb3d48867f3151643eac7111e773Dan Gohman      return I;
1986f2766d59744bb3d48867f3151643eac7111e773Dan Gohman    }
199b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman    default:
200b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman      // Unhandled instruction. Halt "fast" selection and bail.
201b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman      return I;
202b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman    }
203b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  }
204b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
205b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return I;
206b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
207b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
208bb466331e7e50d03497ce40ee344870236fd9c32Dan GohmanFastISel::FastISel(MachineFunction &mf)
20983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  : MF(mf), MRI(mf.getRegInfo()),
21083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    TD(*mf.getTarget().getTargetData()),
21183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    TII(*mf.getTarget().getInstrInfo()),
21283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    TLI(*mf.getTarget().getTargetLowering()) {
213bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman}
214bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman
215e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan GohmanFastISel::~FastISel() {}
216e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan Gohman
217b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmit_(MVT::SimpleValueType, ISD::NodeType) {
218b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
219b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
220b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
221b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmit_r(MVT::SimpleValueType, ISD::NodeType,
222b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                              unsigned /*Op0*/) {
223b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
224b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
225b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
226b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmit_rr(MVT::SimpleValueType, ISD::NodeType,
227b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                               unsigned /*Op0*/, unsigned /*Op0*/) {
228b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
229b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
230b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
23183785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengunsigned FastISel::FastEmit_i(MVT::SimpleValueType, uint64_t) {
23283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return 0;
23383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
23483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
23583785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengunsigned FastISel::FastEmit_ri(MVT::SimpleValueType, ISD::NodeType,
23683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng                               unsigned /*Op0*/, uint64_t Imm,
23783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng                               MVT::SimpleValueType ImmType) {
23883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return 0;
23983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
24083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
24183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
24283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// to emit an instruction with an immediate operand using FastEmit_ri.
24383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// If that fails, it materializes the immediate into a register and try
24483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_rr instead.
24583785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengunsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
24683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng                               unsigned Op0, uint64_t Imm,
24783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng                               MVT::SimpleValueType ImmType) {
24883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  unsigned ResultReg = 0;
24983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  // First check if immediate type is legal. If not, we can't use the ri form.
25083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (TLI.getOperationAction(ISD::Constant, ImmType) == TargetLowering::Legal)
25183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    ResultReg = FastEmit_ri(VT, Opcode, Op0, Imm, ImmType);
25283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (ResultReg != 0)
25383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    return ResultReg;
25483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return FastEmit_rr(VT, Opcode, Op0, FastEmit_i(ImmType, Imm));
25583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
25683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
257b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
25877ad79689d755c49146f534107421cb3d9703fedDan Gohman                                 const TargetRegisterClass* RC) {
259b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  unsigned ResultReg = MRI.createVirtualRegister(RC);
260bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
261b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
262f990b571c5c4828206b4e14ae7f95d36739b4336Dan Gohman  MachineInstr *MI = BuildMI(MBB, II, ResultReg);
263b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
264b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
265b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
266b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
267b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                  const TargetRegisterClass *RC,
268b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                  unsigned Op0) {
269b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  unsigned ResultReg = MRI.createVirtualRegister(RC);
270bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
271b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
272f990b571c5c4828206b4e14ae7f95d36739b4336Dan Gohman  MachineInstr *MI = BuildMI(MBB, II, ResultReg).addReg(Op0);
273b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
274b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
275b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
276b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
277b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                   const TargetRegisterClass *RC,
278b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                   unsigned Op0, unsigned Op1) {
279b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  unsigned ResultReg = MRI.createVirtualRegister(RC);
280bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
281b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
282f990b571c5c4828206b4e14ae7f95d36739b4336Dan Gohman  MachineInstr *MI = BuildMI(MBB, II, ResultReg).addReg(Op0).addReg(Op1);
283b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
284b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
285