FastISel.cpp revision 92c1e126473dfa93eeb4c9a124af4fedb40f0d5b
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//
125ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// "Fast" instruction selection is designed to emit very poor code quickly.
135ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// Also, it is not designed to be able to do much lowering, so most illegal
1444d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// types (e.g. i64 on 32-bit targets) and operations are not supported.  It is
1544d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// also not intended to be able to do much optimization, except in a few cases
1644d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// where doing optimizations reduces overall compile time.  For example, folding
1744d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// constants into immediate fields is often done, because it's cheap and it
1844d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// reduces the number of instructions later phases have to examine.
195ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman//
205ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// "Fast" instruction selection is able to fail gracefully and transfer
215ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// control to the SelectionDAG selector for operations that it doesn't
2244d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// support.  In many cases, this allows us to avoid duplicating a lot of
235ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// the complicated lowering logic that SelectionDAG currently has.
245ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman//
255ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// The intended use for "fast" instruction selection is "-O0" mode
265ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// compilation, where the quality of the generated code is irrelevant when
2744d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// weighed against the speed at which the code can be generated.  Also,
285ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// at -O0, the LLVM optimizers are not running, and this makes the
295ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// compile time of codegen a much higher portion of the overall compile
3044d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// time.  Despite its limitations, "fast" instruction selection is able to
315ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// handle enough code on its own to provide noticeable overall speedups
325ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// in -O0 compiles.
335ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman//
345ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// Basic operations are supported in a target-independent way, by reading
355ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// the same instruction descriptions that the SelectionDAG selector reads,
365ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// and identifying simple arithmetic operations that can be directly selected
3744d2a983b76a2a923e34f3162c960443425cb296Chris Lattner// from simple operators.  More complicated operations currently require
385ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman// target-specific code.
395ec9efd61bc4214c787287409498e8b78f28c922Dan Gohman//
40b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman//===----------------------------------------------------------------------===//
41b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
4233134c4a75558288d663267c8991f6bd37a530afDan Gohman#include "llvm/Function.h"
4333134c4a75558288d663267c8991f6bd37a530afDan Gohman#include "llvm/GlobalVariable.h"
446f2766d59744bb3d48867f3151643eac7111e773Dan Gohman#include "llvm/Instructions.h"
4533134c4a75558288d663267c8991f6bd37a530afDan Gohman#include "llvm/IntrinsicInst.h"
46b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/FastISel.h"
47b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/MachineInstrBuilder.h"
4833134c4a75558288d663267c8991f6bd37a530afDan Gohman#include "llvm/CodeGen/MachineModuleInfo.h"
49b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/CodeGen/MachineRegisterInfo.h"
5083489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel#include "llvm/CodeGen/DwarfWriter.h"
5183489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel#include "llvm/Analysis/DebugInfo.h"
5283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng#include "llvm/Target/TargetData.h"
53b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/Target/TargetInstrInfo.h"
5483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng#include "llvm/Target/TargetLowering.h"
55bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman#include "llvm/Target/TargetMachine.h"
56dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#include "SelectionDAGBuild.h"
57b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanusing namespace llvm;
58b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
593df24e667f04a7003342b534310919abc9c87418Dan Gohmanunsigned FastISel::getRegForValue(Value *V) {
60ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  MVT::SimpleValueType VT = TLI.getValueType(V->getType()).getSimpleVT();
61821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman
62c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // Ignore illegal types. We must do this before looking up the value
63c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // in ValueMap because Arguments are given virtual registers regardless
64c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // of whether FastISel can handle them.
65821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman  if (!TLI.isTypeLegal(VT)) {
66821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman    // Promote MVT::i1 to a legal type though, because it's common and easy.
67821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman    if (VT == MVT::i1)
68821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman      VT = TLI.getTypeToTransformTo(VT).getSimpleVT();
69821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman    else
70821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman      return 0;
71821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman  }
72821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman
73c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // Look up the value to see if we already have a register for it. We
74c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // cache values defined by Instructions across blocks, and other values
75c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // only locally. This is because Instructions already have the SSA
76c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // def-dominatess-use requirement enforced.
77c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  if (ValueMap.count(V))
78c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman    return ValueMap[V];
79c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  unsigned Reg = LocalValueMap[V];
80c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  if (Reg != 0)
81c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman    return Reg;
82c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman
83ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
842ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman    if (CI->getValue().getActiveBits() <= 64)
852ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman      Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
860586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  } else if (isa<AllocaInst>(V)) {
872ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman    Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
88205d92589bc8c59d4bba9ddf89e0eb3c4d548cdaDan Gohman  } else if (isa<ConstantPointerNull>(V)) {
891e9e8c3bd5ac018296bddb21a2acb8c643303b39Dan Gohman    // Translate this as an integer zero so that it can be
901e9e8c3bd5ac018296bddb21a2acb8c643303b39Dan Gohman    // local-CSE'd with actual integer zeros.
911e9e8c3bd5ac018296bddb21a2acb8c643303b39Dan Gohman    Reg = getRegForValue(Constant::getNullValue(TD.getIntPtrType()));
92ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  } else if (ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
93104e4ce1629ea84736691bd1ee7867bdf90e8a2eDan Gohman    Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
94ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
95ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    if (!Reg) {
96ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      const APFloat &Flt = CF->getValueAPF();
97ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      MVT IntVT = TLI.getPointerTy();
98ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
99ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      uint64_t x[2];
100ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      uint32_t IntBitWidth = IntVT.getSizeInBits();
10123a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen      bool isExact;
10223a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen      (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
10323a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen                                APFloat::rmTowardZero, &isExact);
10423a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen      if (isExact) {
1052ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman        APInt IntVal(IntBitWidth, 2, x);
106ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
1071e9e8c3bd5ac018296bddb21a2acb8c643303b39Dan Gohman        unsigned IntegerReg = getRegForValue(ConstantInt::get(IntVal));
1082ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman        if (IntegerReg != 0)
1092ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman          Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP, IntegerReg);
1102ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman      }
111ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    }
11240b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
11340b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman    if (!SelectOperator(CE, CE->getOpcode())) return 0;
11440b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman    Reg = LocalValueMap[CE];
115205d92589bc8c59d4bba9ddf89e0eb3c4d548cdaDan Gohman  } else if (isa<UndefValue>(V)) {
116104e4ce1629ea84736691bd1ee7867bdf90e8a2eDan Gohman    Reg = createResultReg(TLI.getRegClassFor(VT));
1179bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, TII.get(TargetInstrInfo::IMPLICIT_DEF), Reg);
118ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  }
119d5d81a457b5ff758b3fcc527af38827490bc68a5Owen Anderson
120dceffe66b9e73ce372ea11c0fc6975504eb8c31dDan Gohman  // If target-independent code couldn't handle the value, give target-specific
121dceffe66b9e73ce372ea11c0fc6975504eb8c31dDan Gohman  // code a try.
1226e6074508c2f781c3e52dfe0e301cb1c7f395a91Owen Anderson  if (!Reg && isa<Constant>(V))
1232ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman    Reg = TargetMaterializeConstant(cast<Constant>(V));
1246e6074508c2f781c3e52dfe0e301cb1c7f395a91Owen Anderson
1252ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman  // Don't cache constant materializations in the general ValueMap.
1262ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman  // To do so would require tracking what uses they dominate.
127dceffe66b9e73ce372ea11c0fc6975504eb8c31dDan Gohman  if (Reg != 0)
128dceffe66b9e73ce372ea11c0fc6975504eb8c31dDan Gohman    LocalValueMap[V] = Reg;
129104e4ce1629ea84736691bd1ee7867bdf90e8a2eDan Gohman  return Reg;
130ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman}
131ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
13259fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Chengunsigned FastISel::lookUpRegForValue(Value *V) {
13359fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  // Look up the value to see if we already have a register for it. We
13459fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  // cache values defined by Instructions across blocks, and other values
13559fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  // only locally. This is because Instructions already have the SSA
13659fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  // def-dominatess-use requirement enforced.
13759fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  if (ValueMap.count(V))
13859fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng    return ValueMap[V];
13959fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  return LocalValueMap[V];
14059fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng}
14159fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng
142cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// UpdateValueMap - Update the value map to include the new mapping for this
143cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// instruction, or insert an extra copy to get the result in a previous
144cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// determined register.
145cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// NOTE: This is only necessary because we might select a block that uses
146cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// a value before we select the block that defines the value.  It might be
147cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// possible to fix this by selecting blocks in reverse postorder.
14895267a1e671efc3c14e916b6978bbb15973b4cdcOwen Andersonvoid FastISel::UpdateValueMap(Value* I, unsigned Reg) {
14940b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  if (!isa<Instruction>(I)) {
15040b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman    LocalValueMap[I] = Reg;
15140b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman    return;
15240b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  }
153cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson  if (!ValueMap.count(I))
154cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson    ValueMap[I] = Reg;
155cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson  else
156f09917847bb082829feba34d1818eb97764839d9Evan Cheng    TII.copyRegToReg(*MBB, MBB->end(), ValueMap[I],
157f09917847bb082829feba34d1818eb97764839d9Evan Cheng                     Reg, MRI.getRegClass(Reg), MRI.getRegClass(Reg));
158cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson}
159cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson
160c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohmanunsigned FastISel::getRegForGEPIndex(Value *Idx) {
161c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  unsigned IdxN = getRegForValue(Idx);
162c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  if (IdxN == 0)
163c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman    // Unhandled operand. Halt "fast" selection and bail.
164c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman    return 0;
165c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman
166c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // If the index is smaller or larger than intptr_t, truncate or extend it.
167c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  MVT PtrVT = TLI.getPointerTy();
168c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  MVT IdxVT = MVT::getMVT(Idx->getType(), /*HandleUnknown=*/false);
169c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  if (IdxVT.bitsLT(PtrVT))
170c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman    IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT.getSimpleVT(),
171c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman                      ISD::SIGN_EXTEND, IdxN);
172c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  else if (IdxVT.bitsGT(PtrVT))
173c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman    IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT.getSimpleVT(),
174c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman                      ISD::TRUNCATE, IdxN);
175c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  return IdxN;
176c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman}
177c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman
178bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// SelectBinaryOp - Select and emit code for a binary operator instruction,
179bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// which has an opcode which directly corresponds to the given ISD opcode.
180bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman///
18140b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohmanbool FastISel::SelectBinaryOp(User *I, ISD::NodeType ISDOpcode) {
182d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  MVT VT = MVT::getMVT(I->getType(), /*HandleUnknown=*/true);
183d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (VT == MVT::Other || !VT.isSimple())
184d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    // Unhandled type. Halt "fast" selection and bail.
185d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    return false;
186638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman
187b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // We only handle legal types. For example, on x86-32 the instruction
188b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // selector contains all of the 64-bit instructions from x86-64,
189b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // under the assumption that i64 won't be used if the target doesn't
190b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // support it.
191638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman  if (!TLI.isTypeLegal(VT)) {
1925dd9c2e9aea7294c184609aff7f2fe82eaea4eb0Dan Gohman    // MVT::i1 is special. Allow AND, OR, or XOR because they
193638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman    // don't require additional zeroing, which makes them easy.
194638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman    if (VT == MVT::i1 &&
1955dd9c2e9aea7294c184609aff7f2fe82eaea4eb0Dan Gohman        (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
1965dd9c2e9aea7294c184609aff7f2fe82eaea4eb0Dan Gohman         ISDOpcode == ISD::XOR))
197638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman      VT = TLI.getTypeToTransformTo(VT);
198638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman    else
199638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman      return false;
200638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman  }
201d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
2023df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned Op0 = getRegForValue(I->getOperand(0));
203d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (Op0 == 0)
204a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman    // Unhandled operand. Halt "fast" selection and bail.
205a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman    return false;
206a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman
207d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  // Check if the second operand is a constant and handle it appropriately.
208d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
209ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
210ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman                                     ISDOpcode, Op0, CI->getZExtValue());
211ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    if (ResultReg != 0) {
212ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      // We successfully emitted code for the given LLVM Instruction.
2133df24e667f04a7003342b534310919abc9c87418Dan Gohman      UpdateValueMap(I, ResultReg);
214ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      return true;
215ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    }
216d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  }
217d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
21810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  // Check if the second operand is a constant float.
21910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
220ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
221ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman                                     ISDOpcode, Op0, CF);
222ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    if (ResultReg != 0) {
223ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      // We successfully emitted code for the given LLVM Instruction.
2243df24e667f04a7003342b534310919abc9c87418Dan Gohman      UpdateValueMap(I, ResultReg);
225ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      return true;
226ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    }
22710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  }
22810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
2293df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned Op1 = getRegForValue(I->getOperand(1));
230d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (Op1 == 0)
231d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    // Unhandled operand. Halt "fast" selection and bail.
232bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    return false;
233bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
234ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // Now we have both operands in registers. Emit the instruction.
2350f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson  unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
2360f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                                   ISDOpcode, Op0, Op1);
237bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  if (ResultReg == 0)
238bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // Target-specific code wasn't able to find a machine opcode for
239bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // the given ISD opcode and type. Halt "fast" selection and bail.
240bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    return false;
241bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
2428014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman  // We successfully emitted code for the given LLVM Instruction.
2433df24e667f04a7003342b534310919abc9c87418Dan Gohman  UpdateValueMap(I, ResultReg);
244bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  return true;
245bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman}
246bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
24740b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohmanbool FastISel::SelectGetElementPtr(User *I) {
2483df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned N = getRegForValue(I->getOperand(0));
24983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (N == 0)
25083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    // Unhandled operand. Halt "fast" selection and bail.
25183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    return false;
25283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
25383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  const Type *Ty = I->getOperand(0)->getType();
2547a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman  MVT::SimpleValueType VT = TLI.getPointerTy().getSimpleVT();
25583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  for (GetElementPtrInst::op_iterator OI = I->op_begin()+1, E = I->op_end();
25683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng       OI != E; ++OI) {
25783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    Value *Idx = *OI;
25883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
25983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
26083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (Field) {
26183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // N = N + Offset
26283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
26383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // FIXME: This can be optimized by combining the add with a
26483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // subsequent one.
2657a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman        N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
26683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (N == 0)
26783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          // Unhandled operand. Halt "fast" selection and bail.
26883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          return false;
26983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      }
27083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      Ty = StTy->getElementType(Field);
27183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    } else {
27283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      Ty = cast<SequentialType>(Ty)->getElementType();
27383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
27483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // If this is a constant subscript, handle it quickly.
27583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
27683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (CI->getZExtValue() == 0) continue;
27783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        uint64_t Offs =
278ceb4d1aecb9deffe59b3dcdc9a783ffde8477be9Duncan Sands          TD.getTypePaddedSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
2797a0e6593d03bd2dd21c3ac7dcf189f1da86b16daDan Gohman        N = FastEmit_ri_(VT, ISD::ADD, N, Offs, VT);
28083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (N == 0)
28183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          // Unhandled operand. Halt "fast" selection and bail.
28283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          return false;
28383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        continue;
28483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      }
28583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
28683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // N = N + Idx * ElementSize;
287ceb4d1aecb9deffe59b3dcdc9a783ffde8477be9Duncan Sands      uint64_t ElementSize = TD.getTypePaddedSize(Ty);
288c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman      unsigned IdxN = getRegForGEPIndex(Idx);
28983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxN == 0)
29083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
29183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
29283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
29380bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman      if (ElementSize != 1) {
294f93cf79505f07cb97597fbc5955462ad7670ca5cDan Gohman        IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, ElementSize, VT);
29580bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman        if (IdxN == 0)
29680bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman          // Unhandled operand. Halt "fast" selection and bail.
29780bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman          return false;
29880bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman      }
2990f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson      N = FastEmit_rr(VT, VT, ISD::ADD, N, IdxN);
30083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (N == 0)
30183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
30283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
30383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    }
30483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  }
30583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
30683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  // We successfully emitted code for the given LLVM Instruction.
3073df24e667f04a7003342b534310919abc9c87418Dan Gohman  UpdateValueMap(I, N);
30883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return true;
309bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman}
310bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
31133134c4a75558288d663267c8991f6bd37a530afDan Gohmanbool FastISel::SelectCall(User *I) {
31233134c4a75558288d663267c8991f6bd37a530afDan Gohman  Function *F = cast<CallInst>(I)->getCalledFunction();
31333134c4a75558288d663267c8991f6bd37a530afDan Gohman  if (!F) return false;
31433134c4a75558288d663267c8991f6bd37a530afDan Gohman
31533134c4a75558288d663267c8991f6bd37a530afDan Gohman  unsigned IID = F->getIntrinsicID();
31633134c4a75558288d663267c8991f6bd37a530afDan Gohman  switch (IID) {
31733134c4a75558288d663267c8991f6bd37a530afDan Gohman  default: break;
31833134c4a75558288d663267c8991f6bd37a530afDan Gohman  case Intrinsic::dbg_stoppoint: {
31933134c4a75558288d663267c8991f6bd37a530afDan Gohman    DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
320b79b5359fbe44bc82bedff2c081ed1db787f8d49Devang Patel    if (DW && DW->ValidDebugInfo(SPI->getContext())) {
32183489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel      DICompileUnit CU(cast<GlobalVariable>(SPI->getContext()));
32283489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel      unsigned SrcFile = DW->RecordSource(CU.getDirectory(),
32383489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel                                          CU.getFilename());
32433134c4a75558288d663267c8991f6bd37a530afDan Gohman      unsigned Line = SPI->getLine();
32533134c4a75558288d663267c8991f6bd37a530afDan Gohman      unsigned Col = SPI->getColumn();
32692c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      unsigned ID = DW->RecordSourceLine(Line, Col, SrcFile);
3279bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling      unsigned Idx = MF.getOrCreateDebugLocID(SrcFile, Line, Col);
3289bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling      setCurDebugLoc(DebugLoc::get(Idx));
32992c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
33092c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      BuildMI(MBB, DL, II).addImm(ID);
33133134c4a75558288d663267c8991f6bd37a530afDan Gohman    }
33233134c4a75558288d663267c8991f6bd37a530afDan Gohman    return true;
33333134c4a75558288d663267c8991f6bd37a530afDan Gohman  }
33433134c4a75558288d663267c8991f6bd37a530afDan Gohman  case Intrinsic::dbg_region_start: {
33533134c4a75558288d663267c8991f6bd37a530afDan Gohman    DbgRegionStartInst *RSI = cast<DbgRegionStartInst>(I);
33692c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling    if (DW && DW->ValidDebugInfo(RSI->getContext())) {
33792c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      unsigned ID =
33892c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling        DW->RecordRegionStart(cast<GlobalVariable>(RSI->getContext()));
33992c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
34092c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      BuildMI(MBB, DL, II).addImm(ID);
34192c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling    }
34233134c4a75558288d663267c8991f6bd37a530afDan Gohman    return true;
34333134c4a75558288d663267c8991f6bd37a530afDan Gohman  }
34433134c4a75558288d663267c8991f6bd37a530afDan Gohman  case Intrinsic::dbg_region_end: {
34533134c4a75558288d663267c8991f6bd37a530afDan Gohman    DbgRegionEndInst *REI = cast<DbgRegionEndInst>(I);
34692c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling    if (DW && DW->ValidDebugInfo(REI->getContext())) {
34792c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      unsigned ID =
34892c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling        DW->RecordRegionEnd(cast<GlobalVariable>(REI->getContext()));
34992c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
35092c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      BuildMI(MBB, DL, II).addImm(ID);
35192c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling    }
35233134c4a75558288d663267c8991f6bd37a530afDan Gohman    return true;
35333134c4a75558288d663267c8991f6bd37a530afDan Gohman  }
35433134c4a75558288d663267c8991f6bd37a530afDan Gohman  case Intrinsic::dbg_func_start: {
35583489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel    if (!DW) return true;
35633134c4a75558288d663267c8991f6bd37a530afDan Gohman    DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
35733134c4a75558288d663267c8991f6bd37a530afDan Gohman    Value *SP = FSI->getSubprogram();
3589bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling
359b79b5359fbe44bc82bedff2c081ed1db787f8d49Devang Patel    if (DW->ValidDebugInfo(SP)) {
3609bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling      // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is what
3619bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling      // (most?) gdb expects.
36283489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel      DISubprogram Subprogram(cast<GlobalVariable>(SP));
36383489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel      DICompileUnit CompileUnit = Subprogram.getCompileUnit();
36483489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel      unsigned SrcFile = DW->RecordSource(CompileUnit.getDirectory(),
36583489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel                                          CompileUnit.getFilename());
3669bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling
367e75808cf3d92d14c5659a15d0d17bd21a54fd8ccDevang Patel      // Record the source line but does not create a label for the normal
368e75808cf3d92d14c5659a15d0d17bd21a54fd8ccDevang Patel      // function start. It will be emitted at asm emission time. However,
369e75808cf3d92d14c5659a15d0d17bd21a54fd8ccDevang Patel      // create a label if this is a beginning of inlined function.
3709bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling      unsigned Line = Subprogram.getLineNumber();
37192c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      unsigned LabelID = DW->RecordSourceLine(Line, 0, SrcFile);
3729bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling      setCurDebugLoc(DebugLoc::get(MF.getOrCreateDebugLocID(SrcFile, Line, 0)));
37392c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling
37492c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      if (DW->getRecordSourceLineCount() != 1) {
37592c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling        const TargetInstrDesc &II = TII.get(TargetInstrInfo::DBG_LABEL);
37692c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling        BuildMI(MBB, DL, II).addImm(LabelID);
37792c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      }
37833134c4a75558288d663267c8991f6bd37a530afDan Gohman    }
3799bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling
38033134c4a75558288d663267c8991f6bd37a530afDan Gohman    return true;
38133134c4a75558288d663267c8991f6bd37a530afDan Gohman  }
38292c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling  case Intrinsic::dbg_declare: {
38392c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling    DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
38492c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling    Value *Variable = DI->getVariable();
38592c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling    if (DW && DW->ValidDebugInfo(Variable)) {
38692c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      // Determine the address of the declared object.
38792c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      Value *Address = DI->getAddress();
38892c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
38992c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling        Address = BCI->getOperand(0);
39092c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      AllocaInst *AI = dyn_cast<AllocaInst>(Address);
39192c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      // Don't handle byval struct arguments or VLAs, for example.
39292c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      if (!AI) break;
39392c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      DenseMap<const AllocaInst*, int>::iterator SI =
39492c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling        StaticAllocaMap.find(AI);
39592c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      if (SI == StaticAllocaMap.end()) break; // VLAs.
39692c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      int FI = SI->second;
39792c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling
39892c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      // Determine the debug globalvariable.
39992c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      GlobalValue *GV = cast<GlobalVariable>(Variable);
40092c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling
40192c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      // Build the DECLARE instruction.
40292c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      const TargetInstrDesc &II = TII.get(TargetInstrInfo::DECLARE);
40392c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling      BuildMI(MBB, DL, II).addFrameIndex(FI).addGlobalAddress(GV);
40492c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling    }
40533134c4a75558288d663267c8991f6bd37a530afDan Gohman    return true;
40692c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling  }
407dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman  case Intrinsic::eh_exception: {
408dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    MVT VT = TLI.getValueType(I->getType());
409dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
410dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    default: break;
411dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    case TargetLowering::Expand: {
412dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      if (!MBB->isLandingPad()) {
413dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        // FIXME: Mark exception register as live in.  Hack for PR1508.
414dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        unsigned Reg = TLI.getExceptionAddressRegister();
415dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        if (Reg) MBB->addLiveIn(Reg);
416dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      }
417dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      unsigned Reg = TLI.getExceptionAddressRegister();
418dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
419dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      unsigned ResultReg = createResultReg(RC);
420dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
421dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                                           Reg, RC, RC);
422dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      assert(InsertedCopy && "Can't copy address registers!");
42324ac408ce891321d1a5d62beaf3487efce6f2b22Evan Cheng      InsertedCopy = InsertedCopy;
424dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      UpdateValueMap(I, ResultReg);
425dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      return true;
426dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
427dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
428dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    break;
429dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman  }
430dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman  case Intrinsic::eh_selector_i32:
431dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman  case Intrinsic::eh_selector_i64: {
432dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    MVT VT = TLI.getValueType(I->getType());
433dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
434dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    default: break;
435dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    case TargetLowering::Expand: {
436dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      MVT VT = (IID == Intrinsic::eh_selector_i32 ?
437dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                           MVT::i32 : MVT::i64);
438dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
439dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      if (MMI) {
440dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        if (MBB->isLandingPad())
441dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman          AddCatchInfo(*cast<CallInst>(I), MMI, MBB);
442dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        else {
443dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#ifndef NDEBUG
444dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman          CatchInfoLost.insert(cast<CallInst>(I));
445dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#endif
446dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman          // FIXME: Mark exception selector register as live in.  Hack for PR1508.
447dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman          unsigned Reg = TLI.getExceptionSelectorRegister();
448dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman          if (Reg) MBB->addLiveIn(Reg);
449dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        }
450dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman
451dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        unsigned Reg = TLI.getExceptionSelectorRegister();
452dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
453dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        unsigned ResultReg = createResultReg(RC);
454dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
455dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                                             Reg, RC, RC);
456dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        assert(InsertedCopy && "Can't copy address registers!");
45724ac408ce891321d1a5d62beaf3487efce6f2b22Evan Cheng        InsertedCopy = InsertedCopy;
458dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        UpdateValueMap(I, ResultReg);
459dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      } else {
460dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        unsigned ResultReg =
461dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman          getRegForValue(Constant::getNullValue(I->getType()));
462dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        UpdateValueMap(I, ResultReg);
463dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      }
464dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      return true;
465dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
466dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
467dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    break;
468dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman  }
46933134c4a75558288d663267c8991f6bd37a530afDan Gohman  }
47033134c4a75558288d663267c8991f6bd37a530afDan Gohman  return false;
47133134c4a75558288d663267c8991f6bd37a530afDan Gohman}
47233134c4a75558288d663267c8991f6bd37a530afDan Gohman
47340b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohmanbool FastISel::SelectCast(User *I, ISD::NodeType Opcode) {
4746336b70541204d1a8377ec1f33748a7260e0a31dOwen Anderson  MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
4756336b70541204d1a8377ec1f33748a7260e0a31dOwen Anderson  MVT DstVT = TLI.getValueType(I->getType());
476d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
477d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
478d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson      DstVT == MVT::Other || !DstVT.isSimple() ||
47991b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman      !TLI.isTypeLegal(DstVT))
480d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    // Unhandled type. Halt "fast" selection and bail.
481d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
482d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
48391b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman  // Check if the source operand is legal. Or as a special case,
48491b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman  // it may be i1 if we're doing zero-extension because that's
48591b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman  // trivially easy and somewhat common.
48691b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman  if (!TLI.isTypeLegal(SrcVT)) {
48791b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman    if (SrcVT == MVT::i1 && Opcode == ISD::ZERO_EXTEND)
48891b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman      SrcVT = TLI.getTypeToTransformTo(SrcVT);
48991b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman    else
49091b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman      // Unhandled type. Halt "fast" selection and bail.
49191b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman      return false;
49291b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman  }
49391b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman
4943df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned InputReg = getRegForValue(I->getOperand(0));
495d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  if (!InputReg)
496d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    // Unhandled operand.  Halt "fast" selection and bail.
497d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
498d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
499d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
500d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson                                  DstVT.getSimpleVT(),
501d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson                                  Opcode,
502d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson                                  InputReg);
503d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  if (!ResultReg)
504d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
505d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
5063df24e667f04a7003342b534310919abc9c87418Dan Gohman  UpdateValueMap(I, ResultReg);
507d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  return true;
508d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson}
509d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
51040b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohmanbool FastISel::SelectBitCast(User *I) {
511ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // If the bitcast doesn't change the type, just use the operand value.
512ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (I->getType() == I->getOperand(0)->getType()) {
5133df24e667f04a7003342b534310919abc9c87418Dan Gohman    unsigned Reg = getRegForValue(I->getOperand(0));
514a318dabc0edbcc7a2b54d99b026a093361ec14fcDan Gohman    if (Reg == 0)
515a318dabc0edbcc7a2b54d99b026a093361ec14fcDan Gohman      return false;
5163df24e667f04a7003342b534310919abc9c87418Dan Gohman    UpdateValueMap(I, Reg);
517ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    return true;
518ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  }
519ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
520ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
5216336b70541204d1a8377ec1f33748a7260e0a31dOwen Anderson  MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
5226336b70541204d1a8377ec1f33748a7260e0a31dOwen Anderson  MVT DstVT = TLI.getValueType(I->getType());
523d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
524d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
525d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson      DstVT == MVT::Other || !DstVT.isSimple() ||
526d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson      !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
527d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    // Unhandled type. Halt "fast" selection and bail.
528d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
529d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
5303df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned Op0 = getRegForValue(I->getOperand(0));
531ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (Op0 == 0)
532ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    // Unhandled operand. Halt "fast" selection and bail.
533d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
534d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
535ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // First, try to perform the bitcast by inserting a reg-reg copy.
536ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  unsigned ResultReg = 0;
537ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
538ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
539ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
540ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    ResultReg = createResultReg(DstClass);
541ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
542ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
543ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman                                         Op0, DstClass, SrcClass);
544ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    if (!InsertedCopy)
545ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      ResultReg = 0;
546ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  }
547ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
548ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // If the reg-reg copy failed, select a BIT_CONVERT opcode.
549ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (!ResultReg)
550ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
551ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman                           ISD::BIT_CONVERT, Op0);
552ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
553ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (!ResultReg)
554d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
555d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
5563df24e667f04a7003342b534310919abc9c87418Dan Gohman  UpdateValueMap(I, ResultReg);
557d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  return true;
558d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson}
559d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
5603df24e667f04a7003342b534310919abc9c87418Dan Gohmanbool
5613df24e667f04a7003342b534310919abc9c87418Dan GohmanFastISel::SelectInstruction(Instruction *I) {
56240b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  return SelectOperator(I, I->getOpcode());
56340b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman}
56440b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman
565d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman/// FastEmitBranch - Emit an unconditional branch to the given block,
566d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman/// unless it is the immediate (fall-through) successor, and update
567d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman/// the CFG.
568d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohmanvoid
569d98d6203e429b2d7208b6687931e9079e85e95ecDan GohmanFastISel::FastEmitBranch(MachineBasicBlock *MSucc) {
570d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  MachineFunction::iterator NextMBB =
571d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman     next(MachineFunction::iterator(MBB));
572d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman
573d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  if (MBB->isLayoutSuccessor(MSucc)) {
574d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman    // The unconditional fall-through case, which needs no instructions.
575d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  } else {
576d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman    // The unconditional branch case.
577d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman    TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>());
578d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  }
579d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  MBB->addSuccessor(MSucc);
580d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman}
581d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman
58240b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohmanbool
58340b189e4e257924d90aaf63bf2e12bc7bbca961aDan GohmanFastISel::SelectOperator(User *I, unsigned Opcode) {
58440b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  switch (Opcode) {
5853df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Add: {
5863df24e667f04a7003342b534310919abc9c87418Dan Gohman    ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FADD : ISD::ADD;
5873df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, Opc);
5883df24e667f04a7003342b534310919abc9c87418Dan Gohman  }
5893df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Sub: {
5903df24e667f04a7003342b534310919abc9c87418Dan Gohman    ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FSUB : ISD::SUB;
5913df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, Opc);
5923df24e667f04a7003342b534310919abc9c87418Dan Gohman  }
5933df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Mul: {
5943df24e667f04a7003342b534310919abc9c87418Dan Gohman    ISD::NodeType Opc = I->getType()->isFPOrFPVector() ? ISD::FMUL : ISD::MUL;
5953df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, Opc);
5963df24e667f04a7003342b534310919abc9c87418Dan Gohman  }
5973df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::SDiv:
5983df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SDIV);
5993df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::UDiv:
6003df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::UDIV);
6013df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::FDiv:
6023df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::FDIV);
6033df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::SRem:
6043df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SREM);
6053df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::URem:
6063df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::UREM);
6073df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::FRem:
6083df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::FREM);
6093df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Shl:
6103df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SHL);
6113df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::LShr:
6123df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SRL);
6133df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::AShr:
6143df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SRA);
6153df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::And:
6163df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::AND);
6173df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Or:
6183df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::OR);
6193df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Xor:
6203df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::XOR);
6213df24e667f04a7003342b534310919abc9c87418Dan Gohman
6223df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::GetElementPtr:
6233df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectGetElementPtr(I);
6243df24e667f04a7003342b534310919abc9c87418Dan Gohman
6253df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Br: {
6263df24e667f04a7003342b534310919abc9c87418Dan Gohman    BranchInst *BI = cast<BranchInst>(I);
6273df24e667f04a7003342b534310919abc9c87418Dan Gohman
6283df24e667f04a7003342b534310919abc9c87418Dan Gohman    if (BI->isUnconditional()) {
6293df24e667f04a7003342b534310919abc9c87418Dan Gohman      BasicBlock *LLVMSucc = BI->getSuccessor(0);
6303df24e667f04a7003342b534310919abc9c87418Dan Gohman      MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
631d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman      FastEmitBranch(MSucc);
6323df24e667f04a7003342b534310919abc9c87418Dan Gohman      return true;
6339d5b41624003daf259b33fc953aa471049700353Owen Anderson    }
6343df24e667f04a7003342b534310919abc9c87418Dan Gohman
6353df24e667f04a7003342b534310919abc9c87418Dan Gohman    // Conditional branches are not handed yet.
6363df24e667f04a7003342b534310919abc9c87418Dan Gohman    // Halt "fast" selection and bail.
6373df24e667f04a7003342b534310919abc9c87418Dan Gohman    return false;
638b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  }
639b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
640087c8507e592bbbede1746f07bd44b28559e3684Dan Gohman  case Instruction::Unreachable:
641087c8507e592bbbede1746f07bd44b28559e3684Dan Gohman    // Nothing to emit.
642087c8507e592bbbede1746f07bd44b28559e3684Dan Gohman    return true;
643087c8507e592bbbede1746f07bd44b28559e3684Dan Gohman
6443df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::PHI:
6453df24e667f04a7003342b534310919abc9c87418Dan Gohman    // PHI nodes are already emitted.
6463df24e667f04a7003342b534310919abc9c87418Dan Gohman    return true;
6470586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman
6480586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  case Instruction::Alloca:
6490586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    // FunctionLowering has the static-sized case covered.
6500586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    if (StaticAllocaMap.count(cast<AllocaInst>(I)))
6510586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman      return true;
6520586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman
6530586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    // Dynamic-sized alloca is not handled yet.
6540586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    return false;
6553df24e667f04a7003342b534310919abc9c87418Dan Gohman
65633134c4a75558288d663267c8991f6bd37a530afDan Gohman  case Instruction::Call:
65733134c4a75558288d663267c8991f6bd37a530afDan Gohman    return SelectCall(I);
65833134c4a75558288d663267c8991f6bd37a530afDan Gohman
6593df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::BitCast:
6603df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBitCast(I);
6613df24e667f04a7003342b534310919abc9c87418Dan Gohman
6623df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::FPToSI:
6633df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::FP_TO_SINT);
6643df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::ZExt:
6653df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::ZERO_EXTEND);
6663df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::SExt:
6673df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::SIGN_EXTEND);
6683df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Trunc:
6693df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::TRUNCATE);
6703df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::SIToFP:
6713df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::SINT_TO_FP);
6723df24e667f04a7003342b534310919abc9c87418Dan Gohman
6733df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::IntToPtr: // Deliberate fall-through.
6743df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::PtrToInt: {
6753df24e667f04a7003342b534310919abc9c87418Dan Gohman    MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
6763df24e667f04a7003342b534310919abc9c87418Dan Gohman    MVT DstVT = TLI.getValueType(I->getType());
6773df24e667f04a7003342b534310919abc9c87418Dan Gohman    if (DstVT.bitsGT(SrcVT))
6783df24e667f04a7003342b534310919abc9c87418Dan Gohman      return SelectCast(I, ISD::ZERO_EXTEND);
6793df24e667f04a7003342b534310919abc9c87418Dan Gohman    if (DstVT.bitsLT(SrcVT))
6803df24e667f04a7003342b534310919abc9c87418Dan Gohman      return SelectCast(I, ISD::TRUNCATE);
6813df24e667f04a7003342b534310919abc9c87418Dan Gohman    unsigned Reg = getRegForValue(I->getOperand(0));
6823df24e667f04a7003342b534310919abc9c87418Dan Gohman    if (Reg == 0) return false;
6833df24e667f04a7003342b534310919abc9c87418Dan Gohman    UpdateValueMap(I, Reg);
6843df24e667f04a7003342b534310919abc9c87418Dan Gohman    return true;
6853df24e667f04a7003342b534310919abc9c87418Dan Gohman  }
686d57dd5f4e6740520820bc0fca42a540e31c27a73Dan Gohman
6873df24e667f04a7003342b534310919abc9c87418Dan Gohman  default:
6883df24e667f04a7003342b534310919abc9c87418Dan Gohman    // Unhandled instruction. Halt "fast" selection and bail.
6893df24e667f04a7003342b534310919abc9c87418Dan Gohman    return false;
6903df24e667f04a7003342b534310919abc9c87418Dan Gohman  }
691b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
692b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
6933df24e667f04a7003342b534310919abc9c87418Dan GohmanFastISel::FastISel(MachineFunction &mf,
694d57dd5f4e6740520820bc0fca42a540e31c27a73Dan Gohman                   MachineModuleInfo *mmi,
69583489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel                   DwarfWriter *dw,
6963df24e667f04a7003342b534310919abc9c87418Dan Gohman                   DenseMap<const Value *, unsigned> &vm,
6970586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman                   DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
698dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                   DenseMap<const AllocaInst *, int> &am
699dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#ifndef NDEBUG
700dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                   , SmallSet<Instruction*, 8> &cil
701dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#endif
702dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                   )
7033df24e667f04a7003342b534310919abc9c87418Dan Gohman  : MBB(0),
7043df24e667f04a7003342b534310919abc9c87418Dan Gohman    ValueMap(vm),
7053df24e667f04a7003342b534310919abc9c87418Dan Gohman    MBBMap(bm),
7060586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    StaticAllocaMap(am),
707dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#ifndef NDEBUG
708dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    CatchInfoLost(cil),
709dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#endif
7103df24e667f04a7003342b534310919abc9c87418Dan Gohman    MF(mf),
711d57dd5f4e6740520820bc0fca42a540e31c27a73Dan Gohman    MMI(mmi),
71283489bb7700c69b7a4a8da59365c42d3f5c8129bDevang Patel    DW(dw),
7133df24e667f04a7003342b534310919abc9c87418Dan Gohman    MRI(MF.getRegInfo()),
7140586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    MFI(*MF.getFrameInfo()),
7150586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    MCP(*MF.getConstantPool()),
7163df24e667f04a7003342b534310919abc9c87418Dan Gohman    TM(MF.getTarget()),
71722bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TD(*TM.getTargetData()),
71822bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TII(*TM.getInstrInfo()),
71922bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TLI(*TM.getTargetLowering()) {
720bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman}
721bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman
722e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan GohmanFastISel::~FastISel() {}
723e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan Gohman
72436fd941fc029c6ea50ed08d26a2bfe4932b9789cEvan Chengunsigned FastISel::FastEmit_(MVT::SimpleValueType, MVT::SimpleValueType,
72536fd941fc029c6ea50ed08d26a2bfe4932b9789cEvan Cheng                             ISD::NodeType) {
726b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
727b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
728b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
7290f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_r(MVT::SimpleValueType, MVT::SimpleValueType,
7300f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                              ISD::NodeType, unsigned /*Op0*/) {
731b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
732b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
733b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
7340f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_rr(MVT::SimpleValueType, MVT::SimpleValueType,
7350f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               ISD::NodeType, unsigned /*Op0*/,
7360f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               unsigned /*Op0*/) {
737b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
738b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
739b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
7400f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_i(MVT::SimpleValueType, MVT::SimpleValueType,
7410f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                              ISD::NodeType, uint64_t /*Imm*/) {
74283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return 0;
74383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
74483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
74510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohmanunsigned FastISel::FastEmit_f(MVT::SimpleValueType, MVT::SimpleValueType,
74610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                              ISD::NodeType, ConstantFP * /*FPImm*/) {
74710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  return 0;
74810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman}
74910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
7500f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_ri(MVT::SimpleValueType, MVT::SimpleValueType,
7510f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               ISD::NodeType, unsigned /*Op0*/,
7520f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               uint64_t /*Imm*/) {
753d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return 0;
754d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
755d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
75610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohmanunsigned FastISel::FastEmit_rf(MVT::SimpleValueType, MVT::SimpleValueType,
75710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                               ISD::NodeType, unsigned /*Op0*/,
75810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                               ConstantFP * /*FPImm*/) {
75910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  return 0;
76010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman}
76110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
7620f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Andersonunsigned FastISel::FastEmit_rri(MVT::SimpleValueType, MVT::SimpleValueType,
7630f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                                ISD::NodeType,
764d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                unsigned /*Op0*/, unsigned /*Op1*/,
765d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                uint64_t /*Imm*/) {
76683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return 0;
76783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
76883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
76983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
77083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// to emit an instruction with an immediate operand using FastEmit_ri.
77183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// If that fails, it materializes the immediate into a register and try
77283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_rr instead.
77383785c80968165b30fcdd111ceb2c28d38bcff86Evan Chengunsigned FastISel::FastEmit_ri_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
774d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                unsigned Op0, uint64_t Imm,
775d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                MVT::SimpleValueType ImmType) {
77683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  // First check if immediate type is legal. If not, we can't use the ri form.
777151ed61a2f9c3482d35a54d502e7cd147f22a21bDan Gohman  unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Imm);
77883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (ResultReg != 0)
77983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    return ResultReg;
7800f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson  unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
781d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (MaterialReg == 0)
782d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    return 0;
7830f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson  return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
784d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
785d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
78610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
78710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman/// to emit an instruction with a floating-point immediate operand using
78810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman/// FastEmit_rf. If that fails, it materializes the immediate into a register
78910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman/// and try FastEmit_rr instead.
79010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohmanunsigned FastISel::FastEmit_rf_(MVT::SimpleValueType VT, ISD::NodeType Opcode,
79110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                                unsigned Op0, ConstantFP *FPImm,
79210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                                MVT::SimpleValueType ImmType) {
79310df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  // First check if immediate type is legal. If not, we can't use the rf form.
794151ed61a2f9c3482d35a54d502e7cd147f22a21bDan Gohman  unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, FPImm);
79510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  if (ResultReg != 0)
79610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    return ResultReg;
79710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
79810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  // Materialize the constant in a register.
79910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
80010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  if (MaterialReg == 0) {
80196a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // If the target doesn't have a way to directly enter a floating-point
80296a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // value into a register, use an alternate approach.
80396a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // TODO: The current approach only supports floating-point constants
80496a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // that can be constructed by conversion from integer values. This should
80596a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // be replaced by code that creates a load from a constant-pool entry,
80696a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // which will require some target-specific work.
80710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    const APFloat &Flt = FPImm->getValueAPF();
80810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    MVT IntVT = TLI.getPointerTy();
80910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
81010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    uint64_t x[2];
81110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    uint32_t IntBitWidth = IntVT.getSizeInBits();
81223a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen    bool isExact;
81323a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen    (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
81423a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen                             APFloat::rmTowardZero, &isExact);
81523a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen    if (!isExact)
81610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman      return 0;
81710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    APInt IntVal(IntBitWidth, 2, x);
81810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
81910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
82010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                                     ISD::Constant, IntVal.getZExtValue());
82110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    if (IntegerReg == 0)
82210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman      return 0;
82310df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
82410df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                             ISD::SINT_TO_FP, IntegerReg);
82510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    if (MaterialReg == 0)
82610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman      return 0;
82710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  }
82810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  return FastEmit_rr(VT, VT, Opcode, Op0, MaterialReg);
82910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman}
83010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
831d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
832d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return MRI.createVirtualRegister(RC);
83383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
83483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
835b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
83677ad79689d755c49146f534107421cb3d9703fedDan Gohman                                 const TargetRegisterClass* RC) {
837d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
838bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
839b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
8409bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling  BuildMI(MBB, DL, II, ResultReg);
841b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
842b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
843b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
844b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
845b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                  const TargetRegisterClass *RC,
846b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                  unsigned Op0) {
847d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
848bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
849b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
8505960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
8519bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II, ResultReg).addReg(Op0);
8525960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
8539bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II).addReg(Op0);
8545960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
8555960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng                                         II.ImplicitDefs[0], RC, RC);
8565960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
8575960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
8585960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
8595960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng
860b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
861b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
862b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
863b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
864b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                   const TargetRegisterClass *RC,
865b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                   unsigned Op0, unsigned Op1) {
866d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
867bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
868b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
8695960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
8709bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1);
8715960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
8729bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1);
8735960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
8745960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng                                         II.ImplicitDefs[0], RC, RC);
8755960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
8765960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
8775960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
878b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
879b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
880d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
881d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
882d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                   const TargetRegisterClass *RC,
883d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                   unsigned Op0, uint64_t Imm) {
884d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
885d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
886d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
8875960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
8889bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Imm);
8895960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
8909bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II).addReg(Op0).addImm(Imm);
8915960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
8925960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng                                         II.ImplicitDefs[0], RC, RC);
8935960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
8945960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
8955960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
896d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return ResultReg;
897d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
898d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
89910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohmanunsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
90010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                                   const TargetRegisterClass *RC,
90110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                                   unsigned Op0, ConstantFP *FPImm) {
90210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  unsigned ResultReg = createResultReg(RC);
90310df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
90410df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
9055960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
9069bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addFPImm(FPImm);
9075960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
9089bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II).addReg(Op0).addFPImm(FPImm);
9095960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
9105960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng                                         II.ImplicitDefs[0], RC, RC);
9115960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
9125960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
9135960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
91410df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  return ResultReg;
91510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman}
91610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
917d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
918d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                    const TargetRegisterClass *RC,
919d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                    unsigned Op0, unsigned Op1, uint64_t Imm) {
920d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
921d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
922d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
9235960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
9249bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addReg(Op1).addImm(Imm);
9255960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
9269bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II).addReg(Op0).addReg(Op1).addImm(Imm);
9275960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
9285960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng                                         II.ImplicitDefs[0], RC, RC);
9295960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
9305960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
9315960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
932d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return ResultReg;
933d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
9346d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson
9356d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Andersonunsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
9366d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                                  const TargetRegisterClass *RC,
9376d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                                  uint64_t Imm) {
9386d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  unsigned ResultReg = createResultReg(RC);
9396d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
9406d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson
9415960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
9429bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
9435960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
9449bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II).addImm(Imm);
9455960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
9465960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng                                         II.ImplicitDefs[0], RC, RC);
9475960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
9485960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
9495960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
9506d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  return ResultReg;
951b41aec54767a825ac54c8822e787700bb08a3460Evan Cheng}
9528970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson
953536ab130ec95cbb7bf30530251dafa7dfecc8471Evan Chengunsigned FastISel::FastEmitInst_extractsubreg(MVT::SimpleValueType RetVT,
954536ab130ec95cbb7bf30530251dafa7dfecc8471Evan Cheng                                              unsigned Op0, uint32_t Idx) {
95540a468f24909792f000e3ccc1dda7a27b9c34b69Owen Anderson  const TargetRegisterClass* RC = MRI.getRegClass(Op0);
9568970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson
957536ab130ec95cbb7bf30530251dafa7dfecc8471Evan Cheng  unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
9588970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson  const TargetInstrDesc &II = TII.get(TargetInstrInfo::EXTRACT_SUBREG);
9598970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson
9605960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
9619bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II, ResultReg).addReg(Op0).addImm(Idx);
9625960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
9639bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II).addReg(Op0).addImm(Idx);
9645960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
9655960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng                                         II.ImplicitDefs[0], RC, RC);
9665960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
9675960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
9685960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
9698970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson  return ResultReg;
9708970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson}
971