FastISel.cpp revision 3bf912593301152b65accb9d9c37a95172f1df5a
13b172f1c018a093d0dae3105324d782103fcda6dDan 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/Analysis/DebugInfo.h"
5183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng#include "llvm/Target/TargetData.h"
52b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman#include "llvm/Target/TargetInstrInfo.h"
5383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng#include "llvm/Target/TargetLowering.h"
54bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman#include "llvm/Target/TargetMachine.h"
55ba5be5c07bb19dcf484e3aa40cd139dd07c10407Dan Gohman#include "llvm/Support/ErrorHandling.h"
5666336edf823f8d64d77dc5ab2bbefc21ef82f6ecDan Gohman#include "FunctionLoweringInfo.h"
57b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanusing namespace llvm;
58b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
59a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohmanbool FastISel::hasTrivialKill(const Value *V) const {
607f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman  // Don't consider constants or arguments to have trivial kills.
61a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  const Instruction *I = dyn_cast<Instruction>(V);
627f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman  if (!I)
637f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman    return false;
647f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman
657f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman  // No-op casts are trivially coalesced by fast-isel.
667f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman  if (const CastInst *Cast = dyn_cast<CastInst>(I))
677f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman    if (Cast->isNoopCast(TD.getIntPtrType(Cast->getContext())) &&
687f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman        !hasTrivialKill(Cast->getOperand(0)))
697f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman      return false;
707f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman
717f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman  // Only instructions with a single use in the same basic block are considered
727f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman  // to have trivial kills.
737f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman  return I->hasOneUse() &&
747f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman         !(I->getOpcode() == Instruction::BitCast ||
757f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman           I->getOpcode() == Instruction::PtrToInt ||
767f0d69555caab5f5468ddf74b5840f59c4c6fd83Dan Gohman           I->getOpcode() == Instruction::IntToPtr) &&
77e1308d86ed2402483aa9d056af531fd773e1121fDan Gohman         cast<Instruction>(I->use_begin())->getParent() == I->getParent();
78a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman}
79a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
8046510a73e977273ec67747eb34cbdb43f815e451Dan Gohmanunsigned FastISel::getRegForValue(const Value *V) {
81e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT RealVT = TLI.getValueType(V->getType(), /*AllowUnknown=*/true);
824fd552880c9f42f117bd79929ea0179f99bd6bb7Dan Gohman  // Don't handle non-simple values in FastISel.
834fd552880c9f42f117bd79929ea0179f99bd6bb7Dan Gohman  if (!RealVT.isSimple())
844fd552880c9f42f117bd79929ea0179f99bd6bb7Dan Gohman    return 0;
85821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman
86c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // Ignore illegal types. We must do this before looking up the value
87c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // in ValueMap because Arguments are given virtual registers regardless
88c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // of whether FastISel can handle them.
89825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  MVT VT = RealVT.getSimpleVT();
90821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman  if (!TLI.isTypeLegal(VT)) {
91825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    // Promote MVT::i1 to a legal type though, because it's common and easy.
92825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (VT == MVT::i1)
9323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson      VT = TLI.getTypeToTransformTo(V->getContext(), VT).getSimpleVT();
94821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman    else
95821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman      return 0;
96821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman  }
97821164875706dd28e48c6cc3cea5c8ffa6e658d1Dan Gohman
98c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // Look up the value to see if we already have a register for it. We
99c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // cache values defined by Instructions across blocks, and other values
100c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // only locally. This is because Instructions already have the SSA
1015c9cf19d1e7d044e786f03331bc3f004966a69d2Dan Gohman  // def-dominates-use requirement enforced.
102eddc114a66b4520369e0f86ab1d3132dac2449b2Dan Gohman  DenseMap<const Value *, unsigned>::iterator I = ValueMap.find(V);
103eddc114a66b4520369e0f86ab1d3132dac2449b2Dan Gohman  if (I != ValueMap.end())
104eddc114a66b4520369e0f86ab1d3132dac2449b2Dan Gohman    return I->second;
105c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  unsigned Reg = LocalValueMap[V];
106c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  if (Reg != 0)
107c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman    return Reg;
108c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman
10997c94b8fc630b55e17a37481910810639e28d086Dan Gohman  // In bottom-up mode, just create the virtual register which will be used
11097c94b8fc630b55e17a37481910810639e28d086Dan Gohman  // to hold the value. It will be materialized later.
11197c94b8fc630b55e17a37481910810639e28d086Dan Gohman  if (IsBottomUp) {
11297c94b8fc630b55e17a37481910810639e28d086Dan Gohman    Reg = createResultReg(TLI.getRegClassFor(VT));
11397c94b8fc630b55e17a37481910810639e28d086Dan Gohman    if (isa<Instruction>(V))
11497c94b8fc630b55e17a37481910810639e28d086Dan Gohman      ValueMap[V] = Reg;
11597c94b8fc630b55e17a37481910810639e28d086Dan Gohman    else
11697c94b8fc630b55e17a37481910810639e28d086Dan Gohman      LocalValueMap[V] = Reg;
11797c94b8fc630b55e17a37481910810639e28d086Dan Gohman    return Reg;
11897c94b8fc630b55e17a37481910810639e28d086Dan Gohman  }
11997c94b8fc630b55e17a37481910810639e28d086Dan Gohman
1201fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman  return materializeRegForValue(V, VT);
1211fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman}
1221fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman
1231fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman/// materializeRegForValue - Helper for getRegForVale. This function is
1241fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman/// called when the value isn't already available in a register and must
1251fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman/// be materialized with new instructions.
1261fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohmanunsigned FastISel::materializeRegForValue(const Value *V, MVT VT) {
1271fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman  unsigned Reg = 0;
1281fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman
12946510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
1302ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman    if (CI->getValue().getActiveBits() <= 64)
1312ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman      Reg = FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
1320586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  } else if (isa<AllocaInst>(V)) {
1332ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman    Reg = TargetMaterializeAlloca(cast<AllocaInst>(V));
134205d92589bc8c59d4bba9ddf89e0eb3c4d548cdaDan Gohman  } else if (isa<ConstantPointerNull>(V)) {
1351e9e8c3bd5ac018296bddb21a2acb8c643303b39Dan Gohman    // Translate this as an integer zero so that it can be
1361e9e8c3bd5ac018296bddb21a2acb8c643303b39Dan Gohman    // local-CSE'd with actual integer zeros.
1371d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson    Reg =
1381d0be15f89cb5056e20e2d24faa8d6afb1573bcaOwen Anderson      getRegForValue(Constant::getNullValue(TD.getIntPtrType(V->getContext())));
13946510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
1404183e31978146ea529a87a2fc47b96aeb6cbe000Dan Gohman    // Try to emit the constant directly.
141104e4ce1629ea84736691bd1ee7867bdf90e8a2eDan Gohman    Reg = FastEmit_f(VT, VT, ISD::ConstantFP, CF);
142ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
143ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    if (!Reg) {
1444183e31978146ea529a87a2fc47b96aeb6cbe000Dan Gohman      // Try to emit the constant by using an integer constant with a cast.
145ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      const APFloat &Flt = CF->getValueAPF();
146e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson      EVT IntVT = TLI.getPointerTy();
147ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
148ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      uint64_t x[2];
149ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      uint32_t IntBitWidth = IntVT.getSizeInBits();
15023a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen      bool isExact;
15123a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen      (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
15223a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen                                APFloat::rmTowardZero, &isExact);
15323a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen      if (isExact) {
1542ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman        APInt IntVal(IntBitWidth, 2, x);
155ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
156e922c0201916e0b980ab3cfe91e1413e68d55647Owen Anderson        unsigned IntegerReg =
157eed707b1e6097aac2bb6b3d47271f6300ace7f2eOwen Anderson          getRegForValue(ConstantInt::get(V->getContext(), IntVal));
1582ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman        if (IntegerReg != 0)
159a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman          Reg = FastEmit_r(IntVT.getSimpleVT(), VT, ISD::SINT_TO_FP,
160a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                           IntegerReg, /*Kill=*/false);
1612ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman      }
162ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    }
16346510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  } else if (const Operator *Op = dyn_cast<Operator>(V)) {
16432acbc1e50defdb7e27a4e8274f78988cf302c14Dan Gohman    if (!SelectOperator(Op, Op->getOpcode())) return 0;
16532acbc1e50defdb7e27a4e8274f78988cf302c14Dan Gohman    Reg = LocalValueMap[Op];
166205d92589bc8c59d4bba9ddf89e0eb3c4d548cdaDan Gohman  } else if (isa<UndefValue>(V)) {
167104e4ce1629ea84736691bd1ee7867bdf90e8a2eDan Gohman    Reg = createResultReg(TLI.getRegClassFor(VT));
168518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner    BuildMI(MBB, DL, TII.get(TargetOpcode::IMPLICIT_DEF), Reg);
169ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  }
170d5d81a457b5ff758b3fcc527af38827490bc68a5Owen Anderson
171dceffe66b9e73ce372ea11c0fc6975504eb8c31dDan Gohman  // If target-independent code couldn't handle the value, give target-specific
172dceffe66b9e73ce372ea11c0fc6975504eb8c31dDan Gohman  // code a try.
1736e6074508c2f781c3e52dfe0e301cb1c7f395a91Owen Anderson  if (!Reg && isa<Constant>(V))
1742ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman    Reg = TargetMaterializeConstant(cast<Constant>(V));
1756e6074508c2f781c3e52dfe0e301cb1c7f395a91Owen Anderson
1762ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman  // Don't cache constant materializations in the general ValueMap.
1772ff7fd146159d97abe94391a33b4385abb06bbb0Dan Gohman  // To do so would require tracking what uses they dominate.
178dceffe66b9e73ce372ea11c0fc6975504eb8c31dDan Gohman  if (Reg != 0)
179dceffe66b9e73ce372ea11c0fc6975504eb8c31dDan Gohman    LocalValueMap[V] = Reg;
180104e4ce1629ea84736691bd1ee7867bdf90e8a2eDan Gohman  return Reg;
181ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman}
182ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
18346510a73e977273ec67747eb34cbdb43f815e451Dan Gohmanunsigned FastISel::lookUpRegForValue(const Value *V) {
18459fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  // Look up the value to see if we already have a register for it. We
18559fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  // cache values defined by Instructions across blocks, and other values
18659fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  // only locally. This is because Instructions already have the SSA
1871fdc614bee2a324fcc210d1e46d9b6fca3ca324bDan Gohman  // def-dominates-use requirement enforced.
18859fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  if (ValueMap.count(V))
18959fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng    return ValueMap[V];
19059fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng  return LocalValueMap[V];
19159fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng}
19259fbc80f6b3b5c71dfb84149f589625f7ed510e3Evan Cheng
193cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// UpdateValueMap - Update the value map to include the new mapping for this
194cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// instruction, or insert an extra copy to get the result in a previous
195cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// determined register.
196cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// NOTE: This is only necessary because we might select a block that uses
197cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// a value before we select the block that defines the value.  It might be
198cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson/// possible to fix this by selecting blocks in reverse postorder.
19946510a73e977273ec67747eb34cbdb43f815e451Dan Gohmanunsigned FastISel::UpdateValueMap(const Value *I, unsigned Reg) {
20040b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  if (!isa<Instruction>(I)) {
20140b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman    LocalValueMap[I] = Reg;
202c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner    return Reg;
203c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner  }
204c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner
205c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner  unsigned &AssignedReg = ValueMap[I];
206c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner  if (AssignedReg == 0)
207c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner    AssignedReg = Reg;
20836e3946ac2f30a0bda66538ef2b974b1c8fbdc97Chris Lattner  else if (Reg != AssignedReg) {
209c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner    const TargetRegisterClass *RegClass = MRI.getRegClass(Reg);
210c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner    TII.copyRegToReg(*MBB, MBB->end(), AssignedReg,
21134dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                     Reg, RegClass, RegClass, DL);
21240b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  }
213c5040ab6065d5c569a1af0848b6e672b22b174b7Chris Lattner  return AssignedReg;
214cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson}
215cc54e76cc67bbc9badc024ab29053602769bd255Owen Anderson
216a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohmanstd::pair<unsigned, bool> FastISel::getRegForGEPIndex(const Value *Idx) {
217c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  unsigned IdxN = getRegForValue(Idx);
218c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  if (IdxN == 0)
219c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman    // Unhandled operand. Halt "fast" selection and bail.
220a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    return std::pair<unsigned, bool>(0, false);
221a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
222a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  bool IdxNIsKill = hasTrivialKill(Idx);
223c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman
224c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman  // If the index is smaller or larger than intptr_t, truncate or extend it.
225766b5efd99c01e26f00f22d81b57d1385b3d2ab0Owen Anderson  MVT PtrVT = TLI.getPointerTy();
226e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
227a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  if (IdxVT.bitsLT(PtrVT)) {
228a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::SIGN_EXTEND,
229a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                      IdxN, IdxNIsKill);
230a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    IdxNIsKill = true;
231a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  }
232a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  else if (IdxVT.bitsGT(PtrVT)) {
233a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    IdxN = FastEmit_r(IdxVT.getSimpleVT(), PtrVT, ISD::TRUNCATE,
234a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                      IdxN, IdxNIsKill);
235a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    IdxNIsKill = true;
236a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  }
237a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
238c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman}
239c8a1a3c426209e9c7b35e279e1578a89edc40af6Dan Gohman
240bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// SelectBinaryOp - Select and emit code for a binary operator instruction,
241bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman/// which has an opcode which directly corresponds to the given ISD opcode.
242bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman///
24346510a73e977273ec67747eb34cbdb43f815e451Dan Gohmanbool FastISel::SelectBinaryOp(const User *I, unsigned ISDOpcode) {
244e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT VT = EVT::getEVT(I->getType(), /*HandleUnknown=*/true);
245825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (VT == MVT::Other || !VT.isSimple())
246d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    // Unhandled type. Halt "fast" selection and bail.
247d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    return false;
248638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman
249b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // We only handle legal types. For example, on x86-32 the instruction
250b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // selector contains all of the 64-bit instructions from x86-64,
251b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // under the assumption that i64 won't be used if the target doesn't
252b71fea248fd7cf9ab2c5737997a3dc5682948dc4Dan Gohman  // support it.
253638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman  if (!TLI.isTypeLegal(VT)) {
254825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    // MVT::i1 is special. Allow AND, OR, or XOR because they
255638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman    // don't require additional zeroing, which makes them easy.
256825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (VT == MVT::i1 &&
2575dd9c2e9aea7294c184609aff7f2fe82eaea4eb0Dan Gohman        (ISDOpcode == ISD::AND || ISDOpcode == ISD::OR ||
2585dd9c2e9aea7294c184609aff7f2fe82eaea4eb0Dan Gohman         ISDOpcode == ISD::XOR))
25923b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson      VT = TLI.getTypeToTransformTo(I->getContext(), VT);
260638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman    else
261638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman      return false;
262638c6830c6d0d6871065d2b00178ee4aa7d4d044Dan Gohman  }
263d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
2643df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned Op0 = getRegForValue(I->getOperand(0));
265d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (Op0 == 0)
266a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman    // Unhandled operand. Halt "fast" selection and bail.
267a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman    return false;
268a7f2dff98e68ed8b2ac32f953768c04f26b52beaDan Gohman
269a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  bool Op0IsKill = hasTrivialKill(I->getOperand(0));
270a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
271d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  // Check if the second operand is a constant and handle it appropriately.
272d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(1))) {
273ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    unsigned ResultReg = FastEmit_ri(VT.getSimpleVT(), VT.getSimpleVT(),
274a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                     ISDOpcode, Op0, Op0IsKill,
275a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                     CI->getZExtValue());
276ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    if (ResultReg != 0) {
277ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      // We successfully emitted code for the given LLVM Instruction.
2783df24e667f04a7003342b534310919abc9c87418Dan Gohman      UpdateValueMap(I, ResultReg);
279ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      return true;
280ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    }
281d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  }
282d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
28310df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  // Check if the second operand is a constant float.
28410df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  if (ConstantFP *CF = dyn_cast<ConstantFP>(I->getOperand(1))) {
285ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    unsigned ResultReg = FastEmit_rf(VT.getSimpleVT(), VT.getSimpleVT(),
286a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                     ISDOpcode, Op0, Op0IsKill, CF);
287ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    if (ResultReg != 0) {
288ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      // We successfully emitted code for the given LLVM Instruction.
2893df24e667f04a7003342b534310919abc9c87418Dan Gohman      UpdateValueMap(I, ResultReg);
290ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      return true;
291ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    }
29210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  }
29310df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
2943df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned Op1 = getRegForValue(I->getOperand(1));
295d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (Op1 == 0)
296d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    // Unhandled operand. Halt "fast" selection and bail.
297bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    return false;
298bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
299a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  bool Op1IsKill = hasTrivialKill(I->getOperand(1));
300a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
301ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // Now we have both operands in registers. Emit the instruction.
3020f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson  unsigned ResultReg = FastEmit_rr(VT.getSimpleVT(), VT.getSimpleVT(),
303a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   ISDOpcode,
304a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   Op0, Op0IsKill,
305a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   Op1, Op1IsKill);
306bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  if (ResultReg == 0)
307bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // Target-specific code wasn't able to find a machine opcode for
308bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    // the given ISD opcode and type. Halt "fast" selection and bail.
309bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman    return false;
310bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
3118014e865800cc911697a4c0c42f077df9fcc9805Dan Gohman  // We successfully emitted code for the given LLVM Instruction.
3123df24e667f04a7003342b534310919abc9c87418Dan Gohman  UpdateValueMap(I, ResultReg);
313bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman  return true;
314bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman}
315bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
31646510a73e977273ec67747eb34cbdb43f815e451Dan Gohmanbool FastISel::SelectGetElementPtr(const User *I) {
3173df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned N = getRegForValue(I->getOperand(0));
31883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (N == 0)
31983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    // Unhandled operand. Halt "fast" selection and bail.
32083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    return false;
32183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
322a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  bool NIsKill = hasTrivialKill(I->getOperand(0));
323a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
32483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  const Type *Ty = I->getOperand(0)->getType();
325825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  MVT VT = TLI.getPointerTy();
32646510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  for (GetElementPtrInst::const_op_iterator OI = I->op_begin()+1,
32746510a73e977273ec67747eb34cbdb43f815e451Dan Gohman       E = I->op_end(); OI != E; ++OI) {
32846510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const Value *Idx = *OI;
32983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
33083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
33183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (Field) {
33283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // N = N + Offset
33383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        uint64_t Offs = TD.getStructLayout(StTy)->getElementOffset(Field);
33483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // FIXME: This can be optimized by combining the add with a
33583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // subsequent one.
336a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman        N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
33783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (N == 0)
33883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          // Unhandled operand. Halt "fast" selection and bail.
33983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          return false;
340a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman        NIsKill = true;
34183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      }
34283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      Ty = StTy->getElementType(Field);
34383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    } else {
34483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      Ty = cast<SequentialType>(Ty)->getElementType();
34583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
34683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // If this is a constant subscript, handle it quickly.
34746510a73e977273ec67747eb34cbdb43f815e451Dan Gohman      if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
34883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (CI->getZExtValue() == 0) continue;
34983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        uint64_t Offs =
350777d2306b36816a53bc1ae1244c0dc7d998ae691Duncan Sands          TD.getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
351a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman        N = FastEmit_ri_(VT, ISD::ADD, N, NIsKill, Offs, VT);
35283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        if (N == 0)
35383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          // Unhandled operand. Halt "fast" selection and bail.
35483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng          return false;
355a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman        NIsKill = true;
35683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        continue;
35783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      }
35883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
35983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      // N = N + Idx * ElementSize;
360777d2306b36816a53bc1ae1244c0dc7d998ae691Duncan Sands      uint64_t ElementSize = TD.getTypeAllocSize(Ty);
361a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
362a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      unsigned IdxN = Pair.first;
363a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      bool IdxNIsKill = Pair.second;
36483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (IdxN == 0)
36583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
36683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
36783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
36880bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman      if (ElementSize != 1) {
369a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman        IdxN = FastEmit_ri_(VT, ISD::MUL, IdxN, IdxNIsKill, ElementSize, VT);
37080bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman        if (IdxN == 0)
37180bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman          // Unhandled operand. Halt "fast" selection and bail.
37280bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman          return false;
373a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman        IdxNIsKill = true;
37480bc6e2243b7ae99da42bf2e61df4ebccf8d8821Dan Gohman      }
375a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      N = FastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
37683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng      if (N == 0)
37783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        // Unhandled operand. Halt "fast" selection and bail.
37883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng        return false;
37983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    }
38083785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  }
38183785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
38283785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  // We successfully emitted code for the given LLVM Instruction.
3833df24e667f04a7003342b534310919abc9c87418Dan Gohman  UpdateValueMap(I, N);
38483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return true;
385bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman}
386bdedd4477331b3b0d28d74658baf05f675f2d195Dan Gohman
38746510a73e977273ec67747eb34cbdb43f815e451Dan Gohmanbool FastISel::SelectCall(const User *I) {
38846510a73e977273ec67747eb34cbdb43f815e451Dan Gohman  const Function *F = cast<CallInst>(I)->getCalledFunction();
38933134c4a75558288d663267c8991f6bd37a530afDan Gohman  if (!F) return false;
39033134c4a75558288d663267c8991f6bd37a530afDan Gohman
3914183e31978146ea529a87a2fc47b96aeb6cbe000Dan Gohman  // Handle selected intrinsic function calls.
39233134c4a75558288d663267c8991f6bd37a530afDan Gohman  unsigned IID = F->getIntrinsicID();
39333134c4a75558288d663267c8991f6bd37a530afDan Gohman  switch (IID) {
39433134c4a75558288d663267c8991f6bd37a530afDan Gohman  default: break;
39592c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling  case Intrinsic::dbg_declare: {
39646510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const DbgDeclareInst *DI = cast<DbgDeclareInst>(I);
39702f0dbd97a9bc01528aa12c2f260d928683ab411Devang Patel    if (!DIVariable(DI->getVariable()).Verify() ||
398ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        !MF.getMMI().hasDebugInfo())
3997e1e31f467d87c834d8baf673929865907901313Devang Patel      return true;
40092c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling
40146510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const Value *Address = DI->getAddress();
402dc9185657593b5c1db86cb95a04a68fc09194993Dale Johannesen    if (!Address)
403dc9185657593b5c1db86cb95a04a68fc09194993Dale Johannesen      return true;
404343b42e428079363ab09828734b2debfd7dbdc9eDale Johannesen    if (isa<UndefValue>(Address))
405343b42e428079363ab09828734b2debfd7dbdc9eDale Johannesen      return true;
40646510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4077e1e31f467d87c834d8baf673929865907901313Devang Patel    // Don't handle byval struct arguments or VLAs, for example.
4087dc7840850eb4a61da80c846be85308734a47bf5Dale Johannesen    // Note that if we have a byval struct argument, fast ISel is turned off;
4097dc7840850eb4a61da80c846be85308734a47bf5Dale Johannesen    // those are handled in SelectionDAGBuilder.
41054fc4d6a48b32772b1a114a43e20de521257899bDevang Patel    if (AI) {
41154fc4d6a48b32772b1a114a43e20de521257899bDevang Patel      DenseMap<const AllocaInst*, int>::iterator SI =
41254fc4d6a48b32772b1a114a43e20de521257899bDevang Patel        StaticAllocaMap.find(AI);
41354fc4d6a48b32772b1a114a43e20de521257899bDevang Patel      if (SI == StaticAllocaMap.end()) break; // VLAs.
41454fc4d6a48b32772b1a114a43e20de521257899bDevang Patel      int FI = SI->second;
41554fc4d6a48b32772b1a114a43e20de521257899bDevang Patel      if (!DI->getDebugLoc().isUnknown())
41654fc4d6a48b32772b1a114a43e20de521257899bDevang Patel        MF.getMMI().setVariableDbgInfo(DI->getVariable(), FI, DI->getDebugLoc());
41754fc4d6a48b32772b1a114a43e20de521257899bDevang Patel    } else
41854fc4d6a48b32772b1a114a43e20de521257899bDevang Patel      // Building the map above is target independent.  Generating DBG_VALUE
41954fc4d6a48b32772b1a114a43e20de521257899bDevang Patel      // inline is target dependent; do this now.
42054fc4d6a48b32772b1a114a43e20de521257899bDevang Patel      (void)TargetSelectInstruction(cast<Instruction>(I));
42133134c4a75558288d663267c8991f6bd37a530afDan Gohman    return true;
42292c1e126473dfa93eeb4c9a124af4fedb40f0d5bBill Wendling  }
42345df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen  case Intrinsic::dbg_value: {
424343b42e428079363ab09828734b2debfd7dbdc9eDale Johannesen    // This form of DBG_VALUE is target-independent.
42546510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const DbgValueInst *DI = cast<DbgValueInst>(I);
42645df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen    const TargetInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
42746510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const Value *V = DI->getValue();
42845df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen    if (!V) {
42945df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      // Currently the optimizer can produce this; insert an undef to
43045df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      // help debugging.  Probably the optimizer should not do this.
43145df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
43245df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen                                     addMetadata(DI->getVariable());
43346510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
43445df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      BuildMI(MBB, DL, II).addImm(CI->getZExtValue()).addImm(DI->getOffset()).
43545df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen                                     addMetadata(DI->getVariable());
43646510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    } else if (const ConstantFP *CF = dyn_cast<ConstantFP>(V)) {
43745df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      BuildMI(MBB, DL, II).addFPImm(CF).addImm(DI->getOffset()).
43845df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen                                     addMetadata(DI->getVariable());
43945df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen    } else if (unsigned Reg = lookUpRegForValue(V)) {
44045df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      BuildMI(MBB, DL, II).addReg(Reg, RegState::Debug).addImm(DI->getOffset()).
44145df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen                                     addMetadata(DI->getVariable());
44245df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen    } else {
44345df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      // We can't yet handle anything else here because it would require
44445df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      // generating code, thus altering codegen because of debug info.
44545df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      // Insert an undef so we can see what we dropped.
44645df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen      BuildMI(MBB, DL, II).addReg(0U).addImm(DI->getOffset()).
44745df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen                                     addMetadata(DI->getVariable());
44845df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen    }
44945df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen    return true;
45045df7616528e3d101c5b2938bd4d865321205d33Dale Johannesen  }
451dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman  case Intrinsic::eh_exception: {
452e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT VT = TLI.getValueType(I->getType());
453dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    switch (TLI.getOperationAction(ISD::EXCEPTIONADDR, VT)) {
454dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    default: break;
455dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    case TargetLowering::Expand: {
456b0f1e1780c736c62fb99e5824825d2a60a53b53bDuncan Sands      assert(MBB->isLandingPad() && "Call to eh.exception not in landing pad!");
457dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      unsigned Reg = TLI.getExceptionAddressRegister();
458dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
459dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      unsigned ResultReg = createResultReg(RC);
460dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
46134dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                           Reg, RC, RC, DL);
462dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      assert(InsertedCopy && "Can't copy address registers!");
46324ac408ce891321d1a5d62beaf3487efce6f2b22Evan Cheng      InsertedCopy = InsertedCopy;
464dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      UpdateValueMap(I, ResultReg);
465dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      return true;
466dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
467dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
468dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    break;
469dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman  }
470b01bbdcc1af27bd90b552bb1b62b48916e0d4be3Duncan Sands  case Intrinsic::eh_selector: {
471e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT VT = TLI.getValueType(I->getType());
472dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    switch (TLI.getOperationAction(ISD::EHSELECTION, VT)) {
473dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    default: break;
474dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    case TargetLowering::Expand: {
475ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      if (MBB->isLandingPad())
476ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        AddCatchInfo(*cast<CallInst>(I), &MF.getMMI(), MBB);
477ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      else {
478dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#ifndef NDEBUG
479ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        CatchInfoLost.insert(cast<CallInst>(I));
480dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#endif
481ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        // FIXME: Mark exception selector register as live in.  Hack for PR1508.
482dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman        unsigned Reg = TLI.getExceptionSelectorRegister();
483ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        if (Reg) MBB->addLiveIn(Reg);
484dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      }
485ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner
486ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      unsigned Reg = TLI.getExceptionSelectorRegister();
487ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      EVT SrcVT = TLI.getPointerTy();
488ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      const TargetRegisterClass *RC = TLI.getRegClassFor(SrcVT);
489ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      unsigned ResultReg = createResultReg(RC);
490ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg, Reg,
49134dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                           RC, RC, DL);
492ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      assert(InsertedCopy && "Can't copy address registers!");
493ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      InsertedCopy = InsertedCopy;
494ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner
495a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      bool ResultRegIsKill = hasTrivialKill(I);
496a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
497ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      // Cast the register to the type of the selector.
498ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      if (SrcVT.bitsGT(MVT::i32))
499ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32, ISD::TRUNCATE,
500a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               ResultReg, ResultRegIsKill);
501ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      else if (SrcVT.bitsLT(MVT::i32))
502ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        ResultReg = FastEmit_r(SrcVT.getSimpleVT(), MVT::i32,
503a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               ISD::SIGN_EXTEND, ResultReg, ResultRegIsKill);
504ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      if (ResultReg == 0)
505ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        // Unhandled operand. Halt "fast" selection and bail.
506ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner        return false;
507ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner
508ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner      UpdateValueMap(I, ResultReg);
509ed3a8067a60ecf2c215e77327a57904c3ebc3355Chris Lattner
510dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman      return true;
511dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
512dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    }
513dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    break;
514dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman  }
51533134c4a75558288d663267c8991f6bd37a530afDan Gohman  }
5164183e31978146ea529a87a2fc47b96aeb6cbe000Dan Gohman
5174183e31978146ea529a87a2fc47b96aeb6cbe000Dan Gohman  // An arbitrary call. Bail.
51833134c4a75558288d663267c8991f6bd37a530afDan Gohman  return false;
51933134c4a75558288d663267c8991f6bd37a530afDan Gohman}
52033134c4a75558288d663267c8991f6bd37a530afDan Gohman
52146510a73e977273ec67747eb34cbdb43f815e451Dan Gohmanbool FastISel::SelectCast(const User *I, unsigned Opcode) {
522e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
523e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT DstVT = TLI.getValueType(I->getType());
524d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
525825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
526825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      DstVT == MVT::Other || !DstVT.isSimple())
527d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    // Unhandled type. Halt "fast" selection and bail.
528d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
529d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
530474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman  // Check if the destination type is legal. Or as a special case,
531474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman  // it may be i1 if we're doing a truncate because that's
532474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman  // easy and somewhat common.
533474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman  if (!TLI.isTypeLegal(DstVT))
534825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (DstVT != MVT::i1 || Opcode != ISD::TRUNCATE)
535474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman      // Unhandled type. Halt "fast" selection and bail.
536474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman      return false;
537474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman
53891b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman  // Check if the source operand is legal. Or as a special case,
53991b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman  // it may be i1 if we're doing zero-extension because that's
540474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman  // easy and somewhat common.
541474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman  if (!TLI.isTypeLegal(SrcVT))
542825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson    if (SrcVT != MVT::i1 || Opcode != ISD::ZERO_EXTEND)
54391b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman      // Unhandled type. Halt "fast" selection and bail.
54491b6f97ce4273fee5516692e3f27cd76d67986fcDan Gohman      return false;
545474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman
5463df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned InputReg = getRegForValue(I->getOperand(0));
547d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  if (!InputReg)
548d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    // Unhandled operand.  Halt "fast" selection and bail.
549d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
55014ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman
551a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  bool InputRegIsKill = hasTrivialKill(I->getOperand(0));
552a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
55314ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman  // If the operand is i1, arrange for the high bits in the register to be zero.
554825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (SrcVT == MVT::i1) {
55523b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson   SrcVT = TLI.getTypeToTransformTo(I->getContext(), SrcVT);
556a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman   InputReg = FastEmitZExtFromI1(SrcVT.getSimpleVT(), InputReg, InputRegIsKill);
55714ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman   if (!InputReg)
55814ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman     return false;
559a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman   InputRegIsKill = true;
56014ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman  }
561474d3b3f40e117a66946e9fb9d2016b4c05caef0Dan Gohman  // If the result is i1, truncate to the target's type for i1 first.
562825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (DstVT == MVT::i1)
56323b9b19b1a5a00faa9fce0788155c7dbfd00bfb1Owen Anderson    DstVT = TLI.getTypeToTransformTo(I->getContext(), DstVT);
56414ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman
565d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  unsigned ResultReg = FastEmit_r(SrcVT.getSimpleVT(),
566d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson                                  DstVT.getSimpleVT(),
567d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson                                  Opcode,
568a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                  InputReg, InputRegIsKill);
569d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  if (!ResultReg)
570d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
571d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
5723df24e667f04a7003342b534310919abc9c87418Dan Gohman  UpdateValueMap(I, ResultReg);
573d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  return true;
574d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson}
575d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
57646510a73e977273ec67747eb34cbdb43f815e451Dan Gohmanbool FastISel::SelectBitCast(const User *I) {
577ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // If the bitcast doesn't change the type, just use the operand value.
578ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (I->getType() == I->getOperand(0)->getType()) {
5793df24e667f04a7003342b534310919abc9c87418Dan Gohman    unsigned Reg = getRegForValue(I->getOperand(0));
580a318dabc0edbcc7a2b54d99b026a093361ec14fcDan Gohman    if (Reg == 0)
581a318dabc0edbcc7a2b54d99b026a093361ec14fcDan Gohman      return false;
5823df24e667f04a7003342b534310919abc9c87418Dan Gohman    UpdateValueMap(I, Reg);
583ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    return true;
584ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  }
585ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
586ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // Bitcasts of other values become reg-reg copies or BIT_CONVERT operators.
587e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
588e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson  EVT DstVT = TLI.getValueType(I->getType());
589d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
590825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson  if (SrcVT == MVT::Other || !SrcVT.isSimple() ||
591825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Anderson      DstVT == MVT::Other || !DstVT.isSimple() ||
592d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson      !TLI.isTypeLegal(SrcVT) || !TLI.isTypeLegal(DstVT))
593d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    // Unhandled type. Halt "fast" selection and bail.
594d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
595d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
5963df24e667f04a7003342b534310919abc9c87418Dan Gohman  unsigned Op0 = getRegForValue(I->getOperand(0));
597ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (Op0 == 0)
598ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    // Unhandled operand. Halt "fast" selection and bail.
599d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
600a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
601a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  bool Op0IsKill = hasTrivialKill(I->getOperand(0));
602d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
603ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // First, try to perform the bitcast by inserting a reg-reg copy.
604ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  unsigned ResultReg = 0;
605ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (SrcVT.getSimpleVT() == DstVT.getSimpleVT()) {
606ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    TargetRegisterClass* SrcClass = TLI.getRegClassFor(SrcVT);
607ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    TargetRegisterClass* DstClass = TLI.getRegClassFor(DstVT);
608ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    ResultReg = createResultReg(DstClass);
609ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
610ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
61134dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                         Op0, DstClass, SrcClass, DL);
612ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    if (!InsertedCopy)
613ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman      ResultReg = 0;
614ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  }
615ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
616ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  // If the reg-reg copy failed, select a BIT_CONVERT opcode.
617ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (!ResultReg)
618ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman    ResultReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(),
619a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                           ISD::BIT_CONVERT, Op0, Op0IsKill);
620ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman
621ad368ac2b5f303050e9aaa357e2b806fae38f81bDan Gohman  if (!ResultReg)
622d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson    return false;
623d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
6243df24e667f04a7003342b534310919abc9c87418Dan Gohman  UpdateValueMap(I, ResultReg);
625d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson  return true;
626d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson}
627d0533c9998d3baf41848ba559a9b2f2c65296d14Owen Anderson
6283df24e667f04a7003342b534310919abc9c87418Dan Gohmanbool
62946510a73e977273ec67747eb34cbdb43f815e451Dan GohmanFastISel::SelectInstruction(const Instruction *I) {
630e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  // Just before the terminator instruction, insert instructions to
631e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  // feed PHI nodes in successor blocks.
632e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman  if (isa<TerminatorInst>(I))
633e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman    if (!HandlePHINodesInSuccessorBlocks(I->getParent()))
634e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman      return false;
635e8c92dd439581bec7e3516cbdbea74e2e60fe7f0Dan Gohman
6368ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman  DL = I->getDebugLoc();
6378ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman
6386e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  // First, try doing target-independent selection.
6398ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman  if (SelectOperator(I, I->getOpcode())) {
6408ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman    DL = DebugLoc();
6416e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman    return true;
6428ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman  }
6436e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman
6446e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  // Next, try calling the target to attempt to handle the instruction.
6458ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman  if (TargetSelectInstruction(I)) {
6468ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman    DL = DebugLoc();
6476e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman    return true;
6488ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman  }
6496e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman
6508ba3aa7f9c5048c31172788f98ad2b90ffad565aDan Gohman  DL = DebugLoc();
6516e3ff375474c4fd78feb0b8463eb273a23cb4404Dan Gohman  return false;
65240b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman}
65340b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman
654d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman/// FastEmitBranch - Emit an unconditional branch to the given block,
655d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman/// unless it is the immediate (fall-through) successor, and update
656d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman/// the CFG.
657d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohmanvoid
6583bf912593301152b65accb9d9c37a95172f1df5aStuart HastingsFastISel::FastEmitBranch(MachineBasicBlock *MSucc, DebugLoc DL) {
659d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  if (MBB->isLayoutSuccessor(MSucc)) {
660d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman    // The unconditional fall-through case, which needs no instructions.
661d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  } else {
662d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman    // The unconditional branch case.
6633bf912593301152b65accb9d9c37a95172f1df5aStuart Hastings    TII.InsertBranch(*MBB, MSucc, NULL, SmallVector<MachineOperand, 0>(), DL);
664d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  }
665d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman  MBB->addSuccessor(MSucc);
666d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman}
667d98d6203e429b2d7208b6687931e9079e85e95ecDan Gohman
6683d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman/// SelectFNeg - Emit an FNeg operation.
6693d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman///
6703d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohmanbool
67146510a73e977273ec67747eb34cbdb43f815e451Dan GohmanFastISel::SelectFNeg(const User *I) {
6723d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman  unsigned OpReg = getRegForValue(BinaryOperator::getFNegArgument(I));
6733d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman  if (OpReg == 0) return false;
6743d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman
675a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  bool OpRegIsKill = hasTrivialKill(I);
676a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman
6774a215a13c3035a8818254b61267be77def4a2a37Dan Gohman  // If the target has ISD::FNEG, use it.
6784a215a13c3035a8818254b61267be77def4a2a37Dan Gohman  EVT VT = TLI.getValueType(I->getType());
6794a215a13c3035a8818254b61267be77def4a2a37Dan Gohman  unsigned ResultReg = FastEmit_r(VT.getSimpleVT(), VT.getSimpleVT(),
680a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                  ISD::FNEG, OpReg, OpRegIsKill);
6814a215a13c3035a8818254b61267be77def4a2a37Dan Gohman  if (ResultReg != 0) {
6824a215a13c3035a8818254b61267be77def4a2a37Dan Gohman    UpdateValueMap(I, ResultReg);
6834a215a13c3035a8818254b61267be77def4a2a37Dan Gohman    return true;
6844a215a13c3035a8818254b61267be77def4a2a37Dan Gohman  }
6854a215a13c3035a8818254b61267be77def4a2a37Dan Gohman
6865e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman  // Bitcast the value to integer, twiddle the sign bit with xor,
6875e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman  // and then bitcast it back to floating-point.
6883d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman  if (VT.getSizeInBits() > 64) return false;
6895e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman  EVT IntVT = EVT::getIntegerVT(I->getContext(), VT.getSizeInBits());
6905e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman  if (!TLI.isTypeLegal(IntVT))
6915e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman    return false;
6925e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman
6935e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman  unsigned IntReg = FastEmit_r(VT.getSimpleVT(), IntVT.getSimpleVT(),
694a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               ISD::BIT_CONVERT, OpReg, OpRegIsKill);
6955e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman  if (IntReg == 0)
6965e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman    return false;
6975e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman
698a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  unsigned IntResultReg = FastEmit_ri_(IntVT.getSimpleVT(), ISD::XOR,
699a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                       IntReg, /*Kill=*/true,
7005e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman                                       UINT64_C(1) << (VT.getSizeInBits()-1),
7015e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman                                       IntVT.getSimpleVT());
7025e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman  if (IntResultReg == 0)
7035e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman    return false;
7045e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman
7055e5abb77fe9eeb92a55f119fdb721bca4508094aDan Gohman  ResultReg = FastEmit_r(IntVT.getSimpleVT(), VT.getSimpleVT(),
706a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                         ISD::BIT_CONVERT, IntResultReg, /*Kill=*/true);
7073d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman  if (ResultReg == 0)
7083d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman    return false;
7093d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman
7103d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman  UpdateValueMap(I, ResultReg);
7113d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman  return true;
7123d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman}
7133d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman
71440b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohmanbool
71546510a73e977273ec67747eb34cbdb43f815e451Dan GohmanFastISel::SelectOperator(const User *I, unsigned Opcode) {
71640b189e4e257924d90aaf63bf2e12bc7bbca961aDan Gohman  switch (Opcode) {
717ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case Instruction::Add:
718ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman    return SelectBinaryOp(I, ISD::ADD);
719ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case Instruction::FAdd:
720ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman    return SelectBinaryOp(I, ISD::FADD);
721ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case Instruction::Sub:
722ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman    return SelectBinaryOp(I, ISD::SUB);
723ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case Instruction::FSub:
7243d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman    // FNeg is currently represented in LLVM IR as a special case of FSub.
7253d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman    if (BinaryOperator::isFNeg(I))
7263d45a853db014fdddcdb79424e663dfed5eccbc7Dan Gohman      return SelectFNeg(I);
727ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman    return SelectBinaryOp(I, ISD::FSUB);
728ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case Instruction::Mul:
729ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman    return SelectBinaryOp(I, ISD::MUL);
730ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman  case Instruction::FMul:
731ae3a0be92e33bc716722aa600983fc1535acb122Dan Gohman    return SelectBinaryOp(I, ISD::FMUL);
7323df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::SDiv:
7333df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SDIV);
7343df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::UDiv:
7353df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::UDIV);
7363df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::FDiv:
7373df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::FDIV);
7383df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::SRem:
7393df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SREM);
7403df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::URem:
7413df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::UREM);
7423df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::FRem:
7433df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::FREM);
7443df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Shl:
7453df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SHL);
7463df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::LShr:
7473df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SRL);
7483df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::AShr:
7493df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::SRA);
7503df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::And:
7513df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::AND);
7523df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Or:
7533df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::OR);
7543df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Xor:
7553df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBinaryOp(I, ISD::XOR);
7563df24e667f04a7003342b534310919abc9c87418Dan Gohman
7573df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::GetElementPtr:
7583df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectGetElementPtr(I);
7593df24e667f04a7003342b534310919abc9c87418Dan Gohman
7603df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Br: {
76146510a73e977273ec67747eb34cbdb43f815e451Dan Gohman    const BranchInst *BI = cast<BranchInst>(I);
7623df24e667f04a7003342b534310919abc9c87418Dan Gohman
7633df24e667f04a7003342b534310919abc9c87418Dan Gohman    if (BI->isUnconditional()) {
76446510a73e977273ec67747eb34cbdb43f815e451Dan Gohman      const BasicBlock *LLVMSucc = BI->getSuccessor(0);
7653df24e667f04a7003342b534310919abc9c87418Dan Gohman      MachineBasicBlock *MSucc = MBBMap[LLVMSucc];
7663bf912593301152b65accb9d9c37a95172f1df5aStuart Hastings      FastEmitBranch(MSucc, BI->getDebugLoc());
7673df24e667f04a7003342b534310919abc9c87418Dan Gohman      return true;
7689d5b41624003daf259b33fc953aa471049700353Owen Anderson    }
7693df24e667f04a7003342b534310919abc9c87418Dan Gohman
7703df24e667f04a7003342b534310919abc9c87418Dan Gohman    // Conditional branches are not handed yet.
7713df24e667f04a7003342b534310919abc9c87418Dan Gohman    // Halt "fast" selection and bail.
7723df24e667f04a7003342b534310919abc9c87418Dan Gohman    return false;
773b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  }
774b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
775087c8507e592bbbede1746f07bd44b28559e3684Dan Gohman  case Instruction::Unreachable:
776087c8507e592bbbede1746f07bd44b28559e3684Dan Gohman    // Nothing to emit.
777087c8507e592bbbede1746f07bd44b28559e3684Dan Gohman    return true;
778087c8507e592bbbede1746f07bd44b28559e3684Dan Gohman
7790586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman  case Instruction::Alloca:
7800586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    // FunctionLowering has the static-sized case covered.
7810586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    if (StaticAllocaMap.count(cast<AllocaInst>(I)))
7820586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman      return true;
7830586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman
7840586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    // Dynamic-sized alloca is not handled yet.
7850586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    return false;
7863df24e667f04a7003342b534310919abc9c87418Dan Gohman
78733134c4a75558288d663267c8991f6bd37a530afDan Gohman  case Instruction::Call:
78833134c4a75558288d663267c8991f6bd37a530afDan Gohman    return SelectCall(I);
78933134c4a75558288d663267c8991f6bd37a530afDan Gohman
7903df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::BitCast:
7913df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectBitCast(I);
7923df24e667f04a7003342b534310919abc9c87418Dan Gohman
7933df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::FPToSI:
7943df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::FP_TO_SINT);
7953df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::ZExt:
7963df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::ZERO_EXTEND);
7973df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::SExt:
7983df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::SIGN_EXTEND);
7993df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::Trunc:
8003df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::TRUNCATE);
8013df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::SIToFP:
8023df24e667f04a7003342b534310919abc9c87418Dan Gohman    return SelectCast(I, ISD::SINT_TO_FP);
8033df24e667f04a7003342b534310919abc9c87418Dan Gohman
8043df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::IntToPtr: // Deliberate fall-through.
8053df24e667f04a7003342b534310919abc9c87418Dan Gohman  case Instruction::PtrToInt: {
806e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
807e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT DstVT = TLI.getValueType(I->getType());
8083df24e667f04a7003342b534310919abc9c87418Dan Gohman    if (DstVT.bitsGT(SrcVT))
8093df24e667f04a7003342b534310919abc9c87418Dan Gohman      return SelectCast(I, ISD::ZERO_EXTEND);
8103df24e667f04a7003342b534310919abc9c87418Dan Gohman    if (DstVT.bitsLT(SrcVT))
8113df24e667f04a7003342b534310919abc9c87418Dan Gohman      return SelectCast(I, ISD::TRUNCATE);
8123df24e667f04a7003342b534310919abc9c87418Dan Gohman    unsigned Reg = getRegForValue(I->getOperand(0));
8133df24e667f04a7003342b534310919abc9c87418Dan Gohman    if (Reg == 0) return false;
8143df24e667f04a7003342b534310919abc9c87418Dan Gohman    UpdateValueMap(I, Reg);
8153df24e667f04a7003342b534310919abc9c87418Dan Gohman    return true;
8163df24e667f04a7003342b534310919abc9c87418Dan Gohman  }
817d57dd5f4e6740520820bc0fca42a540e31c27a73Dan Gohman
818ba5be5c07bb19dcf484e3aa40cd139dd07c10407Dan Gohman  case Instruction::PHI:
819ba5be5c07bb19dcf484e3aa40cd139dd07c10407Dan Gohman    llvm_unreachable("FastISel shouldn't visit PHI nodes!");
820ba5be5c07bb19dcf484e3aa40cd139dd07c10407Dan Gohman
8213df24e667f04a7003342b534310919abc9c87418Dan Gohman  default:
8223df24e667f04a7003342b534310919abc9c87418Dan Gohman    // Unhandled instruction. Halt "fast" selection and bail.
8233df24e667f04a7003342b534310919abc9c87418Dan Gohman    return false;
8243df24e667f04a7003342b534310919abc9c87418Dan Gohman  }
825b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
826b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
8273df24e667f04a7003342b534310919abc9c87418Dan GohmanFastISel::FastISel(MachineFunction &mf,
8283df24e667f04a7003342b534310919abc9c87418Dan Gohman                   DenseMap<const Value *, unsigned> &vm,
8290586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman                   DenseMap<const BasicBlock *, MachineBasicBlock *> &bm,
830f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman                   DenseMap<const AllocaInst *, int> &am,
831f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman                   std::vector<std::pair<MachineInstr*, unsigned> > &pn
832dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#ifndef NDEBUG
8332520864773dcb73d76d297605f4bc41c0cf3fa39Dan Gohman                   , SmallSet<const Instruction *, 8> &cil
834dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#endif
835dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman                   )
8363df24e667f04a7003342b534310919abc9c87418Dan Gohman  : MBB(0),
8373df24e667f04a7003342b534310919abc9c87418Dan Gohman    ValueMap(vm),
8383df24e667f04a7003342b534310919abc9c87418Dan Gohman    MBBMap(bm),
8390586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    StaticAllocaMap(am),
840f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    PHINodesToUpdate(pn),
841dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#ifndef NDEBUG
842dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman    CatchInfoLost(cil),
843dd5b58ad7be78be90390074f0df138778af5c895Dan Gohman#endif
8443df24e667f04a7003342b534310919abc9c87418Dan Gohman    MF(mf),
8453df24e667f04a7003342b534310919abc9c87418Dan Gohman    MRI(MF.getRegInfo()),
8460586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    MFI(*MF.getFrameInfo()),
8470586d91bb3e516d5826826522d9a90ed6ef74d86Dan Gohman    MCP(*MF.getConstantPool()),
8483df24e667f04a7003342b534310919abc9c87418Dan Gohman    TM(MF.getTarget()),
84922bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TD(*TM.getTargetData()),
85022bb31103de3337f0bb74c7bee16d1817d4dca14Dan Gohman    TII(*TM.getInstrInfo()),
851a7a0ed79012ea36f838239cf1d04959711aec2a9Dan Gohman    TLI(*TM.getTargetLowering()),
852a7a0ed79012ea36f838239cf1d04959711aec2a9Dan Gohman    IsBottomUp(false) {
853bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman}
854bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman
855e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan GohmanFastISel::~FastISel() {}
856e285a74f7cf9dd3ccf4fe758576cf83301f8a43eDan Gohman
857825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Andersonunsigned FastISel::FastEmit_(MVT, MVT,
8587c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                             unsigned) {
859b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
860b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
861b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
862825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Andersonunsigned FastISel::FastEmit_r(MVT, MVT,
863a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                              unsigned,
864a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                              unsigned /*Op0*/, bool /*Op0IsKill*/) {
865b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
866b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
867b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
868825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Andersonunsigned FastISel::FastEmit_rr(MVT, MVT,
869a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned,
870a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned /*Op0*/, bool /*Op0IsKill*/,
871a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned /*Op1*/, bool /*Op1IsKill*/) {
872b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return 0;
873b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
874b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
8757c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohmanunsigned FastISel::FastEmit_i(MVT, MVT, unsigned, uint64_t /*Imm*/) {
87683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return 0;
87783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
87883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
879825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Andersonunsigned FastISel::FastEmit_f(MVT, MVT,
88046510a73e977273ec67747eb34cbdb43f815e451Dan Gohman                              unsigned, const ConstantFP * /*FPImm*/) {
88110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  return 0;
88210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman}
88310df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
884825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Andersonunsigned FastISel::FastEmit_ri(MVT, MVT,
885a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned,
886a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned /*Op0*/, bool /*Op0IsKill*/,
8870f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson                               uint64_t /*Imm*/) {
888d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return 0;
889d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
890d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
891825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Andersonunsigned FastISel::FastEmit_rf(MVT, MVT,
892a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned,
893a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                               unsigned /*Op0*/, bool /*Op0IsKill*/,
89446510a73e977273ec67747eb34cbdb43f815e451Dan Gohman                               const ConstantFP * /*FPImm*/) {
89510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  return 0;
89610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman}
89710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
898825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Andersonunsigned FastISel::FastEmit_rri(MVT, MVT,
8997c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohman                                unsigned,
900a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                unsigned /*Op0*/, bool /*Op0IsKill*/,
901a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                unsigned /*Op1*/, bool /*Op1IsKill*/,
902d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                uint64_t /*Imm*/) {
90383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  return 0;
90483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
90583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
90683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_ri_ - This method is a wrapper of FastEmit_ri. It first tries
90783785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// to emit an instruction with an immediate operand using FastEmit_ri.
90883785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// If that fails, it materializes the immediate into a register and try
90983785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng/// FastEmit_rr instead.
9107c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohmanunsigned FastISel::FastEmit_ri_(MVT VT, unsigned Opcode,
911a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                unsigned Op0, bool Op0IsKill,
912a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                uint64_t Imm, MVT ImmType) {
91383785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  // First check if immediate type is legal. If not, we can't use the ri form.
914a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  unsigned ResultReg = FastEmit_ri(VT, VT, Opcode, Op0, Op0IsKill, Imm);
91583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng  if (ResultReg != 0)
91683785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng    return ResultReg;
9170f84e4e31009eecf2dfcbe6113b65d0919f30254Owen Anderson  unsigned MaterialReg = FastEmit_i(ImmType, ImmType, ISD::Constant, Imm);
918d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  if (MaterialReg == 0)
919d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman    return 0;
920a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  return FastEmit_rr(VT, VT, Opcode,
921a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                     Op0, Op0IsKill,
922a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                     MaterialReg, /*Kill=*/true);
923d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
924d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
92510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman/// FastEmit_rf_ - This method is a wrapper of FastEmit_ri. It first tries
92610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman/// to emit an instruction with a floating-point immediate operand using
92710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman/// FastEmit_rf. If that fails, it materializes the immediate into a register
92810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman/// and try FastEmit_rr instead.
9297c3ecb6838ef7a2ca306c0f3cd68022f0855ae71Dan Gohmanunsigned FastISel::FastEmit_rf_(MVT VT, unsigned Opcode,
930a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                unsigned Op0, bool Op0IsKill,
931a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                const ConstantFP *FPImm, MVT ImmType) {
93210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  // First check if immediate type is legal. If not, we can't use the rf form.
933a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  unsigned ResultReg = FastEmit_rf(VT, VT, Opcode, Op0, Op0IsKill, FPImm);
93410df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  if (ResultReg != 0)
93510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    return ResultReg;
93610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
93710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  // Materialize the constant in a register.
93810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  unsigned MaterialReg = FastEmit_f(ImmType, ImmType, ISD::ConstantFP, FPImm);
93910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  if (MaterialReg == 0) {
94096a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // If the target doesn't have a way to directly enter a floating-point
94196a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // value into a register, use an alternate approach.
94296a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // TODO: The current approach only supports floating-point constants
94396a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // that can be constructed by conversion from integer values. This should
94496a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // be replaced by code that creates a load from a constant-pool entry,
94596a9999d79345fa7bc7e2f2a3f28edef4c69e6b5Dan Gohman    // which will require some target-specific work.
94610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    const APFloat &Flt = FPImm->getValueAPF();
947e50ed30282bb5b4a9ed952580523f2dda16215acOwen Anderson    EVT IntVT = TLI.getPointerTy();
94810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
94910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    uint64_t x[2];
95010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    uint32_t IntBitWidth = IntVT.getSizeInBits();
95123a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen    bool isExact;
95223a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen    (void) Flt.convertToInteger(x, IntBitWidth, /*isSigned=*/true,
95323a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen                             APFloat::rmTowardZero, &isExact);
95423a98551ab65eeb8fe5019df8b7db4891582a4bdDale Johannesen    if (!isExact)
95510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman      return 0;
95610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    APInt IntVal(IntBitWidth, 2, x);
95710df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
95810df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    unsigned IntegerReg = FastEmit_i(IntVT.getSimpleVT(), IntVT.getSimpleVT(),
95910df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                                     ISD::Constant, IntVal.getZExtValue());
96010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    if (IntegerReg == 0)
96110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman      return 0;
96210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    MaterialReg = FastEmit_r(IntVT.getSimpleVT(), VT,
963a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                             ISD::SINT_TO_FP, IntegerReg, /*Kill=*/true);
96410df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman    if (MaterialReg == 0)
96510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman      return 0;
96610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  }
967a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  return FastEmit_rr(VT, VT, Opcode,
968a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                     Op0, Op0IsKill,
969a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                     MaterialReg, /*Kill=*/true);
97010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman}
97110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
972d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::createResultReg(const TargetRegisterClass* RC) {
973d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return MRI.createVirtualRegister(RC);
97483785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng}
97583785c80968165b30fcdd111ceb2c28d38bcff86Evan Cheng
976b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_(unsigned MachineInstOpcode,
97777ad79689d755c49146f534107421cb3d9703fedDan Gohman                                 const TargetRegisterClass* RC) {
978d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
979bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
980b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
9819bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling  BuildMI(MBB, DL, II, ResultReg);
982b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
983b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
984b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
985b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_r(unsigned MachineInstOpcode,
986b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                  const TargetRegisterClass *RC,
987a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                  unsigned Op0, bool Op0IsKill) {
988d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
989bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
990b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
9915960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
992a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II, ResultReg).addReg(Op0, Op0IsKill * RegState::Kill);
9935960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
994a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II).addReg(Op0, Op0IsKill * RegState::Kill);
9955960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
99634dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                         II.ImplicitDefs[0], RC, RC, DL);
9975960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
9985960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
9995960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
10005960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng
1001b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
1002b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
1003b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
1004b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohmanunsigned FastISel::FastEmitInst_rr(unsigned MachineInstOpcode,
1005b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman                                   const TargetRegisterClass *RC,
1006a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   unsigned Op0, bool Op0IsKill,
1007a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   unsigned Op1, bool Op1IsKill) {
1008d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
1009bb466331e7e50d03497ce40ee344870236fd9c32Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1010b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman
10115960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
1012a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II, ResultReg)
1013a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1014a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op1, Op1IsKill * RegState::Kill);
10155960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
1016a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II)
1017a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1018a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op1, Op1IsKill * RegState::Kill);
10195960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
102034dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                         II.ImplicitDefs[0], RC, RC, DL);
10215960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
10225960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
10235960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
1024b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman  return ResultReg;
1025b0cf29c5cfff797284b3660dc233e135feb65d9aDan Gohman}
1026d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
1027d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmitInst_ri(unsigned MachineInstOpcode,
1028d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                   const TargetRegisterClass *RC,
1029a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   unsigned Op0, bool Op0IsKill,
1030a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   uint64_t Imm) {
1031d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
1032d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1033d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
10345960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
1035a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II, ResultReg)
1036a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1037a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addImm(Imm);
10385960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
1039a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II)
1040a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1041a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addImm(Imm);
10425960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
104334dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                         II.ImplicitDefs[0], RC, RC, DL);
10445960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
10455960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
10465960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
1047d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return ResultReg;
1048d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
1049d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
105010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohmanunsigned FastISel::FastEmitInst_rf(unsigned MachineInstOpcode,
105110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman                                   const TargetRegisterClass *RC,
1052a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   unsigned Op0, bool Op0IsKill,
1053a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                   const ConstantFP *FPImm) {
105410df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  unsigned ResultReg = createResultReg(RC);
105510df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
105610df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
10575960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
1058a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II, ResultReg)
1059a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1060a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addFPImm(FPImm);
10615960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
1062a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II)
1063a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1064a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addFPImm(FPImm);
10655960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
106634dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                         II.ImplicitDefs[0], RC, RC, DL);
10675960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
10685960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
10695960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
107010df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman  return ResultReg;
107110df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman}
107210df0fa73e396bbc93a8940e8b53827390c54d10Dan Gohman
1073d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohmanunsigned FastISel::FastEmitInst_rri(unsigned MachineInstOpcode,
1074d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman                                    const TargetRegisterClass *RC,
1075a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                    unsigned Op0, bool Op0IsKill,
1076a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                    unsigned Op1, bool Op1IsKill,
1077a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                    uint64_t Imm) {
1078d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  unsigned ResultReg = createResultReg(RC);
1079d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
1080d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman
10815960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
1082a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II, ResultReg)
1083a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1084a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op1, Op1IsKill * RegState::Kill)
1085a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addImm(Imm);
10865960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
1087a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II)
1088a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1089a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op1, Op1IsKill * RegState::Kill)
1090a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addImm(Imm);
10915960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
109234dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                         II.ImplicitDefs[0], RC, RC, DL);
10935960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
10945960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
10955960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
1096d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman  return ResultReg;
1097d5fe57d2f980c6bd1a61450f99c254a76d0f1683Dan Gohman}
10986d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson
10996d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Andersonunsigned FastISel::FastEmitInst_i(unsigned MachineInstOpcode,
11006d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                                  const TargetRegisterClass *RC,
11016d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson                                  uint64_t Imm) {
11026d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  unsigned ResultReg = createResultReg(RC);
11036d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  const TargetInstrDesc &II = TII.get(MachineInstOpcode);
11046d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson
11055960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
11069bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II, ResultReg).addImm(Imm);
11075960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
11089bc96a57206cbebaa9b0ba9979f949eb10c1592cBill Wendling    BuildMI(MBB, DL, II).addImm(Imm);
11095960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
111034dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                         II.ImplicitDefs[0], RC, RC, DL);
11115960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
11125960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
11135960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
11146d0c25ec3a7ca822e68f73a4481eee43eb5c9485Owen Anderson  return ResultReg;
1115b41aec54767a825ac54c8822e787700bb08a3460Evan Cheng}
11168970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson
1117825b72b0571821bf2d378749f69d6c4cfb52d2f9Owen Andersonunsigned FastISel::FastEmitInst_extractsubreg(MVT RetVT,
1118a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                              unsigned Op0, bool Op0IsKill,
1119a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman                                              uint32_t Idx) {
112040a468f24909792f000e3ccc1dda7a27b9c34b69Owen Anderson  const TargetRegisterClass* RC = MRI.getRegClass(Op0);
11218970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson
1122536ab130ec95cbb7bf30530251dafa7dfecc8471Evan Cheng  unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
1123518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner  const TargetInstrDesc &II = TII.get(TargetOpcode::EXTRACT_SUBREG);
11248970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson
11255960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  if (II.getNumDefs() >= 1)
1126a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II, ResultReg)
1127a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1128a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addImm(Idx);
11295960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  else {
1130a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman    BuildMI(MBB, DL, II)
1131a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addReg(Op0, Op0IsKill * RegState::Kill)
1132a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman      .addImm(Idx);
11335960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    bool InsertedCopy = TII.copyRegToReg(*MBB, MBB->end(), ResultReg,
113434dcc6fadca0a1117cdbd0e9b35c991a55b6e556Dan Gohman                                         II.ImplicitDefs[0], RC, RC, DL);
11355960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng    if (!InsertedCopy)
11365960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng      ResultReg = 0;
11375960e4eb68be6b3bca6369f6a95d7de5ed8a9eadEvan Cheng  }
11388970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson  return ResultReg;
11398970f00deff00ffce1f35cf00883357e1582daa1Owen Anderson}
114014ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman
114114ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman/// FastEmitZExtFromI1 - Emit MachineInstrs to compute the value of Op
114214ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman/// with all but the least significant bit set to zero.
1143a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohmanunsigned FastISel::FastEmitZExtFromI1(MVT VT, unsigned Op0, bool Op0IsKill) {
1144a6cb641f48df20f6f79018569b519e5a32e897a2Dan Gohman  return FastEmit_ri(VT, VT, ISD::AND, Op0, Op0IsKill, 1);
114514ea1ec2324cb595f2e035bbf54ddcd483f17c11Dan Gohman}
1146f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1147f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman/// HandlePHINodesInSuccessorBlocks - Handle PHI nodes in successor blocks.
1148f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman/// Emit code to ensure constants are copied into registers when needed.
1149f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman/// Remember the virtual registers that need to be added to the Machine PHI
1150f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman/// nodes as input.  We cannot just directly add them, because expansion
1151f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman/// might result in multiple MBB's for one BB.  As such, the start of the
1152f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman/// BB might correspond to a different MBB than the end.
1153f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohmanbool FastISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
1154f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman  const TerminatorInst *TI = LLVMBB->getTerminator();
1155f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1156f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman  SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
1157f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman  unsigned OrigNumPHINodesToUpdate = PHINodesToUpdate.size();
1158f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1159f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman  // Check successor nodes' PHI nodes that expect a constant to be available
1160f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman  // from this block.
1161f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman  for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
1162f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    const BasicBlock *SuccBB = TI->getSuccessor(succ);
1163f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    if (!isa<PHINode>(SuccBB->begin())) continue;
1164f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    MachineBasicBlock *SuccMBB = MBBMap[SuccBB];
1165f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1166f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    // If this terminator has multiple identical successors (common for
1167f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    // switches), only handle each succ once.
1168f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    if (!SuccsHandled.insert(SuccMBB)) continue;
1169f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1170f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    MachineBasicBlock::iterator MBBI = SuccMBB->begin();
1171f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1172f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    // At this point we know that there is a 1-1 correspondence between LLVM PHI
1173f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    // nodes and Machine PHI nodes, but the incoming operands have not been
1174f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    // emitted yet.
1175f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    for (BasicBlock::const_iterator I = SuccBB->begin();
1176f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman         const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
1177fb95f89e8ebd0b71408472e2544e2b8d6d4738e5Dan Gohman
1178f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      // Ignore dead phi's.
1179f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      if (PN->use_empty()) continue;
1180f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1181f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      // Only handle legal types. Two interesting things to note here. First,
1182f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      // by bailing out early, we may leave behind some dead instructions,
1183f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
1184f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      // own moves. Second, this check is necessary becuase FastISel doesn't
1185f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      // use CreateRegForValue to create registers, so it always creates
1186f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      // exactly one register for each non-void instruction.
1187f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
1188f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
1189f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman        // Promote MVT::i1.
1190f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman        if (VT == MVT::i1)
1191f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman          VT = TLI.getTypeToTransformTo(LLVMBB->getContext(), VT);
1192f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman        else {
1193f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman          PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1194f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman          return false;
1195f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman        }
1196f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      }
1197f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1198f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
1199f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1200fb95f89e8ebd0b71408472e2544e2b8d6d4738e5Dan Gohman      // Set the DebugLoc for the copy. Prefer the location of the operand
1201fb95f89e8ebd0b71408472e2544e2b8d6d4738e5Dan Gohman      // if there is one; use the location of the PHI otherwise.
1202fb95f89e8ebd0b71408472e2544e2b8d6d4738e5Dan Gohman      DL = PN->getDebugLoc();
1203fb95f89e8ebd0b71408472e2544e2b8d6d4738e5Dan Gohman      if (const Instruction *Inst = dyn_cast<Instruction>(PHIOp))
1204fb95f89e8ebd0b71408472e2544e2b8d6d4738e5Dan Gohman        DL = Inst->getDebugLoc();
1205fb95f89e8ebd0b71408472e2544e2b8d6d4738e5Dan Gohman
1206f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      unsigned Reg = getRegForValue(PHIOp);
1207f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      if (Reg == 0) {
1208f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman        PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
1209f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman        return false;
1210f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      }
1211f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman      PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
1212fb95f89e8ebd0b71408472e2544e2b8d6d4738e5Dan Gohman      DL = DebugLoc();
1213f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman    }
1214f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman  }
1215f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman
1216f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman  return true;
1217f81eca0ab908fdcf98ae0efaa75acccc8ba40dc2Dan Gohman}
1218